dcache.c 88 KB

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