dcache.c 54 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099210021012102210321042105210621072108210921102111211221132114211521162117211821192120212121222123212421252126212721282129213021312132213321342135213621372138213921402141
  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/smp_lock.h>
  23. #include <linux/hash.h>
  24. #include <linux/cache.h>
  25. #include <linux/module.h>
  26. #include <linux/mount.h>
  27. #include <linux/file.h>
  28. #include <asm/uaccess.h>
  29. #include <linux/security.h>
  30. #include <linux/seqlock.h>
  31. #include <linux/swap.h>
  32. #include <linux/bootmem.h>
  33. #include "internal.h"
  34. int sysctl_vfs_cache_pressure __read_mostly = 100;
  35. EXPORT_SYMBOL_GPL(sysctl_vfs_cache_pressure);
  36. __cacheline_aligned_in_smp DEFINE_SPINLOCK(dcache_lock);
  37. static __cacheline_aligned_in_smp DEFINE_SEQLOCK(rename_lock);
  38. EXPORT_SYMBOL(dcache_lock);
  39. static struct kmem_cache *dentry_cache __read_mostly;
  40. #define DNAME_INLINE_LEN (sizeof(struct dentry)-offsetof(struct dentry,d_iname))
  41. /*
  42. * This is the single most critical data structure when it comes
  43. * to the dcache: the hashtable for lookups. Somebody should try
  44. * to make this good - I've just made it work.
  45. *
  46. * This hash-function tries to avoid losing too many bits of hash
  47. * information, yet avoid using a prime hash-size or similar.
  48. */
  49. #define D_HASHBITS d_hash_shift
  50. #define D_HASHMASK d_hash_mask
  51. static unsigned int d_hash_mask __read_mostly;
  52. static unsigned int d_hash_shift __read_mostly;
  53. static struct hlist_head *dentry_hashtable __read_mostly;
  54. static LIST_HEAD(dentry_unused);
  55. /* Statistics gathering. */
  56. struct dentry_stat_t dentry_stat = {
  57. .age_limit = 45,
  58. };
  59. static void __d_free(struct dentry *dentry)
  60. {
  61. if (dname_external(dentry))
  62. kfree(dentry->d_name.name);
  63. kmem_cache_free(dentry_cache, dentry);
  64. }
  65. static void d_callback(struct rcu_head *head)
  66. {
  67. struct dentry * dentry = container_of(head, struct dentry, d_u.d_rcu);
  68. __d_free(dentry);
  69. }
  70. /*
  71. * no dcache_lock, please. The caller must decrement dentry_stat.nr_dentry
  72. * inside dcache_lock.
  73. */
  74. static void d_free(struct dentry *dentry)
  75. {
  76. if (dentry->d_op && dentry->d_op->d_release)
  77. dentry->d_op->d_release(dentry);
  78. /* if dentry was never inserted into hash, immediate free is OK */
  79. if (dentry->d_hash.pprev == NULL)
  80. __d_free(dentry);
  81. else
  82. call_rcu(&dentry->d_u.d_rcu, d_callback);
  83. }
  84. /*
  85. * Release the dentry's inode, using the filesystem
  86. * d_iput() operation if defined.
  87. * Called with dcache_lock and per dentry lock held, drops both.
  88. */
  89. static void dentry_iput(struct dentry * dentry)
  90. {
  91. struct inode *inode = dentry->d_inode;
  92. if (inode) {
  93. dentry->d_inode = NULL;
  94. list_del_init(&dentry->d_alias);
  95. spin_unlock(&dentry->d_lock);
  96. spin_unlock(&dcache_lock);
  97. if (!inode->i_nlink)
  98. fsnotify_inoderemove(inode);
  99. if (dentry->d_op && dentry->d_op->d_iput)
  100. dentry->d_op->d_iput(dentry, inode);
  101. else
  102. iput(inode);
  103. } else {
  104. spin_unlock(&dentry->d_lock);
  105. spin_unlock(&dcache_lock);
  106. }
  107. }
  108. /*
  109. * This is dput
  110. *
  111. * This is complicated by the fact that we do not want to put
  112. * dentries that are no longer on any hash chain on the unused
  113. * list: we'd much rather just get rid of them immediately.
  114. *
  115. * However, that implies that we have to traverse the dentry
  116. * tree upwards to the parents which might _also_ now be
  117. * scheduled for deletion (it may have been only waiting for
  118. * its last child to go away).
  119. *
  120. * This tail recursion is done by hand as we don't want to depend
  121. * on the compiler to always get this right (gcc generally doesn't).
  122. * Real recursion would eat up our stack space.
  123. */
  124. /*
  125. * dput - release a dentry
  126. * @dentry: dentry to release
  127. *
  128. * Release a dentry. This will drop the usage count and if appropriate
  129. * call the dentry unlink method as well as removing it from the queues and
  130. * releasing its resources. If the parent dentries were scheduled for release
  131. * they too may now get deleted.
  132. *
  133. * no dcache lock, please.
  134. */
  135. void dput(struct dentry *dentry)
  136. {
  137. if (!dentry)
  138. return;
  139. repeat:
  140. if (atomic_read(&dentry->d_count) == 1)
  141. might_sleep();
  142. if (!atomic_dec_and_lock(&dentry->d_count, &dcache_lock))
  143. return;
  144. spin_lock(&dentry->d_lock);
  145. if (atomic_read(&dentry->d_count)) {
  146. spin_unlock(&dentry->d_lock);
  147. spin_unlock(&dcache_lock);
  148. return;
  149. }
  150. /*
  151. * AV: ->d_delete() is _NOT_ allowed to block now.
  152. */
  153. if (dentry->d_op && dentry->d_op->d_delete) {
  154. if (dentry->d_op->d_delete(dentry))
  155. goto unhash_it;
  156. }
  157. /* Unreachable? Get rid of it */
  158. if (d_unhashed(dentry))
  159. goto kill_it;
  160. if (list_empty(&dentry->d_lru)) {
  161. dentry->d_flags |= DCACHE_REFERENCED;
  162. list_add(&dentry->d_lru, &dentry_unused);
  163. dentry_stat.nr_unused++;
  164. }
  165. spin_unlock(&dentry->d_lock);
  166. spin_unlock(&dcache_lock);
  167. return;
  168. unhash_it:
  169. __d_drop(dentry);
  170. kill_it: {
  171. struct dentry *parent;
  172. /* If dentry was on d_lru list
  173. * delete it from there
  174. */
  175. if (!list_empty(&dentry->d_lru)) {
  176. list_del(&dentry->d_lru);
  177. dentry_stat.nr_unused--;
  178. }
  179. list_del(&dentry->d_u.d_child);
  180. dentry_stat.nr_dentry--; /* For d_free, below */
  181. /*drops the locks, at that point nobody can reach this dentry */
  182. dentry_iput(dentry);
  183. parent = dentry->d_parent;
  184. d_free(dentry);
  185. if (dentry == parent)
  186. return;
  187. dentry = parent;
  188. goto repeat;
  189. }
  190. }
  191. /**
  192. * d_invalidate - invalidate a dentry
  193. * @dentry: dentry to invalidate
  194. *
  195. * Try to invalidate the dentry if it turns out to be
  196. * possible. If there are other dentries that can be
  197. * reached through this one we can't delete it and we
  198. * return -EBUSY. On success we return 0.
  199. *
  200. * no dcache lock.
  201. */
  202. int d_invalidate(struct dentry * dentry)
  203. {
  204. /*
  205. * If it's already been dropped, return OK.
  206. */
  207. spin_lock(&dcache_lock);
  208. if (d_unhashed(dentry)) {
  209. spin_unlock(&dcache_lock);
  210. return 0;
  211. }
  212. /*
  213. * Check whether to do a partial shrink_dcache
  214. * to get rid of unused child entries.
  215. */
  216. if (!list_empty(&dentry->d_subdirs)) {
  217. spin_unlock(&dcache_lock);
  218. shrink_dcache_parent(dentry);
  219. spin_lock(&dcache_lock);
  220. }
  221. /*
  222. * Somebody else still using it?
  223. *
  224. * If it's a directory, we can't drop it
  225. * for fear of somebody re-populating it
  226. * with children (even though dropping it
  227. * would make it unreachable from the root,
  228. * we might still populate it if it was a
  229. * working directory or similar).
  230. */
  231. spin_lock(&dentry->d_lock);
  232. if (atomic_read(&dentry->d_count) > 1) {
  233. if (dentry->d_inode && S_ISDIR(dentry->d_inode->i_mode)) {
  234. spin_unlock(&dentry->d_lock);
  235. spin_unlock(&dcache_lock);
  236. return -EBUSY;
  237. }
  238. }
  239. __d_drop(dentry);
  240. spin_unlock(&dentry->d_lock);
  241. spin_unlock(&dcache_lock);
  242. return 0;
  243. }
  244. /* This should be called _only_ with dcache_lock held */
  245. static inline struct dentry * __dget_locked(struct dentry *dentry)
  246. {
  247. atomic_inc(&dentry->d_count);
  248. if (!list_empty(&dentry->d_lru)) {
  249. dentry_stat.nr_unused--;
  250. list_del_init(&dentry->d_lru);
  251. }
  252. return dentry;
  253. }
  254. struct dentry * dget_locked(struct dentry *dentry)
  255. {
  256. return __dget_locked(dentry);
  257. }
  258. /**
  259. * d_find_alias - grab a hashed alias of inode
  260. * @inode: inode in question
  261. * @want_discon: flag, used by d_splice_alias, to request
  262. * that only a DISCONNECTED alias be returned.
  263. *
  264. * If inode has a hashed alias, or is a directory and has any alias,
  265. * acquire the reference to alias and return it. Otherwise return NULL.
  266. * Notice that if inode is a directory there can be only one alias and
  267. * it can be unhashed only if it has no children, or if it is the root
  268. * of a filesystem.
  269. *
  270. * If the inode has an IS_ROOT, DCACHE_DISCONNECTED alias, then prefer
  271. * any other hashed alias over that one unless @want_discon is set,
  272. * in which case only return an IS_ROOT, DCACHE_DISCONNECTED alias.
  273. */
  274. static struct dentry * __d_find_alias(struct inode *inode, int want_discon)
  275. {
  276. struct list_head *head, *next, *tmp;
  277. struct dentry *alias, *discon_alias=NULL;
  278. head = &inode->i_dentry;
  279. next = inode->i_dentry.next;
  280. while (next != head) {
  281. tmp = next;
  282. next = tmp->next;
  283. prefetch(next);
  284. alias = list_entry(tmp, struct dentry, d_alias);
  285. if (S_ISDIR(inode->i_mode) || !d_unhashed(alias)) {
  286. if (IS_ROOT(alias) &&
  287. (alias->d_flags & DCACHE_DISCONNECTED))
  288. discon_alias = alias;
  289. else if (!want_discon) {
  290. __dget_locked(alias);
  291. return alias;
  292. }
  293. }
  294. }
  295. if (discon_alias)
  296. __dget_locked(discon_alias);
  297. return discon_alias;
  298. }
  299. struct dentry * d_find_alias(struct inode *inode)
  300. {
  301. struct dentry *de = NULL;
  302. if (!list_empty(&inode->i_dentry)) {
  303. spin_lock(&dcache_lock);
  304. de = __d_find_alias(inode, 0);
  305. spin_unlock(&dcache_lock);
  306. }
  307. return de;
  308. }
  309. /*
  310. * Try to kill dentries associated with this inode.
  311. * WARNING: you must own a reference to inode.
  312. */
  313. void d_prune_aliases(struct inode *inode)
  314. {
  315. struct dentry *dentry;
  316. restart:
  317. spin_lock(&dcache_lock);
  318. list_for_each_entry(dentry, &inode->i_dentry, d_alias) {
  319. spin_lock(&dentry->d_lock);
  320. if (!atomic_read(&dentry->d_count)) {
  321. __dget_locked(dentry);
  322. __d_drop(dentry);
  323. spin_unlock(&dentry->d_lock);
  324. spin_unlock(&dcache_lock);
  325. dput(dentry);
  326. goto restart;
  327. }
  328. spin_unlock(&dentry->d_lock);
  329. }
  330. spin_unlock(&dcache_lock);
  331. }
  332. /*
  333. * Throw away a dentry - free the inode, dput the parent. This requires that
  334. * the LRU list has already been removed.
  335. *
  336. * Called with dcache_lock, drops it and then regains.
  337. * Called with dentry->d_lock held, drops it.
  338. */
  339. static void prune_one_dentry(struct dentry * dentry)
  340. {
  341. struct dentry * parent;
  342. __d_drop(dentry);
  343. list_del(&dentry->d_u.d_child);
  344. dentry_stat.nr_dentry--; /* For d_free, below */
  345. dentry_iput(dentry);
  346. parent = dentry->d_parent;
  347. d_free(dentry);
  348. if (parent != dentry)
  349. dput(parent);
  350. spin_lock(&dcache_lock);
  351. }
  352. /**
  353. * prune_dcache - shrink the dcache
  354. * @count: number of entries to try and free
  355. * @sb: if given, ignore dentries for other superblocks
  356. * which are being unmounted.
  357. *
  358. * Shrink the dcache. This is done when we need
  359. * more memory, or simply when we need to unmount
  360. * something (at which point we need to unuse
  361. * all dentries).
  362. *
  363. * This function may fail to free any resources if
  364. * all the dentries are in use.
  365. */
  366. static void prune_dcache(int count, struct super_block *sb)
  367. {
  368. spin_lock(&dcache_lock);
  369. for (; count ; count--) {
  370. struct dentry *dentry;
  371. struct list_head *tmp;
  372. struct rw_semaphore *s_umount;
  373. cond_resched_lock(&dcache_lock);
  374. tmp = dentry_unused.prev;
  375. if (sb) {
  376. /* Try to find a dentry for this sb, but don't try
  377. * too hard, if they aren't near the tail they will
  378. * be moved down again soon
  379. */
  380. int skip = count;
  381. while (skip && tmp != &dentry_unused &&
  382. list_entry(tmp, struct dentry, d_lru)->d_sb != sb) {
  383. skip--;
  384. tmp = tmp->prev;
  385. }
  386. }
  387. if (tmp == &dentry_unused)
  388. break;
  389. list_del_init(tmp);
  390. prefetch(dentry_unused.prev);
  391. dentry_stat.nr_unused--;
  392. dentry = list_entry(tmp, struct dentry, d_lru);
  393. spin_lock(&dentry->d_lock);
  394. /*
  395. * We found an inuse dentry which was not removed from
  396. * dentry_unused because of laziness during lookup. Do not free
  397. * it - just keep it off the dentry_unused list.
  398. */
  399. if (atomic_read(&dentry->d_count)) {
  400. spin_unlock(&dentry->d_lock);
  401. continue;
  402. }
  403. /* If the dentry was recently referenced, don't free it. */
  404. if (dentry->d_flags & DCACHE_REFERENCED) {
  405. dentry->d_flags &= ~DCACHE_REFERENCED;
  406. list_add(&dentry->d_lru, &dentry_unused);
  407. dentry_stat.nr_unused++;
  408. spin_unlock(&dentry->d_lock);
  409. continue;
  410. }
  411. /*
  412. * If the dentry is not DCACHED_REFERENCED, it is time
  413. * to remove it from the dcache, provided the super block is
  414. * NULL (which means we are trying to reclaim memory)
  415. * or this dentry belongs to the same super block that
  416. * we want to shrink.
  417. */
  418. /*
  419. * If this dentry is for "my" filesystem, then I can prune it
  420. * without taking the s_umount lock (I already hold it).
  421. */
  422. if (sb && dentry->d_sb == sb) {
  423. prune_one_dentry(dentry);
  424. continue;
  425. }
  426. /*
  427. * ...otherwise we need to be sure this filesystem isn't being
  428. * unmounted, otherwise we could race with
  429. * generic_shutdown_super(), and end up holding a reference to
  430. * an inode while the filesystem is unmounted.
  431. * So we try to get s_umount, and make sure s_root isn't NULL.
  432. * (Take a local copy of s_umount to avoid a use-after-free of
  433. * `dentry').
  434. */
  435. s_umount = &dentry->d_sb->s_umount;
  436. if (down_read_trylock(s_umount)) {
  437. if (dentry->d_sb->s_root != NULL) {
  438. prune_one_dentry(dentry);
  439. up_read(s_umount);
  440. continue;
  441. }
  442. up_read(s_umount);
  443. }
  444. spin_unlock(&dentry->d_lock);
  445. /*
  446. * Insert dentry at the head of the list as inserting at the
  447. * tail leads to a cycle.
  448. */
  449. list_add(&dentry->d_lru, &dentry_unused);
  450. dentry_stat.nr_unused++;
  451. }
  452. spin_unlock(&dcache_lock);
  453. }
  454. /*
  455. * Shrink the dcache for the specified super block.
  456. * This allows us to unmount a device without disturbing
  457. * the dcache for the other devices.
  458. *
  459. * This implementation makes just two traversals of the
  460. * unused list. On the first pass we move the selected
  461. * dentries to the most recent end, and on the second
  462. * pass we free them. The second pass must restart after
  463. * each dput(), but since the target dentries are all at
  464. * the end, it's really just a single traversal.
  465. */
  466. /**
  467. * shrink_dcache_sb - shrink dcache for a superblock
  468. * @sb: superblock
  469. *
  470. * Shrink the dcache for the specified super block. This
  471. * is used to free the dcache before unmounting a file
  472. * system
  473. */
  474. void shrink_dcache_sb(struct super_block * sb)
  475. {
  476. struct list_head *tmp, *next;
  477. struct dentry *dentry;
  478. /*
  479. * Pass one ... move the dentries for the specified
  480. * superblock to the most recent end of the unused list.
  481. */
  482. spin_lock(&dcache_lock);
  483. list_for_each_safe(tmp, next, &dentry_unused) {
  484. dentry = list_entry(tmp, struct dentry, d_lru);
  485. if (dentry->d_sb != sb)
  486. continue;
  487. list_move(tmp, &dentry_unused);
  488. }
  489. /*
  490. * Pass two ... free the dentries for this superblock.
  491. */
  492. repeat:
  493. list_for_each_safe(tmp, next, &dentry_unused) {
  494. dentry = list_entry(tmp, struct dentry, d_lru);
  495. if (dentry->d_sb != sb)
  496. continue;
  497. dentry_stat.nr_unused--;
  498. list_del_init(tmp);
  499. spin_lock(&dentry->d_lock);
  500. if (atomic_read(&dentry->d_count)) {
  501. spin_unlock(&dentry->d_lock);
  502. continue;
  503. }
  504. prune_one_dentry(dentry);
  505. cond_resched_lock(&dcache_lock);
  506. goto repeat;
  507. }
  508. spin_unlock(&dcache_lock);
  509. }
  510. /*
  511. * destroy a single subtree of dentries for unmount
  512. * - see the comments on shrink_dcache_for_umount() for a description of the
  513. * locking
  514. */
  515. static void shrink_dcache_for_umount_subtree(struct dentry *dentry)
  516. {
  517. struct dentry *parent;
  518. unsigned detached = 0;
  519. BUG_ON(!IS_ROOT(dentry));
  520. /* detach this root from the system */
  521. spin_lock(&dcache_lock);
  522. if (!list_empty(&dentry->d_lru)) {
  523. dentry_stat.nr_unused--;
  524. list_del_init(&dentry->d_lru);
  525. }
  526. __d_drop(dentry);
  527. spin_unlock(&dcache_lock);
  528. for (;;) {
  529. /* descend to the first leaf in the current subtree */
  530. while (!list_empty(&dentry->d_subdirs)) {
  531. struct dentry *loop;
  532. /* this is a branch with children - detach all of them
  533. * from the system in one go */
  534. spin_lock(&dcache_lock);
  535. list_for_each_entry(loop, &dentry->d_subdirs,
  536. d_u.d_child) {
  537. if (!list_empty(&loop->d_lru)) {
  538. dentry_stat.nr_unused--;
  539. list_del_init(&loop->d_lru);
  540. }
  541. __d_drop(loop);
  542. cond_resched_lock(&dcache_lock);
  543. }
  544. spin_unlock(&dcache_lock);
  545. /* move to the first child */
  546. dentry = list_entry(dentry->d_subdirs.next,
  547. struct dentry, d_u.d_child);
  548. }
  549. /* consume the dentries from this leaf up through its parents
  550. * until we find one with children or run out altogether */
  551. do {
  552. struct inode *inode;
  553. if (atomic_read(&dentry->d_count) != 0) {
  554. printk(KERN_ERR
  555. "BUG: Dentry %p{i=%lx,n=%s}"
  556. " still in use (%d)"
  557. " [unmount of %s %s]\n",
  558. dentry,
  559. dentry->d_inode ?
  560. dentry->d_inode->i_ino : 0UL,
  561. dentry->d_name.name,
  562. atomic_read(&dentry->d_count),
  563. dentry->d_sb->s_type->name,
  564. dentry->d_sb->s_id);
  565. BUG();
  566. }
  567. parent = dentry->d_parent;
  568. if (parent == dentry)
  569. parent = NULL;
  570. else
  571. atomic_dec(&parent->d_count);
  572. list_del(&dentry->d_u.d_child);
  573. detached++;
  574. inode = dentry->d_inode;
  575. if (inode) {
  576. dentry->d_inode = NULL;
  577. list_del_init(&dentry->d_alias);
  578. if (dentry->d_op && dentry->d_op->d_iput)
  579. dentry->d_op->d_iput(dentry, inode);
  580. else
  581. iput(inode);
  582. }
  583. d_free(dentry);
  584. /* finished when we fall off the top of the tree,
  585. * otherwise we ascend to the parent and move to the
  586. * next sibling if there is one */
  587. if (!parent)
  588. goto out;
  589. dentry = parent;
  590. } while (list_empty(&dentry->d_subdirs));
  591. dentry = list_entry(dentry->d_subdirs.next,
  592. struct dentry, d_u.d_child);
  593. }
  594. out:
  595. /* several dentries were freed, need to correct nr_dentry */
  596. spin_lock(&dcache_lock);
  597. dentry_stat.nr_dentry -= detached;
  598. spin_unlock(&dcache_lock);
  599. }
  600. /*
  601. * destroy the dentries attached to a superblock on unmounting
  602. * - we don't need to use dentry->d_lock, and only need dcache_lock when
  603. * removing the dentry from the system lists and hashes because:
  604. * - the superblock is detached from all mountings and open files, so the
  605. * dentry trees will not be rearranged by the VFS
  606. * - s_umount is write-locked, so the memory pressure shrinker will ignore
  607. * any dentries belonging to this superblock that it comes across
  608. * - the filesystem itself is no longer permitted to rearrange the dentries
  609. * in this superblock
  610. */
  611. void shrink_dcache_for_umount(struct super_block *sb)
  612. {
  613. struct dentry *dentry;
  614. if (down_read_trylock(&sb->s_umount))
  615. BUG();
  616. dentry = sb->s_root;
  617. sb->s_root = NULL;
  618. atomic_dec(&dentry->d_count);
  619. shrink_dcache_for_umount_subtree(dentry);
  620. while (!hlist_empty(&sb->s_anon)) {
  621. dentry = hlist_entry(sb->s_anon.first, struct dentry, d_hash);
  622. shrink_dcache_for_umount_subtree(dentry);
  623. }
  624. }
  625. /*
  626. * Search for at least 1 mount point in the dentry's subdirs.
  627. * We descend to the next level whenever the d_subdirs
  628. * list is non-empty and continue searching.
  629. */
  630. /**
  631. * have_submounts - check for mounts over a dentry
  632. * @parent: dentry to check.
  633. *
  634. * Return true if the parent or its subdirectories contain
  635. * a mount point
  636. */
  637. int have_submounts(struct dentry *parent)
  638. {
  639. struct dentry *this_parent = parent;
  640. struct list_head *next;
  641. spin_lock(&dcache_lock);
  642. if (d_mountpoint(parent))
  643. goto positive;
  644. repeat:
  645. next = this_parent->d_subdirs.next;
  646. resume:
  647. while (next != &this_parent->d_subdirs) {
  648. struct list_head *tmp = next;
  649. struct dentry *dentry = list_entry(tmp, struct dentry, d_u.d_child);
  650. next = tmp->next;
  651. /* Have we found a mount point ? */
  652. if (d_mountpoint(dentry))
  653. goto positive;
  654. if (!list_empty(&dentry->d_subdirs)) {
  655. this_parent = dentry;
  656. goto repeat;
  657. }
  658. }
  659. /*
  660. * All done at this level ... ascend and resume the search.
  661. */
  662. if (this_parent != parent) {
  663. next = this_parent->d_u.d_child.next;
  664. this_parent = this_parent->d_parent;
  665. goto resume;
  666. }
  667. spin_unlock(&dcache_lock);
  668. return 0; /* No mount points found in tree */
  669. positive:
  670. spin_unlock(&dcache_lock);
  671. return 1;
  672. }
  673. /*
  674. * Search the dentry child list for the specified parent,
  675. * and move any unused dentries to the end of the unused
  676. * list for prune_dcache(). We descend to the next level
  677. * whenever the d_subdirs list is non-empty and continue
  678. * searching.
  679. *
  680. * It returns zero iff there are no unused children,
  681. * otherwise it returns the number of children moved to
  682. * the end of the unused list. This may not be the total
  683. * number of unused children, because select_parent can
  684. * drop the lock and return early due to latency
  685. * constraints.
  686. */
  687. static int select_parent(struct dentry * parent)
  688. {
  689. struct dentry *this_parent = parent;
  690. struct list_head *next;
  691. int found = 0;
  692. spin_lock(&dcache_lock);
  693. repeat:
  694. next = this_parent->d_subdirs.next;
  695. resume:
  696. while (next != &this_parent->d_subdirs) {
  697. struct list_head *tmp = next;
  698. struct dentry *dentry = list_entry(tmp, struct dentry, d_u.d_child);
  699. next = tmp->next;
  700. if (!list_empty(&dentry->d_lru)) {
  701. dentry_stat.nr_unused--;
  702. list_del_init(&dentry->d_lru);
  703. }
  704. /*
  705. * move only zero ref count dentries to the end
  706. * of the unused list for prune_dcache
  707. */
  708. if (!atomic_read(&dentry->d_count)) {
  709. list_add_tail(&dentry->d_lru, &dentry_unused);
  710. dentry_stat.nr_unused++;
  711. found++;
  712. }
  713. /*
  714. * We can return to the caller if we have found some (this
  715. * ensures forward progress). We'll be coming back to find
  716. * the rest.
  717. */
  718. if (found && need_resched())
  719. goto out;
  720. /*
  721. * Descend a level if the d_subdirs list is non-empty.
  722. */
  723. if (!list_empty(&dentry->d_subdirs)) {
  724. this_parent = dentry;
  725. goto repeat;
  726. }
  727. }
  728. /*
  729. * All done at this level ... ascend and resume the search.
  730. */
  731. if (this_parent != parent) {
  732. next = this_parent->d_u.d_child.next;
  733. this_parent = this_parent->d_parent;
  734. goto resume;
  735. }
  736. out:
  737. spin_unlock(&dcache_lock);
  738. return found;
  739. }
  740. /**
  741. * shrink_dcache_parent - prune dcache
  742. * @parent: parent of entries to prune
  743. *
  744. * Prune the dcache to remove unused children of the parent dentry.
  745. */
  746. void shrink_dcache_parent(struct dentry * parent)
  747. {
  748. int found;
  749. while ((found = select_parent(parent)) != 0)
  750. prune_dcache(found, parent->d_sb);
  751. }
  752. /*
  753. * Scan `nr' dentries and return the number which remain.
  754. *
  755. * We need to avoid reentering the filesystem if the caller is performing a
  756. * GFP_NOFS allocation attempt. One example deadlock is:
  757. *
  758. * ext2_new_block->getblk->GFP->shrink_dcache_memory->prune_dcache->
  759. * prune_one_dentry->dput->dentry_iput->iput->inode->i_sb->s_op->put_inode->
  760. * ext2_discard_prealloc->ext2_free_blocks->lock_super->DEADLOCK.
  761. *
  762. * In this case we return -1 to tell the caller that we baled.
  763. */
  764. static int shrink_dcache_memory(int nr, gfp_t gfp_mask)
  765. {
  766. if (nr) {
  767. if (!(gfp_mask & __GFP_FS))
  768. return -1;
  769. prune_dcache(nr, NULL);
  770. }
  771. return (dentry_stat.nr_unused / 100) * sysctl_vfs_cache_pressure;
  772. }
  773. /**
  774. * d_alloc - allocate a dcache entry
  775. * @parent: parent of entry to allocate
  776. * @name: qstr of the name
  777. *
  778. * Allocates a dentry. It returns %NULL if there is insufficient memory
  779. * available. On a success the dentry is returned. The name passed in is
  780. * copied and the copy passed in may be reused after this call.
  781. */
  782. struct dentry *d_alloc(struct dentry * parent, const struct qstr *name)
  783. {
  784. struct dentry *dentry;
  785. char *dname;
  786. dentry = kmem_cache_alloc(dentry_cache, GFP_KERNEL);
  787. if (!dentry)
  788. return NULL;
  789. if (name->len > DNAME_INLINE_LEN-1) {
  790. dname = kmalloc(name->len + 1, GFP_KERNEL);
  791. if (!dname) {
  792. kmem_cache_free(dentry_cache, dentry);
  793. return NULL;
  794. }
  795. } else {
  796. dname = dentry->d_iname;
  797. }
  798. dentry->d_name.name = dname;
  799. dentry->d_name.len = name->len;
  800. dentry->d_name.hash = name->hash;
  801. memcpy(dname, name->name, name->len);
  802. dname[name->len] = 0;
  803. atomic_set(&dentry->d_count, 1);
  804. dentry->d_flags = DCACHE_UNHASHED;
  805. spin_lock_init(&dentry->d_lock);
  806. dentry->d_inode = NULL;
  807. dentry->d_parent = NULL;
  808. dentry->d_sb = NULL;
  809. dentry->d_op = NULL;
  810. dentry->d_fsdata = NULL;
  811. dentry->d_mounted = 0;
  812. #ifdef CONFIG_PROFILING
  813. dentry->d_cookie = NULL;
  814. #endif
  815. INIT_HLIST_NODE(&dentry->d_hash);
  816. INIT_LIST_HEAD(&dentry->d_lru);
  817. INIT_LIST_HEAD(&dentry->d_subdirs);
  818. INIT_LIST_HEAD(&dentry->d_alias);
  819. if (parent) {
  820. dentry->d_parent = dget(parent);
  821. dentry->d_sb = parent->d_sb;
  822. } else {
  823. INIT_LIST_HEAD(&dentry->d_u.d_child);
  824. }
  825. spin_lock(&dcache_lock);
  826. if (parent)
  827. list_add(&dentry->d_u.d_child, &parent->d_subdirs);
  828. dentry_stat.nr_dentry++;
  829. spin_unlock(&dcache_lock);
  830. return dentry;
  831. }
  832. struct dentry *d_alloc_name(struct dentry *parent, const char *name)
  833. {
  834. struct qstr q;
  835. q.name = name;
  836. q.len = strlen(name);
  837. q.hash = full_name_hash(q.name, q.len);
  838. return d_alloc(parent, &q);
  839. }
  840. /**
  841. * d_instantiate - fill in inode information for a dentry
  842. * @entry: dentry to complete
  843. * @inode: inode to attach to this dentry
  844. *
  845. * Fill in inode information in the entry.
  846. *
  847. * This turns negative dentries into productive full members
  848. * of society.
  849. *
  850. * NOTE! This assumes that the inode count has been incremented
  851. * (or otherwise set) by the caller to indicate that it is now
  852. * in use by the dcache.
  853. */
  854. void d_instantiate(struct dentry *entry, struct inode * inode)
  855. {
  856. BUG_ON(!list_empty(&entry->d_alias));
  857. spin_lock(&dcache_lock);
  858. if (inode)
  859. list_add(&entry->d_alias, &inode->i_dentry);
  860. entry->d_inode = inode;
  861. fsnotify_d_instantiate(entry, inode);
  862. spin_unlock(&dcache_lock);
  863. security_d_instantiate(entry, inode);
  864. }
  865. /**
  866. * d_instantiate_unique - instantiate a non-aliased dentry
  867. * @entry: dentry to instantiate
  868. * @inode: inode to attach to this dentry
  869. *
  870. * Fill in inode information in the entry. On success, it returns NULL.
  871. * If an unhashed alias of "entry" already exists, then we return the
  872. * aliased dentry instead and drop one reference to inode.
  873. *
  874. * Note that in order to avoid conflicts with rename() etc, the caller
  875. * had better be holding the parent directory semaphore.
  876. *
  877. * This also assumes that the inode count has been incremented
  878. * (or otherwise set) by the caller to indicate that it is now
  879. * in use by the dcache.
  880. */
  881. static struct dentry *__d_instantiate_unique(struct dentry *entry,
  882. struct inode *inode)
  883. {
  884. struct dentry *alias;
  885. int len = entry->d_name.len;
  886. const char *name = entry->d_name.name;
  887. unsigned int hash = entry->d_name.hash;
  888. if (!inode) {
  889. entry->d_inode = NULL;
  890. return NULL;
  891. }
  892. list_for_each_entry(alias, &inode->i_dentry, d_alias) {
  893. struct qstr *qstr = &alias->d_name;
  894. if (qstr->hash != hash)
  895. continue;
  896. if (alias->d_parent != entry->d_parent)
  897. continue;
  898. if (qstr->len != len)
  899. continue;
  900. if (memcmp(qstr->name, name, len))
  901. continue;
  902. dget_locked(alias);
  903. return alias;
  904. }
  905. list_add(&entry->d_alias, &inode->i_dentry);
  906. entry->d_inode = inode;
  907. fsnotify_d_instantiate(entry, inode);
  908. return NULL;
  909. }
  910. struct dentry *d_instantiate_unique(struct dentry *entry, struct inode *inode)
  911. {
  912. struct dentry *result;
  913. BUG_ON(!list_empty(&entry->d_alias));
  914. spin_lock(&dcache_lock);
  915. result = __d_instantiate_unique(entry, inode);
  916. spin_unlock(&dcache_lock);
  917. if (!result) {
  918. security_d_instantiate(entry, inode);
  919. return NULL;
  920. }
  921. BUG_ON(!d_unhashed(result));
  922. iput(inode);
  923. return result;
  924. }
  925. EXPORT_SYMBOL(d_instantiate_unique);
  926. /**
  927. * d_alloc_root - allocate root dentry
  928. * @root_inode: inode to allocate the root for
  929. *
  930. * Allocate a root ("/") dentry for the inode given. The inode is
  931. * instantiated and returned. %NULL is returned if there is insufficient
  932. * memory or the inode passed is %NULL.
  933. */
  934. struct dentry * d_alloc_root(struct inode * root_inode)
  935. {
  936. struct dentry *res = NULL;
  937. if (root_inode) {
  938. static const struct qstr name = { .name = "/", .len = 1 };
  939. res = d_alloc(NULL, &name);
  940. if (res) {
  941. res->d_sb = root_inode->i_sb;
  942. res->d_parent = res;
  943. d_instantiate(res, root_inode);
  944. }
  945. }
  946. return res;
  947. }
  948. static inline struct hlist_head *d_hash(struct dentry *parent,
  949. unsigned long hash)
  950. {
  951. hash += ((unsigned long) parent ^ GOLDEN_RATIO_PRIME) / L1_CACHE_BYTES;
  952. hash = hash ^ ((hash ^ GOLDEN_RATIO_PRIME) >> D_HASHBITS);
  953. return dentry_hashtable + (hash & D_HASHMASK);
  954. }
  955. /**
  956. * d_alloc_anon - allocate an anonymous dentry
  957. * @inode: inode to allocate the dentry for
  958. *
  959. * This is similar to d_alloc_root. It is used by filesystems when
  960. * creating a dentry for a given inode, often in the process of
  961. * mapping a filehandle to a dentry. The returned dentry may be
  962. * anonymous, or may have a full name (if the inode was already
  963. * in the cache). The file system may need to make further
  964. * efforts to connect this dentry into the dcache properly.
  965. *
  966. * When called on a directory inode, we must ensure that
  967. * the inode only ever has one dentry. If a dentry is
  968. * found, that is returned instead of allocating a new one.
  969. *
  970. * On successful return, the reference to the inode has been transferred
  971. * to the dentry. If %NULL is returned (indicating kmalloc failure),
  972. * the reference on the inode has not been released.
  973. */
  974. struct dentry * d_alloc_anon(struct inode *inode)
  975. {
  976. static const struct qstr anonstring = { .name = "" };
  977. struct dentry *tmp;
  978. struct dentry *res;
  979. if ((res = d_find_alias(inode))) {
  980. iput(inode);
  981. return res;
  982. }
  983. tmp = d_alloc(NULL, &anonstring);
  984. if (!tmp)
  985. return NULL;
  986. tmp->d_parent = tmp; /* make sure dput doesn't croak */
  987. spin_lock(&dcache_lock);
  988. res = __d_find_alias(inode, 0);
  989. if (!res) {
  990. /* attach a disconnected dentry */
  991. res = tmp;
  992. tmp = NULL;
  993. spin_lock(&res->d_lock);
  994. res->d_sb = inode->i_sb;
  995. res->d_parent = res;
  996. res->d_inode = inode;
  997. res->d_flags |= DCACHE_DISCONNECTED;
  998. res->d_flags &= ~DCACHE_UNHASHED;
  999. list_add(&res->d_alias, &inode->i_dentry);
  1000. hlist_add_head(&res->d_hash, &inode->i_sb->s_anon);
  1001. spin_unlock(&res->d_lock);
  1002. inode = NULL; /* don't drop reference */
  1003. }
  1004. spin_unlock(&dcache_lock);
  1005. if (inode)
  1006. iput(inode);
  1007. if (tmp)
  1008. dput(tmp);
  1009. return res;
  1010. }
  1011. /**
  1012. * d_splice_alias - splice a disconnected dentry into the tree if one exists
  1013. * @inode: the inode which may have a disconnected dentry
  1014. * @dentry: a negative dentry which we want to point to the inode.
  1015. *
  1016. * If inode is a directory and has a 'disconnected' dentry (i.e. IS_ROOT and
  1017. * DCACHE_DISCONNECTED), then d_move that in place of the given dentry
  1018. * and return it, else simply d_add the inode to the dentry and return NULL.
  1019. *
  1020. * This is needed in the lookup routine of any filesystem that is exportable
  1021. * (via knfsd) so that we can build dcache paths to directories effectively.
  1022. *
  1023. * If a dentry was found and moved, then it is returned. Otherwise NULL
  1024. * is returned. This matches the expected return value of ->lookup.
  1025. *
  1026. */
  1027. struct dentry *d_splice_alias(struct inode *inode, struct dentry *dentry)
  1028. {
  1029. struct dentry *new = NULL;
  1030. if (inode && S_ISDIR(inode->i_mode)) {
  1031. spin_lock(&dcache_lock);
  1032. new = __d_find_alias(inode, 1);
  1033. if (new) {
  1034. BUG_ON(!(new->d_flags & DCACHE_DISCONNECTED));
  1035. fsnotify_d_instantiate(new, inode);
  1036. spin_unlock(&dcache_lock);
  1037. security_d_instantiate(new, inode);
  1038. d_rehash(dentry);
  1039. d_move(new, dentry);
  1040. iput(inode);
  1041. } else {
  1042. /* d_instantiate takes dcache_lock, so we do it by hand */
  1043. list_add(&dentry->d_alias, &inode->i_dentry);
  1044. dentry->d_inode = inode;
  1045. fsnotify_d_instantiate(dentry, inode);
  1046. spin_unlock(&dcache_lock);
  1047. security_d_instantiate(dentry, inode);
  1048. d_rehash(dentry);
  1049. }
  1050. } else
  1051. d_add(dentry, inode);
  1052. return new;
  1053. }
  1054. /**
  1055. * d_lookup - search for a dentry
  1056. * @parent: parent dentry
  1057. * @name: qstr of name we wish to find
  1058. *
  1059. * Searches the children of the parent dentry for the name in question. If
  1060. * the dentry is found its reference count is incremented and the dentry
  1061. * is returned. The caller must use d_put to free the entry when it has
  1062. * finished using it. %NULL is returned on failure.
  1063. *
  1064. * __d_lookup is dcache_lock free. The hash list is protected using RCU.
  1065. * Memory barriers are used while updating and doing lockless traversal.
  1066. * To avoid races with d_move while rename is happening, d_lock is used.
  1067. *
  1068. * Overflows in memcmp(), while d_move, are avoided by keeping the length
  1069. * and name pointer in one structure pointed by d_qstr.
  1070. *
  1071. * rcu_read_lock() and rcu_read_unlock() are used to disable preemption while
  1072. * lookup is going on.
  1073. *
  1074. * dentry_unused list is not updated even if lookup finds the required dentry
  1075. * in there. It is updated in places such as prune_dcache, shrink_dcache_sb,
  1076. * select_parent and __dget_locked. This laziness saves lookup from dcache_lock
  1077. * acquisition.
  1078. *
  1079. * d_lookup() is protected against the concurrent renames in some unrelated
  1080. * directory using the seqlockt_t rename_lock.
  1081. */
  1082. struct dentry * d_lookup(struct dentry * parent, struct qstr * name)
  1083. {
  1084. struct dentry * dentry = NULL;
  1085. unsigned long seq;
  1086. do {
  1087. seq = read_seqbegin(&rename_lock);
  1088. dentry = __d_lookup(parent, name);
  1089. if (dentry)
  1090. break;
  1091. } while (read_seqretry(&rename_lock, seq));
  1092. return dentry;
  1093. }
  1094. struct dentry * __d_lookup(struct dentry * parent, struct qstr * name)
  1095. {
  1096. unsigned int len = name->len;
  1097. unsigned int hash = name->hash;
  1098. const unsigned char *str = name->name;
  1099. struct hlist_head *head = d_hash(parent,hash);
  1100. struct dentry *found = NULL;
  1101. struct hlist_node *node;
  1102. struct dentry *dentry;
  1103. rcu_read_lock();
  1104. hlist_for_each_entry_rcu(dentry, node, head, d_hash) {
  1105. struct qstr *qstr;
  1106. if (dentry->d_name.hash != hash)
  1107. continue;
  1108. if (dentry->d_parent != parent)
  1109. continue;
  1110. spin_lock(&dentry->d_lock);
  1111. /*
  1112. * Recheck the dentry after taking the lock - d_move may have
  1113. * changed things. Don't bother checking the hash because we're
  1114. * about to compare the whole name anyway.
  1115. */
  1116. if (dentry->d_parent != parent)
  1117. goto next;
  1118. /*
  1119. * It is safe to compare names since d_move() cannot
  1120. * change the qstr (protected by d_lock).
  1121. */
  1122. qstr = &dentry->d_name;
  1123. if (parent->d_op && parent->d_op->d_compare) {
  1124. if (parent->d_op->d_compare(parent, qstr, name))
  1125. goto next;
  1126. } else {
  1127. if (qstr->len != len)
  1128. goto next;
  1129. if (memcmp(qstr->name, str, len))
  1130. goto next;
  1131. }
  1132. if (!d_unhashed(dentry)) {
  1133. atomic_inc(&dentry->d_count);
  1134. found = dentry;
  1135. }
  1136. spin_unlock(&dentry->d_lock);
  1137. break;
  1138. next:
  1139. spin_unlock(&dentry->d_lock);
  1140. }
  1141. rcu_read_unlock();
  1142. return found;
  1143. }
  1144. /**
  1145. * d_hash_and_lookup - hash the qstr then search for a dentry
  1146. * @dir: Directory to search in
  1147. * @name: qstr of name we wish to find
  1148. *
  1149. * On hash failure or on lookup failure NULL is returned.
  1150. */
  1151. struct dentry *d_hash_and_lookup(struct dentry *dir, struct qstr *name)
  1152. {
  1153. struct dentry *dentry = NULL;
  1154. /*
  1155. * Check for a fs-specific hash function. Note that we must
  1156. * calculate the standard hash first, as the d_op->d_hash()
  1157. * routine may choose to leave the hash value unchanged.
  1158. */
  1159. name->hash = full_name_hash(name->name, name->len);
  1160. if (dir->d_op && dir->d_op->d_hash) {
  1161. if (dir->d_op->d_hash(dir, name) < 0)
  1162. goto out;
  1163. }
  1164. dentry = d_lookup(dir, name);
  1165. out:
  1166. return dentry;
  1167. }
  1168. /**
  1169. * d_validate - verify dentry provided from insecure source
  1170. * @dentry: The dentry alleged to be valid child of @dparent
  1171. * @dparent: The parent dentry (known to be valid)
  1172. * @hash: Hash of the dentry
  1173. * @len: Length of the name
  1174. *
  1175. * An insecure source has sent us a dentry, here we verify it and dget() it.
  1176. * This is used by ncpfs in its readdir implementation.
  1177. * Zero is returned in the dentry is invalid.
  1178. */
  1179. int d_validate(struct dentry *dentry, struct dentry *dparent)
  1180. {
  1181. struct hlist_head *base;
  1182. struct hlist_node *lhp;
  1183. /* Check whether the ptr might be valid at all.. */
  1184. if (!kmem_ptr_validate(dentry_cache, dentry))
  1185. goto out;
  1186. if (dentry->d_parent != dparent)
  1187. goto out;
  1188. spin_lock(&dcache_lock);
  1189. base = d_hash(dparent, dentry->d_name.hash);
  1190. hlist_for_each(lhp,base) {
  1191. /* hlist_for_each_entry_rcu() not required for d_hash list
  1192. * as it is parsed under dcache_lock
  1193. */
  1194. if (dentry == hlist_entry(lhp, struct dentry, d_hash)) {
  1195. __dget_locked(dentry);
  1196. spin_unlock(&dcache_lock);
  1197. return 1;
  1198. }
  1199. }
  1200. spin_unlock(&dcache_lock);
  1201. out:
  1202. return 0;
  1203. }
  1204. /*
  1205. * When a file is deleted, we have two options:
  1206. * - turn this dentry into a negative dentry
  1207. * - unhash this dentry and free it.
  1208. *
  1209. * Usually, we want to just turn this into
  1210. * a negative dentry, but if anybody else is
  1211. * currently using the dentry or the inode
  1212. * we can't do that and we fall back on removing
  1213. * it from the hash queues and waiting for
  1214. * it to be deleted later when it has no users
  1215. */
  1216. /**
  1217. * d_delete - delete a dentry
  1218. * @dentry: The dentry to delete
  1219. *
  1220. * Turn the dentry into a negative dentry if possible, otherwise
  1221. * remove it from the hash queues so it can be deleted later
  1222. */
  1223. void d_delete(struct dentry * dentry)
  1224. {
  1225. int isdir = 0;
  1226. /*
  1227. * Are we the only user?
  1228. */
  1229. spin_lock(&dcache_lock);
  1230. spin_lock(&dentry->d_lock);
  1231. isdir = S_ISDIR(dentry->d_inode->i_mode);
  1232. if (atomic_read(&dentry->d_count) == 1) {
  1233. dentry_iput(dentry);
  1234. fsnotify_nameremove(dentry, isdir);
  1235. /* remove this and other inotify debug checks after 2.6.18 */
  1236. dentry->d_flags &= ~DCACHE_INOTIFY_PARENT_WATCHED;
  1237. return;
  1238. }
  1239. if (!d_unhashed(dentry))
  1240. __d_drop(dentry);
  1241. spin_unlock(&dentry->d_lock);
  1242. spin_unlock(&dcache_lock);
  1243. fsnotify_nameremove(dentry, isdir);
  1244. }
  1245. static void __d_rehash(struct dentry * entry, struct hlist_head *list)
  1246. {
  1247. entry->d_flags &= ~DCACHE_UNHASHED;
  1248. hlist_add_head_rcu(&entry->d_hash, list);
  1249. }
  1250. static void _d_rehash(struct dentry * entry)
  1251. {
  1252. __d_rehash(entry, d_hash(entry->d_parent, entry->d_name.hash));
  1253. }
  1254. /**
  1255. * d_rehash - add an entry back to the hash
  1256. * @entry: dentry to add to the hash
  1257. *
  1258. * Adds a dentry to the hash according to its name.
  1259. */
  1260. void d_rehash(struct dentry * entry)
  1261. {
  1262. spin_lock(&dcache_lock);
  1263. spin_lock(&entry->d_lock);
  1264. _d_rehash(entry);
  1265. spin_unlock(&entry->d_lock);
  1266. spin_unlock(&dcache_lock);
  1267. }
  1268. #define do_switch(x,y) do { \
  1269. __typeof__ (x) __tmp = x; \
  1270. x = y; y = __tmp; } while (0)
  1271. /*
  1272. * When switching names, the actual string doesn't strictly have to
  1273. * be preserved in the target - because we're dropping the target
  1274. * anyway. As such, we can just do a simple memcpy() to copy over
  1275. * the new name before we switch.
  1276. *
  1277. * Note that we have to be a lot more careful about getting the hash
  1278. * switched - we have to switch the hash value properly even if it
  1279. * then no longer matches the actual (corrupted) string of the target.
  1280. * The hash value has to match the hash queue that the dentry is on..
  1281. */
  1282. static void switch_names(struct dentry *dentry, struct dentry *target)
  1283. {
  1284. if (dname_external(target)) {
  1285. if (dname_external(dentry)) {
  1286. /*
  1287. * Both external: swap the pointers
  1288. */
  1289. do_switch(target->d_name.name, dentry->d_name.name);
  1290. } else {
  1291. /*
  1292. * dentry:internal, target:external. Steal target's
  1293. * storage and make target internal.
  1294. */
  1295. dentry->d_name.name = target->d_name.name;
  1296. target->d_name.name = target->d_iname;
  1297. }
  1298. } else {
  1299. if (dname_external(dentry)) {
  1300. /*
  1301. * dentry:external, target:internal. Give dentry's
  1302. * storage to target and make dentry internal
  1303. */
  1304. memcpy(dentry->d_iname, target->d_name.name,
  1305. target->d_name.len + 1);
  1306. target->d_name.name = dentry->d_name.name;
  1307. dentry->d_name.name = dentry->d_iname;
  1308. } else {
  1309. /*
  1310. * Both are internal. Just copy target to dentry
  1311. */
  1312. memcpy(dentry->d_iname, target->d_name.name,
  1313. target->d_name.len + 1);
  1314. }
  1315. }
  1316. }
  1317. /*
  1318. * We cannibalize "target" when moving dentry on top of it,
  1319. * because it's going to be thrown away anyway. We could be more
  1320. * polite about it, though.
  1321. *
  1322. * This forceful removal will result in ugly /proc output if
  1323. * somebody holds a file open that got deleted due to a rename.
  1324. * We could be nicer about the deleted file, and let it show
  1325. * up under the name it got deleted rather than the name that
  1326. * deleted it.
  1327. */
  1328. /*
  1329. * d_move_locked - move a dentry
  1330. * @dentry: entry to move
  1331. * @target: new dentry
  1332. *
  1333. * Update the dcache to reflect the move of a file name. Negative
  1334. * dcache entries should not be moved in this way.
  1335. */
  1336. static void d_move_locked(struct dentry * dentry, struct dentry * target)
  1337. {
  1338. struct hlist_head *list;
  1339. if (!dentry->d_inode)
  1340. printk(KERN_WARNING "VFS: moving negative dcache entry\n");
  1341. write_seqlock(&rename_lock);
  1342. /*
  1343. * XXXX: do we really need to take target->d_lock?
  1344. */
  1345. if (target < dentry) {
  1346. spin_lock(&target->d_lock);
  1347. spin_lock_nested(&dentry->d_lock, DENTRY_D_LOCK_NESTED);
  1348. } else {
  1349. spin_lock(&dentry->d_lock);
  1350. spin_lock_nested(&target->d_lock, DENTRY_D_LOCK_NESTED);
  1351. }
  1352. /* Move the dentry to the target hash queue, if on different bucket */
  1353. if (dentry->d_flags & DCACHE_UNHASHED)
  1354. goto already_unhashed;
  1355. hlist_del_rcu(&dentry->d_hash);
  1356. already_unhashed:
  1357. list = d_hash(target->d_parent, target->d_name.hash);
  1358. __d_rehash(dentry, list);
  1359. /* Unhash the target: dput() will then get rid of it */
  1360. __d_drop(target);
  1361. list_del(&dentry->d_u.d_child);
  1362. list_del(&target->d_u.d_child);
  1363. /* Switch the names.. */
  1364. switch_names(dentry, target);
  1365. do_switch(dentry->d_name.len, target->d_name.len);
  1366. do_switch(dentry->d_name.hash, target->d_name.hash);
  1367. /* ... and switch the parents */
  1368. if (IS_ROOT(dentry)) {
  1369. dentry->d_parent = target->d_parent;
  1370. target->d_parent = target;
  1371. INIT_LIST_HEAD(&target->d_u.d_child);
  1372. } else {
  1373. do_switch(dentry->d_parent, target->d_parent);
  1374. /* And add them back to the (new) parent lists */
  1375. list_add(&target->d_u.d_child, &target->d_parent->d_subdirs);
  1376. }
  1377. list_add(&dentry->d_u.d_child, &dentry->d_parent->d_subdirs);
  1378. spin_unlock(&target->d_lock);
  1379. fsnotify_d_move(dentry);
  1380. spin_unlock(&dentry->d_lock);
  1381. write_sequnlock(&rename_lock);
  1382. }
  1383. /**
  1384. * d_move - move a dentry
  1385. * @dentry: entry to move
  1386. * @target: new dentry
  1387. *
  1388. * Update the dcache to reflect the move of a file name. Negative
  1389. * dcache entries should not be moved in this way.
  1390. */
  1391. void d_move(struct dentry * dentry, struct dentry * target)
  1392. {
  1393. spin_lock(&dcache_lock);
  1394. d_move_locked(dentry, target);
  1395. spin_unlock(&dcache_lock);
  1396. }
  1397. /*
  1398. * Helper that returns 1 if p1 is a parent of p2, else 0
  1399. */
  1400. static int d_isparent(struct dentry *p1, struct dentry *p2)
  1401. {
  1402. struct dentry *p;
  1403. for (p = p2; p->d_parent != p; p = p->d_parent) {
  1404. if (p->d_parent == p1)
  1405. return 1;
  1406. }
  1407. return 0;
  1408. }
  1409. /*
  1410. * This helper attempts to cope with remotely renamed directories
  1411. *
  1412. * It assumes that the caller is already holding
  1413. * dentry->d_parent->d_inode->i_mutex and the dcache_lock
  1414. *
  1415. * Note: If ever the locking in lock_rename() changes, then please
  1416. * remember to update this too...
  1417. *
  1418. * On return, dcache_lock will have been unlocked.
  1419. */
  1420. static struct dentry *__d_unalias(struct dentry *dentry, struct dentry *alias)
  1421. {
  1422. struct mutex *m1 = NULL, *m2 = NULL;
  1423. struct dentry *ret;
  1424. /* If alias and dentry share a parent, then no extra locks required */
  1425. if (alias->d_parent == dentry->d_parent)
  1426. goto out_unalias;
  1427. /* Check for loops */
  1428. ret = ERR_PTR(-ELOOP);
  1429. if (d_isparent(alias, dentry))
  1430. goto out_err;
  1431. /* See lock_rename() */
  1432. ret = ERR_PTR(-EBUSY);
  1433. if (!mutex_trylock(&dentry->d_sb->s_vfs_rename_mutex))
  1434. goto out_err;
  1435. m1 = &dentry->d_sb->s_vfs_rename_mutex;
  1436. if (!mutex_trylock(&alias->d_parent->d_inode->i_mutex))
  1437. goto out_err;
  1438. m2 = &alias->d_parent->d_inode->i_mutex;
  1439. out_unalias:
  1440. d_move_locked(alias, dentry);
  1441. ret = alias;
  1442. out_err:
  1443. spin_unlock(&dcache_lock);
  1444. if (m2)
  1445. mutex_unlock(m2);
  1446. if (m1)
  1447. mutex_unlock(m1);
  1448. return ret;
  1449. }
  1450. /*
  1451. * Prepare an anonymous dentry for life in the superblock's dentry tree as a
  1452. * named dentry in place of the dentry to be replaced.
  1453. */
  1454. static void __d_materialise_dentry(struct dentry *dentry, struct dentry *anon)
  1455. {
  1456. struct dentry *dparent, *aparent;
  1457. switch_names(dentry, anon);
  1458. do_switch(dentry->d_name.len, anon->d_name.len);
  1459. do_switch(dentry->d_name.hash, anon->d_name.hash);
  1460. dparent = dentry->d_parent;
  1461. aparent = anon->d_parent;
  1462. dentry->d_parent = (aparent == anon) ? dentry : aparent;
  1463. list_del(&dentry->d_u.d_child);
  1464. if (!IS_ROOT(dentry))
  1465. list_add(&dentry->d_u.d_child, &dentry->d_parent->d_subdirs);
  1466. else
  1467. INIT_LIST_HEAD(&dentry->d_u.d_child);
  1468. anon->d_parent = (dparent == dentry) ? anon : dparent;
  1469. list_del(&anon->d_u.d_child);
  1470. if (!IS_ROOT(anon))
  1471. list_add(&anon->d_u.d_child, &anon->d_parent->d_subdirs);
  1472. else
  1473. INIT_LIST_HEAD(&anon->d_u.d_child);
  1474. anon->d_flags &= ~DCACHE_DISCONNECTED;
  1475. }
  1476. /**
  1477. * d_materialise_unique - introduce an inode into the tree
  1478. * @dentry: candidate dentry
  1479. * @inode: inode to bind to the dentry, to which aliases may be attached
  1480. *
  1481. * Introduces an dentry into the tree, substituting an extant disconnected
  1482. * root directory alias in its place if there is one
  1483. */
  1484. struct dentry *d_materialise_unique(struct dentry *dentry, struct inode *inode)
  1485. {
  1486. struct dentry *actual;
  1487. BUG_ON(!d_unhashed(dentry));
  1488. spin_lock(&dcache_lock);
  1489. if (!inode) {
  1490. actual = dentry;
  1491. dentry->d_inode = NULL;
  1492. goto found_lock;
  1493. }
  1494. if (S_ISDIR(inode->i_mode)) {
  1495. struct dentry *alias;
  1496. /* Does an aliased dentry already exist? */
  1497. alias = __d_find_alias(inode, 0);
  1498. if (alias) {
  1499. actual = alias;
  1500. /* Is this an anonymous mountpoint that we could splice
  1501. * into our tree? */
  1502. if (IS_ROOT(alias)) {
  1503. spin_lock(&alias->d_lock);
  1504. __d_materialise_dentry(dentry, alias);
  1505. __d_drop(alias);
  1506. goto found;
  1507. }
  1508. /* Nope, but we must(!) avoid directory aliasing */
  1509. actual = __d_unalias(dentry, alias);
  1510. if (IS_ERR(actual))
  1511. dput(alias);
  1512. goto out_nolock;
  1513. }
  1514. }
  1515. /* Add a unique reference */
  1516. actual = __d_instantiate_unique(dentry, inode);
  1517. if (!actual)
  1518. actual = dentry;
  1519. else if (unlikely(!d_unhashed(actual)))
  1520. goto shouldnt_be_hashed;
  1521. found_lock:
  1522. spin_lock(&actual->d_lock);
  1523. found:
  1524. _d_rehash(actual);
  1525. spin_unlock(&actual->d_lock);
  1526. spin_unlock(&dcache_lock);
  1527. out_nolock:
  1528. if (actual == dentry) {
  1529. security_d_instantiate(dentry, inode);
  1530. return NULL;
  1531. }
  1532. iput(inode);
  1533. return actual;
  1534. shouldnt_be_hashed:
  1535. spin_unlock(&dcache_lock);
  1536. BUG();
  1537. goto shouldnt_be_hashed;
  1538. }
  1539. /**
  1540. * d_path - return the path of a dentry
  1541. * @dentry: dentry to report
  1542. * @vfsmnt: vfsmnt to which the dentry belongs
  1543. * @root: root dentry
  1544. * @rootmnt: vfsmnt to which the root dentry belongs
  1545. * @buffer: buffer to return value in
  1546. * @buflen: buffer length
  1547. *
  1548. * Convert a dentry into an ASCII path name. If the entry has been deleted
  1549. * the string " (deleted)" is appended. Note that this is ambiguous.
  1550. *
  1551. * Returns the buffer or an error code if the path was too long.
  1552. *
  1553. * "buflen" should be positive. Caller holds the dcache_lock.
  1554. */
  1555. static char * __d_path( struct dentry *dentry, struct vfsmount *vfsmnt,
  1556. struct dentry *root, struct vfsmount *rootmnt,
  1557. char *buffer, int buflen)
  1558. {
  1559. char * end = buffer+buflen;
  1560. char * retval;
  1561. int namelen;
  1562. *--end = '\0';
  1563. buflen--;
  1564. if (!IS_ROOT(dentry) && d_unhashed(dentry)) {
  1565. buflen -= 10;
  1566. end -= 10;
  1567. if (buflen < 0)
  1568. goto Elong;
  1569. memcpy(end, " (deleted)", 10);
  1570. }
  1571. if (buflen < 1)
  1572. goto Elong;
  1573. /* Get '/' right */
  1574. retval = end-1;
  1575. *retval = '/';
  1576. for (;;) {
  1577. struct dentry * parent;
  1578. if (dentry == root && vfsmnt == rootmnt)
  1579. break;
  1580. if (dentry == vfsmnt->mnt_root || IS_ROOT(dentry)) {
  1581. /* Global root? */
  1582. spin_lock(&vfsmount_lock);
  1583. if (vfsmnt->mnt_parent == vfsmnt) {
  1584. spin_unlock(&vfsmount_lock);
  1585. goto global_root;
  1586. }
  1587. dentry = vfsmnt->mnt_mountpoint;
  1588. vfsmnt = vfsmnt->mnt_parent;
  1589. spin_unlock(&vfsmount_lock);
  1590. continue;
  1591. }
  1592. parent = dentry->d_parent;
  1593. prefetch(parent);
  1594. namelen = dentry->d_name.len;
  1595. buflen -= namelen + 1;
  1596. if (buflen < 0)
  1597. goto Elong;
  1598. end -= namelen;
  1599. memcpy(end, dentry->d_name.name, namelen);
  1600. *--end = '/';
  1601. retval = end;
  1602. dentry = parent;
  1603. }
  1604. return retval;
  1605. global_root:
  1606. namelen = dentry->d_name.len;
  1607. buflen -= namelen;
  1608. if (buflen < 0)
  1609. goto Elong;
  1610. retval -= namelen-1; /* hit the slash */
  1611. memcpy(retval, dentry->d_name.name, namelen);
  1612. return retval;
  1613. Elong:
  1614. return ERR_PTR(-ENAMETOOLONG);
  1615. }
  1616. /* write full pathname into buffer and return start of pathname */
  1617. char * d_path(struct dentry *dentry, struct vfsmount *vfsmnt,
  1618. char *buf, int buflen)
  1619. {
  1620. char *res;
  1621. struct vfsmount *rootmnt;
  1622. struct dentry *root;
  1623. read_lock(&current->fs->lock);
  1624. rootmnt = mntget(current->fs->rootmnt);
  1625. root = dget(current->fs->root);
  1626. read_unlock(&current->fs->lock);
  1627. spin_lock(&dcache_lock);
  1628. res = __d_path(dentry, vfsmnt, root, rootmnt, buf, buflen);
  1629. spin_unlock(&dcache_lock);
  1630. dput(root);
  1631. mntput(rootmnt);
  1632. return res;
  1633. }
  1634. /*
  1635. * NOTE! The user-level library version returns a
  1636. * character pointer. The kernel system call just
  1637. * returns the length of the buffer filled (which
  1638. * includes the ending '\0' character), or a negative
  1639. * error value. So libc would do something like
  1640. *
  1641. * char *getcwd(char * buf, size_t size)
  1642. * {
  1643. * int retval;
  1644. *
  1645. * retval = sys_getcwd(buf, size);
  1646. * if (retval >= 0)
  1647. * return buf;
  1648. * errno = -retval;
  1649. * return NULL;
  1650. * }
  1651. */
  1652. asmlinkage long sys_getcwd(char __user *buf, unsigned long size)
  1653. {
  1654. int error;
  1655. struct vfsmount *pwdmnt, *rootmnt;
  1656. struct dentry *pwd, *root;
  1657. char *page = (char *) __get_free_page(GFP_USER);
  1658. if (!page)
  1659. return -ENOMEM;
  1660. read_lock(&current->fs->lock);
  1661. pwdmnt = mntget(current->fs->pwdmnt);
  1662. pwd = dget(current->fs->pwd);
  1663. rootmnt = mntget(current->fs->rootmnt);
  1664. root = dget(current->fs->root);
  1665. read_unlock(&current->fs->lock);
  1666. error = -ENOENT;
  1667. /* Has the current directory has been unlinked? */
  1668. spin_lock(&dcache_lock);
  1669. if (pwd->d_parent == pwd || !d_unhashed(pwd)) {
  1670. unsigned long len;
  1671. char * cwd;
  1672. cwd = __d_path(pwd, pwdmnt, root, rootmnt, page, PAGE_SIZE);
  1673. spin_unlock(&dcache_lock);
  1674. error = PTR_ERR(cwd);
  1675. if (IS_ERR(cwd))
  1676. goto out;
  1677. error = -ERANGE;
  1678. len = PAGE_SIZE + page - cwd;
  1679. if (len <= size) {
  1680. error = len;
  1681. if (copy_to_user(buf, cwd, len))
  1682. error = -EFAULT;
  1683. }
  1684. } else
  1685. spin_unlock(&dcache_lock);
  1686. out:
  1687. dput(pwd);
  1688. mntput(pwdmnt);
  1689. dput(root);
  1690. mntput(rootmnt);
  1691. free_page((unsigned long) page);
  1692. return error;
  1693. }
  1694. /*
  1695. * Test whether new_dentry is a subdirectory of old_dentry.
  1696. *
  1697. * Trivially implemented using the dcache structure
  1698. */
  1699. /**
  1700. * is_subdir - is new dentry a subdirectory of old_dentry
  1701. * @new_dentry: new dentry
  1702. * @old_dentry: old dentry
  1703. *
  1704. * Returns 1 if new_dentry is a subdirectory of the parent (at any depth).
  1705. * Returns 0 otherwise.
  1706. * Caller must ensure that "new_dentry" is pinned before calling is_subdir()
  1707. */
  1708. int is_subdir(struct dentry * new_dentry, struct dentry * old_dentry)
  1709. {
  1710. int result;
  1711. struct dentry * saved = new_dentry;
  1712. unsigned long seq;
  1713. /* need rcu_readlock to protect against the d_parent trashing due to
  1714. * d_move
  1715. */
  1716. rcu_read_lock();
  1717. do {
  1718. /* for restarting inner loop in case of seq retry */
  1719. new_dentry = saved;
  1720. result = 0;
  1721. seq = read_seqbegin(&rename_lock);
  1722. for (;;) {
  1723. if (new_dentry != old_dentry) {
  1724. struct dentry * parent = new_dentry->d_parent;
  1725. if (parent == new_dentry)
  1726. break;
  1727. new_dentry = parent;
  1728. continue;
  1729. }
  1730. result = 1;
  1731. break;
  1732. }
  1733. } while (read_seqretry(&rename_lock, seq));
  1734. rcu_read_unlock();
  1735. return result;
  1736. }
  1737. void d_genocide(struct dentry *root)
  1738. {
  1739. struct dentry *this_parent = root;
  1740. struct list_head *next;
  1741. spin_lock(&dcache_lock);
  1742. repeat:
  1743. next = this_parent->d_subdirs.next;
  1744. resume:
  1745. while (next != &this_parent->d_subdirs) {
  1746. struct list_head *tmp = next;
  1747. struct dentry *dentry = list_entry(tmp, struct dentry, d_u.d_child);
  1748. next = tmp->next;
  1749. if (d_unhashed(dentry)||!dentry->d_inode)
  1750. continue;
  1751. if (!list_empty(&dentry->d_subdirs)) {
  1752. this_parent = dentry;
  1753. goto repeat;
  1754. }
  1755. atomic_dec(&dentry->d_count);
  1756. }
  1757. if (this_parent != root) {
  1758. next = this_parent->d_u.d_child.next;
  1759. atomic_dec(&this_parent->d_count);
  1760. this_parent = this_parent->d_parent;
  1761. goto resume;
  1762. }
  1763. spin_unlock(&dcache_lock);
  1764. }
  1765. /**
  1766. * find_inode_number - check for dentry with name
  1767. * @dir: directory to check
  1768. * @name: Name to find.
  1769. *
  1770. * Check whether a dentry already exists for the given name,
  1771. * and return the inode number if it has an inode. Otherwise
  1772. * 0 is returned.
  1773. *
  1774. * This routine is used to post-process directory listings for
  1775. * filesystems using synthetic inode numbers, and is necessary
  1776. * to keep getcwd() working.
  1777. */
  1778. ino_t find_inode_number(struct dentry *dir, struct qstr *name)
  1779. {
  1780. struct dentry * dentry;
  1781. ino_t ino = 0;
  1782. dentry = d_hash_and_lookup(dir, name);
  1783. if (dentry) {
  1784. if (dentry->d_inode)
  1785. ino = dentry->d_inode->i_ino;
  1786. dput(dentry);
  1787. }
  1788. return ino;
  1789. }
  1790. static __initdata unsigned long dhash_entries;
  1791. static int __init set_dhash_entries(char *str)
  1792. {
  1793. if (!str)
  1794. return 0;
  1795. dhash_entries = simple_strtoul(str, &str, 0);
  1796. return 1;
  1797. }
  1798. __setup("dhash_entries=", set_dhash_entries);
  1799. static void __init dcache_init_early(void)
  1800. {
  1801. int loop;
  1802. /* If hashes are distributed across NUMA nodes, defer
  1803. * hash allocation until vmalloc space is available.
  1804. */
  1805. if (hashdist)
  1806. return;
  1807. dentry_hashtable =
  1808. alloc_large_system_hash("Dentry cache",
  1809. sizeof(struct hlist_head),
  1810. dhash_entries,
  1811. 13,
  1812. HASH_EARLY,
  1813. &d_hash_shift,
  1814. &d_hash_mask,
  1815. 0);
  1816. for (loop = 0; loop < (1 << d_hash_shift); loop++)
  1817. INIT_HLIST_HEAD(&dentry_hashtable[loop]);
  1818. }
  1819. static void __init dcache_init(unsigned long mempages)
  1820. {
  1821. int loop;
  1822. /*
  1823. * A constructor could be added for stable state like the lists,
  1824. * but it is probably not worth it because of the cache nature
  1825. * of the dcache.
  1826. */
  1827. dentry_cache = kmem_cache_create("dentry_cache",
  1828. sizeof(struct dentry),
  1829. 0,
  1830. (SLAB_RECLAIM_ACCOUNT|SLAB_PANIC|
  1831. SLAB_MEM_SPREAD),
  1832. NULL, NULL);
  1833. set_shrinker(DEFAULT_SEEKS, shrink_dcache_memory);
  1834. /* Hash may have been set up in dcache_init_early */
  1835. if (!hashdist)
  1836. return;
  1837. dentry_hashtable =
  1838. alloc_large_system_hash("Dentry cache",
  1839. sizeof(struct hlist_head),
  1840. dhash_entries,
  1841. 13,
  1842. 0,
  1843. &d_hash_shift,
  1844. &d_hash_mask,
  1845. 0);
  1846. for (loop = 0; loop < (1 << d_hash_shift); loop++)
  1847. INIT_HLIST_HEAD(&dentry_hashtable[loop]);
  1848. }
  1849. /* SLAB cache for __getname() consumers */
  1850. struct kmem_cache *names_cachep __read_mostly;
  1851. /* SLAB cache for file structures */
  1852. struct kmem_cache *filp_cachep __read_mostly;
  1853. EXPORT_SYMBOL(d_genocide);
  1854. void __init vfs_caches_init_early(void)
  1855. {
  1856. dcache_init_early();
  1857. inode_init_early();
  1858. }
  1859. void __init vfs_caches_init(unsigned long mempages)
  1860. {
  1861. unsigned long reserve;
  1862. /* Base hash sizes on available memory, with a reserve equal to
  1863. 150% of current kernel size */
  1864. reserve = min((mempages - nr_free_pages()) * 3/2, mempages - 1);
  1865. mempages -= reserve;
  1866. names_cachep = kmem_cache_create("names_cache", PATH_MAX, 0,
  1867. SLAB_HWCACHE_ALIGN|SLAB_PANIC, NULL, NULL);
  1868. filp_cachep = kmem_cache_create("filp", sizeof(struct file), 0,
  1869. SLAB_HWCACHE_ALIGN|SLAB_PANIC, NULL, NULL);
  1870. dcache_init(mempages);
  1871. inode_init(mempages);
  1872. files_init(mempages);
  1873. mnt_init(mempages);
  1874. bdev_cache_init();
  1875. chrdev_init();
  1876. }
  1877. EXPORT_SYMBOL(d_alloc);
  1878. EXPORT_SYMBOL(d_alloc_anon);
  1879. EXPORT_SYMBOL(d_alloc_root);
  1880. EXPORT_SYMBOL(d_delete);
  1881. EXPORT_SYMBOL(d_find_alias);
  1882. EXPORT_SYMBOL(d_instantiate);
  1883. EXPORT_SYMBOL(d_invalidate);
  1884. EXPORT_SYMBOL(d_lookup);
  1885. EXPORT_SYMBOL(d_move);
  1886. EXPORT_SYMBOL_GPL(d_materialise_unique);
  1887. EXPORT_SYMBOL(d_path);
  1888. EXPORT_SYMBOL(d_prune_aliases);
  1889. EXPORT_SYMBOL(d_rehash);
  1890. EXPORT_SYMBOL(d_splice_alias);
  1891. EXPORT_SYMBOL(d_validate);
  1892. EXPORT_SYMBOL(dget_locked);
  1893. EXPORT_SYMBOL(dput);
  1894. EXPORT_SYMBOL(find_inode_number);
  1895. EXPORT_SYMBOL(have_submounts);
  1896. EXPORT_SYMBOL(names_cachep);
  1897. EXPORT_SYMBOL(shrink_dcache_parent);
  1898. EXPORT_SYMBOL(shrink_dcache_sb);