2 * linux/include/linux/jfs.h
4 * Written by Stephen C. Tweedie <sct@redhat.com>
6 * Copyright 1998-2000 Red Hat, Inc --- All Rights Reserved
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.
12 * Definitions for transaction data structures for the buffer cache
13 * filesystem journaling support.
19 /* Allow this file to be included directly into e2fsprogs */
21 #include "jfs_compat.h"
25 * Debug code enabled by default for kernel builds
33 extern int journal_enable_debug;
36 #define jfs_debug(n, f, a...) \
38 if ((n) <= journal_enable_debug) { \
39 printk (KERN_DEBUG "JFS DEBUG: (%s, %d): %s: ", \
40 __FILE__, __LINE__, __FUNCTION__); \
45 #define jfs_debug(f, a...) /**/
48 #define JFS_MIN_JOURNAL_BLOCKS 1024
51 * Internal structures used by the logging mechanism:
54 #define JFS_MAGIC_NUMBER 0xc03b3998U /* The first 4 bytes of /dev/random! */
62 * Descriptor block types:
65 #define JFS_DESCRIPTOR_BLOCK 1
66 #define JFS_COMMIT_BLOCK 2
67 #define JFS_SUPERBLOCK_V1 3
68 #define JFS_SUPERBLOCK_V2 4
69 #define JFS_REVOKE_BLOCK 5
72 * Standard header for all descriptor blocks:
74 typedef struct journal_header_s
83 * The block tag: used to describe a single buffer in the journal
85 typedef struct journal_block_tag_s
87 __u32 t_blocknr; /* The on-disk block number */
88 __u32 t_flags; /* See below */
89 } journal_block_tag_t;
92 * The revoke descriptor: used on disk to describe a series of blocks to
93 * be revoked from the log
95 typedef struct journal_revoke_header_s
97 journal_header_t r_header;
98 int r_count; /* Count of bytes used in the block */
99 } journal_revoke_header_t;
102 /* Definitions for the journal tag flags word: */
103 #define JFS_FLAG_ESCAPE 1 /* on-disk block is escaped */
104 #define JFS_FLAG_SAME_UUID 2 /* block has same uuid as previous */
105 #define JFS_FLAG_DELETED 4 /* block deleted by this transaction */
106 #define JFS_FLAG_LAST_TAG 8 /* last tag in this descriptor block */
110 * The journal superblock. All fields are in big-endian byte order.
112 typedef struct journal_superblock_s
115 journal_header_t s_header;
118 /* Static information describing the journal */
119 __u32 s_blocksize; /* journal device blocksize */
120 __u32 s_maxlen; /* total blocks in journal file */
121 __u32 s_first; /* first block of log information */
124 /* Dynamic information describing the current state of the log */
125 __u32 s_sequence; /* first commit ID expected in log */
126 __u32 s_start; /* blocknr of start of log */
129 /* Error value, as set by journal_abort(). */
133 /* Remaining fields are only valid in a version-2 superblock */
134 __u32 s_feature_compat; /* compatible feature set */
135 __u32 s_feature_incompat; /* incompatible feature set */
136 __u32 s_feature_ro_compat; /* readonly-compatible feature set */
138 __u8 s_uuid[16]; /* 128-bit uuid for journal */
141 __u32 s_nr_users; /* Nr of filesystems sharing log */
143 __u32 s_dynsuper; /* Blocknr of dynamic superblock copy*/
146 __u32 s_max_transaction; /* Limit of journal blocks per trans.*/
147 __u32 s_max_trans_data; /* Limit of data blocks per trans. */
153 __u8 s_users[16*48]; /* ids of all fs'es sharing the log */
155 } journal_superblock_t;
157 #define JFS_HAS_COMPAT_FEATURE(j,mask) \
158 ((j)->j_format_version >= 2 && \
159 ((j)->j_superblock->s_feature_compat & cpu_to_be32((mask))))
160 #define JFS_HAS_RO_COMPAT_FEATURE(j,mask) \
161 ((j)->j_format_version >= 2 && \
162 ((j)->j_superblock->s_feature_ro_compat & cpu_to_be32((mask))))
163 #define JFS_HAS_INCOMPAT_FEATURE(j,mask) \
164 ((j)->j_format_version >= 2 && \
165 ((j)->j_superblock->s_feature_incompat & cpu_to_be32((mask))))
167 #define JFS_FEATURE_INCOMPAT_REVOKE 0x00000001
169 /* Features known to this kernel version: */
170 #define JFS_KNOWN_COMPAT_FEATURES 0
171 #define JFS_KNOWN_ROCOMPAT_FEATURES 0
172 #define JFS_KNOWN_INCOMPAT_FEATURES JFS_FEATURE_INCOMPAT_REVOKE
176 #include <linux/fs.h>
179 #define J_ASSERT(assert) \
180 do { if (!(assert)) { \
182 "Assertion failure in %s() at %s line %d: " \
184 __FUNCTION__, __FILE__, __LINE__, # assert); \
185 * ((char *) 0) = 0; \
189 struct jfs_revoke_table_s;
191 /* The handle_t type represents a single atomic update being performed
192 * by some process. All filesystem modifications made by the process go
193 * through this handle. Recursive operations (such as quota operations)
194 * are gathered into a single update.
196 * The buffer credits field is used to account for journaled buffers
197 * being modified by the running process. To ensure that there is
198 * enough log space for all outstanding operations, we need to limit the
199 * number of outstanding buffers possible at any time. When the
200 * operation completes, any buffer credits not used are credited back to
201 * the transaction, so that at all times we know how many buffers the
202 * outstanding updates on a transaction might possibly touch. */
206 /* Which compound transaction is this update a part of? */
207 transaction_t * h_transaction;
209 /* Number of remaining buffers we are allowed to dirty: */
210 int h_buffer_credits;
212 /* Reference count on this handle */
216 unsigned int h_sync : 1; /* sync-on-close */
217 unsigned int h_jdata : 1; /* force data journaling */
221 /* The transaction_t type is the guts of the journaling mechanism. It
222 * tracks a compound transaction through its various states:
224 * RUNNING: accepting new updates
225 * LOCKED: Updates still running but we don't accept new ones
226 * RUNDOWN: Updates are tidying up but have finished requesting
227 * new buffers to modify (state not used for now)
228 * FLUSH: All updates complete, but we are still writing to disk
229 * COMMIT: All data on disk, writing commit record
230 * FINISHED: We still have to keep the transaction for checkpointing.
232 * The transaction keeps track of all of the buffers modified by a
233 * running transaction, and all of the buffers committed but not yet
234 * flushed to home for finished transactions.
239 /* Pointer to the journal for this transaction. */
240 journal_t * t_journal;
242 /* Sequence number for this transaction */
245 /* Transaction's current state */
255 /* Where in the log does this transaction's commit start? */
256 unsigned long t_log_start;
258 /* Doubly-linked circular list of all inodes owned by this
260 struct inode * t_ilist;
262 /* Number of buffers on the t_buffers list */
265 /* Doubly-linked circular list of all buffers reserved but not
266 yet modified by this transaction */
267 struct buffer_head * t_reserved_list;
269 /* Doubly-linked circular list of all metadata buffers owned by this
271 struct buffer_head * t_buffers;
273 /* Doubly-linked circular list of all data buffers still to be
274 flushed before this transaction can be committed */
275 struct buffer_head * t_datalist;
277 /* Doubly-linked circular list of all forget buffers (superceded
278 buffers which we can un-checkpoint once this transaction
280 struct buffer_head * t_forget;
282 /* Doubly-linked circular list of all buffers still to be
283 flushed before this transaction can be checkpointed */
284 struct buffer_head * t_checkpoint_list;
286 /* Doubly-linked circular list of temporary buffers currently
287 undergoing IO in the log */
288 struct buffer_head * t_iobuf_list;
290 /* Doubly-linked circular list of metadata buffers being
291 shadowed by log IO. The IO buffers on the iobuf list and the
292 shadow buffers on this list match each other one for one at
294 struct buffer_head * t_shadow_list;
296 /* Doubly-linked circular list of control buffers being written
298 struct buffer_head * t_log_list;
300 /* Number of outstanding updates running on this transaction */
303 /* Number of buffers reserved for use by all handles in this
304 * transaction handle but not yet modified. */
305 int t_outstanding_credits;
307 /* Forward and backward links for the circular list of all
308 * transactions awaiting checkpoint */
309 transaction_t *t_cpnext, *t_cpprev;
311 /* When will the transaction expire (become due for commit), in
313 unsigned long t_expires;
317 /* The journal_t maintains all of the journaling state information for a
318 * single filesystem. It is linked to from the fs superblock structure.
320 * We use the journal_t to keep track of all outstanding transaction
321 * activity on the filesystem, and to manage the state of the log
322 * writing process. */
326 /* General journaling state flags */
327 unsigned long j_flags;
329 /* Is there an outstanding uncleared error on the journal (from
333 /* The superblock buffer */
334 struct buffer_head * j_sb_buffer;
335 journal_superblock_t * j_superblock;
337 /* Version of the superblock format */
338 int j_format_version;
340 /* Number of processes waiting to create a barrier lock */
343 /* The barrier lock itself */
344 struct semaphore j_barrier;
346 /* Transactions: The current running transaction... */
347 transaction_t * j_running_transaction;
349 /* ... the transaction we are pushing to disk ... */
350 transaction_t * j_committing_transaction;
352 /* ... and a linked circular list of all transactions waiting
353 * for checkpointing. */
354 transaction_t * j_checkpoint_transactions;
356 /* Wait queue for locking of the journal structure. */
357 struct wait_queue * j_wait_lock;
359 /* Wait queue for waiting for a locked transaction to start
360 committing, or for a barrier lock to be released */
361 struct wait_queue * j_wait_transaction_locked;
363 /* Wait queue for waiting for checkpointing to complete */
364 struct wait_queue * j_wait_logspace;
366 /* Wait queue for waiting for commit to complete */
367 struct wait_queue * j_wait_done_commit;
369 /* Wait queue to trigger checkpointing */
370 struct wait_queue * j_wait_checkpoint;
372 /* Wait queue to trigger commit */
373 struct wait_queue * j_wait_commit;
375 /* Wait queue to wait for updates to complete */
376 struct wait_queue * j_wait_updates;
378 /* Semaphore for locking against concurrent checkpoints */
379 struct semaphore j_checkpoint_sem;
381 /* Journal running state: */
382 /* The lock flag is *NEVER* touched from interrupts. */
383 unsigned int j_locked : 1;
385 /* Journal head: identifies the first unused block in the journal. */
386 unsigned long j_head;
388 /* Journal tail: identifies the oldest still-used block in the
390 unsigned long j_tail;
392 /* Journal free: how many free blocks are there in the journal? */
393 unsigned long j_free;
395 /* Journal start and end: the block numbers of the first usable
396 * block and one beyond the last usable block in the journal. */
397 unsigned long j_first, j_last;
399 /* Device, blocksize and starting block offset for the location
400 * where we store the journal. */
403 unsigned int j_blk_offset;
405 /* Total maximum capacity of the journal region on disk. */
406 unsigned int j_maxlen;
408 /* Optional inode where we store the journal. If present, all
409 * journal block numbers are mapped into this inode via
411 struct inode * j_inode;
413 /* Sequence number of the oldest transaction in the log */
414 tid_t j_tail_sequence;
415 /* Sequence number of the next transaction to grant */
416 tid_t j_transaction_sequence;
417 /* Sequence number of the most recently committed transaction */
418 tid_t j_commit_sequence;
419 /* Sequence number of the most recent transaction wanting commit */
420 tid_t j_commit_request;
422 /* Journal uuid: identifies the object (filesystem, LVM volume
423 * etc) backed by this journal. This will eventually be
424 * replaced by an array of uuids, allowing us to index multiple
425 * devices within a single journal and to perform atomic updates
430 /* Pointer to the current commit thread for this journal */
431 struct task_struct * j_task;
433 /* Maximum number of metadata buffers to allow in a single
434 * compound commit transaction */
435 int j_max_transaction_buffers;
437 /* What is the maximum transaction lifetime before we begin a
439 unsigned long j_commit_interval;
441 /* The timer used to wakeup the commit thread: */
442 struct timer_list * j_commit_timer;
443 int j_commit_timer_active;
445 /* The revoke table: maintains the list of revoked blocks in the
446 current transaction. */
447 struct jfs_revoke_table_s *j_revoke;
451 * Journal flag definitions
453 #define JFS_UNMOUNT 0x001 /* Journal thread is being destroyed */
454 #define JFS_SYNC 0x002 /* Perform synchronous transaction commits */
455 #define JFS_ABORT 0x004 /* Journaling has been aborted for errors. */
456 #define JFS_ACK_ERR 0x008 /* The errno in the sb has been acked */
457 #define JFS_FLUSHED 0x010 /* The journal superblock has been flushed */
458 #define JFS_LOADED 0x020 /* The journal superblock has been loaded */
461 * Journaling internal variables/parameters
464 extern int journal_flush_nr_buffers;
468 * Function declarations for the journaling transaction and buffer
473 extern void journal_unfile_buffer(struct buffer_head *);
474 extern void journal_refile_buffer(struct buffer_head *);
475 extern void journal_file_buffer(struct buffer_head *, transaction_t *, int);
476 extern void journal_clean_data_list(transaction_t *transaction);
478 /* Log buffer allocation */
479 extern struct buffer_head * journal_get_descriptor_buffer(journal_t *);
480 extern unsigned long journal_next_log_block(journal_t *);
482 /* Commit management */
483 extern void journal_commit_transaction(journal_t *);
485 /* Checkpoint list management */
486 extern void journal_remove_checkpoint(struct buffer_head *);
487 extern void journal_insert_checkpoint(struct buffer_head *, transaction_t *);
491 journal_write_metadata_buffer(transaction_t *transaction,
492 struct buffer_head *bh_in,
493 struct buffer_head **bh_out,
496 /* Create and destroy transactions */
497 extern transaction_t * get_transaction (journal_t *);
498 extern void put_transaction (transaction_t *);
500 /* Notify state transitions (called by the log writer thread): */
501 extern int set_transaction_state (transaction_t *, int);
504 /* Transaction locking */
505 extern void __wait_on_journal (journal_t *);
507 /* Journal locking. In 2.2, we assume that the kernel lock is already
509 static inline void lock_journal (journal_t * journal)
512 J_ASSERT(current->lock_depth >= 0);
514 if (journal->j_locked)
515 __wait_on_journal(journal);
516 journal->j_locked = 1;
519 static inline int try_lock_journal (journal_t * journal)
521 if (journal->j_locked)
523 journal->j_locked = 1;
527 static inline void unlock_journal (journal_t * journal)
529 J_ASSERT (journal->j_locked);
530 journal->j_locked = 0;
531 wake_up(&journal->j_wait_lock);
534 /* This function is gross, but unfortunately we need it as long as
535 * existing filesystems want to guard against races by testing
536 * bh->b_count. @@@ Remove this? We no longer abuse b_count so badly!
539 static inline int journal_is_buffer_shared(struct buffer_head *bh)
541 int count = bh->b_count;
542 J_ASSERT (count >= 1);
546 /* The journaling code user interface:
548 * Create and destroy handles
549 * Register buffer modifications against the current transaction.
552 extern handle_t *journal_start (journal_t *, int nblocks);
553 extern int journal_restart (handle_t *, int nblocks);
554 extern int journal_extend (handle_t *, int nblocks);
555 extern int journal_get_write_access (handle_t *, struct buffer_head *);
556 extern int journal_get_create_access (handle_t *, struct buffer_head *);
557 extern int journal_get_undo_access (handle_t *, struct buffer_head *);
558 extern int journal_dirty_data (handle_t *, struct buffer_head *);
559 extern int journal_dirty_metadata (handle_t *, struct buffer_head *);
560 extern void journal_release_buffer (handle_t *, struct buffer_head *);
561 extern void journal_forget (handle_t *, struct buffer_head *);
562 extern void journal_sync_buffer (struct buffer_head *);
563 extern int journal_stop (handle_t *);
564 extern int journal_flush (journal_t *);
566 extern void journal_lock_updates (journal_t *);
567 extern void journal_unlock_updates (journal_t *);
569 extern journal_t * journal_init_dev (kdev_t, int start, int len, int bsize);
570 extern journal_t * journal_init_inode (struct inode *);
571 extern int journal_update_format (journal_t *);
572 extern int journal_check_used_features
573 (journal_t *, unsigned long, unsigned long, unsigned long);
574 extern int journal_check_available_features
575 (journal_t *, unsigned long, unsigned long, unsigned long);
576 extern int journal_set_features
577 (journal_t *, unsigned long, unsigned long, unsigned long);
578 extern int journal_create (journal_t *);
579 extern int journal_load (journal_t *);
580 extern void journal_release (journal_t *);
581 extern int journal_wipe (journal_t *, int);
582 extern int journal_skip_recovery (journal_t *);
583 extern void journal_update_superblock (journal_t *, int);
584 extern void __journal_abort (journal_t *);
585 extern void journal_abort (journal_t *, int);
586 extern int journal_errno (journal_t *);
587 extern void journal_ack_err (journal_t *);
588 extern int journal_clear_err (journal_t *);
590 /* Primary revoke support */
591 #define JOURNAL_REVOKE_DEFAULT_HASH 256
592 extern int journal_revoke (handle_t *, unsigned long, struct buffer_head *);
593 extern void journal_cancel_revoke(handle_t *, struct buffer_head *);
594 extern void journal_write_revoke_records(journal_t *, transaction_t *);
598 /* The log thread user interface:
600 * Request space in the current transaction, and force transaction commit
601 * transitions on demand.
604 extern int log_space_left (journal_t *); /* Called with journal locked */
605 extern void log_start_commit (journal_t *, transaction_t *);
606 extern void log_wait_commit (journal_t *, tid_t);
607 extern int log_do_checkpoint (journal_t *, int);
609 extern void log_wait_for_space(journal_t *, int nblocks);
610 extern void journal_drop_transaction(journal_t *, transaction_t *);
611 extern int cleanup_journal_tail(journal_t *);
614 /* Debugging code only: */
616 #define jfs_ENOSYS() \
618 printk (KERN_ERR "JFS unimplemented function " __FUNCTION__); \
619 current->state = TASK_UNINTERRUPTIBLE; \
626 * Simple test wrapper function to test the JFS_ABORT state flag. This
627 * bit, when set, indicates that we have had a fatal error somewhere,
628 * either inside the journaling layer or indicated to us by the client
629 * (eg. ext3), and that we and should not commit any further
633 static inline int is_journal_abort(journal_t *journal)
635 return journal->j_flags & JFS_ABORT;
638 /* Not all architectures define BUG() */
640 # define BUG() do { \
641 printk("kernel BUG at %s:%d!\n", __FILE__, __LINE__); \
642 * ((char *) 0) = 0; \
646 #endif /* __KERNEL__ */
648 /* Function prototypes, used by both user- and kernel- space */
651 extern int journal_recover (journal_t *);
654 extern int journal_init_revoke(journal_t *, int);
655 extern int journal_set_revoke(journal_t *, unsigned long, tid_t);
656 extern int journal_test_revoke(journal_t *, unsigned long, tid_t);
657 extern void journal_clear_revoke(journal_t *);
658 extern void journal_destroy_revoke(journal_t *);
661 /* Comparison functions for transaction IDs: perform comparisons using
662 * modulo arithmetic so that they work over sequence number wraps. */
664 static inline int tid_ge(tid_t x, tid_t y)
666 int difference = (x - y);
667 return (difference > 0);
670 static inline int tid_geq(tid_t x, tid_t y)
672 int difference = (x - y);
673 return (difference >= 0);
677 #endif /* _LINUX_JFS_H */