1 /* -*- mode: c; c-basic-offset: 8; -*-
2 * vim: noexpandtab sw=8 ts=8 sts=0:
6 * Tracking the up-to-date-ness of a local buffer_head with respect to
9 * Copyright (C) 2002, 2004, 2005 Oracle. All rights reserved.
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.
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.
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.
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
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
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
51 #include <linux/types.h>
52 #include <linux/slab.h>
53 #include <linux/highmem.h>
54 #include <linux/buffer_head.h>
55 #include <linux/rbtree.h>
56 #ifndef CONFIG_OCFS2_COMPAT_JBD
57 # include <linux/jbd2.h>
59 # include <linux/jbd.h>
62 #define MLOG_MASK_PREFIX ML_UPTODATE
64 #include <cluster/masklog.h>
71 struct ocfs2_meta_cache_item {
72 struct rb_node c_node;
76 static struct kmem_cache *ocfs2_uptodate_cachep = NULL;
78 void ocfs2_metadata_cache_init(struct ocfs2_caching_info *ci,
79 spinlock_t *cache_lock,
80 struct mutex *io_mutex)
82 ci->ci_lock = cache_lock;
83 ci->ci_io_mutex = io_mutex;
84 ci->ci_flags |= OCFS2_CACHE_FL_INLINE;
85 ci->ci_num_cached = 0;
88 /* No lock taken here as 'root' is not expected to be visible to other
90 static unsigned int ocfs2_purge_copied_metadata_tree(struct rb_root *root)
92 unsigned int purged = 0;
94 struct ocfs2_meta_cache_item *item;
96 while ((node = rb_last(root)) != NULL) {
97 item = rb_entry(node, struct ocfs2_meta_cache_item, c_node);
99 mlog(0, "Purge item %llu\n",
100 (unsigned long long) item->c_block);
102 rb_erase(&item->c_node, root);
103 kmem_cache_free(ocfs2_uptodate_cachep, item);
110 /* Called from locking and called from ocfs2_clear_inode. Dump the
111 * cache for a given inode.
113 * This function is a few more lines longer than necessary due to some
114 * accounting done here, but I think it's worth tracking down those
115 * bugs sooner -- Mark */
116 void ocfs2_metadata_cache_purge(struct inode *inode)
118 struct ocfs2_inode_info *oi = OCFS2_I(inode);
119 unsigned int tree, to_purge, purged;
120 struct ocfs2_caching_info *ci = &oi->ip_metadata_cache;
121 struct rb_root root = RB_ROOT;
123 spin_lock(ci->ci_lock);
124 tree = !(ci->ci_flags & OCFS2_CACHE_FL_INLINE);
125 to_purge = ci->ci_num_cached;
127 mlog(0, "Purge %u %s items from Inode %llu\n", to_purge,
128 tree ? "array" : "tree", (unsigned long long)oi->ip_blkno);
130 /* If we're a tree, save off the root so that we can safely
131 * initialize the cache. We do the work to free tree members
132 * without the spinlock. */
134 root = ci->ci_cache.ci_tree;
136 ocfs2_metadata_cache_init(ci, ci->ci_lock, ci->ci_io_mutex);
137 spin_unlock(ci->ci_lock);
139 purged = ocfs2_purge_copied_metadata_tree(&root);
140 /* If possible, track the number wiped so that we can more
141 * easily detect counting errors. Unfortunately, this is only
142 * meaningful for trees. */
143 if (tree && purged != to_purge)
144 mlog(ML_ERROR, "Inode %llu, count = %u, purged = %u\n",
145 (unsigned long long)oi->ip_blkno, to_purge, purged);
148 /* Returns the index in the cache array, -1 if not found.
149 * Requires ip_lock. */
150 static int ocfs2_search_cache_array(struct ocfs2_caching_info *ci,
155 for (i = 0; i < ci->ci_num_cached; i++) {
156 if (item == ci->ci_cache.ci_array[i])
163 /* Returns the cache item if found, otherwise NULL.
164 * Requires ip_lock. */
165 static struct ocfs2_meta_cache_item *
166 ocfs2_search_cache_tree(struct ocfs2_caching_info *ci,
169 struct rb_node * n = ci->ci_cache.ci_tree.rb_node;
170 struct ocfs2_meta_cache_item *item = NULL;
173 item = rb_entry(n, struct ocfs2_meta_cache_item, c_node);
175 if (block < item->c_block)
177 else if (block > item->c_block)
186 static int ocfs2_buffer_cached(struct ocfs2_inode_info *oi,
187 struct buffer_head *bh)
190 struct ocfs2_meta_cache_item *item = NULL;
191 struct ocfs2_caching_info *ci = &oi->ip_metadata_cache;
193 spin_lock(ci->ci_lock);
195 mlog(0, "Inode %llu, query block %llu (inline = %u)\n",
196 (unsigned long long)oi->ip_blkno,
197 (unsigned long long) bh->b_blocknr,
198 !!(ci->ci_flags & OCFS2_CACHE_FL_INLINE));
200 if (ci->ci_flags & OCFS2_CACHE_FL_INLINE)
201 index = ocfs2_search_cache_array(&oi->ip_metadata_cache,
204 item = ocfs2_search_cache_tree(&oi->ip_metadata_cache,
207 spin_unlock(ci->ci_lock);
209 mlog(0, "index = %d, item = %p\n", index, item);
211 return (index != -1) || (item != NULL);
214 /* Warning: even if it returns true, this does *not* guarantee that
215 * the block is stored in our inode metadata cache.
217 * This can be called under lock_buffer()
219 int ocfs2_buffer_uptodate(struct inode *inode,
220 struct buffer_head *bh)
222 /* Doesn't matter if the bh is in our cache or not -- if it's
223 * not marked uptodate then we know it can't have correct
225 if (!buffer_uptodate(bh))
228 /* OCFS2 does not allow multiple nodes to be changing the same
229 * block at the same time. */
233 /* Ok, locally the buffer is marked as up to date, now search
234 * our cache to see if we can trust that. */
235 return ocfs2_buffer_cached(OCFS2_I(inode), bh);
239 * Determine whether a buffer is currently out on a read-ahead request.
240 * ci_io_sem should be held to serialize submitters with the logic here.
242 int ocfs2_buffer_read_ahead(struct inode *inode,
243 struct buffer_head *bh)
245 return buffer_locked(bh) && ocfs2_buffer_cached(OCFS2_I(inode), bh);
248 /* Requires ip_lock */
249 static void ocfs2_append_cache_array(struct ocfs2_caching_info *ci,
252 BUG_ON(ci->ci_num_cached >= OCFS2_CACHE_INFO_MAX_ARRAY);
254 mlog(0, "block %llu takes position %u\n", (unsigned long long) block,
257 ci->ci_cache.ci_array[ci->ci_num_cached] = block;
261 /* By now the caller should have checked that the item does *not*
263 * Requires ip_lock. */
264 static void __ocfs2_insert_cache_tree(struct ocfs2_caching_info *ci,
265 struct ocfs2_meta_cache_item *new)
267 sector_t block = new->c_block;
268 struct rb_node *parent = NULL;
269 struct rb_node **p = &ci->ci_cache.ci_tree.rb_node;
270 struct ocfs2_meta_cache_item *tmp;
272 mlog(0, "Insert block %llu num = %u\n", (unsigned long long) block,
278 tmp = rb_entry(parent, struct ocfs2_meta_cache_item, c_node);
280 if (block < tmp->c_block)
282 else if (block > tmp->c_block)
285 /* This should never happen! */
286 mlog(ML_ERROR, "Duplicate block %llu cached!\n",
287 (unsigned long long) block);
292 rb_link_node(&new->c_node, parent, p);
293 rb_insert_color(&new->c_node, &ci->ci_cache.ci_tree);
297 static inline int ocfs2_insert_can_use_array(struct ocfs2_inode_info *oi,
298 struct ocfs2_caching_info *ci)
300 assert_spin_locked(ci->ci_lock);
302 return (ci->ci_flags & OCFS2_CACHE_FL_INLINE) &&
303 (ci->ci_num_cached < OCFS2_CACHE_INFO_MAX_ARRAY);
306 /* tree should be exactly OCFS2_CACHE_INFO_MAX_ARRAY wide. NULL the
307 * pointers in tree after we use them - this allows caller to detect
308 * when to free in case of error. */
309 static void ocfs2_expand_cache(struct ocfs2_inode_info *oi,
310 struct ocfs2_meta_cache_item **tree)
313 struct ocfs2_caching_info *ci = &oi->ip_metadata_cache;
315 mlog_bug_on_msg(ci->ci_num_cached != OCFS2_CACHE_INFO_MAX_ARRAY,
316 "Inode %llu, num cached = %u, should be %u\n",
317 (unsigned long long)oi->ip_blkno, ci->ci_num_cached,
318 OCFS2_CACHE_INFO_MAX_ARRAY);
319 mlog_bug_on_msg(!(ci->ci_flags & OCFS2_CACHE_FL_INLINE),
320 "Inode %llu not marked as inline anymore!\n",
321 (unsigned long long)oi->ip_blkno);
322 assert_spin_locked(ci->ci_lock);
324 /* Be careful to initialize the tree members *first* because
325 * once the ci_tree is used, the array is junk... */
326 for (i = 0; i < OCFS2_CACHE_INFO_MAX_ARRAY; i++)
327 tree[i]->c_block = ci->ci_cache.ci_array[i];
329 ci->ci_flags &= ~OCFS2_CACHE_FL_INLINE;
330 ci->ci_cache.ci_tree = RB_ROOT;
331 /* this will be set again by __ocfs2_insert_cache_tree */
332 ci->ci_num_cached = 0;
334 for (i = 0; i < OCFS2_CACHE_INFO_MAX_ARRAY; i++) {
335 __ocfs2_insert_cache_tree(ci, tree[i]);
339 mlog(0, "Expanded %llu to a tree cache: flags 0x%x, num = %u\n",
340 (unsigned long long)oi->ip_blkno, ci->ci_flags, ci->ci_num_cached);
343 /* Slow path function - memory allocation is necessary. See the
344 * comment above ocfs2_set_buffer_uptodate for more information. */
345 static void __ocfs2_set_buffer_uptodate(struct ocfs2_inode_info *oi,
350 struct ocfs2_caching_info *ci = &oi->ip_metadata_cache;
351 struct ocfs2_meta_cache_item *new = NULL;
352 struct ocfs2_meta_cache_item *tree[OCFS2_CACHE_INFO_MAX_ARRAY] =
355 mlog(0, "Inode %llu, block %llu, expand = %d\n",
356 (unsigned long long)oi->ip_blkno,
357 (unsigned long long)block, expand_tree);
359 new = kmem_cache_alloc(ocfs2_uptodate_cachep, GFP_NOFS);
364 new->c_block = block;
367 /* Do *not* allocate an array here - the removal code
368 * has no way of tracking that. */
369 for (i = 0; i < OCFS2_CACHE_INFO_MAX_ARRAY; i++) {
370 tree[i] = kmem_cache_alloc(ocfs2_uptodate_cachep,
377 /* These are initialized in ocfs2_expand_cache! */
381 spin_lock(ci->ci_lock);
382 if (ocfs2_insert_can_use_array(oi, ci)) {
383 mlog(0, "Someone cleared the tree underneath us\n");
384 /* Ok, items were removed from the cache in between
385 * locks. Detect this and revert back to the fast path */
386 ocfs2_append_cache_array(ci, block);
387 spin_unlock(ci->ci_lock);
392 ocfs2_expand_cache(oi, tree);
394 __ocfs2_insert_cache_tree(ci, new);
395 spin_unlock(ci->ci_lock);
400 kmem_cache_free(ocfs2_uptodate_cachep, new);
402 /* If these were used, then ocfs2_expand_cache re-set them to
405 for (i = 0; i < OCFS2_CACHE_INFO_MAX_ARRAY; i++)
407 kmem_cache_free(ocfs2_uptodate_cachep,
412 /* Item insertion is guarded by ci_io_mutex, so the insertion path takes
413 * advantage of this by not rechecking for a duplicate insert during
414 * the slow case. Additionally, if the cache needs to be bumped up to
415 * a tree, the code will not recheck after acquiring the lock --
416 * multiple paths cannot be expanding to a tree at the same time.
418 * The slow path takes into account that items can be removed
419 * (including the whole tree wiped and reset) when this process it out
420 * allocating memory. In those cases, it reverts back to the fast
423 * Note that this function may actually fail to insert the block if
424 * memory cannot be allocated. This is not fatal however (but may
425 * result in a performance penalty)
427 * Readahead buffers can be passed in here before the I/O request is
430 void ocfs2_set_buffer_uptodate(struct inode *inode,
431 struct buffer_head *bh)
434 struct ocfs2_inode_info *oi = OCFS2_I(inode);
435 struct ocfs2_caching_info *ci = &oi->ip_metadata_cache;
437 /* The block may very well exist in our cache already, so avoid
438 * doing any more work in that case. */
439 if (ocfs2_buffer_cached(oi, bh))
442 mlog(0, "Inode %llu, inserting block %llu\n",
443 (unsigned long long)oi->ip_blkno,
444 (unsigned long long)bh->b_blocknr);
446 /* No need to recheck under spinlock - insertion is guarded by
448 spin_lock(ci->ci_lock);
449 if (ocfs2_insert_can_use_array(oi, ci)) {
450 /* Fast case - it's an array and there's a free
452 ocfs2_append_cache_array(ci, bh->b_blocknr);
453 spin_unlock(ci->ci_lock);
458 if (ci->ci_flags & OCFS2_CACHE_FL_INLINE) {
459 /* We need to bump things up to a tree. */
462 spin_unlock(ci->ci_lock);
464 __ocfs2_set_buffer_uptodate(oi, bh->b_blocknr, expand);
467 /* Called against a newly allocated buffer. Most likely nobody should
468 * be able to read this sort of metadata while it's still being
469 * allocated, but this is careful to take ci_io_mutex anyway. */
470 void ocfs2_set_new_buffer_uptodate(struct inode *inode,
471 struct buffer_head *bh)
473 struct ocfs2_inode_info *oi = OCFS2_I(inode);
474 struct ocfs2_caching_info *ci = &oi->ip_metadata_cache;
476 /* This should definitely *not* exist in our cache */
477 BUG_ON(ocfs2_buffer_cached(oi, bh));
479 set_buffer_uptodate(bh);
481 mutex_lock(ci->ci_io_mutex);
482 ocfs2_set_buffer_uptodate(inode, bh);
483 mutex_unlock(ci->ci_io_mutex);
486 /* Requires ip_lock. */
487 static void ocfs2_remove_metadata_array(struct ocfs2_caching_info *ci,
490 sector_t *array = ci->ci_cache.ci_array;
493 BUG_ON(index < 0 || index >= OCFS2_CACHE_INFO_MAX_ARRAY);
494 BUG_ON(index >= ci->ci_num_cached);
495 BUG_ON(!ci->ci_num_cached);
497 mlog(0, "remove index %d (num_cached = %u\n", index,
502 /* don't need to copy if the array is now empty, or if we
503 * removed at the tail */
504 if (ci->ci_num_cached && index < ci->ci_num_cached) {
505 bytes = sizeof(sector_t) * (ci->ci_num_cached - index);
506 memmove(&array[index], &array[index + 1], bytes);
510 /* Requires ip_lock. */
511 static void ocfs2_remove_metadata_tree(struct ocfs2_caching_info *ci,
512 struct ocfs2_meta_cache_item *item)
514 mlog(0, "remove block %llu from tree\n",
515 (unsigned long long) item->c_block);
517 rb_erase(&item->c_node, &ci->ci_cache.ci_tree);
521 static void ocfs2_remove_block_from_cache(struct inode *inode,
525 struct ocfs2_meta_cache_item *item = NULL;
526 struct ocfs2_inode_info *oi = OCFS2_I(inode);
527 struct ocfs2_caching_info *ci = &oi->ip_metadata_cache;
529 spin_lock(ci->ci_lock);
530 mlog(0, "Inode %llu, remove %llu, items = %u, array = %u\n",
531 (unsigned long long)oi->ip_blkno,
532 (unsigned long long) block, ci->ci_num_cached,
533 ci->ci_flags & OCFS2_CACHE_FL_INLINE);
535 if (ci->ci_flags & OCFS2_CACHE_FL_INLINE) {
536 index = ocfs2_search_cache_array(ci, block);
538 ocfs2_remove_metadata_array(ci, index);
540 item = ocfs2_search_cache_tree(ci, block);
542 ocfs2_remove_metadata_tree(ci, item);
544 spin_unlock(ci->ci_lock);
547 kmem_cache_free(ocfs2_uptodate_cachep, item);
551 * Called when we remove a chunk of metadata from an inode. We don't
552 * bother reverting things to an inlined array in the case of a remove
553 * which moves us back under the limit.
555 void ocfs2_remove_from_cache(struct inode *inode,
556 struct buffer_head *bh)
558 sector_t block = bh->b_blocknr;
560 ocfs2_remove_block_from_cache(inode, block);
563 /* Called when we remove xattr clusters from an inode. */
564 void ocfs2_remove_xattr_clusters_from_cache(struct inode *inode,
568 unsigned int i, b_len = ocfs2_clusters_to_blocks(inode->i_sb, 1) * c_len;
570 for (i = 0; i < b_len; i++, block++)
571 ocfs2_remove_block_from_cache(inode, block);
574 int __init init_ocfs2_uptodate_cache(void)
576 ocfs2_uptodate_cachep = kmem_cache_create("ocfs2_uptodate",
577 sizeof(struct ocfs2_meta_cache_item),
578 0, SLAB_HWCACHE_ALIGN, NULL);
579 if (!ocfs2_uptodate_cachep)
582 mlog(0, "%u inlined cache items per inode.\n",
583 OCFS2_CACHE_INFO_MAX_ARRAY);
588 void exit_ocfs2_uptodate_cache(void)
590 if (ocfs2_uptodate_cachep)
591 kmem_cache_destroy(ocfs2_uptodate_cachep);