dcache.c 96 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038203920402041204220432044204520462047204820492050205120522053205420552056205720582059206020612062206320642065206620672068206920702071207220732074207520762077207820792080208120822083208420852086208720882089209020912092209320942095209620972098209921002101210221032104210521062107210821092110211121122113211421152116211721182119212021212122212321242125212621272128212921302131213221332134213521362137213821392140214121422143214421452146214721482149215021512152215321542155215621572158215921602161216221632164216521662167216821692170217121722173217421752176217721782179218021812182218321842185218621872188218921902191219221932194219521962197219821992200220122022203220422052206220722082209221022112212221322142215221622172218221922202221222222232224222522262227222822292230223122322233223422352236223722382239224022412242224322442245224622472248224922502251225222532254225522562257225822592260226122622263226422652266226722682269227022712272227322742275227622772278227922802281228222832284228522862287228822892290229122922293229422952296229722982299230023012302230323042305230623072308230923102311231223132314231523162317231823192320232123222323232423252326232723282329233023312332233323342335233623372338233923402341234223432344234523462347234823492350235123522353235423552356235723582359236023612362236323642365236623672368236923702371237223732374237523762377237823792380238123822383238423852386238723882389239023912392239323942395239623972398239924002401240224032404240524062407240824092410241124122413241424152416241724182419242024212422242324242425242624272428242924302431243224332434243524362437243824392440244124422443244424452446244724482449245024512452245324542455245624572458245924602461246224632464246524662467246824692470247124722473247424752476247724782479248024812482248324842485248624872488248924902491249224932494249524962497249824992500250125022503250425052506250725082509251025112512251325142515251625172518251925202521252225232524252525262527252825292530253125322533253425352536253725382539254025412542254325442545254625472548254925502551255225532554255525562557255825592560256125622563256425652566256725682569257025712572257325742575257625772578257925802581258225832584258525862587258825892590259125922593259425952596259725982599260026012602260326042605260626072608260926102611261226132614261526162617261826192620262126222623262426252626262726282629263026312632263326342635263626372638263926402641264226432644264526462647264826492650265126522653265426552656265726582659266026612662266326642665266626672668266926702671267226732674267526762677267826792680268126822683268426852686268726882689269026912692269326942695269626972698269927002701270227032704270527062707270827092710271127122713271427152716271727182719272027212722272327242725272627272728272927302731273227332734273527362737273827392740274127422743274427452746274727482749275027512752275327542755275627572758275927602761276227632764276527662767276827692770277127722773277427752776277727782779278027812782278327842785278627872788278927902791279227932794279527962797279827992800280128022803280428052806280728082809281028112812281328142815281628172818281928202821282228232824282528262827282828292830283128322833283428352836283728382839284028412842284328442845284628472848284928502851285228532854285528562857285828592860286128622863286428652866286728682869287028712872287328742875287628772878287928802881288228832884288528862887288828892890289128922893289428952896289728982899290029012902290329042905290629072908290929102911291229132914291529162917291829192920292129222923292429252926292729282929293029312932293329342935293629372938293929402941294229432944294529462947294829492950295129522953295429552956295729582959296029612962296329642965296629672968296929702971297229732974297529762977297829792980298129822983298429852986298729882989299029912992299329942995299629972998299930003001300230033004300530063007300830093010301130123013301430153016301730183019302030213022302330243025302630273028302930303031303230333034303530363037303830393040304130423043304430453046304730483049305030513052305330543055305630573058305930603061306230633064306530663067306830693070307130723073307430753076307730783079308030813082308330843085308630873088308930903091309230933094309530963097309830993100310131023103310431053106310731083109311031113112311331143115311631173118311931203121312231233124312531263127312831293130313131323133313431353136313731383139314031413142314331443145314631473148314931503151315231533154315531563157315831593160316131623163316431653166316731683169317031713172317331743175317631773178317931803181318231833184318531863187318831893190319131923193319431953196319731983199320032013202320332043205320632073208320932103211321232133214321532163217321832193220322132223223322432253226322732283229323032313232323332343235323632373238323932403241324232433244324532463247324832493250325132523253325432553256325732583259326032613262326332643265326632673268326932703271327232733274327532763277327832793280328132823283328432853286328732883289329032913292329332943295329632973298329933003301330233033304330533063307330833093310331133123313331433153316331733183319332033213322332333243325332633273328332933303331333233333334333533363337333833393340334133423343334433453346334733483349335033513352335333543355335633573358335933603361336233633364336533663367336833693370337133723373337433753376337733783379338033813382338333843385338633873388338933903391339233933394339533963397339833993400340134023403340434053406340734083409341034113412341334143415341634173418341934203421342234233424342534263427342834293430343134323433343434353436343734383439344034413442344334443445344634473448344934503451345234533454345534563457345834593460346134623463346434653466346734683469347034713472347334743475347634773478347934803481348234833484348534863487348834893490349134923493349434953496349734983499350035013502350335043505350635073508350935103511351235133514351535163517351835193520352135223523352435253526352735283529353035313532353335343535353635373538353935403541354235433544354535463547354835493550355135523553355435553556355735583559356035613562356335643565356635673568356935703571357235733574357535763577357835793580358135823583358435853586358735883589359035913592359335943595359635973598359936003601360236033604360536063607360836093610361136123613361436153616361736183619362036213622362336243625362636273628362936303631363236333634363536363637363836393640364136423643364436453646364736483649365036513652365336543655365636573658365936603661366236633664366536663667366836693670367136723673367436753676367736783679368036813682368336843685368636873688368936903691369236933694369536963697369836993700370137023703370437053706370737083709371037113712371337143715371637173718
  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 <linux/list_lru.h>
  39. #include <linux/kasan.h>
  40. #include "internal.h"
  41. #include "mount.h"
  42. /*
  43. * Usage:
  44. * dcache->d_inode->i_lock protects:
  45. * - i_dentry, d_u.d_alias, d_inode of aliases
  46. * dcache_hash_bucket lock protects:
  47. * - the dcache hash table
  48. * s_anon bl list spinlock protects:
  49. * - the s_anon list (see __d_drop)
  50. * dentry->d_sb->s_dentry_lru_lock protects:
  51. * - the dcache lru lists and counters
  52. * d_lock protects:
  53. * - d_flags
  54. * - d_name
  55. * - d_lru
  56. * - d_count
  57. * - d_unhashed()
  58. * - d_parent and d_subdirs
  59. * - childrens' d_child and d_parent
  60. * - d_u.d_alias, d_inode
  61. *
  62. * Ordering:
  63. * dentry->d_inode->i_lock
  64. * dentry->d_lock
  65. * dentry->d_sb->s_dentry_lru_lock
  66. * dcache_hash_bucket lock
  67. * s_anon lock
  68. *
  69. * If there is an ancestor relationship:
  70. * dentry->d_parent->...->d_parent->d_lock
  71. * ...
  72. * dentry->d_parent->d_lock
  73. * dentry->d_lock
  74. *
  75. * If no ancestor relationship:
  76. * if (dentry1 < dentry2)
  77. * dentry1->d_lock
  78. * dentry2->d_lock
  79. */
  80. int sysctl_vfs_cache_pressure __read_mostly = 100;
  81. EXPORT_SYMBOL_GPL(sysctl_vfs_cache_pressure);
  82. __cacheline_aligned_in_smp DEFINE_SEQLOCK(rename_lock);
  83. EXPORT_SYMBOL(rename_lock);
  84. static struct kmem_cache *dentry_cache __read_mostly;
  85. /*
  86. * This is the single most critical data structure when it comes
  87. * to the dcache: the hashtable for lookups. Somebody should try
  88. * to make this good - I've just made it work.
  89. *
  90. * This hash-function tries to avoid losing too many bits of hash
  91. * information, yet avoid using a prime hash-size or similar.
  92. */
  93. static unsigned int d_hash_mask __read_mostly;
  94. static unsigned int d_hash_shift __read_mostly;
  95. static struct hlist_bl_head *dentry_hashtable __read_mostly;
  96. static inline struct hlist_bl_head *d_hash(const struct dentry *parent,
  97. unsigned int hash)
  98. {
  99. hash += (unsigned long) parent / L1_CACHE_BYTES;
  100. return dentry_hashtable + hash_32(hash, d_hash_shift);
  101. }
  102. #define IN_LOOKUP_SHIFT 10
  103. static struct hlist_bl_head in_lookup_hashtable[1 << IN_LOOKUP_SHIFT];
  104. static inline struct hlist_bl_head *in_lookup_hash(const struct dentry *parent,
  105. unsigned int hash)
  106. {
  107. hash += (unsigned long) parent / L1_CACHE_BYTES;
  108. return in_lookup_hashtable + hash_32(hash, IN_LOOKUP_SHIFT);
  109. }
  110. /* Statistics gathering. */
  111. struct dentry_stat_t dentry_stat = {
  112. .age_limit = 45,
  113. };
  114. static DEFINE_PER_CPU(long, nr_dentry);
  115. static DEFINE_PER_CPU(long, nr_dentry_unused);
  116. #if defined(CONFIG_SYSCTL) && defined(CONFIG_PROC_FS)
  117. /*
  118. * Here we resort to our own counters instead of using generic per-cpu counters
  119. * for consistency with what the vfs inode code does. We are expected to harvest
  120. * better code and performance by having our own specialized counters.
  121. *
  122. * Please note that the loop is done over all possible CPUs, not over all online
  123. * CPUs. The reason for this is that we don't want to play games with CPUs going
  124. * on and off. If one of them goes off, we will just keep their counters.
  125. *
  126. * glommer: See cffbc8a for details, and if you ever intend to change this,
  127. * please update all vfs counters to match.
  128. */
  129. static long get_nr_dentry(void)
  130. {
  131. int i;
  132. long sum = 0;
  133. for_each_possible_cpu(i)
  134. sum += per_cpu(nr_dentry, i);
  135. return sum < 0 ? 0 : sum;
  136. }
  137. static long get_nr_dentry_unused(void)
  138. {
  139. int i;
  140. long sum = 0;
  141. for_each_possible_cpu(i)
  142. sum += per_cpu(nr_dentry_unused, i);
  143. return sum < 0 ? 0 : sum;
  144. }
  145. int proc_nr_dentry(struct ctl_table *table, int write, void __user *buffer,
  146. size_t *lenp, loff_t *ppos)
  147. {
  148. dentry_stat.nr_dentry = get_nr_dentry();
  149. dentry_stat.nr_unused = get_nr_dentry_unused();
  150. return proc_doulongvec_minmax(table, write, buffer, lenp, ppos);
  151. }
  152. #endif
  153. /*
  154. * Compare 2 name strings, return 0 if they match, otherwise non-zero.
  155. * The strings are both count bytes long, and count is non-zero.
  156. */
  157. #ifdef CONFIG_DCACHE_WORD_ACCESS
  158. #include <asm/word-at-a-time.h>
  159. /*
  160. * NOTE! 'cs' and 'scount' come from a dentry, so it has a
  161. * aligned allocation for this particular component. We don't
  162. * strictly need the load_unaligned_zeropad() safety, but it
  163. * doesn't hurt either.
  164. *
  165. * In contrast, 'ct' and 'tcount' can be from a pathname, and do
  166. * need the careful unaligned handling.
  167. */
  168. static inline int dentry_string_cmp(const unsigned char *cs, const unsigned char *ct, unsigned tcount)
  169. {
  170. unsigned long a,b,mask;
  171. for (;;) {
  172. a = *(unsigned long *)cs;
  173. b = load_unaligned_zeropad(ct);
  174. if (tcount < sizeof(unsigned long))
  175. break;
  176. if (unlikely(a != b))
  177. return 1;
  178. cs += sizeof(unsigned long);
  179. ct += sizeof(unsigned long);
  180. tcount -= sizeof(unsigned long);
  181. if (!tcount)
  182. return 0;
  183. }
  184. mask = bytemask_from_count(tcount);
  185. return unlikely(!!((a ^ b) & mask));
  186. }
  187. #else
  188. static inline int dentry_string_cmp(const unsigned char *cs, const unsigned char *ct, unsigned tcount)
  189. {
  190. do {
  191. if (*cs != *ct)
  192. return 1;
  193. cs++;
  194. ct++;
  195. tcount--;
  196. } while (tcount);
  197. return 0;
  198. }
  199. #endif
  200. static inline int dentry_cmp(const struct dentry *dentry, const unsigned char *ct, unsigned tcount)
  201. {
  202. const unsigned char *cs;
  203. /*
  204. * Be careful about RCU walk racing with rename:
  205. * use ACCESS_ONCE to fetch the name pointer.
  206. *
  207. * NOTE! Even if a rename will mean that the length
  208. * was not loaded atomically, we don't care. The
  209. * RCU walk will check the sequence count eventually,
  210. * and catch it. And we won't overrun the buffer,
  211. * because we're reading the name pointer atomically,
  212. * and a dentry name is guaranteed to be properly
  213. * terminated with a NUL byte.
  214. *
  215. * End result: even if 'len' is wrong, we'll exit
  216. * early because the data cannot match (there can
  217. * be no NUL in the ct/tcount data)
  218. */
  219. cs = ACCESS_ONCE(dentry->d_name.name);
  220. smp_read_barrier_depends();
  221. return dentry_string_cmp(cs, ct, tcount);
  222. }
  223. struct external_name {
  224. union {
  225. atomic_t count;
  226. struct rcu_head head;
  227. } u;
  228. unsigned char name[];
  229. };
  230. static inline struct external_name *external_name(struct dentry *dentry)
  231. {
  232. return container_of(dentry->d_name.name, struct external_name, name[0]);
  233. }
  234. static void __d_free(struct rcu_head *head)
  235. {
  236. struct dentry *dentry = container_of(head, struct dentry, d_u.d_rcu);
  237. kmem_cache_free(dentry_cache, dentry);
  238. }
  239. static void __d_free_external(struct rcu_head *head)
  240. {
  241. struct dentry *dentry = container_of(head, struct dentry, d_u.d_rcu);
  242. kfree(external_name(dentry));
  243. kmem_cache_free(dentry_cache, dentry);
  244. }
  245. static inline int dname_external(const struct dentry *dentry)
  246. {
  247. return dentry->d_name.name != dentry->d_iname;
  248. }
  249. static inline void __d_set_inode_and_type(struct dentry *dentry,
  250. struct inode *inode,
  251. unsigned type_flags)
  252. {
  253. unsigned flags;
  254. dentry->d_inode = inode;
  255. flags = READ_ONCE(dentry->d_flags);
  256. flags &= ~(DCACHE_ENTRY_TYPE | DCACHE_FALLTHRU);
  257. flags |= type_flags;
  258. WRITE_ONCE(dentry->d_flags, flags);
  259. }
  260. static inline void __d_clear_type_and_inode(struct dentry *dentry)
  261. {
  262. unsigned flags = READ_ONCE(dentry->d_flags);
  263. flags &= ~(DCACHE_ENTRY_TYPE | DCACHE_FALLTHRU);
  264. WRITE_ONCE(dentry->d_flags, flags);
  265. dentry->d_inode = NULL;
  266. }
  267. static void dentry_free(struct dentry *dentry)
  268. {
  269. WARN_ON(!hlist_unhashed(&dentry->d_u.d_alias));
  270. if (unlikely(dname_external(dentry))) {
  271. struct external_name *p = external_name(dentry);
  272. if (likely(atomic_dec_and_test(&p->u.count))) {
  273. call_rcu(&dentry->d_u.d_rcu, __d_free_external);
  274. return;
  275. }
  276. }
  277. /* if dentry was never visible to RCU, immediate free is OK */
  278. if (!(dentry->d_flags & DCACHE_RCUACCESS))
  279. __d_free(&dentry->d_u.d_rcu);
  280. else
  281. call_rcu(&dentry->d_u.d_rcu, __d_free);
  282. }
  283. /**
  284. * dentry_rcuwalk_invalidate - invalidate in-progress rcu-walk lookups
  285. * @dentry: the target dentry
  286. * After this call, in-progress rcu-walk path lookup will fail. This
  287. * should be called after unhashing, and after changing d_inode (if
  288. * the dentry has not already been unhashed).
  289. */
  290. static inline void dentry_rcuwalk_invalidate(struct dentry *dentry)
  291. {
  292. lockdep_assert_held(&dentry->d_lock);
  293. /* Go through am invalidation barrier */
  294. write_seqcount_invalidate(&dentry->d_seq);
  295. }
  296. /*
  297. * Release the dentry's inode, using the filesystem
  298. * d_iput() operation if defined. Dentry has no refcount
  299. * and is unhashed.
  300. */
  301. static void dentry_iput(struct dentry * dentry)
  302. __releases(dentry->d_lock)
  303. __releases(dentry->d_inode->i_lock)
  304. {
  305. struct inode *inode = dentry->d_inode;
  306. if (inode) {
  307. __d_clear_type_and_inode(dentry);
  308. hlist_del_init(&dentry->d_u.d_alias);
  309. spin_unlock(&dentry->d_lock);
  310. spin_unlock(&inode->i_lock);
  311. if (!inode->i_nlink)
  312. fsnotify_inoderemove(inode);
  313. if (dentry->d_op && dentry->d_op->d_iput)
  314. dentry->d_op->d_iput(dentry, inode);
  315. else
  316. iput(inode);
  317. } else {
  318. spin_unlock(&dentry->d_lock);
  319. }
  320. }
  321. /*
  322. * Release the dentry's inode, using the filesystem
  323. * d_iput() operation if defined. dentry remains in-use.
  324. */
  325. static void dentry_unlink_inode(struct dentry * dentry)
  326. __releases(dentry->d_lock)
  327. __releases(dentry->d_inode->i_lock)
  328. {
  329. struct inode *inode = dentry->d_inode;
  330. raw_write_seqcount_begin(&dentry->d_seq);
  331. __d_clear_type_and_inode(dentry);
  332. hlist_del_init(&dentry->d_u.d_alias);
  333. raw_write_seqcount_end(&dentry->d_seq);
  334. spin_unlock(&dentry->d_lock);
  335. spin_unlock(&inode->i_lock);
  336. if (!inode->i_nlink)
  337. fsnotify_inoderemove(inode);
  338. if (dentry->d_op && dentry->d_op->d_iput)
  339. dentry->d_op->d_iput(dentry, inode);
  340. else
  341. iput(inode);
  342. }
  343. /*
  344. * The DCACHE_LRU_LIST bit is set whenever the 'd_lru' entry
  345. * is in use - which includes both the "real" per-superblock
  346. * LRU list _and_ the DCACHE_SHRINK_LIST use.
  347. *
  348. * The DCACHE_SHRINK_LIST bit is set whenever the dentry is
  349. * on the shrink list (ie not on the superblock LRU list).
  350. *
  351. * The per-cpu "nr_dentry_unused" counters are updated with
  352. * the DCACHE_LRU_LIST bit.
  353. *
  354. * These helper functions make sure we always follow the
  355. * rules. d_lock must be held by the caller.
  356. */
  357. #define D_FLAG_VERIFY(dentry,x) WARN_ON_ONCE(((dentry)->d_flags & (DCACHE_LRU_LIST | DCACHE_SHRINK_LIST)) != (x))
  358. static void d_lru_add(struct dentry *dentry)
  359. {
  360. D_FLAG_VERIFY(dentry, 0);
  361. dentry->d_flags |= DCACHE_LRU_LIST;
  362. this_cpu_inc(nr_dentry_unused);
  363. WARN_ON_ONCE(!list_lru_add(&dentry->d_sb->s_dentry_lru, &dentry->d_lru));
  364. }
  365. static void d_lru_del(struct dentry *dentry)
  366. {
  367. D_FLAG_VERIFY(dentry, DCACHE_LRU_LIST);
  368. dentry->d_flags &= ~DCACHE_LRU_LIST;
  369. this_cpu_dec(nr_dentry_unused);
  370. WARN_ON_ONCE(!list_lru_del(&dentry->d_sb->s_dentry_lru, &dentry->d_lru));
  371. }
  372. static void d_shrink_del(struct dentry *dentry)
  373. {
  374. D_FLAG_VERIFY(dentry, DCACHE_SHRINK_LIST | DCACHE_LRU_LIST);
  375. list_del_init(&dentry->d_lru);
  376. dentry->d_flags &= ~(DCACHE_SHRINK_LIST | DCACHE_LRU_LIST);
  377. this_cpu_dec(nr_dentry_unused);
  378. }
  379. static void d_shrink_add(struct dentry *dentry, struct list_head *list)
  380. {
  381. D_FLAG_VERIFY(dentry, 0);
  382. list_add(&dentry->d_lru, list);
  383. dentry->d_flags |= DCACHE_SHRINK_LIST | DCACHE_LRU_LIST;
  384. this_cpu_inc(nr_dentry_unused);
  385. }
  386. /*
  387. * These can only be called under the global LRU lock, ie during the
  388. * callback for freeing the LRU list. "isolate" removes it from the
  389. * LRU lists entirely, while shrink_move moves it to the indicated
  390. * private list.
  391. */
  392. static void d_lru_isolate(struct list_lru_one *lru, struct dentry *dentry)
  393. {
  394. D_FLAG_VERIFY(dentry, DCACHE_LRU_LIST);
  395. dentry->d_flags &= ~DCACHE_LRU_LIST;
  396. this_cpu_dec(nr_dentry_unused);
  397. list_lru_isolate(lru, &dentry->d_lru);
  398. }
  399. static void d_lru_shrink_move(struct list_lru_one *lru, struct dentry *dentry,
  400. struct list_head *list)
  401. {
  402. D_FLAG_VERIFY(dentry, DCACHE_LRU_LIST);
  403. dentry->d_flags |= DCACHE_SHRINK_LIST;
  404. list_lru_isolate_move(lru, &dentry->d_lru, list);
  405. }
  406. /*
  407. * dentry_lru_(add|del)_list) must be called with d_lock held.
  408. */
  409. static void dentry_lru_add(struct dentry *dentry)
  410. {
  411. if (unlikely(!(dentry->d_flags & DCACHE_LRU_LIST)))
  412. d_lru_add(dentry);
  413. }
  414. /**
  415. * d_drop - drop a dentry
  416. * @dentry: dentry to drop
  417. *
  418. * d_drop() unhashes the entry from the parent dentry hashes, so that it won't
  419. * be found through a VFS lookup any more. Note that this is different from
  420. * deleting the dentry - d_delete will try to mark the dentry negative if
  421. * possible, giving a successful _negative_ lookup, while d_drop will
  422. * just make the cache lookup fail.
  423. *
  424. * d_drop() is used mainly for stuff that wants to invalidate a dentry for some
  425. * reason (NFS timeouts or autofs deletes).
  426. *
  427. * __d_drop requires dentry->d_lock.
  428. */
  429. void __d_drop(struct dentry *dentry)
  430. {
  431. if (!d_unhashed(dentry)) {
  432. struct hlist_bl_head *b;
  433. /*
  434. * Hashed dentries are normally on the dentry hashtable,
  435. * with the exception of those newly allocated by
  436. * d_obtain_alias, which are always IS_ROOT:
  437. */
  438. if (unlikely(IS_ROOT(dentry)))
  439. b = &dentry->d_sb->s_anon;
  440. else
  441. b = d_hash(dentry->d_parent, dentry->d_name.hash);
  442. hlist_bl_lock(b);
  443. __hlist_bl_del(&dentry->d_hash);
  444. dentry->d_hash.pprev = NULL;
  445. hlist_bl_unlock(b);
  446. dentry_rcuwalk_invalidate(dentry);
  447. }
  448. }
  449. EXPORT_SYMBOL(__d_drop);
  450. void d_drop(struct dentry *dentry)
  451. {
  452. spin_lock(&dentry->d_lock);
  453. __d_drop(dentry);
  454. spin_unlock(&dentry->d_lock);
  455. }
  456. EXPORT_SYMBOL(d_drop);
  457. static inline void dentry_unlist(struct dentry *dentry, struct dentry *parent)
  458. {
  459. struct dentry *next;
  460. /*
  461. * Inform d_walk() and shrink_dentry_list() that we are no longer
  462. * attached to the dentry tree
  463. */
  464. dentry->d_flags |= DCACHE_DENTRY_KILLED;
  465. if (unlikely(list_empty(&dentry->d_child)))
  466. return;
  467. __list_del_entry(&dentry->d_child);
  468. /*
  469. * Cursors can move around the list of children. While we'd been
  470. * a normal list member, it didn't matter - ->d_child.next would've
  471. * been updated. However, from now on it won't be and for the
  472. * things like d_walk() it might end up with a nasty surprise.
  473. * Normally d_walk() doesn't care about cursors moving around -
  474. * ->d_lock on parent prevents that and since a cursor has no children
  475. * of its own, we get through it without ever unlocking the parent.
  476. * There is one exception, though - if we ascend from a child that
  477. * gets killed as soon as we unlock it, the next sibling is found
  478. * using the value left in its ->d_child.next. And if _that_
  479. * pointed to a cursor, and cursor got moved (e.g. by lseek())
  480. * before d_walk() regains parent->d_lock, we'll end up skipping
  481. * everything the cursor had been moved past.
  482. *
  483. * Solution: make sure that the pointer left behind in ->d_child.next
  484. * points to something that won't be moving around. I.e. skip the
  485. * cursors.
  486. */
  487. while (dentry->d_child.next != &parent->d_subdirs) {
  488. next = list_entry(dentry->d_child.next, struct dentry, d_child);
  489. if (likely(!(next->d_flags & DCACHE_DENTRY_CURSOR)))
  490. break;
  491. dentry->d_child.next = next->d_child.next;
  492. }
  493. }
  494. static void __dentry_kill(struct dentry *dentry)
  495. {
  496. struct dentry *parent = NULL;
  497. bool can_free = true;
  498. if (!IS_ROOT(dentry))
  499. parent = dentry->d_parent;
  500. /*
  501. * The dentry is now unrecoverably dead to the world.
  502. */
  503. lockref_mark_dead(&dentry->d_lockref);
  504. /*
  505. * inform the fs via d_prune that this dentry is about to be
  506. * unhashed and destroyed.
  507. */
  508. if (dentry->d_flags & DCACHE_OP_PRUNE)
  509. dentry->d_op->d_prune(dentry);
  510. if (dentry->d_flags & DCACHE_LRU_LIST) {
  511. if (!(dentry->d_flags & DCACHE_SHRINK_LIST))
  512. d_lru_del(dentry);
  513. }
  514. /* if it was on the hash then remove it */
  515. __d_drop(dentry);
  516. dentry_unlist(dentry, parent);
  517. if (parent)
  518. spin_unlock(&parent->d_lock);
  519. dentry_iput(dentry);
  520. /*
  521. * dentry_iput drops the locks, at which point nobody (except
  522. * transient RCU lookups) can reach this dentry.
  523. */
  524. BUG_ON(dentry->d_lockref.count > 0);
  525. this_cpu_dec(nr_dentry);
  526. if (dentry->d_op && dentry->d_op->d_release)
  527. dentry->d_op->d_release(dentry);
  528. spin_lock(&dentry->d_lock);
  529. if (dentry->d_flags & DCACHE_SHRINK_LIST) {
  530. dentry->d_flags |= DCACHE_MAY_FREE;
  531. can_free = false;
  532. }
  533. spin_unlock(&dentry->d_lock);
  534. if (likely(can_free))
  535. dentry_free(dentry);
  536. }
  537. /*
  538. * Finish off a dentry we've decided to kill.
  539. * dentry->d_lock must be held, returns with it unlocked.
  540. * If ref is non-zero, then decrement the refcount too.
  541. * Returns dentry requiring refcount drop, or NULL if we're done.
  542. */
  543. static struct dentry *dentry_kill(struct dentry *dentry)
  544. __releases(dentry->d_lock)
  545. {
  546. struct inode *inode = dentry->d_inode;
  547. struct dentry *parent = NULL;
  548. if (inode && unlikely(!spin_trylock(&inode->i_lock)))
  549. goto failed;
  550. if (!IS_ROOT(dentry)) {
  551. parent = dentry->d_parent;
  552. if (unlikely(!spin_trylock(&parent->d_lock))) {
  553. if (inode)
  554. spin_unlock(&inode->i_lock);
  555. goto failed;
  556. }
  557. }
  558. __dentry_kill(dentry);
  559. return parent;
  560. failed:
  561. spin_unlock(&dentry->d_lock);
  562. cpu_relax();
  563. return dentry; /* try again with same dentry */
  564. }
  565. static inline struct dentry *lock_parent(struct dentry *dentry)
  566. {
  567. struct dentry *parent = dentry->d_parent;
  568. if (IS_ROOT(dentry))
  569. return NULL;
  570. if (unlikely(dentry->d_lockref.count < 0))
  571. return NULL;
  572. if (likely(spin_trylock(&parent->d_lock)))
  573. return parent;
  574. rcu_read_lock();
  575. spin_unlock(&dentry->d_lock);
  576. again:
  577. parent = ACCESS_ONCE(dentry->d_parent);
  578. spin_lock(&parent->d_lock);
  579. /*
  580. * We can't blindly lock dentry until we are sure
  581. * that we won't violate the locking order.
  582. * Any changes of dentry->d_parent must have
  583. * been done with parent->d_lock held, so
  584. * spin_lock() above is enough of a barrier
  585. * for checking if it's still our child.
  586. */
  587. if (unlikely(parent != dentry->d_parent)) {
  588. spin_unlock(&parent->d_lock);
  589. goto again;
  590. }
  591. rcu_read_unlock();
  592. if (parent != dentry)
  593. spin_lock_nested(&dentry->d_lock, DENTRY_D_LOCK_NESTED);
  594. else
  595. parent = NULL;
  596. return parent;
  597. }
  598. /*
  599. * Try to do a lockless dput(), and return whether that was successful.
  600. *
  601. * If unsuccessful, we return false, having already taken the dentry lock.
  602. *
  603. * The caller needs to hold the RCU read lock, so that the dentry is
  604. * guaranteed to stay around even if the refcount goes down to zero!
  605. */
  606. static inline bool fast_dput(struct dentry *dentry)
  607. {
  608. int ret;
  609. unsigned int d_flags;
  610. /*
  611. * If we have a d_op->d_delete() operation, we sould not
  612. * let the dentry count go to zero, so use "put_or_lock".
  613. */
  614. if (unlikely(dentry->d_flags & DCACHE_OP_DELETE))
  615. return lockref_put_or_lock(&dentry->d_lockref);
  616. /*
  617. * .. otherwise, we can try to just decrement the
  618. * lockref optimistically.
  619. */
  620. ret = lockref_put_return(&dentry->d_lockref);
  621. /*
  622. * If the lockref_put_return() failed due to the lock being held
  623. * by somebody else, the fast path has failed. We will need to
  624. * get the lock, and then check the count again.
  625. */
  626. if (unlikely(ret < 0)) {
  627. spin_lock(&dentry->d_lock);
  628. if (dentry->d_lockref.count > 1) {
  629. dentry->d_lockref.count--;
  630. spin_unlock(&dentry->d_lock);
  631. return 1;
  632. }
  633. return 0;
  634. }
  635. /*
  636. * If we weren't the last ref, we're done.
  637. */
  638. if (ret)
  639. return 1;
  640. /*
  641. * Careful, careful. The reference count went down
  642. * to zero, but we don't hold the dentry lock, so
  643. * somebody else could get it again, and do another
  644. * dput(), and we need to not race with that.
  645. *
  646. * However, there is a very special and common case
  647. * where we don't care, because there is nothing to
  648. * do: the dentry is still hashed, it does not have
  649. * a 'delete' op, and it's referenced and already on
  650. * the LRU list.
  651. *
  652. * NOTE! Since we aren't locked, these values are
  653. * not "stable". However, it is sufficient that at
  654. * some point after we dropped the reference the
  655. * dentry was hashed and the flags had the proper
  656. * value. Other dentry users may have re-gotten
  657. * a reference to the dentry and change that, but
  658. * our work is done - we can leave the dentry
  659. * around with a zero refcount.
  660. */
  661. smp_rmb();
  662. d_flags = ACCESS_ONCE(dentry->d_flags);
  663. d_flags &= DCACHE_REFERENCED | DCACHE_LRU_LIST | DCACHE_DISCONNECTED;
  664. /* Nothing to do? Dropping the reference was all we needed? */
  665. if (d_flags == (DCACHE_REFERENCED | DCACHE_LRU_LIST) && !d_unhashed(dentry))
  666. return 1;
  667. /*
  668. * Not the fast normal case? Get the lock. We've already decremented
  669. * the refcount, but we'll need to re-check the situation after
  670. * getting the lock.
  671. */
  672. spin_lock(&dentry->d_lock);
  673. /*
  674. * Did somebody else grab a reference to it in the meantime, and
  675. * we're no longer the last user after all? Alternatively, somebody
  676. * else could have killed it and marked it dead. Either way, we
  677. * don't need to do anything else.
  678. */
  679. if (dentry->d_lockref.count) {
  680. spin_unlock(&dentry->d_lock);
  681. return 1;
  682. }
  683. /*
  684. * Re-get the reference we optimistically dropped. We hold the
  685. * lock, and we just tested that it was zero, so we can just
  686. * set it to 1.
  687. */
  688. dentry->d_lockref.count = 1;
  689. return 0;
  690. }
  691. /*
  692. * This is dput
  693. *
  694. * This is complicated by the fact that we do not want to put
  695. * dentries that are no longer on any hash chain on the unused
  696. * list: we'd much rather just get rid of them immediately.
  697. *
  698. * However, that implies that we have to traverse the dentry
  699. * tree upwards to the parents which might _also_ now be
  700. * scheduled for deletion (it may have been only waiting for
  701. * its last child to go away).
  702. *
  703. * This tail recursion is done by hand as we don't want to depend
  704. * on the compiler to always get this right (gcc generally doesn't).
  705. * Real recursion would eat up our stack space.
  706. */
  707. /*
  708. * dput - release a dentry
  709. * @dentry: dentry to release
  710. *
  711. * Release a dentry. This will drop the usage count and if appropriate
  712. * call the dentry unlink method as well as removing it from the queues and
  713. * releasing its resources. If the parent dentries were scheduled for release
  714. * they too may now get deleted.
  715. */
  716. void dput(struct dentry *dentry)
  717. {
  718. if (unlikely(!dentry))
  719. return;
  720. repeat:
  721. rcu_read_lock();
  722. if (likely(fast_dput(dentry))) {
  723. rcu_read_unlock();
  724. return;
  725. }
  726. /* Slow case: now with the dentry lock held */
  727. rcu_read_unlock();
  728. WARN_ON(d_in_lookup(dentry));
  729. /* Unreachable? Get rid of it */
  730. if (unlikely(d_unhashed(dentry)))
  731. goto kill_it;
  732. if (unlikely(dentry->d_flags & DCACHE_DISCONNECTED))
  733. goto kill_it;
  734. if (unlikely(dentry->d_flags & DCACHE_OP_DELETE)) {
  735. if (dentry->d_op->d_delete(dentry))
  736. goto kill_it;
  737. }
  738. if (!(dentry->d_flags & DCACHE_REFERENCED))
  739. dentry->d_flags |= DCACHE_REFERENCED;
  740. dentry_lru_add(dentry);
  741. dentry->d_lockref.count--;
  742. spin_unlock(&dentry->d_lock);
  743. return;
  744. kill_it:
  745. dentry = dentry_kill(dentry);
  746. if (dentry)
  747. goto repeat;
  748. }
  749. EXPORT_SYMBOL(dput);
  750. /* This must be called with d_lock held */
  751. static inline void __dget_dlock(struct dentry *dentry)
  752. {
  753. dentry->d_lockref.count++;
  754. }
  755. static inline void __dget(struct dentry *dentry)
  756. {
  757. lockref_get(&dentry->d_lockref);
  758. }
  759. struct dentry *dget_parent(struct dentry *dentry)
  760. {
  761. int gotref;
  762. struct dentry *ret;
  763. /*
  764. * Do optimistic parent lookup without any
  765. * locking.
  766. */
  767. rcu_read_lock();
  768. ret = ACCESS_ONCE(dentry->d_parent);
  769. gotref = lockref_get_not_zero(&ret->d_lockref);
  770. rcu_read_unlock();
  771. if (likely(gotref)) {
  772. if (likely(ret == ACCESS_ONCE(dentry->d_parent)))
  773. return ret;
  774. dput(ret);
  775. }
  776. repeat:
  777. /*
  778. * Don't need rcu_dereference because we re-check it was correct under
  779. * the lock.
  780. */
  781. rcu_read_lock();
  782. ret = dentry->d_parent;
  783. spin_lock(&ret->d_lock);
  784. if (unlikely(ret != dentry->d_parent)) {
  785. spin_unlock(&ret->d_lock);
  786. rcu_read_unlock();
  787. goto repeat;
  788. }
  789. rcu_read_unlock();
  790. BUG_ON(!ret->d_lockref.count);
  791. ret->d_lockref.count++;
  792. spin_unlock(&ret->d_lock);
  793. return ret;
  794. }
  795. EXPORT_SYMBOL(dget_parent);
  796. /**
  797. * d_find_alias - grab a hashed alias of inode
  798. * @inode: inode in question
  799. *
  800. * If inode has a hashed alias, or is a directory and has any alias,
  801. * acquire the reference to alias and return it. Otherwise return NULL.
  802. * Notice that if inode is a directory there can be only one alias and
  803. * it can be unhashed only if it has no children, or if it is the root
  804. * of a filesystem, or if the directory was renamed and d_revalidate
  805. * was the first vfs operation to notice.
  806. *
  807. * If the inode has an IS_ROOT, DCACHE_DISCONNECTED alias, then prefer
  808. * any other hashed alias over that one.
  809. */
  810. static struct dentry *__d_find_alias(struct inode *inode)
  811. {
  812. struct dentry *alias, *discon_alias;
  813. again:
  814. discon_alias = NULL;
  815. hlist_for_each_entry(alias, &inode->i_dentry, d_u.d_alias) {
  816. spin_lock(&alias->d_lock);
  817. if (S_ISDIR(inode->i_mode) || !d_unhashed(alias)) {
  818. if (IS_ROOT(alias) &&
  819. (alias->d_flags & DCACHE_DISCONNECTED)) {
  820. discon_alias = alias;
  821. } else {
  822. __dget_dlock(alias);
  823. spin_unlock(&alias->d_lock);
  824. return alias;
  825. }
  826. }
  827. spin_unlock(&alias->d_lock);
  828. }
  829. if (discon_alias) {
  830. alias = discon_alias;
  831. spin_lock(&alias->d_lock);
  832. if (S_ISDIR(inode->i_mode) || !d_unhashed(alias)) {
  833. __dget_dlock(alias);
  834. spin_unlock(&alias->d_lock);
  835. return alias;
  836. }
  837. spin_unlock(&alias->d_lock);
  838. goto again;
  839. }
  840. return NULL;
  841. }
  842. struct dentry *d_find_alias(struct inode *inode)
  843. {
  844. struct dentry *de = NULL;
  845. if (!hlist_empty(&inode->i_dentry)) {
  846. spin_lock(&inode->i_lock);
  847. de = __d_find_alias(inode);
  848. spin_unlock(&inode->i_lock);
  849. }
  850. return de;
  851. }
  852. EXPORT_SYMBOL(d_find_alias);
  853. /*
  854. * Try to kill dentries associated with this inode.
  855. * WARNING: you must own a reference to inode.
  856. */
  857. void d_prune_aliases(struct inode *inode)
  858. {
  859. struct dentry *dentry;
  860. restart:
  861. spin_lock(&inode->i_lock);
  862. hlist_for_each_entry(dentry, &inode->i_dentry, d_u.d_alias) {
  863. spin_lock(&dentry->d_lock);
  864. if (!dentry->d_lockref.count) {
  865. struct dentry *parent = lock_parent(dentry);
  866. if (likely(!dentry->d_lockref.count)) {
  867. __dentry_kill(dentry);
  868. dput(parent);
  869. goto restart;
  870. }
  871. if (parent)
  872. spin_unlock(&parent->d_lock);
  873. }
  874. spin_unlock(&dentry->d_lock);
  875. }
  876. spin_unlock(&inode->i_lock);
  877. }
  878. EXPORT_SYMBOL(d_prune_aliases);
  879. static void shrink_dentry_list(struct list_head *list)
  880. {
  881. struct dentry *dentry, *parent;
  882. while (!list_empty(list)) {
  883. struct inode *inode;
  884. dentry = list_entry(list->prev, struct dentry, d_lru);
  885. spin_lock(&dentry->d_lock);
  886. parent = lock_parent(dentry);
  887. /*
  888. * The dispose list is isolated and dentries are not accounted
  889. * to the LRU here, so we can simply remove it from the list
  890. * here regardless of whether it is referenced or not.
  891. */
  892. d_shrink_del(dentry);
  893. /*
  894. * We found an inuse dentry which was not removed from
  895. * the LRU because of laziness during lookup. Do not free it.
  896. */
  897. if (dentry->d_lockref.count > 0) {
  898. spin_unlock(&dentry->d_lock);
  899. if (parent)
  900. spin_unlock(&parent->d_lock);
  901. continue;
  902. }
  903. if (unlikely(dentry->d_flags & DCACHE_DENTRY_KILLED)) {
  904. bool can_free = dentry->d_flags & DCACHE_MAY_FREE;
  905. spin_unlock(&dentry->d_lock);
  906. if (parent)
  907. spin_unlock(&parent->d_lock);
  908. if (can_free)
  909. dentry_free(dentry);
  910. continue;
  911. }
  912. inode = dentry->d_inode;
  913. if (inode && unlikely(!spin_trylock(&inode->i_lock))) {
  914. d_shrink_add(dentry, list);
  915. spin_unlock(&dentry->d_lock);
  916. if (parent)
  917. spin_unlock(&parent->d_lock);
  918. continue;
  919. }
  920. __dentry_kill(dentry);
  921. /*
  922. * We need to prune ancestors too. This is necessary to prevent
  923. * quadratic behavior of shrink_dcache_parent(), but is also
  924. * expected to be beneficial in reducing dentry cache
  925. * fragmentation.
  926. */
  927. dentry = parent;
  928. while (dentry && !lockref_put_or_lock(&dentry->d_lockref)) {
  929. parent = lock_parent(dentry);
  930. if (dentry->d_lockref.count != 1) {
  931. dentry->d_lockref.count--;
  932. spin_unlock(&dentry->d_lock);
  933. if (parent)
  934. spin_unlock(&parent->d_lock);
  935. break;
  936. }
  937. inode = dentry->d_inode; /* can't be NULL */
  938. if (unlikely(!spin_trylock(&inode->i_lock))) {
  939. spin_unlock(&dentry->d_lock);
  940. if (parent)
  941. spin_unlock(&parent->d_lock);
  942. cpu_relax();
  943. continue;
  944. }
  945. __dentry_kill(dentry);
  946. dentry = parent;
  947. }
  948. }
  949. }
  950. static enum lru_status dentry_lru_isolate(struct list_head *item,
  951. struct list_lru_one *lru, spinlock_t *lru_lock, void *arg)
  952. {
  953. struct list_head *freeable = arg;
  954. struct dentry *dentry = container_of(item, struct dentry, d_lru);
  955. /*
  956. * we are inverting the lru lock/dentry->d_lock here,
  957. * so use a trylock. If we fail to get the lock, just skip
  958. * it
  959. */
  960. if (!spin_trylock(&dentry->d_lock))
  961. return LRU_SKIP;
  962. /*
  963. * Referenced dentries are still in use. If they have active
  964. * counts, just remove them from the LRU. Otherwise give them
  965. * another pass through the LRU.
  966. */
  967. if (dentry->d_lockref.count) {
  968. d_lru_isolate(lru, dentry);
  969. spin_unlock(&dentry->d_lock);
  970. return LRU_REMOVED;
  971. }
  972. if (dentry->d_flags & DCACHE_REFERENCED) {
  973. dentry->d_flags &= ~DCACHE_REFERENCED;
  974. spin_unlock(&dentry->d_lock);
  975. /*
  976. * The list move itself will be made by the common LRU code. At
  977. * this point, we've dropped the dentry->d_lock but keep the
  978. * lru lock. This is safe to do, since every list movement is
  979. * protected by the lru lock even if both locks are held.
  980. *
  981. * This is guaranteed by the fact that all LRU management
  982. * functions are intermediated by the LRU API calls like
  983. * list_lru_add and list_lru_del. List movement in this file
  984. * only ever occur through this functions or through callbacks
  985. * like this one, that are called from the LRU API.
  986. *
  987. * The only exceptions to this are functions like
  988. * shrink_dentry_list, and code that first checks for the
  989. * DCACHE_SHRINK_LIST flag. Those are guaranteed to be
  990. * operating only with stack provided lists after they are
  991. * properly isolated from the main list. It is thus, always a
  992. * local access.
  993. */
  994. return LRU_ROTATE;
  995. }
  996. d_lru_shrink_move(lru, dentry, freeable);
  997. spin_unlock(&dentry->d_lock);
  998. return LRU_REMOVED;
  999. }
  1000. /**
  1001. * prune_dcache_sb - shrink the dcache
  1002. * @sb: superblock
  1003. * @sc: shrink control, passed to list_lru_shrink_walk()
  1004. *
  1005. * Attempt to shrink the superblock dcache LRU by @sc->nr_to_scan entries. This
  1006. * is done when we need more memory and called from the superblock shrinker
  1007. * function.
  1008. *
  1009. * This function may fail to free any resources if all the dentries are in
  1010. * use.
  1011. */
  1012. long prune_dcache_sb(struct super_block *sb, struct shrink_control *sc)
  1013. {
  1014. LIST_HEAD(dispose);
  1015. long freed;
  1016. freed = list_lru_shrink_walk(&sb->s_dentry_lru, sc,
  1017. dentry_lru_isolate, &dispose);
  1018. shrink_dentry_list(&dispose);
  1019. return freed;
  1020. }
  1021. static enum lru_status dentry_lru_isolate_shrink(struct list_head *item,
  1022. struct list_lru_one *lru, spinlock_t *lru_lock, void *arg)
  1023. {
  1024. struct list_head *freeable = arg;
  1025. struct dentry *dentry = container_of(item, struct dentry, d_lru);
  1026. /*
  1027. * we are inverting the lru lock/dentry->d_lock here,
  1028. * so use a trylock. If we fail to get the lock, just skip
  1029. * it
  1030. */
  1031. if (!spin_trylock(&dentry->d_lock))
  1032. return LRU_SKIP;
  1033. d_lru_shrink_move(lru, dentry, freeable);
  1034. spin_unlock(&dentry->d_lock);
  1035. return LRU_REMOVED;
  1036. }
  1037. /**
  1038. * shrink_dcache_sb - shrink dcache for a superblock
  1039. * @sb: superblock
  1040. *
  1041. * Shrink the dcache for the specified super block. This is used to free
  1042. * the dcache before unmounting a file system.
  1043. */
  1044. void shrink_dcache_sb(struct super_block *sb)
  1045. {
  1046. long freed;
  1047. do {
  1048. LIST_HEAD(dispose);
  1049. freed = list_lru_walk(&sb->s_dentry_lru,
  1050. dentry_lru_isolate_shrink, &dispose, UINT_MAX);
  1051. this_cpu_sub(nr_dentry_unused, freed);
  1052. shrink_dentry_list(&dispose);
  1053. } while (freed > 0);
  1054. }
  1055. EXPORT_SYMBOL(shrink_dcache_sb);
  1056. /**
  1057. * enum d_walk_ret - action to talke during tree walk
  1058. * @D_WALK_CONTINUE: contrinue walk
  1059. * @D_WALK_QUIT: quit walk
  1060. * @D_WALK_NORETRY: quit when retry is needed
  1061. * @D_WALK_SKIP: skip this dentry and its children
  1062. */
  1063. enum d_walk_ret {
  1064. D_WALK_CONTINUE,
  1065. D_WALK_QUIT,
  1066. D_WALK_NORETRY,
  1067. D_WALK_SKIP,
  1068. };
  1069. /**
  1070. * d_walk - walk the dentry tree
  1071. * @parent: start of walk
  1072. * @data: data passed to @enter() and @finish()
  1073. * @enter: callback when first entering the dentry
  1074. * @finish: callback when successfully finished the walk
  1075. *
  1076. * The @enter() and @finish() callbacks are called with d_lock held.
  1077. */
  1078. static void d_walk(struct dentry *parent, void *data,
  1079. enum d_walk_ret (*enter)(void *, struct dentry *),
  1080. void (*finish)(void *))
  1081. {
  1082. struct dentry *this_parent;
  1083. struct list_head *next;
  1084. unsigned seq = 0;
  1085. enum d_walk_ret ret;
  1086. bool retry = true;
  1087. again:
  1088. read_seqbegin_or_lock(&rename_lock, &seq);
  1089. this_parent = parent;
  1090. spin_lock(&this_parent->d_lock);
  1091. ret = enter(data, this_parent);
  1092. switch (ret) {
  1093. case D_WALK_CONTINUE:
  1094. break;
  1095. case D_WALK_QUIT:
  1096. case D_WALK_SKIP:
  1097. goto out_unlock;
  1098. case D_WALK_NORETRY:
  1099. retry = false;
  1100. break;
  1101. }
  1102. repeat:
  1103. next = this_parent->d_subdirs.next;
  1104. resume:
  1105. while (next != &this_parent->d_subdirs) {
  1106. struct list_head *tmp = next;
  1107. struct dentry *dentry = list_entry(tmp, struct dentry, d_child);
  1108. next = tmp->next;
  1109. if (unlikely(dentry->d_flags & DCACHE_DENTRY_CURSOR))
  1110. continue;
  1111. spin_lock_nested(&dentry->d_lock, DENTRY_D_LOCK_NESTED);
  1112. ret = enter(data, dentry);
  1113. switch (ret) {
  1114. case D_WALK_CONTINUE:
  1115. break;
  1116. case D_WALK_QUIT:
  1117. spin_unlock(&dentry->d_lock);
  1118. goto out_unlock;
  1119. case D_WALK_NORETRY:
  1120. retry = false;
  1121. break;
  1122. case D_WALK_SKIP:
  1123. spin_unlock(&dentry->d_lock);
  1124. continue;
  1125. }
  1126. if (!list_empty(&dentry->d_subdirs)) {
  1127. spin_unlock(&this_parent->d_lock);
  1128. spin_release(&dentry->d_lock.dep_map, 1, _RET_IP_);
  1129. this_parent = dentry;
  1130. spin_acquire(&this_parent->d_lock.dep_map, 0, 1, _RET_IP_);
  1131. goto repeat;
  1132. }
  1133. spin_unlock(&dentry->d_lock);
  1134. }
  1135. /*
  1136. * All done at this level ... ascend and resume the search.
  1137. */
  1138. rcu_read_lock();
  1139. ascend:
  1140. if (this_parent != parent) {
  1141. struct dentry *child = this_parent;
  1142. this_parent = child->d_parent;
  1143. spin_unlock(&child->d_lock);
  1144. spin_lock(&this_parent->d_lock);
  1145. /* might go back up the wrong parent if we have had a rename. */
  1146. if (need_seqretry(&rename_lock, seq))
  1147. goto rename_retry;
  1148. /* go into the first sibling still alive */
  1149. do {
  1150. next = child->d_child.next;
  1151. if (next == &this_parent->d_subdirs)
  1152. goto ascend;
  1153. child = list_entry(next, struct dentry, d_child);
  1154. } while (unlikely(child->d_flags & DCACHE_DENTRY_KILLED));
  1155. rcu_read_unlock();
  1156. goto resume;
  1157. }
  1158. if (need_seqretry(&rename_lock, seq))
  1159. goto rename_retry;
  1160. rcu_read_unlock();
  1161. if (finish)
  1162. finish(data);
  1163. out_unlock:
  1164. spin_unlock(&this_parent->d_lock);
  1165. done_seqretry(&rename_lock, seq);
  1166. return;
  1167. rename_retry:
  1168. spin_unlock(&this_parent->d_lock);
  1169. rcu_read_unlock();
  1170. BUG_ON(seq & 1);
  1171. if (!retry)
  1172. return;
  1173. seq = 1;
  1174. goto again;
  1175. }
  1176. /*
  1177. * Search for at least 1 mount point in the dentry's subdirs.
  1178. * We descend to the next level whenever the d_subdirs
  1179. * list is non-empty and continue searching.
  1180. */
  1181. static enum d_walk_ret check_mount(void *data, struct dentry *dentry)
  1182. {
  1183. int *ret = data;
  1184. if (d_mountpoint(dentry)) {
  1185. *ret = 1;
  1186. return D_WALK_QUIT;
  1187. }
  1188. return D_WALK_CONTINUE;
  1189. }
  1190. /**
  1191. * have_submounts - check for mounts over a dentry
  1192. * @parent: dentry to check.
  1193. *
  1194. * Return true if the parent or its subdirectories contain
  1195. * a mount point
  1196. */
  1197. int have_submounts(struct dentry *parent)
  1198. {
  1199. int ret = 0;
  1200. d_walk(parent, &ret, check_mount, NULL);
  1201. return ret;
  1202. }
  1203. EXPORT_SYMBOL(have_submounts);
  1204. /*
  1205. * Called by mount code to set a mountpoint and check if the mountpoint is
  1206. * reachable (e.g. NFS can unhash a directory dentry and then the complete
  1207. * subtree can become unreachable).
  1208. *
  1209. * Only one of d_invalidate() and d_set_mounted() must succeed. For
  1210. * this reason take rename_lock and d_lock on dentry and ancestors.
  1211. */
  1212. int d_set_mounted(struct dentry *dentry)
  1213. {
  1214. struct dentry *p;
  1215. int ret = -ENOENT;
  1216. write_seqlock(&rename_lock);
  1217. for (p = dentry->d_parent; !IS_ROOT(p); p = p->d_parent) {
  1218. /* Need exclusion wrt. d_invalidate() */
  1219. spin_lock(&p->d_lock);
  1220. if (unlikely(d_unhashed(p))) {
  1221. spin_unlock(&p->d_lock);
  1222. goto out;
  1223. }
  1224. spin_unlock(&p->d_lock);
  1225. }
  1226. spin_lock(&dentry->d_lock);
  1227. if (!d_unlinked(dentry)) {
  1228. dentry->d_flags |= DCACHE_MOUNTED;
  1229. ret = 0;
  1230. }
  1231. spin_unlock(&dentry->d_lock);
  1232. out:
  1233. write_sequnlock(&rename_lock);
  1234. return ret;
  1235. }
  1236. /*
  1237. * Search the dentry child list of the specified parent,
  1238. * and move any unused dentries to the end of the unused
  1239. * list for prune_dcache(). We descend to the next level
  1240. * whenever the d_subdirs list is non-empty and continue
  1241. * searching.
  1242. *
  1243. * It returns zero iff there are no unused children,
  1244. * otherwise it returns the number of children moved to
  1245. * the end of the unused list. This may not be the total
  1246. * number of unused children, because select_parent can
  1247. * drop the lock and return early due to latency
  1248. * constraints.
  1249. */
  1250. struct select_data {
  1251. struct dentry *start;
  1252. struct list_head dispose;
  1253. int found;
  1254. };
  1255. static enum d_walk_ret select_collect(void *_data, struct dentry *dentry)
  1256. {
  1257. struct select_data *data = _data;
  1258. enum d_walk_ret ret = D_WALK_CONTINUE;
  1259. if (data->start == dentry)
  1260. goto out;
  1261. if (dentry->d_flags & DCACHE_SHRINK_LIST) {
  1262. data->found++;
  1263. } else {
  1264. if (dentry->d_flags & DCACHE_LRU_LIST)
  1265. d_lru_del(dentry);
  1266. if (!dentry->d_lockref.count) {
  1267. d_shrink_add(dentry, &data->dispose);
  1268. data->found++;
  1269. }
  1270. }
  1271. /*
  1272. * We can return to the caller if we have found some (this
  1273. * ensures forward progress). We'll be coming back to find
  1274. * the rest.
  1275. */
  1276. if (!list_empty(&data->dispose))
  1277. ret = need_resched() ? D_WALK_QUIT : D_WALK_NORETRY;
  1278. out:
  1279. return ret;
  1280. }
  1281. /**
  1282. * shrink_dcache_parent - prune dcache
  1283. * @parent: parent of entries to prune
  1284. *
  1285. * Prune the dcache to remove unused children of the parent dentry.
  1286. */
  1287. void shrink_dcache_parent(struct dentry *parent)
  1288. {
  1289. for (;;) {
  1290. struct select_data data;
  1291. INIT_LIST_HEAD(&data.dispose);
  1292. data.start = parent;
  1293. data.found = 0;
  1294. d_walk(parent, &data, select_collect, NULL);
  1295. if (!data.found)
  1296. break;
  1297. shrink_dentry_list(&data.dispose);
  1298. cond_resched();
  1299. }
  1300. }
  1301. EXPORT_SYMBOL(shrink_dcache_parent);
  1302. static enum d_walk_ret umount_check(void *_data, struct dentry *dentry)
  1303. {
  1304. /* it has busy descendents; complain about those instead */
  1305. if (!list_empty(&dentry->d_subdirs))
  1306. return D_WALK_CONTINUE;
  1307. /* root with refcount 1 is fine */
  1308. if (dentry == _data && dentry->d_lockref.count == 1)
  1309. return D_WALK_CONTINUE;
  1310. printk(KERN_ERR "BUG: Dentry %p{i=%lx,n=%pd} "
  1311. " still in use (%d) [unmount of %s %s]\n",
  1312. dentry,
  1313. dentry->d_inode ?
  1314. dentry->d_inode->i_ino : 0UL,
  1315. dentry,
  1316. dentry->d_lockref.count,
  1317. dentry->d_sb->s_type->name,
  1318. dentry->d_sb->s_id);
  1319. WARN_ON(1);
  1320. return D_WALK_CONTINUE;
  1321. }
  1322. static void do_one_tree(struct dentry *dentry)
  1323. {
  1324. shrink_dcache_parent(dentry);
  1325. d_walk(dentry, dentry, umount_check, NULL);
  1326. d_drop(dentry);
  1327. dput(dentry);
  1328. }
  1329. /*
  1330. * destroy the dentries attached to a superblock on unmounting
  1331. */
  1332. void shrink_dcache_for_umount(struct super_block *sb)
  1333. {
  1334. struct dentry *dentry;
  1335. WARN(down_read_trylock(&sb->s_umount), "s_umount should've been locked");
  1336. dentry = sb->s_root;
  1337. sb->s_root = NULL;
  1338. do_one_tree(dentry);
  1339. while (!hlist_bl_empty(&sb->s_anon)) {
  1340. dentry = dget(hlist_bl_entry(hlist_bl_first(&sb->s_anon), struct dentry, d_hash));
  1341. do_one_tree(dentry);
  1342. }
  1343. }
  1344. struct detach_data {
  1345. struct select_data select;
  1346. struct dentry *mountpoint;
  1347. };
  1348. static enum d_walk_ret detach_and_collect(void *_data, struct dentry *dentry)
  1349. {
  1350. struct detach_data *data = _data;
  1351. if (d_mountpoint(dentry)) {
  1352. __dget_dlock(dentry);
  1353. data->mountpoint = dentry;
  1354. return D_WALK_QUIT;
  1355. }
  1356. return select_collect(&data->select, dentry);
  1357. }
  1358. static void check_and_drop(void *_data)
  1359. {
  1360. struct detach_data *data = _data;
  1361. if (!data->mountpoint && !data->select.found)
  1362. __d_drop(data->select.start);
  1363. }
  1364. /**
  1365. * d_invalidate - detach submounts, prune dcache, and drop
  1366. * @dentry: dentry to invalidate (aka detach, prune and drop)
  1367. *
  1368. * no dcache lock.
  1369. *
  1370. * The final d_drop is done as an atomic operation relative to
  1371. * rename_lock ensuring there are no races with d_set_mounted. This
  1372. * ensures there are no unhashed dentries on the path to a mountpoint.
  1373. */
  1374. void d_invalidate(struct dentry *dentry)
  1375. {
  1376. /*
  1377. * If it's already been dropped, return OK.
  1378. */
  1379. spin_lock(&dentry->d_lock);
  1380. if (d_unhashed(dentry)) {
  1381. spin_unlock(&dentry->d_lock);
  1382. return;
  1383. }
  1384. spin_unlock(&dentry->d_lock);
  1385. /* Negative dentries can be dropped without further checks */
  1386. if (!dentry->d_inode) {
  1387. d_drop(dentry);
  1388. return;
  1389. }
  1390. for (;;) {
  1391. struct detach_data data;
  1392. data.mountpoint = NULL;
  1393. INIT_LIST_HEAD(&data.select.dispose);
  1394. data.select.start = dentry;
  1395. data.select.found = 0;
  1396. d_walk(dentry, &data, detach_and_collect, check_and_drop);
  1397. if (data.select.found)
  1398. shrink_dentry_list(&data.select.dispose);
  1399. if (data.mountpoint) {
  1400. detach_mounts(data.mountpoint);
  1401. dput(data.mountpoint);
  1402. }
  1403. if (!data.mountpoint && !data.select.found)
  1404. break;
  1405. cond_resched();
  1406. }
  1407. }
  1408. EXPORT_SYMBOL(d_invalidate);
  1409. /**
  1410. * __d_alloc - allocate a dcache entry
  1411. * @sb: filesystem it will belong to
  1412. * @name: qstr of the name
  1413. *
  1414. * Allocates a dentry. It returns %NULL if there is insufficient memory
  1415. * available. On a success the dentry is returned. The name passed in is
  1416. * copied and the copy passed in may be reused after this call.
  1417. */
  1418. struct dentry *__d_alloc(struct super_block *sb, const struct qstr *name)
  1419. {
  1420. struct dentry *dentry;
  1421. char *dname;
  1422. dentry = kmem_cache_alloc(dentry_cache, GFP_KERNEL);
  1423. if (!dentry)
  1424. return NULL;
  1425. /*
  1426. * We guarantee that the inline name is always NUL-terminated.
  1427. * This way the memcpy() done by the name switching in rename
  1428. * will still always have a NUL at the end, even if we might
  1429. * be overwriting an internal NUL character
  1430. */
  1431. dentry->d_iname[DNAME_INLINE_LEN-1] = 0;
  1432. if (unlikely(!name)) {
  1433. static const struct qstr anon = QSTR_INIT("/", 1);
  1434. name = &anon;
  1435. dname = dentry->d_iname;
  1436. } else if (name->len > DNAME_INLINE_LEN-1) {
  1437. size_t size = offsetof(struct external_name, name[1]);
  1438. struct external_name *p = kmalloc(size + name->len,
  1439. GFP_KERNEL_ACCOUNT);
  1440. if (!p) {
  1441. kmem_cache_free(dentry_cache, dentry);
  1442. return NULL;
  1443. }
  1444. atomic_set(&p->u.count, 1);
  1445. dname = p->name;
  1446. if (IS_ENABLED(CONFIG_DCACHE_WORD_ACCESS))
  1447. kasan_unpoison_shadow(dname,
  1448. round_up(name->len + 1, sizeof(unsigned long)));
  1449. } else {
  1450. dname = dentry->d_iname;
  1451. }
  1452. dentry->d_name.len = name->len;
  1453. dentry->d_name.hash = name->hash;
  1454. memcpy(dname, name->name, name->len);
  1455. dname[name->len] = 0;
  1456. /* Make sure we always see the terminating NUL character */
  1457. smp_wmb();
  1458. dentry->d_name.name = dname;
  1459. dentry->d_lockref.count = 1;
  1460. dentry->d_flags = 0;
  1461. spin_lock_init(&dentry->d_lock);
  1462. seqcount_init(&dentry->d_seq);
  1463. dentry->d_inode = NULL;
  1464. dentry->d_parent = dentry;
  1465. dentry->d_sb = sb;
  1466. dentry->d_op = NULL;
  1467. dentry->d_fsdata = NULL;
  1468. INIT_HLIST_BL_NODE(&dentry->d_hash);
  1469. INIT_LIST_HEAD(&dentry->d_lru);
  1470. INIT_LIST_HEAD(&dentry->d_subdirs);
  1471. INIT_HLIST_NODE(&dentry->d_u.d_alias);
  1472. INIT_LIST_HEAD(&dentry->d_child);
  1473. d_set_d_op(dentry, dentry->d_sb->s_d_op);
  1474. this_cpu_inc(nr_dentry);
  1475. return dentry;
  1476. }
  1477. /**
  1478. * d_alloc - allocate a dcache entry
  1479. * @parent: parent of entry to allocate
  1480. * @name: qstr of the name
  1481. *
  1482. * Allocates a dentry. It returns %NULL if there is insufficient memory
  1483. * available. On a success the dentry is returned. The name passed in is
  1484. * copied and the copy passed in may be reused after this call.
  1485. */
  1486. struct dentry *d_alloc(struct dentry * parent, const struct qstr *name)
  1487. {
  1488. struct dentry *dentry = __d_alloc(parent->d_sb, name);
  1489. if (!dentry)
  1490. return NULL;
  1491. dentry->d_flags |= DCACHE_RCUACCESS;
  1492. spin_lock(&parent->d_lock);
  1493. /*
  1494. * don't need child lock because it is not subject
  1495. * to concurrency here
  1496. */
  1497. __dget_dlock(parent);
  1498. dentry->d_parent = parent;
  1499. list_add(&dentry->d_child, &parent->d_subdirs);
  1500. spin_unlock(&parent->d_lock);
  1501. return dentry;
  1502. }
  1503. EXPORT_SYMBOL(d_alloc);
  1504. struct dentry *d_alloc_cursor(struct dentry * parent)
  1505. {
  1506. struct dentry *dentry = __d_alloc(parent->d_sb, NULL);
  1507. if (dentry) {
  1508. dentry->d_flags |= DCACHE_RCUACCESS | DCACHE_DENTRY_CURSOR;
  1509. dentry->d_parent = dget(parent);
  1510. }
  1511. return dentry;
  1512. }
  1513. /**
  1514. * d_alloc_pseudo - allocate a dentry (for lookup-less filesystems)
  1515. * @sb: the superblock
  1516. * @name: qstr of the name
  1517. *
  1518. * For a filesystem that just pins its dentries in memory and never
  1519. * performs lookups at all, return an unhashed IS_ROOT dentry.
  1520. */
  1521. struct dentry *d_alloc_pseudo(struct super_block *sb, const struct qstr *name)
  1522. {
  1523. return __d_alloc(sb, name);
  1524. }
  1525. EXPORT_SYMBOL(d_alloc_pseudo);
  1526. struct dentry *d_alloc_name(struct dentry *parent, const char *name)
  1527. {
  1528. struct qstr q;
  1529. q.name = name;
  1530. q.hash_len = hashlen_string(name);
  1531. return d_alloc(parent, &q);
  1532. }
  1533. EXPORT_SYMBOL(d_alloc_name);
  1534. void d_set_d_op(struct dentry *dentry, const struct dentry_operations *op)
  1535. {
  1536. WARN_ON_ONCE(dentry->d_op);
  1537. WARN_ON_ONCE(dentry->d_flags & (DCACHE_OP_HASH |
  1538. DCACHE_OP_COMPARE |
  1539. DCACHE_OP_REVALIDATE |
  1540. DCACHE_OP_WEAK_REVALIDATE |
  1541. DCACHE_OP_DELETE |
  1542. DCACHE_OP_SELECT_INODE |
  1543. DCACHE_OP_REAL));
  1544. dentry->d_op = op;
  1545. if (!op)
  1546. return;
  1547. if (op->d_hash)
  1548. dentry->d_flags |= DCACHE_OP_HASH;
  1549. if (op->d_compare)
  1550. dentry->d_flags |= DCACHE_OP_COMPARE;
  1551. if (op->d_revalidate)
  1552. dentry->d_flags |= DCACHE_OP_REVALIDATE;
  1553. if (op->d_weak_revalidate)
  1554. dentry->d_flags |= DCACHE_OP_WEAK_REVALIDATE;
  1555. if (op->d_delete)
  1556. dentry->d_flags |= DCACHE_OP_DELETE;
  1557. if (op->d_prune)
  1558. dentry->d_flags |= DCACHE_OP_PRUNE;
  1559. if (op->d_select_inode)
  1560. dentry->d_flags |= DCACHE_OP_SELECT_INODE;
  1561. if (op->d_real)
  1562. dentry->d_flags |= DCACHE_OP_REAL;
  1563. }
  1564. EXPORT_SYMBOL(d_set_d_op);
  1565. /*
  1566. * d_set_fallthru - Mark a dentry as falling through to a lower layer
  1567. * @dentry - The dentry to mark
  1568. *
  1569. * Mark a dentry as falling through to the lower layer (as set with
  1570. * d_pin_lower()). This flag may be recorded on the medium.
  1571. */
  1572. void d_set_fallthru(struct dentry *dentry)
  1573. {
  1574. spin_lock(&dentry->d_lock);
  1575. dentry->d_flags |= DCACHE_FALLTHRU;
  1576. spin_unlock(&dentry->d_lock);
  1577. }
  1578. EXPORT_SYMBOL(d_set_fallthru);
  1579. static unsigned d_flags_for_inode(struct inode *inode)
  1580. {
  1581. unsigned add_flags = DCACHE_REGULAR_TYPE;
  1582. if (!inode)
  1583. return DCACHE_MISS_TYPE;
  1584. if (S_ISDIR(inode->i_mode)) {
  1585. add_flags = DCACHE_DIRECTORY_TYPE;
  1586. if (unlikely(!(inode->i_opflags & IOP_LOOKUP))) {
  1587. if (unlikely(!inode->i_op->lookup))
  1588. add_flags = DCACHE_AUTODIR_TYPE;
  1589. else
  1590. inode->i_opflags |= IOP_LOOKUP;
  1591. }
  1592. goto type_determined;
  1593. }
  1594. if (unlikely(!(inode->i_opflags & IOP_NOFOLLOW))) {
  1595. if (unlikely(inode->i_op->get_link)) {
  1596. add_flags = DCACHE_SYMLINK_TYPE;
  1597. goto type_determined;
  1598. }
  1599. inode->i_opflags |= IOP_NOFOLLOW;
  1600. }
  1601. if (unlikely(!S_ISREG(inode->i_mode)))
  1602. add_flags = DCACHE_SPECIAL_TYPE;
  1603. type_determined:
  1604. if (unlikely(IS_AUTOMOUNT(inode)))
  1605. add_flags |= DCACHE_NEED_AUTOMOUNT;
  1606. return add_flags;
  1607. }
  1608. static void __d_instantiate(struct dentry *dentry, struct inode *inode)
  1609. {
  1610. unsigned add_flags = d_flags_for_inode(inode);
  1611. WARN_ON(d_in_lookup(dentry));
  1612. spin_lock(&dentry->d_lock);
  1613. hlist_add_head(&dentry->d_u.d_alias, &inode->i_dentry);
  1614. raw_write_seqcount_begin(&dentry->d_seq);
  1615. __d_set_inode_and_type(dentry, inode, add_flags);
  1616. raw_write_seqcount_end(&dentry->d_seq);
  1617. __fsnotify_d_instantiate(dentry);
  1618. spin_unlock(&dentry->d_lock);
  1619. }
  1620. /**
  1621. * d_instantiate - fill in inode information for a dentry
  1622. * @entry: dentry to complete
  1623. * @inode: inode to attach to this dentry
  1624. *
  1625. * Fill in inode information in the entry.
  1626. *
  1627. * This turns negative dentries into productive full members
  1628. * of society.
  1629. *
  1630. * NOTE! This assumes that the inode count has been incremented
  1631. * (or otherwise set) by the caller to indicate that it is now
  1632. * in use by the dcache.
  1633. */
  1634. void d_instantiate(struct dentry *entry, struct inode * inode)
  1635. {
  1636. BUG_ON(!hlist_unhashed(&entry->d_u.d_alias));
  1637. if (inode) {
  1638. security_d_instantiate(entry, inode);
  1639. spin_lock(&inode->i_lock);
  1640. __d_instantiate(entry, inode);
  1641. spin_unlock(&inode->i_lock);
  1642. }
  1643. }
  1644. EXPORT_SYMBOL(d_instantiate);
  1645. /**
  1646. * d_instantiate_no_diralias - instantiate a non-aliased dentry
  1647. * @entry: dentry to complete
  1648. * @inode: inode to attach to this dentry
  1649. *
  1650. * Fill in inode information in the entry. If a directory alias is found, then
  1651. * return an error (and drop inode). Together with d_materialise_unique() this
  1652. * guarantees that a directory inode may never have more than one alias.
  1653. */
  1654. int d_instantiate_no_diralias(struct dentry *entry, struct inode *inode)
  1655. {
  1656. BUG_ON(!hlist_unhashed(&entry->d_u.d_alias));
  1657. security_d_instantiate(entry, inode);
  1658. spin_lock(&inode->i_lock);
  1659. if (S_ISDIR(inode->i_mode) && !hlist_empty(&inode->i_dentry)) {
  1660. spin_unlock(&inode->i_lock);
  1661. iput(inode);
  1662. return -EBUSY;
  1663. }
  1664. __d_instantiate(entry, inode);
  1665. spin_unlock(&inode->i_lock);
  1666. return 0;
  1667. }
  1668. EXPORT_SYMBOL(d_instantiate_no_diralias);
  1669. struct dentry *d_make_root(struct inode *root_inode)
  1670. {
  1671. struct dentry *res = NULL;
  1672. if (root_inode) {
  1673. res = __d_alloc(root_inode->i_sb, NULL);
  1674. if (res)
  1675. d_instantiate(res, root_inode);
  1676. else
  1677. iput(root_inode);
  1678. }
  1679. return res;
  1680. }
  1681. EXPORT_SYMBOL(d_make_root);
  1682. static struct dentry * __d_find_any_alias(struct inode *inode)
  1683. {
  1684. struct dentry *alias;
  1685. if (hlist_empty(&inode->i_dentry))
  1686. return NULL;
  1687. alias = hlist_entry(inode->i_dentry.first, struct dentry, d_u.d_alias);
  1688. __dget(alias);
  1689. return alias;
  1690. }
  1691. /**
  1692. * d_find_any_alias - find any alias for a given inode
  1693. * @inode: inode to find an alias for
  1694. *
  1695. * If any aliases exist for the given inode, take and return a
  1696. * reference for one of them. If no aliases exist, return %NULL.
  1697. */
  1698. struct dentry *d_find_any_alias(struct inode *inode)
  1699. {
  1700. struct dentry *de;
  1701. spin_lock(&inode->i_lock);
  1702. de = __d_find_any_alias(inode);
  1703. spin_unlock(&inode->i_lock);
  1704. return de;
  1705. }
  1706. EXPORT_SYMBOL(d_find_any_alias);
  1707. static struct dentry *__d_obtain_alias(struct inode *inode, int disconnected)
  1708. {
  1709. struct dentry *tmp;
  1710. struct dentry *res;
  1711. unsigned add_flags;
  1712. if (!inode)
  1713. return ERR_PTR(-ESTALE);
  1714. if (IS_ERR(inode))
  1715. return ERR_CAST(inode);
  1716. res = d_find_any_alias(inode);
  1717. if (res)
  1718. goto out_iput;
  1719. tmp = __d_alloc(inode->i_sb, NULL);
  1720. if (!tmp) {
  1721. res = ERR_PTR(-ENOMEM);
  1722. goto out_iput;
  1723. }
  1724. security_d_instantiate(tmp, inode);
  1725. spin_lock(&inode->i_lock);
  1726. res = __d_find_any_alias(inode);
  1727. if (res) {
  1728. spin_unlock(&inode->i_lock);
  1729. dput(tmp);
  1730. goto out_iput;
  1731. }
  1732. /* attach a disconnected dentry */
  1733. add_flags = d_flags_for_inode(inode);
  1734. if (disconnected)
  1735. add_flags |= DCACHE_DISCONNECTED;
  1736. spin_lock(&tmp->d_lock);
  1737. __d_set_inode_and_type(tmp, inode, add_flags);
  1738. hlist_add_head(&tmp->d_u.d_alias, &inode->i_dentry);
  1739. hlist_bl_lock(&tmp->d_sb->s_anon);
  1740. hlist_bl_add_head(&tmp->d_hash, &tmp->d_sb->s_anon);
  1741. hlist_bl_unlock(&tmp->d_sb->s_anon);
  1742. spin_unlock(&tmp->d_lock);
  1743. spin_unlock(&inode->i_lock);
  1744. return tmp;
  1745. out_iput:
  1746. iput(inode);
  1747. return res;
  1748. }
  1749. /**
  1750. * d_obtain_alias - find or allocate a DISCONNECTED dentry for a given inode
  1751. * @inode: inode to allocate the dentry for
  1752. *
  1753. * Obtain a dentry for an inode resulting from NFS filehandle conversion or
  1754. * similar open by handle operations. The returned dentry may be anonymous,
  1755. * or may have a full name (if the inode was already in the cache).
  1756. *
  1757. * When called on a directory inode, we must ensure that the inode only ever
  1758. * has one dentry. If a dentry is found, that is returned instead of
  1759. * allocating a new one.
  1760. *
  1761. * On successful return, the reference to the inode has been transferred
  1762. * to the dentry. In case of an error the reference on the inode is released.
  1763. * To make it easier to use in export operations a %NULL or IS_ERR inode may
  1764. * be passed in and the error will be propagated to the return value,
  1765. * with a %NULL @inode replaced by ERR_PTR(-ESTALE).
  1766. */
  1767. struct dentry *d_obtain_alias(struct inode *inode)
  1768. {
  1769. return __d_obtain_alias(inode, 1);
  1770. }
  1771. EXPORT_SYMBOL(d_obtain_alias);
  1772. /**
  1773. * d_obtain_root - find or allocate a dentry for a given inode
  1774. * @inode: inode to allocate the dentry for
  1775. *
  1776. * Obtain an IS_ROOT dentry for the root of a filesystem.
  1777. *
  1778. * We must ensure that directory inodes only ever have one dentry. If a
  1779. * dentry is found, that is returned instead of allocating a new one.
  1780. *
  1781. * On successful return, the reference to the inode has been transferred
  1782. * to the dentry. In case of an error the reference on the inode is
  1783. * released. A %NULL or IS_ERR inode may be passed in and will be the
  1784. * error will be propagate to the return value, with a %NULL @inode
  1785. * replaced by ERR_PTR(-ESTALE).
  1786. */
  1787. struct dentry *d_obtain_root(struct inode *inode)
  1788. {
  1789. return __d_obtain_alias(inode, 0);
  1790. }
  1791. EXPORT_SYMBOL(d_obtain_root);
  1792. /**
  1793. * d_add_ci - lookup or allocate new dentry with case-exact name
  1794. * @inode: the inode case-insensitive lookup has found
  1795. * @dentry: the negative dentry that was passed to the parent's lookup func
  1796. * @name: the case-exact name to be associated with the returned dentry
  1797. *
  1798. * This is to avoid filling the dcache with case-insensitive names to the
  1799. * same inode, only the actual correct case is stored in the dcache for
  1800. * case-insensitive filesystems.
  1801. *
  1802. * For a case-insensitive lookup match and if the the case-exact dentry
  1803. * already exists in in the dcache, use it and return it.
  1804. *
  1805. * If no entry exists with the exact case name, allocate new dentry with
  1806. * the exact case, and return the spliced entry.
  1807. */
  1808. struct dentry *d_add_ci(struct dentry *dentry, struct inode *inode,
  1809. struct qstr *name)
  1810. {
  1811. struct dentry *found, *res;
  1812. /*
  1813. * First check if a dentry matching the name already exists,
  1814. * if not go ahead and create it now.
  1815. */
  1816. found = d_hash_and_lookup(dentry->d_parent, name);
  1817. if (found) {
  1818. iput(inode);
  1819. return found;
  1820. }
  1821. if (d_in_lookup(dentry)) {
  1822. found = d_alloc_parallel(dentry->d_parent, name,
  1823. dentry->d_wait);
  1824. if (IS_ERR(found) || !d_in_lookup(found)) {
  1825. iput(inode);
  1826. return found;
  1827. }
  1828. } else {
  1829. found = d_alloc(dentry->d_parent, name);
  1830. if (!found) {
  1831. iput(inode);
  1832. return ERR_PTR(-ENOMEM);
  1833. }
  1834. }
  1835. res = d_splice_alias(inode, found);
  1836. if (res) {
  1837. dput(found);
  1838. return res;
  1839. }
  1840. return found;
  1841. }
  1842. EXPORT_SYMBOL(d_add_ci);
  1843. /*
  1844. * Do the slow-case of the dentry name compare.
  1845. *
  1846. * Unlike the dentry_cmp() function, we need to atomically
  1847. * load the name and length information, so that the
  1848. * filesystem can rely on them, and can use the 'name' and
  1849. * 'len' information without worrying about walking off the
  1850. * end of memory etc.
  1851. *
  1852. * Thus the read_seqcount_retry() and the "duplicate" info
  1853. * in arguments (the low-level filesystem should not look
  1854. * at the dentry inode or name contents directly, since
  1855. * rename can change them while we're in RCU mode).
  1856. */
  1857. enum slow_d_compare {
  1858. D_COMP_OK,
  1859. D_COMP_NOMATCH,
  1860. D_COMP_SEQRETRY,
  1861. };
  1862. static noinline enum slow_d_compare slow_dentry_cmp(
  1863. const struct dentry *parent,
  1864. struct dentry *dentry,
  1865. unsigned int seq,
  1866. const struct qstr *name)
  1867. {
  1868. int tlen = dentry->d_name.len;
  1869. const char *tname = dentry->d_name.name;
  1870. if (read_seqcount_retry(&dentry->d_seq, seq)) {
  1871. cpu_relax();
  1872. return D_COMP_SEQRETRY;
  1873. }
  1874. if (parent->d_op->d_compare(parent, dentry, tlen, tname, name))
  1875. return D_COMP_NOMATCH;
  1876. return D_COMP_OK;
  1877. }
  1878. /**
  1879. * __d_lookup_rcu - search for a dentry (racy, store-free)
  1880. * @parent: parent dentry
  1881. * @name: qstr of name we wish to find
  1882. * @seqp: returns d_seq value at the point where the dentry was found
  1883. * Returns: dentry, or NULL
  1884. *
  1885. * __d_lookup_rcu is the dcache lookup function for rcu-walk name
  1886. * resolution (store-free path walking) design described in
  1887. * Documentation/filesystems/path-lookup.txt.
  1888. *
  1889. * This is not to be used outside core vfs.
  1890. *
  1891. * __d_lookup_rcu must only be used in rcu-walk mode, ie. with vfsmount lock
  1892. * held, and rcu_read_lock held. The returned dentry must not be stored into
  1893. * without taking d_lock and checking d_seq sequence count against @seq
  1894. * returned here.
  1895. *
  1896. * A refcount may be taken on the found dentry with the d_rcu_to_refcount
  1897. * function.
  1898. *
  1899. * Alternatively, __d_lookup_rcu may be called again to look up the child of
  1900. * the returned dentry, so long as its parent's seqlock is checked after the
  1901. * child is looked up. Thus, an interlocking stepping of sequence lock checks
  1902. * is formed, giving integrity down the path walk.
  1903. *
  1904. * NOTE! The caller *has* to check the resulting dentry against the sequence
  1905. * number we've returned before using any of the resulting dentry state!
  1906. */
  1907. struct dentry *__d_lookup_rcu(const struct dentry *parent,
  1908. const struct qstr *name,
  1909. unsigned *seqp)
  1910. {
  1911. u64 hashlen = name->hash_len;
  1912. const unsigned char *str = name->name;
  1913. struct hlist_bl_head *b = d_hash(parent, hashlen_hash(hashlen));
  1914. struct hlist_bl_node *node;
  1915. struct dentry *dentry;
  1916. /*
  1917. * Note: There is significant duplication with __d_lookup_rcu which is
  1918. * required to prevent single threaded performance regressions
  1919. * especially on architectures where smp_rmb (in seqcounts) are costly.
  1920. * Keep the two functions in sync.
  1921. */
  1922. /*
  1923. * The hash list is protected using RCU.
  1924. *
  1925. * Carefully use d_seq when comparing a candidate dentry, to avoid
  1926. * races with d_move().
  1927. *
  1928. * It is possible that concurrent renames can mess up our list
  1929. * walk here and result in missing our dentry, resulting in the
  1930. * false-negative result. d_lookup() protects against concurrent
  1931. * renames using rename_lock seqlock.
  1932. *
  1933. * See Documentation/filesystems/path-lookup.txt for more details.
  1934. */
  1935. hlist_bl_for_each_entry_rcu(dentry, node, b, d_hash) {
  1936. unsigned seq;
  1937. seqretry:
  1938. /*
  1939. * The dentry sequence count protects us from concurrent
  1940. * renames, and thus protects parent and name fields.
  1941. *
  1942. * The caller must perform a seqcount check in order
  1943. * to do anything useful with the returned dentry.
  1944. *
  1945. * NOTE! We do a "raw" seqcount_begin here. That means that
  1946. * we don't wait for the sequence count to stabilize if it
  1947. * is in the middle of a sequence change. If we do the slow
  1948. * dentry compare, we will do seqretries until it is stable,
  1949. * and if we end up with a successful lookup, we actually
  1950. * want to exit RCU lookup anyway.
  1951. */
  1952. seq = raw_seqcount_begin(&dentry->d_seq);
  1953. if (dentry->d_parent != parent)
  1954. continue;
  1955. if (d_unhashed(dentry))
  1956. continue;
  1957. if (unlikely(parent->d_flags & DCACHE_OP_COMPARE)) {
  1958. if (dentry->d_name.hash != hashlen_hash(hashlen))
  1959. continue;
  1960. *seqp = seq;
  1961. switch (slow_dentry_cmp(parent, dentry, seq, name)) {
  1962. case D_COMP_OK:
  1963. return dentry;
  1964. case D_COMP_NOMATCH:
  1965. continue;
  1966. default:
  1967. goto seqretry;
  1968. }
  1969. }
  1970. if (dentry->d_name.hash_len != hashlen)
  1971. continue;
  1972. *seqp = seq;
  1973. if (!dentry_cmp(dentry, str, hashlen_len(hashlen)))
  1974. return dentry;
  1975. }
  1976. return NULL;
  1977. }
  1978. /**
  1979. * d_lookup - search for a dentry
  1980. * @parent: parent dentry
  1981. * @name: qstr of name we wish to find
  1982. * Returns: dentry, or NULL
  1983. *
  1984. * d_lookup searches the children of the parent dentry for the name in
  1985. * question. If the dentry is found its reference count is incremented and the
  1986. * dentry is returned. The caller must use dput to free the entry when it has
  1987. * finished using it. %NULL is returned if the dentry does not exist.
  1988. */
  1989. struct dentry *d_lookup(const struct dentry *parent, const struct qstr *name)
  1990. {
  1991. struct dentry *dentry;
  1992. unsigned seq;
  1993. do {
  1994. seq = read_seqbegin(&rename_lock);
  1995. dentry = __d_lookup(parent, name);
  1996. if (dentry)
  1997. break;
  1998. } while (read_seqretry(&rename_lock, seq));
  1999. return dentry;
  2000. }
  2001. EXPORT_SYMBOL(d_lookup);
  2002. /**
  2003. * __d_lookup - search for a dentry (racy)
  2004. * @parent: parent dentry
  2005. * @name: qstr of name we wish to find
  2006. * Returns: dentry, or NULL
  2007. *
  2008. * __d_lookup is like d_lookup, however it may (rarely) return a
  2009. * false-negative result due to unrelated rename activity.
  2010. *
  2011. * __d_lookup is slightly faster by avoiding rename_lock read seqlock,
  2012. * however it must be used carefully, eg. with a following d_lookup in
  2013. * the case of failure.
  2014. *
  2015. * __d_lookup callers must be commented.
  2016. */
  2017. struct dentry *__d_lookup(const struct dentry *parent, const struct qstr *name)
  2018. {
  2019. unsigned int len = name->len;
  2020. unsigned int hash = name->hash;
  2021. const unsigned char *str = name->name;
  2022. struct hlist_bl_head *b = d_hash(parent, hash);
  2023. struct hlist_bl_node *node;
  2024. struct dentry *found = NULL;
  2025. struct dentry *dentry;
  2026. /*
  2027. * Note: There is significant duplication with __d_lookup_rcu which is
  2028. * required to prevent single threaded performance regressions
  2029. * especially on architectures where smp_rmb (in seqcounts) are costly.
  2030. * Keep the two functions in sync.
  2031. */
  2032. /*
  2033. * The hash list is protected using RCU.
  2034. *
  2035. * Take d_lock when comparing a candidate dentry, to avoid races
  2036. * with d_move().
  2037. *
  2038. * It is possible that concurrent renames can mess up our list
  2039. * walk here and result in missing our dentry, resulting in the
  2040. * false-negative result. d_lookup() protects against concurrent
  2041. * renames using rename_lock seqlock.
  2042. *
  2043. * See Documentation/filesystems/path-lookup.txt for more details.
  2044. */
  2045. rcu_read_lock();
  2046. hlist_bl_for_each_entry_rcu(dentry, node, b, d_hash) {
  2047. if (dentry->d_name.hash != hash)
  2048. continue;
  2049. spin_lock(&dentry->d_lock);
  2050. if (dentry->d_parent != parent)
  2051. goto next;
  2052. if (d_unhashed(dentry))
  2053. goto next;
  2054. /*
  2055. * It is safe to compare names since d_move() cannot
  2056. * change the qstr (protected by d_lock).
  2057. */
  2058. if (parent->d_flags & DCACHE_OP_COMPARE) {
  2059. int tlen = dentry->d_name.len;
  2060. const char *tname = dentry->d_name.name;
  2061. if (parent->d_op->d_compare(parent, dentry, tlen, tname, name))
  2062. goto next;
  2063. } else {
  2064. if (dentry->d_name.len != len)
  2065. goto next;
  2066. if (dentry_cmp(dentry, str, len))
  2067. goto next;
  2068. }
  2069. dentry->d_lockref.count++;
  2070. found = dentry;
  2071. spin_unlock(&dentry->d_lock);
  2072. break;
  2073. next:
  2074. spin_unlock(&dentry->d_lock);
  2075. }
  2076. rcu_read_unlock();
  2077. return found;
  2078. }
  2079. /**
  2080. * d_hash_and_lookup - hash the qstr then search for a dentry
  2081. * @dir: Directory to search in
  2082. * @name: qstr of name we wish to find
  2083. *
  2084. * On lookup failure NULL is returned; on bad name - ERR_PTR(-error)
  2085. */
  2086. struct dentry *d_hash_and_lookup(struct dentry *dir, struct qstr *name)
  2087. {
  2088. /*
  2089. * Check for a fs-specific hash function. Note that we must
  2090. * calculate the standard hash first, as the d_op->d_hash()
  2091. * routine may choose to leave the hash value unchanged.
  2092. */
  2093. name->hash = full_name_hash(name->name, name->len);
  2094. if (dir->d_flags & DCACHE_OP_HASH) {
  2095. int err = dir->d_op->d_hash(dir, name);
  2096. if (unlikely(err < 0))
  2097. return ERR_PTR(err);
  2098. }
  2099. return d_lookup(dir, name);
  2100. }
  2101. EXPORT_SYMBOL(d_hash_and_lookup);
  2102. /*
  2103. * When a file is deleted, we have two options:
  2104. * - turn this dentry into a negative dentry
  2105. * - unhash this dentry and free it.
  2106. *
  2107. * Usually, we want to just turn this into
  2108. * a negative dentry, but if anybody else is
  2109. * currently using the dentry or the inode
  2110. * we can't do that and we fall back on removing
  2111. * it from the hash queues and waiting for
  2112. * it to be deleted later when it has no users
  2113. */
  2114. /**
  2115. * d_delete - delete a dentry
  2116. * @dentry: The dentry to delete
  2117. *
  2118. * Turn the dentry into a negative dentry if possible, otherwise
  2119. * remove it from the hash queues so it can be deleted later
  2120. */
  2121. void d_delete(struct dentry * dentry)
  2122. {
  2123. struct inode *inode;
  2124. int isdir = 0;
  2125. /*
  2126. * Are we the only user?
  2127. */
  2128. again:
  2129. spin_lock(&dentry->d_lock);
  2130. inode = dentry->d_inode;
  2131. isdir = S_ISDIR(inode->i_mode);
  2132. if (dentry->d_lockref.count == 1) {
  2133. if (!spin_trylock(&inode->i_lock)) {
  2134. spin_unlock(&dentry->d_lock);
  2135. cpu_relax();
  2136. goto again;
  2137. }
  2138. dentry->d_flags &= ~DCACHE_CANT_MOUNT;
  2139. dentry_unlink_inode(dentry);
  2140. fsnotify_nameremove(dentry, isdir);
  2141. return;
  2142. }
  2143. if (!d_unhashed(dentry))
  2144. __d_drop(dentry);
  2145. spin_unlock(&dentry->d_lock);
  2146. fsnotify_nameremove(dentry, isdir);
  2147. }
  2148. EXPORT_SYMBOL(d_delete);
  2149. static void __d_rehash(struct dentry * entry, struct hlist_bl_head *b)
  2150. {
  2151. BUG_ON(!d_unhashed(entry));
  2152. hlist_bl_lock(b);
  2153. hlist_bl_add_head_rcu(&entry->d_hash, b);
  2154. hlist_bl_unlock(b);
  2155. }
  2156. static void _d_rehash(struct dentry * entry)
  2157. {
  2158. __d_rehash(entry, d_hash(entry->d_parent, entry->d_name.hash));
  2159. }
  2160. /**
  2161. * d_rehash - add an entry back to the hash
  2162. * @entry: dentry to add to the hash
  2163. *
  2164. * Adds a dentry to the hash according to its name.
  2165. */
  2166. void d_rehash(struct dentry * entry)
  2167. {
  2168. spin_lock(&entry->d_lock);
  2169. _d_rehash(entry);
  2170. spin_unlock(&entry->d_lock);
  2171. }
  2172. EXPORT_SYMBOL(d_rehash);
  2173. static inline unsigned start_dir_add(struct inode *dir)
  2174. {
  2175. for (;;) {
  2176. unsigned n = dir->i_dir_seq;
  2177. if (!(n & 1) && cmpxchg(&dir->i_dir_seq, n, n + 1) == n)
  2178. return n;
  2179. cpu_relax();
  2180. }
  2181. }
  2182. static inline void end_dir_add(struct inode *dir, unsigned n)
  2183. {
  2184. smp_store_release(&dir->i_dir_seq, n + 2);
  2185. }
  2186. static void d_wait_lookup(struct dentry *dentry)
  2187. {
  2188. if (d_in_lookup(dentry)) {
  2189. DECLARE_WAITQUEUE(wait, current);
  2190. add_wait_queue(dentry->d_wait, &wait);
  2191. do {
  2192. set_current_state(TASK_UNINTERRUPTIBLE);
  2193. spin_unlock(&dentry->d_lock);
  2194. schedule();
  2195. spin_lock(&dentry->d_lock);
  2196. } while (d_in_lookup(dentry));
  2197. }
  2198. }
  2199. struct dentry *d_alloc_parallel(struct dentry *parent,
  2200. const struct qstr *name,
  2201. wait_queue_head_t *wq)
  2202. {
  2203. unsigned int len = name->len;
  2204. unsigned int hash = name->hash;
  2205. const unsigned char *str = name->name;
  2206. struct hlist_bl_head *b = in_lookup_hash(parent, hash);
  2207. struct hlist_bl_node *node;
  2208. struct dentry *new = d_alloc(parent, name);
  2209. struct dentry *dentry;
  2210. unsigned seq, r_seq, d_seq;
  2211. if (unlikely(!new))
  2212. return ERR_PTR(-ENOMEM);
  2213. retry:
  2214. rcu_read_lock();
  2215. seq = smp_load_acquire(&parent->d_inode->i_dir_seq) & ~1;
  2216. r_seq = read_seqbegin(&rename_lock);
  2217. dentry = __d_lookup_rcu(parent, name, &d_seq);
  2218. if (unlikely(dentry)) {
  2219. if (!lockref_get_not_dead(&dentry->d_lockref)) {
  2220. rcu_read_unlock();
  2221. goto retry;
  2222. }
  2223. if (read_seqcount_retry(&dentry->d_seq, d_seq)) {
  2224. rcu_read_unlock();
  2225. dput(dentry);
  2226. goto retry;
  2227. }
  2228. rcu_read_unlock();
  2229. dput(new);
  2230. return dentry;
  2231. }
  2232. if (unlikely(read_seqretry(&rename_lock, r_seq))) {
  2233. rcu_read_unlock();
  2234. goto retry;
  2235. }
  2236. hlist_bl_lock(b);
  2237. if (unlikely(parent->d_inode->i_dir_seq != seq)) {
  2238. hlist_bl_unlock(b);
  2239. rcu_read_unlock();
  2240. goto retry;
  2241. }
  2242. /*
  2243. * No changes for the parent since the beginning of d_lookup().
  2244. * Since all removals from the chain happen with hlist_bl_lock(),
  2245. * any potential in-lookup matches are going to stay here until
  2246. * we unlock the chain. All fields are stable in everything
  2247. * we encounter.
  2248. */
  2249. hlist_bl_for_each_entry(dentry, node, b, d_u.d_in_lookup_hash) {
  2250. if (dentry->d_name.hash != hash)
  2251. continue;
  2252. if (dentry->d_parent != parent)
  2253. continue;
  2254. if (parent->d_flags & DCACHE_OP_COMPARE) {
  2255. int tlen = dentry->d_name.len;
  2256. const char *tname = dentry->d_name.name;
  2257. if (parent->d_op->d_compare(parent, dentry, tlen, tname, name))
  2258. continue;
  2259. } else {
  2260. if (dentry->d_name.len != len)
  2261. continue;
  2262. if (dentry_cmp(dentry, str, len))
  2263. continue;
  2264. }
  2265. hlist_bl_unlock(b);
  2266. /* now we can try to grab a reference */
  2267. if (!lockref_get_not_dead(&dentry->d_lockref)) {
  2268. rcu_read_unlock();
  2269. goto retry;
  2270. }
  2271. rcu_read_unlock();
  2272. /*
  2273. * somebody is likely to be still doing lookup for it;
  2274. * wait for them to finish
  2275. */
  2276. spin_lock(&dentry->d_lock);
  2277. d_wait_lookup(dentry);
  2278. /*
  2279. * it's not in-lookup anymore; in principle we should repeat
  2280. * everything from dcache lookup, but it's likely to be what
  2281. * d_lookup() would've found anyway. If it is, just return it;
  2282. * otherwise we really have to repeat the whole thing.
  2283. */
  2284. if (unlikely(dentry->d_name.hash != hash))
  2285. goto mismatch;
  2286. if (unlikely(dentry->d_parent != parent))
  2287. goto mismatch;
  2288. if (unlikely(d_unhashed(dentry)))
  2289. goto mismatch;
  2290. if (parent->d_flags & DCACHE_OP_COMPARE) {
  2291. int tlen = dentry->d_name.len;
  2292. const char *tname = dentry->d_name.name;
  2293. if (parent->d_op->d_compare(parent, dentry, tlen, tname, name))
  2294. goto mismatch;
  2295. } else {
  2296. if (unlikely(dentry->d_name.len != len))
  2297. goto mismatch;
  2298. if (unlikely(dentry_cmp(dentry, str, len)))
  2299. goto mismatch;
  2300. }
  2301. /* OK, it *is* a hashed match; return it */
  2302. spin_unlock(&dentry->d_lock);
  2303. dput(new);
  2304. return dentry;
  2305. }
  2306. rcu_read_unlock();
  2307. /* we can't take ->d_lock here; it's OK, though. */
  2308. new->d_flags |= DCACHE_PAR_LOOKUP;
  2309. new->d_wait = wq;
  2310. hlist_bl_add_head_rcu(&new->d_u.d_in_lookup_hash, b);
  2311. hlist_bl_unlock(b);
  2312. return new;
  2313. mismatch:
  2314. spin_unlock(&dentry->d_lock);
  2315. dput(dentry);
  2316. goto retry;
  2317. }
  2318. EXPORT_SYMBOL(d_alloc_parallel);
  2319. void __d_lookup_done(struct dentry *dentry)
  2320. {
  2321. struct hlist_bl_head *b = in_lookup_hash(dentry->d_parent,
  2322. dentry->d_name.hash);
  2323. hlist_bl_lock(b);
  2324. dentry->d_flags &= ~DCACHE_PAR_LOOKUP;
  2325. __hlist_bl_del(&dentry->d_u.d_in_lookup_hash);
  2326. wake_up_all(dentry->d_wait);
  2327. dentry->d_wait = NULL;
  2328. hlist_bl_unlock(b);
  2329. INIT_HLIST_NODE(&dentry->d_u.d_alias);
  2330. INIT_LIST_HEAD(&dentry->d_lru);
  2331. }
  2332. EXPORT_SYMBOL(__d_lookup_done);
  2333. /* inode->i_lock held if inode is non-NULL */
  2334. static inline void __d_add(struct dentry *dentry, struct inode *inode)
  2335. {
  2336. struct inode *dir = NULL;
  2337. unsigned n;
  2338. spin_lock(&dentry->d_lock);
  2339. if (unlikely(d_in_lookup(dentry))) {
  2340. dir = dentry->d_parent->d_inode;
  2341. n = start_dir_add(dir);
  2342. __d_lookup_done(dentry);
  2343. }
  2344. if (inode) {
  2345. unsigned add_flags = d_flags_for_inode(inode);
  2346. hlist_add_head(&dentry->d_u.d_alias, &inode->i_dentry);
  2347. raw_write_seqcount_begin(&dentry->d_seq);
  2348. __d_set_inode_and_type(dentry, inode, add_flags);
  2349. raw_write_seqcount_end(&dentry->d_seq);
  2350. __fsnotify_d_instantiate(dentry);
  2351. }
  2352. _d_rehash(dentry);
  2353. if (dir)
  2354. end_dir_add(dir, n);
  2355. spin_unlock(&dentry->d_lock);
  2356. if (inode)
  2357. spin_unlock(&inode->i_lock);
  2358. }
  2359. /**
  2360. * d_add - add dentry to hash queues
  2361. * @entry: dentry to add
  2362. * @inode: The inode to attach to this dentry
  2363. *
  2364. * This adds the entry to the hash queues and initializes @inode.
  2365. * The entry was actually filled in earlier during d_alloc().
  2366. */
  2367. void d_add(struct dentry *entry, struct inode *inode)
  2368. {
  2369. if (inode) {
  2370. security_d_instantiate(entry, inode);
  2371. spin_lock(&inode->i_lock);
  2372. }
  2373. __d_add(entry, inode);
  2374. }
  2375. EXPORT_SYMBOL(d_add);
  2376. /**
  2377. * d_exact_alias - find and hash an exact unhashed alias
  2378. * @entry: dentry to add
  2379. * @inode: The inode to go with this dentry
  2380. *
  2381. * If an unhashed dentry with the same name/parent and desired
  2382. * inode already exists, hash and return it. Otherwise, return
  2383. * NULL.
  2384. *
  2385. * Parent directory should be locked.
  2386. */
  2387. struct dentry *d_exact_alias(struct dentry *entry, struct inode *inode)
  2388. {
  2389. struct dentry *alias;
  2390. int len = entry->d_name.len;
  2391. const char *name = entry->d_name.name;
  2392. unsigned int hash = entry->d_name.hash;
  2393. spin_lock(&inode->i_lock);
  2394. hlist_for_each_entry(alias, &inode->i_dentry, d_u.d_alias) {
  2395. /*
  2396. * Don't need alias->d_lock here, because aliases with
  2397. * d_parent == entry->d_parent are not subject to name or
  2398. * parent changes, because the parent inode i_mutex is held.
  2399. */
  2400. if (alias->d_name.hash != hash)
  2401. continue;
  2402. if (alias->d_parent != entry->d_parent)
  2403. continue;
  2404. if (alias->d_name.len != len)
  2405. continue;
  2406. if (dentry_cmp(alias, name, len))
  2407. continue;
  2408. spin_lock(&alias->d_lock);
  2409. if (!d_unhashed(alias)) {
  2410. spin_unlock(&alias->d_lock);
  2411. alias = NULL;
  2412. } else {
  2413. __dget_dlock(alias);
  2414. _d_rehash(alias);
  2415. spin_unlock(&alias->d_lock);
  2416. }
  2417. spin_unlock(&inode->i_lock);
  2418. return alias;
  2419. }
  2420. spin_unlock(&inode->i_lock);
  2421. return NULL;
  2422. }
  2423. EXPORT_SYMBOL(d_exact_alias);
  2424. /**
  2425. * dentry_update_name_case - update case insensitive dentry with a new name
  2426. * @dentry: dentry to be updated
  2427. * @name: new name
  2428. *
  2429. * Update a case insensitive dentry with new case of name.
  2430. *
  2431. * dentry must have been returned by d_lookup with name @name. Old and new
  2432. * name lengths must match (ie. no d_compare which allows mismatched name
  2433. * lengths).
  2434. *
  2435. * Parent inode i_mutex must be held over d_lookup and into this call (to
  2436. * keep renames and concurrent inserts, and readdir(2) away).
  2437. */
  2438. void dentry_update_name_case(struct dentry *dentry, struct qstr *name)
  2439. {
  2440. BUG_ON(!inode_is_locked(dentry->d_parent->d_inode));
  2441. BUG_ON(dentry->d_name.len != name->len); /* d_lookup gives this */
  2442. spin_lock(&dentry->d_lock);
  2443. write_seqcount_begin(&dentry->d_seq);
  2444. memcpy((unsigned char *)dentry->d_name.name, name->name, name->len);
  2445. write_seqcount_end(&dentry->d_seq);
  2446. spin_unlock(&dentry->d_lock);
  2447. }
  2448. EXPORT_SYMBOL(dentry_update_name_case);
  2449. static void swap_names(struct dentry *dentry, struct dentry *target)
  2450. {
  2451. if (unlikely(dname_external(target))) {
  2452. if (unlikely(dname_external(dentry))) {
  2453. /*
  2454. * Both external: swap the pointers
  2455. */
  2456. swap(target->d_name.name, dentry->d_name.name);
  2457. } else {
  2458. /*
  2459. * dentry:internal, target:external. Steal target's
  2460. * storage and make target internal.
  2461. */
  2462. memcpy(target->d_iname, dentry->d_name.name,
  2463. dentry->d_name.len + 1);
  2464. dentry->d_name.name = target->d_name.name;
  2465. target->d_name.name = target->d_iname;
  2466. }
  2467. } else {
  2468. if (unlikely(dname_external(dentry))) {
  2469. /*
  2470. * dentry:external, target:internal. Give dentry's
  2471. * storage to target and make dentry internal
  2472. */
  2473. memcpy(dentry->d_iname, target->d_name.name,
  2474. target->d_name.len + 1);
  2475. target->d_name.name = dentry->d_name.name;
  2476. dentry->d_name.name = dentry->d_iname;
  2477. } else {
  2478. /*
  2479. * Both are internal.
  2480. */
  2481. unsigned int i;
  2482. BUILD_BUG_ON(!IS_ALIGNED(DNAME_INLINE_LEN, sizeof(long)));
  2483. kmemcheck_mark_initialized(dentry->d_iname, DNAME_INLINE_LEN);
  2484. kmemcheck_mark_initialized(target->d_iname, DNAME_INLINE_LEN);
  2485. for (i = 0; i < DNAME_INLINE_LEN / sizeof(long); i++) {
  2486. swap(((long *) &dentry->d_iname)[i],
  2487. ((long *) &target->d_iname)[i]);
  2488. }
  2489. }
  2490. }
  2491. swap(dentry->d_name.hash_len, target->d_name.hash_len);
  2492. }
  2493. static void copy_name(struct dentry *dentry, struct dentry *target)
  2494. {
  2495. struct external_name *old_name = NULL;
  2496. if (unlikely(dname_external(dentry)))
  2497. old_name = external_name(dentry);
  2498. if (unlikely(dname_external(target))) {
  2499. atomic_inc(&external_name(target)->u.count);
  2500. dentry->d_name = target->d_name;
  2501. } else {
  2502. memcpy(dentry->d_iname, target->d_name.name,
  2503. target->d_name.len + 1);
  2504. dentry->d_name.name = dentry->d_iname;
  2505. dentry->d_name.hash_len = target->d_name.hash_len;
  2506. }
  2507. if (old_name && likely(atomic_dec_and_test(&old_name->u.count)))
  2508. kfree_rcu(old_name, u.head);
  2509. }
  2510. static void dentry_lock_for_move(struct dentry *dentry, struct dentry *target)
  2511. {
  2512. /*
  2513. * XXXX: do we really need to take target->d_lock?
  2514. */
  2515. if (IS_ROOT(dentry) || dentry->d_parent == target->d_parent)
  2516. spin_lock(&target->d_parent->d_lock);
  2517. else {
  2518. if (d_ancestor(dentry->d_parent, target->d_parent)) {
  2519. spin_lock(&dentry->d_parent->d_lock);
  2520. spin_lock_nested(&target->d_parent->d_lock,
  2521. DENTRY_D_LOCK_NESTED);
  2522. } else {
  2523. spin_lock(&target->d_parent->d_lock);
  2524. spin_lock_nested(&dentry->d_parent->d_lock,
  2525. DENTRY_D_LOCK_NESTED);
  2526. }
  2527. }
  2528. if (target < dentry) {
  2529. spin_lock_nested(&target->d_lock, 2);
  2530. spin_lock_nested(&dentry->d_lock, 3);
  2531. } else {
  2532. spin_lock_nested(&dentry->d_lock, 2);
  2533. spin_lock_nested(&target->d_lock, 3);
  2534. }
  2535. }
  2536. static void dentry_unlock_for_move(struct dentry *dentry, struct dentry *target)
  2537. {
  2538. if (target->d_parent != dentry->d_parent)
  2539. spin_unlock(&dentry->d_parent->d_lock);
  2540. if (target->d_parent != target)
  2541. spin_unlock(&target->d_parent->d_lock);
  2542. spin_unlock(&target->d_lock);
  2543. spin_unlock(&dentry->d_lock);
  2544. }
  2545. /*
  2546. * When switching names, the actual string doesn't strictly have to
  2547. * be preserved in the target - because we're dropping the target
  2548. * anyway. As such, we can just do a simple memcpy() to copy over
  2549. * the new name before we switch, unless we are going to rehash
  2550. * it. Note that if we *do* unhash the target, we are not allowed
  2551. * to rehash it without giving it a new name/hash key - whether
  2552. * we swap or overwrite the names here, resulting name won't match
  2553. * the reality in filesystem; it's only there for d_path() purposes.
  2554. * Note that all of this is happening under rename_lock, so the
  2555. * any hash lookup seeing it in the middle of manipulations will
  2556. * be discarded anyway. So we do not care what happens to the hash
  2557. * key in that case.
  2558. */
  2559. /*
  2560. * __d_move - move a dentry
  2561. * @dentry: entry to move
  2562. * @target: new dentry
  2563. * @exchange: exchange the two dentries
  2564. *
  2565. * Update the dcache to reflect the move of a file name. Negative
  2566. * dcache entries should not be moved in this way. Caller must hold
  2567. * rename_lock, the i_mutex of the source and target directories,
  2568. * and the sb->s_vfs_rename_mutex if they differ. See lock_rename().
  2569. */
  2570. static void __d_move(struct dentry *dentry, struct dentry *target,
  2571. bool exchange)
  2572. {
  2573. struct inode *dir = NULL;
  2574. unsigned n;
  2575. if (!dentry->d_inode)
  2576. printk(KERN_WARNING "VFS: moving negative dcache entry\n");
  2577. BUG_ON(d_ancestor(dentry, target));
  2578. BUG_ON(d_ancestor(target, dentry));
  2579. dentry_lock_for_move(dentry, target);
  2580. if (unlikely(d_in_lookup(target))) {
  2581. dir = target->d_parent->d_inode;
  2582. n = start_dir_add(dir);
  2583. __d_lookup_done(target);
  2584. }
  2585. write_seqcount_begin(&dentry->d_seq);
  2586. write_seqcount_begin_nested(&target->d_seq, DENTRY_D_LOCK_NESTED);
  2587. /* __d_drop does write_seqcount_barrier, but they're OK to nest. */
  2588. /*
  2589. * Move the dentry to the target hash queue. Don't bother checking
  2590. * for the same hash queue because of how unlikely it is.
  2591. */
  2592. __d_drop(dentry);
  2593. __d_rehash(dentry, d_hash(target->d_parent, target->d_name.hash));
  2594. /*
  2595. * Unhash the target (d_delete() is not usable here). If exchanging
  2596. * the two dentries, then rehash onto the other's hash queue.
  2597. */
  2598. __d_drop(target);
  2599. if (exchange) {
  2600. __d_rehash(target,
  2601. d_hash(dentry->d_parent, dentry->d_name.hash));
  2602. }
  2603. /* Switch the names.. */
  2604. if (exchange)
  2605. swap_names(dentry, target);
  2606. else
  2607. copy_name(dentry, target);
  2608. /* ... and switch them in the tree */
  2609. if (IS_ROOT(dentry)) {
  2610. /* splicing a tree */
  2611. dentry->d_flags |= DCACHE_RCUACCESS;
  2612. dentry->d_parent = target->d_parent;
  2613. target->d_parent = target;
  2614. list_del_init(&target->d_child);
  2615. list_move(&dentry->d_child, &dentry->d_parent->d_subdirs);
  2616. } else {
  2617. /* swapping two dentries */
  2618. swap(dentry->d_parent, target->d_parent);
  2619. list_move(&target->d_child, &target->d_parent->d_subdirs);
  2620. list_move(&dentry->d_child, &dentry->d_parent->d_subdirs);
  2621. if (exchange)
  2622. fsnotify_d_move(target);
  2623. fsnotify_d_move(dentry);
  2624. }
  2625. write_seqcount_end(&target->d_seq);
  2626. write_seqcount_end(&dentry->d_seq);
  2627. if (dir)
  2628. end_dir_add(dir, n);
  2629. dentry_unlock_for_move(dentry, target);
  2630. }
  2631. /*
  2632. * d_move - move a dentry
  2633. * @dentry: entry to move
  2634. * @target: new dentry
  2635. *
  2636. * Update the dcache to reflect the move of a file name. Negative
  2637. * dcache entries should not be moved in this way. See the locking
  2638. * requirements for __d_move.
  2639. */
  2640. void d_move(struct dentry *dentry, struct dentry *target)
  2641. {
  2642. write_seqlock(&rename_lock);
  2643. __d_move(dentry, target, false);
  2644. write_sequnlock(&rename_lock);
  2645. }
  2646. EXPORT_SYMBOL(d_move);
  2647. /*
  2648. * d_exchange - exchange two dentries
  2649. * @dentry1: first dentry
  2650. * @dentry2: second dentry
  2651. */
  2652. void d_exchange(struct dentry *dentry1, struct dentry *dentry2)
  2653. {
  2654. write_seqlock(&rename_lock);
  2655. WARN_ON(!dentry1->d_inode);
  2656. WARN_ON(!dentry2->d_inode);
  2657. WARN_ON(IS_ROOT(dentry1));
  2658. WARN_ON(IS_ROOT(dentry2));
  2659. __d_move(dentry1, dentry2, true);
  2660. write_sequnlock(&rename_lock);
  2661. }
  2662. /**
  2663. * d_ancestor - search for an ancestor
  2664. * @p1: ancestor dentry
  2665. * @p2: child dentry
  2666. *
  2667. * Returns the ancestor dentry of p2 which is a child of p1, if p1 is
  2668. * an ancestor of p2, else NULL.
  2669. */
  2670. struct dentry *d_ancestor(struct dentry *p1, struct dentry *p2)
  2671. {
  2672. struct dentry *p;
  2673. for (p = p2; !IS_ROOT(p); p = p->d_parent) {
  2674. if (p->d_parent == p1)
  2675. return p;
  2676. }
  2677. return NULL;
  2678. }
  2679. /*
  2680. * This helper attempts to cope with remotely renamed directories
  2681. *
  2682. * It assumes that the caller is already holding
  2683. * dentry->d_parent->d_inode->i_mutex, and rename_lock
  2684. *
  2685. * Note: If ever the locking in lock_rename() changes, then please
  2686. * remember to update this too...
  2687. */
  2688. static int __d_unalias(struct inode *inode,
  2689. struct dentry *dentry, struct dentry *alias)
  2690. {
  2691. struct mutex *m1 = NULL;
  2692. struct rw_semaphore *m2 = NULL;
  2693. int ret = -ESTALE;
  2694. /* If alias and dentry share a parent, then no extra locks required */
  2695. if (alias->d_parent == dentry->d_parent)
  2696. goto out_unalias;
  2697. /* See lock_rename() */
  2698. if (!mutex_trylock(&dentry->d_sb->s_vfs_rename_mutex))
  2699. goto out_err;
  2700. m1 = &dentry->d_sb->s_vfs_rename_mutex;
  2701. if (!inode_trylock_shared(alias->d_parent->d_inode))
  2702. goto out_err;
  2703. m2 = &alias->d_parent->d_inode->i_rwsem;
  2704. out_unalias:
  2705. __d_move(alias, dentry, false);
  2706. ret = 0;
  2707. out_err:
  2708. if (m2)
  2709. up_read(m2);
  2710. if (m1)
  2711. mutex_unlock(m1);
  2712. return ret;
  2713. }
  2714. /**
  2715. * d_splice_alias - splice a disconnected dentry into the tree if one exists
  2716. * @inode: the inode which may have a disconnected dentry
  2717. * @dentry: a negative dentry which we want to point to the inode.
  2718. *
  2719. * If inode is a directory and has an IS_ROOT alias, then d_move that in
  2720. * place of the given dentry and return it, else simply d_add the inode
  2721. * to the dentry and return NULL.
  2722. *
  2723. * If a non-IS_ROOT directory is found, the filesystem is corrupt, and
  2724. * we should error out: directories can't have multiple aliases.
  2725. *
  2726. * This is needed in the lookup routine of any filesystem that is exportable
  2727. * (via knfsd) so that we can build dcache paths to directories effectively.
  2728. *
  2729. * If a dentry was found and moved, then it is returned. Otherwise NULL
  2730. * is returned. This matches the expected return value of ->lookup.
  2731. *
  2732. * Cluster filesystems may call this function with a negative, hashed dentry.
  2733. * In that case, we know that the inode will be a regular file, and also this
  2734. * will only occur during atomic_open. So we need to check for the dentry
  2735. * being already hashed only in the final case.
  2736. */
  2737. struct dentry *d_splice_alias(struct inode *inode, struct dentry *dentry)
  2738. {
  2739. if (IS_ERR(inode))
  2740. return ERR_CAST(inode);
  2741. BUG_ON(!d_unhashed(dentry));
  2742. if (!inode)
  2743. goto out;
  2744. security_d_instantiate(dentry, inode);
  2745. spin_lock(&inode->i_lock);
  2746. if (S_ISDIR(inode->i_mode)) {
  2747. struct dentry *new = __d_find_any_alias(inode);
  2748. if (unlikely(new)) {
  2749. /* The reference to new ensures it remains an alias */
  2750. spin_unlock(&inode->i_lock);
  2751. write_seqlock(&rename_lock);
  2752. if (unlikely(d_ancestor(new, dentry))) {
  2753. write_sequnlock(&rename_lock);
  2754. dput(new);
  2755. new = ERR_PTR(-ELOOP);
  2756. pr_warn_ratelimited(
  2757. "VFS: Lookup of '%s' in %s %s"
  2758. " would have caused loop\n",
  2759. dentry->d_name.name,
  2760. inode->i_sb->s_type->name,
  2761. inode->i_sb->s_id);
  2762. } else if (!IS_ROOT(new)) {
  2763. int err = __d_unalias(inode, dentry, new);
  2764. write_sequnlock(&rename_lock);
  2765. if (err) {
  2766. dput(new);
  2767. new = ERR_PTR(err);
  2768. }
  2769. } else {
  2770. __d_move(new, dentry, false);
  2771. write_sequnlock(&rename_lock);
  2772. }
  2773. iput(inode);
  2774. return new;
  2775. }
  2776. }
  2777. out:
  2778. __d_add(dentry, inode);
  2779. return NULL;
  2780. }
  2781. EXPORT_SYMBOL(d_splice_alias);
  2782. static int prepend(char **buffer, int *buflen, const char *str, int namelen)
  2783. {
  2784. *buflen -= namelen;
  2785. if (*buflen < 0)
  2786. return -ENAMETOOLONG;
  2787. *buffer -= namelen;
  2788. memcpy(*buffer, str, namelen);
  2789. return 0;
  2790. }
  2791. /**
  2792. * prepend_name - prepend a pathname in front of current buffer pointer
  2793. * @buffer: buffer pointer
  2794. * @buflen: allocated length of the buffer
  2795. * @name: name string and length qstr structure
  2796. *
  2797. * With RCU path tracing, it may race with d_move(). Use ACCESS_ONCE() to
  2798. * make sure that either the old or the new name pointer and length are
  2799. * fetched. However, there may be mismatch between length and pointer.
  2800. * The length cannot be trusted, we need to copy it byte-by-byte until
  2801. * the length is reached or a null byte is found. It also prepends "/" at
  2802. * the beginning of the name. The sequence number check at the caller will
  2803. * retry it again when a d_move() does happen. So any garbage in the buffer
  2804. * due to mismatched pointer and length will be discarded.
  2805. *
  2806. * Data dependency barrier is needed to make sure that we see that terminating
  2807. * NUL. Alpha strikes again, film at 11...
  2808. */
  2809. static int prepend_name(char **buffer, int *buflen, struct qstr *name)
  2810. {
  2811. const char *dname = ACCESS_ONCE(name->name);
  2812. u32 dlen = ACCESS_ONCE(name->len);
  2813. char *p;
  2814. smp_read_barrier_depends();
  2815. *buflen -= dlen + 1;
  2816. if (*buflen < 0)
  2817. return -ENAMETOOLONG;
  2818. p = *buffer -= dlen + 1;
  2819. *p++ = '/';
  2820. while (dlen--) {
  2821. char c = *dname++;
  2822. if (!c)
  2823. break;
  2824. *p++ = c;
  2825. }
  2826. return 0;
  2827. }
  2828. /**
  2829. * prepend_path - Prepend path string to a buffer
  2830. * @path: the dentry/vfsmount to report
  2831. * @root: root vfsmnt/dentry
  2832. * @buffer: pointer to the end of the buffer
  2833. * @buflen: pointer to buffer length
  2834. *
  2835. * The function will first try to write out the pathname without taking any
  2836. * lock other than the RCU read lock to make sure that dentries won't go away.
  2837. * It only checks the sequence number of the global rename_lock as any change
  2838. * in the dentry's d_seq will be preceded by changes in the rename_lock
  2839. * sequence number. If the sequence number had been changed, it will restart
  2840. * the whole pathname back-tracing sequence again by taking the rename_lock.
  2841. * In this case, there is no need to take the RCU read lock as the recursive
  2842. * parent pointer references will keep the dentry chain alive as long as no
  2843. * rename operation is performed.
  2844. */
  2845. static int prepend_path(const struct path *path,
  2846. const struct path *root,
  2847. char **buffer, int *buflen)
  2848. {
  2849. struct dentry *dentry;
  2850. struct vfsmount *vfsmnt;
  2851. struct mount *mnt;
  2852. int error = 0;
  2853. unsigned seq, m_seq = 0;
  2854. char *bptr;
  2855. int blen;
  2856. rcu_read_lock();
  2857. restart_mnt:
  2858. read_seqbegin_or_lock(&mount_lock, &m_seq);
  2859. seq = 0;
  2860. rcu_read_lock();
  2861. restart:
  2862. bptr = *buffer;
  2863. blen = *buflen;
  2864. error = 0;
  2865. dentry = path->dentry;
  2866. vfsmnt = path->mnt;
  2867. mnt = real_mount(vfsmnt);
  2868. read_seqbegin_or_lock(&rename_lock, &seq);
  2869. while (dentry != root->dentry || vfsmnt != root->mnt) {
  2870. struct dentry * parent;
  2871. if (dentry == vfsmnt->mnt_root || IS_ROOT(dentry)) {
  2872. struct mount *parent = ACCESS_ONCE(mnt->mnt_parent);
  2873. /* Escaped? */
  2874. if (dentry != vfsmnt->mnt_root) {
  2875. bptr = *buffer;
  2876. blen = *buflen;
  2877. error = 3;
  2878. break;
  2879. }
  2880. /* Global root? */
  2881. if (mnt != parent) {
  2882. dentry = ACCESS_ONCE(mnt->mnt_mountpoint);
  2883. mnt = parent;
  2884. vfsmnt = &mnt->mnt;
  2885. continue;
  2886. }
  2887. if (!error)
  2888. error = is_mounted(vfsmnt) ? 1 : 2;
  2889. break;
  2890. }
  2891. parent = dentry->d_parent;
  2892. prefetch(parent);
  2893. error = prepend_name(&bptr, &blen, &dentry->d_name);
  2894. if (error)
  2895. break;
  2896. dentry = parent;
  2897. }
  2898. if (!(seq & 1))
  2899. rcu_read_unlock();
  2900. if (need_seqretry(&rename_lock, seq)) {
  2901. seq = 1;
  2902. goto restart;
  2903. }
  2904. done_seqretry(&rename_lock, seq);
  2905. if (!(m_seq & 1))
  2906. rcu_read_unlock();
  2907. if (need_seqretry(&mount_lock, m_seq)) {
  2908. m_seq = 1;
  2909. goto restart_mnt;
  2910. }
  2911. done_seqretry(&mount_lock, m_seq);
  2912. if (error >= 0 && bptr == *buffer) {
  2913. if (--blen < 0)
  2914. error = -ENAMETOOLONG;
  2915. else
  2916. *--bptr = '/';
  2917. }
  2918. *buffer = bptr;
  2919. *buflen = blen;
  2920. return error;
  2921. }
  2922. /**
  2923. * __d_path - return the path of a dentry
  2924. * @path: the dentry/vfsmount to report
  2925. * @root: root vfsmnt/dentry
  2926. * @buf: buffer to return value in
  2927. * @buflen: buffer length
  2928. *
  2929. * Convert a dentry into an ASCII path name.
  2930. *
  2931. * Returns a pointer into the buffer or an error code if the
  2932. * path was too long.
  2933. *
  2934. * "buflen" should be positive.
  2935. *
  2936. * If the path is not reachable from the supplied root, return %NULL.
  2937. */
  2938. char *__d_path(const struct path *path,
  2939. const struct path *root,
  2940. char *buf, int buflen)
  2941. {
  2942. char *res = buf + buflen;
  2943. int error;
  2944. prepend(&res, &buflen, "\0", 1);
  2945. error = prepend_path(path, root, &res, &buflen);
  2946. if (error < 0)
  2947. return ERR_PTR(error);
  2948. if (error > 0)
  2949. return NULL;
  2950. return res;
  2951. }
  2952. char *d_absolute_path(const struct path *path,
  2953. char *buf, int buflen)
  2954. {
  2955. struct path root = {};
  2956. char *res = buf + buflen;
  2957. int error;
  2958. prepend(&res, &buflen, "\0", 1);
  2959. error = prepend_path(path, &root, &res, &buflen);
  2960. if (error > 1)
  2961. error = -EINVAL;
  2962. if (error < 0)
  2963. return ERR_PTR(error);
  2964. return res;
  2965. }
  2966. /*
  2967. * same as __d_path but appends "(deleted)" for unlinked files.
  2968. */
  2969. static int path_with_deleted(const struct path *path,
  2970. const struct path *root,
  2971. char **buf, int *buflen)
  2972. {
  2973. prepend(buf, buflen, "\0", 1);
  2974. if (d_unlinked(path->dentry)) {
  2975. int error = prepend(buf, buflen, " (deleted)", 10);
  2976. if (error)
  2977. return error;
  2978. }
  2979. return prepend_path(path, root, buf, buflen);
  2980. }
  2981. static int prepend_unreachable(char **buffer, int *buflen)
  2982. {
  2983. return prepend(buffer, buflen, "(unreachable)", 13);
  2984. }
  2985. static void get_fs_root_rcu(struct fs_struct *fs, struct path *root)
  2986. {
  2987. unsigned seq;
  2988. do {
  2989. seq = read_seqcount_begin(&fs->seq);
  2990. *root = fs->root;
  2991. } while (read_seqcount_retry(&fs->seq, seq));
  2992. }
  2993. /**
  2994. * d_path - return the path of a dentry
  2995. * @path: path to report
  2996. * @buf: buffer to return value in
  2997. * @buflen: buffer length
  2998. *
  2999. * Convert a dentry into an ASCII path name. If the entry has been deleted
  3000. * the string " (deleted)" is appended. Note that this is ambiguous.
  3001. *
  3002. * Returns a pointer into the buffer or an error code if the path was
  3003. * too long. Note: Callers should use the returned pointer, not the passed
  3004. * in buffer, to use the name! The implementation often starts at an offset
  3005. * into the buffer, and may leave 0 bytes at the start.
  3006. *
  3007. * "buflen" should be positive.
  3008. */
  3009. char *d_path(const struct path *path, char *buf, int buflen)
  3010. {
  3011. char *res = buf + buflen;
  3012. struct path root;
  3013. int error;
  3014. /*
  3015. * We have various synthetic filesystems that never get mounted. On
  3016. * these filesystems dentries are never used for lookup purposes, and
  3017. * thus don't need to be hashed. They also don't need a name until a
  3018. * user wants to identify the object in /proc/pid/fd/. The little hack
  3019. * below allows us to generate a name for these objects on demand:
  3020. *
  3021. * Some pseudo inodes are mountable. When they are mounted
  3022. * path->dentry == path->mnt->mnt_root. In that case don't call d_dname
  3023. * and instead have d_path return the mounted path.
  3024. */
  3025. if (path->dentry->d_op && path->dentry->d_op->d_dname &&
  3026. (!IS_ROOT(path->dentry) || path->dentry != path->mnt->mnt_root))
  3027. return path->dentry->d_op->d_dname(path->dentry, buf, buflen);
  3028. rcu_read_lock();
  3029. get_fs_root_rcu(current->fs, &root);
  3030. error = path_with_deleted(path, &root, &res, &buflen);
  3031. rcu_read_unlock();
  3032. if (error < 0)
  3033. res = ERR_PTR(error);
  3034. return res;
  3035. }
  3036. EXPORT_SYMBOL(d_path);
  3037. /*
  3038. * Helper function for dentry_operations.d_dname() members
  3039. */
  3040. char *dynamic_dname(struct dentry *dentry, char *buffer, int buflen,
  3041. const char *fmt, ...)
  3042. {
  3043. va_list args;
  3044. char temp[64];
  3045. int sz;
  3046. va_start(args, fmt);
  3047. sz = vsnprintf(temp, sizeof(temp), fmt, args) + 1;
  3048. va_end(args);
  3049. if (sz > sizeof(temp) || sz > buflen)
  3050. return ERR_PTR(-ENAMETOOLONG);
  3051. buffer += buflen - sz;
  3052. return memcpy(buffer, temp, sz);
  3053. }
  3054. char *simple_dname(struct dentry *dentry, char *buffer, int buflen)
  3055. {
  3056. char *end = buffer + buflen;
  3057. /* these dentries are never renamed, so d_lock is not needed */
  3058. if (prepend(&end, &buflen, " (deleted)", 11) ||
  3059. prepend(&end, &buflen, dentry->d_name.name, dentry->d_name.len) ||
  3060. prepend(&end, &buflen, "/", 1))
  3061. end = ERR_PTR(-ENAMETOOLONG);
  3062. return end;
  3063. }
  3064. EXPORT_SYMBOL(simple_dname);
  3065. /*
  3066. * Write full pathname from the root of the filesystem into the buffer.
  3067. */
  3068. static char *__dentry_path(struct dentry *d, char *buf, int buflen)
  3069. {
  3070. struct dentry *dentry;
  3071. char *end, *retval;
  3072. int len, seq = 0;
  3073. int error = 0;
  3074. if (buflen < 2)
  3075. goto Elong;
  3076. rcu_read_lock();
  3077. restart:
  3078. dentry = d;
  3079. end = buf + buflen;
  3080. len = buflen;
  3081. prepend(&end, &len, "\0", 1);
  3082. /* Get '/' right */
  3083. retval = end-1;
  3084. *retval = '/';
  3085. read_seqbegin_or_lock(&rename_lock, &seq);
  3086. while (!IS_ROOT(dentry)) {
  3087. struct dentry *parent = dentry->d_parent;
  3088. prefetch(parent);
  3089. error = prepend_name(&end, &len, &dentry->d_name);
  3090. if (error)
  3091. break;
  3092. retval = end;
  3093. dentry = parent;
  3094. }
  3095. if (!(seq & 1))
  3096. rcu_read_unlock();
  3097. if (need_seqretry(&rename_lock, seq)) {
  3098. seq = 1;
  3099. goto restart;
  3100. }
  3101. done_seqretry(&rename_lock, seq);
  3102. if (error)
  3103. goto Elong;
  3104. return retval;
  3105. Elong:
  3106. return ERR_PTR(-ENAMETOOLONG);
  3107. }
  3108. char *dentry_path_raw(struct dentry *dentry, char *buf, int buflen)
  3109. {
  3110. return __dentry_path(dentry, buf, buflen);
  3111. }
  3112. EXPORT_SYMBOL(dentry_path_raw);
  3113. char *dentry_path(struct dentry *dentry, char *buf, int buflen)
  3114. {
  3115. char *p = NULL;
  3116. char *retval;
  3117. if (d_unlinked(dentry)) {
  3118. p = buf + buflen;
  3119. if (prepend(&p, &buflen, "//deleted", 10) != 0)
  3120. goto Elong;
  3121. buflen++;
  3122. }
  3123. retval = __dentry_path(dentry, buf, buflen);
  3124. if (!IS_ERR(retval) && p)
  3125. *p = '/'; /* restore '/' overriden with '\0' */
  3126. return retval;
  3127. Elong:
  3128. return ERR_PTR(-ENAMETOOLONG);
  3129. }
  3130. static void get_fs_root_and_pwd_rcu(struct fs_struct *fs, struct path *root,
  3131. struct path *pwd)
  3132. {
  3133. unsigned seq;
  3134. do {
  3135. seq = read_seqcount_begin(&fs->seq);
  3136. *root = fs->root;
  3137. *pwd = fs->pwd;
  3138. } while (read_seqcount_retry(&fs->seq, seq));
  3139. }
  3140. /*
  3141. * NOTE! The user-level library version returns a
  3142. * character pointer. The kernel system call just
  3143. * returns the length of the buffer filled (which
  3144. * includes the ending '\0' character), or a negative
  3145. * error value. So libc would do something like
  3146. *
  3147. * char *getcwd(char * buf, size_t size)
  3148. * {
  3149. * int retval;
  3150. *
  3151. * retval = sys_getcwd(buf, size);
  3152. * if (retval >= 0)
  3153. * return buf;
  3154. * errno = -retval;
  3155. * return NULL;
  3156. * }
  3157. */
  3158. SYSCALL_DEFINE2(getcwd, char __user *, buf, unsigned long, size)
  3159. {
  3160. int error;
  3161. struct path pwd, root;
  3162. char *page = __getname();
  3163. if (!page)
  3164. return -ENOMEM;
  3165. rcu_read_lock();
  3166. get_fs_root_and_pwd_rcu(current->fs, &root, &pwd);
  3167. error = -ENOENT;
  3168. if (!d_unlinked(pwd.dentry)) {
  3169. unsigned long len;
  3170. char *cwd = page + PATH_MAX;
  3171. int buflen = PATH_MAX;
  3172. prepend(&cwd, &buflen, "\0", 1);
  3173. error = prepend_path(&pwd, &root, &cwd, &buflen);
  3174. rcu_read_unlock();
  3175. if (error < 0)
  3176. goto out;
  3177. /* Unreachable from current root */
  3178. if (error > 0) {
  3179. error = prepend_unreachable(&cwd, &buflen);
  3180. if (error)
  3181. goto out;
  3182. }
  3183. error = -ERANGE;
  3184. len = PATH_MAX + page - cwd;
  3185. if (len <= size) {
  3186. error = len;
  3187. if (copy_to_user(buf, cwd, len))
  3188. error = -EFAULT;
  3189. }
  3190. } else {
  3191. rcu_read_unlock();
  3192. }
  3193. out:
  3194. __putname(page);
  3195. return error;
  3196. }
  3197. /*
  3198. * Test whether new_dentry is a subdirectory of old_dentry.
  3199. *
  3200. * Trivially implemented using the dcache structure
  3201. */
  3202. /**
  3203. * is_subdir - is new dentry a subdirectory of old_dentry
  3204. * @new_dentry: new dentry
  3205. * @old_dentry: old dentry
  3206. *
  3207. * Returns true if new_dentry is a subdirectory of the parent (at any depth).
  3208. * Returns false otherwise.
  3209. * Caller must ensure that "new_dentry" is pinned before calling is_subdir()
  3210. */
  3211. bool is_subdir(struct dentry *new_dentry, struct dentry *old_dentry)
  3212. {
  3213. bool result;
  3214. unsigned seq;
  3215. if (new_dentry == old_dentry)
  3216. return true;
  3217. do {
  3218. /* for restarting inner loop in case of seq retry */
  3219. seq = read_seqbegin(&rename_lock);
  3220. /*
  3221. * Need rcu_readlock to protect against the d_parent trashing
  3222. * due to d_move
  3223. */
  3224. rcu_read_lock();
  3225. if (d_ancestor(old_dentry, new_dentry))
  3226. result = true;
  3227. else
  3228. result = false;
  3229. rcu_read_unlock();
  3230. } while (read_seqretry(&rename_lock, seq));
  3231. return result;
  3232. }
  3233. static enum d_walk_ret d_genocide_kill(void *data, struct dentry *dentry)
  3234. {
  3235. struct dentry *root = data;
  3236. if (dentry != root) {
  3237. if (d_unhashed(dentry) || !dentry->d_inode)
  3238. return D_WALK_SKIP;
  3239. if (!(dentry->d_flags & DCACHE_GENOCIDE)) {
  3240. dentry->d_flags |= DCACHE_GENOCIDE;
  3241. dentry->d_lockref.count--;
  3242. }
  3243. }
  3244. return D_WALK_CONTINUE;
  3245. }
  3246. void d_genocide(struct dentry *parent)
  3247. {
  3248. d_walk(parent, parent, d_genocide_kill, NULL);
  3249. }
  3250. void d_tmpfile(struct dentry *dentry, struct inode *inode)
  3251. {
  3252. inode_dec_link_count(inode);
  3253. BUG_ON(dentry->d_name.name != dentry->d_iname ||
  3254. !hlist_unhashed(&dentry->d_u.d_alias) ||
  3255. !d_unlinked(dentry));
  3256. spin_lock(&dentry->d_parent->d_lock);
  3257. spin_lock_nested(&dentry->d_lock, DENTRY_D_LOCK_NESTED);
  3258. dentry->d_name.len = sprintf(dentry->d_iname, "#%llu",
  3259. (unsigned long long)inode->i_ino);
  3260. spin_unlock(&dentry->d_lock);
  3261. spin_unlock(&dentry->d_parent->d_lock);
  3262. d_instantiate(dentry, inode);
  3263. }
  3264. EXPORT_SYMBOL(d_tmpfile);
  3265. static __initdata unsigned long dhash_entries;
  3266. static int __init set_dhash_entries(char *str)
  3267. {
  3268. if (!str)
  3269. return 0;
  3270. dhash_entries = simple_strtoul(str, &str, 0);
  3271. return 1;
  3272. }
  3273. __setup("dhash_entries=", set_dhash_entries);
  3274. static void __init dcache_init_early(void)
  3275. {
  3276. unsigned int loop;
  3277. /* If hashes are distributed across NUMA nodes, defer
  3278. * hash allocation until vmalloc space is available.
  3279. */
  3280. if (hashdist)
  3281. return;
  3282. dentry_hashtable =
  3283. alloc_large_system_hash("Dentry cache",
  3284. sizeof(struct hlist_bl_head),
  3285. dhash_entries,
  3286. 13,
  3287. HASH_EARLY,
  3288. &d_hash_shift,
  3289. &d_hash_mask,
  3290. 0,
  3291. 0);
  3292. for (loop = 0; loop < (1U << d_hash_shift); loop++)
  3293. INIT_HLIST_BL_HEAD(dentry_hashtable + loop);
  3294. }
  3295. static void __init dcache_init(void)
  3296. {
  3297. unsigned int loop;
  3298. /*
  3299. * A constructor could be added for stable state like the lists,
  3300. * but it is probably not worth it because of the cache nature
  3301. * of the dcache.
  3302. */
  3303. dentry_cache = KMEM_CACHE(dentry,
  3304. SLAB_RECLAIM_ACCOUNT|SLAB_PANIC|SLAB_MEM_SPREAD|SLAB_ACCOUNT);
  3305. /* Hash may have been set up in dcache_init_early */
  3306. if (!hashdist)
  3307. return;
  3308. dentry_hashtable =
  3309. alloc_large_system_hash("Dentry cache",
  3310. sizeof(struct hlist_bl_head),
  3311. dhash_entries,
  3312. 13,
  3313. 0,
  3314. &d_hash_shift,
  3315. &d_hash_mask,
  3316. 0,
  3317. 0);
  3318. for (loop = 0; loop < (1U << d_hash_shift); loop++)
  3319. INIT_HLIST_BL_HEAD(dentry_hashtable + loop);
  3320. }
  3321. /* SLAB cache for __getname() consumers */
  3322. struct kmem_cache *names_cachep __read_mostly;
  3323. EXPORT_SYMBOL(names_cachep);
  3324. EXPORT_SYMBOL(d_genocide);
  3325. void __init vfs_caches_init_early(void)
  3326. {
  3327. dcache_init_early();
  3328. inode_init_early();
  3329. }
  3330. void __init vfs_caches_init(void)
  3331. {
  3332. names_cachep = kmem_cache_create("names_cache", PATH_MAX, 0,
  3333. SLAB_HWCACHE_ALIGN|SLAB_PANIC, NULL);
  3334. dcache_init();
  3335. inode_init();
  3336. files_init();
  3337. files_maxfiles_init();
  3338. mnt_init();
  3339. bdev_cache_init();
  3340. chrdev_init();
  3341. }