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