]> git.saurik.com Git - apple/xnu.git/blob - bsd/vfs/vfs_journal.h
xnu-792.18.15.tar.gz
[apple/xnu.git] / bsd / vfs / vfs_journal.h
1
2 /*
3 * Copyright (c) 2000-2002 Apple Computer, 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 block_info {
47 off_t bnum; // block # on the file system device
48 size_t bsize; // in bytes
49 struct buf *bp;
50 } block_info;
51
52 typedef struct block_list_header {
53 u_int16_t max_blocks; // max number of blocks in this chunk
54 u_int16_t num_blocks; // number of valid block numbers in block_nums
55 int32_t bytes_used; // how many bytes of this tbuffer are used
56 int32_t checksum; // on-disk: checksum of this header and binfo[0]
57 int32_t pad; // pad out to 16 bytes
58 block_info binfo[1]; // so we can reference them by name
59 } block_list_header;
60
61
62 struct journal;
63
64 typedef struct transaction {
65 int tbuffer_size; // in bytes
66 char *tbuffer; // memory copy of the transaction
67 block_list_header *blhdr; // points to the first byte of tbuffer
68 int num_blhdrs; // how many buffers we've allocated
69 int total_bytes; // total # of bytes in transaction
70 int num_flushed; // how many bytes have been flushed
71 int num_killed; // how many bytes were "killed"
72 off_t journal_start; // where in the journal this transaction starts
73 off_t journal_end; // where in the journal this transaction ends
74 struct journal *jnl; // ptr back to the journal structure
75 struct transaction *next; // list of tr's (either completed or to be free'd)
76 } transaction;
77
78
79 /*
80 * This is written to block zero of the journal and it
81 * maintains overall state about the journal.
82 */
83 typedef struct journal_header {
84 int32_t magic;
85 int32_t endian;
86 volatile off_t start; // zero-based byte offset of the start of the first transaction
87 volatile off_t end; // zero-based byte offset of where free space begins
88 off_t size; // size in bytes of the entire journal
89 int32_t blhdr_size; // size in bytes of each block_list_header in the journal
90 int32_t checksum;
91 int32_t jhdr_size; // block size (in bytes) of the journal header
92 } journal_header;
93
94 #define JOURNAL_HEADER_MAGIC 0x4a4e4c78 // 'JNLx'
95 #define ENDIAN_MAGIC 0x12345678
96
97 #define OLD_JOURNAL_HEADER_MAGIC 0x4a484452 // 'JHDR'
98
99
100 /*
101 * In memory structure about the journal.
102 */
103 typedef struct journal {
104 lck_mtx_t jlock; // protects the struct journal data
105
106 struct vnode *jdev; // vnode of the device where the journal lives
107 off_t jdev_offset; // byte offset to the start of the journal
108
109 struct vnode *fsdev; // vnode of the file system device
110
111 void (*flush)(void *arg); // fs callback to flush meta data blocks
112 void *flush_arg; // arg that's passed to flush()
113
114 int32_t flags;
115 int32_t tbuffer_size; // default transaction buffer size
116
117 char *header_buf; // in-memory copy of the journal header
118 journal_header *jhdr; // points to the first byte of header_buf
119
120 transaction *cur_tr; // for group-commit
121 transaction *completed_trs; // out-of-order transactions that completed
122 transaction *active_tr; // for nested transactions
123 int32_t nested_count; // for nested transactions
124 void *owner; // a ptr that's unique to the calling process
125
126 transaction *tr_freeme; // transaction structs that need to be free'd
127
128 volatile off_t active_start; // the active start that we only keep in memory
129 lck_mtx_t old_start_lock; // protects the old_start
130 volatile off_t old_start[16]; // this is how we do lazy start update
131
132 int last_flush_err; // last error from flushing the cache
133 } journal;
134
135 /* internal-only journal flags (top 16 bits) */
136 #define JOURNAL_CLOSE_PENDING 0x00010000
137 #define JOURNAL_INVALID 0x00020000
138 #define JOURNAL_FLUSHCACHE_ERR 0x00040000 // means we already printed this err
139 #define JOURNAL_NEED_SWAP 0x00080000 // swap any data read from disk
140
141 /* journal_open/create options are always in the low-16 bits */
142 #define JOURNAL_OPTION_FLAGS_MASK 0x0000ffff
143
144 __BEGIN_DECLS
145 /*
146 * Prototypes.
147 */
148
149 /*
150 * Call journal_init() to initialize the journaling code (sets up lock attributes)
151 */
152 void journal_init(void);
153
154 /*
155 * Call journal_create() to create a new journal. You only
156 * call this once, typically at file system creation time.
157 *
158 * The "jvp" argument is the vnode where the journal is written.
159 * The journal starts at "offset" and is "journal_size" bytes long.
160 *
161 * The "fsvp" argument is the vnode of your file system. It may be
162 * the same as "jvp".
163 *
164 * The "min_fs_block_size" argument is the minimum block size
165 * (in bytes) that the file system will ever write. Typically
166 * this is the block size of the file system (1k, 4k, etc) but
167 * on HFS+ it is the minimum block size of the underlying device.
168 *
169 * The flags argument lets you disable group commit if you
170 * want tighter guarantees on transactions (in exchange for
171 * lower performance).
172 *
173 * The tbuffer_size is the size of the transaction buffer
174 * used by the journal. If you specify zero, the journal code
175 * will use a reasonable defaults. The tbuffer_size should
176 * be an integer multiple of the min_fs_block_size.
177 *
178 * Returns a valid journal pointer or NULL if one could not
179 * be created.
180 */
181 journal *journal_create(struct vnode *jvp,
182 off_t offset,
183 off_t journal_size,
184 struct vnode *fsvp,
185 size_t min_fs_block_size,
186 int32_t flags,
187 int32_t tbuffer_size,
188 void (*flush)(void *arg),
189 void *arg);
190
191 /*
192 * Call journal_open() when mounting an existing file system
193 * that has a previously created journal. It will take care
194 * of validating the journal and replaying it if necessary.
195 *
196 * See journal_create() for a description of the arguments.
197 *
198 * Returns a valid journal pointer of NULL if it runs into
199 * trouble reading/playing back the journal.
200 */
201 journal *journal_open(struct vnode *jvp,
202 off_t offset,
203 off_t journal_size,
204 struct vnode *fsvp,
205 size_t min_fs_block_size,
206 int32_t flags,
207 int32_t tbuffer_size,
208 void (*flush)(void *arg),
209 void *arg);
210
211 /*
212 * Test whether the journal is clean or not. This is intended
213 * to be used when you're mounting read-only. If the journal
214 * is not clean for some reason then you should not mount the
215 * volume as your data structures may be in an unknown state.
216 */
217 int journal_is_clean(struct vnode *jvp,
218 off_t offset,
219 off_t journal_size,
220 struct vnode *fsvp,
221 size_t min_fs_block_size);
222
223
224 /*
225 * Call journal_close() just before your file system is unmounted.
226 * It flushes any outstanding transactions and makes sure the
227 * journal is in a consistent state.
228 */
229 void journal_close(journal *journalp);
230
231 /*
232 * flags for journal_create/open. only can use
233 * the low 16 bits for flags because internal
234 * bits go in the high 16.
235 */
236 #define JOURNAL_NO_GROUP_COMMIT 0x00000001
237 #define JOURNAL_RESET 0x00000002
238
239 /*
240 * Transaction related functions.
241 *
242 * Before you start modifying file system meta data, you
243 * should call journal_start_transaction(). Then before
244 * you modify each block, call journal_modify_block_start()
245 * and when you're done, journal_modify_block_end(). When
246 * you've modified the last block as part of a transaction,
247 * call journal_end_transaction() to commit the changes.
248 *
249 * If you decide to abort the modifications to a block you
250 * should call journal_modify_block_abort().
251 *
252 * If as part of a transaction you need want to throw out
253 * any previous copies of a block (because it got deleted)
254 * then call journal_kill_block(). This will mark it so
255 * that the journal does not play it back (effectively
256 * dropping it).
257 */
258 int journal_start_transaction(journal *jnl);
259 int journal_modify_block_start(journal *jnl, struct buf *bp);
260 int journal_modify_block_abort(journal *jnl, struct buf *bp);
261 int journal_modify_block_end(journal *jnl, struct buf *bp);
262 int journal_kill_block(journal *jnl, struct buf *bp);
263 int journal_end_transaction(journal *jnl);
264
265 int journal_active(journal *jnl);
266 int journal_flush(journal *jnl);
267 void *journal_owner(journal *jnl); // compare against current_thread()
268
269 __END_DECLS
270
271 #endif /* __APPLE_API_UNSTABLE */
272 #endif /* !_SYS_VFS_JOURNAL_H_ */