dcache.c 46 KB

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