dcache.c 88 KB

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