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