4 * Written by Stephen C. Tweedie <sct@redhat.com>, 2000
6 * Copyright 2000 Red Hat corp --- All Rights Reserved
8 * This file is part of the Linux kernel and is made available under
9 * the terms of the GNU General Public License, version 2, or at your
10 * option, any later version, incorporated herein by reference.
12 * Journal revoke routines for the generic filesystem journaling code;
13 * part of the ext2fs journaling system.
15 * Revoke is the mechanism used to prevent old log records for deleted
16 * metadata from being replayed on top of newer data using the same
17 * blocks. The revoke mechanism is used in two separate places:
19 * + Commit: during commit we write the entire list of the current
20 * transaction's revoked blocks to the journal
22 * + Recovery: during recovery we record the transaction ID of all
23 * revoked blocks. If there are multiple revoke records in the log
24 * for a single block, only the last one counts, and if there is a log
25 * entry for a block beyond the last revoke, then that log entry still
28 * We can get interactions between revokes and new log data within a
31 * Block is revoked and then journaled:
32 * The desired end result is the journaling of the new block, so we
33 * cancel the revoke before the transaction commits.
35 * Block is journaled and then revoked:
36 * The revoke must take precedence over the write of the block, so we
37 * need either to cancel the journal entry or to write the revoke
38 * later in the log than the log block. In this case, we choose the
39 * latter: journaling a block cancels any revoke record for that block
40 * in the current transaction, so any revoke for that block in the
41 * transaction must have happened after the block was journaled and so
42 * the revoke must take precedence.
44 * Block is revoked and then written as data:
45 * The data write is allowed to succeed, but the revoke is _not_
46 * cancelled. We still need to prevent old log records from
47 * overwriting the new data. We don't even need to clear the revoke
50 * Revoke information on buffers is a tri-state value:
52 * RevokeValid clear: no cached revoke status, need to look it up
53 * RevokeValid set, Revoked clear:
54 * buffer has not been revoked, and cancel_revoke
56 * RevokeValid set, Revoked set:
57 * buffer has been revoked.
63 #include <linux/sched.h>
65 #include <linux/jbd.h>
66 #include <linux/errno.h>
67 #include <linux/slab.h>
68 #include <linux/locks.h>
69 #include <linux/list.h>
70 #include <linux/smp_lock.h>
71 #include <linux/init.h>
74 static lkmem_cache_t
*revoke_record_cache
;
75 static lkmem_cache_t
*revoke_table_cache
;
77 /* Each revoke record represents one single revoked block. During
78 journal replay, this involves recording the transaction ID of the
79 last transaction to revoke this block. */
81 struct jbd_revoke_record_s
83 struct list_head hash
;
84 tid_t sequence
; /* Used for recovery only */
85 unsigned long blocknr
;
89 /* The revoke table is just a simple hash table of revoke records. */
90 struct jbd_revoke_table_s
92 /* It is conceivable that we might want a larger hash table
93 * for recovery. Must be a power of two. */
96 struct list_head
*hash_table
;
101 static void write_one_revoke_record(journal_t
*, transaction_t
*,
102 struct journal_head
**, int *,
103 struct jbd_revoke_record_s
*);
104 static void flush_descriptor(journal_t
*, struct journal_head
*, int);
107 /* Utility functions to maintain the revoke table */
109 /* Borrowed from buffer.c: this is a tried and tested block hash function */
110 static inline int hash(journal_t
*journal
, unsigned long block
)
112 struct jbd_revoke_table_s
*table
= journal
->j_revoke
;
113 int hash_shift
= table
->hash_shift
;
115 return ((block
<< (hash_shift
- 6)) ^
117 (block
<< (hash_shift
- 12))) & (table
->hash_size
- 1);
120 static int insert_revoke_hash(journal_t
*journal
, unsigned long blocknr
,
123 struct list_head
*hash_list
;
124 struct jbd_revoke_record_s
*record
;
129 record
= kmem_cache_alloc(revoke_record_cache
, GFP_NOFS
);
133 record
->sequence
= seq
;
134 record
->blocknr
= blocknr
;
135 hash_list
= &journal
->j_revoke
->hash_table
[hash(journal
, blocknr
)];
136 list_add(&record
->hash
, hash_list
);
141 if (!journal_oom_retry
)
143 jbd_debug(1, "ENOMEM in " __FUNCTION__
", retrying.\n");
144 current
->policy
|= SCHED_YIELD
;
152 /* Find a revoke record in the journal's hash table. */
154 static struct jbd_revoke_record_s
*find_revoke_record(journal_t
*journal
,
155 unsigned long blocknr
)
157 struct list_head
*hash_list
;
158 struct jbd_revoke_record_s
*record
;
160 hash_list
= &journal
->j_revoke
->hash_table
[hash(journal
, blocknr
)];
162 record
= (struct jbd_revoke_record_s
*) hash_list
->next
;
163 while (&(record
->hash
) != hash_list
) {
164 if (record
->blocknr
== blocknr
)
166 record
= (struct jbd_revoke_record_s
*) record
->hash
.next
;
171 int __init
journal_init_revoke_caches(void)
173 revoke_record_cache
= kmem_cache_create("revoke_record",
174 sizeof(struct jbd_revoke_record_s
),
175 0, SLAB_HWCACHE_ALIGN
, NULL
, NULL
);
176 if (revoke_record_cache
== 0)
179 revoke_table_cache
= kmem_cache_create("revoke_table",
180 sizeof(struct jbd_revoke_table_s
),
182 if (revoke_table_cache
== 0) {
183 kmem_cache_destroy(revoke_record_cache
);
184 revoke_record_cache
= NULL
;
190 void journal_destroy_revoke_caches(void)
192 kmem_cache_destroy(revoke_record_cache
);
193 revoke_record_cache
= 0;
194 kmem_cache_destroy(revoke_table_cache
);
195 revoke_table_cache
= 0;
198 /* Initialise the revoke table for a given journal to a given size. */
200 int journal_init_revoke(journal_t
*journal
, int hash_size
)
204 J_ASSERT (journal
->j_revoke
== NULL
);
206 journal
->j_revoke
= kmem_cache_alloc(revoke_table_cache
, GFP_KERNEL
);
207 if (!journal
->j_revoke
)
210 /* Check that the hash_size is a power of two */
211 J_ASSERT ((hash_size
& (hash_size
-1)) == 0);
213 journal
->j_revoke
->hash_size
= hash_size
;
217 while((tmp
>>= 1UL) != 0UL)
219 journal
->j_revoke
->hash_shift
= shift
;
221 journal
->j_revoke
->hash_table
=
222 kmalloc(hash_size
* sizeof(struct list_head
), GFP_KERNEL
);
223 if (!journal
->j_revoke
->hash_table
) {
224 kmem_cache_free(revoke_table_cache
, journal
->j_revoke
);
225 journal
->j_revoke
= NULL
;
229 for (tmp
= 0; tmp
< hash_size
; tmp
++)
230 INIT_LIST_HEAD(&journal
->j_revoke
->hash_table
[tmp
]);
235 /* Destoy a journal's revoke table. The table must already be empty! */
237 void journal_destroy_revoke(journal_t
*journal
)
239 struct jbd_revoke_table_s
*table
;
240 struct list_head
*hash_list
;
243 table
= journal
->j_revoke
;
247 for (i
=0; i
<table
->hash_size
; i
++) {
248 hash_list
= &table
->hash_table
[i
];
249 J_ASSERT (list_empty(hash_list
));
252 kfree(table
->hash_table
);
253 kmem_cache_free(revoke_table_cache
, table
);
254 journal
->j_revoke
= NULL
;
261 * journal_revoke: revoke a given buffer_head from the journal. This
262 * prevents the block from being replayed during recovery if we take a
263 * crash after this current transaction commits. Any subsequent
264 * metadata writes of the buffer in this transaction cancel the
267 * Note that this call may block --- it is up to the caller to make
268 * sure that there are no further calls to journal_write_metadata
269 * before the revoke is complete. In ext3, this implies calling the
270 * revoke before clearing the block bitmap when we are deleting
273 * Revoke performs a journal_forget on any buffer_head passed in as a
274 * parameter, but does _not_ forget the buffer_head if the bh was only
277 * bh_in may not be a journalled buffer - it may have come off
278 * the hash tables without an attached journal_head.
280 * If bh_in is non-zero, journal_revoke() will decrement its b_count
284 int journal_revoke(handle_t
*handle
, unsigned long blocknr
,
285 struct buffer_head
*bh_in
)
287 struct buffer_head
*bh
= NULL
;
293 BUFFER_TRACE(bh_in
, "enter");
295 journal
= handle
->h_transaction
->t_journal
;
296 if (!journal_set_features(journal
, 0, 0, JFS_FEATURE_INCOMPAT_REVOKE
)){
297 J_ASSERT (!"Cannot set revoke feature!");
301 dev
= journal
->j_fs_dev
;
305 bh
= get_hash_table(dev
, blocknr
, journal
->j_blocksize
);
307 BUFFER_TRACE(bh
, "found on hash");
309 #ifdef JBD_EXPENSIVE_CHECKING
311 struct buffer_head
*bh2
;
313 /* If there is a different buffer_head lying around in
314 * memory anywhere... */
315 bh2
= get_hash_table(dev
, blocknr
, journal
->j_blocksize
);
317 /* ... and it has RevokeValid status... */
319 test_bit(BH_RevokeValid
, &bh2
->b_state
))
320 /* ...then it better be revoked too,
321 * since it's illegal to create a revoke
322 * record against a buffer_head which is
323 * not marked revoked --- that would
324 * risk missing a subsequent revoke
326 J_ASSERT_BH(bh2
, test_bit(BH_Revoked
, &
333 /* We really ought not ever to revoke twice in a row without
334 first having the revoke cancelled: it's illegal to free a
335 block twice without allocating it in between! */
337 J_ASSERT_BH(bh
, !test_bit(BH_Revoked
, &bh
->b_state
));
338 set_bit(BH_Revoked
, &bh
->b_state
);
339 set_bit(BH_RevokeValid
, &bh
->b_state
);
341 BUFFER_TRACE(bh_in
, "call journal_forget");
342 journal_forget(handle
, bh_in
);
344 BUFFER_TRACE(bh
, "call brelse");
349 lock_journal(journal
);
350 jbd_debug(2, "insert revoke for block %lu, bh_in=%p\n", blocknr
, bh_in
);
351 err
= insert_revoke_hash(journal
, blocknr
,
352 handle
->h_transaction
->t_tid
);
353 unlock_journal(journal
);
354 BUFFER_TRACE(bh_in
, "exit");
359 * Cancel an outstanding revoke. For use only internally by the
360 * journaling code (called from journal_get_write_access).
362 * We trust the BH_Revoked bit on the buffer if the buffer is already
363 * being journaled: if there is no revoke pending on the buffer, then we
364 * don't do anything here.
366 * This would break if it were possible for a buffer to be revoked and
367 * discarded, and then reallocated within the same transaction. In such
368 * a case we would have lost the revoked bit, but when we arrived here
369 * the second time we would still have a pending revoke to cancel. So,
370 * do not trust the Revoked bit on buffers unless RevokeValid is also
373 * The caller must have the journal locked.
375 int journal_cancel_revoke(handle_t
*handle
, struct journal_head
*jh
)
377 struct jbd_revoke_record_s
*record
;
378 journal_t
*journal
= handle
->h_transaction
->t_journal
;
380 int did_revoke
= 0; /* akpm: debug */
381 struct buffer_head
*bh
= jh2bh(jh
);
383 jbd_debug(4, "journal_head %p, cancelling revoke\n", jh
);
385 /* Is the existing Revoke bit valid? If so, we trust it, and
386 * only perform the full cancel if the revoke bit is set. If
387 * not, we can't trust the revoke bit, and we need to do the
388 * full search for a revoke record. */
389 if (test_and_set_bit(BH_RevokeValid
, &bh
->b_state
))
390 need_cancel
= (test_and_clear_bit(BH_Revoked
, &bh
->b_state
));
393 clear_bit(BH_Revoked
, &bh
->b_state
);
397 record
= find_revoke_record(journal
, bh
->b_blocknr
);
399 jbd_debug(4, "cancelled existing revoke on "
400 "blocknr %lu\n", bh
->b_blocknr
);
401 list_del(&record
->hash
);
402 kmem_cache_free(revoke_record_cache
, record
);
407 #ifdef JBD_EXPENSIVE_CHECKING
408 /* There better not be one left behind by now! */
409 record
= find_revoke_record(journal
, bh
->b_blocknr
);
410 J_ASSERT_JH(jh
, record
== NULL
);
413 /* Finally, have we just cleared revoke on an unhashed
414 * buffer_head? If so, we'd better make sure we clear the
415 * revoked status on any hashed alias too, otherwise the revoke
416 * state machine will get very upset later on. */
417 if (need_cancel
&& !bh
->b_pprev
) {
418 struct buffer_head
*bh2
;
419 bh2
= get_hash_table(bh
->b_dev
, bh
->b_blocknr
, bh
->b_size
);
421 clear_bit(BH_Revoked
, &bh2
->b_state
);
431 * Write revoke records to the journal for all entries in the current
432 * revoke hash, deleting the entries as we go.
434 * Called with the journal lock held.
437 void journal_write_revoke_records(journal_t
*journal
,
438 transaction_t
*transaction
)
440 struct journal_head
*descriptor
;
441 struct jbd_revoke_record_s
*record
;
442 struct jbd_revoke_table_s
*revoke
;
443 struct list_head
*hash_list
;
444 int i
, offset
, count
;
449 revoke
= journal
->j_revoke
;
451 for (i
= 0; i
< revoke
->hash_size
; i
++) {
452 hash_list
= &revoke
->hash_table
[i
];
454 while (!list_empty(hash_list
)) {
455 record
= (struct jbd_revoke_record_s
*)
457 write_one_revoke_record(journal
, transaction
,
458 &descriptor
, &offset
,
461 list_del(&record
->hash
);
462 kmem_cache_free(revoke_record_cache
, record
);
466 flush_descriptor(journal
, descriptor
, offset
);
467 jbd_debug(1, "Wrote %d revoke records\n", count
);
471 * Write out one revoke record. We need to create a new descriptor
472 * block if the old one is full or if we have not already created one.
475 static void write_one_revoke_record(journal_t
*journal
,
476 transaction_t
*transaction
,
477 struct journal_head
**descriptorp
,
479 struct jbd_revoke_record_s
*record
)
481 struct journal_head
*descriptor
;
483 journal_header_t
*header
;
485 /* If we are already aborting, this all becomes a noop. We
486 still need to go round the loop in
487 journal_write_revoke_records in order to free all of the
488 revoke records: only the IO to the journal is omitted. */
489 if (is_journal_aborted(journal
))
492 descriptor
= *descriptorp
;
495 /* Make sure we have a descriptor with space left for the record */
497 if (offset
== journal
->j_blocksize
) {
498 flush_descriptor(journal
, descriptor
, offset
);
504 descriptor
= journal_get_descriptor_buffer(journal
);
507 header
= (journal_header_t
*) &jh2bh(descriptor
)->b_data
[0];
508 header
->h_magic
= htonl(JFS_MAGIC_NUMBER
);
509 header
->h_blocktype
= htonl(JFS_REVOKE_BLOCK
);
510 header
->h_sequence
= htonl(transaction
->t_tid
);
512 /* Record it so that we can wait for IO completion later */
513 JBUFFER_TRACE(descriptor
, "file as BJ_LogCtl");
514 journal_file_buffer(descriptor
, transaction
, BJ_LogCtl
);
516 offset
= sizeof(journal_revoke_header_t
);
517 *descriptorp
= descriptor
;
520 * ((unsigned int *)(&jh2bh(descriptor
)->b_data
[offset
])) =
521 htonl(record
->blocknr
);
527 * Flush a revoke descriptor out to the journal. If we are aborting,
528 * this is a noop; otherwise we are generating a buffer which needs to
529 * be waited for during commit, so it has to go onto the appropriate
530 * journal buffer list.
533 static void flush_descriptor(journal_t
*journal
,
534 struct journal_head
*descriptor
,
537 journal_revoke_header_t
*header
;
539 if (is_journal_aborted(journal
)) {
540 JBUFFER_TRACE(descriptor
, "brelse");
541 __brelse(jh2bh(descriptor
));
545 header
= (journal_revoke_header_t
*) jh2bh(descriptor
)->b_data
;
546 header
->r_count
= htonl(offset
);
547 set_bit(BH_JWrite
, &jh2bh(descriptor
)->b_state
);
549 struct buffer_head
*bh
= jh2bh(descriptor
);
550 BUFFER_TRACE(bh
, "write");
551 ll_rw_block (WRITE
, 1, &bh
);
558 * Revoke support for recovery.
560 * Recovery needs to be able to:
562 * record all revoke records, including the tid of the latest instance
563 * of each revoke in the journal
565 * check whether a given block in a given transaction should be replayed
566 * (ie. has not been revoked by a revoke record in that or a subsequent
569 * empty the revoke table after recovery.
573 * First, setting revoke records. We create a new revoke record for
574 * every block ever revoked in the log as we scan it for recovery, and
575 * we update the existing records if we find multiple revokes for a
579 int journal_set_revoke(journal_t
*journal
,
580 unsigned long blocknr
,
583 struct jbd_revoke_record_s
*record
;
585 record
= find_revoke_record(journal
, blocknr
);
587 /* If we have multiple occurences, only record the
588 * latest sequence number in the hashed record */
589 if (tid_gt(sequence
, record
->sequence
))
590 record
->sequence
= sequence
;
593 return insert_revoke_hash(journal
, blocknr
, sequence
);
597 * Test revoke records. For a given block referenced in the log, has
598 * that block been revoked? A revoke record with a given transaction
599 * sequence number revokes all blocks in that transaction and earlier
600 * ones, but later transactions still need replayed.
603 int journal_test_revoke(journal_t
*journal
,
604 unsigned long blocknr
,
607 struct jbd_revoke_record_s
*record
;
609 record
= find_revoke_record(journal
, blocknr
);
612 if (tid_gt(sequence
, record
->sequence
))
618 * Finally, once recovery is over, we need to clear the revoke table so
619 * that it can be reused by the running filesystem.
622 void journal_clear_revoke(journal_t
*journal
)
625 struct list_head
*hash_list
;
626 struct jbd_revoke_record_s
*record
;
627 struct jbd_revoke_table_s
*revoke
;
629 revoke
= journal
->j_revoke
;
631 for (i
= 0; i
< revoke
->hash_size
; i
++) {
632 hash_list
= &revoke
->hash_table
[i
];
633 while (!list_empty(hash_list
)) {
634 record
= (struct jbd_revoke_record_s
*) hash_list
->next
;
635 list_del(&record
->hash
);
636 kmem_cache_free(revoke_record_cache
, record
);