dcache.c 59 KB

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