1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
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>
56
57#include <cluster/masklog.h>
58
59#include "ocfs2.h"
60
61#include "inode.h"
62#include "uptodate.h"
63#include "ocfs2_trace.h"
64
65struct ocfs2_meta_cache_item {
66 struct rb_node c_node;
67 sector_t c_block;
68};
69
70static struct kmem_cache *ocfs2_uptodate_cachep = NULL;
71
72u64 ocfs2_metadata_cache_owner(struct ocfs2_caching_info *ci)
73{
74 BUG_ON(!ci || !ci->ci_ops);
75
76 return ci->ci_ops->co_owner(ci);
77}
78
79struct super_block *ocfs2_metadata_cache_get_super(struct ocfs2_caching_info *ci)
80{
81 BUG_ON(!ci || !ci->ci_ops);
82
83 return ci->ci_ops->co_get_super(ci);
84}
85
86static void ocfs2_metadata_cache_lock(struct ocfs2_caching_info *ci)
87{
88 BUG_ON(!ci || !ci->ci_ops);
89
90 ci->ci_ops->co_cache_lock(ci);
91}
92
93static void ocfs2_metadata_cache_unlock(struct ocfs2_caching_info *ci)
94{
95 BUG_ON(!ci || !ci->ci_ops);
96
97 ci->ci_ops->co_cache_unlock(ci);
98}
99
100void ocfs2_metadata_cache_io_lock(struct ocfs2_caching_info *ci)
101{
102 BUG_ON(!ci || !ci->ci_ops);
103
104 ci->ci_ops->co_io_lock(ci);
105}
106
107void ocfs2_metadata_cache_io_unlock(struct ocfs2_caching_info *ci)
108{
109 BUG_ON(!ci || !ci->ci_ops);
110
111 ci->ci_ops->co_io_unlock(ci);
112}
113
114
115static void ocfs2_metadata_cache_reset(struct ocfs2_caching_info *ci,
116 int clear)
117{
118 ci->ci_flags |= OCFS2_CACHE_FL_INLINE;
119 ci->ci_num_cached = 0;
120
121 if (clear) {
122 ci->ci_created_trans = 0;
123 ci->ci_last_trans = 0;
124 }
125}
126
127void ocfs2_metadata_cache_init(struct ocfs2_caching_info *ci,
128 const struct ocfs2_caching_operations *ops)
129{
130 BUG_ON(!ops);
131
132 ci->ci_ops = ops;
133 ocfs2_metadata_cache_reset(ci, 1);
134}
135
136void ocfs2_metadata_cache_exit(struct ocfs2_caching_info *ci)
137{
138 ocfs2_metadata_cache_purge(ci);
139 ocfs2_metadata_cache_reset(ci, 1);
140}
141
142
143
144
145static unsigned int ocfs2_purge_copied_metadata_tree(struct rb_root *root)
146{
147 unsigned int purged = 0;
148 struct rb_node *node;
149 struct ocfs2_meta_cache_item *item;
150
151 while ((node = rb_last(root)) != NULL) {
152 item = rb_entry(node, struct ocfs2_meta_cache_item, c_node);
153
154 trace_ocfs2_purge_copied_metadata_tree(
155 (unsigned long long) item->c_block);
156
157 rb_erase(&item->c_node, root);
158 kmem_cache_free(ocfs2_uptodate_cachep, item);
159
160 purged++;
161 }
162 return purged;
163}
164
165
166
167
168
169
170
171void ocfs2_metadata_cache_purge(struct ocfs2_caching_info *ci)
172{
173 unsigned int tree, to_purge, purged;
174 struct rb_root root = RB_ROOT;
175
176 BUG_ON(!ci || !ci->ci_ops);
177
178 ocfs2_metadata_cache_lock(ci);
179 tree = !(ci->ci_flags & OCFS2_CACHE_FL_INLINE);
180 to_purge = ci->ci_num_cached;
181
182 trace_ocfs2_metadata_cache_purge(
183 (unsigned long long)ocfs2_metadata_cache_owner(ci),
184 to_purge, tree);
185
186
187
188
189 if (tree)
190 root = ci->ci_cache.ci_tree;
191
192 ocfs2_metadata_cache_reset(ci, 0);
193 ocfs2_metadata_cache_unlock(ci);
194
195 purged = ocfs2_purge_copied_metadata_tree(&root);
196
197
198
199 if (tree && purged != to_purge)
200 mlog(ML_ERROR, "Owner %llu, count = %u, purged = %u\n",
201 (unsigned long long)ocfs2_metadata_cache_owner(ci),
202 to_purge, purged);
203}
204
205
206
207static int ocfs2_search_cache_array(struct ocfs2_caching_info *ci,
208 sector_t item)
209{
210 int i;
211
212 for (i = 0; i < ci->ci_num_cached; i++) {
213 if (item == ci->ci_cache.ci_array[i])
214 return i;
215 }
216
217 return -1;
218}
219
220
221
222static struct ocfs2_meta_cache_item *
223ocfs2_search_cache_tree(struct ocfs2_caching_info *ci,
224 sector_t block)
225{
226 struct rb_node * n = ci->ci_cache.ci_tree.rb_node;
227 struct ocfs2_meta_cache_item *item = NULL;
228
229 while (n) {
230 item = rb_entry(n, struct ocfs2_meta_cache_item, c_node);
231
232 if (block < item->c_block)
233 n = n->rb_left;
234 else if (block > item->c_block)
235 n = n->rb_right;
236 else
237 return item;
238 }
239
240 return NULL;
241}
242
243static int ocfs2_buffer_cached(struct ocfs2_caching_info *ci,
244 struct buffer_head *bh)
245{
246 int index = -1;
247 struct ocfs2_meta_cache_item *item = NULL;
248
249 ocfs2_metadata_cache_lock(ci);
250
251 trace_ocfs2_buffer_cached_begin(
252 (unsigned long long)ocfs2_metadata_cache_owner(ci),
253 (unsigned long long) bh->b_blocknr,
254 !!(ci->ci_flags & OCFS2_CACHE_FL_INLINE));
255
256 if (ci->ci_flags & OCFS2_CACHE_FL_INLINE)
257 index = ocfs2_search_cache_array(ci, bh->b_blocknr);
258 else
259 item = ocfs2_search_cache_tree(ci, bh->b_blocknr);
260
261 ocfs2_metadata_cache_unlock(ci);
262
263 trace_ocfs2_buffer_cached_end(index, item);
264
265 return (index != -1) || (item != NULL);
266}
267
268
269
270
271
272
273int ocfs2_buffer_uptodate(struct ocfs2_caching_info *ci,
274 struct buffer_head *bh)
275{
276
277
278
279 if (!buffer_uptodate(bh))
280 return 0;
281
282
283
284 if (buffer_jbd(bh))
285 return 1;
286
287
288
289 return ocfs2_buffer_cached(ci, bh);
290}
291
292
293
294
295
296int ocfs2_buffer_read_ahead(struct ocfs2_caching_info *ci,
297 struct buffer_head *bh)
298{
299 return buffer_locked(bh) && ocfs2_buffer_cached(ci, bh);
300}
301
302
303static void ocfs2_append_cache_array(struct ocfs2_caching_info *ci,
304 sector_t block)
305{
306 BUG_ON(ci->ci_num_cached >= OCFS2_CACHE_INFO_MAX_ARRAY);
307
308 trace_ocfs2_append_cache_array(
309 (unsigned long long)ocfs2_metadata_cache_owner(ci),
310 (unsigned long long)block, ci->ci_num_cached);
311
312 ci->ci_cache.ci_array[ci->ci_num_cached] = block;
313 ci->ci_num_cached++;
314}
315
316
317
318
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 trace_ocfs2_insert_cache_tree(
328 (unsigned long long)ocfs2_metadata_cache_owner(ci),
329 (unsigned long long)block, ci->ci_num_cached);
330
331 while(*p) {
332 parent = *p;
333
334 tmp = rb_entry(parent, struct ocfs2_meta_cache_item, c_node);
335
336 if (block < tmp->c_block)
337 p = &(*p)->rb_left;
338 else if (block > tmp->c_block)
339 p = &(*p)->rb_right;
340 else {
341
342 mlog(ML_ERROR, "Duplicate block %llu cached!\n",
343 (unsigned long long) block);
344 BUG();
345 }
346 }
347
348 rb_link_node(&new->c_node, parent, p);
349 rb_insert_color(&new->c_node, &ci->ci_cache.ci_tree);
350 ci->ci_num_cached++;
351}
352
353
354static inline int ocfs2_insert_can_use_array(struct ocfs2_caching_info *ci)
355{
356 return (ci->ci_flags & OCFS2_CACHE_FL_INLINE) &&
357 (ci->ci_num_cached < OCFS2_CACHE_INFO_MAX_ARRAY);
358}
359
360
361
362
363
364
365static void ocfs2_expand_cache(struct ocfs2_caching_info *ci,
366 struct ocfs2_meta_cache_item **tree)
367{
368 int i;
369
370 mlog_bug_on_msg(ci->ci_num_cached != OCFS2_CACHE_INFO_MAX_ARRAY,
371 "Owner %llu, num cached = %u, should be %u\n",
372 (unsigned long long)ocfs2_metadata_cache_owner(ci),
373 ci->ci_num_cached, OCFS2_CACHE_INFO_MAX_ARRAY);
374 mlog_bug_on_msg(!(ci->ci_flags & OCFS2_CACHE_FL_INLINE),
375 "Owner %llu not marked as inline anymore!\n",
376 (unsigned long long)ocfs2_metadata_cache_owner(ci));
377
378
379
380 for (i = 0; i < OCFS2_CACHE_INFO_MAX_ARRAY; i++)
381 tree[i]->c_block = ci->ci_cache.ci_array[i];
382
383 ci->ci_flags &= ~OCFS2_CACHE_FL_INLINE;
384 ci->ci_cache.ci_tree = RB_ROOT;
385
386 ci->ci_num_cached = 0;
387
388 for (i = 0; i < OCFS2_CACHE_INFO_MAX_ARRAY; i++) {
389 __ocfs2_insert_cache_tree(ci, tree[i]);
390 tree[i] = NULL;
391 }
392
393 trace_ocfs2_expand_cache(
394 (unsigned long long)ocfs2_metadata_cache_owner(ci),
395 ci->ci_flags, ci->ci_num_cached);
396}
397
398
399
400static void __ocfs2_set_buffer_uptodate(struct ocfs2_caching_info *ci,
401 sector_t block,
402 int expand_tree)
403{
404 int i;
405 struct ocfs2_meta_cache_item *new = NULL;
406 struct ocfs2_meta_cache_item *tree[OCFS2_CACHE_INFO_MAX_ARRAY] =
407 { NULL, };
408
409 trace_ocfs2_set_buffer_uptodate(
410 (unsigned long long)ocfs2_metadata_cache_owner(ci),
411 (unsigned long long)block, expand_tree);
412
413 new = kmem_cache_alloc(ocfs2_uptodate_cachep, GFP_NOFS);
414 if (!new) {
415 mlog_errno(-ENOMEM);
416 return;
417 }
418 new->c_block = block;
419
420 if (expand_tree) {
421
422
423 for (i = 0; i < OCFS2_CACHE_INFO_MAX_ARRAY; i++) {
424 tree[i] = kmem_cache_alloc(ocfs2_uptodate_cachep,
425 GFP_NOFS);
426 if (!tree[i]) {
427 mlog_errno(-ENOMEM);
428 goto out_free;
429 }
430
431
432 }
433 }
434
435 ocfs2_metadata_cache_lock(ci);
436 if (ocfs2_insert_can_use_array(ci)) {
437
438
439 ocfs2_append_cache_array(ci, block);
440 ocfs2_metadata_cache_unlock(ci);
441 goto out_free;
442 }
443
444 if (expand_tree)
445 ocfs2_expand_cache(ci, tree);
446
447 __ocfs2_insert_cache_tree(ci, new);
448 ocfs2_metadata_cache_unlock(ci);
449
450 new = NULL;
451out_free:
452 if (new)
453 kmem_cache_free(ocfs2_uptodate_cachep, new);
454
455
456
457 if (tree[0]) {
458 for (i = 0; i < OCFS2_CACHE_INFO_MAX_ARRAY; i++)
459 if (tree[i])
460 kmem_cache_free(ocfs2_uptodate_cachep,
461 tree[i]);
462 }
463}
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483void ocfs2_set_buffer_uptodate(struct ocfs2_caching_info *ci,
484 struct buffer_head *bh)
485{
486 int expand;
487
488
489
490 if (ocfs2_buffer_cached(ci, bh))
491 return;
492
493 trace_ocfs2_set_buffer_uptodate_begin(
494 (unsigned long long)ocfs2_metadata_cache_owner(ci),
495 (unsigned long long)bh->b_blocknr);
496
497
498
499 ocfs2_metadata_cache_lock(ci);
500 if (ocfs2_insert_can_use_array(ci)) {
501
502
503 ocfs2_append_cache_array(ci, bh->b_blocknr);
504 ocfs2_metadata_cache_unlock(ci);
505 return;
506 }
507
508 expand = 0;
509 if (ci->ci_flags & OCFS2_CACHE_FL_INLINE) {
510
511 expand = 1;
512 }
513 ocfs2_metadata_cache_unlock(ci);
514
515 __ocfs2_set_buffer_uptodate(ci, bh->b_blocknr, expand);
516}
517
518
519
520
521void ocfs2_set_new_buffer_uptodate(struct ocfs2_caching_info *ci,
522 struct buffer_head *bh)
523{
524
525 BUG_ON(ocfs2_buffer_cached(ci, bh));
526
527 set_buffer_uptodate(bh);
528
529 ocfs2_metadata_cache_io_lock(ci);
530 ocfs2_set_buffer_uptodate(ci, bh);
531 ocfs2_metadata_cache_io_unlock(ci);
532}
533
534
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
541 BUG_ON(index < 0 || index >= OCFS2_CACHE_INFO_MAX_ARRAY);
542 BUG_ON(index >= ci->ci_num_cached);
543 BUG_ON(!ci->ci_num_cached);
544
545 trace_ocfs2_remove_metadata_array(
546 (unsigned long long)ocfs2_metadata_cache_owner(ci),
547 index, ci->ci_num_cached);
548
549 ci->ci_num_cached--;
550
551
552
553 if (ci->ci_num_cached && index < ci->ci_num_cached) {
554 bytes = sizeof(sector_t) * (ci->ci_num_cached - index);
555 memmove(&array[index], &array[index + 1], bytes);
556 }
557}
558
559
560static void ocfs2_remove_metadata_tree(struct ocfs2_caching_info *ci,
561 struct ocfs2_meta_cache_item *item)
562{
563 trace_ocfs2_remove_metadata_tree(
564 (unsigned long long)ocfs2_metadata_cache_owner(ci),
565 (unsigned long long)item->c_block);
566
567 rb_erase(&item->c_node, &ci->ci_cache.ci_tree);
568 ci->ci_num_cached--;
569}
570
571static void ocfs2_remove_block_from_cache(struct ocfs2_caching_info *ci,
572 sector_t block)
573{
574 int index;
575 struct ocfs2_meta_cache_item *item = NULL;
576
577 ocfs2_metadata_cache_lock(ci);
578 trace_ocfs2_remove_block_from_cache(
579 (unsigned long long)ocfs2_metadata_cache_owner(ci),
580 (unsigned long long) block, ci->ci_num_cached,
581 ci->ci_flags);
582
583 if (ci->ci_flags & OCFS2_CACHE_FL_INLINE) {
584 index = ocfs2_search_cache_array(ci, block);
585 if (index != -1)
586 ocfs2_remove_metadata_array(ci, index);
587 } else {
588 item = ocfs2_search_cache_tree(ci, block);
589 if (item)
590 ocfs2_remove_metadata_tree(ci, item);
591 }
592 ocfs2_metadata_cache_unlock(ci);
593
594 if (item)
595 kmem_cache_free(ocfs2_uptodate_cachep, item);
596}
597
598
599
600
601
602
603void ocfs2_remove_from_cache(struct ocfs2_caching_info *ci,
604 struct buffer_head *bh)
605{
606 sector_t block = bh->b_blocknr;
607
608 ocfs2_remove_block_from_cache(ci, block);
609}
610
611
612void ocfs2_remove_xattr_clusters_from_cache(struct ocfs2_caching_info *ci,
613 sector_t block,
614 u32 c_len)
615{
616 struct super_block *sb = ocfs2_metadata_cache_get_super(ci);
617 unsigned int i, b_len = ocfs2_clusters_to_blocks(sb, 1) * c_len;
618
619 for (i = 0; i < b_len; i++, block++)
620 ocfs2_remove_block_from_cache(ci, block);
621}
622
623int __init init_ocfs2_uptodate_cache(void)
624{
625 ocfs2_uptodate_cachep = kmem_cache_create("ocfs2_uptodate",
626 sizeof(struct ocfs2_meta_cache_item),
627 0, SLAB_HWCACHE_ALIGN, NULL);
628 if (!ocfs2_uptodate_cachep)
629 return -ENOMEM;
630
631 return 0;
632}
633
634void exit_ocfs2_uptodate_cache(void)
635{
636 if (ocfs2_uptodate_cachep)
637 kmem_cache_destroy(ocfs2_uptodate_cachep);
638}
639