blob: c43e40e982096d1c46473815045f2a13a01346c2 [file] [log] [blame]
Theodore Ts'o0e8a9562000-12-09 06:41:25 +00001/*
2 * linux/fs/revoke.c
Theodore Ts'oefc6f622008-08-27 23:07:54 -04003 *
Theodore Ts'o0e8a9562000-12-09 06:41:25 +00004 * 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:
Theodore Ts'oefc6f622008-08-27 23:07:54 -040018 *
Theodore Ts'o0e8a9562000-12-09 06:41:25 +000019 * + Commit: during commit we write the entire list of the current
20 * transaction's revoked blocks to the journal
Theodore Ts'oefc6f622008-08-27 23:07:54 -040021 *
Theodore Ts'o0e8a9562000-12-09 06:41:25 +000022 * + 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:
Theodore Ts'oefc6f622008-08-27 23:07:54 -040032 * The desired end result is the journaling of the new block, so we
Theodore Ts'o0e8a9562000-12-09 06:41:25 +000033 * cancel the revoke before the transaction commits.
34 *
35 * Block is journaled and then revoked:
Theodore Ts'o725c4742001-06-08 11:55:44 +000036 * 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
Theodore Ts'o0e8a9562000-12-09 06:41:25 +000038 * later in the log than the log block. In this case, we choose the
Theodore Ts'o725c4742001-06-08 11:55:44 +000039 * 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.
Theodore Ts'o0e8a9562000-12-09 06:41:25 +000043 *
Theodore Ts'oefc6f622008-08-27 23:07:54 -040044 * Block is revoked and then written as data:
Theodore Ts'o0e8a9562000-12-09 06:41:25 +000045 * 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
Theodore Ts'o8cf93332001-12-16 02:23:36 -050053 * RevokeValid set, Revoked clear:
Theodore Ts'o0e8a9562000-12-09 06:41:25 +000054 * buffer has not been revoked, and cancel_revoke
55 * need do nothing.
Theodore Ts'o8cf93332001-12-16 02:23:36 -050056 * RevokeValid set, Revoked set:
Theodore Ts'oefc6f622008-08-27 23:07:54 -040057 * buffer has been revoked.
Theodore Ts'o0e8a9562000-12-09 06:41:25 +000058 */
59
60#ifndef __KERNEL__
61#include "jfs_user.h"
62#else
63#include <linux/sched.h>
64#include <linux/fs.h>
Theodore Ts'o8cf93332001-12-16 02:23:36 -050065#include <linux/jbd.h>
Theodore Ts'o0e8a9562000-12-09 06:41:25 +000066#include <linux/errno.h>
67#include <linux/slab.h>
68#include <linux/locks.h>
Theodore Ts'o0e8a9562000-12-09 06:41:25 +000069#include <linux/list.h>
Theodore Ts'o8cf93332001-12-16 02:23:36 -050070#include <linux/smp_lock.h>
71#include <linux/init.h>
Theodore Ts'o0e8a9562000-12-09 06:41:25 +000072#endif
73
Theodore Ts'o1911bf12008-07-13 08:04:36 -040074static lkmem_cache_t *revoke_record_cache;
75static lkmem_cache_t *revoke_table_cache;
Theodore Ts'o0e8a9562000-12-09 06:41:25 +000076
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. */
80
Theodore Ts'oefc6f622008-08-27 23:07:54 -040081struct jbd_revoke_record_s
Theodore Ts'o0e8a9562000-12-09 06:41:25 +000082{
83 struct list_head hash;
84 tid_t sequence; /* Used for recovery only */
Theodore Ts'oefc6f622008-08-27 23:07:54 -040085 unsigned long blocknr;
Theodore Ts'o0e8a9562000-12-09 06:41:25 +000086};
87
88
89/* The revoke table is just a simple hash table of revoke records. */
Theodore Ts'o8cf93332001-12-16 02:23:36 -050090struct jbd_revoke_table_s
Theodore Ts'o0e8a9562000-12-09 06:41:25 +000091{
92 /* It is conceivable that we might want a larger hash table
93 * for recovery. Must be a power of two. */
Theodore Ts'oefc6f622008-08-27 23:07:54 -040094 int hash_size;
95 int hash_shift;
Theodore Ts'o0e8a9562000-12-09 06:41:25 +000096 struct list_head *hash_table;
97};
98
99
100#ifdef __KERNEL__
101static void write_one_revoke_record(journal_t *, transaction_t *,
Theodore Ts'o8cf93332001-12-16 02:23:36 -0500102 struct journal_head **, int *,
103 struct jbd_revoke_record_s *);
104static void flush_descriptor(journal_t *, struct journal_head *, int);
Theodore Ts'o0e8a9562000-12-09 06:41:25 +0000105#endif
106
107/* Utility functions to maintain the revoke table */
108
109/* Borrowed from buffer.c: this is a tried and tested block hash function */
110static inline int hash(journal_t *journal, unsigned long block)
111{
Theodore Ts'o8cf93332001-12-16 02:23:36 -0500112 struct jbd_revoke_table_s *table = journal->j_revoke;
Theodore Ts'o0e8a9562000-12-09 06:41:25 +0000113 int hash_shift = table->hash_shift;
Theodore Ts'oefc6f622008-08-27 23:07:54 -0400114
Theodore Ts'o0e8a9562000-12-09 06:41:25 +0000115 return ((block << (hash_shift - 6)) ^
116 (block >> 13) ^
117 (block << (hash_shift - 12))) & (table->hash_size - 1);
118}
119
Theodore Ts'o3e699062002-10-13 23:56:28 -0400120static int insert_revoke_hash(journal_t *journal, unsigned long blocknr,
121 tid_t seq)
Theodore Ts'o0e8a9562000-12-09 06:41:25 +0000122{
123 struct list_head *hash_list;
Theodore Ts'o8cf93332001-12-16 02:23:36 -0500124 struct jbd_revoke_record_s *record;
125
Theodore Ts'o546a1ff2002-03-07 23:52:56 -0500126#ifdef __KERNEL__
Theodore Ts'o8cf93332001-12-16 02:23:36 -0500127repeat:
Theodore Ts'o546a1ff2002-03-07 23:52:56 -0500128#endif
Theodore Ts'o8cf93332001-12-16 02:23:36 -0500129 record = kmem_cache_alloc(revoke_record_cache, GFP_NOFS);
Theodore Ts'o0e8a9562000-12-09 06:41:25 +0000130 if (!record)
Theodore Ts'o8cf93332001-12-16 02:23:36 -0500131 goto oom;
Theodore Ts'o0e8a9562000-12-09 06:41:25 +0000132
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);
137 return 0;
Theodore Ts'o8cf93332001-12-16 02:23:36 -0500138
139oom:
140#ifdef __KERNEL__
141 if (!journal_oom_retry)
142 return -ENOMEM;
143 jbd_debug(1, "ENOMEM in " __FUNCTION__ ", retrying.\n");
144 current->policy |= SCHED_YIELD;
145 schedule();
146 goto repeat;
147#else
148 return -ENOMEM;
149#endif
Theodore Ts'o0e8a9562000-12-09 06:41:25 +0000150}
151
152/* Find a revoke record in the journal's hash table. */
153
Theodore Ts'o8cf93332001-12-16 02:23:36 -0500154static struct jbd_revoke_record_s *find_revoke_record(journal_t *journal,
Theodore Ts'o0e8a9562000-12-09 06:41:25 +0000155 unsigned long blocknr)
156{
157 struct list_head *hash_list;
Theodore Ts'o8cf93332001-12-16 02:23:36 -0500158 struct jbd_revoke_record_s *record;
Theodore Ts'oefc6f622008-08-27 23:07:54 -0400159
Theodore Ts'o0e8a9562000-12-09 06:41:25 +0000160 hash_list = &journal->j_revoke->hash_table[hash(journal, blocknr)];
161
Theodore Ts'o8cf93332001-12-16 02:23:36 -0500162 record = (struct jbd_revoke_record_s *) hash_list->next;
Theodore Ts'o0e8a9562000-12-09 06:41:25 +0000163 while (&(record->hash) != hash_list) {
164 if (record->blocknr == blocknr)
165 return record;
Theodore Ts'o8cf93332001-12-16 02:23:36 -0500166 record = (struct jbd_revoke_record_s *) record->hash.next;
Theodore Ts'o0e8a9562000-12-09 06:41:25 +0000167 }
168 return NULL;
169}
170
Theodore Ts'o8cf93332001-12-16 02:23:36 -0500171int __init journal_init_revoke_caches(void)
172{
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)
177 return -ENOMEM;
Theodore Ts'o0e8a9562000-12-09 06:41:25 +0000178
Theodore Ts'o8cf93332001-12-16 02:23:36 -0500179 revoke_table_cache = kmem_cache_create("revoke_table",
180 sizeof(struct jbd_revoke_table_s),
181 0, 0, NULL, NULL);
182 if (revoke_table_cache == 0) {
183 kmem_cache_destroy(revoke_record_cache);
184 revoke_record_cache = NULL;
185 return -ENOMEM;
186 }
187 return 0;
Theodore Ts'oefc6f622008-08-27 23:07:54 -0400188}
Theodore Ts'o8cf93332001-12-16 02:23:36 -0500189
190void journal_destroy_revoke_caches(void)
191{
192 kmem_cache_destroy(revoke_record_cache);
193 revoke_record_cache = 0;
194 kmem_cache_destroy(revoke_table_cache);
195 revoke_table_cache = 0;
196}
Theodore Ts'o0e8a9562000-12-09 06:41:25 +0000197
198/* Initialise the revoke table for a given journal to a given size. */
199
200int journal_init_revoke(journal_t *journal, int hash_size)
201{
202 int shift, tmp;
Theodore Ts'oefc6f622008-08-27 23:07:54 -0400203
Theodore Ts'o0e8a9562000-12-09 06:41:25 +0000204 J_ASSERT (journal->j_revoke == NULL);
Theodore Ts'oefc6f622008-08-27 23:07:54 -0400205
Theodore Ts'o0e8a9562000-12-09 06:41:25 +0000206 journal->j_revoke = kmem_cache_alloc(revoke_table_cache, GFP_KERNEL);
207 if (!journal->j_revoke)
208 return -ENOMEM;
Theodore Ts'oefc6f622008-08-27 23:07:54 -0400209
Theodore Ts'o0e8a9562000-12-09 06:41:25 +0000210 /* Check that the hash_size is a power of two */
211 J_ASSERT ((hash_size & (hash_size-1)) == 0);
212
213 journal->j_revoke->hash_size = hash_size;
214
215 shift = 0;
216 tmp = hash_size;
217 while((tmp >>= 1UL) != 0UL)
218 shift++;
219 journal->j_revoke->hash_shift = shift;
220
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;
226 return -ENOMEM;
227 }
Theodore Ts'oefc6f622008-08-27 23:07:54 -0400228
Theodore Ts'o0e8a9562000-12-09 06:41:25 +0000229 for (tmp = 0; tmp < hash_size; tmp++)
230 INIT_LIST_HEAD(&journal->j_revoke->hash_table[tmp]);
Theodore Ts'oefc6f622008-08-27 23:07:54 -0400231
Theodore Ts'o0e8a9562000-12-09 06:41:25 +0000232 return 0;
233}
234
235/* Destoy a journal's revoke table. The table must already be empty! */
236
237void journal_destroy_revoke(journal_t *journal)
238{
Theodore Ts'o8cf93332001-12-16 02:23:36 -0500239 struct jbd_revoke_table_s *table;
Theodore Ts'o0e8a9562000-12-09 06:41:25 +0000240 struct list_head *hash_list;
241 int i;
Theodore Ts'oefc6f622008-08-27 23:07:54 -0400242
Theodore Ts'o0e8a9562000-12-09 06:41:25 +0000243 table = journal->j_revoke;
244 if (!table)
245 return;
Theodore Ts'oefc6f622008-08-27 23:07:54 -0400246
Theodore Ts'o0e8a9562000-12-09 06:41:25 +0000247 for (i=0; i<table->hash_size; i++) {
248 hash_list = &table->hash_table[i];
249 J_ASSERT (list_empty(hash_list));
250 }
Theodore Ts'oefc6f622008-08-27 23:07:54 -0400251
Theodore Ts'o0e8a9562000-12-09 06:41:25 +0000252 kfree(table->hash_table);
253 kmem_cache_free(revoke_table_cache, table);
254 journal->j_revoke = NULL;
255}
256
257
258#ifdef __KERNEL__
259
Theodore Ts'oefc6f622008-08-27 23:07:54 -0400260/*
Theodore Ts'o0e8a9562000-12-09 06:41:25 +0000261 * 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
Theodore Ts'oefc6f622008-08-27 23:07:54 -0400265 * revoke.
Theodore Ts'o0e8a9562000-12-09 06:41:25 +0000266 *
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
Theodore Ts'oefc6f622008-08-27 23:07:54 -0400271 * metadata.
Theodore Ts'o0e8a9562000-12-09 06:41:25 +0000272 *
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
Theodore Ts'oefc6f622008-08-27 23:07:54 -0400275 * found implicitly.
Theodore Ts'o0e8a9562000-12-09 06:41:25 +0000276 *
Theodore Ts'o8cf93332001-12-16 02:23:36 -0500277 * bh_in may not be a journalled buffer - it may have come off
278 * the hash tables without an attached journal_head.
279 *
280 * If bh_in is non-zero, journal_revoke() will decrement its b_count
281 * by one.
Theodore Ts'o0e8a9562000-12-09 06:41:25 +0000282 */
283
Theodore Ts'oefc6f622008-08-27 23:07:54 -0400284int journal_revoke(handle_t *handle, unsigned long blocknr,
Theodore Ts'o0e8a9562000-12-09 06:41:25 +0000285 struct buffer_head *bh_in)
286{
Theodore Ts'o8cf93332001-12-16 02:23:36 -0500287 struct buffer_head *bh = NULL;
Theodore Ts'o0e8a9562000-12-09 06:41:25 +0000288 journal_t *journal;
289 kdev_t dev;
290 int err;
291
Theodore Ts'o8cf93332001-12-16 02:23:36 -0500292 if (bh_in)
293 BUFFER_TRACE(bh_in, "enter");
294
Theodore Ts'o0e8a9562000-12-09 06:41:25 +0000295 journal = handle->h_transaction->t_journal;
Theodore Ts'o1f735032001-03-29 19:00:50 +0000296 if (!journal_set_features(journal, 0, 0, JFS_FEATURE_INCOMPAT_REVOKE)){
297 J_ASSERT (!"Cannot set revoke feature!");
Theodore Ts'o0e8a9562000-12-09 06:41:25 +0000298 return -EINVAL;
Theodore Ts'o1f735032001-03-29 19:00:50 +0000299 }
Theodore Ts'o8cf93332001-12-16 02:23:36 -0500300
301 dev = journal->j_fs_dev;
Theodore Ts'o0e8a9562000-12-09 06:41:25 +0000302 bh = bh_in;
303
Theodore Ts'o8cf93332001-12-16 02:23:36 -0500304 if (!bh) {
Theodore Ts'o0e8a9562000-12-09 06:41:25 +0000305 bh = get_hash_table(dev, blocknr, journal->j_blocksize);
Theodore Ts'o8cf93332001-12-16 02:23:36 -0500306 if (bh)
307 BUFFER_TRACE(bh, "found on hash");
308 }
309#ifdef JBD_EXPENSIVE_CHECKING
310 else {
311 struct buffer_head *bh2;
312
313 /* If there is a different buffer_head lying around in
314 * memory anywhere... */
315 bh2 = get_hash_table(dev, blocknr, journal->j_blocksize);
316 if (bh2) {
317 /* ... and it has RevokeValid status... */
318 if ((bh2 != bh) &&
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
325 * cancel. */
326 J_ASSERT_BH(bh2, test_bit(BH_Revoked, &
327 bh2->b_state));
328 __brelse(bh2);
329 }
330 }
331#endif
Theodore Ts'o0e8a9562000-12-09 06:41:25 +0000332
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! */
336 if (bh) {
Theodore Ts'o8cf93332001-12-16 02:23:36 -0500337 J_ASSERT_BH(bh, !test_bit(BH_Revoked, &bh->b_state));
338 set_bit(BH_Revoked, &bh->b_state);
Theodore Ts'o0e8a9562000-12-09 06:41:25 +0000339 set_bit(BH_RevokeValid, &bh->b_state);
Theodore Ts'o8cf93332001-12-16 02:23:36 -0500340 if (bh_in) {
341 BUFFER_TRACE(bh_in, "call journal_forget");
Theodore Ts'o0e8a9562000-12-09 06:41:25 +0000342 journal_forget(handle, bh_in);
Theodore Ts'o8cf93332001-12-16 02:23:36 -0500343 } else {
344 BUFFER_TRACE(bh, "call brelse");
345 __brelse(bh);
346 }
Theodore Ts'o0e8a9562000-12-09 06:41:25 +0000347 }
348
349 lock_journal(journal);
Theodore Ts'o8cf93332001-12-16 02:23:36 -0500350 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);
Theodore Ts'o0e8a9562000-12-09 06:41:25 +0000353 unlock_journal(journal);
Theodore Ts'o8cf93332001-12-16 02:23:36 -0500354 BUFFER_TRACE(bh_in, "exit");
Theodore Ts'o0e8a9562000-12-09 06:41:25 +0000355 return err;
356}
357
Theodore Ts'o0e8a9562000-12-09 06:41:25 +0000358/*
359 * Cancel an outstanding revoke. For use only internally by the
360 * journaling code (called from journal_get_write_access).
361 *
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.
365 *
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
371 * set.
372 *
373 * The caller must have the journal locked.
Theodore Ts'o8cf93332001-12-16 02:23:36 -0500374 */
375int journal_cancel_revoke(handle_t *handle, struct journal_head *jh)
Theodore Ts'o0e8a9562000-12-09 06:41:25 +0000376{
Theodore Ts'o8cf93332001-12-16 02:23:36 -0500377 struct jbd_revoke_record_s *record;
Theodore Ts'o0e8a9562000-12-09 06:41:25 +0000378 journal_t *journal = handle->h_transaction->t_journal;
379 int need_cancel;
Theodore Ts'o8cf93332001-12-16 02:23:36 -0500380 int did_revoke = 0; /* akpm: debug */
381 struct buffer_head *bh = jh2bh(jh);
Theodore Ts'oefc6f622008-08-27 23:07:54 -0400382
Theodore Ts'o8cf93332001-12-16 02:23:36 -0500383 jbd_debug(4, "journal_head %p, cancelling revoke\n", jh);
384
Theodore Ts'o0e8a9562000-12-09 06:41:25 +0000385 /* 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));
391 else {
392 need_cancel = 1;
393 clear_bit(BH_Revoked, &bh->b_state);
394 }
Theodore Ts'o8cf93332001-12-16 02:23:36 -0500395
Theodore Ts'o0e8a9562000-12-09 06:41:25 +0000396 if (need_cancel) {
397 record = find_revoke_record(journal, bh->b_blocknr);
398 if (record) {
Theodore Ts'o8cf93332001-12-16 02:23:36 -0500399 jbd_debug(4, "cancelled existing revoke on "
400 "blocknr %lu\n", bh->b_blocknr);
Theodore Ts'o0e8a9562000-12-09 06:41:25 +0000401 list_del(&record->hash);
402 kmem_cache_free(revoke_record_cache, record);
Theodore Ts'o8cf93332001-12-16 02:23:36 -0500403 did_revoke = 1;
Theodore Ts'o0e8a9562000-12-09 06:41:25 +0000404 }
405 }
Theodore Ts'o8cf93332001-12-16 02:23:36 -0500406
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);
411#endif
412
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);
420 if (bh2) {
421 clear_bit(BH_Revoked, &bh2->b_state);
422 __brelse(bh2);
423 }
424 }
Theodore Ts'oefc6f622008-08-27 23:07:54 -0400425
Theodore Ts'o8cf93332001-12-16 02:23:36 -0500426 return did_revoke;
Theodore Ts'o0e8a9562000-12-09 06:41:25 +0000427}
428
429
430/*
431 * Write revoke records to the journal for all entries in the current
432 * revoke hash, deleting the entries as we go.
433 *
434 * Called with the journal lock held.
435 */
436
Theodore Ts'oefc6f622008-08-27 23:07:54 -0400437void journal_write_revoke_records(journal_t *journal,
Theodore Ts'o0e8a9562000-12-09 06:41:25 +0000438 transaction_t *transaction)
439{
Theodore Ts'o8cf93332001-12-16 02:23:36 -0500440 struct journal_head *descriptor;
441 struct jbd_revoke_record_s *record;
442 struct jbd_revoke_table_s *revoke;
Theodore Ts'o0e8a9562000-12-09 06:41:25 +0000443 struct list_head *hash_list;
Theodore Ts'o1f735032001-03-29 19:00:50 +0000444 int i, offset, count;
Theodore Ts'o8cf93332001-12-16 02:23:36 -0500445
Theodore Ts'oefc6f622008-08-27 23:07:54 -0400446 descriptor = NULL;
Theodore Ts'o0e8a9562000-12-09 06:41:25 +0000447 offset = 0;
Theodore Ts'o1f735032001-03-29 19:00:50 +0000448 count = 0;
Theodore Ts'o0e8a9562000-12-09 06:41:25 +0000449 revoke = journal->j_revoke;
Theodore Ts'oefc6f622008-08-27 23:07:54 -0400450
Theodore Ts'o0e8a9562000-12-09 06:41:25 +0000451 for (i = 0; i < revoke->hash_size; i++) {
452 hash_list = &revoke->hash_table[i];
453
454 while (!list_empty(hash_list)) {
Theodore Ts'oefc6f622008-08-27 23:07:54 -0400455 record = (struct jbd_revoke_record_s *)
Theodore Ts'o0e8a9562000-12-09 06:41:25 +0000456 hash_list->next;
457 write_one_revoke_record(journal, transaction,
Theodore Ts'oefc6f622008-08-27 23:07:54 -0400458 &descriptor, &offset,
Theodore Ts'o0e8a9562000-12-09 06:41:25 +0000459 record);
Theodore Ts'o1f735032001-03-29 19:00:50 +0000460 count++;
Theodore Ts'o0e8a9562000-12-09 06:41:25 +0000461 list_del(&record->hash);
462 kmem_cache_free(revoke_record_cache, record);
463 }
464 }
Theodore Ts'oefc6f622008-08-27 23:07:54 -0400465 if (descriptor)
Theodore Ts'o0e8a9562000-12-09 06:41:25 +0000466 flush_descriptor(journal, descriptor, offset);
Theodore Ts'o8cf93332001-12-16 02:23:36 -0500467 jbd_debug(1, "Wrote %d revoke records\n", count);
Theodore Ts'o0e8a9562000-12-09 06:41:25 +0000468}
469
Theodore Ts'oefc6f622008-08-27 23:07:54 -0400470/*
Theodore Ts'o0e8a9562000-12-09 06:41:25 +0000471 * Write out one revoke record. We need to create a new descriptor
Theodore Ts'oefc6f622008-08-27 23:07:54 -0400472 * block if the old one is full or if we have not already created one.
Theodore Ts'o0e8a9562000-12-09 06:41:25 +0000473 */
474
Theodore Ts'oefc6f622008-08-27 23:07:54 -0400475static void write_one_revoke_record(journal_t *journal,
Theodore Ts'o0e8a9562000-12-09 06:41:25 +0000476 transaction_t *transaction,
Theodore Ts'oefc6f622008-08-27 23:07:54 -0400477 struct journal_head **descriptorp,
Theodore Ts'o0e8a9562000-12-09 06:41:25 +0000478 int *offsetp,
Theodore Ts'o8cf93332001-12-16 02:23:36 -0500479 struct jbd_revoke_record_s *record)
Theodore Ts'o0e8a9562000-12-09 06:41:25 +0000480{
Theodore Ts'o8cf93332001-12-16 02:23:36 -0500481 struct journal_head *descriptor;
Theodore Ts'o0e8a9562000-12-09 06:41:25 +0000482 int offset;
483 journal_header_t *header;
Theodore Ts'o8cf93332001-12-16 02:23:36 -0500484
Theodore Ts'o0e8a9562000-12-09 06:41:25 +0000485 /* 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. */
Theodore Ts'o8cf93332001-12-16 02:23:36 -0500489 if (is_journal_aborted(journal))
Theodore Ts'o0e8a9562000-12-09 06:41:25 +0000490 return;
491
492 descriptor = *descriptorp;
493 offset = *offsetp;
Theodore Ts'o8cf93332001-12-16 02:23:36 -0500494
Theodore Ts'o0e8a9562000-12-09 06:41:25 +0000495 /* Make sure we have a descriptor with space left for the record */
496 if (descriptor) {
497 if (offset == journal->j_blocksize) {
498 flush_descriptor(journal, descriptor, offset);
499 descriptor = NULL;
500 }
501 }
Theodore Ts'oefc6f622008-08-27 23:07:54 -0400502
Theodore Ts'o0e8a9562000-12-09 06:41:25 +0000503 if (!descriptor) {
504 descriptor = journal_get_descriptor_buffer(journal);
Theodore Ts'o8cf93332001-12-16 02:23:36 -0500505 if (!descriptor)
506 return;
507 header = (journal_header_t *) &jh2bh(descriptor)->b_data[0];
Theodore Ts'o0e8a9562000-12-09 06:41:25 +0000508 header->h_magic = htonl(JFS_MAGIC_NUMBER);
509 header->h_blocktype = htonl(JFS_REVOKE_BLOCK);
510 header->h_sequence = htonl(transaction->t_tid);
511
512 /* Record it so that we can wait for IO completion later */
Theodore Ts'o8cf93332001-12-16 02:23:36 -0500513 JBUFFER_TRACE(descriptor, "file as BJ_LogCtl");
Theodore Ts'o0e8a9562000-12-09 06:41:25 +0000514 journal_file_buffer(descriptor, transaction, BJ_LogCtl);
Theodore Ts'o8cf93332001-12-16 02:23:36 -0500515
Theodore Ts'o0e8a9562000-12-09 06:41:25 +0000516 offset = sizeof(journal_revoke_header_t);
517 *descriptorp = descriptor;
518 }
Theodore Ts'oefc6f622008-08-27 23:07:54 -0400519
520 * ((unsigned int *)(&jh2bh(descriptor)->b_data[offset])) =
Theodore Ts'o0e8a9562000-12-09 06:41:25 +0000521 htonl(record->blocknr);
522 offset += 4;
523 *offsetp = offset;
524}
525
Theodore Ts'oefc6f622008-08-27 23:07:54 -0400526/*
Theodore Ts'o0e8a9562000-12-09 06:41:25 +0000527 * 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.
531 */
532
Theodore Ts'oefc6f622008-08-27 23:07:54 -0400533static void flush_descriptor(journal_t *journal,
534 struct journal_head *descriptor,
Theodore Ts'o0e8a9562000-12-09 06:41:25 +0000535 int offset)
536{
537 journal_revoke_header_t *header;
Theodore Ts'o8cf93332001-12-16 02:23:36 -0500538
539 if (is_journal_aborted(journal)) {
540 JBUFFER_TRACE(descriptor, "brelse");
541 __brelse(jh2bh(descriptor));
Theodore Ts'o0e8a9562000-12-09 06:41:25 +0000542 return;
543 }
Theodore Ts'oefc6f622008-08-27 23:07:54 -0400544
Theodore Ts'o8cf93332001-12-16 02:23:36 -0500545 header = (journal_revoke_header_t *) jh2bh(descriptor)->b_data;
Theodore Ts'o0e8a9562000-12-09 06:41:25 +0000546 header->r_count = htonl(offset);
Theodore Ts'o8cf93332001-12-16 02:23:36 -0500547 set_bit(BH_JWrite, &jh2bh(descriptor)->b_state);
548 {
549 struct buffer_head *bh = jh2bh(descriptor);
550 BUFFER_TRACE(bh, "write");
551 ll_rw_block (WRITE, 1, &bh);
552 }
Theodore Ts'o0e8a9562000-12-09 06:41:25 +0000553}
554
555#endif
556
Theodore Ts'oefc6f622008-08-27 23:07:54 -0400557/*
Theodore Ts'o0e8a9562000-12-09 06:41:25 +0000558 * Revoke support for recovery.
559 *
560 * Recovery needs to be able to:
561 *
562 * record all revoke records, including the tid of the latest instance
563 * of each revoke in the journal
564 *
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
567 * transaction)
Theodore Ts'oefc6f622008-08-27 23:07:54 -0400568 *
Theodore Ts'o0e8a9562000-12-09 06:41:25 +0000569 * empty the revoke table after recovery.
570 */
571
572/*
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
Theodore Ts'oefc6f622008-08-27 23:07:54 -0400576 * single block.
Theodore Ts'o0e8a9562000-12-09 06:41:25 +0000577 */
578
Theodore Ts'oefc6f622008-08-27 23:07:54 -0400579int journal_set_revoke(journal_t *journal,
580 unsigned long blocknr,
Theodore Ts'o0e8a9562000-12-09 06:41:25 +0000581 tid_t sequence)
582{
Theodore Ts'o8cf93332001-12-16 02:23:36 -0500583 struct jbd_revoke_record_s *record;
Theodore Ts'oefc6f622008-08-27 23:07:54 -0400584
Theodore Ts'o0e8a9562000-12-09 06:41:25 +0000585 record = find_revoke_record(journal, blocknr);
586 if (record) {
587 /* If we have multiple occurences, only record the
588 * latest sequence number in the hashed record */
Theodore Ts'o725c4742001-06-08 11:55:44 +0000589 if (tid_gt(sequence, record->sequence))
Theodore Ts'o0e8a9562000-12-09 06:41:25 +0000590 record->sequence = sequence;
591 return 0;
Theodore Ts'oefc6f622008-08-27 23:07:54 -0400592 }
Theodore Ts'o0e8a9562000-12-09 06:41:25 +0000593 return insert_revoke_hash(journal, blocknr, sequence);
594}
595
Theodore Ts'oefc6f622008-08-27 23:07:54 -0400596/*
Theodore Ts'o0e8a9562000-12-09 06:41:25 +0000597 * 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.
601 */
602
Theodore Ts'oefc6f622008-08-27 23:07:54 -0400603int journal_test_revoke(journal_t *journal,
Theodore Ts'o0e8a9562000-12-09 06:41:25 +0000604 unsigned long blocknr,
605 tid_t sequence)
606{
Theodore Ts'o8cf93332001-12-16 02:23:36 -0500607 struct jbd_revoke_record_s *record;
Theodore Ts'oefc6f622008-08-27 23:07:54 -0400608
Theodore Ts'o0e8a9562000-12-09 06:41:25 +0000609 record = find_revoke_record(journal, blocknr);
610 if (!record)
611 return 0;
Theodore Ts'o725c4742001-06-08 11:55:44 +0000612 if (tid_gt(sequence, record->sequence))
Theodore Ts'o0e8a9562000-12-09 06:41:25 +0000613 return 0;
614 return 1;
615}
616
617/*
618 * Finally, once recovery is over, we need to clear the revoke table so
619 * that it can be reused by the running filesystem.
620 */
621
622void journal_clear_revoke(journal_t *journal)
623{
624 int i;
625 struct list_head *hash_list;
Theodore Ts'o8cf93332001-12-16 02:23:36 -0500626 struct jbd_revoke_record_s *record;
627 struct jbd_revoke_table_s *revoke;
Theodore Ts'oefc6f622008-08-27 23:07:54 -0400628
Theodore Ts'o0e8a9562000-12-09 06:41:25 +0000629 revoke = journal->j_revoke;
Theodore Ts'oefc6f622008-08-27 23:07:54 -0400630
Theodore Ts'o0e8a9562000-12-09 06:41:25 +0000631 for (i = 0; i < revoke->hash_size; i++) {
632 hash_list = &revoke->hash_table[i];
633 while (!list_empty(hash_list)) {
Theodore Ts'o8cf93332001-12-16 02:23:36 -0500634 record = (struct jbd_revoke_record_s*) hash_list->next;
Theodore Ts'o0e8a9562000-12-09 06:41:25 +0000635 list_del(&record->hash);
636 kmem_cache_free(revoke_record_cache, record);
637 }
638 }
639}
640