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