dcache.c 75 KB

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