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