dcache.c 62 KB

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