dcache.c 80 KB

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