dcache.c 72 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099210021012102210321042105210621072108210921102111211221132114211521162117211821192120212121222123212421252126212721282129213021312132213321342135213621372138213921402141214221432144214521462147214821492150215121522153215421552156215721582159216021612162216321642165216621672168216921702171217221732174217521762177217821792180218121822183218421852186218721882189219021912192219321942195219621972198219922002201220222032204220522062207220822092210221122122213221422152216221722182219222022212222222322242225222622272228222922302231223222332234223522362237223822392240224122422243224422452246224722482249225022512252225322542255225622572258225922602261226222632264226522662267226822692270227122722273227422752276227722782279228022812282228322842285228622872288228922902291229222932294229522962297229822992300230123022303230423052306230723082309231023112312231323142315231623172318231923202321232223232324232523262327232823292330233123322333233423352336233723382339234023412342234323442345234623472348234923502351235223532354235523562357235823592360236123622363236423652366236723682369237023712372237323742375237623772378237923802381238223832384238523862387238823892390239123922393239423952396239723982399240024012402240324042405240624072408240924102411241224132414241524162417241824192420242124222423242424252426242724282429243024312432243324342435243624372438243924402441244224432444244524462447244824492450245124522453245424552456245724582459246024612462246324642465246624672468246924702471247224732474247524762477247824792480248124822483248424852486248724882489249024912492249324942495249624972498249925002501250225032504250525062507250825092510251125122513251425152516251725182519252025212522252325242525252625272528252925302531253225332534253525362537253825392540254125422543254425452546254725482549255025512552255325542555255625572558255925602561256225632564256525662567256825692570257125722573257425752576257725782579258025812582258325842585258625872588258925902591259225932594259525962597259825992600260126022603260426052606260726082609261026112612261326142615261626172618261926202621262226232624262526262627262826292630263126322633263426352636263726382639264026412642264326442645264626472648264926502651265226532654265526562657265826592660266126622663266426652666266726682669267026712672267326742675267626772678267926802681268226832684268526862687268826892690269126922693269426952696269726982699270027012702270327042705270627072708270927102711271227132714271527162717271827192720272127222723272427252726272727282729273027312732273327342735273627372738273927402741274227432744274527462747274827492750275127522753275427552756275727582759276027612762276327642765276627672768276927702771277227732774277527762777277827792780278127822783278427852786278727882789279027912792279327942795279627972798279928002801280228032804280528062807280828092810281128122813281428152816281728182819282028212822282328242825282628272828282928302831283228332834283528362837283828392840284128422843284428452846284728482849285028512852285328542855285628572858285928602861286228632864286528662867286828692870
  1. /*
  2. * fs/dcache.c
  3. *
  4. * Complete reimplementation
  5. * (C) 1997 Thomas Schoebel-Theuer,
  6. * with heavy changes by Linus Torvalds
  7. */
  8. /*
  9. * Notes on the allocation strategy:
  10. *
  11. * The dcache is a master of the icache - whenever a dcache entry
  12. * exists, the inode will always exist. "iput()" is done either when
  13. * the dcache entry is deleted or garbage collected.
  14. */
  15. #include <linux/syscalls.h>
  16. #include <linux/string.h>
  17. #include <linux/mm.h>
  18. #include <linux/fs.h>
  19. #include <linux/fsnotify.h>
  20. #include <linux/slab.h>
  21. #include <linux/init.h>
  22. #include <linux/hash.h>
  23. #include <linux/cache.h>
  24. #include <linux/module.h>
  25. #include <linux/mount.h>
  26. #include <linux/file.h>
  27. #include <asm/uaccess.h>
  28. #include <linux/security.h>
  29. #include <linux/seqlock.h>
  30. #include <linux/swap.h>
  31. #include <linux/bootmem.h>
  32. #include <linux/fs_struct.h>
  33. #include <linux/hardirq.h>
  34. #include "internal.h"
  35. /*
  36. * Usage:
  37. * dcache_inode_lock protects:
  38. * - i_dentry, d_alias, d_inode
  39. * dcache_hash_lock protects:
  40. * - the dcache hash table, s_anon lists
  41. * dcache_lru_lock protects:
  42. * - the dcache lru lists and counters
  43. * d_lock protects:
  44. * - d_flags
  45. * - d_name
  46. * - d_lru
  47. * - d_count
  48. * - d_unhashed()
  49. * - d_parent and d_subdirs
  50. * - childrens' d_child and d_parent
  51. * - d_alias, d_inode
  52. *
  53. * Ordering:
  54. * dcache_inode_lock
  55. * dentry->d_lock
  56. * dcache_lru_lock
  57. * dcache_hash_lock
  58. *
  59. * If there is an ancestor relationship:
  60. * dentry->d_parent->...->d_parent->d_lock
  61. * ...
  62. * dentry->d_parent->d_lock
  63. * dentry->d_lock
  64. *
  65. * If no ancestor relationship:
  66. * if (dentry1 < dentry2)
  67. * dentry1->d_lock
  68. * dentry2->d_lock
  69. */
  70. int sysctl_vfs_cache_pressure __read_mostly = 100;
  71. EXPORT_SYMBOL_GPL(sysctl_vfs_cache_pressure);
  72. __cacheline_aligned_in_smp DEFINE_SPINLOCK(dcache_inode_lock);
  73. static __cacheline_aligned_in_smp DEFINE_SPINLOCK(dcache_hash_lock);
  74. static __cacheline_aligned_in_smp DEFINE_SPINLOCK(dcache_lru_lock);
  75. __cacheline_aligned_in_smp DEFINE_SEQLOCK(rename_lock);
  76. EXPORT_SYMBOL(rename_lock);
  77. EXPORT_SYMBOL(dcache_inode_lock);
  78. static struct kmem_cache *dentry_cache __read_mostly;
  79. #define DNAME_INLINE_LEN (sizeof(struct dentry)-offsetof(struct dentry,d_iname))
  80. /*
  81. * This is the single most critical data structure when it comes
  82. * to the dcache: the hashtable for lookups. Somebody should try
  83. * to make this good - I've just made it work.
  84. *
  85. * This hash-function tries to avoid losing too many bits of hash
  86. * information, yet avoid using a prime hash-size or similar.
  87. */
  88. #define D_HASHBITS d_hash_shift
  89. #define D_HASHMASK d_hash_mask
  90. static unsigned int d_hash_mask __read_mostly;
  91. static unsigned int d_hash_shift __read_mostly;
  92. static struct hlist_head *dentry_hashtable __read_mostly;
  93. /* Statistics gathering. */
  94. struct dentry_stat_t dentry_stat = {
  95. .age_limit = 45,
  96. };
  97. static DEFINE_PER_CPU(unsigned int, nr_dentry);
  98. #if defined(CONFIG_SYSCTL) && defined(CONFIG_PROC_FS)
  99. static int get_nr_dentry(void)
  100. {
  101. int i;
  102. int sum = 0;
  103. for_each_possible_cpu(i)
  104. sum += per_cpu(nr_dentry, i);
  105. return sum < 0 ? 0 : sum;
  106. }
  107. int proc_nr_dentry(ctl_table *table, int write, void __user *buffer,
  108. size_t *lenp, loff_t *ppos)
  109. {
  110. dentry_stat.nr_dentry = get_nr_dentry();
  111. return proc_dointvec(table, write, buffer, lenp, ppos);
  112. }
  113. #endif
  114. static void __d_free(struct rcu_head *head)
  115. {
  116. struct dentry *dentry = container_of(head, struct dentry, d_u.d_rcu);
  117. WARN_ON(!list_empty(&dentry->d_alias));
  118. if (dname_external(dentry))
  119. kfree(dentry->d_name.name);
  120. kmem_cache_free(dentry_cache, dentry);
  121. }
  122. /*
  123. * no locks, please.
  124. */
  125. static void d_free(struct dentry *dentry)
  126. {
  127. BUG_ON(dentry->d_count);
  128. this_cpu_dec(nr_dentry);
  129. if (dentry->d_op && dentry->d_op->d_release)
  130. dentry->d_op->d_release(dentry);
  131. /* if dentry was never inserted into hash, immediate free is OK */
  132. if (hlist_unhashed(&dentry->d_hash))
  133. __d_free(&dentry->d_u.d_rcu);
  134. else
  135. call_rcu(&dentry->d_u.d_rcu, __d_free);
  136. }
  137. /*
  138. * Release the dentry's inode, using the filesystem
  139. * d_iput() operation if defined.
  140. */
  141. static void dentry_iput(struct dentry * dentry)
  142. __releases(dentry->d_lock)
  143. __releases(dcache_inode_lock)
  144. {
  145. struct inode *inode = dentry->d_inode;
  146. if (inode) {
  147. dentry->d_inode = NULL;
  148. list_del_init(&dentry->d_alias);
  149. spin_unlock(&dentry->d_lock);
  150. spin_unlock(&dcache_inode_lock);
  151. if (!inode->i_nlink)
  152. fsnotify_inoderemove(inode);
  153. if (dentry->d_op && dentry->d_op->d_iput)
  154. dentry->d_op->d_iput(dentry, inode);
  155. else
  156. iput(inode);
  157. } else {
  158. spin_unlock(&dentry->d_lock);
  159. spin_unlock(&dcache_inode_lock);
  160. }
  161. }
  162. /*
  163. * dentry_lru_(add|del|move_tail) must be called with d_lock held.
  164. */
  165. static void dentry_lru_add(struct dentry *dentry)
  166. {
  167. if (list_empty(&dentry->d_lru)) {
  168. spin_lock(&dcache_lru_lock);
  169. list_add(&dentry->d_lru, &dentry->d_sb->s_dentry_lru);
  170. dentry->d_sb->s_nr_dentry_unused++;
  171. dentry_stat.nr_unused++;
  172. spin_unlock(&dcache_lru_lock);
  173. }
  174. }
  175. static void __dentry_lru_del(struct dentry *dentry)
  176. {
  177. list_del_init(&dentry->d_lru);
  178. dentry->d_sb->s_nr_dentry_unused--;
  179. dentry_stat.nr_unused--;
  180. }
  181. static void dentry_lru_del(struct dentry *dentry)
  182. {
  183. if (!list_empty(&dentry->d_lru)) {
  184. spin_lock(&dcache_lru_lock);
  185. __dentry_lru_del(dentry);
  186. spin_unlock(&dcache_lru_lock);
  187. }
  188. }
  189. static void dentry_lru_move_tail(struct dentry *dentry)
  190. {
  191. spin_lock(&dcache_lru_lock);
  192. if (list_empty(&dentry->d_lru)) {
  193. list_add_tail(&dentry->d_lru, &dentry->d_sb->s_dentry_lru);
  194. dentry->d_sb->s_nr_dentry_unused++;
  195. dentry_stat.nr_unused++;
  196. } else {
  197. list_move_tail(&dentry->d_lru, &dentry->d_sb->s_dentry_lru);
  198. }
  199. spin_unlock(&dcache_lru_lock);
  200. }
  201. /**
  202. * d_kill - kill dentry and return parent
  203. * @dentry: dentry to kill
  204. *
  205. * The dentry must already be unhashed and removed from the LRU.
  206. *
  207. * If this is the root of the dentry tree, return NULL.
  208. *
  209. * dentry->d_lock and parent->d_lock must be held by caller, and are dropped by
  210. * d_kill.
  211. */
  212. static struct dentry *d_kill(struct dentry *dentry, struct dentry *parent)
  213. __releases(dentry->d_lock)
  214. __releases(parent->d_lock)
  215. __releases(dcache_inode_lock)
  216. {
  217. dentry->d_parent = NULL;
  218. list_del(&dentry->d_u.d_child);
  219. if (parent)
  220. spin_unlock(&parent->d_lock);
  221. dentry_iput(dentry);
  222. /*
  223. * dentry_iput drops the locks, at which point nobody (except
  224. * transient RCU lookups) can reach this dentry.
  225. */
  226. d_free(dentry);
  227. return parent;
  228. }
  229. /**
  230. * d_drop - drop a dentry
  231. * @dentry: dentry to drop
  232. *
  233. * d_drop() unhashes the entry from the parent dentry hashes, so that it won't
  234. * be found through a VFS lookup any more. Note that this is different from
  235. * deleting the dentry - d_delete will try to mark the dentry negative if
  236. * possible, giving a successful _negative_ lookup, while d_drop will
  237. * just make the cache lookup fail.
  238. *
  239. * d_drop() is used mainly for stuff that wants to invalidate a dentry for some
  240. * reason (NFS timeouts or autofs deletes).
  241. *
  242. * __d_drop requires dentry->d_lock.
  243. */
  244. void __d_drop(struct dentry *dentry)
  245. {
  246. if (!(dentry->d_flags & DCACHE_UNHASHED)) {
  247. dentry->d_flags |= DCACHE_UNHASHED;
  248. spin_lock(&dcache_hash_lock);
  249. hlist_del_rcu(&dentry->d_hash);
  250. spin_unlock(&dcache_hash_lock);
  251. }
  252. }
  253. EXPORT_SYMBOL(__d_drop);
  254. void d_drop(struct dentry *dentry)
  255. {
  256. spin_lock(&dentry->d_lock);
  257. __d_drop(dentry);
  258. spin_unlock(&dentry->d_lock);
  259. }
  260. EXPORT_SYMBOL(d_drop);
  261. /*
  262. * This is dput
  263. *
  264. * This is complicated by the fact that we do not want to put
  265. * dentries that are no longer on any hash chain on the unused
  266. * list: we'd much rather just get rid of them immediately.
  267. *
  268. * However, that implies that we have to traverse the dentry
  269. * tree upwards to the parents which might _also_ now be
  270. * scheduled for deletion (it may have been only waiting for
  271. * its last child to go away).
  272. *
  273. * This tail recursion is done by hand as we don't want to depend
  274. * on the compiler to always get this right (gcc generally doesn't).
  275. * Real recursion would eat up our stack space.
  276. */
  277. /*
  278. * dput - release a dentry
  279. * @dentry: dentry to release
  280. *
  281. * Release a dentry. This will drop the usage count and if appropriate
  282. * call the dentry unlink method as well as removing it from the queues and
  283. * releasing its resources. If the parent dentries were scheduled for release
  284. * they too may now get deleted.
  285. *
  286. * no dcache lock, please.
  287. */
  288. void dput(struct dentry *dentry)
  289. {
  290. struct dentry *parent;
  291. if (!dentry)
  292. return;
  293. repeat:
  294. if (dentry->d_count == 1)
  295. might_sleep();
  296. spin_lock(&dentry->d_lock);
  297. BUG_ON(!dentry->d_count);
  298. if (dentry->d_count > 1) {
  299. dentry->d_count--;
  300. spin_unlock(&dentry->d_lock);
  301. return;
  302. }
  303. if (dentry->d_op && dentry->d_op->d_delete) {
  304. if (dentry->d_op->d_delete(dentry))
  305. goto kill_it;
  306. }
  307. /* Unreachable? Get rid of it */
  308. if (d_unhashed(dentry))
  309. goto kill_it;
  310. /* Otherwise leave it cached and ensure it's on the LRU */
  311. dentry->d_flags |= DCACHE_REFERENCED;
  312. dentry_lru_add(dentry);
  313. dentry->d_count--;
  314. spin_unlock(&dentry->d_lock);
  315. return;
  316. kill_it:
  317. if (!spin_trylock(&dcache_inode_lock)) {
  318. relock:
  319. spin_unlock(&dentry->d_lock);
  320. cpu_relax();
  321. goto repeat;
  322. }
  323. if (IS_ROOT(dentry))
  324. parent = NULL;
  325. else
  326. parent = dentry->d_parent;
  327. if (parent && !spin_trylock(&parent->d_lock)) {
  328. spin_unlock(&dcache_inode_lock);
  329. goto relock;
  330. }
  331. dentry->d_count--;
  332. /* if dentry was on the d_lru list delete it from there */
  333. dentry_lru_del(dentry);
  334. /* if it was on the hash (d_delete case), then remove it */
  335. __d_drop(dentry);
  336. dentry = d_kill(dentry, parent);
  337. if (dentry)
  338. goto repeat;
  339. }
  340. EXPORT_SYMBOL(dput);
  341. /**
  342. * d_invalidate - invalidate a dentry
  343. * @dentry: dentry to invalidate
  344. *
  345. * Try to invalidate the dentry if it turns out to be
  346. * possible. If there are other dentries that can be
  347. * reached through this one we can't delete it and we
  348. * return -EBUSY. On success we return 0.
  349. *
  350. * no dcache lock.
  351. */
  352. int d_invalidate(struct dentry * dentry)
  353. {
  354. /*
  355. * If it's already been dropped, return OK.
  356. */
  357. spin_lock(&dentry->d_lock);
  358. if (d_unhashed(dentry)) {
  359. spin_unlock(&dentry->d_lock);
  360. return 0;
  361. }
  362. /*
  363. * Check whether to do a partial shrink_dcache
  364. * to get rid of unused child entries.
  365. */
  366. if (!list_empty(&dentry->d_subdirs)) {
  367. spin_unlock(&dentry->d_lock);
  368. shrink_dcache_parent(dentry);
  369. spin_lock(&dentry->d_lock);
  370. }
  371. /*
  372. * Somebody else still using it?
  373. *
  374. * If it's a directory, we can't drop it
  375. * for fear of somebody re-populating it
  376. * with children (even though dropping it
  377. * would make it unreachable from the root,
  378. * we might still populate it if it was a
  379. * working directory or similar).
  380. */
  381. if (dentry->d_count > 1) {
  382. if (dentry->d_inode && S_ISDIR(dentry->d_inode->i_mode)) {
  383. spin_unlock(&dentry->d_lock);
  384. return -EBUSY;
  385. }
  386. }
  387. __d_drop(dentry);
  388. spin_unlock(&dentry->d_lock);
  389. return 0;
  390. }
  391. EXPORT_SYMBOL(d_invalidate);
  392. /* This must be called with d_lock held */
  393. static inline struct dentry * __dget_locked_dlock(struct dentry *dentry)
  394. {
  395. dentry->d_count++;
  396. dentry_lru_del(dentry);
  397. return dentry;
  398. }
  399. /* This must be called with d_lock held */
  400. static inline struct dentry * __dget_locked(struct dentry *dentry)
  401. {
  402. spin_lock(&dentry->d_lock);
  403. __dget_locked_dlock(dentry);
  404. spin_unlock(&dentry->d_lock);
  405. return dentry;
  406. }
  407. struct dentry * dget_locked_dlock(struct dentry *dentry)
  408. {
  409. return __dget_locked_dlock(dentry);
  410. }
  411. struct dentry * dget_locked(struct dentry *dentry)
  412. {
  413. return __dget_locked(dentry);
  414. }
  415. EXPORT_SYMBOL(dget_locked);
  416. struct dentry *dget_parent(struct dentry *dentry)
  417. {
  418. struct dentry *ret;
  419. repeat:
  420. spin_lock(&dentry->d_lock);
  421. ret = dentry->d_parent;
  422. if (!ret)
  423. goto out;
  424. if (dentry == ret) {
  425. ret->d_count++;
  426. goto out;
  427. }
  428. if (!spin_trylock(&ret->d_lock)) {
  429. spin_unlock(&dentry->d_lock);
  430. cpu_relax();
  431. goto repeat;
  432. }
  433. BUG_ON(!ret->d_count);
  434. ret->d_count++;
  435. spin_unlock(&ret->d_lock);
  436. out:
  437. spin_unlock(&dentry->d_lock);
  438. return ret;
  439. }
  440. EXPORT_SYMBOL(dget_parent);
  441. /**
  442. * d_find_alias - grab a hashed alias of inode
  443. * @inode: inode in question
  444. * @want_discon: flag, used by d_splice_alias, to request
  445. * that only a DISCONNECTED alias be returned.
  446. *
  447. * If inode has a hashed alias, or is a directory and has any alias,
  448. * acquire the reference to alias and return it. Otherwise return NULL.
  449. * Notice that if inode is a directory there can be only one alias and
  450. * it can be unhashed only if it has no children, or if it is the root
  451. * of a filesystem.
  452. *
  453. * If the inode has an IS_ROOT, DCACHE_DISCONNECTED alias, then prefer
  454. * any other hashed alias over that one unless @want_discon is set,
  455. * in which case only return an IS_ROOT, DCACHE_DISCONNECTED alias.
  456. */
  457. static struct dentry *__d_find_alias(struct inode *inode, int want_discon)
  458. {
  459. struct dentry *alias, *discon_alias;
  460. again:
  461. discon_alias = NULL;
  462. list_for_each_entry(alias, &inode->i_dentry, d_alias) {
  463. spin_lock(&alias->d_lock);
  464. if (S_ISDIR(inode->i_mode) || !d_unhashed(alias)) {
  465. if (IS_ROOT(alias) &&
  466. (alias->d_flags & DCACHE_DISCONNECTED)) {
  467. discon_alias = alias;
  468. } else if (!want_discon) {
  469. __dget_locked_dlock(alias);
  470. spin_unlock(&alias->d_lock);
  471. return alias;
  472. }
  473. }
  474. spin_unlock(&alias->d_lock);
  475. }
  476. if (discon_alias) {
  477. alias = discon_alias;
  478. spin_lock(&alias->d_lock);
  479. if (S_ISDIR(inode->i_mode) || !d_unhashed(alias)) {
  480. if (IS_ROOT(alias) &&
  481. (alias->d_flags & DCACHE_DISCONNECTED)) {
  482. __dget_locked_dlock(alias);
  483. spin_unlock(&alias->d_lock);
  484. return alias;
  485. }
  486. }
  487. spin_unlock(&alias->d_lock);
  488. goto again;
  489. }
  490. return NULL;
  491. }
  492. struct dentry *d_find_alias(struct inode *inode)
  493. {
  494. struct dentry *de = NULL;
  495. if (!list_empty(&inode->i_dentry)) {
  496. spin_lock(&dcache_inode_lock);
  497. de = __d_find_alias(inode, 0);
  498. spin_unlock(&dcache_inode_lock);
  499. }
  500. return de;
  501. }
  502. EXPORT_SYMBOL(d_find_alias);
  503. /*
  504. * Try to kill dentries associated with this inode.
  505. * WARNING: you must own a reference to inode.
  506. */
  507. void d_prune_aliases(struct inode *inode)
  508. {
  509. struct dentry *dentry;
  510. restart:
  511. spin_lock(&dcache_inode_lock);
  512. list_for_each_entry(dentry, &inode->i_dentry, d_alias) {
  513. spin_lock(&dentry->d_lock);
  514. if (!dentry->d_count) {
  515. __dget_locked_dlock(dentry);
  516. __d_drop(dentry);
  517. spin_unlock(&dentry->d_lock);
  518. spin_unlock(&dcache_inode_lock);
  519. dput(dentry);
  520. goto restart;
  521. }
  522. spin_unlock(&dentry->d_lock);
  523. }
  524. spin_unlock(&dcache_inode_lock);
  525. }
  526. EXPORT_SYMBOL(d_prune_aliases);
  527. /*
  528. * Throw away a dentry - free the inode, dput the parent. This requires that
  529. * the LRU list has already been removed.
  530. *
  531. * Try to prune ancestors as well. This is necessary to prevent
  532. * quadratic behavior of shrink_dcache_parent(), but is also expected
  533. * to be beneficial in reducing dentry cache fragmentation.
  534. */
  535. static void prune_one_dentry(struct dentry *dentry, struct dentry *parent)
  536. __releases(dentry->d_lock)
  537. __releases(parent->d_lock)
  538. __releases(dcache_inode_lock)
  539. {
  540. __d_drop(dentry);
  541. dentry = d_kill(dentry, parent);
  542. /*
  543. * Prune ancestors.
  544. */
  545. while (dentry) {
  546. spin_lock(&dcache_inode_lock);
  547. again:
  548. spin_lock(&dentry->d_lock);
  549. if (IS_ROOT(dentry))
  550. parent = NULL;
  551. else
  552. parent = dentry->d_parent;
  553. if (parent && !spin_trylock(&parent->d_lock)) {
  554. spin_unlock(&dentry->d_lock);
  555. goto again;
  556. }
  557. dentry->d_count--;
  558. if (dentry->d_count) {
  559. if (parent)
  560. spin_unlock(&parent->d_lock);
  561. spin_unlock(&dentry->d_lock);
  562. spin_unlock(&dcache_inode_lock);
  563. return;
  564. }
  565. dentry_lru_del(dentry);
  566. __d_drop(dentry);
  567. dentry = d_kill(dentry, parent);
  568. }
  569. }
  570. static void shrink_dentry_list(struct list_head *list)
  571. {
  572. struct dentry *dentry;
  573. while (!list_empty(list)) {
  574. struct dentry *parent;
  575. dentry = list_entry(list->prev, struct dentry, d_lru);
  576. if (!spin_trylock(&dentry->d_lock)) {
  577. relock:
  578. spin_unlock(&dcache_lru_lock);
  579. cpu_relax();
  580. spin_lock(&dcache_lru_lock);
  581. continue;
  582. }
  583. /*
  584. * We found an inuse dentry which was not removed from
  585. * the LRU because of laziness during lookup. Do not free
  586. * it - just keep it off the LRU list.
  587. */
  588. if (dentry->d_count) {
  589. __dentry_lru_del(dentry);
  590. spin_unlock(&dentry->d_lock);
  591. continue;
  592. }
  593. if (IS_ROOT(dentry))
  594. parent = NULL;
  595. else
  596. parent = dentry->d_parent;
  597. if (parent && !spin_trylock(&parent->d_lock)) {
  598. spin_unlock(&dentry->d_lock);
  599. goto relock;
  600. }
  601. __dentry_lru_del(dentry);
  602. spin_unlock(&dcache_lru_lock);
  603. prune_one_dentry(dentry, parent);
  604. /* dcache_inode_lock and dentry->d_lock dropped */
  605. spin_lock(&dcache_inode_lock);
  606. spin_lock(&dcache_lru_lock);
  607. }
  608. }
  609. /**
  610. * __shrink_dcache_sb - shrink the dentry LRU on a given superblock
  611. * @sb: superblock to shrink dentry LRU.
  612. * @count: number of entries to prune
  613. * @flags: flags to control the dentry processing
  614. *
  615. * If flags contains DCACHE_REFERENCED reference dentries will not be pruned.
  616. */
  617. static void __shrink_dcache_sb(struct super_block *sb, int *count, int flags)
  618. {
  619. /* called from prune_dcache() and shrink_dcache_parent() */
  620. struct dentry *dentry;
  621. LIST_HEAD(referenced);
  622. LIST_HEAD(tmp);
  623. int cnt = *count;
  624. spin_lock(&dcache_inode_lock);
  625. relock:
  626. spin_lock(&dcache_lru_lock);
  627. while (!list_empty(&sb->s_dentry_lru)) {
  628. dentry = list_entry(sb->s_dentry_lru.prev,
  629. struct dentry, d_lru);
  630. BUG_ON(dentry->d_sb != sb);
  631. if (!spin_trylock(&dentry->d_lock)) {
  632. spin_unlock(&dcache_lru_lock);
  633. cpu_relax();
  634. goto relock;
  635. }
  636. /*
  637. * If we are honouring the DCACHE_REFERENCED flag and the
  638. * dentry has this flag set, don't free it. Clear the flag
  639. * and put it back on the LRU.
  640. */
  641. if (flags & DCACHE_REFERENCED &&
  642. dentry->d_flags & DCACHE_REFERENCED) {
  643. dentry->d_flags &= ~DCACHE_REFERENCED;
  644. list_move(&dentry->d_lru, &referenced);
  645. spin_unlock(&dentry->d_lock);
  646. } else {
  647. list_move_tail(&dentry->d_lru, &tmp);
  648. spin_unlock(&dentry->d_lock);
  649. if (!--cnt)
  650. break;
  651. }
  652. /* XXX: re-add cond_resched_lock when dcache_lock goes away */
  653. }
  654. *count = cnt;
  655. shrink_dentry_list(&tmp);
  656. if (!list_empty(&referenced))
  657. list_splice(&referenced, &sb->s_dentry_lru);
  658. spin_unlock(&dcache_lru_lock);
  659. spin_unlock(&dcache_inode_lock);
  660. }
  661. /**
  662. * prune_dcache - shrink the dcache
  663. * @count: number of entries to try to free
  664. *
  665. * Shrink the dcache. This is done when we need more memory, or simply when we
  666. * need to unmount something (at which point we need to unuse all dentries).
  667. *
  668. * This function may fail to free any resources if all the dentries are in use.
  669. */
  670. static void prune_dcache(int count)
  671. {
  672. struct super_block *sb, *p = NULL;
  673. int w_count;
  674. int unused = dentry_stat.nr_unused;
  675. int prune_ratio;
  676. int pruned;
  677. if (unused == 0 || count == 0)
  678. return;
  679. if (count >= unused)
  680. prune_ratio = 1;
  681. else
  682. prune_ratio = unused / count;
  683. spin_lock(&sb_lock);
  684. list_for_each_entry(sb, &super_blocks, s_list) {
  685. if (list_empty(&sb->s_instances))
  686. continue;
  687. if (sb->s_nr_dentry_unused == 0)
  688. continue;
  689. sb->s_count++;
  690. /* Now, we reclaim unused dentrins with fairness.
  691. * We reclaim them same percentage from each superblock.
  692. * We calculate number of dentries to scan on this sb
  693. * as follows, but the implementation is arranged to avoid
  694. * overflows:
  695. * number of dentries to scan on this sb =
  696. * count * (number of dentries on this sb /
  697. * number of dentries in the machine)
  698. */
  699. spin_unlock(&sb_lock);
  700. if (prune_ratio != 1)
  701. w_count = (sb->s_nr_dentry_unused / prune_ratio) + 1;
  702. else
  703. w_count = sb->s_nr_dentry_unused;
  704. pruned = w_count;
  705. /*
  706. * We need to be sure this filesystem isn't being unmounted,
  707. * otherwise we could race with generic_shutdown_super(), and
  708. * end up holding a reference to an inode while the filesystem
  709. * is unmounted. So we try to get s_umount, and make sure
  710. * s_root isn't NULL.
  711. */
  712. if (down_read_trylock(&sb->s_umount)) {
  713. if ((sb->s_root != NULL) &&
  714. (!list_empty(&sb->s_dentry_lru))) {
  715. __shrink_dcache_sb(sb, &w_count,
  716. DCACHE_REFERENCED);
  717. pruned -= w_count;
  718. }
  719. up_read(&sb->s_umount);
  720. }
  721. spin_lock(&sb_lock);
  722. if (p)
  723. __put_super(p);
  724. count -= pruned;
  725. p = sb;
  726. /* more work left to do? */
  727. if (count <= 0)
  728. break;
  729. }
  730. if (p)
  731. __put_super(p);
  732. spin_unlock(&sb_lock);
  733. }
  734. /**
  735. * shrink_dcache_sb - shrink dcache for a superblock
  736. * @sb: superblock
  737. *
  738. * Shrink the dcache for the specified super block. This is used to free
  739. * the dcache before unmounting a file system.
  740. */
  741. void shrink_dcache_sb(struct super_block *sb)
  742. {
  743. LIST_HEAD(tmp);
  744. spin_lock(&dcache_inode_lock);
  745. spin_lock(&dcache_lru_lock);
  746. while (!list_empty(&sb->s_dentry_lru)) {
  747. list_splice_init(&sb->s_dentry_lru, &tmp);
  748. shrink_dentry_list(&tmp);
  749. }
  750. spin_unlock(&dcache_lru_lock);
  751. spin_unlock(&dcache_inode_lock);
  752. }
  753. EXPORT_SYMBOL(shrink_dcache_sb);
  754. /*
  755. * destroy a single subtree of dentries for unmount
  756. * - see the comments on shrink_dcache_for_umount() for a description of the
  757. * locking
  758. */
  759. static void shrink_dcache_for_umount_subtree(struct dentry *dentry)
  760. {
  761. struct dentry *parent;
  762. unsigned detached = 0;
  763. BUG_ON(!IS_ROOT(dentry));
  764. /* detach this root from the system */
  765. spin_lock(&dentry->d_lock);
  766. dentry_lru_del(dentry);
  767. __d_drop(dentry);
  768. spin_unlock(&dentry->d_lock);
  769. for (;;) {
  770. /* descend to the first leaf in the current subtree */
  771. while (!list_empty(&dentry->d_subdirs)) {
  772. struct dentry *loop;
  773. /* this is a branch with children - detach all of them
  774. * from the system in one go */
  775. spin_lock(&dentry->d_lock);
  776. list_for_each_entry(loop, &dentry->d_subdirs,
  777. d_u.d_child) {
  778. spin_lock_nested(&loop->d_lock,
  779. DENTRY_D_LOCK_NESTED);
  780. dentry_lru_del(loop);
  781. __d_drop(loop);
  782. spin_unlock(&loop->d_lock);
  783. }
  784. spin_unlock(&dentry->d_lock);
  785. /* move to the first child */
  786. dentry = list_entry(dentry->d_subdirs.next,
  787. struct dentry, d_u.d_child);
  788. }
  789. /* consume the dentries from this leaf up through its parents
  790. * until we find one with children or run out altogether */
  791. do {
  792. struct inode *inode;
  793. if (dentry->d_count != 0) {
  794. printk(KERN_ERR
  795. "BUG: Dentry %p{i=%lx,n=%s}"
  796. " still in use (%d)"
  797. " [unmount of %s %s]\n",
  798. dentry,
  799. dentry->d_inode ?
  800. dentry->d_inode->i_ino : 0UL,
  801. dentry->d_name.name,
  802. dentry->d_count,
  803. dentry->d_sb->s_type->name,
  804. dentry->d_sb->s_id);
  805. BUG();
  806. }
  807. if (IS_ROOT(dentry)) {
  808. parent = NULL;
  809. list_del(&dentry->d_u.d_child);
  810. } else {
  811. parent = dentry->d_parent;
  812. spin_lock(&parent->d_lock);
  813. parent->d_count--;
  814. list_del(&dentry->d_u.d_child);
  815. spin_unlock(&parent->d_lock);
  816. }
  817. detached++;
  818. inode = dentry->d_inode;
  819. if (inode) {
  820. dentry->d_inode = NULL;
  821. list_del_init(&dentry->d_alias);
  822. if (dentry->d_op && dentry->d_op->d_iput)
  823. dentry->d_op->d_iput(dentry, inode);
  824. else
  825. iput(inode);
  826. }
  827. d_free(dentry);
  828. /* finished when we fall off the top of the tree,
  829. * otherwise we ascend to the parent and move to the
  830. * next sibling if there is one */
  831. if (!parent)
  832. return;
  833. dentry = parent;
  834. } while (list_empty(&dentry->d_subdirs));
  835. dentry = list_entry(dentry->d_subdirs.next,
  836. struct dentry, d_u.d_child);
  837. }
  838. }
  839. /*
  840. * destroy the dentries attached to a superblock on unmounting
  841. * - we don't need to use dentry->d_lock because:
  842. * - the superblock is detached from all mountings and open files, so the
  843. * dentry trees will not be rearranged by the VFS
  844. * - s_umount is write-locked, so the memory pressure shrinker will ignore
  845. * any dentries belonging to this superblock that it comes across
  846. * - the filesystem itself is no longer permitted to rearrange the dentries
  847. * in this superblock
  848. */
  849. void shrink_dcache_for_umount(struct super_block *sb)
  850. {
  851. struct dentry *dentry;
  852. if (down_read_trylock(&sb->s_umount))
  853. BUG();
  854. dentry = sb->s_root;
  855. sb->s_root = NULL;
  856. spin_lock(&dentry->d_lock);
  857. dentry->d_count--;
  858. spin_unlock(&dentry->d_lock);
  859. shrink_dcache_for_umount_subtree(dentry);
  860. while (!hlist_empty(&sb->s_anon)) {
  861. dentry = hlist_entry(sb->s_anon.first, struct dentry, d_hash);
  862. shrink_dcache_for_umount_subtree(dentry);
  863. }
  864. }
  865. /*
  866. * Search for at least 1 mount point in the dentry's subdirs.
  867. * We descend to the next level whenever the d_subdirs
  868. * list is non-empty and continue searching.
  869. */
  870. /**
  871. * have_submounts - check for mounts over a dentry
  872. * @parent: dentry to check.
  873. *
  874. * Return true if the parent or its subdirectories contain
  875. * a mount point
  876. */
  877. int have_submounts(struct dentry *parent)
  878. {
  879. struct dentry *this_parent;
  880. struct list_head *next;
  881. unsigned seq;
  882. int locked = 0;
  883. seq = read_seqbegin(&rename_lock);
  884. again:
  885. this_parent = parent;
  886. if (d_mountpoint(parent))
  887. goto positive;
  888. spin_lock(&this_parent->d_lock);
  889. repeat:
  890. next = this_parent->d_subdirs.next;
  891. resume:
  892. while (next != &this_parent->d_subdirs) {
  893. struct list_head *tmp = next;
  894. struct dentry *dentry = list_entry(tmp, struct dentry, d_u.d_child);
  895. next = tmp->next;
  896. spin_lock_nested(&dentry->d_lock, DENTRY_D_LOCK_NESTED);
  897. /* Have we found a mount point ? */
  898. if (d_mountpoint(dentry)) {
  899. spin_unlock(&dentry->d_lock);
  900. spin_unlock(&this_parent->d_lock);
  901. goto positive;
  902. }
  903. if (!list_empty(&dentry->d_subdirs)) {
  904. spin_unlock(&this_parent->d_lock);
  905. spin_release(&dentry->d_lock.dep_map, 1, _RET_IP_);
  906. this_parent = dentry;
  907. spin_acquire(&this_parent->d_lock.dep_map, 0, 1, _RET_IP_);
  908. goto repeat;
  909. }
  910. spin_unlock(&dentry->d_lock);
  911. }
  912. /*
  913. * All done at this level ... ascend and resume the search.
  914. */
  915. if (this_parent != parent) {
  916. struct dentry *tmp;
  917. struct dentry *child;
  918. tmp = this_parent->d_parent;
  919. rcu_read_lock();
  920. spin_unlock(&this_parent->d_lock);
  921. child = this_parent;
  922. this_parent = tmp;
  923. spin_lock(&this_parent->d_lock);
  924. /* might go back up the wrong parent if we have had a rename
  925. * or deletion */
  926. if (this_parent != child->d_parent ||
  927. (!locked && read_seqretry(&rename_lock, seq))) {
  928. spin_unlock(&this_parent->d_lock);
  929. rcu_read_unlock();
  930. goto rename_retry;
  931. }
  932. rcu_read_unlock();
  933. next = child->d_u.d_child.next;
  934. goto resume;
  935. }
  936. spin_unlock(&this_parent->d_lock);
  937. if (!locked && read_seqretry(&rename_lock, seq))
  938. goto rename_retry;
  939. if (locked)
  940. write_sequnlock(&rename_lock);
  941. return 0; /* No mount points found in tree */
  942. positive:
  943. if (!locked && read_seqretry(&rename_lock, seq))
  944. goto rename_retry;
  945. if (locked)
  946. write_sequnlock(&rename_lock);
  947. return 1;
  948. rename_retry:
  949. locked = 1;
  950. write_seqlock(&rename_lock);
  951. goto again;
  952. }
  953. EXPORT_SYMBOL(have_submounts);
  954. /*
  955. * Search the dentry child list for the specified parent,
  956. * and move any unused dentries to the end of the unused
  957. * list for prune_dcache(). We descend to the next level
  958. * whenever the d_subdirs list is non-empty and continue
  959. * searching.
  960. *
  961. * It returns zero iff there are no unused children,
  962. * otherwise it returns the number of children moved to
  963. * the end of the unused list. This may not be the total
  964. * number of unused children, because select_parent can
  965. * drop the lock and return early due to latency
  966. * constraints.
  967. */
  968. static int select_parent(struct dentry * parent)
  969. {
  970. struct dentry *this_parent;
  971. struct list_head *next;
  972. unsigned seq;
  973. int found = 0;
  974. int locked = 0;
  975. seq = read_seqbegin(&rename_lock);
  976. again:
  977. this_parent = parent;
  978. spin_lock(&this_parent->d_lock);
  979. repeat:
  980. next = this_parent->d_subdirs.next;
  981. resume:
  982. while (next != &this_parent->d_subdirs) {
  983. struct list_head *tmp = next;
  984. struct dentry *dentry = list_entry(tmp, struct dentry, d_u.d_child);
  985. next = tmp->next;
  986. spin_lock_nested(&dentry->d_lock, DENTRY_D_LOCK_NESTED);
  987. /*
  988. * move only zero ref count dentries to the end
  989. * of the unused list for prune_dcache
  990. */
  991. if (!dentry->d_count) {
  992. dentry_lru_move_tail(dentry);
  993. found++;
  994. } else {
  995. dentry_lru_del(dentry);
  996. }
  997. /*
  998. * We can return to the caller if we have found some (this
  999. * ensures forward progress). We'll be coming back to find
  1000. * the rest.
  1001. */
  1002. if (found && need_resched()) {
  1003. spin_unlock(&dentry->d_lock);
  1004. goto out;
  1005. }
  1006. /*
  1007. * Descend a level if the d_subdirs list is non-empty.
  1008. */
  1009. if (!list_empty(&dentry->d_subdirs)) {
  1010. spin_unlock(&this_parent->d_lock);
  1011. spin_release(&dentry->d_lock.dep_map, 1, _RET_IP_);
  1012. this_parent = dentry;
  1013. spin_acquire(&this_parent->d_lock.dep_map, 0, 1, _RET_IP_);
  1014. goto repeat;
  1015. }
  1016. spin_unlock(&dentry->d_lock);
  1017. }
  1018. /*
  1019. * All done at this level ... ascend and resume the search.
  1020. */
  1021. if (this_parent != parent) {
  1022. struct dentry *tmp;
  1023. struct dentry *child;
  1024. tmp = this_parent->d_parent;
  1025. rcu_read_lock();
  1026. spin_unlock(&this_parent->d_lock);
  1027. child = this_parent;
  1028. this_parent = tmp;
  1029. spin_lock(&this_parent->d_lock);
  1030. /* might go back up the wrong parent if we have had a rename
  1031. * or deletion */
  1032. if (this_parent != child->d_parent ||
  1033. (!locked && read_seqretry(&rename_lock, seq))) {
  1034. spin_unlock(&this_parent->d_lock);
  1035. rcu_read_unlock();
  1036. goto rename_retry;
  1037. }
  1038. rcu_read_unlock();
  1039. next = child->d_u.d_child.next;
  1040. goto resume;
  1041. }
  1042. out:
  1043. spin_unlock(&this_parent->d_lock);
  1044. if (!locked && read_seqretry(&rename_lock, seq))
  1045. goto rename_retry;
  1046. if (locked)
  1047. write_sequnlock(&rename_lock);
  1048. return found;
  1049. rename_retry:
  1050. if (found)
  1051. return found;
  1052. locked = 1;
  1053. write_seqlock(&rename_lock);
  1054. goto again;
  1055. }
  1056. /**
  1057. * shrink_dcache_parent - prune dcache
  1058. * @parent: parent of entries to prune
  1059. *
  1060. * Prune the dcache to remove unused children of the parent dentry.
  1061. */
  1062. void shrink_dcache_parent(struct dentry * parent)
  1063. {
  1064. struct super_block *sb = parent->d_sb;
  1065. int found;
  1066. while ((found = select_parent(parent)) != 0)
  1067. __shrink_dcache_sb(sb, &found, 0);
  1068. }
  1069. EXPORT_SYMBOL(shrink_dcache_parent);
  1070. /*
  1071. * Scan `nr' dentries and return the number which remain.
  1072. *
  1073. * We need to avoid reentering the filesystem if the caller is performing a
  1074. * GFP_NOFS allocation attempt. One example deadlock is:
  1075. *
  1076. * ext2_new_block->getblk->GFP->shrink_dcache_memory->prune_dcache->
  1077. * prune_one_dentry->dput->dentry_iput->iput->inode->i_sb->s_op->put_inode->
  1078. * ext2_discard_prealloc->ext2_free_blocks->lock_super->DEADLOCK.
  1079. *
  1080. * In this case we return -1 to tell the caller that we baled.
  1081. */
  1082. static int shrink_dcache_memory(struct shrinker *shrink, int nr, gfp_t gfp_mask)
  1083. {
  1084. if (nr) {
  1085. if (!(gfp_mask & __GFP_FS))
  1086. return -1;
  1087. prune_dcache(nr);
  1088. }
  1089. return (dentry_stat.nr_unused / 100) * sysctl_vfs_cache_pressure;
  1090. }
  1091. static struct shrinker dcache_shrinker = {
  1092. .shrink = shrink_dcache_memory,
  1093. .seeks = DEFAULT_SEEKS,
  1094. };
  1095. /**
  1096. * d_alloc - allocate a dcache entry
  1097. * @parent: parent of entry to allocate
  1098. * @name: qstr of the name
  1099. *
  1100. * Allocates a dentry. It returns %NULL if there is insufficient memory
  1101. * available. On a success the dentry is returned. The name passed in is
  1102. * copied and the copy passed in may be reused after this call.
  1103. */
  1104. struct dentry *d_alloc(struct dentry * parent, const struct qstr *name)
  1105. {
  1106. struct dentry *dentry;
  1107. char *dname;
  1108. dentry = kmem_cache_alloc(dentry_cache, GFP_KERNEL);
  1109. if (!dentry)
  1110. return NULL;
  1111. if (name->len > DNAME_INLINE_LEN-1) {
  1112. dname = kmalloc(name->len + 1, GFP_KERNEL);
  1113. if (!dname) {
  1114. kmem_cache_free(dentry_cache, dentry);
  1115. return NULL;
  1116. }
  1117. } else {
  1118. dname = dentry->d_iname;
  1119. }
  1120. dentry->d_name.name = dname;
  1121. dentry->d_name.len = name->len;
  1122. dentry->d_name.hash = name->hash;
  1123. memcpy(dname, name->name, name->len);
  1124. dname[name->len] = 0;
  1125. dentry->d_count = 1;
  1126. dentry->d_flags = DCACHE_UNHASHED;
  1127. spin_lock_init(&dentry->d_lock);
  1128. dentry->d_inode = NULL;
  1129. dentry->d_parent = NULL;
  1130. dentry->d_sb = NULL;
  1131. dentry->d_op = NULL;
  1132. dentry->d_fsdata = NULL;
  1133. dentry->d_mounted = 0;
  1134. INIT_HLIST_NODE(&dentry->d_hash);
  1135. INIT_LIST_HEAD(&dentry->d_lru);
  1136. INIT_LIST_HEAD(&dentry->d_subdirs);
  1137. INIT_LIST_HEAD(&dentry->d_alias);
  1138. INIT_LIST_HEAD(&dentry->d_u.d_child);
  1139. if (parent) {
  1140. spin_lock(&parent->d_lock);
  1141. /*
  1142. * don't need child lock because it is not subject
  1143. * to concurrency here
  1144. */
  1145. dentry->d_parent = dget_dlock(parent);
  1146. dentry->d_sb = parent->d_sb;
  1147. list_add(&dentry->d_u.d_child, &parent->d_subdirs);
  1148. spin_unlock(&parent->d_lock);
  1149. }
  1150. this_cpu_inc(nr_dentry);
  1151. return dentry;
  1152. }
  1153. EXPORT_SYMBOL(d_alloc);
  1154. struct dentry *d_alloc_name(struct dentry *parent, const char *name)
  1155. {
  1156. struct qstr q;
  1157. q.name = name;
  1158. q.len = strlen(name);
  1159. q.hash = full_name_hash(q.name, q.len);
  1160. return d_alloc(parent, &q);
  1161. }
  1162. EXPORT_SYMBOL(d_alloc_name);
  1163. static void __d_instantiate(struct dentry *dentry, struct inode *inode)
  1164. {
  1165. spin_lock(&dentry->d_lock);
  1166. if (inode)
  1167. list_add(&dentry->d_alias, &inode->i_dentry);
  1168. dentry->d_inode = inode;
  1169. spin_unlock(&dentry->d_lock);
  1170. fsnotify_d_instantiate(dentry, inode);
  1171. }
  1172. /**
  1173. * d_instantiate - fill in inode information for a dentry
  1174. * @entry: dentry to complete
  1175. * @inode: inode to attach to this dentry
  1176. *
  1177. * Fill in inode information in the entry.
  1178. *
  1179. * This turns negative dentries into productive full members
  1180. * of society.
  1181. *
  1182. * NOTE! This assumes that the inode count has been incremented
  1183. * (or otherwise set) by the caller to indicate that it is now
  1184. * in use by the dcache.
  1185. */
  1186. void d_instantiate(struct dentry *entry, struct inode * inode)
  1187. {
  1188. BUG_ON(!list_empty(&entry->d_alias));
  1189. spin_lock(&dcache_inode_lock);
  1190. __d_instantiate(entry, inode);
  1191. spin_unlock(&dcache_inode_lock);
  1192. security_d_instantiate(entry, inode);
  1193. }
  1194. EXPORT_SYMBOL(d_instantiate);
  1195. /**
  1196. * d_instantiate_unique - instantiate a non-aliased dentry
  1197. * @entry: dentry to instantiate
  1198. * @inode: inode to attach to this dentry
  1199. *
  1200. * Fill in inode information in the entry. On success, it returns NULL.
  1201. * If an unhashed alias of "entry" already exists, then we return the
  1202. * aliased dentry instead and drop one reference to inode.
  1203. *
  1204. * Note that in order to avoid conflicts with rename() etc, the caller
  1205. * had better be holding the parent directory semaphore.
  1206. *
  1207. * This also assumes that the inode count has been incremented
  1208. * (or otherwise set) by the caller to indicate that it is now
  1209. * in use by the dcache.
  1210. */
  1211. static struct dentry *__d_instantiate_unique(struct dentry *entry,
  1212. struct inode *inode)
  1213. {
  1214. struct dentry *alias;
  1215. int len = entry->d_name.len;
  1216. const char *name = entry->d_name.name;
  1217. unsigned int hash = entry->d_name.hash;
  1218. if (!inode) {
  1219. __d_instantiate(entry, NULL);
  1220. return NULL;
  1221. }
  1222. list_for_each_entry(alias, &inode->i_dentry, d_alias) {
  1223. struct qstr *qstr = &alias->d_name;
  1224. /*
  1225. * Don't need alias->d_lock here, because aliases with
  1226. * d_parent == entry->d_parent are not subject to name or
  1227. * parent changes, because the parent inode i_mutex is held.
  1228. */
  1229. if (qstr->hash != hash)
  1230. continue;
  1231. if (alias->d_parent != entry->d_parent)
  1232. continue;
  1233. if (qstr->len != len)
  1234. continue;
  1235. if (memcmp(qstr->name, name, len))
  1236. continue;
  1237. dget_locked(alias);
  1238. return alias;
  1239. }
  1240. __d_instantiate(entry, inode);
  1241. return NULL;
  1242. }
  1243. struct dentry *d_instantiate_unique(struct dentry *entry, struct inode *inode)
  1244. {
  1245. struct dentry *result;
  1246. BUG_ON(!list_empty(&entry->d_alias));
  1247. spin_lock(&dcache_inode_lock);
  1248. result = __d_instantiate_unique(entry, inode);
  1249. spin_unlock(&dcache_inode_lock);
  1250. if (!result) {
  1251. security_d_instantiate(entry, inode);
  1252. return NULL;
  1253. }
  1254. BUG_ON(!d_unhashed(result));
  1255. iput(inode);
  1256. return result;
  1257. }
  1258. EXPORT_SYMBOL(d_instantiate_unique);
  1259. /**
  1260. * d_alloc_root - allocate root dentry
  1261. * @root_inode: inode to allocate the root for
  1262. *
  1263. * Allocate a root ("/") dentry for the inode given. The inode is
  1264. * instantiated and returned. %NULL is returned if there is insufficient
  1265. * memory or the inode passed is %NULL.
  1266. */
  1267. struct dentry * d_alloc_root(struct inode * root_inode)
  1268. {
  1269. struct dentry *res = NULL;
  1270. if (root_inode) {
  1271. static const struct qstr name = { .name = "/", .len = 1 };
  1272. res = d_alloc(NULL, &name);
  1273. if (res) {
  1274. res->d_sb = root_inode->i_sb;
  1275. res->d_parent = res;
  1276. d_instantiate(res, root_inode);
  1277. }
  1278. }
  1279. return res;
  1280. }
  1281. EXPORT_SYMBOL(d_alloc_root);
  1282. static inline struct hlist_head *d_hash(struct dentry *parent,
  1283. unsigned long hash)
  1284. {
  1285. hash += ((unsigned long) parent ^ GOLDEN_RATIO_PRIME) / L1_CACHE_BYTES;
  1286. hash = hash ^ ((hash ^ GOLDEN_RATIO_PRIME) >> D_HASHBITS);
  1287. return dentry_hashtable + (hash & D_HASHMASK);
  1288. }
  1289. /**
  1290. * d_obtain_alias - find or allocate a dentry for a given inode
  1291. * @inode: inode to allocate the dentry for
  1292. *
  1293. * Obtain a dentry for an inode resulting from NFS filehandle conversion or
  1294. * similar open by handle operations. The returned dentry may be anonymous,
  1295. * or may have a full name (if the inode was already in the cache).
  1296. *
  1297. * When called on a directory inode, we must ensure that the inode only ever
  1298. * has one dentry. If a dentry is found, that is returned instead of
  1299. * allocating a new one.
  1300. *
  1301. * On successful return, the reference to the inode has been transferred
  1302. * to the dentry. In case of an error the reference on the inode is released.
  1303. * To make it easier to use in export operations a %NULL or IS_ERR inode may
  1304. * be passed in and will be the error will be propagate to the return value,
  1305. * with a %NULL @inode replaced by ERR_PTR(-ESTALE).
  1306. */
  1307. struct dentry *d_obtain_alias(struct inode *inode)
  1308. {
  1309. static const struct qstr anonstring = { .name = "" };
  1310. struct dentry *tmp;
  1311. struct dentry *res;
  1312. if (!inode)
  1313. return ERR_PTR(-ESTALE);
  1314. if (IS_ERR(inode))
  1315. return ERR_CAST(inode);
  1316. res = d_find_alias(inode);
  1317. if (res)
  1318. goto out_iput;
  1319. tmp = d_alloc(NULL, &anonstring);
  1320. if (!tmp) {
  1321. res = ERR_PTR(-ENOMEM);
  1322. goto out_iput;
  1323. }
  1324. tmp->d_parent = tmp; /* make sure dput doesn't croak */
  1325. spin_lock(&dcache_inode_lock);
  1326. res = __d_find_alias(inode, 0);
  1327. if (res) {
  1328. spin_unlock(&dcache_inode_lock);
  1329. dput(tmp);
  1330. goto out_iput;
  1331. }
  1332. /* attach a disconnected dentry */
  1333. spin_lock(&tmp->d_lock);
  1334. tmp->d_sb = inode->i_sb;
  1335. tmp->d_inode = inode;
  1336. tmp->d_flags |= DCACHE_DISCONNECTED;
  1337. tmp->d_flags &= ~DCACHE_UNHASHED;
  1338. list_add(&tmp->d_alias, &inode->i_dentry);
  1339. spin_lock(&dcache_hash_lock);
  1340. hlist_add_head(&tmp->d_hash, &inode->i_sb->s_anon);
  1341. spin_unlock(&dcache_hash_lock);
  1342. spin_unlock(&tmp->d_lock);
  1343. spin_unlock(&dcache_inode_lock);
  1344. return tmp;
  1345. out_iput:
  1346. iput(inode);
  1347. return res;
  1348. }
  1349. EXPORT_SYMBOL(d_obtain_alias);
  1350. /**
  1351. * d_splice_alias - splice a disconnected dentry into the tree if one exists
  1352. * @inode: the inode which may have a disconnected dentry
  1353. * @dentry: a negative dentry which we want to point to the inode.
  1354. *
  1355. * If inode is a directory and has a 'disconnected' dentry (i.e. IS_ROOT and
  1356. * DCACHE_DISCONNECTED), then d_move that in place of the given dentry
  1357. * and return it, else simply d_add the inode to the dentry and return NULL.
  1358. *
  1359. * This is needed in the lookup routine of any filesystem that is exportable
  1360. * (via knfsd) so that we can build dcache paths to directories effectively.
  1361. *
  1362. * If a dentry was found and moved, then it is returned. Otherwise NULL
  1363. * is returned. This matches the expected return value of ->lookup.
  1364. *
  1365. */
  1366. struct dentry *d_splice_alias(struct inode *inode, struct dentry *dentry)
  1367. {
  1368. struct dentry *new = NULL;
  1369. if (inode && S_ISDIR(inode->i_mode)) {
  1370. spin_lock(&dcache_inode_lock);
  1371. new = __d_find_alias(inode, 1);
  1372. if (new) {
  1373. BUG_ON(!(new->d_flags & DCACHE_DISCONNECTED));
  1374. spin_unlock(&dcache_inode_lock);
  1375. security_d_instantiate(new, inode);
  1376. d_move(new, dentry);
  1377. iput(inode);
  1378. } else {
  1379. /* already taking dcache_inode_lock, so d_add() by hand */
  1380. __d_instantiate(dentry, inode);
  1381. spin_unlock(&dcache_inode_lock);
  1382. security_d_instantiate(dentry, inode);
  1383. d_rehash(dentry);
  1384. }
  1385. } else
  1386. d_add(dentry, inode);
  1387. return new;
  1388. }
  1389. EXPORT_SYMBOL(d_splice_alias);
  1390. /**
  1391. * d_add_ci - lookup or allocate new dentry with case-exact name
  1392. * @inode: the inode case-insensitive lookup has found
  1393. * @dentry: the negative dentry that was passed to the parent's lookup func
  1394. * @name: the case-exact name to be associated with the returned dentry
  1395. *
  1396. * This is to avoid filling the dcache with case-insensitive names to the
  1397. * same inode, only the actual correct case is stored in the dcache for
  1398. * case-insensitive filesystems.
  1399. *
  1400. * For a case-insensitive lookup match and if the the case-exact dentry
  1401. * already exists in in the dcache, use it and return it.
  1402. *
  1403. * If no entry exists with the exact case name, allocate new dentry with
  1404. * the exact case, and return the spliced entry.
  1405. */
  1406. struct dentry *d_add_ci(struct dentry *dentry, struct inode *inode,
  1407. struct qstr *name)
  1408. {
  1409. int error;
  1410. struct dentry *found;
  1411. struct dentry *new;
  1412. /*
  1413. * First check if a dentry matching the name already exists,
  1414. * if not go ahead and create it now.
  1415. */
  1416. found = d_hash_and_lookup(dentry->d_parent, name);
  1417. if (!found) {
  1418. new = d_alloc(dentry->d_parent, name);
  1419. if (!new) {
  1420. error = -ENOMEM;
  1421. goto err_out;
  1422. }
  1423. found = d_splice_alias(inode, new);
  1424. if (found) {
  1425. dput(new);
  1426. return found;
  1427. }
  1428. return new;
  1429. }
  1430. /*
  1431. * If a matching dentry exists, and it's not negative use it.
  1432. *
  1433. * Decrement the reference count to balance the iget() done
  1434. * earlier on.
  1435. */
  1436. if (found->d_inode) {
  1437. if (unlikely(found->d_inode != inode)) {
  1438. /* This can't happen because bad inodes are unhashed. */
  1439. BUG_ON(!is_bad_inode(inode));
  1440. BUG_ON(!is_bad_inode(found->d_inode));
  1441. }
  1442. iput(inode);
  1443. return found;
  1444. }
  1445. /*
  1446. * Negative dentry: instantiate it unless the inode is a directory and
  1447. * already has a dentry.
  1448. */
  1449. spin_lock(&dcache_inode_lock);
  1450. if (!S_ISDIR(inode->i_mode) || list_empty(&inode->i_dentry)) {
  1451. __d_instantiate(found, inode);
  1452. spin_unlock(&dcache_inode_lock);
  1453. security_d_instantiate(found, inode);
  1454. return found;
  1455. }
  1456. /*
  1457. * In case a directory already has a (disconnected) entry grab a
  1458. * reference to it, move it in place and use it.
  1459. */
  1460. new = list_entry(inode->i_dentry.next, struct dentry, d_alias);
  1461. dget_locked(new);
  1462. spin_unlock(&dcache_inode_lock);
  1463. security_d_instantiate(found, inode);
  1464. d_move(new, found);
  1465. iput(inode);
  1466. dput(found);
  1467. return new;
  1468. err_out:
  1469. iput(inode);
  1470. return ERR_PTR(error);
  1471. }
  1472. EXPORT_SYMBOL(d_add_ci);
  1473. /**
  1474. * d_lookup - search for a dentry
  1475. * @parent: parent dentry
  1476. * @name: qstr of name we wish to find
  1477. * Returns: dentry, or NULL
  1478. *
  1479. * d_lookup searches the children of the parent dentry for the name in
  1480. * question. If the dentry is found its reference count is incremented and the
  1481. * dentry is returned. The caller must use dput to free the entry when it has
  1482. * finished using it. %NULL is returned if the dentry does not exist.
  1483. */
  1484. struct dentry * d_lookup(struct dentry * parent, struct qstr * name)
  1485. {
  1486. struct dentry * dentry = NULL;
  1487. unsigned seq;
  1488. do {
  1489. seq = read_seqbegin(&rename_lock);
  1490. dentry = __d_lookup(parent, name);
  1491. if (dentry)
  1492. break;
  1493. } while (read_seqretry(&rename_lock, seq));
  1494. return dentry;
  1495. }
  1496. EXPORT_SYMBOL(d_lookup);
  1497. /*
  1498. * __d_lookup - search for a dentry (racy)
  1499. * @parent: parent dentry
  1500. * @name: qstr of name we wish to find
  1501. * Returns: dentry, or NULL
  1502. *
  1503. * __d_lookup is like d_lookup, however it may (rarely) return a
  1504. * false-negative result due to unrelated rename activity.
  1505. *
  1506. * __d_lookup is slightly faster by avoiding rename_lock read seqlock,
  1507. * however it must be used carefully, eg. with a following d_lookup in
  1508. * the case of failure.
  1509. *
  1510. * __d_lookup callers must be commented.
  1511. */
  1512. struct dentry * __d_lookup(struct dentry * parent, struct qstr * name)
  1513. {
  1514. unsigned int len = name->len;
  1515. unsigned int hash = name->hash;
  1516. const unsigned char *str = name->name;
  1517. struct hlist_head *head = d_hash(parent,hash);
  1518. struct dentry *found = NULL;
  1519. struct hlist_node *node;
  1520. struct dentry *dentry;
  1521. /*
  1522. * The hash list is protected using RCU.
  1523. *
  1524. * Take d_lock when comparing a candidate dentry, to avoid races
  1525. * with d_move().
  1526. *
  1527. * It is possible that concurrent renames can mess up our list
  1528. * walk here and result in missing our dentry, resulting in the
  1529. * false-negative result. d_lookup() protects against concurrent
  1530. * renames using rename_lock seqlock.
  1531. *
  1532. * See Documentation/vfs/dcache-locking.txt for more details.
  1533. */
  1534. rcu_read_lock();
  1535. hlist_for_each_entry_rcu(dentry, node, head, d_hash) {
  1536. struct qstr *qstr;
  1537. if (dentry->d_name.hash != hash)
  1538. continue;
  1539. if (dentry->d_parent != parent)
  1540. continue;
  1541. spin_lock(&dentry->d_lock);
  1542. /*
  1543. * Recheck the dentry after taking the lock - d_move may have
  1544. * changed things. Don't bother checking the hash because
  1545. * we're about to compare the whole name anyway.
  1546. */
  1547. if (dentry->d_parent != parent)
  1548. goto next;
  1549. /* non-existing due to RCU? */
  1550. if (d_unhashed(dentry))
  1551. goto next;
  1552. /*
  1553. * It is safe to compare names since d_move() cannot
  1554. * change the qstr (protected by d_lock).
  1555. */
  1556. qstr = &dentry->d_name;
  1557. if (parent->d_op && parent->d_op->d_compare) {
  1558. if (parent->d_op->d_compare(parent, parent->d_inode,
  1559. dentry, dentry->d_inode,
  1560. qstr->len, qstr->name, name))
  1561. goto next;
  1562. } else {
  1563. if (qstr->len != len)
  1564. goto next;
  1565. if (memcmp(qstr->name, str, len))
  1566. goto next;
  1567. }
  1568. dentry->d_count++;
  1569. found = dentry;
  1570. spin_unlock(&dentry->d_lock);
  1571. break;
  1572. next:
  1573. spin_unlock(&dentry->d_lock);
  1574. }
  1575. rcu_read_unlock();
  1576. return found;
  1577. }
  1578. /**
  1579. * d_hash_and_lookup - hash the qstr then search for a dentry
  1580. * @dir: Directory to search in
  1581. * @name: qstr of name we wish to find
  1582. *
  1583. * On hash failure or on lookup failure NULL is returned.
  1584. */
  1585. struct dentry *d_hash_and_lookup(struct dentry *dir, struct qstr *name)
  1586. {
  1587. struct dentry *dentry = NULL;
  1588. /*
  1589. * Check for a fs-specific hash function. Note that we must
  1590. * calculate the standard hash first, as the d_op->d_hash()
  1591. * routine may choose to leave the hash value unchanged.
  1592. */
  1593. name->hash = full_name_hash(name->name, name->len);
  1594. if (dir->d_op && dir->d_op->d_hash) {
  1595. if (dir->d_op->d_hash(dir, dir->d_inode, name) < 0)
  1596. goto out;
  1597. }
  1598. dentry = d_lookup(dir, name);
  1599. out:
  1600. return dentry;
  1601. }
  1602. /**
  1603. * d_validate - verify dentry provided from insecure source (deprecated)
  1604. * @dentry: The dentry alleged to be valid child of @dparent
  1605. * @dparent: The parent dentry (known to be valid)
  1606. *
  1607. * An insecure source has sent us a dentry, here we verify it and dget() it.
  1608. * This is used by ncpfs in its readdir implementation.
  1609. * Zero is returned in the dentry is invalid.
  1610. *
  1611. * This function is slow for big directories, and deprecated, do not use it.
  1612. */
  1613. int d_validate(struct dentry *dentry, struct dentry *dparent)
  1614. {
  1615. struct dentry *child;
  1616. spin_lock(&dparent->d_lock);
  1617. list_for_each_entry(child, &dparent->d_subdirs, d_u.d_child) {
  1618. if (dentry == child) {
  1619. spin_lock_nested(&dentry->d_lock, DENTRY_D_LOCK_NESTED);
  1620. __dget_locked_dlock(dentry);
  1621. spin_unlock(&dentry->d_lock);
  1622. spin_unlock(&dparent->d_lock);
  1623. return 1;
  1624. }
  1625. }
  1626. spin_unlock(&dparent->d_lock);
  1627. return 0;
  1628. }
  1629. EXPORT_SYMBOL(d_validate);
  1630. /*
  1631. * When a file is deleted, we have two options:
  1632. * - turn this dentry into a negative dentry
  1633. * - unhash this dentry and free it.
  1634. *
  1635. * Usually, we want to just turn this into
  1636. * a negative dentry, but if anybody else is
  1637. * currently using the dentry or the inode
  1638. * we can't do that and we fall back on removing
  1639. * it from the hash queues and waiting for
  1640. * it to be deleted later when it has no users
  1641. */
  1642. /**
  1643. * d_delete - delete a dentry
  1644. * @dentry: The dentry to delete
  1645. *
  1646. * Turn the dentry into a negative dentry if possible, otherwise
  1647. * remove it from the hash queues so it can be deleted later
  1648. */
  1649. void d_delete(struct dentry * dentry)
  1650. {
  1651. int isdir = 0;
  1652. /*
  1653. * Are we the only user?
  1654. */
  1655. again:
  1656. spin_lock(&dentry->d_lock);
  1657. isdir = S_ISDIR(dentry->d_inode->i_mode);
  1658. if (dentry->d_count == 1) {
  1659. if (!spin_trylock(&dcache_inode_lock)) {
  1660. spin_unlock(&dentry->d_lock);
  1661. cpu_relax();
  1662. goto again;
  1663. }
  1664. dentry->d_flags &= ~DCACHE_CANT_MOUNT;
  1665. dentry_iput(dentry);
  1666. fsnotify_nameremove(dentry, isdir);
  1667. return;
  1668. }
  1669. if (!d_unhashed(dentry))
  1670. __d_drop(dentry);
  1671. spin_unlock(&dentry->d_lock);
  1672. fsnotify_nameremove(dentry, isdir);
  1673. }
  1674. EXPORT_SYMBOL(d_delete);
  1675. static void __d_rehash(struct dentry * entry, struct hlist_head *list)
  1676. {
  1677. entry->d_flags &= ~DCACHE_UNHASHED;
  1678. hlist_add_head_rcu(&entry->d_hash, list);
  1679. }
  1680. static void _d_rehash(struct dentry * entry)
  1681. {
  1682. __d_rehash(entry, d_hash(entry->d_parent, entry->d_name.hash));
  1683. }
  1684. /**
  1685. * d_rehash - add an entry back to the hash
  1686. * @entry: dentry to add to the hash
  1687. *
  1688. * Adds a dentry to the hash according to its name.
  1689. */
  1690. void d_rehash(struct dentry * entry)
  1691. {
  1692. spin_lock(&entry->d_lock);
  1693. spin_lock(&dcache_hash_lock);
  1694. _d_rehash(entry);
  1695. spin_unlock(&dcache_hash_lock);
  1696. spin_unlock(&entry->d_lock);
  1697. }
  1698. EXPORT_SYMBOL(d_rehash);
  1699. /**
  1700. * dentry_update_name_case - update case insensitive dentry with a new name
  1701. * @dentry: dentry to be updated
  1702. * @name: new name
  1703. *
  1704. * Update a case insensitive dentry with new case of name.
  1705. *
  1706. * dentry must have been returned by d_lookup with name @name. Old and new
  1707. * name lengths must match (ie. no d_compare which allows mismatched name
  1708. * lengths).
  1709. *
  1710. * Parent inode i_mutex must be held over d_lookup and into this call (to
  1711. * keep renames and concurrent inserts, and readdir(2) away).
  1712. */
  1713. void dentry_update_name_case(struct dentry *dentry, struct qstr *name)
  1714. {
  1715. BUG_ON(!mutex_is_locked(&dentry->d_inode->i_mutex));
  1716. BUG_ON(dentry->d_name.len != name->len); /* d_lookup gives this */
  1717. spin_lock(&dentry->d_lock);
  1718. memcpy((unsigned char *)dentry->d_name.name, name->name, name->len);
  1719. spin_unlock(&dentry->d_lock);
  1720. }
  1721. EXPORT_SYMBOL(dentry_update_name_case);
  1722. static void switch_names(struct dentry *dentry, struct dentry *target)
  1723. {
  1724. if (dname_external(target)) {
  1725. if (dname_external(dentry)) {
  1726. /*
  1727. * Both external: swap the pointers
  1728. */
  1729. swap(target->d_name.name, dentry->d_name.name);
  1730. } else {
  1731. /*
  1732. * dentry:internal, target:external. Steal target's
  1733. * storage and make target internal.
  1734. */
  1735. memcpy(target->d_iname, dentry->d_name.name,
  1736. dentry->d_name.len + 1);
  1737. dentry->d_name.name = target->d_name.name;
  1738. target->d_name.name = target->d_iname;
  1739. }
  1740. } else {
  1741. if (dname_external(dentry)) {
  1742. /*
  1743. * dentry:external, target:internal. Give dentry's
  1744. * storage to target and make dentry internal
  1745. */
  1746. memcpy(dentry->d_iname, target->d_name.name,
  1747. target->d_name.len + 1);
  1748. target->d_name.name = dentry->d_name.name;
  1749. dentry->d_name.name = dentry->d_iname;
  1750. } else {
  1751. /*
  1752. * Both are internal. Just copy target to dentry
  1753. */
  1754. memcpy(dentry->d_iname, target->d_name.name,
  1755. target->d_name.len + 1);
  1756. dentry->d_name.len = target->d_name.len;
  1757. return;
  1758. }
  1759. }
  1760. swap(dentry->d_name.len, target->d_name.len);
  1761. }
  1762. static void dentry_lock_for_move(struct dentry *dentry, struct dentry *target)
  1763. {
  1764. /*
  1765. * XXXX: do we really need to take target->d_lock?
  1766. */
  1767. if (IS_ROOT(dentry) || dentry->d_parent == target->d_parent)
  1768. spin_lock(&target->d_parent->d_lock);
  1769. else {
  1770. if (d_ancestor(dentry->d_parent, target->d_parent)) {
  1771. spin_lock(&dentry->d_parent->d_lock);
  1772. spin_lock_nested(&target->d_parent->d_lock,
  1773. DENTRY_D_LOCK_NESTED);
  1774. } else {
  1775. spin_lock(&target->d_parent->d_lock);
  1776. spin_lock_nested(&dentry->d_parent->d_lock,
  1777. DENTRY_D_LOCK_NESTED);
  1778. }
  1779. }
  1780. if (target < dentry) {
  1781. spin_lock_nested(&target->d_lock, 2);
  1782. spin_lock_nested(&dentry->d_lock, 3);
  1783. } else {
  1784. spin_lock_nested(&dentry->d_lock, 2);
  1785. spin_lock_nested(&target->d_lock, 3);
  1786. }
  1787. }
  1788. static void dentry_unlock_parents_for_move(struct dentry *dentry,
  1789. struct dentry *target)
  1790. {
  1791. if (target->d_parent != dentry->d_parent)
  1792. spin_unlock(&dentry->d_parent->d_lock);
  1793. if (target->d_parent != target)
  1794. spin_unlock(&target->d_parent->d_lock);
  1795. }
  1796. /*
  1797. * When switching names, the actual string doesn't strictly have to
  1798. * be preserved in the target - because we're dropping the target
  1799. * anyway. As such, we can just do a simple memcpy() to copy over
  1800. * the new name before we switch.
  1801. *
  1802. * Note that we have to be a lot more careful about getting the hash
  1803. * switched - we have to switch the hash value properly even if it
  1804. * then no longer matches the actual (corrupted) string of the target.
  1805. * The hash value has to match the hash queue that the dentry is on..
  1806. */
  1807. /*
  1808. * d_move - move a dentry
  1809. * @dentry: entry to move
  1810. * @target: new dentry
  1811. *
  1812. * Update the dcache to reflect the move of a file name. Negative
  1813. * dcache entries should not be moved in this way.
  1814. */
  1815. void d_move(struct dentry * dentry, struct dentry * target)
  1816. {
  1817. if (!dentry->d_inode)
  1818. printk(KERN_WARNING "VFS: moving negative dcache entry\n");
  1819. BUG_ON(d_ancestor(dentry, target));
  1820. BUG_ON(d_ancestor(target, dentry));
  1821. write_seqlock(&rename_lock);
  1822. dentry_lock_for_move(dentry, target);
  1823. /* Move the dentry to the target hash queue, if on different bucket */
  1824. spin_lock(&dcache_hash_lock);
  1825. if (!d_unhashed(dentry))
  1826. hlist_del_rcu(&dentry->d_hash);
  1827. __d_rehash(dentry, d_hash(target->d_parent, target->d_name.hash));
  1828. spin_unlock(&dcache_hash_lock);
  1829. /* Unhash the target: dput() will then get rid of it */
  1830. __d_drop(target);
  1831. list_del(&dentry->d_u.d_child);
  1832. list_del(&target->d_u.d_child);
  1833. /* Switch the names.. */
  1834. switch_names(dentry, target);
  1835. swap(dentry->d_name.hash, target->d_name.hash);
  1836. /* ... and switch the parents */
  1837. if (IS_ROOT(dentry)) {
  1838. dentry->d_parent = target->d_parent;
  1839. target->d_parent = target;
  1840. INIT_LIST_HEAD(&target->d_u.d_child);
  1841. } else {
  1842. swap(dentry->d_parent, target->d_parent);
  1843. /* And add them back to the (new) parent lists */
  1844. list_add(&target->d_u.d_child, &target->d_parent->d_subdirs);
  1845. }
  1846. list_add(&dentry->d_u.d_child, &dentry->d_parent->d_subdirs);
  1847. dentry_unlock_parents_for_move(dentry, target);
  1848. spin_unlock(&target->d_lock);
  1849. fsnotify_d_move(dentry);
  1850. spin_unlock(&dentry->d_lock);
  1851. write_sequnlock(&rename_lock);
  1852. }
  1853. EXPORT_SYMBOL(d_move);
  1854. /**
  1855. * d_ancestor - search for an ancestor
  1856. * @p1: ancestor dentry
  1857. * @p2: child dentry
  1858. *
  1859. * Returns the ancestor dentry of p2 which is a child of p1, if p1 is
  1860. * an ancestor of p2, else NULL.
  1861. */
  1862. struct dentry *d_ancestor(struct dentry *p1, struct dentry *p2)
  1863. {
  1864. struct dentry *p;
  1865. for (p = p2; !IS_ROOT(p); p = p->d_parent) {
  1866. if (p->d_parent == p1)
  1867. return p;
  1868. }
  1869. return NULL;
  1870. }
  1871. /*
  1872. * This helper attempts to cope with remotely renamed directories
  1873. *
  1874. * It assumes that the caller is already holding
  1875. * dentry->d_parent->d_inode->i_mutex and the dcache_inode_lock
  1876. *
  1877. * Note: If ever the locking in lock_rename() changes, then please
  1878. * remember to update this too...
  1879. */
  1880. static struct dentry *__d_unalias(struct dentry *dentry, struct dentry *alias)
  1881. __releases(dcache_inode_lock)
  1882. {
  1883. struct mutex *m1 = NULL, *m2 = NULL;
  1884. struct dentry *ret;
  1885. /* If alias and dentry share a parent, then no extra locks required */
  1886. if (alias->d_parent == dentry->d_parent)
  1887. goto out_unalias;
  1888. /* Check for loops */
  1889. ret = ERR_PTR(-ELOOP);
  1890. if (d_ancestor(alias, dentry))
  1891. goto out_err;
  1892. /* See lock_rename() */
  1893. ret = ERR_PTR(-EBUSY);
  1894. if (!mutex_trylock(&dentry->d_sb->s_vfs_rename_mutex))
  1895. goto out_err;
  1896. m1 = &dentry->d_sb->s_vfs_rename_mutex;
  1897. if (!mutex_trylock(&alias->d_parent->d_inode->i_mutex))
  1898. goto out_err;
  1899. m2 = &alias->d_parent->d_inode->i_mutex;
  1900. out_unalias:
  1901. d_move(alias, dentry);
  1902. ret = alias;
  1903. out_err:
  1904. spin_unlock(&dcache_inode_lock);
  1905. if (m2)
  1906. mutex_unlock(m2);
  1907. if (m1)
  1908. mutex_unlock(m1);
  1909. return ret;
  1910. }
  1911. /*
  1912. * Prepare an anonymous dentry for life in the superblock's dentry tree as a
  1913. * named dentry in place of the dentry to be replaced.
  1914. * returns with anon->d_lock held!
  1915. */
  1916. static void __d_materialise_dentry(struct dentry *dentry, struct dentry *anon)
  1917. {
  1918. struct dentry *dparent, *aparent;
  1919. dentry_lock_for_move(anon, dentry);
  1920. dparent = dentry->d_parent;
  1921. aparent = anon->d_parent;
  1922. switch_names(dentry, anon);
  1923. swap(dentry->d_name.hash, anon->d_name.hash);
  1924. dentry->d_parent = (aparent == anon) ? dentry : aparent;
  1925. list_del(&dentry->d_u.d_child);
  1926. if (!IS_ROOT(dentry))
  1927. list_add(&dentry->d_u.d_child, &dentry->d_parent->d_subdirs);
  1928. else
  1929. INIT_LIST_HEAD(&dentry->d_u.d_child);
  1930. anon->d_parent = (dparent == dentry) ? anon : dparent;
  1931. list_del(&anon->d_u.d_child);
  1932. if (!IS_ROOT(anon))
  1933. list_add(&anon->d_u.d_child, &anon->d_parent->d_subdirs);
  1934. else
  1935. INIT_LIST_HEAD(&anon->d_u.d_child);
  1936. dentry_unlock_parents_for_move(anon, dentry);
  1937. spin_unlock(&dentry->d_lock);
  1938. /* anon->d_lock still locked, returns locked */
  1939. anon->d_flags &= ~DCACHE_DISCONNECTED;
  1940. }
  1941. /**
  1942. * d_materialise_unique - introduce an inode into the tree
  1943. * @dentry: candidate dentry
  1944. * @inode: inode to bind to the dentry, to which aliases may be attached
  1945. *
  1946. * Introduces an dentry into the tree, substituting an extant disconnected
  1947. * root directory alias in its place if there is one
  1948. */
  1949. struct dentry *d_materialise_unique(struct dentry *dentry, struct inode *inode)
  1950. {
  1951. struct dentry *actual;
  1952. BUG_ON(!d_unhashed(dentry));
  1953. if (!inode) {
  1954. actual = dentry;
  1955. __d_instantiate(dentry, NULL);
  1956. d_rehash(actual);
  1957. goto out_nolock;
  1958. }
  1959. spin_lock(&dcache_inode_lock);
  1960. if (S_ISDIR(inode->i_mode)) {
  1961. struct dentry *alias;
  1962. /* Does an aliased dentry already exist? */
  1963. alias = __d_find_alias(inode, 0);
  1964. if (alias) {
  1965. actual = alias;
  1966. /* Is this an anonymous mountpoint that we could splice
  1967. * into our tree? */
  1968. if (IS_ROOT(alias)) {
  1969. __d_materialise_dentry(dentry, alias);
  1970. __d_drop(alias);
  1971. goto found;
  1972. }
  1973. /* Nope, but we must(!) avoid directory aliasing */
  1974. actual = __d_unalias(dentry, alias);
  1975. if (IS_ERR(actual))
  1976. dput(alias);
  1977. goto out_nolock;
  1978. }
  1979. }
  1980. /* Add a unique reference */
  1981. actual = __d_instantiate_unique(dentry, inode);
  1982. if (!actual)
  1983. actual = dentry;
  1984. else
  1985. BUG_ON(!d_unhashed(actual));
  1986. spin_lock(&actual->d_lock);
  1987. found:
  1988. spin_lock(&dcache_hash_lock);
  1989. _d_rehash(actual);
  1990. spin_unlock(&dcache_hash_lock);
  1991. spin_unlock(&actual->d_lock);
  1992. spin_unlock(&dcache_inode_lock);
  1993. out_nolock:
  1994. if (actual == dentry) {
  1995. security_d_instantiate(dentry, inode);
  1996. return NULL;
  1997. }
  1998. iput(inode);
  1999. return actual;
  2000. }
  2001. EXPORT_SYMBOL_GPL(d_materialise_unique);
  2002. static int prepend(char **buffer, int *buflen, const char *str, int namelen)
  2003. {
  2004. *buflen -= namelen;
  2005. if (*buflen < 0)
  2006. return -ENAMETOOLONG;
  2007. *buffer -= namelen;
  2008. memcpy(*buffer, str, namelen);
  2009. return 0;
  2010. }
  2011. static int prepend_name(char **buffer, int *buflen, struct qstr *name)
  2012. {
  2013. return prepend(buffer, buflen, name->name, name->len);
  2014. }
  2015. /**
  2016. * Prepend path string to a buffer
  2017. *
  2018. * @path: the dentry/vfsmount to report
  2019. * @root: root vfsmnt/dentry (may be modified by this function)
  2020. * @buffer: pointer to the end of the buffer
  2021. * @buflen: pointer to buffer length
  2022. *
  2023. * Caller holds the rename_lock.
  2024. *
  2025. * If path is not reachable from the supplied root, then the value of
  2026. * root is changed (without modifying refcounts).
  2027. */
  2028. static int prepend_path(const struct path *path, struct path *root,
  2029. char **buffer, int *buflen)
  2030. {
  2031. struct dentry *dentry = path->dentry;
  2032. struct vfsmount *vfsmnt = path->mnt;
  2033. bool slash = false;
  2034. int error = 0;
  2035. br_read_lock(vfsmount_lock);
  2036. while (dentry != root->dentry || vfsmnt != root->mnt) {
  2037. struct dentry * parent;
  2038. if (dentry == vfsmnt->mnt_root || IS_ROOT(dentry)) {
  2039. /* Global root? */
  2040. if (vfsmnt->mnt_parent == vfsmnt) {
  2041. goto global_root;
  2042. }
  2043. dentry = vfsmnt->mnt_mountpoint;
  2044. vfsmnt = vfsmnt->mnt_parent;
  2045. continue;
  2046. }
  2047. parent = dentry->d_parent;
  2048. prefetch(parent);
  2049. spin_lock(&dentry->d_lock);
  2050. error = prepend_name(buffer, buflen, &dentry->d_name);
  2051. spin_unlock(&dentry->d_lock);
  2052. if (!error)
  2053. error = prepend(buffer, buflen, "/", 1);
  2054. if (error)
  2055. break;
  2056. slash = true;
  2057. dentry = parent;
  2058. }
  2059. out:
  2060. if (!error && !slash)
  2061. error = prepend(buffer, buflen, "/", 1);
  2062. br_read_unlock(vfsmount_lock);
  2063. return error;
  2064. global_root:
  2065. /*
  2066. * Filesystems needing to implement special "root names"
  2067. * should do so with ->d_dname()
  2068. */
  2069. if (IS_ROOT(dentry) &&
  2070. (dentry->d_name.len != 1 || dentry->d_name.name[0] != '/')) {
  2071. WARN(1, "Root dentry has weird name <%.*s>\n",
  2072. (int) dentry->d_name.len, dentry->d_name.name);
  2073. }
  2074. root->mnt = vfsmnt;
  2075. root->dentry = dentry;
  2076. goto out;
  2077. }
  2078. /**
  2079. * __d_path - return the path of a dentry
  2080. * @path: the dentry/vfsmount to report
  2081. * @root: root vfsmnt/dentry (may be modified by this function)
  2082. * @buf: buffer to return value in
  2083. * @buflen: buffer length
  2084. *
  2085. * Convert a dentry into an ASCII path name.
  2086. *
  2087. * Returns a pointer into the buffer or an error code if the
  2088. * path was too long.
  2089. *
  2090. * "buflen" should be positive.
  2091. *
  2092. * If path is not reachable from the supplied root, then the value of
  2093. * root is changed (without modifying refcounts).
  2094. */
  2095. char *__d_path(const struct path *path, struct path *root,
  2096. char *buf, int buflen)
  2097. {
  2098. char *res = buf + buflen;
  2099. int error;
  2100. prepend(&res, &buflen, "\0", 1);
  2101. write_seqlock(&rename_lock);
  2102. error = prepend_path(path, root, &res, &buflen);
  2103. write_sequnlock(&rename_lock);
  2104. if (error)
  2105. return ERR_PTR(error);
  2106. return res;
  2107. }
  2108. /*
  2109. * same as __d_path but appends "(deleted)" for unlinked files.
  2110. */
  2111. static int path_with_deleted(const struct path *path, struct path *root,
  2112. char **buf, int *buflen)
  2113. {
  2114. prepend(buf, buflen, "\0", 1);
  2115. if (d_unlinked(path->dentry)) {
  2116. int error = prepend(buf, buflen, " (deleted)", 10);
  2117. if (error)
  2118. return error;
  2119. }
  2120. return prepend_path(path, root, buf, buflen);
  2121. }
  2122. static int prepend_unreachable(char **buffer, int *buflen)
  2123. {
  2124. return prepend(buffer, buflen, "(unreachable)", 13);
  2125. }
  2126. /**
  2127. * d_path - return the path of a dentry
  2128. * @path: path to report
  2129. * @buf: buffer to return value in
  2130. * @buflen: buffer length
  2131. *
  2132. * Convert a dentry into an ASCII path name. If the entry has been deleted
  2133. * the string " (deleted)" is appended. Note that this is ambiguous.
  2134. *
  2135. * Returns a pointer into the buffer or an error code if the path was
  2136. * too long. Note: Callers should use the returned pointer, not the passed
  2137. * in buffer, to use the name! The implementation often starts at an offset
  2138. * into the buffer, and may leave 0 bytes at the start.
  2139. *
  2140. * "buflen" should be positive.
  2141. */
  2142. char *d_path(const struct path *path, char *buf, int buflen)
  2143. {
  2144. char *res = buf + buflen;
  2145. struct path root;
  2146. struct path tmp;
  2147. int error;
  2148. /*
  2149. * We have various synthetic filesystems that never get mounted. On
  2150. * these filesystems dentries are never used for lookup purposes, and
  2151. * thus don't need to be hashed. They also don't need a name until a
  2152. * user wants to identify the object in /proc/pid/fd/. The little hack
  2153. * below allows us to generate a name for these objects on demand:
  2154. */
  2155. if (path->dentry->d_op && path->dentry->d_op->d_dname)
  2156. return path->dentry->d_op->d_dname(path->dentry, buf, buflen);
  2157. get_fs_root(current->fs, &root);
  2158. write_seqlock(&rename_lock);
  2159. tmp = root;
  2160. error = path_with_deleted(path, &tmp, &res, &buflen);
  2161. if (error)
  2162. res = ERR_PTR(error);
  2163. write_sequnlock(&rename_lock);
  2164. path_put(&root);
  2165. return res;
  2166. }
  2167. EXPORT_SYMBOL(d_path);
  2168. /**
  2169. * d_path_with_unreachable - return the path of a dentry
  2170. * @path: path to report
  2171. * @buf: buffer to return value in
  2172. * @buflen: buffer length
  2173. *
  2174. * The difference from d_path() is that this prepends "(unreachable)"
  2175. * to paths which are unreachable from the current process' root.
  2176. */
  2177. char *d_path_with_unreachable(const struct path *path, char *buf, int buflen)
  2178. {
  2179. char *res = buf + buflen;
  2180. struct path root;
  2181. struct path tmp;
  2182. int error;
  2183. if (path->dentry->d_op && path->dentry->d_op->d_dname)
  2184. return path->dentry->d_op->d_dname(path->dentry, buf, buflen);
  2185. get_fs_root(current->fs, &root);
  2186. write_seqlock(&rename_lock);
  2187. tmp = root;
  2188. error = path_with_deleted(path, &tmp, &res, &buflen);
  2189. if (!error && !path_equal(&tmp, &root))
  2190. error = prepend_unreachable(&res, &buflen);
  2191. write_sequnlock(&rename_lock);
  2192. path_put(&root);
  2193. if (error)
  2194. res = ERR_PTR(error);
  2195. return res;
  2196. }
  2197. /*
  2198. * Helper function for dentry_operations.d_dname() members
  2199. */
  2200. char *dynamic_dname(struct dentry *dentry, char *buffer, int buflen,
  2201. const char *fmt, ...)
  2202. {
  2203. va_list args;
  2204. char temp[64];
  2205. int sz;
  2206. va_start(args, fmt);
  2207. sz = vsnprintf(temp, sizeof(temp), fmt, args) + 1;
  2208. va_end(args);
  2209. if (sz > sizeof(temp) || sz > buflen)
  2210. return ERR_PTR(-ENAMETOOLONG);
  2211. buffer += buflen - sz;
  2212. return memcpy(buffer, temp, sz);
  2213. }
  2214. /*
  2215. * Write full pathname from the root of the filesystem into the buffer.
  2216. */
  2217. static char *__dentry_path(struct dentry *dentry, char *buf, int buflen)
  2218. {
  2219. char *end = buf + buflen;
  2220. char *retval;
  2221. prepend(&end, &buflen, "\0", 1);
  2222. if (buflen < 1)
  2223. goto Elong;
  2224. /* Get '/' right */
  2225. retval = end-1;
  2226. *retval = '/';
  2227. while (!IS_ROOT(dentry)) {
  2228. struct dentry *parent = dentry->d_parent;
  2229. int error;
  2230. prefetch(parent);
  2231. spin_lock(&dentry->d_lock);
  2232. error = prepend_name(&end, &buflen, &dentry->d_name);
  2233. spin_unlock(&dentry->d_lock);
  2234. if (error != 0 || prepend(&end, &buflen, "/", 1) != 0)
  2235. goto Elong;
  2236. retval = end;
  2237. dentry = parent;
  2238. }
  2239. return retval;
  2240. Elong:
  2241. return ERR_PTR(-ENAMETOOLONG);
  2242. }
  2243. char *dentry_path_raw(struct dentry *dentry, char *buf, int buflen)
  2244. {
  2245. char *retval;
  2246. write_seqlock(&rename_lock);
  2247. retval = __dentry_path(dentry, buf, buflen);
  2248. write_sequnlock(&rename_lock);
  2249. return retval;
  2250. }
  2251. EXPORT_SYMBOL(dentry_path_raw);
  2252. char *dentry_path(struct dentry *dentry, char *buf, int buflen)
  2253. {
  2254. char *p = NULL;
  2255. char *retval;
  2256. write_seqlock(&rename_lock);
  2257. if (d_unlinked(dentry)) {
  2258. p = buf + buflen;
  2259. if (prepend(&p, &buflen, "//deleted", 10) != 0)
  2260. goto Elong;
  2261. buflen++;
  2262. }
  2263. retval = __dentry_path(dentry, buf, buflen);
  2264. write_sequnlock(&rename_lock);
  2265. if (!IS_ERR(retval) && p)
  2266. *p = '/'; /* restore '/' overriden with '\0' */
  2267. return retval;
  2268. Elong:
  2269. return ERR_PTR(-ENAMETOOLONG);
  2270. }
  2271. /*
  2272. * NOTE! The user-level library version returns a
  2273. * character pointer. The kernel system call just
  2274. * returns the length of the buffer filled (which
  2275. * includes the ending '\0' character), or a negative
  2276. * error value. So libc would do something like
  2277. *
  2278. * char *getcwd(char * buf, size_t size)
  2279. * {
  2280. * int retval;
  2281. *
  2282. * retval = sys_getcwd(buf, size);
  2283. * if (retval >= 0)
  2284. * return buf;
  2285. * errno = -retval;
  2286. * return NULL;
  2287. * }
  2288. */
  2289. SYSCALL_DEFINE2(getcwd, char __user *, buf, unsigned long, size)
  2290. {
  2291. int error;
  2292. struct path pwd, root;
  2293. char *page = (char *) __get_free_page(GFP_USER);
  2294. if (!page)
  2295. return -ENOMEM;
  2296. get_fs_root_and_pwd(current->fs, &root, &pwd);
  2297. error = -ENOENT;
  2298. write_seqlock(&rename_lock);
  2299. if (!d_unlinked(pwd.dentry)) {
  2300. unsigned long len;
  2301. struct path tmp = root;
  2302. char *cwd = page + PAGE_SIZE;
  2303. int buflen = PAGE_SIZE;
  2304. prepend(&cwd, &buflen, "\0", 1);
  2305. error = prepend_path(&pwd, &tmp, &cwd, &buflen);
  2306. write_sequnlock(&rename_lock);
  2307. if (error)
  2308. goto out;
  2309. /* Unreachable from current root */
  2310. if (!path_equal(&tmp, &root)) {
  2311. error = prepend_unreachable(&cwd, &buflen);
  2312. if (error)
  2313. goto out;
  2314. }
  2315. error = -ERANGE;
  2316. len = PAGE_SIZE + page - cwd;
  2317. if (len <= size) {
  2318. error = len;
  2319. if (copy_to_user(buf, cwd, len))
  2320. error = -EFAULT;
  2321. }
  2322. } else {
  2323. write_sequnlock(&rename_lock);
  2324. }
  2325. out:
  2326. path_put(&pwd);
  2327. path_put(&root);
  2328. free_page((unsigned long) page);
  2329. return error;
  2330. }
  2331. /*
  2332. * Test whether new_dentry is a subdirectory of old_dentry.
  2333. *
  2334. * Trivially implemented using the dcache structure
  2335. */
  2336. /**
  2337. * is_subdir - is new dentry a subdirectory of old_dentry
  2338. * @new_dentry: new dentry
  2339. * @old_dentry: old dentry
  2340. *
  2341. * Returns 1 if new_dentry is a subdirectory of the parent (at any depth).
  2342. * Returns 0 otherwise.
  2343. * Caller must ensure that "new_dentry" is pinned before calling is_subdir()
  2344. */
  2345. int is_subdir(struct dentry *new_dentry, struct dentry *old_dentry)
  2346. {
  2347. int result;
  2348. unsigned seq;
  2349. if (new_dentry == old_dentry)
  2350. return 1;
  2351. do {
  2352. /* for restarting inner loop in case of seq retry */
  2353. seq = read_seqbegin(&rename_lock);
  2354. /*
  2355. * Need rcu_readlock to protect against the d_parent trashing
  2356. * due to d_move
  2357. */
  2358. rcu_read_lock();
  2359. if (d_ancestor(old_dentry, new_dentry))
  2360. result = 1;
  2361. else
  2362. result = 0;
  2363. rcu_read_unlock();
  2364. } while (read_seqretry(&rename_lock, seq));
  2365. return result;
  2366. }
  2367. int path_is_under(struct path *path1, struct path *path2)
  2368. {
  2369. struct vfsmount *mnt = path1->mnt;
  2370. struct dentry *dentry = path1->dentry;
  2371. int res;
  2372. br_read_lock(vfsmount_lock);
  2373. if (mnt != path2->mnt) {
  2374. for (;;) {
  2375. if (mnt->mnt_parent == mnt) {
  2376. br_read_unlock(vfsmount_lock);
  2377. return 0;
  2378. }
  2379. if (mnt->mnt_parent == path2->mnt)
  2380. break;
  2381. mnt = mnt->mnt_parent;
  2382. }
  2383. dentry = mnt->mnt_mountpoint;
  2384. }
  2385. res = is_subdir(dentry, path2->dentry);
  2386. br_read_unlock(vfsmount_lock);
  2387. return res;
  2388. }
  2389. EXPORT_SYMBOL(path_is_under);
  2390. void d_genocide(struct dentry *root)
  2391. {
  2392. struct dentry *this_parent;
  2393. struct list_head *next;
  2394. unsigned seq;
  2395. int locked = 0;
  2396. seq = read_seqbegin(&rename_lock);
  2397. again:
  2398. this_parent = root;
  2399. spin_lock(&this_parent->d_lock);
  2400. repeat:
  2401. next = this_parent->d_subdirs.next;
  2402. resume:
  2403. while (next != &this_parent->d_subdirs) {
  2404. struct list_head *tmp = next;
  2405. struct dentry *dentry = list_entry(tmp, struct dentry, d_u.d_child);
  2406. next = tmp->next;
  2407. spin_lock_nested(&dentry->d_lock, DENTRY_D_LOCK_NESTED);
  2408. if (d_unhashed(dentry) || !dentry->d_inode) {
  2409. spin_unlock(&dentry->d_lock);
  2410. continue;
  2411. }
  2412. if (!list_empty(&dentry->d_subdirs)) {
  2413. spin_unlock(&this_parent->d_lock);
  2414. spin_release(&dentry->d_lock.dep_map, 1, _RET_IP_);
  2415. this_parent = dentry;
  2416. spin_acquire(&this_parent->d_lock.dep_map, 0, 1, _RET_IP_);
  2417. goto repeat;
  2418. }
  2419. if (!(dentry->d_flags & DCACHE_GENOCIDE)) {
  2420. dentry->d_flags |= DCACHE_GENOCIDE;
  2421. dentry->d_count--;
  2422. }
  2423. spin_unlock(&dentry->d_lock);
  2424. }
  2425. if (this_parent != root) {
  2426. struct dentry *tmp;
  2427. struct dentry *child;
  2428. tmp = this_parent->d_parent;
  2429. if (!(this_parent->d_flags & DCACHE_GENOCIDE)) {
  2430. this_parent->d_flags |= DCACHE_GENOCIDE;
  2431. this_parent->d_count--;
  2432. }
  2433. rcu_read_lock();
  2434. spin_unlock(&this_parent->d_lock);
  2435. child = this_parent;
  2436. this_parent = tmp;
  2437. spin_lock(&this_parent->d_lock);
  2438. /* might go back up the wrong parent if we have had a rename
  2439. * or deletion */
  2440. if (this_parent != child->d_parent ||
  2441. (!locked && read_seqretry(&rename_lock, seq))) {
  2442. spin_unlock(&this_parent->d_lock);
  2443. rcu_read_unlock();
  2444. goto rename_retry;
  2445. }
  2446. rcu_read_unlock();
  2447. next = child->d_u.d_child.next;
  2448. goto resume;
  2449. }
  2450. spin_unlock(&this_parent->d_lock);
  2451. if (!locked && read_seqretry(&rename_lock, seq))
  2452. goto rename_retry;
  2453. if (locked)
  2454. write_sequnlock(&rename_lock);
  2455. return;
  2456. rename_retry:
  2457. locked = 1;
  2458. write_seqlock(&rename_lock);
  2459. goto again;
  2460. }
  2461. /**
  2462. * find_inode_number - check for dentry with name
  2463. * @dir: directory to check
  2464. * @name: Name to find.
  2465. *
  2466. * Check whether a dentry already exists for the given name,
  2467. * and return the inode number if it has an inode. Otherwise
  2468. * 0 is returned.
  2469. *
  2470. * This routine is used to post-process directory listings for
  2471. * filesystems using synthetic inode numbers, and is necessary
  2472. * to keep getcwd() working.
  2473. */
  2474. ino_t find_inode_number(struct dentry *dir, struct qstr *name)
  2475. {
  2476. struct dentry * dentry;
  2477. ino_t ino = 0;
  2478. dentry = d_hash_and_lookup(dir, name);
  2479. if (dentry) {
  2480. if (dentry->d_inode)
  2481. ino = dentry->d_inode->i_ino;
  2482. dput(dentry);
  2483. }
  2484. return ino;
  2485. }
  2486. EXPORT_SYMBOL(find_inode_number);
  2487. static __initdata unsigned long dhash_entries;
  2488. static int __init set_dhash_entries(char *str)
  2489. {
  2490. if (!str)
  2491. return 0;
  2492. dhash_entries = simple_strtoul(str, &str, 0);
  2493. return 1;
  2494. }
  2495. __setup("dhash_entries=", set_dhash_entries);
  2496. static void __init dcache_init_early(void)
  2497. {
  2498. int loop;
  2499. /* If hashes are distributed across NUMA nodes, defer
  2500. * hash allocation until vmalloc space is available.
  2501. */
  2502. if (hashdist)
  2503. return;
  2504. dentry_hashtable =
  2505. alloc_large_system_hash("Dentry cache",
  2506. sizeof(struct hlist_head),
  2507. dhash_entries,
  2508. 13,
  2509. HASH_EARLY,
  2510. &d_hash_shift,
  2511. &d_hash_mask,
  2512. 0);
  2513. for (loop = 0; loop < (1 << d_hash_shift); loop++)
  2514. INIT_HLIST_HEAD(&dentry_hashtable[loop]);
  2515. }
  2516. static void __init dcache_init(void)
  2517. {
  2518. int loop;
  2519. /*
  2520. * A constructor could be added for stable state like the lists,
  2521. * but it is probably not worth it because of the cache nature
  2522. * of the dcache.
  2523. */
  2524. dentry_cache = KMEM_CACHE(dentry,
  2525. SLAB_RECLAIM_ACCOUNT|SLAB_PANIC|SLAB_MEM_SPREAD);
  2526. register_shrinker(&dcache_shrinker);
  2527. /* Hash may have been set up in dcache_init_early */
  2528. if (!hashdist)
  2529. return;
  2530. dentry_hashtable =
  2531. alloc_large_system_hash("Dentry cache",
  2532. sizeof(struct hlist_head),
  2533. dhash_entries,
  2534. 13,
  2535. 0,
  2536. &d_hash_shift,
  2537. &d_hash_mask,
  2538. 0);
  2539. for (loop = 0; loop < (1 << d_hash_shift); loop++)
  2540. INIT_HLIST_HEAD(&dentry_hashtable[loop]);
  2541. }
  2542. /* SLAB cache for __getname() consumers */
  2543. struct kmem_cache *names_cachep __read_mostly;
  2544. EXPORT_SYMBOL(names_cachep);
  2545. EXPORT_SYMBOL(d_genocide);
  2546. void __init vfs_caches_init_early(void)
  2547. {
  2548. dcache_init_early();
  2549. inode_init_early();
  2550. }
  2551. void __init vfs_caches_init(unsigned long mempages)
  2552. {
  2553. unsigned long reserve;
  2554. /* Base hash sizes on available memory, with a reserve equal to
  2555. 150% of current kernel size */
  2556. reserve = min((mempages - nr_free_pages()) * 3/2, mempages - 1);
  2557. mempages -= reserve;
  2558. names_cachep = kmem_cache_create("names_cache", PATH_MAX, 0,
  2559. SLAB_HWCACHE_ALIGN|SLAB_PANIC, NULL);
  2560. dcache_init();
  2561. inode_init();
  2562. files_init(mempages);
  2563. mnt_init();
  2564. bdev_cache_init();
  2565. chrdev_init();
  2566. }