dcache.c 94 KB

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