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