2 * Copyright (c) 1995-2010 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>
55 #include <kern/kalloc.h>
57 #include <sys/kdebug.h>
58 #include <miscfs/specfs/specdev.h>
59 #include <libkern/OSAtomic.h> /* OSAddAtomic */
61 extern task_t kernel_task
;
63 #define DBG_JOURNAL_FLUSH 1
65 #include <sys/sdt.h> /* DTRACE_IO1 */
76 #include <sys/types.h>
81 #include "vfs_journal.h"
83 #ifndef CONFIG_HFS_TRIM
84 #define CONFIG_HFS_TRIM 0
90 // By default, we grow the list of extents to trim by one page at a time.
91 // We'll opt to flush a transaction if it contains at least
92 // JOURNAL_FLUSH_TRIM_EXTENTS extents to be trimmed (even if the number
93 // of modified blocks is small).
96 JOURNAL_DEFAULT_TRIM_BYTES
= PAGE_SIZE
,
97 JOURNAL_DEFAULT_TRIM_EXTENTS
= JOURNAL_DEFAULT_TRIM_BYTES
/ sizeof(dk_extent_t
),
98 JOURNAL_FLUSH_TRIM_EXTENTS
= JOURNAL_DEFAULT_TRIM_EXTENTS
* 15 / 16
101 unsigned int jnl_trim_flush_limit
= JOURNAL_FLUSH_TRIM_EXTENTS
;
102 SYSCTL_UINT (_kern
, OID_AUTO
, jnl_trim_flush
, CTLFLAG_RW
, &jnl_trim_flush_limit
, 0, "number of trimmed extents to cause a journal flush");
105 /* XXX next prototytype should be from libsa/stdlib.h> but conflicts libkern */
106 __private_extern__
void qsort(
110 int (*)(const void *, const void *));
114 // number of bytes to checksum in a block_list_header
115 // NOTE: this should be enough to clear out the header
116 // fields as well as the first entry of binfo[]
117 #define BLHDR_CHECKSUM_SIZE 32
120 static int end_transaction(transaction
*tr
, int force_it
, errno_t (*callback
)(void*), void *callback_arg
);
121 static void abort_transaction(journal
*jnl
, transaction
*tr
);
122 static void dump_journal(journal
*jnl
);
124 static __inline__
void lock_journal(journal
*jnl
);
125 static __inline__
void unlock_journal(journal
*jnl
);
126 static __inline__
void lock_oldstart(journal
*jnl
);
127 static __inline__
void unlock_oldstart(journal
*jnl
);
133 // 3105942 - Coalesce writes to the same block on journal replay
136 typedef struct bucket
{
143 #define STARTING_BUCKETS 256
145 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
);
146 static int grow_table(struct bucket
**buf_ptr
, int num_buckets
, int new_size
);
147 static int lookup_bucket(struct bucket
**buf_ptr
, off_t block_num
, int num_full
);
148 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
);
149 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
);
151 #define CHECK_JOURNAL(jnl) \
154 panic("%s:%d: null journal ptr?\n", __FILE__, __LINE__);\
156 if (jnl->jdev == NULL) { \
157 panic("%s:%d: jdev is null!\n", __FILE__, __LINE__);\
159 if (jnl->fsdev == NULL) { \
160 panic("%s:%d: fsdev is null!\n", __FILE__, __LINE__);\
162 if (jnl->jhdr->magic != JOURNAL_HEADER_MAGIC) {\
163 panic("%s:%d: jhdr magic corrupted (0x%x != 0x%x)\n",\
164 __FILE__, __LINE__, jnl->jhdr->magic, JOURNAL_HEADER_MAGIC);\
166 if ( jnl->jhdr->start <= 0 \
167 || jnl->jhdr->start > jnl->jhdr->size) {\
168 panic("%s:%d: jhdr start looks bad (0x%llx max size 0x%llx)\n", \
169 __FILE__, __LINE__, jnl->jhdr->start, jnl->jhdr->size);\
171 if ( jnl->jhdr->end <= 0 \
172 || jnl->jhdr->end > jnl->jhdr->size) {\
173 panic("%s:%d: jhdr end looks bad (0x%llx max size 0x%llx)\n", \
174 __FILE__, __LINE__, jnl->jhdr->end, jnl->jhdr->size);\
178 #define CHECK_TRANSACTION(tr) \
181 panic("%s:%d: null transaction ptr?\n", __FILE__, __LINE__);\
183 if (tr->jnl == NULL) {\
184 panic("%s:%d: null tr->jnl ptr?\n", __FILE__, __LINE__);\
186 if (tr->blhdr != (block_list_header *)tr->tbuffer) {\
187 panic("%s:%d: blhdr (%p) != tbuffer (%p)\n", __FILE__, __LINE__, tr->blhdr, tr->tbuffer);\
189 if (tr->total_bytes < 0) {\
190 panic("%s:%d: tr total_bytes looks bad: %d\n", __FILE__, __LINE__, tr->total_bytes);\
192 if (tr->journal_start < 0) {\
193 panic("%s:%d: tr journal start looks bad: 0x%llx\n", __FILE__, __LINE__, tr->journal_start);\
195 if (tr->journal_end < 0) {\
196 panic("%s:%d: tr journal end looks bad: 0x%llx\n", __FILE__, __LINE__, tr->journal_end);\
198 if (tr->blhdr && (tr->blhdr->max_blocks <= 0 || tr->blhdr->max_blocks > (tr->jnl->jhdr->size/tr->jnl->jhdr->jhdr_size))) {\
199 panic("%s:%d: tr blhdr max_blocks looks bad: %d\n", __FILE__, __LINE__, tr->blhdr->max_blocks);\
206 // this isn't a great checksum routine but it will do for now.
207 // we use it to checksum the journal header and the block list
208 // headers that are at the start of each transaction.
211 calc_checksum(char *ptr
, int len
)
215 // this is a lame checksum but for now it'll do
216 for(i
=0; i
< len
; i
++, ptr
++) {
217 cksum
= (cksum
<< 8) ^ (cksum
+ *(unsigned char *)ptr
);
226 lck_grp_attr_t
* jnl_group_attr
;
227 lck_attr_t
* jnl_lock_attr
;
228 lck_grp_t
* jnl_mutex_group
;
233 jnl_lock_attr
= lck_attr_alloc_init();
234 jnl_group_attr
= lck_grp_attr_alloc_init();
235 jnl_mutex_group
= lck_grp_alloc_init("jnl-mutex", jnl_group_attr
);
238 static __inline__
void
239 lock_journal(journal
*jnl
)
241 lck_mtx_lock(&jnl
->jlock
);
244 static __inline__
void
245 unlock_journal(journal
*jnl
)
247 lck_mtx_unlock(&jnl
->jlock
);
250 static __inline__
void
251 lock_oldstart(journal
*jnl
)
253 lck_mtx_lock(&jnl
->old_start_lock
);
256 static __inline__
void
257 unlock_oldstart(journal
*jnl
)
259 lck_mtx_unlock(&jnl
->old_start_lock
);
264 #define JNL_WRITE 0x0001
265 #define JNL_READ 0x0002
266 #define JNL_HEADER 0x8000
269 // This function sets up a fake buf and passes it directly to the
270 // journal device strategy routine (so that it won't get cached in
273 // It also handles range checking the i/o so that we don't write
274 // outside the journal boundaries and it will wrap the i/o back
275 // to the beginning if necessary (skipping over the journal header)
278 do_journal_io(journal
*jnl
, off_t
*offset
, void *data
, size_t len
, int direction
)
285 if (*offset
< 0 || *offset
> jnl
->jhdr
->size
) {
286 panic("jnl: do_jnl_io: bad offset 0x%llx (max 0x%llx)\n", *offset
, jnl
->jhdr
->size
);
289 if (direction
& JNL_WRITE
)
290 max_iosize
= jnl
->max_write_size
;
291 else if (direction
& JNL_READ
)
292 max_iosize
= jnl
->max_read_size
;
294 max_iosize
= 128 * 1024;
297 bp
= alloc_io_buf(jnl
->jdev
, 1);
299 if (*offset
+ (off_t
)curlen
> jnl
->jhdr
->size
&& *offset
!= 0 && jnl
->jhdr
->size
!= 0) {
300 if (*offset
== jnl
->jhdr
->size
) {
301 *offset
= jnl
->jhdr
->jhdr_size
;
303 curlen
= (off_t
)jnl
->jhdr
->size
- *offset
;
307 if (curlen
> max_iosize
) {
312 panic("jnl: do_jnl_io: curlen == %d, offset 0x%llx len %zd\n", curlen
, *offset
, len
);
315 if (*offset
== 0 && (direction
& JNL_HEADER
) == 0) {
316 panic("jnl: request for i/o to jnl-header without JNL_HEADER flag set! (len %d, data %p)\n", curlen
, data
);
319 if (direction
& JNL_READ
)
320 buf_setflags(bp
, B_READ
);
323 * don't have to set any flags
325 vnode_startwrite(jnl
->jdev
);
327 buf_setsize(bp
, curlen
);
328 buf_setcount(bp
, curlen
);
329 buf_setdataptr(bp
, (uintptr_t)data
);
330 buf_setblkno(bp
, (daddr64_t
) ((jnl
->jdev_offset
+ *offset
) / (off_t
)jnl
->jhdr
->jhdr_size
));
331 buf_setlblkno(bp
, (daddr64_t
) ((jnl
->jdev_offset
+ *offset
) / (off_t
)jnl
->jhdr
->jhdr_size
));
332 if ((direction
& JNL_WRITE
) && (jnl
->flags
& JOURNAL_DO_FUA_WRITES
)) {
336 DTRACE_IO1(journal__start
, buf_t
, bp
);
337 err
= VNOP_STRATEGY(bp
);
339 err
= (int)buf_biowait(bp
);
341 DTRACE_IO1(journal__done
, buf_t
, bp
);
345 printf("jnl: %s: do_jnl_io: strategy err 0x%x\n", jnl
->jdev_name
, err
);
352 // handle wrap-around
353 data
= (char *)data
+ curlen
;
354 curlen
= len
- io_sz
;
355 if (*offset
>= jnl
->jhdr
->size
) {
356 *offset
= jnl
->jhdr
->jhdr_size
;
365 read_journal_data(journal
*jnl
, off_t
*offset
, void *data
, size_t len
)
367 return do_journal_io(jnl
, offset
, data
, len
, JNL_READ
);
371 write_journal_data(journal
*jnl
, off_t
*offset
, void *data
, size_t len
)
373 return do_journal_io(jnl
, offset
, data
, len
, JNL_WRITE
);
378 read_journal_header(journal
*jnl
, void *data
, size_t len
)
380 off_t hdr_offset
= 0;
382 return do_journal_io(jnl
, &hdr_offset
, data
, len
, JNL_READ
|JNL_HEADER
);
386 write_journal_header(journal
*jnl
, int updating_start
)
388 static int num_err_prints
= 0;
390 off_t jhdr_offset
= 0;
391 struct vfs_context context
;
393 context
.vc_thread
= current_thread();
394 context
.vc_ucred
= NOCRED
;
396 // Flush the track cache if we're not doing force-unit-access
399 if (!updating_start
&& (jnl
->flags
& JOURNAL_DO_FUA_WRITES
) == 0) {
400 ret
= VNOP_IOCTL(jnl
->jdev
, DKIOCSYNCHRONIZECACHE
, NULL
, FWRITE
, &context
);
404 // Only print this error if it's a different error than the
405 // previous one, or if it's the first time for this device
406 // or if the total number of printfs is less than 25. We
407 // allow for up to 25 printfs to insure that some make it
408 // into the on-disk syslog. Otherwise if we only printed
409 // one, it's possible it would never make it to the syslog
410 // for the root volume and that makes debugging hard.
412 if ( ret
!= jnl
->last_flush_err
413 || (jnl
->flags
& JOURNAL_FLUSHCACHE_ERR
) == 0
414 || num_err_prints
++ < 25) {
416 printf("jnl: %s: flushing fs disk buffer returned 0x%x\n", jnl
->jdev_name
, ret
);
418 jnl
->flags
|= JOURNAL_FLUSHCACHE_ERR
;
419 jnl
->last_flush_err
= ret
;
423 jnl
->jhdr
->checksum
= 0;
424 jnl
->jhdr
->checksum
= calc_checksum((char *)jnl
->jhdr
, JOURNAL_HEADER_CKSUM_SIZE
);
425 if (do_journal_io(jnl
, &jhdr_offset
, jnl
->header_buf
, jnl
->jhdr
->jhdr_size
, JNL_WRITE
|JNL_HEADER
) != (size_t)jnl
->jhdr
->jhdr_size
) {
426 printf("jnl: %s: write_journal_header: error writing the journal header!\n", jnl
->jdev_name
);
427 jnl
->flags
|= JOURNAL_INVALID
;
431 // If we're not doing force-unit-access writes, then we
432 // have to flush after writing the journal header so that
433 // a future transaction doesn't sneak out to disk before
434 // the header does and thus overwrite data that the old
435 // journal header refers to. Saw this exact case happen
436 // on an IDE bus analyzer with Larry Barras so while it
437 // may seem obscure, it's not.
439 if (updating_start
&& (jnl
->flags
& JOURNAL_DO_FUA_WRITES
) == 0) {
440 VNOP_IOCTL(jnl
->jdev
, DKIOCSYNCHRONIZECACHE
, NULL
, FWRITE
, &context
);
449 // this is a work function used to free up transactions that
450 // completed. they can't be free'd from buffer_flushed_callback
451 // because it is called from deep with the disk driver stack
452 // and thus can't do something that would potentially cause
453 // paging. it gets called by each of the journal api entry
454 // points so stuff shouldn't hang around for too long.
457 free_old_stuff(journal
*jnl
)
459 transaction
*tr
, *next
;
463 jnl
->tr_freeme
= NULL
;
464 unlock_oldstart(jnl
);
468 FREE_ZONE(tr
, sizeof(transaction
), M_JNL_TR
);
476 // This is our callback that lets us know when a buffer has been
477 // flushed to disk. It's called from deep within the driver stack
478 // and thus is quite limited in what it can do. Notably, it can
479 // not initiate any new i/o's or allocate/free memory.
482 buffer_flushed_callback(struct buf
*bp
, void *arg
)
486 transaction
*ctr
, *prev
=NULL
, *next
;
488 int bufsize
, amt_flushed
, total_bytes
;
491 //printf("jnl: buf flush: bp @ 0x%x l/blkno %qd/%qd vp 0x%x tr @ 0x%x\n",
492 // bp, buf_lblkno(bp), buf_blkno(bp), buf_vnode(bp), arg);
494 // snarf out the bits we want
495 bufsize
= buf_size(bp
);
496 tr
= (transaction
*)arg
;
498 // then we've already seen it
503 CHECK_TRANSACTION(tr
);
506 if (jnl
->flags
& JOURNAL_INVALID
) {
512 amt_flushed
= tr
->num_killed
;
513 total_bytes
= tr
->total_bytes
;
515 // update the number of blocks that have been flushed.
516 // this buf may represent more than one block so take
517 // that into account.
519 // OSAddAtomic() returns the value of tr->num_flushed before the add
521 amt_flushed
+= OSAddAtomic(bufsize
, &tr
->num_flushed
);
524 // if this transaction isn't done yet, just return as
525 // there is nothing to do.
527 // NOTE: we are careful to not reference anything through
528 // the tr pointer after doing the OSAddAtomic(). if
529 // this if statement fails then we are the last one
530 // and then it's ok to dereference "tr".
532 if ((amt_flushed
+ bufsize
) < total_bytes
) {
536 // this will single thread checking the transaction
539 if (tr
->total_bytes
== (int)0xfbadc0de) {
540 // then someone beat us to it...
541 unlock_oldstart(jnl
);
545 // mark this so that we're the owner of dealing with the
546 // cleanup for this transaction
547 tr
->total_bytes
= 0xfbadc0de;
549 //printf("jnl: tr 0x%x (0x%llx 0x%llx) in jnl 0x%x completed.\n",
550 // tr, tr->journal_start, tr->journal_end, jnl);
552 // find this entry in the old_start[] index and mark it completed
553 for(i
=0; i
< sizeof(jnl
->old_start
)/sizeof(jnl
->old_start
[0]); i
++) {
555 if ((off_t
)(jnl
->old_start
[i
] & ~(0x8000000000000000ULL
)) == tr
->journal_start
) {
556 jnl
->old_start
[i
] &= ~(0x8000000000000000ULL
);
561 if (i
>= sizeof(jnl
->old_start
)/sizeof(jnl
->old_start
[0])) {
562 panic("jnl: buffer_flushed: did not find tr w/start @ %lld (tr %p, jnl %p)\n",
563 tr
->journal_start
, tr
, jnl
);
567 // if we are here then we need to update the journal header
568 // to reflect that this transaction is complete
569 if (tr
->journal_start
== jnl
->active_start
) {
570 jnl
->active_start
= tr
->journal_end
;
571 tr
->journal_start
= tr
->journal_end
= (off_t
)0;
574 // go through the completed_trs list and try to coalesce
575 // entries, restarting back at the beginning if we have to.
576 for(ctr
=jnl
->completed_trs
; ctr
; prev
=ctr
, ctr
=next
) {
577 if (ctr
->journal_start
== jnl
->active_start
) {
578 jnl
->active_start
= ctr
->journal_end
;
580 prev
->next
= ctr
->next
;
582 if (ctr
== jnl
->completed_trs
) {
583 jnl
->completed_trs
= ctr
->next
;
586 next
= jnl
->completed_trs
; // this starts us over again
587 ctr
->next
= jnl
->tr_freeme
;
588 jnl
->tr_freeme
= ctr
;
590 } else if (tr
->journal_end
== ctr
->journal_start
) {
591 ctr
->journal_start
= tr
->journal_start
;
592 next
= jnl
->completed_trs
; // this starts us over again
594 tr
->journal_start
= tr
->journal_end
= (off_t
)0;
595 } else if (tr
->journal_start
== ctr
->journal_end
) {
596 ctr
->journal_end
= tr
->journal_end
;
598 tr
->journal_start
= tr
->journal_end
= (off_t
)0;
599 } else if (ctr
->next
&& ctr
->journal_end
== ctr
->next
->journal_start
) {
600 // coalesce the next entry with this one and link the next
601 // entry in at the head of the tr_freeme list
602 next
= ctr
->next
; // temporarily use the "next" variable
603 ctr
->journal_end
= next
->journal_end
;
604 ctr
->next
= next
->next
;
605 next
->next
= jnl
->tr_freeme
; // link in the next guy at the head of the tr_freeme list
606 jnl
->tr_freeme
= next
;
608 next
= jnl
->completed_trs
; // this starts us over again
615 // if this is true then we didn't merge with anyone
616 // so link ourselves in at the head of the completed
618 if (tr
->journal_start
!= 0) {
619 // put this entry into the correct sorted place
620 // in the list instead of just at the head.
624 for(ctr
=jnl
->completed_trs
; ctr
&& tr
->journal_start
> ctr
->journal_start
; prev
=ctr
, ctr
=ctr
->next
) {
628 if (ctr
== NULL
&& prev
== NULL
) {
629 jnl
->completed_trs
= tr
;
631 } else if (ctr
== jnl
->completed_trs
) {
632 tr
->next
= jnl
->completed_trs
;
633 jnl
->completed_trs
= tr
;
635 tr
->next
= prev
->next
;
639 // if we're here this tr got merged with someone else so
640 // put it on the list to be free'd
641 tr
->next
= jnl
->tr_freeme
;
644 unlock_oldstart(jnl
);
648 #include <libkern/OSByteOrder.h>
650 #define SWAP16(x) OSSwapInt16(x)
651 #define SWAP32(x) OSSwapInt32(x)
652 #define SWAP64(x) OSSwapInt64(x)
656 swap_journal_header(journal
*jnl
)
658 jnl
->jhdr
->magic
= SWAP32(jnl
->jhdr
->magic
);
659 jnl
->jhdr
->endian
= SWAP32(jnl
->jhdr
->endian
);
660 jnl
->jhdr
->start
= SWAP64(jnl
->jhdr
->start
);
661 jnl
->jhdr
->end
= SWAP64(jnl
->jhdr
->end
);
662 jnl
->jhdr
->size
= SWAP64(jnl
->jhdr
->size
);
663 jnl
->jhdr
->blhdr_size
= SWAP32(jnl
->jhdr
->blhdr_size
);
664 jnl
->jhdr
->checksum
= SWAP32(jnl
->jhdr
->checksum
);
665 jnl
->jhdr
->jhdr_size
= SWAP32(jnl
->jhdr
->jhdr_size
);
666 jnl
->jhdr
->sequence_num
= SWAP32(jnl
->jhdr
->sequence_num
);
670 swap_block_list_header(journal
*jnl
, block_list_header
*blhdr
)
674 blhdr
->max_blocks
= SWAP16(blhdr
->max_blocks
);
675 blhdr
->num_blocks
= SWAP16(blhdr
->num_blocks
);
676 blhdr
->bytes_used
= SWAP32(blhdr
->bytes_used
);
677 blhdr
->checksum
= SWAP32(blhdr
->checksum
);
678 blhdr
->flags
= SWAP32(blhdr
->flags
);
680 if (blhdr
->num_blocks
>= ((jnl
->jhdr
->blhdr_size
/ sizeof(block_info
)) - 1)) {
681 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
);
685 for(i
=0; i
< blhdr
->num_blocks
; i
++) {
686 blhdr
->binfo
[i
].bnum
= SWAP64(blhdr
->binfo
[i
].bnum
);
687 blhdr
->binfo
[i
].u
.bi
.bsize
= SWAP32(blhdr
->binfo
[i
].u
.bi
.bsize
);
688 blhdr
->binfo
[i
].u
.bi
.b
.cksum
= SWAP32(blhdr
->binfo
[i
].u
.bi
.b
.cksum
);
694 update_fs_block(journal
*jnl
, void *block_ptr
, off_t fs_block
, size_t bsize
)
697 struct buf
*oblock_bp
=NULL
;
699 // first read the block we want.
700 ret
= buf_meta_bread(jnl
->fsdev
, (daddr64_t
)fs_block
, bsize
, NOCRED
, &oblock_bp
);
702 printf("jnl: %s: update_fs_block: error reading fs block # %lld! (ret %d)\n", jnl
->jdev_name
, fs_block
, ret
);
705 buf_brelse(oblock_bp
);
709 // let's try to be aggressive here and just re-write the block
710 oblock_bp
= buf_getblk(jnl
->fsdev
, (daddr64_t
)fs_block
, bsize
, 0, 0, BLK_META
);
711 if (oblock_bp
== NULL
) {
712 printf("jnl: %s: update_fs_block: buf_getblk() for %lld failed! failing update.\n", jnl
->jdev_name
, fs_block
);
717 // make sure it's the correct size.
718 if (buf_size(oblock_bp
) != bsize
) {
719 buf_brelse(oblock_bp
);
723 // copy the journal data over top of it
724 memcpy((char *)0 + buf_dataptr(oblock_bp
), block_ptr
, bsize
);
726 if ((ret
= VNOP_BWRITE(oblock_bp
)) != 0) {
727 printf("jnl: %s: update_fs_block: failed to update block %lld (ret %d)\n", jnl
->jdev_name
, fs_block
,ret
);
731 // and now invalidate it so that if someone else wants to read
732 // it in a different size they'll be able to do it.
733 ret
= buf_meta_bread(jnl
->fsdev
, (daddr64_t
)fs_block
, bsize
, NOCRED
, &oblock_bp
);
735 buf_markinvalid(oblock_bp
);
736 buf_brelse(oblock_bp
);
743 grow_table(struct bucket
**buf_ptr
, int num_buckets
, int new_size
)
745 struct bucket
*newBuf
;
746 int current_size
= num_buckets
, i
;
748 // return if newsize is less than the current size
749 if (new_size
< num_buckets
) {
753 if ((MALLOC(newBuf
, struct bucket
*, new_size
*sizeof(struct bucket
), M_TEMP
, M_WAITOK
)) == NULL
) {
754 printf("jnl: grow_table: no memory to expand coalesce buffer!\n");
758 // printf("jnl: lookup_bucket: expanded co_buf to %d elems\n", new_size);
760 // copy existing elements
761 bcopy(*buf_ptr
, newBuf
, num_buckets
*sizeof(struct bucket
));
763 // initialize the new ones
764 for(i
=num_buckets
; i
< new_size
; i
++) {
765 newBuf
[i
].block_num
= (off_t
)-1;
768 // free the old container
769 FREE(*buf_ptr
, M_TEMP
);
778 lookup_bucket(struct bucket
**buf_ptr
, off_t block_num
, int num_full
)
780 int lo
, hi
, index
, matches
, i
;
783 return 0; // table is empty, so insert at index=0
790 // perform binary search for block_num
792 int mid
= (hi
- lo
)/2 + lo
;
793 off_t this_num
= (*buf_ptr
)[mid
].block_num
;
795 if (block_num
== this_num
) {
800 if (block_num
< this_num
) {
805 if (block_num
> this_num
) {
811 // check if lo and hi converged on the match
812 if (block_num
== (*buf_ptr
)[hi
].block_num
) {
816 // if no existing entry found, find index for new one
818 index
= (block_num
< (*buf_ptr
)[hi
].block_num
) ? hi
: hi
+ 1;
820 // make sure that we return the right-most index in the case of multiple matches
823 while(i
< num_full
&& block_num
== (*buf_ptr
)[i
].block_num
) {
835 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
)
838 // grow the table if we're out of space
839 if (*num_full_ptr
>= *num_buckets_ptr
) {
840 int new_size
= *num_buckets_ptr
* 2;
841 int grow_size
= grow_table(buf_ptr
, *num_buckets_ptr
, new_size
);
843 if (grow_size
< new_size
) {
844 printf("jnl: %s: add_block: grow_table returned an error!\n", jnl
->jdev_name
);
848 *num_buckets_ptr
= grow_size
; //update num_buckets to reflect the new size
851 // if we're not inserting at the end, we need to bcopy
852 if (blk_index
!= *num_full_ptr
) {
853 bcopy( (*buf_ptr
)+(blk_index
), (*buf_ptr
)+(blk_index
+1), (*num_full_ptr
-blk_index
)*sizeof(struct bucket
) );
856 (*num_full_ptr
)++; // increment only if we're not overwriting
859 // sanity check the values we're about to add
860 if ((off_t
)offset
>= jnl
->jhdr
->size
) {
861 offset
= jnl
->jhdr
->jhdr_size
+ (offset
- jnl
->jhdr
->size
);
864 panic("jnl: insert_block: bad size in insert_block (%zd)\n", size
);
867 (*buf_ptr
)[blk_index
].block_num
= num
;
868 (*buf_ptr
)[blk_index
].block_size
= size
;
869 (*buf_ptr
)[blk_index
].jnl_offset
= offset
;
870 (*buf_ptr
)[blk_index
].cksum
= cksum
;
876 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
)
878 int num_to_remove
, index
, i
, overwrite
, err
;
879 size_t jhdr_size
= jnl
->jhdr
->jhdr_size
, new_offset
;
880 off_t overlap
, block_start
, block_end
;
882 block_start
= block_num
*jhdr_size
;
883 block_end
= block_start
+ size
;
884 overwrite
= (block_num
== (*buf_ptr
)[blk_index
].block_num
&& size
>= (*buf_ptr
)[blk_index
].block_size
);
886 // first, eliminate any overlap with the previous entry
887 if (blk_index
!= 0 && !overwrite
) {
888 off_t prev_block_start
= (*buf_ptr
)[blk_index
-1].block_num
*jhdr_size
;
889 off_t prev_block_end
= prev_block_start
+ (*buf_ptr
)[blk_index
-1].block_size
;
890 overlap
= prev_block_end
- block_start
;
892 if (overlap
% jhdr_size
!= 0) {
893 panic("jnl: do_overlap: overlap with previous entry not a multiple of %zd\n", jhdr_size
);
896 // if the previous entry completely overlaps this one, we need to break it into two pieces.
897 if (prev_block_end
> block_end
) {
898 off_t new_num
= block_end
/ jhdr_size
;
899 size_t new_size
= prev_block_end
- block_end
;
901 new_offset
= (*buf_ptr
)[blk_index
-1].jnl_offset
+ (block_end
- prev_block_start
);
903 err
= insert_block(jnl
, buf_ptr
, blk_index
, new_num
, new_size
, new_offset
, cksum
, num_buckets_ptr
, num_full_ptr
, 0);
905 panic("jnl: do_overlap: error inserting during pre-overlap\n");
909 // Regardless, we need to truncate the previous entry to the beginning of the overlap
910 (*buf_ptr
)[blk_index
-1].block_size
= block_start
- prev_block_start
;
911 (*buf_ptr
)[blk_index
-1].cksum
= 0; // have to blow it away because there's no way to check it
915 // then, bail out fast if there's no overlap with the entries that follow
916 if (!overwrite
&& block_end
<= (off_t
)((*buf_ptr
)[blk_index
].block_num
*jhdr_size
)) {
917 return 0; // no overlap, no overwrite
918 } else if (overwrite
&& (blk_index
+ 1 >= *num_full_ptr
|| block_end
<= (off_t
)((*buf_ptr
)[blk_index
+1].block_num
*jhdr_size
))) {
920 (*buf_ptr
)[blk_index
].cksum
= cksum
; // update this
921 return 1; // simple overwrite
924 // Otherwise, find all cases of total and partial overlap. We use the special
925 // block_num of -2 to designate entries that are completely overlapped and must
926 // be eliminated. The block_num, size, and jnl_offset of partially overlapped
927 // entries must be adjusted to keep the array consistent.
930 while(index
< *num_full_ptr
&& block_end
> (off_t
)((*buf_ptr
)[index
].block_num
*jhdr_size
)) {
931 if (block_end
>= (off_t
)(((*buf_ptr
)[index
].block_num
*jhdr_size
+ (*buf_ptr
)[index
].block_size
))) {
932 (*buf_ptr
)[index
].block_num
= -2; // mark this for deletion
935 overlap
= block_end
- (*buf_ptr
)[index
].block_num
*jhdr_size
;
937 if (overlap
% jhdr_size
!= 0) {
938 panic("jnl: do_overlap: overlap of %lld is not multiple of %zd\n", overlap
, jhdr_size
);
941 // if we partially overlap this entry, adjust its block number, jnl offset, and size
942 (*buf_ptr
)[index
].block_num
+= (overlap
/ jhdr_size
); // make sure overlap is multiple of jhdr_size, or round up
943 (*buf_ptr
)[index
].cksum
= 0;
945 new_offset
= (*buf_ptr
)[index
].jnl_offset
+ overlap
; // check for wrap-around
946 if ((off_t
)new_offset
>= jnl
->jhdr
->size
) {
947 new_offset
= jhdr_size
+ (new_offset
- jnl
->jhdr
->size
);
949 (*buf_ptr
)[index
].jnl_offset
= new_offset
;
951 (*buf_ptr
)[index
].block_size
-= overlap
; // sanity check for negative value
952 if ((*buf_ptr
)[index
].block_size
<= 0) {
953 panic("jnl: do_overlap: after overlap, new block size is invalid (%u)\n", (*buf_ptr
)[index
].block_size
);
954 // return -1; // if above panic is removed, return -1 for error
963 // bcopy over any completely overlapped entries, starting at the right (where the above loop broke out)
964 index
--; // start with the last index used within the above loop
965 while(index
>= blk_index
) {
966 if ((*buf_ptr
)[index
].block_num
== -2) {
967 if (index
== *num_full_ptr
-1) {
968 (*buf_ptr
)[index
].block_num
= -1; // it's the last item in the table... just mark as free
970 bcopy( (*buf_ptr
)+(index
+1), (*buf_ptr
)+(index
), (*num_full_ptr
- (index
+ 1)) * sizeof(struct bucket
) );
977 // eliminate any stale entries at the end of the table
978 for(i
=*num_full_ptr
; i
< (*num_full_ptr
+ num_to_remove
); i
++) {
979 (*buf_ptr
)[i
].block_num
= -1;
982 return 0; // if we got this far, we need to insert the entry into the table (rather than overwrite)
985 // PR-3105942: Coalesce writes to the same block in journal replay
986 // We coalesce writes by maintaining a dynamic sorted array of physical disk blocks
987 // to be replayed and the corresponding location in the journal which contains
988 // the most recent data for those blocks. The array is "played" once the all the
989 // blocks in the journal have been coalesced. The code for the case of conflicting/
990 // overlapping writes to a single block is the most dense. Because coalescing can
991 // disrupt the existing time-ordering of blocks in the journal playback, care
992 // is taken to catch any overlaps and keep the array consistent.
994 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
)
996 int blk_index
, overwriting
;
998 // on return from lookup_bucket(), blk_index is the index into the table where block_num should be
999 // inserted (or the index of the elem to overwrite).
1000 blk_index
= lookup_bucket( buf_ptr
, block_num
, *num_full_ptr
);
1002 // check if the index is within bounds (if we're adding this block to the end of
1003 // the table, blk_index will be equal to num_full)
1004 if (blk_index
< 0 || blk_index
> *num_full_ptr
) {
1005 //printf("jnl: add_block: trouble adding block to co_buf\n");
1007 } // else printf("jnl: add_block: adding block 0x%llx at i=%d\n", block_num, blk_index);
1009 // Determine whether we're overwriting an existing entry by checking for overlap
1010 overwriting
= do_overlap(jnl
, buf_ptr
, blk_index
, block_num
, size
, offset
, cksum
, num_buckets_ptr
, num_full_ptr
);
1011 if (overwriting
< 0) {
1012 return -1; // if we got an error, pass it along
1015 // returns the index, or -1 on error
1016 blk_index
= insert_block(jnl
, buf_ptr
, blk_index
, block_num
, size
, offset
, cksum
, num_buckets_ptr
, num_full_ptr
, overwriting
);
1022 replay_journal(journal
*jnl
)
1024 int i
, orig_checksum
, checksum
, check_block_checksums
=0, bad_blocks
=0;
1026 size_t max_bsize
= 0; /* protected by block_ptr */
1027 block_list_header
*blhdr
;
1028 off_t offset
, txn_start_offset
=0, blhdr_offset
, orig_jnl_start
;
1029 char *buff
, *block_ptr
=NULL
;
1030 struct bucket
*co_buf
;
1031 int num_buckets
= STARTING_BUCKETS
, num_full
, check_past_jnl_end
= 1, in_uncharted_territory
=0;
1032 uint32_t last_sequence_num
= 0;
1034 // wrap the start ptr if it points to the very end of the journal
1035 if (jnl
->jhdr
->start
== jnl
->jhdr
->size
) {
1036 jnl
->jhdr
->start
= jnl
->jhdr
->jhdr_size
;
1038 if (jnl
->jhdr
->end
== jnl
->jhdr
->size
) {
1039 jnl
->jhdr
->end
= jnl
->jhdr
->jhdr_size
;
1042 if (jnl
->jhdr
->start
== jnl
->jhdr
->end
) {
1046 orig_jnl_start
= jnl
->jhdr
->start
;
1048 // allocate memory for the header_block. we'll read each blhdr into this
1049 if (kmem_alloc(kernel_map
, (vm_offset_t
*)&buff
, jnl
->jhdr
->blhdr_size
)) {
1050 printf("jnl: %s: replay_journal: no memory for block buffer! (%d bytes)\n",
1051 jnl
->jdev_name
, jnl
->jhdr
->blhdr_size
);
1055 // allocate memory for the coalesce buffer
1056 if ((MALLOC(co_buf
, struct bucket
*, num_buckets
*sizeof(struct bucket
), M_TEMP
, M_WAITOK
)) == NULL
) {
1057 printf("jnl: %s: replay_journal: no memory for coalesce buffer!\n", jnl
->jdev_name
);
1063 // initialize entries
1064 for(i
=0; i
< num_buckets
; i
++) {
1065 co_buf
[i
].block_num
= -1;
1067 num_full
= 0; // empty at first
1070 printf("jnl: %s: replay_journal: from: %lld to: %lld (joffset 0x%llx)\n",
1071 jnl
->jdev_name
, jnl
->jhdr
->start
, jnl
->jhdr
->end
, jnl
->jdev_offset
);
1073 while(check_past_jnl_end
|| jnl
->jhdr
->start
!= jnl
->jhdr
->end
) {
1074 offset
= blhdr_offset
= jnl
->jhdr
->start
;
1075 ret
= read_journal_data(jnl
, &offset
, buff
, jnl
->jhdr
->blhdr_size
);
1076 if (ret
!= (size_t)jnl
->jhdr
->blhdr_size
) {
1077 printf("jnl: %s: replay_journal: Could not read block list header block @ 0x%llx!\n", jnl
->jdev_name
, offset
);
1079 goto bad_txn_handling
;
1082 blhdr
= (block_list_header
*)buff
;
1084 orig_checksum
= blhdr
->checksum
;
1085 blhdr
->checksum
= 0;
1086 if (jnl
->flags
& JOURNAL_NEED_SWAP
) {
1087 // calculate the checksum based on the unswapped data
1088 // because it is done byte-at-a-time.
1089 orig_checksum
= SWAP32(orig_checksum
);
1090 checksum
= calc_checksum((char *)blhdr
, BLHDR_CHECKSUM_SIZE
);
1091 swap_block_list_header(jnl
, blhdr
);
1093 checksum
= calc_checksum((char *)blhdr
, BLHDR_CHECKSUM_SIZE
);
1098 // XXXdbg - if these checks fail, we should replay as much
1099 // we can in the hopes that it will still leave the
1100 // drive in a better state than if we didn't replay
1103 if (checksum
!= orig_checksum
) {
1104 if (check_past_jnl_end
&& in_uncharted_territory
) {
1106 if (blhdr_offset
!= jnl
->jhdr
->end
) {
1107 printf("jnl: %s: Extra txn replay stopped @ %lld / 0x%llx\n", jnl
->jdev_name
, blhdr_offset
, blhdr_offset
);
1110 check_past_jnl_end
= 0;
1111 jnl
->jhdr
->end
= blhdr_offset
;
1115 printf("jnl: %s: replay_journal: bad block list header @ 0x%llx (checksum 0x%x != 0x%x)\n",
1116 jnl
->jdev_name
, blhdr_offset
, orig_checksum
, checksum
);
1118 if (blhdr_offset
== orig_jnl_start
) {
1119 // if there's nothing in the journal at all, just bail out altogether.
1124 goto bad_txn_handling
;
1127 if ( (last_sequence_num
!= 0)
1128 && (blhdr
->binfo
[0].u
.bi
.b
.sequence_num
!= 0)
1129 && (blhdr
->binfo
[0].u
.bi
.b
.sequence_num
!= last_sequence_num
)
1130 && (blhdr
->binfo
[0].u
.bi
.b
.sequence_num
!= last_sequence_num
+1)) {
1132 txn_start_offset
= jnl
->jhdr
->end
= blhdr_offset
;
1134 if (check_past_jnl_end
) {
1135 check_past_jnl_end
= 0;
1136 printf("jnl: %s: 2: extra replay stopped @ %lld / 0x%llx (seq %d < %d)\n",
1137 jnl
->jdev_name
, blhdr_offset
, blhdr_offset
, blhdr
->binfo
[0].u
.bi
.b
.sequence_num
, last_sequence_num
);
1141 printf("jnl: %s: txn sequence numbers out of order in txn @ %lld / %llx! (%d < %d)\n",
1142 jnl
->jdev_name
, blhdr_offset
, blhdr_offset
, blhdr
->binfo
[0].u
.bi
.b
.sequence_num
, last_sequence_num
);
1144 goto bad_txn_handling
;
1146 last_sequence_num
= blhdr
->binfo
[0].u
.bi
.b
.sequence_num
;
1148 if (blhdr_offset
>= jnl
->jhdr
->end
&& jnl
->jhdr
->start
<= jnl
->jhdr
->end
) {
1149 if (last_sequence_num
== 0) {
1150 check_past_jnl_end
= 0;
1151 printf("jnl: %s: pre-sequence-num-enabled txn's - can not go further than end (%lld %lld).\n",
1152 jnl
->jdev_name
, jnl
->jhdr
->start
, jnl
->jhdr
->end
);
1153 if (jnl
->jhdr
->start
!= jnl
->jhdr
->end
) {
1154 jnl
->jhdr
->start
= jnl
->jhdr
->end
;
1158 printf("jnl: %s: examining extra transactions starting @ %lld / 0x%llx\n", jnl
->jdev_name
, blhdr_offset
, blhdr_offset
);
1161 if ( blhdr
->max_blocks
<= 0 || blhdr
->max_blocks
> (jnl
->jhdr
->size
/jnl
->jhdr
->jhdr_size
)
1162 || blhdr
->num_blocks
<= 0 || blhdr
->num_blocks
> blhdr
->max_blocks
) {
1163 printf("jnl: %s: replay_journal: bad looking journal entry: max: %d num: %d\n",
1164 jnl
->jdev_name
, blhdr
->max_blocks
, blhdr
->num_blocks
);
1166 goto bad_txn_handling
;
1170 for(i
=1; i
< blhdr
->num_blocks
; i
++) {
1171 if (blhdr
->binfo
[i
].bnum
< 0 && blhdr
->binfo
[i
].bnum
!= (off_t
)-1) {
1172 printf("jnl: %s: replay_journal: bogus block number 0x%llx\n", jnl
->jdev_name
, blhdr
->binfo
[i
].bnum
);
1174 goto bad_txn_handling
;
1177 if ((size_t)blhdr
->binfo
[i
].u
.bi
.bsize
> max_bsize
) {
1178 max_bsize
= blhdr
->binfo
[i
].u
.bi
.bsize
;
1182 if (blhdr
->flags
& BLHDR_CHECK_CHECKSUMS
) {
1183 check_block_checksums
= 1;
1184 if (kmem_alloc(kernel_map
, (vm_offset_t
*)&block_ptr
, max_bsize
)) {
1191 if (blhdr
->flags
& BLHDR_FIRST_HEADER
) {
1192 txn_start_offset
= blhdr_offset
;
1195 //printf("jnl: replay_journal: adding %d blocks in journal entry @ 0x%llx to co_buf\n",
1196 // blhdr->num_blocks-1, jnl->jhdr->start);
1198 for(i
=1; i
< blhdr
->num_blocks
; i
++) {
1202 size
= blhdr
->binfo
[i
].u
.bi
.bsize
;
1203 number
= blhdr
->binfo
[i
].bnum
;
1205 // don't add "killed" blocks
1206 if (number
== (off_t
)-1) {
1207 //printf("jnl: replay_journal: skipping killed fs block (index %d)\n", i);
1210 if (check_block_checksums
) {
1214 block_offset
= offset
;
1216 // read the block so we can check the checksum
1217 ret
= read_journal_data(jnl
, &block_offset
, block_ptr
, size
);
1218 if (ret
!= (size_t)size
) {
1219 printf("jnl: %s: replay_journal: Could not read journal entry data @ offset 0x%llx!\n", jnl
->jdev_name
, offset
);
1221 goto bad_txn_handling
;
1224 disk_cksum
= calc_checksum(block_ptr
, size
);
1226 // there is no need to swap the checksum from disk because
1227 // it got swapped when the blhdr was read in.
1228 if (blhdr
->binfo
[i
].u
.bi
.b
.cksum
!= 0 && disk_cksum
!= blhdr
->binfo
[i
].u
.bi
.b
.cksum
) {
1229 printf("jnl: %s: txn starting at %lld (%lld) @ index %3d bnum %lld (%d) with disk cksum != blhdr cksum (0x%.8x 0x%.8x)\n",
1230 jnl
->jdev_name
, txn_start_offset
, blhdr_offset
, i
, number
, size
, disk_cksum
, blhdr
->binfo
[i
].u
.bi
.b
.cksum
);
1231 printf("jnl: 0x%.8x 0x%.8x 0x%.8x 0x%.8x 0x%.8x 0x%.8x 0x%.8x 0x%.8x\n",
1232 *(int *)&block_ptr
[0*sizeof(int)], *(int *)&block_ptr
[1*sizeof(int)], *(int *)&block_ptr
[2*sizeof(int)], *(int *)&block_ptr
[3*sizeof(int)],
1233 *(int *)&block_ptr
[4*sizeof(int)], *(int *)&block_ptr
[5*sizeof(int)], *(int *)&block_ptr
[6*sizeof(int)], *(int *)&block_ptr
[7*sizeof(int)]);
1236 goto bad_txn_handling
;
1241 // add this bucket to co_buf, coalescing where possible
1242 // printf("jnl: replay_journal: adding block 0x%llx\n", number);
1243 ret_val
= add_block(jnl
, &co_buf
, number
, size
, (size_t) offset
, blhdr
->binfo
[i
].u
.bi
.b
.cksum
, &num_buckets
, &num_full
);
1245 if (ret_val
== -1) {
1246 printf("jnl: %s: replay_journal: trouble adding block to co_buf\n", jnl
->jdev_name
);
1248 } // else printf("jnl: replay_journal: added block 0x%llx at i=%d\n", number);
1254 // check if the last block added puts us off the end of the jnl.
1255 // if so, we need to wrap to the beginning and take any remainder
1258 if (offset
>= jnl
->jhdr
->size
) {
1259 offset
= jnl
->jhdr
->jhdr_size
+ (offset
- jnl
->jhdr
->size
);
1264 kmem_free(kernel_map
, (vm_offset_t
)block_ptr
, max_bsize
);
1270 if (txn_start_offset
== 0) {
1271 printf("jnl: %s: no known good txn start offset! aborting journal replay.\n", jnl
->jdev_name
);
1275 jnl
->jhdr
->start
= orig_jnl_start
;
1276 jnl
->jhdr
->end
= txn_start_offset
;
1277 check_past_jnl_end
= 0;
1278 last_sequence_num
= 0;
1279 printf("jnl: %s: restarting journal replay (%lld - %lld)!\n", jnl
->jdev_name
, jnl
->jhdr
->start
, jnl
->jhdr
->end
);
1280 goto restart_replay
;
1283 jnl
->jhdr
->start
+= blhdr
->bytes_used
;
1284 if (jnl
->jhdr
->start
>= jnl
->jhdr
->size
) {
1285 // wrap around and skip the journal header block
1286 jnl
->jhdr
->start
= (jnl
->jhdr
->start
% jnl
->jhdr
->size
) + jnl
->jhdr
->jhdr_size
;
1289 if (jnl
->jhdr
->start
== jnl
->jhdr
->end
) {
1290 in_uncharted_territory
= 1;
1294 if (jnl
->jhdr
->start
!= jnl
->jhdr
->end
) {
1295 printf("jnl: %s: start %lld != end %lld. resetting end.\n", jnl
->jdev_name
, jnl
->jhdr
->start
, jnl
->jhdr
->end
);
1296 jnl
->jhdr
->end
= jnl
->jhdr
->start
;
1299 //printf("jnl: replay_journal: replaying %d blocks\n", num_full);
1302 * make sure it's at least one page in size, so
1303 * start max_bsize at PAGE_SIZE
1305 for (i
= 0, max_bsize
= PAGE_SIZE
; i
< num_full
; i
++) {
1307 if (co_buf
[i
].block_num
== (off_t
)-1)
1310 if (co_buf
[i
].block_size
> max_bsize
)
1311 max_bsize
= co_buf
[i
].block_size
;
1314 * round max_bsize up to the nearest PAGE_SIZE multiple
1316 if (max_bsize
& (PAGE_SIZE
- 1)) {
1317 max_bsize
= (max_bsize
+ PAGE_SIZE
) & ~(PAGE_SIZE
- 1);
1320 if (kmem_alloc(kernel_map
, (vm_offset_t
*)&block_ptr
, max_bsize
)) {
1324 // Replay the coalesced entries in the co-buf
1325 for(i
=0; i
< num_full
; i
++) {
1326 size_t size
= co_buf
[i
].block_size
;
1327 off_t jnl_offset
= (off_t
) co_buf
[i
].jnl_offset
;
1328 off_t number
= co_buf
[i
].block_num
;
1331 // printf("replaying co_buf[%d]: block 0x%llx, size 0x%x, jnl_offset 0x%llx\n", i, co_buf[i].block_num,
1332 // co_buf[i].block_size, co_buf[i].jnl_offset);
1334 if (number
== (off_t
)-1) {
1335 // printf("jnl: replay_journal: skipping killed fs block\n");
1338 // do journal read, and set the phys. block
1339 ret
= read_journal_data(jnl
, &jnl_offset
, block_ptr
, size
);
1341 printf("jnl: %s: replay_journal: Could not read journal entry data @ offset 0x%llx!\n", jnl
->jdev_name
, offset
);
1345 if (update_fs_block(jnl
, block_ptr
, number
, size
) != 0) {
1352 // done replaying; update jnl header
1353 if (write_journal_header(jnl
, 1) != 0) {
1357 printf("jnl: %s: journal replay done.\n", jnl
->jdev_name
);
1361 kmem_free(kernel_map
, (vm_offset_t
)block_ptr
, max_bsize
);
1365 // free the coalesce buffer
1366 FREE(co_buf
, M_TEMP
);
1369 kmem_free(kernel_map
, (vm_offset_t
)buff
, jnl
->jhdr
->blhdr_size
);
1374 kmem_free(kernel_map
, (vm_offset_t
)block_ptr
, max_bsize
);
1377 FREE(co_buf
, M_TEMP
);
1379 kmem_free(kernel_map
, (vm_offset_t
)buff
, jnl
->jhdr
->blhdr_size
);
1385 #define DEFAULT_TRANSACTION_BUFFER_SIZE (128*1024)
1386 #define MAX_TRANSACTION_BUFFER_SIZE (2048*1024)
1388 // XXXdbg - so I can change it in the debugger
1389 int def_tbuffer_size
= 0;
1393 // This function sets the size of the tbuffer and the
1394 // size of the blhdr. It assumes that jnl->jhdr->size
1395 // and jnl->jhdr->jhdr_size are already valid.
1398 size_up_tbuffer(journal
*jnl
, int tbuffer_size
, int phys_blksz
)
1401 // one-time initialization based on how much memory
1402 // there is in the machine.
1404 if (def_tbuffer_size
== 0) {
1405 if (mem_size
< (256*1024*1024)) {
1406 def_tbuffer_size
= DEFAULT_TRANSACTION_BUFFER_SIZE
;
1407 } else if (mem_size
< (512*1024*1024)) {
1408 def_tbuffer_size
= DEFAULT_TRANSACTION_BUFFER_SIZE
* 2;
1409 } else if (mem_size
< (1024*1024*1024)) {
1410 def_tbuffer_size
= DEFAULT_TRANSACTION_BUFFER_SIZE
* 3;
1412 def_tbuffer_size
= DEFAULT_TRANSACTION_BUFFER_SIZE
* (mem_size
/ (256*1024*1024));
1416 // size up the transaction buffer... can't be larger than the number
1417 // of blocks that can fit in a block_list_header block.
1418 if (tbuffer_size
== 0) {
1419 jnl
->tbuffer_size
= def_tbuffer_size
;
1421 // make sure that the specified tbuffer_size isn't too small
1422 if (tbuffer_size
< jnl
->jhdr
->blhdr_size
* 2) {
1423 tbuffer_size
= jnl
->jhdr
->blhdr_size
* 2;
1425 // and make sure it's an even multiple of the block size
1426 if ((tbuffer_size
% jnl
->jhdr
->jhdr_size
) != 0) {
1427 tbuffer_size
-= (tbuffer_size
% jnl
->jhdr
->jhdr_size
);
1430 jnl
->tbuffer_size
= tbuffer_size
;
1433 if (jnl
->tbuffer_size
> (jnl
->jhdr
->size
/ 2)) {
1434 jnl
->tbuffer_size
= (jnl
->jhdr
->size
/ 2);
1437 if (jnl
->tbuffer_size
> MAX_TRANSACTION_BUFFER_SIZE
) {
1438 jnl
->tbuffer_size
= MAX_TRANSACTION_BUFFER_SIZE
;
1441 jnl
->jhdr
->blhdr_size
= (jnl
->tbuffer_size
/ jnl
->jhdr
->jhdr_size
) * sizeof(block_info
);
1442 if (jnl
->jhdr
->blhdr_size
< phys_blksz
) {
1443 jnl
->jhdr
->blhdr_size
= phys_blksz
;
1444 } else if ((jnl
->jhdr
->blhdr_size
% phys_blksz
) != 0) {
1445 // have to round up so we're an even multiple of the physical block size
1446 jnl
->jhdr
->blhdr_size
= (jnl
->jhdr
->blhdr_size
+ (phys_blksz
- 1)) & ~(phys_blksz
- 1);
1453 get_io_info(struct vnode
*devvp
, size_t phys_blksz
, journal
*jnl
, struct vfs_context
*context
)
1456 off_t writeblockcnt
;
1457 off_t readmaxcnt
=0, tmp_readmaxcnt
;
1458 off_t writemaxcnt
=0, tmp_writemaxcnt
;
1459 off_t readsegcnt
, writesegcnt
;
1462 if (VNOP_IOCTL(devvp
, DKIOCGETFEATURES
, (caddr_t
)&features
, 0, context
) == 0) {
1463 if (features
& DK_FEATURE_FORCE_UNIT_ACCESS
) {
1464 const char *name
= vnode_name(devvp
);
1465 jnl
->flags
|= JOURNAL_DO_FUA_WRITES
;
1466 printf("jnl: %s: enabling FUA writes (features 0x%x)\n", name
? name
: "no-name-dev", features
);
1471 // First check the max read size via several different mechanisms...
1473 VNOP_IOCTL(devvp
, DKIOCGETMAXBYTECOUNTREAD
, (caddr_t
)&readmaxcnt
, 0, context
);
1475 if (VNOP_IOCTL(devvp
, DKIOCGETMAXBLOCKCOUNTREAD
, (caddr_t
)&readblockcnt
, 0, context
) == 0) {
1476 tmp_readmaxcnt
= readblockcnt
* phys_blksz
;
1477 if (readmaxcnt
== 0 || (readblockcnt
> 0 && tmp_readmaxcnt
< readmaxcnt
)) {
1478 readmaxcnt
= tmp_readmaxcnt
;
1482 if (VNOP_IOCTL(devvp
, DKIOCGETMAXSEGMENTCOUNTREAD
, (caddr_t
)&readsegcnt
, 0, context
)) {
1486 if (readsegcnt
> 0 && (readsegcnt
* PAGE_SIZE
) < readmaxcnt
) {
1487 readmaxcnt
= readsegcnt
* PAGE_SIZE
;
1490 if (readmaxcnt
== 0) {
1491 readmaxcnt
= 128 * 1024;
1492 } else if (readmaxcnt
> UINT32_MAX
) {
1493 readmaxcnt
= UINT32_MAX
;
1498 // Now check the max writes size via several different mechanisms...
1500 VNOP_IOCTL(devvp
, DKIOCGETMAXBYTECOUNTWRITE
, (caddr_t
)&writemaxcnt
, 0, context
);
1502 if (VNOP_IOCTL(devvp
, DKIOCGETMAXBLOCKCOUNTWRITE
, (caddr_t
)&writeblockcnt
, 0, context
) == 0) {
1503 tmp_writemaxcnt
= writeblockcnt
* phys_blksz
;
1504 if (writemaxcnt
== 0 || (writeblockcnt
> 0 && tmp_writemaxcnt
< writemaxcnt
)) {
1505 writemaxcnt
= tmp_writemaxcnt
;
1509 if (VNOP_IOCTL(devvp
, DKIOCGETMAXSEGMENTCOUNTWRITE
, (caddr_t
)&writesegcnt
, 0, context
)) {
1513 if (writesegcnt
> 0 && (writesegcnt
* PAGE_SIZE
) < writemaxcnt
) {
1514 writemaxcnt
= writesegcnt
* PAGE_SIZE
;
1517 if (writemaxcnt
== 0) {
1518 writemaxcnt
= 128 * 1024;
1519 } else if (writemaxcnt
> UINT32_MAX
) {
1520 writemaxcnt
= UINT32_MAX
;
1523 jnl
->max_read_size
= readmaxcnt
;
1524 jnl
->max_write_size
= writemaxcnt
;
1525 // printf("jnl: %s: max read/write: %lld k / %lld k\n",
1526 // jnl->jdev_name ? jnl->jdev_name : "unknown",
1527 // jnl->max_read_size/1024, jnl->max_write_size/1024);
1532 get_jdev_name(struct vnode
*jvp
)
1534 const char *jdev_name
;
1536 jdev_name
= vnode_name(jvp
);
1537 if (jdev_name
== NULL
) {
1538 jdev_name
= vfs_addname("unknown-dev", strlen("unknown-dev"), 0, 0);
1540 // this just bumps the refcount on the name so we have our own copy
1541 jdev_name
= vfs_addname(jdev_name
, strlen(jdev_name
), 0, 0);
1549 journal_create(struct vnode
*jvp
,
1553 size_t min_fs_blksz
,
1555 int32_t tbuffer_size
,
1556 void (*flush
)(void *arg
),
1560 uint32_t phys_blksz
, new_txn_base
;
1561 struct vfs_context context
;
1562 const char *jdev_name
;
1564 context
.vc_thread
= current_thread();
1565 context
.vc_ucred
= FSCRED
;
1567 jdev_name
= get_jdev_name(jvp
);
1569 /* Get the real physical block size. */
1570 if (VNOP_IOCTL(jvp
, DKIOCGETBLOCKSIZE
, (caddr_t
)&phys_blksz
, 0, &context
)) {
1574 if (journal_size
< (256*1024) || journal_size
> (1024*1024*1024)) {
1575 printf("jnl: create: journal size %lld looks bogus.\n", journal_size
);
1579 if (phys_blksz
> min_fs_blksz
) {
1580 printf("jnl: %s: create: error: phys blksize %u bigger than min fs blksize %zd\n",
1581 jdev_name
, phys_blksz
, min_fs_blksz
);
1585 if ((journal_size
% phys_blksz
) != 0) {
1586 printf("jnl: %s: create: journal size 0x%llx is not an even multiple of block size 0x%ux\n",
1587 jdev_name
, journal_size
, phys_blksz
);
1592 MALLOC_ZONE(jnl
, struct journal
*, sizeof(struct journal
), M_JNL_JNL
, M_WAITOK
);
1593 memset(jnl
, 0, sizeof(*jnl
));
1596 jnl
->jdev_offset
= offset
;
1599 jnl
->flush_arg
= arg
;
1600 jnl
->flags
= (flags
& JOURNAL_OPTION_FLAGS_MASK
);
1601 jnl
->jdev_name
= jdev_name
;
1602 lck_mtx_init(&jnl
->old_start_lock
, jnl_mutex_group
, jnl_lock_attr
);
1604 get_io_info(jvp
, phys_blksz
, jnl
, &context
);
1606 if (kmem_alloc(kernel_map
, (vm_offset_t
*)&jnl
->header_buf
, phys_blksz
)) {
1607 printf("jnl: %s: create: could not allocate space for header buffer (%u bytes)\n", jdev_name
, phys_blksz
);
1608 goto bad_kmem_alloc
;
1610 jnl
->header_buf_size
= phys_blksz
;
1612 jnl
->jhdr
= (journal_header
*)jnl
->header_buf
;
1613 memset(jnl
->jhdr
, 0, sizeof(journal_header
));
1615 // we have to set this up here so that do_journal_io() will work
1616 jnl
->jhdr
->jhdr_size
= phys_blksz
;
1619 // We try and read the journal header to see if there is already one
1620 // out there. If there is, it's possible that it has transactions
1621 // in it that we might replay if we happen to pick a sequence number
1622 // that is a little less than the old one, there is a crash and the
1623 // last txn written ends right at the start of a txn from the previous
1624 // incarnation of this file system. If all that happens we would
1625 // replay the transactions from the old file system and that would
1626 // destroy your disk. Although it is extremely unlikely for all those
1627 // conditions to happen, the probability is non-zero and the result is
1628 // severe - you lose your file system. Therefore if we find a valid
1629 // journal header and the sequence number is non-zero we write junk
1630 // over the entire journal so that there is no way we will encounter
1631 // any old transactions. This is slow but should be a rare event
1632 // since most tools erase the journal.
1634 if ( read_journal_header(jnl
, jnl
->jhdr
, phys_blksz
) == phys_blksz
1635 && jnl
->jhdr
->magic
== JOURNAL_HEADER_MAGIC
1636 && jnl
->jhdr
->sequence_num
!= 0) {
1638 new_txn_base
= (jnl
->jhdr
->sequence_num
+ (journal_size
/ phys_blksz
) + (random() % 16384)) & 0x00ffffff;
1639 printf("jnl: create: avoiding old sequence number 0x%x (0x%x)\n", jnl
->jhdr
->sequence_num
, new_txn_base
);
1645 for(i
=1; i
< journal_size
/ phys_blksz
; i
++) {
1648 // we don't really care what data we write just so long
1649 // as it's not a valid transaction header. since we have
1650 // the header_buf sitting around we'll use that.
1651 write_journal_data(jnl
, &pos
, jnl
->header_buf
, phys_blksz
);
1653 printf("jnl: create: done clearing journal (i=%d)\n", i
);
1656 new_txn_base
= random() & 0x00ffffff;
1659 memset(jnl
->header_buf
, 0, phys_blksz
);
1661 jnl
->jhdr
->magic
= JOURNAL_HEADER_MAGIC
;
1662 jnl
->jhdr
->endian
= ENDIAN_MAGIC
;
1663 jnl
->jhdr
->start
= phys_blksz
; // start at block #1, block #0 is for the jhdr itself
1664 jnl
->jhdr
->end
= phys_blksz
;
1665 jnl
->jhdr
->size
= journal_size
;
1666 jnl
->jhdr
->jhdr_size
= phys_blksz
;
1667 size_up_tbuffer(jnl
, tbuffer_size
, phys_blksz
);
1669 jnl
->active_start
= jnl
->jhdr
->start
;
1671 // XXXdbg - for testing you can force the journal to wrap around
1672 // jnl->jhdr->start = jnl->jhdr->size - (phys_blksz*3);
1673 // jnl->jhdr->end = jnl->jhdr->size - (phys_blksz*3);
1675 jnl
->jhdr
->sequence_num
= new_txn_base
;
1677 lck_mtx_init(&jnl
->jlock
, jnl_mutex_group
, jnl_lock_attr
);
1679 if (write_journal_header(jnl
, 1) != 0) {
1680 printf("jnl: %s: journal_create: failed to write journal header.\n", jdev_name
);
1688 kmem_free(kernel_map
, (vm_offset_t
)jnl
->header_buf
, phys_blksz
);
1691 vfs_removename(jdev_name
);
1694 FREE_ZONE(jnl
, sizeof(struct journal
), M_JNL_JNL
);
1700 journal_open(struct vnode
*jvp
,
1704 size_t min_fs_blksz
,
1706 int32_t tbuffer_size
,
1707 void (*flush
)(void *arg
),
1711 uint32_t orig_blksz
=0;
1712 uint32_t phys_blksz
;
1713 int orig_checksum
, checksum
;
1714 struct vfs_context context
;
1715 const char *jdev_name
= get_jdev_name(jvp
);
1717 context
.vc_thread
= current_thread();
1718 context
.vc_ucred
= FSCRED
;
1720 /* Get the real physical block size. */
1721 if (VNOP_IOCTL(jvp
, DKIOCGETBLOCKSIZE
, (caddr_t
)&phys_blksz
, 0, &context
)) {
1725 if (phys_blksz
> min_fs_blksz
) {
1726 printf("jnl: %s: open: error: phys blksize %u bigger than min fs blksize %zd\n",
1727 jdev_name
, phys_blksz
, min_fs_blksz
);
1731 if (journal_size
< (256*1024) || journal_size
> (1024*1024*1024)) {
1732 printf("jnl: open: journal size %lld looks bogus.\n", journal_size
);
1736 if ((journal_size
% phys_blksz
) != 0) {
1737 printf("jnl: %s: open: journal size 0x%llx is not an even multiple of block size 0x%x\n",
1738 jdev_name
, journal_size
, phys_blksz
);
1742 MALLOC_ZONE(jnl
, struct journal
*, sizeof(struct journal
), M_JNL_JNL
, M_WAITOK
);
1743 memset(jnl
, 0, sizeof(*jnl
));
1746 jnl
->jdev_offset
= offset
;
1749 jnl
->flush_arg
= arg
;
1750 jnl
->flags
= (flags
& JOURNAL_OPTION_FLAGS_MASK
);
1751 jnl
->jdev_name
= jdev_name
;
1752 lck_mtx_init(&jnl
->old_start_lock
, jnl_mutex_group
, jnl_lock_attr
);
1754 get_io_info(jvp
, phys_blksz
, jnl
, &context
);
1756 if (kmem_alloc(kernel_map
, (vm_offset_t
*)&jnl
->header_buf
, phys_blksz
)) {
1757 printf("jnl: %s: create: could not allocate space for header buffer (%u bytes)\n", jdev_name
, phys_blksz
);
1758 goto bad_kmem_alloc
;
1760 jnl
->header_buf_size
= phys_blksz
;
1762 jnl
->jhdr
= (journal_header
*)jnl
->header_buf
;
1763 memset(jnl
->jhdr
, 0, sizeof(journal_header
));
1765 // we have to set this up here so that do_journal_io() will work
1766 jnl
->jhdr
->jhdr_size
= phys_blksz
;
1768 if (read_journal_header(jnl
, jnl
->jhdr
, phys_blksz
) != phys_blksz
) {
1769 printf("jnl: %s: open: could not read %u bytes for the journal header.\n",
1770 jdev_name
, phys_blksz
);
1774 orig_checksum
= jnl
->jhdr
->checksum
;
1775 jnl
->jhdr
->checksum
= 0;
1777 if (jnl
->jhdr
->magic
== SWAP32(JOURNAL_HEADER_MAGIC
)) {
1778 // do this before the swap since it's done byte-at-a-time
1779 orig_checksum
= SWAP32(orig_checksum
);
1780 checksum
= calc_checksum((char *)jnl
->jhdr
, JOURNAL_HEADER_CKSUM_SIZE
);
1781 swap_journal_header(jnl
);
1782 jnl
->flags
|= JOURNAL_NEED_SWAP
;
1784 checksum
= calc_checksum((char *)jnl
->jhdr
, JOURNAL_HEADER_CKSUM_SIZE
);
1787 if (jnl
->jhdr
->magic
!= JOURNAL_HEADER_MAGIC
&& jnl
->jhdr
->magic
!= OLD_JOURNAL_HEADER_MAGIC
) {
1788 printf("jnl: %s: open: journal magic is bad (0x%x != 0x%x)\n",
1789 jnl
->jdev_name
, jnl
->jhdr
->magic
, JOURNAL_HEADER_MAGIC
);
1793 // only check if we're the current journal header magic value
1794 if (jnl
->jhdr
->magic
== JOURNAL_HEADER_MAGIC
) {
1796 if (orig_checksum
!= checksum
) {
1797 printf("jnl: %s: open: journal checksum is bad (0x%x != 0x%x)\n",
1798 jdev_name
, orig_checksum
, checksum
);
1804 // XXXdbg - convert old style magic numbers to the new one
1805 if (jnl
->jhdr
->magic
== OLD_JOURNAL_HEADER_MAGIC
) {
1806 jnl
->jhdr
->magic
= JOURNAL_HEADER_MAGIC
;
1809 if (phys_blksz
!= (size_t)jnl
->jhdr
->jhdr_size
&& jnl
->jhdr
->jhdr_size
!= 0) {
1811 * The volume has probably been resized (such that we had to adjust the
1812 * logical sector size), or copied to media with a different logical
1815 * Temporarily change the device's logical block size to match the
1816 * journal's header size. This will allow us to replay the journal
1817 * safely. If the replay succeeds, we will update the journal's header
1818 * size (later in this function).
1821 orig_blksz
= phys_blksz
;
1822 phys_blksz
= jnl
->jhdr
->jhdr_size
;
1823 VNOP_IOCTL(jvp
, DKIOCSETBLOCKSIZE
, (caddr_t
)&phys_blksz
, FWRITE
, &context
);
1825 printf("jnl: %s: open: temporarily switched block size from %u to %u\n",
1826 jdev_name
, orig_blksz
, phys_blksz
);
1829 if ( jnl
->jhdr
->start
<= 0
1830 || jnl
->jhdr
->start
> jnl
->jhdr
->size
1831 || jnl
->jhdr
->start
> 1024*1024*1024) {
1832 printf("jnl: %s: open: jhdr start looks bad (0x%llx max size 0x%llx)\n",
1833 jdev_name
, jnl
->jhdr
->start
, jnl
->jhdr
->size
);
1837 if ( jnl
->jhdr
->end
<= 0
1838 || jnl
->jhdr
->end
> jnl
->jhdr
->size
1839 || jnl
->jhdr
->end
> 1024*1024*1024) {
1840 printf("jnl: %s: open: jhdr end looks bad (0x%llx max size 0x%llx)\n",
1841 jdev_name
, jnl
->jhdr
->end
, jnl
->jhdr
->size
);
1845 if (jnl
->jhdr
->size
< (256*1024) || jnl
->jhdr
->size
> 1024*1024*1024) {
1846 printf("jnl: %s: open: jhdr size looks bad (0x%llx)\n", jdev_name
, jnl
->jhdr
->size
);
1850 // XXXdbg - can't do these checks because hfs writes all kinds of
1851 // non-uniform sized blocks even on devices that have a block size
1852 // that is larger than 512 bytes (i.e. optical media w/2k blocks).
1853 // therefore these checks will fail and so we just have to punt and
1854 // do more relaxed checking...
1855 // XXXdbg if ((jnl->jhdr->start % jnl->jhdr->jhdr_size) != 0) {
1856 if ((jnl
->jhdr
->start
% 512) != 0) {
1857 printf("jnl: %s: open: journal start (0x%llx) not a multiple of 512?\n",
1858 jdev_name
, jnl
->jhdr
->start
);
1862 //XXXdbg if ((jnl->jhdr->end % jnl->jhdr->jhdr_size) != 0) {
1863 if ((jnl
->jhdr
->end
% 512) != 0) {
1864 printf("jnl: %s: open: journal end (0x%llx) not a multiple of block size (0x%x)?\n",
1865 jdev_name
, jnl
->jhdr
->end
, jnl
->jhdr
->jhdr_size
);
1869 // take care of replaying the journal if necessary
1870 if (flags
& JOURNAL_RESET
) {
1871 printf("jnl: %s: journal start/end pointers reset! (jnl %p; s 0x%llx e 0x%llx)\n",
1872 jdev_name
, jnl
, jnl
->jhdr
->start
, jnl
->jhdr
->end
);
1873 jnl
->jhdr
->start
= jnl
->jhdr
->end
;
1874 } else if (replay_journal(jnl
) != 0) {
1875 printf("jnl: %s: journal_open: Error replaying the journal!\n", jdev_name
);
1880 * When we get here, we know that the journal is empty (jnl->jhdr->start ==
1881 * jnl->jhdr->end). If the device's logical block size was different from
1882 * the journal's header size, then we can now restore the device's logical
1883 * block size and update the journal's header size to match.
1885 * Note that we also adjust the journal's start and end so that they will
1886 * be aligned on the new block size. We pick a new sequence number to
1887 * avoid any problems if a replay found previous transactions using the old
1888 * journal header size. (See the comments in journal_create(), above.)
1890 if (orig_blksz
!= 0) {
1891 VNOP_IOCTL(jvp
, DKIOCSETBLOCKSIZE
, (caddr_t
)&orig_blksz
, FWRITE
, &context
);
1892 phys_blksz
= orig_blksz
;
1895 jnl
->jhdr
->jhdr_size
= phys_blksz
;
1896 jnl
->jhdr
->start
= phys_blksz
;
1897 jnl
->jhdr
->end
= phys_blksz
;
1898 jnl
->jhdr
->sequence_num
= (jnl
->jhdr
->sequence_num
+
1899 (journal_size
/ phys_blksz
) +
1900 (random() % 16384)) & 0x00ffffff;
1902 if (write_journal_header(jnl
, 1)) {
1903 printf("jnl: %s: open: failed to update journal header size\n", jdev_name
);
1908 // make sure this is in sync!
1909 jnl
->active_start
= jnl
->jhdr
->start
;
1911 // set this now, after we've replayed the journal
1912 size_up_tbuffer(jnl
, tbuffer_size
, phys_blksz
);
1914 // TODO: Does this need to change if the device's logical block size changed?
1915 if ((off_t
)(jnl
->jhdr
->blhdr_size
/sizeof(block_info
)-1) > (jnl
->jhdr
->size
/jnl
->jhdr
->jhdr_size
)) {
1916 printf("jnl: %s: open: jhdr size and blhdr size are not compatible (0x%llx, %d, %d)\n", jdev_name
, jnl
->jhdr
->size
,
1917 jnl
->jhdr
->blhdr_size
, jnl
->jhdr
->jhdr_size
);
1921 lck_mtx_init(&jnl
->jlock
, jnl_mutex_group
, jnl_lock_attr
);
1926 if (orig_blksz
!= 0) {
1927 phys_blksz
= orig_blksz
;
1928 VNOP_IOCTL(jvp
, DKIOCSETBLOCKSIZE
, (caddr_t
)&orig_blksz
, FWRITE
, &context
);
1929 printf("jnl: %s: open: restored block size after error\n", jdev_name
);
1931 kmem_free(kernel_map
, (vm_offset_t
)jnl
->header_buf
, phys_blksz
);
1934 vfs_removename(jdev_name
);
1936 FREE_ZONE(jnl
, sizeof(struct journal
), M_JNL_JNL
);
1942 journal_is_clean(struct vnode
*jvp
,
1946 size_t min_fs_block_size
)
1949 uint32_t phys_blksz
;
1951 int orig_checksum
, checksum
;
1952 struct vfs_context context
;
1953 const char *jdev_name
= get_jdev_name(jvp
);
1955 context
.vc_thread
= current_thread();
1956 context
.vc_ucred
= FSCRED
;
1958 /* Get the real physical block size. */
1959 if (VNOP_IOCTL(jvp
, DKIOCGETBLOCKSIZE
, (caddr_t
)&phys_blksz
, 0, &context
)) {
1960 printf("jnl: %s: is_clean: failed to get device block size.\n", jdev_name
);
1964 if (phys_blksz
> (uint32_t)min_fs_block_size
) {
1965 printf("jnl: %s: is_clean: error: phys blksize %d bigger than min fs blksize %zd\n",
1966 jdev_name
, phys_blksz
, min_fs_block_size
);
1970 if (journal_size
< (256*1024) || journal_size
> (1024*1024*1024)) {
1971 printf("jnl: is_clean: journal size %lld looks bogus.\n", journal_size
);
1975 if ((journal_size
% phys_blksz
) != 0) {
1976 printf("jnl: %s: is_clean: journal size 0x%llx is not an even multiple of block size 0x%x\n",
1977 jdev_name
, journal_size
, phys_blksz
);
1981 memset(&jnl
, 0, sizeof(jnl
));
1983 if (kmem_alloc(kernel_map
, (vm_offset_t
*)&jnl
.header_buf
, phys_blksz
)) {
1984 printf("jnl: %s: is_clean: could not allocate space for header buffer (%d bytes)\n", jdev_name
, phys_blksz
);
1987 jnl
.header_buf_size
= phys_blksz
;
1989 get_io_info(jvp
, phys_blksz
, &jnl
, &context
);
1991 jnl
.jhdr
= (journal_header
*)jnl
.header_buf
;
1992 memset(jnl
.jhdr
, 0, sizeof(journal_header
));
1995 jnl
.jdev_offset
= offset
;
1998 // we have to set this up here so that do_journal_io() will work
1999 jnl
.jhdr
->jhdr_size
= phys_blksz
;
2001 if (read_journal_header(&jnl
, jnl
.jhdr
, phys_blksz
) != (unsigned)phys_blksz
) {
2002 printf("jnl: %s: is_clean: could not read %d bytes for the journal header.\n",
2003 jdev_name
, phys_blksz
);
2008 orig_checksum
= jnl
.jhdr
->checksum
;
2009 jnl
.jhdr
->checksum
= 0;
2011 if (jnl
.jhdr
->magic
== SWAP32(JOURNAL_HEADER_MAGIC
)) {
2012 // do this before the swap since it's done byte-at-a-time
2013 orig_checksum
= SWAP32(orig_checksum
);
2014 checksum
= calc_checksum((char *)jnl
.jhdr
, JOURNAL_HEADER_CKSUM_SIZE
);
2015 swap_journal_header(&jnl
);
2016 jnl
.flags
|= JOURNAL_NEED_SWAP
;
2018 checksum
= calc_checksum((char *)jnl
.jhdr
, JOURNAL_HEADER_CKSUM_SIZE
);
2021 if (jnl
.jhdr
->magic
!= JOURNAL_HEADER_MAGIC
&& jnl
.jhdr
->magic
!= OLD_JOURNAL_HEADER_MAGIC
) {
2022 printf("jnl: %s: is_clean: journal magic is bad (0x%x != 0x%x)\n",
2023 jdev_name
, jnl
.jhdr
->magic
, JOURNAL_HEADER_MAGIC
);
2028 if (orig_checksum
!= checksum
) {
2029 printf("jnl: %s: is_clean: journal checksum is bad (0x%x != 0x%x)\n", jdev_name
, orig_checksum
, checksum
);
2035 // if the start and end are equal then the journal is clean.
2036 // otherwise it's not clean and therefore an error.
2038 if (jnl
.jhdr
->start
== jnl
.jhdr
->end
) {
2041 ret
= EBUSY
; // so the caller can differentiate an invalid journal from a "busy" one
2045 kmem_free(kernel_map
, (vm_offset_t
)jnl
.header_buf
, phys_blksz
);
2047 vfs_removename(jdev_name
);
2057 journal_close(journal
*jnl
)
2059 volatile off_t
*start
, *end
;
2064 // set this before doing anything that would block so that
2065 // we start tearing things down properly.
2067 jnl
->flags
|= JOURNAL_CLOSE_PENDING
;
2069 if (jnl
->owner
!= current_thread()) {
2074 // only write stuff to disk if the journal is still valid
2076 if ((jnl
->flags
& JOURNAL_INVALID
) == 0) {
2078 if (jnl
->active_tr
) {
2079 journal_end_transaction(jnl
);
2082 // flush any buffered transactions
2084 transaction
*tr
= jnl
->cur_tr
;
2087 end_transaction(tr
, 1, NULL
, NULL
); // force it to get flushed
2090 //start = &jnl->jhdr->start;
2091 start
= &jnl
->active_start
;
2092 end
= &jnl
->jhdr
->end
;
2094 while (*start
!= *end
&& counter
++ < 5000) {
2095 //printf("jnl: close: flushing the buffer cache (start 0x%llx end 0x%llx)\n", *start, *end);
2097 jnl
->flush(jnl
->flush_arg
);
2099 tsleep((caddr_t
)jnl
, PRIBIO
, "jnl_close", 2);
2102 if (*start
!= *end
) {
2103 printf("jnl: %s: close: buffer flushing didn't seem to flush out all the transactions! (0x%llx - 0x%llx)\n",
2104 jnl
->jdev_name
, *start
, *end
);
2107 // make sure this is in sync when we close the journal
2108 jnl
->jhdr
->start
= jnl
->active_start
;
2110 // if this fails there's not much we can do at this point...
2111 write_journal_header(jnl
, 1);
2113 // if we're here the journal isn't valid any more.
2114 // so make sure we don't leave any locked blocks lying around
2115 printf("jnl: %s: close: journal %p, is invalid. aborting outstanding transactions\n", jnl
->jdev_name
, jnl
);
2116 if (jnl
->active_tr
|| jnl
->cur_tr
) {
2118 if (jnl
->active_tr
) {
2119 tr
= jnl
->active_tr
;
2120 jnl
->active_tr
= NULL
;
2126 abort_transaction(jnl
, tr
);
2127 if (jnl
->active_tr
|| jnl
->cur_tr
) {
2128 panic("jnl: %s: close: jnl @ %p had both an active and cur tr\n", jnl
->jdev_name
, jnl
);
2133 free_old_stuff(jnl
);
2135 kmem_free(kernel_map
, (vm_offset_t
)jnl
->header_buf
, jnl
->header_buf_size
);
2136 jnl
->jhdr
= (void *)0xbeefbabe;
2138 if (jnl
->jdev_name
) {
2139 vfs_removename(jnl
->jdev_name
);
2142 FREE_ZONE(jnl
, sizeof(struct journal
), M_JNL_JNL
);
2146 dump_journal(journal
*jnl
)
2150 printf("journal for dev %s:", jnl
->jdev_name
);
2151 printf(" jdev_offset %.8llx\n", jnl
->jdev_offset
);
2152 printf(" magic: 0x%.8x\n", jnl
->jhdr
->magic
);
2153 printf(" start: 0x%.8llx\n", jnl
->jhdr
->start
);
2154 printf(" end: 0x%.8llx\n", jnl
->jhdr
->end
);
2155 printf(" size: 0x%.8llx\n", jnl
->jhdr
->size
);
2156 printf(" blhdr size: %d\n", jnl
->jhdr
->blhdr_size
);
2157 printf(" jhdr size: %d\n", jnl
->jhdr
->jhdr_size
);
2158 printf(" chksum: 0x%.8x\n", jnl
->jhdr
->checksum
);
2160 printf(" completed transactions:\n");
2161 for(ctr
=jnl
->completed_trs
; ctr
; ctr
=ctr
->next
) {
2162 printf(" 0x%.8llx - 0x%.8llx\n", ctr
->journal_start
, ctr
->journal_end
);
2169 free_space(journal
*jnl
)
2171 off_t free_space_offset
;
2173 if (jnl
->jhdr
->start
< jnl
->jhdr
->end
) {
2174 free_space_offset
= jnl
->jhdr
->size
- (jnl
->jhdr
->end
- jnl
->jhdr
->start
) - jnl
->jhdr
->jhdr_size
;
2175 } else if (jnl
->jhdr
->start
> jnl
->jhdr
->end
) {
2176 free_space_offset
= jnl
->jhdr
->start
- jnl
->jhdr
->end
;
2178 // journal is completely empty
2179 free_space_offset
= jnl
->jhdr
->size
- jnl
->jhdr
->jhdr_size
;
2182 return free_space_offset
;
2187 // The journal must be locked on entry to this function.
2188 // The "desired_size" is in bytes.
2191 check_free_space(journal
*jnl
, int desired_size
)
2196 //printf("jnl: check free space (desired 0x%x, avail 0x%Lx)\n",
2197 // desired_size, free_space(jnl));
2200 int old_start_empty
;
2202 if (counter
++ == 5000) {
2204 panic("jnl: check_free_space: buffer flushing isn't working "
2205 "(jnl @ %p s %lld e %lld f %lld [active start %lld]).\n", jnl
,
2206 jnl
->jhdr
->start
, jnl
->jhdr
->end
, free_space(jnl
), jnl
->active_start
);
2208 if (counter
> 7500) {
2209 printf("jnl: %s: check_free_space: giving up waiting for free space.\n", jnl
->jdev_name
);
2213 // make sure there's space in the journal to hold this transaction
2214 if (free_space(jnl
) > desired_size
&& jnl
->old_start
[0] == 0) {
2218 // here's where we lazily bump up jnl->jhdr->start. we'll consume
2219 // entries until there is enough space for the next transaction.
2221 old_start_empty
= 1;
2223 for(i
=0; i
< sizeof(jnl
->old_start
)/sizeof(jnl
->old_start
[0]); i
++) {
2227 while (jnl
->old_start
[i
] & 0x8000000000000000LL
) {
2228 if (lcl_counter
++ > 1000) {
2229 panic("jnl: check_free_space: tr starting @ 0x%llx not flushing (jnl %p).\n",
2230 jnl
->old_start
[i
], jnl
);
2233 unlock_oldstart(jnl
);
2235 jnl
->flush(jnl
->flush_arg
);
2237 tsleep((caddr_t
)jnl
, PRIBIO
, "check_free_space1", 1);
2241 if (jnl
->old_start
[i
] == 0) {
2245 old_start_empty
= 0;
2246 jnl
->jhdr
->start
= jnl
->old_start
[i
];
2247 jnl
->old_start
[i
] = 0;
2248 if (free_space(jnl
) > desired_size
) {
2249 unlock_oldstart(jnl
);
2250 write_journal_header(jnl
, 1);
2255 unlock_oldstart(jnl
);
2257 // if we bumped the start, loop and try again
2258 if (i
< sizeof(jnl
->old_start
)/sizeof(jnl
->old_start
[0])) {
2260 } else if (old_start_empty
) {
2262 // if there is nothing in old_start anymore then we can
2263 // bump the jhdr->start to be the same as active_start
2264 // since it is possible there was only one very large
2265 // transaction in the old_start array. if we didn't do
2266 // this then jhdr->start would never get updated and we
2267 // would wind up looping until we hit the panic at the
2268 // start of the loop.
2270 jnl
->jhdr
->start
= jnl
->active_start
;
2271 write_journal_header(jnl
, 1);
2276 // if the file system gave us a flush function, call it to so that
2277 // it can flush some blocks which hopefully will cause some transactions
2278 // to complete and thus free up space in the journal.
2280 jnl
->flush(jnl
->flush_arg
);
2283 // wait for a while to avoid being cpu-bound (this will
2284 // put us to sleep for 10 milliseconds)
2285 tsleep((caddr_t
)jnl
, PRIBIO
, "check_free_space2", 1);
2292 * Allocate a new active transaction.
2295 journal_allocate_transaction(journal
*jnl
)
2299 MALLOC_ZONE(tr
, transaction
*, sizeof(transaction
), M_JNL_TR
, M_WAITOK
);
2300 memset(tr
, 0, sizeof(transaction
));
2302 tr
->tbuffer_size
= jnl
->tbuffer_size
;
2304 if (kmem_alloc(kernel_map
, (vm_offset_t
*)&tr
->tbuffer
, tr
->tbuffer_size
)) {
2305 FREE_ZONE(tr
, sizeof(transaction
), M_JNL_TR
);
2306 jnl
->active_tr
= NULL
;
2310 // journal replay code checksum check depends on this.
2311 memset(tr
->tbuffer
, 0, BLHDR_CHECKSUM_SIZE
);
2312 // Fill up the rest of the block with unimportant bytes (0x5a 'Z' chosen for visibility)
2313 memset(tr
->tbuffer
+ BLHDR_CHECKSUM_SIZE
, 0x5a, jnl
->jhdr
->blhdr_size
- BLHDR_CHECKSUM_SIZE
);
2315 tr
->blhdr
= (block_list_header
*)tr
->tbuffer
;
2316 tr
->blhdr
->max_blocks
= (jnl
->jhdr
->blhdr_size
/ sizeof(block_info
)) - 1;
2317 tr
->blhdr
->num_blocks
= 1; // accounts for this header block
2318 tr
->blhdr
->bytes_used
= jnl
->jhdr
->blhdr_size
;
2319 tr
->blhdr
->flags
= BLHDR_CHECK_CHECKSUMS
| BLHDR_FIRST_HEADER
;
2321 tr
->sequence_num
= ++jnl
->jhdr
->sequence_num
;
2323 tr
->total_bytes
= jnl
->jhdr
->blhdr_size
;
2326 jnl
->active_tr
= tr
;
2332 journal_start_transaction(journal
*jnl
)
2338 if (jnl
->flags
& JOURNAL_INVALID
) {
2342 if (jnl
->owner
== current_thread()) {
2343 if (jnl
->active_tr
== NULL
) {
2344 panic("jnl: start_tr: active_tr is NULL (jnl @ %p, owner %p, current_thread %p\n",
2345 jnl
, jnl
->owner
, current_thread());
2347 jnl
->nested_count
++;
2353 if (jnl
->owner
!= NULL
|| jnl
->nested_count
!= 0 || jnl
->active_tr
!= NULL
) {
2354 panic("jnl: start_tr: owner %p, nested count %d, active_tr %p jnl @ %p\n",
2355 jnl
->owner
, jnl
->nested_count
, jnl
->active_tr
, jnl
);
2358 jnl
->owner
= current_thread();
2359 jnl
->nested_count
= 1;
2361 free_old_stuff(jnl
);
2363 // make sure there's room in the journal
2364 if (free_space(jnl
) < jnl
->tbuffer_size
) {
2365 // this is the call that really waits for space to free up
2366 // as well as updating jnl->jhdr->start
2367 if (check_free_space(jnl
, jnl
->tbuffer_size
) != 0) {
2368 printf("jnl: %s: start transaction failed: no space\n", jnl
->jdev_name
);
2374 // if there's a buffered transaction, use it.
2376 jnl
->active_tr
= jnl
->cur_tr
;
2382 ret
= journal_allocate_transaction(jnl
);
2387 // printf("jnl: start_tr: owner 0x%x new tr @ 0x%x\n", jnl->owner, jnl->active_tr);
2393 jnl
->nested_count
= 0;
2394 unlock_journal(jnl
);
2400 journal_modify_block_start(journal
*jnl
, struct buf
*bp
)
2406 if (jnl
->flags
& JOURNAL_INVALID
) {
2410 // XXXdbg - for debugging I want this to be true. later it may
2411 // not be necessary.
2412 if ((buf_flags(bp
) & B_META
) == 0) {
2413 panic("jnl: modify_block_start: bp @ %p is not a meta-data block! (jnl %p)\n", bp
, jnl
);
2416 tr
= jnl
->active_tr
;
2417 CHECK_TRANSACTION(tr
);
2419 if (jnl
->owner
!= current_thread()) {
2420 panic("jnl: modify_block_start: called w/out a transaction! jnl %p, owner %p, curact %p\n",
2421 jnl
, jnl
->owner
, current_thread());
2424 free_old_stuff(jnl
);
2426 //printf("jnl: mod block start (bp 0x%x vp 0x%x l/blkno %qd/%qd bsz %d; total bytes %d)\n",
2427 // bp, buf_vnode(bp), buf_lblkno(bp), buf_blkno(bp), buf_size(bp), tr->total_bytes);
2429 // can't allow blocks that aren't an even multiple of the
2430 // underlying block size.
2431 if ((buf_size(bp
) % jnl
->jhdr
->jhdr_size
) != 0) {
2432 uint32_t phys_blksz
, bad
=0;
2434 if (VNOP_IOCTL(jnl
->jdev
, DKIOCGETBLOCKSIZE
, (caddr_t
)&phys_blksz
, 0, vfs_context_kernel())) {
2436 } else if (phys_blksz
!= (uint32_t)jnl
->jhdr
->jhdr_size
) {
2437 if (phys_blksz
< 512) {
2438 panic("jnl: mod block start: phys blksz %d is too small (%d, %d)\n",
2439 phys_blksz
, buf_size(bp
), jnl
->jhdr
->jhdr_size
);
2442 if ((buf_size(bp
) % phys_blksz
) != 0) {
2444 } else if (phys_blksz
< (uint32_t)jnl
->jhdr
->jhdr_size
) {
2445 jnl
->jhdr
->jhdr_size
= phys_blksz
;
2447 // the phys_blksz is now larger... need to realloc the jhdr
2448 char *new_header_buf
;
2450 printf("jnl: %s: phys blksz got bigger (was: %d/%d now %d)\n",
2451 jnl
->jdev_name
, jnl
->header_buf_size
, jnl
->jhdr
->jhdr_size
, phys_blksz
);
2452 if (kmem_alloc(kernel_map
, (vm_offset_t
*)&new_header_buf
, phys_blksz
)) {
2453 printf("jnl: modify_block_start: %s: create: phys blksz change (was %d, now %d) but could not allocate space for new header\n",
2454 jnl
->jdev_name
, jnl
->jhdr
->jhdr_size
, phys_blksz
);
2457 memcpy(new_header_buf
, jnl
->header_buf
, jnl
->header_buf_size
);
2458 memset(&new_header_buf
[jnl
->header_buf_size
], 0x18, (phys_blksz
- jnl
->header_buf_size
));
2459 kmem_free(kernel_map
, (vm_offset_t
)jnl
->header_buf
, jnl
->header_buf_size
);
2460 jnl
->header_buf
= new_header_buf
;
2461 jnl
->header_buf_size
= phys_blksz
;
2463 jnl
->jhdr
= (journal_header
*)jnl
->header_buf
;
2464 jnl
->jhdr
->jhdr_size
= phys_blksz
;
2472 panic("jnl: mod block start: bufsize %d not a multiple of block size %d\n",
2473 buf_size(bp
), jnl
->jhdr
->jhdr_size
);
2478 // make sure that this transaction isn't bigger than the whole journal
2479 if (tr
->total_bytes
+buf_size(bp
) >= (jnl
->jhdr
->size
- jnl
->jhdr
->jhdr_size
)) {
2480 panic("jnl: transaction too big (%d >= %lld bytes, bufsize %d, tr %p bp %p)\n",
2481 tr
->total_bytes
, (tr
->jnl
->jhdr
->size
- jnl
->jhdr
->jhdr_size
), buf_size(bp
), tr
, bp
);
2485 // if the block is dirty and not already locked we have to write
2486 // it out before we muck with it because it has data that belongs
2487 // (presumably) to another transaction.
2489 if ((buf_flags(bp
) & (B_DELWRI
| B_LOCKED
)) == B_DELWRI
) {
2491 if (buf_flags(bp
) & B_ASYNC
) {
2492 panic("modify_block_start: bp @ %p has async flag set!\n", bp
);
2495 // this will cause it to not be buf_brelse()'d
2496 buf_setflags(bp
, B_NORELSE
);
2499 buf_setflags(bp
, B_LOCKED
);
2505 journal_modify_block_abort(journal
*jnl
, struct buf
*bp
)
2508 block_list_header
*blhdr
;
2513 tr
= jnl
->active_tr
;
2516 // if there's no active transaction then we just want to
2517 // call buf_brelse() and return since this is just a block
2518 // that happened to be modified as part of another tr.
2525 if (jnl
->flags
& JOURNAL_INVALID
) {
2526 /* Still need to buf_brelse(). Callers assume we consume the bp. */
2531 CHECK_TRANSACTION(tr
);
2533 if (jnl
->owner
!= current_thread()) {
2534 panic("jnl: modify_block_abort: called w/out a transaction! jnl %p, owner %p, curact %p\n",
2535 jnl
, jnl
->owner
, current_thread());
2538 free_old_stuff(jnl
);
2540 // printf("jnl: modify_block_abort: tr 0x%x bp 0x%x\n", jnl->active_tr, bp);
2542 // first check if it's already part of this transaction
2543 for(blhdr
=tr
->blhdr
; blhdr
; blhdr
=(block_list_header
*)((long)blhdr
->binfo
[0].bnum
)) {
2544 for(i
=1; i
< blhdr
->num_blocks
; i
++) {
2545 if (bp
== blhdr
->binfo
[i
].u
.bp
) {
2550 if (i
< blhdr
->num_blocks
) {
2556 // if blhdr is null, then this block has only had modify_block_start
2557 // called on it as part of the current transaction. that means that
2558 // it is ok to clear the LOCKED bit since it hasn't actually been
2559 // modified. if blhdr is non-null then modify_block_end was called
2560 // on it and so we need to keep it locked in memory.
2562 if (blhdr
== NULL
) {
2563 buf_clearflags(bp
, B_LOCKED
);
2572 journal_modify_block_end(journal
*jnl
, struct buf
*bp
, void (*func
)(struct buf
*bp
, void *arg
), void *arg
)
2575 int tbuffer_offset
=0;
2577 block_list_header
*blhdr
, *prev
=NULL
;
2582 if (jnl
->flags
& JOURNAL_INVALID
) {
2583 /* Still need to buf_brelse(). Callers assume we consume the bp. */
2588 tr
= jnl
->active_tr
;
2589 CHECK_TRANSACTION(tr
);
2591 if (jnl
->owner
!= current_thread()) {
2592 panic("jnl: modify_block_end: called w/out a transaction! jnl %p, owner %p, curact %p\n",
2593 jnl
, jnl
->owner
, current_thread());
2596 free_old_stuff(jnl
);
2598 //printf("jnl: mod block end: (bp 0x%x vp 0x%x l/blkno %qd/%qd bsz %d, total bytes %d)\n",
2599 // bp, buf_vnode(bp), buf_lblkno(bp), buf_blkno(bp), buf_size(bp), tr->total_bytes);
2601 if ((buf_flags(bp
) & B_LOCKED
) == 0) {
2602 panic("jnl: modify_block_end: bp %p not locked! jnl @ %p\n", bp
, jnl
);
2605 // first check if it's already part of this transaction
2606 for(blhdr
=tr
->blhdr
; blhdr
; prev
=blhdr
,blhdr
=(block_list_header
*)((long)blhdr
->binfo
[0].bnum
)) {
2607 tbuffer_offset
= jnl
->jhdr
->blhdr_size
;
2609 for(i
=1; i
< blhdr
->num_blocks
; i
++) {
2610 if (bp
== blhdr
->binfo
[i
].u
.bp
) {
2613 if (blhdr
->binfo
[i
].bnum
!= (off_t
)-1) {
2614 tbuffer_offset
+= buf_size(blhdr
->binfo
[i
].u
.bp
);
2616 tbuffer_offset
+= blhdr
->binfo
[i
].u
.bi
.bsize
;
2620 if (i
< blhdr
->num_blocks
) {
2627 && (prev
->num_blocks
+1) <= prev
->max_blocks
2628 && (prev
->bytes_used
+buf_size(bp
)) <= (uint32_t)tr
->tbuffer_size
) {
2630 } else if (blhdr
== NULL
) {
2631 block_list_header
*nblhdr
;
2634 panic("jnl: modify block end: no way man, prev == NULL?!?, jnl %p, bp %p\n", jnl
, bp
);
2637 // we got to the end of the list, didn't find the block and there's
2638 // no room in the block_list_header pointed to by prev
2640 // we allocate another tbuffer and link it in at the end of the list
2641 // through prev->binfo[0].bnum. that's a skanky way to do things but
2642 // avoids having yet another linked list of small data structures to manage.
2644 if (kmem_alloc(kernel_map
, (vm_offset_t
*)&nblhdr
, tr
->tbuffer_size
)) {
2645 panic("jnl: end_tr: no space for new block tr @ %p (total bytes: %d)!\n",
2646 tr
, tr
->total_bytes
);
2649 // journal replay code checksum check depends on this.
2650 memset(nblhdr
, 0, BLHDR_CHECKSUM_SIZE
);
2651 // Fill up the rest of the block with unimportant bytes
2652 memset(nblhdr
+ BLHDR_CHECKSUM_SIZE
, 0x5a, jnl
->jhdr
->blhdr_size
- BLHDR_CHECKSUM_SIZE
);
2654 // initialize the new guy
2655 nblhdr
->max_blocks
= (jnl
->jhdr
->blhdr_size
/ sizeof(block_info
)) - 1;
2656 nblhdr
->num_blocks
= 1; // accounts for this header block
2657 nblhdr
->bytes_used
= jnl
->jhdr
->blhdr_size
;
2658 nblhdr
->flags
= BLHDR_CHECK_CHECKSUMS
;
2661 tr
->total_bytes
+= jnl
->jhdr
->blhdr_size
;
2663 // then link him in at the end
2664 prev
->binfo
[0].bnum
= (off_t
)((long)nblhdr
);
2666 // and finally switch to using the new guy
2668 tbuffer_offset
= jnl
->jhdr
->blhdr_size
;
2673 if ((i
+1) > blhdr
->max_blocks
) {
2674 panic("jnl: modify_block_end: i = %d, max_blocks %d\n", i
, blhdr
->max_blocks
);
2677 // if the function pointer is not set then copy the
2678 // block of data now. if the function pointer is set
2679 // the copy will happen after calling the callback in
2680 // end_transaction() just before it goes to disk.
2683 blkptr
= (char *)&((char *)blhdr
)[tbuffer_offset
];
2684 memcpy(blkptr
, (char *)0 + buf_dataptr(bp
), buf_size(bp
));
2687 // if this is true then this is a new block we haven't seen
2688 if (i
>= blhdr
->num_blocks
) {
2694 bsize
= buf_size(bp
);
2696 blhdr
->binfo
[i
].bnum
= (off_t
)(buf_blkno(bp
));
2697 blhdr
->binfo
[i
].u
.bp
= bp
;
2699 void *old_func
=NULL
, *old_arg
=NULL
;
2701 buf_setfilter(bp
, func
, arg
, &old_func
, &old_arg
);
2702 if (old_func
!= NULL
&& old_func
!= func
) {
2703 panic("jnl: modify_block_end: old func %p / arg %p (func %p)", old_func
, old_arg
, func
);
2707 blhdr
->bytes_used
+= bsize
;
2708 tr
->total_bytes
+= bsize
;
2710 blhdr
->num_blocks
++;
2718 journal_kill_block(journal
*jnl
, struct buf
*bp
)
2722 block_list_header
*blhdr
;
2727 if (jnl
->flags
& JOURNAL_INVALID
) {
2731 tr
= jnl
->active_tr
;
2732 CHECK_TRANSACTION(tr
);
2734 if (jnl
->owner
!= current_thread()) {
2735 panic("jnl: modify_block_end: called w/out a transaction! jnl %p, owner %p, curact %p\n",
2736 jnl
, jnl
->owner
, current_thread());
2739 free_old_stuff(jnl
);
2741 bflags
= buf_flags(bp
);
2743 if ( !(bflags
& B_LOCKED
))
2744 panic("jnl: modify_block_end: called with bp not B_LOCKED");
2747 * bp must be BL_BUSY and B_LOCKED
2749 // first check if it's already part of this transaction
2750 for(blhdr
=tr
->blhdr
; blhdr
; blhdr
=(block_list_header
*)((long)blhdr
->binfo
[0].bnum
)) {
2752 for(i
=1; i
< blhdr
->num_blocks
; i
++) {
2753 if (bp
== blhdr
->binfo
[i
].u
.bp
) {
2756 buf_clearflags(bp
, B_LOCKED
);
2758 // this undoes the vnode_ref() in journal_modify_block_end()
2760 vnode_rele_ext(vp
, 0, 1);
2762 // if the block has the DELWRI and FILTER bits sets, then
2763 // things are seriously weird. if it was part of another
2764 // transaction then journal_modify_block_start() should
2765 // have force it to be written.
2767 //if ((bflags & B_DELWRI) && (bflags & B_FILTER)) {
2768 // panic("jnl: kill block: this defies all logic! bp 0x%x\n", bp);
2770 tr
->num_killed
+= buf_size(bp
);
2772 blhdr
->binfo
[i
].bnum
= (off_t
)-1;
2773 blhdr
->binfo
[i
].u
.bp
= NULL
;
2774 blhdr
->binfo
[i
].u
.bi
.bsize
= buf_size(bp
);
2776 buf_markinvalid(bp
);
2783 if (i
< blhdr
->num_blocks
) {
2793 ;________________________________________________________________________________
2795 ; Routine: journal_trim_realloc
2797 ; Function: Increase the amount of memory allocated for the list of extents
2798 ; to be unmapped (trimmed). This routine will be called when
2799 ; adding an extent to the list, and the list already occupies
2800 ; all of the space allocated to it. This routine returns ENOMEM
2801 ; if unable to allocate more space, or 0 if the extent list was
2802 ; grown successfully.
2805 ; tr - The transaction containing the extent list.
2808 ; (result) - ENOMEM or 0.
2811 ; The allocated_count and extents fields of tr->trim are updated
2812 ; if the function returned 0.
2813 ;________________________________________________________________________________
2816 journal_trim_realloc(transaction
*tr
)
2818 if (CONFIG_HFS_TRIM
) {
2820 uint32_t new_allocated_count
;
2822 new_allocated_count
= tr
->trim
.allocated_count
+ JOURNAL_DEFAULT_TRIM_EXTENTS
;
2823 new_extents
= kalloc(new_allocated_count
* sizeof(dk_extent_t
));
2824 if (new_extents
== NULL
) {
2825 printf("journal_trim_realloc: unable to grow extent list!\n");
2827 * Since we could be called when allocating space previously marked
2828 * to be trimmed, we need to empty out the list to be safe.
2830 tr
->trim
.extent_count
= 0;
2834 /* Copy the old extent list to the newly allocated list. */
2835 if (tr
->trim
.extents
!= NULL
) {
2836 memmove(new_extents
,
2838 tr
->trim
.allocated_count
* sizeof(dk_extent_t
));
2839 kfree(tr
->trim
.extents
,
2840 tr
->trim
.allocated_count
* sizeof(dk_extent_t
));
2843 tr
->trim
.allocated_count
= new_allocated_count
;
2844 tr
->trim
.extents
= new_extents
;
2851 ;________________________________________________________________________________
2853 ; Routine: journal_trim_add_extent
2855 ; Function: Make note of a range of bytes that should be unmapped
2856 ; (trimmed). That is, the given range of bytes no longer have
2857 ; useful content, and the device can unmap the previous
2858 ; contents. For example, a solid state disk may reuse the
2859 ; underlying storage for other blocks.
2861 ; The extent will be unmapped after the transaction is written
2865 ; jnl - The journal for the volume containing the byte range.
2866 ; offset - The first byte of the range to be trimmed.
2867 ; length - The number of bytes of the extent being trimmed.
2868 ;________________________________________________________________________________
2870 __private_extern__
int
2871 journal_trim_add_extent(journal
*jnl
, uint64_t offset
, uint64_t length
)
2873 if (CONFIG_HFS_TRIM
) {
2876 dk_extent_t
*extent
;
2877 uint32_t insert_index
;
2878 uint32_t replace_count
;
2882 if (jnl
->flags
& JOURNAL_TRIM_ERR
) {
2884 * A previous trim failed, so we have disabled trim for this volume
2885 * for as long as it remains mounted.
2890 if (jnl
->flags
& JOURNAL_INVALID
) {
2894 tr
= jnl
->active_tr
;
2895 CHECK_TRANSACTION(tr
);
2897 if (jnl
->owner
!= current_thread()) {
2898 panic("jnl: trim_add_extent: called w/out a transaction! jnl %p, owner %p, curact %p\n",
2899 jnl
, jnl
->owner
, current_thread());
2902 free_old_stuff(jnl
);
2904 end
= offset
+ length
;
2907 * Find the range of existing extents that can be combined with the
2908 * input extent. We start by counting the number of extents that end
2909 * strictly before the input extent, then count the number of extents
2910 * that overlap or are contiguous with the input extent.
2912 extent
= tr
->trim
.extents
;
2914 while (insert_index
< tr
->trim
.extent_count
&& extent
->offset
+ extent
->length
< offset
) {
2919 while (insert_index
+ replace_count
< tr
->trim
.extent_count
&& extent
->offset
<= end
) {
2925 * If none of the existing extents can be combined with the input extent,
2926 * then just insert it in the list (before item number insert_index).
2928 if (replace_count
== 0) {
2929 /* If the list was already full, we need to grow it. */
2930 if (tr
->trim
.extent_count
== tr
->trim
.allocated_count
) {
2931 if (journal_trim_realloc(tr
) != 0) {
2932 printf("jnl: trim_add_extent: out of memory!");
2937 /* Shift any existing extents with larger offsets. */
2938 if (insert_index
< tr
->trim
.extent_count
) {
2939 memmove(&tr
->trim
.extents
[insert_index
+1],
2940 &tr
->trim
.extents
[insert_index
],
2941 (tr
->trim
.extent_count
- insert_index
) * sizeof(dk_extent_t
));
2943 tr
->trim
.extent_count
++;
2945 /* Store the new extent in the list. */
2946 tr
->trim
.extents
[insert_index
].offset
= offset
;
2947 tr
->trim
.extents
[insert_index
].length
= length
;
2954 * Update extent number insert_index to be the union of the input extent
2955 * and all of the replaced extents.
2957 if (tr
->trim
.extents
[insert_index
].offset
< offset
)
2958 offset
= tr
->trim
.extents
[insert_index
].offset
;
2959 extent
= &tr
->trim
.extents
[insert_index
+ replace_count
- 1];
2960 if (extent
->offset
+ extent
->length
> end
)
2961 end
= extent
->offset
+ extent
->length
;
2962 tr
->trim
.extents
[insert_index
].offset
= offset
;
2963 tr
->trim
.extents
[insert_index
].length
= end
- offset
;
2966 * If we were replacing more than one existing extent, then shift any
2967 * extents with larger offsets, and update the count of extents.
2969 * We're going to leave extent #insert_index alone since it was just updated, above.
2970 * We need to move extents from index (insert_index + replace_count) through the end of
2971 * the list by (replace_count - 1) positions so that they overwrite extent #(insert_index + 1).
2973 if (replace_count
> 1 && (insert_index
+ replace_count
) < tr
->trim
.extent_count
) {
2974 memmove(&tr
->trim
.extents
[insert_index
+ 1],
2975 &tr
->trim
.extents
[insert_index
+ replace_count
],
2976 (tr
->trim
.extent_count
- insert_index
- replace_count
) * sizeof(dk_extent_t
));
2978 tr
->trim
.extent_count
-= replace_count
- 1;
2985 ;________________________________________________________________________________
2987 ; Routine: journal_trim_remove_extent
2989 ; Function: Make note of a range of bytes, some of which may have previously
2990 ; been passed to journal_trim_add_extent, is now in use on the
2991 ; volume. The given bytes will be not be trimmed as part of
2995 ; jnl - The journal for the volume containing the byte range.
2996 ; offset - The first byte of the range to be trimmed.
2997 ; length - The number of bytes of the extent being trimmed.
2998 ;________________________________________________________________________________
3000 __private_extern__
int
3001 journal_trim_remove_extent(journal
*jnl
, uint64_t offset
, uint64_t length
)
3003 if (CONFIG_HFS_TRIM
) {
3005 dk_extent_t
*extent
;
3007 u_int32_t keep_before
;
3008 u_int32_t keep_after
;
3012 if (jnl
->flags
& JOURNAL_TRIM_ERR
) {
3014 * A previous trim failed, so we have disabled trim for this volume
3015 * for as long as it remains mounted.
3020 if (jnl
->flags
& JOURNAL_INVALID
) {
3024 tr
= jnl
->active_tr
;
3025 CHECK_TRANSACTION(tr
);
3027 if (jnl
->owner
!= current_thread()) {
3028 panic("jnl: trim_remove_extent: called w/out a transaction! jnl %p, owner %p, curact %p\n",
3029 jnl
, jnl
->owner
, current_thread());
3032 free_old_stuff(jnl
);
3034 end
= offset
+ length
;
3037 * Find any existing extents that start before or end after the input
3038 * extent. These extents will be modified if they overlap the input
3039 * extent. Other extents between them will be deleted.
3041 extent
= tr
->trim
.extents
;
3043 while (keep_before
< tr
->trim
.extent_count
&& extent
->offset
< offset
) {
3047 keep_after
= keep_before
;
3048 if (keep_after
> 0) {
3049 /* See if previous extent extends beyond both ends of input extent. */
3053 while (keep_after
< tr
->trim
.extent_count
&& (extent
->offset
+ extent
->length
) <= end
) {
3059 * When we get here, the first keep_before extents (0 .. keep_before-1)
3060 * start before the input extent, and extents (keep_after .. extent_count-1)
3061 * end after the input extent. We'll need to keep, all of those extents,
3062 * but possibly modify #(keep_before-1) and #keep_after to remove the portion
3063 * that overlaps with the input extent.
3067 * Does the input extent start after and end before the same existing
3068 * extent? If so, we have to "punch a hole" in that extent and convert
3069 * it to two separate extents.
3071 if (keep_before
> keep_after
) {
3072 /* If the list was already full, we need to grow it. */
3073 if (tr
->trim
.extent_count
== tr
->trim
.allocated_count
) {
3074 if (journal_trim_realloc(tr
) != 0) {
3075 printf("jnl: trim_remove_extent: out of memory!");
3081 * Make room for a new extent by shifting extents #keep_after and later
3082 * down by one extent. When we're done, extents #keep_before and
3083 * #keep_after will be identical, and we can fall through to removing
3084 * the portion that overlaps the input extent.
3086 memmove(&tr
->trim
.extents
[keep_before
],
3087 &tr
->trim
.extents
[keep_after
],
3088 (tr
->trim
.extent_count
- keep_after
) * sizeof(dk_extent_t
));
3089 ++tr
->trim
.extent_count
;
3093 * Fall through. We now have the case where the length of extent
3094 * #(keep_before - 1) needs to be updated, and the start of extent
3095 * #(keep_after) needs to be updated.
3100 * May need to truncate the end of extent #(keep_before - 1) if it overlaps
3103 if (keep_before
> 0) {
3104 extent
= &tr
->trim
.extents
[keep_before
- 1];
3105 if (extent
->offset
+ extent
->length
> offset
) {
3106 extent
->length
= offset
- extent
->offset
;
3111 * May need to update the start of extent #(keep_after) if it overlaps the
3114 if (keep_after
< tr
->trim
.extent_count
) {
3115 extent
= &tr
->trim
.extents
[keep_after
];
3116 if (extent
->offset
< end
) {
3117 extent
->length
= extent
->offset
+ extent
->length
- end
;
3118 extent
->offset
= end
;
3123 * If there were whole extents that overlapped the input extent, get rid
3124 * of them by shifting any following extents, and updating the count.
3126 if (keep_after
> keep_before
&& keep_after
< tr
->trim
.extent_count
) {
3127 memmove(&tr
->trim
.extents
[keep_before
],
3128 &tr
->trim
.extents
[keep_after
],
3129 (tr
->trim
.extent_count
- keep_after
) * sizeof(dk_extent_t
));
3131 tr
->trim
.extent_count
-= keep_after
- keep_before
;
3138 journal_trim_flush(journal
*jnl
, transaction
*tr
)
3142 if (CONFIG_HFS_TRIM
) {
3143 if ((jnl
->flags
& JOURNAL_TRIM_ERR
) == 0 && tr
->trim
.extent_count
> 0) {
3146 bzero(&unmap
, sizeof(unmap
));
3147 unmap
.extents
= tr
->trim
.extents
;
3148 unmap
.extentsCount
= tr
->trim
.extent_count
;
3149 errno
= VNOP_IOCTL(jnl
->fsdev
, DKIOCUNMAP
, (caddr_t
)&unmap
, FWRITE
, vfs_context_kernel());
3151 printf("jnl: error %d from DKIOCUNMAP (extents=%lx, count=%u); disabling trim for %s\n",
3152 errno
, (unsigned long) (tr
->trim
.extents
), tr
->trim
.extent_count
,
3154 jnl
->flags
|= JOURNAL_TRIM_ERR
;
3157 if (tr
->trim
.extents
) {
3158 kfree(tr
->trim
.extents
, tr
->trim
.allocated_count
* sizeof(dk_extent_t
));
3159 tr
->trim
.allocated_count
= 0;
3160 tr
->trim
.extent_count
= 0;
3161 tr
->trim
.extents
= NULL
;
3170 journal_binfo_cmp(const void *a
, const void *b
)
3172 const block_info
*bi_a
= (const struct block_info
*)a
;
3173 const block_info
*bi_b
= (const struct block_info
*)b
;
3176 if (bi_a
->bnum
== (off_t
)-1) {
3179 if (bi_b
->bnum
== (off_t
)-1) {
3183 // don't have to worry about negative block
3184 // numbers so this is ok to do.
3186 res
= (buf_blkno(bi_a
->u
.bp
) - buf_blkno(bi_b
->u
.bp
));
3193 * End a transaction. If the transaction is small enough, and we're not forcing
3194 * a write to disk, the "active" transaction becomes the "current" transaction,
3195 * and will be reused for the next transaction that is started (group commit).
3197 * If the transaction gets written to disk (because force_it is true, or no
3198 * group commit, or the transaction is sufficiently full), the blocks get
3199 * written into the journal first, then the are written asynchronously. When
3200 * those async writes complete, the transaction can be freed and removed from
3203 * An optional callback can be supplied. If given, it is called after the
3204 * the blocks have been written to the journal, but before the async writes
3205 * of those blocks to their normal on-disk locations. This is used by
3206 * journal_relocate so that the location of the journal can be changed and
3207 * flushed to disk before the blocks get written to their normal locations.
3208 * Note that the callback is only called if the transaction gets written to
3209 * the journal during this end_transaction call; you probably want to set the
3213 * tr Transaction to add to the journal
3214 * force_it If true, force this transaction to the on-disk journal immediately.
3215 * callback See description above. Pass NULL for no callback.
3216 * callback_arg Argument passed to callback routine.
3220 * -1 An error occurred. The journal is marked invalid.
3223 end_transaction(transaction
*tr
, int force_it
, errno_t (*callback
)(void*), void *callback_arg
)
3228 journal
*jnl
= tr
->jnl
;
3229 struct buf
*bp
, **bparray
;
3230 block_list_header
*blhdr
=NULL
, *next
=NULL
;
3231 size_t tbuffer_offset
;
3234 panic("jnl: jnl @ %p already has cur_tr %p, new tr: %p\n",
3235 jnl
, jnl
->cur_tr
, tr
);
3238 // if there weren't any modified blocks in the transaction
3239 // just save off the transaction pointer and return.
3240 if (tr
->total_bytes
== jnl
->jhdr
->blhdr_size
) {
3245 // if our transaction buffer isn't very full, just hang
3246 // on to it and don't actually flush anything. this is
3247 // what is known as "group commit". we will flush the
3248 // transaction buffer if it's full or if we have more than
3249 // one of them so we don't start hogging too much memory.
3251 // We also check the number of extents waiting to be trimmed.
3252 // If it is small enough, then keep accumulating more (so we
3253 // can reduce the overhead of trimming). If there was a
3254 // prior trim error, then we stop issuing trims for this
3255 // volume, so we can also coalesce transactions.
3258 && (jnl
->flags
& JOURNAL_NO_GROUP_COMMIT
) == 0
3259 && tr
->num_blhdrs
< 3
3260 && (tr
->total_bytes
<= ((tr
->tbuffer_size
*tr
->num_blhdrs
) - tr
->tbuffer_size
/8))
3261 && ((jnl
->flags
& JOURNAL_TRIM_ERR
) || (tr
->trim
.extent_count
< jnl_trim_flush_limit
))) {
3268 // if we're here we're going to flush the transaction buffer to disk.
3269 // make sure there is room in the journal first.
3270 check_free_space(jnl
, tr
->total_bytes
);
3272 // range check the end index
3273 if (jnl
->jhdr
->end
<= 0 || jnl
->jhdr
->end
> jnl
->jhdr
->size
) {
3274 panic("jnl: end_transaction: end is bogus 0x%llx (sz 0x%llx)\n",
3275 jnl
->jhdr
->end
, jnl
->jhdr
->size
);
3278 // this transaction starts where the current journal ends
3279 tr
->journal_start
= jnl
->jhdr
->end
;
3280 end
= jnl
->jhdr
->end
;
3283 // if the first entry in old_start[] isn't free yet, loop calling the
3284 // file system flush routine until it is (or we panic).
3288 while ((jnl
->old_start
[0] & 0x8000000000000000LL
) != 0) {
3290 unlock_oldstart(jnl
);
3293 jnl
->flush(jnl
->flush_arg
);
3296 // yield the cpu so others can get in to clear the lock bit
3297 (void)tsleep((void *)jnl
, PRIBIO
, "jnl-old-start-sleep", 1);
3302 panic("jnl: transaction that started at 0x%llx is not completing! jnl %p\n",
3303 jnl
->old_start
[0] & (~0x8000000000000000LL
), jnl
);
3308 // slide everyone else down and put our latest guy in the last
3309 // entry in the old_start array
3312 /* Because old_start is locked above, we can cast away the volatile qualifier before passing it to memcpy. */
3313 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]));
3314 jnl
->old_start
[sizeof(jnl
->old_start
)/sizeof(jnl
->old_start
[0]) - 1] = tr
->journal_start
| 0x8000000000000000LL
;
3316 unlock_oldstart(jnl
);
3319 // for each block, make sure that the physical block # is set
3320 for(blhdr
=tr
->blhdr
; blhdr
; blhdr
=next
) {
3323 tbuffer_offset
= jnl
->jhdr
->blhdr_size
;
3324 for(i
=1; i
< blhdr
->num_blocks
; i
++) {
3329 bp
= blhdr
->binfo
[i
].u
.bp
;
3331 // if this block has a callback function set, call
3332 // it now and then copy the data from the bp into
3334 if (blhdr
->binfo
[i
].bnum
!= (off_t
)-1) {
3335 void (*func
)(struct buf
*, void *);
3339 panic("jnl: inconsistent binfo (NULL bp w/bnum %lld; jnl @ %p, tr %p)\n",
3340 blhdr
->binfo
[i
].bnum
, jnl
, tr
);
3343 buf_setfilter(bp
, NULL
, NULL
, (void **)&func
, &arg
);
3346 // acquire the bp here so that we can safely
3347 // mess around with its data. buf_acquire()
3348 // will return EAGAIN if the buffer was busy,
3349 // so loop trying again.
3351 errno
= buf_acquire(bp
, 0, 0, 0);
3352 } while (errno
== EAGAIN
);
3356 // call the hook function and then copy the
3357 // data into the transaction buffer...
3360 blkptr
= (char *)&((char *)blhdr
)[tbuffer_offset
];
3361 memcpy(blkptr
, (char *)buf_dataptr(bp
), buf_size(bp
));
3365 panic("could not acquire bp %p (err %d)\n", bp
, errno
);
3369 } else { // bnum == -1, only true if a block was "killed"
3371 tbuffer_offset
+= blhdr
->binfo
[i
].u
.bi
.bsize
;
3375 tbuffer_offset
+= buf_size(bp
);
3378 blkno
= buf_blkno(bp
);
3379 lblkno
= buf_lblkno(bp
);
3381 if (vp
== NULL
&& lblkno
== blkno
) {
3382 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",
3383 jnl
->jdev_name
, bp
, lblkno
, blkno
, tr
, jnl
);
3387 // if the lblkno is the same as blkno and this bp isn't
3388 // associated with the underlying file system device then
3389 // we need to call bmap() to get the actual physical block.
3391 if ((lblkno
== blkno
) && (vp
!= jnl
->fsdev
)) {
3393 size_t contig_bytes
;
3395 if (VNOP_BLKTOOFF(vp
, lblkno
, &f_offset
)) {
3396 printf("jnl: %s: end_tr: vnop_blktooff failed @ %p, jnl %p\n", jnl
->jdev_name
, bp
, jnl
);
3399 if (VNOP_BLOCKMAP(vp
, f_offset
, buf_count(bp
), &blkno
, &contig_bytes
, NULL
, 0, NULL
)) {
3400 printf("jnl: %s: end_tr: can't blockmap the bp @ %p, jnl %p\n", jnl
->jdev_name
, bp
, jnl
);
3403 if ((uint32_t)contig_bytes
< buf_count(bp
)) {
3404 printf("jnl: %s: end_tr: blk not physically contiguous on disk@ %p, jnl %p\n", jnl
->jdev_name
, bp
, jnl
);
3407 buf_setblkno(bp
, blkno
);
3409 // update this so we write out the correct physical block number!
3410 blhdr
->binfo
[i
].bnum
= (off_t
)(blkno
);
3413 next
= (block_list_header
*)((long)blhdr
->binfo
[0].bnum
);
3418 for(blhdr
=tr
->blhdr
; blhdr
; blhdr
=(block_list_header
*)((long)blhdr
->binfo
[0].bnum
)) {
3419 amt
= blhdr
->bytes_used
;
3421 blhdr
->binfo
[0].u
.bi
.b
.sequence_num
= tr
->sequence_num
;
3423 blhdr
->checksum
= 0;
3424 blhdr
->checksum
= calc_checksum((char *)blhdr
, BLHDR_CHECKSUM_SIZE
);
3426 if (kmem_alloc(kernel_map
, (vm_offset_t
*)&bparray
, blhdr
->num_blocks
* sizeof(struct buf
*))) {
3427 panic("can't allocate %zd bytes for bparray\n", blhdr
->num_blocks
* sizeof(struct buf
*));
3430 // calculate individual block checksums
3431 tbuffer_offset
= jnl
->jhdr
->blhdr_size
;
3432 for(i
=1; i
< blhdr
->num_blocks
; i
++) {
3435 if (blhdr
->binfo
[i
].bnum
!= (off_t
)-1) {
3436 bparray
[i
] = blhdr
->binfo
[i
].u
.bp
;
3437 bsize
= buf_size(bparray
[i
]);
3438 blhdr
->binfo
[i
].u
.bi
.bsize
= bsize
;
3439 blhdr
->binfo
[i
].u
.bi
.b
.cksum
= calc_checksum(&((char *)blhdr
)[tbuffer_offset
], bsize
);
3442 bsize
= blhdr
->binfo
[i
].u
.bi
.bsize
;
3443 blhdr
->binfo
[i
].u
.bi
.b
.cksum
= 0;
3446 tbuffer_offset
+= bsize
;
3449 ret
= write_journal_data(jnl
, &end
, blhdr
, amt
);
3451 // always put the bp pointers back
3452 for(i
=1; i
< blhdr
->num_blocks
; i
++) {
3453 if (blhdr
->binfo
[i
].bnum
!= (off_t
)-1) {
3454 blhdr
->binfo
[i
].u
.bp
= bparray
[i
];
3458 kmem_free(kernel_map
, (vm_offset_t
)bparray
, blhdr
->num_blocks
* sizeof(struct buf
*));
3461 printf("jnl: %s: end_transaction: only wrote %d of %d bytes to the journal!\n",
3462 jnl
->jdev_name
, ret
, amt
);
3468 jnl
->jhdr
->end
= end
; // update where the journal now ends
3469 tr
->journal_end
= end
; // the transaction ends here too
3470 if (tr
->journal_start
== 0 || tr
->journal_end
== 0) {
3471 panic("jnl: end_transaction: bad tr journal start/end: 0x%llx 0x%llx\n",
3472 tr
->journal_start
, tr
->journal_end
);
3475 if (write_journal_header(jnl
, 0) != 0) {
3480 * If the caller supplied a callback, call it now that the blocks have been
3481 * written to the journal. This is used by journal_relocate so, for example,
3482 * the file system can change its pointer to the new journal.
3484 if (callback
!= NULL
&& callback(callback_arg
) != 0) {
3489 // Send a DKIOCUNMAP for the extents trimmed by this transaction, and
3490 // free up the extent list.
3492 errno
= journal_trim_flush(jnl
, tr
);
3495 // setup for looping through all the blhdr's. we null out the
3496 // tbuffer and blhdr fields so that they're not used any more.
3502 // the buffer_flushed_callback will only be called for the
3503 // real blocks that get flushed so we have to account for
3504 // the block_list_headers here.
3506 tr
->num_flushed
= tr
->num_blhdrs
* jnl
->jhdr
->blhdr_size
;
3508 // for each block, set the iodone callback and unlock it
3509 for(; blhdr
; blhdr
=next
) {
3511 // we can re-order the buf ptrs because everything is written out already
3512 qsort(&blhdr
->binfo
[1], blhdr
->num_blocks
-1, sizeof(block_info
), journal_binfo_cmp
);
3514 for(i
=1; i
< blhdr
->num_blocks
; i
++) {
3515 if (blhdr
->binfo
[i
].bnum
== (off_t
)-1) {
3519 bp
= blhdr
->binfo
[i
].u
.bp
;
3521 // have to pass BAC_REMOVE here because we're going to bawrite()
3522 // the buffer when we're done
3524 errno
= buf_acquire(bp
, BAC_REMOVE
, 0, 0);
3525 } while (errno
== EAGAIN
);
3528 struct vnode
*save_vp
;
3531 if ((buf_flags(bp
) & (B_LOCKED
|B_DELWRI
)) != (B_LOCKED
|B_DELWRI
)) {
3532 if (jnl
->flags
& JOURNAL_CLOSE_PENDING
) {
3533 buf_clearflags(bp
, B_LOCKED
);
3537 panic("jnl: end_tr: !!!DANGER!!! bp %p flags (0x%x) not LOCKED & DELWRI\n", bp
, buf_flags(bp
));
3540 save_vp
= buf_vnode(bp
);
3542 buf_setfilter(bp
, buffer_flushed_callback
, tr
, &cur_filter
, NULL
);
3545 panic("jnl: bp @ %p (blkno %qd, vp %p) has non-null iodone (%p) buffflushcb %p\n",
3546 bp
, buf_blkno(bp
), save_vp
, cur_filter
, buffer_flushed_callback
);
3548 buf_clearflags(bp
, B_LOCKED
);
3550 // kicking off the write here helps performance
3552 // XXXdbg this is good for testing: buf_bdwrite(bp);
3555 // this undoes the vnode_ref() in journal_modify_block_end()
3556 vnode_rele_ext(save_vp
, 0, 1);
3558 printf("jnl: %s: end_transaction: could not acquire block %p (errno %d)!\n",
3559 jnl
->jdev_name
,bp
, errno
);
3563 next
= (block_list_header
*)((long)blhdr
->binfo
[0].bnum
);
3565 // we can free blhdr here since we won't need it any more
3566 blhdr
->binfo
[0].bnum
= 0xdeadc0de;
3567 kmem_free(kernel_map
, (vm_offset_t
)blhdr
, tr
->tbuffer_size
);
3570 //printf("jnl: end_tr: tr @ 0x%x, jnl-blocks: 0x%llx - 0x%llx. exit!\n",
3571 // tr, tr->journal_start, tr->journal_end);
3576 jnl
->flags
|= JOURNAL_INVALID
;
3577 jnl
->old_start
[sizeof(jnl
->old_start
)/sizeof(jnl
->old_start
[0]) - 1] &= ~0x8000000000000000LL
;
3578 abort_transaction(jnl
, tr
); // cleans up list of extents to be trimmed
3583 abort_transaction(journal
*jnl
, transaction
*tr
)
3587 block_list_header
*blhdr
, *next
;
3589 struct vnode
*save_vp
;
3591 // for each block list header, iterate over the blocks then
3592 // free up the memory associated with the block list.
3594 // for each block, clear the lock bit and release it.
3596 for(blhdr
=tr
->blhdr
; blhdr
; blhdr
=next
) {
3598 for(i
=1; i
< blhdr
->num_blocks
; i
++) {
3599 if (blhdr
->binfo
[i
].bnum
== (off_t
)-1) {
3602 if ( (buf_vnode(blhdr
->binfo
[i
].u
.bp
) == NULL
) ||
3603 !(buf_flags(blhdr
->binfo
[i
].u
.bp
) & B_LOCKED
) ) {
3607 errno
= buf_meta_bread(buf_vnode(blhdr
->binfo
[i
].u
.bp
),
3608 buf_lblkno(blhdr
->binfo
[i
].u
.bp
),
3609 buf_size(blhdr
->binfo
[i
].u
.bp
),
3613 if (bp
!= blhdr
->binfo
[i
].u
.bp
) {
3614 panic("jnl: abort_tr: got back a different bp! (bp %p should be %p, jnl %p\n",
3615 bp
, blhdr
->binfo
[i
].u
.bp
, jnl
);
3618 // releasing a bp marked invalid
3619 // also clears the locked and delayed state
3620 buf_markinvalid(bp
);
3621 save_vp
= buf_vnode(bp
);
3625 vnode_rele_ext(save_vp
, 0, 1);
3627 printf("jnl: %s: abort_tr: could not find block %Ld vp %p!\n",
3628 jnl
->jdev_name
, blhdr
->binfo
[i
].bnum
, blhdr
->binfo
[i
].u
.bp
);
3635 next
= (block_list_header
*)((long)blhdr
->binfo
[0].bnum
);
3637 // we can free blhdr here since we won't need it any more
3638 blhdr
->binfo
[0].bnum
= 0xdeadc0de;
3639 kmem_free(kernel_map
, (vm_offset_t
)blhdr
, tr
->tbuffer_size
);
3642 if (tr
->trim
.extents
) {
3643 kfree(tr
->trim
.extents
, tr
->trim
.allocated_count
* sizeof(dk_extent_t
));
3645 tr
->trim
.allocated_count
= 0;
3646 tr
->trim
.extent_count
= 0;
3647 tr
->trim
.extents
= NULL
;
3650 tr
->total_bytes
= 0xdbadc0de;
3651 FREE_ZONE(tr
, sizeof(transaction
), M_JNL_TR
);
3656 journal_end_transaction(journal
*jnl
)
3663 if ((jnl
->flags
& JOURNAL_INVALID
) && jnl
->owner
== NULL
) {
3667 if (jnl
->owner
!= current_thread()) {
3668 panic("jnl: end_tr: I'm not the owner! jnl %p, owner %p, curact %p\n",
3669 jnl
, jnl
->owner
, current_thread());
3672 free_old_stuff(jnl
);
3674 jnl
->nested_count
--;
3675 if (jnl
->nested_count
> 0) {
3677 } else if (jnl
->nested_count
< 0) {
3678 panic("jnl: jnl @ %p has negative nested count (%d). bad boy.\n", jnl
, jnl
->nested_count
);
3681 if (jnl
->flags
& JOURNAL_INVALID
) {
3682 if (jnl
->active_tr
) {
3683 if (jnl
->cur_tr
!= NULL
) {
3684 panic("jnl: journal @ %p has active tr (%p) and cur tr (%p)\n",
3685 jnl
, jnl
->active_tr
, jnl
->cur_tr
);
3688 tr
= jnl
->active_tr
;
3689 jnl
->active_tr
= NULL
;
3690 abort_transaction(jnl
, tr
);
3694 unlock_journal(jnl
);
3699 tr
= jnl
->active_tr
;
3700 CHECK_TRANSACTION(tr
);
3702 // clear this out here so that when check_free_space() calls
3703 // the FS flush function, we don't panic in journal_flush()
3704 // if the FS were to call that. note: check_free_space() is
3705 // called from end_transaction().
3707 jnl
->active_tr
= NULL
;
3708 ret
= end_transaction(tr
, 0, NULL
, NULL
);
3711 unlock_journal(jnl
);
3718 journal_flush(journal
*jnl
)
3720 int need_signal
= 0;
3724 if (jnl
->flags
& JOURNAL_INVALID
) {
3728 KERNEL_DEBUG_CONSTANT((FSDBG_CODE(DBG_JOURNAL
, DBG_JOURNAL_FLUSH
))
3729 | DBG_FUNC_START
, 0, 0, 0, 0, 0);
3731 if (jnl
->owner
!= current_thread()) {
3736 free_old_stuff(jnl
);
3738 // if we're not active, flush any buffered transactions
3739 if (jnl
->active_tr
== NULL
&& jnl
->cur_tr
) {
3740 transaction
*tr
= jnl
->cur_tr
;
3743 end_transaction(tr
, 1, NULL
, NULL
); // force it to get flushed
3747 unlock_journal(jnl
);
3750 KERNEL_DEBUG_CONSTANT((FSDBG_CODE(DBG_JOURNAL
, DBG_JOURNAL_FLUSH
))
3751 | DBG_FUNC_END
, 0, 0, 0, 0, 0);
3757 journal_active(journal
*jnl
)
3759 if (jnl
->flags
& JOURNAL_INVALID
) {
3763 return (jnl
->active_tr
== NULL
) ? 0 : 1;
3767 journal_owner(journal
*jnl
)
3772 int journal_uses_fua(journal
*jnl
)
3774 if (jnl
->flags
& JOURNAL_DO_FUA_WRITES
)
3780 * Relocate the journal.
3782 * You provide the new starting offset and size for the journal. You may
3783 * optionally provide a new tbuffer_size; passing zero defaults to not
3784 * changing the tbuffer size except as needed to fit within the new journal
3787 * You must have already started a transaction. The transaction may contain
3788 * modified blocks (such as those needed to deallocate the old journal,
3789 * allocate the new journal, and update the location and size of the journal
3790 * in filesystem-private structures). Any transactions prior to the active
3791 * transaction will be flushed to the old journal. The new journal will be
3792 * initialized, and the blocks from the active transaction will be written to
3795 * The caller will need to update the structures that identify the location
3796 * and size of the journal. These updates should be made in the supplied
3797 * callback routine. These updates must NOT go into a transaction. You should
3798 * force these updates to the media before returning from the callback. In the
3799 * even of a crash, either the old journal will be found, with an empty journal,
3800 * or the new journal will be found with the contents of the active transaction.
3802 * Upon return from the callback, the blocks from the active transaction are
3803 * written to their normal locations on disk.
3805 * (Remember that we have to ensure that blocks get committed to the journal
3806 * before being committed to their normal locations. But the blocks don't count
3807 * as committed until the new journal is pointed at.)
3809 * Upon return, there is still an active transaction: newly allocated, and
3810 * with no modified blocks. Call journal_end_transaction as normal. You may
3811 * modifiy additional blocks before calling journal_end_transaction, and those
3812 * blocks will (eventually) go to the relocated journal.
3815 * jnl The (opened) journal to relocate.
3816 * offset The new journal byte offset (from start of the journal device).
3817 * journal_size The size, in bytes, of the new journal.
3818 * tbuffer_size The new desired transaction buffer size. Pass zero to keep
3819 * the same size as the current journal. The size will be
3820 * modified as needed to fit the new journal.
3821 * callback Routine called after the new journal has been initialized,
3822 * and the active transaction written to the new journal, but
3823 * before the blocks are written to their normal locations.
3824 * Pass NULL for no callback.
3825 * callback_arg An argument passed to the callback routine.
3829 * EINVAL The offset is not block aligned
3830 * EINVAL The journal_size is not a multiple of the block size
3831 * EINVAL The journal is invalid
3832 * (any) An error returned by journal_flush.
3835 int journal_relocate(journal
*jnl
, off_t offset
, off_t journal_size
, int32_t tbuffer_size
,
3836 errno_t (*callback
)(void *), void *callback_arg
)
3842 * Sanity check inputs, and adjust the size of the transaction buffer.
3844 if ((offset
% jnl
->jhdr
->jhdr_size
) != 0) {
3845 printf("jnl: %s: relocate: offset 0x%llx is not an even multiple of block size 0x%x\n",
3846 jnl
->jdev_name
, offset
, jnl
->jhdr
->jhdr_size
);
3849 if ((journal_size
% jnl
->jhdr
->jhdr_size
) != 0) {
3850 printf("jnl: %s: relocate: journal size 0x%llx is not an even multiple of block size 0x%x\n",
3851 jnl
->jdev_name
, journal_size
, jnl
->jhdr
->jhdr_size
);
3857 /* Guarantee we own the active transaction. */
3858 if (jnl
->flags
& JOURNAL_INVALID
) {
3861 if (jnl
->owner
!= current_thread()) {
3862 panic("jnl: relocate: Not the owner! jnl %p, owner %p, curact %p\n",
3863 jnl
, jnl
->owner
, current_thread());
3866 if (tbuffer_size
== 0)
3867 tbuffer_size
= jnl
->tbuffer_size
;
3868 size_up_tbuffer(jnl
, tbuffer_size
, jnl
->jhdr
->jhdr_size
);
3871 * Flush any non-active transactions. We have to temporarily hide the
3872 * active transaction to make journal_flush flush out non-active but
3873 * current (unwritten) transactions.
3875 tr
= jnl
->active_tr
;
3876 CHECK_TRANSACTION(tr
);
3877 jnl
->active_tr
= NULL
;
3878 ret
= journal_flush(jnl
);
3879 jnl
->active_tr
= tr
;
3884 /* Update the journal's offset and size in memory. */
3885 jnl
->jdev_offset
= offset
;
3886 jnl
->jhdr
->start
= jnl
->jhdr
->end
= jnl
->jhdr
->jhdr_size
;
3887 jnl
->jhdr
->size
= journal_size
;
3888 jnl
->active_start
= jnl
->jhdr
->start
;
3891 * Force the active transaction to be written to the new journal. Call the
3892 * supplied callback after the blocks have been written to the journal, but
3893 * before they get written to their normal on-disk locations.
3895 jnl
->active_tr
= NULL
;
3896 ret
= end_transaction(tr
, 1, callback
, callback_arg
);
3898 printf("jnl: %s: relocate: end_transaction failed (%d)\n", jnl
->jdev_name
, ret
);
3903 * Create a new, empty transaction to be the active transaction. This way
3904 * our caller can use journal_end_transaction as usual.
3906 ret
= journal_allocate_transaction(jnl
);
3908 printf("jnl: %s: relocate: could not allocate new transaction (%d)\n", jnl
->jdev_name
, ret
);
3915 jnl
->flags
|= JOURNAL_INVALID
;
3916 abort_transaction(jnl
, tr
);
3921 #else // !JOURNALING - so provide stub functions
3923 int journal_uses_fua(__unused journal
*jnl
)
3929 journal_create(__unused
struct vnode
*jvp
,
3930 __unused off_t offset
,
3931 __unused off_t journal_size
,
3932 __unused
struct vnode
*fsvp
,
3933 __unused
size_t min_fs_blksz
,
3934 __unused
int32_t flags
,
3935 __unused
int32_t tbuffer_size
,
3936 __unused
void (*flush
)(void *arg
),
3943 journal_open(__unused
struct vnode
*jvp
,
3944 __unused off_t offset
,
3945 __unused off_t journal_size
,
3946 __unused
struct vnode
*fsvp
,
3947 __unused
size_t min_fs_blksz
,
3948 __unused
int32_t flags
,
3949 __unused
int32_t tbuffer_size
,
3950 __unused
void (*flush
)(void *arg
),
3958 journal_modify_block_start(__unused journal
*jnl
, __unused
struct buf
*bp
)
3964 journal_modify_block_end(__unused journal
*jnl
,
3965 __unused
struct buf
*bp
,
3966 __unused
void (*func
)(struct buf
*bp
, void *arg
),
3973 journal_kill_block(__unused journal
*jnl
, __unused
struct buf
*bp
)
3978 int journal_relocate(__unused journal
*jnl
,
3979 __unused off_t offset
,
3980 __unused off_t journal_size
,
3981 __unused
int32_t tbuffer_size
,
3982 __unused
errno_t (*callback
)(void *),
3983 __unused
void *callback_arg
)
3989 journal_close(__unused journal
*jnl
)
3994 journal_start_transaction(__unused journal
*jnl
)
4000 journal_end_transaction(__unused journal
*jnl
)
4006 journal_flush(__unused journal
*jnl
)
4012 journal_is_clean(__unused
struct vnode
*jvp
,
4013 __unused off_t offset
,
4014 __unused off_t journal_size
,
4015 __unused
struct vnode
*fsvp
,
4016 __unused
size_t min_fs_block_size
)
4023 journal_owner(__unused journal
*jnl
)
4027 #endif // !JOURNALING