dcache.c 55 KB

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