dcache.c 53 KB

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