blob: 226d0429fd7f0938ef26c25b7ec9db333b58ba50 [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 Becker6e5a3d72009-02-10 19:00:37 -080078static u64 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
85static 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
92static 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
99static 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
106static 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
114void 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 Becker47460d62009-02-10 16:05:07 -0800120 ci->ci_flags |= OCFS2_CACHE_FL_INLINE;
Mark Fashehccd979b2005-12-15 14:31:24 -0800121 ci->ci_num_cached = 0;
122}
123
124/* No lock taken here as 'root' is not expected to be visible to other
125 * processes. */
126static 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 */
152void 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 Becker6e5a3d72009-02-10 19:00:37 -0800159 BUG_ON(!ci || !ci->ci_ops);
160
161 ocfs2_metadata_cache_lock(ci);
Joel Becker47460d62009-02-10 16:05:07 -0800162 tree = !(ci->ci_flags & OCFS2_CACHE_FL_INLINE);
Mark Fashehccd979b2005-12-15 14:31:24 -0800163 to_purge = ci->ci_num_cached;
164
Joel Becker6e5a3d72009-02-10 19:00:37 -0800165 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 Fashehccd979b2005-12-15 14:31:24 -0800168
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 Becker6e5a3d72009-02-10 19:00:37 -0800175 ocfs2_metadata_cache_init(ci, ci->ci_ops);
176 ocfs2_metadata_cache_unlock(ci);
Mark Fashehccd979b2005-12-15 14:31:24 -0800177
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 Becker6e5a3d72009-02-10 19:00:37 -0800183 mlog(ML_ERROR, "Owner %llu, count = %u, purged = %u\n",
184 (unsigned long long)ocfs2_metadata_cache_owner(ci),
185 to_purge, purged);
Mark Fashehccd979b2005-12-15 14:31:24 -0800186}
187
188/* Returns the index in the cache array, -1 if not found.
189 * Requires ip_lock. */
190static 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. */
205static struct ocfs2_meta_cache_item *
206ocfs2_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
226static 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 Becker47460d62009-02-10 16:05:07 -0800231 struct ocfs2_caching_info *ci = &oi->ip_metadata_cache;
Mark Fashehccd979b2005-12-15 14:31:24 -0800232
Joel Becker6e5a3d72009-02-10 19:00:37 -0800233 ocfs2_metadata_cache_lock(ci);
Mark Fashehccd979b2005-12-15 14:31:24 -0800234
Joel Becker6e5a3d72009-02-10 19:00:37 -0800235 mlog(0, "Owner %llu, query block %llu (inline = %u)\n",
236 (unsigned long long)ocfs2_metadata_cache_owner(ci),
Mark Fashehb06970532006-03-03 10:24:33 -0800237 (unsigned long long) bh->b_blocknr,
Joel Becker47460d62009-02-10 16:05:07 -0800238 !!(ci->ci_flags & OCFS2_CACHE_FL_INLINE));
Mark Fashehccd979b2005-12-15 14:31:24 -0800239
Joel Becker47460d62009-02-10 16:05:07 -0800240 if (ci->ci_flags & OCFS2_CACHE_FL_INLINE)
Mark Fashehccd979b2005-12-15 14:31:24 -0800241 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 Becker6e5a3d72009-02-10 19:00:37 -0800247 ocfs2_metadata_cache_unlock(ci);
Mark Fashehccd979b2005-12-15 14:31:24 -0800248
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 Fashehaa958872006-04-21 13:49:02 -0700255 * the block is stored in our inode metadata cache.
256 *
257 * This can be called under lock_buffer()
258 */
Mark Fashehccd979b2005-12-15 14:31:24 -0800259int 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 Fashehaa958872006-04-21 13:49:02 -0700278/*
279 * Determine whether a buffer is currently out on a read-ahead request.
Joel Becker47460d62009-02-10 16:05:07 -0800280 * ci_io_sem should be held to serialize submitters with the logic here.
Mark Fashehaa958872006-04-21 13:49:02 -0700281 */
282int 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 Fashehccd979b2005-12-15 14:31:24 -0800288/* Requires ip_lock */
289static void ocfs2_append_cache_array(struct ocfs2_caching_info *ci,
290 sector_t block)
291{
Joel Becker47460d62009-02-10 16:05:07 -0800292 BUG_ON(ci->ci_num_cached >= OCFS2_CACHE_INFO_MAX_ARRAY);
Mark Fashehccd979b2005-12-15 14:31:24 -0800293
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. */
304static 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 Becker6e5a3d72009-02-10 19:00:37 -0800337/* co_cache_lock() must be held */
Mark Fashehccd979b2005-12-15 14:31:24 -0800338static inline int ocfs2_insert_can_use_array(struct ocfs2_inode_info *oi,
339 struct ocfs2_caching_info *ci)
340{
Joel Becker47460d62009-02-10 16:05:07 -0800341 return (ci->ci_flags & OCFS2_CACHE_FL_INLINE) &&
342 (ci->ci_num_cached < OCFS2_CACHE_INFO_MAX_ARRAY);
Mark Fashehccd979b2005-12-15 14:31:24 -0800343}
344
Joel Becker47460d62009-02-10 16:05:07 -0800345/* tree should be exactly OCFS2_CACHE_INFO_MAX_ARRAY wide. NULL the
Mark Fashehccd979b2005-12-15 14:31:24 -0800346 * pointers in tree after we use them - this allows caller to detect
Joel Becker6e5a3d72009-02-10 19:00:37 -0800347 * when to free in case of error.
348 *
349 * The co_cache_lock() must be held. */
Mark Fashehccd979b2005-12-15 14:31:24 -0800350static 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 Becker47460d62009-02-10 16:05:07 -0800356 mlog_bug_on_msg(ci->ci_num_cached != OCFS2_CACHE_INFO_MAX_ARRAY,
Joel Becker6e5a3d72009-02-10 19:00:37 -0800357 "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 Becker47460d62009-02-10 16:05:07 -0800360 mlog_bug_on_msg(!(ci->ci_flags & OCFS2_CACHE_FL_INLINE),
Joel Becker6e5a3d72009-02-10 19:00:37 -0800361 "Owner %llu not marked as inline anymore!\n",
362 (unsigned long long)ocfs2_metadata_cache_owner(ci));
Mark Fashehccd979b2005-12-15 14:31:24 -0800363
364 /* Be careful to initialize the tree members *first* because
365 * once the ci_tree is used, the array is junk... */
Joel Becker47460d62009-02-10 16:05:07 -0800366 for (i = 0; i < OCFS2_CACHE_INFO_MAX_ARRAY; i++)
Mark Fashehccd979b2005-12-15 14:31:24 -0800367 tree[i]->c_block = ci->ci_cache.ci_array[i];
368
Joel Becker47460d62009-02-10 16:05:07 -0800369 ci->ci_flags &= ~OCFS2_CACHE_FL_INLINE;
Mark Fashehccd979b2005-12-15 14:31:24 -0800370 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 Becker47460d62009-02-10 16:05:07 -0800374 for (i = 0; i < OCFS2_CACHE_INFO_MAX_ARRAY; i++) {
Mark Fashehccd979b2005-12-15 14:31:24 -0800375 __ocfs2_insert_cache_tree(ci, tree[i]);
376 tree[i] = NULL;
377 }
378
Mark Fashehb06970532006-03-03 10:24:33 -0800379 mlog(0, "Expanded %llu to a tree cache: flags 0x%x, num = %u\n",
Joel Becker6e5a3d72009-02-10 19:00:37 -0800380 (unsigned long long)ocfs2_metadata_cache_owner(ci),
381 ci->ci_flags, ci->ci_num_cached);
Mark Fashehccd979b2005-12-15 14:31:24 -0800382}
383
384/* Slow path function - memory allocation is necessary. See the
385 * comment above ocfs2_set_buffer_uptodate for more information. */
386static 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 Becker47460d62009-02-10 16:05:07 -0800393 struct ocfs2_meta_cache_item *tree[OCFS2_CACHE_INFO_MAX_ARRAY] =
Mark Fashehccd979b2005-12-15 14:31:24 -0800394 { NULL, };
395
Joel Becker6e5a3d72009-02-10 19:00:37 -0800396 mlog(0, "Owner %llu, block %llu, expand = %d\n",
397 (unsigned long long)ocfs2_metadata_cache_owner(ci),
Mark Fashehb06970532006-03-03 10:24:33 -0800398 (unsigned long long)block, expand_tree);
Mark Fashehccd979b2005-12-15 14:31:24 -0800399
Sunil Mushranafae00ab2006-04-12 14:37:00 -0700400 new = kmem_cache_alloc(ocfs2_uptodate_cachep, GFP_NOFS);
Mark Fashehccd979b2005-12-15 14:31:24 -0800401 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 Becker47460d62009-02-10 16:05:07 -0800410 for (i = 0; i < OCFS2_CACHE_INFO_MAX_ARRAY; i++) {
Mark Fashehccd979b2005-12-15 14:31:24 -0800411 tree[i] = kmem_cache_alloc(ocfs2_uptodate_cachep,
Sunil Mushranafae00ab2006-04-12 14:37:00 -0700412 GFP_NOFS);
Mark Fashehccd979b2005-12-15 14:31:24 -0800413 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 Becker6e5a3d72009-02-10 19:00:37 -0800422 ocfs2_metadata_cache_lock(ci);
Mark Fashehccd979b2005-12-15 14:31:24 -0800423 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 Becker6e5a3d72009-02-10 19:00:37 -0800428 ocfs2_metadata_cache_unlock(ci);
Mark Fashehccd979b2005-12-15 14:31:24 -0800429 goto out_free;
430 }
431
432 if (expand_tree)
433 ocfs2_expand_cache(oi, tree);
434
435 __ocfs2_insert_cache_tree(ci, new);
Joel Becker6e5a3d72009-02-10 19:00:37 -0800436 ocfs2_metadata_cache_unlock(ci);
Mark Fashehccd979b2005-12-15 14:31:24 -0800437
438 new = NULL;
439out_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 Becker47460d62009-02-10 16:05:07 -0800446 for (i = 0; i < OCFS2_CACHE_INFO_MAX_ARRAY; i++)
Mark Fashehccd979b2005-12-15 14:31:24 -0800447 if (tree[i])
448 kmem_cache_free(ocfs2_uptodate_cachep,
449 tree[i]);
450 }
451}
452
Joel Becker6e5a3d72009-02-10 19:00:37 -0800453/* Item insertion is guarded by co_io_lock(), so the insertion path takes
Mark Fashehccd979b2005-12-15 14:31:24 -0800454 * 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 Fashehaa958872006-04-21 13:49:02 -0700466 * result in a performance penalty)
467 *
468 * Readahead buffers can be passed in here before the I/O request is
469 * completed.
470 */
Mark Fashehccd979b2005-12-15 14:31:24 -0800471void 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 Becker6e5a3d72009-02-10 19:00:37 -0800483 mlog(0, "Owner %llu, inserting block %llu\n",
484 (unsigned long long)ocfs2_metadata_cache_owner(ci),
Mark Fashehb06970532006-03-03 10:24:33 -0800485 (unsigned long long)bh->b_blocknr);
Mark Fashehccd979b2005-12-15 14:31:24 -0800486
487 /* No need to recheck under spinlock - insertion is guarded by
Joel Becker6e5a3d72009-02-10 19:00:37 -0800488 * co_io_lock() */
489 ocfs2_metadata_cache_lock(ci);
Mark Fashehccd979b2005-12-15 14:31:24 -0800490 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 Becker6e5a3d72009-02-10 19:00:37 -0800494 ocfs2_metadata_cache_unlock(ci);
Mark Fashehccd979b2005-12-15 14:31:24 -0800495 return;
496 }
497
498 expand = 0;
Joel Becker47460d62009-02-10 16:05:07 -0800499 if (ci->ci_flags & OCFS2_CACHE_FL_INLINE) {
Mark Fashehccd979b2005-12-15 14:31:24 -0800500 /* We need to bump things up to a tree. */
501 expand = 1;
502 }
Joel Becker6e5a3d72009-02-10 19:00:37 -0800503 ocfs2_metadata_cache_unlock(ci);
Mark Fashehccd979b2005-12-15 14:31:24 -0800504
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 Becker6e5a3d72009-02-10 19:00:37 -0800510 * allocated, but this is careful to take co_io_lock() anyway. */
Mark Fashehccd979b2005-12-15 14:31:24 -0800511void ocfs2_set_new_buffer_uptodate(struct inode *inode,
512 struct buffer_head *bh)
513{
514 struct ocfs2_inode_info *oi = OCFS2_I(inode);
Joel Becker47460d62009-02-10 16:05:07 -0800515 struct ocfs2_caching_info *ci = &oi->ip_metadata_cache;
Mark Fashehccd979b2005-12-15 14:31:24 -0800516
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 Becker6e5a3d72009-02-10 19:00:37 -0800522 ocfs2_metadata_cache_io_lock(ci);
Mark Fashehccd979b2005-12-15 14:31:24 -0800523 ocfs2_set_buffer_uptodate(inode, bh);
Joel Becker6e5a3d72009-02-10 19:00:37 -0800524 ocfs2_metadata_cache_io_unlock(ci);
Mark Fashehccd979b2005-12-15 14:31:24 -0800525}
526
527/* Requires ip_lock. */
528static 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 Becker47460d62009-02-10 16:05:07 -0800534 BUG_ON(index < 0 || index >= OCFS2_CACHE_INFO_MAX_ARRAY);
Mark Fashehccd979b2005-12-15 14:31:24 -0800535 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. */
552static 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 Maac11c822008-08-18 17:38:47 +0800562static void ocfs2_remove_block_from_cache(struct inode *inode,
563 sector_t block)
Mark Fashehccd979b2005-12-15 14:31:24 -0800564{
565 int index;
Mark Fashehccd979b2005-12-15 14:31:24 -0800566 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 Becker6e5a3d72009-02-10 19:00:37 -0800570 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 Fashehb06970532006-03-03 10:24:33 -0800573 (unsigned long long) block, ci->ci_num_cached,
Joel Becker47460d62009-02-10 16:05:07 -0800574 ci->ci_flags & OCFS2_CACHE_FL_INLINE);
Mark Fashehccd979b2005-12-15 14:31:24 -0800575
Joel Becker47460d62009-02-10 16:05:07 -0800576 if (ci->ci_flags & OCFS2_CACHE_FL_INLINE) {
Mark Fashehccd979b2005-12-15 14:31:24 -0800577 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 Becker6e5a3d72009-02-10 19:00:37 -0800585 ocfs2_metadata_cache_unlock(ci);
Mark Fashehccd979b2005-12-15 14:31:24 -0800586
587 if (item)
588 kmem_cache_free(ocfs2_uptodate_cachep, item);
589}
590
Tao Maac11c822008-08-18 17:38:47 +0800591/*
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 */
596void 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. */
605void ocfs2_remove_xattr_clusters_from_cache(struct inode *inode,
606 sector_t block,
607 u32 c_len)
608{
Mark Fashehfd8351f2008-10-07 12:50:46 -0700609 unsigned int i, b_len = ocfs2_clusters_to_blocks(inode->i_sb, 1) * c_len;
Tao Maac11c822008-08-18 17:38:47 +0800610
611 for (i = 0; i < b_len; i++, block++)
612 ocfs2_remove_block_from_cache(inode, block);
613}
614
Mark Fashehccd979b2005-12-15 14:31:24 -0800615int __init init_ocfs2_uptodate_cache(void)
616{
617 ocfs2_uptodate_cachep = kmem_cache_create("ocfs2_uptodate",
618 sizeof(struct ocfs2_meta_cache_item),
Paul Mundt20c2df82007-07-20 10:11:58 +0900619 0, SLAB_HWCACHE_ALIGN, NULL);
Mark Fashehccd979b2005-12-15 14:31:24 -0800620 if (!ocfs2_uptodate_cachep)
621 return -ENOMEM;
622
623 mlog(0, "%u inlined cache items per inode.\n",
Joel Becker47460d62009-02-10 16:05:07 -0800624 OCFS2_CACHE_INFO_MAX_ARRAY);
Mark Fashehccd979b2005-12-15 14:31:24 -0800625
626 return 0;
627}
628
Adrian Bunk0c6c98f2006-01-07 20:07:02 +0100629void exit_ocfs2_uptodate_cache(void)
Mark Fashehccd979b2005-12-15 14:31:24 -0800630{
631 if (ocfs2_uptodate_cachep)
632 kmem_cache_destroy(ocfs2_uptodate_cachep);
633}