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