Back to home page

OSCL-LXR

 
 

    


0001 // SPDX-License-Identifier: GPL-2.0+
0002 /*
0003  * linux/fs/jbd2/revoke.c
0004  *
0005  * Written by Stephen C. Tweedie <sct@redhat.com>, 2000
0006  *
0007  * Copyright 2000 Red Hat corp --- All Rights Reserved
0008  *
0009  * Journal revoke routines for the generic filesystem journaling code;
0010  * part of the ext2fs journaling system.
0011  *
0012  * Revoke is the mechanism used to prevent old log records for deleted
0013  * metadata from being replayed on top of newer data using the same
0014  * blocks.  The revoke mechanism is used in two separate places:
0015  *
0016  * + Commit: during commit we write the entire list of the current
0017  *   transaction's revoked blocks to the journal
0018  *
0019  * + Recovery: during recovery we record the transaction ID of all
0020  *   revoked blocks.  If there are multiple revoke records in the log
0021  *   for a single block, only the last one counts, and if there is a log
0022  *   entry for a block beyond the last revoke, then that log entry still
0023  *   gets replayed.
0024  *
0025  * We can get interactions between revokes and new log data within a
0026  * single transaction:
0027  *
0028  * Block is revoked and then journaled:
0029  *   The desired end result is the journaling of the new block, so we
0030  *   cancel the revoke before the transaction commits.
0031  *
0032  * Block is journaled and then revoked:
0033  *   The revoke must take precedence over the write of the block, so we
0034  *   need either to cancel the journal entry or to write the revoke
0035  *   later in the log than the log block.  In this case, we choose the
0036  *   latter: journaling a block cancels any revoke record for that block
0037  *   in the current transaction, so any revoke for that block in the
0038  *   transaction must have happened after the block was journaled and so
0039  *   the revoke must take precedence.
0040  *
0041  * Block is revoked and then written as data:
0042  *   The data write is allowed to succeed, but the revoke is _not_
0043  *   cancelled.  We still need to prevent old log records from
0044  *   overwriting the new data.  We don't even need to clear the revoke
0045  *   bit here.
0046  *
0047  * We cache revoke status of a buffer in the current transaction in b_states
0048  * bits.  As the name says, revokevalid flag indicates that the cached revoke
0049  * status of a buffer is valid and we can rely on the cached status.
0050  *
0051  * Revoke information on buffers is a tri-state value:
0052  *
0053  * RevokeValid clear:   no cached revoke status, need to look it up
0054  * RevokeValid set, Revoked clear:
0055  *          buffer has not been revoked, and cancel_revoke
0056  *          need do nothing.
0057  * RevokeValid set, Revoked set:
0058  *          buffer has been revoked.
0059  *
0060  * Locking rules:
0061  * We keep two hash tables of revoke records. One hashtable belongs to the
0062  * running transaction (is pointed to by journal->j_revoke), the other one
0063  * belongs to the committing transaction. Accesses to the second hash table
0064  * happen only from the kjournald and no other thread touches this table.  Also
0065  * journal_switch_revoke_table() which switches which hashtable belongs to the
0066  * running and which to the committing transaction is called only from
0067  * kjournald. Therefore we need no locks when accessing the hashtable belonging
0068  * to the committing transaction.
0069  *
0070  * All users operating on the hash table belonging to the running transaction
0071  * have a handle to the transaction. Therefore they are safe from kjournald
0072  * switching hash tables under them. For operations on the lists of entries in
0073  * the hash table j_revoke_lock is used.
0074  *
0075  * Finally, also replay code uses the hash tables but at this moment no one else
0076  * can touch them (filesystem isn't mounted yet) and hence no locking is
0077  * needed.
0078  */
0079 
0080 #ifndef __KERNEL__
0081 #include "jfs_user.h"
0082 #else
0083 #include <linux/time.h>
0084 #include <linux/fs.h>
0085 #include <linux/jbd2.h>
0086 #include <linux/errno.h>
0087 #include <linux/slab.h>
0088 #include <linux/list.h>
0089 #include <linux/init.h>
0090 #include <linux/bio.h>
0091 #include <linux/log2.h>
0092 #include <linux/hash.h>
0093 #endif
0094 
0095 static struct kmem_cache *jbd2_revoke_record_cache;
0096 static struct kmem_cache *jbd2_revoke_table_cache;
0097 
0098 /* Each revoke record represents one single revoked block.  During
0099    journal replay, this involves recording the transaction ID of the
0100    last transaction to revoke this block. */
0101 
0102 struct jbd2_revoke_record_s
0103 {
0104     struct list_head  hash;
0105     tid_t         sequence; /* Used for recovery only */
0106     unsigned long long    blocknr;
0107 };
0108 
0109 
0110 /* The revoke table is just a simple hash table of revoke records. */
0111 struct jbd2_revoke_table_s
0112 {
0113     /* It is conceivable that we might want a larger hash table
0114      * for recovery.  Must be a power of two. */
0115     int       hash_size;
0116     int       hash_shift;
0117     struct list_head *hash_table;
0118 };
0119 
0120 
0121 #ifdef __KERNEL__
0122 static void write_one_revoke_record(transaction_t *,
0123                     struct list_head *,
0124                     struct buffer_head **, int *,
0125                     struct jbd2_revoke_record_s *);
0126 static void flush_descriptor(journal_t *, struct buffer_head *, int);
0127 #endif
0128 
0129 /* Utility functions to maintain the revoke table */
0130 
0131 static inline int hash(journal_t *journal, unsigned long long block)
0132 {
0133     return hash_64(block, journal->j_revoke->hash_shift);
0134 }
0135 
0136 static int insert_revoke_hash(journal_t *journal, unsigned long long blocknr,
0137                   tid_t seq)
0138 {
0139     struct list_head *hash_list;
0140     struct jbd2_revoke_record_s *record;
0141     gfp_t gfp_mask = GFP_NOFS;
0142 
0143     if (journal_oom_retry)
0144         gfp_mask |= __GFP_NOFAIL;
0145     record = kmem_cache_alloc(jbd2_revoke_record_cache, gfp_mask);
0146     if (!record)
0147         return -ENOMEM;
0148 
0149     record->sequence = seq;
0150     record->blocknr = blocknr;
0151     hash_list = &journal->j_revoke->hash_table[hash(journal, blocknr)];
0152     spin_lock(&journal->j_revoke_lock);
0153     list_add(&record->hash, hash_list);
0154     spin_unlock(&journal->j_revoke_lock);
0155     return 0;
0156 }
0157 
0158 /* Find a revoke record in the journal's hash table. */
0159 
0160 static struct jbd2_revoke_record_s *find_revoke_record(journal_t *journal,
0161                               unsigned long long blocknr)
0162 {
0163     struct list_head *hash_list;
0164     struct jbd2_revoke_record_s *record;
0165 
0166     hash_list = &journal->j_revoke->hash_table[hash(journal, blocknr)];
0167 
0168     spin_lock(&journal->j_revoke_lock);
0169     record = (struct jbd2_revoke_record_s *) hash_list->next;
0170     while (&(record->hash) != hash_list) {
0171         if (record->blocknr == blocknr) {
0172             spin_unlock(&journal->j_revoke_lock);
0173             return record;
0174         }
0175         record = (struct jbd2_revoke_record_s *) record->hash.next;
0176     }
0177     spin_unlock(&journal->j_revoke_lock);
0178     return NULL;
0179 }
0180 
0181 void jbd2_journal_destroy_revoke_record_cache(void)
0182 {
0183     kmem_cache_destroy(jbd2_revoke_record_cache);
0184     jbd2_revoke_record_cache = NULL;
0185 }
0186 
0187 void jbd2_journal_destroy_revoke_table_cache(void)
0188 {
0189     kmem_cache_destroy(jbd2_revoke_table_cache);
0190     jbd2_revoke_table_cache = NULL;
0191 }
0192 
0193 int __init jbd2_journal_init_revoke_record_cache(void)
0194 {
0195     J_ASSERT(!jbd2_revoke_record_cache);
0196     jbd2_revoke_record_cache = KMEM_CACHE(jbd2_revoke_record_s,
0197                     SLAB_HWCACHE_ALIGN|SLAB_TEMPORARY);
0198 
0199     if (!jbd2_revoke_record_cache) {
0200         pr_emerg("JBD2: failed to create revoke_record cache\n");
0201         return -ENOMEM;
0202     }
0203     return 0;
0204 }
0205 
0206 int __init jbd2_journal_init_revoke_table_cache(void)
0207 {
0208     J_ASSERT(!jbd2_revoke_table_cache);
0209     jbd2_revoke_table_cache = KMEM_CACHE(jbd2_revoke_table_s,
0210                          SLAB_TEMPORARY);
0211     if (!jbd2_revoke_table_cache) {
0212         pr_emerg("JBD2: failed to create revoke_table cache\n");
0213         return -ENOMEM;
0214     }
0215     return 0;
0216 }
0217 
0218 static struct jbd2_revoke_table_s *jbd2_journal_init_revoke_table(int hash_size)
0219 {
0220     int shift = 0;
0221     int tmp = hash_size;
0222     struct jbd2_revoke_table_s *table;
0223 
0224     table = kmem_cache_alloc(jbd2_revoke_table_cache, GFP_KERNEL);
0225     if (!table)
0226         goto out;
0227 
0228     while((tmp >>= 1UL) != 0UL)
0229         shift++;
0230 
0231     table->hash_size = hash_size;
0232     table->hash_shift = shift;
0233     table->hash_table =
0234         kmalloc_array(hash_size, sizeof(struct list_head), GFP_KERNEL);
0235     if (!table->hash_table) {
0236         kmem_cache_free(jbd2_revoke_table_cache, table);
0237         table = NULL;
0238         goto out;
0239     }
0240 
0241     for (tmp = 0; tmp < hash_size; tmp++)
0242         INIT_LIST_HEAD(&table->hash_table[tmp]);
0243 
0244 out:
0245     return table;
0246 }
0247 
0248 static void jbd2_journal_destroy_revoke_table(struct jbd2_revoke_table_s *table)
0249 {
0250     int i;
0251     struct list_head *hash_list;
0252 
0253     for (i = 0; i < table->hash_size; i++) {
0254         hash_list = &table->hash_table[i];
0255         J_ASSERT(list_empty(hash_list));
0256     }
0257 
0258     kfree(table->hash_table);
0259     kmem_cache_free(jbd2_revoke_table_cache, table);
0260 }
0261 
0262 /* Initialise the revoke table for a given journal to a given size. */
0263 int jbd2_journal_init_revoke(journal_t *journal, int hash_size)
0264 {
0265     J_ASSERT(journal->j_revoke_table[0] == NULL);
0266     J_ASSERT(is_power_of_2(hash_size));
0267 
0268     journal->j_revoke_table[0] = jbd2_journal_init_revoke_table(hash_size);
0269     if (!journal->j_revoke_table[0])
0270         goto fail0;
0271 
0272     journal->j_revoke_table[1] = jbd2_journal_init_revoke_table(hash_size);
0273     if (!journal->j_revoke_table[1])
0274         goto fail1;
0275 
0276     journal->j_revoke = journal->j_revoke_table[1];
0277 
0278     spin_lock_init(&journal->j_revoke_lock);
0279 
0280     return 0;
0281 
0282 fail1:
0283     jbd2_journal_destroy_revoke_table(journal->j_revoke_table[0]);
0284     journal->j_revoke_table[0] = NULL;
0285 fail0:
0286     return -ENOMEM;
0287 }
0288 
0289 /* Destroy a journal's revoke table.  The table must already be empty! */
0290 void jbd2_journal_destroy_revoke(journal_t *journal)
0291 {
0292     journal->j_revoke = NULL;
0293     if (journal->j_revoke_table[0])
0294         jbd2_journal_destroy_revoke_table(journal->j_revoke_table[0]);
0295     if (journal->j_revoke_table[1])
0296         jbd2_journal_destroy_revoke_table(journal->j_revoke_table[1]);
0297 }
0298 
0299 
0300 #ifdef __KERNEL__
0301 
0302 /*
0303  * jbd2_journal_revoke: revoke a given buffer_head from the journal.  This
0304  * prevents the block from being replayed during recovery if we take a
0305  * crash after this current transaction commits.  Any subsequent
0306  * metadata writes of the buffer in this transaction cancel the
0307  * revoke.
0308  *
0309  * Note that this call may block --- it is up to the caller to make
0310  * sure that there are no further calls to journal_write_metadata
0311  * before the revoke is complete.  In ext3, this implies calling the
0312  * revoke before clearing the block bitmap when we are deleting
0313  * metadata.
0314  *
0315  * Revoke performs a jbd2_journal_forget on any buffer_head passed in as a
0316  * parameter, but does _not_ forget the buffer_head if the bh was only
0317  * found implicitly.
0318  *
0319  * bh_in may not be a journalled buffer - it may have come off
0320  * the hash tables without an attached journal_head.
0321  *
0322  * If bh_in is non-zero, jbd2_journal_revoke() will decrement its b_count
0323  * by one.
0324  */
0325 
0326 int jbd2_journal_revoke(handle_t *handle, unsigned long long blocknr,
0327            struct buffer_head *bh_in)
0328 {
0329     struct buffer_head *bh = NULL;
0330     journal_t *journal;
0331     struct block_device *bdev;
0332     int err;
0333 
0334     might_sleep();
0335     if (bh_in)
0336         BUFFER_TRACE(bh_in, "enter");
0337 
0338     journal = handle->h_transaction->t_journal;
0339     if (!jbd2_journal_set_features(journal, 0, 0, JBD2_FEATURE_INCOMPAT_REVOKE)){
0340         J_ASSERT (!"Cannot set revoke feature!");
0341         return -EINVAL;
0342     }
0343 
0344     bdev = journal->j_fs_dev;
0345     bh = bh_in;
0346 
0347     if (!bh) {
0348         bh = __find_get_block(bdev, blocknr, journal->j_blocksize);
0349         if (bh)
0350             BUFFER_TRACE(bh, "found on hash");
0351     }
0352 #ifdef JBD2_EXPENSIVE_CHECKING
0353     else {
0354         struct buffer_head *bh2;
0355 
0356         /* If there is a different buffer_head lying around in
0357          * memory anywhere... */
0358         bh2 = __find_get_block(bdev, blocknr, journal->j_blocksize);
0359         if (bh2) {
0360             /* ... and it has RevokeValid status... */
0361             if (bh2 != bh && buffer_revokevalid(bh2))
0362                 /* ...then it better be revoked too,
0363                  * since it's illegal to create a revoke
0364                  * record against a buffer_head which is
0365                  * not marked revoked --- that would
0366                  * risk missing a subsequent revoke
0367                  * cancel. */
0368                 J_ASSERT_BH(bh2, buffer_revoked(bh2));
0369             put_bh(bh2);
0370         }
0371     }
0372 #endif
0373 
0374     if (WARN_ON_ONCE(handle->h_revoke_credits <= 0)) {
0375         if (!bh_in)
0376             brelse(bh);
0377         return -EIO;
0378     }
0379     /* We really ought not ever to revoke twice in a row without
0380            first having the revoke cancelled: it's illegal to free a
0381            block twice without allocating it in between! */
0382     if (bh) {
0383         if (!J_EXPECT_BH(bh, !buffer_revoked(bh),
0384                  "inconsistent data on disk")) {
0385             if (!bh_in)
0386                 brelse(bh);
0387             return -EIO;
0388         }
0389         set_buffer_revoked(bh);
0390         set_buffer_revokevalid(bh);
0391         if (bh_in) {
0392             BUFFER_TRACE(bh_in, "call jbd2_journal_forget");
0393             jbd2_journal_forget(handle, bh_in);
0394         } else {
0395             BUFFER_TRACE(bh, "call brelse");
0396             __brelse(bh);
0397         }
0398     }
0399     handle->h_revoke_credits--;
0400 
0401     jbd2_debug(2, "insert revoke for block %llu, bh_in=%p\n",blocknr, bh_in);
0402     err = insert_revoke_hash(journal, blocknr,
0403                 handle->h_transaction->t_tid);
0404     BUFFER_TRACE(bh_in, "exit");
0405     return err;
0406 }
0407 
0408 /*
0409  * Cancel an outstanding revoke.  For use only internally by the
0410  * journaling code (called from jbd2_journal_get_write_access).
0411  *
0412  * We trust buffer_revoked() on the buffer if the buffer is already
0413  * being journaled: if there is no revoke pending on the buffer, then we
0414  * don't do anything here.
0415  *
0416  * This would break if it were possible for a buffer to be revoked and
0417  * discarded, and then reallocated within the same transaction.  In such
0418  * a case we would have lost the revoked bit, but when we arrived here
0419  * the second time we would still have a pending revoke to cancel.  So,
0420  * do not trust the Revoked bit on buffers unless RevokeValid is also
0421  * set.
0422  */
0423 int jbd2_journal_cancel_revoke(handle_t *handle, struct journal_head *jh)
0424 {
0425     struct jbd2_revoke_record_s *record;
0426     journal_t *journal = handle->h_transaction->t_journal;
0427     int need_cancel;
0428     int did_revoke = 0; /* akpm: debug */
0429     struct buffer_head *bh = jh2bh(jh);
0430 
0431     jbd2_debug(4, "journal_head %p, cancelling revoke\n", jh);
0432 
0433     /* Is the existing Revoke bit valid?  If so, we trust it, and
0434      * only perform the full cancel if the revoke bit is set.  If
0435      * not, we can't trust the revoke bit, and we need to do the
0436      * full search for a revoke record. */
0437     if (test_set_buffer_revokevalid(bh)) {
0438         need_cancel = test_clear_buffer_revoked(bh);
0439     } else {
0440         need_cancel = 1;
0441         clear_buffer_revoked(bh);
0442     }
0443 
0444     if (need_cancel) {
0445         record = find_revoke_record(journal, bh->b_blocknr);
0446         if (record) {
0447             jbd2_debug(4, "cancelled existing revoke on "
0448                   "blocknr %llu\n", (unsigned long long)bh->b_blocknr);
0449             spin_lock(&journal->j_revoke_lock);
0450             list_del(&record->hash);
0451             spin_unlock(&journal->j_revoke_lock);
0452             kmem_cache_free(jbd2_revoke_record_cache, record);
0453             did_revoke = 1;
0454         }
0455     }
0456 
0457 #ifdef JBD2_EXPENSIVE_CHECKING
0458     /* There better not be one left behind by now! */
0459     record = find_revoke_record(journal, bh->b_blocknr);
0460     J_ASSERT_JH(jh, record == NULL);
0461 #endif
0462 
0463     /* Finally, have we just cleared revoke on an unhashed
0464      * buffer_head?  If so, we'd better make sure we clear the
0465      * revoked status on any hashed alias too, otherwise the revoke
0466      * state machine will get very upset later on. */
0467     if (need_cancel) {
0468         struct buffer_head *bh2;
0469         bh2 = __find_get_block(bh->b_bdev, bh->b_blocknr, bh->b_size);
0470         if (bh2) {
0471             if (bh2 != bh)
0472                 clear_buffer_revoked(bh2);
0473             __brelse(bh2);
0474         }
0475     }
0476     return did_revoke;
0477 }
0478 
0479 /*
0480  * journal_clear_revoked_flag clears revoked flag of buffers in
0481  * revoke table to reflect there is no revoked buffers in the next
0482  * transaction which is going to be started.
0483  */
0484 void jbd2_clear_buffer_revoked_flags(journal_t *journal)
0485 {
0486     struct jbd2_revoke_table_s *revoke = journal->j_revoke;
0487     int i = 0;
0488 
0489     for (i = 0; i < revoke->hash_size; i++) {
0490         struct list_head *hash_list;
0491         struct list_head *list_entry;
0492         hash_list = &revoke->hash_table[i];
0493 
0494         list_for_each(list_entry, hash_list) {
0495             struct jbd2_revoke_record_s *record;
0496             struct buffer_head *bh;
0497             record = (struct jbd2_revoke_record_s *)list_entry;
0498             bh = __find_get_block(journal->j_fs_dev,
0499                           record->blocknr,
0500                           journal->j_blocksize);
0501             if (bh) {
0502                 clear_buffer_revoked(bh);
0503                 __brelse(bh);
0504             }
0505         }
0506     }
0507 }
0508 
0509 /* journal_switch_revoke table select j_revoke for next transaction
0510  * we do not want to suspend any processing until all revokes are
0511  * written -bzzz
0512  */
0513 void jbd2_journal_switch_revoke_table(journal_t *journal)
0514 {
0515     int i;
0516 
0517     if (journal->j_revoke == journal->j_revoke_table[0])
0518         journal->j_revoke = journal->j_revoke_table[1];
0519     else
0520         journal->j_revoke = journal->j_revoke_table[0];
0521 
0522     for (i = 0; i < journal->j_revoke->hash_size; i++)
0523         INIT_LIST_HEAD(&journal->j_revoke->hash_table[i]);
0524 }
0525 
0526 /*
0527  * Write revoke records to the journal for all entries in the current
0528  * revoke hash, deleting the entries as we go.
0529  */
0530 void jbd2_journal_write_revoke_records(transaction_t *transaction,
0531                        struct list_head *log_bufs)
0532 {
0533     journal_t *journal = transaction->t_journal;
0534     struct buffer_head *descriptor;
0535     struct jbd2_revoke_record_s *record;
0536     struct jbd2_revoke_table_s *revoke;
0537     struct list_head *hash_list;
0538     int i, offset, count;
0539 
0540     descriptor = NULL;
0541     offset = 0;
0542     count = 0;
0543 
0544     /* select revoke table for committing transaction */
0545     revoke = journal->j_revoke == journal->j_revoke_table[0] ?
0546         journal->j_revoke_table[1] : journal->j_revoke_table[0];
0547 
0548     for (i = 0; i < revoke->hash_size; i++) {
0549         hash_list = &revoke->hash_table[i];
0550 
0551         while (!list_empty(hash_list)) {
0552             record = (struct jbd2_revoke_record_s *)
0553                 hash_list->next;
0554             write_one_revoke_record(transaction, log_bufs,
0555                         &descriptor, &offset, record);
0556             count++;
0557             list_del(&record->hash);
0558             kmem_cache_free(jbd2_revoke_record_cache, record);
0559         }
0560     }
0561     if (descriptor)
0562         flush_descriptor(journal, descriptor, offset);
0563     jbd2_debug(1, "Wrote %d revoke records\n", count);
0564 }
0565 
0566 /*
0567  * Write out one revoke record.  We need to create a new descriptor
0568  * block if the old one is full or if we have not already created one.
0569  */
0570 
0571 static void write_one_revoke_record(transaction_t *transaction,
0572                     struct list_head *log_bufs,
0573                     struct buffer_head **descriptorp,
0574                     int *offsetp,
0575                     struct jbd2_revoke_record_s *record)
0576 {
0577     journal_t *journal = transaction->t_journal;
0578     int csum_size = 0;
0579     struct buffer_head *descriptor;
0580     int sz, offset;
0581 
0582     /* If we are already aborting, this all becomes a noop.  We
0583            still need to go round the loop in
0584            jbd2_journal_write_revoke_records in order to free all of the
0585            revoke records: only the IO to the journal is omitted. */
0586     if (is_journal_aborted(journal))
0587         return;
0588 
0589     descriptor = *descriptorp;
0590     offset = *offsetp;
0591 
0592     /* Do we need to leave space at the end for a checksum? */
0593     if (jbd2_journal_has_csum_v2or3(journal))
0594         csum_size = sizeof(struct jbd2_journal_block_tail);
0595 
0596     if (jbd2_has_feature_64bit(journal))
0597         sz = 8;
0598     else
0599         sz = 4;
0600 
0601     /* Make sure we have a descriptor with space left for the record */
0602     if (descriptor) {
0603         if (offset + sz > journal->j_blocksize - csum_size) {
0604             flush_descriptor(journal, descriptor, offset);
0605             descriptor = NULL;
0606         }
0607     }
0608 
0609     if (!descriptor) {
0610         descriptor = jbd2_journal_get_descriptor_buffer(transaction,
0611                             JBD2_REVOKE_BLOCK);
0612         if (!descriptor)
0613             return;
0614 
0615         /* Record it so that we can wait for IO completion later */
0616         BUFFER_TRACE(descriptor, "file in log_bufs");
0617         jbd2_file_log_bh(log_bufs, descriptor);
0618 
0619         offset = sizeof(jbd2_journal_revoke_header_t);
0620         *descriptorp = descriptor;
0621     }
0622 
0623     if (jbd2_has_feature_64bit(journal))
0624         * ((__be64 *)(&descriptor->b_data[offset])) =
0625             cpu_to_be64(record->blocknr);
0626     else
0627         * ((__be32 *)(&descriptor->b_data[offset])) =
0628             cpu_to_be32(record->blocknr);
0629     offset += sz;
0630 
0631     *offsetp = offset;
0632 }
0633 
0634 /*
0635  * Flush a revoke descriptor out to the journal.  If we are aborting,
0636  * this is a noop; otherwise we are generating a buffer which needs to
0637  * be waited for during commit, so it has to go onto the appropriate
0638  * journal buffer list.
0639  */
0640 
0641 static void flush_descriptor(journal_t *journal,
0642                  struct buffer_head *descriptor,
0643                  int offset)
0644 {
0645     jbd2_journal_revoke_header_t *header;
0646 
0647     if (is_journal_aborted(journal))
0648         return;
0649 
0650     header = (jbd2_journal_revoke_header_t *)descriptor->b_data;
0651     header->r_count = cpu_to_be32(offset);
0652     jbd2_descriptor_block_csum_set(journal, descriptor);
0653 
0654     set_buffer_jwrite(descriptor);
0655     BUFFER_TRACE(descriptor, "write");
0656     set_buffer_dirty(descriptor);
0657     write_dirty_buffer(descriptor, REQ_SYNC);
0658 }
0659 #endif
0660 
0661 /*
0662  * Revoke support for recovery.
0663  *
0664  * Recovery needs to be able to:
0665  *
0666  *  record all revoke records, including the tid of the latest instance
0667  *  of each revoke in the journal
0668  *
0669  *  check whether a given block in a given transaction should be replayed
0670  *  (ie. has not been revoked by a revoke record in that or a subsequent
0671  *  transaction)
0672  *
0673  *  empty the revoke table after recovery.
0674  */
0675 
0676 /*
0677  * First, setting revoke records.  We create a new revoke record for
0678  * every block ever revoked in the log as we scan it for recovery, and
0679  * we update the existing records if we find multiple revokes for a
0680  * single block.
0681  */
0682 
0683 int jbd2_journal_set_revoke(journal_t *journal,
0684                unsigned long long blocknr,
0685                tid_t sequence)
0686 {
0687     struct jbd2_revoke_record_s *record;
0688 
0689     record = find_revoke_record(journal, blocknr);
0690     if (record) {
0691         /* If we have multiple occurrences, only record the
0692          * latest sequence number in the hashed record */
0693         if (tid_gt(sequence, record->sequence))
0694             record->sequence = sequence;
0695         return 0;
0696     }
0697     return insert_revoke_hash(journal, blocknr, sequence);
0698 }
0699 
0700 /*
0701  * Test revoke records.  For a given block referenced in the log, has
0702  * that block been revoked?  A revoke record with a given transaction
0703  * sequence number revokes all blocks in that transaction and earlier
0704  * ones, but later transactions still need replayed.
0705  */
0706 
0707 int jbd2_journal_test_revoke(journal_t *journal,
0708             unsigned long long blocknr,
0709             tid_t sequence)
0710 {
0711     struct jbd2_revoke_record_s *record;
0712 
0713     record = find_revoke_record(journal, blocknr);
0714     if (!record)
0715         return 0;
0716     if (tid_gt(sequence, record->sequence))
0717         return 0;
0718     return 1;
0719 }
0720 
0721 /*
0722  * Finally, once recovery is over, we need to clear the revoke table so
0723  * that it can be reused by the running filesystem.
0724  */
0725 
0726 void jbd2_journal_clear_revoke(journal_t *journal)
0727 {
0728     int i;
0729     struct list_head *hash_list;
0730     struct jbd2_revoke_record_s *record;
0731     struct jbd2_revoke_table_s *revoke;
0732 
0733     revoke = journal->j_revoke;
0734 
0735     for (i = 0; i < revoke->hash_size; i++) {
0736         hash_list = &revoke->hash_table[i];
0737         while (!list_empty(hash_list)) {
0738             record = (struct jbd2_revoke_record_s*) hash_list->next;
0739             list_del(&record->hash);
0740             kmem_cache_free(jbd2_revoke_record_cache, record);
0741         }
0742     }
0743 }