Whamcloud - gitweb
ChangeLog, jfs.h:
[tools/e2fsprogs.git] / e2fsck / revoke.c
1 /*
2  * linux/fs/revoke.c
3  * 
4  * 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:
18  * 
19  * + Commit: during commit we write the entire list of the current
20  *   transaction's revoked blocks to the journal
21  * 
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
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:
32  *   The desired end result is the journaling of the new block, so we 
33  *   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  *
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
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, Revoke clear:
54  *                      buffer has not been revoked, and cancel_revoke
55  *                      need do nothing.
56  * RevokeValid set, Revoke set:
57  * buffer has been revoked.  
58  */
59
60 #ifndef __KERNEL__
61 #include "jfs_user.h"
62 #else
63 #include <linux/sched.h>
64 #include <linux/fs.h>
65 #include <linux/jfs.h>
66 #include <linux/errno.h>
67 #include <linux/slab.h>
68 #include <linux/locks.h>
69 #include <linux/buffer.h>
70 #include <linux/list.h>
71 #endif
72
73 static kmem_cache_t *revoke_record_cache;
74 static kmem_cache_t *revoke_table_cache;
75
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
80 struct jfs_revoke_record_s 
81 {
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. */
89 struct jfs_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. */
93         int               hash_size; 
94         int               hash_shift; 
95         struct list_head *hash_table;
96 };
97
98
99 #ifdef __KERNEL__
100 static void write_one_revoke_record(journal_t *, transaction_t *,
101                                     struct buffer_head **, int *,
102                                     struct jfs_revoke_record_s *);
103 static void flush_descriptor(journal_t *, struct buffer_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 */
109 static inline int hash(journal_t *journal, unsigned long block)
110 {
111         struct jfs_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
119 static int insert_revoke_hash(journal_t *journal,
120                               unsigned long blocknr, tid_t seq)
121 {
122         struct list_head *hash_list;
123         struct jfs_revoke_record_s *record;
124         
125         record = kmem_cache_alloc(revoke_record_cache, GFP_KERNEL);
126         if (!record)
127                 return -ENOMEM;
128
129         record->sequence = seq;
130         record->blocknr = blocknr;
131         hash_list = &journal->j_revoke->hash_table[hash(journal, blocknr)];
132         list_add(&record->hash, hash_list);
133         return 0;
134 }
135
136 /* Find a revoke record in the journal's hash table. */
137
138 static struct jfs_revoke_record_s *find_revoke_record(journal_t *journal,
139                                                       unsigned long blocknr)
140 {
141         struct list_head *hash_list;
142         struct jfs_revoke_record_s *record;
143         
144         hash_list = &journal->j_revoke->hash_table[hash(journal, blocknr)];
145
146         record = (struct jfs_revoke_record_s *) hash_list->next;
147         while (&(record->hash) != hash_list) {
148                 if (record->blocknr == blocknr)
149                         return record;
150                 record = (struct jfs_revoke_record_s *) record->hash.next;
151         }
152         return NULL;
153 }
154
155
156
157 /* Initialise the revoke table for a given journal to a given size. */
158
159 int journal_init_revoke(journal_t *journal, int hash_size)
160 {
161         int shift, tmp;
162         
163         J_ASSERT (journal->j_revoke == NULL);
164         
165         if (!revoke_record_cache)
166                 revoke_record_cache = 
167                         kmem_cache_create ("revoke_record",
168                                            sizeof(struct jfs_revoke_record_s),
169                                            0, SLAB_HWCACHE_ALIGN, NULL, NULL);
170         
171         if (!revoke_table_cache)
172                 revoke_table_cache = 
173                         kmem_cache_create ("revoke_table",
174                                            sizeof(struct jfs_revoke_table_s),
175                                            0, 0, NULL, NULL);
176
177         if (!revoke_record_cache || !revoke_table_cache)
178                 return -ENOMEM;
179         
180         journal->j_revoke = kmem_cache_alloc(revoke_table_cache, GFP_KERNEL);
181         if (!journal->j_revoke)
182                 return -ENOMEM;
183         
184         /* Check that the hash_size is a power of two */
185         J_ASSERT ((hash_size & (hash_size-1)) == 0);
186
187         journal->j_revoke->hash_size = hash_size;
188
189         shift = 0;
190         tmp = hash_size;
191         while((tmp >>= 1UL) != 0UL)
192                 shift++;
193         journal->j_revoke->hash_shift = shift;
194
195         journal->j_revoke->hash_table =
196                 kmalloc(hash_size * sizeof(struct list_head), GFP_KERNEL);
197         if (!journal->j_revoke->hash_table) {
198                 kmem_cache_free(revoke_table_cache, journal->j_revoke);
199                 journal->j_revoke = NULL;
200                 return -ENOMEM;
201         }
202         
203         for (tmp = 0; tmp < hash_size; tmp++)
204                 INIT_LIST_HEAD(&journal->j_revoke->hash_table[tmp]);
205         
206         return 0;
207 }
208
209 /* Destoy a journal's revoke table.  The table must already be empty! */
210
211 void journal_destroy_revoke(journal_t *journal)
212 {
213         struct jfs_revoke_table_s *table;
214         struct list_head *hash_list;
215         int i;
216         
217         table = journal->j_revoke;
218         if (!table)
219                 return;
220         
221         for (i=0; i<table->hash_size; i++) {
222                 hash_list = &table->hash_table[i];
223                 J_ASSERT (list_empty(hash_list));
224         }
225         
226         kfree(table->hash_table);
227         kmem_cache_free(revoke_table_cache, table);
228         journal->j_revoke = NULL;
229 }
230
231
232 #ifdef __KERNEL__
233
234 /* 
235  * journal_revoke: revoke a given buffer_head from the journal.  This
236  * prevents the block from being replayed during recovery if we take a
237  * crash after this current transaction commits.  Any subsequent
238  * metadata writes of the buffer in this transaction cancel the
239  * revoke.  
240  *
241  * Note that this call may block --- it is up to the caller to make
242  * sure that there are no further calls to journal_write_metadata
243  * before the revoke is complete.  In ext3, this implies calling the
244  * revoke before clearing the block bitmap when we are deleting
245  * metadata. 
246  *
247  * Revoke performs a journal_forget on any buffer_head passed in as a
248  * parameter, but does _not_ forget the buffer_head if the bh was only
249  * found implicitly. 
250  *
251  * Revoke must observe the same synchronisation rules as bforget: it
252  * must not discard the buffer once it has blocked.
253  */
254
255 int journal_revoke(handle_t *handle, unsigned long blocknr, 
256                    struct buffer_head *bh_in)
257 {
258         struct buffer_head *bh;
259         journal_t *journal;
260         kdev_t dev;
261         int err;
262
263         journal = handle->h_transaction->t_journal;
264         if (!journal_set_features(journal, 0, 0, JFS_FEATURE_INCOMPAT_REVOKE)){
265                 J_ASSERT (!"Cannot set revoke feature!");
266                 return -EINVAL;
267         }
268         
269         dev = journal->j_dev;
270         bh = bh_in;
271
272         if (!bh)
273                 bh = get_hash_table(dev, blocknr, journal->j_blocksize);
274
275         /* We really ought not ever to revoke twice in a row without
276            first having the revoke cancelled: it's illegal to free a
277            block twice without allocating it in between! */
278         if (bh) {
279                 J_ASSERT (!test_and_set_bit(BH_Revoked, &bh->b_state));
280                 set_bit(BH_RevokeValid, &bh->b_state);
281                 if (bh_in)
282                         journal_forget(handle, bh_in);
283                 else
284                         brelse(bh);
285         }
286
287         lock_journal(journal);
288         err = insert_revoke_hash(journal, blocknr, 
289                                  handle->h_transaction->t_tid);
290         unlock_journal(journal);
291         
292         return err;
293 }
294
295
296 /*
297  * Cancel an outstanding revoke.  For use only internally by the
298  * journaling code (called from journal_get_write_access).
299  *
300  * We trust the BH_Revoked bit on the buffer if the buffer is already
301  * being journaled: if there is no revoke pending on the buffer, then we
302  * don't do anything here.
303  *
304  * This would break if it were possible for a buffer to be revoked and
305  * discarded, and then reallocated within the same transaction.  In such
306  * a case we would have lost the revoked bit, but when we arrived here
307  * the second time we would still have a pending revoke to cancel.  So,
308  * do not trust the Revoked bit on buffers unless RevokeValid is also
309  * set.
310  *
311  * The caller must have the journal locked.
312  * */
313
314 void journal_cancel_revoke(handle_t *handle, struct buffer_head *bh)
315 {
316         struct jfs_revoke_record_s *record;
317         journal_t *journal = handle->h_transaction->t_journal;
318         int need_cancel;
319         
320         J_ASSERT (journal->j_locked);
321         
322         /* Is the existing Revoke bit valid?  If so, we trust it, and
323          * only perform the full cancel if the revoke bit is set.  If
324          * not, we can't trust the revoke bit, and we need to do the
325          * full search for a revoke record. */
326         if (test_and_set_bit(BH_RevokeValid, &bh->b_state))
327                 need_cancel = (test_and_clear_bit(BH_Revoked, &bh->b_state));
328         else {
329                 need_cancel = 1;
330                 clear_bit(BH_Revoked, &bh->b_state);
331         }
332         
333         if (need_cancel) {
334                 record = find_revoke_record(journal, bh->b_blocknr);
335                 if (record) {
336                         list_del(&record->hash);
337                         kmem_cache_free(revoke_record_cache, record);
338                 }
339         }
340 }
341
342
343 /*
344  * Write revoke records to the journal for all entries in the current
345  * revoke hash, deleting the entries as we go.
346  *
347  * Called with the journal lock held.
348  */
349
350 void journal_write_revoke_records(journal_t *journal, 
351                                   transaction_t *transaction)
352 {
353         struct buffer_head *descriptor;
354         struct jfs_revoke_record_s *record;
355         struct jfs_revoke_table_s *revoke;
356         struct list_head *hash_list;
357         int i, offset, count;
358         
359         descriptor = NULL; 
360         offset = 0;
361         count = 0;
362         revoke = journal->j_revoke;
363         
364         for (i = 0; i < revoke->hash_size; i++) {
365                 hash_list = &revoke->hash_table[i];
366
367                 while (!list_empty(hash_list)) {
368                         record = (struct jfs_revoke_record_s *) 
369                                 hash_list->next;
370                         write_one_revoke_record(journal, transaction,
371                                                 &descriptor, &offset, 
372                                                 record);
373                         count++;
374                         list_del(&record->hash);
375                         kmem_cache_free(revoke_record_cache, record);
376                 }
377         }
378         if (descriptor) 
379                 flush_descriptor(journal, descriptor, offset);
380         jfs_debug(1, "Wrote %d revoke records\n", count);
381 }
382
383 /* 
384  * Write out one revoke record.  We need to create a new descriptor
385  * block if the old one is full or if we have not already created one.  
386  */
387
388 static void write_one_revoke_record(journal_t *journal, 
389                                     transaction_t *transaction,
390                                     struct buffer_head **descriptorp, 
391                                     int *offsetp,
392                                     struct jfs_revoke_record_s *record)
393 {
394         struct buffer_head *descriptor;
395         int offset;
396         journal_header_t *header;
397         
398         /* If we are already aborting, this all becomes a noop.  We
399            still need to go round the loop in
400            journal_write_revoke_records in order to free all of the
401            revoke records: only the IO to the journal is omitted. */
402         if (is_journal_abort(journal))
403                 return;
404
405         descriptor = *descriptorp;
406         offset = *offsetp;
407         
408         /* Make sure we have a descriptor with space left for the record */
409         if (descriptor) {
410                 if (offset == journal->j_blocksize) {
411                         flush_descriptor(journal, descriptor, offset);
412                         descriptor = NULL;
413                 }
414         }
415         
416         if (!descriptor) {
417                 descriptor = journal_get_descriptor_buffer(journal);
418                 header = (journal_header_t *) &descriptor->b_data[0];
419                 header->h_magic     = htonl(JFS_MAGIC_NUMBER);
420                 header->h_blocktype = htonl(JFS_REVOKE_BLOCK);
421                 header->h_sequence  = htonl(transaction->t_tid);
422
423                 /* Record it so that we can wait for IO completion later */
424                 journal_file_buffer(descriptor, transaction, BJ_LogCtl);
425                 
426                 offset = sizeof(journal_revoke_header_t);
427                 *descriptorp = descriptor;
428         }
429         
430         * ((unsigned int *)(&descriptor->b_data[offset])) = 
431                 htonl(record->blocknr);
432         offset += 4;
433         *offsetp = offset;
434 }
435
436 /* 
437  * Flush a revoke descriptor out to the journal.  If we are aborting,
438  * this is a noop; otherwise we are generating a buffer which needs to
439  * be waited for during commit, so it has to go onto the appropriate
440  * journal buffer list.
441  */
442
443 static void flush_descriptor(journal_t *journal, 
444                              struct buffer_head *descriptor, 
445                              int offset)
446 {
447         journal_revoke_header_t *header;
448         
449         if (is_journal_abort(journal)) {
450                 brelse(descriptor);
451                 return;
452         }
453         
454         header = (journal_revoke_header_t *) descriptor->b_data;
455         header->r_count = htonl(offset);
456         set_bit(BH_JWrite, &descriptor->b_state);
457         ll_rw_block (WRITE, 1, &descriptor);
458 }
459
460 #endif
461
462 /* 
463  * Revoke support for recovery.
464  *
465  * Recovery needs to be able to:
466  *
467  *  record all revoke records, including the tid of the latest instance
468  *  of each revoke in the journal
469  *
470  *  check whether a given block in a given transaction should be replayed
471  *  (ie. has not been revoked by a revoke record in that or a subsequent
472  *  transaction)
473  * 
474  *  empty the revoke table after recovery.
475  */
476
477 /*
478  * First, setting revoke records.  We create a new revoke record for
479  * every block ever revoked in the log as we scan it for recovery, and
480  * we update the existing records if we find multiple revokes for a
481  * single block. 
482  */
483
484 int journal_set_revoke(journal_t *journal, 
485                        unsigned long blocknr, 
486                        tid_t sequence)
487 {
488         struct jfs_revoke_record_s *record;
489         
490         record = find_revoke_record(journal, blocknr);
491         if (record) {
492                 /* If we have multiple occurences, only record the
493                  * latest sequence number in the hashed record */
494                 if (tid_gt(sequence, record->sequence))
495                         record->sequence = sequence;
496                 return 0;
497         } 
498         return insert_revoke_hash(journal, blocknr, sequence);
499 }
500
501 /* 
502  * Test revoke records.  For a given block referenced in the log, has
503  * that block been revoked?  A revoke record with a given transaction
504  * sequence number revokes all blocks in that transaction and earlier
505  * ones, but later transactions still need replayed.
506  */
507
508 int journal_test_revoke(journal_t *journal, 
509                         unsigned long blocknr,
510                         tid_t sequence)
511 {
512         struct jfs_revoke_record_s *record;
513         
514         record = find_revoke_record(journal, blocknr);
515         if (!record)
516                 return 0;
517         if (tid_gt(sequence, record->sequence))
518                 return 0;
519         return 1;
520 }
521
522 /*
523  * Finally, once recovery is over, we need to clear the revoke table so
524  * that it can be reused by the running filesystem.
525  */
526
527 void journal_clear_revoke(journal_t *journal)
528 {
529         int i;
530         struct list_head *hash_list;
531         struct jfs_revoke_record_s *record;
532         struct jfs_revoke_table_s *revoke;
533         
534         revoke = journal->j_revoke;
535         
536         for (i = 0; i < revoke->hash_size; i++) {
537                 hash_list = &revoke->hash_table[i];
538                 while (!list_empty(hash_list)) {
539                         record = (struct jfs_revoke_record_s*) hash_list->next;
540                         list_del(&record->hash);
541                         kmem_cache_free(revoke_record_cache, record);
542                 }
543         }
544 }
545