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