xref: /aosp_15_r20/external/e2fsprogs/e2fsck/revoke.c (revision 6a54128f25917bfc36a8a6e9d722c04a0b4641b6)
1*6a54128fSAndroid Build Coastguard Worker // SPDX-License-Identifier: GPL-2.0+
2*6a54128fSAndroid Build Coastguard Worker /*
3*6a54128fSAndroid Build Coastguard Worker  * linux/fs/jbd2/revoke.c
4*6a54128fSAndroid Build Coastguard Worker  *
5*6a54128fSAndroid Build Coastguard Worker  * Written by Stephen C. Tweedie <[email protected]>, 2000
6*6a54128fSAndroid Build Coastguard Worker  *
7*6a54128fSAndroid Build Coastguard Worker  * Copyright 2000 Red Hat corp --- All Rights Reserved
8*6a54128fSAndroid Build Coastguard Worker  *
9*6a54128fSAndroid Build Coastguard Worker  * Journal revoke routines for the generic filesystem journaling code;
10*6a54128fSAndroid Build Coastguard Worker  * part of the ext2fs journaling system.
11*6a54128fSAndroid Build Coastguard Worker  *
12*6a54128fSAndroid Build Coastguard Worker  * Revoke is the mechanism used to prevent old log records for deleted
13*6a54128fSAndroid Build Coastguard Worker  * metadata from being replayed on top of newer data using the same
14*6a54128fSAndroid Build Coastguard Worker  * blocks.  The revoke mechanism is used in two separate places:
15*6a54128fSAndroid Build Coastguard Worker  *
16*6a54128fSAndroid Build Coastguard Worker  * + Commit: during commit we write the entire list of the current
17*6a54128fSAndroid Build Coastguard Worker  *   transaction's revoked blocks to the journal
18*6a54128fSAndroid Build Coastguard Worker  *
19*6a54128fSAndroid Build Coastguard Worker  * + Recovery: during recovery we record the transaction ID of all
20*6a54128fSAndroid Build Coastguard Worker  *   revoked blocks.  If there are multiple revoke records in the log
21*6a54128fSAndroid Build Coastguard Worker  *   for a single block, only the last one counts, and if there is a log
22*6a54128fSAndroid Build Coastguard Worker  *   entry for a block beyond the last revoke, then that log entry still
23*6a54128fSAndroid Build Coastguard Worker  *   gets replayed.
24*6a54128fSAndroid Build Coastguard Worker  *
25*6a54128fSAndroid Build Coastguard Worker  * We can get interactions between revokes and new log data within a
26*6a54128fSAndroid Build Coastguard Worker  * single transaction:
27*6a54128fSAndroid Build Coastguard Worker  *
28*6a54128fSAndroid Build Coastguard Worker  * Block is revoked and then journaled:
29*6a54128fSAndroid Build Coastguard Worker  *   The desired end result is the journaling of the new block, so we
30*6a54128fSAndroid Build Coastguard Worker  *   cancel the revoke before the transaction commits.
31*6a54128fSAndroid Build Coastguard Worker  *
32*6a54128fSAndroid Build Coastguard Worker  * Block is journaled and then revoked:
33*6a54128fSAndroid Build Coastguard Worker  *   The revoke must take precedence over the write of the block, so we
34*6a54128fSAndroid Build Coastguard Worker  *   need either to cancel the journal entry or to write the revoke
35*6a54128fSAndroid Build Coastguard Worker  *   later in the log than the log block.  In this case, we choose the
36*6a54128fSAndroid Build Coastguard Worker  *   latter: journaling a block cancels any revoke record for that block
37*6a54128fSAndroid Build Coastguard Worker  *   in the current transaction, so any revoke for that block in the
38*6a54128fSAndroid Build Coastguard Worker  *   transaction must have happened after the block was journaled and so
39*6a54128fSAndroid Build Coastguard Worker  *   the revoke must take precedence.
40*6a54128fSAndroid Build Coastguard Worker  *
41*6a54128fSAndroid Build Coastguard Worker  * Block is revoked and then written as data:
42*6a54128fSAndroid Build Coastguard Worker  *   The data write is allowed to succeed, but the revoke is _not_
43*6a54128fSAndroid Build Coastguard Worker  *   cancelled.  We still need to prevent old log records from
44*6a54128fSAndroid Build Coastguard Worker  *   overwriting the new data.  We don't even need to clear the revoke
45*6a54128fSAndroid Build Coastguard Worker  *   bit here.
46*6a54128fSAndroid Build Coastguard Worker  *
47*6a54128fSAndroid Build Coastguard Worker  * We cache revoke status of a buffer in the current transaction in b_states
48*6a54128fSAndroid Build Coastguard Worker  * bits.  As the name says, revokevalid flag indicates that the cached revoke
49*6a54128fSAndroid Build Coastguard Worker  * status of a buffer is valid and we can rely on the cached status.
50*6a54128fSAndroid Build Coastguard Worker  *
51*6a54128fSAndroid Build Coastguard Worker  * Revoke information on buffers is a tri-state value:
52*6a54128fSAndroid Build Coastguard Worker  *
53*6a54128fSAndroid Build Coastguard Worker  * RevokeValid clear:	no cached revoke status, need to look it up
54*6a54128fSAndroid Build Coastguard Worker  * RevokeValid set, Revoked clear:
55*6a54128fSAndroid Build Coastguard Worker  *			buffer has not been revoked, and cancel_revoke
56*6a54128fSAndroid Build Coastguard Worker  *			need do nothing.
57*6a54128fSAndroid Build Coastguard Worker  * RevokeValid set, Revoked set:
58*6a54128fSAndroid Build Coastguard Worker  *			buffer has been revoked.
59*6a54128fSAndroid Build Coastguard Worker  *
60*6a54128fSAndroid Build Coastguard Worker  * Locking rules:
61*6a54128fSAndroid Build Coastguard Worker  * We keep two hash tables of revoke records. One hashtable belongs to the
62*6a54128fSAndroid Build Coastguard Worker  * running transaction (is pointed to by journal->j_revoke), the other one
63*6a54128fSAndroid Build Coastguard Worker  * belongs to the committing transaction. Accesses to the second hash table
64*6a54128fSAndroid Build Coastguard Worker  * happen only from the kjournald and no other thread touches this table.  Also
65*6a54128fSAndroid Build Coastguard Worker  * journal_switch_revoke_table() which switches which hashtable belongs to the
66*6a54128fSAndroid Build Coastguard Worker  * running and which to the committing transaction is called only from
67*6a54128fSAndroid Build Coastguard Worker  * kjournald. Therefore we need no locks when accessing the hashtable belonging
68*6a54128fSAndroid Build Coastguard Worker  * to the committing transaction.
69*6a54128fSAndroid Build Coastguard Worker  *
70*6a54128fSAndroid Build Coastguard Worker  * All users operating on the hash table belonging to the running transaction
71*6a54128fSAndroid Build Coastguard Worker  * have a handle to the transaction. Therefore they are safe from kjournald
72*6a54128fSAndroid Build Coastguard Worker  * switching hash tables under them. For operations on the lists of entries in
73*6a54128fSAndroid Build Coastguard Worker  * the hash table j_revoke_lock is used.
74*6a54128fSAndroid Build Coastguard Worker  *
75*6a54128fSAndroid Build Coastguard Worker  * Finally, also replay code uses the hash tables but at this moment no one else
76*6a54128fSAndroid Build Coastguard Worker  * can touch them (filesystem isn't mounted yet) and hence no locking is
77*6a54128fSAndroid Build Coastguard Worker  * needed.
78*6a54128fSAndroid Build Coastguard Worker  */
79*6a54128fSAndroid Build Coastguard Worker 
80*6a54128fSAndroid Build Coastguard Worker #ifndef __KERNEL__
81*6a54128fSAndroid Build Coastguard Worker #include "jfs_user.h"
82*6a54128fSAndroid Build Coastguard Worker #else
83*6a54128fSAndroid Build Coastguard Worker #include <linux/time.h>
84*6a54128fSAndroid Build Coastguard Worker #include <linux/fs.h>
85*6a54128fSAndroid Build Coastguard Worker #include <linux/jbd2.h>
86*6a54128fSAndroid Build Coastguard Worker #include <linux/errno.h>
87*6a54128fSAndroid Build Coastguard Worker #include <linux/slab.h>
88*6a54128fSAndroid Build Coastguard Worker #include <linux/list.h>
89*6a54128fSAndroid Build Coastguard Worker #include <linux/init.h>
90*6a54128fSAndroid Build Coastguard Worker #include <linux/bio.h>
91*6a54128fSAndroid Build Coastguard Worker #include <linux/log2.h>
92*6a54128fSAndroid Build Coastguard Worker #include <linux/hash.h>
93*6a54128fSAndroid Build Coastguard Worker #endif
94*6a54128fSAndroid Build Coastguard Worker 
95*6a54128fSAndroid Build Coastguard Worker static struct kmem_cache *jbd2_revoke_record_cache;
96*6a54128fSAndroid Build Coastguard Worker static struct kmem_cache *jbd2_revoke_table_cache;
97*6a54128fSAndroid Build Coastguard Worker 
98*6a54128fSAndroid Build Coastguard Worker /* Each revoke record represents one single revoked block.  During
99*6a54128fSAndroid Build Coastguard Worker    journal replay, this involves recording the transaction ID of the
100*6a54128fSAndroid Build Coastguard Worker    last transaction to revoke this block. */
101*6a54128fSAndroid Build Coastguard Worker 
102*6a54128fSAndroid Build Coastguard Worker struct jbd2_revoke_record_s
103*6a54128fSAndroid Build Coastguard Worker {
104*6a54128fSAndroid Build Coastguard Worker 	struct list_head  hash;
105*6a54128fSAndroid Build Coastguard Worker 	tid_t		  sequence;	/* Used for recovery only */
106*6a54128fSAndroid Build Coastguard Worker 	unsigned long long	  blocknr;
107*6a54128fSAndroid Build Coastguard Worker };
108*6a54128fSAndroid Build Coastguard Worker 
109*6a54128fSAndroid Build Coastguard Worker 
110*6a54128fSAndroid Build Coastguard Worker /* The revoke table is just a simple hash table of revoke records. */
111*6a54128fSAndroid Build Coastguard Worker struct jbd2_revoke_table_s
112*6a54128fSAndroid Build Coastguard Worker {
113*6a54128fSAndroid Build Coastguard Worker 	/* It is conceivable that we might want a larger hash table
114*6a54128fSAndroid Build Coastguard Worker 	 * for recovery.  Must be a power of two. */
115*6a54128fSAndroid Build Coastguard Worker 	int		  hash_size;
116*6a54128fSAndroid Build Coastguard Worker 	int		  hash_shift;
117*6a54128fSAndroid Build Coastguard Worker 	struct list_head *hash_table;
118*6a54128fSAndroid Build Coastguard Worker };
119*6a54128fSAndroid Build Coastguard Worker 
120*6a54128fSAndroid Build Coastguard Worker 
121*6a54128fSAndroid Build Coastguard Worker #ifdef __KERNEL__
122*6a54128fSAndroid Build Coastguard Worker static void write_one_revoke_record(transaction_t *,
123*6a54128fSAndroid Build Coastguard Worker 				    struct list_head *,
124*6a54128fSAndroid Build Coastguard Worker 				    struct buffer_head **, int *,
125*6a54128fSAndroid Build Coastguard Worker 				    struct jbd2_revoke_record_s *);
126*6a54128fSAndroid Build Coastguard Worker static void flush_descriptor(journal_t *, struct buffer_head *, int);
127*6a54128fSAndroid Build Coastguard Worker #endif
128*6a54128fSAndroid Build Coastguard Worker 
129*6a54128fSAndroid Build Coastguard Worker /* Utility functions to maintain the revoke table */
130*6a54128fSAndroid Build Coastguard Worker 
hash(journal_t * journal,unsigned long long block)131*6a54128fSAndroid Build Coastguard Worker static inline int hash(journal_t *journal, unsigned long long block)
132*6a54128fSAndroid Build Coastguard Worker {
133*6a54128fSAndroid Build Coastguard Worker 	return hash_64(block, journal->j_revoke->hash_shift);
134*6a54128fSAndroid Build Coastguard Worker }
135*6a54128fSAndroid Build Coastguard Worker 
insert_revoke_hash(journal_t * journal,unsigned long long blocknr,tid_t seq)136*6a54128fSAndroid Build Coastguard Worker static int insert_revoke_hash(journal_t *journal, unsigned long long blocknr,
137*6a54128fSAndroid Build Coastguard Worker 			      tid_t seq)
138*6a54128fSAndroid Build Coastguard Worker {
139*6a54128fSAndroid Build Coastguard Worker 	struct list_head *hash_list;
140*6a54128fSAndroid Build Coastguard Worker 	struct jbd2_revoke_record_s *record;
141*6a54128fSAndroid Build Coastguard Worker 	gfp_t gfp_mask = GFP_NOFS;
142*6a54128fSAndroid Build Coastguard Worker 
143*6a54128fSAndroid Build Coastguard Worker 	if (journal_oom_retry)
144*6a54128fSAndroid Build Coastguard Worker 		gfp_mask |= __GFP_NOFAIL;
145*6a54128fSAndroid Build Coastguard Worker 	record = kmem_cache_alloc(jbd2_revoke_record_cache, gfp_mask);
146*6a54128fSAndroid Build Coastguard Worker 	if (!record)
147*6a54128fSAndroid Build Coastguard Worker 		return -ENOMEM;
148*6a54128fSAndroid Build Coastguard Worker 
149*6a54128fSAndroid Build Coastguard Worker 	record->sequence = seq;
150*6a54128fSAndroid Build Coastguard Worker 	record->blocknr = blocknr;
151*6a54128fSAndroid Build Coastguard Worker 	hash_list = &journal->j_revoke->hash_table[hash(journal, blocknr)];
152*6a54128fSAndroid Build Coastguard Worker 	spin_lock(&journal->j_revoke_lock);
153*6a54128fSAndroid Build Coastguard Worker 	list_add(&record->hash, hash_list);
154*6a54128fSAndroid Build Coastguard Worker 	spin_unlock(&journal->j_revoke_lock);
155*6a54128fSAndroid Build Coastguard Worker 	return 0;
156*6a54128fSAndroid Build Coastguard Worker }
157*6a54128fSAndroid Build Coastguard Worker 
158*6a54128fSAndroid Build Coastguard Worker /* Find a revoke record in the journal's hash table. */
159*6a54128fSAndroid Build Coastguard Worker 
find_revoke_record(journal_t * journal,unsigned long long blocknr)160*6a54128fSAndroid Build Coastguard Worker static struct jbd2_revoke_record_s *find_revoke_record(journal_t *journal,
161*6a54128fSAndroid Build Coastguard Worker 						      unsigned long long blocknr)
162*6a54128fSAndroid Build Coastguard Worker {
163*6a54128fSAndroid Build Coastguard Worker 	struct list_head *hash_list;
164*6a54128fSAndroid Build Coastguard Worker 	struct jbd2_revoke_record_s *record;
165*6a54128fSAndroid Build Coastguard Worker 
166*6a54128fSAndroid Build Coastguard Worker 	hash_list = &journal->j_revoke->hash_table[hash(journal, blocknr)];
167*6a54128fSAndroid Build Coastguard Worker 
168*6a54128fSAndroid Build Coastguard Worker 	spin_lock(&journal->j_revoke_lock);
169*6a54128fSAndroid Build Coastguard Worker 	record = (struct jbd2_revoke_record_s *) hash_list->next;
170*6a54128fSAndroid Build Coastguard Worker 	while (&(record->hash) != hash_list) {
171*6a54128fSAndroid Build Coastguard Worker 		if (record->blocknr == blocknr) {
172*6a54128fSAndroid Build Coastguard Worker 			spin_unlock(&journal->j_revoke_lock);
173*6a54128fSAndroid Build Coastguard Worker 			return record;
174*6a54128fSAndroid Build Coastguard Worker 		}
175*6a54128fSAndroid Build Coastguard Worker 		record = (struct jbd2_revoke_record_s *) record->hash.next;
176*6a54128fSAndroid Build Coastguard Worker 	}
177*6a54128fSAndroid Build Coastguard Worker 	spin_unlock(&journal->j_revoke_lock);
178*6a54128fSAndroid Build Coastguard Worker 	return NULL;
179*6a54128fSAndroid Build Coastguard Worker }
180*6a54128fSAndroid Build Coastguard Worker 
jbd2_journal_destroy_revoke_record_cache(void)181*6a54128fSAndroid Build Coastguard Worker void jbd2_journal_destroy_revoke_record_cache(void)
182*6a54128fSAndroid Build Coastguard Worker {
183*6a54128fSAndroid Build Coastguard Worker 	kmem_cache_destroy(jbd2_revoke_record_cache);
184*6a54128fSAndroid Build Coastguard Worker 	jbd2_revoke_record_cache = NULL;
185*6a54128fSAndroid Build Coastguard Worker }
186*6a54128fSAndroid Build Coastguard Worker 
jbd2_journal_destroy_revoke_table_cache(void)187*6a54128fSAndroid Build Coastguard Worker void jbd2_journal_destroy_revoke_table_cache(void)
188*6a54128fSAndroid Build Coastguard Worker {
189*6a54128fSAndroid Build Coastguard Worker 	kmem_cache_destroy(jbd2_revoke_table_cache);
190*6a54128fSAndroid Build Coastguard Worker 	jbd2_revoke_table_cache = NULL;
191*6a54128fSAndroid Build Coastguard Worker }
192*6a54128fSAndroid Build Coastguard Worker 
jbd2_journal_init_revoke_record_cache(void)193*6a54128fSAndroid Build Coastguard Worker int __init jbd2_journal_init_revoke_record_cache(void)
194*6a54128fSAndroid Build Coastguard Worker {
195*6a54128fSAndroid Build Coastguard Worker 	J_ASSERT(!jbd2_revoke_record_cache);
196*6a54128fSAndroid Build Coastguard Worker 	jbd2_revoke_record_cache = KMEM_CACHE(jbd2_revoke_record_s,
197*6a54128fSAndroid Build Coastguard Worker 					SLAB_HWCACHE_ALIGN|SLAB_TEMPORARY);
198*6a54128fSAndroid Build Coastguard Worker 
199*6a54128fSAndroid Build Coastguard Worker 	if (!jbd2_revoke_record_cache) {
200*6a54128fSAndroid Build Coastguard Worker 		pr_emerg("JBD2: failed to create revoke_record cache\n");
201*6a54128fSAndroid Build Coastguard Worker 		return -ENOMEM;
202*6a54128fSAndroid Build Coastguard Worker 	}
203*6a54128fSAndroid Build Coastguard Worker 	return 0;
204*6a54128fSAndroid Build Coastguard Worker }
205*6a54128fSAndroid Build Coastguard Worker 
jbd2_journal_init_revoke_table_cache(void)206*6a54128fSAndroid Build Coastguard Worker int __init jbd2_journal_init_revoke_table_cache(void)
207*6a54128fSAndroid Build Coastguard Worker {
208*6a54128fSAndroid Build Coastguard Worker 	J_ASSERT(!jbd2_revoke_table_cache);
209*6a54128fSAndroid Build Coastguard Worker 	jbd2_revoke_table_cache = KMEM_CACHE(jbd2_revoke_table_s,
210*6a54128fSAndroid Build Coastguard Worker 					     SLAB_TEMPORARY);
211*6a54128fSAndroid Build Coastguard Worker 	if (!jbd2_revoke_table_cache) {
212*6a54128fSAndroid Build Coastguard Worker 		pr_emerg("JBD2: failed to create revoke_table cache\n");
213*6a54128fSAndroid Build Coastguard Worker 		return -ENOMEM;
214*6a54128fSAndroid Build Coastguard Worker 	}
215*6a54128fSAndroid Build Coastguard Worker 	return 0;
216*6a54128fSAndroid Build Coastguard Worker }
217*6a54128fSAndroid Build Coastguard Worker 
jbd2_journal_init_revoke_table(int hash_size)218*6a54128fSAndroid Build Coastguard Worker static struct jbd2_revoke_table_s *jbd2_journal_init_revoke_table(int hash_size)
219*6a54128fSAndroid Build Coastguard Worker {
220*6a54128fSAndroid Build Coastguard Worker 	int shift = 0;
221*6a54128fSAndroid Build Coastguard Worker 	int tmp = hash_size;
222*6a54128fSAndroid Build Coastguard Worker 	struct jbd2_revoke_table_s *table;
223*6a54128fSAndroid Build Coastguard Worker 
224*6a54128fSAndroid Build Coastguard Worker 	table = kmem_cache_alloc(jbd2_revoke_table_cache, GFP_KERNEL);
225*6a54128fSAndroid Build Coastguard Worker 	if (!table)
226*6a54128fSAndroid Build Coastguard Worker 		goto out;
227*6a54128fSAndroid Build Coastguard Worker 
228*6a54128fSAndroid Build Coastguard Worker 	while((tmp >>= 1UL) != 0UL)
229*6a54128fSAndroid Build Coastguard Worker 		shift++;
230*6a54128fSAndroid Build Coastguard Worker 
231*6a54128fSAndroid Build Coastguard Worker 	table->hash_size = hash_size;
232*6a54128fSAndroid Build Coastguard Worker 	table->hash_shift = shift;
233*6a54128fSAndroid Build Coastguard Worker 	table->hash_table =
234*6a54128fSAndroid Build Coastguard Worker 		kmalloc_array(hash_size, sizeof(struct list_head), GFP_KERNEL);
235*6a54128fSAndroid Build Coastguard Worker 	if (!table->hash_table) {
236*6a54128fSAndroid Build Coastguard Worker 		kmem_cache_free(jbd2_revoke_table_cache, table);
237*6a54128fSAndroid Build Coastguard Worker 		table = NULL;
238*6a54128fSAndroid Build Coastguard Worker 		goto out;
239*6a54128fSAndroid Build Coastguard Worker 	}
240*6a54128fSAndroid Build Coastguard Worker 
241*6a54128fSAndroid Build Coastguard Worker 	for (tmp = 0; tmp < hash_size; tmp++)
242*6a54128fSAndroid Build Coastguard Worker 		INIT_LIST_HEAD(&table->hash_table[tmp]);
243*6a54128fSAndroid Build Coastguard Worker 
244*6a54128fSAndroid Build Coastguard Worker out:
245*6a54128fSAndroid Build Coastguard Worker 	return table;
246*6a54128fSAndroid Build Coastguard Worker }
247*6a54128fSAndroid Build Coastguard Worker 
jbd2_journal_destroy_revoke_table(struct jbd2_revoke_table_s * table)248*6a54128fSAndroid Build Coastguard Worker static void jbd2_journal_destroy_revoke_table(struct jbd2_revoke_table_s *table)
249*6a54128fSAndroid Build Coastguard Worker {
250*6a54128fSAndroid Build Coastguard Worker 	int i;
251*6a54128fSAndroid Build Coastguard Worker 	struct list_head *hash_list;
252*6a54128fSAndroid Build Coastguard Worker 
253*6a54128fSAndroid Build Coastguard Worker 	for (i = 0; i < table->hash_size; i++) {
254*6a54128fSAndroid Build Coastguard Worker 		hash_list = &table->hash_table[i];
255*6a54128fSAndroid Build Coastguard Worker 		J_ASSERT(list_empty(hash_list));
256*6a54128fSAndroid Build Coastguard Worker 	}
257*6a54128fSAndroid Build Coastguard Worker 
258*6a54128fSAndroid Build Coastguard Worker 	kfree(table->hash_table);
259*6a54128fSAndroid Build Coastguard Worker 	kmem_cache_free(jbd2_revoke_table_cache, table);
260*6a54128fSAndroid Build Coastguard Worker }
261*6a54128fSAndroid Build Coastguard Worker 
262*6a54128fSAndroid Build Coastguard Worker /* Initialise the revoke table for a given journal to a given size. */
jbd2_journal_init_revoke(journal_t * journal,int hash_size)263*6a54128fSAndroid Build Coastguard Worker int jbd2_journal_init_revoke(journal_t *journal, int hash_size)
264*6a54128fSAndroid Build Coastguard Worker {
265*6a54128fSAndroid Build Coastguard Worker 	J_ASSERT(journal->j_revoke_table[0] == NULL);
266*6a54128fSAndroid Build Coastguard Worker 	J_ASSERT(is_power_of_2(hash_size));
267*6a54128fSAndroid Build Coastguard Worker 
268*6a54128fSAndroid Build Coastguard Worker 	journal->j_revoke_table[0] = jbd2_journal_init_revoke_table(hash_size);
269*6a54128fSAndroid Build Coastguard Worker 	if (!journal->j_revoke_table[0])
270*6a54128fSAndroid Build Coastguard Worker 		goto fail0;
271*6a54128fSAndroid Build Coastguard Worker 
272*6a54128fSAndroid Build Coastguard Worker 	journal->j_revoke_table[1] = jbd2_journal_init_revoke_table(hash_size);
273*6a54128fSAndroid Build Coastguard Worker 	if (!journal->j_revoke_table[1])
274*6a54128fSAndroid Build Coastguard Worker 		goto fail1;
275*6a54128fSAndroid Build Coastguard Worker 
276*6a54128fSAndroid Build Coastguard Worker 	journal->j_revoke = journal->j_revoke_table[1];
277*6a54128fSAndroid Build Coastguard Worker 
278*6a54128fSAndroid Build Coastguard Worker 	spin_lock_init(&journal->j_revoke_lock);
279*6a54128fSAndroid Build Coastguard Worker 
280*6a54128fSAndroid Build Coastguard Worker 	return 0;
281*6a54128fSAndroid Build Coastguard Worker 
282*6a54128fSAndroid Build Coastguard Worker fail1:
283*6a54128fSAndroid Build Coastguard Worker 	jbd2_journal_destroy_revoke_table(journal->j_revoke_table[0]);
284*6a54128fSAndroid Build Coastguard Worker 	journal->j_revoke_table[0] = NULL;
285*6a54128fSAndroid Build Coastguard Worker fail0:
286*6a54128fSAndroid Build Coastguard Worker 	return -ENOMEM;
287*6a54128fSAndroid Build Coastguard Worker }
288*6a54128fSAndroid Build Coastguard Worker 
289*6a54128fSAndroid Build Coastguard Worker /* Destroy a journal's revoke table.  The table must already be empty! */
jbd2_journal_destroy_revoke(journal_t * journal)290*6a54128fSAndroid Build Coastguard Worker void jbd2_journal_destroy_revoke(journal_t *journal)
291*6a54128fSAndroid Build Coastguard Worker {
292*6a54128fSAndroid Build Coastguard Worker 	journal->j_revoke = NULL;
293*6a54128fSAndroid Build Coastguard Worker 	if (journal->j_revoke_table[0])
294*6a54128fSAndroid Build Coastguard Worker 		jbd2_journal_destroy_revoke_table(journal->j_revoke_table[0]);
295*6a54128fSAndroid Build Coastguard Worker 	if (journal->j_revoke_table[1])
296*6a54128fSAndroid Build Coastguard Worker 		jbd2_journal_destroy_revoke_table(journal->j_revoke_table[1]);
297*6a54128fSAndroid Build Coastguard Worker }
298*6a54128fSAndroid Build Coastguard Worker 
299*6a54128fSAndroid Build Coastguard Worker 
300*6a54128fSAndroid Build Coastguard Worker #ifdef __KERNEL__
301*6a54128fSAndroid Build Coastguard Worker 
302*6a54128fSAndroid Build Coastguard Worker /*
303*6a54128fSAndroid Build Coastguard Worker  * jbd2_journal_revoke: revoke a given buffer_head from the journal.  This
304*6a54128fSAndroid Build Coastguard Worker  * prevents the block from being replayed during recovery if we take a
305*6a54128fSAndroid Build Coastguard Worker  * crash after this current transaction commits.  Any subsequent
306*6a54128fSAndroid Build Coastguard Worker  * metadata writes of the buffer in this transaction cancel the
307*6a54128fSAndroid Build Coastguard Worker  * revoke.
308*6a54128fSAndroid Build Coastguard Worker  *
309*6a54128fSAndroid Build Coastguard Worker  * Note that this call may block --- it is up to the caller to make
310*6a54128fSAndroid Build Coastguard Worker  * sure that there are no further calls to journal_write_metadata
311*6a54128fSAndroid Build Coastguard Worker  * before the revoke is complete.  In ext3, this implies calling the
312*6a54128fSAndroid Build Coastguard Worker  * revoke before clearing the block bitmap when we are deleting
313*6a54128fSAndroid Build Coastguard Worker  * metadata.
314*6a54128fSAndroid Build Coastguard Worker  *
315*6a54128fSAndroid Build Coastguard Worker  * Revoke performs a jbd2_journal_forget on any buffer_head passed in as a
316*6a54128fSAndroid Build Coastguard Worker  * parameter, but does _not_ forget the buffer_head if the bh was only
317*6a54128fSAndroid Build Coastguard Worker  * found implicitly.
318*6a54128fSAndroid Build Coastguard Worker  *
319*6a54128fSAndroid Build Coastguard Worker  * bh_in may not be a journalled buffer - it may have come off
320*6a54128fSAndroid Build Coastguard Worker  * the hash tables without an attached journal_head.
321*6a54128fSAndroid Build Coastguard Worker  *
322*6a54128fSAndroid Build Coastguard Worker  * If bh_in is non-zero, jbd2_journal_revoke() will decrement its b_count
323*6a54128fSAndroid Build Coastguard Worker  * by one.
324*6a54128fSAndroid Build Coastguard Worker  */
325*6a54128fSAndroid Build Coastguard Worker 
jbd2_journal_revoke(handle_t * handle,unsigned long long blocknr,struct buffer_head * bh_in)326*6a54128fSAndroid Build Coastguard Worker int jbd2_journal_revoke(handle_t *handle, unsigned long long blocknr,
327*6a54128fSAndroid Build Coastguard Worker 		   struct buffer_head *bh_in)
328*6a54128fSAndroid Build Coastguard Worker {
329*6a54128fSAndroid Build Coastguard Worker 	struct buffer_head *bh = NULL;
330*6a54128fSAndroid Build Coastguard Worker 	journal_t *journal;
331*6a54128fSAndroid Build Coastguard Worker 	struct block_device *bdev;
332*6a54128fSAndroid Build Coastguard Worker 	int err;
333*6a54128fSAndroid Build Coastguard Worker 
334*6a54128fSAndroid Build Coastguard Worker 	might_sleep();
335*6a54128fSAndroid Build Coastguard Worker 	if (bh_in)
336*6a54128fSAndroid Build Coastguard Worker 		BUFFER_TRACE(bh_in, "enter");
337*6a54128fSAndroid Build Coastguard Worker 
338*6a54128fSAndroid Build Coastguard Worker 	journal = handle->h_transaction->t_journal;
339*6a54128fSAndroid Build Coastguard Worker 	if (!jbd2_journal_set_features(journal, 0, 0, JBD2_FEATURE_INCOMPAT_REVOKE)){
340*6a54128fSAndroid Build Coastguard Worker 		J_ASSERT (!"Cannot set revoke feature!");
341*6a54128fSAndroid Build Coastguard Worker 		return -EINVAL;
342*6a54128fSAndroid Build Coastguard Worker 	}
343*6a54128fSAndroid Build Coastguard Worker 
344*6a54128fSAndroid Build Coastguard Worker 	bdev = journal->j_fs_dev;
345*6a54128fSAndroid Build Coastguard Worker 	bh = bh_in;
346*6a54128fSAndroid Build Coastguard Worker 
347*6a54128fSAndroid Build Coastguard Worker 	if (!bh) {
348*6a54128fSAndroid Build Coastguard Worker 		bh = __find_get_block(bdev, blocknr, journal->j_blocksize);
349*6a54128fSAndroid Build Coastguard Worker 		if (bh)
350*6a54128fSAndroid Build Coastguard Worker 			BUFFER_TRACE(bh, "found on hash");
351*6a54128fSAndroid Build Coastguard Worker 	}
352*6a54128fSAndroid Build Coastguard Worker #ifdef JBD2_EXPENSIVE_CHECKING
353*6a54128fSAndroid Build Coastguard Worker 	else {
354*6a54128fSAndroid Build Coastguard Worker 		struct buffer_head *bh2;
355*6a54128fSAndroid Build Coastguard Worker 
356*6a54128fSAndroid Build Coastguard Worker 		/* If there is a different buffer_head lying around in
357*6a54128fSAndroid Build Coastguard Worker 		 * memory anywhere... */
358*6a54128fSAndroid Build Coastguard Worker 		bh2 = __find_get_block(bdev, blocknr, journal->j_blocksize);
359*6a54128fSAndroid Build Coastguard Worker 		if (bh2) {
360*6a54128fSAndroid Build Coastguard Worker 			/* ... and it has RevokeValid status... */
361*6a54128fSAndroid Build Coastguard Worker 			if (bh2 != bh && buffer_revokevalid(bh2))
362*6a54128fSAndroid Build Coastguard Worker 				/* ...then it better be revoked too,
363*6a54128fSAndroid Build Coastguard Worker 				 * since it's illegal to create a revoke
364*6a54128fSAndroid Build Coastguard Worker 				 * record against a buffer_head which is
365*6a54128fSAndroid Build Coastguard Worker 				 * not marked revoked --- that would
366*6a54128fSAndroid Build Coastguard Worker 				 * risk missing a subsequent revoke
367*6a54128fSAndroid Build Coastguard Worker 				 * cancel. */
368*6a54128fSAndroid Build Coastguard Worker 				J_ASSERT_BH(bh2, buffer_revoked(bh2));
369*6a54128fSAndroid Build Coastguard Worker 			put_bh(bh2);
370*6a54128fSAndroid Build Coastguard Worker 		}
371*6a54128fSAndroid Build Coastguard Worker 	}
372*6a54128fSAndroid Build Coastguard Worker #endif
373*6a54128fSAndroid Build Coastguard Worker 
374*6a54128fSAndroid Build Coastguard Worker 	if (WARN_ON_ONCE(handle->h_revoke_credits <= 0)) {
375*6a54128fSAndroid Build Coastguard Worker 		if (!bh_in)
376*6a54128fSAndroid Build Coastguard Worker 			brelse(bh);
377*6a54128fSAndroid Build Coastguard Worker 		return -EIO;
378*6a54128fSAndroid Build Coastguard Worker 	}
379*6a54128fSAndroid Build Coastguard Worker 	/* We really ought not ever to revoke twice in a row without
380*6a54128fSAndroid Build Coastguard Worker            first having the revoke cancelled: it's illegal to free a
381*6a54128fSAndroid Build Coastguard Worker            block twice without allocating it in between! */
382*6a54128fSAndroid Build Coastguard Worker 	if (bh) {
383*6a54128fSAndroid Build Coastguard Worker 		if (!J_EXPECT_BH(bh, !buffer_revoked(bh),
384*6a54128fSAndroid Build Coastguard Worker 				 "inconsistent data on disk")) {
385*6a54128fSAndroid Build Coastguard Worker 			if (!bh_in)
386*6a54128fSAndroid Build Coastguard Worker 				brelse(bh);
387*6a54128fSAndroid Build Coastguard Worker 			return -EIO;
388*6a54128fSAndroid Build Coastguard Worker 		}
389*6a54128fSAndroid Build Coastguard Worker 		set_buffer_revoked(bh);
390*6a54128fSAndroid Build Coastguard Worker 		set_buffer_revokevalid(bh);
391*6a54128fSAndroid Build Coastguard Worker 		if (bh_in) {
392*6a54128fSAndroid Build Coastguard Worker 			BUFFER_TRACE(bh_in, "call jbd2_journal_forget");
393*6a54128fSAndroid Build Coastguard Worker 			jbd2_journal_forget(handle, bh_in);
394*6a54128fSAndroid Build Coastguard Worker 		} else {
395*6a54128fSAndroid Build Coastguard Worker 			BUFFER_TRACE(bh, "call brelse");
396*6a54128fSAndroid Build Coastguard Worker 			__brelse(bh);
397*6a54128fSAndroid Build Coastguard Worker 		}
398*6a54128fSAndroid Build Coastguard Worker 	}
399*6a54128fSAndroid Build Coastguard Worker 	handle->h_revoke_credits--;
400*6a54128fSAndroid Build Coastguard Worker 
401*6a54128fSAndroid Build Coastguard Worker 	jbd_debug(2, "insert revoke for block %llu, bh_in=%p\n",blocknr, bh_in);
402*6a54128fSAndroid Build Coastguard Worker 	err = insert_revoke_hash(journal, blocknr,
403*6a54128fSAndroid Build Coastguard Worker 				handle->h_transaction->t_tid);
404*6a54128fSAndroid Build Coastguard Worker 	BUFFER_TRACE(bh_in, "exit");
405*6a54128fSAndroid Build Coastguard Worker 	return err;
406*6a54128fSAndroid Build Coastguard Worker }
407*6a54128fSAndroid Build Coastguard Worker 
408*6a54128fSAndroid Build Coastguard Worker /*
409*6a54128fSAndroid Build Coastguard Worker  * Cancel an outstanding revoke.  For use only internally by the
410*6a54128fSAndroid Build Coastguard Worker  * journaling code (called from jbd2_journal_get_write_access).
411*6a54128fSAndroid Build Coastguard Worker  *
412*6a54128fSAndroid Build Coastguard Worker  * We trust buffer_revoked() on the buffer if the buffer is already
413*6a54128fSAndroid Build Coastguard Worker  * being journaled: if there is no revoke pending on the buffer, then we
414*6a54128fSAndroid Build Coastguard Worker  * don't do anything here.
415*6a54128fSAndroid Build Coastguard Worker  *
416*6a54128fSAndroid Build Coastguard Worker  * This would break if it were possible for a buffer to be revoked and
417*6a54128fSAndroid Build Coastguard Worker  * discarded, and then reallocated within the same transaction.  In such
418*6a54128fSAndroid Build Coastguard Worker  * a case we would have lost the revoked bit, but when we arrived here
419*6a54128fSAndroid Build Coastguard Worker  * the second time we would still have a pending revoke to cancel.  So,
420*6a54128fSAndroid Build Coastguard Worker  * do not trust the Revoked bit on buffers unless RevokeValid is also
421*6a54128fSAndroid Build Coastguard Worker  * set.
422*6a54128fSAndroid Build Coastguard Worker  */
jbd2_journal_cancel_revoke(handle_t * handle,struct journal_head * jh)423*6a54128fSAndroid Build Coastguard Worker int jbd2_journal_cancel_revoke(handle_t *handle, struct journal_head *jh)
424*6a54128fSAndroid Build Coastguard Worker {
425*6a54128fSAndroid Build Coastguard Worker 	struct jbd2_revoke_record_s *record;
426*6a54128fSAndroid Build Coastguard Worker 	journal_t *journal = handle->h_transaction->t_journal;
427*6a54128fSAndroid Build Coastguard Worker 	int need_cancel;
428*6a54128fSAndroid Build Coastguard Worker 	int did_revoke = 0;	/* akpm: debug */
429*6a54128fSAndroid Build Coastguard Worker 	struct buffer_head *bh = jh2bh(jh);
430*6a54128fSAndroid Build Coastguard Worker 
431*6a54128fSAndroid Build Coastguard Worker 	jbd_debug(4, "journal_head %p, cancelling revoke\n", jh);
432*6a54128fSAndroid Build Coastguard Worker 
433*6a54128fSAndroid Build Coastguard Worker 	/* Is the existing Revoke bit valid?  If so, we trust it, and
434*6a54128fSAndroid Build Coastguard Worker 	 * only perform the full cancel if the revoke bit is set.  If
435*6a54128fSAndroid Build Coastguard Worker 	 * not, we can't trust the revoke bit, and we need to do the
436*6a54128fSAndroid Build Coastguard Worker 	 * full search for a revoke record. */
437*6a54128fSAndroid Build Coastguard Worker 	if (test_set_buffer_revokevalid(bh)) {
438*6a54128fSAndroid Build Coastguard Worker 		need_cancel = test_clear_buffer_revoked(bh);
439*6a54128fSAndroid Build Coastguard Worker 	} else {
440*6a54128fSAndroid Build Coastguard Worker 		need_cancel = 1;
441*6a54128fSAndroid Build Coastguard Worker 		clear_buffer_revoked(bh);
442*6a54128fSAndroid Build Coastguard Worker 	}
443*6a54128fSAndroid Build Coastguard Worker 
444*6a54128fSAndroid Build Coastguard Worker 	if (need_cancel) {
445*6a54128fSAndroid Build Coastguard Worker 		record = find_revoke_record(journal, bh->b_blocknr);
446*6a54128fSAndroid Build Coastguard Worker 		if (record) {
447*6a54128fSAndroid Build Coastguard Worker 			jbd_debug(4, "cancelled existing revoke on "
448*6a54128fSAndroid Build Coastguard Worker 				  "blocknr %llu\n", (unsigned long long)bh->b_blocknr);
449*6a54128fSAndroid Build Coastguard Worker 			spin_lock(&journal->j_revoke_lock);
450*6a54128fSAndroid Build Coastguard Worker 			list_del(&record->hash);
451*6a54128fSAndroid Build Coastguard Worker 			spin_unlock(&journal->j_revoke_lock);
452*6a54128fSAndroid Build Coastguard Worker 			kmem_cache_free(jbd2_revoke_record_cache, record);
453*6a54128fSAndroid Build Coastguard Worker 			did_revoke = 1;
454*6a54128fSAndroid Build Coastguard Worker 		}
455*6a54128fSAndroid Build Coastguard Worker 	}
456*6a54128fSAndroid Build Coastguard Worker 
457*6a54128fSAndroid Build Coastguard Worker #ifdef JBD2_EXPENSIVE_CHECKING
458*6a54128fSAndroid Build Coastguard Worker 	/* There better not be one left behind by now! */
459*6a54128fSAndroid Build Coastguard Worker 	record = find_revoke_record(journal, bh->b_blocknr);
460*6a54128fSAndroid Build Coastguard Worker 	J_ASSERT_JH(jh, record == NULL);
461*6a54128fSAndroid Build Coastguard Worker #endif
462*6a54128fSAndroid Build Coastguard Worker 
463*6a54128fSAndroid Build Coastguard Worker 	/* Finally, have we just cleared revoke on an unhashed
464*6a54128fSAndroid Build Coastguard Worker 	 * buffer_head?  If so, we'd better make sure we clear the
465*6a54128fSAndroid Build Coastguard Worker 	 * revoked status on any hashed alias too, otherwise the revoke
466*6a54128fSAndroid Build Coastguard Worker 	 * state machine will get very upset later on. */
467*6a54128fSAndroid Build Coastguard Worker 	if (need_cancel) {
468*6a54128fSAndroid Build Coastguard Worker 		struct buffer_head *bh2;
469*6a54128fSAndroid Build Coastguard Worker 		bh2 = __find_get_block(bh->b_bdev, bh->b_blocknr, bh->b_size);
470*6a54128fSAndroid Build Coastguard Worker 		if (bh2) {
471*6a54128fSAndroid Build Coastguard Worker 			if (bh2 != bh)
472*6a54128fSAndroid Build Coastguard Worker 				clear_buffer_revoked(bh2);
473*6a54128fSAndroid Build Coastguard Worker 			__brelse(bh2);
474*6a54128fSAndroid Build Coastguard Worker 		}
475*6a54128fSAndroid Build Coastguard Worker 	}
476*6a54128fSAndroid Build Coastguard Worker 	return did_revoke;
477*6a54128fSAndroid Build Coastguard Worker }
478*6a54128fSAndroid Build Coastguard Worker 
479*6a54128fSAndroid Build Coastguard Worker /*
480*6a54128fSAndroid Build Coastguard Worker  * journal_clear_revoked_flag clears revoked flag of buffers in
481*6a54128fSAndroid Build Coastguard Worker  * revoke table to reflect there is no revoked buffers in the next
482*6a54128fSAndroid Build Coastguard Worker  * transaction which is going to be started.
483*6a54128fSAndroid Build Coastguard Worker  */
jbd2_clear_buffer_revoked_flags(journal_t * journal)484*6a54128fSAndroid Build Coastguard Worker void jbd2_clear_buffer_revoked_flags(journal_t *journal)
485*6a54128fSAndroid Build Coastguard Worker {
486*6a54128fSAndroid Build Coastguard Worker 	struct jbd2_revoke_table_s *revoke = journal->j_revoke;
487*6a54128fSAndroid Build Coastguard Worker 	int i = 0;
488*6a54128fSAndroid Build Coastguard Worker 
489*6a54128fSAndroid Build Coastguard Worker 	for (i = 0; i < revoke->hash_size; i++) {
490*6a54128fSAndroid Build Coastguard Worker 		struct list_head *hash_list;
491*6a54128fSAndroid Build Coastguard Worker 		struct list_head *list_entry;
492*6a54128fSAndroid Build Coastguard Worker 		hash_list = &revoke->hash_table[i];
493*6a54128fSAndroid Build Coastguard Worker 
494*6a54128fSAndroid Build Coastguard Worker 		list_for_each(list_entry, hash_list) {
495*6a54128fSAndroid Build Coastguard Worker 			struct jbd2_revoke_record_s *record;
496*6a54128fSAndroid Build Coastguard Worker 			struct buffer_head *bh;
497*6a54128fSAndroid Build Coastguard Worker 			record = (struct jbd2_revoke_record_s *)list_entry;
498*6a54128fSAndroid Build Coastguard Worker 			bh = __find_get_block(journal->j_fs_dev,
499*6a54128fSAndroid Build Coastguard Worker 					      record->blocknr,
500*6a54128fSAndroid Build Coastguard Worker 					      journal->j_blocksize);
501*6a54128fSAndroid Build Coastguard Worker 			if (bh) {
502*6a54128fSAndroid Build Coastguard Worker 				clear_buffer_revoked(bh);
503*6a54128fSAndroid Build Coastguard Worker 				__brelse(bh);
504*6a54128fSAndroid Build Coastguard Worker 			}
505*6a54128fSAndroid Build Coastguard Worker 		}
506*6a54128fSAndroid Build Coastguard Worker 	}
507*6a54128fSAndroid Build Coastguard Worker }
508*6a54128fSAndroid Build Coastguard Worker 
509*6a54128fSAndroid Build Coastguard Worker /* journal_switch_revoke table select j_revoke for next transaction
510*6a54128fSAndroid Build Coastguard Worker  * we do not want to suspend any processing until all revokes are
511*6a54128fSAndroid Build Coastguard Worker  * written -bzzz
512*6a54128fSAndroid Build Coastguard Worker  */
jbd2_journal_switch_revoke_table(journal_t * journal)513*6a54128fSAndroid Build Coastguard Worker void jbd2_journal_switch_revoke_table(journal_t *journal)
514*6a54128fSAndroid Build Coastguard Worker {
515*6a54128fSAndroid Build Coastguard Worker 	int i;
516*6a54128fSAndroid Build Coastguard Worker 
517*6a54128fSAndroid Build Coastguard Worker 	if (journal->j_revoke == journal->j_revoke_table[0])
518*6a54128fSAndroid Build Coastguard Worker 		journal->j_revoke = journal->j_revoke_table[1];
519*6a54128fSAndroid Build Coastguard Worker 	else
520*6a54128fSAndroid Build Coastguard Worker 		journal->j_revoke = journal->j_revoke_table[0];
521*6a54128fSAndroid Build Coastguard Worker 
522*6a54128fSAndroid Build Coastguard Worker 	for (i = 0; i < journal->j_revoke->hash_size; i++)
523*6a54128fSAndroid Build Coastguard Worker 		INIT_LIST_HEAD(&journal->j_revoke->hash_table[i]);
524*6a54128fSAndroid Build Coastguard Worker }
525*6a54128fSAndroid Build Coastguard Worker 
526*6a54128fSAndroid Build Coastguard Worker /*
527*6a54128fSAndroid Build Coastguard Worker  * Write revoke records to the journal for all entries in the current
528*6a54128fSAndroid Build Coastguard Worker  * revoke hash, deleting the entries as we go.
529*6a54128fSAndroid Build Coastguard Worker  */
jbd2_journal_write_revoke_records(transaction_t * transaction,struct list_head * log_bufs)530*6a54128fSAndroid Build Coastguard Worker void jbd2_journal_write_revoke_records(transaction_t *transaction,
531*6a54128fSAndroid Build Coastguard Worker 				       struct list_head *log_bufs)
532*6a54128fSAndroid Build Coastguard Worker {
533*6a54128fSAndroid Build Coastguard Worker 	journal_t *journal = transaction->t_journal;
534*6a54128fSAndroid Build Coastguard Worker 	struct buffer_head *descriptor;
535*6a54128fSAndroid Build Coastguard Worker 	struct jbd2_revoke_record_s *record;
536*6a54128fSAndroid Build Coastguard Worker 	struct jbd2_revoke_table_s *revoke;
537*6a54128fSAndroid Build Coastguard Worker 	struct list_head *hash_list;
538*6a54128fSAndroid Build Coastguard Worker 	int i, offset, count;
539*6a54128fSAndroid Build Coastguard Worker 
540*6a54128fSAndroid Build Coastguard Worker 	descriptor = NULL;
541*6a54128fSAndroid Build Coastguard Worker 	offset = 0;
542*6a54128fSAndroid Build Coastguard Worker 	count = 0;
543*6a54128fSAndroid Build Coastguard Worker 
544*6a54128fSAndroid Build Coastguard Worker 	/* select revoke table for committing transaction */
545*6a54128fSAndroid Build Coastguard Worker 	revoke = journal->j_revoke == journal->j_revoke_table[0] ?
546*6a54128fSAndroid Build Coastguard Worker 		journal->j_revoke_table[1] : journal->j_revoke_table[0];
547*6a54128fSAndroid Build Coastguard Worker 
548*6a54128fSAndroid Build Coastguard Worker 	for (i = 0; i < revoke->hash_size; i++) {
549*6a54128fSAndroid Build Coastguard Worker 		hash_list = &revoke->hash_table[i];
550*6a54128fSAndroid Build Coastguard Worker 
551*6a54128fSAndroid Build Coastguard Worker 		while (!list_empty(hash_list)) {
552*6a54128fSAndroid Build Coastguard Worker 			record = (struct jbd2_revoke_record_s *)
553*6a54128fSAndroid Build Coastguard Worker 				hash_list->next;
554*6a54128fSAndroid Build Coastguard Worker 			write_one_revoke_record(transaction, log_bufs,
555*6a54128fSAndroid Build Coastguard Worker 						&descriptor, &offset, record);
556*6a54128fSAndroid Build Coastguard Worker 			count++;
557*6a54128fSAndroid Build Coastguard Worker 			list_del(&record->hash);
558*6a54128fSAndroid Build Coastguard Worker 			kmem_cache_free(jbd2_revoke_record_cache, record);
559*6a54128fSAndroid Build Coastguard Worker 		}
560*6a54128fSAndroid Build Coastguard Worker 	}
561*6a54128fSAndroid Build Coastguard Worker 	if (descriptor)
562*6a54128fSAndroid Build Coastguard Worker 		flush_descriptor(journal, descriptor, offset);
563*6a54128fSAndroid Build Coastguard Worker 	jbd_debug(1, "Wrote %d revoke records\n", count);
564*6a54128fSAndroid Build Coastguard Worker }
565*6a54128fSAndroid Build Coastguard Worker 
566*6a54128fSAndroid Build Coastguard Worker /*
567*6a54128fSAndroid Build Coastguard Worker  * Write out one revoke record.  We need to create a new descriptor
568*6a54128fSAndroid Build Coastguard Worker  * block if the old one is full or if we have not already created one.
569*6a54128fSAndroid Build Coastguard Worker  */
570*6a54128fSAndroid Build Coastguard Worker 
write_one_revoke_record(transaction_t * transaction,struct list_head * log_bufs,struct buffer_head ** descriptorp,int * offsetp,struct jbd2_revoke_record_s * record)571*6a54128fSAndroid Build Coastguard Worker static void write_one_revoke_record(transaction_t *transaction,
572*6a54128fSAndroid Build Coastguard Worker 				    struct list_head *log_bufs,
573*6a54128fSAndroid Build Coastguard Worker 				    struct buffer_head **descriptorp,
574*6a54128fSAndroid Build Coastguard Worker 				    int *offsetp,
575*6a54128fSAndroid Build Coastguard Worker 				    struct jbd2_revoke_record_s *record)
576*6a54128fSAndroid Build Coastguard Worker {
577*6a54128fSAndroid Build Coastguard Worker 	journal_t *journal = transaction->t_journal;
578*6a54128fSAndroid Build Coastguard Worker 	int csum_size = 0;
579*6a54128fSAndroid Build Coastguard Worker 	struct buffer_head *descriptor;
580*6a54128fSAndroid Build Coastguard Worker 	int sz, offset;
581*6a54128fSAndroid Build Coastguard Worker 
582*6a54128fSAndroid Build Coastguard Worker 	/* If we are already aborting, this all becomes a noop.  We
583*6a54128fSAndroid Build Coastguard Worker            still need to go round the loop in
584*6a54128fSAndroid Build Coastguard Worker            jbd2_journal_write_revoke_records in order to free all of the
585*6a54128fSAndroid Build Coastguard Worker            revoke records: only the IO to the journal is omitted. */
586*6a54128fSAndroid Build Coastguard Worker 	if (is_journal_aborted(journal))
587*6a54128fSAndroid Build Coastguard Worker 		return;
588*6a54128fSAndroid Build Coastguard Worker 
589*6a54128fSAndroid Build Coastguard Worker 	descriptor = *descriptorp;
590*6a54128fSAndroid Build Coastguard Worker 	offset = *offsetp;
591*6a54128fSAndroid Build Coastguard Worker 
592*6a54128fSAndroid Build Coastguard Worker 	/* Do we need to leave space at the end for a checksum? */
593*6a54128fSAndroid Build Coastguard Worker 	if (jbd2_journal_has_csum_v2or3(journal))
594*6a54128fSAndroid Build Coastguard Worker 		csum_size = sizeof(struct jbd2_journal_block_tail);
595*6a54128fSAndroid Build Coastguard Worker 
596*6a54128fSAndroid Build Coastguard Worker 	if (jbd2_has_feature_64bit(journal))
597*6a54128fSAndroid Build Coastguard Worker 		sz = 8;
598*6a54128fSAndroid Build Coastguard Worker 	else
599*6a54128fSAndroid Build Coastguard Worker 		sz = 4;
600*6a54128fSAndroid Build Coastguard Worker 
601*6a54128fSAndroid Build Coastguard Worker 	/* Make sure we have a descriptor with space left for the record */
602*6a54128fSAndroid Build Coastguard Worker 	if (descriptor) {
603*6a54128fSAndroid Build Coastguard Worker 		if (offset + sz > journal->j_blocksize - csum_size) {
604*6a54128fSAndroid Build Coastguard Worker 			flush_descriptor(journal, descriptor, offset);
605*6a54128fSAndroid Build Coastguard Worker 			descriptor = NULL;
606*6a54128fSAndroid Build Coastguard Worker 		}
607*6a54128fSAndroid Build Coastguard Worker 	}
608*6a54128fSAndroid Build Coastguard Worker 
609*6a54128fSAndroid Build Coastguard Worker 	if (!descriptor) {
610*6a54128fSAndroid Build Coastguard Worker 		descriptor = jbd2_journal_get_descriptor_buffer(transaction,
611*6a54128fSAndroid Build Coastguard Worker 							JBD2_REVOKE_BLOCK);
612*6a54128fSAndroid Build Coastguard Worker 		if (!descriptor)
613*6a54128fSAndroid Build Coastguard Worker 			return;
614*6a54128fSAndroid Build Coastguard Worker 
615*6a54128fSAndroid Build Coastguard Worker 		/* Record it so that we can wait for IO completion later */
616*6a54128fSAndroid Build Coastguard Worker 		BUFFER_TRACE(descriptor, "file in log_bufs");
617*6a54128fSAndroid Build Coastguard Worker 		jbd2_file_log_bh(log_bufs, descriptor);
618*6a54128fSAndroid Build Coastguard Worker 
619*6a54128fSAndroid Build Coastguard Worker 		offset = sizeof(jbd2_journal_revoke_header_t);
620*6a54128fSAndroid Build Coastguard Worker 		*descriptorp = descriptor;
621*6a54128fSAndroid Build Coastguard Worker 	}
622*6a54128fSAndroid Build Coastguard Worker 
623*6a54128fSAndroid Build Coastguard Worker 	if (jbd2_has_feature_64bit(journal))
624*6a54128fSAndroid Build Coastguard Worker 		* ((__be64 *)(&descriptor->b_data[offset])) =
625*6a54128fSAndroid Build Coastguard Worker 			cpu_to_be64(record->blocknr);
626*6a54128fSAndroid Build Coastguard Worker 	else
627*6a54128fSAndroid Build Coastguard Worker 		* ((__be32 *)(&descriptor->b_data[offset])) =
628*6a54128fSAndroid Build Coastguard Worker 			cpu_to_be32(record->blocknr);
629*6a54128fSAndroid Build Coastguard Worker 	offset += sz;
630*6a54128fSAndroid Build Coastguard Worker 
631*6a54128fSAndroid Build Coastguard Worker 	*offsetp = offset;
632*6a54128fSAndroid Build Coastguard Worker }
633*6a54128fSAndroid Build Coastguard Worker 
634*6a54128fSAndroid Build Coastguard Worker /*
635*6a54128fSAndroid Build Coastguard Worker  * Flush a revoke descriptor out to the journal.  If we are aborting,
636*6a54128fSAndroid Build Coastguard Worker  * this is a noop; otherwise we are generating a buffer which needs to
637*6a54128fSAndroid Build Coastguard Worker  * be waited for during commit, so it has to go onto the appropriate
638*6a54128fSAndroid Build Coastguard Worker  * journal buffer list.
639*6a54128fSAndroid Build Coastguard Worker  */
640*6a54128fSAndroid Build Coastguard Worker 
flush_descriptor(journal_t * journal,struct buffer_head * descriptor,int offset)641*6a54128fSAndroid Build Coastguard Worker static void flush_descriptor(journal_t *journal,
642*6a54128fSAndroid Build Coastguard Worker 			     struct buffer_head *descriptor,
643*6a54128fSAndroid Build Coastguard Worker 			     int offset)
644*6a54128fSAndroid Build Coastguard Worker {
645*6a54128fSAndroid Build Coastguard Worker 	jbd2_journal_revoke_header_t *header;
646*6a54128fSAndroid Build Coastguard Worker 
647*6a54128fSAndroid Build Coastguard Worker 	if (is_journal_aborted(journal))
648*6a54128fSAndroid Build Coastguard Worker 		return;
649*6a54128fSAndroid Build Coastguard Worker 
650*6a54128fSAndroid Build Coastguard Worker 	header = (jbd2_journal_revoke_header_t *)descriptor->b_data;
651*6a54128fSAndroid Build Coastguard Worker 	header->r_count = cpu_to_be32(offset);
652*6a54128fSAndroid Build Coastguard Worker 	jbd2_descriptor_block_csum_set(journal, descriptor);
653*6a54128fSAndroid Build Coastguard Worker 
654*6a54128fSAndroid Build Coastguard Worker 	set_buffer_jwrite(descriptor);
655*6a54128fSAndroid Build Coastguard Worker 	BUFFER_TRACE(descriptor, "write");
656*6a54128fSAndroid Build Coastguard Worker 	set_buffer_dirty(descriptor);
657*6a54128fSAndroid Build Coastguard Worker 	write_dirty_buffer(descriptor, REQ_SYNC);
658*6a54128fSAndroid Build Coastguard Worker }
659*6a54128fSAndroid Build Coastguard Worker #endif
660*6a54128fSAndroid Build Coastguard Worker 
661*6a54128fSAndroid Build Coastguard Worker /*
662*6a54128fSAndroid Build Coastguard Worker  * Revoke support for recovery.
663*6a54128fSAndroid Build Coastguard Worker  *
664*6a54128fSAndroid Build Coastguard Worker  * Recovery needs to be able to:
665*6a54128fSAndroid Build Coastguard Worker  *
666*6a54128fSAndroid Build Coastguard Worker  *  record all revoke records, including the tid of the latest instance
667*6a54128fSAndroid Build Coastguard Worker  *  of each revoke in the journal
668*6a54128fSAndroid Build Coastguard Worker  *
669*6a54128fSAndroid Build Coastguard Worker  *  check whether a given block in a given transaction should be replayed
670*6a54128fSAndroid Build Coastguard Worker  *  (ie. has not been revoked by a revoke record in that or a subsequent
671*6a54128fSAndroid Build Coastguard Worker  *  transaction)
672*6a54128fSAndroid Build Coastguard Worker  *
673*6a54128fSAndroid Build Coastguard Worker  *  empty the revoke table after recovery.
674*6a54128fSAndroid Build Coastguard Worker  */
675*6a54128fSAndroid Build Coastguard Worker 
676*6a54128fSAndroid Build Coastguard Worker /*
677*6a54128fSAndroid Build Coastguard Worker  * First, setting revoke records.  We create a new revoke record for
678*6a54128fSAndroid Build Coastguard Worker  * every block ever revoked in the log as we scan it for recovery, and
679*6a54128fSAndroid Build Coastguard Worker  * we update the existing records if we find multiple revokes for a
680*6a54128fSAndroid Build Coastguard Worker  * single block.
681*6a54128fSAndroid Build Coastguard Worker  */
682*6a54128fSAndroid Build Coastguard Worker 
jbd2_journal_set_revoke(journal_t * journal,unsigned long long blocknr,tid_t sequence)683*6a54128fSAndroid Build Coastguard Worker int jbd2_journal_set_revoke(journal_t *journal,
684*6a54128fSAndroid Build Coastguard Worker 		       unsigned long long blocknr,
685*6a54128fSAndroid Build Coastguard Worker 		       tid_t sequence)
686*6a54128fSAndroid Build Coastguard Worker {
687*6a54128fSAndroid Build Coastguard Worker 	struct jbd2_revoke_record_s *record;
688*6a54128fSAndroid Build Coastguard Worker 
689*6a54128fSAndroid Build Coastguard Worker 	record = find_revoke_record(journal, blocknr);
690*6a54128fSAndroid Build Coastguard Worker 	if (record) {
691*6a54128fSAndroid Build Coastguard Worker 		/* If we have multiple occurrences, only record the
692*6a54128fSAndroid Build Coastguard Worker 		 * latest sequence number in the hashed record */
693*6a54128fSAndroid Build Coastguard Worker 		if (tid_gt(sequence, record->sequence))
694*6a54128fSAndroid Build Coastguard Worker 			record->sequence = sequence;
695*6a54128fSAndroid Build Coastguard Worker 		return 0;
696*6a54128fSAndroid Build Coastguard Worker 	}
697*6a54128fSAndroid Build Coastguard Worker 	return insert_revoke_hash(journal, blocknr, sequence);
698*6a54128fSAndroid Build Coastguard Worker }
699*6a54128fSAndroid Build Coastguard Worker 
700*6a54128fSAndroid Build Coastguard Worker /*
701*6a54128fSAndroid Build Coastguard Worker  * Test revoke records.  For a given block referenced in the log, has
702*6a54128fSAndroid Build Coastguard Worker  * that block been revoked?  A revoke record with a given transaction
703*6a54128fSAndroid Build Coastguard Worker  * sequence number revokes all blocks in that transaction and earlier
704*6a54128fSAndroid Build Coastguard Worker  * ones, but later transactions still need replayed.
705*6a54128fSAndroid Build Coastguard Worker  */
706*6a54128fSAndroid Build Coastguard Worker 
jbd2_journal_test_revoke(journal_t * journal,unsigned long long blocknr,tid_t sequence)707*6a54128fSAndroid Build Coastguard Worker int jbd2_journal_test_revoke(journal_t *journal,
708*6a54128fSAndroid Build Coastguard Worker 			unsigned long long blocknr,
709*6a54128fSAndroid Build Coastguard Worker 			tid_t sequence)
710*6a54128fSAndroid Build Coastguard Worker {
711*6a54128fSAndroid Build Coastguard Worker 	struct jbd2_revoke_record_s *record;
712*6a54128fSAndroid Build Coastguard Worker 
713*6a54128fSAndroid Build Coastguard Worker 	record = find_revoke_record(journal, blocknr);
714*6a54128fSAndroid Build Coastguard Worker 	if (!record)
715*6a54128fSAndroid Build Coastguard Worker 		return 0;
716*6a54128fSAndroid Build Coastguard Worker 	if (tid_gt(sequence, record->sequence))
717*6a54128fSAndroid Build Coastguard Worker 		return 0;
718*6a54128fSAndroid Build Coastguard Worker 	return 1;
719*6a54128fSAndroid Build Coastguard Worker }
720*6a54128fSAndroid Build Coastguard Worker 
721*6a54128fSAndroid Build Coastguard Worker /*
722*6a54128fSAndroid Build Coastguard Worker  * Finally, once recovery is over, we need to clear the revoke table so
723*6a54128fSAndroid Build Coastguard Worker  * that it can be reused by the running filesystem.
724*6a54128fSAndroid Build Coastguard Worker  */
725*6a54128fSAndroid Build Coastguard Worker 
jbd2_journal_clear_revoke(journal_t * journal)726*6a54128fSAndroid Build Coastguard Worker void jbd2_journal_clear_revoke(journal_t *journal)
727*6a54128fSAndroid Build Coastguard Worker {
728*6a54128fSAndroid Build Coastguard Worker 	int i;
729*6a54128fSAndroid Build Coastguard Worker 	struct list_head *hash_list;
730*6a54128fSAndroid Build Coastguard Worker 	struct jbd2_revoke_record_s *record;
731*6a54128fSAndroid Build Coastguard Worker 	struct jbd2_revoke_table_s *revoke;
732*6a54128fSAndroid Build Coastguard Worker 
733*6a54128fSAndroid Build Coastguard Worker 	revoke = journal->j_revoke;
734*6a54128fSAndroid Build Coastguard Worker 
735*6a54128fSAndroid Build Coastguard Worker 	for (i = 0; i < revoke->hash_size; i++) {
736*6a54128fSAndroid Build Coastguard Worker 		hash_list = &revoke->hash_table[i];
737*6a54128fSAndroid Build Coastguard Worker 		while (!list_empty(hash_list)) {
738*6a54128fSAndroid Build Coastguard Worker 			record = (struct jbd2_revoke_record_s*) hash_list->next;
739*6a54128fSAndroid Build Coastguard Worker 			list_del(&record->hash);
740*6a54128fSAndroid Build Coastguard Worker 			kmem_cache_free(jbd2_revoke_record_cache, record);
741*6a54128fSAndroid Build Coastguard Worker 		}
742*6a54128fSAndroid Build Coastguard Worker 	}
743*6a54128fSAndroid Build Coastguard Worker }
744