1/*
2 * linux/include/linux/jbd.h
3 *
4 * Written by Stephen C. Tweedie <sct@redhat.com>
5 *
6 * Copyright 1998-2000 Red Hat, Inc --- 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 * Definitions for transaction data structures for the buffer cache
13 * filesystem journaling support.
14 */
15
16#ifndef _LINUX_JBD_H
17#define _LINUX_JBD_H
18
19#if defined(CONFIG_JBD) || defined(CONFIG_JBD_MODULE) || !defined(__KERNEL__)
20
21/* Allow this file to be included directly into e2fsprogs */
22#ifndef __KERNEL__
23#include "jfs_compat.h"
24#define JFS_DEBUG
25#define jfs_debug jbd_debug
26#else
27
28#include <linux/journal-head.h>
29#include <linux/stddef.h>
30#include <asm/semaphore.h>
31#endif
32
33#ifndef __GNUC__
34#define __FUNCTION__ ""
35#endif
36
37#define journal_oom_retry 1
38
39#ifdef __STDC__
40#ifdef CONFIG_JBD_DEBUG
41/*
42 * Define JBD_EXPENSIVE_CHECKING to enable more expensive internal
43 * consistency checks.  By default we don't do this unless
44 * CONFIG_JBD_DEBUG is on.
45 */
46#define JBD_EXPENSIVE_CHECKING
47extern int journal_enable_debug;
48
49#define jbd_debug(n, f, a...)						\
50	do {								\
51		if ((n) <= journal_enable_debug) {			\
52			printk (KERN_DEBUG "(%s, %d): %s: ",		\
53				__FILE__, __LINE__, __FUNCTION__);	\
54		  	printk (f, ## a);				\
55		}							\
56	} while (0)
57#else
58#ifdef __GNUC__
59#define jbd_debug(f, a...)	/**/
60#else
61#define jbd_debug(f, ...)	/**/
62#endif
63#endif
64#else
65#define jbd_debug(x)		/* AIX doesn't do STDC */
66#endif
67
68extern void * __jbd_kmalloc (char *where, size_t size, int flags, int retry);
69#define jbd_kmalloc(size, flags) \
70	__jbd_kmalloc(__FUNCTION__, (size), (flags), journal_oom_retry)
71#define jbd_rep_kmalloc(size, flags) \
72	__jbd_kmalloc(__FUNCTION__, (size), (flags), 1)
73
74#define JFS_MIN_JOURNAL_BLOCKS 1024
75
76#ifdef __KERNEL__
77typedef struct handle_s		handle_t;	/* Atomic operation type */
78typedef struct journal_s	journal_t;	/* Journal control structure */
79#endif
80
81/*
82 * Internal structures used by the logging mechanism:
83 */
84
85#define JFS_MAGIC_NUMBER 0xc03b3998U /* The first 4 bytes of /dev/random! */
86
87/*
88 * On-disk structures
89 */
90
91/*
92 * Descriptor block types:
93 */
94
95#define JFS_DESCRIPTOR_BLOCK	1
96#define JFS_COMMIT_BLOCK	2
97#define JFS_SUPERBLOCK_V1	3
98#define JFS_SUPERBLOCK_V2	4
99#define JFS_REVOKE_BLOCK	5
100
101/*
102 * Standard header for all descriptor blocks:
103 */
104typedef struct journal_header_s
105{
106	__u32		h_magic;
107	__u32		h_blocktype;
108	__u32		h_sequence;
109} journal_header_t;
110
111/*
112 * Checksum types.
113 */
114#define JBD2_CRC32_CHKSUM   1
115#define JBD2_MD5_CHKSUM     2
116#define JBD2_SHA1_CHKSUM    3
117
118#define JBD2_CRC32_CHKSUM_SIZE 4
119
120#define JBD2_CHECKSUM_BYTES (32 / sizeof(__u32))
121/*
122 * Commit block header for storing transactional checksums:
123 */
124struct commit_header {
125	__u32		h_magic;
126	__u32		h_blocktype;
127	__u32		h_sequence;
128	unsigned char	h_chksum_type;
129	unsigned char	h_chksum_size;
130	unsigned char	h_padding[2];
131	__u32		h_chksum[JBD2_CHECKSUM_BYTES];
132	__u64		h_commit_sec;
133	__u32		h_commit_nsec;
134};
135
136/*
137 * The block tag: used to describe a single buffer in the journal
138 */
139typedef struct journal_block_tag_s
140{
141	__u32		t_blocknr;	/* The on-disk block number */
142	__u32		t_flags;	/* See below */
143	__u32		t_blocknr_high; /* most-significant high 32bits. */
144} journal_block_tag_t;
145
146#define JBD_TAG_SIZE64 (sizeof(journal_block_tag_t))
147#define JBD_TAG_SIZE32 (8)
148
149/*
150 * The revoke descriptor: used on disk to describe a series of blocks to
151 * be revoked from the log
152 */
153typedef struct journal_revoke_header_s
154{
155	journal_header_t r_header;
156	int		 r_count;	/* Count of bytes used in the block */
157} journal_revoke_header_t;
158
159
160/* Definitions for the journal tag flags word: */
161#define JFS_FLAG_ESCAPE		1	/* on-disk block is escaped */
162#define JFS_FLAG_SAME_UUID	2	/* block has same uuid as previous */
163#define JFS_FLAG_DELETED	4	/* block deleted by this transaction */
164#define JFS_FLAG_LAST_TAG	8	/* last tag in this descriptor block */
165
166
167/*
168 * The journal superblock.  All fields are in big-endian byte order.
169 */
170typedef struct journal_superblock_s
171{
172/* 0x0000 */
173	journal_header_t s_header;
174
175/* 0x000C */
176	/* Static information describing the journal */
177	__u32	s_blocksize;		/* journal device blocksize */
178	__u32	s_maxlen;		/* total blocks in journal file */
179	__u32	s_first;		/* first block of log information */
180
181/* 0x0018 */
182	/* Dynamic information describing the current state of the log */
183	__u32	s_sequence;		/* first commit ID expected in log */
184	__u32	s_start;		/* blocknr of start of log */
185
186/* 0x0020 */
187	/* Error value, as set by journal_abort(). */
188	__s32	s_errno;
189
190/* 0x0024 */
191	/* Remaining fields are only valid in a version-2 superblock */
192	__u32	s_feature_compat; 	/* compatible feature set */
193	__u32	s_feature_incompat; 	/* incompatible feature set */
194	__u32	s_feature_ro_compat; 	/* readonly-compatible feature set */
195/* 0x0030 */
196	__u8	s_uuid[16];		/* 128-bit uuid for journal */
197
198/* 0x0040 */
199	__u32	s_nr_users;		/* Nr of filesystems sharing log */
200
201	__u32	s_dynsuper;		/* Blocknr of dynamic superblock copy*/
202
203/* 0x0048 */
204	__u32	s_max_transaction;	/* Limit of journal blocks per trans.*/
205	__u32	s_max_trans_data;	/* Limit of data blocks per trans. */
206
207/* 0x0050 */
208	__u32	s_padding[44];
209
210/* 0x0100 */
211	__u8	s_users[16*48];		/* ids of all fs'es sharing the log */
212/* 0x0400 */
213} journal_superblock_t;
214
215#define JFS_HAS_COMPAT_FEATURE(j,mask)					\
216	((j)->j_format_version >= 2 &&					\
217	 ((j)->j_superblock->s_feature_compat & cpu_to_be32((mask))))
218#define JFS_HAS_RO_COMPAT_FEATURE(j,mask)				\
219	((j)->j_format_version >= 2 &&					\
220	 ((j)->j_superblock->s_feature_ro_compat & cpu_to_be32((mask))))
221#define JFS_HAS_INCOMPAT_FEATURE(j,mask)				\
222	((j)->j_format_version >= 2 &&					\
223	 ((j)->j_superblock->s_feature_incompat & cpu_to_be32((mask))))
224
225#define JFS_FEATURE_COMPAT_CHECKSUM	0x00000001
226
227#define JFS_FEATURE_INCOMPAT_REVOKE	0x00000001
228
229#define JFS_FEATURE_INCOMPAT_REVOKE		0x00000001
230#define JFS_FEATURE_INCOMPAT_64BIT		0x00000002
231#define JFS_FEATURE_INCOMPAT_ASYNC_COMMIT	0x00000004
232
233/* Features known to this kernel version: */
234#define JFS_KNOWN_COMPAT_FEATURES	0
235#define JFS_KNOWN_ROCOMPAT_FEATURES	0
236#define JFS_KNOWN_INCOMPAT_FEATURES	(JFS_FEATURE_INCOMPAT_REVOKE|\
237					 JFS_FEATURE_INCOMPAT_ASYNC_COMMIT)
238
239#ifdef __KERNEL__
240
241#include <linux/fs.h>
242#include <linux/sched.h>
243
244#define JBD_ASSERTIONS
245#ifdef JBD_ASSERTIONS
246#define J_ASSERT(assert)						\
247do {									\
248	if (!(assert)) {						\
249		printk (KERN_EMERG					\
250			"Assertion failure in %s() at %s:%d: \"%s\"\n",	\
251			__FUNCTION__, __FILE__, __LINE__, # assert);	\
252		BUG();							\
253	}								\
254} while (0)
255
256#if defined(CONFIG_BUFFER_DEBUG)
257void buffer_assertion_failure(struct buffer_head *bh);
258#define J_ASSERT_BH(bh, expr)						\
259	do {								\
260		if (!(expr))						\
261			buffer_assertion_failure(bh);			\
262		J_ASSERT(expr);						\
263	} while (0)
264#define J_ASSERT_JH(jh, expr)	J_ASSERT_BH(jh2bh(jh), expr)
265#else
266#define J_ASSERT_BH(bh, expr)	J_ASSERT(expr)
267#define J_ASSERT_JH(jh, expr)	J_ASSERT(expr)
268#endif
269
270#else
271#define J_ASSERT(assert)
272#endif		/* JBD_ASSERTIONS */
273
274enum jbd_state_bits {
275	BH_JWrite
276	  = BH_PrivateStart,	/* 1 if being written to log (@@@ DEBUGGING) */
277	BH_Freed,		/* 1 if buffer has been freed (truncated) */
278	BH_Revoked,		/* 1 if buffer has been revoked from the log */
279	BH_RevokeValid,		/* 1 if buffer revoked flag is valid */
280	BH_JBDDirty,		/* 1 if buffer is dirty but journaled */
281};
282
283/* Return true if the buffer is one which JBD is managing */
284static inline int buffer_jbd(struct buffer_head *bh)
285{
286	return __buffer_state(bh, JBD);
287}
288
289static inline struct buffer_head *jh2bh(struct journal_head *jh)
290{
291	return jh->b_bh;
292}
293
294static inline struct journal_head *bh2jh(struct buffer_head *bh)
295{
296	return bh->b_private;
297}
298
299struct jbd_revoke_table_s;
300
301/* The handle_t type represents a single atomic update being performed
302 * by some process.  All filesystem modifications made by the process go
303 * through this handle.  Recursive operations (such as quota operations)
304 * are gathered into a single update.
305 *
306 * The buffer credits field is used to account for journaled buffers
307 * being modified by the running process.  To ensure that there is
308 * enough log space for all outstanding operations, we need to limit the
309 * number of outstanding buffers possible at any time.  When the
310 * operation completes, any buffer credits not used are credited back to
311 * the transaction, so that at all times we know how many buffers the
312 * outstanding updates on a transaction might possibly touch. */
313
314struct handle_s
315{
316	/* Which compound transaction is this update a part of? */
317	transaction_t	      * h_transaction;
318
319	/* Number of remaining buffers we are allowed to dirty: */
320	int			h_buffer_credits;
321
322	/* Reference count on this handle */
323	int			h_ref;
324
325	/* Field for caller's use to track errors through large fs
326	   operations */
327	int			h_err;
328
329	/* Flags */
330	unsigned int	h_sync:		1;	/* sync-on-close */
331	unsigned int	h_jdata:	1;	/* force data journaling */
332	unsigned int	h_aborted:	1;	/* fatal error on handle */
333};
334
335
336/* The transaction_t type is the guts of the journaling mechanism.  It
337 * tracks a compound transaction through its various states:
338 *
339 * RUNNING:	accepting new updates
340 * LOCKED:	Updates still running but we don't accept new ones
341 * RUNDOWN:	Updates are tidying up but have finished requesting
342 *		new buffers to modify (state not used for now)
343 * FLUSH:       All updates complete, but we are still writing to disk
344 * COMMIT:      All data on disk, writing commit record
345 * FINISHED:	We still have to keep the transaction for checkpointing.
346 *
347 * The transaction keeps track of all of the buffers modified by a
348 * running transaction, and all of the buffers committed but not yet
349 * flushed to home for finished transactions.
350 */
351
352struct transaction_s
353{
354	/* Pointer to the journal for this transaction. */
355	journal_t *		t_journal;
356
357	/* Sequence number for this transaction */
358	tid_t			t_tid;
359
360	/* Transaction's current state */
361	enum {
362		T_RUNNING,
363		T_LOCKED,
364		T_RUNDOWN,
365		T_FLUSH,
366		T_COMMIT,
367		T_FINISHED
368	}			t_state;
369
370	/* Where in the log does this transaction's commit start? */
371	unsigned long		t_log_start;
372
373	/* Doubly-linked circular list of all inodes owned by this
374           transaction */	/* AKPM: unused */
375	struct inode *		t_ilist;
376
377	/* Number of buffers on the t_buffers list */
378	int			t_nr_buffers;
379
380	/* Doubly-linked circular list of all buffers reserved but not
381           yet modified by this transaction */
382	struct journal_head *	t_reserved_list;
383
384	/* Doubly-linked circular list of all metadata buffers owned by this
385           transaction */
386	struct journal_head *	t_buffers;
387
388	/*
389	 * Doubly-linked circular list of all data buffers still to be
390	 * flushed before this transaction can be committed.
391	 * Protected by journal_datalist_lock.
392	 */
393	struct journal_head *	t_sync_datalist;
394
395	/*
396	 * Doubly-linked circular list of all writepage data buffers
397	 * still to be written before this transaction can be committed.
398	 * Protected by journal_datalist_lock.
399	 */
400	struct journal_head *	t_async_datalist;
401
402	/* Doubly-linked circular list of all forget buffers (superceded
403           buffers which we can un-checkpoint once this transaction
404           commits) */
405	struct journal_head *	t_forget;
406
407	/*
408	 * Doubly-linked circular list of all buffers still to be
409	 * flushed before this transaction can be checkpointed.
410	 */
411	/* Protected by journal_datalist_lock */
412	struct journal_head *	t_checkpoint_list;
413
414	/* Doubly-linked circular list of temporary buffers currently
415           undergoing IO in the log */
416	struct journal_head *	t_iobuf_list;
417
418	/* Doubly-linked circular list of metadata buffers being
419           shadowed by log IO.  The IO buffers on the iobuf list and the
420           shadow buffers on this list match each other one for one at
421           all times. */
422	struct journal_head *	t_shadow_list;
423
424	/* Doubly-linked circular list of control buffers being written
425           to the log. */
426	struct journal_head *	t_log_list;
427
428	/* Number of outstanding updates running on this transaction */
429	int			t_updates;
430
431	/* Number of buffers reserved for use by all handles in this
432	 * transaction handle but not yet modified. */
433	int			t_outstanding_credits;
434
435	/*
436	 * Forward and backward links for the circular list of all
437	 * transactions awaiting checkpoint.
438	 */
439	/* Protected by journal_datalist_lock */
440	transaction_t		*t_cpnext, *t_cpprev;
441
442	/* When will the transaction expire (become due for commit), in
443	 * jiffies ? */
444	unsigned long		t_expires;
445
446	/* How many handles used this transaction? */
447	int t_handle_count;
448};
449
450
451/* The journal_t maintains all of the journaling state information for a
452 * single filesystem.  It is linked to from the fs superblock structure.
453 *
454 * We use the journal_t to keep track of all outstanding transaction
455 * activity on the filesystem, and to manage the state of the log
456 * writing process. */
457
458struct journal_s
459{
460	/* General journaling state flags */
461	unsigned long		j_flags;
462
463	/* Is there an outstanding uncleared error on the journal (from
464	 * a prior abort)? */
465	int			j_errno;
466
467	/* The superblock buffer */
468	struct buffer_head *	j_sb_buffer;
469	journal_superblock_t *	j_superblock;
470
471	/* Version of the superblock format */
472	int			j_format_version;
473
474	/* Number of processes waiting to create a barrier lock */
475	int			j_barrier_count;
476
477	/* The barrier lock itself */
478	struct semaphore	j_barrier;
479
480	/* Transactions: The current running transaction... */
481	transaction_t *		j_running_transaction;
482
483	/* ... the transaction we are pushing to disk ... */
484	transaction_t *		j_committing_transaction;
485
486	/* ... and a linked circular list of all transactions waiting
487	 * for checkpointing. */
488	/* Protected by journal_datalist_lock */
489	transaction_t *		j_checkpoint_transactions;
490
491	/* Wait queue for waiting for a locked transaction to start
492           committing, or for a barrier lock to be released */
493	wait_queue_head_t	j_wait_transaction_locked;
494
495	/* Wait queue for waiting for checkpointing to complete */
496	wait_queue_head_t	j_wait_logspace;
497
498	/* Wait queue for waiting for commit to complete */
499	wait_queue_head_t	j_wait_done_commit;
500
501	/* Wait queue to trigger checkpointing */
502	wait_queue_head_t	j_wait_checkpoint;
503
504	/* Wait queue to trigger commit */
505	wait_queue_head_t	j_wait_commit;
506
507	/* Wait queue to wait for updates to complete */
508	wait_queue_head_t	j_wait_updates;
509
510	/* Semaphore for locking against concurrent checkpoints */
511	struct semaphore 	j_checkpoint_sem;
512
513	/* The main journal lock, used by lock_journal() */
514	struct semaphore	j_sem;
515
516	/* Journal head: identifies the first unused block in the journal. */
517	unsigned long		j_head;
518
519	/* Journal tail: identifies the oldest still-used block in the
520	 * journal. */
521	unsigned long		j_tail;
522
523	/* Journal free: how many free blocks are there in the journal? */
524	unsigned long		j_free;
525
526	/* Journal start and end: the block numbers of the first usable
527	 * block and one beyond the last usable block in the journal. */
528	unsigned long		j_first, j_last;
529
530	/* Device, blocksize and starting block offset for the location
531	 * where we store the journal. */
532	kdev_t			j_dev;
533	int			j_blocksize;
534	unsigned int		j_blk_offset;
535
536	/* Device which holds the client fs.  For internal journal this
537	 * will be equal to j_dev. */
538	kdev_t			j_fs_dev;
539
540	/* Total maximum capacity of the journal region on disk. */
541	unsigned int		j_maxlen;
542
543	/* Optional inode where we store the journal.  If present, all
544	 * journal block numbers are mapped into this inode via
545	 * bmap(). */
546	struct inode *		j_inode;
547
548	/* Sequence number of the oldest transaction in the log */
549	tid_t			j_tail_sequence;
550	/* Sequence number of the next transaction to grant */
551	tid_t			j_transaction_sequence;
552	/* Sequence number of the most recently committed transaction */
553	tid_t			j_commit_sequence;
554	/* Sequence number of the most recent transaction wanting commit */
555	tid_t			j_commit_request;
556
557	/* Journal uuid: identifies the object (filesystem, LVM volume
558	 * etc) backed by this journal.  This will eventually be
559	 * replaced by an array of uuids, allowing us to index multiple
560	 * devices within a single journal and to perform atomic updates
561	 * across them.  */
562
563	__u8			j_uuid[16];
564
565	/* Pointer to the current commit thread for this journal */
566	struct task_struct *	j_task;
567
568	/* Maximum number of metadata buffers to allow in a single
569	 * compound commit transaction */
570	int			j_max_transaction_buffers;
571
572	/* What is the maximum transaction lifetime before we begin a
573	 * commit? */
574	unsigned long		j_commit_interval;
575
576	/* The timer used to wakeup the commit thread: */
577	struct timer_list *	j_commit_timer;
578	int			j_commit_timer_active;
579
580	/* Link all journals together - system-wide */
581	struct list_head	j_all_journals;
582
583	/* The revoke table: maintains the list of revoked blocks in the
584           current transaction. */
585	struct jbd_revoke_table_s *j_revoke;
586
587	/* Failed journal commit ID */
588	unsigned int		j_failed_commit;
589};
590
591/*
592 * Journal flag definitions
593 */
594#define JFS_UNMOUNT	0x001	/* Journal thread is being destroyed */
595#define JFS_ABORT	0x002	/* Journaling has been aborted for errors. */
596#define JFS_ACK_ERR	0x004	/* The errno in the sb has been acked */
597#define JFS_FLUSHED	0x008	/* The journal superblock has been flushed */
598#define JFS_LOADED	0x010	/* The journal superblock has been loaded */
599
600/*
601 * Function declarations for the journaling transaction and buffer
602 * management
603 */
604
605/* Filing buffers */
606extern void __journal_unfile_buffer(struct journal_head *);
607extern void journal_unfile_buffer(struct journal_head *);
608extern void __journal_refile_buffer(struct journal_head *);
609extern void journal_refile_buffer(struct journal_head *);
610extern void __journal_file_buffer(struct journal_head *, transaction_t *, int);
611extern void __journal_free_buffer(struct journal_head *bh);
612extern void journal_file_buffer(struct journal_head *, transaction_t *, int);
613extern void __journal_clean_data_list(transaction_t *transaction);
614
615/* Log buffer allocation */
616extern struct journal_head * journal_get_descriptor_buffer(journal_t *);
617extern unsigned long journal_next_log_block(journal_t *);
618
619/* Commit management */
620extern void journal_commit_transaction(journal_t *);
621
622/* Checkpoint list management */
623int __journal_clean_checkpoint_list(journal_t *journal);
624extern void journal_remove_checkpoint(struct journal_head *);
625extern void __journal_remove_checkpoint(struct journal_head *);
626extern void journal_insert_checkpoint(struct journal_head *, transaction_t *);
627extern void __journal_insert_checkpoint(struct journal_head *,transaction_t *);
628
629/* Buffer IO */
630extern int
631journal_write_metadata_buffer(transaction_t	  *transaction,
632			      struct journal_head  *jh_in,
633			      struct journal_head **jh_out,
634			      int		   blocknr);
635
636/* Transaction locking */
637extern void		__wait_on_journal (journal_t *);
638
639/*
640 * Journal locking.
641 *
642 * We need to lock the journal during transaction state changes so that
643 * nobody ever tries to take a handle on the running transaction while
644 * we are in the middle of moving it to the commit phase.
645 *
646 * Note that the locking is completely interrupt unsafe.  We never touch
647 * journal structures from interrupts.
648 *
649 * In 2.2, the BKL was required for lock_journal.  This is no longer
650 * the case.
651 */
652
653static inline void lock_journal(journal_t *journal)
654{
655	down(&journal->j_sem);
656}
657
658/* This returns zero if we acquired the semaphore */
659static inline int try_lock_journal(journal_t * journal)
660{
661	return down_trylock(&journal->j_sem);
662}
663
664static inline void unlock_journal(journal_t * journal)
665{
666	up(&journal->j_sem);
667}
668
669
670static inline handle_t *journal_current_handle(void)
671{
672	return current->journal_info;
673}
674
675/* The journaling code user interface:
676 *
677 * Create and destroy handles
678 * Register buffer modifications against the current transaction.
679 */
680
681extern handle_t *journal_start(journal_t *, int nblocks);
682extern handle_t *journal_try_start(journal_t *, int nblocks);
683extern int	 journal_restart (handle_t *, int nblocks);
684extern int	 journal_extend (handle_t *, int nblocks);
685extern int	 journal_get_write_access (handle_t *, struct buffer_head *);
686extern int	 journal_get_create_access (handle_t *, struct buffer_head *);
687extern int	 journal_get_undo_access (handle_t *, struct buffer_head *);
688extern int	 journal_dirty_data (handle_t *,
689				struct buffer_head *, int async);
690extern int	 journal_dirty_metadata (handle_t *, struct buffer_head *);
691extern void	 journal_release_buffer (handle_t *, struct buffer_head *);
692extern void	 journal_forget (handle_t *, struct buffer_head *);
693extern void	 journal_sync_buffer (struct buffer_head *);
694extern int	 journal_flushpage(journal_t *, struct page *, unsigned long);
695extern int	 journal_try_to_free_buffers(journal_t *, struct page *, int);
696extern int	 journal_stop(handle_t *);
697extern int	 journal_flush (journal_t *);
698
699extern void	 journal_lock_updates (journal_t *);
700extern void	 journal_unlock_updates (journal_t *);
701
702extern journal_t * journal_init_dev(kdev_t dev, kdev_t fs_dev,
703				int start, int len, int bsize);
704extern journal_t * journal_init_inode (struct inode *);
705extern int	   journal_update_format (journal_t *);
706extern int	   journal_check_used_features
707		   (journal_t *, unsigned long, unsigned long, unsigned long);
708extern int	   journal_check_available_features
709		   (journal_t *, unsigned long, unsigned long, unsigned long);
710extern int	   journal_set_features
711		   (journal_t *, unsigned long, unsigned long, unsigned long);
712extern int	   journal_create     (journal_t *);
713extern int	   journal_load       (journal_t *journal);
714extern void	   journal_destroy    (journal_t *);
715extern int	   journal_recover    (journal_t *journal);
716extern int	   journal_wipe       (journal_t *, int);
717extern int	   journal_skip_recovery (journal_t *);
718extern void	   journal_update_superblock (journal_t *, int);
719extern void	   __journal_abort      (journal_t *);
720extern void	   journal_abort      (journal_t *, int);
721extern int	   journal_errno      (journal_t *);
722extern void	   journal_ack_err    (journal_t *);
723extern int	   journal_clear_err  (journal_t *);
724extern unsigned long journal_bmap(journal_t *journal, unsigned long blocknr);
725extern int	    journal_force_commit(journal_t *journal);
726
727/*
728 * journal_head management
729 */
730extern struct journal_head
731		*journal_add_journal_head(struct buffer_head *bh);
732extern void	journal_remove_journal_head(struct buffer_head *bh);
733extern void	__journal_remove_journal_head(struct buffer_head *bh);
734extern void	journal_unlock_journal_head(struct journal_head *jh);
735
736/* Primary revoke support */
737#define JOURNAL_REVOKE_DEFAULT_HASH 256
738extern int	   journal_init_revoke(journal_t *, int);
739extern void	   journal_destroy_revoke_caches(void);
740extern int	   journal_init_revoke_caches(void);
741
742extern void	   journal_destroy_revoke(journal_t *);
743extern int	   journal_revoke (handle_t *,
744				unsigned long, struct buffer_head *);
745extern int	   journal_cancel_revoke(handle_t *, struct journal_head *);
746extern void	   journal_write_revoke_records(journal_t *, transaction_t *);
747
748/* Recovery revoke support */
749extern int	   journal_set_revoke(journal_t *, unsigned long, tid_t);
750extern int	   journal_test_revoke(journal_t *, unsigned long, tid_t);
751extern void	   journal_clear_revoke(journal_t *);
752extern void	   journal_brelse_array(struct buffer_head *b[], int n);
753
754/* The log thread user interface:
755 *
756 * Request space in the current transaction, and force transaction commit
757 * transitions on demand.
758 */
759
760extern int	log_space_left (journal_t *); /* Called with journal locked */
761extern tid_t	log_start_commit (journal_t *, transaction_t *);
762extern void	log_wait_commit (journal_t *, tid_t);
763extern int	log_do_checkpoint (journal_t *, int);
764
765extern void	log_wait_for_space(journal_t *, int nblocks);
766extern void	__journal_drop_transaction(journal_t *, transaction_t *);
767extern int	cleanup_journal_tail(journal_t *);
768
769/* Reduce journal memory usage by flushing */
770extern void shrink_journal_memory(void);
771
772/* Debugging code only: */
773
774#define jbd_ENOSYS() \
775do {								      \
776	printk (KERN_ERR "JBD unimplemented function " __FUNCTION__); \
777	current->state = TASK_UNINTERRUPTIBLE;			      \
778	schedule();						      \
779} while (1)
780
781/*
782 * is_journal_abort
783 *
784 * Simple test wrapper function to test the JFS_ABORT state flag.  This
785 * bit, when set, indicates that we have had a fatal error somewhere,
786 * either inside the journaling layer or indicated to us by the client
787 * (eg. ext3), and that we and should not commit any further
788 * transactions.
789 */
790
791static inline int is_journal_aborted(journal_t *journal)
792{
793	return journal->j_flags & JFS_ABORT;
794}
795
796static inline int is_handle_aborted(handle_t *handle)
797{
798	if (handle->h_aborted)
799		return 1;
800	return is_journal_aborted(handle->h_transaction->t_journal);
801}
802
803static inline void journal_abort_handle(handle_t *handle)
804{
805	handle->h_aborted = 1;
806}
807
808/* Not all architectures define BUG() */
809#ifndef BUG
810#define BUG() do { \
811        printk("kernel BUG at %s:%d!\n", __FILE__, __LINE__); \
812	* ((char *) 0) = 0; \
813 } while (0)
814#endif /* BUG */
815
816#else
817
818extern int	   journal_recover    (journal_t *journal);
819extern int	   journal_skip_recovery (journal_t *);
820
821/* Primary revoke support */
822extern int	   journal_init_revoke(journal_t *, int);
823extern void	   journal_destroy_revoke_caches(void);
824extern int	   journal_init_revoke_caches(void);
825
826/* Recovery revoke support */
827extern int	   journal_set_revoke(journal_t *, unsigned long, tid_t);
828extern int	   journal_test_revoke(journal_t *, unsigned long, tid_t);
829extern void	   journal_clear_revoke(journal_t *);
830extern void	   journal_brelse_array(struct buffer_head *b[], int n);
831
832extern void	   journal_destroy_revoke(journal_t *);
833#endif /* __KERNEL__   */
834
835static inline int tid_gt(tid_t x, tid_t y) EXT2FS_ATTR((unused));
836static inline int tid_geq(tid_t x, tid_t y) EXT2FS_ATTR((unused));
837
838/* Comparison functions for transaction IDs: perform comparisons using
839 * modulo arithmetic so that they work over sequence number wraps. */
840
841static inline int tid_gt(tid_t x, tid_t y)
842{
843	int difference = (x - y);
844	return (difference > 0);
845}
846
847static inline int tid_geq(tid_t x, tid_t y)
848{
849	int difference = (x - y);
850	return (difference >= 0);
851}
852
853extern int journal_blocks_per_page(struct inode *inode);
854
855/*
856 * Definitions which augment the buffer_head layer
857 */
858
859/* journaling buffer types */
860#define BJ_None		0	/* Not journaled */
861#define BJ_SyncData	1	/* Normal data: flush before commit */
862#define BJ_AsyncData	2	/* writepage data: wait on it before commit */
863#define BJ_Metadata	3	/* Normal journaled metadata */
864#define BJ_Forget	4	/* Buffer superceded by this transaction */
865#define BJ_IO		5	/* Buffer is for temporary IO use */
866#define BJ_Shadow	6	/* Buffer contents being shadowed to the log */
867#define BJ_LogCtl	7	/* Buffer contains log descriptors */
868#define BJ_Reserved	8	/* Buffer is reserved for access by journal */
869#define BJ_Types	9
870
871extern int jbd_blocks_per_page(struct inode *inode);
872
873#ifdef __KERNEL__
874
875extern spinlock_t jh_splice_lock;
876/*
877 * Once `expr1' has been found true, take jh_splice_lock
878 * and then reevaluate everything.
879 */
880#define SPLICE_LOCK(expr1, expr2)				\
881	({							\
882		int ret = (expr1);				\
883		if (ret) {					\
884			spin_lock(&jh_splice_lock);		\
885			ret = (expr1) && (expr2);		\
886			spin_unlock(&jh_splice_lock);		\
887		}						\
888		ret;						\
889	})
890
891/*
892 * A number of buffer state predicates.  They test for
893 * buffer_jbd() because they are used in core kernel code.
894 *
895 * These will be racy on SMP unless we're *sure* that the
896 * buffer won't be detached from the journalling system
897 * in parallel.
898 */
899
900/* Return true if the buffer is on journal list `list' */
901static inline int buffer_jlist_eq(struct buffer_head *bh, int list)
902{
903	return SPLICE_LOCK(buffer_jbd(bh), bh2jh(bh)->b_jlist == list);
904}
905
906/* Return true if this bufer is dirty wrt the journal */
907static inline int buffer_jdirty(struct buffer_head *bh)
908{
909	return buffer_jbd(bh) && __buffer_state(bh, JBDDirty);
910}
911
912/* Return true if it's a data buffer which journalling is managing */
913static inline int buffer_jbd_data(struct buffer_head *bh)
914{
915	return SPLICE_LOCK(buffer_jbd(bh),
916			bh2jh(bh)->b_jlist == BJ_SyncData ||
917			bh2jh(bh)->b_jlist == BJ_AsyncData);
918}
919
920#ifdef CONFIG_SMP
921#define assert_spin_locked(lock)	J_ASSERT(spin_is_locked(lock))
922#else
923#define assert_spin_locked(lock)	do {} while(0)
924#endif
925
926#define buffer_trace_init(bh)	do {} while (0)
927#define print_buffer_fields(bh)	do {} while (0)
928#define print_buffer_trace(bh)	do {} while (0)
929#define BUFFER_TRACE(bh, info)	do {} while (0)
930#define BUFFER_TRACE2(bh, bh2, info)	do {} while (0)
931#define JBUFFER_TRACE(jh, info)	do {} while (0)
932
933#endif	/* __KERNEL__ */
934
935#endif	/* CONFIG_JBD || CONFIG_JBD_MODULE || !__KERNEL__ */
936
937/*
938 * Compatibility no-ops which allow the kernel to compile without CONFIG_JBD
939 * go here.
940 */
941
942#if defined(__KERNEL__) && !(defined(CONFIG_JBD) || defined(CONFIG_JBD_MODULE))
943
944#define J_ASSERT(expr)			do {} while (0)
945#define J_ASSERT_BH(bh, expr)		do {} while (0)
946#define buffer_jbd(bh)			0
947#define buffer_jlist_eq(bh, val)	0
948#define journal_buffer_journal_lru(bh)	0
949
950#endif	/* defined(__KERNEL__) && !defined(CONFIG_JBD) */
951#endif	/* _LINUX_JBD_H */
952