2 * Copyright (c) 1995-2002 Apple Computer, Inc. All rights reserved.
4 * @APPLE_LICENSE_HEADER_START@
6 * Copyright (c) 1999-2003 Apple Computer, Inc. All Rights Reserved.
8 * This file contains Original Code and/or Modifications of Original Code
9 * as defined in and that are subject to the Apple Public Source License
10 * Version 2.0 (the 'License'). You may not use this file except in
11 * compliance with the License. Please obtain a copy of the License at
12 * http://www.opensource.apple.com/apsl/ and read it before using this
15 * The Original Code and all software distributed under the License are
16 * distributed on an 'AS IS' basis, WITHOUT WARRANTY OF ANY KIND, EITHER
17 * EXPRESS OR IMPLIED, AND APPLE HEREBY DISCLAIMS ALL SUCH WARRANTIES,
18 * INCLUDING WITHOUT LIMITATION, ANY WARRANTIES OF MERCHANTABILITY,
19 * FITNESS FOR A PARTICULAR PURPOSE, QUIET ENJOYMENT OR NON-INFRINGEMENT.
20 * Please see the License for the specific language governing rights and
21 * limitations under the License.
23 * @APPLE_LICENSE_HEADER_END@
26 // This file implements a simple write-ahead journaling layer.
27 // In theory any file system can make use of it by calling these
28 // functions when the fs wants to modify meta-data blocks. See
29 // vfs_journal.h for a more detailed description of the api and
32 // Dominic Giampaolo (dbg@apple.com)
37 #include <sys/param.h>
38 #include <sys/systm.h>
39 #include <sys/kernel.h>
44 #include <sys/mount.h>
45 #include <sys/namei.h>
46 #include <sys/vnode.h>
47 #include <sys/ioctl.h>
50 #include <sys/malloc.h>
51 #include <sys/vnode.h>
52 #include <kern/thread_act.h>
54 #include <miscfs/specfs/specdev.h>
56 extern task_t kernel_task
;
68 #include <sys/types.h>
73 #include "vfs_journal.h"
76 // number of bytes to checksum in a block_list_header
77 // NOTE: this should be enough to clear out the header
78 // fields as well as the first entry of binfo[]
79 #define BLHDR_CHECKSUM_SIZE 32
83 static int end_transaction(transaction
*tr
, int force_it
);
84 static void abort_transaction(journal
*jnl
, transaction
*tr
);
85 static void dump_journal(journal
*jnl
);
89 // 3105942 - Coalesce writes to the same block on journal replay
92 typedef struct bucket
{
98 #define STARTING_BUCKETS 256
100 static int add_block(journal
*jnl
, struct bucket
**buf_ptr
, off_t block_num
, size_t size
, size_t offset
, int *num_buckets_ptr
, int *num_full_ptr
);
101 static int grow_table(struct bucket
**buf_ptr
, int num_buckets
, int new_size
);
102 static int lookup_bucket(struct bucket
**buf_ptr
, off_t block_num
, int num_full
);
103 static int do_overlap(journal
*jnl
, struct bucket
**buf_ptr
, int blk_index
, off_t block_num
, size_t size
, size_t offset
, int *num_buckets_ptr
, int *num_full_ptr
);
104 static int insert_block(journal
*jnl
, struct bucket
**buf_ptr
, int blk_index
, off_t num
, size_t size
, size_t offset
, int *num_buckets_ptr
, int *num_full_ptr
, int overwriting
);
106 #define CHECK_JOURNAL(jnl) \
109 panic("%s:%d: null journal ptr?\n", __FILE__, __LINE__);\
111 if (jnl->jdev == NULL) { \
112 panic("%s:%d: jdev is null!\n", __FILE__, __LINE__);\
114 if (jnl->fsdev == NULL) { \
115 panic("%s:%d: fsdev is null!\n", __FILE__, __LINE__);\
117 if (jnl->jhdr->magic != JOURNAL_HEADER_MAGIC) {\
118 panic("%s:%d: jhdr magic corrupted (0x%x != 0x%x)\n",\
119 __FILE__, __LINE__, jnl->jhdr->magic, JOURNAL_HEADER_MAGIC);\
121 if ( jnl->jhdr->start <= 0 \
122 || jnl->jhdr->start > jnl->jhdr->size\
123 || jnl->jhdr->start > 1024*1024*1024) {\
124 panic("%s:%d: jhdr start looks bad (0x%llx max size 0x%llx)\n", \
125 __FILE__, __LINE__, jnl->jhdr->start, jnl->jhdr->size);\
127 if ( jnl->jhdr->end <= 0 \
128 || jnl->jhdr->end > jnl->jhdr->size\
129 || jnl->jhdr->end > 1024*1024*1024) {\
130 panic("%s:%d: jhdr end looks bad (0x%llx max size 0x%llx)\n", \
131 __FILE__, __LINE__, jnl->jhdr->end, jnl->jhdr->size);\
133 if (jnl->jhdr->size > 1024*1024*1024) {\
134 panic("%s:%d: jhdr size looks bad (0x%llx)\n",\
135 __FILE__, __LINE__, jnl->jhdr->size);\
139 #define CHECK_TRANSACTION(tr) \
142 panic("%s:%d: null transaction ptr?\n", __FILE__, __LINE__);\
144 if (tr->jnl == NULL) {\
145 panic("%s:%d: null tr->jnl ptr?\n", __FILE__, __LINE__);\
147 if (tr->blhdr != (block_list_header *)tr->tbuffer) {\
148 panic("%s:%d: blhdr (0x%x) != tbuffer (0x%x)\n", __FILE__, __LINE__, tr->blhdr, tr->tbuffer);\
150 if (tr->total_bytes < 0) {\
151 panic("%s:%d: tr total_bytes looks bad: %d\n", __FILE__, __LINE__, tr->total_bytes);\
153 if (tr->journal_start < 0 || tr->journal_start > 1024*1024*1024) {\
154 panic("%s:%d: tr journal start looks bad: 0x%llx\n", __FILE__, __LINE__, tr->journal_start);\
156 if (tr->journal_end < 0 || tr->journal_end > 1024*1024*1024) {\
157 panic("%s:%d: tr journal end looks bad: 0x%llx\n", __FILE__, __LINE__, tr->journal_end);\
159 if (tr->blhdr && (tr->blhdr->max_blocks <= 0 || tr->blhdr->max_blocks > (tr->jnl->jhdr->size/tr->jnl->jhdr->jhdr_size))) {\
160 panic("%s:%d: tr blhdr max_blocks looks bad: %d\n", __FILE__, __LINE__, tr->blhdr->max_blocks);\
167 // this isn't a great checksum routine but it will do for now.
168 // we use it to checksum the journal header and the block list
169 // headers that are at the start of each transaction.
172 calc_checksum(char *ptr
, int len
)
176 // this is a lame checksum but for now it'll do
177 for(i
=0; i
< len
; i
++, ptr
++) {
178 cksum
= (cksum
<< 8) ^ (cksum
+ *(unsigned char *)ptr
);
185 #define JNL_WRITE 0x0001
186 #define JNL_READ 0x0002
187 #define JNL_HEADER 0x8000
190 // This function sets up a fake buf and passes it directly to the
191 // journal device strategy routine (so that it won't get cached in
194 // It also handles range checking the i/o so that we don't write
195 // outside the journal boundaries and it will wrap the i/o back
196 // to the beginning if necessary (skipping over the journal header)
199 do_journal_io(journal
*jnl
, off_t
*offset
, void *data
, size_t len
, int direction
)
201 int err
, io_sz
=0, curlen
=len
;
203 int max_iosize
=0, max_vectors
;
205 if (*offset
< 0 || *offset
> jnl
->jhdr
->size
) {
206 panic("jnl: do_jnl_io: bad offset 0x%llx (max 0x%llx)\n", *offset
, jnl
->jhdr
->size
);
210 bp
= alloc_io_buf(jnl
->jdev
, 1);
212 if (direction
& JNL_WRITE
) {
213 bp
->b_flags
|= 0; // don't have to set any flags (was: B_WRITEINPROG)
214 jnl
->jdev
->v_numoutput
++;
215 vfs_io_attributes(jnl
->jdev
, B_WRITE
, &max_iosize
, &max_vectors
);
216 } else if (direction
& JNL_READ
) {
217 bp
->b_flags
|= B_READ
;
218 vfs_io_attributes(jnl
->jdev
, B_READ
, &max_iosize
, &max_vectors
);
221 if (max_iosize
== 0) {
222 max_iosize
= 128 * 1024;
225 if (*offset
+ (off_t
)curlen
> jnl
->jhdr
->size
&& *offset
!= 0 && jnl
->jhdr
->size
!= 0) {
226 if (*offset
== jnl
->jhdr
->size
) {
227 *offset
= jnl
->jhdr
->jhdr_size
;
229 curlen
= (off_t
)jnl
->jhdr
->size
- *offset
;
233 if (curlen
> max_iosize
) {
238 panic("jnl: do_jnl_io: curlen == %d, offset 0x%llx len %d\n", curlen
, *offset
, len
);
241 if (*offset
== 0 && (direction
& JNL_HEADER
) == 0) {
242 panic("jnl: request for i/o to jnl-header without JNL_HEADER flag set! (len %d, data %p)\n", curlen
, data
);
245 bp
->b_bufsize
= curlen
;
246 bp
->b_bcount
= curlen
;
248 bp
->b_blkno
= (daddr_t
) ((jnl
->jdev_offset
+ *offset
) / (off_t
)jnl
->jhdr
->jhdr_size
);
249 bp
->b_lblkno
= (daddr_t
) ((jnl
->jdev_offset
+ *offset
) / (off_t
)jnl
->jhdr
->jhdr_size
);
251 err
= VOP_STRATEGY(bp
);
257 bp
->b_bufsize
= bp
->b_bcount
= 0;
258 bp
->b_blkno
= bp
->b_lblkno
= -1;
263 printf("jnl: do_jnl_io: strategy err 0x%x\n", err
);
270 // handle wrap-around
271 data
= (char *)data
+ curlen
;
272 curlen
= len
- io_sz
;
273 if (*offset
>= jnl
->jhdr
->size
) {
274 *offset
= jnl
->jhdr
->jhdr_size
;
283 read_journal_data(journal
*jnl
, off_t
*offset
, void *data
, size_t len
)
285 return do_journal_io(jnl
, offset
, data
, len
, JNL_READ
);
289 write_journal_data(journal
*jnl
, off_t
*offset
, void *data
, size_t len
)
291 return do_journal_io(jnl
, offset
, data
, len
, JNL_WRITE
);
296 read_journal_header(journal
*jnl
, void *data
, size_t len
)
298 off_t hdr_offset
= 0;
300 return do_journal_io(jnl
, &hdr_offset
, data
, len
, JNL_READ
|JNL_HEADER
);
304 write_journal_header(journal
*jnl
)
306 static int num_err_prints
= 0;
308 off_t jhdr_offset
= 0;
311 // XXXdbg note: this ioctl doesn't seem to do anything on firewire disks.
313 ret
= VOP_IOCTL(jnl
->jdev
, DKIOCSYNCHRONIZECACHE
, NULL
, FWRITE
, NOCRED
, current_proc());
316 // Only print this error if it's a different error than the
317 // previous one, or if it's the first time for this device
318 // or if the total number of printfs is less than 25. We
319 // allow for up to 25 printfs to insure that some make it
320 // into the on-disk syslog. Otherwise if we only printed
321 // one, it's possible it would never make it to the syslog
322 // for the root volume and that makes debugging hard.
324 if ( ret
!= jnl
->last_flush_err
325 || (jnl
->flags
& JOURNAL_FLUSHCACHE_ERR
) == 0
326 || num_err_prints
++ < 25) {
328 printf("jnl: flushing fs disk buffer returned 0x%x\n", ret
);
330 jnl
->flags
|= JOURNAL_FLUSHCACHE_ERR
;
331 jnl
->last_flush_err
= ret
;
336 jnl
->jhdr
->checksum
= 0;
337 jnl
->jhdr
->checksum
= calc_checksum((char *)jnl
->jhdr
, sizeof(struct journal_header
));
338 if (do_journal_io(jnl
, &jhdr_offset
, jnl
->header_buf
, jnl
->jhdr
->jhdr_size
, JNL_WRITE
|JNL_HEADER
) != jnl
->jhdr
->jhdr_size
) {
339 printf("jnl: write_journal_header: error writing the journal header!\n");
340 jnl
->flags
|= JOURNAL_INVALID
;
344 // Have to flush after writing the journal header so that
345 // a future transaction doesn't sneak out to disk before
346 // the header does and thus overwrite data that the old
347 // journal header refers to. Saw this exact case happen
348 // on an IDE bus analyzer with Larry Barras so while it
349 // may seem obscure, it's not.
351 VOP_IOCTL(jnl
->jdev
, DKIOCSYNCHRONIZECACHE
, NULL
, FWRITE
, NOCRED
, current_proc());
359 // this is a work function used to free up transactions that
360 // completed. they can't be free'd from buffer_flushed_callback
361 // because it is called from deep with the disk driver stack
362 // and thus can't do something that would potentially cause
363 // paging. it gets called by each of the journal api entry
364 // points so stuff shouldn't hang around for too long.
367 free_old_stuff(journal
*jnl
)
369 transaction
*tr
, *next
;
371 for(tr
=jnl
->tr_freeme
; tr
; tr
=next
) {
373 FREE_ZONE(tr
, sizeof(transaction
), M_JNL_TR
);
376 jnl
->tr_freeme
= NULL
;
382 // This is our callback that lets us know when a buffer has been
383 // flushed to disk. It's called from deep within the driver stack
384 // and thus is quite limited in what it can do. Notably, it can
385 // not initiate any new i/o's or allocate/free memory.
388 buffer_flushed_callback(struct buf
*bp
)
392 transaction
*ctr
, *prev
=NULL
, *next
;
396 //printf("jnl: buf flush: bp @ 0x%x l/blkno %d/%d vp 0x%x tr @ 0x%x\n",
397 // bp, bp->b_lblkno, bp->b_blkno, bp->b_vp, bp->b_transaction);
399 // snarf out the bits we want
400 bufsize
= bp
->b_bufsize
;
401 tr
= bp
->b_transaction
;
403 bp
->b_iodone
= NULL
; // don't call us for this guy again
404 bp
->b_transaction
= NULL
;
407 // This is what biodone() would do if it didn't call us.
408 // NOTE: THIS CODE *HAS* TO BE HERE!
410 if (ISSET(bp
->b_flags
, B_ASYNC
)) { /* if async, release it */
412 } else { /* or just wakeup the buffer */
413 CLR(bp
->b_flags
, B_WANTED
);
417 // NOTE: from here on out we do *NOT* touch bp anymore.
420 // then we've already seen it
425 CHECK_TRANSACTION(tr
);
428 if (jnl
->flags
& JOURNAL_INVALID
) {
434 // update the number of blocks that have been flushed.
435 // this buf may represent more than one block so take
436 // that into account.
437 tr
->num_flushed
+= bufsize
;
440 // if this transaction isn't done yet, just return as
441 // there is nothing to do.
442 if ((tr
->num_flushed
+ tr
->num_killed
) < tr
->total_bytes
) {
446 //printf("jnl: tr 0x%x (0x%llx 0x%llx) in jnl 0x%x completed.\n",
447 // tr, tr->journal_start, tr->journal_end, jnl);
449 // find this entry in the old_start[] index and mark it completed
450 simple_lock(&jnl
->old_start_lock
);
451 for(i
=0; i
< sizeof(jnl
->old_start
)/sizeof(jnl
->old_start
[0]); i
++) {
453 if ((jnl
->old_start
[i
] & ~(0x8000000000000000LL
)) == tr
->journal_start
) {
454 jnl
->old_start
[i
] &= ~(0x8000000000000000LL
);
458 if (i
>= sizeof(jnl
->old_start
)/sizeof(jnl
->old_start
[0])) {
459 panic("jnl: buffer_flushed: did not find tr w/start @ %lld (tr 0x%x, jnl 0x%x)\n",
460 tr
->journal_start
, tr
, jnl
);
462 simple_unlock(&jnl
->old_start_lock
);
465 // if we are here then we need to update the journal header
466 // to reflect that this transaction is complete
467 if (tr
->journal_start
== jnl
->active_start
) {
468 jnl
->active_start
= tr
->journal_end
;
469 tr
->journal_start
= tr
->journal_end
= (off_t
)0;
472 // go through the completed_trs list and try to coalesce
473 // entries, restarting back at the beginning if we have to.
474 for(ctr
=jnl
->completed_trs
; ctr
; prev
=ctr
, ctr
=next
) {
475 if (ctr
->journal_start
== jnl
->active_start
) {
476 jnl
->active_start
= ctr
->journal_end
;
478 prev
->next
= ctr
->next
;
480 if (ctr
== jnl
->completed_trs
) {
481 jnl
->completed_trs
= ctr
->next
;
484 next
= jnl
->completed_trs
; // this starts us over again
485 ctr
->next
= jnl
->tr_freeme
;
486 jnl
->tr_freeme
= ctr
;
488 } else if (tr
->journal_end
== ctr
->journal_start
) {
489 ctr
->journal_start
= tr
->journal_start
;
490 next
= jnl
->completed_trs
; // this starts us over again
492 tr
->journal_start
= tr
->journal_end
= (off_t
)0;
493 } else if (tr
->journal_start
== ctr
->journal_end
) {
494 ctr
->journal_end
= tr
->journal_end
;
496 tr
->journal_start
= tr
->journal_end
= (off_t
)0;
502 // at this point no one should be using this guy anymore
503 tr
->total_bytes
= 0xfbadc0de;
505 // if this is true then we didn't merge with anyone
506 // so link ourselves in at the head of the completed
508 if (tr
->journal_start
!= 0) {
509 // put this entry into the correct sorted place
510 // in the list instead of just at the head.
514 for(ctr
=jnl
->completed_trs
; ctr
&& tr
->journal_start
> ctr
->journal_start
; prev
=ctr
, ctr
=ctr
->next
) {
518 if (ctr
== NULL
&& prev
== NULL
) {
519 jnl
->completed_trs
= tr
;
521 } else if (ctr
== jnl
->completed_trs
) {
522 tr
->next
= jnl
->completed_trs
;
523 jnl
->completed_trs
= tr
;
525 tr
->next
= prev
->next
;
529 // if we're here this tr got merged with someone else so
530 // put it on the list to be free'd
531 tr
->next
= jnl
->tr_freeme
;
537 #include <libkern/OSByteOrder.h>
539 #define SWAP16(x) OSSwapInt16(x)
540 #define SWAP32(x) OSSwapInt32(x)
541 #define SWAP64(x) OSSwapInt64(x)
545 swap_journal_header(journal
*jnl
)
547 jnl
->jhdr
->magic
= SWAP32(jnl
->jhdr
->magic
);
548 jnl
->jhdr
->endian
= SWAP32(jnl
->jhdr
->endian
);
549 jnl
->jhdr
->start
= SWAP64(jnl
->jhdr
->start
);
550 jnl
->jhdr
->end
= SWAP64(jnl
->jhdr
->end
);
551 jnl
->jhdr
->size
= SWAP64(jnl
->jhdr
->size
);
552 jnl
->jhdr
->blhdr_size
= SWAP32(jnl
->jhdr
->blhdr_size
);
553 jnl
->jhdr
->checksum
= SWAP32(jnl
->jhdr
->checksum
);
554 jnl
->jhdr
->jhdr_size
= SWAP32(jnl
->jhdr
->jhdr_size
);
558 swap_block_list_header(journal
*jnl
, block_list_header
*blhdr
)
562 blhdr
->max_blocks
= SWAP16(blhdr
->max_blocks
);
563 blhdr
->num_blocks
= SWAP16(blhdr
->num_blocks
);
564 blhdr
->bytes_used
= SWAP32(blhdr
->bytes_used
);
565 blhdr
->checksum
= SWAP32(blhdr
->checksum
);
566 blhdr
->pad
= SWAP32(blhdr
->pad
);
568 if (blhdr
->num_blocks
* sizeof(blhdr
->binfo
[0]) > jnl
->jhdr
->blhdr_size
) {
569 printf("jnl: blhdr num blocks looks suspicious (%d). not swapping.\n", blhdr
->num_blocks
);
573 for(i
=0; i
< blhdr
->num_blocks
; i
++) {
574 blhdr
->binfo
[i
].bnum
= SWAP64(blhdr
->binfo
[i
].bnum
);
575 blhdr
->binfo
[i
].bsize
= SWAP32(blhdr
->binfo
[i
].bsize
);
576 blhdr
->binfo
[i
].bp
= (void *)SWAP32((int)blhdr
->binfo
[i
].bp
);
582 update_fs_block(journal
*jnl
, void *block_ptr
, off_t fs_block
, size_t bsize
)
585 struct buf
*oblock_bp
=NULL
;
587 // first read the block we want.
588 ret
= meta_bread(jnl
->fsdev
, (daddr_t
)fs_block
, bsize
, NOCRED
, &oblock_bp
);
590 printf("jnl: update_fs_block: error reading fs block # %lld! (ret %d)\n", fs_block
, ret
);
597 // let's try to be aggressive here and just re-write the block
598 oblock_bp
= getblk(jnl
->fsdev
, (daddr_t
)fs_block
, bsize
, 0, 0, BLK_META
);
599 if (oblock_bp
== NULL
) {
600 printf("jnl: update_fs_block: getblk() for %lld failed! failing update.\n", fs_block
);
605 // make sure it's the correct size.
606 if (oblock_bp
->b_bufsize
!= bsize
) {
611 // copy the journal data over top of it
612 memcpy(oblock_bp
->b_data
, block_ptr
, bsize
);
614 if ((ret
= VOP_BWRITE(oblock_bp
)) != 0) {
615 printf("jnl: update_fs_block: failed to update block %lld (ret %d)\n", fs_block
,ret
);
619 // and now invalidate it so that if someone else wants to read
620 // it in a different size they'll be able to do it.
621 ret
= meta_bread(jnl
->fsdev
, (daddr_t
)fs_block
, bsize
, NOCRED
, &oblock_bp
);
623 oblock_bp
->b_flags
|= B_INVAL
;
631 grow_table(struct bucket
**buf_ptr
, int num_buckets
, int new_size
)
633 struct bucket
*newBuf
;
634 int current_size
= num_buckets
, i
;
636 // return if newsize is less than the current size
637 if (new_size
< num_buckets
) {
641 if ((MALLOC(newBuf
, struct bucket
*, new_size
*sizeof(struct bucket
), M_TEMP
, M_WAITOK
)) == NULL
) {
642 printf("jnl: grow_table: no memory to expand coalesce buffer!\n");
646 // printf("jnl: lookup_bucket: expanded co_buf to %d elems\n", new_size);
648 // copy existing elements
649 bcopy(*buf_ptr
, newBuf
, num_buckets
*sizeof(struct bucket
));
651 // initialize the new ones
652 for(i
=num_buckets
; i
< new_size
; i
++) {
653 newBuf
[i
].block_num
= (off_t
)-1;
656 // free the old container
657 FREE(*buf_ptr
, M_TEMP
);
666 lookup_bucket(struct bucket
**buf_ptr
, off_t block_num
, int num_full
)
668 int lo
, hi
, index
, matches
, i
;
671 return 0; // table is empty, so insert at index=0
678 // perform binary search for block_num
680 int mid
= (hi
- lo
)/2 + lo
;
681 off_t this_num
= (*buf_ptr
)[mid
].block_num
;
683 if (block_num
== this_num
) {
688 if (block_num
< this_num
) {
693 if (block_num
> this_num
) {
699 // check if lo and hi converged on the match
700 if (block_num
== (*buf_ptr
)[hi
].block_num
) {
704 // if no existing entry found, find index for new one
706 index
= (block_num
< (*buf_ptr
)[hi
].block_num
) ? hi
: hi
+ 1;
708 // make sure that we return the right-most index in the case of multiple matches
711 while(i
< num_full
&& block_num
== (*buf_ptr
)[i
].block_num
) {
723 insert_block(journal
*jnl
, struct bucket
**buf_ptr
, int blk_index
, off_t num
, size_t size
, size_t offset
, int *num_buckets_ptr
, int *num_full_ptr
, int overwriting
)
726 // grow the table if we're out of space
727 if (*num_full_ptr
>= *num_buckets_ptr
) {
728 int new_size
= *num_buckets_ptr
* 2;
729 int grow_size
= grow_table(buf_ptr
, *num_buckets_ptr
, new_size
);
731 if (grow_size
< new_size
) {
732 printf("jnl: add_block: grow_table returned an error!\n");
736 *num_buckets_ptr
= grow_size
; //update num_buckets to reflect the new size
739 // if we're not inserting at the end, we need to bcopy
740 if (blk_index
!= *num_full_ptr
) {
741 bcopy( (*buf_ptr
)+(blk_index
), (*buf_ptr
)+(blk_index
+1), (*num_full_ptr
-blk_index
)*sizeof(struct bucket
) );
744 (*num_full_ptr
)++; // increment only if we're not overwriting
747 // sanity check the values we're about to add
748 if (offset
>= jnl
->jhdr
->size
) {
749 offset
= jnl
->jhdr
->jhdr_size
+ (offset
- jnl
->jhdr
->size
);
752 panic("jnl: insert_block: bad size in insert_block (%d)\n", size
);
755 (*buf_ptr
)[blk_index
].block_num
= num
;
756 (*buf_ptr
)[blk_index
].block_size
= size
;
757 (*buf_ptr
)[blk_index
].jnl_offset
= offset
;
763 do_overlap(journal
*jnl
, struct bucket
**buf_ptr
, int blk_index
, off_t block_num
, size_t size
, size_t offset
, int *num_buckets_ptr
, int *num_full_ptr
)
765 int num_to_remove
, index
, i
, overwrite
, err
;
766 size_t jhdr_size
= jnl
->jhdr
->jhdr_size
, new_offset
;
767 off_t overlap
, block_start
, block_end
;
769 block_start
= block_num
*jhdr_size
;
770 block_end
= block_start
+ size
;
771 overwrite
= (block_num
== (*buf_ptr
)[blk_index
].block_num
&& size
>= (*buf_ptr
)[blk_index
].block_size
);
773 // first, eliminate any overlap with the previous entry
774 if (blk_index
!= 0 && !overwrite
) {
775 off_t prev_block_start
= (*buf_ptr
)[blk_index
-1].block_num
*jhdr_size
;
776 off_t prev_block_end
= prev_block_start
+ (*buf_ptr
)[blk_index
-1].block_size
;
777 overlap
= prev_block_end
- block_start
;
779 if (overlap
% jhdr_size
!= 0) {
780 panic("jnl: do_overlap: overlap with previous entry not a multiple of %d\n", jhdr_size
);
783 // if the previous entry completely overlaps this one, we need to break it into two pieces.
784 if (prev_block_end
> block_end
) {
785 off_t new_num
= block_end
/ jhdr_size
;
786 size_t new_size
= prev_block_end
- block_end
;
787 size_t new_offset
= (*buf_ptr
)[blk_index
-1].jnl_offset
+ (block_end
- prev_block_start
);
789 err
= insert_block(jnl
, buf_ptr
, blk_index
, new_num
, new_size
, new_offset
, num_buckets_ptr
, num_full_ptr
, 0);
791 panic("jnl: do_overlap: error inserting during pre-overlap\n");
795 // Regardless, we need to truncate the previous entry to the beginning of the overlap
796 (*buf_ptr
)[blk_index
-1].block_size
= block_start
- prev_block_start
;
800 // then, bail out fast if there's no overlap with the entries that follow
801 if (!overwrite
&& block_end
<= (*buf_ptr
)[blk_index
].block_num
*jhdr_size
) {
802 return 0; // no overlap, no overwrite
803 } else if (overwrite
&& (blk_index
+ 1 >= *num_full_ptr
|| block_end
<= (*buf_ptr
)[blk_index
+1].block_num
*jhdr_size
)) {
804 return 1; // simple overwrite
807 // Otherwise, find all cases of total and partial overlap. We use the special
808 // block_num of -2 to designate entries that are completely overlapped and must
809 // be eliminated. The block_num, size, and jnl_offset of partially overlapped
810 // entries must be adjusted to keep the array consistent.
813 while(index
< *num_full_ptr
&& block_end
> (*buf_ptr
)[index
].block_num
*jhdr_size
) {
814 if (block_end
>= ((*buf_ptr
)[index
].block_num
*jhdr_size
+ (*buf_ptr
)[index
].block_size
)) {
815 (*buf_ptr
)[index
].block_num
= -2; // mark this for deletion
818 overlap
= block_end
- (*buf_ptr
)[index
].block_num
*jhdr_size
;
820 if (overlap
% jhdr_size
!= 0) {
821 panic("jnl: do_overlap: overlap of %d is not multiple of %d\n", overlap
, jhdr_size
);
824 // if we partially overlap this entry, adjust its block number, jnl offset, and size
825 (*buf_ptr
)[index
].block_num
+= (overlap
/ jhdr_size
); // make sure overlap is multiple of jhdr_size, or round up
827 new_offset
= (*buf_ptr
)[index
].jnl_offset
+ overlap
; // check for wrap-around
828 if (new_offset
>= jnl
->jhdr
->size
) {
829 new_offset
= jhdr_size
+ (new_offset
- jnl
->jhdr
->size
);
831 (*buf_ptr
)[index
].jnl_offset
= new_offset
;
833 (*buf_ptr
)[index
].block_size
-= overlap
; // sanity check for negative value
834 if ((*buf_ptr
)[index
].block_size
<= 0) {
835 panic("jnl: do_overlap: after overlap, new block size is invalid (%d)\n", (*buf_ptr
)[index
].block_size
);
836 // return -1; // if above panic is removed, return -1 for error
845 // bcopy over any completely overlapped entries, starting at the right (where the above loop broke out)
846 index
--; // start with the last index used within the above loop
847 while(index
>= blk_index
) {
848 if ((*buf_ptr
)[index
].block_num
== -2) {
849 if (index
== *num_full_ptr
-1) {
850 (*buf_ptr
)[index
].block_num
= -1; // it's the last item in the table... just mark as free
852 bcopy( (*buf_ptr
)+(index
+1), (*buf_ptr
)+(index
), (*num_full_ptr
- (index
+ 1)) * sizeof(struct bucket
) );
859 // eliminate any stale entries at the end of the table
860 for(i
=*num_full_ptr
; i
< (*num_full_ptr
+ num_to_remove
); i
++) {
861 (*buf_ptr
)[i
].block_num
= -1;
864 return 0; // if we got this far, we need to insert the entry into the table (rather than overwrite)
867 // PR-3105942: Coalesce writes to the same block in journal replay
868 // We coalesce writes by maintaining a dynamic sorted array of physical disk blocks
869 // to be replayed and the corresponding location in the journal which contains
870 // the most recent data for those blocks. The array is "played" once the all the
871 // blocks in the journal have been coalesced. The code for the case of conflicting/
872 // overlapping writes to a single block is the most dense. Because coalescing can
873 // disrupt the existing time-ordering of blocks in the journal playback, care
874 // is taken to catch any overlaps and keep the array consistent.
876 add_block(journal
*jnl
, struct bucket
**buf_ptr
, off_t block_num
, size_t size
, size_t offset
, int *num_buckets_ptr
, int *num_full_ptr
)
878 int blk_index
, overwriting
;
879 size_t jhdr_size
= jnl
->jhdr
->jhdr_size
;
881 // on return from lookup_bucket(), blk_index is the index into the table where block_num should be
882 // inserted (or the index of the elem to overwrite).
883 blk_index
= lookup_bucket( buf_ptr
, block_num
, *num_full_ptr
);
885 // check if the index is within bounds (if we're adding this block to the end of
886 // the table, blk_index will be equal to num_full)
887 if (blk_index
< 0 || blk_index
> *num_full_ptr
) {
888 //printf("jnl: add_block: trouble adding block to co_buf\n");
890 } // else printf("jnl: add_block: adding block 0x%llx at i=%d\n", block_num, blk_index);
892 // Determine whether we're overwriting an existing entry by checking for overlap
893 overwriting
= do_overlap(jnl
, buf_ptr
, blk_index
, block_num
, size
, offset
, num_buckets_ptr
, num_full_ptr
);
894 if (overwriting
< 0) {
895 return -1; // if we got an error, pass it along
898 // returns the index, or -1 on error
899 blk_index
= insert_block(jnl
, buf_ptr
, blk_index
, block_num
, size
, offset
, num_buckets_ptr
, num_full_ptr
, overwriting
);
905 replay_journal(journal
*jnl
)
907 int i
, ret
, orig_checksum
, checksum
, max_bsize
;
908 struct buf
*oblock_bp
;
909 block_list_header
*blhdr
;
911 char *buf
, *block_ptr
=NULL
;
912 struct bucket
*co_buf
;
913 int num_buckets
= STARTING_BUCKETS
, num_full
;
915 // wrap the start ptr if it points to the very end of the journal
916 if (jnl
->jhdr
->start
== jnl
->jhdr
->size
) {
917 jnl
->jhdr
->start
= jnl
->jhdr
->jhdr_size
;
919 if (jnl
->jhdr
->end
== jnl
->jhdr
->size
) {
920 jnl
->jhdr
->end
= jnl
->jhdr
->jhdr_size
;
923 if (jnl
->jhdr
->start
== jnl
->jhdr
->end
) {
927 // allocate memory for the header_block. we'll read each blhdr into this
928 if (kmem_alloc(kernel_map
, (vm_offset_t
*)&buf
, jnl
->jhdr
->blhdr_size
)) {
929 printf("jnl: replay_journal: no memory for block buffer! (%d bytes)\n",
930 jnl
->jhdr
->blhdr_size
);
934 // allocate memory for the coalesce buffer
935 if ((MALLOC(co_buf
, struct bucket
*, num_buckets
*sizeof(struct bucket
), M_TEMP
, M_WAITOK
)) == NULL
) {
936 printf("jnl: replay_journal: no memory for coalesce buffer!\n");
940 // initialize entries
941 for(i
=0; i
< num_buckets
; i
++) {
942 co_buf
[i
].block_num
= -1;
944 num_full
= 0; // empty at first
947 printf("jnl: replay_journal: from: %lld to: %lld (joffset 0x%llx)\n",
948 jnl
->jhdr
->start
, jnl
->jhdr
->end
, jnl
->jdev_offset
);
950 while(jnl
->jhdr
->start
!= jnl
->jhdr
->end
) {
951 offset
= jnl
->jhdr
->start
;
952 ret
= read_journal_data(jnl
, &offset
, buf
, jnl
->jhdr
->blhdr_size
);
953 if (ret
!= jnl
->jhdr
->blhdr_size
) {
954 printf("jnl: replay_journal: Could not read block list header block @ 0x%llx!\n", offset
);
958 blhdr
= (block_list_header
*)buf
;
960 orig_checksum
= blhdr
->checksum
;
962 if (jnl
->flags
& JOURNAL_NEED_SWAP
) {
963 // calculate the checksum based on the unswapped data
964 // because it is done byte-at-a-time.
965 orig_checksum
= SWAP32(orig_checksum
);
966 checksum
= calc_checksum((char *)blhdr
, BLHDR_CHECKSUM_SIZE
);
967 swap_block_list_header(jnl
, blhdr
);
969 checksum
= calc_checksum((char *)blhdr
, BLHDR_CHECKSUM_SIZE
);
971 if (checksum
!= orig_checksum
) {
972 printf("jnl: replay_journal: bad block list header @ 0x%llx (checksum 0x%x != 0x%x)\n",
973 offset
, orig_checksum
, checksum
);
976 if ( blhdr
->max_blocks
<= 0 || blhdr
->max_blocks
> 2048
977 || blhdr
->num_blocks
<= 0 || blhdr
->num_blocks
> blhdr
->max_blocks
) {
978 printf("jnl: replay_journal: bad looking journal entry: max: %d num: %d\n",
979 blhdr
->max_blocks
, blhdr
->num_blocks
);
983 for(i
=1,max_bsize
=0; i
< blhdr
->num_blocks
; i
++) {
984 if (blhdr
->binfo
[i
].bnum
< 0 && blhdr
->binfo
[i
].bnum
!= (off_t
)-1) {
985 printf("jnl: replay_journal: bogus block number 0x%llx\n", blhdr
->binfo
[i
].bnum
);
988 if (blhdr
->binfo
[i
].bsize
> max_bsize
) {
989 max_bsize
= blhdr
->binfo
[i
].bsize
;
993 // make sure it's at least one page in size.
994 if (max_bsize
& (PAGE_SIZE
- 1)) {
995 max_bsize
= (max_bsize
+ PAGE_SIZE
) & ~(PAGE_SIZE
- 1);
999 //printf("jnl: replay_journal: adding %d blocks in journal entry @ 0x%llx to co_buf\n",
1000 // blhdr->num_blocks-1, jnl->jhdr->start);
1001 for(i
=1; i
< blhdr
->num_blocks
; i
++) {
1005 size
= blhdr
->binfo
[i
].bsize
;
1006 number
= blhdr
->binfo
[i
].bnum
;
1008 // don't add "killed" blocks
1009 if (number
== (off_t
)-1) {
1010 //printf("jnl: replay_journal: skipping killed fs block (index %d)\n", i);
1012 // add this bucket to co_buf, coalescing where possible
1013 // printf("jnl: replay_journal: adding block 0x%llx\n", number);
1014 ret_val
= add_block(jnl
, &co_buf
, number
, size
, (size_t) offset
, &num_buckets
, &num_full
);
1016 if (ret_val
== -1) {
1017 printf("jnl: replay_journal: trouble adding block to co_buf\n");
1019 } // else printf("jnl: replay_journal: added block 0x%llx at i=%d\n", number);
1025 // check if the last block added puts us off the end of the jnl.
1026 // if so, we need to wrap to the beginning and take any remainder
1029 if (offset
>= jnl
->jhdr
->size
) {
1030 offset
= jnl
->jhdr
->jhdr_size
+ (offset
- jnl
->jhdr
->size
);
1035 jnl
->jhdr
->start
+= blhdr
->bytes_used
;
1036 if (jnl
->jhdr
->start
>= jnl
->jhdr
->size
) {
1037 // wrap around and skip the journal header block
1038 jnl
->jhdr
->start
= (jnl
->jhdr
->start
% jnl
->jhdr
->size
) + jnl
->jhdr
->jhdr_size
;
1043 //printf("jnl: replay_journal: replaying %d blocks\n", num_full);
1045 if (kmem_alloc(kernel_map
, (vm_offset_t
*)&block_ptr
, max_bsize
)) {
1049 // Replay the coalesced entries in the co-buf
1050 for(i
=0; i
< num_full
; i
++) {
1051 size_t size
= co_buf
[i
].block_size
;
1052 off_t jnl_offset
= (off_t
) co_buf
[i
].jnl_offset
;
1053 off_t number
= co_buf
[i
].block_num
;
1056 // printf("replaying co_buf[%d]: block 0x%llx, size 0x%x, jnl_offset 0x%llx\n", i, co_buf[i].block_num,
1057 // co_buf[i].block_size, co_buf[i].jnl_offset);
1059 if (number
== (off_t
)-1) {
1060 // printf("jnl: replay_journal: skipping killed fs block\n");
1063 // do journal read, and set the phys. block
1064 ret
= read_journal_data(jnl
, &jnl_offset
, block_ptr
, size
);
1066 printf("jnl: replay_journal: Could not read journal entry data @ offset 0x%llx!\n", offset
);
1070 if (update_fs_block(jnl
, block_ptr
, number
, size
) != 0) {
1077 // done replaying; update jnl header
1078 if (write_journal_header(jnl
) != 0) {
1083 kmem_free(kernel_map
, (vm_offset_t
)block_ptr
, max_bsize
);
1086 // free the coalesce buffer
1087 FREE(co_buf
, M_TEMP
);
1090 kmem_free(kernel_map
, (vm_offset_t
)buf
, jnl
->jhdr
->blhdr_size
);
1095 kmem_free(kernel_map
, (vm_offset_t
)block_ptr
, max_bsize
);
1098 FREE(co_buf
, M_TEMP
);
1100 kmem_free(kernel_map
, (vm_offset_t
)buf
, jnl
->jhdr
->blhdr_size
);
1106 #define DEFAULT_TRANSACTION_BUFFER_SIZE (128*1024)
1107 //#define DEFAULT_TRANSACTION_BUFFER_SIZE (256*1024) // better performance but uses more mem
1108 #define MAX_TRANSACTION_BUFFER_SIZE (512*1024)
1110 // XXXdbg - so I can change it in the debugger
1111 int def_tbuffer_size
= 0;
1115 // This function sets the size of the tbuffer and the
1116 // size of the blhdr. It assumes that jnl->jhdr->size
1117 // and jnl->jhdr->jhdr_size are already valid.
1120 size_up_tbuffer(journal
*jnl
, int tbuffer_size
, int phys_blksz
)
1123 // one-time initialization based on how much memory
1124 // there is in the machine.
1126 if (def_tbuffer_size
== 0) {
1127 if (mem_size
< (256*1024*1024)) {
1128 def_tbuffer_size
= DEFAULT_TRANSACTION_BUFFER_SIZE
;
1129 } else if (mem_size
< (512*1024*1024)) {
1130 def_tbuffer_size
= DEFAULT_TRANSACTION_BUFFER_SIZE
* 2;
1131 } else if (mem_size
< (1024*1024*1024)) {
1132 def_tbuffer_size
= DEFAULT_TRANSACTION_BUFFER_SIZE
* 3;
1133 } else if (mem_size
>= (1024*1024*1024)) {
1134 def_tbuffer_size
= DEFAULT_TRANSACTION_BUFFER_SIZE
* 4;
1138 // size up the transaction buffer... can't be larger than the number
1139 // of blocks that can fit in a block_list_header block.
1140 if (tbuffer_size
== 0) {
1141 jnl
->tbuffer_size
= def_tbuffer_size
;
1143 // make sure that the specified tbuffer_size isn't too small
1144 if (tbuffer_size
< jnl
->jhdr
->blhdr_size
* 2) {
1145 tbuffer_size
= jnl
->jhdr
->blhdr_size
* 2;
1147 // and make sure it's an even multiple of the block size
1148 if ((tbuffer_size
% jnl
->jhdr
->jhdr_size
) != 0) {
1149 tbuffer_size
-= (tbuffer_size
% jnl
->jhdr
->jhdr_size
);
1152 jnl
->tbuffer_size
= tbuffer_size
;
1155 if (jnl
->tbuffer_size
> (jnl
->jhdr
->size
/ 2)) {
1156 jnl
->tbuffer_size
= (jnl
->jhdr
->size
/ 2);
1159 if (jnl
->tbuffer_size
> MAX_TRANSACTION_BUFFER_SIZE
) {
1160 jnl
->tbuffer_size
= MAX_TRANSACTION_BUFFER_SIZE
;
1163 jnl
->jhdr
->blhdr_size
= (jnl
->tbuffer_size
/ jnl
->jhdr
->jhdr_size
) * sizeof(block_info
);
1164 if (jnl
->jhdr
->blhdr_size
< phys_blksz
) {
1165 jnl
->jhdr
->blhdr_size
= phys_blksz
;
1166 } else if ((jnl
->jhdr
->blhdr_size
% phys_blksz
) != 0) {
1167 // have to round up so we're an even multiple of the physical block size
1168 jnl
->jhdr
->blhdr_size
= (jnl
->jhdr
->blhdr_size
+ (phys_blksz
- 1)) & ~(phys_blksz
- 1);
1175 journal_create(struct vnode
*jvp
,
1179 size_t min_fs_blksz
,
1181 int32_t tbuffer_size
,
1182 void (*flush
)(void *arg
),
1186 int ret
, phys_blksz
;
1188 /* Get the real physical block size. */
1189 if (VOP_IOCTL(jvp
, DKIOCGETBLOCKSIZE
, (caddr_t
)&phys_blksz
, 0, FSCRED
, NULL
)) {
1193 if (phys_blksz
> min_fs_blksz
) {
1194 printf("jnl: create: error: phys blksize %d bigger than min fs blksize %d\n",
1195 phys_blksz
, min_fs_blksz
);
1199 if ((journal_size
% phys_blksz
) != 0) {
1200 printf("jnl: create: journal size 0x%llx is not an even multiple of block size 0x%x\n",
1201 journal_size
, phys_blksz
);
1205 MALLOC_ZONE(jnl
, struct journal
*, sizeof(struct journal
), M_JNL_JNL
, M_WAITOK
);
1206 memset(jnl
, 0, sizeof(*jnl
));
1209 jnl
->jdev_offset
= offset
;
1212 jnl
->flush_arg
= arg
;
1213 jnl
->flags
= (flags
& JOURNAL_OPTION_FLAGS_MASK
);
1214 simple_lock_init(&jnl
->old_start_lock
);
1216 if (kmem_alloc(kernel_map
, (vm_offset_t
*)&jnl
->header_buf
, phys_blksz
)) {
1217 printf("jnl: create: could not allocate space for header buffer (%d bytes)\n", phys_blksz
);
1218 goto bad_kmem_alloc
;
1221 memset(jnl
->header_buf
, 0, phys_blksz
);
1223 jnl
->jhdr
= (journal_header
*)jnl
->header_buf
;
1224 jnl
->jhdr
->magic
= JOURNAL_HEADER_MAGIC
;
1225 jnl
->jhdr
->endian
= ENDIAN_MAGIC
;
1226 jnl
->jhdr
->start
= phys_blksz
; // start at block #1, block #0 is for the jhdr itself
1227 jnl
->jhdr
->end
= phys_blksz
;
1228 jnl
->jhdr
->size
= journal_size
;
1229 jnl
->jhdr
->jhdr_size
= phys_blksz
;
1230 size_up_tbuffer(jnl
, tbuffer_size
, phys_blksz
);
1232 jnl
->active_start
= jnl
->jhdr
->start
;
1234 // XXXdbg - for testing you can force the journal to wrap around
1235 // jnl->jhdr->start = jnl->jhdr->size - (phys_blksz*3);
1236 // jnl->jhdr->end = jnl->jhdr->size - (phys_blksz*3);
1238 lockinit(&jnl
->jlock
, PINOD
, "journal", 0, 0);
1240 if (write_journal_header(jnl
) != 0) {
1241 printf("jnl: journal_create: failed to write journal header.\n");
1249 kmem_free(kernel_map
, (vm_offset_t
)jnl
->header_buf
, phys_blksz
);
1252 FREE_ZONE(jnl
, sizeof(struct journal
), M_JNL_JNL
);
1258 journal_open(struct vnode
*jvp
,
1262 size_t min_fs_blksz
,
1264 int32_t tbuffer_size
,
1265 void (*flush
)(void *arg
),
1269 int orig_blksz
=0, phys_blksz
, blhdr_size
;
1270 int orig_checksum
, checksum
;
1272 /* Get the real physical block size. */
1273 if (VOP_IOCTL(jvp
, DKIOCGETBLOCKSIZE
, (caddr_t
)&phys_blksz
, 0, FSCRED
, NULL
)) {
1277 if (phys_blksz
> min_fs_blksz
) {
1278 printf("jnl: create: error: phys blksize %d bigger than min fs blksize %d\n",
1279 phys_blksz
, min_fs_blksz
);
1283 if ((journal_size
% phys_blksz
) != 0) {
1284 printf("jnl: open: journal size 0x%llx is not an even multiple of block size 0x%x\n",
1285 journal_size
, phys_blksz
);
1289 MALLOC_ZONE(jnl
, struct journal
*, sizeof(struct journal
), M_JNL_JNL
, M_WAITOK
);
1290 memset(jnl
, 0, sizeof(*jnl
));
1293 jnl
->jdev_offset
= offset
;
1296 jnl
->flush_arg
= arg
;
1297 jnl
->flags
= (flags
& JOURNAL_OPTION_FLAGS_MASK
);
1298 simple_lock_init(&jnl
->old_start_lock
);
1300 if (kmem_alloc(kernel_map
, (vm_offset_t
*)&jnl
->header_buf
, phys_blksz
)) {
1301 printf("jnl: create: could not allocate space for header buffer (%d bytes)\n", phys_blksz
);
1302 goto bad_kmem_alloc
;
1305 jnl
->jhdr
= (journal_header
*)jnl
->header_buf
;
1306 memset(jnl
->jhdr
, 0, sizeof(journal_header
)+4);
1308 // we have to set this up here so that do_journal_io() will work
1309 jnl
->jhdr
->jhdr_size
= phys_blksz
;
1311 if (read_journal_header(jnl
, jnl
->jhdr
, phys_blksz
) != phys_blksz
) {
1312 printf("jnl: open: could not read %d bytes for the journal header.\n",
1317 orig_checksum
= jnl
->jhdr
->checksum
;
1318 jnl
->jhdr
->checksum
= 0;
1320 if (jnl
->jhdr
->magic
== SWAP32(JOURNAL_HEADER_MAGIC
)) {
1321 // do this before the swap since it's done byte-at-a-time
1322 orig_checksum
= SWAP32(orig_checksum
);
1323 checksum
= calc_checksum((char *)jnl
->jhdr
, sizeof(struct journal_header
));
1324 swap_journal_header(jnl
);
1325 jnl
->flags
|= JOURNAL_NEED_SWAP
;
1327 checksum
= calc_checksum((char *)jnl
->jhdr
, sizeof(struct journal_header
));
1330 if (jnl
->jhdr
->magic
!= JOURNAL_HEADER_MAGIC
&& jnl
->jhdr
->magic
!= OLD_JOURNAL_HEADER_MAGIC
) {
1331 printf("jnl: open: journal magic is bad (0x%x != 0x%x)\n",
1332 jnl
->jhdr
->magic
, JOURNAL_HEADER_MAGIC
);
1336 // only check if we're the current journal header magic value
1337 if (jnl
->jhdr
->magic
== JOURNAL_HEADER_MAGIC
) {
1339 if (orig_checksum
!= checksum
) {
1340 printf("jnl: open: journal checksum is bad (0x%x != 0x%x)\n",
1341 orig_checksum
, checksum
);
1347 // XXXdbg - convert old style magic numbers to the new one
1348 if (jnl
->jhdr
->magic
== OLD_JOURNAL_HEADER_MAGIC
) {
1349 jnl
->jhdr
->magic
= JOURNAL_HEADER_MAGIC
;
1352 if (phys_blksz
!= jnl
->jhdr
->jhdr_size
&& jnl
->jhdr
->jhdr_size
!= 0) {
1353 printf("jnl: open: phys_blksz %d does not match journal header size %d\n",
1354 phys_blksz
, jnl
->jhdr
->jhdr_size
);
1356 orig_blksz
= phys_blksz
;
1357 phys_blksz
= jnl
->jhdr
->jhdr_size
;
1358 if (VOP_IOCTL(jvp
, DKIOCSETBLOCKSIZE
, (caddr_t
)&phys_blksz
, FWRITE
, FSCRED
, NULL
)) {
1359 printf("jnl: could not set block size to %d bytes.\n", phys_blksz
);
1362 // goto bad_journal;
1365 if ( jnl
->jhdr
->start
<= 0
1366 || jnl
->jhdr
->start
> jnl
->jhdr
->size
1367 || jnl
->jhdr
->start
> 1024*1024*1024) {
1368 printf("jnl: open: jhdr start looks bad (0x%llx max size 0x%llx)\n",
1369 jnl
->jhdr
->start
, jnl
->jhdr
->size
);
1373 if ( jnl
->jhdr
->end
<= 0
1374 || jnl
->jhdr
->end
> jnl
->jhdr
->size
1375 || jnl
->jhdr
->end
> 1024*1024*1024) {
1376 printf("jnl: open: jhdr end looks bad (0x%llx max size 0x%llx)\n",
1377 jnl
->jhdr
->end
, jnl
->jhdr
->size
);
1381 if (jnl
->jhdr
->size
> 1024*1024*1024) {
1382 printf("jnl: open: jhdr size looks bad (0x%llx)\n", jnl
->jhdr
->size
);
1386 // XXXdbg - can't do these checks because hfs writes all kinds of
1387 // non-uniform sized blocks even on devices that have a block size
1388 // that is larger than 512 bytes (i.e. optical media w/2k blocks).
1389 // therefore these checks will fail and so we just have to punt and
1390 // do more relaxed checking...
1391 // XXXdbg if ((jnl->jhdr->start % jnl->jhdr->jhdr_size) != 0) {
1392 if ((jnl
->jhdr
->start
% 512) != 0) {
1393 printf("jnl: open: journal start (0x%llx) not a multiple of 512?\n",
1398 //XXXdbg if ((jnl->jhdr->end % jnl->jhdr->jhdr_size) != 0) {
1399 if ((jnl
->jhdr
->end
% 512) != 0) {
1400 printf("jnl: open: journal end (0x%llx) not a multiple of block size (0x%x)?\n",
1401 jnl
->jhdr
->end
, jnl
->jhdr
->jhdr_size
);
1405 // take care of replaying the journal if necessary
1406 if (flags
& JOURNAL_RESET
) {
1407 printf("jnl: journal start/end pointers reset! (jnl 0x%x; s 0x%llx e 0x%llx)\n",
1408 jnl
, jnl
->jhdr
->start
, jnl
->jhdr
->end
);
1409 jnl
->jhdr
->start
= jnl
->jhdr
->end
;
1410 } else if (replay_journal(jnl
) != 0) {
1411 printf("jnl: journal_open: Error replaying the journal!\n");
1415 if (orig_blksz
!= 0) {
1416 VOP_IOCTL(jvp
, DKIOCSETBLOCKSIZE
, (caddr_t
)&orig_blksz
, FWRITE
, FSCRED
, NULL
);
1417 phys_blksz
= orig_blksz
;
1418 if (orig_blksz
< jnl
->jhdr
->jhdr_size
) {
1419 printf("jnl: open: jhdr_size is %d but orig phys blk size is %d. switching.\n",
1420 jnl
->jhdr
->jhdr_size
, orig_blksz
);
1422 jnl
->jhdr
->jhdr_size
= orig_blksz
;
1426 // make sure this is in sync!
1427 jnl
->active_start
= jnl
->jhdr
->start
;
1429 // set this now, after we've replayed the journal
1430 size_up_tbuffer(jnl
, tbuffer_size
, phys_blksz
);
1432 lockinit(&jnl
->jlock
, PINOD
, "journal", 0, 0);
1437 if (orig_blksz
!= 0) {
1438 phys_blksz
= orig_blksz
;
1439 VOP_IOCTL(jvp
, DKIOCSETBLOCKSIZE
, (caddr_t
)&orig_blksz
, FWRITE
, FSCRED
, NULL
);
1441 kmem_free(kernel_map
, (vm_offset_t
)jnl
->header_buf
, phys_blksz
);
1443 FREE_ZONE(jnl
, sizeof(struct journal
), M_JNL_JNL
);
1448 journal_close(journal
*jnl
)
1450 volatile off_t
*start
, *end
;
1455 // set this before doing anything that would block so that
1456 // we start tearing things down properly.
1458 jnl
->flags
|= JOURNAL_CLOSE_PENDING
;
1460 if (jnl
->owner
!= current_act()) {
1463 ret
= lockmgr(&jnl
->jlock
, LK_EXCLUSIVE
|LK_RETRY
, NULL
, current_proc());
1465 printf("jnl: close: locking the journal (0x%x) failed %d.\n", jnl
, ret
);
1471 // only write stuff to disk if the journal is still valid
1473 if ((jnl
->flags
& JOURNAL_INVALID
) == 0) {
1475 if (jnl
->active_tr
) {
1476 journal_end_transaction(jnl
);
1479 // flush any buffered transactions
1481 transaction
*tr
= jnl
->cur_tr
;
1484 end_transaction(tr
, 1); // force it to get flushed
1487 //start = &jnl->jhdr->start;
1488 start
= &jnl
->active_start
;
1489 end
= &jnl
->jhdr
->end
;
1491 while (*start
!= *end
&& counter
++ < 500) {
1492 printf("jnl: close: flushing the buffer cache (start 0x%llx end 0x%llx)\n", *start
, *end
);
1494 jnl
->flush(jnl
->flush_arg
);
1496 tsleep((caddr_t
)jnl
, PRIBIO
, "jnl_close", 1);
1499 if (*start
!= *end
) {
1500 printf("jnl: close: buffer flushing didn't seem to flush out all the transactions! (0x%llx - 0x%llx)\n",
1504 // make sure this is in sync when we close the journal
1505 jnl
->jhdr
->start
= jnl
->active_start
;
1507 // if this fails there's not much we can do at this point...
1508 write_journal_header(jnl
);
1510 // if we're here the journal isn't valid any more.
1511 // so make sure we don't leave any locked blocks lying around
1512 printf("jnl: close: journal 0x%x, is invalid. aborting outstanding transactions\n", jnl
);
1513 if (jnl
->active_tr
|| jnl
->cur_tr
) {
1515 if (jnl
->active_tr
) {
1516 tr
= jnl
->active_tr
;
1517 jnl
->active_tr
= NULL
;
1523 abort_transaction(jnl
, tr
);
1524 if (jnl
->active_tr
|| jnl
->cur_tr
) {
1525 panic("jnl: close: jnl @ 0x%x had both an active and cur tr\n", jnl
);
1530 free_old_stuff(jnl
);
1532 kmem_free(kernel_map
, (vm_offset_t
)jnl
->header_buf
, jnl
->jhdr
->jhdr_size
);
1533 jnl
->jhdr
= (void *)0xbeefbabe;
1535 FREE_ZONE(jnl
, sizeof(struct journal
), M_JNL_JNL
);
1539 dump_journal(journal
*jnl
)
1544 printf(" jdev_offset %.8llx\n", jnl
->jdev_offset
);
1545 printf(" magic: 0x%.8x\n", jnl
->jhdr
->magic
);
1546 printf(" start: 0x%.8llx\n", jnl
->jhdr
->start
);
1547 printf(" end: 0x%.8llx\n", jnl
->jhdr
->end
);
1548 printf(" size: 0x%.8llx\n", jnl
->jhdr
->size
);
1549 printf(" blhdr size: %d\n", jnl
->jhdr
->blhdr_size
);
1550 printf(" jhdr size: %d\n", jnl
->jhdr
->jhdr_size
);
1551 printf(" chksum: 0x%.8x\n", jnl
->jhdr
->checksum
);
1553 printf(" completed transactions:\n");
1554 for(ctr
=jnl
->completed_trs
; ctr
; ctr
=ctr
->next
) {
1555 printf(" 0x%.8llx - 0x%.8llx\n", ctr
->journal_start
, ctr
->journal_end
);
1562 free_space(journal
*jnl
)
1566 if (jnl
->jhdr
->start
< jnl
->jhdr
->end
) {
1567 free_space
= jnl
->jhdr
->size
- (jnl
->jhdr
->end
- jnl
->jhdr
->start
) - jnl
->jhdr
->jhdr_size
;
1568 } else if (jnl
->jhdr
->start
> jnl
->jhdr
->end
) {
1569 free_space
= jnl
->jhdr
->start
- jnl
->jhdr
->end
;
1571 // journal is completely empty
1572 free_space
= jnl
->jhdr
->size
- jnl
->jhdr
->jhdr_size
;
1580 // The journal must be locked on entry to this function.
1581 // The "desired_size" is in bytes.
1584 check_free_space(journal
*jnl
, int desired_size
)
1588 //printf("jnl: check free space (desired 0x%x, avail 0x%Lx)\n",
1589 // desired_size, free_space(jnl));
1592 int old_start_empty
;
1594 if (counter
++ == 5000) {
1596 panic("jnl: check_free_space: buffer flushing isn't working "
1597 "(jnl @ 0x%x s %lld e %lld f %lld [active start %lld]).\n", jnl
,
1598 jnl
->jhdr
->start
, jnl
->jhdr
->end
, free_space(jnl
), jnl
->active_start
);
1600 if (counter
> 7500) {
1601 printf("jnl: check_free_space: giving up waiting for free space.\n");
1605 // make sure there's space in the journal to hold this transaction
1606 if (free_space(jnl
) > desired_size
) {
1611 // here's where we lazily bump up jnl->jhdr->start. we'll consume
1612 // entries until there is enough space for the next transaction.
1614 old_start_empty
= 1;
1615 simple_lock(&jnl
->old_start_lock
);
1616 for(i
=0; i
< sizeof(jnl
->old_start
)/sizeof(jnl
->old_start
[0]); i
++) {
1620 while (jnl
->old_start
[i
] & 0x8000000000000000LL
) {
1621 if (counter
++ > 100) {
1622 panic("jnl: check_free_space: tr starting @ 0x%llx not flushing (jnl 0x%x).\n",
1623 jnl
->old_start
[i
], jnl
);
1626 simple_unlock(&jnl
->old_start_lock
);
1628 jnl
->flush(jnl
->flush_arg
);
1630 tsleep((caddr_t
)jnl
, PRIBIO
, "check_free_space1", 1);
1631 simple_lock(&jnl
->old_start_lock
);
1634 if (jnl
->old_start
[i
] == 0) {
1638 old_start_empty
= 0;
1639 jnl
->jhdr
->start
= jnl
->old_start
[i
];
1640 jnl
->old_start
[i
] = 0;
1641 if (free_space(jnl
) > desired_size
) {
1642 write_journal_header(jnl
);
1646 simple_unlock(&jnl
->old_start_lock
);
1648 // if we bumped the start, loop and try again
1649 if (i
< sizeof(jnl
->old_start
)/sizeof(jnl
->old_start
[0])) {
1651 } else if (old_start_empty
) {
1653 // if there is nothing in old_start anymore then we can
1654 // bump the jhdr->start to be the same as active_start
1655 // since it is possible there was only one very large
1656 // transaction in the old_start array. if we didn't do
1657 // this then jhdr->start would never get updated and we
1658 // would wind up looping until we hit the panic at the
1659 // start of the loop.
1661 jnl
->jhdr
->start
= jnl
->active_start
;
1662 write_journal_header(jnl
);
1667 // if the file system gave us a flush function, call it to so that
1668 // it can flush some blocks which hopefully will cause some transactions
1669 // to complete and thus free up space in the journal.
1671 jnl
->flush(jnl
->flush_arg
);
1674 // wait for a while to avoid being cpu-bound (this will
1675 // put us to sleep for 10 milliseconds)
1676 tsleep((caddr_t
)jnl
, PRIBIO
, "check_free_space2", 1);
1683 journal_start_transaction(journal
*jnl
)
1691 if (jnl
->flags
& JOURNAL_INVALID
) {
1695 if (jnl
->owner
== current_act()) {
1696 if (jnl
->active_tr
== NULL
) {
1697 panic("jnl: start_tr: active_tr is NULL (jnl @ 0x%x, owner 0x%x, current_act 0x%x\n",
1698 jnl
, jnl
->owner
, current_act());
1700 jnl
->nested_count
++;
1704 ret
= lockmgr(&jnl
->jlock
, LK_EXCLUSIVE
|LK_RETRY
, NULL
, current_proc());
1706 printf("jnl: start_tr: locking the journal (0x%x) failed %d.\n", jnl
, ret
);
1710 if (jnl
->owner
!= NULL
|| jnl
->nested_count
!= 0 || jnl
->active_tr
!= NULL
) {
1711 panic("jnl: start_tr: owner 0x%x, nested count 0x%x, active_tr 0x%x jnl @ 0x%x\n",
1712 jnl
->owner
, jnl
->nested_count
, jnl
->active_tr
, jnl
);
1715 jnl
->owner
= current_act();
1716 jnl
->nested_count
= 1;
1718 free_old_stuff(jnl
);
1720 // make sure there's room in the journal
1721 if (check_free_space(jnl
, jnl
->tbuffer_size
) != 0) {
1722 printf("jnl: start transaction failed: no space\n");
1727 // if there's a buffered transaction, use it.
1729 jnl
->active_tr
= jnl
->cur_tr
;
1735 MALLOC_ZONE(tr
, transaction
*, sizeof(transaction
), M_JNL_TR
, M_WAITOK
);
1736 memset(tr
, 0, sizeof(transaction
));
1738 tr
->tbuffer_size
= jnl
->tbuffer_size
;
1739 thread_wire_internal(host_priv_self(), current_act(), TRUE
, &prev_priv
);
1740 if (kmem_alloc(kernel_map
, (vm_offset_t
*)&tr
->tbuffer
, tr
->tbuffer_size
)) {
1741 FREE_ZONE(tr
, sizeof(transaction
), M_JNL_TR
);
1742 printf("jnl: start transaction failed: no tbuffer mem\n");
1744 thread_wire_internal(host_priv_self(), current_act(), prev_priv
, NULL
);
1747 thread_wire_internal(host_priv_self(), current_act(), prev_priv
, NULL
);
1749 // journal replay code checksum check depends on this.
1750 memset(tr
->tbuffer
, 0, BLHDR_CHECKSUM_SIZE
);
1752 tr
->blhdr
= (block_list_header
*)tr
->tbuffer
;
1753 tr
->blhdr
->max_blocks
= (jnl
->jhdr
->blhdr_size
/ sizeof(block_info
)) - 1;
1754 tr
->blhdr
->num_blocks
= 1; // accounts for this header block
1755 tr
->blhdr
->bytes_used
= jnl
->jhdr
->blhdr_size
;
1758 tr
->total_bytes
= jnl
->jhdr
->blhdr_size
;
1761 jnl
->active_tr
= tr
;
1763 // printf("jnl: start_tr: owner 0x%x new tr @ 0x%x\n", jnl->owner, tr);
1769 jnl
->nested_count
= 0;
1770 lockmgr(&jnl
->jlock
, LK_RELEASE
, NULL
, current_proc());
1776 journal_modify_block_start(journal
*jnl
, struct buf
*bp
)
1782 if (jnl
->flags
& JOURNAL_INVALID
) {
1786 // XXXdbg - for debugging I want this to be true. later it may
1787 // not be necessary.
1788 if ((bp
->b_flags
& B_META
) == 0) {
1789 panic("jnl: modify_block_start: bp @ 0x%x is not a meta-data block! (jnl 0x%x)\n", bp
, jnl
);
1792 tr
= jnl
->active_tr
;
1793 CHECK_TRANSACTION(tr
);
1795 if (jnl
->owner
!= current_act()) {
1796 panic("jnl: modify_block_start: called w/out a transaction! jnl 0x%x, owner 0x%x, curact 0x%x\n",
1797 jnl
, jnl
->owner
, current_act());
1800 free_old_stuff(jnl
);
1802 //printf("jnl: mod block start (bp 0x%x vp 0x%x l/blkno %d/%d bsz %d; total bytes %d)\n",
1803 // bp, bp->b_vp, bp->b_lblkno, bp->b_blkno, bp->b_bufsize, tr->total_bytes);
1805 // can't allow blocks that aren't an even multiple of the
1806 // underlying block size.
1807 if ((bp
->b_bufsize
% jnl
->jhdr
->jhdr_size
) != 0) {
1808 panic("jnl: mod block start: bufsize %d not a multiple of block size %d\n",
1809 bp
->b_bufsize
, jnl
->jhdr
->jhdr_size
);
1813 // make sure that this transaction isn't bigger than the whole journal
1814 if (tr
->total_bytes
+bp
->b_bufsize
>= (jnl
->jhdr
->size
- jnl
->jhdr
->jhdr_size
)) {
1815 panic("jnl: transaction too big (%d >= %lld bytes, bufsize %d, tr 0x%x bp 0x%x)\n",
1816 tr
->total_bytes
, (tr
->jnl
->jhdr
->size
- jnl
->jhdr
->jhdr_size
), bp
->b_bufsize
, tr
, bp
);
1820 // if the block is dirty and not already locked we have to write
1821 // it out before we muck with it because it has data that belongs
1822 // (presumably) to another transaction.
1824 if ((bp
->b_flags
& B_DELWRI
) && (bp
->b_flags
& B_LOCKED
) == 0) {
1826 // this will cause it to not be brelse()'d
1827 bp
->b_flags
|= B_NORELSE
;
1831 bp
->b_flags
|= B_LOCKED
;
1837 journal_modify_block_abort(journal
*jnl
, struct buf
*bp
)
1840 block_list_header
*blhdr
;
1845 tr
= jnl
->active_tr
;
1848 // if there's no active transaction then we just want to
1849 // call brelse() and return since this is just a block
1850 // that happened to be modified as part of another tr.
1857 if (jnl
->flags
& JOURNAL_INVALID
) {
1861 CHECK_TRANSACTION(tr
);
1863 if (jnl
->owner
!= current_act()) {
1864 panic("jnl: modify_block_abort: called w/out a transaction! jnl 0x%x, owner 0x%x, curact 0x%x\n",
1865 jnl
, jnl
->owner
, current_act());
1868 free_old_stuff(jnl
);
1870 // printf("jnl: modify_block_abort: tr 0x%x bp 0x%x\n", jnl->active_tr, bp);
1872 // first check if it's already part of this transaction
1873 for(blhdr
=tr
->blhdr
; blhdr
; blhdr
=(block_list_header
*)((long)blhdr
->binfo
[0].bnum
)) {
1874 for(i
=1; i
< blhdr
->num_blocks
; i
++) {
1875 if (bp
== blhdr
->binfo
[i
].bp
) {
1876 if (bp
->b_bufsize
!= blhdr
->binfo
[i
].bsize
) {
1877 panic("jnl: bp @ 0x%x changed size on me! (%d vs. %d, jnl 0x%x)\n",
1878 bp
, bp
->b_bufsize
, blhdr
->binfo
[i
].bsize
, jnl
);
1884 if (i
< blhdr
->num_blocks
) {
1890 // if blhdr is null, then this block has only had modify_block_start
1891 // called on it as part of the current transaction. that means that
1892 // it is ok to clear the LOCKED bit since it hasn't actually been
1893 // modified. if blhdr is non-null then modify_block_end was called
1894 // on it and so we need to keep it locked in memory.
1896 if (blhdr
== NULL
) {
1897 bp
->b_flags
&= ~(B_LOCKED
);
1906 journal_modify_block_end(journal
*jnl
, struct buf
*bp
)
1908 int i
, j
, tbuffer_offset
;
1910 block_list_header
*blhdr
, *prev
=NULL
;
1915 if (jnl
->flags
& JOURNAL_INVALID
) {
1919 tr
= jnl
->active_tr
;
1920 CHECK_TRANSACTION(tr
);
1922 if (jnl
->owner
!= current_act()) {
1923 panic("jnl: modify_block_end: called w/out a transaction! jnl 0x%x, owner 0x%x, curact 0x%x\n",
1924 jnl
, jnl
->owner
, current_act());
1927 free_old_stuff(jnl
);
1929 //printf("jnl: mod block end: (bp 0x%x vp 0x%x l/blkno %d/%d bsz %d, total bytes %d)\n",
1930 // bp, bp->b_vp, bp->b_lblkno, bp->b_blkno, bp->b_bufsize, tr->total_bytes);
1932 if ((bp
->b_flags
& B_LOCKED
) == 0) {
1933 panic("jnl: modify_block_end: bp 0x%x not locked! jnl @ 0x%x\n", bp
, jnl
);
1934 bp
->b_flags
|= B_LOCKED
;
1937 // first check if it's already part of this transaction
1938 for(blhdr
=tr
->blhdr
; blhdr
; prev
=blhdr
,blhdr
=(block_list_header
*)((long)blhdr
->binfo
[0].bnum
)) {
1939 tbuffer_offset
= jnl
->jhdr
->blhdr_size
;
1941 for(i
=1; i
< blhdr
->num_blocks
; i
++) {
1942 if (bp
== blhdr
->binfo
[i
].bp
) {
1943 if (bp
->b_bufsize
!= blhdr
->binfo
[i
].bsize
) {
1944 panic("jnl: bp @ 0x%x changed size on me! (%d vs. %d, jnl 0x%x)\n",
1945 bp
, bp
->b_bufsize
, blhdr
->binfo
[i
].bsize
, jnl
);
1949 tbuffer_offset
+= blhdr
->binfo
[i
].bsize
;
1952 if (i
< blhdr
->num_blocks
) {
1959 && (prev
->num_blocks
+1) <= prev
->max_blocks
1960 && (prev
->bytes_used
+bp
->b_bufsize
) <= tr
->tbuffer_size
) {
1962 } else if (blhdr
== NULL
) {
1963 block_list_header
*nblhdr
;
1967 panic("jnl: modify block end: no way man, prev == NULL?!?, jnl 0x%x, bp 0x%x\n", jnl
, bp
);
1970 // we got to the end of the list, didn't find the block and there's
1971 // no room in the block_list_header pointed to by prev
1973 // we allocate another tbuffer and link it in at the end of the list
1974 // through prev->binfo[0].bnum. that's a skanky way to do things but
1975 // avoids having yet another linked list of small data structures to manage.
1977 thread_wire_internal(host_priv_self(), current_act(), TRUE
, &prev_priv
);
1978 if (kmem_alloc(kernel_map
, (vm_offset_t
*)&nblhdr
, tr
->tbuffer_size
)) {
1979 panic("jnl: end_tr: no space for new block tr @ 0x%x (total bytes: %d)!\n",
1980 tr
, tr
->total_bytes
);
1982 thread_wire_internal(host_priv_self(), current_act(), prev_priv
, NULL
);
1984 // journal replay code checksum check depends on this.
1985 memset(nblhdr
, 0, BLHDR_CHECKSUM_SIZE
);
1987 // initialize the new guy
1988 nblhdr
->max_blocks
= (jnl
->jhdr
->blhdr_size
/ sizeof(block_info
)) - 1;
1989 nblhdr
->num_blocks
= 1; // accounts for this header block
1990 nblhdr
->bytes_used
= jnl
->jhdr
->blhdr_size
;
1993 tr
->total_bytes
+= jnl
->jhdr
->blhdr_size
;
1995 // then link him in at the end
1996 prev
->binfo
[0].bnum
= (off_t
)((long)nblhdr
);
1998 // and finally switch to using the new guy
2000 tbuffer_offset
= jnl
->jhdr
->blhdr_size
;
2005 if ((i
+1) > blhdr
->max_blocks
) {
2006 panic("jnl: modify_block_end: i = %d, max_blocks %d\n", i
, blhdr
->max_blocks
);
2009 // copy the data into the in-memory transaction buffer
2010 blkptr
= (char *)&((char *)blhdr
)[tbuffer_offset
];
2011 memcpy(blkptr
, bp
->b_data
, bp
->b_bufsize
);
2013 // if this is true then this is a new block we haven't seen
2014 if (i
>= blhdr
->num_blocks
) {
2015 vget(bp
->b_vp
, 0, current_proc());
2017 blhdr
->binfo
[i
].bnum
= (off_t
)((unsigned)bp
->b_blkno
);
2018 blhdr
->binfo
[i
].bsize
= bp
->b_bufsize
;
2019 blhdr
->binfo
[i
].bp
= bp
;
2021 blhdr
->bytes_used
+= bp
->b_bufsize
;
2022 tr
->total_bytes
+= bp
->b_bufsize
;
2024 blhdr
->num_blocks
++;
2033 journal_kill_block(journal
*jnl
, struct buf
*bp
)
2036 block_list_header
*blhdr
;
2041 if (jnl
->flags
& JOURNAL_INVALID
) {
2045 tr
= jnl
->active_tr
;
2046 CHECK_TRANSACTION(tr
);
2048 if (jnl
->owner
!= current_act()) {
2049 panic("jnl: modify_block_end: called w/out a transaction! jnl 0x%x, owner 0x%x, curact 0x%x\n",
2050 jnl
, jnl
->owner
, current_act());
2053 free_old_stuff(jnl
);
2055 if ((bp
->b_flags
& B_LOCKED
) == 0) {
2056 panic("jnl: kill block: bp 0x%x not locked! jnl @ 0x%x\n", bp
, jnl
);
2059 // first check if it's already part of this transaction
2060 for(blhdr
=tr
->blhdr
; blhdr
; blhdr
=(block_list_header
*)((long)blhdr
->binfo
[0].bnum
)) {
2062 for(i
=1; i
< blhdr
->num_blocks
; i
++) {
2063 if (bp
== blhdr
->binfo
[i
].bp
) {
2064 bp
->b_flags
&= ~B_LOCKED
;
2066 // this undoes the vget() in journal_modify_block_end()
2069 // if the block has the DELWRI and CALL bits sets, then
2070 // things are seriously weird. if it was part of another
2071 // transaction then journal_modify_block_start() should
2072 // have force it to be written.
2074 if ((bp
->b_flags
& B_DELWRI
) && (bp
->b_flags
& B_CALL
)) {
2075 panic("jnl: kill block: this defies all logic! bp 0x%x\n", bp
);
2077 tr
->num_killed
+= bp
->b_bufsize
;
2080 if (bp
->b_flags
& B_BUSY
) {
2084 blhdr
->binfo
[i
].bp
= NULL
;
2085 blhdr
->binfo
[i
].bnum
= (off_t
)-1;
2090 if (i
< blhdr
->num_blocks
) {
2100 journal_binfo_cmp(void *a
, void *b
)
2102 block_info
*bi_a
= (struct block_info
*)a
,
2103 *bi_b
= (struct block_info
*)b
;
2106 if (bi_a
->bp
== NULL
) {
2109 if (bi_b
->bp
== NULL
) {
2113 // don't have to worry about negative block
2114 // numbers so this is ok to do.
2116 res
= (bi_a
->bp
->b_blkno
- bi_b
->bp
->b_blkno
);
2123 end_transaction(transaction
*tr
, int force_it
)
2127 journal
*jnl
= tr
->jnl
;
2129 block_list_header
*blhdr
=NULL
, *next
=NULL
;
2132 panic("jnl: jnl @ 0x%x already has cur_tr 0x%x, new tr: 0x%x\n",
2133 jnl
, jnl
->cur_tr
, tr
);
2136 // if there weren't any modified blocks in the transaction
2137 // just save off the transaction pointer and return.
2138 if (tr
->total_bytes
== jnl
->jhdr
->blhdr_size
) {
2143 // if our transaction buffer isn't very full, just hang
2144 // on to it and don't actually flush anything. this is
2145 // what is known as "group commit". we will flush the
2146 // transaction buffer if it's full or if we have more than
2147 // one of them so we don't start hogging too much memory.
2150 && (jnl
->flags
& JOURNAL_NO_GROUP_COMMIT
) == 0
2151 && tr
->num_blhdrs
< 3
2152 && (tr
->total_bytes
<= ((tr
->tbuffer_size
*tr
->num_blhdrs
) - tr
->tbuffer_size
/8))) {
2159 // if we're here we're going to flush the transaction buffer to disk.
2160 // make sure there is room in the journal first.
2161 check_free_space(jnl
, tr
->total_bytes
);
2163 // range check the end index
2164 if (jnl
->jhdr
->end
<= 0 || jnl
->jhdr
->end
> jnl
->jhdr
->size
) {
2165 panic("jnl: end_transaction: end is bogus 0x%llx (sz 0x%llx)\n",
2166 jnl
->jhdr
->end
, jnl
->jhdr
->size
);
2169 // this transaction starts where the current journal ends
2170 tr
->journal_start
= jnl
->jhdr
->end
;
2171 end
= jnl
->jhdr
->end
;
2174 // if the first entry in old_start[] isn't free yet, loop calling the
2175 // file system flush routine until it is (or we panic).
2178 simple_lock(&jnl
->old_start_lock
);
2179 while ((jnl
->old_start
[0] & 0x8000000000000000LL
) != 0) {
2181 simple_unlock(&jnl
->old_start_lock
);
2184 jnl
->flush(jnl
->flush_arg
);
2187 // yield the cpu so others can get in to clear the lock bit
2188 (void)tsleep((void *)jnl
, PRIBIO
, "jnl-old-start-sleep", 1);
2190 simple_lock(&jnl
->old_start_lock
);
2193 panic("jnl: transaction that started at 0x%llx is not completing! jnl 0x%x\n",
2194 jnl
->old_start
[0] & (~0x8000000000000000LL
), jnl
);
2199 // slide everyone else down and put our latest guy in the last
2200 // entry in the old_start array
2202 memcpy(&jnl
->old_start
[0], &jnl
->old_start
[1], sizeof(jnl
->old_start
)-sizeof(jnl
->old_start
[0]));
2203 jnl
->old_start
[sizeof(jnl
->old_start
)/sizeof(jnl
->old_start
[0]) - 1] = tr
->journal_start
| 0x8000000000000000LL
;
2205 simple_unlock(&jnl
->old_start_lock
);
2208 // for each block, make sure that the physical block # is set
2209 for(blhdr
=tr
->blhdr
; blhdr
; blhdr
=next
) {
2211 for(i
=1; i
< blhdr
->num_blocks
; i
++) {
2213 bp
= blhdr
->binfo
[i
].bp
;
2214 if (bp
== NULL
) { // only true if a block was "killed"
2215 if (blhdr
->binfo
[i
].bnum
!= (off_t
)-1) {
2216 panic("jnl: inconsistent binfo (NULL bp w/bnum %lld; jnl @ 0x%x, tr 0x%x)\n",
2217 blhdr
->binfo
[i
].bnum
, jnl
, tr
);
2222 if (bp
->b_vp
== NULL
&& bp
->b_lblkno
== bp
->b_blkno
) {
2223 panic("jnl: end_tr: DANGER! bp @ 0x%x w/null vp and l/blkno = %d/%d\n",
2224 bp
, bp
->b_lblkno
, bp
->b_blkno
);
2227 // if the lblkno is the same as blkno and this bp isn't
2228 // associated with the underlying file system device then
2229 // we need to call bmap() to get the actual physical block.
2231 if ((bp
->b_lblkno
== bp
->b_blkno
) && (bp
->b_vp
!= jnl
->fsdev
)) {
2232 if (VOP_BMAP(bp
->b_vp
, bp
->b_lblkno
, NULL
, &bp
->b_blkno
, NULL
) != 0) {
2233 printf("jnl: end_tr: can't bmap the bp @ 0x%x, jnl 0x%x\n", bp
, jnl
);
2238 // update this so we write out the correct physical block number!
2239 blhdr
->binfo
[i
].bnum
= (off_t
)((unsigned)bp
->b_blkno
);
2242 next
= (block_list_header
*)((long)blhdr
->binfo
[0].bnum
);
2245 for(blhdr
=tr
->blhdr
; blhdr
; blhdr
=(block_list_header
*)((long)blhdr
->binfo
[0].bnum
)) {
2247 amt
= blhdr
->bytes_used
;
2249 blhdr
->checksum
= 0;
2250 blhdr
->checksum
= calc_checksum((char *)blhdr
, BLHDR_CHECKSUM_SIZE
);
2252 ret
= write_journal_data(jnl
, &end
, blhdr
, amt
);
2254 printf("jnl: end_transaction: only wrote %d of %d bytes to the journal!\n",
2261 jnl
->jhdr
->end
= end
; // update where the journal now ends
2262 tr
->journal_end
= end
; // the transaction ends here too
2263 if (tr
->journal_start
== 0 || tr
->journal_end
== 0) {
2264 panic("jnl: end_transaction: bad tr journal start/end: 0x%llx 0x%llx\n",
2265 tr
->journal_start
, tr
->journal_end
);
2268 if (write_journal_header(jnl
) != 0) {
2273 // setup for looping through all the blhdr's. we null out the
2274 // tbuffer and blhdr fields so that they're not used any more.
2280 // the buffer_flushed_callback will only be called for the
2281 // real blocks that get flushed so we have to account for
2282 // the block_list_headers here.
2284 tr
->num_flushed
= tr
->num_blhdrs
* jnl
->jhdr
->blhdr_size
;
2286 // for each block, set the iodone callback and unlock it
2287 for(; blhdr
; blhdr
=next
) {
2289 // we can re-order the buf ptrs because everything is written out already
2290 qsort(&blhdr
->binfo
[1], blhdr
->num_blocks
-1, sizeof(block_info
), journal_binfo_cmp
);
2292 for(i
=1; i
< blhdr
->num_blocks
; i
++) {
2293 if (blhdr
->binfo
[i
].bp
== NULL
) {
2297 ret
= meta_bread(blhdr
->binfo
[i
].bp
->b_vp
,
2298 (daddr_t
)blhdr
->binfo
[i
].bp
->b_lblkno
,
2299 blhdr
->binfo
[i
].bp
->b_bufsize
,
2302 if (ret
== 0 && bp
!= NULL
) {
2303 struct vnode
*save_vp
;
2305 if (bp
!= blhdr
->binfo
[i
].bp
) {
2306 panic("jnl: end_tr: got back a different bp! (bp 0x%x should be 0x%x, jnl 0x%x\n",
2307 bp
, blhdr
->binfo
[i
].bp
, jnl
);
2310 if ((bp
->b_flags
& (B_LOCKED
|B_DELWRI
)) != (B_LOCKED
|B_DELWRI
)) {
2311 if (jnl
->flags
& JOURNAL_CLOSE_PENDING
) {
2315 panic("jnl: end_tr: !!!DANGER!!! bp 0x%x flags (0x%x) not LOCKED & DELWRI\n", bp
, bp
->b_flags
);
2319 if (bp
->b_iodone
!= NULL
) {
2320 panic("jnl: bp @ 0x%x (blkno %d, vp 0x%x) has non-null iodone (0x%x) buffflushcb 0x%x\n",
2321 bp
, bp
->b_blkno
, bp
->b_vp
, bp
->b_iodone
, buffer_flushed_callback
);
2326 bp
->b_iodone
= buffer_flushed_callback
;
2327 bp
->b_transaction
= tr
;
2328 bp
->b_flags
|= B_CALL
;
2329 bp
->b_flags
&= ~(B_LOCKED
);
2331 // kicking off the write here helps performance
2333 // XXXdbg this is good for testing: bdwrite(bp);
2336 // this undoes the vget() in journal_modify_block_end()
2340 printf("jnl: end_transaction: could not find block %Ld vp 0x%x!\n",
2341 blhdr
->binfo
[i
].bnum
, blhdr
->binfo
[i
].bp
);
2348 next
= (block_list_header
*)((long)blhdr
->binfo
[0].bnum
);
2350 // we can free blhdr here since we won't need it any more
2351 blhdr
->binfo
[0].bnum
= 0xdeadc0de;
2352 kmem_free(kernel_map
, (vm_offset_t
)blhdr
, tr
->tbuffer_size
);
2355 //printf("jnl: end_tr: tr @ 0x%x, jnl-blocks: 0x%llx - 0x%llx. exit!\n",
2356 // tr, tr->journal_start, tr->journal_end);
2361 jnl
->flags
|= JOURNAL_INVALID
;
2362 abort_transaction(jnl
, tr
);
2367 abort_transaction(journal
*jnl
, transaction
*tr
)
2370 block_list_header
*blhdr
, *next
;
2372 struct vnode
*save_vp
;
2374 // for each block list header, iterate over the blocks then
2375 // free up the memory associated with the block list.
2377 // for each block, clear the lock bit and release it.
2379 for(blhdr
=tr
->blhdr
; blhdr
; blhdr
=next
) {
2381 for(i
=1; i
< blhdr
->num_blocks
; i
++) {
2382 if (blhdr
->binfo
[i
].bp
== NULL
) {
2386 ret
= meta_bread(blhdr
->binfo
[i
].bp
->b_vp
,
2387 (daddr_t
)blhdr
->binfo
[i
].bp
->b_lblkno
,
2388 blhdr
->binfo
[i
].bp
->b_bufsize
,
2392 if (bp
!= blhdr
->binfo
[i
].bp
) {
2393 panic("jnl: abort_tr: got back a different bp! (bp 0x%x should be 0x%x, jnl 0x%x\n",
2394 bp
, blhdr
->binfo
[i
].bp
, jnl
);
2397 // clear the locked bit and the delayed-write bit. we
2398 // don't want these blocks going to disk.
2399 bp
->b_flags
&= ~(B_LOCKED
|B_DELWRI
);
2400 bp
->b_flags
|= B_INVAL
;
2408 printf("jnl: abort_tr: could not find block %Ld vp 0x%x!\n",
2409 blhdr
->binfo
[i
].bnum
, blhdr
->binfo
[i
].bp
);
2416 next
= (block_list_header
*)((long)blhdr
->binfo
[0].bnum
);
2418 // we can free blhdr here since we won't need it any more
2419 blhdr
->binfo
[0].bnum
= 0xdeadc0de;
2420 kmem_free(kernel_map
, (vm_offset_t
)blhdr
, tr
->tbuffer_size
);
2425 tr
->total_bytes
= 0xdbadc0de;
2426 FREE_ZONE(tr
, sizeof(transaction
), M_JNL_TR
);
2431 journal_end_transaction(journal
*jnl
)
2438 if ((jnl
->flags
& JOURNAL_INVALID
) && jnl
->owner
== NULL
) {
2442 if (jnl
->owner
!= current_act()) {
2443 panic("jnl: end_tr: I'm not the owner! jnl 0x%x, owner 0x%x, curact 0x%x\n",
2444 jnl
, jnl
->owner
, current_act());
2447 free_old_stuff(jnl
);
2449 jnl
->nested_count
--;
2450 if (jnl
->nested_count
> 0) {
2452 } else if (jnl
->nested_count
< 0) {
2453 panic("jnl: jnl @ 0x%x has negative nested count (%d). bad boy.\n", jnl
, jnl
->nested_count
);
2456 if (jnl
->flags
& JOURNAL_INVALID
) {
2457 if (jnl
->active_tr
) {
2460 if (jnl
->cur_tr
!= NULL
) {
2461 panic("jnl: journal @ 0x%x has active tr (0x%x) and cur tr (0x%x)\n",
2462 jnl
, jnl
->active_tr
, jnl
->cur_tr
);
2465 tr
= jnl
->active_tr
;
2466 jnl
->active_tr
= NULL
;
2467 abort_transaction(jnl
, tr
);
2471 lockmgr(&jnl
->jlock
, LK_RELEASE
, NULL
, current_proc());
2476 tr
= jnl
->active_tr
;
2477 CHECK_TRANSACTION(tr
);
2479 // clear this out here so that when check_free_space() calls
2480 // the FS flush function, we don't panic in journal_flush()
2481 // if the FS were to call that. note: check_free_space() is
2482 // called from end_transaction().
2484 jnl
->active_tr
= NULL
;
2485 ret
= end_transaction(tr
, 0);
2488 lockmgr(&jnl
->jlock
, LK_RELEASE
, NULL
, current_proc());
2495 journal_flush(journal
*jnl
)
2497 int need_signal
= 0;
2501 if (jnl
->flags
& JOURNAL_INVALID
) {
2505 if (jnl
->owner
!= current_act()) {
2508 ret
= lockmgr(&jnl
->jlock
, LK_EXCLUSIVE
|LK_RETRY
, NULL
, current_proc());
2510 printf("jnl: flush: locking the journal (0x%x) failed %d.\n", jnl
, ret
);
2516 free_old_stuff(jnl
);
2518 // if we're not active, flush any buffered transactions
2519 if (jnl
->active_tr
== NULL
&& jnl
->cur_tr
) {
2520 transaction
*tr
= jnl
->cur_tr
;
2523 end_transaction(tr
, 1); // force it to get flushed
2527 lockmgr(&jnl
->jlock
, LK_RELEASE
, NULL
, current_proc());
2534 journal_active(journal
*jnl
)
2536 if (jnl
->flags
& JOURNAL_INVALID
) {
2540 return (jnl
->active_tr
== NULL
) ? 0 : 1;