dcache.c 60 KB

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