dcache.c 78 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038203920402041204220432044204520462047204820492050205120522053205420552056205720582059206020612062206320642065206620672068206920702071207220732074207520762077207820792080208120822083208420852086208720882089209020912092209320942095209620972098209921002101210221032104210521062107210821092110211121122113211421152116211721182119212021212122212321242125212621272128212921302131213221332134213521362137213821392140214121422143214421452146214721482149215021512152215321542155215621572158215921602161216221632164216521662167216821692170217121722173217421752176217721782179218021812182218321842185218621872188218921902191219221932194219521962197219821992200220122022203220422052206220722082209221022112212221322142215221622172218221922202221222222232224222522262227222822292230223122322233223422352236223722382239224022412242224322442245224622472248224922502251225222532254225522562257225822592260226122622263226422652266226722682269227022712272227322742275227622772278227922802281228222832284228522862287228822892290229122922293229422952296229722982299230023012302230323042305230623072308230923102311231223132314231523162317231823192320232123222323232423252326232723282329233023312332233323342335233623372338233923402341234223432344234523462347234823492350235123522353235423552356235723582359236023612362236323642365236623672368236923702371237223732374237523762377237823792380238123822383238423852386238723882389239023912392239323942395239623972398239924002401240224032404240524062407240824092410241124122413241424152416241724182419242024212422242324242425242624272428242924302431243224332434243524362437243824392440244124422443244424452446244724482449245024512452245324542455245624572458245924602461246224632464246524662467246824692470247124722473247424752476247724782479248024812482248324842485248624872488248924902491249224932494249524962497249824992500250125022503250425052506250725082509251025112512251325142515251625172518251925202521252225232524252525262527252825292530253125322533253425352536253725382539254025412542254325442545254625472548254925502551255225532554255525562557255825592560256125622563256425652566256725682569257025712572257325742575257625772578257925802581258225832584258525862587258825892590259125922593259425952596259725982599260026012602260326042605260626072608260926102611261226132614261526162617261826192620262126222623262426252626262726282629263026312632263326342635263626372638263926402641264226432644264526462647264826492650265126522653265426552656265726582659266026612662266326642665266626672668266926702671267226732674267526762677267826792680268126822683268426852686268726882689269026912692269326942695269626972698269927002701270227032704270527062707270827092710271127122713271427152716271727182719272027212722272327242725272627272728272927302731273227332734273527362737273827392740274127422743274427452746274727482749275027512752275327542755275627572758275927602761276227632764276527662767276827692770277127722773277427752776277727782779278027812782278327842785278627872788278927902791279227932794279527962797279827992800280128022803280428052806280728082809281028112812281328142815281628172818281928202821282228232824282528262827282828292830283128322833283428352836283728382839284028412842284328442845284628472848284928502851285228532854285528562857285828592860286128622863286428652866286728682869287028712872287328742875287628772878287928802881288228832884288528862887288828892890289128922893289428952896289728982899290029012902290329042905290629072908290929102911291229132914291529162917291829192920292129222923292429252926292729282929293029312932293329342935293629372938293929402941294229432944294529462947294829492950295129522953295429552956295729582959296029612962296329642965296629672968296929702971297229732974297529762977297829792980298129822983298429852986298729882989299029912992299329942995299629972998299930003001300230033004300530063007300830093010301130123013301430153016301730183019302030213022302330243025302630273028302930303031303230333034303530363037303830393040304130423043304430453046304730483049305030513052305330543055305630573058305930603061306230633064306530663067306830693070307130723073307430753076307730783079308030813082308330843085308630873088308930903091309230933094309530963097309830993100
  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. /*
  1641. * This seqcount check is required to ensure name and
  1642. * len are loaded atomically, so as not to walk off the
  1643. * edge of memory when walking. If we could load this
  1644. * atomically some other way, we could drop this check.
  1645. */
  1646. if (read_seqcount_retry(&dentry->d_seq, *seq))
  1647. goto seqretry;
  1648. if (parent->d_flags & DCACHE_OP_COMPARE) {
  1649. if (parent->d_op->d_compare(parent, *inode,
  1650. dentry, i,
  1651. tlen, tname, name))
  1652. continue;
  1653. } else {
  1654. if (dentry_cmp(tname, tlen, str, len))
  1655. continue;
  1656. }
  1657. /*
  1658. * No extra seqcount check is required after the name
  1659. * compare. The caller must perform a seqcount check in
  1660. * order to do anything useful with the returned dentry
  1661. * anyway.
  1662. */
  1663. *inode = i;
  1664. return dentry;
  1665. }
  1666. return NULL;
  1667. }
  1668. /**
  1669. * d_lookup - search for a dentry
  1670. * @parent: parent dentry
  1671. * @name: qstr of name we wish to find
  1672. * Returns: dentry, or NULL
  1673. *
  1674. * d_lookup searches the children of the parent dentry for the name in
  1675. * question. If the dentry is found its reference count is incremented and the
  1676. * dentry is returned. The caller must use dput to free the entry when it has
  1677. * finished using it. %NULL is returned if the dentry does not exist.
  1678. */
  1679. struct dentry *d_lookup(struct dentry *parent, struct qstr *name)
  1680. {
  1681. struct dentry *dentry;
  1682. unsigned seq;
  1683. do {
  1684. seq = read_seqbegin(&rename_lock);
  1685. dentry = __d_lookup(parent, name);
  1686. if (dentry)
  1687. break;
  1688. } while (read_seqretry(&rename_lock, seq));
  1689. return dentry;
  1690. }
  1691. EXPORT_SYMBOL(d_lookup);
  1692. /**
  1693. * __d_lookup - search for a dentry (racy)
  1694. * @parent: parent dentry
  1695. * @name: qstr of name we wish to find
  1696. * Returns: dentry, or NULL
  1697. *
  1698. * __d_lookup is like d_lookup, however it may (rarely) return a
  1699. * false-negative result due to unrelated rename activity.
  1700. *
  1701. * __d_lookup is slightly faster by avoiding rename_lock read seqlock,
  1702. * however it must be used carefully, eg. with a following d_lookup in
  1703. * the case of failure.
  1704. *
  1705. * __d_lookup callers must be commented.
  1706. */
  1707. struct dentry *__d_lookup(struct dentry *parent, struct qstr *name)
  1708. {
  1709. unsigned int len = name->len;
  1710. unsigned int hash = name->hash;
  1711. const unsigned char *str = name->name;
  1712. struct hlist_bl_head *b = d_hash(parent, hash);
  1713. struct hlist_bl_node *node;
  1714. struct dentry *found = NULL;
  1715. struct dentry *dentry;
  1716. /*
  1717. * Note: There is significant duplication with __d_lookup_rcu which is
  1718. * required to prevent single threaded performance regressions
  1719. * especially on architectures where smp_rmb (in seqcounts) are costly.
  1720. * Keep the two functions in sync.
  1721. */
  1722. /*
  1723. * The hash list is protected using RCU.
  1724. *
  1725. * Take d_lock when comparing a candidate dentry, to avoid races
  1726. * with d_move().
  1727. *
  1728. * It is possible that concurrent renames can mess up our list
  1729. * walk here and result in missing our dentry, resulting in the
  1730. * false-negative result. d_lookup() protects against concurrent
  1731. * renames using rename_lock seqlock.
  1732. *
  1733. * See Documentation/filesystems/path-lookup.txt for more details.
  1734. */
  1735. rcu_read_lock();
  1736. hlist_bl_for_each_entry_rcu(dentry, node, b, d_hash) {
  1737. const char *tname;
  1738. int tlen;
  1739. if (dentry->d_name.hash != hash)
  1740. continue;
  1741. spin_lock(&dentry->d_lock);
  1742. if (dentry->d_parent != parent)
  1743. goto next;
  1744. if (d_unhashed(dentry))
  1745. goto next;
  1746. /*
  1747. * It is safe to compare names since d_move() cannot
  1748. * change the qstr (protected by d_lock).
  1749. */
  1750. tlen = dentry->d_name.len;
  1751. tname = dentry->d_name.name;
  1752. if (parent->d_flags & DCACHE_OP_COMPARE) {
  1753. if (parent->d_op->d_compare(parent, parent->d_inode,
  1754. dentry, dentry->d_inode,
  1755. tlen, tname, name))
  1756. goto next;
  1757. } else {
  1758. if (dentry_cmp(tname, tlen, str, len))
  1759. goto next;
  1760. }
  1761. dentry->d_count++;
  1762. found = dentry;
  1763. spin_unlock(&dentry->d_lock);
  1764. break;
  1765. next:
  1766. spin_unlock(&dentry->d_lock);
  1767. }
  1768. rcu_read_unlock();
  1769. return found;
  1770. }
  1771. /**
  1772. * d_hash_and_lookup - hash the qstr then search for a dentry
  1773. * @dir: Directory to search in
  1774. * @name: qstr of name we wish to find
  1775. *
  1776. * On hash failure or on lookup failure NULL is returned.
  1777. */
  1778. struct dentry *d_hash_and_lookup(struct dentry *dir, struct qstr *name)
  1779. {
  1780. struct dentry *dentry = NULL;
  1781. /*
  1782. * Check for a fs-specific hash function. Note that we must
  1783. * calculate the standard hash first, as the d_op->d_hash()
  1784. * routine may choose to leave the hash value unchanged.
  1785. */
  1786. name->hash = full_name_hash(name->name, name->len);
  1787. if (dir->d_flags & DCACHE_OP_HASH) {
  1788. if (dir->d_op->d_hash(dir, dir->d_inode, name) < 0)
  1789. goto out;
  1790. }
  1791. dentry = d_lookup(dir, name);
  1792. out:
  1793. return dentry;
  1794. }
  1795. /**
  1796. * d_validate - verify dentry provided from insecure source (deprecated)
  1797. * @dentry: The dentry alleged to be valid child of @dparent
  1798. * @dparent: The parent dentry (known to be valid)
  1799. *
  1800. * An insecure source has sent us a dentry, here we verify it and dget() it.
  1801. * This is used by ncpfs in its readdir implementation.
  1802. * Zero is returned in the dentry is invalid.
  1803. *
  1804. * This function is slow for big directories, and deprecated, do not use it.
  1805. */
  1806. int d_validate(struct dentry *dentry, struct dentry *dparent)
  1807. {
  1808. struct dentry *child;
  1809. spin_lock(&dparent->d_lock);
  1810. list_for_each_entry(child, &dparent->d_subdirs, d_u.d_child) {
  1811. if (dentry == child) {
  1812. spin_lock_nested(&dentry->d_lock, DENTRY_D_LOCK_NESTED);
  1813. __dget_dlock(dentry);
  1814. spin_unlock(&dentry->d_lock);
  1815. spin_unlock(&dparent->d_lock);
  1816. return 1;
  1817. }
  1818. }
  1819. spin_unlock(&dparent->d_lock);
  1820. return 0;
  1821. }
  1822. EXPORT_SYMBOL(d_validate);
  1823. /*
  1824. * When a file is deleted, we have two options:
  1825. * - turn this dentry into a negative dentry
  1826. * - unhash this dentry and free it.
  1827. *
  1828. * Usually, we want to just turn this into
  1829. * a negative dentry, but if anybody else is
  1830. * currently using the dentry or the inode
  1831. * we can't do that and we fall back on removing
  1832. * it from the hash queues and waiting for
  1833. * it to be deleted later when it has no users
  1834. */
  1835. /**
  1836. * d_delete - delete a dentry
  1837. * @dentry: The dentry to delete
  1838. *
  1839. * Turn the dentry into a negative dentry if possible, otherwise
  1840. * remove it from the hash queues so it can be deleted later
  1841. */
  1842. void d_delete(struct dentry * dentry)
  1843. {
  1844. struct inode *inode;
  1845. int isdir = 0;
  1846. /*
  1847. * Are we the only user?
  1848. */
  1849. again:
  1850. spin_lock(&dentry->d_lock);
  1851. inode = dentry->d_inode;
  1852. isdir = S_ISDIR(inode->i_mode);
  1853. if (dentry->d_count == 1) {
  1854. if (inode && !spin_trylock(&inode->i_lock)) {
  1855. spin_unlock(&dentry->d_lock);
  1856. cpu_relax();
  1857. goto again;
  1858. }
  1859. dentry->d_flags &= ~DCACHE_CANT_MOUNT;
  1860. dentry_unlink_inode(dentry);
  1861. fsnotify_nameremove(dentry, isdir);
  1862. return;
  1863. }
  1864. if (!d_unhashed(dentry))
  1865. __d_drop(dentry);
  1866. spin_unlock(&dentry->d_lock);
  1867. fsnotify_nameremove(dentry, isdir);
  1868. }
  1869. EXPORT_SYMBOL(d_delete);
  1870. static void __d_rehash(struct dentry * entry, struct hlist_bl_head *b)
  1871. {
  1872. BUG_ON(!d_unhashed(entry));
  1873. hlist_bl_lock(b);
  1874. entry->d_flags |= DCACHE_RCUACCESS;
  1875. hlist_bl_add_head_rcu(&entry->d_hash, b);
  1876. hlist_bl_unlock(b);
  1877. }
  1878. static void _d_rehash(struct dentry * entry)
  1879. {
  1880. __d_rehash(entry, d_hash(entry->d_parent, entry->d_name.hash));
  1881. }
  1882. /**
  1883. * d_rehash - add an entry back to the hash
  1884. * @entry: dentry to add to the hash
  1885. *
  1886. * Adds a dentry to the hash according to its name.
  1887. */
  1888. void d_rehash(struct dentry * entry)
  1889. {
  1890. spin_lock(&entry->d_lock);
  1891. _d_rehash(entry);
  1892. spin_unlock(&entry->d_lock);
  1893. }
  1894. EXPORT_SYMBOL(d_rehash);
  1895. /**
  1896. * dentry_update_name_case - update case insensitive dentry with a new name
  1897. * @dentry: dentry to be updated
  1898. * @name: new name
  1899. *
  1900. * Update a case insensitive dentry with new case of name.
  1901. *
  1902. * dentry must have been returned by d_lookup with name @name. Old and new
  1903. * name lengths must match (ie. no d_compare which allows mismatched name
  1904. * lengths).
  1905. *
  1906. * Parent inode i_mutex must be held over d_lookup and into this call (to
  1907. * keep renames and concurrent inserts, and readdir(2) away).
  1908. */
  1909. void dentry_update_name_case(struct dentry *dentry, struct qstr *name)
  1910. {
  1911. BUG_ON(!mutex_is_locked(&dentry->d_parent->d_inode->i_mutex));
  1912. BUG_ON(dentry->d_name.len != name->len); /* d_lookup gives this */
  1913. spin_lock(&dentry->d_lock);
  1914. write_seqcount_begin(&dentry->d_seq);
  1915. memcpy((unsigned char *)dentry->d_name.name, name->name, name->len);
  1916. write_seqcount_end(&dentry->d_seq);
  1917. spin_unlock(&dentry->d_lock);
  1918. }
  1919. EXPORT_SYMBOL(dentry_update_name_case);
  1920. static void switch_names(struct dentry *dentry, struct dentry *target)
  1921. {
  1922. if (dname_external(target)) {
  1923. if (dname_external(dentry)) {
  1924. /*
  1925. * Both external: swap the pointers
  1926. */
  1927. swap(target->d_name.name, dentry->d_name.name);
  1928. } else {
  1929. /*
  1930. * dentry:internal, target:external. Steal target's
  1931. * storage and make target internal.
  1932. */
  1933. memcpy(target->d_iname, dentry->d_name.name,
  1934. dentry->d_name.len + 1);
  1935. dentry->d_name.name = target->d_name.name;
  1936. target->d_name.name = target->d_iname;
  1937. }
  1938. } else {
  1939. if (dname_external(dentry)) {
  1940. /*
  1941. * dentry:external, target:internal. Give dentry's
  1942. * storage to target and make dentry internal
  1943. */
  1944. memcpy(dentry->d_iname, target->d_name.name,
  1945. target->d_name.len + 1);
  1946. target->d_name.name = dentry->d_name.name;
  1947. dentry->d_name.name = dentry->d_iname;
  1948. } else {
  1949. /*
  1950. * Both are internal. Just copy target to dentry
  1951. */
  1952. memcpy(dentry->d_iname, target->d_name.name,
  1953. target->d_name.len + 1);
  1954. dentry->d_name.len = target->d_name.len;
  1955. return;
  1956. }
  1957. }
  1958. swap(dentry->d_name.len, target->d_name.len);
  1959. }
  1960. static void dentry_lock_for_move(struct dentry *dentry, struct dentry *target)
  1961. {
  1962. /*
  1963. * XXXX: do we really need to take target->d_lock?
  1964. */
  1965. if (IS_ROOT(dentry) || dentry->d_parent == target->d_parent)
  1966. spin_lock(&target->d_parent->d_lock);
  1967. else {
  1968. if (d_ancestor(dentry->d_parent, target->d_parent)) {
  1969. spin_lock(&dentry->d_parent->d_lock);
  1970. spin_lock_nested(&target->d_parent->d_lock,
  1971. DENTRY_D_LOCK_NESTED);
  1972. } else {
  1973. spin_lock(&target->d_parent->d_lock);
  1974. spin_lock_nested(&dentry->d_parent->d_lock,
  1975. DENTRY_D_LOCK_NESTED);
  1976. }
  1977. }
  1978. if (target < dentry) {
  1979. spin_lock_nested(&target->d_lock, 2);
  1980. spin_lock_nested(&dentry->d_lock, 3);
  1981. } else {
  1982. spin_lock_nested(&dentry->d_lock, 2);
  1983. spin_lock_nested(&target->d_lock, 3);
  1984. }
  1985. }
  1986. static void dentry_unlock_parents_for_move(struct dentry *dentry,
  1987. struct dentry *target)
  1988. {
  1989. if (target->d_parent != dentry->d_parent)
  1990. spin_unlock(&dentry->d_parent->d_lock);
  1991. if (target->d_parent != target)
  1992. spin_unlock(&target->d_parent->d_lock);
  1993. }
  1994. /*
  1995. * When switching names, the actual string doesn't strictly have to
  1996. * be preserved in the target - because we're dropping the target
  1997. * anyway. As such, we can just do a simple memcpy() to copy over
  1998. * the new name before we switch.
  1999. *
  2000. * Note that we have to be a lot more careful about getting the hash
  2001. * switched - we have to switch the hash value properly even if it
  2002. * then no longer matches the actual (corrupted) string of the target.
  2003. * The hash value has to match the hash queue that the dentry is on..
  2004. */
  2005. /*
  2006. * __d_move - move a dentry
  2007. * @dentry: entry to move
  2008. * @target: new dentry
  2009. *
  2010. * Update the dcache to reflect the move of a file name. Negative
  2011. * dcache entries should not be moved in this way. Caller hold
  2012. * rename_lock.
  2013. */
  2014. static void __d_move(struct dentry * dentry, struct dentry * target)
  2015. {
  2016. if (!dentry->d_inode)
  2017. printk(KERN_WARNING "VFS: moving negative dcache entry\n");
  2018. BUG_ON(d_ancestor(dentry, target));
  2019. BUG_ON(d_ancestor(target, dentry));
  2020. dentry_lock_for_move(dentry, target);
  2021. write_seqcount_begin(&dentry->d_seq);
  2022. write_seqcount_begin(&target->d_seq);
  2023. /* __d_drop does write_seqcount_barrier, but they're OK to nest. */
  2024. /*
  2025. * Move the dentry to the target hash queue. Don't bother checking
  2026. * for the same hash queue because of how unlikely it is.
  2027. */
  2028. __d_drop(dentry);
  2029. __d_rehash(dentry, d_hash(target->d_parent, target->d_name.hash));
  2030. /* Unhash the target: dput() will then get rid of it */
  2031. __d_drop(target);
  2032. list_del(&dentry->d_u.d_child);
  2033. list_del(&target->d_u.d_child);
  2034. /* Switch the names.. */
  2035. switch_names(dentry, target);
  2036. swap(dentry->d_name.hash, target->d_name.hash);
  2037. /* ... and switch the parents */
  2038. if (IS_ROOT(dentry)) {
  2039. dentry->d_parent = target->d_parent;
  2040. target->d_parent = target;
  2041. INIT_LIST_HEAD(&target->d_u.d_child);
  2042. } else {
  2043. swap(dentry->d_parent, target->d_parent);
  2044. /* And add them back to the (new) parent lists */
  2045. list_add(&target->d_u.d_child, &target->d_parent->d_subdirs);
  2046. }
  2047. list_add(&dentry->d_u.d_child, &dentry->d_parent->d_subdirs);
  2048. write_seqcount_end(&target->d_seq);
  2049. write_seqcount_end(&dentry->d_seq);
  2050. dentry_unlock_parents_for_move(dentry, target);
  2051. spin_unlock(&target->d_lock);
  2052. fsnotify_d_move(dentry);
  2053. spin_unlock(&dentry->d_lock);
  2054. }
  2055. /*
  2056. * d_move - move a dentry
  2057. * @dentry: entry to move
  2058. * @target: new dentry
  2059. *
  2060. * Update the dcache to reflect the move of a file name. Negative
  2061. * dcache entries should not be moved in this way.
  2062. */
  2063. void d_move(struct dentry *dentry, struct dentry *target)
  2064. {
  2065. write_seqlock(&rename_lock);
  2066. __d_move(dentry, target);
  2067. write_sequnlock(&rename_lock);
  2068. }
  2069. EXPORT_SYMBOL(d_move);
  2070. /**
  2071. * d_ancestor - search for an ancestor
  2072. * @p1: ancestor dentry
  2073. * @p2: child dentry
  2074. *
  2075. * Returns the ancestor dentry of p2 which is a child of p1, if p1 is
  2076. * an ancestor of p2, else NULL.
  2077. */
  2078. struct dentry *d_ancestor(struct dentry *p1, struct dentry *p2)
  2079. {
  2080. struct dentry *p;
  2081. for (p = p2; !IS_ROOT(p); p = p->d_parent) {
  2082. if (p->d_parent == p1)
  2083. return p;
  2084. }
  2085. return NULL;
  2086. }
  2087. /*
  2088. * This helper attempts to cope with remotely renamed directories
  2089. *
  2090. * It assumes that the caller is already holding
  2091. * dentry->d_parent->d_inode->i_mutex, inode->i_lock and rename_lock
  2092. *
  2093. * Note: If ever the locking in lock_rename() changes, then please
  2094. * remember to update this too...
  2095. */
  2096. static struct dentry *__d_unalias(struct inode *inode,
  2097. struct dentry *dentry, struct dentry *alias)
  2098. {
  2099. struct mutex *m1 = NULL, *m2 = NULL;
  2100. struct dentry *ret;
  2101. /* If alias and dentry share a parent, then no extra locks required */
  2102. if (alias->d_parent == dentry->d_parent)
  2103. goto out_unalias;
  2104. /* See lock_rename() */
  2105. ret = ERR_PTR(-EBUSY);
  2106. if (!mutex_trylock(&dentry->d_sb->s_vfs_rename_mutex))
  2107. goto out_err;
  2108. m1 = &dentry->d_sb->s_vfs_rename_mutex;
  2109. if (!mutex_trylock(&alias->d_parent->d_inode->i_mutex))
  2110. goto out_err;
  2111. m2 = &alias->d_parent->d_inode->i_mutex;
  2112. out_unalias:
  2113. __d_move(alias, dentry);
  2114. ret = alias;
  2115. out_err:
  2116. spin_unlock(&inode->i_lock);
  2117. if (m2)
  2118. mutex_unlock(m2);
  2119. if (m1)
  2120. mutex_unlock(m1);
  2121. return ret;
  2122. }
  2123. /*
  2124. * Prepare an anonymous dentry for life in the superblock's dentry tree as a
  2125. * named dentry in place of the dentry to be replaced.
  2126. * returns with anon->d_lock held!
  2127. */
  2128. static void __d_materialise_dentry(struct dentry *dentry, struct dentry *anon)
  2129. {
  2130. struct dentry *dparent, *aparent;
  2131. dentry_lock_for_move(anon, dentry);
  2132. write_seqcount_begin(&dentry->d_seq);
  2133. write_seqcount_begin(&anon->d_seq);
  2134. dparent = dentry->d_parent;
  2135. aparent = anon->d_parent;
  2136. switch_names(dentry, anon);
  2137. swap(dentry->d_name.hash, anon->d_name.hash);
  2138. dentry->d_parent = (aparent == anon) ? dentry : aparent;
  2139. list_del(&dentry->d_u.d_child);
  2140. if (!IS_ROOT(dentry))
  2141. list_add(&dentry->d_u.d_child, &dentry->d_parent->d_subdirs);
  2142. else
  2143. INIT_LIST_HEAD(&dentry->d_u.d_child);
  2144. anon->d_parent = (dparent == dentry) ? anon : dparent;
  2145. list_del(&anon->d_u.d_child);
  2146. if (!IS_ROOT(anon))
  2147. list_add(&anon->d_u.d_child, &anon->d_parent->d_subdirs);
  2148. else
  2149. INIT_LIST_HEAD(&anon->d_u.d_child);
  2150. write_seqcount_end(&dentry->d_seq);
  2151. write_seqcount_end(&anon->d_seq);
  2152. dentry_unlock_parents_for_move(anon, dentry);
  2153. spin_unlock(&dentry->d_lock);
  2154. /* anon->d_lock still locked, returns locked */
  2155. anon->d_flags &= ~DCACHE_DISCONNECTED;
  2156. }
  2157. /**
  2158. * d_materialise_unique - introduce an inode into the tree
  2159. * @dentry: candidate dentry
  2160. * @inode: inode to bind to the dentry, to which aliases may be attached
  2161. *
  2162. * Introduces an dentry into the tree, substituting an extant disconnected
  2163. * root directory alias in its place if there is one
  2164. */
  2165. struct dentry *d_materialise_unique(struct dentry *dentry, struct inode *inode)
  2166. {
  2167. struct dentry *actual;
  2168. BUG_ON(!d_unhashed(dentry));
  2169. if (!inode) {
  2170. actual = dentry;
  2171. __d_instantiate(dentry, NULL);
  2172. d_rehash(actual);
  2173. goto out_nolock;
  2174. }
  2175. spin_lock(&inode->i_lock);
  2176. if (S_ISDIR(inode->i_mode)) {
  2177. struct dentry *alias;
  2178. /* Does an aliased dentry already exist? */
  2179. alias = __d_find_alias(inode, 0);
  2180. if (alias) {
  2181. actual = alias;
  2182. write_seqlock(&rename_lock);
  2183. if (d_ancestor(alias, dentry)) {
  2184. /* Check for loops */
  2185. actual = ERR_PTR(-ELOOP);
  2186. } else if (IS_ROOT(alias)) {
  2187. /* Is this an anonymous mountpoint that we
  2188. * could splice into our tree? */
  2189. __d_materialise_dentry(dentry, alias);
  2190. write_sequnlock(&rename_lock);
  2191. __d_drop(alias);
  2192. goto found;
  2193. } else {
  2194. /* Nope, but we must(!) avoid directory
  2195. * aliasing */
  2196. actual = __d_unalias(inode, dentry, alias);
  2197. }
  2198. write_sequnlock(&rename_lock);
  2199. if (IS_ERR(actual))
  2200. dput(alias);
  2201. goto out_nolock;
  2202. }
  2203. }
  2204. /* Add a unique reference */
  2205. actual = __d_instantiate_unique(dentry, inode);
  2206. if (!actual)
  2207. actual = dentry;
  2208. else
  2209. BUG_ON(!d_unhashed(actual));
  2210. spin_lock(&actual->d_lock);
  2211. found:
  2212. _d_rehash(actual);
  2213. spin_unlock(&actual->d_lock);
  2214. spin_unlock(&inode->i_lock);
  2215. out_nolock:
  2216. if (actual == dentry) {
  2217. security_d_instantiate(dentry, inode);
  2218. return NULL;
  2219. }
  2220. iput(inode);
  2221. return actual;
  2222. }
  2223. EXPORT_SYMBOL_GPL(d_materialise_unique);
  2224. static int prepend(char **buffer, int *buflen, const char *str, int namelen)
  2225. {
  2226. *buflen -= namelen;
  2227. if (*buflen < 0)
  2228. return -ENAMETOOLONG;
  2229. *buffer -= namelen;
  2230. memcpy(*buffer, str, namelen);
  2231. return 0;
  2232. }
  2233. static int prepend_name(char **buffer, int *buflen, struct qstr *name)
  2234. {
  2235. return prepend(buffer, buflen, name->name, name->len);
  2236. }
  2237. /**
  2238. * prepend_path - Prepend path string to a buffer
  2239. * @path: the dentry/vfsmount to report
  2240. * @root: root vfsmnt/dentry (may be modified by this function)
  2241. * @buffer: pointer to the end of the buffer
  2242. * @buflen: pointer to buffer length
  2243. *
  2244. * Caller holds the rename_lock.
  2245. *
  2246. * If path is not reachable from the supplied root, then the value of
  2247. * root is changed (without modifying refcounts).
  2248. */
  2249. static int prepend_path(const struct path *path, struct path *root,
  2250. char **buffer, int *buflen)
  2251. {
  2252. struct dentry *dentry = path->dentry;
  2253. struct vfsmount *vfsmnt = path->mnt;
  2254. bool slash = false;
  2255. int error = 0;
  2256. br_read_lock(vfsmount_lock);
  2257. while (dentry != root->dentry || vfsmnt != root->mnt) {
  2258. struct dentry * parent;
  2259. if (dentry == vfsmnt->mnt_root || IS_ROOT(dentry)) {
  2260. /* Global root? */
  2261. if (vfsmnt->mnt_parent == vfsmnt) {
  2262. goto global_root;
  2263. }
  2264. dentry = vfsmnt->mnt_mountpoint;
  2265. vfsmnt = vfsmnt->mnt_parent;
  2266. continue;
  2267. }
  2268. parent = dentry->d_parent;
  2269. prefetch(parent);
  2270. spin_lock(&dentry->d_lock);
  2271. error = prepend_name(buffer, buflen, &dentry->d_name);
  2272. spin_unlock(&dentry->d_lock);
  2273. if (!error)
  2274. error = prepend(buffer, buflen, "/", 1);
  2275. if (error)
  2276. break;
  2277. slash = true;
  2278. dentry = parent;
  2279. }
  2280. out:
  2281. if (!error && !slash)
  2282. error = prepend(buffer, buflen, "/", 1);
  2283. br_read_unlock(vfsmount_lock);
  2284. return error;
  2285. global_root:
  2286. /*
  2287. * Filesystems needing to implement special "root names"
  2288. * should do so with ->d_dname()
  2289. */
  2290. if (IS_ROOT(dentry) &&
  2291. (dentry->d_name.len != 1 || dentry->d_name.name[0] != '/')) {
  2292. WARN(1, "Root dentry has weird name <%.*s>\n",
  2293. (int) dentry->d_name.len, dentry->d_name.name);
  2294. }
  2295. root->mnt = vfsmnt;
  2296. root->dentry = dentry;
  2297. goto out;
  2298. }
  2299. /**
  2300. * __d_path - return the path of a dentry
  2301. * @path: the dentry/vfsmount to report
  2302. * @root: root vfsmnt/dentry (may be modified by this function)
  2303. * @buf: buffer to return value in
  2304. * @buflen: buffer length
  2305. *
  2306. * Convert a dentry into an ASCII path name.
  2307. *
  2308. * Returns a pointer into the buffer or an error code if the
  2309. * path was too long.
  2310. *
  2311. * "buflen" should be positive.
  2312. *
  2313. * If path is not reachable from the supplied root, then the value of
  2314. * root is changed (without modifying refcounts).
  2315. */
  2316. char *__d_path(const struct path *path, struct path *root,
  2317. char *buf, int buflen)
  2318. {
  2319. char *res = buf + buflen;
  2320. int error;
  2321. prepend(&res, &buflen, "\0", 1);
  2322. write_seqlock(&rename_lock);
  2323. error = prepend_path(path, root, &res, &buflen);
  2324. write_sequnlock(&rename_lock);
  2325. if (error)
  2326. return ERR_PTR(error);
  2327. return res;
  2328. }
  2329. /*
  2330. * same as __d_path but appends "(deleted)" for unlinked files.
  2331. */
  2332. static int path_with_deleted(const struct path *path, struct path *root,
  2333. char **buf, int *buflen)
  2334. {
  2335. prepend(buf, buflen, "\0", 1);
  2336. if (d_unlinked(path->dentry)) {
  2337. int error = prepend(buf, buflen, " (deleted)", 10);
  2338. if (error)
  2339. return error;
  2340. }
  2341. return prepend_path(path, root, buf, buflen);
  2342. }
  2343. static int prepend_unreachable(char **buffer, int *buflen)
  2344. {
  2345. return prepend(buffer, buflen, "(unreachable)", 13);
  2346. }
  2347. /**
  2348. * d_path - return the path of a dentry
  2349. * @path: path to report
  2350. * @buf: buffer to return value in
  2351. * @buflen: buffer length
  2352. *
  2353. * Convert a dentry into an ASCII path name. If the entry has been deleted
  2354. * the string " (deleted)" is appended. Note that this is ambiguous.
  2355. *
  2356. * Returns a pointer into the buffer or an error code if the path was
  2357. * too long. Note: Callers should use the returned pointer, not the passed
  2358. * in buffer, to use the name! The implementation often starts at an offset
  2359. * into the buffer, and may leave 0 bytes at the start.
  2360. *
  2361. * "buflen" should be positive.
  2362. */
  2363. char *d_path(const struct path *path, char *buf, int buflen)
  2364. {
  2365. char *res = buf + buflen;
  2366. struct path root;
  2367. struct path tmp;
  2368. int error;
  2369. /*
  2370. * We have various synthetic filesystems that never get mounted. On
  2371. * these filesystems dentries are never used for lookup purposes, and
  2372. * thus don't need to be hashed. They also don't need a name until a
  2373. * user wants to identify the object in /proc/pid/fd/. The little hack
  2374. * below allows us to generate a name for these objects on demand:
  2375. */
  2376. if (path->dentry->d_op && path->dentry->d_op->d_dname)
  2377. return path->dentry->d_op->d_dname(path->dentry, buf, buflen);
  2378. get_fs_root(current->fs, &root);
  2379. write_seqlock(&rename_lock);
  2380. tmp = root;
  2381. error = path_with_deleted(path, &tmp, &res, &buflen);
  2382. if (error)
  2383. res = ERR_PTR(error);
  2384. write_sequnlock(&rename_lock);
  2385. path_put(&root);
  2386. return res;
  2387. }
  2388. EXPORT_SYMBOL(d_path);
  2389. /**
  2390. * d_path_with_unreachable - return the path of a dentry
  2391. * @path: path to report
  2392. * @buf: buffer to return value in
  2393. * @buflen: buffer length
  2394. *
  2395. * The difference from d_path() is that this prepends "(unreachable)"
  2396. * to paths which are unreachable from the current process' root.
  2397. */
  2398. char *d_path_with_unreachable(const struct path *path, char *buf, int buflen)
  2399. {
  2400. char *res = buf + buflen;
  2401. struct path root;
  2402. struct path tmp;
  2403. int error;
  2404. if (path->dentry->d_op && path->dentry->d_op->d_dname)
  2405. return path->dentry->d_op->d_dname(path->dentry, buf, buflen);
  2406. get_fs_root(current->fs, &root);
  2407. write_seqlock(&rename_lock);
  2408. tmp = root;
  2409. error = path_with_deleted(path, &tmp, &res, &buflen);
  2410. if (!error && !path_equal(&tmp, &root))
  2411. error = prepend_unreachable(&res, &buflen);
  2412. write_sequnlock(&rename_lock);
  2413. path_put(&root);
  2414. if (error)
  2415. res = ERR_PTR(error);
  2416. return res;
  2417. }
  2418. /*
  2419. * Helper function for dentry_operations.d_dname() members
  2420. */
  2421. char *dynamic_dname(struct dentry *dentry, char *buffer, int buflen,
  2422. const char *fmt, ...)
  2423. {
  2424. va_list args;
  2425. char temp[64];
  2426. int sz;
  2427. va_start(args, fmt);
  2428. sz = vsnprintf(temp, sizeof(temp), fmt, args) + 1;
  2429. va_end(args);
  2430. if (sz > sizeof(temp) || sz > buflen)
  2431. return ERR_PTR(-ENAMETOOLONG);
  2432. buffer += buflen - sz;
  2433. return memcpy(buffer, temp, sz);
  2434. }
  2435. /*
  2436. * Write full pathname from the root of the filesystem into the buffer.
  2437. */
  2438. static char *__dentry_path(struct dentry *dentry, char *buf, int buflen)
  2439. {
  2440. char *end = buf + buflen;
  2441. char *retval;
  2442. prepend(&end, &buflen, "\0", 1);
  2443. if (buflen < 1)
  2444. goto Elong;
  2445. /* Get '/' right */
  2446. retval = end-1;
  2447. *retval = '/';
  2448. while (!IS_ROOT(dentry)) {
  2449. struct dentry *parent = dentry->d_parent;
  2450. int error;
  2451. prefetch(parent);
  2452. spin_lock(&dentry->d_lock);
  2453. error = prepend_name(&end, &buflen, &dentry->d_name);
  2454. spin_unlock(&dentry->d_lock);
  2455. if (error != 0 || prepend(&end, &buflen, "/", 1) != 0)
  2456. goto Elong;
  2457. retval = end;
  2458. dentry = parent;
  2459. }
  2460. return retval;
  2461. Elong:
  2462. return ERR_PTR(-ENAMETOOLONG);
  2463. }
  2464. char *dentry_path_raw(struct dentry *dentry, char *buf, int buflen)
  2465. {
  2466. char *retval;
  2467. write_seqlock(&rename_lock);
  2468. retval = __dentry_path(dentry, buf, buflen);
  2469. write_sequnlock(&rename_lock);
  2470. return retval;
  2471. }
  2472. EXPORT_SYMBOL(dentry_path_raw);
  2473. char *dentry_path(struct dentry *dentry, char *buf, int buflen)
  2474. {
  2475. char *p = NULL;
  2476. char *retval;
  2477. write_seqlock(&rename_lock);
  2478. if (d_unlinked(dentry)) {
  2479. p = buf + buflen;
  2480. if (prepend(&p, &buflen, "//deleted", 10) != 0)
  2481. goto Elong;
  2482. buflen++;
  2483. }
  2484. retval = __dentry_path(dentry, buf, buflen);
  2485. write_sequnlock(&rename_lock);
  2486. if (!IS_ERR(retval) && p)
  2487. *p = '/'; /* restore '/' overriden with '\0' */
  2488. return retval;
  2489. Elong:
  2490. return ERR_PTR(-ENAMETOOLONG);
  2491. }
  2492. /*
  2493. * NOTE! The user-level library version returns a
  2494. * character pointer. The kernel system call just
  2495. * returns the length of the buffer filled (which
  2496. * includes the ending '\0' character), or a negative
  2497. * error value. So libc would do something like
  2498. *
  2499. * char *getcwd(char * buf, size_t size)
  2500. * {
  2501. * int retval;
  2502. *
  2503. * retval = sys_getcwd(buf, size);
  2504. * if (retval >= 0)
  2505. * return buf;
  2506. * errno = -retval;
  2507. * return NULL;
  2508. * }
  2509. */
  2510. SYSCALL_DEFINE2(getcwd, char __user *, buf, unsigned long, size)
  2511. {
  2512. int error;
  2513. struct path pwd, root;
  2514. char *page = (char *) __get_free_page(GFP_USER);
  2515. if (!page)
  2516. return -ENOMEM;
  2517. get_fs_root_and_pwd(current->fs, &root, &pwd);
  2518. error = -ENOENT;
  2519. write_seqlock(&rename_lock);
  2520. if (!d_unlinked(pwd.dentry)) {
  2521. unsigned long len;
  2522. struct path tmp = root;
  2523. char *cwd = page + PAGE_SIZE;
  2524. int buflen = PAGE_SIZE;
  2525. prepend(&cwd, &buflen, "\0", 1);
  2526. error = prepend_path(&pwd, &tmp, &cwd, &buflen);
  2527. write_sequnlock(&rename_lock);
  2528. if (error)
  2529. goto out;
  2530. /* Unreachable from current root */
  2531. if (!path_equal(&tmp, &root)) {
  2532. error = prepend_unreachable(&cwd, &buflen);
  2533. if (error)
  2534. goto out;
  2535. }
  2536. error = -ERANGE;
  2537. len = PAGE_SIZE + page - cwd;
  2538. if (len <= size) {
  2539. error = len;
  2540. if (copy_to_user(buf, cwd, len))
  2541. error = -EFAULT;
  2542. }
  2543. } else {
  2544. write_sequnlock(&rename_lock);
  2545. }
  2546. out:
  2547. path_put(&pwd);
  2548. path_put(&root);
  2549. free_page((unsigned long) page);
  2550. return error;
  2551. }
  2552. /*
  2553. * Test whether new_dentry is a subdirectory of old_dentry.
  2554. *
  2555. * Trivially implemented using the dcache structure
  2556. */
  2557. /**
  2558. * is_subdir - is new dentry a subdirectory of old_dentry
  2559. * @new_dentry: new dentry
  2560. * @old_dentry: old dentry
  2561. *
  2562. * Returns 1 if new_dentry is a subdirectory of the parent (at any depth).
  2563. * Returns 0 otherwise.
  2564. * Caller must ensure that "new_dentry" is pinned before calling is_subdir()
  2565. */
  2566. int is_subdir(struct dentry *new_dentry, struct dentry *old_dentry)
  2567. {
  2568. int result;
  2569. unsigned seq;
  2570. if (new_dentry == old_dentry)
  2571. return 1;
  2572. do {
  2573. /* for restarting inner loop in case of seq retry */
  2574. seq = read_seqbegin(&rename_lock);
  2575. /*
  2576. * Need rcu_readlock to protect against the d_parent trashing
  2577. * due to d_move
  2578. */
  2579. rcu_read_lock();
  2580. if (d_ancestor(old_dentry, new_dentry))
  2581. result = 1;
  2582. else
  2583. result = 0;
  2584. rcu_read_unlock();
  2585. } while (read_seqretry(&rename_lock, seq));
  2586. return result;
  2587. }
  2588. int path_is_under(struct path *path1, struct path *path2)
  2589. {
  2590. struct vfsmount *mnt = path1->mnt;
  2591. struct dentry *dentry = path1->dentry;
  2592. int res;
  2593. br_read_lock(vfsmount_lock);
  2594. if (mnt != path2->mnt) {
  2595. for (;;) {
  2596. if (mnt->mnt_parent == mnt) {
  2597. br_read_unlock(vfsmount_lock);
  2598. return 0;
  2599. }
  2600. if (mnt->mnt_parent == path2->mnt)
  2601. break;
  2602. mnt = mnt->mnt_parent;
  2603. }
  2604. dentry = mnt->mnt_mountpoint;
  2605. }
  2606. res = is_subdir(dentry, path2->dentry);
  2607. br_read_unlock(vfsmount_lock);
  2608. return res;
  2609. }
  2610. EXPORT_SYMBOL(path_is_under);
  2611. void d_genocide(struct dentry *root)
  2612. {
  2613. struct dentry *this_parent;
  2614. struct list_head *next;
  2615. unsigned seq;
  2616. int locked = 0;
  2617. seq = read_seqbegin(&rename_lock);
  2618. again:
  2619. this_parent = root;
  2620. spin_lock(&this_parent->d_lock);
  2621. repeat:
  2622. next = this_parent->d_subdirs.next;
  2623. resume:
  2624. while (next != &this_parent->d_subdirs) {
  2625. struct list_head *tmp = next;
  2626. struct dentry *dentry = list_entry(tmp, struct dentry, d_u.d_child);
  2627. next = tmp->next;
  2628. spin_lock_nested(&dentry->d_lock, DENTRY_D_LOCK_NESTED);
  2629. if (d_unhashed(dentry) || !dentry->d_inode) {
  2630. spin_unlock(&dentry->d_lock);
  2631. continue;
  2632. }
  2633. if (!list_empty(&dentry->d_subdirs)) {
  2634. spin_unlock(&this_parent->d_lock);
  2635. spin_release(&dentry->d_lock.dep_map, 1, _RET_IP_);
  2636. this_parent = dentry;
  2637. spin_acquire(&this_parent->d_lock.dep_map, 0, 1, _RET_IP_);
  2638. goto repeat;
  2639. }
  2640. if (!(dentry->d_flags & DCACHE_GENOCIDE)) {
  2641. dentry->d_flags |= DCACHE_GENOCIDE;
  2642. dentry->d_count--;
  2643. }
  2644. spin_unlock(&dentry->d_lock);
  2645. }
  2646. if (this_parent != root) {
  2647. struct dentry *child = this_parent;
  2648. if (!(this_parent->d_flags & DCACHE_GENOCIDE)) {
  2649. this_parent->d_flags |= DCACHE_GENOCIDE;
  2650. this_parent->d_count--;
  2651. }
  2652. this_parent = try_to_ascend(this_parent, locked, seq);
  2653. if (!this_parent)
  2654. goto rename_retry;
  2655. next = child->d_u.d_child.next;
  2656. goto resume;
  2657. }
  2658. spin_unlock(&this_parent->d_lock);
  2659. if (!locked && read_seqretry(&rename_lock, seq))
  2660. goto rename_retry;
  2661. if (locked)
  2662. write_sequnlock(&rename_lock);
  2663. return;
  2664. rename_retry:
  2665. locked = 1;
  2666. write_seqlock(&rename_lock);
  2667. goto again;
  2668. }
  2669. /**
  2670. * find_inode_number - check for dentry with name
  2671. * @dir: directory to check
  2672. * @name: Name to find.
  2673. *
  2674. * Check whether a dentry already exists for the given name,
  2675. * and return the inode number if it has an inode. Otherwise
  2676. * 0 is returned.
  2677. *
  2678. * This routine is used to post-process directory listings for
  2679. * filesystems using synthetic inode numbers, and is necessary
  2680. * to keep getcwd() working.
  2681. */
  2682. ino_t find_inode_number(struct dentry *dir, struct qstr *name)
  2683. {
  2684. struct dentry * dentry;
  2685. ino_t ino = 0;
  2686. dentry = d_hash_and_lookup(dir, name);
  2687. if (dentry) {
  2688. if (dentry->d_inode)
  2689. ino = dentry->d_inode->i_ino;
  2690. dput(dentry);
  2691. }
  2692. return ino;
  2693. }
  2694. EXPORT_SYMBOL(find_inode_number);
  2695. static __initdata unsigned long dhash_entries;
  2696. static int __init set_dhash_entries(char *str)
  2697. {
  2698. if (!str)
  2699. return 0;
  2700. dhash_entries = simple_strtoul(str, &str, 0);
  2701. return 1;
  2702. }
  2703. __setup("dhash_entries=", set_dhash_entries);
  2704. static void __init dcache_init_early(void)
  2705. {
  2706. int loop;
  2707. /* If hashes are distributed across NUMA nodes, defer
  2708. * hash allocation until vmalloc space is available.
  2709. */
  2710. if (hashdist)
  2711. return;
  2712. dentry_hashtable =
  2713. alloc_large_system_hash("Dentry cache",
  2714. sizeof(struct hlist_bl_head),
  2715. dhash_entries,
  2716. 13,
  2717. HASH_EARLY,
  2718. &d_hash_shift,
  2719. &d_hash_mask,
  2720. 0);
  2721. for (loop = 0; loop < (1 << d_hash_shift); loop++)
  2722. INIT_HLIST_BL_HEAD(dentry_hashtable + loop);
  2723. }
  2724. static void __init dcache_init(void)
  2725. {
  2726. int loop;
  2727. /*
  2728. * A constructor could be added for stable state like the lists,
  2729. * but it is probably not worth it because of the cache nature
  2730. * of the dcache.
  2731. */
  2732. dentry_cache = KMEM_CACHE(dentry,
  2733. SLAB_RECLAIM_ACCOUNT|SLAB_PANIC|SLAB_MEM_SPREAD);
  2734. register_shrinker(&dcache_shrinker);
  2735. /* Hash may have been set up in dcache_init_early */
  2736. if (!hashdist)
  2737. return;
  2738. dentry_hashtable =
  2739. alloc_large_system_hash("Dentry cache",
  2740. sizeof(struct hlist_bl_head),
  2741. dhash_entries,
  2742. 13,
  2743. 0,
  2744. &d_hash_shift,
  2745. &d_hash_mask,
  2746. 0);
  2747. for (loop = 0; loop < (1 << d_hash_shift); loop++)
  2748. INIT_HLIST_BL_HEAD(dentry_hashtable + loop);
  2749. }
  2750. /* SLAB cache for __getname() consumers */
  2751. struct kmem_cache *names_cachep __read_mostly;
  2752. EXPORT_SYMBOL(names_cachep);
  2753. EXPORT_SYMBOL(d_genocide);
  2754. void __init vfs_caches_init_early(void)
  2755. {
  2756. dcache_init_early();
  2757. inode_init_early();
  2758. }
  2759. void __init vfs_caches_init(unsigned long mempages)
  2760. {
  2761. unsigned long reserve;
  2762. /* Base hash sizes on available memory, with a reserve equal to
  2763. 150% of current kernel size */
  2764. reserve = min((mempages - nr_free_pages()) * 3/2, mempages - 1);
  2765. mempages -= reserve;
  2766. names_cachep = kmem_cache_create("names_cache", PATH_MAX, 0,
  2767. SLAB_HWCACHE_ALIGN|SLAB_PANIC, NULL);
  2768. dcache_init();
  2769. inode_init();
  2770. files_init(mempages);
  2771. mnt_init();
  2772. bdev_cache_init();
  2773. chrdev_init();
  2774. }