dcache.c 78 KB

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