blob: 81c82200b90804b970487420eb92da2d71f28d75 [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>
Joel Becker2b4e30f2008-09-03 20:03:41 -070056#ifndef CONFIG_OCFS2_COMPAT_JBD
57# include <linux/jbd2.h>
58#else
59# include <linux/jbd.h>
60#endif
Mark Fashehccd979b2005-12-15 14:31:24 -080061
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
71struct ocfs2_meta_cache_item {
72 struct rb_node c_node;
73 sector_t c_block;
74};
75
Christoph Lametere18b8902006-12-06 20:33:20 -080076static struct kmem_cache *ocfs2_uptodate_cachep = NULL;
Mark Fashehccd979b2005-12-15 14:31:24 -080077
Joel Becker8cb471e2009-02-10 20:00:41 -080078u64 ocfs2_metadata_cache_owner(struct ocfs2_caching_info *ci)
Mark Fashehccd979b2005-12-15 14:31:24 -080079{
Joel Becker6e5a3d72009-02-10 19:00:37 -080080 BUG_ON(!ci || !ci->ci_ops);
81
82 return ci->ci_ops->co_owner(ci);
83}
84
Joel Becker8cb471e2009-02-10 20:00:41 -080085struct super_block *ocfs2_metadata_cache_get_super(struct ocfs2_caching_info *ci)
86{
87 BUG_ON(!ci || !ci->ci_ops);
88
89 return ci->ci_ops->co_get_super(ci);
90}
91
Joel Becker6e5a3d72009-02-10 19:00:37 -080092static void ocfs2_metadata_cache_lock(struct ocfs2_caching_info *ci)
93{
94 BUG_ON(!ci || !ci->ci_ops);
95
96 ci->ci_ops->co_cache_lock(ci);
97}
98
99static void ocfs2_metadata_cache_unlock(struct ocfs2_caching_info *ci)
100{
101 BUG_ON(!ci || !ci->ci_ops);
102
103 ci->ci_ops->co_cache_unlock(ci);
104}
105
Joel Becker8cb471e2009-02-10 20:00:41 -0800106void ocfs2_metadata_cache_io_lock(struct ocfs2_caching_info *ci)
Joel Becker6e5a3d72009-02-10 19:00:37 -0800107{
108 BUG_ON(!ci || !ci->ci_ops);
109
110 ci->ci_ops->co_io_lock(ci);
111}
112
Joel Becker8cb471e2009-02-10 20:00:41 -0800113void ocfs2_metadata_cache_io_unlock(struct ocfs2_caching_info *ci)
Joel Becker6e5a3d72009-02-10 19:00:37 -0800114{
115 BUG_ON(!ci || !ci->ci_ops);
116
117 ci->ci_ops->co_io_unlock(ci);
118}
119
120
Joel Becker66fb3452009-02-12 15:24:40 -0800121static void ocfs2_metadata_cache_reset(struct ocfs2_caching_info *ci,
122 int clear)
123{
124 ci->ci_flags |= OCFS2_CACHE_FL_INLINE;
125 ci->ci_num_cached = 0;
126
127 if (clear)
128 ci->ci_last_trans = 0;
129}
130
Joel Becker6e5a3d72009-02-10 19:00:37 -0800131void ocfs2_metadata_cache_init(struct ocfs2_caching_info *ci,
132 const struct ocfs2_caching_operations *ops)
133{
134 BUG_ON(!ops);
135
136 ci->ci_ops = ops;
Joel Becker66fb3452009-02-12 15:24:40 -0800137 ocfs2_metadata_cache_reset(ci, 1);
Mark Fashehccd979b2005-12-15 14:31:24 -0800138}
139
Joel Becker66fb3452009-02-12 15:24:40 -0800140void ocfs2_metadata_cache_exit(struct ocfs2_caching_info *ci)
141{
142 ocfs2_metadata_cache_purge(ci);
143 ocfs2_metadata_cache_reset(ci, 1);
144}
145
146
Mark Fashehccd979b2005-12-15 14:31:24 -0800147/* No lock taken here as 'root' is not expected to be visible to other
148 * processes. */
149static unsigned int ocfs2_purge_copied_metadata_tree(struct rb_root *root)
150{
151 unsigned int purged = 0;
152 struct rb_node *node;
153 struct ocfs2_meta_cache_item *item;
154
155 while ((node = rb_last(root)) != NULL) {
156 item = rb_entry(node, struct ocfs2_meta_cache_item, c_node);
157
158 mlog(0, "Purge item %llu\n",
159 (unsigned long long) item->c_block);
160
161 rb_erase(&item->c_node, root);
162 kmem_cache_free(ocfs2_uptodate_cachep, item);
163
164 purged++;
165 }
166 return purged;
167}
168
169/* Called from locking and called from ocfs2_clear_inode. Dump the
170 * cache for a given inode.
171 *
172 * This function is a few more lines longer than necessary due to some
173 * accounting done here, but I think it's worth tracking down those
174 * bugs sooner -- Mark */
Joel Becker8cb471e2009-02-10 20:00:41 -0800175void ocfs2_metadata_cache_purge(struct ocfs2_caching_info *ci)
Mark Fashehccd979b2005-12-15 14:31:24 -0800176{
Mark Fashehccd979b2005-12-15 14:31:24 -0800177 unsigned int tree, to_purge, purged;
Mark Fashehccd979b2005-12-15 14:31:24 -0800178 struct rb_root root = RB_ROOT;
179
Joel Becker6e5a3d72009-02-10 19:00:37 -0800180 BUG_ON(!ci || !ci->ci_ops);
181
182 ocfs2_metadata_cache_lock(ci);
Joel Becker47460d62009-02-10 16:05:07 -0800183 tree = !(ci->ci_flags & OCFS2_CACHE_FL_INLINE);
Mark Fashehccd979b2005-12-15 14:31:24 -0800184 to_purge = ci->ci_num_cached;
185
Joel Becker6e5a3d72009-02-10 19:00:37 -0800186 mlog(0, "Purge %u %s items from Owner %llu\n", to_purge,
187 tree ? "array" : "tree",
188 (unsigned long long)ocfs2_metadata_cache_owner(ci));
Mark Fashehccd979b2005-12-15 14:31:24 -0800189
190 /* If we're a tree, save off the root so that we can safely
191 * initialize the cache. We do the work to free tree members
192 * without the spinlock. */
193 if (tree)
194 root = ci->ci_cache.ci_tree;
195
Joel Becker66fb3452009-02-12 15:24:40 -0800196 ocfs2_metadata_cache_reset(ci, 0);
Joel Becker6e5a3d72009-02-10 19:00:37 -0800197 ocfs2_metadata_cache_unlock(ci);
Mark Fashehccd979b2005-12-15 14:31:24 -0800198
199 purged = ocfs2_purge_copied_metadata_tree(&root);
200 /* If possible, track the number wiped so that we can more
201 * easily detect counting errors. Unfortunately, this is only
202 * meaningful for trees. */
203 if (tree && purged != to_purge)
Joel Becker6e5a3d72009-02-10 19:00:37 -0800204 mlog(ML_ERROR, "Owner %llu, count = %u, purged = %u\n",
205 (unsigned long long)ocfs2_metadata_cache_owner(ci),
206 to_purge, purged);
Mark Fashehccd979b2005-12-15 14:31:24 -0800207}
208
209/* Returns the index in the cache array, -1 if not found.
210 * Requires ip_lock. */
211static int ocfs2_search_cache_array(struct ocfs2_caching_info *ci,
212 sector_t item)
213{
214 int i;
215
216 for (i = 0; i < ci->ci_num_cached; i++) {
217 if (item == ci->ci_cache.ci_array[i])
218 return i;
219 }
220
221 return -1;
222}
223
224/* Returns the cache item if found, otherwise NULL.
225 * Requires ip_lock. */
226static struct ocfs2_meta_cache_item *
227ocfs2_search_cache_tree(struct ocfs2_caching_info *ci,
228 sector_t block)
229{
230 struct rb_node * n = ci->ci_cache.ci_tree.rb_node;
231 struct ocfs2_meta_cache_item *item = NULL;
232
233 while (n) {
234 item = rb_entry(n, struct ocfs2_meta_cache_item, c_node);
235
236 if (block < item->c_block)
237 n = n->rb_left;
238 else if (block > item->c_block)
239 n = n->rb_right;
240 else
241 return item;
242 }
243
244 return NULL;
245}
246
Joel Becker8cb471e2009-02-10 20:00:41 -0800247static int ocfs2_buffer_cached(struct ocfs2_caching_info *ci,
Mark Fashehccd979b2005-12-15 14:31:24 -0800248 struct buffer_head *bh)
249{
250 int index = -1;
251 struct ocfs2_meta_cache_item *item = NULL;
252
Joel Becker6e5a3d72009-02-10 19:00:37 -0800253 ocfs2_metadata_cache_lock(ci);
Mark Fashehccd979b2005-12-15 14:31:24 -0800254
Joel Becker6e5a3d72009-02-10 19:00:37 -0800255 mlog(0, "Owner %llu, query block %llu (inline = %u)\n",
256 (unsigned long long)ocfs2_metadata_cache_owner(ci),
Mark Fashehb06970532006-03-03 10:24:33 -0800257 (unsigned long long) bh->b_blocknr,
Joel Becker47460d62009-02-10 16:05:07 -0800258 !!(ci->ci_flags & OCFS2_CACHE_FL_INLINE));
Mark Fashehccd979b2005-12-15 14:31:24 -0800259
Joel Becker47460d62009-02-10 16:05:07 -0800260 if (ci->ci_flags & OCFS2_CACHE_FL_INLINE)
Joel Becker8cb471e2009-02-10 20:00:41 -0800261 index = ocfs2_search_cache_array(ci, bh->b_blocknr);
Mark Fashehccd979b2005-12-15 14:31:24 -0800262 else
Joel Becker8cb471e2009-02-10 20:00:41 -0800263 item = ocfs2_search_cache_tree(ci, bh->b_blocknr);
Mark Fashehccd979b2005-12-15 14:31:24 -0800264
Joel Becker6e5a3d72009-02-10 19:00:37 -0800265 ocfs2_metadata_cache_unlock(ci);
Mark Fashehccd979b2005-12-15 14:31:24 -0800266
267 mlog(0, "index = %d, item = %p\n", index, item);
268
269 return (index != -1) || (item != NULL);
270}
271
272/* Warning: even if it returns true, this does *not* guarantee that
Mark Fashehaa958872006-04-21 13:49:02 -0700273 * the block is stored in our inode metadata cache.
274 *
275 * This can be called under lock_buffer()
276 */
Joel Becker8cb471e2009-02-10 20:00:41 -0800277int ocfs2_buffer_uptodate(struct ocfs2_caching_info *ci,
Mark Fashehccd979b2005-12-15 14:31:24 -0800278 struct buffer_head *bh)
279{
280 /* Doesn't matter if the bh is in our cache or not -- if it's
281 * not marked uptodate then we know it can't have correct
282 * data. */
283 if (!buffer_uptodate(bh))
284 return 0;
285
286 /* OCFS2 does not allow multiple nodes to be changing the same
287 * block at the same time. */
288 if (buffer_jbd(bh))
289 return 1;
290
291 /* Ok, locally the buffer is marked as up to date, now search
292 * our cache to see if we can trust that. */
Joel Becker8cb471e2009-02-10 20:00:41 -0800293 return ocfs2_buffer_cached(ci, bh);
Mark Fashehccd979b2005-12-15 14:31:24 -0800294}
295
Joel Becker8cb471e2009-02-10 20:00:41 -0800296/*
Mark Fashehaa958872006-04-21 13:49:02 -0700297 * Determine whether a buffer is currently out on a read-ahead request.
Joel Becker47460d62009-02-10 16:05:07 -0800298 * ci_io_sem should be held to serialize submitters with the logic here.
Mark Fashehaa958872006-04-21 13:49:02 -0700299 */
Joel Becker8cb471e2009-02-10 20:00:41 -0800300int ocfs2_buffer_read_ahead(struct ocfs2_caching_info *ci,
Mark Fashehaa958872006-04-21 13:49:02 -0700301 struct buffer_head *bh)
302{
Joel Becker8cb471e2009-02-10 20:00:41 -0800303 return buffer_locked(bh) && ocfs2_buffer_cached(ci, bh);
Mark Fashehaa958872006-04-21 13:49:02 -0700304}
305
Mark Fashehccd979b2005-12-15 14:31:24 -0800306/* Requires ip_lock */
307static void ocfs2_append_cache_array(struct ocfs2_caching_info *ci,
308 sector_t block)
309{
Joel Becker47460d62009-02-10 16:05:07 -0800310 BUG_ON(ci->ci_num_cached >= OCFS2_CACHE_INFO_MAX_ARRAY);
Mark Fashehccd979b2005-12-15 14:31:24 -0800311
312 mlog(0, "block %llu takes position %u\n", (unsigned long long) block,
313 ci->ci_num_cached);
314
315 ci->ci_cache.ci_array[ci->ci_num_cached] = block;
316 ci->ci_num_cached++;
317}
318
319/* By now the caller should have checked that the item does *not*
320 * exist in the tree.
321 * Requires ip_lock. */
322static void __ocfs2_insert_cache_tree(struct ocfs2_caching_info *ci,
323 struct ocfs2_meta_cache_item *new)
324{
325 sector_t block = new->c_block;
326 struct rb_node *parent = NULL;
327 struct rb_node **p = &ci->ci_cache.ci_tree.rb_node;
328 struct ocfs2_meta_cache_item *tmp;
329
330 mlog(0, "Insert block %llu num = %u\n", (unsigned long long) block,
331 ci->ci_num_cached);
332
333 while(*p) {
334 parent = *p;
335
336 tmp = rb_entry(parent, struct ocfs2_meta_cache_item, c_node);
337
338 if (block < tmp->c_block)
339 p = &(*p)->rb_left;
340 else if (block > tmp->c_block)
341 p = &(*p)->rb_right;
342 else {
343 /* This should never happen! */
344 mlog(ML_ERROR, "Duplicate block %llu cached!\n",
345 (unsigned long long) block);
346 BUG();
347 }
348 }
349
350 rb_link_node(&new->c_node, parent, p);
351 rb_insert_color(&new->c_node, &ci->ci_cache.ci_tree);
352 ci->ci_num_cached++;
353}
354
Joel Becker6e5a3d72009-02-10 19:00:37 -0800355/* co_cache_lock() must be held */
Joel Becker8cb471e2009-02-10 20:00:41 -0800356static inline int ocfs2_insert_can_use_array(struct ocfs2_caching_info *ci)
Mark Fashehccd979b2005-12-15 14:31:24 -0800357{
Joel Becker47460d62009-02-10 16:05:07 -0800358 return (ci->ci_flags & OCFS2_CACHE_FL_INLINE) &&
359 (ci->ci_num_cached < OCFS2_CACHE_INFO_MAX_ARRAY);
Mark Fashehccd979b2005-12-15 14:31:24 -0800360}
361
Joel Becker47460d62009-02-10 16:05:07 -0800362/* tree should be exactly OCFS2_CACHE_INFO_MAX_ARRAY wide. NULL the
Mark Fashehccd979b2005-12-15 14:31:24 -0800363 * pointers in tree after we use them - this allows caller to detect
Joel Becker6e5a3d72009-02-10 19:00:37 -0800364 * when to free in case of error.
365 *
366 * The co_cache_lock() must be held. */
Joel Becker8cb471e2009-02-10 20:00:41 -0800367static void ocfs2_expand_cache(struct ocfs2_caching_info *ci,
Mark Fashehccd979b2005-12-15 14:31:24 -0800368 struct ocfs2_meta_cache_item **tree)
369{
370 int i;
Mark Fashehccd979b2005-12-15 14:31:24 -0800371
Joel Becker47460d62009-02-10 16:05:07 -0800372 mlog_bug_on_msg(ci->ci_num_cached != OCFS2_CACHE_INFO_MAX_ARRAY,
Joel Becker6e5a3d72009-02-10 19:00:37 -0800373 "Owner %llu, num cached = %u, should be %u\n",
374 (unsigned long long)ocfs2_metadata_cache_owner(ci),
375 ci->ci_num_cached, OCFS2_CACHE_INFO_MAX_ARRAY);
Joel Becker47460d62009-02-10 16:05:07 -0800376 mlog_bug_on_msg(!(ci->ci_flags & OCFS2_CACHE_FL_INLINE),
Joel Becker6e5a3d72009-02-10 19:00:37 -0800377 "Owner %llu not marked as inline anymore!\n",
378 (unsigned long long)ocfs2_metadata_cache_owner(ci));
Mark Fashehccd979b2005-12-15 14:31:24 -0800379
380 /* Be careful to initialize the tree members *first* because
381 * once the ci_tree is used, the array is junk... */
Joel Becker47460d62009-02-10 16:05:07 -0800382 for (i = 0; i < OCFS2_CACHE_INFO_MAX_ARRAY; i++)
Mark Fashehccd979b2005-12-15 14:31:24 -0800383 tree[i]->c_block = ci->ci_cache.ci_array[i];
384
Joel Becker47460d62009-02-10 16:05:07 -0800385 ci->ci_flags &= ~OCFS2_CACHE_FL_INLINE;
Mark Fashehccd979b2005-12-15 14:31:24 -0800386 ci->ci_cache.ci_tree = RB_ROOT;
387 /* this will be set again by __ocfs2_insert_cache_tree */
388 ci->ci_num_cached = 0;
389
Joel Becker47460d62009-02-10 16:05:07 -0800390 for (i = 0; i < OCFS2_CACHE_INFO_MAX_ARRAY; i++) {
Mark Fashehccd979b2005-12-15 14:31:24 -0800391 __ocfs2_insert_cache_tree(ci, tree[i]);
392 tree[i] = NULL;
393 }
394
Mark Fashehb06970532006-03-03 10:24:33 -0800395 mlog(0, "Expanded %llu to a tree cache: flags 0x%x, num = %u\n",
Joel Becker6e5a3d72009-02-10 19:00:37 -0800396 (unsigned long long)ocfs2_metadata_cache_owner(ci),
397 ci->ci_flags, ci->ci_num_cached);
Mark Fashehccd979b2005-12-15 14:31:24 -0800398}
399
400/* Slow path function - memory allocation is necessary. See the
401 * comment above ocfs2_set_buffer_uptodate for more information. */
Joel Becker8cb471e2009-02-10 20:00:41 -0800402static void __ocfs2_set_buffer_uptodate(struct ocfs2_caching_info *ci,
Mark Fashehccd979b2005-12-15 14:31:24 -0800403 sector_t block,
404 int expand_tree)
405{
406 int i;
Mark Fashehccd979b2005-12-15 14:31:24 -0800407 struct ocfs2_meta_cache_item *new = NULL;
Joel Becker47460d62009-02-10 16:05:07 -0800408 struct ocfs2_meta_cache_item *tree[OCFS2_CACHE_INFO_MAX_ARRAY] =
Mark Fashehccd979b2005-12-15 14:31:24 -0800409 { NULL, };
410
Joel Becker6e5a3d72009-02-10 19:00:37 -0800411 mlog(0, "Owner %llu, block %llu, expand = %d\n",
412 (unsigned long long)ocfs2_metadata_cache_owner(ci),
Mark Fashehb06970532006-03-03 10:24:33 -0800413 (unsigned long long)block, expand_tree);
Mark Fashehccd979b2005-12-15 14:31:24 -0800414
Sunil Mushranafae00ab2006-04-12 14:37:00 -0700415 new = kmem_cache_alloc(ocfs2_uptodate_cachep, GFP_NOFS);
Mark Fashehccd979b2005-12-15 14:31:24 -0800416 if (!new) {
417 mlog_errno(-ENOMEM);
418 return;
419 }
420 new->c_block = block;
421
422 if (expand_tree) {
423 /* Do *not* allocate an array here - the removal code
424 * has no way of tracking that. */
Joel Becker47460d62009-02-10 16:05:07 -0800425 for (i = 0; i < OCFS2_CACHE_INFO_MAX_ARRAY; i++) {
Mark Fashehccd979b2005-12-15 14:31:24 -0800426 tree[i] = kmem_cache_alloc(ocfs2_uptodate_cachep,
Sunil Mushranafae00ab2006-04-12 14:37:00 -0700427 GFP_NOFS);
Mark Fashehccd979b2005-12-15 14:31:24 -0800428 if (!tree[i]) {
429 mlog_errno(-ENOMEM);
430 goto out_free;
431 }
432
433 /* These are initialized in ocfs2_expand_cache! */
434 }
435 }
436
Joel Becker6e5a3d72009-02-10 19:00:37 -0800437 ocfs2_metadata_cache_lock(ci);
Joel Becker8cb471e2009-02-10 20:00:41 -0800438 if (ocfs2_insert_can_use_array(ci)) {
Mark Fashehccd979b2005-12-15 14:31:24 -0800439 mlog(0, "Someone cleared the tree underneath us\n");
440 /* Ok, items were removed from the cache in between
441 * locks. Detect this and revert back to the fast path */
442 ocfs2_append_cache_array(ci, block);
Joel Becker6e5a3d72009-02-10 19:00:37 -0800443 ocfs2_metadata_cache_unlock(ci);
Mark Fashehccd979b2005-12-15 14:31:24 -0800444 goto out_free;
445 }
446
447 if (expand_tree)
Joel Becker8cb471e2009-02-10 20:00:41 -0800448 ocfs2_expand_cache(ci, tree);
Mark Fashehccd979b2005-12-15 14:31:24 -0800449
450 __ocfs2_insert_cache_tree(ci, new);
Joel Becker6e5a3d72009-02-10 19:00:37 -0800451 ocfs2_metadata_cache_unlock(ci);
Mark Fashehccd979b2005-12-15 14:31:24 -0800452
453 new = NULL;
454out_free:
455 if (new)
456 kmem_cache_free(ocfs2_uptodate_cachep, new);
457
458 /* If these were used, then ocfs2_expand_cache re-set them to
459 * NULL for us. */
460 if (tree[0]) {
Joel Becker47460d62009-02-10 16:05:07 -0800461 for (i = 0; i < OCFS2_CACHE_INFO_MAX_ARRAY; i++)
Mark Fashehccd979b2005-12-15 14:31:24 -0800462 if (tree[i])
463 kmem_cache_free(ocfs2_uptodate_cachep,
464 tree[i]);
465 }
466}
467
Joel Becker6e5a3d72009-02-10 19:00:37 -0800468/* Item insertion is guarded by co_io_lock(), so the insertion path takes
Mark Fashehccd979b2005-12-15 14:31:24 -0800469 * advantage of this by not rechecking for a duplicate insert during
470 * the slow case. Additionally, if the cache needs to be bumped up to
471 * a tree, the code will not recheck after acquiring the lock --
472 * multiple paths cannot be expanding to a tree at the same time.
473 *
474 * The slow path takes into account that items can be removed
475 * (including the whole tree wiped and reset) when this process it out
476 * allocating memory. In those cases, it reverts back to the fast
477 * path.
478 *
479 * Note that this function may actually fail to insert the block if
480 * memory cannot be allocated. This is not fatal however (but may
Mark Fashehaa958872006-04-21 13:49:02 -0700481 * result in a performance penalty)
482 *
483 * Readahead buffers can be passed in here before the I/O request is
484 * completed.
485 */
Joel Becker8cb471e2009-02-10 20:00:41 -0800486void ocfs2_set_buffer_uptodate(struct ocfs2_caching_info *ci,
Mark Fashehccd979b2005-12-15 14:31:24 -0800487 struct buffer_head *bh)
488{
489 int expand;
Mark Fashehccd979b2005-12-15 14:31:24 -0800490
491 /* The block may very well exist in our cache already, so avoid
492 * doing any more work in that case. */
Joel Becker8cb471e2009-02-10 20:00:41 -0800493 if (ocfs2_buffer_cached(ci, bh))
Mark Fashehccd979b2005-12-15 14:31:24 -0800494 return;
495
Joel Becker6e5a3d72009-02-10 19:00:37 -0800496 mlog(0, "Owner %llu, inserting block %llu\n",
497 (unsigned long long)ocfs2_metadata_cache_owner(ci),
Mark Fashehb06970532006-03-03 10:24:33 -0800498 (unsigned long long)bh->b_blocknr);
Mark Fashehccd979b2005-12-15 14:31:24 -0800499
500 /* No need to recheck under spinlock - insertion is guarded by
Joel Becker6e5a3d72009-02-10 19:00:37 -0800501 * co_io_lock() */
502 ocfs2_metadata_cache_lock(ci);
Joel Becker8cb471e2009-02-10 20:00:41 -0800503 if (ocfs2_insert_can_use_array(ci)) {
Mark Fashehccd979b2005-12-15 14:31:24 -0800504 /* Fast case - it's an array and there's a free
505 * spot. */
506 ocfs2_append_cache_array(ci, bh->b_blocknr);
Joel Becker6e5a3d72009-02-10 19:00:37 -0800507 ocfs2_metadata_cache_unlock(ci);
Mark Fashehccd979b2005-12-15 14:31:24 -0800508 return;
509 }
510
511 expand = 0;
Joel Becker47460d62009-02-10 16:05:07 -0800512 if (ci->ci_flags & OCFS2_CACHE_FL_INLINE) {
Mark Fashehccd979b2005-12-15 14:31:24 -0800513 /* We need to bump things up to a tree. */
514 expand = 1;
515 }
Joel Becker6e5a3d72009-02-10 19:00:37 -0800516 ocfs2_metadata_cache_unlock(ci);
Mark Fashehccd979b2005-12-15 14:31:24 -0800517
Joel Becker8cb471e2009-02-10 20:00:41 -0800518 __ocfs2_set_buffer_uptodate(ci, bh->b_blocknr, expand);
Mark Fashehccd979b2005-12-15 14:31:24 -0800519}
520
521/* Called against a newly allocated buffer. Most likely nobody should
522 * be able to read this sort of metadata while it's still being
Joel Becker6e5a3d72009-02-10 19:00:37 -0800523 * allocated, but this is careful to take co_io_lock() anyway. */
Joel Becker8cb471e2009-02-10 20:00:41 -0800524void ocfs2_set_new_buffer_uptodate(struct ocfs2_caching_info *ci,
Mark Fashehccd979b2005-12-15 14:31:24 -0800525 struct buffer_head *bh)
526{
Mark Fashehccd979b2005-12-15 14:31:24 -0800527 /* This should definitely *not* exist in our cache */
Joel Becker8cb471e2009-02-10 20:00:41 -0800528 BUG_ON(ocfs2_buffer_cached(ci, bh));
Mark Fashehccd979b2005-12-15 14:31:24 -0800529
530 set_buffer_uptodate(bh);
531
Joel Becker6e5a3d72009-02-10 19:00:37 -0800532 ocfs2_metadata_cache_io_lock(ci);
Joel Becker8cb471e2009-02-10 20:00:41 -0800533 ocfs2_set_buffer_uptodate(ci, bh);
Joel Becker6e5a3d72009-02-10 19:00:37 -0800534 ocfs2_metadata_cache_io_unlock(ci);
Mark Fashehccd979b2005-12-15 14:31:24 -0800535}
536
537/* Requires ip_lock. */
538static void ocfs2_remove_metadata_array(struct ocfs2_caching_info *ci,
539 int index)
540{
541 sector_t *array = ci->ci_cache.ci_array;
542 int bytes;
543
Joel Becker47460d62009-02-10 16:05:07 -0800544 BUG_ON(index < 0 || index >= OCFS2_CACHE_INFO_MAX_ARRAY);
Mark Fashehccd979b2005-12-15 14:31:24 -0800545 BUG_ON(index >= ci->ci_num_cached);
546 BUG_ON(!ci->ci_num_cached);
547
548 mlog(0, "remove index %d (num_cached = %u\n", index,
549 ci->ci_num_cached);
550
551 ci->ci_num_cached--;
552
553 /* don't need to copy if the array is now empty, or if we
554 * removed at the tail */
555 if (ci->ci_num_cached && index < ci->ci_num_cached) {
556 bytes = sizeof(sector_t) * (ci->ci_num_cached - index);
557 memmove(&array[index], &array[index + 1], bytes);
558 }
559}
560
561/* Requires ip_lock. */
562static void ocfs2_remove_metadata_tree(struct ocfs2_caching_info *ci,
563 struct ocfs2_meta_cache_item *item)
564{
565 mlog(0, "remove block %llu from tree\n",
566 (unsigned long long) item->c_block);
567
568 rb_erase(&item->c_node, &ci->ci_cache.ci_tree);
569 ci->ci_num_cached--;
570}
571
Joel Becker8cb471e2009-02-10 20:00:41 -0800572static void ocfs2_remove_block_from_cache(struct ocfs2_caching_info *ci,
Tao Maac11c822008-08-18 17:38:47 +0800573 sector_t block)
Mark Fashehccd979b2005-12-15 14:31:24 -0800574{
575 int index;
Mark Fashehccd979b2005-12-15 14:31:24 -0800576 struct ocfs2_meta_cache_item *item = NULL;
Mark Fashehccd979b2005-12-15 14:31:24 -0800577
Joel Becker6e5a3d72009-02-10 19:00:37 -0800578 ocfs2_metadata_cache_lock(ci);
579 mlog(0, "Owner %llu, remove %llu, items = %u, array = %u\n",
580 (unsigned long long)ocfs2_metadata_cache_owner(ci),
Mark Fashehb06970532006-03-03 10:24:33 -0800581 (unsigned long long) block, ci->ci_num_cached,
Joel Becker47460d62009-02-10 16:05:07 -0800582 ci->ci_flags & OCFS2_CACHE_FL_INLINE);
Mark Fashehccd979b2005-12-15 14:31:24 -0800583
Joel Becker47460d62009-02-10 16:05:07 -0800584 if (ci->ci_flags & OCFS2_CACHE_FL_INLINE) {
Mark Fashehccd979b2005-12-15 14:31:24 -0800585 index = ocfs2_search_cache_array(ci, block);
586 if (index != -1)
587 ocfs2_remove_metadata_array(ci, index);
588 } else {
589 item = ocfs2_search_cache_tree(ci, block);
590 if (item)
591 ocfs2_remove_metadata_tree(ci, item);
592 }
Joel Becker6e5a3d72009-02-10 19:00:37 -0800593 ocfs2_metadata_cache_unlock(ci);
Mark Fashehccd979b2005-12-15 14:31:24 -0800594
595 if (item)
596 kmem_cache_free(ocfs2_uptodate_cachep, item);
597}
598
Tao Maac11c822008-08-18 17:38:47 +0800599/*
600 * Called when we remove a chunk of metadata from an inode. We don't
601 * bother reverting things to an inlined array in the case of a remove
602 * which moves us back under the limit.
603 */
Joel Becker8cb471e2009-02-10 20:00:41 -0800604void ocfs2_remove_from_cache(struct ocfs2_caching_info *ci,
Tao Maac11c822008-08-18 17:38:47 +0800605 struct buffer_head *bh)
606{
607 sector_t block = bh->b_blocknr;
608
Joel Becker8cb471e2009-02-10 20:00:41 -0800609 ocfs2_remove_block_from_cache(ci, block);
Tao Maac11c822008-08-18 17:38:47 +0800610}
611
612/* Called when we remove xattr clusters from an inode. */
Joel Becker8cb471e2009-02-10 20:00:41 -0800613void ocfs2_remove_xattr_clusters_from_cache(struct ocfs2_caching_info *ci,
Tao Maac11c822008-08-18 17:38:47 +0800614 sector_t block,
615 u32 c_len)
616{
Joel Becker8cb471e2009-02-10 20:00:41 -0800617 struct super_block *sb = ocfs2_metadata_cache_get_super(ci);
618 unsigned int i, b_len = ocfs2_clusters_to_blocks(sb, 1) * c_len;
Tao Maac11c822008-08-18 17:38:47 +0800619
620 for (i = 0; i < b_len; i++, block++)
Joel Becker8cb471e2009-02-10 20:00:41 -0800621 ocfs2_remove_block_from_cache(ci, block);
Tao Maac11c822008-08-18 17:38:47 +0800622}
623
Mark Fashehccd979b2005-12-15 14:31:24 -0800624int __init init_ocfs2_uptodate_cache(void)
625{
626 ocfs2_uptodate_cachep = kmem_cache_create("ocfs2_uptodate",
627 sizeof(struct ocfs2_meta_cache_item),
Paul Mundt20c2df82007-07-20 10:11:58 +0900628 0, SLAB_HWCACHE_ALIGN, NULL);
Mark Fashehccd979b2005-12-15 14:31:24 -0800629 if (!ocfs2_uptodate_cachep)
630 return -ENOMEM;
631
632 mlog(0, "%u inlined cache items per inode.\n",
Joel Becker47460d62009-02-10 16:05:07 -0800633 OCFS2_CACHE_INFO_MAX_ARRAY);
Mark Fashehccd979b2005-12-15 14:31:24 -0800634
635 return 0;
636}
637
Adrian Bunk0c6c98f2006-01-07 20:07:02 +0100638void exit_ocfs2_uptodate_cache(void)
Mark Fashehccd979b2005-12-15 14:31:24 -0800639{
640 if (ocfs2_uptodate_cachep)
641 kmem_cache_destroy(ocfs2_uptodate_cachep);
642}