blob: 8ff5a7b89b9ae6ef852b86f62d32e1de6b0b8b7c [file] [log] [blame]
Linus Torvalds1da177e2005-04-16 15:20:36 -07001/*
Uwe Kleine-König58862692007-05-09 07:51:49 +02002 * linux/fs/jbd/revoke.c
Mingming Caoae6ddcc2006-09-27 01:49:27 -07003 *
Linus Torvalds1da177e2005-04-16 15:20:36 -07004 * Written by Stephen C. Tweedie <sct@redhat.com>, 2000
5 *
6 * Copyright 2000 Red Hat corp --- All Rights Reserved
7 *
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.
11 *
12 * Journal revoke routines for the generic filesystem journaling code;
13 * part of the ext2fs journaling system.
14 *
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:
Mingming Caoae6ddcc2006-09-27 01:49:27 -070018 *
Linus Torvalds1da177e2005-04-16 15:20:36 -070019 * + Commit: during commit we write the entire list of the current
20 * transaction's revoked blocks to the journal
Mingming Caoae6ddcc2006-09-27 01:49:27 -070021 *
Linus Torvalds1da177e2005-04-16 15:20:36 -070022 * + 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
26 * gets replayed.
27 *
28 * We can get interactions between revokes and new log data within a
29 * single transaction:
30 *
31 * Block is revoked and then journaled:
Mingming Caoae6ddcc2006-09-27 01:49:27 -070032 * The desired end result is the journaling of the new block, so we
Linus Torvalds1da177e2005-04-16 15:20:36 -070033 * cancel the revoke before the transaction commits.
34 *
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.
43 *
Mingming Caoae6ddcc2006-09-27 01:49:27 -070044 * Block is revoked and then written as data:
Linus Torvalds1da177e2005-04-16 15:20:36 -070045 * 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
48 * bit here.
49 *
50 * Revoke information on buffers is a tri-state value:
51 *
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
55 * need do nothing.
56 * RevokeValid set, Revoked set:
Mingming Caoae6ddcc2006-09-27 01:49:27 -070057 * buffer has been revoked.
Linus Torvalds1da177e2005-04-16 15:20:36 -070058 */
59
60#ifndef __KERNEL__
61#include "jfs_user.h"
62#else
63#include <linux/time.h>
64#include <linux/fs.h>
65#include <linux/jbd.h>
66#include <linux/errno.h>
67#include <linux/slab.h>
68#include <linux/list.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070069#include <linux/init.h>
70#endif
vignesh babuf4823942007-07-15 23:41:17 -070071#include <linux/log2.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070072
Christoph Lametere18b8902006-12-06 20:33:20 -080073static struct kmem_cache *revoke_record_cache;
74static struct kmem_cache *revoke_table_cache;
Linus Torvalds1da177e2005-04-16 15:20:36 -070075
76/* Each revoke record represents one single revoked block. During
77 journal replay, this involves recording the transaction ID of the
78 last transaction to revoke this block. */
79
Mingming Caoae6ddcc2006-09-27 01:49:27 -070080struct jbd_revoke_record_s
Linus Torvalds1da177e2005-04-16 15:20:36 -070081{
82 struct list_head hash;
83 tid_t sequence; /* Used for recovery only */
84 unsigned long blocknr;
85};
86
87
88/* The revoke table is just a simple hash table of revoke records. */
89struct jbd_revoke_table_s
90{
91 /* It is conceivable that we might want a larger hash table
92 * for recovery. Must be a power of two. */
Mingming Caoae6ddcc2006-09-27 01:49:27 -070093 int hash_size;
94 int hash_shift;
Linus Torvalds1da177e2005-04-16 15:20:36 -070095 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 jbd_revoke_record_s *);
103static void flush_descriptor(journal_t *, struct journal_head *, int);
104#endif
105
106/* Utility functions to maintain the revoke table */
107
108/* Borrowed from buffer.c: this is a tried and tested block hash function */
109static inline int hash(journal_t *journal, unsigned long block)
110{
111 struct jbd_revoke_table_s *table = journal->j_revoke;
112 int hash_shift = table->hash_shift;
113
114 return ((block << (hash_shift - 6)) ^
115 (block >> 13) ^
116 (block << (hash_shift - 12))) & (table->hash_size - 1);
117}
118
Adrian Bunk022a4a72005-09-06 15:16:41 -0700119static int insert_revoke_hash(journal_t *journal, unsigned long blocknr,
120 tid_t seq)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700121{
122 struct list_head *hash_list;
123 struct jbd_revoke_record_s *record;
124
125repeat:
126 record = kmem_cache_alloc(revoke_record_cache, GFP_NOFS);
127 if (!record)
128 goto oom;
129
130 record->sequence = seq;
131 record->blocknr = blocknr;
132 hash_list = &journal->j_revoke->hash_table[hash(journal, blocknr)];
133 spin_lock(&journal->j_revoke_lock);
134 list_add(&record->hash, hash_list);
135 spin_unlock(&journal->j_revoke_lock);
136 return 0;
137
138oom:
139 if (!journal_oom_retry)
140 return -ENOMEM;
Harvey Harrison08fc99b2008-04-28 02:16:16 -0700141 jbd_debug(1, "ENOMEM in %s, retrying\n", __func__);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700142 yield();
143 goto repeat;
144}
145
146/* Find a revoke record in the journal's hash table. */
147
148static struct jbd_revoke_record_s *find_revoke_record(journal_t *journal,
149 unsigned long blocknr)
150{
151 struct list_head *hash_list;
152 struct jbd_revoke_record_s *record;
153
154 hash_list = &journal->j_revoke->hash_table[hash(journal, blocknr)];
155
156 spin_lock(&journal->j_revoke_lock);
157 record = (struct jbd_revoke_record_s *) hash_list->next;
158 while (&(record->hash) != hash_list) {
159 if (record->blocknr == blocknr) {
160 spin_unlock(&journal->j_revoke_lock);
161 return record;
162 }
163 record = (struct jbd_revoke_record_s *) record->hash.next;
164 }
165 spin_unlock(&journal->j_revoke_lock);
166 return NULL;
167}
168
169int __init journal_init_revoke_caches(void)
170{
171 revoke_record_cache = kmem_cache_create("revoke_record",
172 sizeof(struct jbd_revoke_record_s),
Mel Gormane12ba742007-10-16 01:25:52 -0700173 0,
174 SLAB_HWCACHE_ALIGN|SLAB_TEMPORARY,
175 NULL);
Al Viro1076d172008-03-29 03:07:18 +0000176 if (!revoke_record_cache)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700177 return -ENOMEM;
178
179 revoke_table_cache = kmem_cache_create("revoke_table",
180 sizeof(struct jbd_revoke_table_s),
Mel Gormane12ba742007-10-16 01:25:52 -0700181 0, SLAB_TEMPORARY, NULL);
Al Viro1076d172008-03-29 03:07:18 +0000182 if (!revoke_table_cache) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700183 kmem_cache_destroy(revoke_record_cache);
184 revoke_record_cache = NULL;
185 return -ENOMEM;
186 }
187 return 0;
188}
189
190void journal_destroy_revoke_caches(void)
191{
192 kmem_cache_destroy(revoke_record_cache);
193 revoke_record_cache = NULL;
194 kmem_cache_destroy(revoke_table_cache);
195 revoke_table_cache = NULL;
196}
197
Duane Griffinf4d79ca2008-07-25 01:46:20 -0700198static struct jbd_revoke_table_s *journal_init_revoke_table(int hash_size)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700199{
Duane Griffinf4d79ca2008-07-25 01:46:20 -0700200 int shift = 0;
201 int tmp = hash_size;
202 struct jbd_revoke_table_s *table;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700203
Duane Griffinf4d79ca2008-07-25 01:46:20 -0700204 table = kmem_cache_alloc(revoke_table_cache, GFP_KERNEL);
205 if (!table)
206 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700207
Linus Torvalds1da177e2005-04-16 15:20:36 -0700208 while((tmp >>= 1UL) != 0UL)
209 shift++;
210
Duane Griffinf4d79ca2008-07-25 01:46:20 -0700211 table->hash_size = hash_size;
212 table->hash_shift = shift;
213 table->hash_table =
Linus Torvalds1da177e2005-04-16 15:20:36 -0700214 kmalloc(hash_size * sizeof(struct list_head), GFP_KERNEL);
Duane Griffinf4d79ca2008-07-25 01:46:20 -0700215 if (!table->hash_table) {
216 kmem_cache_free(revoke_table_cache, table);
217 table = NULL;
218 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700219 }
220
221 for (tmp = 0; tmp < hash_size; tmp++)
Duane Griffinf4d79ca2008-07-25 01:46:20 -0700222 INIT_LIST_HEAD(&table->hash_table[tmp]);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700223
Duane Griffinf4d79ca2008-07-25 01:46:20 -0700224out:
225 return table;
226}
227
228static void journal_destroy_revoke_table(struct jbd_revoke_table_s *table)
229{
230 int i;
231 struct list_head *hash_list;
232
233 for (i = 0; i < table->hash_size; i++) {
234 hash_list = &table->hash_table[i];
235 J_ASSERT(list_empty(hash_list));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700236 }
237
Duane Griffinf4d79ca2008-07-25 01:46:20 -0700238 kfree(table->hash_table);
239 kmem_cache_free(revoke_table_cache, table);
240}
241
242/* Initialise the revoke table for a given journal to a given size. */
243int journal_init_revoke(journal_t *journal, int hash_size)
244{
245 J_ASSERT(journal->j_revoke_table[0] == NULL);
246 J_ASSERT(is_power_of_2(hash_size));
247
248 journal->j_revoke_table[0] = journal_init_revoke_table(hash_size);
249 if (!journal->j_revoke_table[0])
250 goto fail0;
251
252 journal->j_revoke_table[1] = journal_init_revoke_table(hash_size);
253 if (!journal->j_revoke_table[1])
254 goto fail1;
255
Linus Torvalds1da177e2005-04-16 15:20:36 -0700256 journal->j_revoke = journal->j_revoke_table[1];
257
Linus Torvalds1da177e2005-04-16 15:20:36 -0700258 spin_lock_init(&journal->j_revoke_lock);
259
260 return 0;
Duane Griffinf4d79ca2008-07-25 01:46:20 -0700261
262fail1:
263 journal_destroy_revoke_table(journal->j_revoke_table[0]);
264fail0:
265 return -ENOMEM;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700266}
267
Duane Griffinf4d79ca2008-07-25 01:46:20 -0700268/* Destroy a journal's revoke table. The table must already be empty! */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700269void journal_destroy_revoke(journal_t *journal)
270{
Linus Torvalds1da177e2005-04-16 15:20:36 -0700271 journal->j_revoke = NULL;
Duane Griffinf4d79ca2008-07-25 01:46:20 -0700272 if (journal->j_revoke_table[0])
273 journal_destroy_revoke_table(journal->j_revoke_table[0]);
274 if (journal->j_revoke_table[1])
275 journal_destroy_revoke_table(journal->j_revoke_table[1]);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700276}
277
278
279#ifdef __KERNEL__
280
Mingming Caoae6ddcc2006-09-27 01:49:27 -0700281/*
Linus Torvalds1da177e2005-04-16 15:20:36 -0700282 * journal_revoke: revoke a given buffer_head from the journal. This
283 * prevents the block from being replayed during recovery if we take a
284 * crash after this current transaction commits. Any subsequent
285 * metadata writes of the buffer in this transaction cancel the
Mingming Caoae6ddcc2006-09-27 01:49:27 -0700286 * revoke.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700287 *
288 * Note that this call may block --- it is up to the caller to make
289 * sure that there are no further calls to journal_write_metadata
290 * before the revoke is complete. In ext3, this implies calling the
291 * revoke before clearing the block bitmap when we are deleting
Mingming Caoae6ddcc2006-09-27 01:49:27 -0700292 * metadata.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700293 *
294 * Revoke performs a journal_forget on any buffer_head passed in as a
295 * parameter, but does _not_ forget the buffer_head if the bh was only
Mingming Caoae6ddcc2006-09-27 01:49:27 -0700296 * found implicitly.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700297 *
298 * bh_in may not be a journalled buffer - it may have come off
299 * the hash tables without an attached journal_head.
300 *
301 * If bh_in is non-zero, journal_revoke() will decrement its b_count
302 * by one.
303 */
304
Mingming Caoae6ddcc2006-09-27 01:49:27 -0700305int journal_revoke(handle_t *handle, unsigned long blocknr,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700306 struct buffer_head *bh_in)
307{
308 struct buffer_head *bh = NULL;
309 journal_t *journal;
310 struct block_device *bdev;
311 int err;
312
313 might_sleep();
314 if (bh_in)
315 BUFFER_TRACE(bh_in, "enter");
316
317 journal = handle->h_transaction->t_journal;
318 if (!journal_set_features(journal, 0, 0, JFS_FEATURE_INCOMPAT_REVOKE)){
319 J_ASSERT (!"Cannot set revoke feature!");
320 return -EINVAL;
321 }
322
323 bdev = journal->j_fs_dev;
324 bh = bh_in;
325
326 if (!bh) {
327 bh = __find_get_block(bdev, blocknr, journal->j_blocksize);
328 if (bh)
329 BUFFER_TRACE(bh, "found on hash");
330 }
331#ifdef JBD_EXPENSIVE_CHECKING
332 else {
333 struct buffer_head *bh2;
334
335 /* If there is a different buffer_head lying around in
336 * memory anywhere... */
337 bh2 = __find_get_block(bdev, blocknr, journal->j_blocksize);
338 if (bh2) {
339 /* ... and it has RevokeValid status... */
340 if (bh2 != bh && buffer_revokevalid(bh2))
341 /* ...then it better be revoked too,
342 * since it's illegal to create a revoke
343 * record against a buffer_head which is
344 * not marked revoked --- that would
345 * risk missing a subsequent revoke
346 * cancel. */
347 J_ASSERT_BH(bh2, buffer_revoked(bh2));
348 put_bh(bh2);
349 }
350 }
351#endif
352
353 /* We really ought not ever to revoke twice in a row without
354 first having the revoke cancelled: it's illegal to free a
355 block twice without allocating it in between! */
356 if (bh) {
357 if (!J_EXPECT_BH(bh, !buffer_revoked(bh),
358 "inconsistent data on disk")) {
359 if (!bh_in)
360 brelse(bh);
361 return -EIO;
362 }
363 set_buffer_revoked(bh);
364 set_buffer_revokevalid(bh);
365 if (bh_in) {
366 BUFFER_TRACE(bh_in, "call journal_forget");
367 journal_forget(handle, bh_in);
368 } else {
369 BUFFER_TRACE(bh, "call brelse");
370 __brelse(bh);
371 }
372 }
373
374 jbd_debug(2, "insert revoke for block %lu, bh_in=%p\n", blocknr, bh_in);
375 err = insert_revoke_hash(journal, blocknr,
376 handle->h_transaction->t_tid);
377 BUFFER_TRACE(bh_in, "exit");
378 return err;
379}
380
381/*
382 * Cancel an outstanding revoke. For use only internally by the
383 * journaling code (called from journal_get_write_access).
384 *
385 * We trust buffer_revoked() on the buffer if the buffer is already
386 * being journaled: if there is no revoke pending on the buffer, then we
387 * don't do anything here.
388 *
389 * This would break if it were possible for a buffer to be revoked and
390 * discarded, and then reallocated within the same transaction. In such
391 * a case we would have lost the revoked bit, but when we arrived here
392 * the second time we would still have a pending revoke to cancel. So,
393 * do not trust the Revoked bit on buffers unless RevokeValid is also
394 * set.
395 *
396 * The caller must have the journal locked.
397 */
398int journal_cancel_revoke(handle_t *handle, struct journal_head *jh)
399{
400 struct jbd_revoke_record_s *record;
401 journal_t *journal = handle->h_transaction->t_journal;
402 int need_cancel;
403 int did_revoke = 0; /* akpm: debug */
404 struct buffer_head *bh = jh2bh(jh);
405
406 jbd_debug(4, "journal_head %p, cancelling revoke\n", jh);
407
408 /* Is the existing Revoke bit valid? If so, we trust it, and
409 * only perform the full cancel if the revoke bit is set. If
410 * not, we can't trust the revoke bit, and we need to do the
411 * full search for a revoke record. */
412 if (test_set_buffer_revokevalid(bh)) {
413 need_cancel = test_clear_buffer_revoked(bh);
414 } else {
415 need_cancel = 1;
416 clear_buffer_revoked(bh);
417 }
418
419 if (need_cancel) {
420 record = find_revoke_record(journal, bh->b_blocknr);
421 if (record) {
422 jbd_debug(4, "cancelled existing revoke on "
423 "blocknr %llu\n", (unsigned long long)bh->b_blocknr);
424 spin_lock(&journal->j_revoke_lock);
425 list_del(&record->hash);
426 spin_unlock(&journal->j_revoke_lock);
427 kmem_cache_free(revoke_record_cache, record);
428 did_revoke = 1;
429 }
430 }
431
432#ifdef JBD_EXPENSIVE_CHECKING
433 /* There better not be one left behind by now! */
434 record = find_revoke_record(journal, bh->b_blocknr);
435 J_ASSERT_JH(jh, record == NULL);
436#endif
437
438 /* Finally, have we just cleared revoke on an unhashed
439 * buffer_head? If so, we'd better make sure we clear the
440 * revoked status on any hashed alias too, otherwise the revoke
441 * state machine will get very upset later on. */
442 if (need_cancel) {
443 struct buffer_head *bh2;
444 bh2 = __find_get_block(bh->b_bdev, bh->b_blocknr, bh->b_size);
445 if (bh2) {
446 if (bh2 != bh)
447 clear_buffer_revoked(bh2);
448 __brelse(bh2);
449 }
450 }
451 return did_revoke;
452}
453
454/* journal_switch_revoke table select j_revoke for next transaction
455 * we do not want to suspend any processing until all revokes are
456 * written -bzzz
457 */
458void journal_switch_revoke_table(journal_t *journal)
459{
460 int i;
461
462 if (journal->j_revoke == journal->j_revoke_table[0])
463 journal->j_revoke = journal->j_revoke_table[1];
464 else
465 journal->j_revoke = journal->j_revoke_table[0];
466
Mingming Caoae6ddcc2006-09-27 01:49:27 -0700467 for (i = 0; i < journal->j_revoke->hash_size; i++)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700468 INIT_LIST_HEAD(&journal->j_revoke->hash_table[i]);
469}
470
471/*
472 * Write revoke records to the journal for all entries in the current
473 * revoke hash, deleting the entries as we go.
474 *
475 * Called with the journal lock held.
476 */
477
Mingming Caoae6ddcc2006-09-27 01:49:27 -0700478void journal_write_revoke_records(journal_t *journal,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700479 transaction_t *transaction)
480{
481 struct journal_head *descriptor;
482 struct jbd_revoke_record_s *record;
483 struct jbd_revoke_table_s *revoke;
484 struct list_head *hash_list;
485 int i, offset, count;
486
Mingming Caoae6ddcc2006-09-27 01:49:27 -0700487 descriptor = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700488 offset = 0;
489 count = 0;
490
491 /* select revoke table for committing transaction */
492 revoke = journal->j_revoke == journal->j_revoke_table[0] ?
493 journal->j_revoke_table[1] : journal->j_revoke_table[0];
494
495 for (i = 0; i < revoke->hash_size; i++) {
496 hash_list = &revoke->hash_table[i];
497
498 while (!list_empty(hash_list)) {
Mingming Caoae6ddcc2006-09-27 01:49:27 -0700499 record = (struct jbd_revoke_record_s *)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700500 hash_list->next;
501 write_one_revoke_record(journal, transaction,
Mingming Caoae6ddcc2006-09-27 01:49:27 -0700502 &descriptor, &offset,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700503 record);
504 count++;
505 list_del(&record->hash);
506 kmem_cache_free(revoke_record_cache, record);
507 }
508 }
509 if (descriptor)
510 flush_descriptor(journal, descriptor, offset);
511 jbd_debug(1, "Wrote %d revoke records\n", count);
512}
513
Mingming Caoae6ddcc2006-09-27 01:49:27 -0700514/*
Linus Torvalds1da177e2005-04-16 15:20:36 -0700515 * Write out one revoke record. We need to create a new descriptor
Mingming Caoae6ddcc2006-09-27 01:49:27 -0700516 * block if the old one is full or if we have not already created one.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700517 */
518
Mingming Caoae6ddcc2006-09-27 01:49:27 -0700519static void write_one_revoke_record(journal_t *journal,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700520 transaction_t *transaction,
Mingming Caoae6ddcc2006-09-27 01:49:27 -0700521 struct journal_head **descriptorp,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700522 int *offsetp,
523 struct jbd_revoke_record_s *record)
524{
525 struct journal_head *descriptor;
526 int offset;
527 journal_header_t *header;
528
529 /* If we are already aborting, this all becomes a noop. We
530 still need to go round the loop in
531 journal_write_revoke_records in order to free all of the
532 revoke records: only the IO to the journal is omitted. */
533 if (is_journal_aborted(journal))
534 return;
535
536 descriptor = *descriptorp;
537 offset = *offsetp;
538
539 /* Make sure we have a descriptor with space left for the record */
540 if (descriptor) {
541 if (offset == journal->j_blocksize) {
542 flush_descriptor(journal, descriptor, offset);
543 descriptor = NULL;
544 }
545 }
546
547 if (!descriptor) {
548 descriptor = journal_get_descriptor_buffer(journal);
549 if (!descriptor)
550 return;
551 header = (journal_header_t *) &jh2bh(descriptor)->b_data[0];
552 header->h_magic = cpu_to_be32(JFS_MAGIC_NUMBER);
553 header->h_blocktype = cpu_to_be32(JFS_REVOKE_BLOCK);
554 header->h_sequence = cpu_to_be32(transaction->t_tid);
555
556 /* Record it so that we can wait for IO completion later */
557 JBUFFER_TRACE(descriptor, "file as BJ_LogCtl");
558 journal_file_buffer(descriptor, transaction, BJ_LogCtl);
559
560 offset = sizeof(journal_revoke_header_t);
561 *descriptorp = descriptor;
562 }
563
Mingming Caoae6ddcc2006-09-27 01:49:27 -0700564 * ((__be32 *)(&jh2bh(descriptor)->b_data[offset])) =
Linus Torvalds1da177e2005-04-16 15:20:36 -0700565 cpu_to_be32(record->blocknr);
566 offset += 4;
567 *offsetp = offset;
568}
569
Mingming Caoae6ddcc2006-09-27 01:49:27 -0700570/*
Linus Torvalds1da177e2005-04-16 15:20:36 -0700571 * Flush a revoke descriptor out to the journal. If we are aborting,
572 * this is a noop; otherwise we are generating a buffer which needs to
573 * be waited for during commit, so it has to go onto the appropriate
574 * journal buffer list.
575 */
576
Mingming Caoae6ddcc2006-09-27 01:49:27 -0700577static void flush_descriptor(journal_t *journal,
578 struct journal_head *descriptor,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700579 int offset)
580{
581 journal_revoke_header_t *header;
582 struct buffer_head *bh = jh2bh(descriptor);
583
584 if (is_journal_aborted(journal)) {
585 put_bh(bh);
586 return;
587 }
588
589 header = (journal_revoke_header_t *) jh2bh(descriptor)->b_data;
590 header->r_count = cpu_to_be32(offset);
591 set_buffer_jwrite(bh);
592 BUFFER_TRACE(bh, "write");
593 set_buffer_dirty(bh);
Jan Kara26707692005-09-06 15:19:12 -0700594 ll_rw_block(SWRITE, 1, &bh);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700595}
596#endif
597
Mingming Caoae6ddcc2006-09-27 01:49:27 -0700598/*
Linus Torvalds1da177e2005-04-16 15:20:36 -0700599 * Revoke support for recovery.
600 *
601 * Recovery needs to be able to:
602 *
603 * record all revoke records, including the tid of the latest instance
604 * of each revoke in the journal
605 *
606 * check whether a given block in a given transaction should be replayed
607 * (ie. has not been revoked by a revoke record in that or a subsequent
608 * transaction)
Mingming Caoae6ddcc2006-09-27 01:49:27 -0700609 *
Linus Torvalds1da177e2005-04-16 15:20:36 -0700610 * empty the revoke table after recovery.
611 */
612
613/*
614 * First, setting revoke records. We create a new revoke record for
615 * every block ever revoked in the log as we scan it for recovery, and
616 * we update the existing records if we find multiple revokes for a
Mingming Caoae6ddcc2006-09-27 01:49:27 -0700617 * single block.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700618 */
619
Mingming Caoae6ddcc2006-09-27 01:49:27 -0700620int journal_set_revoke(journal_t *journal,
621 unsigned long blocknr,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700622 tid_t sequence)
623{
624 struct jbd_revoke_record_s *record;
625
626 record = find_revoke_record(journal, blocknr);
627 if (record) {
628 /* If we have multiple occurrences, only record the
629 * latest sequence number in the hashed record */
630 if (tid_gt(sequence, record->sequence))
631 record->sequence = sequence;
632 return 0;
Mingming Caoae6ddcc2006-09-27 01:49:27 -0700633 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700634 return insert_revoke_hash(journal, blocknr, sequence);
635}
636
Mingming Caoae6ddcc2006-09-27 01:49:27 -0700637/*
Linus Torvalds1da177e2005-04-16 15:20:36 -0700638 * Test revoke records. For a given block referenced in the log, has
639 * that block been revoked? A revoke record with a given transaction
640 * sequence number revokes all blocks in that transaction and earlier
641 * ones, but later transactions still need replayed.
642 */
643
Mingming Caoae6ddcc2006-09-27 01:49:27 -0700644int journal_test_revoke(journal_t *journal,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700645 unsigned long blocknr,
646 tid_t sequence)
647{
648 struct jbd_revoke_record_s *record;
649
650 record = find_revoke_record(journal, blocknr);
651 if (!record)
652 return 0;
653 if (tid_gt(sequence, record->sequence))
654 return 0;
655 return 1;
656}
657
658/*
659 * Finally, once recovery is over, we need to clear the revoke table so
660 * that it can be reused by the running filesystem.
661 */
662
663void journal_clear_revoke(journal_t *journal)
664{
665 int i;
666 struct list_head *hash_list;
667 struct jbd_revoke_record_s *record;
668 struct jbd_revoke_table_s *revoke;
669
670 revoke = journal->j_revoke;
671
672 for (i = 0; i < revoke->hash_size; i++) {
673 hash_list = &revoke->hash_table[i];
674 while (!list_empty(hash_list)) {
675 record = (struct jbd_revoke_record_s*) hash_list->next;
676 list_del(&record->hash);
677 kmem_cache_free(revoke_record_cache, record);
678 }
679 }
680}