dcache.c 88 KB

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