blob: a0a120e82b9712fce6d128a4ab536be2b9dd2d0a [file] [log] [blame]
Mark Fashehccd979b2005-12-15 14:31:24 -08001/* -*- 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>
Mark Fashehccd979b2005-12-15 14:31:24 -080056
57#define MLOG_MASK_PREFIX ML_UPTODATE
58
59#include <cluster/masklog.h>
60
61#include "ocfs2.h"
62
63#include "inode.h"
64#include "uptodate.h"
65
66struct ocfs2_meta_cache_item {
67 struct rb_node c_node;
68 sector_t c_block;
69};
70
Christoph Lametere18b8902006-12-06 20:33:20 -080071static struct kmem_cache *ocfs2_uptodate_cachep = NULL;
Mark Fashehccd979b2005-12-15 14:31:24 -080072
Joel Becker8cb471e2009-02-10 20:00:41 -080073u64 ocfs2_metadata_cache_owner(struct ocfs2_caching_info *ci)
Mark Fashehccd979b2005-12-15 14:31:24 -080074{
Joel Becker6e5a3d72009-02-10 19:00:37 -080075 BUG_ON(!ci || !ci->ci_ops);
76
77 return ci->ci_ops->co_owner(ci);
78}
79
Joel Becker8cb471e2009-02-10 20:00:41 -080080struct super_block *ocfs2_metadata_cache_get_super(struct ocfs2_caching_info *ci)
81{
82 BUG_ON(!ci || !ci->ci_ops);
83
84 return ci->ci_ops->co_get_super(ci);
85}
86
Joel Becker6e5a3d72009-02-10 19:00:37 -080087static void ocfs2_metadata_cache_lock(struct ocfs2_caching_info *ci)
88{
89 BUG_ON(!ci || !ci->ci_ops);
90
91 ci->ci_ops->co_cache_lock(ci);
92}
93
94static void ocfs2_metadata_cache_unlock(struct ocfs2_caching_info *ci)
95{
96 BUG_ON(!ci || !ci->ci_ops);
97
98 ci->ci_ops->co_cache_unlock(ci);
99}
100
Joel Becker8cb471e2009-02-10 20:00:41 -0800101void ocfs2_metadata_cache_io_lock(struct ocfs2_caching_info *ci)
Joel Becker6e5a3d72009-02-10 19:00:37 -0800102{
103 BUG_ON(!ci || !ci->ci_ops);
104
105 ci->ci_ops->co_io_lock(ci);
106}
107
Joel Becker8cb471e2009-02-10 20:00:41 -0800108void ocfs2_metadata_cache_io_unlock(struct ocfs2_caching_info *ci)
Joel Becker6e5a3d72009-02-10 19:00:37 -0800109{
110 BUG_ON(!ci || !ci->ci_ops);
111
112 ci->ci_ops->co_io_unlock(ci);
113}
114
115
Joel Becker66fb3452009-02-12 15:24:40 -0800116static void ocfs2_metadata_cache_reset(struct ocfs2_caching_info *ci,
117 int clear)
118{
119 ci->ci_flags |= OCFS2_CACHE_FL_INLINE;
120 ci->ci_num_cached = 0;
121
Joel Becker292dd272009-02-12 15:41:59 -0800122 if (clear) {
123 ci->ci_created_trans = 0;
Joel Becker66fb3452009-02-12 15:24:40 -0800124 ci->ci_last_trans = 0;
Joel Becker292dd272009-02-12 15:41:59 -0800125 }
Joel Becker66fb3452009-02-12 15:24:40 -0800126}
127
Joel Becker6e5a3d72009-02-10 19:00:37 -0800128void ocfs2_metadata_cache_init(struct ocfs2_caching_info *ci,
129 const struct ocfs2_caching_operations *ops)
130{
131 BUG_ON(!ops);
132
133 ci->ci_ops = ops;
Joel Becker66fb3452009-02-12 15:24:40 -0800134 ocfs2_metadata_cache_reset(ci, 1);
Mark Fashehccd979b2005-12-15 14:31:24 -0800135}
136
Joel Becker66fb3452009-02-12 15:24:40 -0800137void ocfs2_metadata_cache_exit(struct ocfs2_caching_info *ci)
138{
139 ocfs2_metadata_cache_purge(ci);
140 ocfs2_metadata_cache_reset(ci, 1);
141}
142
143
Mark Fashehccd979b2005-12-15 14:31:24 -0800144/* No lock taken here as 'root' is not expected to be visible to other
145 * processes. */
146static unsigned int ocfs2_purge_copied_metadata_tree(struct rb_root *root)
147{
148 unsigned int purged = 0;
149 struct rb_node *node;
150 struct ocfs2_meta_cache_item *item;
151
152 while ((node = rb_last(root)) != NULL) {
153 item = rb_entry(node, struct ocfs2_meta_cache_item, c_node);
154
155 mlog(0, "Purge item %llu\n",
156 (unsigned long long) item->c_block);
157
158 rb_erase(&item->c_node, root);
159 kmem_cache_free(ocfs2_uptodate_cachep, item);
160
161 purged++;
162 }
163 return purged;
164}
165
166/* Called from locking and called from ocfs2_clear_inode. Dump the
167 * cache for a given inode.
168 *
169 * This function is a few more lines longer than necessary due to some
170 * accounting done here, but I think it's worth tracking down those
171 * bugs sooner -- Mark */
Joel Becker8cb471e2009-02-10 20:00:41 -0800172void ocfs2_metadata_cache_purge(struct ocfs2_caching_info *ci)
Mark Fashehccd979b2005-12-15 14:31:24 -0800173{
Mark Fashehccd979b2005-12-15 14:31:24 -0800174 unsigned int tree, to_purge, purged;
Mark Fashehccd979b2005-12-15 14:31:24 -0800175 struct rb_root root = RB_ROOT;
176
Joel Becker6e5a3d72009-02-10 19:00:37 -0800177 BUG_ON(!ci || !ci->ci_ops);
178
179 ocfs2_metadata_cache_lock(ci);
Joel Becker47460d62009-02-10 16:05:07 -0800180 tree = !(ci->ci_flags & OCFS2_CACHE_FL_INLINE);
Mark Fashehccd979b2005-12-15 14:31:24 -0800181 to_purge = ci->ci_num_cached;
182
Joel Becker6e5a3d72009-02-10 19:00:37 -0800183 mlog(0, "Purge %u %s items from Owner %llu\n", to_purge,
184 tree ? "array" : "tree",
185 (unsigned long long)ocfs2_metadata_cache_owner(ci));
Mark Fashehccd979b2005-12-15 14:31:24 -0800186
187 /* If we're a tree, save off the root so that we can safely
188 * initialize the cache. We do the work to free tree members
189 * without the spinlock. */
190 if (tree)
191 root = ci->ci_cache.ci_tree;
192
Joel Becker66fb3452009-02-12 15:24:40 -0800193 ocfs2_metadata_cache_reset(ci, 0);
Joel Becker6e5a3d72009-02-10 19:00:37 -0800194 ocfs2_metadata_cache_unlock(ci);
Mark Fashehccd979b2005-12-15 14:31:24 -0800195
196 purged = ocfs2_purge_copied_metadata_tree(&root);
197 /* If possible, track the number wiped so that we can more
198 * easily detect counting errors. Unfortunately, this is only
199 * meaningful for trees. */
200 if (tree && purged != to_purge)
Joel Becker6e5a3d72009-02-10 19:00:37 -0800201 mlog(ML_ERROR, "Owner %llu, count = %u, purged = %u\n",
202 (unsigned long long)ocfs2_metadata_cache_owner(ci),
203 to_purge, purged);
Mark Fashehccd979b2005-12-15 14:31:24 -0800204}
205
206/* Returns the index in the cache array, -1 if not found.
207 * Requires ip_lock. */
208static int ocfs2_search_cache_array(struct ocfs2_caching_info *ci,
209 sector_t item)
210{
211 int i;
212
213 for (i = 0; i < ci->ci_num_cached; i++) {
214 if (item == ci->ci_cache.ci_array[i])
215 return i;
216 }
217
218 return -1;
219}
220
221/* Returns the cache item if found, otherwise NULL.
222 * Requires ip_lock. */
223static struct ocfs2_meta_cache_item *
224ocfs2_search_cache_tree(struct ocfs2_caching_info *ci,
225 sector_t block)
226{
227 struct rb_node * n = ci->ci_cache.ci_tree.rb_node;
228 struct ocfs2_meta_cache_item *item = NULL;
229
230 while (n) {
231 item = rb_entry(n, struct ocfs2_meta_cache_item, c_node);
232
233 if (block < item->c_block)
234 n = n->rb_left;
235 else if (block > item->c_block)
236 n = n->rb_right;
237 else
238 return item;
239 }
240
241 return NULL;
242}
243
Joel Becker8cb471e2009-02-10 20:00:41 -0800244static int ocfs2_buffer_cached(struct ocfs2_caching_info *ci,
Mark Fashehccd979b2005-12-15 14:31:24 -0800245 struct buffer_head *bh)
246{
247 int index = -1;
248 struct ocfs2_meta_cache_item *item = NULL;
249
Joel Becker6e5a3d72009-02-10 19:00:37 -0800250 ocfs2_metadata_cache_lock(ci);
Mark Fashehccd979b2005-12-15 14:31:24 -0800251
Joel Becker6e5a3d72009-02-10 19:00:37 -0800252 mlog(0, "Owner %llu, query block %llu (inline = %u)\n",
253 (unsigned long long)ocfs2_metadata_cache_owner(ci),
Mark Fashehb06970532006-03-03 10:24:33 -0800254 (unsigned long long) bh->b_blocknr,
Joel Becker47460d62009-02-10 16:05:07 -0800255 !!(ci->ci_flags & OCFS2_CACHE_FL_INLINE));
Mark Fashehccd979b2005-12-15 14:31:24 -0800256
Joel Becker47460d62009-02-10 16:05:07 -0800257 if (ci->ci_flags & OCFS2_CACHE_FL_INLINE)
Joel Becker8cb471e2009-02-10 20:00:41 -0800258 index = ocfs2_search_cache_array(ci, bh->b_blocknr);
Mark Fashehccd979b2005-12-15 14:31:24 -0800259 else
Joel Becker8cb471e2009-02-10 20:00:41 -0800260 item = ocfs2_search_cache_tree(ci, bh->b_blocknr);
Mark Fashehccd979b2005-12-15 14:31:24 -0800261
Joel Becker6e5a3d72009-02-10 19:00:37 -0800262 ocfs2_metadata_cache_unlock(ci);
Mark Fashehccd979b2005-12-15 14:31:24 -0800263
264 mlog(0, "index = %d, item = %p\n", index, item);
265
266 return (index != -1) || (item != NULL);
267}
268
269/* Warning: even if it returns true, this does *not* guarantee that
Sunil Mushran2bd63212010-01-25 16:57:38 -0800270 * the block is stored in our inode metadata cache.
271 *
Mark Fashehaa958872006-04-21 13:49:02 -0700272 * This can be called under lock_buffer()
273 */
Joel Becker8cb471e2009-02-10 20:00:41 -0800274int ocfs2_buffer_uptodate(struct ocfs2_caching_info *ci,
Mark Fashehccd979b2005-12-15 14:31:24 -0800275 struct buffer_head *bh)
276{
277 /* Doesn't matter if the bh is in our cache or not -- if it's
278 * not marked uptodate then we know it can't have correct
279 * data. */
280 if (!buffer_uptodate(bh))
281 return 0;
282
283 /* OCFS2 does not allow multiple nodes to be changing the same
284 * block at the same time. */
285 if (buffer_jbd(bh))
286 return 1;
287
288 /* Ok, locally the buffer is marked as up to date, now search
289 * our cache to see if we can trust that. */
Joel Becker8cb471e2009-02-10 20:00:41 -0800290 return ocfs2_buffer_cached(ci, bh);
Mark Fashehccd979b2005-12-15 14:31:24 -0800291}
292
Joel Becker8cb471e2009-02-10 20:00:41 -0800293/*
Mark Fashehaa958872006-04-21 13:49:02 -0700294 * Determine whether a buffer is currently out on a read-ahead request.
Joel Becker47460d62009-02-10 16:05:07 -0800295 * ci_io_sem should be held to serialize submitters with the logic here.
Mark Fashehaa958872006-04-21 13:49:02 -0700296 */
Joel Becker8cb471e2009-02-10 20:00:41 -0800297int ocfs2_buffer_read_ahead(struct ocfs2_caching_info *ci,
Mark Fashehaa958872006-04-21 13:49:02 -0700298 struct buffer_head *bh)
299{
Joel Becker8cb471e2009-02-10 20:00:41 -0800300 return buffer_locked(bh) && ocfs2_buffer_cached(ci, bh);
Mark Fashehaa958872006-04-21 13:49:02 -0700301}
302
Mark Fashehccd979b2005-12-15 14:31:24 -0800303/* Requires ip_lock */
304static void ocfs2_append_cache_array(struct ocfs2_caching_info *ci,
305 sector_t block)
306{
Joel Becker47460d62009-02-10 16:05:07 -0800307 BUG_ON(ci->ci_num_cached >= OCFS2_CACHE_INFO_MAX_ARRAY);
Mark Fashehccd979b2005-12-15 14:31:24 -0800308
309 mlog(0, "block %llu takes position %u\n", (unsigned long long) block,
310 ci->ci_num_cached);
311
312 ci->ci_cache.ci_array[ci->ci_num_cached] = block;
313 ci->ci_num_cached++;
314}
315
316/* By now the caller should have checked that the item does *not*
317 * exist in the tree.
318 * Requires ip_lock. */
319static void __ocfs2_insert_cache_tree(struct ocfs2_caching_info *ci,
320 struct ocfs2_meta_cache_item *new)
321{
322 sector_t block = new->c_block;
323 struct rb_node *parent = NULL;
324 struct rb_node **p = &ci->ci_cache.ci_tree.rb_node;
325 struct ocfs2_meta_cache_item *tmp;
326
327 mlog(0, "Insert block %llu num = %u\n", (unsigned long long) block,
328 ci->ci_num_cached);
329
330 while(*p) {
331 parent = *p;
332
333 tmp = rb_entry(parent, struct ocfs2_meta_cache_item, c_node);
334
335 if (block < tmp->c_block)
336 p = &(*p)->rb_left;
337 else if (block > tmp->c_block)
338 p = &(*p)->rb_right;
339 else {
340 /* This should never happen! */
341 mlog(ML_ERROR, "Duplicate block %llu cached!\n",
342 (unsigned long long) block);
343 BUG();
344 }
345 }
346
347 rb_link_node(&new->c_node, parent, p);
348 rb_insert_color(&new->c_node, &ci->ci_cache.ci_tree);
349 ci->ci_num_cached++;
350}
351
Joel Becker6e5a3d72009-02-10 19:00:37 -0800352/* co_cache_lock() must be held */
Joel Becker8cb471e2009-02-10 20:00:41 -0800353static inline int ocfs2_insert_can_use_array(struct ocfs2_caching_info *ci)
Mark Fashehccd979b2005-12-15 14:31:24 -0800354{
Joel Becker47460d62009-02-10 16:05:07 -0800355 return (ci->ci_flags & OCFS2_CACHE_FL_INLINE) &&
356 (ci->ci_num_cached < OCFS2_CACHE_INFO_MAX_ARRAY);
Mark Fashehccd979b2005-12-15 14:31:24 -0800357}
358
Joel Becker47460d62009-02-10 16:05:07 -0800359/* tree should be exactly OCFS2_CACHE_INFO_MAX_ARRAY wide. NULL the
Mark Fashehccd979b2005-12-15 14:31:24 -0800360 * pointers in tree after we use them - this allows caller to detect
Joel Becker6e5a3d72009-02-10 19:00:37 -0800361 * when to free in case of error.
362 *
363 * The co_cache_lock() must be held. */
Joel Becker8cb471e2009-02-10 20:00:41 -0800364static void ocfs2_expand_cache(struct ocfs2_caching_info *ci,
Mark Fashehccd979b2005-12-15 14:31:24 -0800365 struct ocfs2_meta_cache_item **tree)
366{
367 int i;
Mark Fashehccd979b2005-12-15 14:31:24 -0800368
Joel Becker47460d62009-02-10 16:05:07 -0800369 mlog_bug_on_msg(ci->ci_num_cached != OCFS2_CACHE_INFO_MAX_ARRAY,
Joel Becker6e5a3d72009-02-10 19:00:37 -0800370 "Owner %llu, num cached = %u, should be %u\n",
371 (unsigned long long)ocfs2_metadata_cache_owner(ci),
372 ci->ci_num_cached, OCFS2_CACHE_INFO_MAX_ARRAY);
Joel Becker47460d62009-02-10 16:05:07 -0800373 mlog_bug_on_msg(!(ci->ci_flags & OCFS2_CACHE_FL_INLINE),
Joel Becker6e5a3d72009-02-10 19:00:37 -0800374 "Owner %llu not marked as inline anymore!\n",
375 (unsigned long long)ocfs2_metadata_cache_owner(ci));
Mark Fashehccd979b2005-12-15 14:31:24 -0800376
377 /* Be careful to initialize the tree members *first* because
378 * once the ci_tree is used, the array is junk... */
Joel Becker47460d62009-02-10 16:05:07 -0800379 for (i = 0; i < OCFS2_CACHE_INFO_MAX_ARRAY; i++)
Mark Fashehccd979b2005-12-15 14:31:24 -0800380 tree[i]->c_block = ci->ci_cache.ci_array[i];
381
Joel Becker47460d62009-02-10 16:05:07 -0800382 ci->ci_flags &= ~OCFS2_CACHE_FL_INLINE;
Mark Fashehccd979b2005-12-15 14:31:24 -0800383 ci->ci_cache.ci_tree = RB_ROOT;
384 /* this will be set again by __ocfs2_insert_cache_tree */
385 ci->ci_num_cached = 0;
386
Joel Becker47460d62009-02-10 16:05:07 -0800387 for (i = 0; i < OCFS2_CACHE_INFO_MAX_ARRAY; i++) {
Mark Fashehccd979b2005-12-15 14:31:24 -0800388 __ocfs2_insert_cache_tree(ci, tree[i]);
389 tree[i] = NULL;
390 }
391
Mark Fashehb06970532006-03-03 10:24:33 -0800392 mlog(0, "Expanded %llu to a tree cache: flags 0x%x, num = %u\n",
Joel Becker6e5a3d72009-02-10 19:00:37 -0800393 (unsigned long long)ocfs2_metadata_cache_owner(ci),
394 ci->ci_flags, ci->ci_num_cached);
Mark Fashehccd979b2005-12-15 14:31:24 -0800395}
396
397/* Slow path function - memory allocation is necessary. See the
398 * comment above ocfs2_set_buffer_uptodate for more information. */
Joel Becker8cb471e2009-02-10 20:00:41 -0800399static void __ocfs2_set_buffer_uptodate(struct ocfs2_caching_info *ci,
Mark Fashehccd979b2005-12-15 14:31:24 -0800400 sector_t block,
401 int expand_tree)
402{
403 int i;
Mark Fashehccd979b2005-12-15 14:31:24 -0800404 struct ocfs2_meta_cache_item *new = NULL;
Joel Becker47460d62009-02-10 16:05:07 -0800405 struct ocfs2_meta_cache_item *tree[OCFS2_CACHE_INFO_MAX_ARRAY] =
Mark Fashehccd979b2005-12-15 14:31:24 -0800406 { NULL, };
407
Joel Becker6e5a3d72009-02-10 19:00:37 -0800408 mlog(0, "Owner %llu, block %llu, expand = %d\n",
409 (unsigned long long)ocfs2_metadata_cache_owner(ci),
Mark Fashehb06970532006-03-03 10:24:33 -0800410 (unsigned long long)block, expand_tree);
Mark Fashehccd979b2005-12-15 14:31:24 -0800411
Sunil Mushranafae00ab2006-04-12 14:37:00 -0700412 new = kmem_cache_alloc(ocfs2_uptodate_cachep, GFP_NOFS);
Mark Fashehccd979b2005-12-15 14:31:24 -0800413 if (!new) {
414 mlog_errno(-ENOMEM);
415 return;
416 }
417 new->c_block = block;
418
419 if (expand_tree) {
420 /* Do *not* allocate an array here - the removal code
421 * has no way of tracking that. */
Joel Becker47460d62009-02-10 16:05:07 -0800422 for (i = 0; i < OCFS2_CACHE_INFO_MAX_ARRAY; i++) {
Mark Fashehccd979b2005-12-15 14:31:24 -0800423 tree[i] = kmem_cache_alloc(ocfs2_uptodate_cachep,
Sunil Mushranafae00ab2006-04-12 14:37:00 -0700424 GFP_NOFS);
Mark Fashehccd979b2005-12-15 14:31:24 -0800425 if (!tree[i]) {
426 mlog_errno(-ENOMEM);
427 goto out_free;
428 }
429
430 /* These are initialized in ocfs2_expand_cache! */
431 }
432 }
433
Joel Becker6e5a3d72009-02-10 19:00:37 -0800434 ocfs2_metadata_cache_lock(ci);
Joel Becker8cb471e2009-02-10 20:00:41 -0800435 if (ocfs2_insert_can_use_array(ci)) {
Mark Fashehccd979b2005-12-15 14:31:24 -0800436 mlog(0, "Someone cleared the tree underneath us\n");
437 /* Ok, items were removed from the cache in between
438 * locks. Detect this and revert back to the fast path */
439 ocfs2_append_cache_array(ci, block);
Joel Becker6e5a3d72009-02-10 19:00:37 -0800440 ocfs2_metadata_cache_unlock(ci);
Mark Fashehccd979b2005-12-15 14:31:24 -0800441 goto out_free;
442 }
443
444 if (expand_tree)
Joel Becker8cb471e2009-02-10 20:00:41 -0800445 ocfs2_expand_cache(ci, tree);
Mark Fashehccd979b2005-12-15 14:31:24 -0800446
447 __ocfs2_insert_cache_tree(ci, new);
Joel Becker6e5a3d72009-02-10 19:00:37 -0800448 ocfs2_metadata_cache_unlock(ci);
Mark Fashehccd979b2005-12-15 14:31:24 -0800449
450 new = NULL;
451out_free:
452 if (new)
453 kmem_cache_free(ocfs2_uptodate_cachep, new);
454
455 /* If these were used, then ocfs2_expand_cache re-set them to
456 * NULL for us. */
457 if (tree[0]) {
Joel Becker47460d62009-02-10 16:05:07 -0800458 for (i = 0; i < OCFS2_CACHE_INFO_MAX_ARRAY; i++)
Mark Fashehccd979b2005-12-15 14:31:24 -0800459 if (tree[i])
460 kmem_cache_free(ocfs2_uptodate_cachep,
461 tree[i]);
462 }
463}
464
Joel Becker6e5a3d72009-02-10 19:00:37 -0800465/* Item insertion is guarded by co_io_lock(), so the insertion path takes
Mark Fashehccd979b2005-12-15 14:31:24 -0800466 * advantage of this by not rechecking for a duplicate insert during
467 * the slow case. Additionally, if the cache needs to be bumped up to
468 * a tree, the code will not recheck after acquiring the lock --
469 * multiple paths cannot be expanding to a tree at the same time.
470 *
471 * The slow path takes into account that items can be removed
472 * (including the whole tree wiped and reset) when this process it out
473 * allocating memory. In those cases, it reverts back to the fast
474 * path.
475 *
476 * Note that this function may actually fail to insert the block if
477 * memory cannot be allocated. This is not fatal however (but may
Mark Fashehaa958872006-04-21 13:49:02 -0700478 * result in a performance penalty)
479 *
480 * Readahead buffers can be passed in here before the I/O request is
481 * completed.
482 */
Joel Becker8cb471e2009-02-10 20:00:41 -0800483void ocfs2_set_buffer_uptodate(struct ocfs2_caching_info *ci,
Mark Fashehccd979b2005-12-15 14:31:24 -0800484 struct buffer_head *bh)
485{
486 int expand;
Mark Fashehccd979b2005-12-15 14:31:24 -0800487
488 /* The block may very well exist in our cache already, so avoid
489 * doing any more work in that case. */
Joel Becker8cb471e2009-02-10 20:00:41 -0800490 if (ocfs2_buffer_cached(ci, bh))
Mark Fashehccd979b2005-12-15 14:31:24 -0800491 return;
492
Joel Becker6e5a3d72009-02-10 19:00:37 -0800493 mlog(0, "Owner %llu, inserting block %llu\n",
494 (unsigned long long)ocfs2_metadata_cache_owner(ci),
Mark Fashehb06970532006-03-03 10:24:33 -0800495 (unsigned long long)bh->b_blocknr);
Mark Fashehccd979b2005-12-15 14:31:24 -0800496
497 /* No need to recheck under spinlock - insertion is guarded by
Joel Becker6e5a3d72009-02-10 19:00:37 -0800498 * co_io_lock() */
499 ocfs2_metadata_cache_lock(ci);
Joel Becker8cb471e2009-02-10 20:00:41 -0800500 if (ocfs2_insert_can_use_array(ci)) {
Mark Fashehccd979b2005-12-15 14:31:24 -0800501 /* Fast case - it's an array and there's a free
502 * spot. */
503 ocfs2_append_cache_array(ci, bh->b_blocknr);
Joel Becker6e5a3d72009-02-10 19:00:37 -0800504 ocfs2_metadata_cache_unlock(ci);
Mark Fashehccd979b2005-12-15 14:31:24 -0800505 return;
506 }
507
508 expand = 0;
Joel Becker47460d62009-02-10 16:05:07 -0800509 if (ci->ci_flags & OCFS2_CACHE_FL_INLINE) {
Mark Fashehccd979b2005-12-15 14:31:24 -0800510 /* We need to bump things up to a tree. */
511 expand = 1;
512 }
Joel Becker6e5a3d72009-02-10 19:00:37 -0800513 ocfs2_metadata_cache_unlock(ci);
Mark Fashehccd979b2005-12-15 14:31:24 -0800514
Joel Becker8cb471e2009-02-10 20:00:41 -0800515 __ocfs2_set_buffer_uptodate(ci, bh->b_blocknr, expand);
Mark Fashehccd979b2005-12-15 14:31:24 -0800516}
517
518/* Called against a newly allocated buffer. Most likely nobody should
519 * be able to read this sort of metadata while it's still being
Joel Becker6e5a3d72009-02-10 19:00:37 -0800520 * allocated, but this is careful to take co_io_lock() anyway. */
Joel Becker8cb471e2009-02-10 20:00:41 -0800521void ocfs2_set_new_buffer_uptodate(struct ocfs2_caching_info *ci,
Mark Fashehccd979b2005-12-15 14:31:24 -0800522 struct buffer_head *bh)
523{
Mark Fashehccd979b2005-12-15 14:31:24 -0800524 /* This should definitely *not* exist in our cache */
Joel Becker8cb471e2009-02-10 20:00:41 -0800525 BUG_ON(ocfs2_buffer_cached(ci, bh));
Mark Fashehccd979b2005-12-15 14:31:24 -0800526
527 set_buffer_uptodate(bh);
528
Joel Becker6e5a3d72009-02-10 19:00:37 -0800529 ocfs2_metadata_cache_io_lock(ci);
Joel Becker8cb471e2009-02-10 20:00:41 -0800530 ocfs2_set_buffer_uptodate(ci, bh);
Joel Becker6e5a3d72009-02-10 19:00:37 -0800531 ocfs2_metadata_cache_io_unlock(ci);
Mark Fashehccd979b2005-12-15 14:31:24 -0800532}
533
534/* Requires ip_lock. */
535static void ocfs2_remove_metadata_array(struct ocfs2_caching_info *ci,
536 int index)
537{
538 sector_t *array = ci->ci_cache.ci_array;
539 int bytes;
540
Joel Becker47460d62009-02-10 16:05:07 -0800541 BUG_ON(index < 0 || index >= OCFS2_CACHE_INFO_MAX_ARRAY);
Mark Fashehccd979b2005-12-15 14:31:24 -0800542 BUG_ON(index >= ci->ci_num_cached);
543 BUG_ON(!ci->ci_num_cached);
544
545 mlog(0, "remove index %d (num_cached = %u\n", index,
546 ci->ci_num_cached);
547
548 ci->ci_num_cached--;
549
550 /* don't need to copy if the array is now empty, or if we
551 * removed at the tail */
552 if (ci->ci_num_cached && index < ci->ci_num_cached) {
553 bytes = sizeof(sector_t) * (ci->ci_num_cached - index);
554 memmove(&array[index], &array[index + 1], bytes);
555 }
556}
557
558/* Requires ip_lock. */
559static void ocfs2_remove_metadata_tree(struct ocfs2_caching_info *ci,
560 struct ocfs2_meta_cache_item *item)
561{
562 mlog(0, "remove block %llu from tree\n",
563 (unsigned long long) item->c_block);
564
565 rb_erase(&item->c_node, &ci->ci_cache.ci_tree);
566 ci->ci_num_cached--;
567}
568
Joel Becker8cb471e2009-02-10 20:00:41 -0800569static void ocfs2_remove_block_from_cache(struct ocfs2_caching_info *ci,
Tao Maac11c822008-08-18 17:38:47 +0800570 sector_t block)
Mark Fashehccd979b2005-12-15 14:31:24 -0800571{
572 int index;
Mark Fashehccd979b2005-12-15 14:31:24 -0800573 struct ocfs2_meta_cache_item *item = NULL;
Mark Fashehccd979b2005-12-15 14:31:24 -0800574
Joel Becker6e5a3d72009-02-10 19:00:37 -0800575 ocfs2_metadata_cache_lock(ci);
576 mlog(0, "Owner %llu, remove %llu, items = %u, array = %u\n",
577 (unsigned long long)ocfs2_metadata_cache_owner(ci),
Mark Fashehb06970532006-03-03 10:24:33 -0800578 (unsigned long long) block, ci->ci_num_cached,
Joel Becker47460d62009-02-10 16:05:07 -0800579 ci->ci_flags & OCFS2_CACHE_FL_INLINE);
Mark Fashehccd979b2005-12-15 14:31:24 -0800580
Joel Becker47460d62009-02-10 16:05:07 -0800581 if (ci->ci_flags & OCFS2_CACHE_FL_INLINE) {
Mark Fashehccd979b2005-12-15 14:31:24 -0800582 index = ocfs2_search_cache_array(ci, block);
583 if (index != -1)
584 ocfs2_remove_metadata_array(ci, index);
585 } else {
586 item = ocfs2_search_cache_tree(ci, block);
587 if (item)
588 ocfs2_remove_metadata_tree(ci, item);
589 }
Joel Becker6e5a3d72009-02-10 19:00:37 -0800590 ocfs2_metadata_cache_unlock(ci);
Mark Fashehccd979b2005-12-15 14:31:24 -0800591
592 if (item)
593 kmem_cache_free(ocfs2_uptodate_cachep, item);
594}
595
Tao Maac11c822008-08-18 17:38:47 +0800596/*
597 * Called when we remove a chunk of metadata from an inode. We don't
598 * bother reverting things to an inlined array in the case of a remove
599 * which moves us back under the limit.
600 */
Joel Becker8cb471e2009-02-10 20:00:41 -0800601void ocfs2_remove_from_cache(struct ocfs2_caching_info *ci,
Tao Maac11c822008-08-18 17:38:47 +0800602 struct buffer_head *bh)
603{
604 sector_t block = bh->b_blocknr;
605
Joel Becker8cb471e2009-02-10 20:00:41 -0800606 ocfs2_remove_block_from_cache(ci, block);
Tao Maac11c822008-08-18 17:38:47 +0800607}
608
609/* Called when we remove xattr clusters from an inode. */
Joel Becker8cb471e2009-02-10 20:00:41 -0800610void ocfs2_remove_xattr_clusters_from_cache(struct ocfs2_caching_info *ci,
Tao Maac11c822008-08-18 17:38:47 +0800611 sector_t block,
612 u32 c_len)
613{
Joel Becker8cb471e2009-02-10 20:00:41 -0800614 struct super_block *sb = ocfs2_metadata_cache_get_super(ci);
615 unsigned int i, b_len = ocfs2_clusters_to_blocks(sb, 1) * c_len;
Tao Maac11c822008-08-18 17:38:47 +0800616
617 for (i = 0; i < b_len; i++, block++)
Joel Becker8cb471e2009-02-10 20:00:41 -0800618 ocfs2_remove_block_from_cache(ci, block);
Tao Maac11c822008-08-18 17:38:47 +0800619}
620
Mark Fashehccd979b2005-12-15 14:31:24 -0800621int __init init_ocfs2_uptodate_cache(void)
622{
623 ocfs2_uptodate_cachep = kmem_cache_create("ocfs2_uptodate",
624 sizeof(struct ocfs2_meta_cache_item),
Paul Mundt20c2df82007-07-20 10:11:58 +0900625 0, SLAB_HWCACHE_ALIGN, NULL);
Mark Fashehccd979b2005-12-15 14:31:24 -0800626 if (!ocfs2_uptodate_cachep)
627 return -ENOMEM;
628
629 mlog(0, "%u inlined cache items per inode.\n",
Joel Becker47460d62009-02-10 16:05:07 -0800630 OCFS2_CACHE_INFO_MAX_ARRAY);
Mark Fashehccd979b2005-12-15 14:31:24 -0800631
632 return 0;
633}
634
Adrian Bunk0c6c98f2006-01-07 20:07:02 +0100635void exit_ocfs2_uptodate_cache(void)
Mark Fashehccd979b2005-12-15 14:31:24 -0800636{
637 if (ocfs2_uptodate_cachep)
638 kmem_cache_destroy(ocfs2_uptodate_cachep);
639}