dcache.c 77 KB

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