dcache.c 79 KB

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