dcache.c 60 KB

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