uptodate.c 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544
  1. /* -*- mode: c; c-basic-offset: 8; -*-
  2. * vim: noexpandtab sw=8 ts=8 sts=0:
  3. *
  4. * uptodate.c
  5. *
  6. * Tracking the up-to-date-ness of a local buffer_head with respect to
  7. * the cluster.
  8. *
  9. * Copyright (C) 2002, 2004, 2005 Oracle. All rights reserved.
  10. *
  11. * This program is free software; you can redistribute it and/or
  12. * modify it under the terms of the GNU General Public
  13. * License as published by the Free Software Foundation; either
  14. * version 2 of the License, or (at your option) any later version.
  15. *
  16. * This program is distributed in the hope that it will be useful,
  17. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  18. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  19. * General Public License for more details.
  20. *
  21. * You should have received a copy of the GNU General Public
  22. * License along with this program; if not, write to the
  23. * Free Software Foundation, Inc., 59 Temple Place - Suite 330,
  24. * Boston, MA 021110-1307, USA.
  25. *
  26. * Standard buffer head caching flags (uptodate, etc) are insufficient
  27. * in a clustered environment - a buffer may be marked up to date on
  28. * our local node but could have been modified by another cluster
  29. * member. As a result an additional (and performant) caching scheme
  30. * is required. A further requirement is that we consume as little
  31. * memory as possible - we never pin buffer_head structures in order
  32. * to cache them.
  33. *
  34. * We track the existence of up to date buffers on the inodes which
  35. * are associated with them. Because we don't want to pin
  36. * buffer_heads, this is only a (strong) hint and several other checks
  37. * are made in the I/O path to ensure that we don't use a stale or
  38. * invalid buffer without going to disk:
  39. * - buffer_jbd is used liberally - if a bh is in the journal on
  40. * this node then it *must* be up to date.
  41. * - the standard buffer_uptodate() macro is used to detect buffers
  42. * which may be invalid (even if we have an up to date tracking
  43. * item for them)
  44. *
  45. * For a full understanding of how this code works together, one
  46. * should read the callers in dlmglue.c, the I/O functions in
  47. * buffer_head_io.c and ocfs2_journal_access in journal.c
  48. */
  49. #include <linux/fs.h>
  50. #include <linux/types.h>
  51. #include <linux/slab.h>
  52. #include <linux/highmem.h>
  53. #include <linux/buffer_head.h>
  54. #include <linux/rbtree.h>
  55. #include <linux/jbd.h>
  56. #define MLOG_MASK_PREFIX ML_UPTODATE
  57. #include <cluster/masklog.h>
  58. #include "ocfs2.h"
  59. #include "inode.h"
  60. #include "uptodate.h"
  61. struct ocfs2_meta_cache_item {
  62. struct rb_node c_node;
  63. sector_t c_block;
  64. };
  65. static kmem_cache_t *ocfs2_uptodate_cachep = NULL;
  66. void ocfs2_metadata_cache_init(struct inode *inode)
  67. {
  68. struct ocfs2_inode_info *oi = OCFS2_I(inode);
  69. struct ocfs2_caching_info *ci = &oi->ip_metadata_cache;
  70. oi->ip_flags |= OCFS2_INODE_CACHE_INLINE;
  71. ci->ci_num_cached = 0;
  72. }
  73. /* No lock taken here as 'root' is not expected to be visible to other
  74. * processes. */
  75. static unsigned int ocfs2_purge_copied_metadata_tree(struct rb_root *root)
  76. {
  77. unsigned int purged = 0;
  78. struct rb_node *node;
  79. struct ocfs2_meta_cache_item *item;
  80. while ((node = rb_last(root)) != NULL) {
  81. item = rb_entry(node, struct ocfs2_meta_cache_item, c_node);
  82. mlog(0, "Purge item %llu\n",
  83. (unsigned long long) item->c_block);
  84. rb_erase(&item->c_node, root);
  85. kmem_cache_free(ocfs2_uptodate_cachep, item);
  86. purged++;
  87. }
  88. return purged;
  89. }
  90. /* Called from locking and called from ocfs2_clear_inode. Dump the
  91. * cache for a given inode.
  92. *
  93. * This function is a few more lines longer than necessary due to some
  94. * accounting done here, but I think it's worth tracking down those
  95. * bugs sooner -- Mark */
  96. void ocfs2_metadata_cache_purge(struct inode *inode)
  97. {
  98. struct ocfs2_inode_info *oi = OCFS2_I(inode);
  99. unsigned int tree, to_purge, purged;
  100. struct ocfs2_caching_info *ci = &oi->ip_metadata_cache;
  101. struct rb_root root = RB_ROOT;
  102. spin_lock(&oi->ip_lock);
  103. tree = !(oi->ip_flags & OCFS2_INODE_CACHE_INLINE);
  104. to_purge = ci->ci_num_cached;
  105. mlog(0, "Purge %u %s items from Inode %"MLFu64"\n", to_purge,
  106. tree ? "array" : "tree", oi->ip_blkno);
  107. /* If we're a tree, save off the root so that we can safely
  108. * initialize the cache. We do the work to free tree members
  109. * without the spinlock. */
  110. if (tree)
  111. root = ci->ci_cache.ci_tree;
  112. ocfs2_metadata_cache_init(inode);
  113. spin_unlock(&oi->ip_lock);
  114. purged = ocfs2_purge_copied_metadata_tree(&root);
  115. /* If possible, track the number wiped so that we can more
  116. * easily detect counting errors. Unfortunately, this is only
  117. * meaningful for trees. */
  118. if (tree && purged != to_purge)
  119. mlog(ML_ERROR, "Inode %"MLFu64", count = %u, purged = %u\n",
  120. oi->ip_blkno, to_purge, purged);
  121. }
  122. /* Returns the index in the cache array, -1 if not found.
  123. * Requires ip_lock. */
  124. static int ocfs2_search_cache_array(struct ocfs2_caching_info *ci,
  125. sector_t item)
  126. {
  127. int i;
  128. for (i = 0; i < ci->ci_num_cached; i++) {
  129. if (item == ci->ci_cache.ci_array[i])
  130. return i;
  131. }
  132. return -1;
  133. }
  134. /* Returns the cache item if found, otherwise NULL.
  135. * Requires ip_lock. */
  136. static struct ocfs2_meta_cache_item *
  137. ocfs2_search_cache_tree(struct ocfs2_caching_info *ci,
  138. sector_t block)
  139. {
  140. struct rb_node * n = ci->ci_cache.ci_tree.rb_node;
  141. struct ocfs2_meta_cache_item *item = NULL;
  142. while (n) {
  143. item = rb_entry(n, struct ocfs2_meta_cache_item, c_node);
  144. if (block < item->c_block)
  145. n = n->rb_left;
  146. else if (block > item->c_block)
  147. n = n->rb_right;
  148. else
  149. return item;
  150. }
  151. return NULL;
  152. }
  153. static int ocfs2_buffer_cached(struct ocfs2_inode_info *oi,
  154. struct buffer_head *bh)
  155. {
  156. int index = -1;
  157. struct ocfs2_meta_cache_item *item = NULL;
  158. spin_lock(&oi->ip_lock);
  159. mlog(0, "Inode %"MLFu64", query block %llu (inline = %u)\n",
  160. oi->ip_blkno, (unsigned long long) bh->b_blocknr,
  161. !!(oi->ip_flags & OCFS2_INODE_CACHE_INLINE));
  162. if (oi->ip_flags & OCFS2_INODE_CACHE_INLINE)
  163. index = ocfs2_search_cache_array(&oi->ip_metadata_cache,
  164. bh->b_blocknr);
  165. else
  166. item = ocfs2_search_cache_tree(&oi->ip_metadata_cache,
  167. bh->b_blocknr);
  168. spin_unlock(&oi->ip_lock);
  169. mlog(0, "index = %d, item = %p\n", index, item);
  170. return (index != -1) || (item != NULL);
  171. }
  172. /* Warning: even if it returns true, this does *not* guarantee that
  173. * the block is stored in our inode metadata cache. */
  174. int ocfs2_buffer_uptodate(struct inode *inode,
  175. struct buffer_head *bh)
  176. {
  177. /* Doesn't matter if the bh is in our cache or not -- if it's
  178. * not marked uptodate then we know it can't have correct
  179. * data. */
  180. if (!buffer_uptodate(bh))
  181. return 0;
  182. /* OCFS2 does not allow multiple nodes to be changing the same
  183. * block at the same time. */
  184. if (buffer_jbd(bh))
  185. return 1;
  186. /* Ok, locally the buffer is marked as up to date, now search
  187. * our cache to see if we can trust that. */
  188. return ocfs2_buffer_cached(OCFS2_I(inode), bh);
  189. }
  190. /* Requires ip_lock */
  191. static void ocfs2_append_cache_array(struct ocfs2_caching_info *ci,
  192. sector_t block)
  193. {
  194. BUG_ON(ci->ci_num_cached >= OCFS2_INODE_MAX_CACHE_ARRAY);
  195. mlog(0, "block %llu takes position %u\n", (unsigned long long) block,
  196. ci->ci_num_cached);
  197. ci->ci_cache.ci_array[ci->ci_num_cached] = block;
  198. ci->ci_num_cached++;
  199. }
  200. /* By now the caller should have checked that the item does *not*
  201. * exist in the tree.
  202. * Requires ip_lock. */
  203. static void __ocfs2_insert_cache_tree(struct ocfs2_caching_info *ci,
  204. struct ocfs2_meta_cache_item *new)
  205. {
  206. sector_t block = new->c_block;
  207. struct rb_node *parent = NULL;
  208. struct rb_node **p = &ci->ci_cache.ci_tree.rb_node;
  209. struct ocfs2_meta_cache_item *tmp;
  210. mlog(0, "Insert block %llu num = %u\n", (unsigned long long) block,
  211. ci->ci_num_cached);
  212. while(*p) {
  213. parent = *p;
  214. tmp = rb_entry(parent, struct ocfs2_meta_cache_item, c_node);
  215. if (block < tmp->c_block)
  216. p = &(*p)->rb_left;
  217. else if (block > tmp->c_block)
  218. p = &(*p)->rb_right;
  219. else {
  220. /* This should never happen! */
  221. mlog(ML_ERROR, "Duplicate block %llu cached!\n",
  222. (unsigned long long) block);
  223. BUG();
  224. }
  225. }
  226. rb_link_node(&new->c_node, parent, p);
  227. rb_insert_color(&new->c_node, &ci->ci_cache.ci_tree);
  228. ci->ci_num_cached++;
  229. }
  230. static inline int ocfs2_insert_can_use_array(struct ocfs2_inode_info *oi,
  231. struct ocfs2_caching_info *ci)
  232. {
  233. assert_spin_locked(&oi->ip_lock);
  234. return (oi->ip_flags & OCFS2_INODE_CACHE_INLINE) &&
  235. (ci->ci_num_cached < OCFS2_INODE_MAX_CACHE_ARRAY);
  236. }
  237. /* tree should be exactly OCFS2_INODE_MAX_CACHE_ARRAY wide. NULL the
  238. * pointers in tree after we use them - this allows caller to detect
  239. * when to free in case of error. */
  240. static void ocfs2_expand_cache(struct ocfs2_inode_info *oi,
  241. struct ocfs2_meta_cache_item **tree)
  242. {
  243. int i;
  244. struct ocfs2_caching_info *ci = &oi->ip_metadata_cache;
  245. mlog_bug_on_msg(ci->ci_num_cached != OCFS2_INODE_MAX_CACHE_ARRAY,
  246. "Inode %"MLFu64", num cached = %u, should be %u\n",
  247. oi->ip_blkno, ci->ci_num_cached,
  248. OCFS2_INODE_MAX_CACHE_ARRAY);
  249. mlog_bug_on_msg(!(oi->ip_flags & OCFS2_INODE_CACHE_INLINE),
  250. "Inode %"MLFu64" not marked as inline anymore!\n",
  251. oi->ip_blkno);
  252. assert_spin_locked(&oi->ip_lock);
  253. /* Be careful to initialize the tree members *first* because
  254. * once the ci_tree is used, the array is junk... */
  255. for(i = 0; i < OCFS2_INODE_MAX_CACHE_ARRAY; i++)
  256. tree[i]->c_block = ci->ci_cache.ci_array[i];
  257. oi->ip_flags &= ~OCFS2_INODE_CACHE_INLINE;
  258. ci->ci_cache.ci_tree = RB_ROOT;
  259. /* this will be set again by __ocfs2_insert_cache_tree */
  260. ci->ci_num_cached = 0;
  261. for(i = 0; i < OCFS2_INODE_MAX_CACHE_ARRAY; i++) {
  262. __ocfs2_insert_cache_tree(ci, tree[i]);
  263. tree[i] = NULL;
  264. }
  265. mlog(0, "Expanded %"MLFu64" to a tree cache: flags 0x%x, num = %u\n",
  266. oi->ip_blkno, oi->ip_flags, ci->ci_num_cached);
  267. }
  268. /* Slow path function - memory allocation is necessary. See the
  269. * comment above ocfs2_set_buffer_uptodate for more information. */
  270. static void __ocfs2_set_buffer_uptodate(struct ocfs2_inode_info *oi,
  271. sector_t block,
  272. int expand_tree)
  273. {
  274. int i;
  275. struct ocfs2_caching_info *ci = &oi->ip_metadata_cache;
  276. struct ocfs2_meta_cache_item *new = NULL;
  277. struct ocfs2_meta_cache_item *tree[OCFS2_INODE_MAX_CACHE_ARRAY] =
  278. { NULL, };
  279. mlog(0, "Inode %"MLFu64", block %llu, expand = %d\n",
  280. oi->ip_blkno, (unsigned long long) block, expand_tree);
  281. new = kmem_cache_alloc(ocfs2_uptodate_cachep, GFP_KERNEL);
  282. if (!new) {
  283. mlog_errno(-ENOMEM);
  284. return;
  285. }
  286. new->c_block = block;
  287. if (expand_tree) {
  288. /* Do *not* allocate an array here - the removal code
  289. * has no way of tracking that. */
  290. for(i = 0; i < OCFS2_INODE_MAX_CACHE_ARRAY; i++) {
  291. tree[i] = kmem_cache_alloc(ocfs2_uptodate_cachep,
  292. GFP_KERNEL);
  293. if (!tree[i]) {
  294. mlog_errno(-ENOMEM);
  295. goto out_free;
  296. }
  297. /* These are initialized in ocfs2_expand_cache! */
  298. }
  299. }
  300. spin_lock(&oi->ip_lock);
  301. if (ocfs2_insert_can_use_array(oi, ci)) {
  302. mlog(0, "Someone cleared the tree underneath us\n");
  303. /* Ok, items were removed from the cache in between
  304. * locks. Detect this and revert back to the fast path */
  305. ocfs2_append_cache_array(ci, block);
  306. spin_unlock(&oi->ip_lock);
  307. goto out_free;
  308. }
  309. if (expand_tree)
  310. ocfs2_expand_cache(oi, tree);
  311. __ocfs2_insert_cache_tree(ci, new);
  312. spin_unlock(&oi->ip_lock);
  313. new = NULL;
  314. out_free:
  315. if (new)
  316. kmem_cache_free(ocfs2_uptodate_cachep, new);
  317. /* If these were used, then ocfs2_expand_cache re-set them to
  318. * NULL for us. */
  319. if (tree[0]) {
  320. for(i = 0; i < OCFS2_INODE_MAX_CACHE_ARRAY; i++)
  321. if (tree[i])
  322. kmem_cache_free(ocfs2_uptodate_cachep,
  323. tree[i]);
  324. }
  325. }
  326. /* Item insertion is guarded by ip_io_mutex, so the insertion path takes
  327. * advantage of this by not rechecking for a duplicate insert during
  328. * the slow case. Additionally, if the cache needs to be bumped up to
  329. * a tree, the code will not recheck after acquiring the lock --
  330. * multiple paths cannot be expanding to a tree at the same time.
  331. *
  332. * The slow path takes into account that items can be removed
  333. * (including the whole tree wiped and reset) when this process it out
  334. * allocating memory. In those cases, it reverts back to the fast
  335. * path.
  336. *
  337. * Note that this function may actually fail to insert the block if
  338. * memory cannot be allocated. This is not fatal however (but may
  339. * result in a performance penalty) */
  340. void ocfs2_set_buffer_uptodate(struct inode *inode,
  341. struct buffer_head *bh)
  342. {
  343. int expand;
  344. struct ocfs2_inode_info *oi = OCFS2_I(inode);
  345. struct ocfs2_caching_info *ci = &oi->ip_metadata_cache;
  346. /* The block may very well exist in our cache already, so avoid
  347. * doing any more work in that case. */
  348. if (ocfs2_buffer_cached(oi, bh))
  349. return;
  350. mlog(0, "Inode %"MLFu64", inserting block %llu\n", oi->ip_blkno,
  351. (unsigned long long) bh->b_blocknr);
  352. /* No need to recheck under spinlock - insertion is guarded by
  353. * ip_io_mutex */
  354. spin_lock(&oi->ip_lock);
  355. if (ocfs2_insert_can_use_array(oi, ci)) {
  356. /* Fast case - it's an array and there's a free
  357. * spot. */
  358. ocfs2_append_cache_array(ci, bh->b_blocknr);
  359. spin_unlock(&oi->ip_lock);
  360. return;
  361. }
  362. expand = 0;
  363. if (oi->ip_flags & OCFS2_INODE_CACHE_INLINE) {
  364. /* We need to bump things up to a tree. */
  365. expand = 1;
  366. }
  367. spin_unlock(&oi->ip_lock);
  368. __ocfs2_set_buffer_uptodate(oi, bh->b_blocknr, expand);
  369. }
  370. /* Called against a newly allocated buffer. Most likely nobody should
  371. * be able to read this sort of metadata while it's still being
  372. * allocated, but this is careful to take ip_io_mutex anyway. */
  373. void ocfs2_set_new_buffer_uptodate(struct inode *inode,
  374. struct buffer_head *bh)
  375. {
  376. struct ocfs2_inode_info *oi = OCFS2_I(inode);
  377. /* This should definitely *not* exist in our cache */
  378. BUG_ON(ocfs2_buffer_cached(oi, bh));
  379. set_buffer_uptodate(bh);
  380. mutex_lock(&oi->ip_io_mutex);
  381. ocfs2_set_buffer_uptodate(inode, bh);
  382. mutex_unlock(&oi->ip_io_mutex);
  383. }
  384. /* Requires ip_lock. */
  385. static void ocfs2_remove_metadata_array(struct ocfs2_caching_info *ci,
  386. int index)
  387. {
  388. sector_t *array = ci->ci_cache.ci_array;
  389. int bytes;
  390. BUG_ON(index < 0 || index >= OCFS2_INODE_MAX_CACHE_ARRAY);
  391. BUG_ON(index >= ci->ci_num_cached);
  392. BUG_ON(!ci->ci_num_cached);
  393. mlog(0, "remove index %d (num_cached = %u\n", index,
  394. ci->ci_num_cached);
  395. ci->ci_num_cached--;
  396. /* don't need to copy if the array is now empty, or if we
  397. * removed at the tail */
  398. if (ci->ci_num_cached && index < ci->ci_num_cached) {
  399. bytes = sizeof(sector_t) * (ci->ci_num_cached - index);
  400. memmove(&array[index], &array[index + 1], bytes);
  401. }
  402. }
  403. /* Requires ip_lock. */
  404. static void ocfs2_remove_metadata_tree(struct ocfs2_caching_info *ci,
  405. struct ocfs2_meta_cache_item *item)
  406. {
  407. mlog(0, "remove block %llu from tree\n",
  408. (unsigned long long) item->c_block);
  409. rb_erase(&item->c_node, &ci->ci_cache.ci_tree);
  410. ci->ci_num_cached--;
  411. }
  412. /* Called when we remove a chunk of metadata from an inode. We don't
  413. * bother reverting things to an inlined array in the case of a remove
  414. * which moves us back under the limit. */
  415. void ocfs2_remove_from_cache(struct inode *inode,
  416. struct buffer_head *bh)
  417. {
  418. int index;
  419. sector_t block = bh->b_blocknr;
  420. struct ocfs2_meta_cache_item *item = NULL;
  421. struct ocfs2_inode_info *oi = OCFS2_I(inode);
  422. struct ocfs2_caching_info *ci = &oi->ip_metadata_cache;
  423. spin_lock(&oi->ip_lock);
  424. mlog(0, "Inode %"MLFu64", remove %llu, items = %u, array = %u\n",
  425. oi->ip_blkno, (unsigned long long) block, ci->ci_num_cached,
  426. oi->ip_flags & OCFS2_INODE_CACHE_INLINE);
  427. if (oi->ip_flags & OCFS2_INODE_CACHE_INLINE) {
  428. index = ocfs2_search_cache_array(ci, block);
  429. if (index != -1)
  430. ocfs2_remove_metadata_array(ci, index);
  431. } else {
  432. item = ocfs2_search_cache_tree(ci, block);
  433. if (item)
  434. ocfs2_remove_metadata_tree(ci, item);
  435. }
  436. spin_unlock(&oi->ip_lock);
  437. if (item)
  438. kmem_cache_free(ocfs2_uptodate_cachep, item);
  439. }
  440. int __init init_ocfs2_uptodate_cache(void)
  441. {
  442. ocfs2_uptodate_cachep = kmem_cache_create("ocfs2_uptodate",
  443. sizeof(struct ocfs2_meta_cache_item),
  444. 0, SLAB_HWCACHE_ALIGN, NULL, NULL);
  445. if (!ocfs2_uptodate_cachep)
  446. return -ENOMEM;
  447. mlog(0, "%u inlined cache items per inode.\n",
  448. OCFS2_INODE_MAX_CACHE_ARRAY);
  449. return 0;
  450. }
  451. void exit_ocfs2_uptodate_cache(void)
  452. {
  453. if (ocfs2_uptodate_cachep)
  454. kmem_cache_destroy(ocfs2_uptodate_cachep);
  455. }