dcache.c 88 KB

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