Whamcloud - gitweb
Add fallocate.c to lib/exte2fs/Android.mk
[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 1
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 #define jbd_debug(f, a...)      /**/
50 #else
51 #define jbd_debug(f, ...)       /**/
52 #endif
53 #endif
54 #else
55 #define jbd_debug(x)            /* AIX doesn't do STDC */
56 #endif
57
58 extern void * __jbd_kmalloc (char *where, size_t size, int flags, int retry);
59 #define jbd_kmalloc(size, flags) \
60         __jbd_kmalloc(__FUNCTION__, (size), (flags), journal_oom_retry)
61 #define jbd_rep_kmalloc(size, flags) \
62         __jbd_kmalloc(__FUNCTION__, (size), (flags), 1)
63
64 #define JFS_MIN_JOURNAL_BLOCKS 1024
65
66 /*
67  * Internal structures used by the logging mechanism:
68  */
69
70 #define JFS_MAGIC_NUMBER 0xc03b3998U /* The first 4 bytes of /dev/random! */
71
72 /*
73  * On-disk structures
74  */
75
76 /*
77  * Descriptor block types:
78  */
79
80 #define JFS_DESCRIPTOR_BLOCK    1
81 #define JFS_COMMIT_BLOCK        2
82 #define JFS_SUPERBLOCK_V1       3
83 #define JFS_SUPERBLOCK_V2       4
84 #define JFS_REVOKE_BLOCK        5
85
86 /*
87  * Standard header for all descriptor blocks:
88  */
89 typedef struct journal_header_s
90 {
91         __u32           h_magic;
92         __u32           h_blocktype;
93         __u32           h_sequence;
94 } journal_header_t;
95
96 /*
97  * Checksum types.
98  */
99 #define JBD2_CRC32_CHKSUM   1
100 #define JBD2_MD5_CHKSUM     2
101 #define JBD2_SHA1_CHKSUM    3
102 #define JBD2_CRC32C_CHKSUM  4
103
104 #define JBD2_CRC32_CHKSUM_SIZE 4
105
106 #define JBD2_CHECKSUM_BYTES (32 / sizeof(__u32))
107 /*
108  * Commit block header for storing transactional checksums:
109  *
110  * NOTE: If FEATURE_COMPAT_CHECKSUM (checksum v1) is set, the h_chksum*
111  * fields are used to store a checksum of the descriptor and data blocks.
112  *
113  * If FEATURE_INCOMPAT_CSUM_V2 (checksum v2) is set, then the h_chksum
114  * field is used to store crc32c(uuid+commit_block).  Each journal metadata
115  * block gets its own checksum, and data block checksums are stored in
116  * journal_block_tag (in the descriptor).  The other h_chksum* fields are
117  * not used.
118  *
119  * If FEATURE_INCOMPAT_CSUM_V3 is set, the descriptor block uses
120  * journal_block_tag3_t to store a full 32-bit checksum.  Everything else
121  * is the same as v2.
122  *
123  * Checksum v1, v2, and v3 are mutually exclusive features.
124  */
125 struct commit_header {
126         __u32           h_magic;
127         __u32           h_blocktype;
128         __u32           h_sequence;
129         unsigned char   h_chksum_type;
130         unsigned char   h_chksum_size;
131         unsigned char   h_padding[2];
132         __u32           h_chksum[JBD2_CHECKSUM_BYTES];
133         __u64           h_commit_sec;
134         __u32           h_commit_nsec;
135 };
136
137 /*
138  * The block tag: used to describe a single buffer in the journal
139  */
140 typedef struct journal_block_tag3_s
141 {
142         __u32           t_blocknr;      /* The on-disk block number */
143         __u32           t_flags;        /* See below */
144         __u32           t_blocknr_high; /* most-significant high 32bits. */
145         __u32           t_checksum;     /* crc32c(uuid+seq+block) */
146 } journal_block_tag3_t;
147
148 typedef struct journal_block_tag_s
149 {
150         __u32           t_blocknr;      /* The on-disk block number */
151         __u16           t_checksum;     /* truncated crc32c(uuid+seq+block) */
152         __u16           t_flags;        /* See below */
153         __u32           t_blocknr_high; /* most-significant high 32bits. */
154 } journal_block_tag_t;
155
156 /* Tail of descriptor block, for checksumming */
157 struct journal_block_tail {
158         __be32          t_checksum;
159 };
160
161 /*
162  * The revoke descriptor: used on disk to describe a series of blocks to
163  * be revoked from the log
164  */
165 typedef struct journal_revoke_header_s
166 {
167         journal_header_t r_header;
168         int              r_count;       /* Count of bytes used in the block */
169 } journal_revoke_header_t;
170
171 /* Tail of revoke block, for checksumming */
172 struct journal_revoke_tail {
173         __be32          r_checksum;
174 };
175
176 /* Definitions for the journal tag flags word: */
177 #define JFS_FLAG_ESCAPE         1       /* on-disk block is escaped */
178 #define JFS_FLAG_SAME_UUID      2       /* block has same uuid as previous */
179 #define JFS_FLAG_DELETED        4       /* block deleted by this transaction */
180 #define JFS_FLAG_LAST_TAG       8       /* last tag in this descriptor block */
181
182
183 #define UUID_SIZE 16
184 #define JFS_USERS_MAX 48
185 #define JFS_USERS_SIZE (UUID_SIZE * JFS_USERS_MAX)
186 /*
187  * The journal superblock.  All fields are in big-endian byte order.
188  */
189 typedef struct journal_superblock_s
190 {
191 /* 0x0000 */
192         journal_header_t s_header;
193
194 /* 0x000C */
195         /* Static information describing the journal */
196         __u32   s_blocksize;            /* journal device blocksize */
197         __u32   s_maxlen;               /* total blocks in journal file */
198         __u32   s_first;                /* first block of log information */
199
200 /* 0x0018 */
201         /* Dynamic information describing the current state of the log */
202         __u32   s_sequence;             /* first commit ID expected in log */
203         __u32   s_start;                /* blocknr of start of log */
204
205 /* 0x0020 */
206         /* Error value, as set by journal_abort(). */
207         __s32   s_errno;
208
209 /* 0x0024 */
210         /* Remaining fields are only valid in a version-2 superblock */
211         __u32   s_feature_compat;       /* compatible feature set */
212         __u32   s_feature_incompat;     /* incompatible feature set */
213         __u32   s_feature_ro_compat;    /* readonly-compatible feature set */
214 /* 0x0030 */
215         __u8    s_uuid[16];             /* 128-bit uuid for journal */
216
217 /* 0x0040 */
218         __u32   s_nr_users;             /* Nr of filesystems sharing log */
219
220         __u32   s_dynsuper;             /* Blocknr of dynamic superblock copy*/
221
222 /* 0x0048 */
223         __u32   s_max_transaction;      /* Limit of journal blocks per trans.*/
224         __u32   s_max_trans_data;       /* Limit of data blocks per trans. */
225
226 /* 0x0050 */
227         __u8    s_checksum_type;        /* checksum type */
228         __u8    s_padding2[3];
229         __u32   s_padding[42];
230         __u32   s_checksum;             /* crc32c(superblock) */
231
232 /* 0x0100 */
233         __u8    s_users[JFS_USERS_SIZE];                /* ids of all fs'es sharing the log */
234
235 /* 0x0400 */
236 } journal_superblock_t;
237
238 #define JFS_HAS_COMPAT_FEATURE(j,mask)                                  \
239         ((j)->j_format_version >= 2 &&                                  \
240          ((j)->j_superblock->s_feature_compat & ext2fs_cpu_to_be32((mask))))
241 #define JFS_HAS_RO_COMPAT_FEATURE(j,mask)                               \
242         ((j)->j_format_version >= 2 &&                                  \
243          ((j)->j_superblock->s_feature_ro_compat & ext2fs_cpu_to_be32((mask))))
244 #define JFS_HAS_INCOMPAT_FEATURE(j,mask)                                \
245         ((j)->j_format_version >= 2 &&                                  \
246          ((j)->j_superblock->s_feature_incompat & ext2fs_cpu_to_be32((mask))))
247
248 #define JFS_FEATURE_COMPAT_CHECKSUM     0x00000001
249
250 #define JFS_FEATURE_INCOMPAT_REVOKE             0x00000001
251 #define JFS_FEATURE_INCOMPAT_64BIT              0x00000002
252 #define JFS_FEATURE_INCOMPAT_ASYNC_COMMIT       0x00000004
253 #define JFS_FEATURE_INCOMPAT_CSUM_V2            0x00000008
254 #define JFS_FEATURE_INCOMPAT_CSUM_V3            0x00000010
255
256 /* Features known to this kernel version: */
257 #define JFS_KNOWN_COMPAT_FEATURES       0
258 #define JFS_KNOWN_ROCOMPAT_FEATURES     0
259 #define JFS_KNOWN_INCOMPAT_FEATURES     (JFS_FEATURE_INCOMPAT_REVOKE|\
260                                          JFS_FEATURE_INCOMPAT_ASYNC_COMMIT|\
261                                          JFS_FEATURE_INCOMPAT_64BIT|\
262                                          JFS_FEATURE_INCOMPAT_CSUM_V2|\
263                                          JFS_FEATURE_INCOMPAT_CSUM_V3)
264
265 #if (defined(E2FSCK_INCLUDE_INLINE_FUNCS) || !defined(NO_INLINE_FUNCS))
266 #ifdef E2FSCK_INCLUDE_INLINE_FUNCS
267 #if (__STDC_VERSION__ >= 199901L)
268 #define _INLINE_ extern inline
269 #else
270 #define _INLINE_ inline
271 #endif
272 #else /* !E2FSCK_INCLUDE_INLINE FUNCS */
273 #if (__STDC_VERSION__ >= 199901L)
274 #define _INLINE_ inline
275 #else /* not C99 */
276 #ifdef __GNUC__
277 #define _INLINE_ extern __inline__
278 #else                           /* For Watcom C */
279 #define _INLINE_ extern inline
280 #endif /* __GNUC__ */
281 #endif /* __STDC_VERSION__ >= 199901L */
282 #endif /* INCLUDE_INLINE_FUNCS */
283
284 /*
285  * helper functions to deal with 32 or 64bit block numbers.
286  */
287 _INLINE_ size_t journal_tag_bytes(journal_t *journal)
288 {
289         size_t sz;
290
291         if (JFS_HAS_INCOMPAT_FEATURE(journal, JFS_FEATURE_INCOMPAT_CSUM_V3))
292                 return sizeof(journal_block_tag3_t);
293
294         sz = sizeof(journal_block_tag_t);
295
296         if (JFS_HAS_INCOMPAT_FEATURE(journal, JFS_FEATURE_INCOMPAT_CSUM_V2))
297                 sz += sizeof(__u16);
298
299         if (JFS_HAS_INCOMPAT_FEATURE(journal, JFS_FEATURE_INCOMPAT_64BIT))
300                 return sz;
301
302         return sz - sizeof(__u32);
303 }
304
305 _INLINE_ int journal_has_csum_v2or3(journal_t *journal)
306 {
307         if (JFS_HAS_INCOMPAT_FEATURE(journal, JFS_FEATURE_INCOMPAT_CSUM_V2) ||
308             JFS_HAS_INCOMPAT_FEATURE(journal, JFS_FEATURE_INCOMPAT_CSUM_V3))
309                 return 1;
310
311         return 0;
312 }
313 #undef _INLINE_
314 #endif
315
316 static inline int tid_gt(tid_t x, tid_t y) EXT2FS_ATTR((unused));
317 static inline int tid_geq(tid_t x, tid_t y) EXT2FS_ATTR((unused));
318
319 /* Comparison functions for transaction IDs: perform comparisons using
320  * modulo arithmetic so that they work over sequence number wraps. */
321
322 static inline int tid_gt(tid_t x, tid_t y)
323 {
324         int difference = (x - y);
325         return (difference > 0);
326 }
327
328 static inline int tid_geq(tid_t x, tid_t y)
329 {
330         int difference = (x - y);
331         return (difference >= 0);
332 }
333
334 extern int journal_blocks_per_page(struct inode *inode);
335
336 /*
337  * Definitions which augment the buffer_head layer
338  */
339
340 /* journaling buffer types */
341 #define BJ_None         0       /* Not journaled */
342 #define BJ_SyncData     1       /* Normal data: flush before commit */
343 #define BJ_AsyncData    2       /* writepage data: wait on it before commit */
344 #define BJ_Metadata     3       /* Normal journaled metadata */
345 #define BJ_Forget       4       /* Buffer superceded by this transaction */
346 #define BJ_IO           5       /* Buffer is for temporary IO use */
347 #define BJ_Shadow       6       /* Buffer contents being shadowed to the log */
348 #define BJ_LogCtl       7       /* Buffer contains log descriptors */
349 #define BJ_Reserved     8       /* Buffer is reserved for access by journal */
350 #define BJ_Types        9
351
352 extern int jbd_blocks_per_page(struct inode *inode);
353
354 #endif  /* _LINUX_JBD_H */