2 * Copyright (c) 1995-2002 Apple Computer, Inc. All rights reserved.
4 * @APPLE_LICENSE_HEADER_START@
6 * The contents of this file constitute Original Code as defined in and
7 * are subject to the Apple Public Source License Version 1.1 (the
8 * "License"). You may not use this file except in compliance with the
9 * License. Please obtain a copy of the License at
10 * http://www.apple.com/publicsource and read it before using this file.
12 * This Original Code and all software distributed under the License are
13 * distributed on an "AS IS" basis, WITHOUT WARRANTY OF ANY KIND, EITHER
14 * EXPRESS OR IMPLIED, AND APPLE HEREBY DISCLAIMS ALL SUCH WARRANTIES,
15 * INCLUDING WITHOUT LIMITATION, ANY WARRANTIES OF MERCHANTABILITY,
16 * FITNESS FOR A PARTICULAR PURPOSE OR NON-INFRINGEMENT. Please see the
17 * License for the specific language governing rights and limitations
20 * @APPLE_LICENSE_HEADER_END@
23 // This file implements a simple write-ahead journaling layer.
24 // In theory any file system can make use of it by calling these
25 // functions when the fs wants to modify meta-data blocks. See
26 // vfs_journal.h for a more detailed description of the api and
29 // Dominic Giampaolo (dbg@apple.com)
34 #include <sys/param.h>
35 #include <sys/systm.h>
36 #include <sys/kernel.h>
41 #include <sys/mount.h>
42 #include <sys/namei.h>
43 #include <sys/vnode.h>
44 #include <sys/ioctl.h>
47 #include <sys/malloc.h>
48 #include <sys/vnode.h>
49 #include <kern/thread_act.h>
51 #include <miscfs/specfs/specdev.h>
53 extern task_t kernel_task
;
65 #include <sys/types.h>
70 #include "vfs_journal.h"
73 // number of bytes to checksum in a block_list_header
74 // NOTE: this should be enough to clear out the header
75 // fields as well as the first entry of binfo[]
76 #define BLHDR_CHECKSUM_SIZE 32
80 static int end_transaction(transaction
*tr
, int force_it
);
81 static void abort_transaction(journal
*jnl
, transaction
*tr
);
82 static void dump_journal(journal
*jnl
);
85 #define CHECK_JOURNAL(jnl) \
88 panic("%s:%d: null journal ptr?\n", __FILE__, __LINE__);\
90 if (jnl->jdev == NULL) { \
91 panic("%s:%d: jdev is null!\n", __FILE__, __LINE__);\
93 if (jnl->fsdev == NULL) { \
94 panic("%s:%d: fsdev is null!\n", __FILE__, __LINE__);\
96 if (jnl->jhdr->magic != JOURNAL_HEADER_MAGIC) {\
97 panic("%s:%d: jhdr magic corrupted (0x%x != 0x%x)\n",\
98 __FILE__, __LINE__, jnl->jhdr->magic, JOURNAL_HEADER_MAGIC);\
100 if ( jnl->jhdr->start <= 0 \
101 || jnl->jhdr->start > jnl->jhdr->size\
102 || jnl->jhdr->start > 128*1024*1024) {\
103 panic("%s:%d: jhdr start looks bad (0x%llx max size 0x%llx)\n", \
104 __FILE__, __LINE__, jnl->jhdr->start, jnl->jhdr->size);\
106 if ( jnl->jhdr->end <= 0 \
107 || jnl->jhdr->end > jnl->jhdr->size\
108 || jnl->jhdr->end > 128*1024*1024) {\
109 panic("%s:%d: jhdr end looks bad (0x%llx max size 0x%llx)\n", \
110 __FILE__, __LINE__, jnl->jhdr->end, jnl->jhdr->size);\
112 if (jnl->jhdr->size > 128*1024*1024) {\
113 panic("%s:%d: jhdr size looks bad (0x%llx)\n",\
114 __FILE__, __LINE__, jnl->jhdr->size);\
118 #define CHECK_TRANSACTION(tr) \
121 panic("%s:%d: null transaction ptr?\n", __FILE__, __LINE__);\
123 if (tr->jnl == NULL) {\
124 panic("%s:%d: null tr->jnl ptr?\n", __FILE__, __LINE__);\
126 if (tr->blhdr != (block_list_header *)tr->tbuffer) {\
127 panic("%s:%d: blhdr (0x%x) != tbuffer (0x%x)\n", __FILE__, __LINE__, tr->blhdr, tr->tbuffer);\
129 if (tr->total_bytes < 0) {\
130 panic("%s:%d: tr total_bytes looks bad: %d\n", __FILE__, __LINE__, tr->total_bytes);\
132 if (tr->journal_start < 0 || tr->journal_start > 128*1024*1024) {\
133 panic("%s:%d: tr journal start looks bad: 0x%llx\n", __FILE__, __LINE__, tr->journal_start);\
135 if (tr->journal_end < 0 || tr->journal_end > 128*1024*1024) {\
136 panic("%s:%d: tr journal end looks bad: 0x%llx\n", __FILE__, __LINE__, tr->journal_end);\
138 if (tr->blhdr && (tr->blhdr->max_blocks <= 0 || tr->blhdr->max_blocks > 2048)) {\
139 panic("%s:%d: tr blhdr max_blocks looks bad: %d\n", __FILE__, __LINE__, tr->blhdr->max_blocks);\
146 // this isn't a great checksum routine but it will do for now.
147 // we use it to checksum the journal header and the block list
148 // headers that are at the start of each transaction.
151 calc_checksum(char *ptr
, int len
)
155 // this is a lame checksum but for now it'll do
156 for(i
=0; i
< len
; i
++, ptr
++) {
157 cksum
= (cksum
<< 8) ^ (cksum
+ *(unsigned char *)ptr
);
168 // This function sets up a fake buf and passes it directly to the
169 // journal device strategy routine (so that it won't get cached in
172 // It also handles range checking the i/o so that we don't write
173 // outside the journal boundaries and it will wrap the i/o back
174 // to the beginning if necessary (skipping over the journal header)
177 do_journal_io(journal
*jnl
, off_t
*offset
, void *data
, size_t len
, int direction
)
179 int err
, io_sz
=0, curlen
=len
;
181 int max_iosize
=0, max_vectors
;
183 if (*offset
< 0 || *offset
> jnl
->jhdr
->size
) {
184 panic("jnl: do_jnl_io: bad offset 0x%llx (max 0x%llx)\n", *offset
, jnl
->jhdr
->size
);
188 bp
= alloc_io_buf(jnl
->jdev
, 1);
190 if (direction
== JNL_WRITE
) {
191 bp
->b_flags
|= 0; // don't have to set any flags (was: B_WRITEINPROG)
192 jnl
->jdev
->v_numoutput
++;
193 vfs_io_attributes(jnl
->jdev
, B_WRITE
, &max_iosize
, &max_vectors
);
194 } else if (direction
== JNL_READ
) {
195 bp
->b_flags
|= B_READ
;
196 vfs_io_attributes(jnl
->jdev
, B_READ
, &max_iosize
, &max_vectors
);
199 if (max_iosize
== 0) {
200 max_iosize
= 128 * 1024;
203 if (*offset
+ (off_t
)curlen
> jnl
->jhdr
->size
&& *offset
!= 0 && jnl
->jhdr
->size
!= 0) {
204 if (*offset
== jnl
->jhdr
->size
) {
205 *offset
= jnl
->jhdr
->jhdr_size
;
207 curlen
= (off_t
)jnl
->jhdr
->size
- *offset
;
211 if (curlen
> max_iosize
) {
216 panic("jnl: do_jnl_io: curlen == %d, offset 0x%llx len %d\n", curlen
, *offset
, len
);
219 bp
->b_bufsize
= curlen
;
220 bp
->b_bcount
= curlen
;
222 bp
->b_blkno
= (daddr_t
) ((jnl
->jdev_offset
+ *offset
) / (off_t
)jnl
->jhdr
->jhdr_size
);
223 bp
->b_lblkno
= (daddr_t
) ((jnl
->jdev_offset
+ *offset
) / (off_t
)jnl
->jhdr
->jhdr_size
);
225 err
= VOP_STRATEGY(bp
);
231 bp
->b_bufsize
= bp
->b_bcount
= 0;
232 bp
->b_blkno
= bp
->b_lblkno
= -1;
237 printf("jnl: do_jnl_io: strategy err 0x%x\n", err
);
244 // handle wrap-around
245 data
= (char *)data
+ curlen
;
246 curlen
= len
- io_sz
;
247 if (*offset
>= jnl
->jhdr
->size
) {
248 *offset
= jnl
->jhdr
->jhdr_size
;
257 read_journal_data(journal
*jnl
, off_t
*offset
, void *data
, size_t len
)
259 return do_journal_io(jnl
, offset
, data
, len
, JNL_READ
);
263 write_journal_data(journal
*jnl
, off_t
*offset
, void *data
, size_t len
)
265 return do_journal_io(jnl
, offset
, data
, len
, JNL_WRITE
);
270 write_journal_header(journal
*jnl
)
273 off_t jhdr_offset
= 0;
276 // XXXdbg note: this ioctl doesn't seem to do anything on firewire disks.
278 ret
= VOP_IOCTL(jnl
->jdev
, DKIOCSYNCHRONIZECACHE
, NULL
, FWRITE
, NOCRED
, current_proc());
280 printf("jnl: flushing fs disk buffer returned 0x%x\n", ret
);
284 jnl
->jhdr
->checksum
= 0;
285 jnl
->jhdr
->checksum
= calc_checksum((char *)jnl
->jhdr
, sizeof(struct journal_header
));
286 if (write_journal_data(jnl
, &jhdr_offset
, jnl
->header_buf
, jnl
->jhdr
->jhdr_size
) != jnl
->jhdr
->jhdr_size
) {
287 printf("jnl: write_journal_header: error writing the journal header!\n");
288 jnl
->flags
|= JOURNAL_INVALID
;
298 // this is a work function used to free up transactions that
299 // completed. they can't be free'd from buffer_flushed_callback
300 // because it is called from deep with the disk driver stack
301 // and thus can't do something that would potentially cause
302 // paging. it gets called by each of the journal api entry
303 // points so stuff shouldn't hang around for too long.
306 free_old_stuff(journal
*jnl
)
308 transaction
*tr
, *next
;
310 for(tr
=jnl
->tr_freeme
; tr
; tr
=next
) {
312 FREE_ZONE(tr
, sizeof(transaction
), M_JNL_TR
);
315 jnl
->tr_freeme
= NULL
;
321 // This is our callback that lets us know when a buffer has been
322 // flushed to disk. It's called from deep within the driver stack
323 // and thus is quite limited in what it can do. Notably, it can
324 // not initiate any new i/o's or allocate/free memory.
327 buffer_flushed_callback(struct buf
*bp
)
331 transaction
*ctr
, *prev
=NULL
, *next
;
335 //printf("jnl: buf flush: bp @ 0x%x l/blkno %d/%d vp 0x%x tr @ 0x%x\n",
336 // bp, bp->b_lblkno, bp->b_blkno, bp->b_vp, bp->b_transaction);
338 // snarf out the bits we want
339 bufsize
= bp
->b_bufsize
;
340 tr
= bp
->b_transaction
;
342 bp
->b_iodone
= NULL
; // don't call us for this guy again
343 bp
->b_transaction
= NULL
;
346 // This is what biodone() would do if it didn't call us.
347 // NOTE: THIS CODE *HAS* TO BE HERE!
349 if (ISSET(bp
->b_flags
, B_ASYNC
)) { /* if async, release it */
351 } else { /* or just wakeup the buffer */
352 CLR(bp
->b_flags
, B_WANTED
);
356 // NOTE: from here on out we do *NOT* touch bp anymore.
359 // then we've already seen it
364 CHECK_TRANSACTION(tr
);
367 if (jnl
->flags
& JOURNAL_INVALID
) {
373 // update the number of blocks that have been flushed.
374 // this buf may represent more than one block so take
375 // that into account.
376 tr
->num_flushed
+= bufsize
;
379 // if this transaction isn't done yet, just return as
380 // there is nothing to do.
381 if ((tr
->num_flushed
+ tr
->num_killed
) < tr
->total_bytes
) {
385 //printf("jnl: tr 0x%x (0x%llx 0x%llx) in jnl 0x%x completed.\n",
386 // tr, tr->journal_start, tr->journal_end, jnl);
388 // find this entry in the old_start[] index and mark it completed
389 simple_lock(&jnl
->old_start_lock
);
390 for(i
=0; i
< sizeof(jnl
->old_start
)/sizeof(jnl
->old_start
[0]); i
++) {
392 if ((jnl
->old_start
[i
] & ~(0x8000000000000000LL
)) == tr
->journal_start
) {
393 jnl
->old_start
[i
] &= ~(0x8000000000000000LL
);
397 if (i
>= sizeof(jnl
->old_start
)/sizeof(jnl
->old_start
[0])) {
398 panic("jnl: buffer_flushed: did not find tr w/start @ %lld (tr 0x%x, jnl 0x%x)\n",
399 tr
->journal_start
, tr
, jnl
);
401 simple_unlock(&jnl
->old_start_lock
);
404 // if we are here then we need to update the journal header
405 // to reflect that this transaction is complete
406 if (tr
->journal_start
== jnl
->active_start
) {
407 jnl
->active_start
= tr
->journal_end
;
408 tr
->journal_start
= tr
->journal_end
= (off_t
)0;
411 // go through the completed_trs list and try to coalesce
412 // entries, restarting back at the beginning if we have to.
413 for(ctr
=jnl
->completed_trs
; ctr
; prev
=ctr
, ctr
=next
) {
414 if (ctr
->journal_start
== jnl
->active_start
) {
415 jnl
->active_start
= ctr
->journal_end
;
417 prev
->next
= ctr
->next
;
419 if (ctr
== jnl
->completed_trs
) {
420 jnl
->completed_trs
= ctr
->next
;
423 next
= jnl
->completed_trs
; // this starts us over again
424 ctr
->next
= jnl
->tr_freeme
;
425 jnl
->tr_freeme
= ctr
;
427 } else if (tr
->journal_end
== ctr
->journal_start
) {
428 ctr
->journal_start
= tr
->journal_start
;
429 next
= jnl
->completed_trs
; // this starts us over again
431 tr
->journal_start
= tr
->journal_end
= (off_t
)0;
432 } else if (tr
->journal_start
== ctr
->journal_end
) {
433 ctr
->journal_end
= tr
->journal_end
;
435 tr
->journal_start
= tr
->journal_end
= (off_t
)0;
441 // at this point no one should be using this guy anymore
442 tr
->total_bytes
= 0xfbadc0de;
444 // if this is true then we didn't merge with anyone
445 // so link ourselves in at the head of the completed
447 if (tr
->journal_start
!= 0) {
448 // put this entry into the correct sorted place
449 // in the list instead of just at the head.
453 for(ctr
=jnl
->completed_trs
; ctr
&& tr
->journal_start
> ctr
->journal_start
; prev
=ctr
, ctr
=ctr
->next
) {
457 if (ctr
== NULL
&& prev
== NULL
) {
458 jnl
->completed_trs
= tr
;
460 } else if (ctr
== jnl
->completed_trs
) {
461 tr
->next
= jnl
->completed_trs
;
462 jnl
->completed_trs
= tr
;
464 tr
->next
= prev
->next
;
468 // if we're here this tr got merged with someone else so
469 // put it on the list to be free'd
470 tr
->next
= jnl
->tr_freeme
;
476 update_fs_block(journal
*jnl
, void *block_ptr
, off_t fs_block
, size_t bsize
)
479 struct buf
*oblock_bp
=NULL
;
481 // first read the block we want.
482 ret
= meta_bread(jnl
->fsdev
, (daddr_t
)fs_block
, bsize
, NOCRED
, &oblock_bp
);
484 printf("jnl: update_fs_block: error reading fs block # %lld! (ret %d)\n", fs_block
, ret
);
491 // let's try to be aggressive here and just re-write the block
492 oblock_bp
= getblk(jnl
->fsdev
, (daddr_t
)fs_block
, bsize
, 0, 0, BLK_META
);
493 if (oblock_bp
== NULL
) {
494 printf("jnl: update_fs_block: getblk() for %lld failed! failing update.\n", fs_block
);
499 // make sure it's the correct size.
500 if (oblock_bp
->b_bufsize
!= bsize
) {
505 // copy the journal data over top of it
506 memcpy(oblock_bp
->b_data
, block_ptr
, bsize
);
508 if ((ret
= VOP_BWRITE(oblock_bp
)) != 0) {
509 printf("jnl: update_fs_block: failed to update block %lld (ret %d)\n", fs_block
,ret
);
513 // and now invalidate it so that if someone else wants to read
514 // it in a different size they'll be able to do it.
515 ret
= meta_bread(jnl
->fsdev
, (daddr_t
)fs_block
, bsize
, NOCRED
, &oblock_bp
);
517 oblock_bp
->b_flags
|= B_INVAL
;
526 replay_journal(journal
*jnl
)
528 int i
, ret
, checksum
, max_bsize
;
529 struct buf
*oblock_bp
;
530 block_list_header
*blhdr
;
532 char *buf
, *block_ptr
=NULL
;
534 // wrap the start ptr if it points to the very end of the journal
535 if (jnl
->jhdr
->start
== jnl
->jhdr
->size
) {
536 jnl
->jhdr
->start
= jnl
->jhdr
->jhdr_size
;
538 if (jnl
->jhdr
->end
== jnl
->jhdr
->size
) {
539 jnl
->jhdr
->end
= jnl
->jhdr
->jhdr_size
;
542 if (jnl
->jhdr
->start
== jnl
->jhdr
->end
) {
546 // allocate memory for the header_block. we'll read each blhdr into this
547 if (kmem_alloc(kernel_map
, (vm_offset_t
*)&buf
, jnl
->jhdr
->blhdr_size
)) {
548 printf("jnl: replay_journal: no memory for block buffer! (%d bytes)\n",
549 jnl
->jhdr
->blhdr_size
);
554 printf("jnl: replay_journal: from: %lld to: %lld (joffset 0x%llx)\n",
555 jnl
->jhdr
->start
, jnl
->jhdr
->end
, jnl
->jdev_offset
);
557 while(jnl
->jhdr
->start
!= jnl
->jhdr
->end
) {
558 offset
= jnl
->jhdr
->start
;
559 ret
= read_journal_data(jnl
, &offset
, buf
, jnl
->jhdr
->blhdr_size
);
560 if (ret
!= jnl
->jhdr
->blhdr_size
) {
561 printf("jnl: replay_journal: Could not read block list header block @ 0x%llx!\n", offset
);
565 blhdr
= (block_list_header
*)buf
;
566 checksum
= blhdr
->checksum
;
568 if (checksum
!= calc_checksum((char *)blhdr
, BLHDR_CHECKSUM_SIZE
)) {
569 printf("jnl: replay_journal: bad block list header @ 0x%llx (checksum 0x%x != 0x%x)\n",
570 offset
, checksum
, calc_checksum((char *)blhdr
, BLHDR_CHECKSUM_SIZE
));
573 if ( blhdr
->max_blocks
<= 0 || blhdr
->max_blocks
> 2048
574 || blhdr
->num_blocks
<= 0 || blhdr
->num_blocks
> blhdr
->max_blocks
) {
575 printf("jnl: replay_journal: bad looking journal entry: max: %d num: %d\n",
576 blhdr
->max_blocks
, blhdr
->num_blocks
);
580 for(i
=1,max_bsize
=0; i
< blhdr
->num_blocks
; i
++) {
581 if (blhdr
->binfo
[i
].bnum
< 0 && blhdr
->binfo
[i
].bnum
!= (off_t
)-1) {
582 printf("jnl: replay_journal: bogus block number 0x%llx\n", blhdr
->binfo
[i
].bnum
);
585 if (blhdr
->binfo
[i
].bsize
> max_bsize
) {
586 max_bsize
= blhdr
->binfo
[i
].bsize
;
590 // make sure it's at least one page in size.
591 if (max_bsize
& (PAGE_SIZE
- 1)) {
592 max_bsize
= (max_bsize
+ PAGE_SIZE
) & ~(PAGE_SIZE
- 1);
595 if (kmem_alloc(kernel_map
, (vm_offset_t
*)&block_ptr
, max_bsize
)) {
599 //printf("jnl: replay_journal: %d blocks in journal entry @ 0x%llx\n", blhdr->num_blocks-1,
600 // jnl->jhdr->start);
601 for(i
=1; i
< blhdr
->num_blocks
; i
++) {
604 size
= blhdr
->binfo
[i
].bsize
;
606 ret
= read_journal_data(jnl
, &offset
, block_ptr
, size
);
608 printf("jnl: replay_journal: Could not read journal entry data @ offset 0x%llx!\n", offset
);
612 // don't replay "killed" blocks
613 if (blhdr
->binfo
[i
].bnum
== (off_t
)-1) {
614 // printf("jnl: replay_journal: skipping killed fs block (slot %d)\n", i);
616 //printf("jnl: replay_journal: fixing fs block # %lld (%d)\n",
617 // blhdr->binfo[i].bnum, blhdr->binfo[i].bsize);
619 if (update_fs_block(jnl
, block_ptr
, blhdr
->binfo
[i
].bnum
, blhdr
->binfo
[i
].bsize
) != 0) {
624 // check if we need to wrap offset back to the beginning
625 // (which is just past the journal header)
627 if (offset
>= jnl
->jhdr
->size
) {
628 offset
= jnl
->jhdr
->jhdr_size
;
632 kmem_free(kernel_map
, (vm_offset_t
)block_ptr
, max_bsize
);
635 jnl
->jhdr
->start
+= blhdr
->bytes_used
;
636 if (jnl
->jhdr
->start
>= jnl
->jhdr
->size
) {
637 // wrap around and skip the journal header block
638 jnl
->jhdr
->start
= (jnl
->jhdr
->start
% jnl
->jhdr
->size
) + jnl
->jhdr
->jhdr_size
;
641 // only update the on-disk journal header if we've reached the
642 // last chunk of updates from this transaction. if binfo[0].bnum
643 // is zero then we know we're at the end.
644 if (blhdr
->binfo
[0].bnum
== 0) {
645 if (write_journal_header(jnl
) != 0) {
651 kmem_free(kernel_map
, (vm_offset_t
)buf
, jnl
->jhdr
->blhdr_size
);
656 kmem_free(kernel_map
, (vm_offset_t
)block_ptr
, max_bsize
);
658 kmem_free(kernel_map
, (vm_offset_t
)buf
, jnl
->jhdr
->blhdr_size
);
663 #define DEFAULT_TRANSACTION_BUFFER_SIZE (128*1024)
664 //#define DEFAULT_TRANSACTION_BUFFER_SIZE (256*1024) // better performance but uses more mem
665 #define MAX_TRANSACTION_BUFFER_SIZE (512*1024)
667 // XXXdbg - so I can change it in the debugger
668 int def_tbuffer_size
= 0;
672 // This function sets the size of the tbuffer and the
673 // size of the blhdr. It assumes that jnl->jhdr->size
674 // and jnl->jhdr->jhdr_size are already valid.
677 size_up_tbuffer(journal
*jnl
, int tbuffer_size
, int phys_blksz
)
680 // one-time initialization based on how much memory
681 // there is in the machine.
683 if (def_tbuffer_size
== 0) {
684 if (mem_size
< (256*1024*1024)) {
685 def_tbuffer_size
= DEFAULT_TRANSACTION_BUFFER_SIZE
;
686 } else if (mem_size
< (512*1024*1024)) {
687 def_tbuffer_size
= DEFAULT_TRANSACTION_BUFFER_SIZE
* 2;
688 } else if (mem_size
< (1024*1024*1024)) {
689 def_tbuffer_size
= DEFAULT_TRANSACTION_BUFFER_SIZE
* 3;
690 } else if (mem_size
>= (1024*1024*1024)) {
691 def_tbuffer_size
= DEFAULT_TRANSACTION_BUFFER_SIZE
* 4;
695 // size up the transaction buffer... can't be larger than the number
696 // of blocks that can fit in a block_list_header block.
697 if (tbuffer_size
== 0) {
698 jnl
->tbuffer_size
= def_tbuffer_size
;
700 // make sure that the specified tbuffer_size isn't too small
701 if (tbuffer_size
< jnl
->jhdr
->blhdr_size
* 2) {
702 tbuffer_size
= jnl
->jhdr
->blhdr_size
* 2;
704 // and make sure it's an even multiple of the block size
705 if ((tbuffer_size
% jnl
->jhdr
->jhdr_size
) != 0) {
706 tbuffer_size
-= (tbuffer_size
% jnl
->jhdr
->jhdr_size
);
709 jnl
->tbuffer_size
= tbuffer_size
;
712 if (jnl
->tbuffer_size
> (jnl
->jhdr
->size
/ 2)) {
713 jnl
->tbuffer_size
= (jnl
->jhdr
->size
/ 2);
716 if (jnl
->tbuffer_size
> MAX_TRANSACTION_BUFFER_SIZE
) {
717 jnl
->tbuffer_size
= MAX_TRANSACTION_BUFFER_SIZE
;
720 jnl
->jhdr
->blhdr_size
= (jnl
->tbuffer_size
/ jnl
->jhdr
->jhdr_size
) * sizeof(block_info
);
721 if (jnl
->jhdr
->blhdr_size
< phys_blksz
) {
722 jnl
->jhdr
->blhdr_size
= phys_blksz
;
729 journal_create(struct vnode
*jvp
,
735 int32_t tbuffer_size
,
736 void (*flush
)(void *arg
),
742 /* Get the real physical block size. */
743 if (VOP_IOCTL(jvp
, DKIOCGETBLOCKSIZE
, (caddr_t
)&phys_blksz
, 0, FSCRED
, NULL
)) {
747 if (phys_blksz
> min_fs_blksz
) {
748 printf("jnl: create: error: phys blksize %d bigger than min fs blksize %d\n",
749 phys_blksz
, min_fs_blksz
);
753 if ((journal_size
% phys_blksz
) != 0) {
754 printf("jnl: create: journal size 0x%llx is not an even multiple of block size 0x%x\n",
755 journal_size
, phys_blksz
);
759 MALLOC_ZONE(jnl
, struct journal
*, sizeof(struct journal
), M_JNL_JNL
, M_WAITOK
);
760 memset(jnl
, 0, sizeof(*jnl
));
763 jnl
->jdev_offset
= offset
;
766 jnl
->flush_arg
= arg
;
767 jnl
->flags
= (flags
& JOURNAL_OPTION_FLAGS_MASK
);
768 simple_lock_init(&jnl
->old_start_lock
);
770 if (kmem_alloc(kernel_map
, (vm_offset_t
*)&jnl
->header_buf
, phys_blksz
)) {
771 printf("jnl: create: could not allocate space for header buffer (%d bytes)\n", phys_blksz
);
775 memset(jnl
->header_buf
, 0, phys_blksz
);
777 jnl
->jhdr
= (journal_header
*)jnl
->header_buf
;
778 jnl
->jhdr
->magic
= JOURNAL_HEADER_MAGIC
;
779 jnl
->jhdr
->endian
= ENDIAN_MAGIC
;
780 jnl
->jhdr
->start
= phys_blksz
; // start at block #1, block #0 is for the jhdr itself
781 jnl
->jhdr
->end
= phys_blksz
;
782 jnl
->jhdr
->size
= journal_size
;
783 jnl
->jhdr
->jhdr_size
= phys_blksz
;
784 size_up_tbuffer(jnl
, tbuffer_size
, phys_blksz
);
786 jnl
->active_start
= jnl
->jhdr
->start
;
788 // XXXdbg - for testing you can force the journal to wrap around
789 // jnl->jhdr->start = jnl->jhdr->size - (phys_blksz*3);
790 // jnl->jhdr->end = jnl->jhdr->size - (phys_blksz*3);
792 if (semaphore_create(kernel_task
, &jnl
->jsem
, SYNC_POLICY_FIFO
, 1) != 0) {
793 printf("jnl: journal_create: failed to create journal semaphore..\n");
797 if (write_journal_header(jnl
) != 0) {
798 printf("jnl: journal_create: failed to write journal header.\n");
806 semaphore_destroy(kernel_task
, jnl
->jsem
);
808 kmem_free(kernel_map
, (vm_offset_t
)jnl
->header_buf
, phys_blksz
);
811 FREE_ZONE(jnl
, sizeof(struct journal
), M_JNL_JNL
);
817 journal_open(struct vnode
*jvp
,
823 int32_t tbuffer_size
,
824 void (*flush
)(void *arg
),
828 int orig_blksz
=0, phys_blksz
, blhdr_size
;
831 /* Get the real physical block size. */
832 if (VOP_IOCTL(jvp
, DKIOCGETBLOCKSIZE
, (caddr_t
)&phys_blksz
, 0, FSCRED
, NULL
)) {
836 if (phys_blksz
> min_fs_blksz
) {
837 printf("jnl: create: error: phys blksize %d bigger than min fs blksize %d\n",
838 phys_blksz
, min_fs_blksz
);
842 if ((journal_size
% phys_blksz
) != 0) {
843 printf("jnl: open: journal size 0x%llx is not an even multiple of block size 0x%x\n",
844 journal_size
, phys_blksz
);
848 MALLOC_ZONE(jnl
, struct journal
*, sizeof(struct journal
), M_JNL_JNL
, M_WAITOK
);
849 memset(jnl
, 0, sizeof(*jnl
));
852 jnl
->jdev_offset
= offset
;
855 jnl
->flush_arg
= arg
;
856 jnl
->flags
= (flags
& JOURNAL_OPTION_FLAGS_MASK
);
857 simple_lock_init(&jnl
->old_start_lock
);
859 if (kmem_alloc(kernel_map
, (vm_offset_t
*)&jnl
->header_buf
, phys_blksz
)) {
860 printf("jnl: create: could not allocate space for header buffer (%d bytes)\n", phys_blksz
);
864 jnl
->jhdr
= (journal_header
*)jnl
->header_buf
;
865 memset(jnl
->jhdr
, 0, sizeof(journal_header
)+4);
867 // we have to set this up here so that do_journal_io() will work
868 jnl
->jhdr
->jhdr_size
= phys_blksz
;
870 if (read_journal_data(jnl
, &hdr_offset
, jnl
->jhdr
, phys_blksz
) != phys_blksz
) {
871 printf("jnl: open: could not read %d bytes for the journal header.\n",
876 if (jnl
->jhdr
->magic
!= JOURNAL_HEADER_MAGIC
&& jnl
->jhdr
->magic
!= OLD_JOURNAL_HEADER_MAGIC
) {
877 printf("jnl: open: journal magic is bad (0x%x != 0x%x)\n",
878 jnl
->jhdr
->magic
, JOURNAL_HEADER_MAGIC
);
882 // only check if we're the current journal header magic value
883 if (jnl
->jhdr
->magic
== JOURNAL_HEADER_MAGIC
) {
884 int orig_checksum
= jnl
->jhdr
->checksum
;
886 jnl
->jhdr
->checksum
= 0;
887 if (orig_checksum
!= calc_checksum((char *)jnl
->jhdr
, sizeof(struct journal_header
))) {
888 printf("jnl: open: journal checksum is bad (0x%x != 0x%x)\n", orig_checksum
,
889 calc_checksum((char *)jnl
->jhdr
, sizeof(struct journal_header
)));
894 // XXXdbg - convert old style magic numbers to the new one
895 if (jnl
->jhdr
->magic
== OLD_JOURNAL_HEADER_MAGIC
) {
896 jnl
->jhdr
->magic
= JOURNAL_HEADER_MAGIC
;
899 if (phys_blksz
!= jnl
->jhdr
->jhdr_size
&& jnl
->jhdr
->jhdr_size
!= 0) {
900 printf("jnl: open: phys_blksz %d does not match journal header size %d\n",
901 phys_blksz
, jnl
->jhdr
->jhdr_size
);
903 orig_blksz
= phys_blksz
;
904 phys_blksz
= jnl
->jhdr
->jhdr_size
;
905 if (VOP_IOCTL(jvp
, DKIOCSETBLOCKSIZE
, (caddr_t
)&phys_blksz
, FWRITE
, FSCRED
, NULL
)) {
906 printf("jnl: could not set block size to %d bytes.\n", phys_blksz
);
912 if ( jnl
->jhdr
->start
<= 0
913 || jnl
->jhdr
->start
> jnl
->jhdr
->size
914 || jnl
->jhdr
->start
> 128*1024*1024) {
915 printf("jnl: open: jhdr start looks bad (0x%llx max size 0x%llx)\n",
916 jnl
->jhdr
->start
, jnl
->jhdr
->size
);
920 if ( jnl
->jhdr
->end
<= 0
921 || jnl
->jhdr
->end
> jnl
->jhdr
->size
922 || jnl
->jhdr
->end
> 128*1024*1024) {
923 printf("jnl: open: jhdr end looks bad (0x%llx max size 0x%llx)\n",
924 jnl
->jhdr
->end
, jnl
->jhdr
->size
);
928 if (jnl
->jhdr
->size
> 128*1024*1024) {
929 printf("jnl: open: jhdr size looks bad (0x%llx)\n", jnl
->jhdr
->size
);
933 // XXXdbg - can't do these checks because hfs writes all kinds of
934 // non-uniform sized blocks even on devices that have a block size
935 // that is larger than 512 bytes (i.e. optical media w/2k blocks).
936 // therefore these checks will fail and so we just have to punt and
937 // do more relaxed checking...
938 // XXXdbg if ((jnl->jhdr->start % jnl->jhdr->jhdr_size) != 0) {
939 if ((jnl
->jhdr
->start
% 512) != 0) {
940 printf("jnl: open: journal start (0x%llx) not a multiple of 512?\n",
945 //XXXdbg if ((jnl->jhdr->end % jnl->jhdr->jhdr_size) != 0) {
946 if ((jnl
->jhdr
->end
% 512) != 0) {
947 printf("jnl: open: journal end (0x%llx) not a multiple of block size (0x%x)?\n",
948 jnl
->jhdr
->end
, jnl
->jhdr
->jhdr_size
);
952 // take care of replaying the journal if necessary
953 if (flags
& JOURNAL_RESET
) {
954 printf("jnl: journal start/end pointers reset! (jnl 0x%x; s 0x%llx e 0x%llx)\n",
955 jnl
, jnl
->jhdr
->start
, jnl
->jhdr
->end
);
956 jnl
->jhdr
->start
= jnl
->jhdr
->end
;
957 } else if (replay_journal(jnl
) != 0) {
958 printf("jnl: journal_open: Error replaying the journal!\n");
962 if (orig_blksz
!= 0) {
963 VOP_IOCTL(jvp
, DKIOCSETBLOCKSIZE
, (caddr_t
)&orig_blksz
, FWRITE
, FSCRED
, NULL
);
964 phys_blksz
= orig_blksz
;
967 // make sure this is in sync!
968 jnl
->active_start
= jnl
->jhdr
->start
;
970 // set this now, after we've replayed the journal
971 size_up_tbuffer(jnl
, tbuffer_size
, phys_blksz
);
973 if (semaphore_create(kernel_task
, &jnl
->jsem
, SYNC_POLICY_FIFO
, 1) != 0) {
974 printf("jnl: journal_create: failed to create journal semaphore..\n");
981 if (orig_blksz
!= 0) {
982 phys_blksz
= orig_blksz
;
983 VOP_IOCTL(jvp
, DKIOCSETBLOCKSIZE
, (caddr_t
)&orig_blksz
, FWRITE
, FSCRED
, NULL
);
985 kmem_free(kernel_map
, (vm_offset_t
)jnl
->header_buf
, phys_blksz
);
987 FREE_ZONE(jnl
, sizeof(struct journal
), M_JNL_JNL
);
992 journal_close(journal
*jnl
)
994 volatile off_t
*start
, *end
;
999 // set this before doing anything that would block so that
1000 // we start tearing things down properly.
1002 jnl
->flags
|= JOURNAL_CLOSE_PENDING
;
1004 if (jnl
->owner
!= current_act()) {
1007 while ((ret
= semaphore_wait(jnl
->jsem
)) == KERN_ABORTED
) {
1008 // just keep trying if we've been ^C'ed
1011 printf("jnl: close: sem wait failed.\n");
1017 // only write stuff to disk if the journal is still valid
1019 if ((jnl
->flags
& JOURNAL_INVALID
) == 0) {
1021 if (jnl
->active_tr
) {
1022 journal_end_transaction(jnl
);
1025 // flush any buffered transactions
1027 transaction
*tr
= jnl
->cur_tr
;
1030 end_transaction(tr
, 1); // force it to get flushed
1033 //start = &jnl->jhdr->start;
1034 start
= &jnl
->active_start
;
1035 end
= &jnl
->jhdr
->end
;
1037 while (*start
!= *end
&& counter
++ < 500) {
1038 printf("jnl: close: flushing the buffer cache (start 0x%llx end 0x%llx)\n", *start
, *end
);
1040 jnl
->flush(jnl
->flush_arg
);
1042 tsleep((caddr_t
)jnl
, PRIBIO
, "jnl_close", 1);
1045 if (*start
!= *end
) {
1046 printf("jnl: close: buffer flushing didn't seem to flush out all the transactions! (0x%llx - 0x%llx)\n",
1050 // make sure this is in sync when we close the journal
1051 jnl
->jhdr
->start
= jnl
->active_start
;
1053 // if this fails there's not much we can do at this point...
1054 write_journal_header(jnl
);
1056 // if we're here the journal isn't valid any more.
1057 // so make sure we don't leave any locked blocks lying around
1058 printf("jnl: close: journal 0x%x, is invalid. aborting outstanding transactions\n", jnl
);
1059 if (jnl
->active_tr
|| jnl
->cur_tr
) {
1061 if (jnl
->active_tr
) {
1062 tr
= jnl
->active_tr
;
1063 jnl
->active_tr
= NULL
;
1069 abort_transaction(jnl
, tr
);
1070 if (jnl
->active_tr
|| jnl
->cur_tr
) {
1071 panic("jnl: close: jnl @ 0x%x had both an active and cur tr\n", jnl
);
1076 free_old_stuff(jnl
);
1078 kmem_free(kernel_map
, (vm_offset_t
)jnl
->header_buf
, jnl
->jhdr
->jhdr_size
);
1079 jnl
->jhdr
= (void *)0xbeefbabe;
1081 semaphore_destroy(kernel_task
, jnl
->jsem
);
1082 FREE_ZONE(jnl
, sizeof(struct journal
), M_JNL_JNL
);
1086 dump_journal(journal
*jnl
)
1091 printf(" jdev_offset %.8llx\n", jnl
->jdev_offset
);
1092 printf(" magic: 0x%.8x\n", jnl
->jhdr
->magic
);
1093 printf(" start: 0x%.8llx\n", jnl
->jhdr
->start
);
1094 printf(" end: 0x%.8llx\n", jnl
->jhdr
->end
);
1095 printf(" size: 0x%.8llx\n", jnl
->jhdr
->size
);
1096 printf(" blhdr size: %d\n", jnl
->jhdr
->blhdr_size
);
1097 printf(" jhdr size: %d\n", jnl
->jhdr
->jhdr_size
);
1098 printf(" chksum: 0x%.8x\n", jnl
->jhdr
->checksum
);
1100 printf(" completed transactions:\n");
1101 for(ctr
=jnl
->completed_trs
; ctr
; ctr
=ctr
->next
) {
1102 printf(" 0x%.8llx - 0x%.8llx\n", ctr
->journal_start
, ctr
->journal_end
);
1109 free_space(journal
*jnl
)
1113 if (jnl
->jhdr
->start
< jnl
->jhdr
->end
) {
1114 free_space
= jnl
->jhdr
->size
- (jnl
->jhdr
->end
- jnl
->jhdr
->start
) - jnl
->jhdr
->jhdr_size
;
1115 } else if (jnl
->jhdr
->start
> jnl
->jhdr
->end
) {
1116 free_space
= jnl
->jhdr
->start
- jnl
->jhdr
->end
;
1118 // journal is completely empty
1119 free_space
= jnl
->jhdr
->size
- jnl
->jhdr
->jhdr_size
;
1127 // The journal must be locked on entry to this function.
1128 // The "desired_size" is in bytes.
1131 check_free_space(journal
*jnl
, int desired_size
)
1135 //printf("jnl: check free space (desired 0x%x, avail 0x%Lx)\n",
1136 // desired_size, free_space(jnl));
1139 if (counter
++ == 5000) {
1141 panic("jnl: check_free_space: buffer flushing isn't working "
1142 "(jnl @ 0x%x s %lld e %lld f %lld [active start %lld]).\n", jnl
,
1143 jnl
->jhdr
->start
, jnl
->jhdr
->end
, free_space(jnl
), jnl
->active_start
);
1145 if (counter
> 7500) {
1146 printf("jnl: check_free_space: giving up waiting for free space.\n");
1150 // make sure there's space in the journal to hold this transaction
1151 if (free_space(jnl
) > desired_size
) {
1156 // here's where we lazily bump up jnl->jhdr->start. we'll consume
1157 // entries until there is enough space for the next transaction.
1159 simple_lock(&jnl
->old_start_lock
);
1160 for(i
=0; i
< sizeof(jnl
->old_start
)/sizeof(jnl
->old_start
[0]); i
++) {
1164 while (jnl
->old_start
[i
] & 0x8000000000000000LL
) {
1165 if (counter
++ > 100) {
1166 panic("jnl: check_free_space: tr starting @ 0x%llx not flushing (jnl 0x%x).\n",
1167 jnl
->old_start
[i
], jnl
);
1170 simple_unlock(&jnl
->old_start_lock
);
1172 jnl
->flush(jnl
->flush_arg
);
1174 tsleep((caddr_t
)jnl
, PRIBIO
, "check_free_space1", 1);
1175 simple_lock(&jnl
->old_start_lock
);
1178 if (jnl
->old_start
[i
] == 0) {
1182 jnl
->jhdr
->start
= jnl
->old_start
[i
];
1183 jnl
->old_start
[i
] = 0;
1184 if (free_space(jnl
) > desired_size
) {
1185 write_journal_header(jnl
);
1189 simple_unlock(&jnl
->old_start_lock
);
1191 // if we bumped the start, loop and try again
1192 if (i
< sizeof(jnl
->old_start
)/sizeof(jnl
->old_start
[0])) {
1197 // if the file system gave us a flush function, call it to so that
1198 // it can flush some blocks which hopefully will cause some transactions
1199 // to complete and thus free up space in the journal.
1201 jnl
->flush(jnl
->flush_arg
);
1204 // wait for a while to avoid being cpu-bound (this will
1205 // put us to sleep for 10 milliseconds)
1206 tsleep((caddr_t
)jnl
, PRIBIO
, "check_free_space2", 1);
1213 journal_start_transaction(journal
*jnl
)
1220 if (jnl
->flags
& JOURNAL_INVALID
) {
1224 if (jnl
->owner
== current_act()) {
1225 if (jnl
->active_tr
== NULL
) {
1226 panic("jnl: start_tr: active_tr is NULL (jnl @ 0x%x, owner 0x%x, current_act 0x%x\n",
1227 jnl
, jnl
->owner
, current_act());
1229 jnl
->nested_count
++;
1233 while ((ret
= semaphore_wait(jnl
->jsem
)) == KERN_ABORTED
) {
1234 // just keep looping if we've been ^C'ed
1237 printf("jnl: start_tr: sem wait failed.\n");
1241 if (jnl
->owner
!= NULL
|| jnl
->nested_count
!= 0 || jnl
->active_tr
!= NULL
) {
1242 panic("jnl: start_tr: owner 0x%x, nested count 0x%x, active_tr 0x%x jnl @ 0x%x\n",
1243 jnl
->owner
, jnl
->nested_count
, jnl
->active_tr
, jnl
);
1246 jnl
->owner
= current_act();
1247 jnl
->nested_count
= 1;
1249 free_old_stuff(jnl
);
1251 // make sure there's room in the journal
1252 if (check_free_space(jnl
, jnl
->tbuffer_size
) != 0) {
1253 printf("jnl: start transaction failed: no space\n");
1258 // if there's a buffered transaction, use it.
1260 jnl
->active_tr
= jnl
->cur_tr
;
1266 MALLOC_ZONE(tr
, transaction
*, sizeof(transaction
), M_JNL_TR
, M_WAITOK
);
1267 memset(tr
, 0, sizeof(transaction
));
1269 tr
->tbuffer_size
= jnl
->tbuffer_size
;
1270 if (kmem_alloc(kernel_map
, (vm_offset_t
*)&tr
->tbuffer
, tr
->tbuffer_size
)) {
1271 FREE_ZONE(tr
, sizeof(transaction
), M_JNL_TR
);
1272 printf("jnl: start transaction failed: no tbuffer mem\n");
1277 // journal replay code checksum check depends on this.
1278 memset(tr
->tbuffer
, 0, BLHDR_CHECKSUM_SIZE
);
1280 tr
->blhdr
= (block_list_header
*)tr
->tbuffer
;
1281 tr
->blhdr
->max_blocks
= (jnl
->jhdr
->blhdr_size
/ sizeof(block_info
)) - 1;
1282 tr
->blhdr
->num_blocks
= 1; // accounts for this header block
1283 tr
->blhdr
->bytes_used
= jnl
->jhdr
->blhdr_size
;
1286 tr
->total_bytes
= jnl
->jhdr
->blhdr_size
;
1289 jnl
->active_tr
= tr
;
1291 // printf("jnl: start_tr: owner 0x%x new tr @ 0x%x\n", jnl->owner, tr);
1297 jnl
->nested_count
= 0;
1298 semaphore_signal(jnl
->jsem
);
1304 journal_modify_block_start(journal
*jnl
, struct buf
*bp
)
1310 if (jnl
->flags
& JOURNAL_INVALID
) {
1314 // XXXdbg - for debugging I want this to be true. later it may
1315 // not be necessary.
1316 if ((bp
->b_flags
& B_META
) == 0) {
1317 panic("jnl: modify_block_start: bp @ 0x%x is not a meta-data block! (jnl 0x%x)\n", bp
, jnl
);
1320 tr
= jnl
->active_tr
;
1321 CHECK_TRANSACTION(tr
);
1323 if (jnl
->owner
!= current_act()) {
1324 panic("jnl: modify_block_start: called w/out a transaction! jnl 0x%x, owner 0x%x, curact 0x%x\n",
1325 jnl
, jnl
->owner
, current_act());
1328 free_old_stuff(jnl
);
1330 //printf("jnl: mod block start (bp 0x%x vp 0x%x l/blkno %d/%d bsz %d; total bytes %d)\n",
1331 // bp, bp->b_vp, bp->b_lblkno, bp->b_blkno, bp->b_bufsize, tr->total_bytes);
1333 // can't allow blocks that aren't an even multiple of the
1334 // underlying block size.
1335 if ((bp
->b_bufsize
% jnl
->jhdr
->jhdr_size
) != 0) {
1336 panic("jnl: mod block start: bufsize %d not a multiple of block size %d\n",
1337 bp
->b_bufsize
, jnl
->jhdr
->jhdr_size
);
1341 // make sure that this transaction isn't bigger than the whole journal
1342 if (tr
->total_bytes
+bp
->b_bufsize
>= (jnl
->jhdr
->size
- jnl
->jhdr
->jhdr_size
)) {
1343 panic("jnl: transaction too big (%d >= %lld bytes, bufsize %d, tr 0x%x bp 0x%x)\n",
1344 tr
->total_bytes
, (tr
->jnl
->jhdr
->size
- jnl
->jhdr
->jhdr_size
), bp
->b_bufsize
, tr
, bp
);
1348 // if the block is dirty and not already locked we have to write
1349 // it out before we muck with it because it has data that belongs
1350 // (presumably) to another transaction.
1352 if ((bp
->b_flags
& B_DELWRI
) && (bp
->b_flags
& B_LOCKED
) == 0) {
1354 // this will cause it to not be brelse()'d
1355 bp
->b_flags
|= B_NORELSE
;
1359 bp
->b_flags
|= B_LOCKED
;
1365 journal_modify_block_abort(journal
*jnl
, struct buf
*bp
)
1368 block_list_header
*blhdr
;
1373 tr
= jnl
->active_tr
;
1376 // if there's no active transaction then we just want to
1377 // call brelse() and return since this is just a block
1378 // that happened to be modified as part of another tr.
1385 if (jnl
->flags
& JOURNAL_INVALID
) {
1389 CHECK_TRANSACTION(tr
);
1391 if (jnl
->owner
!= current_act()) {
1392 panic("jnl: modify_block_abort: called w/out a transaction! jnl 0x%x, owner 0x%x, curact 0x%x\n",
1393 jnl
, jnl
->owner
, current_act());
1396 free_old_stuff(jnl
);
1398 // printf("jnl: modify_block_abort: tr 0x%x bp 0x%x\n", jnl->active_tr, bp);
1400 // first check if it's already part of this transaction
1401 for(blhdr
=tr
->blhdr
; blhdr
; blhdr
=(block_list_header
*)((long)blhdr
->binfo
[0].bnum
)) {
1402 for(i
=1; i
< blhdr
->num_blocks
; i
++) {
1403 if (bp
== blhdr
->binfo
[i
].bp
) {
1404 if (bp
->b_bufsize
!= blhdr
->binfo
[i
].bsize
) {
1405 panic("jnl: bp @ 0x%x changed size on me! (%d vs. %d, jnl 0x%x)\n",
1406 bp
, bp
->b_bufsize
, blhdr
->binfo
[i
].bsize
, jnl
);
1412 if (i
< blhdr
->num_blocks
) {
1418 // if blhdr is null, then this block has only had modify_block_start
1419 // called on it as part of the current transaction. that means that
1420 // it is ok to clear the LOCKED bit since it hasn't actually been
1421 // modified. if blhdr is non-null then modify_block_end was called
1422 // on it and so we need to keep it locked in memory.
1424 if (blhdr
== NULL
) {
1425 bp
->b_flags
&= ~(B_LOCKED
);
1434 journal_modify_block_end(journal
*jnl
, struct buf
*bp
)
1436 int i
, j
, tbuffer_offset
;
1438 block_list_header
*blhdr
, *prev
=NULL
;
1443 if (jnl
->flags
& JOURNAL_INVALID
) {
1447 tr
= jnl
->active_tr
;
1448 CHECK_TRANSACTION(tr
);
1450 if (jnl
->owner
!= current_act()) {
1451 panic("jnl: modify_block_end: called w/out a transaction! jnl 0x%x, owner 0x%x, curact 0x%x\n",
1452 jnl
, jnl
->owner
, current_act());
1455 free_old_stuff(jnl
);
1457 //printf("jnl: mod block end: (bp 0x%x vp 0x%x l/blkno %d/%d bsz %d, total bytes %d)\n",
1458 // bp, bp->b_vp, bp->b_lblkno, bp->b_blkno, bp->b_bufsize, tr->total_bytes);
1460 if ((bp
->b_flags
& B_LOCKED
) == 0) {
1461 panic("jnl: modify_block_end: bp 0x%x not locked! jnl @ 0x%x\n", bp
, jnl
);
1462 bp
->b_flags
|= B_LOCKED
;
1465 // first check if it's already part of this transaction
1466 for(blhdr
=tr
->blhdr
; blhdr
; prev
=blhdr
,blhdr
=(block_list_header
*)((long)blhdr
->binfo
[0].bnum
)) {
1467 tbuffer_offset
= jnl
->jhdr
->blhdr_size
;
1469 for(i
=1; i
< blhdr
->num_blocks
; i
++) {
1470 if (bp
== blhdr
->binfo
[i
].bp
) {
1471 if (bp
->b_bufsize
!= blhdr
->binfo
[i
].bsize
) {
1472 panic("jnl: bp @ 0x%x changed size on me! (%d vs. %d, jnl 0x%x)\n",
1473 bp
, bp
->b_bufsize
, blhdr
->binfo
[i
].bsize
, jnl
);
1477 tbuffer_offset
+= blhdr
->binfo
[i
].bsize
;
1480 if (i
< blhdr
->num_blocks
) {
1487 && (prev
->num_blocks
+1) <= prev
->max_blocks
1488 && (prev
->bytes_used
+bp
->b_bufsize
) <= tr
->tbuffer_size
) {
1490 } else if (blhdr
== NULL
) {
1491 block_list_header
*nblhdr
;
1494 panic("jnl: modify block end: no way man, prev == NULL?!?, jnl 0x%x, bp 0x%x\n", jnl
, bp
);
1497 // we got to the end of the list, didn't find the block and there's
1498 // no room in the block_list_header pointed to by prev
1500 // we allocate another tbuffer and link it in at the end of the list
1501 // through prev->binfo[0].bnum. that's a skanky way to do things but
1502 // avoids having yet another linked list of small data structures to manage.
1504 if (kmem_alloc(kernel_map
, (vm_offset_t
*)&nblhdr
, tr
->tbuffer_size
)) {
1505 panic("jnl: end_tr: no space for new block tr @ 0x%x (total bytes: %d)!\n",
1506 tr
, tr
->total_bytes
);
1509 // journal replay code checksum check depends on this.
1510 memset(nblhdr
, 0, BLHDR_CHECKSUM_SIZE
);
1512 // initialize the new guy
1513 nblhdr
->max_blocks
= (jnl
->jhdr
->blhdr_size
/ sizeof(block_info
)) - 1;
1514 nblhdr
->num_blocks
= 1; // accounts for this header block
1515 nblhdr
->bytes_used
= jnl
->jhdr
->blhdr_size
;
1518 tr
->total_bytes
+= jnl
->jhdr
->blhdr_size
;
1520 // then link him in at the end
1521 prev
->binfo
[0].bnum
= (off_t
)((long)nblhdr
);
1523 // and finally switch to using the new guy
1525 tbuffer_offset
= jnl
->jhdr
->blhdr_size
;
1530 if ((i
+1) > blhdr
->max_blocks
) {
1531 panic("jnl: modify_block_end: i = %d, max_blocks %d\n", i
, blhdr
->max_blocks
);
1534 // copy the data into the in-memory transaction buffer
1535 blkptr
= (char *)&((char *)blhdr
)[tbuffer_offset
];
1536 memcpy(blkptr
, bp
->b_data
, bp
->b_bufsize
);
1538 // if this is true then this is a new block we haven't seen
1539 if (i
>= blhdr
->num_blocks
) {
1540 vget(bp
->b_vp
, 0, current_proc());
1542 blhdr
->binfo
[i
].bnum
= bp
->b_blkno
;
1543 blhdr
->binfo
[i
].bsize
= bp
->b_bufsize
;
1544 blhdr
->binfo
[i
].bp
= bp
;
1546 blhdr
->bytes_used
+= bp
->b_bufsize
;
1547 tr
->total_bytes
+= bp
->b_bufsize
;
1549 blhdr
->num_blocks
++;
1558 journal_kill_block(journal
*jnl
, struct buf
*bp
)
1561 block_list_header
*blhdr
;
1566 if (jnl
->flags
& JOURNAL_INVALID
) {
1570 tr
= jnl
->active_tr
;
1571 CHECK_TRANSACTION(tr
);
1573 if (jnl
->owner
!= current_act()) {
1574 panic("jnl: modify_block_end: called w/out a transaction! jnl 0x%x, owner 0x%x, curact 0x%x\n",
1575 jnl
, jnl
->owner
, current_act());
1578 free_old_stuff(jnl
);
1580 if ((bp
->b_flags
& B_LOCKED
) == 0) {
1581 panic("jnl: kill block: bp 0x%x not locked! jnl @ 0x%x\n", bp
, jnl
);
1584 // first check if it's already part of this transaction
1585 for(blhdr
=tr
->blhdr
; blhdr
; blhdr
=(block_list_header
*)((long)blhdr
->binfo
[0].bnum
)) {
1587 for(i
=1; i
< blhdr
->num_blocks
; i
++) {
1588 if (bp
== blhdr
->binfo
[i
].bp
) {
1589 bp
->b_flags
&= ~B_LOCKED
;
1591 // this undoes the vget() in journal_modify_block_end()
1594 // if the block has the DELWRI and CALL bits sets, then
1595 // things are seriously weird. if it was part of another
1596 // transaction then journal_modify_block_start() should
1597 // have force it to be written.
1599 if ((bp
->b_flags
& B_DELWRI
) && (bp
->b_flags
& B_CALL
)) {
1600 panic("jnl: kill block: this defies all logic! bp 0x%x\n", bp
);
1602 tr
->num_killed
+= bp
->b_bufsize
;
1605 if (bp
->b_flags
& B_BUSY
) {
1609 blhdr
->binfo
[i
].bp
= NULL
;
1610 blhdr
->binfo
[i
].bnum
= (off_t
)-1;
1615 if (i
< blhdr
->num_blocks
) {
1625 journal_binfo_cmp(void *a
, void *b
)
1627 block_info
*bi_a
= (struct block_info
*)a
,
1628 *bi_b
= (struct block_info
*)b
;
1631 if (bi_a
->bp
== NULL
) {
1634 if (bi_b
->bp
== NULL
) {
1638 // don't have to worry about negative block
1639 // numbers so this is ok to do.
1641 res
= (bi_a
->bp
->b_blkno
- bi_b
->bp
->b_blkno
);
1648 end_transaction(transaction
*tr
, int force_it
)
1652 journal
*jnl
= tr
->jnl
;
1654 block_list_header
*blhdr
=NULL
, *next
=NULL
;
1657 panic("jnl: jnl @ 0x%x already has cur_tr 0x%x, new tr: 0x%x\n",
1658 jnl
, jnl
->cur_tr
, tr
);
1661 // if there weren't any modified blocks in the transaction
1662 // just save off the transaction pointer and return.
1663 if (tr
->total_bytes
== jnl
->jhdr
->blhdr_size
) {
1668 // if our transaction buffer isn't very full, just hang
1669 // on to it and don't actually flush anything. this is
1670 // what is known as "group commit". we will flush the
1671 // transaction buffer if it's full or if we have more than
1672 // one of them so we don't start hogging too much memory.
1675 && (jnl
->flags
& JOURNAL_NO_GROUP_COMMIT
) == 0
1676 && tr
->num_blhdrs
< 3
1677 && (tr
->total_bytes
<= ((tr
->tbuffer_size
*tr
->num_blhdrs
) - tr
->tbuffer_size
/8))) {
1684 // if we're here we're going to flush the transaction buffer to disk.
1685 // make sure there is room in the journal first.
1686 check_free_space(jnl
, tr
->total_bytes
);
1688 // range check the end index
1689 if (jnl
->jhdr
->end
<= 0 || jnl
->jhdr
->end
> jnl
->jhdr
->size
) {
1690 panic("jnl: end_transaction: end is bogus 0x%llx (sz 0x%llx)\n",
1691 jnl
->jhdr
->end
, jnl
->jhdr
->size
);
1694 // this transaction starts where the current journal ends
1695 tr
->journal_start
= jnl
->jhdr
->end
;
1696 end
= jnl
->jhdr
->end
;
1699 // if the first entry in old_start[] isn't free yet, loop calling the
1700 // file system flush routine until it is (or we panic).
1703 simple_lock(&jnl
->old_start_lock
);
1704 while ((jnl
->old_start
[0] & 0x8000000000000000LL
) != 0) {
1706 simple_unlock(&jnl
->old_start_lock
);
1709 jnl
->flush(jnl
->flush_arg
);
1712 // yield the cpu so others can get in to clear the lock bit
1713 (void)tsleep((void *)jnl
, PRIBIO
, "jnl-old-start-sleep", 1);
1715 simple_lock(&jnl
->old_start_lock
);
1718 panic("jnl: transaction that started at 0x%llx is not completing! jnl 0x%x\n",
1719 jnl
->old_start
[0] & (~0x8000000000000000LL
), jnl
);
1724 // slide everyone else down and put our latest guy in the last
1725 // entry in the old_start array
1727 memcpy(&jnl
->old_start
[0], &jnl
->old_start
[1], sizeof(jnl
->old_start
)-sizeof(jnl
->old_start
[0]));
1728 jnl
->old_start
[sizeof(jnl
->old_start
)/sizeof(jnl
->old_start
[0]) - 1] = tr
->journal_start
| 0x8000000000000000LL
;
1730 simple_unlock(&jnl
->old_start_lock
);
1733 // for each block, make sure that the physical block # is set
1734 for(blhdr
=tr
->blhdr
; blhdr
; blhdr
=next
) {
1736 for(i
=1; i
< blhdr
->num_blocks
; i
++) {
1738 bp
= blhdr
->binfo
[i
].bp
;
1739 if (bp
== NULL
) { // only true if a block was "killed"
1740 if (blhdr
->binfo
[i
].bnum
!= (off_t
)-1) {
1741 panic("jnl: inconsistent binfo (NULL bp w/bnum %lld; jnl @ 0x%x, tr 0x%x)\n",
1742 blhdr
->binfo
[i
].bnum
, jnl
, tr
);
1747 if (bp
->b_vp
== NULL
&& bp
->b_lblkno
== bp
->b_blkno
) {
1748 panic("jnl: end_tr: DANGER! bp @ 0x%x w/null vp and l/blkno = %d/%d\n",
1749 bp
, bp
->b_lblkno
, bp
->b_blkno
);
1752 // if the lblkno is the same as blkno and this bp isn't
1753 // associated with the underlying file system device then
1754 // we need to call bmap() to get the actual physical block.
1756 if ((bp
->b_lblkno
== bp
->b_blkno
) && (bp
->b_vp
!= jnl
->fsdev
)) {
1757 if (VOP_BMAP(bp
->b_vp
, bp
->b_lblkno
, NULL
, &bp
->b_blkno
, NULL
) != 0) {
1758 printf("jnl: end_tr: can't bmap the bp @ 0x%x, jnl 0x%x\n", bp
, jnl
);
1763 // update this so we write out the correct physical block number!
1764 blhdr
->binfo
[i
].bnum
= bp
->b_blkno
;
1767 next
= (block_list_header
*)((long)blhdr
->binfo
[0].bnum
);
1770 for(blhdr
=tr
->blhdr
; blhdr
; blhdr
=(block_list_header
*)((long)blhdr
->binfo
[0].bnum
)) {
1772 amt
= blhdr
->bytes_used
;
1774 blhdr
->checksum
= 0;
1775 blhdr
->checksum
= calc_checksum((char *)blhdr
, BLHDR_CHECKSUM_SIZE
);
1777 ret
= write_journal_data(jnl
, &end
, blhdr
, amt
);
1779 printf("jnl: end_transaction: only wrote %d of %d bytes to the journal!\n",
1786 jnl
->jhdr
->end
= end
; // update where the journal now ends
1787 tr
->journal_end
= end
; // the transaction ends here too
1788 if (tr
->journal_start
== 0 || tr
->journal_end
== 0) {
1789 panic("jnl: end_transaction: bad tr journal start/end: 0x%llx 0x%llx\n",
1790 tr
->journal_start
, tr
->journal_end
);
1793 if (write_journal_header(jnl
) != 0) {
1798 // setup for looping through all the blhdr's. we null out the
1799 // tbuffer and blhdr fields so that they're not used any more.
1805 // the buffer_flushed_callback will only be called for the
1806 // real blocks that get flushed so we have to account for
1807 // the block_list_headers here.
1809 tr
->num_flushed
= tr
->num_blhdrs
* jnl
->jhdr
->blhdr_size
;
1811 // for each block, set the iodone callback and unlock it
1812 for(; blhdr
; blhdr
=next
) {
1814 // we can re-order the buf ptrs because everything is written out already
1815 qsort(&blhdr
->binfo
[1], blhdr
->num_blocks
-1, sizeof(block_info
), journal_binfo_cmp
);
1817 for(i
=1; i
< blhdr
->num_blocks
; i
++) {
1818 if (blhdr
->binfo
[i
].bp
== NULL
) {
1822 ret
= meta_bread(blhdr
->binfo
[i
].bp
->b_vp
,
1823 (daddr_t
)blhdr
->binfo
[i
].bp
->b_lblkno
,
1824 blhdr
->binfo
[i
].bp
->b_bufsize
,
1827 if (ret
== 0 && bp
!= NULL
) {
1828 struct vnode
*save_vp
;
1830 if (bp
!= blhdr
->binfo
[i
].bp
) {
1831 panic("jnl: end_tr: got back a different bp! (bp 0x%x should be 0x%x, jnl 0x%x\n",
1832 bp
, blhdr
->binfo
[i
].bp
, jnl
);
1835 if ((bp
->b_flags
& (B_LOCKED
|B_DELWRI
)) != (B_LOCKED
|B_DELWRI
)) {
1836 if (jnl
->flags
& JOURNAL_CLOSE_PENDING
) {
1840 panic("jnl: end_tr: !!!DANGER!!! bp 0x%x flags (0x%x) not LOCKED & DELWRI\n", bp
, bp
->b_flags
);
1844 if (bp
->b_iodone
!= NULL
) {
1845 panic("jnl: bp @ 0x%x (blkno %d, vp 0x%x) has non-null iodone (0x%x) buffflushcb 0x%x\n",
1846 bp
, bp
->b_blkno
, bp
->b_vp
, bp
->b_iodone
, buffer_flushed_callback
);
1851 bp
->b_iodone
= buffer_flushed_callback
;
1852 bp
->b_transaction
= tr
;
1853 bp
->b_flags
|= B_CALL
;
1854 bp
->b_flags
&= ~(B_LOCKED
);
1856 // kicking off the write here helps performance
1858 // XXXdbg this is good for testing: bdwrite(bp);
1861 // this undoes the vget() in journal_modify_block_end()
1865 printf("jnl: end_transaction: could not find block %Ld vp 0x%x!\n",
1866 blhdr
->binfo
[i
].bnum
, blhdr
->binfo
[i
].bp
);
1873 next
= (block_list_header
*)((long)blhdr
->binfo
[0].bnum
);
1875 // we can free blhdr here since we won't need it any more
1876 blhdr
->binfo
[0].bnum
= 0xdeadc0de;
1877 kmem_free(kernel_map
, (vm_offset_t
)blhdr
, tr
->tbuffer_size
);
1880 //printf("jnl: end_tr: tr @ 0x%x, jnl-blocks: 0x%llx - 0x%llx. exit!\n",
1881 // tr, tr->journal_start, tr->journal_end);
1886 jnl
->flags
|= JOURNAL_INVALID
;
1887 abort_transaction(jnl
, tr
);
1892 abort_transaction(journal
*jnl
, transaction
*tr
)
1895 block_list_header
*blhdr
, *next
;
1898 // for each block list header, iterate over the blocks then
1899 // free up the memory associated with the block list.
1901 // for each block, clear the lock bit and release it.
1903 for(blhdr
=tr
->blhdr
; blhdr
; blhdr
=next
) {
1905 for(i
=1; i
< blhdr
->num_blocks
; i
++) {
1906 if (blhdr
->binfo
[i
].bp
== NULL
) {
1910 ret
= meta_bread(blhdr
->binfo
[i
].bp
->b_vp
,
1911 (daddr_t
)blhdr
->binfo
[i
].bp
->b_lblkno
,
1912 blhdr
->binfo
[i
].bp
->b_bufsize
,
1916 if (bp
!= blhdr
->binfo
[i
].bp
) {
1917 panic("jnl: abort_tr: got back a different bp! (bp 0x%x should be 0x%x, jnl 0x%x\n",
1918 bp
, blhdr
->binfo
[i
].bp
, jnl
);
1921 // clear the locked bit and the delayed-write bit. we
1922 // don't want these blocks going to disk.
1923 bp
->b_flags
&= ~(B_LOCKED
|B_DELWRI
);
1924 bp
->b_flags
|= B_INVAL
;
1929 printf("jnl: abort_tr: could not find block %Ld vp 0x%x!\n",
1930 blhdr
->binfo
[i
].bnum
, blhdr
->binfo
[i
].bp
);
1937 next
= (block_list_header
*)((long)blhdr
->binfo
[0].bnum
);
1939 // we can free blhdr here since we won't need it any more
1940 blhdr
->binfo
[0].bnum
= 0xdeadc0de;
1941 kmem_free(kernel_map
, (vm_offset_t
)blhdr
, tr
->tbuffer_size
);
1946 tr
->total_bytes
= 0xdbadc0de;
1947 FREE_ZONE(tr
, sizeof(transaction
), M_JNL_TR
);
1952 journal_end_transaction(journal
*jnl
)
1959 if ((jnl
->flags
& JOURNAL_INVALID
) && jnl
->owner
== NULL
) {
1963 if (jnl
->owner
!= current_act()) {
1964 panic("jnl: end_tr: I'm not the owner! jnl 0x%x, owner 0x%x, curact 0x%x\n",
1965 jnl
, jnl
->owner
, current_act());
1968 free_old_stuff(jnl
);
1970 jnl
->nested_count
--;
1971 if (jnl
->nested_count
> 0) {
1973 } else if (jnl
->nested_count
< 0) {
1974 panic("jnl: jnl @ 0x%x has negative nested count (%d). bad boy.\n", jnl
, jnl
->nested_count
);
1977 if (jnl
->flags
& JOURNAL_INVALID
) {
1978 if (jnl
->active_tr
) {
1981 if (jnl
->cur_tr
!= NULL
) {
1982 panic("jnl: journal @ 0x%x has active tr (0x%x) and cur tr (0x%x)\n",
1983 jnl
, jnl
->active_tr
, jnl
->cur_tr
);
1986 tr
= jnl
->active_tr
;
1987 jnl
->active_tr
= NULL
;
1988 abort_transaction(jnl
, tr
);
1992 semaphore_signal(jnl
->jsem
);
1997 tr
= jnl
->active_tr
;
1998 CHECK_TRANSACTION(tr
);
2000 // clear this out here so that when check_free_space() calls
2001 // the FS flush function, we don't panic in journal_flush()
2002 // if the FS were to call that. note: check_free_space() is
2003 // called from end_transaction().
2005 jnl
->active_tr
= NULL
;
2006 ret
= end_transaction(tr
, 0);
2009 semaphore_signal(jnl
->jsem
);
2016 journal_flush(journal
*jnl
)
2018 int need_signal
= 0;
2022 if (jnl
->flags
& JOURNAL_INVALID
) {
2026 if (jnl
->owner
!= current_act()) {
2029 while ((ret
= semaphore_wait(jnl
->jsem
)) == KERN_ABORTED
) {
2030 // just keep looping if we've ben ^C'ed
2033 printf("jnl: flush: sem wait failed.\n");
2039 free_old_stuff(jnl
);
2041 // if we're not active, flush any buffered transactions
2042 if (jnl
->active_tr
== NULL
&& jnl
->cur_tr
) {
2043 transaction
*tr
= jnl
->cur_tr
;
2046 end_transaction(tr
, 1); // force it to get flushed
2050 semaphore_signal(jnl
->jsem
);
2057 journal_active(journal
*jnl
)
2059 if (jnl
->flags
& JOURNAL_INVALID
) {
2063 return (jnl
->active_tr
== NULL
) ? 0 : 1;