dcache.c 80 KB

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