2 * Copyright (c) 1995-2008 Apple Inc. All rights reserved.
4 * @APPLE_OSREFERENCE_LICENSE_HEADER_START@
6 * This file contains Original Code and/or Modifications of Original Code
7 * as defined in and that are subject to the Apple Public Source License
8 * Version 2.0 (the 'License'). You may not use this file except in
9 * compliance with the License. The rights granted to you under the License
10 * may not be used to create, or enable the creation or redistribution of,
11 * unlawful or unlicensed copies of an Apple operating system, or to
12 * circumvent, violate, or enable the circumvention or violation of, any
13 * terms of an Apple operating system software license agreement.
15 * Please obtain a copy of the License at
16 * http://www.opensource.apple.com/apsl/ and read it before using this file.
18 * The Original Code and all software distributed under the License are
19 * distributed on an 'AS IS' basis, WITHOUT WARRANTY OF ANY KIND, EITHER
20 * EXPRESS OR IMPLIED, AND APPLE HEREBY DISCLAIMS ALL SUCH WARRANTIES,
21 * INCLUDING WITHOUT LIMITATION, ANY WARRANTIES OF MERCHANTABILITY,
22 * FITNESS FOR A PARTICULAR PURPOSE, QUIET ENJOYMENT OR NON-INFRINGEMENT.
23 * Please see the License for the specific language governing rights and
24 * limitations under the License.
26 * @APPLE_OSREFERENCE_LICENSE_HEADER_END@
29 // This file implements a simple write-ahead journaling layer.
30 // In theory any file system can make use of it by calling these
31 // functions when the fs wants to modify meta-data blocks. See
32 // vfs_journal.h for a more detailed description of the api and
35 // Dominic Giampaolo (dbg@apple.com)
40 #include <sys/param.h>
41 #include <sys/systm.h>
42 #include <sys/kernel.h>
43 #include <sys/file_internal.h>
45 #include <sys/buf_internal.h>
46 #include <sys/proc_internal.h>
47 #include <sys/mount_internal.h>
48 #include <sys/namei.h>
49 #include <sys/vnode_internal.h>
50 #include <sys/ioctl.h>
53 #include <sys/malloc.h>
54 #include <kern/thread.h>
56 #include <sys/kdebug.h>
57 #include <miscfs/specfs/specdev.h>
58 #include <libkern/OSAtomic.h> /* OSAddAtomic */
60 extern task_t kernel_task
;
62 #define DBG_JOURNAL_FLUSH 1
64 #include <sys/sdt.h> /* DTRACE_IO1 */
75 #include <sys/types.h>
80 #include "vfs_journal.h"
84 /* XXX next prototytype should be from libsa/stdlib.h> but conflicts libkern */
85 __private_extern__
void qsort(
89 int (*)(const void *, const void *));
93 // number of bytes to checksum in a block_list_header
94 // NOTE: this should be enough to clear out the header
95 // fields as well as the first entry of binfo[]
96 #define BLHDR_CHECKSUM_SIZE 32
99 static int end_transaction(transaction
*tr
, int force_it
, errno_t (*callback
)(void*), void *callback_arg
);
100 static void abort_transaction(journal
*jnl
, transaction
*tr
);
101 static void dump_journal(journal
*jnl
);
103 static __inline__
void lock_journal(journal
*jnl
);
104 static __inline__
void unlock_journal(journal
*jnl
);
105 static __inline__
void lock_oldstart(journal
*jnl
);
106 static __inline__
void unlock_oldstart(journal
*jnl
);
112 // 3105942 - Coalesce writes to the same block on journal replay
115 typedef struct bucket
{
122 #define STARTING_BUCKETS 256
124 static int add_block(journal
*jnl
, struct bucket
**buf_ptr
, off_t block_num
, size_t size
, size_t offset
, int32_t cksum
, int *num_buckets_ptr
, int *num_full_ptr
);
125 static int grow_table(struct bucket
**buf_ptr
, int num_buckets
, int new_size
);
126 static int lookup_bucket(struct bucket
**buf_ptr
, off_t block_num
, int num_full
);
127 static int do_overlap(journal
*jnl
, struct bucket
**buf_ptr
, int blk_index
, off_t block_num
, size_t size
, size_t offset
, int32_t cksum
, int *num_buckets_ptr
, int *num_full_ptr
);
128 static int insert_block(journal
*jnl
, struct bucket
**buf_ptr
, int blk_index
, off_t num
, size_t size
, size_t offset
, int32_t cksum
, int *num_buckets_ptr
, int *num_full_ptr
, int overwriting
);
130 #define CHECK_JOURNAL(jnl) \
133 panic("%s:%d: null journal ptr?\n", __FILE__, __LINE__);\
135 if (jnl->jdev == NULL) { \
136 panic("%s:%d: jdev is null!\n", __FILE__, __LINE__);\
138 if (jnl->fsdev == NULL) { \
139 panic("%s:%d: fsdev is null!\n", __FILE__, __LINE__);\
141 if (jnl->jhdr->magic != JOURNAL_HEADER_MAGIC) {\
142 panic("%s:%d: jhdr magic corrupted (0x%x != 0x%x)\n",\
143 __FILE__, __LINE__, jnl->jhdr->magic, JOURNAL_HEADER_MAGIC);\
145 if ( jnl->jhdr->start <= 0 \
146 || jnl->jhdr->start > jnl->jhdr->size) {\
147 panic("%s:%d: jhdr start looks bad (0x%llx max size 0x%llx)\n", \
148 __FILE__, __LINE__, jnl->jhdr->start, jnl->jhdr->size);\
150 if ( jnl->jhdr->end <= 0 \
151 || jnl->jhdr->end > jnl->jhdr->size) {\
152 panic("%s:%d: jhdr end looks bad (0x%llx max size 0x%llx)\n", \
153 __FILE__, __LINE__, jnl->jhdr->end, jnl->jhdr->size);\
157 #define CHECK_TRANSACTION(tr) \
160 panic("%s:%d: null transaction ptr?\n", __FILE__, __LINE__);\
162 if (tr->jnl == NULL) {\
163 panic("%s:%d: null tr->jnl ptr?\n", __FILE__, __LINE__);\
165 if (tr->blhdr != (block_list_header *)tr->tbuffer) {\
166 panic("%s:%d: blhdr (%p) != tbuffer (%p)\n", __FILE__, __LINE__, tr->blhdr, tr->tbuffer);\
168 if (tr->total_bytes < 0) {\
169 panic("%s:%d: tr total_bytes looks bad: %d\n", __FILE__, __LINE__, tr->total_bytes);\
171 if (tr->journal_start < 0) {\
172 panic("%s:%d: tr journal start looks bad: 0x%llx\n", __FILE__, __LINE__, tr->journal_start);\
174 if (tr->journal_end < 0) {\
175 panic("%s:%d: tr journal end looks bad: 0x%llx\n", __FILE__, __LINE__, tr->journal_end);\
177 if (tr->blhdr && (tr->blhdr->max_blocks <= 0 || tr->blhdr->max_blocks > (tr->jnl->jhdr->size/tr->jnl->jhdr->jhdr_size))) {\
178 panic("%s:%d: tr blhdr max_blocks looks bad: %d\n", __FILE__, __LINE__, tr->blhdr->max_blocks);\
185 // this isn't a great checksum routine but it will do for now.
186 // we use it to checksum the journal header and the block list
187 // headers that are at the start of each transaction.
190 calc_checksum(char *ptr
, int len
)
194 // this is a lame checksum but for now it'll do
195 for(i
=0; i
< len
; i
++, ptr
++) {
196 cksum
= (cksum
<< 8) ^ (cksum
+ *(unsigned char *)ptr
);
205 lck_grp_attr_t
* jnl_group_attr
;
206 lck_attr_t
* jnl_lock_attr
;
207 lck_grp_t
* jnl_mutex_group
;
212 jnl_lock_attr
= lck_attr_alloc_init();
213 jnl_group_attr
= lck_grp_attr_alloc_init();
214 jnl_mutex_group
= lck_grp_alloc_init("jnl-mutex", jnl_group_attr
);
217 static __inline__
void
218 lock_journal(journal
*jnl
)
220 lck_mtx_lock(&jnl
->jlock
);
223 static __inline__
void
224 unlock_journal(journal
*jnl
)
226 lck_mtx_unlock(&jnl
->jlock
);
229 static __inline__
void
230 lock_oldstart(journal
*jnl
)
232 lck_mtx_lock(&jnl
->old_start_lock
);
235 static __inline__
void
236 unlock_oldstart(journal
*jnl
)
238 lck_mtx_unlock(&jnl
->old_start_lock
);
243 #define JNL_WRITE 0x0001
244 #define JNL_READ 0x0002
245 #define JNL_HEADER 0x8000
248 // This function sets up a fake buf and passes it directly to the
249 // journal device strategy routine (so that it won't get cached in
252 // It also handles range checking the i/o so that we don't write
253 // outside the journal boundaries and it will wrap the i/o back
254 // to the beginning if necessary (skipping over the journal header)
257 do_journal_io(journal
*jnl
, off_t
*offset
, void *data
, size_t len
, int direction
)
264 if (*offset
< 0 || *offset
> jnl
->jhdr
->size
) {
265 panic("jnl: do_jnl_io: bad offset 0x%llx (max 0x%llx)\n", *offset
, jnl
->jhdr
->size
);
268 if (direction
& JNL_WRITE
)
269 max_iosize
= jnl
->max_write_size
;
270 else if (direction
& JNL_READ
)
271 max_iosize
= jnl
->max_read_size
;
273 max_iosize
= 128 * 1024;
276 bp
= alloc_io_buf(jnl
->jdev
, 1);
278 if (*offset
+ (off_t
)curlen
> jnl
->jhdr
->size
&& *offset
!= 0 && jnl
->jhdr
->size
!= 0) {
279 if (*offset
== jnl
->jhdr
->size
) {
280 *offset
= jnl
->jhdr
->jhdr_size
;
282 curlen
= (off_t
)jnl
->jhdr
->size
- *offset
;
286 if (curlen
> max_iosize
) {
291 panic("jnl: do_jnl_io: curlen == %d, offset 0x%llx len %zd\n", curlen
, *offset
, len
);
294 if (*offset
== 0 && (direction
& JNL_HEADER
) == 0) {
295 panic("jnl: request for i/o to jnl-header without JNL_HEADER flag set! (len %d, data %p)\n", curlen
, data
);
298 if (direction
& JNL_READ
)
299 buf_setflags(bp
, B_READ
);
302 * don't have to set any flags
304 vnode_startwrite(jnl
->jdev
);
306 buf_setsize(bp
, curlen
);
307 buf_setcount(bp
, curlen
);
308 buf_setdataptr(bp
, (uintptr_t)data
);
309 buf_setblkno(bp
, (daddr64_t
) ((jnl
->jdev_offset
+ *offset
) / (off_t
)jnl
->jhdr
->jhdr_size
));
310 buf_setlblkno(bp
, (daddr64_t
) ((jnl
->jdev_offset
+ *offset
) / (off_t
)jnl
->jhdr
->jhdr_size
));
311 if ((direction
& JNL_WRITE
) && (jnl
->flags
& JOURNAL_DO_FUA_WRITES
)) {
315 DTRACE_IO1(journal__start
, buf_t
, bp
);
316 err
= VNOP_STRATEGY(bp
);
318 err
= (int)buf_biowait(bp
);
320 DTRACE_IO1(journal__done
, buf_t
, bp
);
324 printf("jnl: %s: do_jnl_io: strategy err 0x%x\n", jnl
->jdev_name
, err
);
331 // handle wrap-around
332 data
= (char *)data
+ curlen
;
333 curlen
= len
- io_sz
;
334 if (*offset
>= jnl
->jhdr
->size
) {
335 *offset
= jnl
->jhdr
->jhdr_size
;
344 read_journal_data(journal
*jnl
, off_t
*offset
, void *data
, size_t len
)
346 return do_journal_io(jnl
, offset
, data
, len
, JNL_READ
);
350 write_journal_data(journal
*jnl
, off_t
*offset
, void *data
, size_t len
)
352 return do_journal_io(jnl
, offset
, data
, len
, JNL_WRITE
);
357 read_journal_header(journal
*jnl
, void *data
, size_t len
)
359 off_t hdr_offset
= 0;
361 return do_journal_io(jnl
, &hdr_offset
, data
, len
, JNL_READ
|JNL_HEADER
);
365 write_journal_header(journal
*jnl
, int updating_start
)
367 static int num_err_prints
= 0;
369 off_t jhdr_offset
= 0;
370 struct vfs_context context
;
372 context
.vc_thread
= current_thread();
373 context
.vc_ucred
= NOCRED
;
375 // Flush the track cache if we're not doing force-unit-access
378 if (!updating_start
&& (jnl
->flags
& JOURNAL_DO_FUA_WRITES
) == 0) {
379 ret
= VNOP_IOCTL(jnl
->jdev
, DKIOCSYNCHRONIZECACHE
, NULL
, FWRITE
, &context
);
383 // Only print this error if it's a different error than the
384 // previous one, or if it's the first time for this device
385 // or if the total number of printfs is less than 25. We
386 // allow for up to 25 printfs to insure that some make it
387 // into the on-disk syslog. Otherwise if we only printed
388 // one, it's possible it would never make it to the syslog
389 // for the root volume and that makes debugging hard.
391 if ( ret
!= jnl
->last_flush_err
392 || (jnl
->flags
& JOURNAL_FLUSHCACHE_ERR
) == 0
393 || num_err_prints
++ < 25) {
395 printf("jnl: %s: flushing fs disk buffer returned 0x%x\n", jnl
->jdev_name
, ret
);
397 jnl
->flags
|= JOURNAL_FLUSHCACHE_ERR
;
398 jnl
->last_flush_err
= ret
;
402 jnl
->jhdr
->checksum
= 0;
403 jnl
->jhdr
->checksum
= calc_checksum((char *)jnl
->jhdr
, JOURNAL_HEADER_CKSUM_SIZE
);
404 if (do_journal_io(jnl
, &jhdr_offset
, jnl
->header_buf
, jnl
->jhdr
->jhdr_size
, JNL_WRITE
|JNL_HEADER
) != (size_t)jnl
->jhdr
->jhdr_size
) {
405 printf("jnl: %s: write_journal_header: error writing the journal header!\n", jnl
->jdev_name
);
406 jnl
->flags
|= JOURNAL_INVALID
;
410 // If we're not doing force-unit-access writes, then we
411 // have to flush after writing the journal header so that
412 // a future transaction doesn't sneak out to disk before
413 // the header does and thus overwrite data that the old
414 // journal header refers to. Saw this exact case happen
415 // on an IDE bus analyzer with Larry Barras so while it
416 // may seem obscure, it's not.
418 if (updating_start
&& (jnl
->flags
& JOURNAL_DO_FUA_WRITES
) == 0) {
419 VNOP_IOCTL(jnl
->jdev
, DKIOCSYNCHRONIZECACHE
, NULL
, FWRITE
, &context
);
428 // this is a work function used to free up transactions that
429 // completed. they can't be free'd from buffer_flushed_callback
430 // because it is called from deep with the disk driver stack
431 // and thus can't do something that would potentially cause
432 // paging. it gets called by each of the journal api entry
433 // points so stuff shouldn't hang around for too long.
436 free_old_stuff(journal
*jnl
)
438 transaction
*tr
, *next
;
442 jnl
->tr_freeme
= NULL
;
443 unlock_oldstart(jnl
);
447 FREE_ZONE(tr
, sizeof(transaction
), M_JNL_TR
);
455 // This is our callback that lets us know when a buffer has been
456 // flushed to disk. It's called from deep within the driver stack
457 // and thus is quite limited in what it can do. Notably, it can
458 // not initiate any new i/o's or allocate/free memory.
461 buffer_flushed_callback(struct buf
*bp
, void *arg
)
465 transaction
*ctr
, *prev
=NULL
, *next
;
467 int bufsize
, amt_flushed
, total_bytes
;
470 //printf("jnl: buf flush: bp @ 0x%x l/blkno %qd/%qd vp 0x%x tr @ 0x%x\n",
471 // bp, buf_lblkno(bp), buf_blkno(bp), buf_vnode(bp), arg);
473 // snarf out the bits we want
474 bufsize
= buf_size(bp
);
475 tr
= (transaction
*)arg
;
477 // then we've already seen it
482 CHECK_TRANSACTION(tr
);
485 if (jnl
->flags
& JOURNAL_INVALID
) {
491 amt_flushed
= tr
->num_killed
;
492 total_bytes
= tr
->total_bytes
;
494 // update the number of blocks that have been flushed.
495 // this buf may represent more than one block so take
496 // that into account.
498 // OSAddAtomic() returns the value of tr->num_flushed before the add
500 amt_flushed
+= OSAddAtomic(bufsize
, &tr
->num_flushed
);
503 // if this transaction isn't done yet, just return as
504 // there is nothing to do.
506 // NOTE: we are careful to not reference anything through
507 // the tr pointer after doing the OSAddAtomic(). if
508 // this if statement fails then we are the last one
509 // and then it's ok to dereference "tr".
511 if ((amt_flushed
+ bufsize
) < total_bytes
) {
515 // this will single thread checking the transaction
518 if (tr
->total_bytes
== (int)0xfbadc0de) {
519 // then someone beat us to it...
520 unlock_oldstart(jnl
);
524 // mark this so that we're the owner of dealing with the
525 // cleanup for this transaction
526 tr
->total_bytes
= 0xfbadc0de;
528 //printf("jnl: tr 0x%x (0x%llx 0x%llx) in jnl 0x%x completed.\n",
529 // tr, tr->journal_start, tr->journal_end, jnl);
531 // find this entry in the old_start[] index and mark it completed
532 for(i
=0; i
< sizeof(jnl
->old_start
)/sizeof(jnl
->old_start
[0]); i
++) {
534 if ((off_t
)(jnl
->old_start
[i
] & ~(0x8000000000000000ULL
)) == tr
->journal_start
) {
535 jnl
->old_start
[i
] &= ~(0x8000000000000000ULL
);
540 if (i
>= sizeof(jnl
->old_start
)/sizeof(jnl
->old_start
[0])) {
541 panic("jnl: buffer_flushed: did not find tr w/start @ %lld (tr %p, jnl %p)\n",
542 tr
->journal_start
, tr
, jnl
);
546 // if we are here then we need to update the journal header
547 // to reflect that this transaction is complete
548 if (tr
->journal_start
== jnl
->active_start
) {
549 jnl
->active_start
= tr
->journal_end
;
550 tr
->journal_start
= tr
->journal_end
= (off_t
)0;
553 // go through the completed_trs list and try to coalesce
554 // entries, restarting back at the beginning if we have to.
555 for(ctr
=jnl
->completed_trs
; ctr
; prev
=ctr
, ctr
=next
) {
556 if (ctr
->journal_start
== jnl
->active_start
) {
557 jnl
->active_start
= ctr
->journal_end
;
559 prev
->next
= ctr
->next
;
561 if (ctr
== jnl
->completed_trs
) {
562 jnl
->completed_trs
= ctr
->next
;
565 next
= jnl
->completed_trs
; // this starts us over again
566 ctr
->next
= jnl
->tr_freeme
;
567 jnl
->tr_freeme
= ctr
;
569 } else if (tr
->journal_end
== ctr
->journal_start
) {
570 ctr
->journal_start
= tr
->journal_start
;
571 next
= jnl
->completed_trs
; // this starts us over again
573 tr
->journal_start
= tr
->journal_end
= (off_t
)0;
574 } else if (tr
->journal_start
== ctr
->journal_end
) {
575 ctr
->journal_end
= tr
->journal_end
;
577 tr
->journal_start
= tr
->journal_end
= (off_t
)0;
578 } else if (ctr
->next
&& ctr
->journal_end
== ctr
->next
->journal_start
) {
579 // coalesce the next entry with this one and link the next
580 // entry in at the head of the tr_freeme list
581 next
= ctr
->next
; // temporarily use the "next" variable
582 ctr
->journal_end
= next
->journal_end
;
583 ctr
->next
= next
->next
;
584 next
->next
= jnl
->tr_freeme
; // link in the next guy at the head of the tr_freeme list
585 jnl
->tr_freeme
= next
;
587 next
= jnl
->completed_trs
; // this starts us over again
594 // if this is true then we didn't merge with anyone
595 // so link ourselves in at the head of the completed
597 if (tr
->journal_start
!= 0) {
598 // put this entry into the correct sorted place
599 // in the list instead of just at the head.
603 for(ctr
=jnl
->completed_trs
; ctr
&& tr
->journal_start
> ctr
->journal_start
; prev
=ctr
, ctr
=ctr
->next
) {
607 if (ctr
== NULL
&& prev
== NULL
) {
608 jnl
->completed_trs
= tr
;
610 } else if (ctr
== jnl
->completed_trs
) {
611 tr
->next
= jnl
->completed_trs
;
612 jnl
->completed_trs
= tr
;
614 tr
->next
= prev
->next
;
618 // if we're here this tr got merged with someone else so
619 // put it on the list to be free'd
620 tr
->next
= jnl
->tr_freeme
;
623 unlock_oldstart(jnl
);
627 #include <libkern/OSByteOrder.h>
629 #define SWAP16(x) OSSwapInt16(x)
630 #define SWAP32(x) OSSwapInt32(x)
631 #define SWAP64(x) OSSwapInt64(x)
635 swap_journal_header(journal
*jnl
)
637 jnl
->jhdr
->magic
= SWAP32(jnl
->jhdr
->magic
);
638 jnl
->jhdr
->endian
= SWAP32(jnl
->jhdr
->endian
);
639 jnl
->jhdr
->start
= SWAP64(jnl
->jhdr
->start
);
640 jnl
->jhdr
->end
= SWAP64(jnl
->jhdr
->end
);
641 jnl
->jhdr
->size
= SWAP64(jnl
->jhdr
->size
);
642 jnl
->jhdr
->blhdr_size
= SWAP32(jnl
->jhdr
->blhdr_size
);
643 jnl
->jhdr
->checksum
= SWAP32(jnl
->jhdr
->checksum
);
644 jnl
->jhdr
->jhdr_size
= SWAP32(jnl
->jhdr
->jhdr_size
);
645 jnl
->jhdr
->sequence_num
= SWAP32(jnl
->jhdr
->sequence_num
);
649 swap_block_list_header(journal
*jnl
, block_list_header
*blhdr
)
653 blhdr
->max_blocks
= SWAP16(blhdr
->max_blocks
);
654 blhdr
->num_blocks
= SWAP16(blhdr
->num_blocks
);
655 blhdr
->bytes_used
= SWAP32(blhdr
->bytes_used
);
656 blhdr
->checksum
= SWAP32(blhdr
->checksum
);
657 blhdr
->flags
= SWAP32(blhdr
->flags
);
659 if (blhdr
->num_blocks
>= ((jnl
->jhdr
->blhdr_size
/ sizeof(block_info
)) - 1)) {
660 printf("jnl: %s: blhdr num blocks looks suspicious (%d / blhdr size %d). not swapping.\n", jnl
->jdev_name
, blhdr
->num_blocks
, jnl
->jhdr
->blhdr_size
);
664 for(i
=0; i
< blhdr
->num_blocks
; i
++) {
665 blhdr
->binfo
[i
].bnum
= SWAP64(blhdr
->binfo
[i
].bnum
);
666 blhdr
->binfo
[i
].u
.bi
.bsize
= SWAP32(blhdr
->binfo
[i
].u
.bi
.bsize
);
667 blhdr
->binfo
[i
].u
.bi
.b
.cksum
= SWAP32(blhdr
->binfo
[i
].u
.bi
.b
.cksum
);
673 update_fs_block(journal
*jnl
, void *block_ptr
, off_t fs_block
, size_t bsize
)
676 struct buf
*oblock_bp
=NULL
;
678 // first read the block we want.
679 ret
= buf_meta_bread(jnl
->fsdev
, (daddr64_t
)fs_block
, bsize
, NOCRED
, &oblock_bp
);
681 printf("jnl: %s: update_fs_block: error reading fs block # %lld! (ret %d)\n", jnl
->jdev_name
, fs_block
, ret
);
684 buf_brelse(oblock_bp
);
688 // let's try to be aggressive here and just re-write the block
689 oblock_bp
= buf_getblk(jnl
->fsdev
, (daddr64_t
)fs_block
, bsize
, 0, 0, BLK_META
);
690 if (oblock_bp
== NULL
) {
691 printf("jnl: %s: update_fs_block: buf_getblk() for %lld failed! failing update.\n", jnl
->jdev_name
, fs_block
);
696 // make sure it's the correct size.
697 if (buf_size(oblock_bp
) != bsize
) {
698 buf_brelse(oblock_bp
);
702 // copy the journal data over top of it
703 memcpy((char *)0 + buf_dataptr(oblock_bp
), block_ptr
, bsize
);
705 if ((ret
= VNOP_BWRITE(oblock_bp
)) != 0) {
706 printf("jnl: %s: update_fs_block: failed to update block %lld (ret %d)\n", jnl
->jdev_name
, fs_block
,ret
);
710 // and now invalidate it so that if someone else wants to read
711 // it in a different size they'll be able to do it.
712 ret
= buf_meta_bread(jnl
->fsdev
, (daddr64_t
)fs_block
, bsize
, NOCRED
, &oblock_bp
);
714 buf_markinvalid(oblock_bp
);
715 buf_brelse(oblock_bp
);
722 grow_table(struct bucket
**buf_ptr
, int num_buckets
, int new_size
)
724 struct bucket
*newBuf
;
725 int current_size
= num_buckets
, i
;
727 // return if newsize is less than the current size
728 if (new_size
< num_buckets
) {
732 if ((MALLOC(newBuf
, struct bucket
*, new_size
*sizeof(struct bucket
), M_TEMP
, M_WAITOK
)) == NULL
) {
733 printf("jnl: grow_table: no memory to expand coalesce buffer!\n");
737 // printf("jnl: lookup_bucket: expanded co_buf to %d elems\n", new_size);
739 // copy existing elements
740 bcopy(*buf_ptr
, newBuf
, num_buckets
*sizeof(struct bucket
));
742 // initialize the new ones
743 for(i
=num_buckets
; i
< new_size
; i
++) {
744 newBuf
[i
].block_num
= (off_t
)-1;
747 // free the old container
748 FREE(*buf_ptr
, M_TEMP
);
757 lookup_bucket(struct bucket
**buf_ptr
, off_t block_num
, int num_full
)
759 int lo
, hi
, index
, matches
, i
;
762 return 0; // table is empty, so insert at index=0
769 // perform binary search for block_num
771 int mid
= (hi
- lo
)/2 + lo
;
772 off_t this_num
= (*buf_ptr
)[mid
].block_num
;
774 if (block_num
== this_num
) {
779 if (block_num
< this_num
) {
784 if (block_num
> this_num
) {
790 // check if lo and hi converged on the match
791 if (block_num
== (*buf_ptr
)[hi
].block_num
) {
795 // if no existing entry found, find index for new one
797 index
= (block_num
< (*buf_ptr
)[hi
].block_num
) ? hi
: hi
+ 1;
799 // make sure that we return the right-most index in the case of multiple matches
802 while(i
< num_full
&& block_num
== (*buf_ptr
)[i
].block_num
) {
814 insert_block(journal
*jnl
, struct bucket
**buf_ptr
, int blk_index
, off_t num
, size_t size
, size_t offset
, int32_t cksum
, int *num_buckets_ptr
, int *num_full_ptr
, int overwriting
)
817 // grow the table if we're out of space
818 if (*num_full_ptr
>= *num_buckets_ptr
) {
819 int new_size
= *num_buckets_ptr
* 2;
820 int grow_size
= grow_table(buf_ptr
, *num_buckets_ptr
, new_size
);
822 if (grow_size
< new_size
) {
823 printf("jnl: %s: add_block: grow_table returned an error!\n", jnl
->jdev_name
);
827 *num_buckets_ptr
= grow_size
; //update num_buckets to reflect the new size
830 // if we're not inserting at the end, we need to bcopy
831 if (blk_index
!= *num_full_ptr
) {
832 bcopy( (*buf_ptr
)+(blk_index
), (*buf_ptr
)+(blk_index
+1), (*num_full_ptr
-blk_index
)*sizeof(struct bucket
) );
835 (*num_full_ptr
)++; // increment only if we're not overwriting
838 // sanity check the values we're about to add
839 if ((off_t
)offset
>= jnl
->jhdr
->size
) {
840 offset
= jnl
->jhdr
->jhdr_size
+ (offset
- jnl
->jhdr
->size
);
843 panic("jnl: insert_block: bad size in insert_block (%zd)\n", size
);
846 (*buf_ptr
)[blk_index
].block_num
= num
;
847 (*buf_ptr
)[blk_index
].block_size
= size
;
848 (*buf_ptr
)[blk_index
].jnl_offset
= offset
;
849 (*buf_ptr
)[blk_index
].cksum
= cksum
;
855 do_overlap(journal
*jnl
, struct bucket
**buf_ptr
, int blk_index
, off_t block_num
, size_t size
, __unused
size_t offset
, int32_t cksum
, int *num_buckets_ptr
, int *num_full_ptr
)
857 int num_to_remove
, index
, i
, overwrite
, err
;
858 size_t jhdr_size
= jnl
->jhdr
->jhdr_size
, new_offset
;
859 off_t overlap
, block_start
, block_end
;
861 block_start
= block_num
*jhdr_size
;
862 block_end
= block_start
+ size
;
863 overwrite
= (block_num
== (*buf_ptr
)[blk_index
].block_num
&& size
>= (*buf_ptr
)[blk_index
].block_size
);
865 // first, eliminate any overlap with the previous entry
866 if (blk_index
!= 0 && !overwrite
) {
867 off_t prev_block_start
= (*buf_ptr
)[blk_index
-1].block_num
*jhdr_size
;
868 off_t prev_block_end
= prev_block_start
+ (*buf_ptr
)[blk_index
-1].block_size
;
869 overlap
= prev_block_end
- block_start
;
871 if (overlap
% jhdr_size
!= 0) {
872 panic("jnl: do_overlap: overlap with previous entry not a multiple of %zd\n", jhdr_size
);
875 // if the previous entry completely overlaps this one, we need to break it into two pieces.
876 if (prev_block_end
> block_end
) {
877 off_t new_num
= block_end
/ jhdr_size
;
878 size_t new_size
= prev_block_end
- block_end
;
880 new_offset
= (*buf_ptr
)[blk_index
-1].jnl_offset
+ (block_end
- prev_block_start
);
882 err
= insert_block(jnl
, buf_ptr
, blk_index
, new_num
, new_size
, new_offset
, cksum
, num_buckets_ptr
, num_full_ptr
, 0);
884 panic("jnl: do_overlap: error inserting during pre-overlap\n");
888 // Regardless, we need to truncate the previous entry to the beginning of the overlap
889 (*buf_ptr
)[blk_index
-1].block_size
= block_start
- prev_block_start
;
890 (*buf_ptr
)[blk_index
-1].cksum
= 0; // have to blow it away because there's no way to check it
894 // then, bail out fast if there's no overlap with the entries that follow
895 if (!overwrite
&& block_end
<= (off_t
)((*buf_ptr
)[blk_index
].block_num
*jhdr_size
)) {
896 return 0; // no overlap, no overwrite
897 } else if (overwrite
&& (blk_index
+ 1 >= *num_full_ptr
|| block_end
<= (off_t
)((*buf_ptr
)[blk_index
+1].block_num
*jhdr_size
))) {
899 (*buf_ptr
)[blk_index
].cksum
= cksum
; // update this
900 return 1; // simple overwrite
903 // Otherwise, find all cases of total and partial overlap. We use the special
904 // block_num of -2 to designate entries that are completely overlapped and must
905 // be eliminated. The block_num, size, and jnl_offset of partially overlapped
906 // entries must be adjusted to keep the array consistent.
909 while(index
< *num_full_ptr
&& block_end
> (off_t
)((*buf_ptr
)[index
].block_num
*jhdr_size
)) {
910 if (block_end
>= (off_t
)(((*buf_ptr
)[index
].block_num
*jhdr_size
+ (*buf_ptr
)[index
].block_size
))) {
911 (*buf_ptr
)[index
].block_num
= -2; // mark this for deletion
914 overlap
= block_end
- (*buf_ptr
)[index
].block_num
*jhdr_size
;
916 if (overlap
% jhdr_size
!= 0) {
917 panic("jnl: do_overlap: overlap of %lld is not multiple of %zd\n", overlap
, jhdr_size
);
920 // if we partially overlap this entry, adjust its block number, jnl offset, and size
921 (*buf_ptr
)[index
].block_num
+= (overlap
/ jhdr_size
); // make sure overlap is multiple of jhdr_size, or round up
922 (*buf_ptr
)[index
].cksum
= 0;
924 new_offset
= (*buf_ptr
)[index
].jnl_offset
+ overlap
; // check for wrap-around
925 if ((off_t
)new_offset
>= jnl
->jhdr
->size
) {
926 new_offset
= jhdr_size
+ (new_offset
- jnl
->jhdr
->size
);
928 (*buf_ptr
)[index
].jnl_offset
= new_offset
;
930 (*buf_ptr
)[index
].block_size
-= overlap
; // sanity check for negative value
931 if ((*buf_ptr
)[index
].block_size
<= 0) {
932 panic("jnl: do_overlap: after overlap, new block size is invalid (%u)\n", (*buf_ptr
)[index
].block_size
);
933 // return -1; // if above panic is removed, return -1 for error
942 // bcopy over any completely overlapped entries, starting at the right (where the above loop broke out)
943 index
--; // start with the last index used within the above loop
944 while(index
>= blk_index
) {
945 if ((*buf_ptr
)[index
].block_num
== -2) {
946 if (index
== *num_full_ptr
-1) {
947 (*buf_ptr
)[index
].block_num
= -1; // it's the last item in the table... just mark as free
949 bcopy( (*buf_ptr
)+(index
+1), (*buf_ptr
)+(index
), (*num_full_ptr
- (index
+ 1)) * sizeof(struct bucket
) );
956 // eliminate any stale entries at the end of the table
957 for(i
=*num_full_ptr
; i
< (*num_full_ptr
+ num_to_remove
); i
++) {
958 (*buf_ptr
)[i
].block_num
= -1;
961 return 0; // if we got this far, we need to insert the entry into the table (rather than overwrite)
964 // PR-3105942: Coalesce writes to the same block in journal replay
965 // We coalesce writes by maintaining a dynamic sorted array of physical disk blocks
966 // to be replayed and the corresponding location in the journal which contains
967 // the most recent data for those blocks. The array is "played" once the all the
968 // blocks in the journal have been coalesced. The code for the case of conflicting/
969 // overlapping writes to a single block is the most dense. Because coalescing can
970 // disrupt the existing time-ordering of blocks in the journal playback, care
971 // is taken to catch any overlaps and keep the array consistent.
973 add_block(journal
*jnl
, struct bucket
**buf_ptr
, off_t block_num
, size_t size
, __unused
size_t offset
, int32_t cksum
, int *num_buckets_ptr
, int *num_full_ptr
)
975 int blk_index
, overwriting
;
977 // on return from lookup_bucket(), blk_index is the index into the table where block_num should be
978 // inserted (or the index of the elem to overwrite).
979 blk_index
= lookup_bucket( buf_ptr
, block_num
, *num_full_ptr
);
981 // check if the index is within bounds (if we're adding this block to the end of
982 // the table, blk_index will be equal to num_full)
983 if (blk_index
< 0 || blk_index
> *num_full_ptr
) {
984 //printf("jnl: add_block: trouble adding block to co_buf\n");
986 } // else printf("jnl: add_block: adding block 0x%llx at i=%d\n", block_num, blk_index);
988 // Determine whether we're overwriting an existing entry by checking for overlap
989 overwriting
= do_overlap(jnl
, buf_ptr
, blk_index
, block_num
, size
, offset
, cksum
, num_buckets_ptr
, num_full_ptr
);
990 if (overwriting
< 0) {
991 return -1; // if we got an error, pass it along
994 // returns the index, or -1 on error
995 blk_index
= insert_block(jnl
, buf_ptr
, blk_index
, block_num
, size
, offset
, cksum
, num_buckets_ptr
, num_full_ptr
, overwriting
);
1001 replay_journal(journal
*jnl
)
1003 int i
, orig_checksum
, checksum
, check_block_checksums
=0, bad_blocks
=0;
1005 size_t max_bsize
= 0; /* protected by block_ptr */
1006 block_list_header
*blhdr
;
1007 off_t offset
, txn_start_offset
=0, blhdr_offset
, orig_jnl_start
;
1008 char *buff
, *block_ptr
=NULL
;
1009 struct bucket
*co_buf
;
1010 int num_buckets
= STARTING_BUCKETS
, num_full
, check_past_jnl_end
= 1, in_uncharted_territory
=0;
1011 uint32_t last_sequence_num
= 0;
1013 // wrap the start ptr if it points to the very end of the journal
1014 if (jnl
->jhdr
->start
== jnl
->jhdr
->size
) {
1015 jnl
->jhdr
->start
= jnl
->jhdr
->jhdr_size
;
1017 if (jnl
->jhdr
->end
== jnl
->jhdr
->size
) {
1018 jnl
->jhdr
->end
= jnl
->jhdr
->jhdr_size
;
1021 if (jnl
->jhdr
->start
== jnl
->jhdr
->end
) {
1025 orig_jnl_start
= jnl
->jhdr
->start
;
1027 // allocate memory for the header_block. we'll read each blhdr into this
1028 if (kmem_alloc(kernel_map
, (vm_offset_t
*)&buff
, jnl
->jhdr
->blhdr_size
)) {
1029 printf("jnl: %s: replay_journal: no memory for block buffer! (%d bytes)\n",
1030 jnl
->jdev_name
, jnl
->jhdr
->blhdr_size
);
1034 // allocate memory for the coalesce buffer
1035 if ((MALLOC(co_buf
, struct bucket
*, num_buckets
*sizeof(struct bucket
), M_TEMP
, M_WAITOK
)) == NULL
) {
1036 printf("jnl: %s: replay_journal: no memory for coalesce buffer!\n", jnl
->jdev_name
);
1042 // initialize entries
1043 for(i
=0; i
< num_buckets
; i
++) {
1044 co_buf
[i
].block_num
= -1;
1046 num_full
= 0; // empty at first
1049 printf("jnl: %s: replay_journal: from: %lld to: %lld (joffset 0x%llx)\n",
1050 jnl
->jdev_name
, jnl
->jhdr
->start
, jnl
->jhdr
->end
, jnl
->jdev_offset
);
1052 while(check_past_jnl_end
|| jnl
->jhdr
->start
!= jnl
->jhdr
->end
) {
1053 offset
= blhdr_offset
= jnl
->jhdr
->start
;
1054 ret
= read_journal_data(jnl
, &offset
, buff
, jnl
->jhdr
->blhdr_size
);
1055 if (ret
!= (size_t)jnl
->jhdr
->blhdr_size
) {
1056 printf("jnl: %s: replay_journal: Could not read block list header block @ 0x%llx!\n", jnl
->jdev_name
, offset
);
1058 goto bad_txn_handling
;
1061 blhdr
= (block_list_header
*)buff
;
1063 orig_checksum
= blhdr
->checksum
;
1064 blhdr
->checksum
= 0;
1065 if (jnl
->flags
& JOURNAL_NEED_SWAP
) {
1066 // calculate the checksum based on the unswapped data
1067 // because it is done byte-at-a-time.
1068 orig_checksum
= SWAP32(orig_checksum
);
1069 checksum
= calc_checksum((char *)blhdr
, BLHDR_CHECKSUM_SIZE
);
1070 swap_block_list_header(jnl
, blhdr
);
1072 checksum
= calc_checksum((char *)blhdr
, BLHDR_CHECKSUM_SIZE
);
1077 // XXXdbg - if these checks fail, we should replay as much
1078 // we can in the hopes that it will still leave the
1079 // drive in a better state than if we didn't replay
1082 if (checksum
!= orig_checksum
) {
1083 if (check_past_jnl_end
&& in_uncharted_territory
) {
1085 if (blhdr_offset
!= jnl
->jhdr
->end
) {
1086 printf("jnl: %s: Extra txn replay stopped @ %lld / 0x%llx\n", jnl
->jdev_name
, blhdr_offset
, blhdr_offset
);
1089 check_past_jnl_end
= 0;
1090 jnl
->jhdr
->end
= blhdr_offset
;
1094 printf("jnl: %s: replay_journal: bad block list header @ 0x%llx (checksum 0x%x != 0x%x)\n",
1095 jnl
->jdev_name
, blhdr_offset
, orig_checksum
, checksum
);
1097 if (blhdr_offset
== orig_jnl_start
) {
1098 // if there's nothing in the journal at all, just bail out altogether.
1103 goto bad_txn_handling
;
1106 if ( (last_sequence_num
!= 0)
1107 && (blhdr
->binfo
[0].u
.bi
.b
.sequence_num
!= 0)
1108 && (blhdr
->binfo
[0].u
.bi
.b
.sequence_num
!= last_sequence_num
)
1109 && (blhdr
->binfo
[0].u
.bi
.b
.sequence_num
!= last_sequence_num
+1)) {
1111 txn_start_offset
= jnl
->jhdr
->end
= blhdr_offset
;
1113 if (check_past_jnl_end
) {
1114 check_past_jnl_end
= 0;
1115 printf("jnl: %s: 2: extra replay stopped @ %lld / 0x%llx (seq %d < %d)\n",
1116 jnl
->jdev_name
, blhdr_offset
, blhdr_offset
, blhdr
->binfo
[0].u
.bi
.b
.sequence_num
, last_sequence_num
);
1120 printf("jnl: %s: txn sequence numbers out of order in txn @ %lld / %llx! (%d < %d)\n",
1121 jnl
->jdev_name
, blhdr_offset
, blhdr_offset
, blhdr
->binfo
[0].u
.bi
.b
.sequence_num
, last_sequence_num
);
1123 goto bad_txn_handling
;
1125 last_sequence_num
= blhdr
->binfo
[0].u
.bi
.b
.sequence_num
;
1127 if (blhdr_offset
>= jnl
->jhdr
->end
&& jnl
->jhdr
->start
<= jnl
->jhdr
->end
) {
1128 if (last_sequence_num
== 0) {
1129 check_past_jnl_end
= 0;
1130 printf("jnl: %s: pre-sequence-num-enabled txn's - can not go further than end (%lld %lld).\n",
1131 jnl
->jdev_name
, jnl
->jhdr
->start
, jnl
->jhdr
->end
);
1132 if (jnl
->jhdr
->start
!= jnl
->jhdr
->end
) {
1133 jnl
->jhdr
->start
= jnl
->jhdr
->end
;
1137 printf("jnl: %s: examining extra transactions starting @ %lld / 0x%llx\n", jnl
->jdev_name
, blhdr_offset
, blhdr_offset
);
1140 if ( blhdr
->max_blocks
<= 0 || blhdr
->max_blocks
> (jnl
->jhdr
->size
/jnl
->jhdr
->jhdr_size
)
1141 || blhdr
->num_blocks
<= 0 || blhdr
->num_blocks
> blhdr
->max_blocks
) {
1142 printf("jnl: %s: replay_journal: bad looking journal entry: max: %d num: %d\n",
1143 jnl
->jdev_name
, blhdr
->max_blocks
, blhdr
->num_blocks
);
1145 goto bad_txn_handling
;
1149 for(i
=1; i
< blhdr
->num_blocks
; i
++) {
1150 if (blhdr
->binfo
[i
].bnum
< 0 && blhdr
->binfo
[i
].bnum
!= (off_t
)-1) {
1151 printf("jnl: %s: replay_journal: bogus block number 0x%llx\n", jnl
->jdev_name
, blhdr
->binfo
[i
].bnum
);
1153 goto bad_txn_handling
;
1156 if ((size_t)blhdr
->binfo
[i
].u
.bi
.bsize
> max_bsize
) {
1157 max_bsize
= blhdr
->binfo
[i
].u
.bi
.bsize
;
1161 if (blhdr
->flags
& BLHDR_CHECK_CHECKSUMS
) {
1162 check_block_checksums
= 1;
1163 if (kmem_alloc(kernel_map
, (vm_offset_t
*)&block_ptr
, max_bsize
)) {
1170 if (blhdr
->flags
& BLHDR_FIRST_HEADER
) {
1171 txn_start_offset
= blhdr_offset
;
1174 //printf("jnl: replay_journal: adding %d blocks in journal entry @ 0x%llx to co_buf\n",
1175 // blhdr->num_blocks-1, jnl->jhdr->start);
1177 for(i
=1; i
< blhdr
->num_blocks
; i
++) {
1181 size
= blhdr
->binfo
[i
].u
.bi
.bsize
;
1182 number
= blhdr
->binfo
[i
].bnum
;
1184 // don't add "killed" blocks
1185 if (number
== (off_t
)-1) {
1186 //printf("jnl: replay_journal: skipping killed fs block (index %d)\n", i);
1189 if (check_block_checksums
) {
1193 block_offset
= offset
;
1195 // read the block so we can check the checksum
1196 ret
= read_journal_data(jnl
, &block_offset
, block_ptr
, size
);
1197 if (ret
!= (size_t)size
) {
1198 printf("jnl: %s: replay_journal: Could not read journal entry data @ offset 0x%llx!\n", jnl
->jdev_name
, offset
);
1200 goto bad_txn_handling
;
1203 disk_cksum
= calc_checksum(block_ptr
, size
);
1205 // there is no need to swap the checksum from disk because
1206 // it got swapped when the blhdr was read in.
1207 if (blhdr
->binfo
[i
].u
.bi
.b
.cksum
!= 0 && disk_cksum
!= blhdr
->binfo
[i
].u
.bi
.b
.cksum
) {
1208 printf("jnl: %s: txn starting at %lld (%lld) @ index %3d bnum %lld (%d) with disk cksum != blhdr cksum (0x%.8x 0x%.8x)\n",
1209 jnl
->jdev_name
, txn_start_offset
, blhdr_offset
, i
, number
, size
, disk_cksum
, blhdr
->binfo
[i
].u
.bi
.b
.cksum
);
1210 printf("jnl: 0x%.8x 0x%.8x 0x%.8x 0x%.8x 0x%.8x 0x%.8x 0x%.8x 0x%.8x\n",
1211 *(int *)&block_ptr
[0*sizeof(int)], *(int *)&block_ptr
[1*sizeof(int)], *(int *)&block_ptr
[2*sizeof(int)], *(int *)&block_ptr
[3*sizeof(int)],
1212 *(int *)&block_ptr
[4*sizeof(int)], *(int *)&block_ptr
[5*sizeof(int)], *(int *)&block_ptr
[6*sizeof(int)], *(int *)&block_ptr
[7*sizeof(int)]);
1215 goto bad_txn_handling
;
1220 // add this bucket to co_buf, coalescing where possible
1221 // printf("jnl: replay_journal: adding block 0x%llx\n", number);
1222 ret_val
= add_block(jnl
, &co_buf
, number
, size
, (size_t) offset
, blhdr
->binfo
[i
].u
.bi
.b
.cksum
, &num_buckets
, &num_full
);
1224 if (ret_val
== -1) {
1225 printf("jnl: %s: replay_journal: trouble adding block to co_buf\n", jnl
->jdev_name
);
1227 } // else printf("jnl: replay_journal: added block 0x%llx at i=%d\n", number);
1233 // check if the last block added puts us off the end of the jnl.
1234 // if so, we need to wrap to the beginning and take any remainder
1237 if (offset
>= jnl
->jhdr
->size
) {
1238 offset
= jnl
->jhdr
->jhdr_size
+ (offset
- jnl
->jhdr
->size
);
1243 kmem_free(kernel_map
, (vm_offset_t
)block_ptr
, max_bsize
);
1249 if (txn_start_offset
== 0) {
1250 printf("jnl: %s: no known good txn start offset! aborting journal replay.\n", jnl
->jdev_name
);
1254 jnl
->jhdr
->start
= orig_jnl_start
;
1255 jnl
->jhdr
->end
= txn_start_offset
;
1256 check_past_jnl_end
= 0;
1257 last_sequence_num
= 0;
1258 printf("jnl: %s: restarting journal replay (%lld - %lld)!\n", jnl
->jdev_name
, jnl
->jhdr
->start
, jnl
->jhdr
->end
);
1259 goto restart_replay
;
1262 jnl
->jhdr
->start
+= blhdr
->bytes_used
;
1263 if (jnl
->jhdr
->start
>= jnl
->jhdr
->size
) {
1264 // wrap around and skip the journal header block
1265 jnl
->jhdr
->start
= (jnl
->jhdr
->start
% jnl
->jhdr
->size
) + jnl
->jhdr
->jhdr_size
;
1268 if (jnl
->jhdr
->start
== jnl
->jhdr
->end
) {
1269 in_uncharted_territory
= 1;
1273 if (jnl
->jhdr
->start
!= jnl
->jhdr
->end
) {
1274 printf("jnl: %s: start %lld != end %lld. resetting end.\n", jnl
->jdev_name
, jnl
->jhdr
->start
, jnl
->jhdr
->end
);
1275 jnl
->jhdr
->end
= jnl
->jhdr
->start
;
1278 //printf("jnl: replay_journal: replaying %d blocks\n", num_full);
1281 * make sure it's at least one page in size, so
1282 * start max_bsize at PAGE_SIZE
1284 for (i
= 0, max_bsize
= PAGE_SIZE
; i
< num_full
; i
++) {
1286 if (co_buf
[i
].block_num
== (off_t
)-1)
1289 if (co_buf
[i
].block_size
> max_bsize
)
1290 max_bsize
= co_buf
[i
].block_size
;
1293 * round max_bsize up to the nearest PAGE_SIZE multiple
1295 if (max_bsize
& (PAGE_SIZE
- 1)) {
1296 max_bsize
= (max_bsize
+ PAGE_SIZE
) & ~(PAGE_SIZE
- 1);
1299 if (kmem_alloc(kernel_map
, (vm_offset_t
*)&block_ptr
, max_bsize
)) {
1303 // Replay the coalesced entries in the co-buf
1304 for(i
=0; i
< num_full
; i
++) {
1305 size_t size
= co_buf
[i
].block_size
;
1306 off_t jnl_offset
= (off_t
) co_buf
[i
].jnl_offset
;
1307 off_t number
= co_buf
[i
].block_num
;
1310 // printf("replaying co_buf[%d]: block 0x%llx, size 0x%x, jnl_offset 0x%llx\n", i, co_buf[i].block_num,
1311 // co_buf[i].block_size, co_buf[i].jnl_offset);
1313 if (number
== (off_t
)-1) {
1314 // printf("jnl: replay_journal: skipping killed fs block\n");
1317 // do journal read, and set the phys. block
1318 ret
= read_journal_data(jnl
, &jnl_offset
, block_ptr
, size
);
1320 printf("jnl: %s: replay_journal: Could not read journal entry data @ offset 0x%llx!\n", jnl
->jdev_name
, offset
);
1324 if (update_fs_block(jnl
, block_ptr
, number
, size
) != 0) {
1331 // done replaying; update jnl header
1332 if (write_journal_header(jnl
, 1) != 0) {
1336 printf("jnl: %s: journal replay done.\n", jnl
->jdev_name
);
1340 kmem_free(kernel_map
, (vm_offset_t
)block_ptr
, max_bsize
);
1344 // free the coalesce buffer
1345 FREE(co_buf
, M_TEMP
);
1348 kmem_free(kernel_map
, (vm_offset_t
)buff
, jnl
->jhdr
->blhdr_size
);
1353 kmem_free(kernel_map
, (vm_offset_t
)block_ptr
, max_bsize
);
1356 FREE(co_buf
, M_TEMP
);
1358 kmem_free(kernel_map
, (vm_offset_t
)buff
, jnl
->jhdr
->blhdr_size
);
1364 #define DEFAULT_TRANSACTION_BUFFER_SIZE (128*1024)
1365 #define MAX_TRANSACTION_BUFFER_SIZE (2048*1024)
1367 // XXXdbg - so I can change it in the debugger
1368 int def_tbuffer_size
= 0;
1372 // This function sets the size of the tbuffer and the
1373 // size of the blhdr. It assumes that jnl->jhdr->size
1374 // and jnl->jhdr->jhdr_size are already valid.
1377 size_up_tbuffer(journal
*jnl
, int tbuffer_size
, int phys_blksz
)
1380 // one-time initialization based on how much memory
1381 // there is in the machine.
1383 if (def_tbuffer_size
== 0) {
1384 if (mem_size
< (256*1024*1024)) {
1385 def_tbuffer_size
= DEFAULT_TRANSACTION_BUFFER_SIZE
;
1386 } else if (mem_size
< (512*1024*1024)) {
1387 def_tbuffer_size
= DEFAULT_TRANSACTION_BUFFER_SIZE
* 2;
1388 } else if (mem_size
< (1024*1024*1024)) {
1389 def_tbuffer_size
= DEFAULT_TRANSACTION_BUFFER_SIZE
* 3;
1391 def_tbuffer_size
= DEFAULT_TRANSACTION_BUFFER_SIZE
* (mem_size
/ (256*1024*1024));
1395 // size up the transaction buffer... can't be larger than the number
1396 // of blocks that can fit in a block_list_header block.
1397 if (tbuffer_size
== 0) {
1398 jnl
->tbuffer_size
= def_tbuffer_size
;
1400 // make sure that the specified tbuffer_size isn't too small
1401 if (tbuffer_size
< jnl
->jhdr
->blhdr_size
* 2) {
1402 tbuffer_size
= jnl
->jhdr
->blhdr_size
* 2;
1404 // and make sure it's an even multiple of the block size
1405 if ((tbuffer_size
% jnl
->jhdr
->jhdr_size
) != 0) {
1406 tbuffer_size
-= (tbuffer_size
% jnl
->jhdr
->jhdr_size
);
1409 jnl
->tbuffer_size
= tbuffer_size
;
1412 if (jnl
->tbuffer_size
> (jnl
->jhdr
->size
/ 2)) {
1413 jnl
->tbuffer_size
= (jnl
->jhdr
->size
/ 2);
1416 if (jnl
->tbuffer_size
> MAX_TRANSACTION_BUFFER_SIZE
) {
1417 jnl
->tbuffer_size
= MAX_TRANSACTION_BUFFER_SIZE
;
1420 jnl
->jhdr
->blhdr_size
= (jnl
->tbuffer_size
/ jnl
->jhdr
->jhdr_size
) * sizeof(block_info
);
1421 if (jnl
->jhdr
->blhdr_size
< phys_blksz
) {
1422 jnl
->jhdr
->blhdr_size
= phys_blksz
;
1423 } else if ((jnl
->jhdr
->blhdr_size
% phys_blksz
) != 0) {
1424 // have to round up so we're an even multiple of the physical block size
1425 jnl
->jhdr
->blhdr_size
= (jnl
->jhdr
->blhdr_size
+ (phys_blksz
- 1)) & ~(phys_blksz
- 1);
1432 get_io_info(struct vnode
*devvp
, size_t phys_blksz
, journal
*jnl
, struct vfs_context
*context
)
1435 off_t writeblockcnt
;
1436 off_t readmaxcnt
=0, tmp_readmaxcnt
;
1437 off_t writemaxcnt
=0, tmp_writemaxcnt
;
1438 off_t readsegcnt
, writesegcnt
;
1441 if (VNOP_IOCTL(devvp
, DKIOCGETFEATURES
, (caddr_t
)&features
, 0, context
) == 0) {
1442 if (features
& DK_FEATURE_FORCE_UNIT_ACCESS
) {
1443 const char *name
= vnode_name(devvp
);
1444 jnl
->flags
|= JOURNAL_DO_FUA_WRITES
;
1445 printf("jnl: %s: enabling FUA writes (features 0x%x)\n", name
? name
: "no-name-dev", features
);
1450 // First check the max read size via several different mechanisms...
1452 VNOP_IOCTL(devvp
, DKIOCGETMAXBYTECOUNTREAD
, (caddr_t
)&readmaxcnt
, 0, context
);
1454 if (VNOP_IOCTL(devvp
, DKIOCGETMAXBLOCKCOUNTREAD
, (caddr_t
)&readblockcnt
, 0, context
) == 0) {
1455 tmp_readmaxcnt
= readblockcnt
* phys_blksz
;
1456 if (readmaxcnt
== 0 || (readblockcnt
> 0 && tmp_readmaxcnt
< readmaxcnt
)) {
1457 readmaxcnt
= tmp_readmaxcnt
;
1461 if (VNOP_IOCTL(devvp
, DKIOCGETMAXSEGMENTCOUNTREAD
, (caddr_t
)&readsegcnt
, 0, context
)) {
1465 if (readsegcnt
> 0 && (readsegcnt
* PAGE_SIZE
) < readmaxcnt
) {
1466 readmaxcnt
= readsegcnt
* PAGE_SIZE
;
1469 if (readmaxcnt
== 0) {
1470 readmaxcnt
= 128 * 1024;
1471 } else if (readmaxcnt
> UINT32_MAX
) {
1472 readmaxcnt
= UINT32_MAX
;
1477 // Now check the max writes size via several different mechanisms...
1479 VNOP_IOCTL(devvp
, DKIOCGETMAXBYTECOUNTWRITE
, (caddr_t
)&writemaxcnt
, 0, context
);
1481 if (VNOP_IOCTL(devvp
, DKIOCGETMAXBLOCKCOUNTWRITE
, (caddr_t
)&writeblockcnt
, 0, context
) == 0) {
1482 tmp_writemaxcnt
= writeblockcnt
* phys_blksz
;
1483 if (writemaxcnt
== 0 || (writeblockcnt
> 0 && tmp_writemaxcnt
< writemaxcnt
)) {
1484 writemaxcnt
= tmp_writemaxcnt
;
1488 if (VNOP_IOCTL(devvp
, DKIOCGETMAXSEGMENTCOUNTWRITE
, (caddr_t
)&writesegcnt
, 0, context
)) {
1492 if (writesegcnt
> 0 && (writesegcnt
* PAGE_SIZE
) < writemaxcnt
) {
1493 writemaxcnt
= writesegcnt
* PAGE_SIZE
;
1496 if (writemaxcnt
== 0) {
1497 writemaxcnt
= 128 * 1024;
1498 } else if (writemaxcnt
> UINT32_MAX
) {
1499 writemaxcnt
= UINT32_MAX
;
1502 jnl
->max_read_size
= readmaxcnt
;
1503 jnl
->max_write_size
= writemaxcnt
;
1504 // printf("jnl: %s: max read/write: %lld k / %lld k\n",
1505 // jnl->jdev_name ? jnl->jdev_name : "unknown",
1506 // jnl->max_read_size/1024, jnl->max_write_size/1024);
1511 get_jdev_name(struct vnode
*jvp
)
1513 const char *jdev_name
;
1515 jdev_name
= vnode_name(jvp
);
1516 if (jdev_name
== NULL
) {
1517 jdev_name
= vfs_addname("unknown-dev", strlen("unknown-dev"), 0, 0);
1519 // this just bumps the refcount on the name so we have our own copy
1520 jdev_name
= vfs_addname(jdev_name
, strlen(jdev_name
), 0, 0);
1528 journal_create(struct vnode
*jvp
,
1532 size_t min_fs_blksz
,
1534 int32_t tbuffer_size
,
1535 void (*flush
)(void *arg
),
1539 uint32_t phys_blksz
, new_txn_base
;
1540 struct vfs_context context
;
1541 const char *jdev_name
;
1543 context
.vc_thread
= current_thread();
1544 context
.vc_ucred
= FSCRED
;
1546 jdev_name
= get_jdev_name(jvp
);
1548 /* Get the real physical block size. */
1549 if (VNOP_IOCTL(jvp
, DKIOCGETBLOCKSIZE
, (caddr_t
)&phys_blksz
, 0, &context
)) {
1553 if (journal_size
< (256*1024) || journal_size
> (1024*1024*1024)) {
1554 printf("jnl: create: journal size %lld looks bogus.\n", journal_size
);
1558 if (phys_blksz
> min_fs_blksz
) {
1559 printf("jnl: %s: create: error: phys blksize %u bigger than min fs blksize %zd\n",
1560 jdev_name
, phys_blksz
, min_fs_blksz
);
1564 if ((journal_size
% phys_blksz
) != 0) {
1565 printf("jnl: %s: create: journal size 0x%llx is not an even multiple of block size 0x%ux\n",
1566 jdev_name
, journal_size
, phys_blksz
);
1571 MALLOC_ZONE(jnl
, struct journal
*, sizeof(struct journal
), M_JNL_JNL
, M_WAITOK
);
1572 memset(jnl
, 0, sizeof(*jnl
));
1575 jnl
->jdev_offset
= offset
;
1578 jnl
->flush_arg
= arg
;
1579 jnl
->flags
= (flags
& JOURNAL_OPTION_FLAGS_MASK
);
1580 jnl
->jdev_name
= jdev_name
;
1581 lck_mtx_init(&jnl
->old_start_lock
, jnl_mutex_group
, jnl_lock_attr
);
1583 get_io_info(jvp
, phys_blksz
, jnl
, &context
);
1585 if (kmem_alloc(kernel_map
, (vm_offset_t
*)&jnl
->header_buf
, phys_blksz
)) {
1586 printf("jnl: %s: create: could not allocate space for header buffer (%u bytes)\n", jdev_name
, phys_blksz
);
1587 goto bad_kmem_alloc
;
1589 jnl
->header_buf_size
= phys_blksz
;
1591 jnl
->jhdr
= (journal_header
*)jnl
->header_buf
;
1592 memset(jnl
->jhdr
, 0, sizeof(journal_header
));
1594 // we have to set this up here so that do_journal_io() will work
1595 jnl
->jhdr
->jhdr_size
= phys_blksz
;
1598 // We try and read the journal header to see if there is already one
1599 // out there. If there is, it's possible that it has transactions
1600 // in it that we might replay if we happen to pick a sequence number
1601 // that is a little less than the old one, there is a crash and the
1602 // last txn written ends right at the start of a txn from the previous
1603 // incarnation of this file system. If all that happens we would
1604 // replay the transactions from the old file system and that would
1605 // destroy your disk. Although it is extremely unlikely for all those
1606 // conditions to happen, the probability is non-zero and the result is
1607 // severe - you lose your file system. Therefore if we find a valid
1608 // journal header and the sequence number is non-zero we write junk
1609 // over the entire journal so that there is no way we will encounter
1610 // any old transactions. This is slow but should be a rare event
1611 // since most tools erase the journal.
1613 if ( read_journal_header(jnl
, jnl
->jhdr
, phys_blksz
) == phys_blksz
1614 && jnl
->jhdr
->magic
== JOURNAL_HEADER_MAGIC
1615 && jnl
->jhdr
->sequence_num
!= 0) {
1617 new_txn_base
= (jnl
->jhdr
->sequence_num
+ (journal_size
/ phys_blksz
) + (random() % 16384)) & 0x00ffffff;
1618 printf("jnl: create: avoiding old sequence number 0x%x (0x%x)\n", jnl
->jhdr
->sequence_num
, new_txn_base
);
1624 for(i
=1; i
< journal_size
/ phys_blksz
; i
++) {
1627 // we don't really care what data we write just so long
1628 // as it's not a valid transaction header. since we have
1629 // the header_buf sitting around we'll use that.
1630 write_journal_data(jnl
, &pos
, jnl
->header_buf
, phys_blksz
);
1632 printf("jnl: create: done clearing journal (i=%d)\n", i
);
1635 new_txn_base
= random() & 0x00ffffff;
1638 memset(jnl
->header_buf
, 0, phys_blksz
);
1640 jnl
->jhdr
->magic
= JOURNAL_HEADER_MAGIC
;
1641 jnl
->jhdr
->endian
= ENDIAN_MAGIC
;
1642 jnl
->jhdr
->start
= phys_blksz
; // start at block #1, block #0 is for the jhdr itself
1643 jnl
->jhdr
->end
= phys_blksz
;
1644 jnl
->jhdr
->size
= journal_size
;
1645 jnl
->jhdr
->jhdr_size
= phys_blksz
;
1646 size_up_tbuffer(jnl
, tbuffer_size
, phys_blksz
);
1648 jnl
->active_start
= jnl
->jhdr
->start
;
1650 // XXXdbg - for testing you can force the journal to wrap around
1651 // jnl->jhdr->start = jnl->jhdr->size - (phys_blksz*3);
1652 // jnl->jhdr->end = jnl->jhdr->size - (phys_blksz*3);
1654 jnl
->jhdr
->sequence_num
= new_txn_base
;
1656 lck_mtx_init(&jnl
->jlock
, jnl_mutex_group
, jnl_lock_attr
);
1658 if (write_journal_header(jnl
, 1) != 0) {
1659 printf("jnl: %s: journal_create: failed to write journal header.\n", jdev_name
);
1667 kmem_free(kernel_map
, (vm_offset_t
)jnl
->header_buf
, phys_blksz
);
1670 vfs_removename(jdev_name
);
1673 FREE_ZONE(jnl
, sizeof(struct journal
), M_JNL_JNL
);
1679 journal_open(struct vnode
*jvp
,
1683 size_t min_fs_blksz
,
1685 int32_t tbuffer_size
,
1686 void (*flush
)(void *arg
),
1690 uint32_t orig_blksz
=0;
1691 uint32_t phys_blksz
;
1692 int orig_checksum
, checksum
;
1693 struct vfs_context context
;
1694 const char *jdev_name
= get_jdev_name(jvp
);
1696 context
.vc_thread
= current_thread();
1697 context
.vc_ucred
= FSCRED
;
1699 /* Get the real physical block size. */
1700 if (VNOP_IOCTL(jvp
, DKIOCGETBLOCKSIZE
, (caddr_t
)&phys_blksz
, 0, &context
)) {
1704 if (phys_blksz
> min_fs_blksz
) {
1705 printf("jnl: %s: open: error: phys blksize %u bigger than min fs blksize %zd\n",
1706 jdev_name
, phys_blksz
, min_fs_blksz
);
1710 if (journal_size
< (256*1024) || journal_size
> (1024*1024*1024)) {
1711 printf("jnl: open: journal size %lld looks bogus.\n", journal_size
);
1715 if ((journal_size
% phys_blksz
) != 0) {
1716 printf("jnl: %s: open: journal size 0x%llx is not an even multiple of block size 0x%x\n",
1717 jdev_name
, journal_size
, phys_blksz
);
1721 MALLOC_ZONE(jnl
, struct journal
*, sizeof(struct journal
), M_JNL_JNL
, M_WAITOK
);
1722 memset(jnl
, 0, sizeof(*jnl
));
1725 jnl
->jdev_offset
= offset
;
1728 jnl
->flush_arg
= arg
;
1729 jnl
->flags
= (flags
& JOURNAL_OPTION_FLAGS_MASK
);
1730 jnl
->jdev_name
= jdev_name
;
1731 lck_mtx_init(&jnl
->old_start_lock
, jnl_mutex_group
, jnl_lock_attr
);
1733 get_io_info(jvp
, phys_blksz
, jnl
, &context
);
1735 if (kmem_alloc(kernel_map
, (vm_offset_t
*)&jnl
->header_buf
, phys_blksz
)) {
1736 printf("jnl: %s: create: could not allocate space for header buffer (%u bytes)\n", jdev_name
, phys_blksz
);
1737 goto bad_kmem_alloc
;
1739 jnl
->header_buf_size
= phys_blksz
;
1741 jnl
->jhdr
= (journal_header
*)jnl
->header_buf
;
1742 memset(jnl
->jhdr
, 0, sizeof(journal_header
));
1744 // we have to set this up here so that do_journal_io() will work
1745 jnl
->jhdr
->jhdr_size
= phys_blksz
;
1747 if (read_journal_header(jnl
, jnl
->jhdr
, phys_blksz
) != phys_blksz
) {
1748 printf("jnl: %s: open: could not read %u bytes for the journal header.\n",
1749 jdev_name
, phys_blksz
);
1753 orig_checksum
= jnl
->jhdr
->checksum
;
1754 jnl
->jhdr
->checksum
= 0;
1756 if (jnl
->jhdr
->magic
== SWAP32(JOURNAL_HEADER_MAGIC
)) {
1757 // do this before the swap since it's done byte-at-a-time
1758 orig_checksum
= SWAP32(orig_checksum
);
1759 checksum
= calc_checksum((char *)jnl
->jhdr
, JOURNAL_HEADER_CKSUM_SIZE
);
1760 swap_journal_header(jnl
);
1761 jnl
->flags
|= JOURNAL_NEED_SWAP
;
1763 checksum
= calc_checksum((char *)jnl
->jhdr
, JOURNAL_HEADER_CKSUM_SIZE
);
1766 if (jnl
->jhdr
->magic
!= JOURNAL_HEADER_MAGIC
&& jnl
->jhdr
->magic
!= OLD_JOURNAL_HEADER_MAGIC
) {
1767 printf("jnl: %s: open: journal magic is bad (0x%x != 0x%x)\n",
1768 jnl
->jdev_name
, jnl
->jhdr
->magic
, JOURNAL_HEADER_MAGIC
);
1772 // only check if we're the current journal header magic value
1773 if (jnl
->jhdr
->magic
== JOURNAL_HEADER_MAGIC
) {
1775 if (orig_checksum
!= checksum
) {
1776 printf("jnl: %s: open: journal checksum is bad (0x%x != 0x%x)\n",
1777 jdev_name
, orig_checksum
, checksum
);
1783 // XXXdbg - convert old style magic numbers to the new one
1784 if (jnl
->jhdr
->magic
== OLD_JOURNAL_HEADER_MAGIC
) {
1785 jnl
->jhdr
->magic
= JOURNAL_HEADER_MAGIC
;
1788 if (phys_blksz
!= (size_t)jnl
->jhdr
->jhdr_size
&& jnl
->jhdr
->jhdr_size
!= 0) {
1790 * The volume has probably been resized (such that we had to adjust the
1791 * logical sector size), or copied to media with a different logical
1792 * sector size. If the journal is empty, then just switch to the
1793 * current logical sector size. If the journal is not empty, then
1794 * fail to open the journal.
1797 if (jnl
->jhdr
->start
== jnl
->jhdr
->end
) {
1798 printf("jnl: %s: open: changing journal header size from %d to %u\n",
1799 jdev_name
, jnl
->jhdr
->jhdr_size
, phys_blksz
);
1800 jnl
->jhdr
->jhdr_size
= phys_blksz
;
1801 if (write_journal_header(jnl
, 1)) {
1802 printf("jnl: %s: open: failed to update journal header size\n", jdev_name
);
1806 printf("jnl: %s: open: phys_blksz %u does not match journal header size %d, and journal is not empty!\n",
1807 jdev_name
, phys_blksz
, jnl
->jhdr
->jhdr_size
);
1812 if ( jnl
->jhdr
->start
<= 0
1813 || jnl
->jhdr
->start
> jnl
->jhdr
->size
1814 || jnl
->jhdr
->start
> 1024*1024*1024) {
1815 printf("jnl: %s: open: jhdr start looks bad (0x%llx max size 0x%llx)\n",
1816 jdev_name
, jnl
->jhdr
->start
, jnl
->jhdr
->size
);
1820 if ( jnl
->jhdr
->end
<= 0
1821 || jnl
->jhdr
->end
> jnl
->jhdr
->size
1822 || jnl
->jhdr
->end
> 1024*1024*1024) {
1823 printf("jnl: %s: open: jhdr end looks bad (0x%llx max size 0x%llx)\n",
1824 jdev_name
, jnl
->jhdr
->end
, jnl
->jhdr
->size
);
1828 if (jnl
->jhdr
->size
< (256*1024) || jnl
->jhdr
->size
> 1024*1024*1024) {
1829 printf("jnl: %s: open: jhdr size looks bad (0x%llx)\n", jdev_name
, jnl
->jhdr
->size
);
1833 // XXXdbg - can't do these checks because hfs writes all kinds of
1834 // non-uniform sized blocks even on devices that have a block size
1835 // that is larger than 512 bytes (i.e. optical media w/2k blocks).
1836 // therefore these checks will fail and so we just have to punt and
1837 // do more relaxed checking...
1838 // XXXdbg if ((jnl->jhdr->start % jnl->jhdr->jhdr_size) != 0) {
1839 if ((jnl
->jhdr
->start
% 512) != 0) {
1840 printf("jnl: %s: open: journal start (0x%llx) not a multiple of 512?\n",
1841 jdev_name
, jnl
->jhdr
->start
);
1845 //XXXdbg if ((jnl->jhdr->end % jnl->jhdr->jhdr_size) != 0) {
1846 if ((jnl
->jhdr
->end
% 512) != 0) {
1847 printf("jnl: %s: open: journal end (0x%llx) not a multiple of block size (0x%x)?\n",
1848 jdev_name
, jnl
->jhdr
->end
, jnl
->jhdr
->jhdr_size
);
1852 // take care of replaying the journal if necessary
1853 if (flags
& JOURNAL_RESET
) {
1854 printf("jnl: %s: journal start/end pointers reset! (jnl %p; s 0x%llx e 0x%llx)\n",
1855 jdev_name
, jnl
, jnl
->jhdr
->start
, jnl
->jhdr
->end
);
1856 jnl
->jhdr
->start
= jnl
->jhdr
->end
;
1857 } else if (replay_journal(jnl
) != 0) {
1858 printf("jnl: %s: journal_open: Error replaying the journal!\n", jdev_name
);
1862 if (orig_blksz
!= 0) {
1863 VNOP_IOCTL(jvp
, DKIOCSETBLOCKSIZE
, (caddr_t
)&orig_blksz
, FWRITE
, &context
);
1864 phys_blksz
= orig_blksz
;
1865 if (orig_blksz
< (uint32_t)jnl
->jhdr
->jhdr_size
) {
1866 printf("jnl: %s: open: jhdr_size is %d but orig phys blk size is %d. switching.\n",
1867 jdev_name
, jnl
->jhdr
->jhdr_size
, orig_blksz
);
1869 jnl
->jhdr
->jhdr_size
= orig_blksz
;
1873 // make sure this is in sync!
1874 jnl
->active_start
= jnl
->jhdr
->start
;
1876 // set this now, after we've replayed the journal
1877 size_up_tbuffer(jnl
, tbuffer_size
, phys_blksz
);
1879 if ((off_t
)(jnl
->jhdr
->blhdr_size
/sizeof(block_info
)-1) > (jnl
->jhdr
->size
/jnl
->jhdr
->jhdr_size
)) {
1880 printf("jnl: %s: open: jhdr size and blhdr size are not compatible (0x%llx, %d, %d)\n", jdev_name
, jnl
->jhdr
->size
,
1881 jnl
->jhdr
->blhdr_size
, jnl
->jhdr
->jhdr_size
);
1885 lck_mtx_init(&jnl
->jlock
, jnl_mutex_group
, jnl_lock_attr
);
1890 if (orig_blksz
!= 0) {
1891 phys_blksz
= orig_blksz
;
1892 VNOP_IOCTL(jvp
, DKIOCSETBLOCKSIZE
, (caddr_t
)&orig_blksz
, FWRITE
, &context
);
1894 kmem_free(kernel_map
, (vm_offset_t
)jnl
->header_buf
, phys_blksz
);
1897 vfs_removename(jdev_name
);
1899 FREE_ZONE(jnl
, sizeof(struct journal
), M_JNL_JNL
);
1905 journal_is_clean(struct vnode
*jvp
,
1909 size_t min_fs_block_size
)
1912 uint32_t phys_blksz
;
1914 int orig_checksum
, checksum
;
1915 struct vfs_context context
;
1916 const char *jdev_name
= get_jdev_name(jvp
);
1918 context
.vc_thread
= current_thread();
1919 context
.vc_ucred
= FSCRED
;
1921 /* Get the real physical block size. */
1922 if (VNOP_IOCTL(jvp
, DKIOCGETBLOCKSIZE
, (caddr_t
)&phys_blksz
, 0, &context
)) {
1923 printf("jnl: %s: is_clean: failed to get device block size.\n", jdev_name
);
1927 if (phys_blksz
> (uint32_t)min_fs_block_size
) {
1928 printf("jnl: %s: is_clean: error: phys blksize %d bigger than min fs blksize %zd\n",
1929 jdev_name
, phys_blksz
, min_fs_block_size
);
1933 if (journal_size
< (256*1024) || journal_size
> (1024*1024*1024)) {
1934 printf("jnl: is_clean: journal size %lld looks bogus.\n", journal_size
);
1938 if ((journal_size
% phys_blksz
) != 0) {
1939 printf("jnl: %s: is_clean: journal size 0x%llx is not an even multiple of block size 0x%x\n",
1940 jdev_name
, journal_size
, phys_blksz
);
1944 memset(&jnl
, 0, sizeof(jnl
));
1946 if (kmem_alloc(kernel_map
, (vm_offset_t
*)&jnl
.header_buf
, phys_blksz
)) {
1947 printf("jnl: %s: is_clean: could not allocate space for header buffer (%d bytes)\n", jdev_name
, phys_blksz
);
1950 jnl
.header_buf_size
= phys_blksz
;
1952 get_io_info(jvp
, phys_blksz
, &jnl
, &context
);
1954 jnl
.jhdr
= (journal_header
*)jnl
.header_buf
;
1955 memset(jnl
.jhdr
, 0, sizeof(journal_header
));
1958 jnl
.jdev_offset
= offset
;
1961 // we have to set this up here so that do_journal_io() will work
1962 jnl
.jhdr
->jhdr_size
= phys_blksz
;
1964 if (read_journal_header(&jnl
, jnl
.jhdr
, phys_blksz
) != (unsigned)phys_blksz
) {
1965 printf("jnl: %s: is_clean: could not read %d bytes for the journal header.\n",
1966 jdev_name
, phys_blksz
);
1971 orig_checksum
= jnl
.jhdr
->checksum
;
1972 jnl
.jhdr
->checksum
= 0;
1974 if (jnl
.jhdr
->magic
== SWAP32(JOURNAL_HEADER_MAGIC
)) {
1975 // do this before the swap since it's done byte-at-a-time
1976 orig_checksum
= SWAP32(orig_checksum
);
1977 checksum
= calc_checksum((char *)jnl
.jhdr
, JOURNAL_HEADER_CKSUM_SIZE
);
1978 swap_journal_header(&jnl
);
1979 jnl
.flags
|= JOURNAL_NEED_SWAP
;
1981 checksum
= calc_checksum((char *)jnl
.jhdr
, JOURNAL_HEADER_CKSUM_SIZE
);
1984 if (jnl
.jhdr
->magic
!= JOURNAL_HEADER_MAGIC
&& jnl
.jhdr
->magic
!= OLD_JOURNAL_HEADER_MAGIC
) {
1985 printf("jnl: %s: is_clean: journal magic is bad (0x%x != 0x%x)\n",
1986 jdev_name
, jnl
.jhdr
->magic
, JOURNAL_HEADER_MAGIC
);
1991 if (orig_checksum
!= checksum
) {
1992 printf("jnl: %s: is_clean: journal checksum is bad (0x%x != 0x%x)\n", jdev_name
, orig_checksum
, checksum
);
1998 // if the start and end are equal then the journal is clean.
1999 // otherwise it's not clean and therefore an error.
2001 if (jnl
.jhdr
->start
== jnl
.jhdr
->end
) {
2004 ret
= EBUSY
; // so the caller can differentiate an invalid journal from a "busy" one
2008 kmem_free(kernel_map
, (vm_offset_t
)jnl
.header_buf
, phys_blksz
);
2010 vfs_removename(jdev_name
);
2020 journal_close(journal
*jnl
)
2022 volatile off_t
*start
, *end
;
2027 // set this before doing anything that would block so that
2028 // we start tearing things down properly.
2030 jnl
->flags
|= JOURNAL_CLOSE_PENDING
;
2032 if (jnl
->owner
!= current_thread()) {
2037 // only write stuff to disk if the journal is still valid
2039 if ((jnl
->flags
& JOURNAL_INVALID
) == 0) {
2041 if (jnl
->active_tr
) {
2042 journal_end_transaction(jnl
);
2045 // flush any buffered transactions
2047 transaction
*tr
= jnl
->cur_tr
;
2050 end_transaction(tr
, 1, NULL
, NULL
); // force it to get flushed
2053 //start = &jnl->jhdr->start;
2054 start
= &jnl
->active_start
;
2055 end
= &jnl
->jhdr
->end
;
2057 while (*start
!= *end
&& counter
++ < 5000) {
2058 //printf("jnl: close: flushing the buffer cache (start 0x%llx end 0x%llx)\n", *start, *end);
2060 jnl
->flush(jnl
->flush_arg
);
2062 tsleep((caddr_t
)jnl
, PRIBIO
, "jnl_close", 2);
2065 if (*start
!= *end
) {
2066 printf("jnl: %s: close: buffer flushing didn't seem to flush out all the transactions! (0x%llx - 0x%llx)\n",
2067 jnl
->jdev_name
, *start
, *end
);
2070 // make sure this is in sync when we close the journal
2071 jnl
->jhdr
->start
= jnl
->active_start
;
2073 // if this fails there's not much we can do at this point...
2074 write_journal_header(jnl
, 1);
2076 // if we're here the journal isn't valid any more.
2077 // so make sure we don't leave any locked blocks lying around
2078 printf("jnl: %s: close: journal %p, is invalid. aborting outstanding transactions\n", jnl
->jdev_name
, jnl
);
2079 if (jnl
->active_tr
|| jnl
->cur_tr
) {
2081 if (jnl
->active_tr
) {
2082 tr
= jnl
->active_tr
;
2083 jnl
->active_tr
= NULL
;
2089 abort_transaction(jnl
, tr
);
2090 if (jnl
->active_tr
|| jnl
->cur_tr
) {
2091 panic("jnl: %s: close: jnl @ %p had both an active and cur tr\n", jnl
->jdev_name
, jnl
);
2096 free_old_stuff(jnl
);
2098 kmem_free(kernel_map
, (vm_offset_t
)jnl
->header_buf
, jnl
->header_buf_size
);
2099 jnl
->jhdr
= (void *)0xbeefbabe;
2101 if (jnl
->jdev_name
) {
2102 vfs_removename(jnl
->jdev_name
);
2105 FREE_ZONE(jnl
, sizeof(struct journal
), M_JNL_JNL
);
2109 dump_journal(journal
*jnl
)
2113 printf("journal for dev %s:", jnl
->jdev_name
);
2114 printf(" jdev_offset %.8llx\n", jnl
->jdev_offset
);
2115 printf(" magic: 0x%.8x\n", jnl
->jhdr
->magic
);
2116 printf(" start: 0x%.8llx\n", jnl
->jhdr
->start
);
2117 printf(" end: 0x%.8llx\n", jnl
->jhdr
->end
);
2118 printf(" size: 0x%.8llx\n", jnl
->jhdr
->size
);
2119 printf(" blhdr size: %d\n", jnl
->jhdr
->blhdr_size
);
2120 printf(" jhdr size: %d\n", jnl
->jhdr
->jhdr_size
);
2121 printf(" chksum: 0x%.8x\n", jnl
->jhdr
->checksum
);
2123 printf(" completed transactions:\n");
2124 for(ctr
=jnl
->completed_trs
; ctr
; ctr
=ctr
->next
) {
2125 printf(" 0x%.8llx - 0x%.8llx\n", ctr
->journal_start
, ctr
->journal_end
);
2132 free_space(journal
*jnl
)
2134 off_t free_space_offset
;
2136 if (jnl
->jhdr
->start
< jnl
->jhdr
->end
) {
2137 free_space_offset
= jnl
->jhdr
->size
- (jnl
->jhdr
->end
- jnl
->jhdr
->start
) - jnl
->jhdr
->jhdr_size
;
2138 } else if (jnl
->jhdr
->start
> jnl
->jhdr
->end
) {
2139 free_space_offset
= jnl
->jhdr
->start
- jnl
->jhdr
->end
;
2141 // journal is completely empty
2142 free_space_offset
= jnl
->jhdr
->size
- jnl
->jhdr
->jhdr_size
;
2145 return free_space_offset
;
2150 // The journal must be locked on entry to this function.
2151 // The "desired_size" is in bytes.
2154 check_free_space(journal
*jnl
, int desired_size
)
2159 //printf("jnl: check free space (desired 0x%x, avail 0x%Lx)\n",
2160 // desired_size, free_space(jnl));
2163 int old_start_empty
;
2165 if (counter
++ == 5000) {
2167 panic("jnl: check_free_space: buffer flushing isn't working "
2168 "(jnl @ %p s %lld e %lld f %lld [active start %lld]).\n", jnl
,
2169 jnl
->jhdr
->start
, jnl
->jhdr
->end
, free_space(jnl
), jnl
->active_start
);
2171 if (counter
> 7500) {
2172 printf("jnl: %s: check_free_space: giving up waiting for free space.\n", jnl
->jdev_name
);
2176 // make sure there's space in the journal to hold this transaction
2177 if (free_space(jnl
) > desired_size
&& jnl
->old_start
[0] == 0) {
2181 // here's where we lazily bump up jnl->jhdr->start. we'll consume
2182 // entries until there is enough space for the next transaction.
2184 old_start_empty
= 1;
2186 for(i
=0; i
< sizeof(jnl
->old_start
)/sizeof(jnl
->old_start
[0]); i
++) {
2190 while (jnl
->old_start
[i
] & 0x8000000000000000LL
) {
2191 if (lcl_counter
++ > 1000) {
2192 panic("jnl: check_free_space: tr starting @ 0x%llx not flushing (jnl %p).\n",
2193 jnl
->old_start
[i
], jnl
);
2196 unlock_oldstart(jnl
);
2198 jnl
->flush(jnl
->flush_arg
);
2200 tsleep((caddr_t
)jnl
, PRIBIO
, "check_free_space1", 1);
2204 if (jnl
->old_start
[i
] == 0) {
2208 old_start_empty
= 0;
2209 jnl
->jhdr
->start
= jnl
->old_start
[i
];
2210 jnl
->old_start
[i
] = 0;
2211 if (free_space(jnl
) > desired_size
) {
2212 unlock_oldstart(jnl
);
2213 write_journal_header(jnl
, 1);
2218 unlock_oldstart(jnl
);
2220 // if we bumped the start, loop and try again
2221 if (i
< sizeof(jnl
->old_start
)/sizeof(jnl
->old_start
[0])) {
2223 } else if (old_start_empty
) {
2225 // if there is nothing in old_start anymore then we can
2226 // bump the jhdr->start to be the same as active_start
2227 // since it is possible there was only one very large
2228 // transaction in the old_start array. if we didn't do
2229 // this then jhdr->start would never get updated and we
2230 // would wind up looping until we hit the panic at the
2231 // start of the loop.
2233 jnl
->jhdr
->start
= jnl
->active_start
;
2234 write_journal_header(jnl
, 1);
2239 // if the file system gave us a flush function, call it to so that
2240 // it can flush some blocks which hopefully will cause some transactions
2241 // to complete and thus free up space in the journal.
2243 jnl
->flush(jnl
->flush_arg
);
2246 // wait for a while to avoid being cpu-bound (this will
2247 // put us to sleep for 10 milliseconds)
2248 tsleep((caddr_t
)jnl
, PRIBIO
, "check_free_space2", 1);
2255 * Allocate a new active transaction.
2258 journal_allocate_transaction(journal
*jnl
)
2262 MALLOC_ZONE(tr
, transaction
*, sizeof(transaction
), M_JNL_TR
, M_WAITOK
);
2263 memset(tr
, 0, sizeof(transaction
));
2265 tr
->tbuffer_size
= jnl
->tbuffer_size
;
2267 if (kmem_alloc(kernel_map
, (vm_offset_t
*)&tr
->tbuffer
, tr
->tbuffer_size
)) {
2268 FREE_ZONE(tr
, sizeof(transaction
), M_JNL_TR
);
2269 jnl
->active_tr
= NULL
;
2273 // journal replay code checksum check depends on this.
2274 memset(tr
->tbuffer
, 0, BLHDR_CHECKSUM_SIZE
);
2275 // Fill up the rest of the block with unimportant bytes (0x5a 'Z' chosen for visibility)
2276 memset(tr
->tbuffer
+ BLHDR_CHECKSUM_SIZE
, 0x5a, jnl
->jhdr
->blhdr_size
- BLHDR_CHECKSUM_SIZE
);
2278 tr
->blhdr
= (block_list_header
*)tr
->tbuffer
;
2279 tr
->blhdr
->max_blocks
= (jnl
->jhdr
->blhdr_size
/ sizeof(block_info
)) - 1;
2280 tr
->blhdr
->num_blocks
= 1; // accounts for this header block
2281 tr
->blhdr
->bytes_used
= jnl
->jhdr
->blhdr_size
;
2282 tr
->blhdr
->flags
= BLHDR_CHECK_CHECKSUMS
| BLHDR_FIRST_HEADER
;
2284 tr
->sequence_num
= ++jnl
->jhdr
->sequence_num
;
2286 tr
->total_bytes
= jnl
->jhdr
->blhdr_size
;
2289 jnl
->active_tr
= tr
;
2295 journal_start_transaction(journal
*jnl
)
2301 if (jnl
->flags
& JOURNAL_INVALID
) {
2305 if (jnl
->owner
== current_thread()) {
2306 if (jnl
->active_tr
== NULL
) {
2307 panic("jnl: start_tr: active_tr is NULL (jnl @ %p, owner %p, current_thread %p\n",
2308 jnl
, jnl
->owner
, current_thread());
2310 jnl
->nested_count
++;
2316 if (jnl
->owner
!= NULL
|| jnl
->nested_count
!= 0 || jnl
->active_tr
!= NULL
) {
2317 panic("jnl: start_tr: owner %p, nested count %d, active_tr %p jnl @ %p\n",
2318 jnl
->owner
, jnl
->nested_count
, jnl
->active_tr
, jnl
);
2321 jnl
->owner
= current_thread();
2322 jnl
->nested_count
= 1;
2324 free_old_stuff(jnl
);
2326 // make sure there's room in the journal
2327 if (free_space(jnl
) < jnl
->tbuffer_size
) {
2328 // this is the call that really waits for space to free up
2329 // as well as updating jnl->jhdr->start
2330 if (check_free_space(jnl
, jnl
->tbuffer_size
) != 0) {
2331 printf("jnl: %s: start transaction failed: no space\n", jnl
->jdev_name
);
2337 // if there's a buffered transaction, use it.
2339 jnl
->active_tr
= jnl
->cur_tr
;
2345 ret
= journal_allocate_transaction(jnl
);
2350 // printf("jnl: start_tr: owner 0x%x new tr @ 0x%x\n", jnl->owner, jnl->active_tr);
2356 jnl
->nested_count
= 0;
2357 unlock_journal(jnl
);
2363 journal_modify_block_start(journal
*jnl
, struct buf
*bp
)
2369 if (jnl
->flags
& JOURNAL_INVALID
) {
2373 // XXXdbg - for debugging I want this to be true. later it may
2374 // not be necessary.
2375 if ((buf_flags(bp
) & B_META
) == 0) {
2376 panic("jnl: modify_block_start: bp @ %p is not a meta-data block! (jnl %p)\n", bp
, jnl
);
2379 tr
= jnl
->active_tr
;
2380 CHECK_TRANSACTION(tr
);
2382 if (jnl
->owner
!= current_thread()) {
2383 panic("jnl: modify_block_start: called w/out a transaction! jnl %p, owner %p, curact %p\n",
2384 jnl
, jnl
->owner
, current_thread());
2387 free_old_stuff(jnl
);
2389 //printf("jnl: mod block start (bp 0x%x vp 0x%x l/blkno %qd/%qd bsz %d; total bytes %d)\n",
2390 // bp, buf_vnode(bp), buf_lblkno(bp), buf_blkno(bp), buf_size(bp), tr->total_bytes);
2392 // can't allow blocks that aren't an even multiple of the
2393 // underlying block size.
2394 if ((buf_size(bp
) % jnl
->jhdr
->jhdr_size
) != 0) {
2395 uint32_t phys_blksz
, bad
=0;
2397 if (VNOP_IOCTL(jnl
->jdev
, DKIOCGETBLOCKSIZE
, (caddr_t
)&phys_blksz
, 0, vfs_context_kernel())) {
2399 } else if (phys_blksz
!= (uint32_t)jnl
->jhdr
->jhdr_size
) {
2400 if (phys_blksz
< 512) {
2401 panic("jnl: mod block start: phys blksz %d is too small (%d, %d)\n",
2402 phys_blksz
, buf_size(bp
), jnl
->jhdr
->jhdr_size
);
2405 if ((buf_size(bp
) % phys_blksz
) != 0) {
2407 } else if (phys_blksz
< (uint32_t)jnl
->jhdr
->jhdr_size
) {
2408 jnl
->jhdr
->jhdr_size
= phys_blksz
;
2410 // the phys_blksz is now larger... need to realloc the jhdr
2411 char *new_header_buf
;
2413 printf("jnl: %s: phys blksz got bigger (was: %d/%d now %d)\n",
2414 jnl
->jdev_name
, jnl
->header_buf_size
, jnl
->jhdr
->jhdr_size
, phys_blksz
);
2415 if (kmem_alloc(kernel_map
, (vm_offset_t
*)&new_header_buf
, phys_blksz
)) {
2416 printf("jnl: modify_block_start: %s: create: phys blksz change (was %d, now %d) but could not allocate space for new header\n",
2417 jnl
->jdev_name
, jnl
->jhdr
->jhdr_size
, phys_blksz
);
2420 memcpy(new_header_buf
, jnl
->header_buf
, jnl
->header_buf_size
);
2421 memset(&new_header_buf
[jnl
->header_buf_size
], 0x18, (phys_blksz
- jnl
->header_buf_size
));
2422 kmem_free(kernel_map
, (vm_offset_t
)jnl
->header_buf
, jnl
->header_buf_size
);
2423 jnl
->header_buf
= new_header_buf
;
2424 jnl
->header_buf_size
= phys_blksz
;
2426 jnl
->jhdr
= (journal_header
*)jnl
->header_buf
;
2427 jnl
->jhdr
->jhdr_size
= phys_blksz
;
2435 panic("jnl: mod block start: bufsize %d not a multiple of block size %d\n",
2436 buf_size(bp
), jnl
->jhdr
->jhdr_size
);
2441 // make sure that this transaction isn't bigger than the whole journal
2442 if (tr
->total_bytes
+buf_size(bp
) >= (jnl
->jhdr
->size
- jnl
->jhdr
->jhdr_size
)) {
2443 panic("jnl: transaction too big (%d >= %lld bytes, bufsize %d, tr %p bp %p)\n",
2444 tr
->total_bytes
, (tr
->jnl
->jhdr
->size
- jnl
->jhdr
->jhdr_size
), buf_size(bp
), tr
, bp
);
2448 // if the block is dirty and not already locked we have to write
2449 // it out before we muck with it because it has data that belongs
2450 // (presumably) to another transaction.
2452 if ((buf_flags(bp
) & (B_DELWRI
| B_LOCKED
)) == B_DELWRI
) {
2454 if (buf_flags(bp
) & B_ASYNC
) {
2455 panic("modify_block_start: bp @ %p has async flag set!\n", bp
);
2458 // this will cause it to not be buf_brelse()'d
2459 buf_setflags(bp
, B_NORELSE
);
2462 buf_setflags(bp
, B_LOCKED
);
2468 journal_modify_block_abort(journal
*jnl
, struct buf
*bp
)
2471 block_list_header
*blhdr
;
2476 tr
= jnl
->active_tr
;
2479 // if there's no active transaction then we just want to
2480 // call buf_brelse() and return since this is just a block
2481 // that happened to be modified as part of another tr.
2488 if (jnl
->flags
& JOURNAL_INVALID
) {
2492 CHECK_TRANSACTION(tr
);
2494 if (jnl
->owner
!= current_thread()) {
2495 panic("jnl: modify_block_abort: called w/out a transaction! jnl %p, owner %p, curact %p\n",
2496 jnl
, jnl
->owner
, current_thread());
2499 free_old_stuff(jnl
);
2501 // printf("jnl: modify_block_abort: tr 0x%x bp 0x%x\n", jnl->active_tr, bp);
2503 // first check if it's already part of this transaction
2504 for(blhdr
=tr
->blhdr
; blhdr
; blhdr
=(block_list_header
*)((long)blhdr
->binfo
[0].bnum
)) {
2505 for(i
=1; i
< blhdr
->num_blocks
; i
++) {
2506 if (bp
== blhdr
->binfo
[i
].u
.bp
) {
2511 if (i
< blhdr
->num_blocks
) {
2517 // if blhdr is null, then this block has only had modify_block_start
2518 // called on it as part of the current transaction. that means that
2519 // it is ok to clear the LOCKED bit since it hasn't actually been
2520 // modified. if blhdr is non-null then modify_block_end was called
2521 // on it and so we need to keep it locked in memory.
2523 if (blhdr
== NULL
) {
2524 buf_clearflags(bp
, B_LOCKED
);
2533 journal_modify_block_end(journal
*jnl
, struct buf
*bp
, void (*func
)(struct buf
*bp
, void *arg
), void *arg
)
2536 int tbuffer_offset
=0;
2538 block_list_header
*blhdr
, *prev
=NULL
;
2543 if (jnl
->flags
& JOURNAL_INVALID
) {
2547 tr
= jnl
->active_tr
;
2548 CHECK_TRANSACTION(tr
);
2550 if (jnl
->owner
!= current_thread()) {
2551 panic("jnl: modify_block_end: called w/out a transaction! jnl %p, owner %p, curact %p\n",
2552 jnl
, jnl
->owner
, current_thread());
2555 free_old_stuff(jnl
);
2557 //printf("jnl: mod block end: (bp 0x%x vp 0x%x l/blkno %qd/%qd bsz %d, total bytes %d)\n",
2558 // bp, buf_vnode(bp), buf_lblkno(bp), buf_blkno(bp), buf_size(bp), tr->total_bytes);
2560 if ((buf_flags(bp
) & B_LOCKED
) == 0) {
2561 panic("jnl: modify_block_end: bp %p not locked! jnl @ %p\n", bp
, jnl
);
2564 // first check if it's already part of this transaction
2565 for(blhdr
=tr
->blhdr
; blhdr
; prev
=blhdr
,blhdr
=(block_list_header
*)((long)blhdr
->binfo
[0].bnum
)) {
2566 tbuffer_offset
= jnl
->jhdr
->blhdr_size
;
2568 for(i
=1; i
< blhdr
->num_blocks
; i
++) {
2569 if (bp
== blhdr
->binfo
[i
].u
.bp
) {
2572 if (blhdr
->binfo
[i
].bnum
!= (off_t
)-1) {
2573 tbuffer_offset
+= buf_size(blhdr
->binfo
[i
].u
.bp
);
2575 tbuffer_offset
+= blhdr
->binfo
[i
].u
.bi
.bsize
;
2579 if (i
< blhdr
->num_blocks
) {
2586 && (prev
->num_blocks
+1) <= prev
->max_blocks
2587 && (prev
->bytes_used
+buf_size(bp
)) <= (uint32_t)tr
->tbuffer_size
) {
2589 } else if (blhdr
== NULL
) {
2590 block_list_header
*nblhdr
;
2593 panic("jnl: modify block end: no way man, prev == NULL?!?, jnl %p, bp %p\n", jnl
, bp
);
2596 // we got to the end of the list, didn't find the block and there's
2597 // no room in the block_list_header pointed to by prev
2599 // we allocate another tbuffer and link it in at the end of the list
2600 // through prev->binfo[0].bnum. that's a skanky way to do things but
2601 // avoids having yet another linked list of small data structures to manage.
2603 if (kmem_alloc(kernel_map
, (vm_offset_t
*)&nblhdr
, tr
->tbuffer_size
)) {
2604 panic("jnl: end_tr: no space for new block tr @ %p (total bytes: %d)!\n",
2605 tr
, tr
->total_bytes
);
2608 // journal replay code checksum check depends on this.
2609 memset(nblhdr
, 0, BLHDR_CHECKSUM_SIZE
);
2610 // Fill up the rest of the block with unimportant bytes
2611 memset(nblhdr
+ BLHDR_CHECKSUM_SIZE
, 0x5a, jnl
->jhdr
->blhdr_size
- BLHDR_CHECKSUM_SIZE
);
2613 // initialize the new guy
2614 nblhdr
->max_blocks
= (jnl
->jhdr
->blhdr_size
/ sizeof(block_info
)) - 1;
2615 nblhdr
->num_blocks
= 1; // accounts for this header block
2616 nblhdr
->bytes_used
= jnl
->jhdr
->blhdr_size
;
2617 nblhdr
->flags
= BLHDR_CHECK_CHECKSUMS
;
2620 tr
->total_bytes
+= jnl
->jhdr
->blhdr_size
;
2622 // then link him in at the end
2623 prev
->binfo
[0].bnum
= (off_t
)((long)nblhdr
);
2625 // and finally switch to using the new guy
2627 tbuffer_offset
= jnl
->jhdr
->blhdr_size
;
2632 if ((i
+1) > blhdr
->max_blocks
) {
2633 panic("jnl: modify_block_end: i = %d, max_blocks %d\n", i
, blhdr
->max_blocks
);
2636 // if the function pointer is not set then copy the
2637 // block of data now. if the function pointer is set
2638 // the copy will happen after calling the callback in
2639 // end_transaction() just before it goes to disk.
2642 blkptr
= (char *)&((char *)blhdr
)[tbuffer_offset
];
2643 memcpy(blkptr
, (char *)0 + buf_dataptr(bp
), buf_size(bp
));
2646 // if this is true then this is a new block we haven't seen
2647 if (i
>= blhdr
->num_blocks
) {
2653 bsize
= buf_size(bp
);
2655 blhdr
->binfo
[i
].bnum
= (off_t
)(buf_blkno(bp
));
2656 blhdr
->binfo
[i
].u
.bp
= bp
;
2658 void *old_func
=NULL
, *old_arg
=NULL
;
2660 buf_setfilter(bp
, func
, arg
, &old_func
, &old_arg
);
2661 if (old_func
!= NULL
&& old_func
!= func
) {
2662 panic("jnl: modify_block_end: old func %p / arg %p (func %p)", old_func
, old_arg
, func
);
2666 blhdr
->bytes_used
+= bsize
;
2667 tr
->total_bytes
+= bsize
;
2669 blhdr
->num_blocks
++;
2677 journal_kill_block(journal
*jnl
, struct buf
*bp
)
2681 block_list_header
*blhdr
;
2686 if (jnl
->flags
& JOURNAL_INVALID
) {
2690 tr
= jnl
->active_tr
;
2691 CHECK_TRANSACTION(tr
);
2693 if (jnl
->owner
!= current_thread()) {
2694 panic("jnl: modify_block_end: called w/out a transaction! jnl %p, owner %p, curact %p\n",
2695 jnl
, jnl
->owner
, current_thread());
2698 free_old_stuff(jnl
);
2700 bflags
= buf_flags(bp
);
2702 if ( !(bflags
& B_LOCKED
))
2703 panic("jnl: modify_block_end: called with bp not B_LOCKED");
2706 * bp must be BL_BUSY and B_LOCKED
2708 // first check if it's already part of this transaction
2709 for(blhdr
=tr
->blhdr
; blhdr
; blhdr
=(block_list_header
*)((long)blhdr
->binfo
[0].bnum
)) {
2711 for(i
=1; i
< blhdr
->num_blocks
; i
++) {
2712 if (bp
== blhdr
->binfo
[i
].u
.bp
) {
2715 buf_clearflags(bp
, B_LOCKED
);
2717 // this undoes the vnode_ref() in journal_modify_block_end()
2719 vnode_rele_ext(vp
, 0, 1);
2721 // if the block has the DELWRI and FILTER bits sets, then
2722 // things are seriously weird. if it was part of another
2723 // transaction then journal_modify_block_start() should
2724 // have force it to be written.
2726 //if ((bflags & B_DELWRI) && (bflags & B_FILTER)) {
2727 // panic("jnl: kill block: this defies all logic! bp 0x%x\n", bp);
2729 tr
->num_killed
+= buf_size(bp
);
2731 blhdr
->binfo
[i
].bnum
= (off_t
)-1;
2732 blhdr
->binfo
[i
].u
.bp
= NULL
;
2733 blhdr
->binfo
[i
].u
.bi
.bsize
= buf_size(bp
);
2735 buf_markinvalid(bp
);
2742 if (i
< blhdr
->num_blocks
) {
2752 journal_binfo_cmp(const void *a
, const void *b
)
2754 const block_info
*bi_a
= (const struct block_info
*)a
;
2755 const block_info
*bi_b
= (const struct block_info
*)b
;
2758 if (bi_a
->bnum
== (off_t
)-1) {
2761 if (bi_b
->bnum
== (off_t
)-1) {
2765 // don't have to worry about negative block
2766 // numbers so this is ok to do.
2768 res
= (buf_blkno(bi_a
->u
.bp
) - buf_blkno(bi_b
->u
.bp
));
2775 * End a transaction. If the transaction is small enough, and we're not forcing
2776 * a write to disk, the "active" transaction becomes the "current" transaction,
2777 * and will be reused for the next transaction that is started (group commit).
2779 * If the transaction gets written to disk (because force_it is true, or no
2780 * group commit, or the transaction is sufficiently full), the blocks get
2781 * written into the journal first, then the are written asynchronously. When
2782 * those async writes complete, the transaction can be freed and removed from
2785 * An optional callback can be supplied. If given, it is called after the
2786 * the blocks have been written to the journal, but before the async writes
2787 * of those blocks to their normal on-disk locations. This is used by
2788 * journal_relocate so that the location of the journal can be changed and
2789 * flushed to disk before the blocks get written to their normal locations.
2790 * Note that the callback is only called if the transaction gets written to
2791 * the journal during this end_transaction call; you probably want to set the
2795 * tr Transaction to add to the journal
2796 * force_it If true, force this transaction to the on-disk journal immediately.
2797 * callback See description above. Pass NULL for no callback.
2798 * callback_arg Argument passed to callback routine.
2802 * -1 An error occurred. The journal is marked invalid.
2805 end_transaction(transaction
*tr
, int force_it
, errno_t (*callback
)(void*), void *callback_arg
)
2810 journal
*jnl
= tr
->jnl
;
2811 struct buf
*bp
, **bparray
;
2812 block_list_header
*blhdr
=NULL
, *next
=NULL
;
2813 size_t tbuffer_offset
;
2816 panic("jnl: jnl @ %p already has cur_tr %p, new tr: %p\n",
2817 jnl
, jnl
->cur_tr
, tr
);
2820 // if there weren't any modified blocks in the transaction
2821 // just save off the transaction pointer and return.
2822 if (tr
->total_bytes
== jnl
->jhdr
->blhdr_size
) {
2827 // if our transaction buffer isn't very full, just hang
2828 // on to it and don't actually flush anything. this is
2829 // what is known as "group commit". we will flush the
2830 // transaction buffer if it's full or if we have more than
2831 // one of them so we don't start hogging too much memory.
2834 && (jnl
->flags
& JOURNAL_NO_GROUP_COMMIT
) == 0
2835 && tr
->num_blhdrs
< 3
2836 && (tr
->total_bytes
<= ((tr
->tbuffer_size
*tr
->num_blhdrs
) - tr
->tbuffer_size
/8))) {
2843 // if we're here we're going to flush the transaction buffer to disk.
2844 // make sure there is room in the journal first.
2845 check_free_space(jnl
, tr
->total_bytes
);
2847 // range check the end index
2848 if (jnl
->jhdr
->end
<= 0 || jnl
->jhdr
->end
> jnl
->jhdr
->size
) {
2849 panic("jnl: end_transaction: end is bogus 0x%llx (sz 0x%llx)\n",
2850 jnl
->jhdr
->end
, jnl
->jhdr
->size
);
2853 // this transaction starts where the current journal ends
2854 tr
->journal_start
= jnl
->jhdr
->end
;
2855 end
= jnl
->jhdr
->end
;
2858 // if the first entry in old_start[] isn't free yet, loop calling the
2859 // file system flush routine until it is (or we panic).
2863 while ((jnl
->old_start
[0] & 0x8000000000000000LL
) != 0) {
2865 unlock_oldstart(jnl
);
2868 jnl
->flush(jnl
->flush_arg
);
2871 // yield the cpu so others can get in to clear the lock bit
2872 (void)tsleep((void *)jnl
, PRIBIO
, "jnl-old-start-sleep", 1);
2877 panic("jnl: transaction that started at 0x%llx is not completing! jnl %p\n",
2878 jnl
->old_start
[0] & (~0x8000000000000000LL
), jnl
);
2883 // slide everyone else down and put our latest guy in the last
2884 // entry in the old_start array
2887 /* Because old_start is locked above, we can cast away the volatile qualifier before passing it to memcpy. */
2888 memcpy(__CAST_AWAY_QUALIFIER(&jnl
->old_start
[0], volatile, void *), __CAST_AWAY_QUALIFIER(&jnl
->old_start
[1], volatile, void *), sizeof(jnl
->old_start
)-sizeof(jnl
->old_start
[0]));
2889 jnl
->old_start
[sizeof(jnl
->old_start
)/sizeof(jnl
->old_start
[0]) - 1] = tr
->journal_start
| 0x8000000000000000LL
;
2891 unlock_oldstart(jnl
);
2894 // for each block, make sure that the physical block # is set
2895 for(blhdr
=tr
->blhdr
; blhdr
; blhdr
=next
) {
2898 tbuffer_offset
= jnl
->jhdr
->blhdr_size
;
2899 for(i
=1; i
< blhdr
->num_blocks
; i
++) {
2904 bp
= blhdr
->binfo
[i
].u
.bp
;
2906 // if this block has a callback function set, call
2907 // it now and then copy the data from the bp into
2909 if (blhdr
->binfo
[i
].bnum
!= (off_t
)-1) {
2910 void (*func
)(struct buf
*, void *);
2914 panic("jnl: inconsistent binfo (NULL bp w/bnum %lld; jnl @ %p, tr %p)\n",
2915 blhdr
->binfo
[i
].bnum
, jnl
, tr
);
2918 buf_setfilter(bp
, NULL
, NULL
, (void **)&func
, &arg
);
2921 // acquire the bp here so that we can safely
2922 // mess around with its data. buf_acquire()
2923 // will return EAGAIN if the buffer was busy,
2924 // so loop trying again.
2926 errno
= buf_acquire(bp
, 0, 0, 0);
2927 } while (errno
== EAGAIN
);
2931 // call the hook function and then copy the
2932 // data into the transaction buffer...
2935 blkptr
= (char *)&((char *)blhdr
)[tbuffer_offset
];
2936 memcpy(blkptr
, (char *)buf_dataptr(bp
), buf_size(bp
));
2940 panic("could not acquire bp %p (err %d)\n", bp
, errno
);
2944 } else { // bnum == -1, only true if a block was "killed"
2946 tbuffer_offset
+= blhdr
->binfo
[i
].u
.bi
.bsize
;
2950 tbuffer_offset
+= buf_size(bp
);
2953 blkno
= buf_blkno(bp
);
2954 lblkno
= buf_lblkno(bp
);
2956 if (vp
== NULL
&& lblkno
== blkno
) {
2957 printf("jnl: %s: end_tr: bad news! bp @ %p w/null vp and l/blkno = %qd/%qd. aborting the transaction (tr %p jnl %p).\n",
2958 jnl
->jdev_name
, bp
, lblkno
, blkno
, tr
, jnl
);
2962 // if the lblkno is the same as blkno and this bp isn't
2963 // associated with the underlying file system device then
2964 // we need to call bmap() to get the actual physical block.
2966 if ((lblkno
== blkno
) && (vp
!= jnl
->fsdev
)) {
2968 size_t contig_bytes
;
2970 if (VNOP_BLKTOOFF(vp
, lblkno
, &f_offset
)) {
2971 printf("jnl: %s: end_tr: vnop_blktooff failed @ %p, jnl %p\n", jnl
->jdev_name
, bp
, jnl
);
2974 if (VNOP_BLOCKMAP(vp
, f_offset
, buf_count(bp
), &blkno
, &contig_bytes
, NULL
, 0, NULL
)) {
2975 printf("jnl: %s: end_tr: can't blockmap the bp @ %p, jnl %p\n", jnl
->jdev_name
, bp
, jnl
);
2978 if ((uint32_t)contig_bytes
< buf_count(bp
)) {
2979 printf("jnl: %s: end_tr: blk not physically contiguous on disk@ %p, jnl %p\n", jnl
->jdev_name
, bp
, jnl
);
2982 buf_setblkno(bp
, blkno
);
2984 // update this so we write out the correct physical block number!
2985 blhdr
->binfo
[i
].bnum
= (off_t
)(blkno
);
2988 next
= (block_list_header
*)((long)blhdr
->binfo
[0].bnum
);
2993 for(blhdr
=tr
->blhdr
; blhdr
; blhdr
=(block_list_header
*)((long)blhdr
->binfo
[0].bnum
)) {
2994 amt
= blhdr
->bytes_used
;
2996 blhdr
->binfo
[0].u
.bi
.b
.sequence_num
= tr
->sequence_num
;
2998 blhdr
->checksum
= 0;
2999 blhdr
->checksum
= calc_checksum((char *)blhdr
, BLHDR_CHECKSUM_SIZE
);
3001 if (kmem_alloc(kernel_map
, (vm_offset_t
*)&bparray
, blhdr
->num_blocks
* sizeof(struct buf
*))) {
3002 panic("can't allocate %zd bytes for bparray\n", blhdr
->num_blocks
* sizeof(struct buf
*));
3005 // calculate individual block checksums
3006 tbuffer_offset
= jnl
->jhdr
->blhdr_size
;
3007 for(i
=1; i
< blhdr
->num_blocks
; i
++) {
3010 if (blhdr
->binfo
[i
].bnum
!= (off_t
)-1) {
3011 bparray
[i
] = blhdr
->binfo
[i
].u
.bp
;
3012 bsize
= buf_size(bparray
[i
]);
3013 blhdr
->binfo
[i
].u
.bi
.bsize
= bsize
;
3014 blhdr
->binfo
[i
].u
.bi
.b
.cksum
= calc_checksum(&((char *)blhdr
)[tbuffer_offset
], bsize
);
3017 bsize
= blhdr
->binfo
[i
].u
.bi
.bsize
;
3018 blhdr
->binfo
[i
].u
.bi
.b
.cksum
= 0;
3021 tbuffer_offset
+= bsize
;
3024 ret
= write_journal_data(jnl
, &end
, blhdr
, amt
);
3026 // always put the bp pointers back
3027 for(i
=1; i
< blhdr
->num_blocks
; i
++) {
3028 if (blhdr
->binfo
[i
].bnum
!= (off_t
)-1) {
3029 blhdr
->binfo
[i
].u
.bp
= bparray
[i
];
3033 kmem_free(kernel_map
, (vm_offset_t
)bparray
, blhdr
->num_blocks
* sizeof(struct buf
*));
3036 printf("jnl: %s: end_transaction: only wrote %d of %d bytes to the journal!\n",
3037 jnl
->jdev_name
, ret
, amt
);
3043 jnl
->jhdr
->end
= end
; // update where the journal now ends
3044 tr
->journal_end
= end
; // the transaction ends here too
3045 if (tr
->journal_start
== 0 || tr
->journal_end
== 0) {
3046 panic("jnl: end_transaction: bad tr journal start/end: 0x%llx 0x%llx\n",
3047 tr
->journal_start
, tr
->journal_end
);
3050 if (write_journal_header(jnl
, 0) != 0) {
3055 * If the caller supplied a callback, call it now that the blocks have been
3056 * written to the journal. This is used by journal_relocate so, for example,
3057 * the file system can change its pointer to the new journal.
3059 if (callback
!= NULL
&& callback(callback_arg
) != 0) {
3064 // setup for looping through all the blhdr's. we null out the
3065 // tbuffer and blhdr fields so that they're not used any more.
3071 // the buffer_flushed_callback will only be called for the
3072 // real blocks that get flushed so we have to account for
3073 // the block_list_headers here.
3075 tr
->num_flushed
= tr
->num_blhdrs
* jnl
->jhdr
->blhdr_size
;
3077 // for each block, set the iodone callback and unlock it
3078 for(; blhdr
; blhdr
=next
) {
3080 // we can re-order the buf ptrs because everything is written out already
3081 qsort(&blhdr
->binfo
[1], blhdr
->num_blocks
-1, sizeof(block_info
), journal_binfo_cmp
);
3083 for(i
=1; i
< blhdr
->num_blocks
; i
++) {
3084 if (blhdr
->binfo
[i
].bnum
== (off_t
)-1) {
3088 bp
= blhdr
->binfo
[i
].u
.bp
;
3090 // have to pass BAC_REMOVE here because we're going to bawrite()
3091 // the buffer when we're done
3093 errno
= buf_acquire(bp
, BAC_REMOVE
, 0, 0);
3094 } while (errno
== EAGAIN
);
3097 struct vnode
*save_vp
;
3100 if ((buf_flags(bp
) & (B_LOCKED
|B_DELWRI
)) != (B_LOCKED
|B_DELWRI
)) {
3101 if (jnl
->flags
& JOURNAL_CLOSE_PENDING
) {
3102 buf_clearflags(bp
, B_LOCKED
);
3106 panic("jnl: end_tr: !!!DANGER!!! bp %p flags (0x%x) not LOCKED & DELWRI\n", bp
, buf_flags(bp
));
3109 save_vp
= buf_vnode(bp
);
3111 buf_setfilter(bp
, buffer_flushed_callback
, tr
, &cur_filter
, NULL
);
3114 panic("jnl: bp @ %p (blkno %qd, vp %p) has non-null iodone (%p) buffflushcb %p\n",
3115 bp
, buf_blkno(bp
), save_vp
, cur_filter
, buffer_flushed_callback
);
3117 buf_clearflags(bp
, B_LOCKED
);
3119 // kicking off the write here helps performance
3121 // XXXdbg this is good for testing: buf_bdwrite(bp);
3124 // this undoes the vnode_ref() in journal_modify_block_end()
3125 vnode_rele_ext(save_vp
, 0, 1);
3127 printf("jnl: %s: end_transaction: could not acquire block %p (errno %d)!\n",
3128 jnl
->jdev_name
,bp
, errno
);
3132 next
= (block_list_header
*)((long)blhdr
->binfo
[0].bnum
);
3134 // we can free blhdr here since we won't need it any more
3135 blhdr
->binfo
[0].bnum
= 0xdeadc0de;
3136 kmem_free(kernel_map
, (vm_offset_t
)blhdr
, tr
->tbuffer_size
);
3139 //printf("jnl: end_tr: tr @ 0x%x, jnl-blocks: 0x%llx - 0x%llx. exit!\n",
3140 // tr, tr->journal_start, tr->journal_end);
3145 jnl
->flags
|= JOURNAL_INVALID
;
3146 jnl
->old_start
[sizeof(jnl
->old_start
)/sizeof(jnl
->old_start
[0]) - 1] &= ~0x8000000000000000LL
;
3147 abort_transaction(jnl
, tr
);
3152 abort_transaction(journal
*jnl
, transaction
*tr
)
3156 block_list_header
*blhdr
, *next
;
3158 struct vnode
*save_vp
;
3160 // for each block list header, iterate over the blocks then
3161 // free up the memory associated with the block list.
3163 // for each block, clear the lock bit and release it.
3165 for(blhdr
=tr
->blhdr
; blhdr
; blhdr
=next
) {
3167 for(i
=1; i
< blhdr
->num_blocks
; i
++) {
3168 if (blhdr
->binfo
[i
].bnum
== (off_t
)-1) {
3171 if ( (buf_vnode(blhdr
->binfo
[i
].u
.bp
) == NULL
) ||
3172 !(buf_flags(blhdr
->binfo
[i
].u
.bp
) & B_LOCKED
) ) {
3176 errno
= buf_meta_bread(buf_vnode(blhdr
->binfo
[i
].u
.bp
),
3177 buf_lblkno(blhdr
->binfo
[i
].u
.bp
),
3178 buf_size(blhdr
->binfo
[i
].u
.bp
),
3182 if (bp
!= blhdr
->binfo
[i
].u
.bp
) {
3183 panic("jnl: abort_tr: got back a different bp! (bp %p should be %p, jnl %p\n",
3184 bp
, blhdr
->binfo
[i
].u
.bp
, jnl
);
3187 // releasing a bp marked invalid
3188 // also clears the locked and delayed state
3189 buf_markinvalid(bp
);
3190 save_vp
= buf_vnode(bp
);
3194 vnode_rele_ext(save_vp
, 0, 1);
3196 printf("jnl: %s: abort_tr: could not find block %Ld vp %p!\n",
3197 jnl
->jdev_name
, blhdr
->binfo
[i
].bnum
, blhdr
->binfo
[i
].u
.bp
);
3204 next
= (block_list_header
*)((long)blhdr
->binfo
[0].bnum
);
3206 // we can free blhdr here since we won't need it any more
3207 blhdr
->binfo
[0].bnum
= 0xdeadc0de;
3208 kmem_free(kernel_map
, (vm_offset_t
)blhdr
, tr
->tbuffer_size
);
3213 tr
->total_bytes
= 0xdbadc0de;
3214 FREE_ZONE(tr
, sizeof(transaction
), M_JNL_TR
);
3219 journal_end_transaction(journal
*jnl
)
3226 if ((jnl
->flags
& JOURNAL_INVALID
) && jnl
->owner
== NULL
) {
3230 if (jnl
->owner
!= current_thread()) {
3231 panic("jnl: end_tr: I'm not the owner! jnl %p, owner %p, curact %p\n",
3232 jnl
, jnl
->owner
, current_thread());
3235 free_old_stuff(jnl
);
3237 jnl
->nested_count
--;
3238 if (jnl
->nested_count
> 0) {
3240 } else if (jnl
->nested_count
< 0) {
3241 panic("jnl: jnl @ %p has negative nested count (%d). bad boy.\n", jnl
, jnl
->nested_count
);
3244 if (jnl
->flags
& JOURNAL_INVALID
) {
3245 if (jnl
->active_tr
) {
3246 if (jnl
->cur_tr
!= NULL
) {
3247 panic("jnl: journal @ %p has active tr (%p) and cur tr (%p)\n",
3248 jnl
, jnl
->active_tr
, jnl
->cur_tr
);
3251 tr
= jnl
->active_tr
;
3252 jnl
->active_tr
= NULL
;
3253 abort_transaction(jnl
, tr
);
3257 unlock_journal(jnl
);
3262 tr
= jnl
->active_tr
;
3263 CHECK_TRANSACTION(tr
);
3265 // clear this out here so that when check_free_space() calls
3266 // the FS flush function, we don't panic in journal_flush()
3267 // if the FS were to call that. note: check_free_space() is
3268 // called from end_transaction().
3270 jnl
->active_tr
= NULL
;
3271 ret
= end_transaction(tr
, 0, NULL
, NULL
);
3274 unlock_journal(jnl
);
3281 journal_flush(journal
*jnl
)
3283 int need_signal
= 0;
3287 if (jnl
->flags
& JOURNAL_INVALID
) {
3291 KERNEL_DEBUG_CONSTANT((FSDBG_CODE(DBG_JOURNAL
, DBG_JOURNAL_FLUSH
))
3292 | DBG_FUNC_START
, 0, 0, 0, 0, 0);
3294 if (jnl
->owner
!= current_thread()) {
3299 free_old_stuff(jnl
);
3301 // if we're not active, flush any buffered transactions
3302 if (jnl
->active_tr
== NULL
&& jnl
->cur_tr
) {
3303 transaction
*tr
= jnl
->cur_tr
;
3306 end_transaction(tr
, 1, NULL
, NULL
); // force it to get flushed
3310 unlock_journal(jnl
);
3313 KERNEL_DEBUG_CONSTANT((FSDBG_CODE(DBG_JOURNAL
, DBG_JOURNAL_FLUSH
))
3314 | DBG_FUNC_END
, 0, 0, 0, 0, 0);
3320 journal_active(journal
*jnl
)
3322 if (jnl
->flags
& JOURNAL_INVALID
) {
3326 return (jnl
->active_tr
== NULL
) ? 0 : 1;
3330 journal_owner(journal
*jnl
)
3335 int journal_uses_fua(journal
*jnl
)
3337 if (jnl
->flags
& JOURNAL_DO_FUA_WRITES
)
3343 * Relocate the journal.
3345 * You provide the new starting offset and size for the journal. You may
3346 * optionally provide a new tbuffer_size; passing zero defaults to not
3347 * changing the tbuffer size except as needed to fit within the new journal
3350 * You must have already started a transaction. The transaction may contain
3351 * modified blocks (such as those needed to deallocate the old journal,
3352 * allocate the new journal, and update the location and size of the journal
3353 * in filesystem-private structures). Any transactions prior to the active
3354 * transaction will be flushed to the old journal. The new journal will be
3355 * initialized, and the blocks from the active transaction will be written to
3358 * The caller will need to update the structures that identify the location
3359 * and size of the journal. These updates should be made in the supplied
3360 * callback routine. These updates must NOT go into a transaction. You should
3361 * force these updates to the media before returning from the callback. In the
3362 * even of a crash, either the old journal will be found, with an empty journal,
3363 * or the new journal will be found with the contents of the active transaction.
3365 * Upon return from the callback, the blocks from the active transaction are
3366 * written to their normal locations on disk.
3368 * (Remember that we have to ensure that blocks get committed to the journal
3369 * before being committed to their normal locations. But the blocks don't count
3370 * as committed until the new journal is pointed at.)
3372 * Upon return, there is still an active transaction: newly allocated, and
3373 * with no modified blocks. Call journal_end_transaction as normal. You may
3374 * modifiy additional blocks before calling journal_end_transaction, and those
3375 * blocks will (eventually) go to the relocated journal.
3378 * jnl The (opened) journal to relocate.
3379 * offset The new journal byte offset (from start of the journal device).
3380 * journal_size The size, in bytes, of the new journal.
3381 * tbuffer_size The new desired transaction buffer size. Pass zero to keep
3382 * the same size as the current journal. The size will be
3383 * modified as needed to fit the new journal.
3384 * callback Routine called after the new journal has been initialized,
3385 * and the active transaction written to the new journal, but
3386 * before the blocks are written to their normal locations.
3387 * Pass NULL for no callback.
3388 * callback_arg An argument passed to the callback routine.
3392 * EINVAL The offset is not block aligned
3393 * EINVAL The journal_size is not a multiple of the block size
3394 * EINVAL The journal is invalid
3395 * (any) An error returned by journal_flush.
3398 int journal_relocate(journal
*jnl
, off_t offset
, off_t journal_size
, int32_t tbuffer_size
,
3399 errno_t (*callback
)(void *), void *callback_arg
)
3405 * Sanity check inputs, and adjust the size of the transaction buffer.
3407 if ((offset
% jnl
->jhdr
->jhdr_size
) != 0) {
3408 printf("jnl: %s: relocate: offset 0x%llx is not an even multiple of block size 0x%x\n",
3409 jnl
->jdev_name
, offset
, jnl
->jhdr
->jhdr_size
);
3412 if ((journal_size
% jnl
->jhdr
->jhdr_size
) != 0) {
3413 printf("jnl: %s: relocate: journal size 0x%llx is not an even multiple of block size 0x%x\n",
3414 jnl
->jdev_name
, journal_size
, jnl
->jhdr
->jhdr_size
);
3420 /* Guarantee we own the active transaction. */
3421 if (jnl
->flags
& JOURNAL_INVALID
) {
3424 if (jnl
->owner
!= current_thread()) {
3425 panic("jnl: relocate: Not the owner! jnl %p, owner %p, curact %p\n",
3426 jnl
, jnl
->owner
, current_thread());
3429 if (tbuffer_size
== 0)
3430 tbuffer_size
= jnl
->tbuffer_size
;
3431 size_up_tbuffer(jnl
, tbuffer_size
, jnl
->jhdr
->jhdr_size
);
3434 * Flush any non-active transactions. We have to temporarily hide the
3435 * active transaction to make journal_flush flush out non-active but
3436 * current (unwritten) transactions.
3438 tr
= jnl
->active_tr
;
3439 CHECK_TRANSACTION(tr
);
3440 jnl
->active_tr
= NULL
;
3441 ret
= journal_flush(jnl
);
3442 jnl
->active_tr
= tr
;
3447 /* Update the journal's offset and size in memory. */
3448 jnl
->jdev_offset
= offset
;
3449 jnl
->jhdr
->start
= jnl
->jhdr
->end
= jnl
->jhdr
->jhdr_size
;
3450 jnl
->jhdr
->size
= journal_size
;
3451 jnl
->active_start
= jnl
->jhdr
->start
;
3454 * Force the active transaction to be written to the new journal. Call the
3455 * supplied callback after the blocks have been written to the journal, but
3456 * before they get written to their normal on-disk locations.
3458 jnl
->active_tr
= NULL
;
3459 ret
= end_transaction(tr
, 1, callback
, callback_arg
);
3461 printf("jnl: %s: relocate: end_transaction failed (%d)\n", jnl
->jdev_name
, ret
);
3466 * Create a new, empty transaction to be the active transaction. This way
3467 * our caller can use journal_end_transaction as usual.
3469 ret
= journal_allocate_transaction(jnl
);
3471 printf("jnl: %s: relocate: could not allocate new transaction (%d)\n", jnl
->jdev_name
, ret
);
3478 jnl
->flags
|= JOURNAL_INVALID
;
3479 abort_transaction(jnl
, tr
);
3484 #else // !JOURNALING - so provide stub functions
3486 int journal_uses_fua(__unused journal
*jnl
)
3492 journal_create(__unused
struct vnode
*jvp
,
3493 __unused off_t offset
,
3494 __unused off_t journal_size
,
3495 __unused
struct vnode
*fsvp
,
3496 __unused
size_t min_fs_blksz
,
3497 __unused
int32_t flags
,
3498 __unused
int32_t tbuffer_size
,
3499 __unused
void (*flush
)(void *arg
),
3506 journal_open(__unused
struct vnode
*jvp
,
3507 __unused off_t offset
,
3508 __unused off_t journal_size
,
3509 __unused
struct vnode
*fsvp
,
3510 __unused
size_t min_fs_blksz
,
3511 __unused
int32_t flags
,
3512 __unused
int32_t tbuffer_size
,
3513 __unused
void (*flush
)(void *arg
),
3521 journal_modify_block_start(__unused journal
*jnl
, __unused
struct buf
*bp
)
3527 journal_modify_block_end(__unused journal
*jnl
,
3528 __unused
struct buf
*bp
,
3529 __unused
void (*func
)(struct buf
*bp
, void *arg
),
3536 journal_kill_block(__unused journal
*jnl
, __unused
struct buf
*bp
)
3541 int journal_relocate(__unused journal
*jnl
,
3542 __unused off_t offset
,
3543 __unused off_t journal_size
,
3544 __unused
int32_t tbuffer_size
,
3545 __unused
errno_t (*callback
)(void *),
3546 __unused
void *callback_arg
)
3552 journal_close(__unused journal
*jnl
)
3557 journal_start_transaction(__unused journal
*jnl
)
3563 journal_end_transaction(__unused journal
*jnl
)
3569 journal_flush(__unused journal
*jnl
)
3575 journal_is_clean(__unused
struct vnode
*jvp
,
3576 __unused off_t offset
,
3577 __unused off_t journal_size
,
3578 __unused
struct vnode
*fsvp
,
3579 __unused
size_t min_fs_block_size
)
3586 journal_owner(__unused journal
*jnl
)
3590 #endif // !JOURNALING