dcache.c 44 KB

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