dcache.c 46 KB

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