dcache.c 60 KB

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