dcache.c 76 KB

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