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.
64 #include <linux/sched.h>
66 #include <linux/jbd.h>
67 #include <linux/errno.h>
68 #include <linux/slab.h>
69 #include <linux/locks.h>
70 #include <linux/list.h>
71 #include <linux/smp_lock.h>
72 #include <linux/init.h>
75 static lkmem_cache_t *revoke_record_cache;
76 static lkmem_cache_t *revoke_table_cache;
78 /* Each revoke record represents one single revoked block. During
79 journal replay, this involves recording the transaction ID of the
80 last transaction to revoke this block. */
82 struct jbd_revoke_record_s
84 struct list_head hash;
85 tid_t sequence; /* Used for recovery only */
86 unsigned long blocknr;
90 /* The revoke table is just a simple hash table of revoke records. */
91 struct jbd_revoke_table_s
93 /* It is conceivable that we might want a larger hash table
94 * for recovery. Must be a power of two. */
97 struct list_head *hash_table;
102 static void write_one_revoke_record(journal_t *, transaction_t *,
103 struct journal_head **, int *,
104 struct jbd_revoke_record_s *);
105 static void flush_descriptor(journal_t *, struct journal_head *, int);
108 /* Utility functions to maintain the revoke table */
110 /* Borrowed from buffer.c: this is a tried and tested block hash function */
111 static inline int hash(journal_t *journal, unsigned long block)
113 struct jbd_revoke_table_s *table = journal->j_revoke;
114 int hash_shift = table->hash_shift;
116 return ((block << (hash_shift - 6)) ^
118 (block << (hash_shift - 12))) & (table->hash_size - 1);
121 static int insert_revoke_hash(journal_t *journal, unsigned long blocknr,
124 struct list_head *hash_list;
125 struct jbd_revoke_record_s *record;
130 record = kmem_cache_alloc(revoke_record_cache, GFP_NOFS);
134 record->sequence = seq;
135 record->blocknr = blocknr;
136 hash_list = &journal->j_revoke->hash_table[hash(journal, blocknr)];
137 list_add(&record->hash, hash_list);
142 if (!journal_oom_retry)
144 jbd_debug(1, "ENOMEM in " __FUNCTION__ ", retrying.\n");
145 current->policy |= SCHED_YIELD;
153 /* Find a revoke record in the journal's hash table. */
155 static struct jbd_revoke_record_s *find_revoke_record(journal_t *journal,
156 unsigned long blocknr)
158 struct list_head *hash_list;
159 struct jbd_revoke_record_s *record;
161 hash_list = &journal->j_revoke->hash_table[hash(journal, blocknr)];
163 record = (struct jbd_revoke_record_s *) hash_list->next;
164 while (&(record->hash) != hash_list) {
165 if (record->blocknr == blocknr)
167 record = (struct jbd_revoke_record_s *) record->hash.next;
172 int __init journal_init_revoke_caches(void)
174 revoke_record_cache = kmem_cache_create("revoke_record",
175 sizeof(struct jbd_revoke_record_s),
176 0, SLAB_HWCACHE_ALIGN, NULL, NULL);
177 if (revoke_record_cache == 0)
180 revoke_table_cache = kmem_cache_create("revoke_table",
181 sizeof(struct jbd_revoke_table_s),
183 if (revoke_table_cache == 0) {
184 kmem_cache_destroy(revoke_record_cache);
185 revoke_record_cache = NULL;
191 void journal_destroy_revoke_caches(void)
193 kmem_cache_destroy(revoke_record_cache);
194 revoke_record_cache = 0;
195 kmem_cache_destroy(revoke_table_cache);
196 revoke_table_cache = 0;
199 /* Initialise the revoke table for a given journal to a given size. */
201 int journal_init_revoke(journal_t *journal, int hash_size)
205 J_ASSERT (journal->j_revoke == NULL);
207 journal->j_revoke = kmem_cache_alloc(revoke_table_cache, GFP_KERNEL);
208 if (!journal->j_revoke)
211 /* Check that the hash_size is a power of two */
212 J_ASSERT ((hash_size & (hash_size-1)) == 0);
214 journal->j_revoke->hash_size = hash_size;
218 while((tmp >>= 1UL) != 0UL)
220 journal->j_revoke->hash_shift = shift;
222 journal->j_revoke->hash_table =
223 kmalloc(hash_size * sizeof(struct list_head), GFP_KERNEL);
224 if (!journal->j_revoke->hash_table) {
225 kmem_cache_free(revoke_table_cache, journal->j_revoke);
226 journal->j_revoke = NULL;
230 for (tmp = 0; tmp < hash_size; tmp++)
231 INIT_LIST_HEAD(&journal->j_revoke->hash_table[tmp]);
236 /* Destoy a journal's revoke table. The table must already be empty! */
238 void journal_destroy_revoke(journal_t *journal)
240 struct jbd_revoke_table_s *table;
241 struct list_head *hash_list;
244 table = journal->j_revoke;
248 for (i=0; i<table->hash_size; i++) {
249 hash_list = &table->hash_table[i];
250 J_ASSERT (list_empty(hash_list));
253 kfree(table->hash_table);
254 kmem_cache_free(revoke_table_cache, table);
255 journal->j_revoke = NULL;
262 * journal_revoke: revoke a given buffer_head from the journal. This
263 * prevents the block from being replayed during recovery if we take a
264 * crash after this current transaction commits. Any subsequent
265 * metadata writes of the buffer in this transaction cancel the
268 * Note that this call may block --- it is up to the caller to make
269 * sure that there are no further calls to journal_write_metadata
270 * before the revoke is complete. In ext3, this implies calling the
271 * revoke before clearing the block bitmap when we are deleting
274 * Revoke performs a journal_forget on any buffer_head passed in as a
275 * parameter, but does _not_ forget the buffer_head if the bh was only
278 * bh_in may not be a journalled buffer - it may have come off
279 * the hash tables without an attached journal_head.
281 * If bh_in is non-zero, journal_revoke() will decrement its b_count
285 int journal_revoke(handle_t *handle, unsigned long blocknr,
286 struct buffer_head *bh_in)
288 struct buffer_head *bh = NULL;
294 BUFFER_TRACE(bh_in, "enter");
296 journal = handle->h_transaction->t_journal;
297 if (!journal_set_features(journal, 0, 0, JFS_FEATURE_INCOMPAT_REVOKE)){
298 J_ASSERT (!"Cannot set revoke feature!");
302 dev = journal->j_fs_dev;
306 bh = get_hash_table(dev, blocknr, journal->j_blocksize);
308 BUFFER_TRACE(bh, "found on hash");
310 #ifdef JBD_EXPENSIVE_CHECKING
312 struct buffer_head *bh2;
314 /* If there is a different buffer_head lying around in
315 * memory anywhere... */
316 bh2 = get_hash_table(dev, blocknr, journal->j_blocksize);
318 /* ... and it has RevokeValid status... */
320 test_bit(BH_RevokeValid, &bh2->b_state))
321 /* ...then it better be revoked too,
322 * since it's illegal to create a revoke
323 * record against a buffer_head which is
324 * not marked revoked --- that would
325 * risk missing a subsequent revoke
327 J_ASSERT_BH(bh2, test_bit(BH_Revoked, &
334 /* We really ought not ever to revoke twice in a row without
335 first having the revoke cancelled: it's illegal to free a
336 block twice without allocating it in between! */
338 J_ASSERT_BH(bh, !test_bit(BH_Revoked, &bh->b_state));
339 set_bit(BH_Revoked, &bh->b_state);
340 set_bit(BH_RevokeValid, &bh->b_state);
342 BUFFER_TRACE(bh_in, "call journal_forget");
343 journal_forget(handle, bh_in);
345 BUFFER_TRACE(bh, "call brelse");
350 lock_journal(journal);
351 jbd_debug(2, "insert revoke for block %lu, bh_in=%p\n", blocknr, bh_in);
352 err = insert_revoke_hash(journal, blocknr,
353 handle->h_transaction->t_tid);
354 unlock_journal(journal);
355 BUFFER_TRACE(bh_in, "exit");
360 * Cancel an outstanding revoke. For use only internally by the
361 * journaling code (called from journal_get_write_access).
363 * We trust the BH_Revoked bit on the buffer if the buffer is already
364 * being journaled: if there is no revoke pending on the buffer, then we
365 * don't do anything here.
367 * This would break if it were possible for a buffer to be revoked and
368 * discarded, and then reallocated within the same transaction. In such
369 * a case we would have lost the revoked bit, but when we arrived here
370 * the second time we would still have a pending revoke to cancel. So,
371 * do not trust the Revoked bit on buffers unless RevokeValid is also
374 * The caller must have the journal locked.
376 int journal_cancel_revoke(handle_t *handle, struct journal_head *jh)
378 struct jbd_revoke_record_s *record;
379 journal_t *journal = handle->h_transaction->t_journal;
381 int did_revoke = 0; /* akpm: debug */
382 struct buffer_head *bh = jh2bh(jh);
384 jbd_debug(4, "journal_head %p, cancelling revoke\n", jh);
386 /* Is the existing Revoke bit valid? If so, we trust it, and
387 * only perform the full cancel if the revoke bit is set. If
388 * not, we can't trust the revoke bit, and we need to do the
389 * full search for a revoke record. */
390 if (test_and_set_bit(BH_RevokeValid, &bh->b_state))
391 need_cancel = (test_and_clear_bit(BH_Revoked, &bh->b_state));
394 clear_bit(BH_Revoked, &bh->b_state);
398 record = find_revoke_record(journal, bh->b_blocknr);
400 jbd_debug(4, "cancelled existing revoke on "
401 "blocknr %lu\n", bh->b_blocknr);
402 list_del(&record->hash);
403 kmem_cache_free(revoke_record_cache, record);
408 #ifdef JBD_EXPENSIVE_CHECKING
409 /* There better not be one left behind by now! */
410 record = find_revoke_record(journal, bh->b_blocknr);
411 J_ASSERT_JH(jh, record == NULL);
414 /* Finally, have we just cleared revoke on an unhashed
415 * buffer_head? If so, we'd better make sure we clear the
416 * revoked status on any hashed alias too, otherwise the revoke
417 * state machine will get very upset later on. */
418 if (need_cancel && !bh->b_pprev) {
419 struct buffer_head *bh2;
420 bh2 = get_hash_table(bh->b_dev, bh->b_blocknr, bh->b_size);
422 clear_bit(BH_Revoked, &bh2->b_state);
432 * Write revoke records to the journal for all entries in the current
433 * revoke hash, deleting the entries as we go.
435 * Called with the journal lock held.
438 void journal_write_revoke_records(journal_t *journal,
439 transaction_t *transaction)
441 struct journal_head *descriptor;
442 struct jbd_revoke_record_s *record;
443 struct jbd_revoke_table_s *revoke;
444 struct list_head *hash_list;
445 int i, offset, count;
450 revoke = journal->j_revoke;
452 for (i = 0; i < revoke->hash_size; i++) {
453 hash_list = &revoke->hash_table[i];
455 while (!list_empty(hash_list)) {
456 record = (struct jbd_revoke_record_s *)
458 write_one_revoke_record(journal, transaction,
459 &descriptor, &offset,
462 list_del(&record->hash);
463 kmem_cache_free(revoke_record_cache, record);
467 flush_descriptor(journal, descriptor, offset);
468 jbd_debug(1, "Wrote %d revoke records\n", count);
472 * Write out one revoke record. We need to create a new descriptor
473 * block if the old one is full or if we have not already created one.
476 static void write_one_revoke_record(journal_t *journal,
477 transaction_t *transaction,
478 struct journal_head **descriptorp,
480 struct jbd_revoke_record_s *record)
482 struct journal_head *descriptor;
484 journal_header_t *header;
486 /* If we are already aborting, this all becomes a noop. We
487 still need to go round the loop in
488 journal_write_revoke_records in order to free all of the
489 revoke records: only the IO to the journal is omitted. */
490 if (is_journal_aborted(journal))
493 descriptor = *descriptorp;
496 /* Make sure we have a descriptor with space left for the record */
498 if (offset == journal->j_blocksize) {
499 flush_descriptor(journal, descriptor, offset);
505 descriptor = journal_get_descriptor_buffer(journal);
508 header = (journal_header_t *) &jh2bh(descriptor)->b_data[0];
509 header->h_magic = htonl(JFS_MAGIC_NUMBER);
510 header->h_blocktype = htonl(JFS_REVOKE_BLOCK);
511 header->h_sequence = htonl(transaction->t_tid);
513 /* Record it so that we can wait for IO completion later */
514 JBUFFER_TRACE(descriptor, "file as BJ_LogCtl");
515 journal_file_buffer(descriptor, transaction, BJ_LogCtl);
517 offset = sizeof(journal_revoke_header_t);
518 *descriptorp = descriptor;
521 * ((unsigned int *)(&jh2bh(descriptor)->b_data[offset])) =
522 htonl(record->blocknr);
528 * Flush a revoke descriptor out to the journal. If we are aborting,
529 * this is a noop; otherwise we are generating a buffer which needs to
530 * be waited for during commit, so it has to go onto the appropriate
531 * journal buffer list.
534 static void flush_descriptor(journal_t *journal,
535 struct journal_head *descriptor,
538 journal_revoke_header_t *header;
540 if (is_journal_aborted(journal)) {
541 JBUFFER_TRACE(descriptor, "brelse");
542 __brelse(jh2bh(descriptor));
546 header = (journal_revoke_header_t *) jh2bh(descriptor)->b_data;
547 header->r_count = htonl(offset);
548 set_bit(BH_JWrite, &jh2bh(descriptor)->b_state);
550 struct buffer_head *bh = jh2bh(descriptor);
551 BUFFER_TRACE(bh, "write");
552 ll_rw_block (WRITE, 1, &bh);
559 * Revoke support for recovery.
561 * Recovery needs to be able to:
563 * record all revoke records, including the tid of the latest instance
564 * of each revoke in the journal
566 * check whether a given block in a given transaction should be replayed
567 * (ie. has not been revoked by a revoke record in that or a subsequent
570 * empty the revoke table after recovery.
574 * First, setting revoke records. We create a new revoke record for
575 * every block ever revoked in the log as we scan it for recovery, and
576 * we update the existing records if we find multiple revokes for a
580 int journal_set_revoke(journal_t *journal,
581 unsigned long blocknr,
584 struct jbd_revoke_record_s *record;
586 record = find_revoke_record(journal, blocknr);
588 /* If we have multiple occurences, only record the
589 * latest sequence number in the hashed record */
590 if (tid_gt(sequence, record->sequence))
591 record->sequence = sequence;
594 return insert_revoke_hash(journal, blocknr, sequence);
598 * Test revoke records. For a given block referenced in the log, has
599 * that block been revoked? A revoke record with a given transaction
600 * sequence number revokes all blocks in that transaction and earlier
601 * ones, but later transactions still need replayed.
604 int journal_test_revoke(journal_t *journal,
605 unsigned long blocknr,
608 struct jbd_revoke_record_s *record;
610 record = find_revoke_record(journal, blocknr);
613 if (tid_gt(sequence, record->sequence))
619 * Finally, once recovery is over, we need to clear the revoke table so
620 * that it can be reused by the running filesystem.
623 void journal_clear_revoke(journal_t *journal)
626 struct list_head *hash_list;
627 struct jbd_revoke_record_s *record;
628 struct jbd_revoke_table_s *revoke;
630 revoke = journal->j_revoke;
632 for (i = 0; i < revoke->hash_size; i++) {
633 hash_list = &revoke->hash_table[i];
634 while (!list_empty(hash_list)) {
635 record = (struct jbd_revoke_record_s*) hash_list->next;
636 list_del(&record->hash);
637 kmem_cache_free(revoke_record_cache, record);