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