dcache.c 89 KB

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