dcache.c 79 KB

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