// SPDX-License-Identifier: GPL-2.0-or-later /* * uptodate.c * * Tracking the up-to-date-ness of a local buffer_head with respect to * the cluster. * * Copyright (C) 2002, 2004, 2005 Oracle. All rights reserved. * * Standard buffer head caching flags (uptodate, etc) are insufficient * in a clustered environment - a buffer may be marked up to date on * our local node but could have been modified by another cluster * member. As a result an additional (and performant) caching scheme * is required. A further requirement is that we consume as little * memory as possible - we never pin buffer_head structures in order * to cache them. * * We track the existence of up to date buffers on the inodes which * are associated with them. Because we don't want to pin * buffer_heads, this is only a (strong) hint and several other checks * are made in the I/O path to ensure that we don't use a stale or * invalid buffer without going to disk: * - buffer_jbd is used liberally - if a bh is in the journal on * this node then it *must* be up to date. * - the standard buffer_uptodate() macro is used to detect buffers * which may be invalid (even if we have an up to date tracking * item for them) * * For a full understanding of how this code works together, one * should read the callers in dlmglue.c, the I/O functions in * buffer_head_io.c and ocfs2_journal_access in journal.c */ #include <linux/fs.h> #include <linux/types.h> #include <linux/slab.h> #include <linux/highmem.h> #include <linux/buffer_head.h> #include <linux/rbtree.h> #include <cluster/masklog.h> #include "ocfs2.h" #include "inode.h" #include "uptodate.h" #include "ocfs2_trace.h" struct ocfs2_meta_cache_item { … }; static struct kmem_cache *ocfs2_uptodate_cachep; u64 ocfs2_metadata_cache_owner(struct ocfs2_caching_info *ci) { … } struct super_block *ocfs2_metadata_cache_get_super(struct ocfs2_caching_info *ci) { … } static void ocfs2_metadata_cache_lock(struct ocfs2_caching_info *ci) { … } static void ocfs2_metadata_cache_unlock(struct ocfs2_caching_info *ci) { … } void ocfs2_metadata_cache_io_lock(struct ocfs2_caching_info *ci) { … } void ocfs2_metadata_cache_io_unlock(struct ocfs2_caching_info *ci) { … } static void ocfs2_metadata_cache_reset(struct ocfs2_caching_info *ci, int clear) { … } void ocfs2_metadata_cache_init(struct ocfs2_caching_info *ci, const struct ocfs2_caching_operations *ops) { … } void ocfs2_metadata_cache_exit(struct ocfs2_caching_info *ci) { … } /* No lock taken here as 'root' is not expected to be visible to other * processes. */ static unsigned int ocfs2_purge_copied_metadata_tree(struct rb_root *root) { … } /* Called from locking and called from ocfs2_clear_inode. Dump the * cache for a given inode. * * This function is a few more lines longer than necessary due to some * accounting done here, but I think it's worth tracking down those * bugs sooner -- Mark */ void ocfs2_metadata_cache_purge(struct ocfs2_caching_info *ci) { … } /* Returns the index in the cache array, -1 if not found. * Requires ip_lock. */ static int ocfs2_search_cache_array(struct ocfs2_caching_info *ci, sector_t item) { … } /* Returns the cache item if found, otherwise NULL. * Requires ip_lock. */ static struct ocfs2_meta_cache_item * ocfs2_search_cache_tree(struct ocfs2_caching_info *ci, sector_t block) { … } static int ocfs2_buffer_cached(struct ocfs2_caching_info *ci, struct buffer_head *bh) { … } /* Warning: even if it returns true, this does *not* guarantee that * the block is stored in our inode metadata cache. * * This can be called under lock_buffer() */ int ocfs2_buffer_uptodate(struct ocfs2_caching_info *ci, struct buffer_head *bh) { … } /* * Determine whether a buffer is currently out on a read-ahead request. * ci_io_sem should be held to serialize submitters with the logic here. */ int ocfs2_buffer_read_ahead(struct ocfs2_caching_info *ci, struct buffer_head *bh) { … } /* Requires ip_lock */ static void ocfs2_append_cache_array(struct ocfs2_caching_info *ci, sector_t block) { … } /* By now the caller should have checked that the item does *not* * exist in the tree. * Requires ip_lock. */ static void __ocfs2_insert_cache_tree(struct ocfs2_caching_info *ci, struct ocfs2_meta_cache_item *new) { … } /* co_cache_lock() must be held */ static inline int ocfs2_insert_can_use_array(struct ocfs2_caching_info *ci) { … } /* tree should be exactly OCFS2_CACHE_INFO_MAX_ARRAY wide. NULL the * pointers in tree after we use them - this allows caller to detect * when to free in case of error. * * The co_cache_lock() must be held. */ static void ocfs2_expand_cache(struct ocfs2_caching_info *ci, struct ocfs2_meta_cache_item **tree) { … } /* Slow path function - memory allocation is necessary. See the * comment above ocfs2_set_buffer_uptodate for more information. */ static void __ocfs2_set_buffer_uptodate(struct ocfs2_caching_info *ci, sector_t block, int expand_tree) { … } /* Item insertion is guarded by co_io_lock(), so the insertion path takes * advantage of this by not rechecking for a duplicate insert during * the slow case. Additionally, if the cache needs to be bumped up to * a tree, the code will not recheck after acquiring the lock -- * multiple paths cannot be expanding to a tree at the same time. * * The slow path takes into account that items can be removed * (including the whole tree wiped and reset) when this process it out * allocating memory. In those cases, it reverts back to the fast * path. * * Note that this function may actually fail to insert the block if * memory cannot be allocated. This is not fatal however (but may * result in a performance penalty) * * Readahead buffers can be passed in here before the I/O request is * completed. */ void ocfs2_set_buffer_uptodate(struct ocfs2_caching_info *ci, struct buffer_head *bh) { … } /* Called against a newly allocated buffer. Most likely nobody should * be able to read this sort of metadata while it's still being * allocated, but this is careful to take co_io_lock() anyway. */ void ocfs2_set_new_buffer_uptodate(struct ocfs2_caching_info *ci, struct buffer_head *bh) { … } /* Requires ip_lock. */ static void ocfs2_remove_metadata_array(struct ocfs2_caching_info *ci, int index) { … } /* Requires ip_lock. */ static void ocfs2_remove_metadata_tree(struct ocfs2_caching_info *ci, struct ocfs2_meta_cache_item *item) { … } static void ocfs2_remove_block_from_cache(struct ocfs2_caching_info *ci, sector_t block) { … } /* * Called when we remove a chunk of metadata from an inode. We don't * bother reverting things to an inlined array in the case of a remove * which moves us back under the limit. */ void ocfs2_remove_from_cache(struct ocfs2_caching_info *ci, struct buffer_head *bh) { … } /* Called when we remove xattr clusters from an inode. */ void ocfs2_remove_xattr_clusters_from_cache(struct ocfs2_caching_info *ci, sector_t block, u32 c_len) { … } int __init init_ocfs2_uptodate_cache(void) { … } void exit_ocfs2_uptodate_cache(void) { … }