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