dcache.c 81 KB

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