dcache.c 82 KB

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