dcache.c 63 KB

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