dcache.c 89 KB

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