dcache.c 49 KB

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