dcache.c 44 KB

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