]>
Commit | Line | Data |
---|---|---|
b4c24cb9 | 1 | /* |
91447636 | 2 | * Copyright (c) 1995-2004 Apple Computer, Inc. All rights reserved. |
b4c24cb9 | 3 | * |
6601e61a | 4 | * @APPLE_LICENSE_HEADER_START@ |
b4c24cb9 | 5 | * |
6601e61a A |
6 | * The contents of this file constitute Original Code as defined in and |
7 | * are subject to the Apple Public Source License Version 1.1 (the | |
8 | * "License"). You may not use this file except in compliance with the | |
9 | * License. Please obtain a copy of the License at | |
10 | * http://www.apple.com/publicsource and read it before using this file. | |
8f6c56a5 | 11 | * |
6601e61a A |
12 | * This Original Code and all software distributed under the License are |
13 | * distributed on an "AS IS" basis, WITHOUT WARRANTY OF ANY KIND, EITHER | |
8f6c56a5 A |
14 | * EXPRESS OR IMPLIED, AND APPLE HEREBY DISCLAIMS ALL SUCH WARRANTIES, |
15 | * INCLUDING WITHOUT LIMITATION, ANY WARRANTIES OF MERCHANTABILITY, | |
6601e61a A |
16 | * FITNESS FOR A PARTICULAR PURPOSE OR NON-INFRINGEMENT. Please see the |
17 | * License for the specific language governing rights and limitations | |
18 | * under the License. | |
8f6c56a5 | 19 | * |
6601e61a | 20 | * @APPLE_LICENSE_HEADER_END@ |
b4c24cb9 A |
21 | */ |
22 | // | |
23 | // This file implements a simple write-ahead journaling layer. | |
24 | // In theory any file system can make use of it by calling these | |
25 | // functions when the fs wants to modify meta-data blocks. See | |
26 | // vfs_journal.h for a more detailed description of the api and | |
27 | // data structures. | |
28 | // | |
29 | // Dominic Giampaolo (dbg@apple.com) | |
30 | // | |
31 | ||
32 | #ifdef KERNEL | |
33 | ||
34 | #include <sys/param.h> | |
35 | #include <sys/systm.h> | |
36 | #include <sys/kernel.h> | |
91447636 | 37 | #include <sys/file_internal.h> |
b4c24cb9 | 38 | #include <sys/stat.h> |
91447636 A |
39 | #include <sys/buf_internal.h> |
40 | #include <sys/proc_internal.h> | |
41 | #include <sys/mount_internal.h> | |
b4c24cb9 | 42 | #include <sys/namei.h> |
91447636 | 43 | #include <sys/vnode_internal.h> |
b4c24cb9 A |
44 | #include <sys/ioctl.h> |
45 | #include <sys/tty.h> | |
46 | #include <sys/ubc.h> | |
47 | #include <sys/malloc.h> | |
91447636 | 48 | #include <kern/thread.h> |
b4c24cb9 A |
49 | #include <sys/disk.h> |
50 | #include <miscfs/specfs/specdev.h> | |
51 | ||
52 | extern task_t kernel_task; | |
53 | ||
54 | #else | |
55 | ||
56 | #include <stdio.h> | |
57 | #include <stdlib.h> | |
58 | #include <string.h> | |
59 | #include <limits.h> | |
60 | #include <errno.h> | |
61 | #include <fcntl.h> | |
62 | #include <unistd.h> | |
63 | #include <stdarg.h> | |
64 | #include <sys/types.h> | |
65 | #include "compat.h" | |
66 | ||
67 | #endif /* KERNEL */ | |
68 | ||
69 | #include "vfs_journal.h" | |
70 | ||
71 | ||
72 | // number of bytes to checksum in a block_list_header | |
73 | // NOTE: this should be enough to clear out the header | |
74 | // fields as well as the first entry of binfo[] | |
75 | #define BLHDR_CHECKSUM_SIZE 32 | |
76 | ||
77 | ||
78 | ||
79 | static int end_transaction(transaction *tr, int force_it); | |
80 | static void abort_transaction(journal *jnl, transaction *tr); | |
81 | static void dump_journal(journal *jnl); | |
82 | ||
91447636 A |
83 | static __inline__ void lock_journal(journal *jnl); |
84 | static __inline__ void unlock_journal(journal *jnl); | |
85 | static __inline__ void lock_oldstart(journal *jnl); | |
86 | static __inline__ void unlock_oldstart(journal *jnl); | |
87 | ||
88 | ||
89 | ||
b4c24cb9 | 90 | |
55e303ae A |
91 | // |
92 | // 3105942 - Coalesce writes to the same block on journal replay | |
93 | // | |
94 | ||
95 | typedef struct bucket { | |
96 | off_t block_num; | |
97 | size_t jnl_offset; | |
98 | size_t block_size; | |
99 | } bucket; | |
100 | ||
101 | #define STARTING_BUCKETS 256 | |
102 | ||
103 | static int add_block(journal *jnl, struct bucket **buf_ptr, off_t block_num, size_t size, size_t offset, int *num_buckets_ptr, int *num_full_ptr); | |
104 | static int grow_table(struct bucket **buf_ptr, int num_buckets, int new_size); | |
105 | static int lookup_bucket(struct bucket **buf_ptr, off_t block_num, int num_full); | |
106 | static int do_overlap(journal *jnl, struct bucket **buf_ptr, int blk_index, off_t block_num, size_t size, size_t offset, int *num_buckets_ptr, int *num_full_ptr); | |
107 | static int insert_block(journal *jnl, struct bucket **buf_ptr, int blk_index, off_t num, size_t size, size_t offset, int *num_buckets_ptr, int *num_full_ptr, int overwriting); | |
108 | ||
b4c24cb9 A |
109 | #define CHECK_JOURNAL(jnl) \ |
110 | do { \ | |
111 | if (jnl == NULL) {\ | |
112 | panic("%s:%d: null journal ptr?\n", __FILE__, __LINE__);\ | |
113 | }\ | |
114 | if (jnl->jdev == NULL) { \ | |
115 | panic("%s:%d: jdev is null!\n", __FILE__, __LINE__);\ | |
116 | } \ | |
117 | if (jnl->fsdev == NULL) { \ | |
118 | panic("%s:%d: fsdev is null!\n", __FILE__, __LINE__);\ | |
119 | } \ | |
120 | if (jnl->jhdr->magic != JOURNAL_HEADER_MAGIC) {\ | |
121 | panic("%s:%d: jhdr magic corrupted (0x%x != 0x%x)\n",\ | |
122 | __FILE__, __LINE__, jnl->jhdr->magic, JOURNAL_HEADER_MAGIC);\ | |
123 | }\ | |
124 | if ( jnl->jhdr->start <= 0 \ | |
125 | || jnl->jhdr->start > jnl->jhdr->size\ | |
55e303ae | 126 | || jnl->jhdr->start > 1024*1024*1024) {\ |
b4c24cb9 A |
127 | panic("%s:%d: jhdr start looks bad (0x%llx max size 0x%llx)\n", \ |
128 | __FILE__, __LINE__, jnl->jhdr->start, jnl->jhdr->size);\ | |
129 | }\ | |
130 | if ( jnl->jhdr->end <= 0 \ | |
131 | || jnl->jhdr->end > jnl->jhdr->size\ | |
55e303ae | 132 | || jnl->jhdr->end > 1024*1024*1024) {\ |
b4c24cb9 A |
133 | panic("%s:%d: jhdr end looks bad (0x%llx max size 0x%llx)\n", \ |
134 | __FILE__, __LINE__, jnl->jhdr->end, jnl->jhdr->size);\ | |
135 | }\ | |
55e303ae | 136 | if (jnl->jhdr->size > 1024*1024*1024) {\ |
b4c24cb9 A |
137 | panic("%s:%d: jhdr size looks bad (0x%llx)\n",\ |
138 | __FILE__, __LINE__, jnl->jhdr->size);\ | |
139 | } \ | |
140 | } while(0) | |
141 | ||
142 | #define CHECK_TRANSACTION(tr) \ | |
143 | do {\ | |
144 | if (tr == NULL) {\ | |
145 | panic("%s:%d: null transaction ptr?\n", __FILE__, __LINE__);\ | |
146 | }\ | |
147 | if (tr->jnl == NULL) {\ | |
148 | panic("%s:%d: null tr->jnl ptr?\n", __FILE__, __LINE__);\ | |
149 | }\ | |
150 | if (tr->blhdr != (block_list_header *)tr->tbuffer) {\ | |
151 | panic("%s:%d: blhdr (0x%x) != tbuffer (0x%x)\n", __FILE__, __LINE__, tr->blhdr, tr->tbuffer);\ | |
152 | }\ | |
153 | if (tr->total_bytes < 0) {\ | |
154 | panic("%s:%d: tr total_bytes looks bad: %d\n", __FILE__, __LINE__, tr->total_bytes);\ | |
155 | }\ | |
55e303ae | 156 | if (tr->journal_start < 0 || tr->journal_start > 1024*1024*1024) {\ |
b4c24cb9 A |
157 | panic("%s:%d: tr journal start looks bad: 0x%llx\n", __FILE__, __LINE__, tr->journal_start);\ |
158 | }\ | |
55e303ae | 159 | if (tr->journal_end < 0 || tr->journal_end > 1024*1024*1024) {\ |
b4c24cb9 A |
160 | panic("%s:%d: tr journal end looks bad: 0x%llx\n", __FILE__, __LINE__, tr->journal_end);\ |
161 | }\ | |
55e303ae | 162 | if (tr->blhdr && (tr->blhdr->max_blocks <= 0 || tr->blhdr->max_blocks > (tr->jnl->jhdr->size/tr->jnl->jhdr->jhdr_size))) {\ |
b4c24cb9 A |
163 | panic("%s:%d: tr blhdr max_blocks looks bad: %d\n", __FILE__, __LINE__, tr->blhdr->max_blocks);\ |
164 | }\ | |
165 | } while(0) | |
166 | ||
167 | ||
168 | ||
169 | // | |
170 | // this isn't a great checksum routine but it will do for now. | |
171 | // we use it to checksum the journal header and the block list | |
172 | // headers that are at the start of each transaction. | |
173 | // | |
174 | static int | |
175 | calc_checksum(char *ptr, int len) | |
176 | { | |
177 | int i, cksum=0; | |
178 | ||
179 | // this is a lame checksum but for now it'll do | |
180 | for(i=0; i < len; i++, ptr++) { | |
181 | cksum = (cksum << 8) ^ (cksum + *(unsigned char *)ptr); | |
182 | } | |
183 | ||
184 | return (~cksum); | |
185 | } | |
186 | ||
91447636 A |
187 | // |
188 | // Journal Locking | |
189 | // | |
190 | lck_grp_attr_t * jnl_group_attr; | |
191 | lck_attr_t * jnl_lock_attr; | |
192 | lck_grp_t * jnl_mutex_group; | |
193 | ||
194 | void | |
195 | journal_init() | |
196 | { | |
197 | jnl_lock_attr = lck_attr_alloc_init(); | |
198 | jnl_group_attr = lck_grp_attr_alloc_init(); | |
199 | jnl_mutex_group = lck_grp_alloc_init("jnl-mutex", jnl_group_attr); | |
91447636 A |
200 | } |
201 | ||
202 | static __inline__ void | |
203 | lock_journal(journal *jnl) | |
204 | { | |
205 | lck_mtx_lock(&jnl->jlock); | |
206 | } | |
207 | ||
208 | static __inline__ void | |
209 | unlock_journal(journal *jnl) | |
210 | { | |
211 | lck_mtx_unlock(&jnl->jlock); | |
212 | } | |
213 | ||
214 | static __inline__ void | |
215 | lock_oldstart(journal *jnl) | |
216 | { | |
217 | lck_mtx_lock(&jnl->old_start_lock); | |
218 | } | |
219 | ||
220 | static __inline__ void | |
221 | unlock_oldstart(journal *jnl) | |
222 | { | |
223 | lck_mtx_unlock(&jnl->old_start_lock); | |
224 | } | |
225 | ||
226 | ||
b4c24cb9 | 227 | |
55e303ae A |
228 | #define JNL_WRITE 0x0001 |
229 | #define JNL_READ 0x0002 | |
230 | #define JNL_HEADER 0x8000 | |
b4c24cb9 A |
231 | |
232 | // | |
233 | // This function sets up a fake buf and passes it directly to the | |
234 | // journal device strategy routine (so that it won't get cached in | |
235 | // the block cache. | |
236 | // | |
237 | // It also handles range checking the i/o so that we don't write | |
238 | // outside the journal boundaries and it will wrap the i/o back | |
239 | // to the beginning if necessary (skipping over the journal header) | |
240 | // | |
241 | static size_t | |
242 | do_journal_io(journal *jnl, off_t *offset, void *data, size_t len, int direction) | |
243 | { | |
244 | int err, io_sz=0, curlen=len; | |
91447636 A |
245 | buf_t bp; |
246 | int max_iosize = 128 * 1024; | |
247 | struct vfsioattr ioattr; | |
b4c24cb9 A |
248 | |
249 | if (*offset < 0 || *offset > jnl->jhdr->size) { | |
250 | panic("jnl: do_jnl_io: bad offset 0x%llx (max 0x%llx)\n", *offset, jnl->jhdr->size); | |
251 | } | |
91447636 A |
252 | vfs_ioattr(vnode_mount(jnl->jdev), &ioattr); |
253 | ||
254 | if (direction & JNL_WRITE) | |
255 | max_iosize = ioattr.io_maxwritecnt; | |
256 | else if (direction & JNL_READ) | |
257 | max_iosize = ioattr.io_maxreadcnt; | |
b4c24cb9 A |
258 | |
259 | again: | |
260 | bp = alloc_io_buf(jnl->jdev, 1); | |
261 | ||
b4c24cb9 A |
262 | if (*offset + (off_t)curlen > jnl->jhdr->size && *offset != 0 && jnl->jhdr->size != 0) { |
263 | if (*offset == jnl->jhdr->size) { | |
264 | *offset = jnl->jhdr->jhdr_size; | |
265 | } else { | |
266 | curlen = (off_t)jnl->jhdr->size - *offset; | |
267 | } | |
268 | } | |
269 | ||
270 | if (curlen > max_iosize) { | |
271 | curlen = max_iosize; | |
272 | } | |
273 | ||
274 | if (curlen <= 0) { | |
275 | panic("jnl: do_jnl_io: curlen == %d, offset 0x%llx len %d\n", curlen, *offset, len); | |
276 | } | |
277 | ||
55e303ae A |
278 | if (*offset == 0 && (direction & JNL_HEADER) == 0) { |
279 | panic("jnl: request for i/o to jnl-header without JNL_HEADER flag set! (len %d, data %p)\n", curlen, data); | |
280 | } | |
281 | ||
91447636 A |
282 | if (direction & JNL_READ) |
283 | buf_setflags(bp, B_READ); | |
284 | else { | |
285 | /* | |
286 | * don't have to set any flags | |
287 | */ | |
288 | vnode_startwrite(jnl->jdev); | |
289 | } | |
290 | buf_setsize(bp, curlen); | |
291 | buf_setcount(bp, curlen); | |
292 | buf_setdataptr(bp, (uintptr_t)data); | |
293 | buf_setblkno(bp, (daddr64_t) ((jnl->jdev_offset + *offset) / (off_t)jnl->jhdr->jhdr_size)); | |
294 | buf_setlblkno(bp, (daddr64_t) ((jnl->jdev_offset + *offset) / (off_t)jnl->jhdr->jhdr_size)); | |
295 | ||
296 | err = VNOP_STRATEGY(bp); | |
b4c24cb9 | 297 | if (!err) { |
91447636 | 298 | err = (int)buf_biowait(bp); |
b4c24cb9 | 299 | } |
b4c24cb9 A |
300 | free_io_buf(bp); |
301 | ||
302 | if (err) { | |
303 | printf("jnl: do_jnl_io: strategy err 0x%x\n", err); | |
304 | return 0; | |
305 | } | |
306 | ||
307 | *offset += curlen; | |
308 | io_sz += curlen; | |
309 | if (io_sz != len) { | |
310 | // handle wrap-around | |
311 | data = (char *)data + curlen; | |
312 | curlen = len - io_sz; | |
313 | if (*offset >= jnl->jhdr->size) { | |
314 | *offset = jnl->jhdr->jhdr_size; | |
315 | } | |
316 | goto again; | |
317 | } | |
318 | ||
319 | return io_sz; | |
320 | } | |
321 | ||
322 | static size_t | |
323 | read_journal_data(journal *jnl, off_t *offset, void *data, size_t len) | |
324 | { | |
325 | return do_journal_io(jnl, offset, data, len, JNL_READ); | |
326 | } | |
327 | ||
328 | static size_t | |
329 | write_journal_data(journal *jnl, off_t *offset, void *data, size_t len) | |
330 | { | |
331 | return do_journal_io(jnl, offset, data, len, JNL_WRITE); | |
332 | } | |
333 | ||
334 | ||
55e303ae A |
335 | static int |
336 | read_journal_header(journal *jnl, void *data, size_t len) | |
337 | { | |
338 | off_t hdr_offset = 0; | |
339 | ||
340 | return do_journal_io(jnl, &hdr_offset, data, len, JNL_READ|JNL_HEADER); | |
341 | } | |
342 | ||
b4c24cb9 A |
343 | static int |
344 | write_journal_header(journal *jnl) | |
345 | { | |
55e303ae | 346 | static int num_err_prints = 0; |
b4c24cb9 A |
347 | int ret; |
348 | off_t jhdr_offset = 0; | |
91447636 A |
349 | struct vfs_context context; |
350 | ||
351 | context.vc_proc = current_proc(); | |
352 | context.vc_ucred = NOCRED; | |
b4c24cb9 A |
353 | // |
354 | // XXXdbg note: this ioctl doesn't seem to do anything on firewire disks. | |
355 | // | |
91447636 | 356 | ret = VNOP_IOCTL(jnl->jdev, DKIOCSYNCHRONIZECACHE, NULL, FWRITE, &context); |
b4c24cb9 | 357 | if (ret != 0) { |
55e303ae A |
358 | // |
359 | // Only print this error if it's a different error than the | |
360 | // previous one, or if it's the first time for this device | |
361 | // or if the total number of printfs is less than 25. We | |
362 | // allow for up to 25 printfs to insure that some make it | |
363 | // into the on-disk syslog. Otherwise if we only printed | |
364 | // one, it's possible it would never make it to the syslog | |
365 | // for the root volume and that makes debugging hard. | |
366 | // | |
367 | if ( ret != jnl->last_flush_err | |
368 | || (jnl->flags & JOURNAL_FLUSHCACHE_ERR) == 0 | |
369 | || num_err_prints++ < 25) { | |
370 | ||
371 | printf("jnl: flushing fs disk buffer returned 0x%x\n", ret); | |
372 | ||
373 | jnl->flags |= JOURNAL_FLUSHCACHE_ERR; | |
374 | jnl->last_flush_err = ret; | |
375 | } | |
b4c24cb9 A |
376 | } |
377 | ||
55e303ae | 378 | |
b4c24cb9 A |
379 | jnl->jhdr->checksum = 0; |
380 | jnl->jhdr->checksum = calc_checksum((char *)jnl->jhdr, sizeof(struct journal_header)); | |
55e303ae A |
381 | if (do_journal_io(jnl, &jhdr_offset, jnl->header_buf, jnl->jhdr->jhdr_size, JNL_WRITE|JNL_HEADER) != jnl->jhdr->jhdr_size) { |
382 | printf("jnl: write_journal_header: error writing the journal header!\n"); | |
383 | jnl->flags |= JOURNAL_INVALID; | |
384 | return -1; | |
b4c24cb9 A |
385 | } |
386 | ||
55e303ae A |
387 | // Have to flush after writing the journal header so that |
388 | // a future transaction doesn't sneak out to disk before | |
389 | // the header does and thus overwrite data that the old | |
390 | // journal header refers to. Saw this exact case happen | |
391 | // on an IDE bus analyzer with Larry Barras so while it | |
392 | // may seem obscure, it's not. | |
393 | // | |
91447636 | 394 | VNOP_IOCTL(jnl->jdev, DKIOCSYNCHRONIZECACHE, NULL, FWRITE, &context); |
55e303ae | 395 | |
b4c24cb9 A |
396 | return 0; |
397 | } | |
398 | ||
399 | ||
400 | ||
401 | // | |
402 | // this is a work function used to free up transactions that | |
403 | // completed. they can't be free'd from buffer_flushed_callback | |
404 | // because it is called from deep with the disk driver stack | |
405 | // and thus can't do something that would potentially cause | |
406 | // paging. it gets called by each of the journal api entry | |
407 | // points so stuff shouldn't hang around for too long. | |
408 | // | |
409 | static void | |
410 | free_old_stuff(journal *jnl) | |
411 | { | |
412 | transaction *tr, *next; | |
413 | ||
91447636 A |
414 | lock_oldstart(jnl); |
415 | tr = jnl->tr_freeme; | |
416 | jnl->tr_freeme = NULL; | |
417 | unlock_oldstart(jnl); | |
418 | ||
419 | for(; tr; tr=next) { | |
420 | next = tr->next; | |
421 | FREE_ZONE(tr, sizeof(transaction), M_JNL_TR); | |
b4c24cb9 A |
422 | } |
423 | ||
b4c24cb9 A |
424 | } |
425 | ||
426 | ||
427 | ||
428 | // | |
429 | // This is our callback that lets us know when a buffer has been | |
430 | // flushed to disk. It's called from deep within the driver stack | |
431 | // and thus is quite limited in what it can do. Notably, it can | |
432 | // not initiate any new i/o's or allocate/free memory. | |
433 | // | |
434 | static void | |
91447636 | 435 | buffer_flushed_callback(struct buf *bp, void *arg) |
b4c24cb9 A |
436 | { |
437 | transaction *tr; | |
438 | journal *jnl; | |
439 | transaction *ctr, *prev=NULL, *next; | |
440 | int i, bufsize; | |
441 | ||
442 | ||
91447636 A |
443 | //printf("jnl: buf flush: bp @ 0x%x l/blkno %qd/%qd vp 0x%x tr @ 0x%x\n", |
444 | // bp, buf_lblkno(bp), buf_blkno(bp), buf_vnode(bp), arg); | |
b4c24cb9 A |
445 | |
446 | // snarf out the bits we want | |
91447636 A |
447 | bufsize = buf_size(bp); |
448 | tr = (transaction *)arg; | |
b4c24cb9 A |
449 | |
450 | // then we've already seen it | |
451 | if (tr == NULL) { | |
452 | return; | |
453 | } | |
454 | ||
455 | CHECK_TRANSACTION(tr); | |
456 | ||
457 | jnl = tr->jnl; | |
458 | if (jnl->flags & JOURNAL_INVALID) { | |
459 | return; | |
460 | } | |
461 | ||
462 | CHECK_JOURNAL(jnl); | |
463 | ||
464 | // update the number of blocks that have been flushed. | |
465 | // this buf may represent more than one block so take | |
466 | // that into account. | |
91447636 | 467 | OSAddAtomic(bufsize, &tr->num_flushed); |
b4c24cb9 A |
468 | |
469 | ||
470 | // if this transaction isn't done yet, just return as | |
471 | // there is nothing to do. | |
472 | if ((tr->num_flushed + tr->num_killed) < tr->total_bytes) { | |
473 | return; | |
474 | } | |
475 | ||
91447636 A |
476 | // this will single thread checking the transaction |
477 | lock_oldstart(jnl); | |
478 | ||
479 | if (tr->total_bytes == 0xfbadc0de) { | |
480 | // then someone beat us to it... | |
481 | unlock_oldstart(jnl); | |
482 | return; | |
483 | } | |
484 | ||
485 | // mark this so that we're the owner of dealing with the | |
486 | // cleanup for this transaction | |
487 | tr->total_bytes = 0xfbadc0de; | |
488 | ||
b4c24cb9 A |
489 | //printf("jnl: tr 0x%x (0x%llx 0x%llx) in jnl 0x%x completed.\n", |
490 | // tr, tr->journal_start, tr->journal_end, jnl); | |
491 | ||
492 | // find this entry in the old_start[] index and mark it completed | |
b4c24cb9 A |
493 | for(i=0; i < sizeof(jnl->old_start)/sizeof(jnl->old_start[0]); i++) { |
494 | ||
495 | if ((jnl->old_start[i] & ~(0x8000000000000000LL)) == tr->journal_start) { | |
496 | jnl->old_start[i] &= ~(0x8000000000000000LL); | |
497 | break; | |
498 | } | |
499 | } | |
500 | if (i >= sizeof(jnl->old_start)/sizeof(jnl->old_start[0])) { | |
501 | panic("jnl: buffer_flushed: did not find tr w/start @ %lld (tr 0x%x, jnl 0x%x)\n", | |
502 | tr->journal_start, tr, jnl); | |
503 | } | |
91447636 | 504 | unlock_oldstart(jnl); |
b4c24cb9 A |
505 | |
506 | ||
507 | // if we are here then we need to update the journal header | |
508 | // to reflect that this transaction is complete | |
509 | if (tr->journal_start == jnl->active_start) { | |
510 | jnl->active_start = tr->journal_end; | |
511 | tr->journal_start = tr->journal_end = (off_t)0; | |
512 | } | |
513 | ||
514 | // go through the completed_trs list and try to coalesce | |
515 | // entries, restarting back at the beginning if we have to. | |
516 | for(ctr=jnl->completed_trs; ctr; prev=ctr, ctr=next) { | |
517 | if (ctr->journal_start == jnl->active_start) { | |
518 | jnl->active_start = ctr->journal_end; | |
519 | if (prev) { | |
520 | prev->next = ctr->next; | |
521 | } | |
522 | if (ctr == jnl->completed_trs) { | |
523 | jnl->completed_trs = ctr->next; | |
524 | } | |
525 | ||
91447636 | 526 | lock_oldstart(jnl); |
b4c24cb9 A |
527 | next = jnl->completed_trs; // this starts us over again |
528 | ctr->next = jnl->tr_freeme; | |
529 | jnl->tr_freeme = ctr; | |
530 | ctr = NULL; | |
91447636 | 531 | unlock_oldstart(jnl); |
b4c24cb9 A |
532 | } else if (tr->journal_end == ctr->journal_start) { |
533 | ctr->journal_start = tr->journal_start; | |
534 | next = jnl->completed_trs; // this starts us over again | |
535 | ctr = NULL; | |
536 | tr->journal_start = tr->journal_end = (off_t)0; | |
537 | } else if (tr->journal_start == ctr->journal_end) { | |
538 | ctr->journal_end = tr->journal_end; | |
539 | next = ctr->next; | |
540 | tr->journal_start = tr->journal_end = (off_t)0; | |
541 | } else { | |
542 | next = ctr->next; | |
543 | } | |
544 | } | |
545 | ||
b4c24cb9 A |
546 | // if this is true then we didn't merge with anyone |
547 | // so link ourselves in at the head of the completed | |
548 | // transaction list. | |
549 | if (tr->journal_start != 0) { | |
550 | // put this entry into the correct sorted place | |
551 | // in the list instead of just at the head. | |
552 | // | |
553 | ||
554 | prev = NULL; | |
555 | for(ctr=jnl->completed_trs; ctr && tr->journal_start > ctr->journal_start; prev=ctr, ctr=ctr->next) { | |
556 | // just keep looping | |
557 | } | |
558 | ||
559 | if (ctr == NULL && prev == NULL) { | |
560 | jnl->completed_trs = tr; | |
561 | tr->next = NULL; | |
562 | } else if (ctr == jnl->completed_trs) { | |
563 | tr->next = jnl->completed_trs; | |
564 | jnl->completed_trs = tr; | |
565 | } else { | |
566 | tr->next = prev->next; | |
567 | prev->next = tr; | |
568 | } | |
569 | } else { | |
570 | // if we're here this tr got merged with someone else so | |
571 | // put it on the list to be free'd | |
91447636 | 572 | lock_oldstart(jnl); |
b4c24cb9 A |
573 | tr->next = jnl->tr_freeme; |
574 | jnl->tr_freeme = tr; | |
91447636 | 575 | unlock_oldstart(jnl); |
b4c24cb9 A |
576 | } |
577 | } | |
578 | ||
55e303ae A |
579 | |
580 | #include <libkern/OSByteOrder.h> | |
581 | ||
582 | #define SWAP16(x) OSSwapInt16(x) | |
583 | #define SWAP32(x) OSSwapInt32(x) | |
584 | #define SWAP64(x) OSSwapInt64(x) | |
585 | ||
586 | ||
587 | static void | |
588 | swap_journal_header(journal *jnl) | |
589 | { | |
590 | jnl->jhdr->magic = SWAP32(jnl->jhdr->magic); | |
591 | jnl->jhdr->endian = SWAP32(jnl->jhdr->endian); | |
592 | jnl->jhdr->start = SWAP64(jnl->jhdr->start); | |
593 | jnl->jhdr->end = SWAP64(jnl->jhdr->end); | |
594 | jnl->jhdr->size = SWAP64(jnl->jhdr->size); | |
595 | jnl->jhdr->blhdr_size = SWAP32(jnl->jhdr->blhdr_size); | |
596 | jnl->jhdr->checksum = SWAP32(jnl->jhdr->checksum); | |
597 | jnl->jhdr->jhdr_size = SWAP32(jnl->jhdr->jhdr_size); | |
598 | } | |
599 | ||
600 | static void | |
601 | swap_block_list_header(journal *jnl, block_list_header *blhdr) | |
602 | { | |
603 | int i; | |
604 | ||
605 | blhdr->max_blocks = SWAP16(blhdr->max_blocks); | |
606 | blhdr->num_blocks = SWAP16(blhdr->num_blocks); | |
607 | blhdr->bytes_used = SWAP32(blhdr->bytes_used); | |
608 | blhdr->checksum = SWAP32(blhdr->checksum); | |
609 | blhdr->pad = SWAP32(blhdr->pad); | |
610 | ||
611 | if (blhdr->num_blocks * sizeof(blhdr->binfo[0]) > jnl->jhdr->blhdr_size) { | |
612 | printf("jnl: blhdr num blocks looks suspicious (%d). not swapping.\n", blhdr->num_blocks); | |
613 | return; | |
614 | } | |
615 | ||
616 | for(i=0; i < blhdr->num_blocks; i++) { | |
617 | blhdr->binfo[i].bnum = SWAP64(blhdr->binfo[i].bnum); | |
618 | blhdr->binfo[i].bsize = SWAP32(blhdr->binfo[i].bsize); | |
619 | blhdr->binfo[i].bp = (void *)SWAP32((int)blhdr->binfo[i].bp); | |
620 | } | |
621 | } | |
622 | ||
623 | ||
b4c24cb9 A |
624 | static int |
625 | update_fs_block(journal *jnl, void *block_ptr, off_t fs_block, size_t bsize) | |
626 | { | |
91447636 | 627 | int ret; |
b4c24cb9 A |
628 | struct buf *oblock_bp=NULL; |
629 | ||
630 | // first read the block we want. | |
91447636 | 631 | ret = buf_meta_bread(jnl->fsdev, (daddr64_t)fs_block, bsize, NOCRED, &oblock_bp); |
b4c24cb9 A |
632 | if (ret != 0) { |
633 | printf("jnl: update_fs_block: error reading fs block # %lld! (ret %d)\n", fs_block, ret); | |
634 | ||
635 | if (oblock_bp) { | |
91447636 | 636 | buf_brelse(oblock_bp); |
b4c24cb9 A |
637 | oblock_bp = NULL; |
638 | } | |
639 | ||
640 | // let's try to be aggressive here and just re-write the block | |
91447636 | 641 | oblock_bp = buf_getblk(jnl->fsdev, (daddr64_t)fs_block, bsize, 0, 0, BLK_META); |
b4c24cb9 | 642 | if (oblock_bp == NULL) { |
91447636 | 643 | printf("jnl: update_fs_block: buf_getblk() for %lld failed! failing update.\n", fs_block); |
b4c24cb9 A |
644 | return -1; |
645 | } | |
646 | } | |
647 | ||
648 | // make sure it's the correct size. | |
91447636 A |
649 | if (buf_size(oblock_bp) != bsize) { |
650 | buf_brelse(oblock_bp); | |
b4c24cb9 A |
651 | return -1; |
652 | } | |
653 | ||
654 | // copy the journal data over top of it | |
91447636 | 655 | memcpy((void *)buf_dataptr(oblock_bp), block_ptr, bsize); |
b4c24cb9 | 656 | |
91447636 | 657 | if ((ret = VNOP_BWRITE(oblock_bp)) != 0) { |
b4c24cb9 | 658 | printf("jnl: update_fs_block: failed to update block %lld (ret %d)\n", fs_block,ret); |
b4c24cb9 A |
659 | return ret; |
660 | } | |
661 | ||
662 | // and now invalidate it so that if someone else wants to read | |
663 | // it in a different size they'll be able to do it. | |
91447636 | 664 | ret = buf_meta_bread(jnl->fsdev, (daddr64_t)fs_block, bsize, NOCRED, &oblock_bp); |
b4c24cb9 | 665 | if (oblock_bp) { |
91447636 A |
666 | buf_markinvalid(oblock_bp); |
667 | buf_brelse(oblock_bp); | |
b4c24cb9 A |
668 | } |
669 | ||
670 | return 0; | |
671 | } | |
672 | ||
55e303ae A |
673 | static int |
674 | grow_table(struct bucket **buf_ptr, int num_buckets, int new_size) | |
675 | { | |
676 | struct bucket *newBuf; | |
677 | int current_size = num_buckets, i; | |
678 | ||
679 | // return if newsize is less than the current size | |
680 | if (new_size < num_buckets) { | |
681 | return current_size; | |
682 | } | |
683 | ||
684 | if ((MALLOC(newBuf, struct bucket *, new_size*sizeof(struct bucket), M_TEMP, M_WAITOK)) == NULL) { | |
685 | printf("jnl: grow_table: no memory to expand coalesce buffer!\n"); | |
686 | return -1; | |
687 | } | |
688 | ||
689 | // printf("jnl: lookup_bucket: expanded co_buf to %d elems\n", new_size); | |
690 | ||
691 | // copy existing elements | |
692 | bcopy(*buf_ptr, newBuf, num_buckets*sizeof(struct bucket)); | |
693 | ||
694 | // initialize the new ones | |
695 | for(i=num_buckets; i < new_size; i++) { | |
696 | newBuf[i].block_num = (off_t)-1; | |
697 | } | |
698 | ||
699 | // free the old container | |
700 | FREE(*buf_ptr, M_TEMP); | |
701 | ||
702 | // reset the buf_ptr | |
703 | *buf_ptr = newBuf; | |
704 | ||
705 | return new_size; | |
706 | } | |
707 | ||
708 | static int | |
709 | lookup_bucket(struct bucket **buf_ptr, off_t block_num, int num_full) | |
710 | { | |
711 | int lo, hi, index, matches, i; | |
712 | ||
713 | if (num_full == 0) { | |
714 | return 0; // table is empty, so insert at index=0 | |
715 | } | |
716 | ||
717 | lo = 0; | |
718 | hi = num_full - 1; | |
719 | index = -1; | |
720 | ||
721 | // perform binary search for block_num | |
722 | do { | |
723 | int mid = (hi - lo)/2 + lo; | |
724 | off_t this_num = (*buf_ptr)[mid].block_num; | |
725 | ||
726 | if (block_num == this_num) { | |
727 | index = mid; | |
728 | break; | |
729 | } | |
730 | ||
731 | if (block_num < this_num) { | |
732 | hi = mid; | |
733 | continue; | |
734 | } | |
735 | ||
736 | if (block_num > this_num) { | |
737 | lo = mid + 1; | |
738 | continue; | |
739 | } | |
740 | } while(lo < hi); | |
741 | ||
742 | // check if lo and hi converged on the match | |
743 | if (block_num == (*buf_ptr)[hi].block_num) { | |
744 | index = hi; | |
745 | } | |
746 | ||
747 | // if no existing entry found, find index for new one | |
748 | if (index == -1) { | |
749 | index = (block_num < (*buf_ptr)[hi].block_num) ? hi : hi + 1; | |
750 | } else { | |
751 | // make sure that we return the right-most index in the case of multiple matches | |
752 | matches = 0; | |
753 | i = index + 1; | |
754 | while(i < num_full && block_num == (*buf_ptr)[i].block_num) { | |
755 | matches++; | |
756 | i++; | |
757 | } | |
758 | ||
759 | index += matches; | |
760 | } | |
761 | ||
762 | return index; | |
763 | } | |
764 | ||
765 | static int | |
766 | insert_block(journal *jnl, struct bucket **buf_ptr, int blk_index, off_t num, size_t size, size_t offset, int *num_buckets_ptr, int *num_full_ptr, int overwriting) | |
767 | { | |
768 | if (!overwriting) { | |
769 | // grow the table if we're out of space | |
770 | if (*num_full_ptr >= *num_buckets_ptr) { | |
771 | int new_size = *num_buckets_ptr * 2; | |
772 | int grow_size = grow_table(buf_ptr, *num_buckets_ptr, new_size); | |
773 | ||
774 | if (grow_size < new_size) { | |
775 | printf("jnl: add_block: grow_table returned an error!\n"); | |
776 | return -1; | |
777 | } | |
778 | ||
779 | *num_buckets_ptr = grow_size; //update num_buckets to reflect the new size | |
780 | } | |
781 | ||
782 | // if we're not inserting at the end, we need to bcopy | |
783 | if (blk_index != *num_full_ptr) { | |
784 | bcopy( (*buf_ptr)+(blk_index), (*buf_ptr)+(blk_index+1), (*num_full_ptr-blk_index)*sizeof(struct bucket) ); | |
785 | } | |
786 | ||
787 | (*num_full_ptr)++; // increment only if we're not overwriting | |
788 | } | |
789 | ||
790 | // sanity check the values we're about to add | |
791 | if (offset >= jnl->jhdr->size) { | |
792 | offset = jnl->jhdr->jhdr_size + (offset - jnl->jhdr->size); | |
793 | } | |
794 | if (size <= 0) { | |
795 | panic("jnl: insert_block: bad size in insert_block (%d)\n", size); | |
796 | } | |
797 | ||
798 | (*buf_ptr)[blk_index].block_num = num; | |
799 | (*buf_ptr)[blk_index].block_size = size; | |
800 | (*buf_ptr)[blk_index].jnl_offset = offset; | |
801 | ||
802 | return blk_index; | |
803 | } | |
804 | ||
805 | static int | |
806 | do_overlap(journal *jnl, struct bucket **buf_ptr, int blk_index, off_t block_num, size_t size, size_t offset, int *num_buckets_ptr, int *num_full_ptr) | |
807 | { | |
808 | int num_to_remove, index, i, overwrite, err; | |
809 | size_t jhdr_size = jnl->jhdr->jhdr_size, new_offset; | |
810 | off_t overlap, block_start, block_end; | |
811 | ||
812 | block_start = block_num*jhdr_size; | |
813 | block_end = block_start + size; | |
814 | overwrite = (block_num == (*buf_ptr)[blk_index].block_num && size >= (*buf_ptr)[blk_index].block_size); | |
815 | ||
816 | // first, eliminate any overlap with the previous entry | |
817 | if (blk_index != 0 && !overwrite) { | |
818 | off_t prev_block_start = (*buf_ptr)[blk_index-1].block_num*jhdr_size; | |
819 | off_t prev_block_end = prev_block_start + (*buf_ptr)[blk_index-1].block_size; | |
820 | overlap = prev_block_end - block_start; | |
821 | if (overlap > 0) { | |
822 | if (overlap % jhdr_size != 0) { | |
823 | panic("jnl: do_overlap: overlap with previous entry not a multiple of %d\n", jhdr_size); | |
824 | } | |
825 | ||
826 | // if the previous entry completely overlaps this one, we need to break it into two pieces. | |
827 | if (prev_block_end > block_end) { | |
828 | off_t new_num = block_end / jhdr_size; | |
829 | size_t new_size = prev_block_end - block_end; | |
91447636 A |
830 | |
831 | new_offset = (*buf_ptr)[blk_index-1].jnl_offset + (block_end - prev_block_start); | |
55e303ae A |
832 | |
833 | err = insert_block(jnl, buf_ptr, blk_index, new_num, new_size, new_offset, num_buckets_ptr, num_full_ptr, 0); | |
834 | if (err < 0) { | |
835 | panic("jnl: do_overlap: error inserting during pre-overlap\n"); | |
836 | } | |
837 | } | |
838 | ||
839 | // Regardless, we need to truncate the previous entry to the beginning of the overlap | |
840 | (*buf_ptr)[blk_index-1].block_size = block_start - prev_block_start; | |
841 | } | |
842 | } | |
843 | ||
844 | // then, bail out fast if there's no overlap with the entries that follow | |
845 | if (!overwrite && block_end <= (*buf_ptr)[blk_index].block_num*jhdr_size) { | |
846 | return 0; // no overlap, no overwrite | |
847 | } else if (overwrite && (blk_index + 1 >= *num_full_ptr || block_end <= (*buf_ptr)[blk_index+1].block_num*jhdr_size)) { | |
848 | return 1; // simple overwrite | |
849 | } | |
850 | ||
851 | // Otherwise, find all cases of total and partial overlap. We use the special | |
852 | // block_num of -2 to designate entries that are completely overlapped and must | |
853 | // be eliminated. The block_num, size, and jnl_offset of partially overlapped | |
854 | // entries must be adjusted to keep the array consistent. | |
855 | index = blk_index; | |
856 | num_to_remove = 0; | |
857 | while(index < *num_full_ptr && block_end > (*buf_ptr)[index].block_num*jhdr_size) { | |
858 | if (block_end >= ((*buf_ptr)[index].block_num*jhdr_size + (*buf_ptr)[index].block_size)) { | |
859 | (*buf_ptr)[index].block_num = -2; // mark this for deletion | |
860 | num_to_remove++; | |
861 | } else { | |
862 | overlap = block_end - (*buf_ptr)[index].block_num*jhdr_size; | |
863 | if (overlap > 0) { | |
864 | if (overlap % jhdr_size != 0) { | |
91447636 | 865 | panic("jnl: do_overlap: overlap of %lld is not multiple of %d\n", overlap, jhdr_size); |
55e303ae A |
866 | } |
867 | ||
868 | // if we partially overlap this entry, adjust its block number, jnl offset, and size | |
869 | (*buf_ptr)[index].block_num += (overlap / jhdr_size); // make sure overlap is multiple of jhdr_size, or round up | |
870 | ||
871 | new_offset = (*buf_ptr)[index].jnl_offset + overlap; // check for wrap-around | |
872 | if (new_offset >= jnl->jhdr->size) { | |
873 | new_offset = jhdr_size + (new_offset - jnl->jhdr->size); | |
874 | } | |
875 | (*buf_ptr)[index].jnl_offset = new_offset; | |
876 | ||
877 | (*buf_ptr)[index].block_size -= overlap; // sanity check for negative value | |
878 | if ((*buf_ptr)[index].block_size <= 0) { | |
879 | panic("jnl: do_overlap: after overlap, new block size is invalid (%d)\n", (*buf_ptr)[index].block_size); | |
880 | // return -1; // if above panic is removed, return -1 for error | |
881 | } | |
882 | } | |
883 | ||
884 | } | |
885 | ||
886 | index++; | |
887 | } | |
888 | ||
889 | // bcopy over any completely overlapped entries, starting at the right (where the above loop broke out) | |
890 | index--; // start with the last index used within the above loop | |
891 | while(index >= blk_index) { | |
892 | if ((*buf_ptr)[index].block_num == -2) { | |
893 | if (index == *num_full_ptr-1) { | |
894 | (*buf_ptr)[index].block_num = -1; // it's the last item in the table... just mark as free | |
895 | } else { | |
896 | bcopy( (*buf_ptr)+(index+1), (*buf_ptr)+(index), (*num_full_ptr - (index + 1)) * sizeof(struct bucket) ); | |
897 | } | |
898 | (*num_full_ptr)--; | |
899 | } | |
900 | index--; | |
901 | } | |
902 | ||
903 | // eliminate any stale entries at the end of the table | |
904 | for(i=*num_full_ptr; i < (*num_full_ptr + num_to_remove); i++) { | |
905 | (*buf_ptr)[i].block_num = -1; | |
906 | } | |
907 | ||
908 | return 0; // if we got this far, we need to insert the entry into the table (rather than overwrite) | |
909 | } | |
910 | ||
911 | // PR-3105942: Coalesce writes to the same block in journal replay | |
912 | // We coalesce writes by maintaining a dynamic sorted array of physical disk blocks | |
913 | // to be replayed and the corresponding location in the journal which contains | |
914 | // the most recent data for those blocks. The array is "played" once the all the | |
915 | // blocks in the journal have been coalesced. The code for the case of conflicting/ | |
916 | // overlapping writes to a single block is the most dense. Because coalescing can | |
917 | // disrupt the existing time-ordering of blocks in the journal playback, care | |
918 | // is taken to catch any overlaps and keep the array consistent. | |
919 | static int | |
920 | add_block(journal *jnl, struct bucket **buf_ptr, off_t block_num, size_t size, size_t offset, int *num_buckets_ptr, int *num_full_ptr) | |
921 | { | |
922 | int blk_index, overwriting; | |
55e303ae A |
923 | |
924 | // on return from lookup_bucket(), blk_index is the index into the table where block_num should be | |
925 | // inserted (or the index of the elem to overwrite). | |
926 | blk_index = lookup_bucket( buf_ptr, block_num, *num_full_ptr); | |
927 | ||
928 | // check if the index is within bounds (if we're adding this block to the end of | |
929 | // the table, blk_index will be equal to num_full) | |
930 | if (blk_index < 0 || blk_index > *num_full_ptr) { | |
931 | //printf("jnl: add_block: trouble adding block to co_buf\n"); | |
932 | return -1; | |
933 | } // else printf("jnl: add_block: adding block 0x%llx at i=%d\n", block_num, blk_index); | |
934 | ||
935 | // Determine whether we're overwriting an existing entry by checking for overlap | |
936 | overwriting = do_overlap(jnl, buf_ptr, blk_index, block_num, size, offset, num_buckets_ptr, num_full_ptr); | |
937 | if (overwriting < 0) { | |
938 | return -1; // if we got an error, pass it along | |
939 | } | |
940 | ||
941 | // returns the index, or -1 on error | |
942 | blk_index = insert_block(jnl, buf_ptr, blk_index, block_num, size, offset, num_buckets_ptr, num_full_ptr, overwriting); | |
943 | ||
944 | return blk_index; | |
945 | } | |
b4c24cb9 A |
946 | |
947 | static int | |
948 | replay_journal(journal *jnl) | |
949 | { | |
55e303ae | 950 | int i, ret, orig_checksum, checksum, max_bsize; |
b4c24cb9 A |
951 | block_list_header *blhdr; |
952 | off_t offset; | |
91447636 | 953 | char *buff, *block_ptr=NULL; |
55e303ae A |
954 | struct bucket *co_buf; |
955 | int num_buckets = STARTING_BUCKETS, num_full; | |
956 | ||
b4c24cb9 A |
957 | // wrap the start ptr if it points to the very end of the journal |
958 | if (jnl->jhdr->start == jnl->jhdr->size) { | |
959 | jnl->jhdr->start = jnl->jhdr->jhdr_size; | |
960 | } | |
961 | if (jnl->jhdr->end == jnl->jhdr->size) { | |
962 | jnl->jhdr->end = jnl->jhdr->jhdr_size; | |
963 | } | |
964 | ||
965 | if (jnl->jhdr->start == jnl->jhdr->end) { | |
966 | return 0; | |
967 | } | |
968 | ||
969 | // allocate memory for the header_block. we'll read each blhdr into this | |
91447636 | 970 | if (kmem_alloc(kernel_map, (vm_offset_t *)&buff, jnl->jhdr->blhdr_size)) { |
b4c24cb9 A |
971 | printf("jnl: replay_journal: no memory for block buffer! (%d bytes)\n", |
972 | jnl->jhdr->blhdr_size); | |
973 | return -1; | |
974 | } | |
55e303ae A |
975 | |
976 | // allocate memory for the coalesce buffer | |
977 | if ((MALLOC(co_buf, struct bucket *, num_buckets*sizeof(struct bucket), M_TEMP, M_WAITOK)) == NULL) { | |
978 | printf("jnl: replay_journal: no memory for coalesce buffer!\n"); | |
979 | return -1; | |
980 | } | |
981 | ||
982 | // initialize entries | |
983 | for(i=0; i < num_buckets; i++) { | |
984 | co_buf[i].block_num = -1; | |
985 | } | |
986 | num_full = 0; // empty at first | |
987 | ||
b4c24cb9 A |
988 | |
989 | printf("jnl: replay_journal: from: %lld to: %lld (joffset 0x%llx)\n", | |
990 | jnl->jhdr->start, jnl->jhdr->end, jnl->jdev_offset); | |
991 | ||
992 | while(jnl->jhdr->start != jnl->jhdr->end) { | |
993 | offset = jnl->jhdr->start; | |
91447636 | 994 | ret = read_journal_data(jnl, &offset, buff, jnl->jhdr->blhdr_size); |
b4c24cb9 A |
995 | if (ret != jnl->jhdr->blhdr_size) { |
996 | printf("jnl: replay_journal: Could not read block list header block @ 0x%llx!\n", offset); | |
997 | goto bad_replay; | |
998 | } | |
999 | ||
91447636 | 1000 | blhdr = (block_list_header *)buff; |
55e303ae A |
1001 | |
1002 | orig_checksum = blhdr->checksum; | |
b4c24cb9 | 1003 | blhdr->checksum = 0; |
55e303ae A |
1004 | if (jnl->flags & JOURNAL_NEED_SWAP) { |
1005 | // calculate the checksum based on the unswapped data | |
1006 | // because it is done byte-at-a-time. | |
1007 | orig_checksum = SWAP32(orig_checksum); | |
1008 | checksum = calc_checksum((char *)blhdr, BLHDR_CHECKSUM_SIZE); | |
1009 | swap_block_list_header(jnl, blhdr); | |
1010 | } else { | |
1011 | checksum = calc_checksum((char *)blhdr, BLHDR_CHECKSUM_SIZE); | |
b4c24cb9 | 1012 | } |
55e303ae | 1013 | if (checksum != orig_checksum) { |
91447636 A |
1014 | printf("jnl: replay_journal: bad block list header @ 0x%llx (checksum 0x%x != 0x%x)\n", |
1015 | offset, orig_checksum, checksum); | |
1016 | goto bad_replay; | |
1017 | } | |
b4c24cb9 A |
1018 | if ( blhdr->max_blocks <= 0 || blhdr->max_blocks > 2048 |
1019 | || blhdr->num_blocks <= 0 || blhdr->num_blocks > blhdr->max_blocks) { | |
91447636 A |
1020 | printf("jnl: replay_journal: bad looking journal entry: max: %d num: %d\n", |
1021 | blhdr->max_blocks, blhdr->num_blocks); | |
1022 | goto bad_replay; | |
b4c24cb9 A |
1023 | } |
1024 | ||
483a1d10 | 1025 | for(i=1; i < blhdr->num_blocks; i++) { |
b4c24cb9 A |
1026 | if (blhdr->binfo[i].bnum < 0 && blhdr->binfo[i].bnum != (off_t)-1) { |
1027 | printf("jnl: replay_journal: bogus block number 0x%llx\n", blhdr->binfo[i].bnum); | |
1028 | goto bad_replay; | |
1029 | } | |
b4c24cb9 A |
1030 | } |
1031 | ||
55e303ae A |
1032 | //printf("jnl: replay_journal: adding %d blocks in journal entry @ 0x%llx to co_buf\n", |
1033 | // blhdr->num_blocks-1, jnl->jhdr->start); | |
b4c24cb9 | 1034 | for(i=1; i < blhdr->num_blocks; i++) { |
55e303ae A |
1035 | int size, ret_val; |
1036 | off_t number; | |
b4c24cb9 A |
1037 | |
1038 | size = blhdr->binfo[i].bsize; | |
55e303ae A |
1039 | number = blhdr->binfo[i].bnum; |
1040 | ||
1041 | // don't add "killed" blocks | |
1042 | if (number == (off_t)-1) { | |
1043 | //printf("jnl: replay_journal: skipping killed fs block (index %d)\n", i); | |
b4c24cb9 | 1044 | } else { |
55e303ae A |
1045 | // add this bucket to co_buf, coalescing where possible |
1046 | // printf("jnl: replay_journal: adding block 0x%llx\n", number); | |
1047 | ret_val = add_block(jnl, &co_buf, number, size, (size_t) offset, &num_buckets, &num_full); | |
1048 | ||
1049 | if (ret_val == -1) { | |
1050 | printf("jnl: replay_journal: trouble adding block to co_buf\n"); | |
1051 | goto bad_replay; | |
1052 | } // else printf("jnl: replay_journal: added block 0x%llx at i=%d\n", number); | |
b4c24cb9 | 1053 | } |
55e303ae A |
1054 | |
1055 | // increment offset | |
1056 | offset += size; | |
1057 | ||
1058 | // check if the last block added puts us off the end of the jnl. | |
1059 | // if so, we need to wrap to the beginning and take any remainder | |
1060 | // into account | |
b4c24cb9 A |
1061 | // |
1062 | if (offset >= jnl->jhdr->size) { | |
55e303ae | 1063 | offset = jnl->jhdr->jhdr_size + (offset - jnl->jhdr->size); |
b4c24cb9 A |
1064 | } |
1065 | } | |
1066 | ||
55e303ae | 1067 | |
b4c24cb9 A |
1068 | jnl->jhdr->start += blhdr->bytes_used; |
1069 | if (jnl->jhdr->start >= jnl->jhdr->size) { | |
1070 | // wrap around and skip the journal header block | |
1071 | jnl->jhdr->start = (jnl->jhdr->start % jnl->jhdr->size) + jnl->jhdr->jhdr_size; | |
1072 | } | |
55e303ae | 1073 | } |
b4c24cb9 | 1074 | |
55e303ae A |
1075 | |
1076 | //printf("jnl: replay_journal: replaying %d blocks\n", num_full); | |
1077 | ||
483a1d10 A |
1078 | /* |
1079 | * make sure it's at least one page in size, so | |
1080 | * start max_bsize at PAGE_SIZE | |
1081 | */ | |
1082 | for (i = 0, max_bsize = PAGE_SIZE; i < num_full; i++) { | |
1083 | ||
1084 | if (co_buf[i].block_num == (off_t)-1) | |
1085 | continue; | |
1086 | ||
1087 | if (co_buf[i].block_size > max_bsize) | |
1088 | max_bsize = co_buf[i].block_size; | |
1089 | } | |
1090 | /* | |
1091 | * round max_bsize up to the nearest PAGE_SIZE multiple | |
1092 | */ | |
1093 | if (max_bsize & (PAGE_SIZE - 1)) { | |
1094 | max_bsize = (max_bsize + PAGE_SIZE) & ~(PAGE_SIZE - 1); | |
1095 | } | |
1096 | ||
55e303ae A |
1097 | if (kmem_alloc(kernel_map, (vm_offset_t *)&block_ptr, max_bsize)) { |
1098 | goto bad_replay; | |
1099 | } | |
1100 | ||
1101 | // Replay the coalesced entries in the co-buf | |
1102 | for(i=0; i < num_full; i++) { | |
1103 | size_t size = co_buf[i].block_size; | |
1104 | off_t jnl_offset = (off_t) co_buf[i].jnl_offset; | |
1105 | off_t number = co_buf[i].block_num; | |
1106 | ||
1107 | ||
1108 | // printf("replaying co_buf[%d]: block 0x%llx, size 0x%x, jnl_offset 0x%llx\n", i, co_buf[i].block_num, | |
1109 | // co_buf[i].block_size, co_buf[i].jnl_offset); | |
1110 | ||
1111 | if (number == (off_t)-1) { | |
1112 | // printf("jnl: replay_journal: skipping killed fs block\n"); | |
1113 | } else { | |
1114 | ||
1115 | // do journal read, and set the phys. block | |
1116 | ret = read_journal_data(jnl, &jnl_offset, block_ptr, size); | |
1117 | if (ret != size) { | |
1118 | printf("jnl: replay_journal: Could not read journal entry data @ offset 0x%llx!\n", offset); | |
1119 | goto bad_replay; | |
1120 | } | |
1121 | ||
1122 | if (update_fs_block(jnl, block_ptr, number, size) != 0) { | |
1123 | goto bad_replay; | |
1124 | } | |
1125 | } | |
b4c24cb9 | 1126 | } |
55e303ae | 1127 | |
b4c24cb9 | 1128 | |
55e303ae A |
1129 | // done replaying; update jnl header |
1130 | if (write_journal_header(jnl) != 0) { | |
1131 | goto bad_replay; | |
1132 | } | |
1133 | ||
1134 | // free block_ptr | |
1135 | kmem_free(kernel_map, (vm_offset_t)block_ptr, max_bsize); | |
1136 | block_ptr = NULL; | |
1137 | ||
1138 | // free the coalesce buffer | |
1139 | FREE(co_buf, M_TEMP); | |
1140 | co_buf = NULL; | |
1141 | ||
91447636 | 1142 | kmem_free(kernel_map, (vm_offset_t)buff, jnl->jhdr->blhdr_size); |
b4c24cb9 A |
1143 | return 0; |
1144 | ||
1145 | bad_replay: | |
1146 | if (block_ptr) { | |
1147 | kmem_free(kernel_map, (vm_offset_t)block_ptr, max_bsize); | |
1148 | } | |
55e303ae A |
1149 | if (co_buf) { |
1150 | FREE(co_buf, M_TEMP); | |
1151 | } | |
91447636 | 1152 | kmem_free(kernel_map, (vm_offset_t)buff, jnl->jhdr->blhdr_size); |
55e303ae | 1153 | |
b4c24cb9 A |
1154 | return -1; |
1155 | } | |
1156 | ||
1157 | ||
1158 | #define DEFAULT_TRANSACTION_BUFFER_SIZE (128*1024) | |
1159 | //#define DEFAULT_TRANSACTION_BUFFER_SIZE (256*1024) // better performance but uses more mem | |
1160 | #define MAX_TRANSACTION_BUFFER_SIZE (512*1024) | |
1161 | ||
1162 | // XXXdbg - so I can change it in the debugger | |
1163 | int def_tbuffer_size = 0; | |
1164 | ||
1165 | ||
1166 | // | |
1167 | // This function sets the size of the tbuffer and the | |
1168 | // size of the blhdr. It assumes that jnl->jhdr->size | |
1169 | // and jnl->jhdr->jhdr_size are already valid. | |
1170 | // | |
1171 | static void | |
1172 | size_up_tbuffer(journal *jnl, int tbuffer_size, int phys_blksz) | |
1173 | { | |
1174 | // | |
1175 | // one-time initialization based on how much memory | |
1176 | // there is in the machine. | |
1177 | // | |
1178 | if (def_tbuffer_size == 0) { | |
1179 | if (mem_size < (256*1024*1024)) { | |
1180 | def_tbuffer_size = DEFAULT_TRANSACTION_BUFFER_SIZE; | |
1181 | } else if (mem_size < (512*1024*1024)) { | |
1182 | def_tbuffer_size = DEFAULT_TRANSACTION_BUFFER_SIZE * 2; | |
1183 | } else if (mem_size < (1024*1024*1024)) { | |
1184 | def_tbuffer_size = DEFAULT_TRANSACTION_BUFFER_SIZE * 3; | |
1185 | } else if (mem_size >= (1024*1024*1024)) { | |
1186 | def_tbuffer_size = DEFAULT_TRANSACTION_BUFFER_SIZE * 4; | |
1187 | } | |
1188 | } | |
1189 | ||
1190 | // size up the transaction buffer... can't be larger than the number | |
1191 | // of blocks that can fit in a block_list_header block. | |
1192 | if (tbuffer_size == 0) { | |
1193 | jnl->tbuffer_size = def_tbuffer_size; | |
1194 | } else { | |
1195 | // make sure that the specified tbuffer_size isn't too small | |
1196 | if (tbuffer_size < jnl->jhdr->blhdr_size * 2) { | |
1197 | tbuffer_size = jnl->jhdr->blhdr_size * 2; | |
1198 | } | |
1199 | // and make sure it's an even multiple of the block size | |
1200 | if ((tbuffer_size % jnl->jhdr->jhdr_size) != 0) { | |
1201 | tbuffer_size -= (tbuffer_size % jnl->jhdr->jhdr_size); | |
1202 | } | |
1203 | ||
1204 | jnl->tbuffer_size = tbuffer_size; | |
1205 | } | |
1206 | ||
1207 | if (jnl->tbuffer_size > (jnl->jhdr->size / 2)) { | |
1208 | jnl->tbuffer_size = (jnl->jhdr->size / 2); | |
1209 | } | |
1210 | ||
1211 | if (jnl->tbuffer_size > MAX_TRANSACTION_BUFFER_SIZE) { | |
1212 | jnl->tbuffer_size = MAX_TRANSACTION_BUFFER_SIZE; | |
1213 | } | |
1214 | ||
1215 | jnl->jhdr->blhdr_size = (jnl->tbuffer_size / jnl->jhdr->jhdr_size) * sizeof(block_info); | |
55e303ae A |
1216 | if (jnl->jhdr->blhdr_size < phys_blksz) { |
1217 | jnl->jhdr->blhdr_size = phys_blksz; | |
1218 | } else if ((jnl->jhdr->blhdr_size % phys_blksz) != 0) { | |
1219 | // have to round up so we're an even multiple of the physical block size | |
1220 | jnl->jhdr->blhdr_size = (jnl->jhdr->blhdr_size + (phys_blksz - 1)) & ~(phys_blksz - 1); | |
1221 | } | |
b4c24cb9 A |
1222 | } |
1223 | ||
1224 | ||
1225 | ||
1226 | journal * | |
1227 | journal_create(struct vnode *jvp, | |
1228 | off_t offset, | |
1229 | off_t journal_size, | |
1230 | struct vnode *fsvp, | |
1231 | size_t min_fs_blksz, | |
1232 | int32_t flags, | |
1233 | int32_t tbuffer_size, | |
1234 | void (*flush)(void *arg), | |
1235 | void *arg) | |
1236 | { | |
1237 | journal *jnl; | |
91447636 A |
1238 | int phys_blksz; |
1239 | struct vfs_context context; | |
1240 | ||
1241 | context.vc_proc = current_proc(); | |
1242 | context.vc_ucred = FSCRED; | |
b4c24cb9 A |
1243 | |
1244 | /* Get the real physical block size. */ | |
91447636 | 1245 | if (VNOP_IOCTL(jvp, DKIOCGETBLOCKSIZE, (caddr_t)&phys_blksz, 0, &context)) { |
b4c24cb9 A |
1246 | return NULL; |
1247 | } | |
1248 | ||
1249 | if (phys_blksz > min_fs_blksz) { | |
1250 | printf("jnl: create: error: phys blksize %d bigger than min fs blksize %d\n", | |
1251 | phys_blksz, min_fs_blksz); | |
1252 | return NULL; | |
1253 | } | |
1254 | ||
1255 | if ((journal_size % phys_blksz) != 0) { | |
1256 | printf("jnl: create: journal size 0x%llx is not an even multiple of block size 0x%x\n", | |
1257 | journal_size, phys_blksz); | |
1258 | return NULL; | |
1259 | } | |
1260 | ||
d7e50217 | 1261 | MALLOC_ZONE(jnl, struct journal *, sizeof(struct journal), M_JNL_JNL, M_WAITOK); |
b4c24cb9 A |
1262 | memset(jnl, 0, sizeof(*jnl)); |
1263 | ||
1264 | jnl->jdev = jvp; | |
1265 | jnl->jdev_offset = offset; | |
1266 | jnl->fsdev = fsvp; | |
1267 | jnl->flush = flush; | |
1268 | jnl->flush_arg = arg; | |
1269 | jnl->flags = (flags & JOURNAL_OPTION_FLAGS_MASK); | |
91447636 | 1270 | lck_mtx_init(&jnl->old_start_lock, jnl_mutex_group, jnl_lock_attr); |
b4c24cb9 A |
1271 | |
1272 | if (kmem_alloc(kernel_map, (vm_offset_t *)&jnl->header_buf, phys_blksz)) { | |
1273 | printf("jnl: create: could not allocate space for header buffer (%d bytes)\n", phys_blksz); | |
1274 | goto bad_kmem_alloc; | |
1275 | } | |
1276 | ||
1277 | memset(jnl->header_buf, 0, phys_blksz); | |
1278 | ||
1279 | jnl->jhdr = (journal_header *)jnl->header_buf; | |
1280 | jnl->jhdr->magic = JOURNAL_HEADER_MAGIC; | |
1281 | jnl->jhdr->endian = ENDIAN_MAGIC; | |
1282 | jnl->jhdr->start = phys_blksz; // start at block #1, block #0 is for the jhdr itself | |
1283 | jnl->jhdr->end = phys_blksz; | |
1284 | jnl->jhdr->size = journal_size; | |
1285 | jnl->jhdr->jhdr_size = phys_blksz; | |
1286 | size_up_tbuffer(jnl, tbuffer_size, phys_blksz); | |
1287 | ||
1288 | jnl->active_start = jnl->jhdr->start; | |
1289 | ||
1290 | // XXXdbg - for testing you can force the journal to wrap around | |
1291 | // jnl->jhdr->start = jnl->jhdr->size - (phys_blksz*3); | |
1292 | // jnl->jhdr->end = jnl->jhdr->size - (phys_blksz*3); | |
1293 | ||
91447636 | 1294 | lck_mtx_init(&jnl->jlock, jnl_mutex_group, jnl_lock_attr); |
b4c24cb9 A |
1295 | |
1296 | if (write_journal_header(jnl) != 0) { | |
1297 | printf("jnl: journal_create: failed to write journal header.\n"); | |
1298 | goto bad_write; | |
1299 | } | |
1300 | ||
1301 | return jnl; | |
1302 | ||
1303 | ||
1304 | bad_write: | |
b4c24cb9 A |
1305 | kmem_free(kernel_map, (vm_offset_t)jnl->header_buf, phys_blksz); |
1306 | bad_kmem_alloc: | |
1307 | jnl->jhdr = NULL; | |
d7e50217 | 1308 | FREE_ZONE(jnl, sizeof(struct journal), M_JNL_JNL); |
b4c24cb9 A |
1309 | return NULL; |
1310 | } | |
1311 | ||
1312 | ||
1313 | journal * | |
1314 | journal_open(struct vnode *jvp, | |
1315 | off_t offset, | |
1316 | off_t journal_size, | |
1317 | struct vnode *fsvp, | |
1318 | size_t min_fs_blksz, | |
1319 | int32_t flags, | |
1320 | int32_t tbuffer_size, | |
1321 | void (*flush)(void *arg), | |
1322 | void *arg) | |
1323 | { | |
1324 | journal *jnl; | |
91447636 | 1325 | int orig_blksz=0, phys_blksz; |
55e303ae | 1326 | int orig_checksum, checksum; |
91447636 A |
1327 | struct vfs_context context; |
1328 | ||
1329 | context.vc_proc = current_proc(); | |
1330 | context.vc_ucred = FSCRED; | |
b4c24cb9 A |
1331 | |
1332 | /* Get the real physical block size. */ | |
91447636 | 1333 | if (VNOP_IOCTL(jvp, DKIOCGETBLOCKSIZE, (caddr_t)&phys_blksz, 0, &context)) { |
b4c24cb9 A |
1334 | return NULL; |
1335 | } | |
1336 | ||
1337 | if (phys_blksz > min_fs_blksz) { | |
1338 | printf("jnl: create: error: phys blksize %d bigger than min fs blksize %d\n", | |
1339 | phys_blksz, min_fs_blksz); | |
1340 | return NULL; | |
1341 | } | |
1342 | ||
1343 | if ((journal_size % phys_blksz) != 0) { | |
1344 | printf("jnl: open: journal size 0x%llx is not an even multiple of block size 0x%x\n", | |
1345 | journal_size, phys_blksz); | |
1346 | return NULL; | |
1347 | } | |
1348 | ||
d7e50217 | 1349 | MALLOC_ZONE(jnl, struct journal *, sizeof(struct journal), M_JNL_JNL, M_WAITOK); |
b4c24cb9 A |
1350 | memset(jnl, 0, sizeof(*jnl)); |
1351 | ||
1352 | jnl->jdev = jvp; | |
1353 | jnl->jdev_offset = offset; | |
1354 | jnl->fsdev = fsvp; | |
1355 | jnl->flush = flush; | |
1356 | jnl->flush_arg = arg; | |
1357 | jnl->flags = (flags & JOURNAL_OPTION_FLAGS_MASK); | |
91447636 | 1358 | lck_mtx_init(&jnl->old_start_lock, jnl_mutex_group, jnl_lock_attr); |
b4c24cb9 A |
1359 | |
1360 | if (kmem_alloc(kernel_map, (vm_offset_t *)&jnl->header_buf, phys_blksz)) { | |
1361 | printf("jnl: create: could not allocate space for header buffer (%d bytes)\n", phys_blksz); | |
1362 | goto bad_kmem_alloc; | |
1363 | } | |
1364 | ||
1365 | jnl->jhdr = (journal_header *)jnl->header_buf; | |
1366 | memset(jnl->jhdr, 0, sizeof(journal_header)+4); | |
1367 | ||
1368 | // we have to set this up here so that do_journal_io() will work | |
1369 | jnl->jhdr->jhdr_size = phys_blksz; | |
1370 | ||
55e303ae | 1371 | if (read_journal_header(jnl, jnl->jhdr, phys_blksz) != phys_blksz) { |
b4c24cb9 A |
1372 | printf("jnl: open: could not read %d bytes for the journal header.\n", |
1373 | phys_blksz); | |
1374 | goto bad_journal; | |
1375 | } | |
1376 | ||
55e303ae A |
1377 | orig_checksum = jnl->jhdr->checksum; |
1378 | jnl->jhdr->checksum = 0; | |
1379 | ||
1380 | if (jnl->jhdr->magic == SWAP32(JOURNAL_HEADER_MAGIC)) { | |
1381 | // do this before the swap since it's done byte-at-a-time | |
1382 | orig_checksum = SWAP32(orig_checksum); | |
1383 | checksum = calc_checksum((char *)jnl->jhdr, sizeof(struct journal_header)); | |
1384 | swap_journal_header(jnl); | |
1385 | jnl->flags |= JOURNAL_NEED_SWAP; | |
1386 | } else { | |
1387 | checksum = calc_checksum((char *)jnl->jhdr, sizeof(struct journal_header)); | |
1388 | } | |
1389 | ||
b4c24cb9 A |
1390 | if (jnl->jhdr->magic != JOURNAL_HEADER_MAGIC && jnl->jhdr->magic != OLD_JOURNAL_HEADER_MAGIC) { |
1391 | printf("jnl: open: journal magic is bad (0x%x != 0x%x)\n", | |
1392 | jnl->jhdr->magic, JOURNAL_HEADER_MAGIC); | |
1393 | goto bad_journal; | |
1394 | } | |
1395 | ||
1396 | // only check if we're the current journal header magic value | |
1397 | if (jnl->jhdr->magic == JOURNAL_HEADER_MAGIC) { | |
b4c24cb9 | 1398 | |
55e303ae A |
1399 | if (orig_checksum != checksum) { |
1400 | printf("jnl: open: journal checksum is bad (0x%x != 0x%x)\n", | |
1401 | orig_checksum, checksum); | |
1402 | ||
b4c24cb9 A |
1403 | //goto bad_journal; |
1404 | } | |
1405 | } | |
1406 | ||
1407 | // XXXdbg - convert old style magic numbers to the new one | |
1408 | if (jnl->jhdr->magic == OLD_JOURNAL_HEADER_MAGIC) { | |
1409 | jnl->jhdr->magic = JOURNAL_HEADER_MAGIC; | |
1410 | } | |
1411 | ||
1412 | if (phys_blksz != jnl->jhdr->jhdr_size && jnl->jhdr->jhdr_size != 0) { | |
1413 | printf("jnl: open: phys_blksz %d does not match journal header size %d\n", | |
1414 | phys_blksz, jnl->jhdr->jhdr_size); | |
1415 | ||
1416 | orig_blksz = phys_blksz; | |
1417 | phys_blksz = jnl->jhdr->jhdr_size; | |
91447636 | 1418 | if (VNOP_IOCTL(jvp, DKIOCSETBLOCKSIZE, (caddr_t)&phys_blksz, FWRITE, &context)) { |
b4c24cb9 A |
1419 | printf("jnl: could not set block size to %d bytes.\n", phys_blksz); |
1420 | goto bad_journal; | |
1421 | } | |
1422 | // goto bad_journal; | |
1423 | } | |
1424 | ||
1425 | if ( jnl->jhdr->start <= 0 | |
1426 | || jnl->jhdr->start > jnl->jhdr->size | |
55e303ae | 1427 | || jnl->jhdr->start > 1024*1024*1024) { |
b4c24cb9 A |
1428 | printf("jnl: open: jhdr start looks bad (0x%llx max size 0x%llx)\n", |
1429 | jnl->jhdr->start, jnl->jhdr->size); | |
1430 | goto bad_journal; | |
1431 | } | |
1432 | ||
1433 | if ( jnl->jhdr->end <= 0 | |
1434 | || jnl->jhdr->end > jnl->jhdr->size | |
55e303ae | 1435 | || jnl->jhdr->end > 1024*1024*1024) { |
b4c24cb9 A |
1436 | printf("jnl: open: jhdr end looks bad (0x%llx max size 0x%llx)\n", |
1437 | jnl->jhdr->end, jnl->jhdr->size); | |
1438 | goto bad_journal; | |
1439 | } | |
1440 | ||
55e303ae | 1441 | if (jnl->jhdr->size > 1024*1024*1024) { |
b4c24cb9 A |
1442 | printf("jnl: open: jhdr size looks bad (0x%llx)\n", jnl->jhdr->size); |
1443 | goto bad_journal; | |
1444 | } | |
1445 | ||
1446 | // XXXdbg - can't do these checks because hfs writes all kinds of | |
1447 | // non-uniform sized blocks even on devices that have a block size | |
1448 | // that is larger than 512 bytes (i.e. optical media w/2k blocks). | |
1449 | // therefore these checks will fail and so we just have to punt and | |
1450 | // do more relaxed checking... | |
1451 | // XXXdbg if ((jnl->jhdr->start % jnl->jhdr->jhdr_size) != 0) { | |
1452 | if ((jnl->jhdr->start % 512) != 0) { | |
1453 | printf("jnl: open: journal start (0x%llx) not a multiple of 512?\n", | |
1454 | jnl->jhdr->start); | |
1455 | goto bad_journal; | |
1456 | } | |
1457 | ||
1458 | //XXXdbg if ((jnl->jhdr->end % jnl->jhdr->jhdr_size) != 0) { | |
1459 | if ((jnl->jhdr->end % 512) != 0) { | |
1460 | printf("jnl: open: journal end (0x%llx) not a multiple of block size (0x%x)?\n", | |
1461 | jnl->jhdr->end, jnl->jhdr->jhdr_size); | |
1462 | goto bad_journal; | |
1463 | } | |
1464 | ||
1465 | // take care of replaying the journal if necessary | |
1466 | if (flags & JOURNAL_RESET) { | |
1467 | printf("jnl: journal start/end pointers reset! (jnl 0x%x; s 0x%llx e 0x%llx)\n", | |
1468 | jnl, jnl->jhdr->start, jnl->jhdr->end); | |
1469 | jnl->jhdr->start = jnl->jhdr->end; | |
1470 | } else if (replay_journal(jnl) != 0) { | |
1471 | printf("jnl: journal_open: Error replaying the journal!\n"); | |
1472 | goto bad_journal; | |
1473 | } | |
1474 | ||
1475 | if (orig_blksz != 0) { | |
91447636 | 1476 | VNOP_IOCTL(jvp, DKIOCSETBLOCKSIZE, (caddr_t)&orig_blksz, FWRITE, &context); |
b4c24cb9 | 1477 | phys_blksz = orig_blksz; |
55e303ae A |
1478 | if (orig_blksz < jnl->jhdr->jhdr_size) { |
1479 | printf("jnl: open: jhdr_size is %d but orig phys blk size is %d. switching.\n", | |
1480 | jnl->jhdr->jhdr_size, orig_blksz); | |
1481 | ||
1482 | jnl->jhdr->jhdr_size = orig_blksz; | |
1483 | } | |
b4c24cb9 A |
1484 | } |
1485 | ||
1486 | // make sure this is in sync! | |
1487 | jnl->active_start = jnl->jhdr->start; | |
1488 | ||
1489 | // set this now, after we've replayed the journal | |
1490 | size_up_tbuffer(jnl, tbuffer_size, phys_blksz); | |
1491 | ||
91447636 | 1492 | lck_mtx_init(&jnl->jlock, jnl_mutex_group, jnl_lock_attr); |
b4c24cb9 A |
1493 | |
1494 | return jnl; | |
1495 | ||
1496 | bad_journal: | |
1497 | if (orig_blksz != 0) { | |
1498 | phys_blksz = orig_blksz; | |
91447636 | 1499 | VNOP_IOCTL(jvp, DKIOCSETBLOCKSIZE, (caddr_t)&orig_blksz, FWRITE, &context); |
b4c24cb9 A |
1500 | } |
1501 | kmem_free(kernel_map, (vm_offset_t)jnl->header_buf, phys_blksz); | |
1502 | bad_kmem_alloc: | |
d7e50217 | 1503 | FREE_ZONE(jnl, sizeof(struct journal), M_JNL_JNL); |
b4c24cb9 A |
1504 | return NULL; |
1505 | } | |
1506 | ||
743b1565 A |
1507 | |
1508 | int | |
1509 | journal_is_clean(struct vnode *jvp, | |
1510 | off_t offset, | |
1511 | off_t journal_size, | |
1512 | struct vnode *fsvp, | |
1513 | size_t min_fs_block_size) | |
1514 | { | |
1515 | journal jnl; | |
1516 | int phys_blksz, ret; | |
1517 | int orig_checksum, checksum; | |
1518 | struct vfs_context context; | |
1519 | ||
1520 | context.vc_proc = current_proc(); | |
1521 | context.vc_ucred = FSCRED; | |
1522 | ||
1523 | /* Get the real physical block size. */ | |
1524 | if (VNOP_IOCTL(jvp, DKIOCGETBLOCKSIZE, (caddr_t)&phys_blksz, 0, &context)) { | |
1525 | printf("jnl: is_clean: failed to get device block size.\n"); | |
1526 | return EINVAL; | |
1527 | } | |
1528 | ||
1529 | if (phys_blksz > min_fs_block_size) { | |
1530 | printf("jnl: is_clean: error: phys blksize %d bigger than min fs blksize %d\n", | |
1531 | phys_blksz, min_fs_block_size); | |
1532 | return EINVAL; | |
1533 | } | |
1534 | ||
1535 | if ((journal_size % phys_blksz) != 0) { | |
1536 | printf("jnl: is_clean: journal size 0x%llx is not an even multiple of block size 0x%x\n", | |
1537 | journal_size, phys_blksz); | |
1538 | return EINVAL; | |
1539 | } | |
1540 | ||
1541 | memset(&jnl, 0, sizeof(jnl)); | |
1542 | ||
1543 | if (kmem_alloc(kernel_map, (vm_offset_t *)&jnl.header_buf, phys_blksz)) { | |
1544 | printf("jnl: is_clean: could not allocate space for header buffer (%d bytes)\n", phys_blksz); | |
1545 | return ENOMEM; | |
1546 | } | |
1547 | ||
1548 | jnl.jhdr = (journal_header *)jnl.header_buf; | |
1549 | memset(jnl.jhdr, 0, sizeof(journal_header)+4); | |
1550 | ||
1551 | jnl.jdev = jvp; | |
1552 | jnl.jdev_offset = offset; | |
1553 | jnl.fsdev = fsvp; | |
1554 | ||
1555 | // we have to set this up here so that do_journal_io() will work | |
1556 | jnl.jhdr->jhdr_size = phys_blksz; | |
1557 | ||
1558 | if (read_journal_header(&jnl, jnl.jhdr, phys_blksz) != phys_blksz) { | |
1559 | printf("jnl: is_clean: could not read %d bytes for the journal header.\n", | |
1560 | phys_blksz); | |
1561 | ret = EINVAL; | |
1562 | goto get_out; | |
1563 | } | |
1564 | ||
1565 | orig_checksum = jnl.jhdr->checksum; | |
1566 | jnl.jhdr->checksum = 0; | |
1567 | ||
1568 | if (jnl.jhdr->magic == SWAP32(JOURNAL_HEADER_MAGIC)) { | |
1569 | // do this before the swap since it's done byte-at-a-time | |
1570 | orig_checksum = SWAP32(orig_checksum); | |
1571 | checksum = calc_checksum((char *)jnl.jhdr, sizeof(struct journal_header)); | |
1572 | swap_journal_header(&jnl); | |
1573 | jnl.flags |= JOURNAL_NEED_SWAP; | |
1574 | } else { | |
1575 | checksum = calc_checksum((char *)jnl.jhdr, sizeof(struct journal_header)); | |
1576 | } | |
1577 | ||
1578 | if (jnl.jhdr->magic != JOURNAL_HEADER_MAGIC && jnl.jhdr->magic != OLD_JOURNAL_HEADER_MAGIC) { | |
1579 | printf("jnl: is_clean: journal magic is bad (0x%x != 0x%x)\n", | |
1580 | jnl.jhdr->magic, JOURNAL_HEADER_MAGIC); | |
1581 | ret = EINVAL; | |
1582 | goto get_out; | |
1583 | } | |
1584 | ||
1585 | if (orig_checksum != checksum) { | |
1586 | printf("jnl: is_clean: journal checksum is bad (0x%x != 0x%x)\n", orig_checksum, checksum); | |
1587 | ret = EINVAL; | |
1588 | goto get_out; | |
1589 | } | |
1590 | ||
1591 | // | |
1592 | // if the start and end are equal then the journal is clean. | |
1593 | // otherwise it's not clean and therefore an error. | |
1594 | // | |
1595 | if (jnl.jhdr->start == jnl.jhdr->end) { | |
1596 | ret = 0; | |
1597 | } else { | |
1598 | ret = EINVAL; | |
1599 | } | |
1600 | ||
1601 | get_out: | |
1602 | kmem_free(kernel_map, (vm_offset_t)jnl.header_buf, phys_blksz); | |
1603 | ||
1604 | return ret; | |
1605 | ||
1606 | ||
1607 | } | |
1608 | ||
1609 | ||
1610 | ||
b4c24cb9 A |
1611 | void |
1612 | journal_close(journal *jnl) | |
1613 | { | |
1614 | volatile off_t *start, *end; | |
1615 | int counter=0; | |
1616 | ||
1617 | CHECK_JOURNAL(jnl); | |
1618 | ||
1619 | // set this before doing anything that would block so that | |
1620 | // we start tearing things down properly. | |
1621 | // | |
1622 | jnl->flags |= JOURNAL_CLOSE_PENDING; | |
1623 | ||
91447636 A |
1624 | if (jnl->owner != current_thread()) { |
1625 | lock_journal(jnl); | |
b4c24cb9 A |
1626 | } |
1627 | ||
1628 | // | |
1629 | // only write stuff to disk if the journal is still valid | |
1630 | // | |
1631 | if ((jnl->flags & JOURNAL_INVALID) == 0) { | |
1632 | ||
1633 | if (jnl->active_tr) { | |
1634 | journal_end_transaction(jnl); | |
1635 | } | |
1636 | ||
1637 | // flush any buffered transactions | |
1638 | if (jnl->cur_tr) { | |
1639 | transaction *tr = jnl->cur_tr; | |
1640 | ||
1641 | jnl->cur_tr = NULL; | |
1642 | end_transaction(tr, 1); // force it to get flushed | |
1643 | } | |
1644 | ||
1645 | //start = &jnl->jhdr->start; | |
1646 | start = &jnl->active_start; | |
1647 | end = &jnl->jhdr->end; | |
1648 | ||
1649 | while (*start != *end && counter++ < 500) { | |
1650 | printf("jnl: close: flushing the buffer cache (start 0x%llx end 0x%llx)\n", *start, *end); | |
1651 | if (jnl->flush) { | |
1652 | jnl->flush(jnl->flush_arg); | |
1653 | } | |
d7e50217 | 1654 | tsleep((caddr_t)jnl, PRIBIO, "jnl_close", 1); |
b4c24cb9 A |
1655 | } |
1656 | ||
1657 | if (*start != *end) { | |
1658 | printf("jnl: close: buffer flushing didn't seem to flush out all the transactions! (0x%llx - 0x%llx)\n", | |
1659 | *start, *end); | |
1660 | } | |
1661 | ||
1662 | // make sure this is in sync when we close the journal | |
1663 | jnl->jhdr->start = jnl->active_start; | |
1664 | ||
1665 | // if this fails there's not much we can do at this point... | |
1666 | write_journal_header(jnl); | |
1667 | } else { | |
1668 | // if we're here the journal isn't valid any more. | |
1669 | // so make sure we don't leave any locked blocks lying around | |
1670 | printf("jnl: close: journal 0x%x, is invalid. aborting outstanding transactions\n", jnl); | |
1671 | if (jnl->active_tr || jnl->cur_tr) { | |
1672 | transaction *tr; | |
1673 | if (jnl->active_tr) { | |
1674 | tr = jnl->active_tr; | |
1675 | jnl->active_tr = NULL; | |
1676 | } else { | |
1677 | tr = jnl->cur_tr; | |
1678 | jnl->cur_tr = NULL; | |
1679 | } | |
1680 | ||
1681 | abort_transaction(jnl, tr); | |
1682 | if (jnl->active_tr || jnl->cur_tr) { | |
1683 | panic("jnl: close: jnl @ 0x%x had both an active and cur tr\n", jnl); | |
1684 | } | |
1685 | } | |
1686 | } | |
1687 | ||
1688 | free_old_stuff(jnl); | |
1689 | ||
1690 | kmem_free(kernel_map, (vm_offset_t)jnl->header_buf, jnl->jhdr->jhdr_size); | |
1691 | jnl->jhdr = (void *)0xbeefbabe; | |
1692 | ||
d7e50217 | 1693 | FREE_ZONE(jnl, sizeof(struct journal), M_JNL_JNL); |
b4c24cb9 A |
1694 | } |
1695 | ||
1696 | static void | |
1697 | dump_journal(journal *jnl) | |
1698 | { | |
1699 | transaction *ctr; | |
1700 | ||
1701 | printf("journal:"); | |
1702 | printf(" jdev_offset %.8llx\n", jnl->jdev_offset); | |
1703 | printf(" magic: 0x%.8x\n", jnl->jhdr->magic); | |
1704 | printf(" start: 0x%.8llx\n", jnl->jhdr->start); | |
1705 | printf(" end: 0x%.8llx\n", jnl->jhdr->end); | |
1706 | printf(" size: 0x%.8llx\n", jnl->jhdr->size); | |
1707 | printf(" blhdr size: %d\n", jnl->jhdr->blhdr_size); | |
1708 | printf(" jhdr size: %d\n", jnl->jhdr->jhdr_size); | |
1709 | printf(" chksum: 0x%.8x\n", jnl->jhdr->checksum); | |
1710 | ||
1711 | printf(" completed transactions:\n"); | |
1712 | for(ctr=jnl->completed_trs; ctr; ctr=ctr->next) { | |
1713 | printf(" 0x%.8llx - 0x%.8llx\n", ctr->journal_start, ctr->journal_end); | |
1714 | } | |
1715 | } | |
1716 | ||
1717 | ||
1718 | ||
1719 | static off_t | |
1720 | free_space(journal *jnl) | |
1721 | { | |
1722 | off_t free_space; | |
1723 | ||
1724 | if (jnl->jhdr->start < jnl->jhdr->end) { | |
1725 | free_space = jnl->jhdr->size - (jnl->jhdr->end - jnl->jhdr->start) - jnl->jhdr->jhdr_size; | |
1726 | } else if (jnl->jhdr->start > jnl->jhdr->end) { | |
1727 | free_space = jnl->jhdr->start - jnl->jhdr->end; | |
1728 | } else { | |
1729 | // journal is completely empty | |
1730 | free_space = jnl->jhdr->size - jnl->jhdr->jhdr_size; | |
1731 | } | |
1732 | ||
1733 | return free_space; | |
1734 | } | |
1735 | ||
1736 | ||
1737 | // | |
1738 | // The journal must be locked on entry to this function. | |
1739 | // The "desired_size" is in bytes. | |
1740 | // | |
1741 | static int | |
1742 | check_free_space(journal *jnl, int desired_size) | |
1743 | { | |
1744 | int i, counter=0; | |
1745 | ||
1746 | //printf("jnl: check free space (desired 0x%x, avail 0x%Lx)\n", | |
1747 | // desired_size, free_space(jnl)); | |
1748 | ||
1749 | while (1) { | |
55e303ae A |
1750 | int old_start_empty; |
1751 | ||
b4c24cb9 A |
1752 | if (counter++ == 5000) { |
1753 | dump_journal(jnl); | |
1754 | panic("jnl: check_free_space: buffer flushing isn't working " | |
1755 | "(jnl @ 0x%x s %lld e %lld f %lld [active start %lld]).\n", jnl, | |
1756 | jnl->jhdr->start, jnl->jhdr->end, free_space(jnl), jnl->active_start); | |
1757 | } | |
1758 | if (counter > 7500) { | |
1759 | printf("jnl: check_free_space: giving up waiting for free space.\n"); | |
1760 | return ENOSPC; | |
1761 | } | |
1762 | ||
1763 | // make sure there's space in the journal to hold this transaction | |
1764 | if (free_space(jnl) > desired_size) { | |
1765 | break; | |
1766 | } | |
1767 | ||
1768 | // | |
1769 | // here's where we lazily bump up jnl->jhdr->start. we'll consume | |
1770 | // entries until there is enough space for the next transaction. | |
1771 | // | |
55e303ae | 1772 | old_start_empty = 1; |
91447636 | 1773 | lock_oldstart(jnl); |
b4c24cb9 A |
1774 | for(i=0; i < sizeof(jnl->old_start)/sizeof(jnl->old_start[0]); i++) { |
1775 | int counter; | |
1776 | ||
1777 | counter = 0; | |
1778 | while (jnl->old_start[i] & 0x8000000000000000LL) { | |
1779 | if (counter++ > 100) { | |
1780 | panic("jnl: check_free_space: tr starting @ 0x%llx not flushing (jnl 0x%x).\n", | |
1781 | jnl->old_start[i], jnl); | |
1782 | } | |
1783 | ||
91447636 | 1784 | unlock_oldstart(jnl); |
b4c24cb9 A |
1785 | if (jnl->flush) { |
1786 | jnl->flush(jnl->flush_arg); | |
1787 | } | |
1788 | tsleep((caddr_t)jnl, PRIBIO, "check_free_space1", 1); | |
91447636 | 1789 | lock_oldstart(jnl); |
b4c24cb9 A |
1790 | } |
1791 | ||
1792 | if (jnl->old_start[i] == 0) { | |
1793 | continue; | |
1794 | } | |
1795 | ||
55e303ae | 1796 | old_start_empty = 0; |
b4c24cb9 A |
1797 | jnl->jhdr->start = jnl->old_start[i]; |
1798 | jnl->old_start[i] = 0; | |
1799 | if (free_space(jnl) > desired_size) { | |
91447636 | 1800 | unlock_oldstart(jnl); |
b4c24cb9 | 1801 | write_journal_header(jnl); |
91447636 | 1802 | lock_oldstart(jnl); |
b4c24cb9 A |
1803 | break; |
1804 | } | |
1805 | } | |
91447636 | 1806 | unlock_oldstart(jnl); |
b4c24cb9 A |
1807 | |
1808 | // if we bumped the start, loop and try again | |
1809 | if (i < sizeof(jnl->old_start)/sizeof(jnl->old_start[0])) { | |
1810 | continue; | |
55e303ae A |
1811 | } else if (old_start_empty) { |
1812 | // | |
1813 | // if there is nothing in old_start anymore then we can | |
1814 | // bump the jhdr->start to be the same as active_start | |
1815 | // since it is possible there was only one very large | |
1816 | // transaction in the old_start array. if we didn't do | |
1817 | // this then jhdr->start would never get updated and we | |
1818 | // would wind up looping until we hit the panic at the | |
1819 | // start of the loop. | |
1820 | // | |
1821 | jnl->jhdr->start = jnl->active_start; | |
1822 | write_journal_header(jnl); | |
1823 | continue; | |
b4c24cb9 A |
1824 | } |
1825 | ||
1826 | ||
1827 | // if the file system gave us a flush function, call it to so that | |
1828 | // it can flush some blocks which hopefully will cause some transactions | |
1829 | // to complete and thus free up space in the journal. | |
1830 | if (jnl->flush) { | |
1831 | jnl->flush(jnl->flush_arg); | |
1832 | } | |
1833 | ||
1834 | // wait for a while to avoid being cpu-bound (this will | |
1835 | // put us to sleep for 10 milliseconds) | |
1836 | tsleep((caddr_t)jnl, PRIBIO, "check_free_space2", 1); | |
1837 | } | |
1838 | ||
1839 | return 0; | |
1840 | } | |
1841 | ||
1842 | int | |
1843 | journal_start_transaction(journal *jnl) | |
1844 | { | |
1845 | int ret; | |
1846 | transaction *tr; | |
1847 | ||
1848 | CHECK_JOURNAL(jnl); | |
1849 | ||
1850 | if (jnl->flags & JOURNAL_INVALID) { | |
1851 | return EINVAL; | |
1852 | } | |
1853 | ||
91447636 | 1854 | if (jnl->owner == current_thread()) { |
b4c24cb9 | 1855 | if (jnl->active_tr == NULL) { |
91447636 A |
1856 | panic("jnl: start_tr: active_tr is NULL (jnl @ 0x%x, owner 0x%x, current_thread 0x%x\n", |
1857 | jnl, jnl->owner, current_thread()); | |
b4c24cb9 A |
1858 | } |
1859 | jnl->nested_count++; | |
1860 | return 0; | |
1861 | } | |
1862 | ||
91447636 | 1863 | lock_journal(jnl); |
b4c24cb9 A |
1864 | |
1865 | if (jnl->owner != NULL || jnl->nested_count != 0 || jnl->active_tr != NULL) { | |
1866 | panic("jnl: start_tr: owner 0x%x, nested count 0x%x, active_tr 0x%x jnl @ 0x%x\n", | |
1867 | jnl->owner, jnl->nested_count, jnl->active_tr, jnl); | |
1868 | } | |
1869 | ||
91447636 | 1870 | jnl->owner = current_thread(); |
b4c24cb9 A |
1871 | jnl->nested_count = 1; |
1872 | ||
1873 | free_old_stuff(jnl); | |
1874 | ||
1875 | // make sure there's room in the journal | |
1876 | if (check_free_space(jnl, jnl->tbuffer_size) != 0) { | |
1877 | printf("jnl: start transaction failed: no space\n"); | |
1878 | ret = ENOSPC; | |
1879 | goto bad_start; | |
1880 | } | |
1881 | ||
1882 | // if there's a buffered transaction, use it. | |
1883 | if (jnl->cur_tr) { | |
1884 | jnl->active_tr = jnl->cur_tr; | |
1885 | jnl->cur_tr = NULL; | |
1886 | ||
1887 | return 0; | |
1888 | } | |
1889 | ||
d7e50217 | 1890 | MALLOC_ZONE(tr, transaction *, sizeof(transaction), M_JNL_TR, M_WAITOK); |
b4c24cb9 A |
1891 | memset(tr, 0, sizeof(transaction)); |
1892 | ||
1893 | tr->tbuffer_size = jnl->tbuffer_size; | |
91447636 | 1894 | |
b4c24cb9 | 1895 | if (kmem_alloc(kernel_map, (vm_offset_t *)&tr->tbuffer, tr->tbuffer_size)) { |
d7e50217 | 1896 | FREE_ZONE(tr, sizeof(transaction), M_JNL_TR); |
b4c24cb9 A |
1897 | printf("jnl: start transaction failed: no tbuffer mem\n"); |
1898 | ret = ENOMEM; | |
1899 | goto bad_start; | |
1900 | } | |
1901 | ||
1902 | // journal replay code checksum check depends on this. | |
1903 | memset(tr->tbuffer, 0, BLHDR_CHECKSUM_SIZE); | |
8f6c56a5 A |
1904 | // Fill up the rest of the block with unimportant bytes (0x5a 'Z' chosen for visibility) |
1905 | memset(tr->tbuffer + BLHDR_CHECKSUM_SIZE, 0x5a, jnl->jhdr->blhdr_size - BLHDR_CHECKSUM_SIZE); | |
b4c24cb9 A |
1906 | |
1907 | tr->blhdr = (block_list_header *)tr->tbuffer; | |
1908 | tr->blhdr->max_blocks = (jnl->jhdr->blhdr_size / sizeof(block_info)) - 1; | |
1909 | tr->blhdr->num_blocks = 1; // accounts for this header block | |
1910 | tr->blhdr->bytes_used = jnl->jhdr->blhdr_size; | |
1911 | ||
1912 | tr->num_blhdrs = 1; | |
1913 | tr->total_bytes = jnl->jhdr->blhdr_size; | |
1914 | tr->jnl = jnl; | |
1915 | ||
1916 | jnl->active_tr = tr; | |
1917 | ||
1918 | // printf("jnl: start_tr: owner 0x%x new tr @ 0x%x\n", jnl->owner, tr); | |
1919 | ||
1920 | return 0; | |
1921 | ||
1922 | bad_start: | |
1923 | jnl->owner = NULL; | |
1924 | jnl->nested_count = 0; | |
91447636 | 1925 | unlock_journal(jnl); |
b4c24cb9 A |
1926 | return ret; |
1927 | } | |
1928 | ||
1929 | ||
1930 | int | |
1931 | journal_modify_block_start(journal *jnl, struct buf *bp) | |
1932 | { | |
1933 | transaction *tr; | |
1934 | ||
1935 | CHECK_JOURNAL(jnl); | |
1936 | ||
1937 | if (jnl->flags & JOURNAL_INVALID) { | |
1938 | return EINVAL; | |
1939 | } | |
1940 | ||
1941 | // XXXdbg - for debugging I want this to be true. later it may | |
1942 | // not be necessary. | |
91447636 | 1943 | if ((buf_flags(bp) & B_META) == 0) { |
b4c24cb9 A |
1944 | panic("jnl: modify_block_start: bp @ 0x%x is not a meta-data block! (jnl 0x%x)\n", bp, jnl); |
1945 | } | |
1946 | ||
1947 | tr = jnl->active_tr; | |
1948 | CHECK_TRANSACTION(tr); | |
1949 | ||
91447636 | 1950 | if (jnl->owner != current_thread()) { |
b4c24cb9 | 1951 | panic("jnl: modify_block_start: called w/out a transaction! jnl 0x%x, owner 0x%x, curact 0x%x\n", |
91447636 | 1952 | jnl, jnl->owner, current_thread()); |
b4c24cb9 A |
1953 | } |
1954 | ||
1955 | free_old_stuff(jnl); | |
1956 | ||
91447636 A |
1957 | //printf("jnl: mod block start (bp 0x%x vp 0x%x l/blkno %qd/%qd bsz %d; total bytes %d)\n", |
1958 | // bp, buf_vnode(bp), buf_lblkno(bp), buf_blkno(bp), buf_size(bp), tr->total_bytes); | |
b4c24cb9 A |
1959 | |
1960 | // can't allow blocks that aren't an even multiple of the | |
1961 | // underlying block size. | |
91447636 | 1962 | if ((buf_size(bp) % jnl->jhdr->jhdr_size) != 0) { |
b4c24cb9 | 1963 | panic("jnl: mod block start: bufsize %d not a multiple of block size %d\n", |
91447636 | 1964 | buf_size(bp), jnl->jhdr->jhdr_size); |
b4c24cb9 A |
1965 | return -1; |
1966 | } | |
1967 | ||
1968 | // make sure that this transaction isn't bigger than the whole journal | |
91447636 | 1969 | if (tr->total_bytes+buf_size(bp) >= (jnl->jhdr->size - jnl->jhdr->jhdr_size)) { |
b4c24cb9 | 1970 | panic("jnl: transaction too big (%d >= %lld bytes, bufsize %d, tr 0x%x bp 0x%x)\n", |
91447636 | 1971 | tr->total_bytes, (tr->jnl->jhdr->size - jnl->jhdr->jhdr_size), buf_size(bp), tr, bp); |
b4c24cb9 A |
1972 | return -1; |
1973 | } | |
1974 | ||
1975 | // if the block is dirty and not already locked we have to write | |
1976 | // it out before we muck with it because it has data that belongs | |
1977 | // (presumably) to another transaction. | |
1978 | // | |
91447636 | 1979 | if ((buf_flags(bp) & (B_DELWRI | B_LOCKED)) == B_DELWRI) { |
b4c24cb9 | 1980 | |
91447636 A |
1981 | if (buf_flags(bp) & B_ASYNC) { |
1982 | panic("modify_block_start: bp @ 0x% has async flag set!\n", bp); | |
1983 | } | |
b4c24cb9 | 1984 | |
91447636 A |
1985 | // this will cause it to not be buf_brelse()'d |
1986 | buf_setflags(bp, B_NORELSE); | |
1987 | VNOP_BWRITE(bp); | |
1988 | } | |
1989 | buf_setflags(bp, B_LOCKED); | |
b4c24cb9 A |
1990 | |
1991 | return 0; | |
1992 | } | |
1993 | ||
1994 | int | |
1995 | journal_modify_block_abort(journal *jnl, struct buf *bp) | |
1996 | { | |
1997 | transaction *tr; | |
1998 | block_list_header *blhdr; | |
1999 | int i, j; | |
2000 | ||
2001 | CHECK_JOURNAL(jnl); | |
2002 | ||
2003 | tr = jnl->active_tr; | |
2004 | ||
2005 | // | |
2006 | // if there's no active transaction then we just want to | |
91447636 | 2007 | // call buf_brelse() and return since this is just a block |
b4c24cb9 A |
2008 | // that happened to be modified as part of another tr. |
2009 | // | |
2010 | if (tr == NULL) { | |
91447636 | 2011 | buf_brelse(bp); |
b4c24cb9 A |
2012 | return 0; |
2013 | } | |
2014 | ||
2015 | if (jnl->flags & JOURNAL_INVALID) { | |
2016 | return EINVAL; | |
2017 | } | |
2018 | ||
2019 | CHECK_TRANSACTION(tr); | |
2020 | ||
91447636 | 2021 | if (jnl->owner != current_thread()) { |
b4c24cb9 | 2022 | panic("jnl: modify_block_abort: called w/out a transaction! jnl 0x%x, owner 0x%x, curact 0x%x\n", |
91447636 | 2023 | jnl, jnl->owner, current_thread()); |
b4c24cb9 A |
2024 | } |
2025 | ||
2026 | free_old_stuff(jnl); | |
2027 | ||
2028 | // printf("jnl: modify_block_abort: tr 0x%x bp 0x%x\n", jnl->active_tr, bp); | |
2029 | ||
2030 | // first check if it's already part of this transaction | |
2031 | for(blhdr=tr->blhdr; blhdr; blhdr=(block_list_header *)((long)blhdr->binfo[0].bnum)) { | |
2032 | for(i=1; i < blhdr->num_blocks; i++) { | |
2033 | if (bp == blhdr->binfo[i].bp) { | |
91447636 | 2034 | if (buf_size(bp) != blhdr->binfo[i].bsize) { |
b4c24cb9 | 2035 | panic("jnl: bp @ 0x%x changed size on me! (%d vs. %d, jnl 0x%x)\n", |
91447636 | 2036 | bp, buf_size(bp), blhdr->binfo[i].bsize, jnl); |
b4c24cb9 A |
2037 | } |
2038 | break; | |
2039 | } | |
2040 | } | |
2041 | ||
2042 | if (i < blhdr->num_blocks) { | |
2043 | break; | |
2044 | } | |
2045 | } | |
2046 | ||
2047 | // | |
2048 | // if blhdr is null, then this block has only had modify_block_start | |
2049 | // called on it as part of the current transaction. that means that | |
2050 | // it is ok to clear the LOCKED bit since it hasn't actually been | |
2051 | // modified. if blhdr is non-null then modify_block_end was called | |
2052 | // on it and so we need to keep it locked in memory. | |
2053 | // | |
2054 | if (blhdr == NULL) { | |
91447636 | 2055 | buf_clearflags(bp, B_LOCKED); |
b4c24cb9 A |
2056 | } |
2057 | ||
91447636 | 2058 | buf_brelse(bp); |
b4c24cb9 A |
2059 | return 0; |
2060 | } | |
2061 | ||
2062 | ||
2063 | int | |
2064 | journal_modify_block_end(journal *jnl, struct buf *bp) | |
2065 | { | |
2066 | int i, j, tbuffer_offset; | |
2067 | char *blkptr; | |
2068 | block_list_header *blhdr, *prev=NULL; | |
2069 | transaction *tr; | |
2070 | ||
2071 | CHECK_JOURNAL(jnl); | |
2072 | ||
2073 | if (jnl->flags & JOURNAL_INVALID) { | |
2074 | return EINVAL; | |
2075 | } | |
2076 | ||
2077 | tr = jnl->active_tr; | |
2078 | CHECK_TRANSACTION(tr); | |
2079 | ||
91447636 | 2080 | if (jnl->owner != current_thread()) { |
b4c24cb9 | 2081 | panic("jnl: modify_block_end: called w/out a transaction! jnl 0x%x, owner 0x%x, curact 0x%x\n", |
91447636 | 2082 | jnl, jnl->owner, current_thread()); |
b4c24cb9 A |
2083 | } |
2084 | ||
2085 | free_old_stuff(jnl); | |
2086 | ||
91447636 A |
2087 | //printf("jnl: mod block end: (bp 0x%x vp 0x%x l/blkno %qd/%qd bsz %d, total bytes %d)\n", |
2088 | // bp, buf_vnode(bp), buf_lblkno(bp), buf_blkno(bp), buf_size(bp), tr->total_bytes); | |
b4c24cb9 | 2089 | |
91447636 | 2090 | if ((buf_flags(bp) & B_LOCKED) == 0) { |
b4c24cb9 | 2091 | panic("jnl: modify_block_end: bp 0x%x not locked! jnl @ 0x%x\n", bp, jnl); |
b4c24cb9 A |
2092 | } |
2093 | ||
2094 | // first check if it's already part of this transaction | |
2095 | for(blhdr=tr->blhdr; blhdr; prev=blhdr,blhdr=(block_list_header *)((long)blhdr->binfo[0].bnum)) { | |
2096 | tbuffer_offset = jnl->jhdr->blhdr_size; | |
2097 | ||
2098 | for(i=1; i < blhdr->num_blocks; i++) { | |
2099 | if (bp == blhdr->binfo[i].bp) { | |
91447636 | 2100 | if (buf_size(bp) != blhdr->binfo[i].bsize) { |
b4c24cb9 | 2101 | panic("jnl: bp @ 0x%x changed size on me! (%d vs. %d, jnl 0x%x)\n", |
91447636 | 2102 | bp, buf_size(bp), blhdr->binfo[i].bsize, jnl); |
b4c24cb9 A |
2103 | } |
2104 | break; | |
2105 | } | |
2106 | tbuffer_offset += blhdr->binfo[i].bsize; | |
2107 | } | |
2108 | ||
2109 | if (i < blhdr->num_blocks) { | |
2110 | break; | |
2111 | } | |
2112 | } | |
2113 | ||
2114 | if (blhdr == NULL | |
2115 | && prev | |
2116 | && (prev->num_blocks+1) <= prev->max_blocks | |
91447636 | 2117 | && (prev->bytes_used+buf_size(bp)) <= tr->tbuffer_size) { |
b4c24cb9 A |
2118 | blhdr = prev; |
2119 | } else if (blhdr == NULL) { | |
2120 | block_list_header *nblhdr; | |
2121 | ||
2122 | if (prev == NULL) { | |
2123 | panic("jnl: modify block end: no way man, prev == NULL?!?, jnl 0x%x, bp 0x%x\n", jnl, bp); | |
2124 | } | |
2125 | ||
2126 | // we got to the end of the list, didn't find the block and there's | |
2127 | // no room in the block_list_header pointed to by prev | |
2128 | ||
2129 | // we allocate another tbuffer and link it in at the end of the list | |
2130 | // through prev->binfo[0].bnum. that's a skanky way to do things but | |
2131 | // avoids having yet another linked list of small data structures to manage. | |
2132 | ||
2133 | if (kmem_alloc(kernel_map, (vm_offset_t *)&nblhdr, tr->tbuffer_size)) { | |
2134 | panic("jnl: end_tr: no space for new block tr @ 0x%x (total bytes: %d)!\n", | |
2135 | tr, tr->total_bytes); | |
2136 | } | |
2137 | ||
2138 | // journal replay code checksum check depends on this. | |
2139 | memset(nblhdr, 0, BLHDR_CHECKSUM_SIZE); | |
8f6c56a5 A |
2140 | // Fill up the rest of the block with unimportant bytes |
2141 | memset(nblhdr + BLHDR_CHECKSUM_SIZE, 0x5a, jnl->jhdr->blhdr_size - BLHDR_CHECKSUM_SIZE); | |
b4c24cb9 A |
2142 | |
2143 | // initialize the new guy | |
2144 | nblhdr->max_blocks = (jnl->jhdr->blhdr_size / sizeof(block_info)) - 1; | |
2145 | nblhdr->num_blocks = 1; // accounts for this header block | |
2146 | nblhdr->bytes_used = jnl->jhdr->blhdr_size; | |
2147 | ||
2148 | tr->num_blhdrs++; | |
2149 | tr->total_bytes += jnl->jhdr->blhdr_size; | |
2150 | ||
2151 | // then link him in at the end | |
2152 | prev->binfo[0].bnum = (off_t)((long)nblhdr); | |
2153 | ||
2154 | // and finally switch to using the new guy | |
2155 | blhdr = nblhdr; | |
2156 | tbuffer_offset = jnl->jhdr->blhdr_size; | |
2157 | i = 1; | |
2158 | } | |
2159 | ||
2160 | ||
2161 | if ((i+1) > blhdr->max_blocks) { | |
2162 | panic("jnl: modify_block_end: i = %d, max_blocks %d\n", i, blhdr->max_blocks); | |
2163 | } | |
2164 | ||
2165 | // copy the data into the in-memory transaction buffer | |
2166 | blkptr = (char *)&((char *)blhdr)[tbuffer_offset]; | |
91447636 | 2167 | memcpy(blkptr, buf_dataptr(bp), buf_size(bp)); |
b4c24cb9 A |
2168 | |
2169 | // if this is true then this is a new block we haven't seen | |
2170 | if (i >= blhdr->num_blocks) { | |
91447636 A |
2171 | int bsize; |
2172 | vnode_t vp; | |
2173 | ||
2174 | vp = buf_vnode(bp); | |
2175 | vnode_ref(vp); | |
2176 | bsize = buf_size(bp); | |
b4c24cb9 | 2177 | |
91447636 A |
2178 | blhdr->binfo[i].bnum = (off_t)(buf_blkno(bp)); |
2179 | blhdr->binfo[i].bsize = bsize; | |
b4c24cb9 A |
2180 | blhdr->binfo[i].bp = bp; |
2181 | ||
91447636 A |
2182 | blhdr->bytes_used += bsize; |
2183 | tr->total_bytes += bsize; | |
b4c24cb9 A |
2184 | |
2185 | blhdr->num_blocks++; | |
2186 | } | |
91447636 | 2187 | buf_bdwrite(bp); |
b4c24cb9 A |
2188 | |
2189 | return 0; | |
2190 | } | |
2191 | ||
2192 | int | |
2193 | journal_kill_block(journal *jnl, struct buf *bp) | |
2194 | { | |
2195 | int i; | |
91447636 | 2196 | int bflags; |
b4c24cb9 A |
2197 | block_list_header *blhdr; |
2198 | transaction *tr; | |
2199 | ||
2200 | CHECK_JOURNAL(jnl); | |
2201 | ||
2202 | if (jnl->flags & JOURNAL_INVALID) { | |
2203 | return EINVAL; | |
2204 | } | |
2205 | ||
2206 | tr = jnl->active_tr; | |
2207 | CHECK_TRANSACTION(tr); | |
2208 | ||
91447636 | 2209 | if (jnl->owner != current_thread()) { |
b4c24cb9 | 2210 | panic("jnl: modify_block_end: called w/out a transaction! jnl 0x%x, owner 0x%x, curact 0x%x\n", |
91447636 | 2211 | jnl, jnl->owner, current_thread()); |
b4c24cb9 A |
2212 | } |
2213 | ||
2214 | free_old_stuff(jnl); | |
2215 | ||
91447636 A |
2216 | bflags = buf_flags(bp); |
2217 | ||
2218 | if ( !(bflags & B_LOCKED)) | |
2219 | panic("jnl: modify_block_end: called with bp not B_LOCKED"); | |
b4c24cb9 | 2220 | |
91447636 A |
2221 | /* |
2222 | * bp must be BL_BUSY and B_LOCKED | |
2223 | */ | |
b4c24cb9 A |
2224 | // first check if it's already part of this transaction |
2225 | for(blhdr=tr->blhdr; blhdr; blhdr=(block_list_header *)((long)blhdr->binfo[0].bnum)) { | |
2226 | ||
2227 | for(i=1; i < blhdr->num_blocks; i++) { | |
2228 | if (bp == blhdr->binfo[i].bp) { | |
91447636 | 2229 | vnode_t vp; |
b4c24cb9 | 2230 | |
91447636 | 2231 | buf_clearflags(bp, B_LOCKED); |
b4c24cb9 | 2232 | |
91447636 A |
2233 | // this undoes the vnode_ref() in journal_modify_block_end() |
2234 | vp = buf_vnode(bp); | |
2235 | vnode_rele_ext(vp, 0, 1); | |
2236 | ||
2237 | // if the block has the DELWRI and FILTER bits sets, then | |
b4c24cb9 A |
2238 | // things are seriously weird. if it was part of another |
2239 | // transaction then journal_modify_block_start() should | |
2240 | // have force it to be written. | |
2241 | // | |
91447636 A |
2242 | //if ((bflags & B_DELWRI) && (bflags & B_FILTER)) { |
2243 | // panic("jnl: kill block: this defies all logic! bp 0x%x\n", bp); | |
2244 | //} else { | |
2245 | tr->num_killed += buf_size(bp); | |
2246 | //} | |
b4c24cb9 A |
2247 | blhdr->binfo[i].bp = NULL; |
2248 | blhdr->binfo[i].bnum = (off_t)-1; | |
91447636 A |
2249 | |
2250 | buf_brelse(bp); | |
2251 | ||
b4c24cb9 A |
2252 | break; |
2253 | } | |
2254 | } | |
2255 | ||
2256 | if (i < blhdr->num_blocks) { | |
2257 | break; | |
2258 | } | |
2259 | } | |
2260 | ||
2261 | return 0; | |
2262 | } | |
2263 | ||
2264 | ||
2265 | static int | |
2266 | journal_binfo_cmp(void *a, void *b) | |
2267 | { | |
91447636 A |
2268 | block_info *bi_a = (struct block_info *)a; |
2269 | block_info *bi_b = (struct block_info *)b; | |
2270 | daddr64_t res; | |
b4c24cb9 A |
2271 | |
2272 | if (bi_a->bp == NULL) { | |
2273 | return 1; | |
2274 | } | |
2275 | if (bi_b->bp == NULL) { | |
2276 | return -1; | |
2277 | } | |
2278 | ||
2279 | // don't have to worry about negative block | |
2280 | // numbers so this is ok to do. | |
2281 | // | |
91447636 | 2282 | res = (buf_blkno(bi_a->bp) - buf_blkno(bi_b->bp)); |
b4c24cb9 A |
2283 | |
2284 | return (int)res; | |
2285 | } | |
2286 | ||
2287 | ||
2288 | static int | |
2289 | end_transaction(transaction *tr, int force_it) | |
2290 | { | |
2291 | int i, j, ret, amt; | |
91447636 | 2292 | errno_t errno; |
b4c24cb9 A |
2293 | off_t end; |
2294 | journal *jnl = tr->jnl; | |
2295 | struct buf *bp; | |
2296 | block_list_header *blhdr=NULL, *next=NULL; | |
2297 | ||
2298 | if (jnl->cur_tr) { | |
2299 | panic("jnl: jnl @ 0x%x already has cur_tr 0x%x, new tr: 0x%x\n", | |
2300 | jnl, jnl->cur_tr, tr); | |
2301 | } | |
2302 | ||
2303 | // if there weren't any modified blocks in the transaction | |
2304 | // just save off the transaction pointer and return. | |
2305 | if (tr->total_bytes == jnl->jhdr->blhdr_size) { | |
2306 | jnl->cur_tr = tr; | |
91447636 | 2307 | return 0; |
b4c24cb9 A |
2308 | } |
2309 | ||
2310 | // if our transaction buffer isn't very full, just hang | |
2311 | // on to it and don't actually flush anything. this is | |
2312 | // what is known as "group commit". we will flush the | |
2313 | // transaction buffer if it's full or if we have more than | |
2314 | // one of them so we don't start hogging too much memory. | |
2315 | // | |
2316 | if ( force_it == 0 | |
2317 | && (jnl->flags & JOURNAL_NO_GROUP_COMMIT) == 0 | |
2318 | && tr->num_blhdrs < 3 | |
2319 | && (tr->total_bytes <= ((tr->tbuffer_size*tr->num_blhdrs) - tr->tbuffer_size/8))) { | |
2320 | ||
2321 | jnl->cur_tr = tr; | |
91447636 | 2322 | return 0; |
b4c24cb9 A |
2323 | } |
2324 | ||
2325 | ||
2326 | // if we're here we're going to flush the transaction buffer to disk. | |
2327 | // make sure there is room in the journal first. | |
2328 | check_free_space(jnl, tr->total_bytes); | |
2329 | ||
2330 | // range check the end index | |
2331 | if (jnl->jhdr->end <= 0 || jnl->jhdr->end > jnl->jhdr->size) { | |
2332 | panic("jnl: end_transaction: end is bogus 0x%llx (sz 0x%llx)\n", | |
2333 | jnl->jhdr->end, jnl->jhdr->size); | |
2334 | } | |
2335 | ||
2336 | // this transaction starts where the current journal ends | |
2337 | tr->journal_start = jnl->jhdr->end; | |
2338 | end = jnl->jhdr->end; | |
2339 | ||
2340 | // | |
2341 | // if the first entry in old_start[] isn't free yet, loop calling the | |
2342 | // file system flush routine until it is (or we panic). | |
2343 | // | |
2344 | i = 0; | |
91447636 | 2345 | lock_oldstart(jnl); |
b4c24cb9 A |
2346 | while ((jnl->old_start[0] & 0x8000000000000000LL) != 0) { |
2347 | if (jnl->flush) { | |
91447636 | 2348 | unlock_oldstart(jnl); |
b4c24cb9 A |
2349 | |
2350 | if (jnl->flush) { | |
2351 | jnl->flush(jnl->flush_arg); | |
2352 | } | |
2353 | ||
2354 | // yield the cpu so others can get in to clear the lock bit | |
2355 | (void)tsleep((void *)jnl, PRIBIO, "jnl-old-start-sleep", 1); | |
2356 | ||
91447636 | 2357 | lock_oldstart(jnl); |
b4c24cb9 | 2358 | } |
91447636 | 2359 | if (i++ >= 500) { |
b4c24cb9 A |
2360 | panic("jnl: transaction that started at 0x%llx is not completing! jnl 0x%x\n", |
2361 | jnl->old_start[0] & (~0x8000000000000000LL), jnl); | |
2362 | } | |
2363 | } | |
2364 | ||
2365 | // | |
2366 | // slide everyone else down and put our latest guy in the last | |
2367 | // entry in the old_start array | |
2368 | // | |
2369 | memcpy(&jnl->old_start[0], &jnl->old_start[1], sizeof(jnl->old_start)-sizeof(jnl->old_start[0])); | |
2370 | jnl->old_start[sizeof(jnl->old_start)/sizeof(jnl->old_start[0]) - 1] = tr->journal_start | 0x8000000000000000LL; | |
2371 | ||
91447636 | 2372 | unlock_oldstart(jnl); |
b4c24cb9 A |
2373 | |
2374 | ||
2375 | // for each block, make sure that the physical block # is set | |
2376 | for(blhdr=tr->blhdr; blhdr; blhdr=next) { | |
2377 | ||
2378 | for(i=1; i < blhdr->num_blocks; i++) { | |
91447636 A |
2379 | daddr64_t blkno; |
2380 | daddr64_t lblkno; | |
2381 | struct vnode *vp; | |
2382 | ||
b4c24cb9 A |
2383 | bp = blhdr->binfo[i].bp; |
2384 | if (bp == NULL) { // only true if a block was "killed" | |
2385 | if (blhdr->binfo[i].bnum != (off_t)-1) { | |
2386 | panic("jnl: inconsistent binfo (NULL bp w/bnum %lld; jnl @ 0x%x, tr 0x%x)\n", | |
2387 | blhdr->binfo[i].bnum, jnl, tr); | |
2388 | } | |
2389 | continue; | |
2390 | } | |
91447636 A |
2391 | vp = buf_vnode(bp); |
2392 | blkno = buf_blkno(bp); | |
2393 | lblkno = buf_lblkno(bp); | |
2394 | ||
2395 | if (vp == NULL && lblkno == blkno) { | |
2396 | printf("jnl: end_tr: bad news! bp @ 0x%x w/null vp and l/blkno = %qd/%qd. aborting the transaction (tr 0x%x jnl 0x%x).\n", | |
2397 | bp, lblkno, blkno, tr, jnl); | |
2398 | goto bad_journal; | |
b4c24cb9 A |
2399 | } |
2400 | ||
2401 | // if the lblkno is the same as blkno and this bp isn't | |
2402 | // associated with the underlying file system device then | |
2403 | // we need to call bmap() to get the actual physical block. | |
2404 | // | |
91447636 A |
2405 | if ((lblkno == blkno) && (vp != jnl->fsdev)) { |
2406 | off_t f_offset; | |
2407 | size_t contig_bytes; | |
2408 | ||
2409 | if (VNOP_BLKTOOFF(vp, lblkno, &f_offset)) { | |
2410 | printf("jnl: end_tr: vnop_blktooff failed @ 0x%x, jnl 0x%x\n", bp, jnl); | |
2411 | goto bad_journal; | |
2412 | } | |
2413 | if (VNOP_BLOCKMAP(vp, f_offset, buf_count(bp), &blkno, &contig_bytes, NULL, 0, NULL)) { | |
2414 | printf("jnl: end_tr: can't blockmap the bp @ 0x%x, jnl 0x%x\n", bp, jnl); | |
b4c24cb9 A |
2415 | goto bad_journal; |
2416 | } | |
91447636 A |
2417 | if ((uint32_t)contig_bytes < buf_count(bp)) { |
2418 | printf("jnl: end_tr: blk not physically contiguous on disk@ 0x%x, jnl 0x%x\n", bp, jnl); | |
2419 | goto bad_journal; | |
2420 | } | |
2421 | buf_setblkno(bp, blkno); | |
b4c24cb9 | 2422 | } |
b4c24cb9 | 2423 | // update this so we write out the correct physical block number! |
91447636 | 2424 | blhdr->binfo[i].bnum = (off_t)(blkno); |
b4c24cb9 A |
2425 | } |
2426 | ||
2427 | next = (block_list_header *)((long)blhdr->binfo[0].bnum); | |
2428 | } | |
2429 | ||
2430 | for(blhdr=tr->blhdr; blhdr; blhdr=(block_list_header *)((long)blhdr->binfo[0].bnum)) { | |
2431 | ||
2432 | amt = blhdr->bytes_used; | |
2433 | ||
2434 | blhdr->checksum = 0; | |
2435 | blhdr->checksum = calc_checksum((char *)blhdr, BLHDR_CHECKSUM_SIZE); | |
2436 | ||
2437 | ret = write_journal_data(jnl, &end, blhdr, amt); | |
2438 | if (ret != amt) { | |
2439 | printf("jnl: end_transaction: only wrote %d of %d bytes to the journal!\n", | |
2440 | ret, amt); | |
2441 | ||
2442 | goto bad_journal; | |
2443 | } | |
2444 | } | |
2445 | ||
2446 | jnl->jhdr->end = end; // update where the journal now ends | |
2447 | tr->journal_end = end; // the transaction ends here too | |
2448 | if (tr->journal_start == 0 || tr->journal_end == 0) { | |
2449 | panic("jnl: end_transaction: bad tr journal start/end: 0x%llx 0x%llx\n", | |
2450 | tr->journal_start, tr->journal_end); | |
2451 | } | |
2452 | ||
2453 | if (write_journal_header(jnl) != 0) { | |
2454 | goto bad_journal; | |
2455 | } | |
2456 | ||
2457 | // | |
2458 | // setup for looping through all the blhdr's. we null out the | |
2459 | // tbuffer and blhdr fields so that they're not used any more. | |
2460 | // | |
2461 | blhdr = tr->blhdr; | |
2462 | tr->tbuffer = NULL; | |
2463 | tr->blhdr = NULL; | |
2464 | ||
2465 | // the buffer_flushed_callback will only be called for the | |
2466 | // real blocks that get flushed so we have to account for | |
2467 | // the block_list_headers here. | |
2468 | // | |
2469 | tr->num_flushed = tr->num_blhdrs * jnl->jhdr->blhdr_size; | |
2470 | ||
2471 | // for each block, set the iodone callback and unlock it | |
2472 | for(; blhdr; blhdr=next) { | |
2473 | ||
2474 | // we can re-order the buf ptrs because everything is written out already | |
2475 | qsort(&blhdr->binfo[1], blhdr->num_blocks-1, sizeof(block_info), journal_binfo_cmp); | |
2476 | ||
2477 | for(i=1; i < blhdr->num_blocks; i++) { | |
2478 | if (blhdr->binfo[i].bp == NULL) { | |
2479 | continue; | |
2480 | } | |
2481 | ||
91447636 A |
2482 | errno = buf_meta_bread(buf_vnode(blhdr->binfo[i].bp), |
2483 | buf_lblkno(blhdr->binfo[i].bp), | |
2484 | buf_size(blhdr->binfo[i].bp), | |
b4c24cb9 A |
2485 | NOCRED, |
2486 | &bp); | |
91447636 | 2487 | if (errno == 0 && bp != NULL) { |
b4c24cb9 | 2488 | struct vnode *save_vp; |
91447636 A |
2489 | void *cur_filter; |
2490 | ||
b4c24cb9 A |
2491 | if (bp != blhdr->binfo[i].bp) { |
2492 | panic("jnl: end_tr: got back a different bp! (bp 0x%x should be 0x%x, jnl 0x%x\n", | |
2493 | bp, blhdr->binfo[i].bp, jnl); | |
2494 | } | |
2495 | ||
91447636 | 2496 | if ((buf_flags(bp) & (B_LOCKED|B_DELWRI)) != (B_LOCKED|B_DELWRI)) { |
b4c24cb9 | 2497 | if (jnl->flags & JOURNAL_CLOSE_PENDING) { |
91447636 A |
2498 | buf_clearflags(bp, B_LOCKED); |
2499 | buf_brelse(bp); | |
b4c24cb9 A |
2500 | continue; |
2501 | } else { | |
91447636 | 2502 | panic("jnl: end_tr: !!!DANGER!!! bp 0x%x flags (0x%x) not LOCKED & DELWRI\n", bp, buf_flags(bp)); |
b4c24cb9 A |
2503 | } |
2504 | } | |
91447636 | 2505 | save_vp = buf_vnode(bp); |
b4c24cb9 | 2506 | |
91447636 | 2507 | buf_setfilter(bp, buffer_flushed_callback, tr, &cur_filter, NULL); |
b4c24cb9 | 2508 | |
91447636 A |
2509 | if (cur_filter) { |
2510 | panic("jnl: bp @ 0x%x (blkno %qd, vp 0x%x) has non-null iodone (0x%x) buffflushcb 0x%x\n", | |
2511 | bp, buf_blkno(bp), save_vp, cur_filter, buffer_flushed_callback); | |
2512 | } | |
2513 | buf_clearflags(bp, B_LOCKED); | |
b4c24cb9 A |
2514 | |
2515 | // kicking off the write here helps performance | |
91447636 A |
2516 | buf_bawrite(bp); |
2517 | // XXXdbg this is good for testing: buf_bdwrite(bp); | |
2518 | //buf_bdwrite(bp); | |
b4c24cb9 | 2519 | |
91447636 A |
2520 | // this undoes the vnode_ref() in journal_modify_block_end() |
2521 | vnode_rele_ext(save_vp, 0, 1); | |
b4c24cb9 A |
2522 | } else { |
2523 | printf("jnl: end_transaction: could not find block %Ld vp 0x%x!\n", | |
2524 | blhdr->binfo[i].bnum, blhdr->binfo[i].bp); | |
d7e50217 | 2525 | if (bp) { |
91447636 A |
2526 | buf_clearflags(bp, B_LOCKED); |
2527 | buf_brelse(bp); | |
d7e50217 | 2528 | } |
b4c24cb9 A |
2529 | } |
2530 | } | |
2531 | ||
2532 | next = (block_list_header *)((long)blhdr->binfo[0].bnum); | |
2533 | ||
2534 | // we can free blhdr here since we won't need it any more | |
2535 | blhdr->binfo[0].bnum = 0xdeadc0de; | |
2536 | kmem_free(kernel_map, (vm_offset_t)blhdr, tr->tbuffer_size); | |
2537 | } | |
2538 | ||
2539 | //printf("jnl: end_tr: tr @ 0x%x, jnl-blocks: 0x%llx - 0x%llx. exit!\n", | |
2540 | // tr, tr->journal_start, tr->journal_end); | |
2541 | return 0; | |
2542 | ||
2543 | ||
2544 | bad_journal: | |
2545 | jnl->flags |= JOURNAL_INVALID; | |
91447636 | 2546 | jnl->old_start[sizeof(jnl->old_start)/sizeof(jnl->old_start[0]) - 1] &= ~0x8000000000000000LL; |
b4c24cb9 A |
2547 | abort_transaction(jnl, tr); |
2548 | return -1; | |
2549 | } | |
2550 | ||
2551 | static void | |
2552 | abort_transaction(journal *jnl, transaction *tr) | |
2553 | { | |
91447636 A |
2554 | int i; |
2555 | errno_t errno; | |
b4c24cb9 A |
2556 | block_list_header *blhdr, *next; |
2557 | struct buf *bp; | |
55e303ae | 2558 | struct vnode *save_vp; |
b4c24cb9 A |
2559 | |
2560 | // for each block list header, iterate over the blocks then | |
2561 | // free up the memory associated with the block list. | |
2562 | // | |
2563 | // for each block, clear the lock bit and release it. | |
2564 | // | |
2565 | for(blhdr=tr->blhdr; blhdr; blhdr=next) { | |
2566 | ||
2567 | for(i=1; i < blhdr->num_blocks; i++) { | |
2568 | if (blhdr->binfo[i].bp == NULL) { | |
2569 | continue; | |
2570 | } | |
91447636 A |
2571 | if ( (buf_vnode(blhdr->binfo[i].bp) == NULL) || |
2572 | !(buf_flags(blhdr->binfo[i].bp) & B_LOCKED) ) { | |
2573 | continue; | |
2574 | } | |
2575 | ||
2576 | errno = buf_meta_bread(buf_vnode(blhdr->binfo[i].bp), | |
2577 | buf_lblkno(blhdr->binfo[i].bp), | |
2578 | buf_size(blhdr->binfo[i].bp), | |
b4c24cb9 A |
2579 | NOCRED, |
2580 | &bp); | |
91447636 | 2581 | if (errno == 0) { |
b4c24cb9 A |
2582 | if (bp != blhdr->binfo[i].bp) { |
2583 | panic("jnl: abort_tr: got back a different bp! (bp 0x%x should be 0x%x, jnl 0x%x\n", | |
2584 | bp, blhdr->binfo[i].bp, jnl); | |
2585 | } | |
2586 | ||
91447636 A |
2587 | // releasing a bp marked invalid |
2588 | // also clears the locked and delayed state | |
2589 | buf_markinvalid(bp); | |
2590 | save_vp = buf_vnode(bp); | |
b4c24cb9 | 2591 | |
91447636 | 2592 | buf_brelse(bp); |
55e303ae | 2593 | |
91447636 | 2594 | vnode_rele_ext(save_vp, 0, 1); |
b4c24cb9 A |
2595 | } else { |
2596 | printf("jnl: abort_tr: could not find block %Ld vp 0x%x!\n", | |
2597 | blhdr->binfo[i].bnum, blhdr->binfo[i].bp); | |
d7e50217 | 2598 | if (bp) { |
91447636 | 2599 | buf_brelse(bp); |
d7e50217 | 2600 | } |
b4c24cb9 A |
2601 | } |
2602 | } | |
2603 | ||
2604 | next = (block_list_header *)((long)blhdr->binfo[0].bnum); | |
2605 | ||
2606 | // we can free blhdr here since we won't need it any more | |
2607 | blhdr->binfo[0].bnum = 0xdeadc0de; | |
2608 | kmem_free(kernel_map, (vm_offset_t)blhdr, tr->tbuffer_size); | |
2609 | } | |
2610 | ||
2611 | tr->tbuffer = NULL; | |
2612 | tr->blhdr = NULL; | |
2613 | tr->total_bytes = 0xdbadc0de; | |
d7e50217 | 2614 | FREE_ZONE(tr, sizeof(transaction), M_JNL_TR); |
b4c24cb9 A |
2615 | } |
2616 | ||
2617 | ||
2618 | int | |
2619 | journal_end_transaction(journal *jnl) | |
2620 | { | |
2621 | int ret; | |
91447636 | 2622 | transaction *tr; |
b4c24cb9 A |
2623 | |
2624 | CHECK_JOURNAL(jnl); | |
2625 | ||
2626 | if ((jnl->flags & JOURNAL_INVALID) && jnl->owner == NULL) { | |
2627 | return 0; | |
2628 | } | |
2629 | ||
91447636 | 2630 | if (jnl->owner != current_thread()) { |
b4c24cb9 | 2631 | panic("jnl: end_tr: I'm not the owner! jnl 0x%x, owner 0x%x, curact 0x%x\n", |
91447636 | 2632 | jnl, jnl->owner, current_thread()); |
b4c24cb9 A |
2633 | } |
2634 | ||
2635 | free_old_stuff(jnl); | |
2636 | ||
2637 | jnl->nested_count--; | |
2638 | if (jnl->nested_count > 0) { | |
2639 | return 0; | |
2640 | } else if (jnl->nested_count < 0) { | |
2641 | panic("jnl: jnl @ 0x%x has negative nested count (%d). bad boy.\n", jnl, jnl->nested_count); | |
2642 | } | |
2643 | ||
2644 | if (jnl->flags & JOURNAL_INVALID) { | |
2645 | if (jnl->active_tr) { | |
b4c24cb9 A |
2646 | if (jnl->cur_tr != NULL) { |
2647 | panic("jnl: journal @ 0x%x has active tr (0x%x) and cur tr (0x%x)\n", | |
2648 | jnl, jnl->active_tr, jnl->cur_tr); | |
2649 | } | |
2650 | ||
2651 | tr = jnl->active_tr; | |
2652 | jnl->active_tr = NULL; | |
2653 | abort_transaction(jnl, tr); | |
2654 | } | |
2655 | ||
2656 | jnl->owner = NULL; | |
91447636 | 2657 | unlock_journal(jnl); |
b4c24cb9 A |
2658 | |
2659 | return EINVAL; | |
2660 | } | |
2661 | ||
2662 | tr = jnl->active_tr; | |
2663 | CHECK_TRANSACTION(tr); | |
2664 | ||
2665 | // clear this out here so that when check_free_space() calls | |
2666 | // the FS flush function, we don't panic in journal_flush() | |
2667 | // if the FS were to call that. note: check_free_space() is | |
2668 | // called from end_transaction(). | |
2669 | // | |
2670 | jnl->active_tr = NULL; | |
2671 | ret = end_transaction(tr, 0); | |
2672 | ||
2673 | jnl->owner = NULL; | |
91447636 | 2674 | unlock_journal(jnl); |
b4c24cb9 A |
2675 | |
2676 | return ret; | |
2677 | } | |
2678 | ||
2679 | ||
2680 | int | |
2681 | journal_flush(journal *jnl) | |
2682 | { | |
2683 | int need_signal = 0; | |
2684 | ||
2685 | CHECK_JOURNAL(jnl); | |
2686 | ||
2687 | if (jnl->flags & JOURNAL_INVALID) { | |
2688 | return -1; | |
2689 | } | |
2690 | ||
91447636 | 2691 | if (jnl->owner != current_thread()) { |
b4c24cb9 A |
2692 | int ret; |
2693 | ||
91447636 | 2694 | lock_journal(jnl); |
b4c24cb9 A |
2695 | need_signal = 1; |
2696 | } | |
2697 | ||
2698 | free_old_stuff(jnl); | |
2699 | ||
2700 | // if we're not active, flush any buffered transactions | |
2701 | if (jnl->active_tr == NULL && jnl->cur_tr) { | |
2702 | transaction *tr = jnl->cur_tr; | |
2703 | ||
2704 | jnl->cur_tr = NULL; | |
2705 | end_transaction(tr, 1); // force it to get flushed | |
2706 | } | |
2707 | ||
2708 | if (need_signal) { | |
91447636 | 2709 | unlock_journal(jnl); |
b4c24cb9 A |
2710 | } |
2711 | ||
2712 | return 0; | |
2713 | } | |
2714 | ||
2715 | int | |
2716 | journal_active(journal *jnl) | |
2717 | { | |
2718 | if (jnl->flags & JOURNAL_INVALID) { | |
2719 | return -1; | |
2720 | } | |
2721 | ||
2722 | return (jnl->active_tr == NULL) ? 0 : 1; | |
2723 | } | |
91447636 A |
2724 | |
2725 | void * | |
2726 | journal_owner(journal *jnl) | |
2727 | { | |
2728 | return jnl->owner; | |
2729 | } |