dcache.c 89 KB

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