dcache.c 44 KB

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