dcache.c 88 KB

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