dcache.c 49 KB

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