dcache.c 88 KB

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