dcache.c 46 KB

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