1 // SPDX-License-Identifier: GPL-2.0-or-later
5 * Tracking the up-to-date-ness of a local buffer_head with respect to
8 * Copyright (C) 2002, 2004, 2005 Oracle. All rights reserved.
10 * Standard buffer head caching flags (uptodate, etc) are insufficient
11 * in a clustered environment - a buffer may be marked up to date on
12 * our local node but could have been modified by another cluster
13 * member. As a result an additional (and performant) caching scheme
14 * is required. A further requirement is that we consume as little
15 * memory as possible - we never pin buffer_head structures in order
18 * We track the existence of up to date buffers on the inodes which
19 * are associated with them. Because we don't want to pin
20 * buffer_heads, this is only a (strong) hint and several other checks
21 * are made in the I/O path to ensure that we don't use a stale or
22 * invalid buffer without going to disk:
23 * - buffer_jbd is used liberally - if a bh is in the journal on
24 * this node then it *must* be up to date.
25 * - the standard buffer_uptodate() macro is used to detect buffers
26 * which may be invalid (even if we have an up to date tracking
29 * For a full understanding of how this code works together, one
30 * should read the callers in dlmglue.c, the I/O functions in
31 * buffer_head_io.c and ocfs2_journal_access in journal.c
35 #include <linux/types.h>
36 #include <linux/slab.h>
37 #include <linux/highmem.h>
38 #include <linux/buffer_head.h>
39 #include <linux/rbtree.h>
41 #include <cluster/masklog.h>
47 #include "ocfs2_trace.h"
49 struct ocfs2_meta_cache_item {
50 struct rb_node c_node;
54 static struct kmem_cache *ocfs2_uptodate_cachep;
56 u64 ocfs2_metadata_cache_owner(struct ocfs2_caching_info *ci)
58 BUG_ON(!ci || !ci->ci_ops);
60 return ci->ci_ops->co_owner(ci);
63 struct super_block *ocfs2_metadata_cache_get_super(struct ocfs2_caching_info *ci)
65 BUG_ON(!ci || !ci->ci_ops);
67 return ci->ci_ops->co_get_super(ci);
70 static void ocfs2_metadata_cache_lock(struct ocfs2_caching_info *ci)
72 BUG_ON(!ci || !ci->ci_ops);
74 ci->ci_ops->co_cache_lock(ci);
77 static void ocfs2_metadata_cache_unlock(struct ocfs2_caching_info *ci)
79 BUG_ON(!ci || !ci->ci_ops);
81 ci->ci_ops->co_cache_unlock(ci);
84 void ocfs2_metadata_cache_io_lock(struct ocfs2_caching_info *ci)
86 BUG_ON(!ci || !ci->ci_ops);
88 ci->ci_ops->co_io_lock(ci);
91 void ocfs2_metadata_cache_io_unlock(struct ocfs2_caching_info *ci)
93 BUG_ON(!ci || !ci->ci_ops);
95 ci->ci_ops->co_io_unlock(ci);
99 static void ocfs2_metadata_cache_reset(struct ocfs2_caching_info *ci,
102 ci->ci_flags |= OCFS2_CACHE_FL_INLINE;
103 ci->ci_num_cached = 0;
106 ci->ci_created_trans = 0;
107 ci->ci_last_trans = 0;
111 void ocfs2_metadata_cache_init(struct ocfs2_caching_info *ci,
112 const struct ocfs2_caching_operations *ops)
117 ocfs2_metadata_cache_reset(ci, 1);
120 void ocfs2_metadata_cache_exit(struct ocfs2_caching_info *ci)
122 ocfs2_metadata_cache_purge(ci);
123 ocfs2_metadata_cache_reset(ci, 1);
127 /* No lock taken here as 'root' is not expected to be visible to other
129 static unsigned int ocfs2_purge_copied_metadata_tree(struct rb_root *root)
131 unsigned int purged = 0;
132 struct rb_node *node;
133 struct ocfs2_meta_cache_item *item;
135 while ((node = rb_last(root)) != NULL) {
136 item = rb_entry(node, struct ocfs2_meta_cache_item, c_node);
138 trace_ocfs2_purge_copied_metadata_tree(
139 (unsigned long long) item->c_block);
141 rb_erase(&item->c_node, root);
142 kmem_cache_free(ocfs2_uptodate_cachep, item);
149 /* Called from locking and called from ocfs2_clear_inode. Dump the
150 * cache for a given inode.
152 * This function is a few more lines longer than necessary due to some
153 * accounting done here, but I think it's worth tracking down those
154 * bugs sooner -- Mark */
155 void ocfs2_metadata_cache_purge(struct ocfs2_caching_info *ci)
157 unsigned int tree, to_purge, purged;
158 struct rb_root root = RB_ROOT;
160 BUG_ON(!ci || !ci->ci_ops);
162 ocfs2_metadata_cache_lock(ci);
163 tree = !(ci->ci_flags & OCFS2_CACHE_FL_INLINE);
164 to_purge = ci->ci_num_cached;
166 trace_ocfs2_metadata_cache_purge(
167 (unsigned long long)ocfs2_metadata_cache_owner(ci),
170 /* If we're a tree, save off the root so that we can safely
171 * initialize the cache. We do the work to free tree members
172 * without the spinlock. */
174 root = ci->ci_cache.ci_tree;
176 ocfs2_metadata_cache_reset(ci, 0);
177 ocfs2_metadata_cache_unlock(ci);
179 purged = ocfs2_purge_copied_metadata_tree(&root);
180 /* If possible, track the number wiped so that we can more
181 * easily detect counting errors. Unfortunately, this is only
182 * meaningful for trees. */
183 if (tree && purged != to_purge)
184 mlog(ML_ERROR, "Owner %llu, count = %u, purged = %u\n",
185 (unsigned long long)ocfs2_metadata_cache_owner(ci),
189 /* Returns the index in the cache array, -1 if not found.
190 * Requires ip_lock. */
191 static int ocfs2_search_cache_array(struct ocfs2_caching_info *ci,
196 for (i = 0; i < ci->ci_num_cached; i++) {
197 if (item == ci->ci_cache.ci_array[i])
204 /* Returns the cache item if found, otherwise NULL.
205 * Requires ip_lock. */
206 static struct ocfs2_meta_cache_item *
207 ocfs2_search_cache_tree(struct ocfs2_caching_info *ci,
210 struct rb_node * n = ci->ci_cache.ci_tree.rb_node;
211 struct ocfs2_meta_cache_item *item = NULL;
214 item = rb_entry(n, struct ocfs2_meta_cache_item, c_node);
216 if (block < item->c_block)
218 else if (block > item->c_block)
227 static int ocfs2_buffer_cached(struct ocfs2_caching_info *ci,
228 struct buffer_head *bh)
231 struct ocfs2_meta_cache_item *item = NULL;
233 ocfs2_metadata_cache_lock(ci);
235 trace_ocfs2_buffer_cached_begin(
236 (unsigned long long)ocfs2_metadata_cache_owner(ci),
237 (unsigned long long) bh->b_blocknr,
238 !!(ci->ci_flags & OCFS2_CACHE_FL_INLINE));
240 if (ci->ci_flags & OCFS2_CACHE_FL_INLINE)
241 index = ocfs2_search_cache_array(ci, bh->b_blocknr);
243 item = ocfs2_search_cache_tree(ci, bh->b_blocknr);
245 ocfs2_metadata_cache_unlock(ci);
247 trace_ocfs2_buffer_cached_end(index, item);
249 return (index != -1) || (item != NULL);
252 /* Warning: even if it returns true, this does *not* guarantee that
253 * the block is stored in our inode metadata cache.
255 * This can be called under lock_buffer()
257 int ocfs2_buffer_uptodate(struct ocfs2_caching_info *ci,
258 struct buffer_head *bh)
260 /* Doesn't matter if the bh is in our cache or not -- if it's
261 * not marked uptodate then we know it can't have correct
263 if (!buffer_uptodate(bh))
266 /* OCFS2 does not allow multiple nodes to be changing the same
267 * block at the same time. */
271 /* Ok, locally the buffer is marked as up to date, now search
272 * our cache to see if we can trust that. */
273 return ocfs2_buffer_cached(ci, bh);
277 * Determine whether a buffer is currently out on a read-ahead request.
278 * ci_io_sem should be held to serialize submitters with the logic here.
280 int ocfs2_buffer_read_ahead(struct ocfs2_caching_info *ci,
281 struct buffer_head *bh)
283 return buffer_locked(bh) && ocfs2_buffer_cached(ci, bh);
286 /* Requires ip_lock */
287 static void ocfs2_append_cache_array(struct ocfs2_caching_info *ci,
290 BUG_ON(ci->ci_num_cached >= OCFS2_CACHE_INFO_MAX_ARRAY);
292 trace_ocfs2_append_cache_array(
293 (unsigned long long)ocfs2_metadata_cache_owner(ci),
294 (unsigned long long)block, ci->ci_num_cached);
296 ci->ci_cache.ci_array[ci->ci_num_cached] = block;
300 /* By now the caller should have checked that the item does *not*
302 * Requires ip_lock. */
303 static void __ocfs2_insert_cache_tree(struct ocfs2_caching_info *ci,
304 struct ocfs2_meta_cache_item *new)
306 sector_t block = new->c_block;
307 struct rb_node *parent = NULL;
308 struct rb_node **p = &ci->ci_cache.ci_tree.rb_node;
309 struct ocfs2_meta_cache_item *tmp;
311 trace_ocfs2_insert_cache_tree(
312 (unsigned long long)ocfs2_metadata_cache_owner(ci),
313 (unsigned long long)block, ci->ci_num_cached);
318 tmp = rb_entry(parent, struct ocfs2_meta_cache_item, c_node);
320 if (block < tmp->c_block)
322 else if (block > tmp->c_block)
325 /* This should never happen! */
326 mlog(ML_ERROR, "Duplicate block %llu cached!\n",
327 (unsigned long long) block);
332 rb_link_node(&new->c_node, parent, p);
333 rb_insert_color(&new->c_node, &ci->ci_cache.ci_tree);
337 /* co_cache_lock() must be held */
338 static inline int ocfs2_insert_can_use_array(struct ocfs2_caching_info *ci)
340 return (ci->ci_flags & OCFS2_CACHE_FL_INLINE) &&
341 (ci->ci_num_cached < OCFS2_CACHE_INFO_MAX_ARRAY);
344 /* tree should be exactly OCFS2_CACHE_INFO_MAX_ARRAY wide. NULL the
345 * pointers in tree after we use them - this allows caller to detect
346 * when to free in case of error.
348 * The co_cache_lock() must be held. */
349 static void ocfs2_expand_cache(struct ocfs2_caching_info *ci,
350 struct ocfs2_meta_cache_item **tree)
354 mlog_bug_on_msg(ci->ci_num_cached != OCFS2_CACHE_INFO_MAX_ARRAY,
355 "Owner %llu, num cached = %u, should be %u\n",
356 (unsigned long long)ocfs2_metadata_cache_owner(ci),
357 ci->ci_num_cached, OCFS2_CACHE_INFO_MAX_ARRAY);
358 mlog_bug_on_msg(!(ci->ci_flags & OCFS2_CACHE_FL_INLINE),
359 "Owner %llu not marked as inline anymore!\n",
360 (unsigned long long)ocfs2_metadata_cache_owner(ci));
362 /* Be careful to initialize the tree members *first* because
363 * once the ci_tree is used, the array is junk... */
364 for (i = 0; i < OCFS2_CACHE_INFO_MAX_ARRAY; i++)
365 tree[i]->c_block = ci->ci_cache.ci_array[i];
367 ci->ci_flags &= ~OCFS2_CACHE_FL_INLINE;
368 ci->ci_cache.ci_tree = RB_ROOT;
369 /* this will be set again by __ocfs2_insert_cache_tree */
370 ci->ci_num_cached = 0;
372 for (i = 0; i < OCFS2_CACHE_INFO_MAX_ARRAY; i++) {
373 __ocfs2_insert_cache_tree(ci, tree[i]);
377 trace_ocfs2_expand_cache(
378 (unsigned long long)ocfs2_metadata_cache_owner(ci),
379 ci->ci_flags, ci->ci_num_cached);
382 /* Slow path function - memory allocation is necessary. See the
383 * comment above ocfs2_set_buffer_uptodate for more information. */
384 static void __ocfs2_set_buffer_uptodate(struct ocfs2_caching_info *ci,
389 struct ocfs2_meta_cache_item *new = NULL;
390 struct ocfs2_meta_cache_item *tree[OCFS2_CACHE_INFO_MAX_ARRAY] =
393 trace_ocfs2_set_buffer_uptodate(
394 (unsigned long long)ocfs2_metadata_cache_owner(ci),
395 (unsigned long long)block, expand_tree);
397 new = kmem_cache_alloc(ocfs2_uptodate_cachep, GFP_NOFS);
402 new->c_block = block;
405 /* Do *not* allocate an array here - the removal code
406 * has no way of tracking that. */
407 for (i = 0; i < OCFS2_CACHE_INFO_MAX_ARRAY; i++) {
408 tree[i] = kmem_cache_alloc(ocfs2_uptodate_cachep,
415 /* These are initialized in ocfs2_expand_cache! */
419 ocfs2_metadata_cache_lock(ci);
420 if (ocfs2_insert_can_use_array(ci)) {
421 /* Ok, items were removed from the cache in between
422 * locks. Detect this and revert back to the fast path */
423 ocfs2_append_cache_array(ci, block);
424 ocfs2_metadata_cache_unlock(ci);
429 ocfs2_expand_cache(ci, tree);
431 __ocfs2_insert_cache_tree(ci, new);
432 ocfs2_metadata_cache_unlock(ci);
437 kmem_cache_free(ocfs2_uptodate_cachep, new);
439 /* If these were used, then ocfs2_expand_cache re-set them to
442 for (i = 0; i < OCFS2_CACHE_INFO_MAX_ARRAY; i++)
444 kmem_cache_free(ocfs2_uptodate_cachep,
449 /* Item insertion is guarded by co_io_lock(), so the insertion path takes
450 * advantage of this by not rechecking for a duplicate insert during
451 * the slow case. Additionally, if the cache needs to be bumped up to
452 * a tree, the code will not recheck after acquiring the lock --
453 * multiple paths cannot be expanding to a tree at the same time.
455 * The slow path takes into account that items can be removed
456 * (including the whole tree wiped and reset) when this process it out
457 * allocating memory. In those cases, it reverts back to the fast
460 * Note that this function may actually fail to insert the block if
461 * memory cannot be allocated. This is not fatal however (but may
462 * result in a performance penalty)
464 * Readahead buffers can be passed in here before the I/O request is
467 void ocfs2_set_buffer_uptodate(struct ocfs2_caching_info *ci,
468 struct buffer_head *bh)
472 /* The block may very well exist in our cache already, so avoid
473 * doing any more work in that case. */
474 if (ocfs2_buffer_cached(ci, bh))
477 trace_ocfs2_set_buffer_uptodate_begin(
478 (unsigned long long)ocfs2_metadata_cache_owner(ci),
479 (unsigned long long)bh->b_blocknr);
481 /* No need to recheck under spinlock - insertion is guarded by
483 ocfs2_metadata_cache_lock(ci);
484 if (ocfs2_insert_can_use_array(ci)) {
485 /* Fast case - it's an array and there's a free
487 ocfs2_append_cache_array(ci, bh->b_blocknr);
488 ocfs2_metadata_cache_unlock(ci);
493 if (ci->ci_flags & OCFS2_CACHE_FL_INLINE) {
494 /* We need to bump things up to a tree. */
497 ocfs2_metadata_cache_unlock(ci);
499 __ocfs2_set_buffer_uptodate(ci, bh->b_blocknr, expand);
502 /* Called against a newly allocated buffer. Most likely nobody should
503 * be able to read this sort of metadata while it's still being
504 * allocated, but this is careful to take co_io_lock() anyway. */
505 void ocfs2_set_new_buffer_uptodate(struct ocfs2_caching_info *ci,
506 struct buffer_head *bh)
508 /* This should definitely *not* exist in our cache */
509 BUG_ON(ocfs2_buffer_cached(ci, bh));
511 set_buffer_uptodate(bh);
513 ocfs2_metadata_cache_io_lock(ci);
514 ocfs2_set_buffer_uptodate(ci, bh);
515 ocfs2_metadata_cache_io_unlock(ci);
518 /* Requires ip_lock. */
519 static void ocfs2_remove_metadata_array(struct ocfs2_caching_info *ci,
522 sector_t *array = ci->ci_cache.ci_array;
525 BUG_ON(index < 0 || index >= OCFS2_CACHE_INFO_MAX_ARRAY);
526 BUG_ON(index >= ci->ci_num_cached);
527 BUG_ON(!ci->ci_num_cached);
529 trace_ocfs2_remove_metadata_array(
530 (unsigned long long)ocfs2_metadata_cache_owner(ci),
531 index, ci->ci_num_cached);
535 /* don't need to copy if the array is now empty, or if we
536 * removed at the tail */
537 if (ci->ci_num_cached && index < ci->ci_num_cached) {
538 bytes = sizeof(sector_t) * (ci->ci_num_cached - index);
539 memmove(&array[index], &array[index + 1], bytes);
543 /* Requires ip_lock. */
544 static void ocfs2_remove_metadata_tree(struct ocfs2_caching_info *ci,
545 struct ocfs2_meta_cache_item *item)
547 trace_ocfs2_remove_metadata_tree(
548 (unsigned long long)ocfs2_metadata_cache_owner(ci),
549 (unsigned long long)item->c_block);
551 rb_erase(&item->c_node, &ci->ci_cache.ci_tree);
555 static void ocfs2_remove_block_from_cache(struct ocfs2_caching_info *ci,
559 struct ocfs2_meta_cache_item *item = NULL;
561 ocfs2_metadata_cache_lock(ci);
562 trace_ocfs2_remove_block_from_cache(
563 (unsigned long long)ocfs2_metadata_cache_owner(ci),
564 (unsigned long long) block, ci->ci_num_cached,
567 if (ci->ci_flags & OCFS2_CACHE_FL_INLINE) {
568 index = ocfs2_search_cache_array(ci, block);
570 ocfs2_remove_metadata_array(ci, index);
572 item = ocfs2_search_cache_tree(ci, block);
574 ocfs2_remove_metadata_tree(ci, item);
576 ocfs2_metadata_cache_unlock(ci);
579 kmem_cache_free(ocfs2_uptodate_cachep, item);
583 * Called when we remove a chunk of metadata from an inode. We don't
584 * bother reverting things to an inlined array in the case of a remove
585 * which moves us back under the limit.
587 void ocfs2_remove_from_cache(struct ocfs2_caching_info *ci,
588 struct buffer_head *bh)
590 sector_t block = bh->b_blocknr;
592 ocfs2_remove_block_from_cache(ci, block);
595 /* Called when we remove xattr clusters from an inode. */
596 void ocfs2_remove_xattr_clusters_from_cache(struct ocfs2_caching_info *ci,
600 struct super_block *sb = ocfs2_metadata_cache_get_super(ci);
601 unsigned int i, b_len = ocfs2_clusters_to_blocks(sb, 1) * c_len;
603 for (i = 0; i < b_len; i++, block++)
604 ocfs2_remove_block_from_cache(ci, block);
607 int __init init_ocfs2_uptodate_cache(void)
609 ocfs2_uptodate_cachep = kmem_cache_create("ocfs2_uptodate",
610 sizeof(struct ocfs2_meta_cache_item),
611 0, SLAB_HWCACHE_ALIGN, NULL);
612 if (!ocfs2_uptodate_cachep)
618 void exit_ocfs2_uptodate_cache(void)
620 kmem_cache_destroy(ocfs2_uptodate_cachep);