dcache.c 84 KB

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