patch-2_6_7-vs1_9_1_12
[linux-2.6.git] / fs / jbd / 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, Revoked clear:
54  *                      buffer has not been revoked, and cancel_revoke
55  *                      need do nothing.
56  * RevokeValid set, Revoked set:
57  *                      buffer has been revoked.  
58  */
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>
69 #include <linux/smp_lock.h>
70 #include <linux/init.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 jbd_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 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. */
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 journal_head **, int *,
102                                     struct jbd_revoke_record_s *);
103 static 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 */
109 static 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
119 int insert_revoke_hash(journal_t *journal, unsigned long blocknr, tid_t seq)
120 {
121         struct list_head *hash_list;
122         struct jbd_revoke_record_s *record;
123
124 repeat:
125         record = kmem_cache_alloc(revoke_record_cache, GFP_NOFS);
126         if (!record)
127                 goto oom;
128
129         record->sequence = seq;
130         record->blocknr = blocknr;
131         hash_list = &journal->j_revoke->hash_table[hash(journal, blocknr)];
132         spin_lock(&journal->j_revoke_lock);
133         list_add(&record->hash, hash_list);
134         spin_unlock(&journal->j_revoke_lock);
135         return 0;
136
137 oom:
138         if (!journal_oom_retry)
139                 return -ENOMEM;
140         jbd_debug(1, "ENOMEM in %s, retrying\n", __FUNCTION__);
141         yield();
142         goto repeat;
143 }
144
145 /* Find a revoke record in the journal's hash table. */
146
147 static struct jbd_revoke_record_s *find_revoke_record(journal_t *journal,
148                                                       unsigned long blocknr)
149 {
150         struct list_head *hash_list;
151         struct jbd_revoke_record_s *record;
152
153         hash_list = &journal->j_revoke->hash_table[hash(journal, blocknr)];
154
155         spin_lock(&journal->j_revoke_lock);
156         record = (struct jbd_revoke_record_s *) hash_list->next;
157         while (&(record->hash) != hash_list) {
158                 if (record->blocknr == blocknr) {
159                         spin_unlock(&journal->j_revoke_lock);
160                         return record;
161                 }
162                 record = (struct jbd_revoke_record_s *) record->hash.next;
163         }
164         spin_unlock(&journal->j_revoke_lock);
165         return NULL;
166 }
167
168 int __init journal_init_revoke_caches(void)
169 {
170         revoke_record_cache = kmem_cache_create("revoke_record",
171                                            sizeof(struct jbd_revoke_record_s),
172                                            0, SLAB_HWCACHE_ALIGN, NULL, NULL);
173         if (revoke_record_cache == 0)
174                 return -ENOMEM;
175
176         revoke_table_cache = kmem_cache_create("revoke_table",
177                                            sizeof(struct jbd_revoke_table_s),
178                                            0, 0, NULL, NULL);
179         if (revoke_table_cache == 0) {
180                 kmem_cache_destroy(revoke_record_cache);
181                 revoke_record_cache = NULL;
182                 return -ENOMEM;
183         }
184         return 0;
185 }
186
187 void journal_destroy_revoke_caches(void)
188 {
189         kmem_cache_destroy(revoke_record_cache);
190         revoke_record_cache = 0;
191         kmem_cache_destroy(revoke_table_cache);
192         revoke_table_cache = 0;
193 }
194
195 /* Initialise the revoke table for a given journal to a given size. */
196
197 int journal_init_revoke(journal_t *journal, int hash_size)
198 {
199         int shift, tmp;
200
201         J_ASSERT (journal->j_revoke_table[0] == NULL);
202
203         shift = 0;
204         tmp = hash_size;
205         while((tmp >>= 1UL) != 0UL)
206                 shift++;
207
208         journal->j_revoke_table[0] = kmem_cache_alloc(revoke_table_cache, GFP_KERNEL);
209         if (!journal->j_revoke_table[0])
210                 return -ENOMEM;
211         journal->j_revoke = journal->j_revoke_table[0];
212
213         /* Check that the hash_size is a power of two */
214         J_ASSERT ((hash_size & (hash_size-1)) == 0);
215
216         journal->j_revoke->hash_size = hash_size;
217
218         journal->j_revoke->hash_shift = shift;
219
220         journal->j_revoke->hash_table =
221                 kmalloc(hash_size * sizeof(struct list_head), GFP_KERNEL);
222         if (!journal->j_revoke->hash_table) {
223                 kmem_cache_free(revoke_table_cache, journal->j_revoke_table[0]);
224                 journal->j_revoke = NULL;
225                 return -ENOMEM;
226         }
227
228         for (tmp = 0; tmp < hash_size; tmp++)
229                 INIT_LIST_HEAD(&journal->j_revoke->hash_table[tmp]);
230
231         journal->j_revoke_table[1] = kmem_cache_alloc(revoke_table_cache, GFP_KERNEL);
232         if (!journal->j_revoke_table[1]) {
233                 kfree(journal->j_revoke_table[0]->hash_table);
234                 kmem_cache_free(revoke_table_cache, journal->j_revoke_table[0]);
235                 return -ENOMEM;
236         }
237
238         journal->j_revoke = journal->j_revoke_table[1];
239
240         /* Check that the hash_size is a power of two */
241         J_ASSERT ((hash_size & (hash_size-1)) == 0);
242
243         journal->j_revoke->hash_size = hash_size;
244
245         journal->j_revoke->hash_shift = shift;
246
247         journal->j_revoke->hash_table =
248                 kmalloc(hash_size * sizeof(struct list_head), GFP_KERNEL);
249         if (!journal->j_revoke->hash_table) {
250                 kfree(journal->j_revoke_table[0]->hash_table);
251                 kmem_cache_free(revoke_table_cache, journal->j_revoke_table[0]);
252                 kmem_cache_free(revoke_table_cache, journal->j_revoke_table[1]);
253                 journal->j_revoke = NULL;
254                 return -ENOMEM;
255         }
256
257         for (tmp = 0; tmp < hash_size; tmp++)
258                 INIT_LIST_HEAD(&journal->j_revoke->hash_table[tmp]);
259
260         spin_lock_init(&journal->j_revoke_lock);
261
262         return 0;
263 }
264
265 /* Destoy a journal's revoke table.  The table must already be empty! */
266
267 void journal_destroy_revoke(journal_t *journal)
268 {
269         struct jbd_revoke_table_s *table;
270         struct list_head *hash_list;
271         int i;
272
273         table = journal->j_revoke_table[0];
274         if (!table)
275                 return;
276
277         for (i=0; i<table->hash_size; i++) {
278                 hash_list = &table->hash_table[i];
279                 J_ASSERT (list_empty(hash_list));
280         }
281
282         kfree(table->hash_table);
283         kmem_cache_free(revoke_table_cache, table);
284         journal->j_revoke = NULL;
285
286         table = journal->j_revoke_table[1];
287         if (!table)
288                 return;
289
290         for (i=0; i<table->hash_size; i++) {
291                 hash_list = &table->hash_table[i];
292                 J_ASSERT (list_empty(hash_list));
293         }
294
295         kfree(table->hash_table);
296         kmem_cache_free(revoke_table_cache, table);
297         journal->j_revoke = NULL;
298 }
299
300
301 #ifdef __KERNEL__
302
303 /* 
304  * journal_revoke: revoke a given buffer_head from the journal.  This
305  * prevents the block from being replayed during recovery if we take a
306  * crash after this current transaction commits.  Any subsequent
307  * metadata writes of the buffer in this transaction cancel the
308  * revoke.  
309  *
310  * Note that this call may block --- it is up to the caller to make
311  * sure that there are no further calls to journal_write_metadata
312  * before the revoke is complete.  In ext3, this implies calling the
313  * revoke before clearing the block bitmap when we are deleting
314  * metadata. 
315  *
316  * Revoke performs a journal_forget on any buffer_head passed in as a
317  * parameter, but does _not_ forget the buffer_head if the bh was only
318  * found implicitly. 
319  *
320  * bh_in may not be a journalled buffer - it may have come off
321  * the hash tables without an attached journal_head.
322  *
323  * If bh_in is non-zero, journal_revoke() will decrement its b_count
324  * by one.
325  */
326
327 int journal_revoke(handle_t *handle, unsigned long blocknr, 
328                    struct buffer_head *bh_in)
329 {
330         struct buffer_head *bh = NULL;
331         journal_t *journal;
332         struct block_device *bdev;
333         int err;
334
335         if (bh_in)
336                 BUFFER_TRACE(bh_in, "enter");
337
338         journal = handle->h_transaction->t_journal;
339         if (!journal_set_features(journal, 0, 0, JFS_FEATURE_INCOMPAT_REVOKE)){
340                 J_ASSERT (!"Cannot set revoke feature!");
341                 return -EINVAL;
342         }
343
344         bdev = journal->j_fs_dev;
345         bh = bh_in;
346
347         if (!bh) {
348                 bh = __find_get_block(bdev, blocknr, journal->j_blocksize);
349                 if (bh)
350                         BUFFER_TRACE(bh, "found on hash");
351         }
352 #ifdef JBD_EXPENSIVE_CHECKING
353         else {
354                 struct buffer_head *bh2;
355
356                 /* If there is a different buffer_head lying around in
357                  * memory anywhere... */
358                 bh2 = __find_get_block(bdev, blocknr, journal->j_blocksize);
359                 if (bh2) {
360                         /* ... and it has RevokeValid status... */
361                         if (bh2 != bh && buffer_revokevalid(bh2))
362                                 /* ...then it better be revoked too,
363                                  * since it's illegal to create a revoke
364                                  * record against a buffer_head which is
365                                  * not marked revoked --- that would
366                                  * risk missing a subsequent revoke
367                                  * cancel. */
368                                 J_ASSERT_BH(bh2, buffer_revoked(bh2));
369                         put_bh(bh2);
370                 }
371         }
372 #endif
373
374         /* We really ought not ever to revoke twice in a row without
375            first having the revoke cancelled: it's illegal to free a
376            block twice without allocating it in between! */
377         if (bh) {
378                 J_ASSERT_BH(bh, !buffer_revoked(bh));
379                 set_buffer_revoked(bh);
380                 set_buffer_revokevalid(bh);
381                 if (bh_in) {
382                         BUFFER_TRACE(bh_in, "call journal_forget");
383                         journal_forget(handle, bh_in);
384                 } else {
385                         BUFFER_TRACE(bh, "call brelse");
386                         __brelse(bh);
387                 }
388         }
389
390         jbd_debug(2, "insert revoke for block %lu, bh_in=%p\n", blocknr, bh_in);
391         err = insert_revoke_hash(journal, blocknr,
392                                 handle->h_transaction->t_tid);
393         BUFFER_TRACE(bh_in, "exit");
394         return err;
395 }
396
397 /*
398  * Cancel an outstanding revoke.  For use only internally by the
399  * journaling code (called from journal_get_write_access).
400  *
401  * We trust buffer_revoked() on the buffer if the buffer is already
402  * being journaled: if there is no revoke pending on the buffer, then we
403  * don't do anything here.
404  *
405  * This would break if it were possible for a buffer to be revoked and
406  * discarded, and then reallocated within the same transaction.  In such
407  * a case we would have lost the revoked bit, but when we arrived here
408  * the second time we would still have a pending revoke to cancel.  So,
409  * do not trust the Revoked bit on buffers unless RevokeValid is also
410  * set.
411  *
412  * The caller must have the journal locked.
413  */
414 int journal_cancel_revoke(handle_t *handle, struct journal_head *jh)
415 {
416         struct jbd_revoke_record_s *record;
417         journal_t *journal = handle->h_transaction->t_journal;
418         int need_cancel;
419         int did_revoke = 0;     /* akpm: debug */
420         struct buffer_head *bh = jh2bh(jh);
421
422         jbd_debug(4, "journal_head %p, cancelling revoke\n", jh);
423
424         /* Is the existing Revoke bit valid?  If so, we trust it, and
425          * only perform the full cancel if the revoke bit is set.  If
426          * not, we can't trust the revoke bit, and we need to do the
427          * full search for a revoke record. */
428         if (test_set_buffer_revokevalid(bh)) {
429                 need_cancel = test_clear_buffer_revoked(bh);
430         } else {
431                 need_cancel = 1;
432                 clear_buffer_revoked(bh);
433         }
434
435         if (need_cancel) {
436                 record = find_revoke_record(journal, bh->b_blocknr);
437                 if (record) {
438                         jbd_debug(4, "cancelled existing revoke on "
439                                   "blocknr %llu\n", (unsigned long long)bh->b_blocknr);
440                         spin_lock(&journal->j_revoke_lock);
441                         list_del(&record->hash);
442                         spin_unlock(&journal->j_revoke_lock);
443                         kmem_cache_free(revoke_record_cache, record);
444                         did_revoke = 1;
445                 }
446         }
447
448 #ifdef JBD_EXPENSIVE_CHECKING
449         /* There better not be one left behind by now! */
450         record = find_revoke_record(journal, bh->b_blocknr);
451         J_ASSERT_JH(jh, record == NULL);
452 #endif
453
454         /* Finally, have we just cleared revoke on an unhashed
455          * buffer_head?  If so, we'd better make sure we clear the
456          * revoked status on any hashed alias too, otherwise the revoke
457          * state machine will get very upset later on. */
458         if (need_cancel) {
459                 struct buffer_head *bh2;
460                 bh2 = __find_get_block(bh->b_bdev, bh->b_blocknr, bh->b_size);
461                 if (bh2) {
462                         if (bh2 != bh)
463                                 clear_buffer_revoked(bh2);
464                         __brelse(bh2);
465                 }
466         }
467         return did_revoke;
468 }
469
470 /* journal_switch_revoke table select j_revoke for next transaction
471  * we do not want to suspend any processing until all revokes are
472  * written -bzzz
473  */
474 void journal_switch_revoke_table(journal_t *journal)
475 {
476         int i;
477
478         if (journal->j_revoke == journal->j_revoke_table[0])
479                 journal->j_revoke = journal->j_revoke_table[1];
480         else
481                 journal->j_revoke = journal->j_revoke_table[0];
482
483         for (i = 0; i < journal->j_revoke->hash_size; i++) 
484                 INIT_LIST_HEAD(&journal->j_revoke->hash_table[i]);
485 }
486
487 /*
488  * Write revoke records to the journal for all entries in the current
489  * revoke hash, deleting the entries as we go.
490  *
491  * Called with the journal lock held.
492  */
493
494 void journal_write_revoke_records(journal_t *journal, 
495                                   transaction_t *transaction)
496 {
497         struct journal_head *descriptor;
498         struct jbd_revoke_record_s *record;
499         struct jbd_revoke_table_s *revoke;
500         struct list_head *hash_list;
501         int i, offset, count;
502
503         descriptor = NULL; 
504         offset = 0;
505         count = 0;
506
507         /* select revoke table for committing transaction */
508         revoke = journal->j_revoke == journal->j_revoke_table[0] ?
509                 journal->j_revoke_table[1] : journal->j_revoke_table[0];
510
511         for (i = 0; i < revoke->hash_size; i++) {
512                 hash_list = &revoke->hash_table[i];
513
514                 while (!list_empty(hash_list)) {
515                         record = (struct jbd_revoke_record_s *) 
516                                 hash_list->next;
517                         write_one_revoke_record(journal, transaction,
518                                                 &descriptor, &offset, 
519                                                 record);
520                         count++;
521                         list_del(&record->hash);
522                         kmem_cache_free(revoke_record_cache, record);
523                 }
524         }
525         if (descriptor)
526                 flush_descriptor(journal, descriptor, offset);
527         jbd_debug(1, "Wrote %d revoke records\n", count);
528 }
529
530 /* 
531  * Write out one revoke record.  We need to create a new descriptor
532  * block if the old one is full or if we have not already created one.  
533  */
534
535 static void write_one_revoke_record(journal_t *journal, 
536                                     transaction_t *transaction,
537                                     struct journal_head **descriptorp, 
538                                     int *offsetp,
539                                     struct jbd_revoke_record_s *record)
540 {
541         struct journal_head *descriptor;
542         int offset;
543         journal_header_t *header;
544
545         /* If we are already aborting, this all becomes a noop.  We
546            still need to go round the loop in
547            journal_write_revoke_records in order to free all of the
548            revoke records: only the IO to the journal is omitted. */
549         if (is_journal_aborted(journal))
550                 return;
551
552         descriptor = *descriptorp;
553         offset = *offsetp;
554
555         /* Make sure we have a descriptor with space left for the record */
556         if (descriptor) {
557                 if (offset == journal->j_blocksize) {
558                         flush_descriptor(journal, descriptor, offset);
559                         descriptor = NULL;
560                 }
561         }
562
563         if (!descriptor) {
564                 descriptor = journal_get_descriptor_buffer(journal);
565                 if (!descriptor)
566                         return;
567                 header = (journal_header_t *) &jh2bh(descriptor)->b_data[0];
568                 header->h_magic     = htonl(JFS_MAGIC_NUMBER);
569                 header->h_blocktype = htonl(JFS_REVOKE_BLOCK);
570                 header->h_sequence  = htonl(transaction->t_tid);
571
572                 /* Record it so that we can wait for IO completion later */
573                 JBUFFER_TRACE(descriptor, "file as BJ_LogCtl");
574                 journal_file_buffer(descriptor, transaction, BJ_LogCtl);
575
576                 offset = sizeof(journal_revoke_header_t);
577                 *descriptorp = descriptor;
578         }
579
580         * ((unsigned int *)(&jh2bh(descriptor)->b_data[offset])) = 
581                 htonl(record->blocknr);
582         offset += 4;
583         *offsetp = offset;
584 }
585
586 /* 
587  * Flush a revoke descriptor out to the journal.  If we are aborting,
588  * this is a noop; otherwise we are generating a buffer which needs to
589  * be waited for during commit, so it has to go onto the appropriate
590  * journal buffer list.
591  */
592
593 static void flush_descriptor(journal_t *journal, 
594                              struct journal_head *descriptor, 
595                              int offset)
596 {
597         journal_revoke_header_t *header;
598         struct buffer_head *bh = jh2bh(descriptor);
599
600         if (is_journal_aborted(journal)) {
601                 put_bh(bh);
602                 return;
603         }
604
605         header = (journal_revoke_header_t *) jh2bh(descriptor)->b_data;
606         header->r_count = htonl(offset);
607         set_buffer_jwrite(bh);
608         BUFFER_TRACE(bh, "write");
609         set_buffer_dirty(bh);
610         ll_rw_block(WRITE, 1, &bh);
611 }
612 #endif
613
614 /* 
615  * Revoke support for recovery.
616  *
617  * Recovery needs to be able to:
618  *
619  *  record all revoke records, including the tid of the latest instance
620  *  of each revoke in the journal
621  *
622  *  check whether a given block in a given transaction should be replayed
623  *  (ie. has not been revoked by a revoke record in that or a subsequent
624  *  transaction)
625  * 
626  *  empty the revoke table after recovery.
627  */
628
629 /*
630  * First, setting revoke records.  We create a new revoke record for
631  * every block ever revoked in the log as we scan it for recovery, and
632  * we update the existing records if we find multiple revokes for a
633  * single block. 
634  */
635
636 int journal_set_revoke(journal_t *journal, 
637                        unsigned long blocknr, 
638                        tid_t sequence)
639 {
640         struct jbd_revoke_record_s *record;
641
642         record = find_revoke_record(journal, blocknr);
643         if (record) {
644                 /* If we have multiple occurrences, only record the
645                  * latest sequence number in the hashed record */
646                 if (tid_gt(sequence, record->sequence))
647                         record->sequence = sequence;
648                 return 0;
649         } 
650         return insert_revoke_hash(journal, blocknr, sequence);
651 }
652
653 /* 
654  * Test revoke records.  For a given block referenced in the log, has
655  * that block been revoked?  A revoke record with a given transaction
656  * sequence number revokes all blocks in that transaction and earlier
657  * ones, but later transactions still need replayed.
658  */
659
660 int journal_test_revoke(journal_t *journal, 
661                         unsigned long blocknr,
662                         tid_t sequence)
663 {
664         struct jbd_revoke_record_s *record;
665
666         record = find_revoke_record(journal, blocknr);
667         if (!record)
668                 return 0;
669         if (tid_gt(sequence, record->sequence))
670                 return 0;
671         return 1;
672 }
673
674 /*
675  * Finally, once recovery is over, we need to clear the revoke table so
676  * that it can be reused by the running filesystem.
677  */
678
679 void journal_clear_revoke(journal_t *journal)
680 {
681         int i;
682         struct list_head *hash_list;
683         struct jbd_revoke_record_s *record;
684         struct jbd_revoke_table_s *revoke;
685
686         revoke = journal->j_revoke;
687
688         for (i = 0; i < revoke->hash_size; i++) {
689                 hash_list = &revoke->hash_table[i];
690                 while (!list_empty(hash_list)) {
691                         record = (struct jbd_revoke_record_s*) hash_list->next;
692                         list_del(&record->hash);
693                         kmem_cache_free(revoke_record_cache, record);
694                 }
695         }
696 }