dcache.c 94 KB

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