dcache.c 95 KB

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