dcache.c 71 KB

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