]>
git.saurik.com Git - apple/xnu.git/blob - bsd/vfs/vfs_journal.h
3 * Copyright (c) 2000-2002 Apple Computer, Inc. All rights reserved.
5 * @APPLE_LICENSE_OSREFERENCE_HEADER_START@
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
11 * License may not be used to create, or enable the creation or
12 * redistribution of, unlawful or unlicensed copies of an Apple operating
13 * system, or to circumvent, violate, or enable the circumvention or
14 * violation of, any terms of an Apple operating system software license
17 * Please obtain a copy of the License at
18 * http://www.opensource.apple.com/apsl/ and read it before using this
21 * The Original Code and all software distributed under the License are
22 * distributed on an 'AS IS' basis, WITHOUT WARRANTY OF ANY KIND, EITHER
23 * EXPRESS OR IMPLIED, AND APPLE HEREBY DISCLAIMS ALL SUCH WARRANTIES,
24 * INCLUDING WITHOUT LIMITATION, ANY WARRANTIES OF MERCHANTABILITY,
25 * FITNESS FOR A PARTICULAR PURPOSE, QUIET ENJOYMENT OR NON-INFRINGEMENT.
26 * Please see the License for the specific language governing rights and
27 * limitations under the License.
29 * @APPLE_LICENSE_OSREFERENCE_HEADER_END@
32 * This header contains the structures and function prototypes
33 * for the vfs journaling code. The data types are not meant
34 * to be modified by user code. Just use the functions and do
35 * not mess around with the structs.
37 #ifndef _SYS_VFS_JOURNAL_H_
38 #define _SYS_VFS_JOURNAL_H_
40 #include <sys/appleapiopts.h>
41 #include <sys/cdefs.h>
43 #ifdef __APPLE_API_UNSTABLE
45 #include <sys/types.h>
46 #include <kern/locks.h>
48 typedef struct block_info
{
49 off_t bnum
; // block # on the file system device
50 size_t bsize
; // in bytes
54 typedef struct block_list_header
{
55 u_int16_t max_blocks
; // max number of blocks in this chunk
56 u_int16_t num_blocks
; // number of valid block numbers in block_nums
57 int32_t bytes_used
; // how many bytes of this tbuffer are used
58 int32_t checksum
; // on-disk: checksum of this header and binfo[0]
59 int32_t pad
; // pad out to 16 bytes
60 block_info binfo
[1]; // so we can reference them by name
66 typedef struct transaction
{
67 int tbuffer_size
; // in bytes
68 char *tbuffer
; // memory copy of the transaction
69 block_list_header
*blhdr
; // points to the first byte of tbuffer
70 int num_blhdrs
; // how many buffers we've allocated
71 int total_bytes
; // total # of bytes in transaction
72 int num_flushed
; // how many bytes have been flushed
73 int num_killed
; // how many bytes were "killed"
74 off_t journal_start
; // where in the journal this transaction starts
75 off_t journal_end
; // where in the journal this transaction ends
76 struct journal
*jnl
; // ptr back to the journal structure
77 struct transaction
*next
; // list of tr's (either completed or to be free'd)
82 * This is written to block zero of the journal and it
83 * maintains overall state about the journal.
85 typedef struct journal_header
{
88 volatile off_t start
; // zero-based byte offset of the start of the first transaction
89 volatile off_t end
; // zero-based byte offset of where free space begins
90 off_t size
; // size in bytes of the entire journal
91 int32_t blhdr_size
; // size in bytes of each block_list_header in the journal
93 int32_t jhdr_size
; // block size (in bytes) of the journal header
96 #define JOURNAL_HEADER_MAGIC 0x4a4e4c78 // 'JNLx'
97 #define ENDIAN_MAGIC 0x12345678
99 #define OLD_JOURNAL_HEADER_MAGIC 0x4a484452 // 'JHDR'
103 * In memory structure about the journal.
105 typedef struct journal
{
106 lck_mtx_t jlock
; // protects the struct journal data
108 struct vnode
*jdev
; // vnode of the device where the journal lives
109 off_t jdev_offset
; // byte offset to the start of the journal
111 struct vnode
*fsdev
; // vnode of the file system device
113 void (*flush
)(void *arg
); // fs callback to flush meta data blocks
114 void *flush_arg
; // arg that's passed to flush()
117 int32_t tbuffer_size
; // default transaction buffer size
119 char *header_buf
; // in-memory copy of the journal header
120 journal_header
*jhdr
; // points to the first byte of header_buf
122 transaction
*cur_tr
; // for group-commit
123 transaction
*completed_trs
; // out-of-order transactions that completed
124 transaction
*active_tr
; // for nested transactions
125 int32_t nested_count
; // for nested transactions
126 void *owner
; // a ptr that's unique to the calling process
128 transaction
*tr_freeme
; // transaction structs that need to be free'd
130 volatile off_t active_start
; // the active start that we only keep in memory
131 lck_mtx_t old_start_lock
; // protects the old_start
132 volatile off_t old_start
[16]; // this is how we do lazy start update
134 int last_flush_err
; // last error from flushing the cache
137 /* internal-only journal flags (top 16 bits) */
138 #define JOURNAL_CLOSE_PENDING 0x00010000
139 #define JOURNAL_INVALID 0x00020000
140 #define JOURNAL_FLUSHCACHE_ERR 0x00040000 // means we already printed this err
141 #define JOURNAL_NEED_SWAP 0x00080000 // swap any data read from disk
143 /* journal_open/create options are always in the low-16 bits */
144 #define JOURNAL_OPTION_FLAGS_MASK 0x0000ffff
152 * Call journal_init() to initialize the journaling code (sets up lock attributes)
154 void journal_init(void);
157 * Call journal_create() to create a new journal. You only
158 * call this once, typically at file system creation time.
160 * The "jvp" argument is the vnode where the journal is written.
161 * The journal starts at "offset" and is "journal_size" bytes long.
163 * The "fsvp" argument is the vnode of your file system. It may be
166 * The "min_fs_block_size" argument is the minimum block size
167 * (in bytes) that the file system will ever write. Typically
168 * this is the block size of the file system (1k, 4k, etc) but
169 * on HFS+ it is the minimum block size of the underlying device.
171 * The flags argument lets you disable group commit if you
172 * want tighter guarantees on transactions (in exchange for
173 * lower performance).
175 * The tbuffer_size is the size of the transaction buffer
176 * used by the journal. If you specify zero, the journal code
177 * will use a reasonable defaults. The tbuffer_size should
178 * be an integer multiple of the min_fs_block_size.
180 * Returns a valid journal pointer or NULL if one could not
183 journal
*journal_create(struct vnode
*jvp
,
187 size_t min_fs_block_size
,
189 int32_t tbuffer_size
,
190 void (*flush
)(void *arg
),
194 * Call journal_open() when mounting an existing file system
195 * that has a previously created journal. It will take care
196 * of validating the journal and replaying it if necessary.
198 * See journal_create() for a description of the arguments.
200 * Returns a valid journal pointer of NULL if it runs into
201 * trouble reading/playing back the journal.
203 journal
*journal_open(struct vnode
*jvp
,
207 size_t min_fs_block_size
,
209 int32_t tbuffer_size
,
210 void (*flush
)(void *arg
),
214 * Test whether the journal is clean or not. This is intended
215 * to be used when you're mounting read-only. If the journal
216 * is not clean for some reason then you should not mount the
217 * volume as your data structures may be in an unknown state.
219 int journal_is_clean(struct vnode
*jvp
,
223 size_t min_fs_block_size
);
227 * Call journal_close() just before your file system is unmounted.
228 * It flushes any outstanding transactions and makes sure the
229 * journal is in a consistent state.
231 void journal_close(journal
*journalp
);
234 * flags for journal_create/open. only can use
235 * the low 16 bits for flags because internal
236 * bits go in the high 16.
238 #define JOURNAL_NO_GROUP_COMMIT 0x00000001
239 #define JOURNAL_RESET 0x00000002
242 * Transaction related functions.
244 * Before you start modifying file system meta data, you
245 * should call journal_start_transaction(). Then before
246 * you modify each block, call journal_modify_block_start()
247 * and when you're done, journal_modify_block_end(). When
248 * you've modified the last block as part of a transaction,
249 * call journal_end_transaction() to commit the changes.
251 * If you decide to abort the modifications to a block you
252 * should call journal_modify_block_abort().
254 * If as part of a transaction you need want to throw out
255 * any previous copies of a block (because it got deleted)
256 * then call journal_kill_block(). This will mark it so
257 * that the journal does not play it back (effectively
260 int journal_start_transaction(journal
*jnl
);
261 int journal_modify_block_start(journal
*jnl
, struct buf
*bp
);
262 int journal_modify_block_abort(journal
*jnl
, struct buf
*bp
);
263 int journal_modify_block_end(journal
*jnl
, struct buf
*bp
);
264 int journal_kill_block(journal
*jnl
, struct buf
*bp
);
265 int journal_end_transaction(journal
*jnl
);
267 int journal_active(journal
*jnl
);
268 int journal_flush(journal
*jnl
);
269 void *journal_owner(journal
*jnl
); // compare against current_thread()
273 #endif /* __APPLE_API_UNSTABLE */
274 #endif /* !_SYS_VFS_JOURNAL_H_ */