dcache.c 57 KB

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