dcache.c 45 KB

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