Mark Fasheh | ccd979b | 2005-12-15 14:31:24 -0800 | [diff] [blame] | 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 | |
| 50 | #include <linux/fs.h> |
| 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> |
Joel Becker | 2b4e30f | 2008-09-03 20:03:41 -0700 | [diff] [blame] | 56 | #ifndef CONFIG_OCFS2_COMPAT_JBD |
| 57 | # include <linux/jbd2.h> |
| 58 | #else |
| 59 | # include <linux/jbd.h> |
| 60 | #endif |
Mark Fasheh | ccd979b | 2005-12-15 14:31:24 -0800 | [diff] [blame] | 61 | |
| 62 | #define MLOG_MASK_PREFIX ML_UPTODATE |
| 63 | |
| 64 | #include <cluster/masklog.h> |
| 65 | |
| 66 | #include "ocfs2.h" |
| 67 | |
| 68 | #include "inode.h" |
| 69 | #include "uptodate.h" |
| 70 | |
| 71 | struct ocfs2_meta_cache_item { |
| 72 | struct rb_node c_node; |
| 73 | sector_t c_block; |
| 74 | }; |
| 75 | |
Christoph Lameter | e18b890 | 2006-12-06 20:33:20 -0800 | [diff] [blame] | 76 | static struct kmem_cache *ocfs2_uptodate_cachep = NULL; |
Mark Fasheh | ccd979b | 2005-12-15 14:31:24 -0800 | [diff] [blame] | 77 | |
Joel Becker | 6e5a3d7 | 2009-02-10 19:00:37 -0800 | [diff] [blame^] | 78 | static u64 ocfs2_metadata_cache_owner(struct ocfs2_caching_info *ci) |
Mark Fasheh | ccd979b | 2005-12-15 14:31:24 -0800 | [diff] [blame] | 79 | { |
Joel Becker | 6e5a3d7 | 2009-02-10 19:00:37 -0800 | [diff] [blame^] | 80 | BUG_ON(!ci || !ci->ci_ops); |
| 81 | |
| 82 | return ci->ci_ops->co_owner(ci); |
| 83 | } |
| 84 | |
| 85 | static void ocfs2_metadata_cache_lock(struct ocfs2_caching_info *ci) |
| 86 | { |
| 87 | BUG_ON(!ci || !ci->ci_ops); |
| 88 | |
| 89 | ci->ci_ops->co_cache_lock(ci); |
| 90 | } |
| 91 | |
| 92 | static void ocfs2_metadata_cache_unlock(struct ocfs2_caching_info *ci) |
| 93 | { |
| 94 | BUG_ON(!ci || !ci->ci_ops); |
| 95 | |
| 96 | ci->ci_ops->co_cache_unlock(ci); |
| 97 | } |
| 98 | |
| 99 | static void ocfs2_metadata_cache_io_lock(struct ocfs2_caching_info *ci) |
| 100 | { |
| 101 | BUG_ON(!ci || !ci->ci_ops); |
| 102 | |
| 103 | ci->ci_ops->co_io_lock(ci); |
| 104 | } |
| 105 | |
| 106 | static void ocfs2_metadata_cache_io_unlock(struct ocfs2_caching_info *ci) |
| 107 | { |
| 108 | BUG_ON(!ci || !ci->ci_ops); |
| 109 | |
| 110 | ci->ci_ops->co_io_unlock(ci); |
| 111 | } |
| 112 | |
| 113 | |
| 114 | void ocfs2_metadata_cache_init(struct ocfs2_caching_info *ci, |
| 115 | const struct ocfs2_caching_operations *ops) |
| 116 | { |
| 117 | BUG_ON(!ops); |
| 118 | |
| 119 | ci->ci_ops = ops; |
Joel Becker | 47460d6 | 2009-02-10 16:05:07 -0800 | [diff] [blame] | 120 | ci->ci_flags |= OCFS2_CACHE_FL_INLINE; |
Mark Fasheh | ccd979b | 2005-12-15 14:31:24 -0800 | [diff] [blame] | 121 | ci->ci_num_cached = 0; |
| 122 | } |
| 123 | |
| 124 | /* No lock taken here as 'root' is not expected to be visible to other |
| 125 | * processes. */ |
| 126 | static unsigned int ocfs2_purge_copied_metadata_tree(struct rb_root *root) |
| 127 | { |
| 128 | unsigned int purged = 0; |
| 129 | struct rb_node *node; |
| 130 | struct ocfs2_meta_cache_item *item; |
| 131 | |
| 132 | while ((node = rb_last(root)) != NULL) { |
| 133 | item = rb_entry(node, struct ocfs2_meta_cache_item, c_node); |
| 134 | |
| 135 | mlog(0, "Purge item %llu\n", |
| 136 | (unsigned long long) item->c_block); |
| 137 | |
| 138 | rb_erase(&item->c_node, root); |
| 139 | kmem_cache_free(ocfs2_uptodate_cachep, item); |
| 140 | |
| 141 | purged++; |
| 142 | } |
| 143 | return purged; |
| 144 | } |
| 145 | |
| 146 | /* Called from locking and called from ocfs2_clear_inode. Dump the |
| 147 | * cache for a given inode. |
| 148 | * |
| 149 | * This function is a few more lines longer than necessary due to some |
| 150 | * accounting done here, but I think it's worth tracking down those |
| 151 | * bugs sooner -- Mark */ |
| 152 | void ocfs2_metadata_cache_purge(struct inode *inode) |
| 153 | { |
| 154 | struct ocfs2_inode_info *oi = OCFS2_I(inode); |
| 155 | unsigned int tree, to_purge, purged; |
| 156 | struct ocfs2_caching_info *ci = &oi->ip_metadata_cache; |
| 157 | struct rb_root root = RB_ROOT; |
| 158 | |
Joel Becker | 6e5a3d7 | 2009-02-10 19:00:37 -0800 | [diff] [blame^] | 159 | BUG_ON(!ci || !ci->ci_ops); |
| 160 | |
| 161 | ocfs2_metadata_cache_lock(ci); |
Joel Becker | 47460d6 | 2009-02-10 16:05:07 -0800 | [diff] [blame] | 162 | tree = !(ci->ci_flags & OCFS2_CACHE_FL_INLINE); |
Mark Fasheh | ccd979b | 2005-12-15 14:31:24 -0800 | [diff] [blame] | 163 | to_purge = ci->ci_num_cached; |
| 164 | |
Joel Becker | 6e5a3d7 | 2009-02-10 19:00:37 -0800 | [diff] [blame^] | 165 | mlog(0, "Purge %u %s items from Owner %llu\n", to_purge, |
| 166 | tree ? "array" : "tree", |
| 167 | (unsigned long long)ocfs2_metadata_cache_owner(ci)); |
Mark Fasheh | ccd979b | 2005-12-15 14:31:24 -0800 | [diff] [blame] | 168 | |
| 169 | /* If we're a tree, save off the root so that we can safely |
| 170 | * initialize the cache. We do the work to free tree members |
| 171 | * without the spinlock. */ |
| 172 | if (tree) |
| 173 | root = ci->ci_cache.ci_tree; |
| 174 | |
Joel Becker | 6e5a3d7 | 2009-02-10 19:00:37 -0800 | [diff] [blame^] | 175 | ocfs2_metadata_cache_init(ci, ci->ci_ops); |
| 176 | ocfs2_metadata_cache_unlock(ci); |
Mark Fasheh | ccd979b | 2005-12-15 14:31:24 -0800 | [diff] [blame] | 177 | |
| 178 | purged = ocfs2_purge_copied_metadata_tree(&root); |
| 179 | /* If possible, track the number wiped so that we can more |
| 180 | * easily detect counting errors. Unfortunately, this is only |
| 181 | * meaningful for trees. */ |
| 182 | if (tree && purged != to_purge) |
Joel Becker | 6e5a3d7 | 2009-02-10 19:00:37 -0800 | [diff] [blame^] | 183 | mlog(ML_ERROR, "Owner %llu, count = %u, purged = %u\n", |
| 184 | (unsigned long long)ocfs2_metadata_cache_owner(ci), |
| 185 | to_purge, purged); |
Mark Fasheh | ccd979b | 2005-12-15 14:31:24 -0800 | [diff] [blame] | 186 | } |
| 187 | |
| 188 | /* Returns the index in the cache array, -1 if not found. |
| 189 | * Requires ip_lock. */ |
| 190 | static int ocfs2_search_cache_array(struct ocfs2_caching_info *ci, |
| 191 | sector_t item) |
| 192 | { |
| 193 | int i; |
| 194 | |
| 195 | for (i = 0; i < ci->ci_num_cached; i++) { |
| 196 | if (item == ci->ci_cache.ci_array[i]) |
| 197 | return i; |
| 198 | } |
| 199 | |
| 200 | return -1; |
| 201 | } |
| 202 | |
| 203 | /* Returns the cache item if found, otherwise NULL. |
| 204 | * Requires ip_lock. */ |
| 205 | static struct ocfs2_meta_cache_item * |
| 206 | ocfs2_search_cache_tree(struct ocfs2_caching_info *ci, |
| 207 | sector_t block) |
| 208 | { |
| 209 | struct rb_node * n = ci->ci_cache.ci_tree.rb_node; |
| 210 | struct ocfs2_meta_cache_item *item = NULL; |
| 211 | |
| 212 | while (n) { |
| 213 | item = rb_entry(n, struct ocfs2_meta_cache_item, c_node); |
| 214 | |
| 215 | if (block < item->c_block) |
| 216 | n = n->rb_left; |
| 217 | else if (block > item->c_block) |
| 218 | n = n->rb_right; |
| 219 | else |
| 220 | return item; |
| 221 | } |
| 222 | |
| 223 | return NULL; |
| 224 | } |
| 225 | |
| 226 | static int ocfs2_buffer_cached(struct ocfs2_inode_info *oi, |
| 227 | struct buffer_head *bh) |
| 228 | { |
| 229 | int index = -1; |
| 230 | struct ocfs2_meta_cache_item *item = NULL; |
Joel Becker | 47460d6 | 2009-02-10 16:05:07 -0800 | [diff] [blame] | 231 | struct ocfs2_caching_info *ci = &oi->ip_metadata_cache; |
Mark Fasheh | ccd979b | 2005-12-15 14:31:24 -0800 | [diff] [blame] | 232 | |
Joel Becker | 6e5a3d7 | 2009-02-10 19:00:37 -0800 | [diff] [blame^] | 233 | ocfs2_metadata_cache_lock(ci); |
Mark Fasheh | ccd979b | 2005-12-15 14:31:24 -0800 | [diff] [blame] | 234 | |
Joel Becker | 6e5a3d7 | 2009-02-10 19:00:37 -0800 | [diff] [blame^] | 235 | mlog(0, "Owner %llu, query block %llu (inline = %u)\n", |
| 236 | (unsigned long long)ocfs2_metadata_cache_owner(ci), |
Mark Fasheh | b069705 | 2006-03-03 10:24:33 -0800 | [diff] [blame] | 237 | (unsigned long long) bh->b_blocknr, |
Joel Becker | 47460d6 | 2009-02-10 16:05:07 -0800 | [diff] [blame] | 238 | !!(ci->ci_flags & OCFS2_CACHE_FL_INLINE)); |
Mark Fasheh | ccd979b | 2005-12-15 14:31:24 -0800 | [diff] [blame] | 239 | |
Joel Becker | 47460d6 | 2009-02-10 16:05:07 -0800 | [diff] [blame] | 240 | if (ci->ci_flags & OCFS2_CACHE_FL_INLINE) |
Mark Fasheh | ccd979b | 2005-12-15 14:31:24 -0800 | [diff] [blame] | 241 | index = ocfs2_search_cache_array(&oi->ip_metadata_cache, |
| 242 | bh->b_blocknr); |
| 243 | else |
| 244 | item = ocfs2_search_cache_tree(&oi->ip_metadata_cache, |
| 245 | bh->b_blocknr); |
| 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 | |
| 249 | mlog(0, "index = %d, item = %p\n", index, item); |
| 250 | |
| 251 | return (index != -1) || (item != NULL); |
| 252 | } |
| 253 | |
| 254 | /* Warning: even if it returns true, this does *not* guarantee that |
Mark Fasheh | aa95887 | 2006-04-21 13:49:02 -0700 | [diff] [blame] | 255 | * the block is stored in our inode metadata cache. |
| 256 | * |
| 257 | * This can be called under lock_buffer() |
| 258 | */ |
Mark Fasheh | ccd979b | 2005-12-15 14:31:24 -0800 | [diff] [blame] | 259 | int ocfs2_buffer_uptodate(struct inode *inode, |
| 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. */ |
| 275 | return ocfs2_buffer_cached(OCFS2_I(inode), bh); |
| 276 | } |
| 277 | |
Mark Fasheh | aa95887 | 2006-04-21 13:49:02 -0700 | [diff] [blame] | 278 | /* |
| 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 | */ |
| 282 | int ocfs2_buffer_read_ahead(struct inode *inode, |
| 283 | struct buffer_head *bh) |
| 284 | { |
| 285 | return buffer_locked(bh) && ocfs2_buffer_cached(OCFS2_I(inode), bh); |
| 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 | |
| 294 | mlog(0, "block %llu takes position %u\n", (unsigned long long) block, |
| 295 | ci->ci_num_cached); |
| 296 | |
| 297 | ci->ci_cache.ci_array[ci->ci_num_cached] = block; |
| 298 | ci->ci_num_cached++; |
| 299 | } |
| 300 | |
| 301 | /* By now the caller should have checked that the item does *not* |
| 302 | * exist in the tree. |
| 303 | * Requires ip_lock. */ |
| 304 | static void __ocfs2_insert_cache_tree(struct ocfs2_caching_info *ci, |
| 305 | struct ocfs2_meta_cache_item *new) |
| 306 | { |
| 307 | sector_t block = new->c_block; |
| 308 | struct rb_node *parent = NULL; |
| 309 | struct rb_node **p = &ci->ci_cache.ci_tree.rb_node; |
| 310 | struct ocfs2_meta_cache_item *tmp; |
| 311 | |
| 312 | mlog(0, "Insert block %llu num = %u\n", (unsigned long long) block, |
| 313 | ci->ci_num_cached); |
| 314 | |
| 315 | while(*p) { |
| 316 | parent = *p; |
| 317 | |
| 318 | tmp = rb_entry(parent, struct ocfs2_meta_cache_item, c_node); |
| 319 | |
| 320 | if (block < tmp->c_block) |
| 321 | p = &(*p)->rb_left; |
| 322 | else if (block > tmp->c_block) |
| 323 | p = &(*p)->rb_right; |
| 324 | else { |
| 325 | /* This should never happen! */ |
| 326 | mlog(ML_ERROR, "Duplicate block %llu cached!\n", |
| 327 | (unsigned long long) block); |
| 328 | BUG(); |
| 329 | } |
| 330 | } |
| 331 | |
| 332 | rb_link_node(&new->c_node, parent, p); |
| 333 | rb_insert_color(&new->c_node, &ci->ci_cache.ci_tree); |
| 334 | ci->ci_num_cached++; |
| 335 | } |
| 336 | |
Joel Becker | 6e5a3d7 | 2009-02-10 19:00:37 -0800 | [diff] [blame^] | 337 | /* co_cache_lock() must be held */ |
Mark Fasheh | ccd979b | 2005-12-15 14:31:24 -0800 | [diff] [blame] | 338 | static inline int ocfs2_insert_can_use_array(struct ocfs2_inode_info *oi, |
| 339 | struct ocfs2_caching_info *ci) |
| 340 | { |
Joel Becker | 47460d6 | 2009-02-10 16:05:07 -0800 | [diff] [blame] | 341 | return (ci->ci_flags & OCFS2_CACHE_FL_INLINE) && |
| 342 | (ci->ci_num_cached < OCFS2_CACHE_INFO_MAX_ARRAY); |
Mark Fasheh | ccd979b | 2005-12-15 14:31:24 -0800 | [diff] [blame] | 343 | } |
| 344 | |
Joel Becker | 47460d6 | 2009-02-10 16:05:07 -0800 | [diff] [blame] | 345 | /* tree should be exactly OCFS2_CACHE_INFO_MAX_ARRAY wide. NULL the |
Mark Fasheh | ccd979b | 2005-12-15 14:31:24 -0800 | [diff] [blame] | 346 | * pointers in tree after we use them - this allows caller to detect |
Joel Becker | 6e5a3d7 | 2009-02-10 19:00:37 -0800 | [diff] [blame^] | 347 | * when to free in case of error. |
| 348 | * |
| 349 | * The co_cache_lock() must be held. */ |
Mark Fasheh | ccd979b | 2005-12-15 14:31:24 -0800 | [diff] [blame] | 350 | static void ocfs2_expand_cache(struct ocfs2_inode_info *oi, |
| 351 | struct ocfs2_meta_cache_item **tree) |
| 352 | { |
| 353 | int i; |
| 354 | struct ocfs2_caching_info *ci = &oi->ip_metadata_cache; |
| 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 | |
Mark Fasheh | b069705 | 2006-03-03 10:24:33 -0800 | [diff] [blame] | 379 | mlog(0, "Expanded %llu to a tree cache: flags 0x%x, num = %u\n", |
Joel Becker | 6e5a3d7 | 2009-02-10 19:00:37 -0800 | [diff] [blame^] | 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. */ |
| 386 | static void __ocfs2_set_buffer_uptodate(struct ocfs2_inode_info *oi, |
| 387 | sector_t block, |
| 388 | int expand_tree) |
| 389 | { |
| 390 | int i; |
| 391 | struct ocfs2_caching_info *ci = &oi->ip_metadata_cache; |
| 392 | struct ocfs2_meta_cache_item *new = NULL; |
Joel Becker | 47460d6 | 2009-02-10 16:05:07 -0800 | [diff] [blame] | 393 | struct ocfs2_meta_cache_item *tree[OCFS2_CACHE_INFO_MAX_ARRAY] = |
Mark Fasheh | ccd979b | 2005-12-15 14:31:24 -0800 | [diff] [blame] | 394 | { NULL, }; |
| 395 | |
Joel Becker | 6e5a3d7 | 2009-02-10 19:00:37 -0800 | [diff] [blame^] | 396 | mlog(0, "Owner %llu, block %llu, expand = %d\n", |
| 397 | (unsigned long long)ocfs2_metadata_cache_owner(ci), |
Mark Fasheh | b069705 | 2006-03-03 10:24:33 -0800 | [diff] [blame] | 398 | (unsigned long long)block, expand_tree); |
Mark Fasheh | ccd979b | 2005-12-15 14:31:24 -0800 | [diff] [blame] | 399 | |
Sunil Mushran | afae00ab | 2006-04-12 14:37:00 -0700 | [diff] [blame] | 400 | new = kmem_cache_alloc(ocfs2_uptodate_cachep, GFP_NOFS); |
Mark Fasheh | ccd979b | 2005-12-15 14:31:24 -0800 | [diff] [blame] | 401 | if (!new) { |
| 402 | mlog_errno(-ENOMEM); |
| 403 | return; |
| 404 | } |
| 405 | new->c_block = block; |
| 406 | |
| 407 | if (expand_tree) { |
| 408 | /* Do *not* allocate an array here - the removal code |
| 409 | * has no way of tracking that. */ |
Joel Becker | 47460d6 | 2009-02-10 16:05:07 -0800 | [diff] [blame] | 410 | for (i = 0; i < OCFS2_CACHE_INFO_MAX_ARRAY; i++) { |
Mark Fasheh | ccd979b | 2005-12-15 14:31:24 -0800 | [diff] [blame] | 411 | tree[i] = kmem_cache_alloc(ocfs2_uptodate_cachep, |
Sunil Mushran | afae00ab | 2006-04-12 14:37:00 -0700 | [diff] [blame] | 412 | GFP_NOFS); |
Mark Fasheh | ccd979b | 2005-12-15 14:31:24 -0800 | [diff] [blame] | 413 | if (!tree[i]) { |
| 414 | mlog_errno(-ENOMEM); |
| 415 | goto out_free; |
| 416 | } |
| 417 | |
| 418 | /* These are initialized in ocfs2_expand_cache! */ |
| 419 | } |
| 420 | } |
| 421 | |
Joel Becker | 6e5a3d7 | 2009-02-10 19:00:37 -0800 | [diff] [blame^] | 422 | ocfs2_metadata_cache_lock(ci); |
Mark Fasheh | ccd979b | 2005-12-15 14:31:24 -0800 | [diff] [blame] | 423 | if (ocfs2_insert_can_use_array(oi, ci)) { |
| 424 | mlog(0, "Someone cleared the tree underneath us\n"); |
| 425 | /* Ok, items were removed from the cache in between |
| 426 | * locks. Detect this and revert back to the fast path */ |
| 427 | ocfs2_append_cache_array(ci, block); |
Joel Becker | 6e5a3d7 | 2009-02-10 19:00:37 -0800 | [diff] [blame^] | 428 | ocfs2_metadata_cache_unlock(ci); |
Mark Fasheh | ccd979b | 2005-12-15 14:31:24 -0800 | [diff] [blame] | 429 | goto out_free; |
| 430 | } |
| 431 | |
| 432 | if (expand_tree) |
| 433 | ocfs2_expand_cache(oi, tree); |
| 434 | |
| 435 | __ocfs2_insert_cache_tree(ci, new); |
Joel Becker | 6e5a3d7 | 2009-02-10 19:00:37 -0800 | [diff] [blame^] | 436 | ocfs2_metadata_cache_unlock(ci); |
Mark Fasheh | ccd979b | 2005-12-15 14:31:24 -0800 | [diff] [blame] | 437 | |
| 438 | new = NULL; |
| 439 | out_free: |
| 440 | if (new) |
| 441 | kmem_cache_free(ocfs2_uptodate_cachep, new); |
| 442 | |
| 443 | /* If these were used, then ocfs2_expand_cache re-set them to |
| 444 | * NULL for us. */ |
| 445 | if (tree[0]) { |
Joel Becker | 47460d6 | 2009-02-10 16:05:07 -0800 | [diff] [blame] | 446 | for (i = 0; i < OCFS2_CACHE_INFO_MAX_ARRAY; i++) |
Mark Fasheh | ccd979b | 2005-12-15 14:31:24 -0800 | [diff] [blame] | 447 | if (tree[i]) |
| 448 | kmem_cache_free(ocfs2_uptodate_cachep, |
| 449 | tree[i]); |
| 450 | } |
| 451 | } |
| 452 | |
Joel Becker | 6e5a3d7 | 2009-02-10 19:00:37 -0800 | [diff] [blame^] | 453 | /* 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] | 454 | * advantage of this by not rechecking for a duplicate insert during |
| 455 | * the slow case. Additionally, if the cache needs to be bumped up to |
| 456 | * a tree, the code will not recheck after acquiring the lock -- |
| 457 | * multiple paths cannot be expanding to a tree at the same time. |
| 458 | * |
| 459 | * The slow path takes into account that items can be removed |
| 460 | * (including the whole tree wiped and reset) when this process it out |
| 461 | * allocating memory. In those cases, it reverts back to the fast |
| 462 | * path. |
| 463 | * |
| 464 | * Note that this function may actually fail to insert the block if |
| 465 | * memory cannot be allocated. This is not fatal however (but may |
Mark Fasheh | aa95887 | 2006-04-21 13:49:02 -0700 | [diff] [blame] | 466 | * result in a performance penalty) |
| 467 | * |
| 468 | * Readahead buffers can be passed in here before the I/O request is |
| 469 | * completed. |
| 470 | */ |
Mark Fasheh | ccd979b | 2005-12-15 14:31:24 -0800 | [diff] [blame] | 471 | void ocfs2_set_buffer_uptodate(struct inode *inode, |
| 472 | struct buffer_head *bh) |
| 473 | { |
| 474 | int expand; |
| 475 | struct ocfs2_inode_info *oi = OCFS2_I(inode); |
| 476 | struct ocfs2_caching_info *ci = &oi->ip_metadata_cache; |
| 477 | |
| 478 | /* The block may very well exist in our cache already, so avoid |
| 479 | * doing any more work in that case. */ |
| 480 | if (ocfs2_buffer_cached(oi, bh)) |
| 481 | return; |
| 482 | |
Joel Becker | 6e5a3d7 | 2009-02-10 19:00:37 -0800 | [diff] [blame^] | 483 | mlog(0, "Owner %llu, inserting block %llu\n", |
| 484 | (unsigned long long)ocfs2_metadata_cache_owner(ci), |
Mark Fasheh | b069705 | 2006-03-03 10:24:33 -0800 | [diff] [blame] | 485 | (unsigned long long)bh->b_blocknr); |
Mark Fasheh | ccd979b | 2005-12-15 14:31:24 -0800 | [diff] [blame] | 486 | |
| 487 | /* No need to recheck under spinlock - insertion is guarded by |
Joel Becker | 6e5a3d7 | 2009-02-10 19:00:37 -0800 | [diff] [blame^] | 488 | * co_io_lock() */ |
| 489 | ocfs2_metadata_cache_lock(ci); |
Mark Fasheh | ccd979b | 2005-12-15 14:31:24 -0800 | [diff] [blame] | 490 | if (ocfs2_insert_can_use_array(oi, ci)) { |
| 491 | /* Fast case - it's an array and there's a free |
| 492 | * spot. */ |
| 493 | ocfs2_append_cache_array(ci, bh->b_blocknr); |
Joel Becker | 6e5a3d7 | 2009-02-10 19:00:37 -0800 | [diff] [blame^] | 494 | ocfs2_metadata_cache_unlock(ci); |
Mark Fasheh | ccd979b | 2005-12-15 14:31:24 -0800 | [diff] [blame] | 495 | return; |
| 496 | } |
| 497 | |
| 498 | expand = 0; |
Joel Becker | 47460d6 | 2009-02-10 16:05:07 -0800 | [diff] [blame] | 499 | if (ci->ci_flags & OCFS2_CACHE_FL_INLINE) { |
Mark Fasheh | ccd979b | 2005-12-15 14:31:24 -0800 | [diff] [blame] | 500 | /* We need to bump things up to a tree. */ |
| 501 | expand = 1; |
| 502 | } |
Joel Becker | 6e5a3d7 | 2009-02-10 19:00:37 -0800 | [diff] [blame^] | 503 | ocfs2_metadata_cache_unlock(ci); |
Mark Fasheh | ccd979b | 2005-12-15 14:31:24 -0800 | [diff] [blame] | 504 | |
| 505 | __ocfs2_set_buffer_uptodate(oi, bh->b_blocknr, expand); |
| 506 | } |
| 507 | |
| 508 | /* Called against a newly allocated buffer. Most likely nobody should |
| 509 | * 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^] | 510 | * allocated, but this is careful to take co_io_lock() anyway. */ |
Mark Fasheh | ccd979b | 2005-12-15 14:31:24 -0800 | [diff] [blame] | 511 | void ocfs2_set_new_buffer_uptodate(struct inode *inode, |
| 512 | struct buffer_head *bh) |
| 513 | { |
| 514 | struct ocfs2_inode_info *oi = OCFS2_I(inode); |
Joel Becker | 47460d6 | 2009-02-10 16:05:07 -0800 | [diff] [blame] | 515 | struct ocfs2_caching_info *ci = &oi->ip_metadata_cache; |
Mark Fasheh | ccd979b | 2005-12-15 14:31:24 -0800 | [diff] [blame] | 516 | |
| 517 | /* This should definitely *not* exist in our cache */ |
| 518 | BUG_ON(ocfs2_buffer_cached(oi, bh)); |
| 519 | |
| 520 | set_buffer_uptodate(bh); |
| 521 | |
Joel Becker | 6e5a3d7 | 2009-02-10 19:00:37 -0800 | [diff] [blame^] | 522 | ocfs2_metadata_cache_io_lock(ci); |
Mark Fasheh | ccd979b | 2005-12-15 14:31:24 -0800 | [diff] [blame] | 523 | ocfs2_set_buffer_uptodate(inode, bh); |
Joel Becker | 6e5a3d7 | 2009-02-10 19:00:37 -0800 | [diff] [blame^] | 524 | ocfs2_metadata_cache_io_unlock(ci); |
Mark Fasheh | ccd979b | 2005-12-15 14:31:24 -0800 | [diff] [blame] | 525 | } |
| 526 | |
| 527 | /* Requires ip_lock. */ |
| 528 | static void ocfs2_remove_metadata_array(struct ocfs2_caching_info *ci, |
| 529 | int index) |
| 530 | { |
| 531 | sector_t *array = ci->ci_cache.ci_array; |
| 532 | int bytes; |
| 533 | |
Joel Becker | 47460d6 | 2009-02-10 16:05:07 -0800 | [diff] [blame] | 534 | BUG_ON(index < 0 || index >= OCFS2_CACHE_INFO_MAX_ARRAY); |
Mark Fasheh | ccd979b | 2005-12-15 14:31:24 -0800 | [diff] [blame] | 535 | BUG_ON(index >= ci->ci_num_cached); |
| 536 | BUG_ON(!ci->ci_num_cached); |
| 537 | |
| 538 | mlog(0, "remove index %d (num_cached = %u\n", index, |
| 539 | ci->ci_num_cached); |
| 540 | |
| 541 | ci->ci_num_cached--; |
| 542 | |
| 543 | /* don't need to copy if the array is now empty, or if we |
| 544 | * removed at the tail */ |
| 545 | if (ci->ci_num_cached && index < ci->ci_num_cached) { |
| 546 | bytes = sizeof(sector_t) * (ci->ci_num_cached - index); |
| 547 | memmove(&array[index], &array[index + 1], bytes); |
| 548 | } |
| 549 | } |
| 550 | |
| 551 | /* Requires ip_lock. */ |
| 552 | static void ocfs2_remove_metadata_tree(struct ocfs2_caching_info *ci, |
| 553 | struct ocfs2_meta_cache_item *item) |
| 554 | { |
| 555 | mlog(0, "remove block %llu from tree\n", |
| 556 | (unsigned long long) item->c_block); |
| 557 | |
| 558 | rb_erase(&item->c_node, &ci->ci_cache.ci_tree); |
| 559 | ci->ci_num_cached--; |
| 560 | } |
| 561 | |
Tao Ma | ac11c82 | 2008-08-18 17:38:47 +0800 | [diff] [blame] | 562 | static void ocfs2_remove_block_from_cache(struct inode *inode, |
| 563 | sector_t block) |
Mark Fasheh | ccd979b | 2005-12-15 14:31:24 -0800 | [diff] [blame] | 564 | { |
| 565 | int index; |
Mark Fasheh | ccd979b | 2005-12-15 14:31:24 -0800 | [diff] [blame] | 566 | struct ocfs2_meta_cache_item *item = NULL; |
| 567 | struct ocfs2_inode_info *oi = OCFS2_I(inode); |
| 568 | struct ocfs2_caching_info *ci = &oi->ip_metadata_cache; |
| 569 | |
Joel Becker | 6e5a3d7 | 2009-02-10 19:00:37 -0800 | [diff] [blame^] | 570 | ocfs2_metadata_cache_lock(ci); |
| 571 | mlog(0, "Owner %llu, remove %llu, items = %u, array = %u\n", |
| 572 | (unsigned long long)ocfs2_metadata_cache_owner(ci), |
Mark Fasheh | b069705 | 2006-03-03 10:24:33 -0800 | [diff] [blame] | 573 | (unsigned long long) block, ci->ci_num_cached, |
Joel Becker | 47460d6 | 2009-02-10 16:05:07 -0800 | [diff] [blame] | 574 | ci->ci_flags & OCFS2_CACHE_FL_INLINE); |
Mark Fasheh | ccd979b | 2005-12-15 14:31:24 -0800 | [diff] [blame] | 575 | |
Joel Becker | 47460d6 | 2009-02-10 16:05:07 -0800 | [diff] [blame] | 576 | if (ci->ci_flags & OCFS2_CACHE_FL_INLINE) { |
Mark Fasheh | ccd979b | 2005-12-15 14:31:24 -0800 | [diff] [blame] | 577 | index = ocfs2_search_cache_array(ci, block); |
| 578 | if (index != -1) |
| 579 | ocfs2_remove_metadata_array(ci, index); |
| 580 | } else { |
| 581 | item = ocfs2_search_cache_tree(ci, block); |
| 582 | if (item) |
| 583 | ocfs2_remove_metadata_tree(ci, item); |
| 584 | } |
Joel Becker | 6e5a3d7 | 2009-02-10 19:00:37 -0800 | [diff] [blame^] | 585 | ocfs2_metadata_cache_unlock(ci); |
Mark Fasheh | ccd979b | 2005-12-15 14:31:24 -0800 | [diff] [blame] | 586 | |
| 587 | if (item) |
| 588 | kmem_cache_free(ocfs2_uptodate_cachep, item); |
| 589 | } |
| 590 | |
Tao Ma | ac11c82 | 2008-08-18 17:38:47 +0800 | [diff] [blame] | 591 | /* |
| 592 | * Called when we remove a chunk of metadata from an inode. We don't |
| 593 | * bother reverting things to an inlined array in the case of a remove |
| 594 | * which moves us back under the limit. |
| 595 | */ |
| 596 | void ocfs2_remove_from_cache(struct inode *inode, |
| 597 | struct buffer_head *bh) |
| 598 | { |
| 599 | sector_t block = bh->b_blocknr; |
| 600 | |
| 601 | ocfs2_remove_block_from_cache(inode, block); |
| 602 | } |
| 603 | |
| 604 | /* Called when we remove xattr clusters from an inode. */ |
| 605 | void ocfs2_remove_xattr_clusters_from_cache(struct inode *inode, |
| 606 | sector_t block, |
| 607 | u32 c_len) |
| 608 | { |
Mark Fasheh | fd8351f | 2008-10-07 12:50:46 -0700 | [diff] [blame] | 609 | unsigned int i, b_len = ocfs2_clusters_to_blocks(inode->i_sb, 1) * c_len; |
Tao Ma | ac11c82 | 2008-08-18 17:38:47 +0800 | [diff] [blame] | 610 | |
| 611 | for (i = 0; i < b_len; i++, block++) |
| 612 | ocfs2_remove_block_from_cache(inode, block); |
| 613 | } |
| 614 | |
Mark Fasheh | ccd979b | 2005-12-15 14:31:24 -0800 | [diff] [blame] | 615 | int __init init_ocfs2_uptodate_cache(void) |
| 616 | { |
| 617 | ocfs2_uptodate_cachep = kmem_cache_create("ocfs2_uptodate", |
| 618 | sizeof(struct ocfs2_meta_cache_item), |
Paul Mundt | 20c2df8 | 2007-07-20 10:11:58 +0900 | [diff] [blame] | 619 | 0, SLAB_HWCACHE_ALIGN, NULL); |
Mark Fasheh | ccd979b | 2005-12-15 14:31:24 -0800 | [diff] [blame] | 620 | if (!ocfs2_uptodate_cachep) |
| 621 | return -ENOMEM; |
| 622 | |
| 623 | mlog(0, "%u inlined cache items per inode.\n", |
Joel Becker | 47460d6 | 2009-02-10 16:05:07 -0800 | [diff] [blame] | 624 | OCFS2_CACHE_INFO_MAX_ARRAY); |
Mark Fasheh | ccd979b | 2005-12-15 14:31:24 -0800 | [diff] [blame] | 625 | |
| 626 | return 0; |
| 627 | } |
| 628 | |
Adrian Bunk | 0c6c98f | 2006-01-07 20:07:02 +0100 | [diff] [blame] | 629 | void exit_ocfs2_uptodate_cache(void) |
Mark Fasheh | ccd979b | 2005-12-15 14:31:24 -0800 | [diff] [blame] | 630 | { |
| 631 | if (ocfs2_uptodate_cachep) |
| 632 | kmem_cache_destroy(ocfs2_uptodate_cachep); |
| 633 | } |