dcache.c 45 KB

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