dcache.c 95 KB

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