dcache.c 55 KB

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