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