dcache.c 85 KB

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