Whamcloud - gitweb
e2fsck: port fc changes from kernel's recovery.c to e2fsck
[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 #include "jfs_compat.h"
20 #define JFS_DEBUG
21 #define jfs_debug jbd_debug
22
23 #ifndef __GNUC__
24 #define __FUNCTION__ ""
25 #endif
26
27 #define journal_oom_retry 0
28
29 #ifdef __STDC__
30 #ifdef CONFIG_JBD_DEBUG
31 /*
32  * Define JBD_EXPENSIVE_CHECKING to enable more expensive internal
33  * consistency checks.  By default we don't do this unless
34  * CONFIG_JBD_DEBUG is on.
35  */
36 #define JBD_EXPENSIVE_CHECKING
37 extern int journal_enable_debug;
38
39 #define jbd_debug(n, f, a...)                                           \
40         do {                                                            \
41                 if ((n) <= journal_enable_debug) {                      \
42                         printk (KERN_DEBUG "(%s, %d): %s: ",            \
43                                 __FILE__, __LINE__, __FUNCTION__);      \
44                         printk (f, ## a);                               \
45                 }                                                       \
46         } while (0)
47 #else
48 #ifdef __GNUC__
49 #if defined(__KERNEL__) || !defined(CONFIG_JBD_DEBUG)
50 #define jbd_debug(f, a...)      /**/
51 #else
52 extern int journal_enable_debug;
53 #define jbd_debug(n, f, a...)                                           \
54         do {                                                            \
55                 if ((n) <= journal_enable_debug) {                      \
56                         printf("(%s, %d): %s: ",                        \
57                                 __FILE__, __LINE__, __func__);          \
58                         printf(f, ## a);                                \
59                 }                                                       \
60         } while (0)
61 #endif /*__KERNEL__ */
62 #else
63 #define jbd_debug(f, ...)       /**/
64 #endif
65 #endif
66 #else
67 #define jbd_debug(x)            /* AIX doesn't do STDC */
68 #endif
69
70 extern void * __jbd_kmalloc (char *where, size_t size, int flags, int retry);
71 #define jbd_kmalloc(size, flags) \
72         __jbd_kmalloc(__FUNCTION__, (size), (flags), journal_oom_retry)
73 #define jbd_rep_kmalloc(size, flags) \
74         __jbd_kmalloc(__FUNCTION__, (size), (flags), 1)
75 #define jbd_get_num_fc_blks(jsb)                                        \
76         (be32_to_cpu((jsb)->s_num_fc_blks) ?                            \
77          be32_to_cpu((jsb)->s_num_fc_blks) : JBD2_DEFAULT_FAST_COMMIT_BLOCKS)
78
79
80 #define JBD2_MIN_JOURNAL_BLOCKS 1024
81 #define JBD2_DEFAULT_FAST_COMMIT_BLOCKS 256
82
83 /*
84  * Internal structures used by the logging mechanism:
85  */
86
87 #define JBD2_MAGIC_NUMBER 0xc03b3998U /* The first 4 bytes of /dev/random! */
88
89 /*
90  * On-disk structures
91  */
92
93 /*
94  * Descriptor block types:
95  */
96
97 #define JBD2_DESCRIPTOR_BLOCK   1
98 #define JBD2_COMMIT_BLOCK       2
99 #define JBD2_SUPERBLOCK_V1      3
100 #define JBD2_SUPERBLOCK_V2      4
101 #define JBD2_REVOKE_BLOCK       5
102 #define JBD2_FC_BLOCK           6
103
104 /*
105  * Standard header for all descriptor blocks:
106  */
107 typedef struct journal_header_s
108 {
109         __be32          h_magic;
110         __be32          h_blocktype;
111         __be32          h_sequence;
112 } journal_header_t;
113
114 /*
115  * Checksum types.
116  */
117 #define JBD2_CRC32_CHKSUM   1
118 #define JBD2_MD5_CHKSUM     2
119 #define JBD2_SHA1_CHKSUM    3
120 #define JBD2_CRC32C_CHKSUM  4
121
122 #define JBD2_CRC32_CHKSUM_SIZE 4
123
124 #define JBD2_CHECKSUM_BYTES (32 / sizeof(__u32))
125 /*
126  * Commit block header for storing transactional checksums:
127  *
128  * NOTE: If FEATURE_COMPAT_CHECKSUM (checksum v1) is set, the h_chksum*
129  * fields are used to store a checksum of the descriptor and data blocks.
130  *
131  * If FEATURE_INCOMPAT_CSUM_V2 (checksum v2) is set, then the h_chksum
132  * field is used to store crc32c(uuid+commit_block).  Each journal metadata
133  * block gets its own checksum, and data block checksums are stored in
134  * journal_block_tag (in the descriptor).  The other h_chksum* fields are
135  * not used.
136  *
137  * If FEATURE_INCOMPAT_CSUM_V3 is set, the descriptor block uses
138  * journal_block_tag3_t to store a full 32-bit checksum.  Everything else
139  * is the same as v2.
140  *
141  * Checksum v1, v2, and v3 are mutually exclusive features.
142  */
143 struct commit_header {
144         __be32          h_magic;
145         __be32          h_blocktype;
146         __be32          h_sequence;
147         unsigned char   h_chksum_type;
148         unsigned char   h_chksum_size;
149         unsigned char   h_padding[2];
150         __be32          h_chksum[JBD2_CHECKSUM_BYTES];
151         __be64          h_commit_sec;
152         __be32          h_commit_nsec;
153 };
154
155 /*
156  * The block tag: used to describe a single buffer in the journal
157  */
158 typedef struct journal_block_tag3_s
159 {
160         __be32          t_blocknr;      /* The on-disk block number */
161         __be32          t_flags;        /* See below */
162         __be32          t_blocknr_high; /* most-significant high 32bits. */
163         __be32          t_checksum;     /* crc32c(uuid+seq+block) */
164 } journal_block_tag3_t;
165
166 typedef struct journal_block_tag_s
167 {
168         __be32          t_blocknr;      /* The on-disk block number */
169         __be16          t_checksum;     /* truncated crc32c(uuid+seq+block) */
170         __be16          t_flags;        /* See below */
171         __be32          t_blocknr_high; /* most-significant high 32bits. */
172 } journal_block_tag_t;
173
174 /* Tail of descriptor or revoke block, for checksumming */
175 struct jbd2_journal_block_tail {
176         __be32          t_checksum;
177 };
178
179 /*
180  * The revoke descriptor: used on disk to describe a series of blocks to
181  * be revoked from the log
182  */
183 typedef struct journal_revoke_header_s
184 {
185         journal_header_t r_header;
186         __be32           r_count;       /* Count of bytes used in the block */
187 } jbd2_journal_revoke_header_t;
188
189 /* Definitions for the journal tag flags word: */
190 #define JBD2_FLAG_ESCAPE                1       /* on-disk block is escaped */
191 #define JBD2_FLAG_SAME_UUID     2       /* block has same uuid as previous */
192 #define JBD2_FLAG_DELETED       4       /* block deleted by this transaction */
193 #define JBD2_FLAG_LAST_TAG      8       /* last tag in this descriptor block */
194
195
196 #define UUID_SIZE 16
197 #define JBD2_USERS_MAX 48
198 #define JBD2_USERS_SIZE (UUID_SIZE * JBD2_USERS_MAX)
199 /*
200  * The journal superblock.  All fields are in big-endian byte order.
201  */
202 typedef struct journal_superblock_s
203 {
204 /* 0x0000 */
205         journal_header_t s_header;
206
207 /* 0x000C */
208         /* Static information describing the journal */
209         __be32  s_blocksize;            /* journal device blocksize */
210         __be32  s_maxlen;               /* total blocks in journal file */
211         __be32  s_first;                /* first block of log information */
212
213 /* 0x0018 */
214         /* Dynamic information describing the current state of the log */
215         __be32  s_sequence;             /* first commit ID expected in log */
216         __be32  s_start;                /* blocknr of start of log */
217
218 /* 0x0020 */
219         /* Error value, as set by journal_abort(). */
220         __s32   s_errno;
221
222 /* 0x0024 */
223         /* Remaining fields are only valid in a version-2 superblock */
224         __be32  s_feature_compat;       /* compatible feature set */
225         __be32  s_feature_incompat;     /* incompatible feature set */
226         __be32  s_feature_ro_compat;    /* readonly-compatible feature set */
227 /* 0x0030 */
228         __u8    s_uuid[16];             /* 128-bit uuid for journal */
229
230 /* 0x0040 */
231         __be32  s_nr_users;             /* Nr of filesystems sharing log */
232
233         __be32  s_dynsuper;             /* Blocknr of dynamic superblock copy*/
234
235 /* 0x0048 */
236         __be32  s_max_transaction;      /* Limit of journal blocks per trans.*/
237         __be32  s_max_trans_data;       /* Limit of data blocks per trans. */
238
239 /* 0x0050 */
240         __u8    s_checksum_type;        /* checksum type */
241         __u8    s_padding2[3];
242 /* 0x0054 */
243         __be32  s_num_fc_blks;          /* Number of fast commit blocks */
244 /* 0x0058 */
245         __be32  s_padding[41];
246         __be32  s_checksum;             /* crc32c(superblock) */
247
248 /* 0x0100 */
249         __u8    s_users[JBD2_USERS_SIZE];               /* ids of all fs'es sharing the log */
250
251 /* 0x0400 */
252 } journal_superblock_t;
253
254 #define JBD2_HAS_COMPAT_FEATURE(j,mask)                                 \
255         ((j)->j_format_version >= 2 &&                                  \
256          ((j)->j_superblock->s_feature_compat & ext2fs_cpu_to_be32((mask))))
257 #define JBD2_HAS_RO_COMPAT_FEATURE(j,mask)                              \
258         ((j)->j_format_version >= 2 &&                                  \
259          ((j)->j_superblock->s_feature_ro_compat & ext2fs_cpu_to_be32((mask))))
260 #define JBD2_HAS_INCOMPAT_FEATURE(j,mask)                               \
261         ((j)->j_format_version >= 2 &&                                  \
262          ((j)->j_superblock->s_feature_incompat & ext2fs_cpu_to_be32((mask))))
263
264 #define JBD2_FEATURE_COMPAT_CHECKSUM            0x00000001
265
266 #define JBD2_FEATURE_INCOMPAT_REVOKE            0x00000001
267 #define JBD2_FEATURE_INCOMPAT_64BIT             0x00000002
268 #define JBD2_FEATURE_INCOMPAT_ASYNC_COMMIT      0x00000004
269 #define JBD2_FEATURE_INCOMPAT_CSUM_V2           0x00000008
270 #define JBD2_FEATURE_INCOMPAT_CSUM_V3           0x00000010
271 #define JBD2_FEATURE_INCOMPAT_FAST_COMMIT       0x00000020
272
273 /* Features known to this kernel version: */
274 #define JBD2_KNOWN_COMPAT_FEATURES      0
275 #define JBD2_KNOWN_ROCOMPAT_FEATURES    0
276 #define JBD2_KNOWN_INCOMPAT_FEATURES    (JBD2_FEATURE_INCOMPAT_REVOKE|\
277                                          JBD2_FEATURE_INCOMPAT_ASYNC_COMMIT| \
278                                          JBD2_FEATURE_INCOMPAT_64BIT|\
279                                          JBD2_FEATURE_INCOMPAT_CSUM_V2| \
280                                          JBD2_FEATURE_INCOMPAT_CSUM_V3 | \
281                                          JBD2_FEATURE_INCOMPAT_FAST_COMMIT)
282
283 #ifdef NO_INLINE_FUNCS
284 extern size_t journal_tag_bytes(journal_t *journal);
285 extern int jbd2_journal_has_csum_v2or3(journal_t *journal);
286 extern int tid_gt(tid_t x, tid_t y) EXT2FS_ATTR((unused));
287 extern int tid_geq(tid_t x, tid_t y) EXT2FS_ATTR((unused));
288 #endif
289
290 #if (defined(E2FSCK_INCLUDE_INLINE_FUNCS) || !defined(NO_INLINE_FUNCS))
291 #ifdef E2FSCK_INCLUDE_INLINE_FUNCS
292 #if (__STDC_VERSION__ >= 199901L)
293 #define _INLINE_ extern inline
294 #else
295 #define _INLINE_ inline
296 #endif
297 #else /* !E2FSCK_INCLUDE_INLINE FUNCS */
298 #if (__STDC_VERSION__ >= 199901L)
299 #define _INLINE_ inline
300 #else /* not C99 */
301 #ifdef __GNUC__
302 #define _INLINE_ extern __inline__
303 #else                           /* For Watcom C */
304 #define _INLINE_ extern inline
305 #endif /* __GNUC__ */
306 #endif /* __STDC_VERSION__ >= 199901L */
307 #endif /* INCLUDE_INLINE_FUNCS */
308
309 /* journal feature predicate functions */
310 #define JBD2_FEATURE_COMPAT_FUNCS(name, flagname) \
311 _INLINE_ int jbd2_has_feature_##name(journal_t *j); \
312 _INLINE_ int jbd2_has_feature_##name(journal_t *j) \
313 { \
314         return ((j)->j_format_version >= 2 && \
315                 ((j)->j_superblock->s_feature_compat & \
316                  ext2fs_cpu_to_be32(JBD2_FEATURE_COMPAT_##flagname)) != 0); \
317 } \
318 _INLINE_ void jbd2_set_feature_##name(journal_t *j); \
319 _INLINE_ void jbd2_set_feature_##name(journal_t *j) \
320 { \
321         (j)->j_superblock->s_feature_compat |= \
322                 ext2fs_cpu_to_be32(JBD2_FEATURE_COMPAT_##flagname); \
323 } \
324 _INLINE_ void jbd2_clear_feature_##name(journal_t *j); \
325 _INLINE_ void jbd2_clear_feature_##name(journal_t *j) \
326 { \
327         (j)->j_superblock->s_feature_compat &= \
328                 ~ext2fs_cpu_to_be32(JBD2_FEATURE_COMPAT_##flagname); \
329 }
330
331 #define JBD2_FEATURE_RO_COMPAT_FUNCS(name, flagname) \
332 _INLINE_ int jbd2_has_feature_##name(journal_t *j);     \
333 _INLINE_ int jbd2_has_feature_##name(journal_t *j) \
334 { \
335         return ((j)->j_format_version >= 2 && \
336                 ((j)->j_superblock->s_feature_ro_compat & \
337                  ext2fs_cpu_to_be32(JBD2_FEATURE_RO_COMPAT_##flagname)) != 0); \
338 } \
339 _INLINE_ void jbd2_set_feature_##name(journal_t *j); \
340 _INLINE_ void jbd2_set_feature_##name(journal_t *j) \
341 { \
342         (j)->j_superblock->s_feature_ro_compat |= \
343                 ext2fs_cpu_to_be32(JBD2_FEATURE_RO_COMPAT_##flagname); \
344 } \
345 _INLINE_ void jbd2_clear_feature_##name(journal_t *j); \
346 _INLINE_ void jbd2_clear_feature_##name(journal_t *j) \
347 { \
348         (j)->j_superblock->s_feature_ro_compat &= \
349                 ~ext2fs_cpu_to_be32(JBD2_FEATURE_RO_COMPAT_##flagname); \
350 }
351
352 #define JBD2_FEATURE_INCOMPAT_FUNCS(name, flagname) \
353 _INLINE_ int jbd2_has_feature_##name(journal_t *j); \
354 _INLINE_ int jbd2_has_feature_##name(journal_t *j) \
355 { \
356         return ((j)->j_format_version >= 2 && \
357                 ((j)->j_superblock->s_feature_incompat & \
358                  ext2fs_cpu_to_be32(JBD2_FEATURE_INCOMPAT_##flagname)) != 0); \
359 } \
360 _INLINE_ void jbd2_set_feature_##name(journal_t *j); \
361 _INLINE_ void jbd2_set_feature_##name(journal_t *j) \
362 { \
363         (j)->j_superblock->s_feature_incompat |= \
364                 ext2fs_cpu_to_be32(JBD2_FEATURE_INCOMPAT_##flagname); \
365 } \
366 _INLINE_ void jbd2_clear_feature_##name(journal_t *j); \
367 _INLINE_ void jbd2_clear_feature_##name(journal_t *j) \
368 { \
369         (j)->j_superblock->s_feature_incompat &= \
370                 ~ext2fs_cpu_to_be32(JBD2_FEATURE_INCOMPAT_##flagname); \
371 }
372
373 #else
374 #define JBD2_FEATURE_COMPAT_FUNCS(name, flagname) \
375 extern int jbd2_has_feature_##name(journal_t *j); \
376 extern void jbd2_set_feature_##name(journal_t *j); \
377 extern void jbd2_clear_feature_##name(journal_t *j);
378
379 #define JBD2_FEATURE_RO_COMPAT_FUNCS(name, flagname) \
380 extern int jbd2_has_feature_##name(journal_t *j); \
381 extern void jbd2_set_feature_##name(journal_t *j); \
382 extern void jbd2_clear_feature_##name(journal_t *j);
383
384 #define JBD2_FEATURE_INCOMPAT_FUNCS(name, flagname) \
385 extern int jbd2_has_feature_##name(journal_t *j); \
386 extern void jbd2_set_feature_##name(journal_t *j); \
387 extern void jbd2_clear_feature_##name(journal_t *j);
388
389 #endif /* (defined(E2FSCK_INCLUDE_INLINE_FUNCS) || !defined(NO_INLINE_FUNCS)) */
390
391 JBD2_FEATURE_COMPAT_FUNCS(checksum,             CHECKSUM)
392
393 JBD2_FEATURE_INCOMPAT_FUNCS(revoke,             REVOKE)
394 JBD2_FEATURE_INCOMPAT_FUNCS(64bit,              64BIT)
395 JBD2_FEATURE_INCOMPAT_FUNCS(async_commit,       ASYNC_COMMIT)
396 JBD2_FEATURE_INCOMPAT_FUNCS(csum2,              CSUM_V2)
397 JBD2_FEATURE_INCOMPAT_FUNCS(csum3,              CSUM_V3)
398 JBD2_FEATURE_INCOMPAT_FUNCS(fast_commit,        FAST_COMMIT)
399
400 #if (defined(E2FSCK_INCLUDE_INLINE_FUNCS) || !defined(NO_INLINE_FUNCS))
401 /*
402  * helper functions to deal with 32 or 64bit block numbers.
403  */
404 _INLINE_ size_t journal_tag_bytes(journal_t *journal)
405 {
406         size_t sz;
407
408         if (jbd2_has_feature_csum3(journal))
409                 return sizeof(journal_block_tag3_t);
410
411         sz = sizeof(journal_block_tag_t);
412
413         if (jbd2_has_feature_csum2(journal))
414                 sz += sizeof(__u16);
415
416         if (jbd2_has_feature_64bit(journal))
417                 return sz;
418
419         return sz - sizeof(__u32);
420 }
421
422 _INLINE_ int jbd2_journal_has_csum_v2or3(journal_t *journal)
423 {
424         if (jbd2_has_feature_csum2(journal) || jbd2_has_feature_csum3(journal))
425                 return 1;
426
427         return 0;
428 }
429
430 /* Comparison functions for transaction IDs: perform comparisons using
431  * modulo arithmetic so that they work over sequence number wraps. */
432
433 _INLINE_ int tid_gt(tid_t x, tid_t y)
434 {
435         int difference = (x - y);
436         return (difference > 0);
437 }
438
439 _INLINE_ int tid_geq(tid_t x, tid_t y)
440 {
441         int difference = (x - y);
442         return (difference >= 0);
443 }
444 #endif /* (defined(E2FSCK_INCLUDE_INLINE_FUNCS) || !defined(NO_INLINE_FUNCS)) */
445
446 #undef _INLINE_
447
448 extern int journal_blocks_per_page(struct inode *inode);
449
450 /*
451  * Definitions which augment the buffer_head layer
452  */
453
454 /* journaling buffer types */
455 #define BJ_None         0       /* Not journaled */
456 #define BJ_SyncData     1       /* Normal data: flush before commit */
457 #define BJ_AsyncData    2       /* writepage data: wait on it before commit */
458 #define BJ_Metadata     3       /* Normal journaled metadata */
459 #define BJ_Forget       4       /* Buffer superceded by this transaction */
460 #define BJ_IO           5       /* Buffer is for temporary IO use */
461 #define BJ_Shadow       6       /* Buffer contents being shadowed to the log */
462 #define BJ_LogCtl       7       /* Buffer contains log descriptors */
463 #define BJ_Reserved     8       /* Buffer is reserved for access by journal */
464 #define BJ_Types        9
465
466 extern int jbd_blocks_per_page(struct inode *inode);
467
468 #endif  /* _LINUX_JBD_H */