dcache.c 77 KB

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