dcache.c 64 KB

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