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