dcache.c 62 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038203920402041204220432044204520462047204820492050205120522053205420552056205720582059206020612062206320642065206620672068206920702071207220732074207520762077207820792080208120822083208420852086208720882089209020912092209320942095209620972098209921002101210221032104210521062107210821092110211121122113211421152116211721182119212021212122212321242125212621272128212921302131213221332134213521362137213821392140214121422143214421452146214721482149215021512152215321542155215621572158215921602161216221632164216521662167216821692170217121722173217421752176217721782179218021812182218321842185218621872188218921902191219221932194219521962197219821992200220122022203220422052206220722082209221022112212221322142215221622172218221922202221222222232224222522262227222822292230223122322233223422352236223722382239224022412242224322442245224622472248224922502251225222532254225522562257225822592260226122622263226422652266226722682269227022712272227322742275227622772278227922802281228222832284228522862287228822892290229122922293229422952296229722982299230023012302230323042305230623072308230923102311231223132314231523162317231823192320232123222323232423252326232723282329233023312332233323342335233623372338233923402341234223432344234523462347234823492350235123522353235423552356235723582359236023612362236323642365236623672368236923702371237223732374237523762377237823792380238123822383238423852386238723882389239023912392239323942395239623972398239924002401240224032404240524062407240824092410241124122413241424152416241724182419242024212422242324242425242624272428242924302431243224332434243524362437243824392440244124422443244424452446244724482449245024512452245324542455245624572458245924602461246224632464246524662467246824692470
  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, *p = NULL;
  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. if (count >= unused)
  501. prune_ratio = 1;
  502. else
  503. prune_ratio = unused / count;
  504. spin_lock(&sb_lock);
  505. list_for_each_entry(sb, &super_blocks, s_list) {
  506. if (list_empty(&sb->s_instances))
  507. continue;
  508. if (sb->s_nr_dentry_unused == 0)
  509. continue;
  510. sb->s_count++;
  511. /* Now, we reclaim unused dentrins with fairness.
  512. * We reclaim them same percentage from each superblock.
  513. * We calculate number of dentries to scan on this sb
  514. * as follows, but the implementation is arranged to avoid
  515. * overflows:
  516. * number of dentries to scan on this sb =
  517. * count * (number of dentries on this sb /
  518. * number of dentries in the machine)
  519. */
  520. spin_unlock(&sb_lock);
  521. if (prune_ratio != 1)
  522. w_count = (sb->s_nr_dentry_unused / prune_ratio) + 1;
  523. else
  524. w_count = sb->s_nr_dentry_unused;
  525. pruned = w_count;
  526. /*
  527. * We need to be sure this filesystem isn't being unmounted,
  528. * otherwise we could race with generic_shutdown_super(), and
  529. * end up holding a reference to an inode while the filesystem
  530. * is unmounted. So we try to get s_umount, and make sure
  531. * s_root isn't NULL.
  532. */
  533. if (down_read_trylock(&sb->s_umount)) {
  534. if ((sb->s_root != NULL) &&
  535. (!list_empty(&sb->s_dentry_lru))) {
  536. spin_unlock(&dcache_lock);
  537. __shrink_dcache_sb(sb, &w_count,
  538. DCACHE_REFERENCED);
  539. pruned -= w_count;
  540. spin_lock(&dcache_lock);
  541. }
  542. up_read(&sb->s_umount);
  543. }
  544. spin_lock(&sb_lock);
  545. if (p)
  546. __put_super(p);
  547. count -= pruned;
  548. p = sb;
  549. /* more work left to do? */
  550. if (count <= 0)
  551. break;
  552. }
  553. if (p)
  554. __put_super(p);
  555. spin_unlock(&sb_lock);
  556. spin_unlock(&dcache_lock);
  557. }
  558. /**
  559. * shrink_dcache_sb - shrink dcache for a superblock
  560. * @sb: superblock
  561. *
  562. * Shrink the dcache for the specified super block. This
  563. * is used to free the dcache before unmounting a file
  564. * system
  565. */
  566. void shrink_dcache_sb(struct super_block * sb)
  567. {
  568. __shrink_dcache_sb(sb, NULL, 0);
  569. }
  570. EXPORT_SYMBOL(shrink_dcache_sb);
  571. /*
  572. * destroy a single subtree of dentries for unmount
  573. * - see the comments on shrink_dcache_for_umount() for a description of the
  574. * locking
  575. */
  576. static void shrink_dcache_for_umount_subtree(struct dentry *dentry)
  577. {
  578. struct dentry *parent;
  579. unsigned detached = 0;
  580. BUG_ON(!IS_ROOT(dentry));
  581. /* detach this root from the system */
  582. spin_lock(&dcache_lock);
  583. dentry_lru_del_init(dentry);
  584. __d_drop(dentry);
  585. spin_unlock(&dcache_lock);
  586. for (;;) {
  587. /* descend to the first leaf in the current subtree */
  588. while (!list_empty(&dentry->d_subdirs)) {
  589. struct dentry *loop;
  590. /* this is a branch with children - detach all of them
  591. * from the system in one go */
  592. spin_lock(&dcache_lock);
  593. list_for_each_entry(loop, &dentry->d_subdirs,
  594. d_u.d_child) {
  595. dentry_lru_del_init(loop);
  596. __d_drop(loop);
  597. cond_resched_lock(&dcache_lock);
  598. }
  599. spin_unlock(&dcache_lock);
  600. /* move to the first child */
  601. dentry = list_entry(dentry->d_subdirs.next,
  602. struct dentry, d_u.d_child);
  603. }
  604. /* consume the dentries from this leaf up through its parents
  605. * until we find one with children or run out altogether */
  606. do {
  607. struct inode *inode;
  608. if (atomic_read(&dentry->d_count) != 0) {
  609. printk(KERN_ERR
  610. "BUG: Dentry %p{i=%lx,n=%s}"
  611. " still in use (%d)"
  612. " [unmount of %s %s]\n",
  613. dentry,
  614. dentry->d_inode ?
  615. dentry->d_inode->i_ino : 0UL,
  616. dentry->d_name.name,
  617. atomic_read(&dentry->d_count),
  618. dentry->d_sb->s_type->name,
  619. dentry->d_sb->s_id);
  620. BUG();
  621. }
  622. if (IS_ROOT(dentry))
  623. parent = NULL;
  624. else {
  625. parent = dentry->d_parent;
  626. atomic_dec(&parent->d_count);
  627. }
  628. list_del(&dentry->d_u.d_child);
  629. detached++;
  630. inode = dentry->d_inode;
  631. if (inode) {
  632. dentry->d_inode = NULL;
  633. list_del_init(&dentry->d_alias);
  634. if (dentry->d_op && dentry->d_op->d_iput)
  635. dentry->d_op->d_iput(dentry, inode);
  636. else
  637. iput(inode);
  638. }
  639. d_free(dentry);
  640. /* finished when we fall off the top of the tree,
  641. * otherwise we ascend to the parent and move to the
  642. * next sibling if there is one */
  643. if (!parent)
  644. goto out;
  645. dentry = parent;
  646. } while (list_empty(&dentry->d_subdirs));
  647. dentry = list_entry(dentry->d_subdirs.next,
  648. struct dentry, d_u.d_child);
  649. }
  650. out:
  651. /* several dentries were freed, need to correct nr_dentry */
  652. spin_lock(&dcache_lock);
  653. dentry_stat.nr_dentry -= detached;
  654. spin_unlock(&dcache_lock);
  655. }
  656. /*
  657. * destroy the dentries attached to a superblock on unmounting
  658. * - we don't need to use dentry->d_lock, and only need dcache_lock when
  659. * removing the dentry from the system lists and hashes because:
  660. * - the superblock is detached from all mountings and open files, so the
  661. * dentry trees will not be rearranged by the VFS
  662. * - s_umount is write-locked, so the memory pressure shrinker will ignore
  663. * any dentries belonging to this superblock that it comes across
  664. * - the filesystem itself is no longer permitted to rearrange the dentries
  665. * in this superblock
  666. */
  667. void shrink_dcache_for_umount(struct super_block *sb)
  668. {
  669. struct dentry *dentry;
  670. if (down_read_trylock(&sb->s_umount))
  671. BUG();
  672. dentry = sb->s_root;
  673. sb->s_root = NULL;
  674. atomic_dec(&dentry->d_count);
  675. shrink_dcache_for_umount_subtree(dentry);
  676. while (!hlist_empty(&sb->s_anon)) {
  677. dentry = hlist_entry(sb->s_anon.first, struct dentry, d_hash);
  678. shrink_dcache_for_umount_subtree(dentry);
  679. }
  680. }
  681. /*
  682. * Search for at least 1 mount point in the dentry's subdirs.
  683. * We descend to the next level whenever the d_subdirs
  684. * list is non-empty and continue searching.
  685. */
  686. /**
  687. * have_submounts - check for mounts over a dentry
  688. * @parent: dentry to check.
  689. *
  690. * Return true if the parent or its subdirectories contain
  691. * a mount point
  692. */
  693. int have_submounts(struct dentry *parent)
  694. {
  695. struct dentry *this_parent = parent;
  696. struct list_head *next;
  697. spin_lock(&dcache_lock);
  698. if (d_mountpoint(parent))
  699. goto positive;
  700. repeat:
  701. next = this_parent->d_subdirs.next;
  702. resume:
  703. while (next != &this_parent->d_subdirs) {
  704. struct list_head *tmp = next;
  705. struct dentry *dentry = list_entry(tmp, struct dentry, d_u.d_child);
  706. next = tmp->next;
  707. /* Have we found a mount point ? */
  708. if (d_mountpoint(dentry))
  709. goto positive;
  710. if (!list_empty(&dentry->d_subdirs)) {
  711. this_parent = dentry;
  712. goto repeat;
  713. }
  714. }
  715. /*
  716. * All done at this level ... ascend and resume the search.
  717. */
  718. if (this_parent != parent) {
  719. next = this_parent->d_u.d_child.next;
  720. this_parent = this_parent->d_parent;
  721. goto resume;
  722. }
  723. spin_unlock(&dcache_lock);
  724. return 0; /* No mount points found in tree */
  725. positive:
  726. spin_unlock(&dcache_lock);
  727. return 1;
  728. }
  729. EXPORT_SYMBOL(have_submounts);
  730. /*
  731. * Search the dentry child list for the specified parent,
  732. * and move any unused dentries to the end of the unused
  733. * list for prune_dcache(). We descend to the next level
  734. * whenever the d_subdirs list is non-empty and continue
  735. * searching.
  736. *
  737. * It returns zero iff there are no unused children,
  738. * otherwise it returns the number of children moved to
  739. * the end of the unused list. This may not be the total
  740. * number of unused children, because select_parent can
  741. * drop the lock and return early due to latency
  742. * constraints.
  743. */
  744. static int select_parent(struct dentry * parent)
  745. {
  746. struct dentry *this_parent = parent;
  747. struct list_head *next;
  748. int found = 0;
  749. spin_lock(&dcache_lock);
  750. repeat:
  751. next = this_parent->d_subdirs.next;
  752. resume:
  753. while (next != &this_parent->d_subdirs) {
  754. struct list_head *tmp = next;
  755. struct dentry *dentry = list_entry(tmp, struct dentry, d_u.d_child);
  756. next = tmp->next;
  757. dentry_lru_del_init(dentry);
  758. /*
  759. * move only zero ref count dentries to the end
  760. * of the unused list for prune_dcache
  761. */
  762. if (!atomic_read(&dentry->d_count)) {
  763. dentry_lru_add_tail(dentry);
  764. found++;
  765. }
  766. /*
  767. * We can return to the caller if we have found some (this
  768. * ensures forward progress). We'll be coming back to find
  769. * the rest.
  770. */
  771. if (found && need_resched())
  772. goto out;
  773. /*
  774. * Descend a level if the d_subdirs list is non-empty.
  775. */
  776. if (!list_empty(&dentry->d_subdirs)) {
  777. this_parent = dentry;
  778. goto repeat;
  779. }
  780. }
  781. /*
  782. * All done at this level ... ascend and resume the search.
  783. */
  784. if (this_parent != parent) {
  785. next = this_parent->d_u.d_child.next;
  786. this_parent = this_parent->d_parent;
  787. goto resume;
  788. }
  789. out:
  790. spin_unlock(&dcache_lock);
  791. return found;
  792. }
  793. /**
  794. * shrink_dcache_parent - prune dcache
  795. * @parent: parent of entries to prune
  796. *
  797. * Prune the dcache to remove unused children of the parent dentry.
  798. */
  799. void shrink_dcache_parent(struct dentry * parent)
  800. {
  801. struct super_block *sb = parent->d_sb;
  802. int found;
  803. while ((found = select_parent(parent)) != 0)
  804. __shrink_dcache_sb(sb, &found, 0);
  805. }
  806. EXPORT_SYMBOL(shrink_dcache_parent);
  807. /*
  808. * Scan `nr' dentries and return the number which remain.
  809. *
  810. * We need to avoid reentering the filesystem if the caller is performing a
  811. * GFP_NOFS allocation attempt. One example deadlock is:
  812. *
  813. * ext2_new_block->getblk->GFP->shrink_dcache_memory->prune_dcache->
  814. * prune_one_dentry->dput->dentry_iput->iput->inode->i_sb->s_op->put_inode->
  815. * ext2_discard_prealloc->ext2_free_blocks->lock_super->DEADLOCK.
  816. *
  817. * In this case we return -1 to tell the caller that we baled.
  818. */
  819. static int shrink_dcache_memory(struct shrinker *shrink, int nr, gfp_t gfp_mask)
  820. {
  821. if (nr) {
  822. if (!(gfp_mask & __GFP_FS))
  823. return -1;
  824. prune_dcache(nr);
  825. }
  826. return (dentry_stat.nr_unused / 100) * sysctl_vfs_cache_pressure;
  827. }
  828. static struct shrinker dcache_shrinker = {
  829. .shrink = shrink_dcache_memory,
  830. .seeks = DEFAULT_SEEKS,
  831. };
  832. /**
  833. * d_alloc - allocate a dcache entry
  834. * @parent: parent of entry to allocate
  835. * @name: qstr of the name
  836. *
  837. * Allocates a dentry. It returns %NULL if there is insufficient memory
  838. * available. On a success the dentry is returned. The name passed in is
  839. * copied and the copy passed in may be reused after this call.
  840. */
  841. struct dentry *d_alloc(struct dentry * parent, const struct qstr *name)
  842. {
  843. struct dentry *dentry;
  844. char *dname;
  845. dentry = kmem_cache_alloc(dentry_cache, GFP_KERNEL);
  846. if (!dentry)
  847. return NULL;
  848. if (name->len > DNAME_INLINE_LEN-1) {
  849. dname = kmalloc(name->len + 1, GFP_KERNEL);
  850. if (!dname) {
  851. kmem_cache_free(dentry_cache, dentry);
  852. return NULL;
  853. }
  854. } else {
  855. dname = dentry->d_iname;
  856. }
  857. dentry->d_name.name = dname;
  858. dentry->d_name.len = name->len;
  859. dentry->d_name.hash = name->hash;
  860. memcpy(dname, name->name, name->len);
  861. dname[name->len] = 0;
  862. atomic_set(&dentry->d_count, 1);
  863. dentry->d_flags = DCACHE_UNHASHED;
  864. spin_lock_init(&dentry->d_lock);
  865. dentry->d_inode = NULL;
  866. dentry->d_parent = NULL;
  867. dentry->d_sb = NULL;
  868. dentry->d_op = NULL;
  869. dentry->d_fsdata = NULL;
  870. dentry->d_mounted = 0;
  871. INIT_HLIST_NODE(&dentry->d_hash);
  872. INIT_LIST_HEAD(&dentry->d_lru);
  873. INIT_LIST_HEAD(&dentry->d_subdirs);
  874. INIT_LIST_HEAD(&dentry->d_alias);
  875. if (parent) {
  876. dentry->d_parent = dget(parent);
  877. dentry->d_sb = parent->d_sb;
  878. } else {
  879. INIT_LIST_HEAD(&dentry->d_u.d_child);
  880. }
  881. spin_lock(&dcache_lock);
  882. if (parent)
  883. list_add(&dentry->d_u.d_child, &parent->d_subdirs);
  884. dentry_stat.nr_dentry++;
  885. spin_unlock(&dcache_lock);
  886. return dentry;
  887. }
  888. EXPORT_SYMBOL(d_alloc);
  889. struct dentry *d_alloc_name(struct dentry *parent, const char *name)
  890. {
  891. struct qstr q;
  892. q.name = name;
  893. q.len = strlen(name);
  894. q.hash = full_name_hash(q.name, q.len);
  895. return d_alloc(parent, &q);
  896. }
  897. EXPORT_SYMBOL(d_alloc_name);
  898. /* the caller must hold dcache_lock */
  899. static void __d_instantiate(struct dentry *dentry, struct inode *inode)
  900. {
  901. if (inode)
  902. list_add(&dentry->d_alias, &inode->i_dentry);
  903. dentry->d_inode = inode;
  904. fsnotify_d_instantiate(dentry, inode);
  905. }
  906. /**
  907. * d_instantiate - fill in inode information for a dentry
  908. * @entry: dentry to complete
  909. * @inode: inode to attach to this dentry
  910. *
  911. * Fill in inode information in the entry.
  912. *
  913. * This turns negative dentries into productive full members
  914. * of society.
  915. *
  916. * NOTE! This assumes that the inode count has been incremented
  917. * (or otherwise set) by the caller to indicate that it is now
  918. * in use by the dcache.
  919. */
  920. void d_instantiate(struct dentry *entry, struct inode * inode)
  921. {
  922. BUG_ON(!list_empty(&entry->d_alias));
  923. spin_lock(&dcache_lock);
  924. __d_instantiate(entry, inode);
  925. spin_unlock(&dcache_lock);
  926. security_d_instantiate(entry, inode);
  927. }
  928. EXPORT_SYMBOL(d_instantiate);
  929. /**
  930. * d_instantiate_unique - instantiate a non-aliased dentry
  931. * @entry: dentry to instantiate
  932. * @inode: inode to attach to this dentry
  933. *
  934. * Fill in inode information in the entry. On success, it returns NULL.
  935. * If an unhashed alias of "entry" already exists, then we return the
  936. * aliased dentry instead and drop one reference to inode.
  937. *
  938. * Note that in order to avoid conflicts with rename() etc, the caller
  939. * had better be holding the parent directory semaphore.
  940. *
  941. * This also assumes that the inode count has been incremented
  942. * (or otherwise set) by the caller to indicate that it is now
  943. * in use by the dcache.
  944. */
  945. static struct dentry *__d_instantiate_unique(struct dentry *entry,
  946. struct inode *inode)
  947. {
  948. struct dentry *alias;
  949. int len = entry->d_name.len;
  950. const char *name = entry->d_name.name;
  951. unsigned int hash = entry->d_name.hash;
  952. if (!inode) {
  953. __d_instantiate(entry, NULL);
  954. return NULL;
  955. }
  956. list_for_each_entry(alias, &inode->i_dentry, d_alias) {
  957. struct qstr *qstr = &alias->d_name;
  958. if (qstr->hash != hash)
  959. continue;
  960. if (alias->d_parent != entry->d_parent)
  961. continue;
  962. if (qstr->len != len)
  963. continue;
  964. if (memcmp(qstr->name, name, len))
  965. continue;
  966. dget_locked(alias);
  967. return alias;
  968. }
  969. __d_instantiate(entry, inode);
  970. return NULL;
  971. }
  972. struct dentry *d_instantiate_unique(struct dentry *entry, struct inode *inode)
  973. {
  974. struct dentry *result;
  975. BUG_ON(!list_empty(&entry->d_alias));
  976. spin_lock(&dcache_lock);
  977. result = __d_instantiate_unique(entry, inode);
  978. spin_unlock(&dcache_lock);
  979. if (!result) {
  980. security_d_instantiate(entry, inode);
  981. return NULL;
  982. }
  983. BUG_ON(!d_unhashed(result));
  984. iput(inode);
  985. return result;
  986. }
  987. EXPORT_SYMBOL(d_instantiate_unique);
  988. /**
  989. * d_alloc_root - allocate root dentry
  990. * @root_inode: inode to allocate the root for
  991. *
  992. * Allocate a root ("/") dentry for the inode given. The inode is
  993. * instantiated and returned. %NULL is returned if there is insufficient
  994. * memory or the inode passed is %NULL.
  995. */
  996. struct dentry * d_alloc_root(struct inode * root_inode)
  997. {
  998. struct dentry *res = NULL;
  999. if (root_inode) {
  1000. static const struct qstr name = { .name = "/", .len = 1 };
  1001. res = d_alloc(NULL, &name);
  1002. if (res) {
  1003. res->d_sb = root_inode->i_sb;
  1004. res->d_parent = res;
  1005. d_instantiate(res, root_inode);
  1006. }
  1007. }
  1008. return res;
  1009. }
  1010. EXPORT_SYMBOL(d_alloc_root);
  1011. static inline struct hlist_head *d_hash(struct dentry *parent,
  1012. unsigned long hash)
  1013. {
  1014. hash += ((unsigned long) parent ^ GOLDEN_RATIO_PRIME) / L1_CACHE_BYTES;
  1015. hash = hash ^ ((hash ^ GOLDEN_RATIO_PRIME) >> D_HASHBITS);
  1016. return dentry_hashtable + (hash & D_HASHMASK);
  1017. }
  1018. /**
  1019. * d_obtain_alias - find or allocate a dentry for a given inode
  1020. * @inode: inode to allocate the dentry for
  1021. *
  1022. * Obtain a dentry for an inode resulting from NFS filehandle conversion or
  1023. * similar open by handle operations. The returned dentry may be anonymous,
  1024. * or may have a full name (if the inode was already in the cache).
  1025. *
  1026. * When called on a directory inode, we must ensure that the inode only ever
  1027. * has one dentry. If a dentry is found, that is returned instead of
  1028. * allocating a new one.
  1029. *
  1030. * On successful return, the reference to the inode has been transferred
  1031. * to the dentry. In case of an error the reference on the inode is released.
  1032. * To make it easier to use in export operations a %NULL or IS_ERR inode may
  1033. * be passed in and will be the error will be propagate to the return value,
  1034. * with a %NULL @inode replaced by ERR_PTR(-ESTALE).
  1035. */
  1036. struct dentry *d_obtain_alias(struct inode *inode)
  1037. {
  1038. static const struct qstr anonstring = { .name = "" };
  1039. struct dentry *tmp;
  1040. struct dentry *res;
  1041. if (!inode)
  1042. return ERR_PTR(-ESTALE);
  1043. if (IS_ERR(inode))
  1044. return ERR_CAST(inode);
  1045. res = d_find_alias(inode);
  1046. if (res)
  1047. goto out_iput;
  1048. tmp = d_alloc(NULL, &anonstring);
  1049. if (!tmp) {
  1050. res = ERR_PTR(-ENOMEM);
  1051. goto out_iput;
  1052. }
  1053. tmp->d_parent = tmp; /* make sure dput doesn't croak */
  1054. spin_lock(&dcache_lock);
  1055. res = __d_find_alias(inode, 0);
  1056. if (res) {
  1057. spin_unlock(&dcache_lock);
  1058. dput(tmp);
  1059. goto out_iput;
  1060. }
  1061. /* attach a disconnected dentry */
  1062. spin_lock(&tmp->d_lock);
  1063. tmp->d_sb = inode->i_sb;
  1064. tmp->d_inode = inode;
  1065. tmp->d_flags |= DCACHE_DISCONNECTED;
  1066. tmp->d_flags &= ~DCACHE_UNHASHED;
  1067. list_add(&tmp->d_alias, &inode->i_dentry);
  1068. hlist_add_head(&tmp->d_hash, &inode->i_sb->s_anon);
  1069. spin_unlock(&tmp->d_lock);
  1070. spin_unlock(&dcache_lock);
  1071. return tmp;
  1072. out_iput:
  1073. iput(inode);
  1074. return res;
  1075. }
  1076. EXPORT_SYMBOL(d_obtain_alias);
  1077. /**
  1078. * d_splice_alias - splice a disconnected dentry into the tree if one exists
  1079. * @inode: the inode which may have a disconnected dentry
  1080. * @dentry: a negative dentry which we want to point to the inode.
  1081. *
  1082. * If inode is a directory and has a 'disconnected' dentry (i.e. IS_ROOT and
  1083. * DCACHE_DISCONNECTED), then d_move that in place of the given dentry
  1084. * and return it, else simply d_add the inode to the dentry and return NULL.
  1085. *
  1086. * This is needed in the lookup routine of any filesystem that is exportable
  1087. * (via knfsd) so that we can build dcache paths to directories effectively.
  1088. *
  1089. * If a dentry was found and moved, then it is returned. Otherwise NULL
  1090. * is returned. This matches the expected return value of ->lookup.
  1091. *
  1092. */
  1093. struct dentry *d_splice_alias(struct inode *inode, struct dentry *dentry)
  1094. {
  1095. struct dentry *new = NULL;
  1096. if (inode && S_ISDIR(inode->i_mode)) {
  1097. spin_lock(&dcache_lock);
  1098. new = __d_find_alias(inode, 1);
  1099. if (new) {
  1100. BUG_ON(!(new->d_flags & DCACHE_DISCONNECTED));
  1101. spin_unlock(&dcache_lock);
  1102. security_d_instantiate(new, inode);
  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->d_flags &= ~DCACHE_CANT_MOUNT;
  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. EXPORT_SYMBOL(d_delete);
  1392. static void __d_rehash(struct dentry * entry, struct hlist_head *list)
  1393. {
  1394. entry->d_flags &= ~DCACHE_UNHASHED;
  1395. hlist_add_head_rcu(&entry->d_hash, list);
  1396. }
  1397. static void _d_rehash(struct dentry * entry)
  1398. {
  1399. __d_rehash(entry, d_hash(entry->d_parent, entry->d_name.hash));
  1400. }
  1401. /**
  1402. * d_rehash - add an entry back to the hash
  1403. * @entry: dentry to add to the hash
  1404. *
  1405. * Adds a dentry to the hash according to its name.
  1406. */
  1407. void d_rehash(struct dentry * entry)
  1408. {
  1409. spin_lock(&dcache_lock);
  1410. spin_lock(&entry->d_lock);
  1411. _d_rehash(entry);
  1412. spin_unlock(&entry->d_lock);
  1413. spin_unlock(&dcache_lock);
  1414. }
  1415. EXPORT_SYMBOL(d_rehash);
  1416. /*
  1417. * When switching names, the actual string doesn't strictly have to
  1418. * be preserved in the target - because we're dropping the target
  1419. * anyway. As such, we can just do a simple memcpy() to copy over
  1420. * the new name before we switch.
  1421. *
  1422. * Note that we have to be a lot more careful about getting the hash
  1423. * switched - we have to switch the hash value properly even if it
  1424. * then no longer matches the actual (corrupted) string of the target.
  1425. * The hash value has to match the hash queue that the dentry is on..
  1426. */
  1427. static void switch_names(struct dentry *dentry, struct dentry *target)
  1428. {
  1429. if (dname_external(target)) {
  1430. if (dname_external(dentry)) {
  1431. /*
  1432. * Both external: swap the pointers
  1433. */
  1434. swap(target->d_name.name, dentry->d_name.name);
  1435. } else {
  1436. /*
  1437. * dentry:internal, target:external. Steal target's
  1438. * storage and make target internal.
  1439. */
  1440. memcpy(target->d_iname, dentry->d_name.name,
  1441. dentry->d_name.len + 1);
  1442. dentry->d_name.name = target->d_name.name;
  1443. target->d_name.name = target->d_iname;
  1444. }
  1445. } else {
  1446. if (dname_external(dentry)) {
  1447. /*
  1448. * dentry:external, target:internal. Give dentry's
  1449. * storage to target and make dentry internal
  1450. */
  1451. memcpy(dentry->d_iname, target->d_name.name,
  1452. target->d_name.len + 1);
  1453. target->d_name.name = dentry->d_name.name;
  1454. dentry->d_name.name = dentry->d_iname;
  1455. } else {
  1456. /*
  1457. * Both are internal. Just copy target to dentry
  1458. */
  1459. memcpy(dentry->d_iname, target->d_name.name,
  1460. target->d_name.len + 1);
  1461. dentry->d_name.len = target->d_name.len;
  1462. return;
  1463. }
  1464. }
  1465. swap(dentry->d_name.len, target->d_name.len);
  1466. }
  1467. /*
  1468. * We cannibalize "target" when moving dentry on top of it,
  1469. * because it's going to be thrown away anyway. We could be more
  1470. * polite about it, though.
  1471. *
  1472. * This forceful removal will result in ugly /proc output if
  1473. * somebody holds a file open that got deleted due to a rename.
  1474. * We could be nicer about the deleted file, and let it show
  1475. * up under the name it had before it was deleted rather than
  1476. * under the original name of the file that was moved on top of it.
  1477. */
  1478. /*
  1479. * d_move_locked - move a dentry
  1480. * @dentry: entry to move
  1481. * @target: new dentry
  1482. *
  1483. * Update the dcache to reflect the move of a file name. Negative
  1484. * dcache entries should not be moved in this way.
  1485. */
  1486. static void d_move_locked(struct dentry * dentry, struct dentry * target)
  1487. {
  1488. struct hlist_head *list;
  1489. if (!dentry->d_inode)
  1490. printk(KERN_WARNING "VFS: moving negative dcache entry\n");
  1491. write_seqlock(&rename_lock);
  1492. /*
  1493. * XXXX: do we really need to take target->d_lock?
  1494. */
  1495. if (target < dentry) {
  1496. spin_lock(&target->d_lock);
  1497. spin_lock_nested(&dentry->d_lock, DENTRY_D_LOCK_NESTED);
  1498. } else {
  1499. spin_lock(&dentry->d_lock);
  1500. spin_lock_nested(&target->d_lock, DENTRY_D_LOCK_NESTED);
  1501. }
  1502. /* Move the dentry to the target hash queue, if on different bucket */
  1503. if (d_unhashed(dentry))
  1504. goto already_unhashed;
  1505. hlist_del_rcu(&dentry->d_hash);
  1506. already_unhashed:
  1507. list = d_hash(target->d_parent, target->d_name.hash);
  1508. __d_rehash(dentry, list);
  1509. /* Unhash the target: dput() will then get rid of it */
  1510. __d_drop(target);
  1511. list_del(&dentry->d_u.d_child);
  1512. list_del(&target->d_u.d_child);
  1513. /* Switch the names.. */
  1514. switch_names(dentry, target);
  1515. swap(dentry->d_name.hash, target->d_name.hash);
  1516. /* ... and switch the parents */
  1517. if (IS_ROOT(dentry)) {
  1518. dentry->d_parent = target->d_parent;
  1519. target->d_parent = target;
  1520. INIT_LIST_HEAD(&target->d_u.d_child);
  1521. } else {
  1522. swap(dentry->d_parent, target->d_parent);
  1523. /* And add them back to the (new) parent lists */
  1524. list_add(&target->d_u.d_child, &target->d_parent->d_subdirs);
  1525. }
  1526. list_add(&dentry->d_u.d_child, &dentry->d_parent->d_subdirs);
  1527. spin_unlock(&target->d_lock);
  1528. fsnotify_d_move(dentry);
  1529. spin_unlock(&dentry->d_lock);
  1530. write_sequnlock(&rename_lock);
  1531. }
  1532. /**
  1533. * d_move - move a dentry
  1534. * @dentry: entry to move
  1535. * @target: new dentry
  1536. *
  1537. * Update the dcache to reflect the move of a file name. Negative
  1538. * dcache entries should not be moved in this way.
  1539. */
  1540. void d_move(struct dentry * dentry, struct dentry * target)
  1541. {
  1542. spin_lock(&dcache_lock);
  1543. d_move_locked(dentry, target);
  1544. spin_unlock(&dcache_lock);
  1545. }
  1546. EXPORT_SYMBOL(d_move);
  1547. /**
  1548. * d_ancestor - search for an ancestor
  1549. * @p1: ancestor dentry
  1550. * @p2: child dentry
  1551. *
  1552. * Returns the ancestor dentry of p2 which is a child of p1, if p1 is
  1553. * an ancestor of p2, else NULL.
  1554. */
  1555. struct dentry *d_ancestor(struct dentry *p1, struct dentry *p2)
  1556. {
  1557. struct dentry *p;
  1558. for (p = p2; !IS_ROOT(p); p = p->d_parent) {
  1559. if (p->d_parent == p1)
  1560. return p;
  1561. }
  1562. return NULL;
  1563. }
  1564. /*
  1565. * This helper attempts to cope with remotely renamed directories
  1566. *
  1567. * It assumes that the caller is already holding
  1568. * dentry->d_parent->d_inode->i_mutex and the dcache_lock
  1569. *
  1570. * Note: If ever the locking in lock_rename() changes, then please
  1571. * remember to update this too...
  1572. */
  1573. static struct dentry *__d_unalias(struct dentry *dentry, struct dentry *alias)
  1574. __releases(dcache_lock)
  1575. {
  1576. struct mutex *m1 = NULL, *m2 = NULL;
  1577. struct dentry *ret;
  1578. /* If alias and dentry share a parent, then no extra locks required */
  1579. if (alias->d_parent == dentry->d_parent)
  1580. goto out_unalias;
  1581. /* Check for loops */
  1582. ret = ERR_PTR(-ELOOP);
  1583. if (d_ancestor(alias, dentry))
  1584. goto out_err;
  1585. /* See lock_rename() */
  1586. ret = ERR_PTR(-EBUSY);
  1587. if (!mutex_trylock(&dentry->d_sb->s_vfs_rename_mutex))
  1588. goto out_err;
  1589. m1 = &dentry->d_sb->s_vfs_rename_mutex;
  1590. if (!mutex_trylock(&alias->d_parent->d_inode->i_mutex))
  1591. goto out_err;
  1592. m2 = &alias->d_parent->d_inode->i_mutex;
  1593. out_unalias:
  1594. d_move_locked(alias, dentry);
  1595. ret = alias;
  1596. out_err:
  1597. spin_unlock(&dcache_lock);
  1598. if (m2)
  1599. mutex_unlock(m2);
  1600. if (m1)
  1601. mutex_unlock(m1);
  1602. return ret;
  1603. }
  1604. /*
  1605. * Prepare an anonymous dentry for life in the superblock's dentry tree as a
  1606. * named dentry in place of the dentry to be replaced.
  1607. */
  1608. static void __d_materialise_dentry(struct dentry *dentry, struct dentry *anon)
  1609. {
  1610. struct dentry *dparent, *aparent;
  1611. switch_names(dentry, anon);
  1612. swap(dentry->d_name.hash, anon->d_name.hash);
  1613. dparent = dentry->d_parent;
  1614. aparent = anon->d_parent;
  1615. dentry->d_parent = (aparent == anon) ? dentry : aparent;
  1616. list_del(&dentry->d_u.d_child);
  1617. if (!IS_ROOT(dentry))
  1618. list_add(&dentry->d_u.d_child, &dentry->d_parent->d_subdirs);
  1619. else
  1620. INIT_LIST_HEAD(&dentry->d_u.d_child);
  1621. anon->d_parent = (dparent == dentry) ? anon : dparent;
  1622. list_del(&anon->d_u.d_child);
  1623. if (!IS_ROOT(anon))
  1624. list_add(&anon->d_u.d_child, &anon->d_parent->d_subdirs);
  1625. else
  1626. INIT_LIST_HEAD(&anon->d_u.d_child);
  1627. anon->d_flags &= ~DCACHE_DISCONNECTED;
  1628. }
  1629. /**
  1630. * d_materialise_unique - introduce an inode into the tree
  1631. * @dentry: candidate dentry
  1632. * @inode: inode to bind to the dentry, to which aliases may be attached
  1633. *
  1634. * Introduces an dentry into the tree, substituting an extant disconnected
  1635. * root directory alias in its place if there is one
  1636. */
  1637. struct dentry *d_materialise_unique(struct dentry *dentry, struct inode *inode)
  1638. {
  1639. struct dentry *actual;
  1640. BUG_ON(!d_unhashed(dentry));
  1641. spin_lock(&dcache_lock);
  1642. if (!inode) {
  1643. actual = dentry;
  1644. __d_instantiate(dentry, NULL);
  1645. goto found_lock;
  1646. }
  1647. if (S_ISDIR(inode->i_mode)) {
  1648. struct dentry *alias;
  1649. /* Does an aliased dentry already exist? */
  1650. alias = __d_find_alias(inode, 0);
  1651. if (alias) {
  1652. actual = alias;
  1653. /* Is this an anonymous mountpoint that we could splice
  1654. * into our tree? */
  1655. if (IS_ROOT(alias)) {
  1656. spin_lock(&alias->d_lock);
  1657. __d_materialise_dentry(dentry, alias);
  1658. __d_drop(alias);
  1659. goto found;
  1660. }
  1661. /* Nope, but we must(!) avoid directory aliasing */
  1662. actual = __d_unalias(dentry, alias);
  1663. if (IS_ERR(actual))
  1664. dput(alias);
  1665. goto out_nolock;
  1666. }
  1667. }
  1668. /* Add a unique reference */
  1669. actual = __d_instantiate_unique(dentry, inode);
  1670. if (!actual)
  1671. actual = dentry;
  1672. else if (unlikely(!d_unhashed(actual)))
  1673. goto shouldnt_be_hashed;
  1674. found_lock:
  1675. spin_lock(&actual->d_lock);
  1676. found:
  1677. _d_rehash(actual);
  1678. spin_unlock(&actual->d_lock);
  1679. spin_unlock(&dcache_lock);
  1680. out_nolock:
  1681. if (actual == dentry) {
  1682. security_d_instantiate(dentry, inode);
  1683. return NULL;
  1684. }
  1685. iput(inode);
  1686. return actual;
  1687. shouldnt_be_hashed:
  1688. spin_unlock(&dcache_lock);
  1689. BUG();
  1690. }
  1691. EXPORT_SYMBOL_GPL(d_materialise_unique);
  1692. static int prepend(char **buffer, int *buflen, const char *str, int namelen)
  1693. {
  1694. *buflen -= namelen;
  1695. if (*buflen < 0)
  1696. return -ENAMETOOLONG;
  1697. *buffer -= namelen;
  1698. memcpy(*buffer, str, namelen);
  1699. return 0;
  1700. }
  1701. static int prepend_name(char **buffer, int *buflen, struct qstr *name)
  1702. {
  1703. return prepend(buffer, buflen, name->name, name->len);
  1704. }
  1705. /**
  1706. * Prepend path string to a buffer
  1707. *
  1708. * @path: the dentry/vfsmount to report
  1709. * @root: root vfsmnt/dentry (may be modified by this function)
  1710. * @buffer: pointer to the end of the buffer
  1711. * @buflen: pointer to buffer length
  1712. *
  1713. * Caller holds the dcache_lock.
  1714. *
  1715. * If path is not reachable from the supplied root, then the value of
  1716. * root is changed (without modifying refcounts).
  1717. */
  1718. static int prepend_path(const struct path *path, struct path *root,
  1719. char **buffer, int *buflen)
  1720. {
  1721. struct dentry *dentry = path->dentry;
  1722. struct vfsmount *vfsmnt = path->mnt;
  1723. bool slash = false;
  1724. int error = 0;
  1725. spin_lock(&vfsmount_lock);
  1726. while (dentry != root->dentry || vfsmnt != root->mnt) {
  1727. struct dentry * parent;
  1728. if (dentry == vfsmnt->mnt_root || IS_ROOT(dentry)) {
  1729. /* Global root? */
  1730. if (vfsmnt->mnt_parent == vfsmnt) {
  1731. goto global_root;
  1732. }
  1733. dentry = vfsmnt->mnt_mountpoint;
  1734. vfsmnt = vfsmnt->mnt_parent;
  1735. continue;
  1736. }
  1737. parent = dentry->d_parent;
  1738. prefetch(parent);
  1739. error = prepend_name(buffer, buflen, &dentry->d_name);
  1740. if (!error)
  1741. error = prepend(buffer, buflen, "/", 1);
  1742. if (error)
  1743. break;
  1744. slash = true;
  1745. dentry = parent;
  1746. }
  1747. out:
  1748. if (!error && !slash)
  1749. error = prepend(buffer, buflen, "/", 1);
  1750. spin_unlock(&vfsmount_lock);
  1751. return error;
  1752. global_root:
  1753. /*
  1754. * Filesystems needing to implement special "root names"
  1755. * should do so with ->d_dname()
  1756. */
  1757. if (IS_ROOT(dentry) &&
  1758. (dentry->d_name.len != 1 || dentry->d_name.name[0] != '/')) {
  1759. WARN(1, "Root dentry has weird name <%.*s>\n",
  1760. (int) dentry->d_name.len, dentry->d_name.name);
  1761. }
  1762. root->mnt = vfsmnt;
  1763. root->dentry = dentry;
  1764. goto out;
  1765. }
  1766. /**
  1767. * __d_path - return the path of a dentry
  1768. * @path: the dentry/vfsmount to report
  1769. * @root: root vfsmnt/dentry (may be modified by this function)
  1770. * @buf: buffer to return value in
  1771. * @buflen: buffer length
  1772. *
  1773. * Convert a dentry into an ASCII path name.
  1774. *
  1775. * Returns a pointer into the buffer or an error code if the
  1776. * path was too long.
  1777. *
  1778. * "buflen" should be positive. Caller holds the dcache_lock.
  1779. *
  1780. * If path is not reachable from the supplied root, then the value of
  1781. * root is changed (without modifying refcounts).
  1782. */
  1783. char *__d_path(const struct path *path, struct path *root,
  1784. char *buf, int buflen)
  1785. {
  1786. char *res = buf + buflen;
  1787. int error;
  1788. prepend(&res, &buflen, "\0", 1);
  1789. error = prepend_path(path, root, &res, &buflen);
  1790. if (error)
  1791. return ERR_PTR(error);
  1792. return res;
  1793. }
  1794. /*
  1795. * same as __d_path but appends "(deleted)" for unlinked files.
  1796. */
  1797. static int path_with_deleted(const struct path *path, struct path *root,
  1798. char **buf, int *buflen)
  1799. {
  1800. prepend(buf, buflen, "\0", 1);
  1801. if (d_unlinked(path->dentry)) {
  1802. int error = prepend(buf, buflen, " (deleted)", 10);
  1803. if (error)
  1804. return error;
  1805. }
  1806. return prepend_path(path, root, buf, buflen);
  1807. }
  1808. static int prepend_unreachable(char **buffer, int *buflen)
  1809. {
  1810. return prepend(buffer, buflen, "(unreachable)", 13);
  1811. }
  1812. /**
  1813. * d_path - return the path of a dentry
  1814. * @path: path to report
  1815. * @buf: buffer to return value in
  1816. * @buflen: buffer length
  1817. *
  1818. * Convert a dentry into an ASCII path name. If the entry has been deleted
  1819. * the string " (deleted)" is appended. Note that this is ambiguous.
  1820. *
  1821. * Returns a pointer into the buffer or an error code if the path was
  1822. * too long. Note: Callers should use the returned pointer, not the passed
  1823. * in buffer, to use the name! The implementation often starts at an offset
  1824. * into the buffer, and may leave 0 bytes at the start.
  1825. *
  1826. * "buflen" should be positive.
  1827. */
  1828. char *d_path(const struct path *path, char *buf, int buflen)
  1829. {
  1830. char *res = buf + buflen;
  1831. struct path root;
  1832. struct path tmp;
  1833. int error;
  1834. /*
  1835. * We have various synthetic filesystems that never get mounted. On
  1836. * these filesystems dentries are never used for lookup purposes, and
  1837. * thus don't need to be hashed. They also don't need a name until a
  1838. * user wants to identify the object in /proc/pid/fd/. The little hack
  1839. * below allows us to generate a name for these objects on demand:
  1840. */
  1841. if (path->dentry->d_op && path->dentry->d_op->d_dname)
  1842. return path->dentry->d_op->d_dname(path->dentry, buf, buflen);
  1843. get_fs_root(current->fs, &root);
  1844. spin_lock(&dcache_lock);
  1845. tmp = root;
  1846. error = path_with_deleted(path, &tmp, &res, &buflen);
  1847. if (error)
  1848. res = ERR_PTR(error);
  1849. spin_unlock(&dcache_lock);
  1850. path_put(&root);
  1851. return res;
  1852. }
  1853. EXPORT_SYMBOL(d_path);
  1854. /**
  1855. * d_path_with_unreachable - return the path of a dentry
  1856. * @path: path to report
  1857. * @buf: buffer to return value in
  1858. * @buflen: buffer length
  1859. *
  1860. * The difference from d_path() is that this prepends "(unreachable)"
  1861. * to paths which are unreachable from the current process' root.
  1862. */
  1863. char *d_path_with_unreachable(const struct path *path, char *buf, int buflen)
  1864. {
  1865. char *res = buf + buflen;
  1866. struct path root;
  1867. struct path tmp;
  1868. int error;
  1869. if (path->dentry->d_op && path->dentry->d_op->d_dname)
  1870. return path->dentry->d_op->d_dname(path->dentry, buf, buflen);
  1871. get_fs_root(current->fs, &root);
  1872. spin_lock(&dcache_lock);
  1873. tmp = root;
  1874. error = path_with_deleted(path, &tmp, &res, &buflen);
  1875. if (!error && !path_equal(&tmp, &root))
  1876. error = prepend_unreachable(&res, &buflen);
  1877. spin_unlock(&dcache_lock);
  1878. path_put(&root);
  1879. if (error)
  1880. res = ERR_PTR(error);
  1881. return res;
  1882. }
  1883. /*
  1884. * Helper function for dentry_operations.d_dname() members
  1885. */
  1886. char *dynamic_dname(struct dentry *dentry, char *buffer, int buflen,
  1887. const char *fmt, ...)
  1888. {
  1889. va_list args;
  1890. char temp[64];
  1891. int sz;
  1892. va_start(args, fmt);
  1893. sz = vsnprintf(temp, sizeof(temp), fmt, args) + 1;
  1894. va_end(args);
  1895. if (sz > sizeof(temp) || sz > buflen)
  1896. return ERR_PTR(-ENAMETOOLONG);
  1897. buffer += buflen - sz;
  1898. return memcpy(buffer, temp, sz);
  1899. }
  1900. /*
  1901. * Write full pathname from the root of the filesystem into the buffer.
  1902. */
  1903. char *__dentry_path(struct dentry *dentry, char *buf, int buflen)
  1904. {
  1905. char *end = buf + buflen;
  1906. char *retval;
  1907. prepend(&end, &buflen, "\0", 1);
  1908. if (buflen < 1)
  1909. goto Elong;
  1910. /* Get '/' right */
  1911. retval = end-1;
  1912. *retval = '/';
  1913. while (!IS_ROOT(dentry)) {
  1914. struct dentry *parent = dentry->d_parent;
  1915. prefetch(parent);
  1916. if ((prepend_name(&end, &buflen, &dentry->d_name) != 0) ||
  1917. (prepend(&end, &buflen, "/", 1) != 0))
  1918. goto Elong;
  1919. retval = end;
  1920. dentry = parent;
  1921. }
  1922. return retval;
  1923. Elong:
  1924. return ERR_PTR(-ENAMETOOLONG);
  1925. }
  1926. EXPORT_SYMBOL(__dentry_path);
  1927. char *dentry_path(struct dentry *dentry, char *buf, int buflen)
  1928. {
  1929. char *p = NULL;
  1930. char *retval;
  1931. spin_lock(&dcache_lock);
  1932. if (d_unlinked(dentry)) {
  1933. p = buf + buflen;
  1934. if (prepend(&p, &buflen, "//deleted", 10) != 0)
  1935. goto Elong;
  1936. buflen++;
  1937. }
  1938. retval = __dentry_path(dentry, buf, buflen);
  1939. spin_unlock(&dcache_lock);
  1940. if (!IS_ERR(retval) && p)
  1941. *p = '/'; /* restore '/' overriden with '\0' */
  1942. return retval;
  1943. Elong:
  1944. spin_unlock(&dcache_lock);
  1945. return ERR_PTR(-ENAMETOOLONG);
  1946. }
  1947. /*
  1948. * NOTE! The user-level library version returns a
  1949. * character pointer. The kernel system call just
  1950. * returns the length of the buffer filled (which
  1951. * includes the ending '\0' character), or a negative
  1952. * error value. So libc would do something like
  1953. *
  1954. * char *getcwd(char * buf, size_t size)
  1955. * {
  1956. * int retval;
  1957. *
  1958. * retval = sys_getcwd(buf, size);
  1959. * if (retval >= 0)
  1960. * return buf;
  1961. * errno = -retval;
  1962. * return NULL;
  1963. * }
  1964. */
  1965. SYSCALL_DEFINE2(getcwd, char __user *, buf, unsigned long, size)
  1966. {
  1967. int error;
  1968. struct path pwd, root;
  1969. char *page = (char *) __get_free_page(GFP_USER);
  1970. if (!page)
  1971. return -ENOMEM;
  1972. get_fs_root_and_pwd(current->fs, &root, &pwd);
  1973. error = -ENOENT;
  1974. spin_lock(&dcache_lock);
  1975. if (!d_unlinked(pwd.dentry)) {
  1976. unsigned long len;
  1977. struct path tmp = root;
  1978. char *cwd = page + PAGE_SIZE;
  1979. int buflen = PAGE_SIZE;
  1980. prepend(&cwd, &buflen, "\0", 1);
  1981. error = prepend_path(&pwd, &tmp, &cwd, &buflen);
  1982. spin_unlock(&dcache_lock);
  1983. if (error)
  1984. goto out;
  1985. /* Unreachable from current root */
  1986. if (!path_equal(&tmp, &root)) {
  1987. error = prepend_unreachable(&cwd, &buflen);
  1988. if (error)
  1989. goto out;
  1990. }
  1991. error = -ERANGE;
  1992. len = PAGE_SIZE + page - cwd;
  1993. if (len <= size) {
  1994. error = len;
  1995. if (copy_to_user(buf, cwd, len))
  1996. error = -EFAULT;
  1997. }
  1998. } else
  1999. spin_unlock(&dcache_lock);
  2000. out:
  2001. path_put(&pwd);
  2002. path_put(&root);
  2003. free_page((unsigned long) page);
  2004. return error;
  2005. }
  2006. /*
  2007. * Test whether new_dentry is a subdirectory of old_dentry.
  2008. *
  2009. * Trivially implemented using the dcache structure
  2010. */
  2011. /**
  2012. * is_subdir - is new dentry a subdirectory of old_dentry
  2013. * @new_dentry: new dentry
  2014. * @old_dentry: old dentry
  2015. *
  2016. * Returns 1 if new_dentry is a subdirectory of the parent (at any depth).
  2017. * Returns 0 otherwise.
  2018. * Caller must ensure that "new_dentry" is pinned before calling is_subdir()
  2019. */
  2020. int is_subdir(struct dentry *new_dentry, struct dentry *old_dentry)
  2021. {
  2022. int result;
  2023. unsigned long seq;
  2024. if (new_dentry == old_dentry)
  2025. return 1;
  2026. /*
  2027. * Need rcu_readlock to protect against the d_parent trashing
  2028. * due to d_move
  2029. */
  2030. rcu_read_lock();
  2031. do {
  2032. /* for restarting inner loop in case of seq retry */
  2033. seq = read_seqbegin(&rename_lock);
  2034. if (d_ancestor(old_dentry, new_dentry))
  2035. result = 1;
  2036. else
  2037. result = 0;
  2038. } while (read_seqretry(&rename_lock, seq));
  2039. rcu_read_unlock();
  2040. return result;
  2041. }
  2042. int path_is_under(struct path *path1, struct path *path2)
  2043. {
  2044. struct vfsmount *mnt = path1->mnt;
  2045. struct dentry *dentry = path1->dentry;
  2046. int res;
  2047. spin_lock(&vfsmount_lock);
  2048. if (mnt != path2->mnt) {
  2049. for (;;) {
  2050. if (mnt->mnt_parent == mnt) {
  2051. spin_unlock(&vfsmount_lock);
  2052. return 0;
  2053. }
  2054. if (mnt->mnt_parent == path2->mnt)
  2055. break;
  2056. mnt = mnt->mnt_parent;
  2057. }
  2058. dentry = mnt->mnt_mountpoint;
  2059. }
  2060. res = is_subdir(dentry, path2->dentry);
  2061. spin_unlock(&vfsmount_lock);
  2062. return res;
  2063. }
  2064. EXPORT_SYMBOL(path_is_under);
  2065. void d_genocide(struct dentry *root)
  2066. {
  2067. struct dentry *this_parent = root;
  2068. struct list_head *next;
  2069. spin_lock(&dcache_lock);
  2070. repeat:
  2071. next = this_parent->d_subdirs.next;
  2072. resume:
  2073. while (next != &this_parent->d_subdirs) {
  2074. struct list_head *tmp = next;
  2075. struct dentry *dentry = list_entry(tmp, struct dentry, d_u.d_child);
  2076. next = tmp->next;
  2077. if (d_unhashed(dentry)||!dentry->d_inode)
  2078. continue;
  2079. if (!list_empty(&dentry->d_subdirs)) {
  2080. this_parent = dentry;
  2081. goto repeat;
  2082. }
  2083. atomic_dec(&dentry->d_count);
  2084. }
  2085. if (this_parent != root) {
  2086. next = this_parent->d_u.d_child.next;
  2087. atomic_dec(&this_parent->d_count);
  2088. this_parent = this_parent->d_parent;
  2089. goto resume;
  2090. }
  2091. spin_unlock(&dcache_lock);
  2092. }
  2093. /**
  2094. * find_inode_number - check for dentry with name
  2095. * @dir: directory to check
  2096. * @name: Name to find.
  2097. *
  2098. * Check whether a dentry already exists for the given name,
  2099. * and return the inode number if it has an inode. Otherwise
  2100. * 0 is returned.
  2101. *
  2102. * This routine is used to post-process directory listings for
  2103. * filesystems using synthetic inode numbers, and is necessary
  2104. * to keep getcwd() working.
  2105. */
  2106. ino_t find_inode_number(struct dentry *dir, struct qstr *name)
  2107. {
  2108. struct dentry * dentry;
  2109. ino_t ino = 0;
  2110. dentry = d_hash_and_lookup(dir, name);
  2111. if (dentry) {
  2112. if (dentry->d_inode)
  2113. ino = dentry->d_inode->i_ino;
  2114. dput(dentry);
  2115. }
  2116. return ino;
  2117. }
  2118. EXPORT_SYMBOL(find_inode_number);
  2119. static __initdata unsigned long dhash_entries;
  2120. static int __init set_dhash_entries(char *str)
  2121. {
  2122. if (!str)
  2123. return 0;
  2124. dhash_entries = simple_strtoul(str, &str, 0);
  2125. return 1;
  2126. }
  2127. __setup("dhash_entries=", set_dhash_entries);
  2128. static void __init dcache_init_early(void)
  2129. {
  2130. int loop;
  2131. /* If hashes are distributed across NUMA nodes, defer
  2132. * hash allocation until vmalloc space is available.
  2133. */
  2134. if (hashdist)
  2135. return;
  2136. dentry_hashtable =
  2137. alloc_large_system_hash("Dentry cache",
  2138. sizeof(struct hlist_head),
  2139. dhash_entries,
  2140. 13,
  2141. HASH_EARLY,
  2142. &d_hash_shift,
  2143. &d_hash_mask,
  2144. 0);
  2145. for (loop = 0; loop < (1 << d_hash_shift); loop++)
  2146. INIT_HLIST_HEAD(&dentry_hashtable[loop]);
  2147. }
  2148. static void __init dcache_init(void)
  2149. {
  2150. int loop;
  2151. /*
  2152. * A constructor could be added for stable state like the lists,
  2153. * but it is probably not worth it because of the cache nature
  2154. * of the dcache.
  2155. */
  2156. dentry_cache = KMEM_CACHE(dentry,
  2157. SLAB_RECLAIM_ACCOUNT|SLAB_PANIC|SLAB_MEM_SPREAD);
  2158. register_shrinker(&dcache_shrinker);
  2159. /* Hash may have been set up in dcache_init_early */
  2160. if (!hashdist)
  2161. return;
  2162. dentry_hashtable =
  2163. alloc_large_system_hash("Dentry cache",
  2164. sizeof(struct hlist_head),
  2165. dhash_entries,
  2166. 13,
  2167. 0,
  2168. &d_hash_shift,
  2169. &d_hash_mask,
  2170. 0);
  2171. for (loop = 0; loop < (1 << d_hash_shift); loop++)
  2172. INIT_HLIST_HEAD(&dentry_hashtable[loop]);
  2173. }
  2174. /* SLAB cache for __getname() consumers */
  2175. struct kmem_cache *names_cachep __read_mostly;
  2176. EXPORT_SYMBOL(names_cachep);
  2177. EXPORT_SYMBOL(d_genocide);
  2178. void __init vfs_caches_init_early(void)
  2179. {
  2180. dcache_init_early();
  2181. inode_init_early();
  2182. }
  2183. void __init vfs_caches_init(unsigned long mempages)
  2184. {
  2185. unsigned long reserve;
  2186. /* Base hash sizes on available memory, with a reserve equal to
  2187. 150% of current kernel size */
  2188. reserve = min((mempages - nr_free_pages()) * 3/2, mempages - 1);
  2189. mempages -= reserve;
  2190. names_cachep = kmem_cache_create("names_cache", PATH_MAX, 0,
  2191. SLAB_HWCACHE_ALIGN|SLAB_PANIC, NULL);
  2192. dcache_init();
  2193. inode_init();
  2194. files_init(mempages);
  2195. mnt_init();
  2196. bdev_cache_init();
  2197. chrdev_init();
  2198. }