dcache.c 95 KB

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