Commit | Line | Data |
---|---|---|
ccd979bd MF |
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> | |
2b4e30fb JB |
56 | #ifndef CONFIG_OCFS2_COMPAT_JBD |
57 | # include <linux/jbd2.h> | |
58 | #else | |
59 | # include <linux/jbd.h> | |
60 | #endif | |
ccd979bd MF |
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 | ||
e18b890b | 76 | static struct kmem_cache *ocfs2_uptodate_cachep = NULL; |
ccd979bd | 77 | |
8cb471e8 | 78 | u64 ocfs2_metadata_cache_owner(struct ocfs2_caching_info *ci) |
6e5a3d75 JB |
79 | { |
80 | BUG_ON(!ci || !ci->ci_ops); | |
81 | ||
82 | return ci->ci_ops->co_owner(ci); | |
83 | } | |
84 | ||
8cb471e8 JB |
85 | struct 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 | ||
6e5a3d75 JB |
92 | static 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 | ||
99 | static 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 | ||
8cb471e8 | 106 | void ocfs2_metadata_cache_io_lock(struct ocfs2_caching_info *ci) |
6e5a3d75 JB |
107 | { |
108 | BUG_ON(!ci || !ci->ci_ops); | |
109 | ||
110 | ci->ci_ops->co_io_lock(ci); | |
111 | } | |
112 | ||
8cb471e8 | 113 | void ocfs2_metadata_cache_io_unlock(struct ocfs2_caching_info *ci) |
6e5a3d75 JB |
114 | { |
115 | BUG_ON(!ci || !ci->ci_ops); | |
116 | ||
117 | ci->ci_ops->co_io_unlock(ci); | |
118 | } | |
119 | ||
120 | ||
47460d65 | 121 | void ocfs2_metadata_cache_init(struct ocfs2_caching_info *ci, |
6e5a3d75 | 122 | const struct ocfs2_caching_operations *ops) |
ccd979bd | 123 | { |
6e5a3d75 JB |
124 | BUG_ON(!ops); |
125 | ||
126 | ci->ci_ops = ops; | |
47460d65 | 127 | ci->ci_flags |= OCFS2_CACHE_FL_INLINE; |
ccd979bd MF |
128 | ci->ci_num_cached = 0; |
129 | } | |
130 | ||
131 | /* No lock taken here as 'root' is not expected to be visible to other | |
132 | * processes. */ | |
133 | static unsigned int ocfs2_purge_copied_metadata_tree(struct rb_root *root) | |
134 | { | |
135 | unsigned int purged = 0; | |
136 | struct rb_node *node; | |
137 | struct ocfs2_meta_cache_item *item; | |
138 | ||
139 | while ((node = rb_last(root)) != NULL) { | |
140 | item = rb_entry(node, struct ocfs2_meta_cache_item, c_node); | |
141 | ||
142 | mlog(0, "Purge item %llu\n", | |
143 | (unsigned long long) item->c_block); | |
144 | ||
145 | rb_erase(&item->c_node, root); | |
146 | kmem_cache_free(ocfs2_uptodate_cachep, item); | |
147 | ||
148 | purged++; | |
149 | } | |
150 | return purged; | |
151 | } | |
152 | ||
153 | /* Called from locking and called from ocfs2_clear_inode. Dump the | |
154 | * cache for a given inode. | |
155 | * | |
156 | * This function is a few more lines longer than necessary due to some | |
157 | * accounting done here, but I think it's worth tracking down those | |
158 | * bugs sooner -- Mark */ | |
8cb471e8 | 159 | void ocfs2_metadata_cache_purge(struct ocfs2_caching_info *ci) |
ccd979bd | 160 | { |
ccd979bd | 161 | unsigned int tree, to_purge, purged; |
ccd979bd MF |
162 | struct rb_root root = RB_ROOT; |
163 | ||
6e5a3d75 JB |
164 | BUG_ON(!ci || !ci->ci_ops); |
165 | ||
166 | ocfs2_metadata_cache_lock(ci); | |
47460d65 | 167 | tree = !(ci->ci_flags & OCFS2_CACHE_FL_INLINE); |
ccd979bd MF |
168 | to_purge = ci->ci_num_cached; |
169 | ||
6e5a3d75 JB |
170 | mlog(0, "Purge %u %s items from Owner %llu\n", to_purge, |
171 | tree ? "array" : "tree", | |
172 | (unsigned long long)ocfs2_metadata_cache_owner(ci)); | |
ccd979bd MF |
173 | |
174 | /* If we're a tree, save off the root so that we can safely | |
175 | * initialize the cache. We do the work to free tree members | |
176 | * without the spinlock. */ | |
177 | if (tree) | |
178 | root = ci->ci_cache.ci_tree; | |
179 | ||
6e5a3d75 JB |
180 | ocfs2_metadata_cache_init(ci, ci->ci_ops); |
181 | ocfs2_metadata_cache_unlock(ci); | |
ccd979bd MF |
182 | |
183 | purged = ocfs2_purge_copied_metadata_tree(&root); | |
184 | /* If possible, track the number wiped so that we can more | |
185 | * easily detect counting errors. Unfortunately, this is only | |
186 | * meaningful for trees. */ | |
187 | if (tree && purged != to_purge) | |
6e5a3d75 JB |
188 | mlog(ML_ERROR, "Owner %llu, count = %u, purged = %u\n", |
189 | (unsigned long long)ocfs2_metadata_cache_owner(ci), | |
190 | to_purge, purged); | |
ccd979bd MF |
191 | } |
192 | ||
193 | /* Returns the index in the cache array, -1 if not found. | |
194 | * Requires ip_lock. */ | |
195 | static int ocfs2_search_cache_array(struct ocfs2_caching_info *ci, | |
196 | sector_t item) | |
197 | { | |
198 | int i; | |
199 | ||
200 | for (i = 0; i < ci->ci_num_cached; i++) { | |
201 | if (item == ci->ci_cache.ci_array[i]) | |
202 | return i; | |
203 | } | |
204 | ||
205 | return -1; | |
206 | } | |
207 | ||
208 | /* Returns the cache item if found, otherwise NULL. | |
209 | * Requires ip_lock. */ | |
210 | static struct ocfs2_meta_cache_item * | |
211 | ocfs2_search_cache_tree(struct ocfs2_caching_info *ci, | |
212 | sector_t block) | |
213 | { | |
214 | struct rb_node * n = ci->ci_cache.ci_tree.rb_node; | |
215 | struct ocfs2_meta_cache_item *item = NULL; | |
216 | ||
217 | while (n) { | |
218 | item = rb_entry(n, struct ocfs2_meta_cache_item, c_node); | |
219 | ||
220 | if (block < item->c_block) | |
221 | n = n->rb_left; | |
222 | else if (block > item->c_block) | |
223 | n = n->rb_right; | |
224 | else | |
225 | return item; | |
226 | } | |
227 | ||
228 | return NULL; | |
229 | } | |
230 | ||
8cb471e8 | 231 | static int ocfs2_buffer_cached(struct ocfs2_caching_info *ci, |
ccd979bd MF |
232 | struct buffer_head *bh) |
233 | { | |
234 | int index = -1; | |
235 | struct ocfs2_meta_cache_item *item = NULL; | |
236 | ||
6e5a3d75 | 237 | ocfs2_metadata_cache_lock(ci); |
ccd979bd | 238 | |
6e5a3d75 JB |
239 | mlog(0, "Owner %llu, query block %llu (inline = %u)\n", |
240 | (unsigned long long)ocfs2_metadata_cache_owner(ci), | |
b0697053 | 241 | (unsigned long long) bh->b_blocknr, |
47460d65 | 242 | !!(ci->ci_flags & OCFS2_CACHE_FL_INLINE)); |
ccd979bd | 243 | |
47460d65 | 244 | if (ci->ci_flags & OCFS2_CACHE_FL_INLINE) |
8cb471e8 | 245 | index = ocfs2_search_cache_array(ci, bh->b_blocknr); |
ccd979bd | 246 | else |
8cb471e8 | 247 | item = ocfs2_search_cache_tree(ci, bh->b_blocknr); |
ccd979bd | 248 | |
6e5a3d75 | 249 | ocfs2_metadata_cache_unlock(ci); |
ccd979bd MF |
250 | |
251 | mlog(0, "index = %d, item = %p\n", index, item); | |
252 | ||
253 | return (index != -1) || (item != NULL); | |
254 | } | |
255 | ||
256 | /* Warning: even if it returns true, this does *not* guarantee that | |
aa958874 MF |
257 | * the block is stored in our inode metadata cache. |
258 | * | |
259 | * This can be called under lock_buffer() | |
260 | */ | |
8cb471e8 | 261 | int ocfs2_buffer_uptodate(struct ocfs2_caching_info *ci, |
ccd979bd MF |
262 | struct buffer_head *bh) |
263 | { | |
264 | /* Doesn't matter if the bh is in our cache or not -- if it's | |
265 | * not marked uptodate then we know it can't have correct | |
266 | * data. */ | |
267 | if (!buffer_uptodate(bh)) | |
268 | return 0; | |
269 | ||
270 | /* OCFS2 does not allow multiple nodes to be changing the same | |
271 | * block at the same time. */ | |
272 | if (buffer_jbd(bh)) | |
273 | return 1; | |
274 | ||
275 | /* Ok, locally the buffer is marked as up to date, now search | |
276 | * our cache to see if we can trust that. */ | |
8cb471e8 | 277 | return ocfs2_buffer_cached(ci, bh); |
ccd979bd MF |
278 | } |
279 | ||
8cb471e8 | 280 | /* |
aa958874 | 281 | * Determine whether a buffer is currently out on a read-ahead request. |
47460d65 | 282 | * ci_io_sem should be held to serialize submitters with the logic here. |
aa958874 | 283 | */ |
8cb471e8 | 284 | int ocfs2_buffer_read_ahead(struct ocfs2_caching_info *ci, |
aa958874 MF |
285 | struct buffer_head *bh) |
286 | { | |
8cb471e8 | 287 | return buffer_locked(bh) && ocfs2_buffer_cached(ci, bh); |
aa958874 MF |
288 | } |
289 | ||
ccd979bd MF |
290 | /* Requires ip_lock */ |
291 | static void ocfs2_append_cache_array(struct ocfs2_caching_info *ci, | |
292 | sector_t block) | |
293 | { | |
47460d65 | 294 | BUG_ON(ci->ci_num_cached >= OCFS2_CACHE_INFO_MAX_ARRAY); |
ccd979bd MF |
295 | |
296 | mlog(0, "block %llu takes position %u\n", (unsigned long long) block, | |
297 | ci->ci_num_cached); | |
298 | ||
299 | ci->ci_cache.ci_array[ci->ci_num_cached] = block; | |
300 | ci->ci_num_cached++; | |
301 | } | |
302 | ||
303 | /* By now the caller should have checked that the item does *not* | |
304 | * exist in the tree. | |
305 | * Requires ip_lock. */ | |
306 | static void __ocfs2_insert_cache_tree(struct ocfs2_caching_info *ci, | |
307 | struct ocfs2_meta_cache_item *new) | |
308 | { | |
309 | sector_t block = new->c_block; | |
310 | struct rb_node *parent = NULL; | |
311 | struct rb_node **p = &ci->ci_cache.ci_tree.rb_node; | |
312 | struct ocfs2_meta_cache_item *tmp; | |
313 | ||
314 | mlog(0, "Insert block %llu num = %u\n", (unsigned long long) block, | |
315 | ci->ci_num_cached); | |
316 | ||
317 | while(*p) { | |
318 | parent = *p; | |
319 | ||
320 | tmp = rb_entry(parent, struct ocfs2_meta_cache_item, c_node); | |
321 | ||
322 | if (block < tmp->c_block) | |
323 | p = &(*p)->rb_left; | |
324 | else if (block > tmp->c_block) | |
325 | p = &(*p)->rb_right; | |
326 | else { | |
327 | /* This should never happen! */ | |
328 | mlog(ML_ERROR, "Duplicate block %llu cached!\n", | |
329 | (unsigned long long) block); | |
330 | BUG(); | |
331 | } | |
332 | } | |
333 | ||
334 | rb_link_node(&new->c_node, parent, p); | |
335 | rb_insert_color(&new->c_node, &ci->ci_cache.ci_tree); | |
336 | ci->ci_num_cached++; | |
337 | } | |
338 | ||
6e5a3d75 | 339 | /* co_cache_lock() must be held */ |
8cb471e8 | 340 | static inline int ocfs2_insert_can_use_array(struct ocfs2_caching_info *ci) |
ccd979bd | 341 | { |
47460d65 JB |
342 | return (ci->ci_flags & OCFS2_CACHE_FL_INLINE) && |
343 | (ci->ci_num_cached < OCFS2_CACHE_INFO_MAX_ARRAY); | |
ccd979bd MF |
344 | } |
345 | ||
47460d65 | 346 | /* tree should be exactly OCFS2_CACHE_INFO_MAX_ARRAY wide. NULL the |
ccd979bd | 347 | * pointers in tree after we use them - this allows caller to detect |
6e5a3d75 JB |
348 | * when to free in case of error. |
349 | * | |
350 | * The co_cache_lock() must be held. */ | |
8cb471e8 | 351 | static void ocfs2_expand_cache(struct ocfs2_caching_info *ci, |
ccd979bd MF |
352 | struct ocfs2_meta_cache_item **tree) |
353 | { | |
354 | int i; | |
ccd979bd | 355 | |
47460d65 | 356 | mlog_bug_on_msg(ci->ci_num_cached != OCFS2_CACHE_INFO_MAX_ARRAY, |
6e5a3d75 JB |
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); | |
47460d65 | 360 | mlog_bug_on_msg(!(ci->ci_flags & OCFS2_CACHE_FL_INLINE), |
6e5a3d75 JB |
361 | "Owner %llu not marked as inline anymore!\n", |
362 | (unsigned long long)ocfs2_metadata_cache_owner(ci)); | |
ccd979bd MF |
363 | |
364 | /* Be careful to initialize the tree members *first* because | |
365 | * once the ci_tree is used, the array is junk... */ | |
47460d65 | 366 | for (i = 0; i < OCFS2_CACHE_INFO_MAX_ARRAY; i++) |
ccd979bd MF |
367 | tree[i]->c_block = ci->ci_cache.ci_array[i]; |
368 | ||
47460d65 | 369 | ci->ci_flags &= ~OCFS2_CACHE_FL_INLINE; |
ccd979bd MF |
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 | ||
47460d65 | 374 | for (i = 0; i < OCFS2_CACHE_INFO_MAX_ARRAY; i++) { |
ccd979bd MF |
375 | __ocfs2_insert_cache_tree(ci, tree[i]); |
376 | tree[i] = NULL; | |
377 | } | |
378 | ||
b0697053 | 379 | mlog(0, "Expanded %llu to a tree cache: flags 0x%x, num = %u\n", |
6e5a3d75 JB |
380 | (unsigned long long)ocfs2_metadata_cache_owner(ci), |
381 | ci->ci_flags, ci->ci_num_cached); | |
ccd979bd MF |
382 | } |
383 | ||
384 | /* Slow path function - memory allocation is necessary. See the | |
385 | * comment above ocfs2_set_buffer_uptodate for more information. */ | |
8cb471e8 | 386 | static void __ocfs2_set_buffer_uptodate(struct ocfs2_caching_info *ci, |
ccd979bd MF |
387 | sector_t block, |
388 | int expand_tree) | |
389 | { | |
390 | int i; | |
ccd979bd | 391 | struct ocfs2_meta_cache_item *new = NULL; |
47460d65 | 392 | struct ocfs2_meta_cache_item *tree[OCFS2_CACHE_INFO_MAX_ARRAY] = |
ccd979bd MF |
393 | { NULL, }; |
394 | ||
6e5a3d75 JB |
395 | mlog(0, "Owner %llu, block %llu, expand = %d\n", |
396 | (unsigned long long)ocfs2_metadata_cache_owner(ci), | |
b0697053 | 397 | (unsigned long long)block, expand_tree); |
ccd979bd | 398 | |
afae00ab | 399 | new = kmem_cache_alloc(ocfs2_uptodate_cachep, GFP_NOFS); |
ccd979bd MF |
400 | if (!new) { |
401 | mlog_errno(-ENOMEM); | |
402 | return; | |
403 | } | |
404 | new->c_block = block; | |
405 | ||
406 | if (expand_tree) { | |
407 | /* Do *not* allocate an array here - the removal code | |
408 | * has no way of tracking that. */ | |
47460d65 | 409 | for (i = 0; i < OCFS2_CACHE_INFO_MAX_ARRAY; i++) { |
ccd979bd | 410 | tree[i] = kmem_cache_alloc(ocfs2_uptodate_cachep, |
afae00ab | 411 | GFP_NOFS); |
ccd979bd MF |
412 | if (!tree[i]) { |
413 | mlog_errno(-ENOMEM); | |
414 | goto out_free; | |
415 | } | |
416 | ||
417 | /* These are initialized in ocfs2_expand_cache! */ | |
418 | } | |
419 | } | |
420 | ||
6e5a3d75 | 421 | ocfs2_metadata_cache_lock(ci); |
8cb471e8 | 422 | if (ocfs2_insert_can_use_array(ci)) { |
ccd979bd MF |
423 | mlog(0, "Someone cleared the tree underneath us\n"); |
424 | /* Ok, items were removed from the cache in between | |
425 | * locks. Detect this and revert back to the fast path */ | |
426 | ocfs2_append_cache_array(ci, block); | |
6e5a3d75 | 427 | ocfs2_metadata_cache_unlock(ci); |
ccd979bd MF |
428 | goto out_free; |
429 | } | |
430 | ||
431 | if (expand_tree) | |
8cb471e8 | 432 | ocfs2_expand_cache(ci, tree); |
ccd979bd MF |
433 | |
434 | __ocfs2_insert_cache_tree(ci, new); | |
6e5a3d75 | 435 | ocfs2_metadata_cache_unlock(ci); |
ccd979bd MF |
436 | |
437 | new = NULL; | |
438 | out_free: | |
439 | if (new) | |
440 | kmem_cache_free(ocfs2_uptodate_cachep, new); | |
441 | ||
442 | /* If these were used, then ocfs2_expand_cache re-set them to | |
443 | * NULL for us. */ | |
444 | if (tree[0]) { | |
47460d65 | 445 | for (i = 0; i < OCFS2_CACHE_INFO_MAX_ARRAY; i++) |
ccd979bd MF |
446 | if (tree[i]) |
447 | kmem_cache_free(ocfs2_uptodate_cachep, | |
448 | tree[i]); | |
449 | } | |
450 | } | |
451 | ||
6e5a3d75 | 452 | /* Item insertion is guarded by co_io_lock(), so the insertion path takes |
ccd979bd MF |
453 | * advantage of this by not rechecking for a duplicate insert during |
454 | * the slow case. Additionally, if the cache needs to be bumped up to | |
455 | * a tree, the code will not recheck after acquiring the lock -- | |
456 | * multiple paths cannot be expanding to a tree at the same time. | |
457 | * | |
458 | * The slow path takes into account that items can be removed | |
459 | * (including the whole tree wiped and reset) when this process it out | |
460 | * allocating memory. In those cases, it reverts back to the fast | |
461 | * path. | |
462 | * | |
463 | * Note that this function may actually fail to insert the block if | |
464 | * memory cannot be allocated. This is not fatal however (but may | |
aa958874 MF |
465 | * result in a performance penalty) |
466 | * | |
467 | * Readahead buffers can be passed in here before the I/O request is | |
468 | * completed. | |
469 | */ | |
8cb471e8 | 470 | void ocfs2_set_buffer_uptodate(struct ocfs2_caching_info *ci, |
ccd979bd MF |
471 | struct buffer_head *bh) |
472 | { | |
473 | int expand; | |
ccd979bd MF |
474 | |
475 | /* The block may very well exist in our cache already, so avoid | |
476 | * doing any more work in that case. */ | |
8cb471e8 | 477 | if (ocfs2_buffer_cached(ci, bh)) |
ccd979bd MF |
478 | return; |
479 | ||
6e5a3d75 JB |
480 | mlog(0, "Owner %llu, inserting block %llu\n", |
481 | (unsigned long long)ocfs2_metadata_cache_owner(ci), | |
b0697053 | 482 | (unsigned long long)bh->b_blocknr); |
ccd979bd MF |
483 | |
484 | /* No need to recheck under spinlock - insertion is guarded by | |
6e5a3d75 JB |
485 | * co_io_lock() */ |
486 | ocfs2_metadata_cache_lock(ci); | |
8cb471e8 | 487 | if (ocfs2_insert_can_use_array(ci)) { |
ccd979bd MF |
488 | /* Fast case - it's an array and there's a free |
489 | * spot. */ | |
490 | ocfs2_append_cache_array(ci, bh->b_blocknr); | |
6e5a3d75 | 491 | ocfs2_metadata_cache_unlock(ci); |
ccd979bd MF |
492 | return; |
493 | } | |
494 | ||
495 | expand = 0; | |
47460d65 | 496 | if (ci->ci_flags & OCFS2_CACHE_FL_INLINE) { |
ccd979bd MF |
497 | /* We need to bump things up to a tree. */ |
498 | expand = 1; | |
499 | } | |
6e5a3d75 | 500 | ocfs2_metadata_cache_unlock(ci); |
ccd979bd | 501 | |
8cb471e8 | 502 | __ocfs2_set_buffer_uptodate(ci, bh->b_blocknr, expand); |
ccd979bd MF |
503 | } |
504 | ||
505 | /* Called against a newly allocated buffer. Most likely nobody should | |
506 | * be able to read this sort of metadata while it's still being | |
6e5a3d75 | 507 | * allocated, but this is careful to take co_io_lock() anyway. */ |
8cb471e8 | 508 | void ocfs2_set_new_buffer_uptodate(struct ocfs2_caching_info *ci, |
ccd979bd MF |
509 | struct buffer_head *bh) |
510 | { | |
ccd979bd | 511 | /* This should definitely *not* exist in our cache */ |
8cb471e8 | 512 | BUG_ON(ocfs2_buffer_cached(ci, bh)); |
ccd979bd MF |
513 | |
514 | set_buffer_uptodate(bh); | |
515 | ||
6e5a3d75 | 516 | ocfs2_metadata_cache_io_lock(ci); |
8cb471e8 | 517 | ocfs2_set_buffer_uptodate(ci, bh); |
6e5a3d75 | 518 | ocfs2_metadata_cache_io_unlock(ci); |
ccd979bd MF |
519 | } |
520 | ||
521 | /* Requires ip_lock. */ | |
522 | static void ocfs2_remove_metadata_array(struct ocfs2_caching_info *ci, | |
523 | int index) | |
524 | { | |
525 | sector_t *array = ci->ci_cache.ci_array; | |
526 | int bytes; | |
527 | ||
47460d65 | 528 | BUG_ON(index < 0 || index >= OCFS2_CACHE_INFO_MAX_ARRAY); |
ccd979bd MF |
529 | BUG_ON(index >= ci->ci_num_cached); |
530 | BUG_ON(!ci->ci_num_cached); | |
531 | ||
532 | mlog(0, "remove index %d (num_cached = %u\n", index, | |
533 | ci->ci_num_cached); | |
534 | ||
535 | ci->ci_num_cached--; | |
536 | ||
537 | /* don't need to copy if the array is now empty, or if we | |
538 | * removed at the tail */ | |
539 | if (ci->ci_num_cached && index < ci->ci_num_cached) { | |
540 | bytes = sizeof(sector_t) * (ci->ci_num_cached - index); | |
541 | memmove(&array[index], &array[index + 1], bytes); | |
542 | } | |
543 | } | |
544 | ||
545 | /* Requires ip_lock. */ | |
546 | static void ocfs2_remove_metadata_tree(struct ocfs2_caching_info *ci, | |
547 | struct ocfs2_meta_cache_item *item) | |
548 | { | |
549 | mlog(0, "remove block %llu from tree\n", | |
550 | (unsigned long long) item->c_block); | |
551 | ||
552 | rb_erase(&item->c_node, &ci->ci_cache.ci_tree); | |
553 | ci->ci_num_cached--; | |
554 | } | |
555 | ||
8cb471e8 | 556 | static void ocfs2_remove_block_from_cache(struct ocfs2_caching_info *ci, |
ac11c827 | 557 | sector_t block) |
ccd979bd MF |
558 | { |
559 | int index; | |
ccd979bd | 560 | struct ocfs2_meta_cache_item *item = NULL; |
ccd979bd | 561 | |
6e5a3d75 JB |
562 | ocfs2_metadata_cache_lock(ci); |
563 | mlog(0, "Owner %llu, remove %llu, items = %u, array = %u\n", | |
564 | (unsigned long long)ocfs2_metadata_cache_owner(ci), | |
b0697053 | 565 | (unsigned long long) block, ci->ci_num_cached, |
47460d65 | 566 | ci->ci_flags & OCFS2_CACHE_FL_INLINE); |
ccd979bd | 567 | |
47460d65 | 568 | if (ci->ci_flags & OCFS2_CACHE_FL_INLINE) { |
ccd979bd MF |
569 | index = ocfs2_search_cache_array(ci, block); |
570 | if (index != -1) | |
571 | ocfs2_remove_metadata_array(ci, index); | |
572 | } else { | |
573 | item = ocfs2_search_cache_tree(ci, block); | |
574 | if (item) | |
575 | ocfs2_remove_metadata_tree(ci, item); | |
576 | } | |
6e5a3d75 | 577 | ocfs2_metadata_cache_unlock(ci); |
ccd979bd MF |
578 | |
579 | if (item) | |
580 | kmem_cache_free(ocfs2_uptodate_cachep, item); | |
581 | } | |
582 | ||
ac11c827 TM |
583 | /* |
584 | * Called when we remove a chunk of metadata from an inode. We don't | |
585 | * bother reverting things to an inlined array in the case of a remove | |
586 | * which moves us back under the limit. | |
587 | */ | |
8cb471e8 | 588 | void ocfs2_remove_from_cache(struct ocfs2_caching_info *ci, |
ac11c827 TM |
589 | struct buffer_head *bh) |
590 | { | |
591 | sector_t block = bh->b_blocknr; | |
592 | ||
8cb471e8 | 593 | ocfs2_remove_block_from_cache(ci, block); |
ac11c827 TM |
594 | } |
595 | ||
596 | /* Called when we remove xattr clusters from an inode. */ | |
8cb471e8 | 597 | void ocfs2_remove_xattr_clusters_from_cache(struct ocfs2_caching_info *ci, |
ac11c827 TM |
598 | sector_t block, |
599 | u32 c_len) | |
600 | { | |
8cb471e8 JB |
601 | struct super_block *sb = ocfs2_metadata_cache_get_super(ci); |
602 | unsigned int i, b_len = ocfs2_clusters_to_blocks(sb, 1) * c_len; | |
ac11c827 TM |
603 | |
604 | for (i = 0; i < b_len; i++, block++) | |
8cb471e8 | 605 | ocfs2_remove_block_from_cache(ci, block); |
ac11c827 TM |
606 | } |
607 | ||
ccd979bd MF |
608 | int __init init_ocfs2_uptodate_cache(void) |
609 | { | |
610 | ocfs2_uptodate_cachep = kmem_cache_create("ocfs2_uptodate", | |
611 | sizeof(struct ocfs2_meta_cache_item), | |
20c2df83 | 612 | 0, SLAB_HWCACHE_ALIGN, NULL); |
ccd979bd MF |
613 | if (!ocfs2_uptodate_cachep) |
614 | return -ENOMEM; | |
615 | ||
616 | mlog(0, "%u inlined cache items per inode.\n", | |
47460d65 | 617 | OCFS2_CACHE_INFO_MAX_ARRAY); |
ccd979bd MF |
618 | |
619 | return 0; | |
620 | } | |
621 | ||
0c6c98fb | 622 | void exit_ocfs2_uptodate_cache(void) |
ccd979bd MF |
623 | { |
624 | if (ocfs2_uptodate_cachep) | |
625 | kmem_cache_destroy(ocfs2_uptodate_cachep); | |
626 | } |