dcache.c 49 KB

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