blob: 38c265e43d1a0511b6fa56bb20b5b0d402c06945 [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__
Theodore Ts'od1154eb2011-09-18 17:34:37 -040061#include "config.h"
Theodore Ts'o0e8a9562000-12-09 06:41:25 +000062#include "jfs_user.h"
63#else
64#include <linux/sched.h>
65#include <linux/fs.h>
Theodore Ts'o8cf93332001-12-16 02:23:36 -050066#include <linux/jbd.h>
Theodore Ts'o0e8a9562000-12-09 06:41:25 +000067#include <linux/errno.h>
68#include <linux/slab.h>
69#include <linux/locks.h>
Theodore Ts'o0e8a9562000-12-09 06:41:25 +000070#include <linux/list.h>
Theodore Ts'o8cf93332001-12-16 02:23:36 -050071#include <linux/smp_lock.h>
72#include <linux/init.h>
Theodore Ts'o0e8a9562000-12-09 06:41:25 +000073#endif
74
Theodore Ts'o1911bf12008-07-13 08:04:36 -040075static lkmem_cache_t *revoke_record_cache;
76static lkmem_cache_t *revoke_table_cache;
Theodore Ts'o0e8a9562000-12-09 06:41:25 +000077
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. */
81
Theodore Ts'oefc6f622008-08-27 23:07:54 -040082struct jbd_revoke_record_s
Theodore Ts'o0e8a9562000-12-09 06:41:25 +000083{
84 struct list_head hash;
85 tid_t sequence; /* Used for recovery only */
Theodore Ts'oefc6f622008-08-27 23:07:54 -040086 unsigned long blocknr;
Theodore Ts'o0e8a9562000-12-09 06:41:25 +000087};
88
89
90/* The revoke table is just a simple hash table of revoke records. */
Theodore Ts'o8cf93332001-12-16 02:23:36 -050091struct jbd_revoke_table_s
Theodore Ts'o0e8a9562000-12-09 06:41:25 +000092{
93 /* It is conceivable that we might want a larger hash table
94 * for recovery. Must be a power of two. */
Theodore Ts'oefc6f622008-08-27 23:07:54 -040095 int hash_size;
96 int hash_shift;
Theodore Ts'o0e8a9562000-12-09 06:41:25 +000097 struct list_head *hash_table;
98};
99
100
101#ifdef __KERNEL__
102static void write_one_revoke_record(journal_t *, transaction_t *,
Theodore Ts'o8cf93332001-12-16 02:23:36 -0500103 struct journal_head **, int *,
104 struct jbd_revoke_record_s *);
105static void flush_descriptor(journal_t *, struct journal_head *, int);
Theodore Ts'o0e8a9562000-12-09 06:41:25 +0000106#endif
107
108/* Utility functions to maintain the revoke table */
109
110/* Borrowed from buffer.c: this is a tried and tested block hash function */
111static inline int hash(journal_t *journal, unsigned long block)
112{
Theodore Ts'o8cf93332001-12-16 02:23:36 -0500113 struct jbd_revoke_table_s *table = journal->j_revoke;
Theodore Ts'o0e8a9562000-12-09 06:41:25 +0000114 int hash_shift = table->hash_shift;
Theodore Ts'oefc6f622008-08-27 23:07:54 -0400115
Theodore Ts'o0e8a9562000-12-09 06:41:25 +0000116 return ((block << (hash_shift - 6)) ^
117 (block >> 13) ^
118 (block << (hash_shift - 12))) & (table->hash_size - 1);
119}
120
Theodore Ts'o3e699062002-10-13 23:56:28 -0400121static int insert_revoke_hash(journal_t *journal, unsigned long blocknr,
122 tid_t seq)
Theodore Ts'o0e8a9562000-12-09 06:41:25 +0000123{
124 struct list_head *hash_list;
Theodore Ts'o8cf93332001-12-16 02:23:36 -0500125 struct jbd_revoke_record_s *record;
126
Theodore Ts'o546a1ff2002-03-07 23:52:56 -0500127#ifdef __KERNEL__
Theodore Ts'o8cf93332001-12-16 02:23:36 -0500128repeat:
Theodore Ts'o546a1ff2002-03-07 23:52:56 -0500129#endif
Theodore Ts'o8cf93332001-12-16 02:23:36 -0500130 record = kmem_cache_alloc(revoke_record_cache, GFP_NOFS);
Theodore Ts'o0e8a9562000-12-09 06:41:25 +0000131 if (!record)
Theodore Ts'o8cf93332001-12-16 02:23:36 -0500132 goto oom;
Theodore Ts'o0e8a9562000-12-09 06:41:25 +0000133
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);
138 return 0;
Theodore Ts'o8cf93332001-12-16 02:23:36 -0500139
140oom:
141#ifdef __KERNEL__
142 if (!journal_oom_retry)
143 return -ENOMEM;
144 jbd_debug(1, "ENOMEM in " __FUNCTION__ ", retrying.\n");
145 current->policy |= SCHED_YIELD;
146 schedule();
147 goto repeat;
148#else
149 return -ENOMEM;
150#endif
Theodore Ts'o0e8a9562000-12-09 06:41:25 +0000151}
152
153/* Find a revoke record in the journal's hash table. */
154
Theodore Ts'o8cf93332001-12-16 02:23:36 -0500155static struct jbd_revoke_record_s *find_revoke_record(journal_t *journal,
Theodore Ts'o0e8a9562000-12-09 06:41:25 +0000156 unsigned long blocknr)
157{
158 struct list_head *hash_list;
Theodore Ts'o8cf93332001-12-16 02:23:36 -0500159 struct jbd_revoke_record_s *record;
Theodore Ts'oefc6f622008-08-27 23:07:54 -0400160
Theodore Ts'o0e8a9562000-12-09 06:41:25 +0000161 hash_list = &journal->j_revoke->hash_table[hash(journal, blocknr)];
162
Theodore Ts'o8cf93332001-12-16 02:23:36 -0500163 record = (struct jbd_revoke_record_s *) hash_list->next;
Theodore Ts'o0e8a9562000-12-09 06:41:25 +0000164 while (&(record->hash) != hash_list) {
165 if (record->blocknr == blocknr)
166 return record;
Theodore Ts'o8cf93332001-12-16 02:23:36 -0500167 record = (struct jbd_revoke_record_s *) record->hash.next;
Theodore Ts'o0e8a9562000-12-09 06:41:25 +0000168 }
169 return NULL;
170}
171
Theodore Ts'o8cf93332001-12-16 02:23:36 -0500172int __init journal_init_revoke_caches(void)
173{
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)
178 return -ENOMEM;
Theodore Ts'o0e8a9562000-12-09 06:41:25 +0000179
Theodore Ts'o8cf93332001-12-16 02:23:36 -0500180 revoke_table_cache = kmem_cache_create("revoke_table",
181 sizeof(struct jbd_revoke_table_s),
182 0, 0, NULL, NULL);
183 if (revoke_table_cache == 0) {
184 kmem_cache_destroy(revoke_record_cache);
185 revoke_record_cache = NULL;
186 return -ENOMEM;
187 }
188 return 0;
Theodore Ts'oefc6f622008-08-27 23:07:54 -0400189}
Theodore Ts'o8cf93332001-12-16 02:23:36 -0500190
191void journal_destroy_revoke_caches(void)
192{
193 kmem_cache_destroy(revoke_record_cache);
194 revoke_record_cache = 0;
195 kmem_cache_destroy(revoke_table_cache);
196 revoke_table_cache = 0;
197}
Theodore Ts'o0e8a9562000-12-09 06:41:25 +0000198
199/* Initialise the revoke table for a given journal to a given size. */
200
201int journal_init_revoke(journal_t *journal, int hash_size)
202{
203 int shift, tmp;
Theodore Ts'oefc6f622008-08-27 23:07:54 -0400204
Theodore Ts'o0e8a9562000-12-09 06:41:25 +0000205 J_ASSERT (journal->j_revoke == NULL);
Theodore Ts'oefc6f622008-08-27 23:07:54 -0400206
Theodore Ts'o0e8a9562000-12-09 06:41:25 +0000207 journal->j_revoke = kmem_cache_alloc(revoke_table_cache, GFP_KERNEL);
208 if (!journal->j_revoke)
209 return -ENOMEM;
Theodore Ts'oefc6f622008-08-27 23:07:54 -0400210
Theodore Ts'o0e8a9562000-12-09 06:41:25 +0000211 /* Check that the hash_size is a power of two */
212 J_ASSERT ((hash_size & (hash_size-1)) == 0);
213
214 journal->j_revoke->hash_size = hash_size;
215
216 shift = 0;
217 tmp = hash_size;
218 while((tmp >>= 1UL) != 0UL)
219 shift++;
220 journal->j_revoke->hash_shift = shift;
221
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;
227 return -ENOMEM;
228 }
Theodore Ts'oefc6f622008-08-27 23:07:54 -0400229
Theodore Ts'o0e8a9562000-12-09 06:41:25 +0000230 for (tmp = 0; tmp < hash_size; tmp++)
231 INIT_LIST_HEAD(&journal->j_revoke->hash_table[tmp]);
Theodore Ts'oefc6f622008-08-27 23:07:54 -0400232
Theodore Ts'o0e8a9562000-12-09 06:41:25 +0000233 return 0;
234}
235
236/* Destoy a journal's revoke table. The table must already be empty! */
237
238void journal_destroy_revoke(journal_t *journal)
239{
Theodore Ts'o8cf93332001-12-16 02:23:36 -0500240 struct jbd_revoke_table_s *table;
Theodore Ts'o0e8a9562000-12-09 06:41:25 +0000241 struct list_head *hash_list;
242 int i;
Theodore Ts'oefc6f622008-08-27 23:07:54 -0400243
Theodore Ts'o0e8a9562000-12-09 06:41:25 +0000244 table = journal->j_revoke;
245 if (!table)
246 return;
Theodore Ts'oefc6f622008-08-27 23:07:54 -0400247
Theodore Ts'o0e8a9562000-12-09 06:41:25 +0000248 for (i=0; i<table->hash_size; i++) {
249 hash_list = &table->hash_table[i];
250 J_ASSERT (list_empty(hash_list));
251 }
Theodore Ts'oefc6f622008-08-27 23:07:54 -0400252
Theodore Ts'o0e8a9562000-12-09 06:41:25 +0000253 kfree(table->hash_table);
254 kmem_cache_free(revoke_table_cache, table);
255 journal->j_revoke = NULL;
256}
257
258
259#ifdef __KERNEL__
260
Theodore Ts'oefc6f622008-08-27 23:07:54 -0400261/*
Theodore Ts'o0e8a9562000-12-09 06:41:25 +0000262 * 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
Theodore Ts'oefc6f622008-08-27 23:07:54 -0400266 * revoke.
Theodore Ts'o0e8a9562000-12-09 06:41:25 +0000267 *
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
Theodore Ts'oefc6f622008-08-27 23:07:54 -0400272 * metadata.
Theodore Ts'o0e8a9562000-12-09 06:41:25 +0000273 *
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
Theodore Ts'oefc6f622008-08-27 23:07:54 -0400276 * found implicitly.
Theodore Ts'o0e8a9562000-12-09 06:41:25 +0000277 *
Theodore Ts'o8cf93332001-12-16 02:23:36 -0500278 * bh_in may not be a journalled buffer - it may have come off
279 * the hash tables without an attached journal_head.
280 *
281 * If bh_in is non-zero, journal_revoke() will decrement its b_count
282 * by one.
Theodore Ts'o0e8a9562000-12-09 06:41:25 +0000283 */
284
Theodore Ts'oefc6f622008-08-27 23:07:54 -0400285int journal_revoke(handle_t *handle, unsigned long blocknr,
Theodore Ts'o0e8a9562000-12-09 06:41:25 +0000286 struct buffer_head *bh_in)
287{
Theodore Ts'o8cf93332001-12-16 02:23:36 -0500288 struct buffer_head *bh = NULL;
Theodore Ts'o0e8a9562000-12-09 06:41:25 +0000289 journal_t *journal;
290 kdev_t dev;
291 int err;
292
Theodore Ts'o8cf93332001-12-16 02:23:36 -0500293 if (bh_in)
294 BUFFER_TRACE(bh_in, "enter");
295
Theodore Ts'o0e8a9562000-12-09 06:41:25 +0000296 journal = handle->h_transaction->t_journal;
Theodore Ts'o1f735032001-03-29 19:00:50 +0000297 if (!journal_set_features(journal, 0, 0, JFS_FEATURE_INCOMPAT_REVOKE)){
298 J_ASSERT (!"Cannot set revoke feature!");
Theodore Ts'o0e8a9562000-12-09 06:41:25 +0000299 return -EINVAL;
Theodore Ts'o1f735032001-03-29 19:00:50 +0000300 }
Theodore Ts'o8cf93332001-12-16 02:23:36 -0500301
302 dev = journal->j_fs_dev;
Theodore Ts'o0e8a9562000-12-09 06:41:25 +0000303 bh = bh_in;
304
Theodore Ts'o8cf93332001-12-16 02:23:36 -0500305 if (!bh) {
Theodore Ts'o0e8a9562000-12-09 06:41:25 +0000306 bh = get_hash_table(dev, blocknr, journal->j_blocksize);
Theodore Ts'o8cf93332001-12-16 02:23:36 -0500307 if (bh)
308 BUFFER_TRACE(bh, "found on hash");
309 }
310#ifdef JBD_EXPENSIVE_CHECKING
311 else {
312 struct buffer_head *bh2;
313
314 /* If there is a different buffer_head lying around in
315 * memory anywhere... */
316 bh2 = get_hash_table(dev, blocknr, journal->j_blocksize);
317 if (bh2) {
318 /* ... and it has RevokeValid status... */
319 if ((bh2 != bh) &&
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
326 * cancel. */
327 J_ASSERT_BH(bh2, test_bit(BH_Revoked, &
328 bh2->b_state));
329 __brelse(bh2);
330 }
331 }
332#endif
Theodore Ts'o0e8a9562000-12-09 06:41:25 +0000333
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! */
337 if (bh) {
Theodore Ts'o8cf93332001-12-16 02:23:36 -0500338 J_ASSERT_BH(bh, !test_bit(BH_Revoked, &bh->b_state));
339 set_bit(BH_Revoked, &bh->b_state);
Theodore Ts'o0e8a9562000-12-09 06:41:25 +0000340 set_bit(BH_RevokeValid, &bh->b_state);
Theodore Ts'o8cf93332001-12-16 02:23:36 -0500341 if (bh_in) {
342 BUFFER_TRACE(bh_in, "call journal_forget");
Theodore Ts'o0e8a9562000-12-09 06:41:25 +0000343 journal_forget(handle, bh_in);
Theodore Ts'o8cf93332001-12-16 02:23:36 -0500344 } else {
345 BUFFER_TRACE(bh, "call brelse");
346 __brelse(bh);
347 }
Theodore Ts'o0e8a9562000-12-09 06:41:25 +0000348 }
349
350 lock_journal(journal);
Theodore Ts'o8cf93332001-12-16 02:23:36 -0500351 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);
Theodore Ts'o0e8a9562000-12-09 06:41:25 +0000354 unlock_journal(journal);
Theodore Ts'o8cf93332001-12-16 02:23:36 -0500355 BUFFER_TRACE(bh_in, "exit");
Theodore Ts'o0e8a9562000-12-09 06:41:25 +0000356 return err;
357}
358
Theodore Ts'o0e8a9562000-12-09 06:41:25 +0000359/*
360 * Cancel an outstanding revoke. For use only internally by the
361 * journaling code (called from journal_get_write_access).
362 *
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.
366 *
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
372 * set.
373 *
374 * The caller must have the journal locked.
Theodore Ts'o8cf93332001-12-16 02:23:36 -0500375 */
376int journal_cancel_revoke(handle_t *handle, struct journal_head *jh)
Theodore Ts'o0e8a9562000-12-09 06:41:25 +0000377{
Theodore Ts'o8cf93332001-12-16 02:23:36 -0500378 struct jbd_revoke_record_s *record;
Theodore Ts'o0e8a9562000-12-09 06:41:25 +0000379 journal_t *journal = handle->h_transaction->t_journal;
380 int need_cancel;
Theodore Ts'o8cf93332001-12-16 02:23:36 -0500381 int did_revoke = 0; /* akpm: debug */
382 struct buffer_head *bh = jh2bh(jh);
Theodore Ts'oefc6f622008-08-27 23:07:54 -0400383
Theodore Ts'o8cf93332001-12-16 02:23:36 -0500384 jbd_debug(4, "journal_head %p, cancelling revoke\n", jh);
385
Theodore Ts'o0e8a9562000-12-09 06:41:25 +0000386 /* 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));
392 else {
393 need_cancel = 1;
394 clear_bit(BH_Revoked, &bh->b_state);
395 }
Theodore Ts'o8cf93332001-12-16 02:23:36 -0500396
Theodore Ts'o0e8a9562000-12-09 06:41:25 +0000397 if (need_cancel) {
398 record = find_revoke_record(journal, bh->b_blocknr);
399 if (record) {
Theodore Ts'o8cf93332001-12-16 02:23:36 -0500400 jbd_debug(4, "cancelled existing revoke on "
401 "blocknr %lu\n", bh->b_blocknr);
Theodore Ts'o0e8a9562000-12-09 06:41:25 +0000402 list_del(&record->hash);
403 kmem_cache_free(revoke_record_cache, record);
Theodore Ts'o8cf93332001-12-16 02:23:36 -0500404 did_revoke = 1;
Theodore Ts'o0e8a9562000-12-09 06:41:25 +0000405 }
406 }
Theodore Ts'o8cf93332001-12-16 02:23:36 -0500407
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);
412#endif
413
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);
421 if (bh2) {
422 clear_bit(BH_Revoked, &bh2->b_state);
423 __brelse(bh2);
424 }
425 }
Theodore Ts'oefc6f622008-08-27 23:07:54 -0400426
Theodore Ts'o8cf93332001-12-16 02:23:36 -0500427 return did_revoke;
Theodore Ts'o0e8a9562000-12-09 06:41:25 +0000428}
429
430
431/*
432 * Write revoke records to the journal for all entries in the current
433 * revoke hash, deleting the entries as we go.
434 *
435 * Called with the journal lock held.
436 */
437
Theodore Ts'oefc6f622008-08-27 23:07:54 -0400438void journal_write_revoke_records(journal_t *journal,
Theodore Ts'o0e8a9562000-12-09 06:41:25 +0000439 transaction_t *transaction)
440{
Theodore Ts'o8cf93332001-12-16 02:23:36 -0500441 struct journal_head *descriptor;
442 struct jbd_revoke_record_s *record;
443 struct jbd_revoke_table_s *revoke;
Theodore Ts'o0e8a9562000-12-09 06:41:25 +0000444 struct list_head *hash_list;
Theodore Ts'o1f735032001-03-29 19:00:50 +0000445 int i, offset, count;
Theodore Ts'o8cf93332001-12-16 02:23:36 -0500446
Theodore Ts'oefc6f622008-08-27 23:07:54 -0400447 descriptor = NULL;
Theodore Ts'o0e8a9562000-12-09 06:41:25 +0000448 offset = 0;
Theodore Ts'o1f735032001-03-29 19:00:50 +0000449 count = 0;
Theodore Ts'o0e8a9562000-12-09 06:41:25 +0000450 revoke = journal->j_revoke;
Theodore Ts'oefc6f622008-08-27 23:07:54 -0400451
Theodore Ts'o0e8a9562000-12-09 06:41:25 +0000452 for (i = 0; i < revoke->hash_size; i++) {
453 hash_list = &revoke->hash_table[i];
454
455 while (!list_empty(hash_list)) {
Theodore Ts'oefc6f622008-08-27 23:07:54 -0400456 record = (struct jbd_revoke_record_s *)
Theodore Ts'o0e8a9562000-12-09 06:41:25 +0000457 hash_list->next;
458 write_one_revoke_record(journal, transaction,
Theodore Ts'oefc6f622008-08-27 23:07:54 -0400459 &descriptor, &offset,
Theodore Ts'o0e8a9562000-12-09 06:41:25 +0000460 record);
Theodore Ts'o1f735032001-03-29 19:00:50 +0000461 count++;
Theodore Ts'o0e8a9562000-12-09 06:41:25 +0000462 list_del(&record->hash);
463 kmem_cache_free(revoke_record_cache, record);
464 }
465 }
Theodore Ts'oefc6f622008-08-27 23:07:54 -0400466 if (descriptor)
Theodore Ts'o0e8a9562000-12-09 06:41:25 +0000467 flush_descriptor(journal, descriptor, offset);
Theodore Ts'o8cf93332001-12-16 02:23:36 -0500468 jbd_debug(1, "Wrote %d revoke records\n", count);
Theodore Ts'o0e8a9562000-12-09 06:41:25 +0000469}
470
Theodore Ts'oefc6f622008-08-27 23:07:54 -0400471/*
Theodore Ts'o0e8a9562000-12-09 06:41:25 +0000472 * Write out one revoke record. We need to create a new descriptor
Theodore Ts'oefc6f622008-08-27 23:07:54 -0400473 * block if the old one is full or if we have not already created one.
Theodore Ts'o0e8a9562000-12-09 06:41:25 +0000474 */
475
Theodore Ts'oefc6f622008-08-27 23:07:54 -0400476static void write_one_revoke_record(journal_t *journal,
Theodore Ts'o0e8a9562000-12-09 06:41:25 +0000477 transaction_t *transaction,
Theodore Ts'oefc6f622008-08-27 23:07:54 -0400478 struct journal_head **descriptorp,
Theodore Ts'o0e8a9562000-12-09 06:41:25 +0000479 int *offsetp,
Theodore Ts'o8cf93332001-12-16 02:23:36 -0500480 struct jbd_revoke_record_s *record)
Theodore Ts'o0e8a9562000-12-09 06:41:25 +0000481{
Theodore Ts'o8cf93332001-12-16 02:23:36 -0500482 struct journal_head *descriptor;
Theodore Ts'o0e8a9562000-12-09 06:41:25 +0000483 int offset;
484 journal_header_t *header;
Theodore Ts'o8cf93332001-12-16 02:23:36 -0500485
Theodore Ts'o0e8a9562000-12-09 06:41:25 +0000486 /* 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. */
Theodore Ts'o8cf93332001-12-16 02:23:36 -0500490 if (is_journal_aborted(journal))
Theodore Ts'o0e8a9562000-12-09 06:41:25 +0000491 return;
492
493 descriptor = *descriptorp;
494 offset = *offsetp;
Theodore Ts'o8cf93332001-12-16 02:23:36 -0500495
Theodore Ts'o0e8a9562000-12-09 06:41:25 +0000496 /* Make sure we have a descriptor with space left for the record */
497 if (descriptor) {
498 if (offset == journal->j_blocksize) {
499 flush_descriptor(journal, descriptor, offset);
500 descriptor = NULL;
501 }
502 }
Theodore Ts'oefc6f622008-08-27 23:07:54 -0400503
Theodore Ts'o0e8a9562000-12-09 06:41:25 +0000504 if (!descriptor) {
505 descriptor = journal_get_descriptor_buffer(journal);
Theodore Ts'o8cf93332001-12-16 02:23:36 -0500506 if (!descriptor)
507 return;
508 header = (journal_header_t *) &jh2bh(descriptor)->b_data[0];
Theodore Ts'o0e8a9562000-12-09 06:41:25 +0000509 header->h_magic = htonl(JFS_MAGIC_NUMBER);
510 header->h_blocktype = htonl(JFS_REVOKE_BLOCK);
511 header->h_sequence = htonl(transaction->t_tid);
512
513 /* Record it so that we can wait for IO completion later */
Theodore Ts'o8cf93332001-12-16 02:23:36 -0500514 JBUFFER_TRACE(descriptor, "file as BJ_LogCtl");
Theodore Ts'o0e8a9562000-12-09 06:41:25 +0000515 journal_file_buffer(descriptor, transaction, BJ_LogCtl);
Theodore Ts'o8cf93332001-12-16 02:23:36 -0500516
Theodore Ts'o0e8a9562000-12-09 06:41:25 +0000517 offset = sizeof(journal_revoke_header_t);
518 *descriptorp = descriptor;
519 }
Theodore Ts'oefc6f622008-08-27 23:07:54 -0400520
521 * ((unsigned int *)(&jh2bh(descriptor)->b_data[offset])) =
Theodore Ts'o0e8a9562000-12-09 06:41:25 +0000522 htonl(record->blocknr);
523 offset += 4;
524 *offsetp = offset;
525}
526
Theodore Ts'oefc6f622008-08-27 23:07:54 -0400527/*
Theodore Ts'o0e8a9562000-12-09 06:41:25 +0000528 * 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.
532 */
533
Theodore Ts'oefc6f622008-08-27 23:07:54 -0400534static void flush_descriptor(journal_t *journal,
535 struct journal_head *descriptor,
Theodore Ts'o0e8a9562000-12-09 06:41:25 +0000536 int offset)
537{
538 journal_revoke_header_t *header;
Theodore Ts'o8cf93332001-12-16 02:23:36 -0500539
540 if (is_journal_aborted(journal)) {
541 JBUFFER_TRACE(descriptor, "brelse");
542 __brelse(jh2bh(descriptor));
Theodore Ts'o0e8a9562000-12-09 06:41:25 +0000543 return;
544 }
Theodore Ts'oefc6f622008-08-27 23:07:54 -0400545
Theodore Ts'o8cf93332001-12-16 02:23:36 -0500546 header = (journal_revoke_header_t *) jh2bh(descriptor)->b_data;
Theodore Ts'o0e8a9562000-12-09 06:41:25 +0000547 header->r_count = htonl(offset);
Theodore Ts'o8cf93332001-12-16 02:23:36 -0500548 set_bit(BH_JWrite, &jh2bh(descriptor)->b_state);
549 {
550 struct buffer_head *bh = jh2bh(descriptor);
551 BUFFER_TRACE(bh, "write");
552 ll_rw_block (WRITE, 1, &bh);
553 }
Theodore Ts'o0e8a9562000-12-09 06:41:25 +0000554}
555
556#endif
557
Theodore Ts'oefc6f622008-08-27 23:07:54 -0400558/*
Theodore Ts'o0e8a9562000-12-09 06:41:25 +0000559 * Revoke support for recovery.
560 *
561 * Recovery needs to be able to:
562 *
563 * record all revoke records, including the tid of the latest instance
564 * of each revoke in the journal
565 *
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
568 * transaction)
Theodore Ts'oefc6f622008-08-27 23:07:54 -0400569 *
Theodore Ts'o0e8a9562000-12-09 06:41:25 +0000570 * empty the revoke table after recovery.
571 */
572
573/*
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
Theodore Ts'oefc6f622008-08-27 23:07:54 -0400577 * single block.
Theodore Ts'o0e8a9562000-12-09 06:41:25 +0000578 */
579
Theodore Ts'oefc6f622008-08-27 23:07:54 -0400580int journal_set_revoke(journal_t *journal,
581 unsigned long blocknr,
Theodore Ts'o0e8a9562000-12-09 06:41:25 +0000582 tid_t sequence)
583{
Theodore Ts'o8cf93332001-12-16 02:23:36 -0500584 struct jbd_revoke_record_s *record;
Theodore Ts'oefc6f622008-08-27 23:07:54 -0400585
Theodore Ts'o0e8a9562000-12-09 06:41:25 +0000586 record = find_revoke_record(journal, blocknr);
587 if (record) {
588 /* If we have multiple occurences, only record the
589 * latest sequence number in the hashed record */
Theodore Ts'o725c4742001-06-08 11:55:44 +0000590 if (tid_gt(sequence, record->sequence))
Theodore Ts'o0e8a9562000-12-09 06:41:25 +0000591 record->sequence = sequence;
592 return 0;
Theodore Ts'oefc6f622008-08-27 23:07:54 -0400593 }
Theodore Ts'o0e8a9562000-12-09 06:41:25 +0000594 return insert_revoke_hash(journal, blocknr, sequence);
595}
596
Theodore Ts'oefc6f622008-08-27 23:07:54 -0400597/*
Theodore Ts'o0e8a9562000-12-09 06:41:25 +0000598 * 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.
602 */
603
Theodore Ts'oefc6f622008-08-27 23:07:54 -0400604int journal_test_revoke(journal_t *journal,
Theodore Ts'o0e8a9562000-12-09 06:41:25 +0000605 unsigned long blocknr,
606 tid_t sequence)
607{
Theodore Ts'o8cf93332001-12-16 02:23:36 -0500608 struct jbd_revoke_record_s *record;
Theodore Ts'oefc6f622008-08-27 23:07:54 -0400609
Theodore Ts'o0e8a9562000-12-09 06:41:25 +0000610 record = find_revoke_record(journal, blocknr);
611 if (!record)
612 return 0;
Theodore Ts'o725c4742001-06-08 11:55:44 +0000613 if (tid_gt(sequence, record->sequence))
Theodore Ts'o0e8a9562000-12-09 06:41:25 +0000614 return 0;
615 return 1;
616}
617
618/*
619 * Finally, once recovery is over, we need to clear the revoke table so
620 * that it can be reused by the running filesystem.
621 */
622
623void journal_clear_revoke(journal_t *journal)
624{
625 int i;
626 struct list_head *hash_list;
Theodore Ts'o8cf93332001-12-16 02:23:36 -0500627 struct jbd_revoke_record_s *record;
628 struct jbd_revoke_table_s *revoke;
Theodore Ts'oefc6f622008-08-27 23:07:54 -0400629
Theodore Ts'o0e8a9562000-12-09 06:41:25 +0000630 revoke = journal->j_revoke;
Theodore Ts'oefc6f622008-08-27 23:07:54 -0400631
Theodore Ts'o0e8a9562000-12-09 06:41:25 +0000632 for (i = 0; i < revoke->hash_size; i++) {
633 hash_list = &revoke->hash_table[i];
634 while (!list_empty(hash_list)) {
Theodore Ts'o8cf93332001-12-16 02:23:36 -0500635 record = (struct jbd_revoke_record_s*) hash_list->next;
Theodore Ts'o0e8a9562000-12-09 06:41:25 +0000636 list_del(&record->hash);
637 kmem_cache_free(revoke_record_cache, record);
638 }
639 }
640}
641