Thomas Gleixner | 328970d | 2019-05-24 12:04:05 +0200 | [diff] [blame] | 1 | // SPDX-License-Identifier: GPL-2.0-or-later |
Mark Fasheh | ccd979b | 2005-12-15 14:31:24 -0800 | [diff] [blame] | 2 | /* -*- mode: c; c-basic-offset: 8; -*- |
| 3 | * vim: noexpandtab sw=8 ts=8 sts=0: |
| 4 | * |
| 5 | * uptodate.c |
| 6 | * |
| 7 | * Tracking the up-to-date-ness of a local buffer_head with respect to |
| 8 | * the cluster. |
| 9 | * |
| 10 | * Copyright (C) 2002, 2004, 2005 Oracle. All rights reserved. |
| 11 | * |
Mark Fasheh | ccd979b | 2005-12-15 14:31:24 -0800 | [diff] [blame] | 12 | * Standard buffer head caching flags (uptodate, etc) are insufficient |
| 13 | * in a clustered environment - a buffer may be marked up to date on |
| 14 | * our local node but could have been modified by another cluster |
| 15 | * member. As a result an additional (and performant) caching scheme |
| 16 | * is required. A further requirement is that we consume as little |
| 17 | * memory as possible - we never pin buffer_head structures in order |
| 18 | * to cache them. |
| 19 | * |
| 20 | * We track the existence of up to date buffers on the inodes which |
| 21 | * are associated with them. Because we don't want to pin |
| 22 | * buffer_heads, this is only a (strong) hint and several other checks |
| 23 | * are made in the I/O path to ensure that we don't use a stale or |
| 24 | * invalid buffer without going to disk: |
| 25 | * - buffer_jbd is used liberally - if a bh is in the journal on |
| 26 | * this node then it *must* be up to date. |
| 27 | * - the standard buffer_uptodate() macro is used to detect buffers |
| 28 | * which may be invalid (even if we have an up to date tracking |
| 29 | * item for them) |
| 30 | * |
| 31 | * For a full understanding of how this code works together, one |
| 32 | * should read the callers in dlmglue.c, the I/O functions in |
| 33 | * buffer_head_io.c and ocfs2_journal_access in journal.c |
| 34 | */ |
| 35 | |
| 36 | #include <linux/fs.h> |
| 37 | #include <linux/types.h> |
| 38 | #include <linux/slab.h> |
| 39 | #include <linux/highmem.h> |
| 40 | #include <linux/buffer_head.h> |
| 41 | #include <linux/rbtree.h> |
Mark Fasheh | ccd979b | 2005-12-15 14:31:24 -0800 | [diff] [blame] | 42 | |
Mark Fasheh | ccd979b | 2005-12-15 14:31:24 -0800 | [diff] [blame] | 43 | #include <cluster/masklog.h> |
| 44 | |
| 45 | #include "ocfs2.h" |
| 46 | |
| 47 | #include "inode.h" |
| 48 | #include "uptodate.h" |
Tao Ma | d701485 | 2011-02-24 16:22:20 +0800 | [diff] [blame] | 49 | #include "ocfs2_trace.h" |
Mark Fasheh | ccd979b | 2005-12-15 14:31:24 -0800 | [diff] [blame] | 50 | |
| 51 | struct ocfs2_meta_cache_item { |
| 52 | struct rb_node c_node; |
| 53 | sector_t c_block; |
| 54 | }; |
| 55 | |
Fabian Frederick | 1a5c4e2 | 2014-06-04 16:06:06 -0700 | [diff] [blame] | 56 | static struct kmem_cache *ocfs2_uptodate_cachep; |
Mark Fasheh | ccd979b | 2005-12-15 14:31:24 -0800 | [diff] [blame] | 57 | |
Joel Becker | 8cb471e | 2009-02-10 20:00:41 -0800 | [diff] [blame] | 58 | u64 ocfs2_metadata_cache_owner(struct ocfs2_caching_info *ci) |
Mark Fasheh | ccd979b | 2005-12-15 14:31:24 -0800 | [diff] [blame] | 59 | { |
Joel Becker | 6e5a3d7 | 2009-02-10 19:00:37 -0800 | [diff] [blame] | 60 | BUG_ON(!ci || !ci->ci_ops); |
| 61 | |
| 62 | return ci->ci_ops->co_owner(ci); |
| 63 | } |
| 64 | |
Joel Becker | 8cb471e | 2009-02-10 20:00:41 -0800 | [diff] [blame] | 65 | struct super_block *ocfs2_metadata_cache_get_super(struct ocfs2_caching_info *ci) |
| 66 | { |
| 67 | BUG_ON(!ci || !ci->ci_ops); |
| 68 | |
| 69 | return ci->ci_ops->co_get_super(ci); |
| 70 | } |
| 71 | |
Joel Becker | 6e5a3d7 | 2009-02-10 19:00:37 -0800 | [diff] [blame] | 72 | static void ocfs2_metadata_cache_lock(struct ocfs2_caching_info *ci) |
| 73 | { |
| 74 | BUG_ON(!ci || !ci->ci_ops); |
| 75 | |
| 76 | ci->ci_ops->co_cache_lock(ci); |
| 77 | } |
| 78 | |
| 79 | static void ocfs2_metadata_cache_unlock(struct ocfs2_caching_info *ci) |
| 80 | { |
| 81 | BUG_ON(!ci || !ci->ci_ops); |
| 82 | |
| 83 | ci->ci_ops->co_cache_unlock(ci); |
| 84 | } |
| 85 | |
Joel Becker | 8cb471e | 2009-02-10 20:00:41 -0800 | [diff] [blame] | 86 | void ocfs2_metadata_cache_io_lock(struct ocfs2_caching_info *ci) |
Joel Becker | 6e5a3d7 | 2009-02-10 19:00:37 -0800 | [diff] [blame] | 87 | { |
| 88 | BUG_ON(!ci || !ci->ci_ops); |
| 89 | |
| 90 | ci->ci_ops->co_io_lock(ci); |
| 91 | } |
| 92 | |
Joel Becker | 8cb471e | 2009-02-10 20:00:41 -0800 | [diff] [blame] | 93 | void ocfs2_metadata_cache_io_unlock(struct ocfs2_caching_info *ci) |
Joel Becker | 6e5a3d7 | 2009-02-10 19:00:37 -0800 | [diff] [blame] | 94 | { |
| 95 | BUG_ON(!ci || !ci->ci_ops); |
| 96 | |
| 97 | ci->ci_ops->co_io_unlock(ci); |
| 98 | } |
| 99 | |
| 100 | |
Joel Becker | 66fb345 | 2009-02-12 15:24:40 -0800 | [diff] [blame] | 101 | static void ocfs2_metadata_cache_reset(struct ocfs2_caching_info *ci, |
| 102 | int clear) |
| 103 | { |
| 104 | ci->ci_flags |= OCFS2_CACHE_FL_INLINE; |
| 105 | ci->ci_num_cached = 0; |
| 106 | |
Joel Becker | 292dd27 | 2009-02-12 15:41:59 -0800 | [diff] [blame] | 107 | if (clear) { |
| 108 | ci->ci_created_trans = 0; |
Joel Becker | 66fb345 | 2009-02-12 15:24:40 -0800 | [diff] [blame] | 109 | ci->ci_last_trans = 0; |
Joel Becker | 292dd27 | 2009-02-12 15:41:59 -0800 | [diff] [blame] | 110 | } |
Joel Becker | 66fb345 | 2009-02-12 15:24:40 -0800 | [diff] [blame] | 111 | } |
| 112 | |
Joel Becker | 6e5a3d7 | 2009-02-10 19:00:37 -0800 | [diff] [blame] | 113 | void ocfs2_metadata_cache_init(struct ocfs2_caching_info *ci, |
| 114 | const struct ocfs2_caching_operations *ops) |
| 115 | { |
| 116 | BUG_ON(!ops); |
| 117 | |
| 118 | ci->ci_ops = ops; |
Joel Becker | 66fb345 | 2009-02-12 15:24:40 -0800 | [diff] [blame] | 119 | ocfs2_metadata_cache_reset(ci, 1); |
Mark Fasheh | ccd979b | 2005-12-15 14:31:24 -0800 | [diff] [blame] | 120 | } |
| 121 | |
Joel Becker | 66fb345 | 2009-02-12 15:24:40 -0800 | [diff] [blame] | 122 | void ocfs2_metadata_cache_exit(struct ocfs2_caching_info *ci) |
| 123 | { |
| 124 | ocfs2_metadata_cache_purge(ci); |
| 125 | ocfs2_metadata_cache_reset(ci, 1); |
| 126 | } |
| 127 | |
| 128 | |
Mark Fasheh | ccd979b | 2005-12-15 14:31:24 -0800 | [diff] [blame] | 129 | /* No lock taken here as 'root' is not expected to be visible to other |
| 130 | * processes. */ |
| 131 | static unsigned int ocfs2_purge_copied_metadata_tree(struct rb_root *root) |
| 132 | { |
| 133 | unsigned int purged = 0; |
| 134 | struct rb_node *node; |
| 135 | struct ocfs2_meta_cache_item *item; |
| 136 | |
| 137 | while ((node = rb_last(root)) != NULL) { |
| 138 | item = rb_entry(node, struct ocfs2_meta_cache_item, c_node); |
| 139 | |
Tao Ma | d701485 | 2011-02-24 16:22:20 +0800 | [diff] [blame] | 140 | trace_ocfs2_purge_copied_metadata_tree( |
| 141 | (unsigned long long) item->c_block); |
Mark Fasheh | ccd979b | 2005-12-15 14:31:24 -0800 | [diff] [blame] | 142 | |
| 143 | rb_erase(&item->c_node, root); |
| 144 | kmem_cache_free(ocfs2_uptodate_cachep, item); |
| 145 | |
| 146 | purged++; |
| 147 | } |
| 148 | return purged; |
| 149 | } |
| 150 | |
| 151 | /* Called from locking and called from ocfs2_clear_inode. Dump the |
| 152 | * cache for a given inode. |
| 153 | * |
| 154 | * This function is a few more lines longer than necessary due to some |
| 155 | * accounting done here, but I think it's worth tracking down those |
| 156 | * bugs sooner -- Mark */ |
Joel Becker | 8cb471e | 2009-02-10 20:00:41 -0800 | [diff] [blame] | 157 | void ocfs2_metadata_cache_purge(struct ocfs2_caching_info *ci) |
Mark Fasheh | ccd979b | 2005-12-15 14:31:24 -0800 | [diff] [blame] | 158 | { |
Mark Fasheh | ccd979b | 2005-12-15 14:31:24 -0800 | [diff] [blame] | 159 | unsigned int tree, to_purge, purged; |
Mark Fasheh | ccd979b | 2005-12-15 14:31:24 -0800 | [diff] [blame] | 160 | struct rb_root root = RB_ROOT; |
| 161 | |
Joel Becker | 6e5a3d7 | 2009-02-10 19:00:37 -0800 | [diff] [blame] | 162 | BUG_ON(!ci || !ci->ci_ops); |
| 163 | |
| 164 | ocfs2_metadata_cache_lock(ci); |
Joel Becker | 47460d6 | 2009-02-10 16:05:07 -0800 | [diff] [blame] | 165 | tree = !(ci->ci_flags & OCFS2_CACHE_FL_INLINE); |
Mark Fasheh | ccd979b | 2005-12-15 14:31:24 -0800 | [diff] [blame] | 166 | to_purge = ci->ci_num_cached; |
| 167 | |
Tao Ma | d701485 | 2011-02-24 16:22:20 +0800 | [diff] [blame] | 168 | trace_ocfs2_metadata_cache_purge( |
| 169 | (unsigned long long)ocfs2_metadata_cache_owner(ci), |
| 170 | to_purge, tree); |
Mark Fasheh | ccd979b | 2005-12-15 14:31:24 -0800 | [diff] [blame] | 171 | |
| 172 | /* If we're a tree, save off the root so that we can safely |
| 173 | * initialize the cache. We do the work to free tree members |
| 174 | * without the spinlock. */ |
| 175 | if (tree) |
| 176 | root = ci->ci_cache.ci_tree; |
| 177 | |
Joel Becker | 66fb345 | 2009-02-12 15:24:40 -0800 | [diff] [blame] | 178 | ocfs2_metadata_cache_reset(ci, 0); |
Joel Becker | 6e5a3d7 | 2009-02-10 19:00:37 -0800 | [diff] [blame] | 179 | ocfs2_metadata_cache_unlock(ci); |
Mark Fasheh | ccd979b | 2005-12-15 14:31:24 -0800 | [diff] [blame] | 180 | |
| 181 | purged = ocfs2_purge_copied_metadata_tree(&root); |
| 182 | /* If possible, track the number wiped so that we can more |
| 183 | * easily detect counting errors. Unfortunately, this is only |
| 184 | * meaningful for trees. */ |
| 185 | if (tree && purged != to_purge) |
Joel Becker | 6e5a3d7 | 2009-02-10 19:00:37 -0800 | [diff] [blame] | 186 | mlog(ML_ERROR, "Owner %llu, count = %u, purged = %u\n", |
| 187 | (unsigned long long)ocfs2_metadata_cache_owner(ci), |
| 188 | to_purge, purged); |
Mark Fasheh | ccd979b | 2005-12-15 14:31:24 -0800 | [diff] [blame] | 189 | } |
| 190 | |
| 191 | /* Returns the index in the cache array, -1 if not found. |
| 192 | * Requires ip_lock. */ |
| 193 | static int ocfs2_search_cache_array(struct ocfs2_caching_info *ci, |
| 194 | sector_t item) |
| 195 | { |
| 196 | int i; |
| 197 | |
| 198 | for (i = 0; i < ci->ci_num_cached; i++) { |
| 199 | if (item == ci->ci_cache.ci_array[i]) |
| 200 | return i; |
| 201 | } |
| 202 | |
| 203 | return -1; |
| 204 | } |
| 205 | |
| 206 | /* Returns the cache item if found, otherwise NULL. |
| 207 | * Requires ip_lock. */ |
| 208 | static struct ocfs2_meta_cache_item * |
| 209 | ocfs2_search_cache_tree(struct ocfs2_caching_info *ci, |
| 210 | sector_t block) |
| 211 | { |
| 212 | struct rb_node * n = ci->ci_cache.ci_tree.rb_node; |
| 213 | struct ocfs2_meta_cache_item *item = NULL; |
| 214 | |
| 215 | while (n) { |
| 216 | item = rb_entry(n, struct ocfs2_meta_cache_item, c_node); |
| 217 | |
| 218 | if (block < item->c_block) |
| 219 | n = n->rb_left; |
| 220 | else if (block > item->c_block) |
| 221 | n = n->rb_right; |
| 222 | else |
| 223 | return item; |
| 224 | } |
| 225 | |
| 226 | return NULL; |
| 227 | } |
| 228 | |
Joel Becker | 8cb471e | 2009-02-10 20:00:41 -0800 | [diff] [blame] | 229 | static int ocfs2_buffer_cached(struct ocfs2_caching_info *ci, |
Mark Fasheh | ccd979b | 2005-12-15 14:31:24 -0800 | [diff] [blame] | 230 | struct buffer_head *bh) |
| 231 | { |
| 232 | int index = -1; |
| 233 | struct ocfs2_meta_cache_item *item = NULL; |
| 234 | |
Joel Becker | 6e5a3d7 | 2009-02-10 19:00:37 -0800 | [diff] [blame] | 235 | ocfs2_metadata_cache_lock(ci); |
Mark Fasheh | ccd979b | 2005-12-15 14:31:24 -0800 | [diff] [blame] | 236 | |
Tao Ma | d701485 | 2011-02-24 16:22:20 +0800 | [diff] [blame] | 237 | trace_ocfs2_buffer_cached_begin( |
| 238 | (unsigned long long)ocfs2_metadata_cache_owner(ci), |
| 239 | (unsigned long long) bh->b_blocknr, |
| 240 | !!(ci->ci_flags & OCFS2_CACHE_FL_INLINE)); |
Mark Fasheh | ccd979b | 2005-12-15 14:31:24 -0800 | [diff] [blame] | 241 | |
Joel Becker | 47460d6 | 2009-02-10 16:05:07 -0800 | [diff] [blame] | 242 | if (ci->ci_flags & OCFS2_CACHE_FL_INLINE) |
Joel Becker | 8cb471e | 2009-02-10 20:00:41 -0800 | [diff] [blame] | 243 | index = ocfs2_search_cache_array(ci, bh->b_blocknr); |
Mark Fasheh | ccd979b | 2005-12-15 14:31:24 -0800 | [diff] [blame] | 244 | else |
Joel Becker | 8cb471e | 2009-02-10 20:00:41 -0800 | [diff] [blame] | 245 | item = ocfs2_search_cache_tree(ci, bh->b_blocknr); |
Mark Fasheh | ccd979b | 2005-12-15 14:31:24 -0800 | [diff] [blame] | 246 | |
Joel Becker | 6e5a3d7 | 2009-02-10 19:00:37 -0800 | [diff] [blame] | 247 | ocfs2_metadata_cache_unlock(ci); |
Mark Fasheh | ccd979b | 2005-12-15 14:31:24 -0800 | [diff] [blame] | 248 | |
Tao Ma | d701485 | 2011-02-24 16:22:20 +0800 | [diff] [blame] | 249 | trace_ocfs2_buffer_cached_end(index, item); |
Mark Fasheh | ccd979b | 2005-12-15 14:31:24 -0800 | [diff] [blame] | 250 | |
| 251 | return (index != -1) || (item != NULL); |
| 252 | } |
| 253 | |
| 254 | /* Warning: even if it returns true, this does *not* guarantee that |
Sunil Mushran | 2bd6321 | 2010-01-25 16:57:38 -0800 | [diff] [blame] | 255 | * the block is stored in our inode metadata cache. |
| 256 | * |
Mark Fasheh | aa95887 | 2006-04-21 13:49:02 -0700 | [diff] [blame] | 257 | * This can be called under lock_buffer() |
| 258 | */ |
Joel Becker | 8cb471e | 2009-02-10 20:00:41 -0800 | [diff] [blame] | 259 | int ocfs2_buffer_uptodate(struct ocfs2_caching_info *ci, |
Mark Fasheh | ccd979b | 2005-12-15 14:31:24 -0800 | [diff] [blame] | 260 | struct buffer_head *bh) |
| 261 | { |
| 262 | /* Doesn't matter if the bh is in our cache or not -- if it's |
| 263 | * not marked uptodate then we know it can't have correct |
| 264 | * data. */ |
| 265 | if (!buffer_uptodate(bh)) |
| 266 | return 0; |
| 267 | |
| 268 | /* OCFS2 does not allow multiple nodes to be changing the same |
| 269 | * block at the same time. */ |
| 270 | if (buffer_jbd(bh)) |
| 271 | return 1; |
| 272 | |
| 273 | /* Ok, locally the buffer is marked as up to date, now search |
| 274 | * our cache to see if we can trust that. */ |
Joel Becker | 8cb471e | 2009-02-10 20:00:41 -0800 | [diff] [blame] | 275 | return ocfs2_buffer_cached(ci, bh); |
Mark Fasheh | ccd979b | 2005-12-15 14:31:24 -0800 | [diff] [blame] | 276 | } |
| 277 | |
Joel Becker | 8cb471e | 2009-02-10 20:00:41 -0800 | [diff] [blame] | 278 | /* |
Mark Fasheh | aa95887 | 2006-04-21 13:49:02 -0700 | [diff] [blame] | 279 | * Determine whether a buffer is currently out on a read-ahead request. |
Joel Becker | 47460d6 | 2009-02-10 16:05:07 -0800 | [diff] [blame] | 280 | * ci_io_sem should be held to serialize submitters with the logic here. |
Mark Fasheh | aa95887 | 2006-04-21 13:49:02 -0700 | [diff] [blame] | 281 | */ |
Joel Becker | 8cb471e | 2009-02-10 20:00:41 -0800 | [diff] [blame] | 282 | int ocfs2_buffer_read_ahead(struct ocfs2_caching_info *ci, |
Mark Fasheh | aa95887 | 2006-04-21 13:49:02 -0700 | [diff] [blame] | 283 | struct buffer_head *bh) |
| 284 | { |
Joel Becker | 8cb471e | 2009-02-10 20:00:41 -0800 | [diff] [blame] | 285 | return buffer_locked(bh) && ocfs2_buffer_cached(ci, bh); |
Mark Fasheh | aa95887 | 2006-04-21 13:49:02 -0700 | [diff] [blame] | 286 | } |
| 287 | |
Mark Fasheh | ccd979b | 2005-12-15 14:31:24 -0800 | [diff] [blame] | 288 | /* Requires ip_lock */ |
| 289 | static void ocfs2_append_cache_array(struct ocfs2_caching_info *ci, |
| 290 | sector_t block) |
| 291 | { |
Joel Becker | 47460d6 | 2009-02-10 16:05:07 -0800 | [diff] [blame] | 292 | BUG_ON(ci->ci_num_cached >= OCFS2_CACHE_INFO_MAX_ARRAY); |
Mark Fasheh | ccd979b | 2005-12-15 14:31:24 -0800 | [diff] [blame] | 293 | |
Tao Ma | d701485 | 2011-02-24 16:22:20 +0800 | [diff] [blame] | 294 | trace_ocfs2_append_cache_array( |
| 295 | (unsigned long long)ocfs2_metadata_cache_owner(ci), |
| 296 | (unsigned long long)block, ci->ci_num_cached); |
Mark Fasheh | ccd979b | 2005-12-15 14:31:24 -0800 | [diff] [blame] | 297 | |
| 298 | ci->ci_cache.ci_array[ci->ci_num_cached] = block; |
| 299 | ci->ci_num_cached++; |
| 300 | } |
| 301 | |
| 302 | /* By now the caller should have checked that the item does *not* |
| 303 | * exist in the tree. |
| 304 | * Requires ip_lock. */ |
| 305 | static void __ocfs2_insert_cache_tree(struct ocfs2_caching_info *ci, |
| 306 | struct ocfs2_meta_cache_item *new) |
| 307 | { |
| 308 | sector_t block = new->c_block; |
| 309 | struct rb_node *parent = NULL; |
| 310 | struct rb_node **p = &ci->ci_cache.ci_tree.rb_node; |
| 311 | struct ocfs2_meta_cache_item *tmp; |
| 312 | |
Tao Ma | d701485 | 2011-02-24 16:22:20 +0800 | [diff] [blame] | 313 | trace_ocfs2_insert_cache_tree( |
| 314 | (unsigned long long)ocfs2_metadata_cache_owner(ci), |
| 315 | (unsigned long long)block, ci->ci_num_cached); |
Mark Fasheh | ccd979b | 2005-12-15 14:31:24 -0800 | [diff] [blame] | 316 | |
| 317 | while(*p) { |
| 318 | parent = *p; |
| 319 | |
| 320 | tmp = rb_entry(parent, struct ocfs2_meta_cache_item, c_node); |
| 321 | |
| 322 | if (block < tmp->c_block) |
| 323 | p = &(*p)->rb_left; |
| 324 | else if (block > tmp->c_block) |
| 325 | p = &(*p)->rb_right; |
| 326 | else { |
| 327 | /* This should never happen! */ |
| 328 | mlog(ML_ERROR, "Duplicate block %llu cached!\n", |
| 329 | (unsigned long long) block); |
| 330 | BUG(); |
| 331 | } |
| 332 | } |
| 333 | |
| 334 | rb_link_node(&new->c_node, parent, p); |
| 335 | rb_insert_color(&new->c_node, &ci->ci_cache.ci_tree); |
| 336 | ci->ci_num_cached++; |
| 337 | } |
| 338 | |
Joel Becker | 6e5a3d7 | 2009-02-10 19:00:37 -0800 | [diff] [blame] | 339 | /* co_cache_lock() must be held */ |
Joel Becker | 8cb471e | 2009-02-10 20:00:41 -0800 | [diff] [blame] | 340 | static inline int ocfs2_insert_can_use_array(struct ocfs2_caching_info *ci) |
Mark Fasheh | ccd979b | 2005-12-15 14:31:24 -0800 | [diff] [blame] | 341 | { |
Joel Becker | 47460d6 | 2009-02-10 16:05:07 -0800 | [diff] [blame] | 342 | return (ci->ci_flags & OCFS2_CACHE_FL_INLINE) && |
| 343 | (ci->ci_num_cached < OCFS2_CACHE_INFO_MAX_ARRAY); |
Mark Fasheh | ccd979b | 2005-12-15 14:31:24 -0800 | [diff] [blame] | 344 | } |
| 345 | |
Joel Becker | 47460d6 | 2009-02-10 16:05:07 -0800 | [diff] [blame] | 346 | /* tree should be exactly OCFS2_CACHE_INFO_MAX_ARRAY wide. NULL the |
Mark Fasheh | ccd979b | 2005-12-15 14:31:24 -0800 | [diff] [blame] | 347 | * pointers in tree after we use them - this allows caller to detect |
Joel Becker | 6e5a3d7 | 2009-02-10 19:00:37 -0800 | [diff] [blame] | 348 | * when to free in case of error. |
| 349 | * |
| 350 | * The co_cache_lock() must be held. */ |
Joel Becker | 8cb471e | 2009-02-10 20:00:41 -0800 | [diff] [blame] | 351 | static void ocfs2_expand_cache(struct ocfs2_caching_info *ci, |
Mark Fasheh | ccd979b | 2005-12-15 14:31:24 -0800 | [diff] [blame] | 352 | struct ocfs2_meta_cache_item **tree) |
| 353 | { |
| 354 | int i; |
Mark Fasheh | ccd979b | 2005-12-15 14:31:24 -0800 | [diff] [blame] | 355 | |
Joel Becker | 47460d6 | 2009-02-10 16:05:07 -0800 | [diff] [blame] | 356 | mlog_bug_on_msg(ci->ci_num_cached != OCFS2_CACHE_INFO_MAX_ARRAY, |
Joel Becker | 6e5a3d7 | 2009-02-10 19:00:37 -0800 | [diff] [blame] | 357 | "Owner %llu, num cached = %u, should be %u\n", |
| 358 | (unsigned long long)ocfs2_metadata_cache_owner(ci), |
| 359 | ci->ci_num_cached, OCFS2_CACHE_INFO_MAX_ARRAY); |
Joel Becker | 47460d6 | 2009-02-10 16:05:07 -0800 | [diff] [blame] | 360 | mlog_bug_on_msg(!(ci->ci_flags & OCFS2_CACHE_FL_INLINE), |
Joel Becker | 6e5a3d7 | 2009-02-10 19:00:37 -0800 | [diff] [blame] | 361 | "Owner %llu not marked as inline anymore!\n", |
| 362 | (unsigned long long)ocfs2_metadata_cache_owner(ci)); |
Mark Fasheh | ccd979b | 2005-12-15 14:31:24 -0800 | [diff] [blame] | 363 | |
| 364 | /* Be careful to initialize the tree members *first* because |
| 365 | * once the ci_tree is used, the array is junk... */ |
Joel Becker | 47460d6 | 2009-02-10 16:05:07 -0800 | [diff] [blame] | 366 | for (i = 0; i < OCFS2_CACHE_INFO_MAX_ARRAY; i++) |
Mark Fasheh | ccd979b | 2005-12-15 14:31:24 -0800 | [diff] [blame] | 367 | tree[i]->c_block = ci->ci_cache.ci_array[i]; |
| 368 | |
Joel Becker | 47460d6 | 2009-02-10 16:05:07 -0800 | [diff] [blame] | 369 | ci->ci_flags &= ~OCFS2_CACHE_FL_INLINE; |
Mark Fasheh | ccd979b | 2005-12-15 14:31:24 -0800 | [diff] [blame] | 370 | ci->ci_cache.ci_tree = RB_ROOT; |
| 371 | /* this will be set again by __ocfs2_insert_cache_tree */ |
| 372 | ci->ci_num_cached = 0; |
| 373 | |
Joel Becker | 47460d6 | 2009-02-10 16:05:07 -0800 | [diff] [blame] | 374 | for (i = 0; i < OCFS2_CACHE_INFO_MAX_ARRAY; i++) { |
Mark Fasheh | ccd979b | 2005-12-15 14:31:24 -0800 | [diff] [blame] | 375 | __ocfs2_insert_cache_tree(ci, tree[i]); |
| 376 | tree[i] = NULL; |
| 377 | } |
| 378 | |
Tao Ma | d701485 | 2011-02-24 16:22:20 +0800 | [diff] [blame] | 379 | trace_ocfs2_expand_cache( |
| 380 | (unsigned long long)ocfs2_metadata_cache_owner(ci), |
| 381 | ci->ci_flags, ci->ci_num_cached); |
Mark Fasheh | ccd979b | 2005-12-15 14:31:24 -0800 | [diff] [blame] | 382 | } |
| 383 | |
| 384 | /* Slow path function - memory allocation is necessary. See the |
| 385 | * comment above ocfs2_set_buffer_uptodate for more information. */ |
Joel Becker | 8cb471e | 2009-02-10 20:00:41 -0800 | [diff] [blame] | 386 | static void __ocfs2_set_buffer_uptodate(struct ocfs2_caching_info *ci, |
Mark Fasheh | ccd979b | 2005-12-15 14:31:24 -0800 | [diff] [blame] | 387 | sector_t block, |
| 388 | int expand_tree) |
| 389 | { |
| 390 | int i; |
Mark Fasheh | ccd979b | 2005-12-15 14:31:24 -0800 | [diff] [blame] | 391 | struct ocfs2_meta_cache_item *new = NULL; |
Joel Becker | 47460d6 | 2009-02-10 16:05:07 -0800 | [diff] [blame] | 392 | struct ocfs2_meta_cache_item *tree[OCFS2_CACHE_INFO_MAX_ARRAY] = |
Mark Fasheh | ccd979b | 2005-12-15 14:31:24 -0800 | [diff] [blame] | 393 | { NULL, }; |
| 394 | |
Tao Ma | d701485 | 2011-02-24 16:22:20 +0800 | [diff] [blame] | 395 | trace_ocfs2_set_buffer_uptodate( |
| 396 | (unsigned long long)ocfs2_metadata_cache_owner(ci), |
| 397 | (unsigned long long)block, expand_tree); |
Mark Fasheh | ccd979b | 2005-12-15 14:31:24 -0800 | [diff] [blame] | 398 | |
Sunil Mushran | afae00ab | 2006-04-12 14:37:00 -0700 | [diff] [blame] | 399 | new = kmem_cache_alloc(ocfs2_uptodate_cachep, GFP_NOFS); |
Mark Fasheh | ccd979b | 2005-12-15 14:31:24 -0800 | [diff] [blame] | 400 | if (!new) { |
| 401 | mlog_errno(-ENOMEM); |
| 402 | return; |
| 403 | } |
| 404 | new->c_block = block; |
| 405 | |
| 406 | if (expand_tree) { |
| 407 | /* Do *not* allocate an array here - the removal code |
| 408 | * has no way of tracking that. */ |
Joel Becker | 47460d6 | 2009-02-10 16:05:07 -0800 | [diff] [blame] | 409 | for (i = 0; i < OCFS2_CACHE_INFO_MAX_ARRAY; i++) { |
Mark Fasheh | ccd979b | 2005-12-15 14:31:24 -0800 | [diff] [blame] | 410 | tree[i] = kmem_cache_alloc(ocfs2_uptodate_cachep, |
Sunil Mushran | afae00ab | 2006-04-12 14:37:00 -0700 | [diff] [blame] | 411 | GFP_NOFS); |
Mark Fasheh | ccd979b | 2005-12-15 14:31:24 -0800 | [diff] [blame] | 412 | if (!tree[i]) { |
| 413 | mlog_errno(-ENOMEM); |
| 414 | goto out_free; |
| 415 | } |
| 416 | |
| 417 | /* These are initialized in ocfs2_expand_cache! */ |
| 418 | } |
| 419 | } |
| 420 | |
Joel Becker | 6e5a3d7 | 2009-02-10 19:00:37 -0800 | [diff] [blame] | 421 | ocfs2_metadata_cache_lock(ci); |
Joel Becker | 8cb471e | 2009-02-10 20:00:41 -0800 | [diff] [blame] | 422 | if (ocfs2_insert_can_use_array(ci)) { |
Mark Fasheh | ccd979b | 2005-12-15 14:31:24 -0800 | [diff] [blame] | 423 | /* Ok, items were removed from the cache in between |
| 424 | * locks. Detect this and revert back to the fast path */ |
| 425 | ocfs2_append_cache_array(ci, block); |
Joel Becker | 6e5a3d7 | 2009-02-10 19:00:37 -0800 | [diff] [blame] | 426 | ocfs2_metadata_cache_unlock(ci); |
Mark Fasheh | ccd979b | 2005-12-15 14:31:24 -0800 | [diff] [blame] | 427 | goto out_free; |
| 428 | } |
| 429 | |
| 430 | if (expand_tree) |
Joel Becker | 8cb471e | 2009-02-10 20:00:41 -0800 | [diff] [blame] | 431 | ocfs2_expand_cache(ci, tree); |
Mark Fasheh | ccd979b | 2005-12-15 14:31:24 -0800 | [diff] [blame] | 432 | |
| 433 | __ocfs2_insert_cache_tree(ci, new); |
Joel Becker | 6e5a3d7 | 2009-02-10 19:00:37 -0800 | [diff] [blame] | 434 | ocfs2_metadata_cache_unlock(ci); |
Mark Fasheh | ccd979b | 2005-12-15 14:31:24 -0800 | [diff] [blame] | 435 | |
| 436 | new = NULL; |
| 437 | out_free: |
| 438 | if (new) |
| 439 | kmem_cache_free(ocfs2_uptodate_cachep, new); |
| 440 | |
| 441 | /* If these were used, then ocfs2_expand_cache re-set them to |
| 442 | * NULL for us. */ |
| 443 | if (tree[0]) { |
Joel Becker | 47460d6 | 2009-02-10 16:05:07 -0800 | [diff] [blame] | 444 | for (i = 0; i < OCFS2_CACHE_INFO_MAX_ARRAY; i++) |
Mark Fasheh | ccd979b | 2005-12-15 14:31:24 -0800 | [diff] [blame] | 445 | if (tree[i]) |
| 446 | kmem_cache_free(ocfs2_uptodate_cachep, |
| 447 | tree[i]); |
| 448 | } |
| 449 | } |
| 450 | |
Joel Becker | 6e5a3d7 | 2009-02-10 19:00:37 -0800 | [diff] [blame] | 451 | /* Item insertion is guarded by co_io_lock(), so the insertion path takes |
Mark Fasheh | ccd979b | 2005-12-15 14:31:24 -0800 | [diff] [blame] | 452 | * advantage of this by not rechecking for a duplicate insert during |
| 453 | * the slow case. Additionally, if the cache needs to be bumped up to |
| 454 | * a tree, the code will not recheck after acquiring the lock -- |
| 455 | * multiple paths cannot be expanding to a tree at the same time. |
| 456 | * |
| 457 | * The slow path takes into account that items can be removed |
| 458 | * (including the whole tree wiped and reset) when this process it out |
| 459 | * allocating memory. In those cases, it reverts back to the fast |
| 460 | * path. |
| 461 | * |
| 462 | * Note that this function may actually fail to insert the block if |
| 463 | * memory cannot be allocated. This is not fatal however (but may |
Mark Fasheh | aa95887 | 2006-04-21 13:49:02 -0700 | [diff] [blame] | 464 | * result in a performance penalty) |
| 465 | * |
| 466 | * Readahead buffers can be passed in here before the I/O request is |
| 467 | * completed. |
| 468 | */ |
Joel Becker | 8cb471e | 2009-02-10 20:00:41 -0800 | [diff] [blame] | 469 | void ocfs2_set_buffer_uptodate(struct ocfs2_caching_info *ci, |
Mark Fasheh | ccd979b | 2005-12-15 14:31:24 -0800 | [diff] [blame] | 470 | struct buffer_head *bh) |
| 471 | { |
| 472 | int expand; |
Mark Fasheh | ccd979b | 2005-12-15 14:31:24 -0800 | [diff] [blame] | 473 | |
| 474 | /* The block may very well exist in our cache already, so avoid |
| 475 | * doing any more work in that case. */ |
Joel Becker | 8cb471e | 2009-02-10 20:00:41 -0800 | [diff] [blame] | 476 | if (ocfs2_buffer_cached(ci, bh)) |
Mark Fasheh | ccd979b | 2005-12-15 14:31:24 -0800 | [diff] [blame] | 477 | return; |
| 478 | |
Tao Ma | d701485 | 2011-02-24 16:22:20 +0800 | [diff] [blame] | 479 | trace_ocfs2_set_buffer_uptodate_begin( |
| 480 | (unsigned long long)ocfs2_metadata_cache_owner(ci), |
| 481 | (unsigned long long)bh->b_blocknr); |
Mark Fasheh | ccd979b | 2005-12-15 14:31:24 -0800 | [diff] [blame] | 482 | |
| 483 | /* No need to recheck under spinlock - insertion is guarded by |
Joel Becker | 6e5a3d7 | 2009-02-10 19:00:37 -0800 | [diff] [blame] | 484 | * co_io_lock() */ |
| 485 | ocfs2_metadata_cache_lock(ci); |
Joel Becker | 8cb471e | 2009-02-10 20:00:41 -0800 | [diff] [blame] | 486 | if (ocfs2_insert_can_use_array(ci)) { |
Mark Fasheh | ccd979b | 2005-12-15 14:31:24 -0800 | [diff] [blame] | 487 | /* Fast case - it's an array and there's a free |
| 488 | * spot. */ |
| 489 | ocfs2_append_cache_array(ci, bh->b_blocknr); |
Joel Becker | 6e5a3d7 | 2009-02-10 19:00:37 -0800 | [diff] [blame] | 490 | ocfs2_metadata_cache_unlock(ci); |
Mark Fasheh | ccd979b | 2005-12-15 14:31:24 -0800 | [diff] [blame] | 491 | return; |
| 492 | } |
| 493 | |
| 494 | expand = 0; |
Joel Becker | 47460d6 | 2009-02-10 16:05:07 -0800 | [diff] [blame] | 495 | if (ci->ci_flags & OCFS2_CACHE_FL_INLINE) { |
Mark Fasheh | ccd979b | 2005-12-15 14:31:24 -0800 | [diff] [blame] | 496 | /* We need to bump things up to a tree. */ |
| 497 | expand = 1; |
| 498 | } |
Joel Becker | 6e5a3d7 | 2009-02-10 19:00:37 -0800 | [diff] [blame] | 499 | ocfs2_metadata_cache_unlock(ci); |
Mark Fasheh | ccd979b | 2005-12-15 14:31:24 -0800 | [diff] [blame] | 500 | |
Joel Becker | 8cb471e | 2009-02-10 20:00:41 -0800 | [diff] [blame] | 501 | __ocfs2_set_buffer_uptodate(ci, bh->b_blocknr, expand); |
Mark Fasheh | ccd979b | 2005-12-15 14:31:24 -0800 | [diff] [blame] | 502 | } |
| 503 | |
| 504 | /* Called against a newly allocated buffer. Most likely nobody should |
| 505 | * be able to read this sort of metadata while it's still being |
Joel Becker | 6e5a3d7 | 2009-02-10 19:00:37 -0800 | [diff] [blame] | 506 | * allocated, but this is careful to take co_io_lock() anyway. */ |
Joel Becker | 8cb471e | 2009-02-10 20:00:41 -0800 | [diff] [blame] | 507 | void ocfs2_set_new_buffer_uptodate(struct ocfs2_caching_info *ci, |
Mark Fasheh | ccd979b | 2005-12-15 14:31:24 -0800 | [diff] [blame] | 508 | struct buffer_head *bh) |
| 509 | { |
Mark Fasheh | ccd979b | 2005-12-15 14:31:24 -0800 | [diff] [blame] | 510 | /* This should definitely *not* exist in our cache */ |
Joel Becker | 8cb471e | 2009-02-10 20:00:41 -0800 | [diff] [blame] | 511 | BUG_ON(ocfs2_buffer_cached(ci, bh)); |
Mark Fasheh | ccd979b | 2005-12-15 14:31:24 -0800 | [diff] [blame] | 512 | |
| 513 | set_buffer_uptodate(bh); |
| 514 | |
Joel Becker | 6e5a3d7 | 2009-02-10 19:00:37 -0800 | [diff] [blame] | 515 | ocfs2_metadata_cache_io_lock(ci); |
Joel Becker | 8cb471e | 2009-02-10 20:00:41 -0800 | [diff] [blame] | 516 | ocfs2_set_buffer_uptodate(ci, bh); |
Joel Becker | 6e5a3d7 | 2009-02-10 19:00:37 -0800 | [diff] [blame] | 517 | ocfs2_metadata_cache_io_unlock(ci); |
Mark Fasheh | ccd979b | 2005-12-15 14:31:24 -0800 | [diff] [blame] | 518 | } |
| 519 | |
| 520 | /* Requires ip_lock. */ |
| 521 | static void ocfs2_remove_metadata_array(struct ocfs2_caching_info *ci, |
| 522 | int index) |
| 523 | { |
| 524 | sector_t *array = ci->ci_cache.ci_array; |
| 525 | int bytes; |
| 526 | |
Joel Becker | 47460d6 | 2009-02-10 16:05:07 -0800 | [diff] [blame] | 527 | BUG_ON(index < 0 || index >= OCFS2_CACHE_INFO_MAX_ARRAY); |
Mark Fasheh | ccd979b | 2005-12-15 14:31:24 -0800 | [diff] [blame] | 528 | BUG_ON(index >= ci->ci_num_cached); |
| 529 | BUG_ON(!ci->ci_num_cached); |
| 530 | |
Tao Ma | d701485 | 2011-02-24 16:22:20 +0800 | [diff] [blame] | 531 | trace_ocfs2_remove_metadata_array( |
| 532 | (unsigned long long)ocfs2_metadata_cache_owner(ci), |
| 533 | index, ci->ci_num_cached); |
Mark Fasheh | ccd979b | 2005-12-15 14:31:24 -0800 | [diff] [blame] | 534 | |
| 535 | ci->ci_num_cached--; |
| 536 | |
| 537 | /* don't need to copy if the array is now empty, or if we |
| 538 | * removed at the tail */ |
| 539 | if (ci->ci_num_cached && index < ci->ci_num_cached) { |
| 540 | bytes = sizeof(sector_t) * (ci->ci_num_cached - index); |
| 541 | memmove(&array[index], &array[index + 1], bytes); |
| 542 | } |
| 543 | } |
| 544 | |
| 545 | /* Requires ip_lock. */ |
| 546 | static void ocfs2_remove_metadata_tree(struct ocfs2_caching_info *ci, |
| 547 | struct ocfs2_meta_cache_item *item) |
| 548 | { |
Tao Ma | d701485 | 2011-02-24 16:22:20 +0800 | [diff] [blame] | 549 | trace_ocfs2_remove_metadata_tree( |
| 550 | (unsigned long long)ocfs2_metadata_cache_owner(ci), |
| 551 | (unsigned long long)item->c_block); |
Mark Fasheh | ccd979b | 2005-12-15 14:31:24 -0800 | [diff] [blame] | 552 | |
| 553 | rb_erase(&item->c_node, &ci->ci_cache.ci_tree); |
| 554 | ci->ci_num_cached--; |
| 555 | } |
| 556 | |
Joel Becker | 8cb471e | 2009-02-10 20:00:41 -0800 | [diff] [blame] | 557 | static void ocfs2_remove_block_from_cache(struct ocfs2_caching_info *ci, |
Tao Ma | ac11c82 | 2008-08-18 17:38:47 +0800 | [diff] [blame] | 558 | sector_t block) |
Mark Fasheh | ccd979b | 2005-12-15 14:31:24 -0800 | [diff] [blame] | 559 | { |
| 560 | int index; |
Mark Fasheh | ccd979b | 2005-12-15 14:31:24 -0800 | [diff] [blame] | 561 | struct ocfs2_meta_cache_item *item = NULL; |
Mark Fasheh | ccd979b | 2005-12-15 14:31:24 -0800 | [diff] [blame] | 562 | |
Joel Becker | 6e5a3d7 | 2009-02-10 19:00:37 -0800 | [diff] [blame] | 563 | ocfs2_metadata_cache_lock(ci); |
Tao Ma | d701485 | 2011-02-24 16:22:20 +0800 | [diff] [blame] | 564 | trace_ocfs2_remove_block_from_cache( |
| 565 | (unsigned long long)ocfs2_metadata_cache_owner(ci), |
| 566 | (unsigned long long) block, ci->ci_num_cached, |
| 567 | ci->ci_flags); |
Mark Fasheh | ccd979b | 2005-12-15 14:31:24 -0800 | [diff] [blame] | 568 | |
Joel Becker | 47460d6 | 2009-02-10 16:05:07 -0800 | [diff] [blame] | 569 | if (ci->ci_flags & OCFS2_CACHE_FL_INLINE) { |
Mark Fasheh | ccd979b | 2005-12-15 14:31:24 -0800 | [diff] [blame] | 570 | index = ocfs2_search_cache_array(ci, block); |
| 571 | if (index != -1) |
| 572 | ocfs2_remove_metadata_array(ci, index); |
| 573 | } else { |
| 574 | item = ocfs2_search_cache_tree(ci, block); |
| 575 | if (item) |
| 576 | ocfs2_remove_metadata_tree(ci, item); |
| 577 | } |
Joel Becker | 6e5a3d7 | 2009-02-10 19:00:37 -0800 | [diff] [blame] | 578 | ocfs2_metadata_cache_unlock(ci); |
Mark Fasheh | ccd979b | 2005-12-15 14:31:24 -0800 | [diff] [blame] | 579 | |
| 580 | if (item) |
| 581 | kmem_cache_free(ocfs2_uptodate_cachep, item); |
| 582 | } |
| 583 | |
Tao Ma | ac11c82 | 2008-08-18 17:38:47 +0800 | [diff] [blame] | 584 | /* |
| 585 | * Called when we remove a chunk of metadata from an inode. We don't |
| 586 | * bother reverting things to an inlined array in the case of a remove |
| 587 | * which moves us back under the limit. |
| 588 | */ |
Joel Becker | 8cb471e | 2009-02-10 20:00:41 -0800 | [diff] [blame] | 589 | void ocfs2_remove_from_cache(struct ocfs2_caching_info *ci, |
Tao Ma | ac11c82 | 2008-08-18 17:38:47 +0800 | [diff] [blame] | 590 | struct buffer_head *bh) |
| 591 | { |
| 592 | sector_t block = bh->b_blocknr; |
| 593 | |
Joel Becker | 8cb471e | 2009-02-10 20:00:41 -0800 | [diff] [blame] | 594 | ocfs2_remove_block_from_cache(ci, block); |
Tao Ma | ac11c82 | 2008-08-18 17:38:47 +0800 | [diff] [blame] | 595 | } |
| 596 | |
| 597 | /* Called when we remove xattr clusters from an inode. */ |
Joel Becker | 8cb471e | 2009-02-10 20:00:41 -0800 | [diff] [blame] | 598 | void ocfs2_remove_xattr_clusters_from_cache(struct ocfs2_caching_info *ci, |
Tao Ma | ac11c82 | 2008-08-18 17:38:47 +0800 | [diff] [blame] | 599 | sector_t block, |
| 600 | u32 c_len) |
| 601 | { |
Joel Becker | 8cb471e | 2009-02-10 20:00:41 -0800 | [diff] [blame] | 602 | struct super_block *sb = ocfs2_metadata_cache_get_super(ci); |
| 603 | unsigned int i, b_len = ocfs2_clusters_to_blocks(sb, 1) * c_len; |
Tao Ma | ac11c82 | 2008-08-18 17:38:47 +0800 | [diff] [blame] | 604 | |
| 605 | for (i = 0; i < b_len; i++, block++) |
Joel Becker | 8cb471e | 2009-02-10 20:00:41 -0800 | [diff] [blame] | 606 | ocfs2_remove_block_from_cache(ci, block); |
Tao Ma | ac11c82 | 2008-08-18 17:38:47 +0800 | [diff] [blame] | 607 | } |
| 608 | |
Mark Fasheh | ccd979b | 2005-12-15 14:31:24 -0800 | [diff] [blame] | 609 | int __init init_ocfs2_uptodate_cache(void) |
| 610 | { |
| 611 | ocfs2_uptodate_cachep = kmem_cache_create("ocfs2_uptodate", |
| 612 | sizeof(struct ocfs2_meta_cache_item), |
Paul Mundt | 20c2df8 | 2007-07-20 10:11:58 +0900 | [diff] [blame] | 613 | 0, SLAB_HWCACHE_ALIGN, NULL); |
Mark Fasheh | ccd979b | 2005-12-15 14:31:24 -0800 | [diff] [blame] | 614 | if (!ocfs2_uptodate_cachep) |
| 615 | return -ENOMEM; |
| 616 | |
Mark Fasheh | ccd979b | 2005-12-15 14:31:24 -0800 | [diff] [blame] | 617 | return 0; |
| 618 | } |
| 619 | |
Adrian Bunk | 0c6c98f | 2006-01-07 20:07:02 +0100 | [diff] [blame] | 620 | void exit_ocfs2_uptodate_cache(void) |
Mark Fasheh | ccd979b | 2005-12-15 14:31:24 -0800 | [diff] [blame] | 621 | { |
piaojun | a17b485 | 2018-04-05 16:18:52 -0700 | [diff] [blame] | 622 | kmem_cache_destroy(ocfs2_uptodate_cachep); |
Mark Fasheh | ccd979b | 2005-12-15 14:31:24 -0800 | [diff] [blame] | 623 | } |