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
51
52
53
54
55
56
57
58
59
60#ifndef __KERNEL__
61#include "jfs_user.h"
62#else
63#include <linux/time.h>
64#include <linux/fs.h>
65#include <linux/jbd2.h>
66#include <linux/errno.h>
67#include <linux/slab.h>
68#include <linux/list.h>
69#include <linux/init.h>
70#endif
71#include <linux/log2.h>
72
73static struct kmem_cache *jbd2_revoke_record_cache;
74static struct kmem_cache *jbd2_revoke_table_cache;
75
76
77
78
79
80struct jbd2_revoke_record_s
81{
82 struct list_head hash;
83 tid_t sequence;
84 unsigned long long blocknr;
85};
86
87
88
89struct jbd2_revoke_table_s
90{
91
92
93 int hash_size;
94 int hash_shift;
95 struct list_head *hash_table;
96};
97
98
99#ifdef __KERNEL__
100static void write_one_revoke_record(journal_t *, transaction_t *,
101 struct journal_head **, int *,
102 struct jbd2_revoke_record_s *);
103static void flush_descriptor(journal_t *, struct journal_head *, int);
104#endif
105
106
107
108
109static inline int hash(journal_t *journal, unsigned long long block)
110{
111 struct jbd2_revoke_table_s *table = journal->j_revoke;
112 int hash_shift = table->hash_shift;
113 int hash = (int)block ^ (int)((block >> 31) >> 1);
114
115 return ((hash << (hash_shift - 6)) ^
116 (hash >> 13) ^
117 (hash << (hash_shift - 12))) & (table->hash_size - 1);
118}
119
120static int insert_revoke_hash(journal_t *journal, unsigned long long blocknr,
121 tid_t seq)
122{
123 struct list_head *hash_list;
124 struct jbd2_revoke_record_s *record;
125
126repeat:
127 record = kmem_cache_alloc(jbd2_revoke_record_cache, GFP_NOFS);
128 if (!record)
129 goto oom;
130
131 record->sequence = seq;
132 record->blocknr = blocknr;
133 hash_list = &journal->j_revoke->hash_table[hash(journal, blocknr)];
134 spin_lock(&journal->j_revoke_lock);
135 list_add(&record->hash, hash_list);
136 spin_unlock(&journal->j_revoke_lock);
137 return 0;
138
139oom:
140 if (!journal_oom_retry)
141 return -ENOMEM;
142 jbd_debug(1, "ENOMEM in %s, retrying\n", __FUNCTION__);
143 yield();
144 goto repeat;
145}
146
147
148
149static struct jbd2_revoke_record_s *find_revoke_record(journal_t *journal,
150 unsigned long long blocknr)
151{
152 struct list_head *hash_list;
153 struct jbd2_revoke_record_s *record;
154
155 hash_list = &journal->j_revoke->hash_table[hash(journal, blocknr)];
156
157 spin_lock(&journal->j_revoke_lock);
158 record = (struct jbd2_revoke_record_s *) hash_list->next;
159 while (&(record->hash) != hash_list) {
160 if (record->blocknr == blocknr) {
161 spin_unlock(&journal->j_revoke_lock);
162 return record;
163 }
164 record = (struct jbd2_revoke_record_s *) record->hash.next;
165 }
166 spin_unlock(&journal->j_revoke_lock);
167 return NULL;
168}
169
170int __init jbd2_journal_init_revoke_caches(void)
171{
172 jbd2_revoke_record_cache = kmem_cache_create("jbd2_revoke_record",
173 sizeof(struct jbd2_revoke_record_s),
174 0,
175 SLAB_HWCACHE_ALIGN|SLAB_TEMPORARY,
176 NULL);
177 if (!jbd2_revoke_record_cache)
178 return -ENOMEM;
179
180 jbd2_revoke_table_cache = kmem_cache_create("jbd2_revoke_table",
181 sizeof(struct jbd2_revoke_table_s),
182 0, SLAB_TEMPORARY, NULL);
183 if (!jbd2_revoke_table_cache) {
184 kmem_cache_destroy(jbd2_revoke_record_cache);
185 jbd2_revoke_record_cache = NULL;
186 return -ENOMEM;
187 }
188 return 0;
189}
190
191void jbd2_journal_destroy_revoke_caches(void)
192{
193 kmem_cache_destroy(jbd2_revoke_record_cache);
194 jbd2_revoke_record_cache = NULL;
195 kmem_cache_destroy(jbd2_revoke_table_cache);
196 jbd2_revoke_table_cache = NULL;
197}
198
199
200
201int jbd2_journal_init_revoke(journal_t *journal, int hash_size)
202{
203 int shift, tmp;
204
205 J_ASSERT (journal->j_revoke_table[0] == NULL);
206
207 shift = 0;
208 tmp = hash_size;
209 while((tmp >>= 1UL) != 0UL)
210 shift++;
211
212 journal->j_revoke_table[0] = kmem_cache_alloc(jbd2_revoke_table_cache, GFP_KERNEL);
213 if (!journal->j_revoke_table[0])
214 return -ENOMEM;
215 journal->j_revoke = journal->j_revoke_table[0];
216
217
218 J_ASSERT(is_power_of_2(hash_size));
219
220 journal->j_revoke->hash_size = hash_size;
221
222 journal->j_revoke->hash_shift = shift;
223
224 journal->j_revoke->hash_table =
225 kmalloc(hash_size * sizeof(struct list_head), GFP_KERNEL);
226 if (!journal->j_revoke->hash_table) {
227 kmem_cache_free(jbd2_revoke_table_cache, journal->j_revoke_table[0]);
228 journal->j_revoke = NULL;
229 return -ENOMEM;
230 }
231
232 for (tmp = 0; tmp < hash_size; tmp++)
233 INIT_LIST_HEAD(&journal->j_revoke->hash_table[tmp]);
234
235 journal->j_revoke_table[1] = kmem_cache_alloc(jbd2_revoke_table_cache, GFP_KERNEL);
236 if (!journal->j_revoke_table[1]) {
237 kfree(journal->j_revoke_table[0]->hash_table);
238 kmem_cache_free(jbd2_revoke_table_cache, journal->j_revoke_table[0]);
239 return -ENOMEM;
240 }
241
242 journal->j_revoke = journal->j_revoke_table[1];
243
244
245 J_ASSERT(is_power_of_2(hash_size));
246
247 journal->j_revoke->hash_size = hash_size;
248
249 journal->j_revoke->hash_shift = shift;
250
251 journal->j_revoke->hash_table =
252 kmalloc(hash_size * sizeof(struct list_head), GFP_KERNEL);
253 if (!journal->j_revoke->hash_table) {
254 kfree(journal->j_revoke_table[0]->hash_table);
255 kmem_cache_free(jbd2_revoke_table_cache, journal->j_revoke_table[0]);
256 kmem_cache_free(jbd2_revoke_table_cache, journal->j_revoke_table[1]);
257 journal->j_revoke = NULL;
258 return -ENOMEM;
259 }
260
261 for (tmp = 0; tmp < hash_size; tmp++)
262 INIT_LIST_HEAD(&journal->j_revoke->hash_table[tmp]);
263
264 spin_lock_init(&journal->j_revoke_lock);
265
266 return 0;
267}
268
269
270
271void jbd2_journal_destroy_revoke(journal_t *journal)
272{
273 struct jbd2_revoke_table_s *table;
274 struct list_head *hash_list;
275 int i;
276
277 table = journal->j_revoke_table[0];
278 if (!table)
279 return;
280
281 for (i=0; i<table->hash_size; i++) {
282 hash_list = &table->hash_table[i];
283 J_ASSERT (list_empty(hash_list));
284 }
285
286 kfree(table->hash_table);
287 kmem_cache_free(jbd2_revoke_table_cache, table);
288 journal->j_revoke = NULL;
289
290 table = journal->j_revoke_table[1];
291 if (!table)
292 return;
293
294 for (i=0; i<table->hash_size; i++) {
295 hash_list = &table->hash_table[i];
296 J_ASSERT (list_empty(hash_list));
297 }
298
299 kfree(table->hash_table);
300 kmem_cache_free(jbd2_revoke_table_cache, table);
301 journal->j_revoke = NULL;
302}
303
304
305#ifdef __KERNEL__
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331int jbd2_journal_revoke(handle_t *handle, unsigned long long blocknr,
332 struct buffer_head *bh_in)
333{
334 struct buffer_head *bh = NULL;
335 journal_t *journal;
336 struct block_device *bdev;
337 int err;
338
339 might_sleep();
340 if (bh_in)
341 BUFFER_TRACE(bh_in, "enter");
342
343 journal = handle->h_transaction->t_journal;
344 if (!jbd2_journal_set_features(journal, 0, 0, JBD2_FEATURE_INCOMPAT_REVOKE)){
345 J_ASSERT (!"Cannot set revoke feature!");
346 return -EINVAL;
347 }
348
349 bdev = journal->j_fs_dev;
350 bh = bh_in;
351
352 if (!bh) {
353 bh = __find_get_block(bdev, blocknr, journal->j_blocksize);
354 if (bh)
355 BUFFER_TRACE(bh, "found on hash");
356 }
357#ifdef JBD2_EXPENSIVE_CHECKING
358 else {
359 struct buffer_head *bh2;
360
361
362
363 bh2 = __find_get_block(bdev, blocknr, journal->j_blocksize);
364 if (bh2) {
365
366 if (bh2 != bh && buffer_revokevalid(bh2))
367
368
369
370
371
372
373 J_ASSERT_BH(bh2, buffer_revoked(bh2));
374 put_bh(bh2);
375 }
376 }
377#endif
378
379
380
381
382 if (bh) {
383 if (!J_EXPECT_BH(bh, !buffer_revoked(bh),
384 "inconsistent data on disk")) {
385 if (!bh_in)
386 brelse(bh);
387 return -EIO;
388 }
389 set_buffer_revoked(bh);
390 set_buffer_revokevalid(bh);
391 if (bh_in) {
392 BUFFER_TRACE(bh_in, "call jbd2_journal_forget");
393 jbd2_journal_forget(handle, bh_in);
394 } else {
395 BUFFER_TRACE(bh, "call brelse");
396 __brelse(bh);
397 }
398 }
399
400 jbd_debug(2, "insert revoke for block %llu, bh_in=%p\n",blocknr, bh_in);
401 err = insert_revoke_hash(journal, blocknr,
402 handle->h_transaction->t_tid);
403 BUFFER_TRACE(bh_in, "exit");
404 return err;
405}
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424int jbd2_journal_cancel_revoke(handle_t *handle, struct journal_head *jh)
425{
426 struct jbd2_revoke_record_s *record;
427 journal_t *journal = handle->h_transaction->t_journal;
428 int need_cancel;
429 int did_revoke = 0;
430 struct buffer_head *bh = jh2bh(jh);
431
432 jbd_debug(4, "journal_head %p, cancelling revoke\n", jh);
433
434
435
436
437
438 if (test_set_buffer_revokevalid(bh)) {
439 need_cancel = test_clear_buffer_revoked(bh);
440 } else {
441 need_cancel = 1;
442 clear_buffer_revoked(bh);
443 }
444
445 if (need_cancel) {
446 record = find_revoke_record(journal, bh->b_blocknr);
447 if (record) {
448 jbd_debug(4, "cancelled existing revoke on "
449 "blocknr %llu\n", (unsigned long long)bh->b_blocknr);
450 spin_lock(&journal->j_revoke_lock);
451 list_del(&record->hash);
452 spin_unlock(&journal->j_revoke_lock);
453 kmem_cache_free(jbd2_revoke_record_cache, record);
454 did_revoke = 1;
455 }
456 }
457
458#ifdef JBD2_EXPENSIVE_CHECKING
459
460 record = find_revoke_record(journal, bh->b_blocknr);
461 J_ASSERT_JH(jh, record == NULL);
462#endif
463
464
465
466
467
468 if (need_cancel) {
469 struct buffer_head *bh2;
470 bh2 = __find_get_block(bh->b_bdev, bh->b_blocknr, bh->b_size);
471 if (bh2) {
472 if (bh2 != bh)
473 clear_buffer_revoked(bh2);
474 __brelse(bh2);
475 }
476 }
477 return did_revoke;
478}
479
480
481
482
483
484void jbd2_journal_switch_revoke_table(journal_t *journal)
485{
486 int i;
487
488 if (journal->j_revoke == journal->j_revoke_table[0])
489 journal->j_revoke = journal->j_revoke_table[1];
490 else
491 journal->j_revoke = journal->j_revoke_table[0];
492
493 for (i = 0; i < journal->j_revoke->hash_size; i++)
494 INIT_LIST_HEAD(&journal->j_revoke->hash_table[i]);
495}
496
497
498
499
500
501
502
503
504void jbd2_journal_write_revoke_records(journal_t *journal,
505 transaction_t *transaction)
506{
507 struct journal_head *descriptor;
508 struct jbd2_revoke_record_s *record;
509 struct jbd2_revoke_table_s *revoke;
510 struct list_head *hash_list;
511 int i, offset, count;
512
513 descriptor = NULL;
514 offset = 0;
515 count = 0;
516
517
518 revoke = journal->j_revoke == journal->j_revoke_table[0] ?
519 journal->j_revoke_table[1] : journal->j_revoke_table[0];
520
521 for (i = 0; i < revoke->hash_size; i++) {
522 hash_list = &revoke->hash_table[i];
523
524 while (!list_empty(hash_list)) {
525 record = (struct jbd2_revoke_record_s *)
526 hash_list->next;
527 write_one_revoke_record(journal, transaction,
528 &descriptor, &offset,
529 record);
530 count++;
531 list_del(&record->hash);
532 kmem_cache_free(jbd2_revoke_record_cache, record);
533 }
534 }
535 if (descriptor)
536 flush_descriptor(journal, descriptor, offset);
537 jbd_debug(1, "Wrote %d revoke records\n", count);
538}
539
540
541
542
543
544
545static void write_one_revoke_record(journal_t *journal,
546 transaction_t *transaction,
547 struct journal_head **descriptorp,
548 int *offsetp,
549 struct jbd2_revoke_record_s *record)
550{
551 struct journal_head *descriptor;
552 int offset;
553 journal_header_t *header;
554
555
556
557
558
559 if (is_journal_aborted(journal))
560 return;
561
562 descriptor = *descriptorp;
563 offset = *offsetp;
564
565
566 if (descriptor) {
567 if (offset == journal->j_blocksize) {
568 flush_descriptor(journal, descriptor, offset);
569 descriptor = NULL;
570 }
571 }
572
573 if (!descriptor) {
574 descriptor = jbd2_journal_get_descriptor_buffer(journal);
575 if (!descriptor)
576 return;
577 header = (journal_header_t *) &jh2bh(descriptor)->b_data[0];
578 header->h_magic = cpu_to_be32(JBD2_MAGIC_NUMBER);
579 header->h_blocktype = cpu_to_be32(JBD2_REVOKE_BLOCK);
580 header->h_sequence = cpu_to_be32(transaction->t_tid);
581
582
583 JBUFFER_TRACE(descriptor, "file as BJ_LogCtl");
584 jbd2_journal_file_buffer(descriptor, transaction, BJ_LogCtl);
585
586 offset = sizeof(jbd2_journal_revoke_header_t);
587 *descriptorp = descriptor;
588 }
589
590 if (JBD2_HAS_INCOMPAT_FEATURE(journal, JBD2_FEATURE_INCOMPAT_64BIT)) {
591 * ((__be64 *)(&jh2bh(descriptor)->b_data[offset])) =
592 cpu_to_be64(record->blocknr);
593 offset += 8;
594
595 } else {
596 * ((__be32 *)(&jh2bh(descriptor)->b_data[offset])) =
597 cpu_to_be32(record->blocknr);
598 offset += 4;
599 }
600
601 *offsetp = offset;
602}
603
604
605
606
607
608
609
610
611static void flush_descriptor(journal_t *journal,
612 struct journal_head *descriptor,
613 int offset)
614{
615 jbd2_journal_revoke_header_t *header;
616 struct buffer_head *bh = jh2bh(descriptor);
617
618 if (is_journal_aborted(journal)) {
619 put_bh(bh);
620 return;
621 }
622
623 header = (jbd2_journal_revoke_header_t *) jh2bh(descriptor)->b_data;
624 header->r_count = cpu_to_be32(offset);
625 set_buffer_jwrite(bh);
626 BUFFER_TRACE(bh, "write");
627 set_buffer_dirty(bh);
628 ll_rw_block(SWRITE, 1, &bh);
629}
630#endif
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654int jbd2_journal_set_revoke(journal_t *journal,
655 unsigned long long blocknr,
656 tid_t sequence)
657{
658 struct jbd2_revoke_record_s *record;
659
660 record = find_revoke_record(journal, blocknr);
661 if (record) {
662
663
664 if (tid_gt(sequence, record->sequence))
665 record->sequence = sequence;
666 return 0;
667 }
668 return insert_revoke_hash(journal, blocknr, sequence);
669}
670
671
672
673
674
675
676
677
678int jbd2_journal_test_revoke(journal_t *journal,
679 unsigned long long blocknr,
680 tid_t sequence)
681{
682 struct jbd2_revoke_record_s *record;
683
684 record = find_revoke_record(journal, blocknr);
685 if (!record)
686 return 0;
687 if (tid_gt(sequence, record->sequence))
688 return 0;
689 return 1;
690}
691
692
693
694
695
696
697void jbd2_journal_clear_revoke(journal_t *journal)
698{
699 int i;
700 struct list_head *hash_list;
701 struct jbd2_revoke_record_s *record;
702 struct jbd2_revoke_table_s *revoke;
703
704 revoke = journal->j_revoke;
705
706 for (i = 0; i < revoke->hash_size; i++) {
707 hash_list = &revoke->hash_table[i];
708 while (!list_empty(hash_list)) {
709 record = (struct jbd2_revoke_record_s*) hash_list->next;
710 list_del(&record->hash);
711 kmem_cache_free(jbd2_revoke_record_cache, record);
712 }
713 }
714}
715