]> git.saurik.com Git - apple/xnu.git/blob - bsd/vfs/vfs_journal.h
xnu-1456.1.26.tar.gz
[apple/xnu.git] / bsd / vfs / vfs_journal.h
1
2 /*
3 * Copyright (c) 2000-2007 Apple Inc. All rights reserved.
4 *
5 * @APPLE_OSREFERENCE_LICENSE_HEADER_START@
6 *
7 * This file contains Original Code and/or Modifications of Original Code
8 * as defined in and that are subject to the Apple Public Source License
9 * Version 2.0 (the 'License'). You may not use this file except in
10 * compliance with the License. The rights granted to you under the License
11 * may not be used to create, or enable the creation or redistribution of,
12 * unlawful or unlicensed copies of an Apple operating system, or to
13 * circumvent, violate, or enable the circumvention or violation of, any
14 * terms of an Apple operating system software license agreement.
15 *
16 * Please obtain a copy of the License at
17 * http://www.opensource.apple.com/apsl/ and read it before using this file.
18 *
19 * The Original Code and all software distributed under the License are
20 * distributed on an 'AS IS' basis, WITHOUT WARRANTY OF ANY KIND, EITHER
21 * EXPRESS OR IMPLIED, AND APPLE HEREBY DISCLAIMS ALL SUCH WARRANTIES,
22 * INCLUDING WITHOUT LIMITATION, ANY WARRANTIES OF MERCHANTABILITY,
23 * FITNESS FOR A PARTICULAR PURPOSE, QUIET ENJOYMENT OR NON-INFRINGEMENT.
24 * Please see the License for the specific language governing rights and
25 * limitations under the License.
26 *
27 * @APPLE_OSREFERENCE_LICENSE_HEADER_END@
28 */
29 /*
30 * This header contains the structures and function prototypes
31 * for the vfs journaling code. The data types are not meant
32 * to be modified by user code. Just use the functions and do
33 * not mess around with the structs.
34 */
35 #ifndef _SYS_VFS_JOURNAL_H_
36 #define _SYS_VFS_JOURNAL_H_
37
38 #include <sys/appleapiopts.h>
39 #include <sys/cdefs.h>
40
41 #ifdef __APPLE_API_UNSTABLE
42
43 #include <sys/types.h>
44 #include <kern/locks.h>
45
46 typedef struct _blk_info {
47 int32_t bsize;
48 union {
49 int32_t cksum;
50 uint32_t sequence_num;
51 } b;
52 } _blk_info;
53
54 typedef struct block_info {
55 off_t bnum; // block # on the file system device
56 union {
57 _blk_info bi;
58 struct buf *bp;
59 } u;
60 } __attribute__((__packed__)) block_info;
61
62 typedef struct block_list_header {
63 u_int16_t max_blocks; // max number of blocks in this chunk
64 u_int16_t num_blocks; // number of valid block numbers in block_nums
65 int32_t bytes_used; // how many bytes of this tbuffer are used
66 int32_t checksum; // on-disk: checksum of this header and binfo[0]
67 int32_t flags; // check-checksums, initial blhdr, etc
68 block_info binfo[1]; // so we can reference them by name
69 } block_list_header;
70
71 #define BLHDR_CHECK_CHECKSUMS 0x0001
72 #define BLHDR_FIRST_HEADER 0x0002
73
74
75 struct journal;
76
77 typedef struct transaction {
78 int tbuffer_size; // in bytes
79 char *tbuffer; // memory copy of the transaction
80 block_list_header *blhdr; // points to the first byte of tbuffer
81 int num_blhdrs; // how many buffers we've allocated
82 int total_bytes; // total # of bytes in transaction
83 int num_flushed; // how many bytes have been flushed
84 int num_killed; // how many bytes were "killed"
85 off_t journal_start; // where in the journal this transaction starts
86 off_t journal_end; // where in the journal this transaction ends
87 struct journal *jnl; // ptr back to the journal structure
88 struct transaction *next; // list of tr's (either completed or to be free'd)
89 uint32_t sequence_num;
90 } transaction;
91
92
93 /*
94 * This is written to block zero of the journal and it
95 * maintains overall state about the journal.
96 */
97 typedef struct journal_header {
98 int32_t magic;
99 int32_t endian;
100 volatile off_t start; // zero-based byte offset of the start of the first transaction
101 volatile off_t end; // zero-based byte offset of where free space begins
102 off_t size; // size in bytes of the entire journal
103 int32_t blhdr_size; // size in bytes of each block_list_header in the journal
104 int32_t checksum;
105 int32_t jhdr_size; // block size (in bytes) of the journal header
106 uint32_t sequence_num; // NEW FIELD: a monotonically increasing value assigned to all txn's
107 } journal_header;
108
109 #define JOURNAL_HEADER_MAGIC 0x4a4e4c78 // 'JNLx'
110 #define ENDIAN_MAGIC 0x12345678
111
112 //
113 // we only checksum the original size of the journal_header to remain
114 // backwards compatible. the size of the original journal_heade is
115 // everything up to the the sequence_num field, hence we use the
116 // offsetof macro to calculate the size.
117 //
118 #define JOURNAL_HEADER_CKSUM_SIZE (offsetof(struct journal_header, sequence_num))
119
120 #define OLD_JOURNAL_HEADER_MAGIC 0x4a484452 // 'JHDR'
121
122
123 /*
124 * In memory structure about the journal.
125 */
126 typedef struct journal {
127 lck_mtx_t jlock; // protects the struct journal data
128
129 struct vnode *jdev; // vnode of the device where the journal lives
130 off_t jdev_offset; // byte offset to the start of the journal
131 const char *jdev_name;
132
133 struct vnode *fsdev; // vnode of the file system device
134
135 void (*flush)(void *arg); // fs callback to flush meta data blocks
136 void *flush_arg; // arg that's passed to flush()
137
138 int32_t flags;
139 int32_t tbuffer_size; // default transaction buffer size
140
141 char *header_buf; // in-memory copy of the journal header
142 int32_t header_buf_size;
143 journal_header *jhdr; // points to the first byte of header_buf
144
145 off_t max_read_size;
146 off_t max_write_size;
147
148 transaction *cur_tr; // for group-commit
149 transaction *completed_trs; // out-of-order transactions that completed
150 transaction *active_tr; // for nested transactions
151 int32_t nested_count; // for nested transactions
152 void *owner; // a ptr that's unique to the calling process
153
154 transaction *tr_freeme; // transaction structs that need to be free'd
155
156 volatile off_t active_start; // the active start that we only keep in memory
157 lck_mtx_t old_start_lock; // protects the old_start
158 volatile off_t old_start[16]; // this is how we do lazy start update
159
160 int last_flush_err; // last error from flushing the cache
161 } journal;
162
163 /* internal-only journal flags (top 16 bits) */
164 #define JOURNAL_CLOSE_PENDING 0x00010000
165 #define JOURNAL_INVALID 0x00020000
166 #define JOURNAL_FLUSHCACHE_ERR 0x00040000 // means we already printed this err
167 #define JOURNAL_NEED_SWAP 0x00080000 // swap any data read from disk
168 #define JOURNAL_DO_FUA_WRITES 0x00100000 // do force-unit-access writes
169
170 /* journal_open/create options are always in the low-16 bits */
171 #define JOURNAL_OPTION_FLAGS_MASK 0x0000ffff
172
173 __BEGIN_DECLS
174 /*
175 * Prototypes.
176 */
177
178 /*
179 * Call journal_init() to initialize the journaling code (sets up lock attributes)
180 */
181 void journal_init(void) __attribute__((section("__TEXT, initcode")));
182
183 /*
184 * Call journal_create() to create a new journal. You only
185 * call this once, typically at file system creation time.
186 *
187 * The "jvp" argument is the vnode where the journal is written.
188 * The journal starts at "offset" and is "journal_size" bytes long.
189 *
190 * The "fsvp" argument is the vnode of your file system. It may be
191 * the same as "jvp".
192 *
193 * The "min_fs_block_size" argument is the minimum block size
194 * (in bytes) that the file system will ever write. Typically
195 * this is the block size of the file system (1k, 4k, etc) but
196 * on HFS+ it is the minimum block size of the underlying device.
197 *
198 * The flags argument lets you disable group commit if you
199 * want tighter guarantees on transactions (in exchange for
200 * lower performance).
201 *
202 * The tbuffer_size is the size of the transaction buffer
203 * used by the journal. If you specify zero, the journal code
204 * will use a reasonable defaults. The tbuffer_size should
205 * be an integer multiple of the min_fs_block_size.
206 *
207 * Returns a valid journal pointer or NULL if one could not
208 * be created.
209 */
210 journal *journal_create(struct vnode *jvp,
211 off_t offset,
212 off_t journal_size,
213 struct vnode *fsvp,
214 size_t min_fs_block_size,
215 int32_t flags,
216 int32_t tbuffer_size,
217 void (*flush)(void *arg),
218 void *arg);
219
220 /*
221 * Call journal_open() when mounting an existing file system
222 * that has a previously created journal. It will take care
223 * of validating the journal and replaying it if necessary.
224 *
225 * See journal_create() for a description of the arguments.
226 *
227 * Returns a valid journal pointer of NULL if it runs into
228 * trouble reading/playing back the journal.
229 */
230 journal *journal_open(struct vnode *jvp,
231 off_t offset,
232 off_t journal_size,
233 struct vnode *fsvp,
234 size_t min_fs_block_size,
235 int32_t flags,
236 int32_t tbuffer_size,
237 void (*flush)(void *arg),
238 void *arg);
239
240 /*
241 * Test whether the journal is clean or not. This is intended
242 * to be used when you're mounting read-only. If the journal
243 * is not clean for some reason then you should not mount the
244 * volume as your data structures may be in an unknown state.
245 */
246 int journal_is_clean(struct vnode *jvp,
247 off_t offset,
248 off_t journal_size,
249 struct vnode *fsvp,
250 size_t min_fs_block_size);
251
252
253 /*
254 * Call journal_close() just before your file system is unmounted.
255 * It flushes any outstanding transactions and makes sure the
256 * journal is in a consistent state.
257 */
258 void journal_close(journal *journalp);
259
260 /*
261 * flags for journal_create/open. only can use
262 * the low 16 bits for flags because internal
263 * bits go in the high 16.
264 */
265 #define JOURNAL_NO_GROUP_COMMIT 0x00000001
266 #define JOURNAL_RESET 0x00000002
267
268 /*
269 * Transaction related functions.
270 *
271 * Before you start modifying file system meta data, you
272 * should call journal_start_transaction(). Then before
273 * you modify each block, call journal_modify_block_start()
274 * and when you're done, journal_modify_block_end(). When
275 * you've modified the last block as part of a transaction,
276 * call journal_end_transaction() to commit the changes.
277 *
278 * If you decide to abort the modifications to a block you
279 * should call journal_modify_block_abort().
280 *
281 * If as part of a transaction you need want to throw out
282 * any previous copies of a block (because it got deleted)
283 * then call journal_kill_block(). This will mark it so
284 * that the journal does not play it back (effectively
285 * dropping it).
286 */
287 int journal_start_transaction(journal *jnl);
288 int journal_modify_block_start(journal *jnl, struct buf *bp);
289 int journal_modify_block_abort(journal *jnl, struct buf *bp);
290 int journal_modify_block_end(journal *jnl, struct buf *bp, void (*func)(struct buf *bp, void *arg), void *arg);
291 int journal_kill_block(journal *jnl, struct buf *bp);
292 int journal_end_transaction(journal *jnl);
293
294 int journal_active(journal *jnl);
295 int journal_flush(journal *jnl);
296 void *journal_owner(journal *jnl); // compare against current_thread()
297 int journal_uses_fua(journal *jnl);
298
299
300 /*
301 * Relocate the journal.
302 *
303 * You provide the new starting offset and size for the journal. You may
304 * optionally provide a new tbuffer_size; passing zero defaults to not
305 * changing the tbuffer size except as needed to fit within the new journal
306 * size.
307 *
308 * You must have already started a transaction. The transaction may contain
309 * modified blocks (such as those needed to deallocate the old journal,
310 * allocate the new journal, and update the location and size of the journal
311 * in filesystem-private structures). Any transactions prior to the active
312 * transaction will be flushed to the old journal. The new journal will be
313 * initialized, and the blocks from the active transaction will be written to
314 * the new journal. The caller will need to update the structures that
315 * identify the location and size of the journal from the callback routine.
316 */
317 int journal_relocate(journal *jnl, off_t offset, off_t journal_size, int32_t tbuffer_size,
318 errno_t (*callback)(void *), void *callback_arg);
319
320 __END_DECLS
321
322 #endif /* __APPLE_API_UNSTABLE */
323 #endif /* !_SYS_VFS_JOURNAL_H_ */