#ifdef KERNEL
#include <sys/param.h>
#include <sys/systm.h>
#include <sys/kernel.h>
#include <sys/file.h>
#include <sys/stat.h>
#include <sys/buf.h>
#include <sys/proc.h>
#include <sys/mount.h>
#include <sys/namei.h>
#include <sys/vnode.h>
#include <sys/ioctl.h>
#include <sys/tty.h>
#include <sys/ubc.h>
#include <sys/malloc.h>
#include <sys/vnode.h>
#include <kern/thread_act.h>
#include <sys/disk.h>
#include <miscfs/specfs/specdev.h>
extern task_t kernel_task;
#else
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <limits.h>
#include <errno.h>
#include <fcntl.h>
#include <unistd.h>
#include <stdarg.h>
#include <sys/types.h>
#include "compat.h"
#endif
#include "vfs_journal.h"
#define BLHDR_CHECKSUM_SIZE 32
static int end_transaction(transaction *tr, int force_it);
static void abort_transaction(journal *jnl, transaction *tr);
static void dump_journal(journal *jnl);
typedef struct bucket {
off_t block_num;
size_t jnl_offset;
size_t block_size;
} bucket;
#define STARTING_BUCKETS 256
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);
static int grow_table(struct bucket **buf_ptr, int num_buckets, int new_size);
static int lookup_bucket(struct bucket **buf_ptr, off_t block_num, int num_full);
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);
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);
#define CHECK_JOURNAL(jnl) \
do { \
if (jnl == NULL) {\
panic("%s:%d: null journal ptr?\n", __FILE__, __LINE__);\
}\
if (jnl->jdev == NULL) { \
panic("%s:%d: jdev is null!\n", __FILE__, __LINE__);\
} \
if (jnl->fsdev == NULL) { \
panic("%s:%d: fsdev is null!\n", __FILE__, __LINE__);\
} \
if (jnl->jhdr->magic != JOURNAL_HEADER_MAGIC) {\
panic("%s:%d: jhdr magic corrupted (0x%x != 0x%x)\n",\
__FILE__, __LINE__, jnl->jhdr->magic, JOURNAL_HEADER_MAGIC);\
}\
if ( jnl->jhdr->start <= 0 \
|| jnl->jhdr->start > jnl->jhdr->size\
|| jnl->jhdr->start > 1024*1024*1024) {\
panic("%s:%d: jhdr start looks bad (0x%llx max size 0x%llx)\n", \
__FILE__, __LINE__, jnl->jhdr->start, jnl->jhdr->size);\
}\
if ( jnl->jhdr->end <= 0 \
|| jnl->jhdr->end > jnl->jhdr->size\
|| jnl->jhdr->end > 1024*1024*1024) {\
panic("%s:%d: jhdr end looks bad (0x%llx max size 0x%llx)\n", \
__FILE__, __LINE__, jnl->jhdr->end, jnl->jhdr->size);\
}\
if (jnl->jhdr->size > 1024*1024*1024) {\
panic("%s:%d: jhdr size looks bad (0x%llx)\n",\
__FILE__, __LINE__, jnl->jhdr->size);\
} \
} while(0)
#define CHECK_TRANSACTION(tr) \
do {\
if (tr == NULL) {\
panic("%s:%d: null transaction ptr?\n", __FILE__, __LINE__);\
}\
if (tr->jnl == NULL) {\
panic("%s:%d: null tr->jnl ptr?\n", __FILE__, __LINE__);\
}\
if (tr->blhdr != (block_list_header *)tr->tbuffer) {\
panic("%s:%d: blhdr (0x%x) != tbuffer (0x%x)\n", __FILE__, __LINE__, tr->blhdr, tr->tbuffer);\
}\
if (tr->total_bytes < 0) {\
panic("%s:%d: tr total_bytes looks bad: %d\n", __FILE__, __LINE__, tr->total_bytes);\
}\
if (tr->journal_start < 0 || tr->journal_start > 1024*1024*1024) {\
panic("%s:%d: tr journal start looks bad: 0x%llx\n", __FILE__, __LINE__, tr->journal_start);\
}\
if (tr->journal_end < 0 || tr->journal_end > 1024*1024*1024) {\
panic("%s:%d: tr journal end looks bad: 0x%llx\n", __FILE__, __LINE__, tr->journal_end);\
}\
if (tr->blhdr && (tr->blhdr->max_blocks <= 0 || tr->blhdr->max_blocks > (tr->jnl->jhdr->size/tr->jnl->jhdr->jhdr_size))) {\
panic("%s:%d: tr blhdr max_blocks looks bad: %d\n", __FILE__, __LINE__, tr->blhdr->max_blocks);\
}\
} while(0)
static int
calc_checksum(char *ptr, int len)
{
int i, cksum=0;
for(i=0; i < len; i++, ptr++) {
cksum = (cksum << 8) ^ (cksum + *(unsigned char *)ptr);
}
return (~cksum);
}
#define JNL_WRITE 0x0001
#define JNL_READ 0x0002
#define JNL_HEADER 0x8000
static size_t
do_journal_io(journal *jnl, off_t *offset, void *data, size_t len, int direction)
{
int err, io_sz=0, curlen=len;
struct buf *bp;
int max_iosize=0, max_vectors;
if (*offset < 0 || *offset > jnl->jhdr->size) {
panic("jnl: do_jnl_io: bad offset 0x%llx (max 0x%llx)\n", *offset, jnl->jhdr->size);
}
again:
bp = alloc_io_buf(jnl->jdev, 1);
if (direction & JNL_WRITE) {
bp->b_flags |= 0; jnl->jdev->v_numoutput++;
vfs_io_attributes(jnl->jdev, B_WRITE, &max_iosize, &max_vectors);
} else if (direction & JNL_READ) {
bp->b_flags |= B_READ;
vfs_io_attributes(jnl->jdev, B_READ, &max_iosize, &max_vectors);
}
if (max_iosize == 0) {
max_iosize = 128 * 1024;
}
if (*offset + (off_t)curlen > jnl->jhdr->size && *offset != 0 && jnl->jhdr->size != 0) {
if (*offset == jnl->jhdr->size) {
*offset = jnl->jhdr->jhdr_size;
} else {
curlen = (off_t)jnl->jhdr->size - *offset;
}
}
if (curlen > max_iosize) {
curlen = max_iosize;
}
if (curlen <= 0) {
panic("jnl: do_jnl_io: curlen == %d, offset 0x%llx len %d\n", curlen, *offset, len);
}
if (*offset == 0 && (direction & JNL_HEADER) == 0) {
panic("jnl: request for i/o to jnl-header without JNL_HEADER flag set! (len %d, data %p)\n", curlen, data);
}
bp->b_bufsize = curlen;
bp->b_bcount = curlen;
bp->b_data = data;
bp->b_blkno = (daddr_t) ((jnl->jdev_offset + *offset) / (off_t)jnl->jhdr->jhdr_size);
bp->b_lblkno = (daddr_t) ((jnl->jdev_offset + *offset) / (off_t)jnl->jhdr->jhdr_size);
err = VOP_STRATEGY(bp);
if (!err) {
err = biowait(bp);
}
bp->b_data = NULL;
bp->b_bufsize = bp->b_bcount = 0;
bp->b_blkno = bp->b_lblkno = -1;
free_io_buf(bp);
if (err) {
printf("jnl: do_jnl_io: strategy err 0x%x\n", err);
return 0;
}
*offset += curlen;
io_sz += curlen;
if (io_sz != len) {
data = (char *)data + curlen;
curlen = len - io_sz;
if (*offset >= jnl->jhdr->size) {
*offset = jnl->jhdr->jhdr_size;
}
goto again;
}
return io_sz;
}
static size_t
read_journal_data(journal *jnl, off_t *offset, void *data, size_t len)
{
return do_journal_io(jnl, offset, data, len, JNL_READ);
}
static size_t
write_journal_data(journal *jnl, off_t *offset, void *data, size_t len)
{
return do_journal_io(jnl, offset, data, len, JNL_WRITE);
}
static int
read_journal_header(journal *jnl, void *data, size_t len)
{
off_t hdr_offset = 0;
return do_journal_io(jnl, &hdr_offset, data, len, JNL_READ|JNL_HEADER);
}
static int
write_journal_header(journal *jnl)
{
static int num_err_prints = 0;
int ret;
off_t jhdr_offset = 0;
ret = VOP_IOCTL(jnl->jdev, DKIOCSYNCHRONIZECACHE, NULL, FWRITE, NOCRED, current_proc());
if (ret != 0) {
if ( ret != jnl->last_flush_err
|| (jnl->flags & JOURNAL_FLUSHCACHE_ERR) == 0
|| num_err_prints++ < 25) {
printf("jnl: flushing fs disk buffer returned 0x%x\n", ret);
jnl->flags |= JOURNAL_FLUSHCACHE_ERR;
jnl->last_flush_err = ret;
}
}
jnl->jhdr->checksum = 0;
jnl->jhdr->checksum = calc_checksum((char *)jnl->jhdr, sizeof(struct journal_header));
if (do_journal_io(jnl, &jhdr_offset, jnl->header_buf, jnl->jhdr->jhdr_size, JNL_WRITE|JNL_HEADER) != jnl->jhdr->jhdr_size) {
printf("jnl: write_journal_header: error writing the journal header!\n");
jnl->flags |= JOURNAL_INVALID;
return -1;
}
VOP_IOCTL(jnl->jdev, DKIOCSYNCHRONIZECACHE, NULL, FWRITE, NOCRED, current_proc());
return 0;
}
static void
free_old_stuff(journal *jnl)
{
transaction *tr, *next;
for(tr=jnl->tr_freeme; tr; tr=next) {
next = tr->next;
FREE_ZONE(tr, sizeof(transaction), M_JNL_TR);
}
jnl->tr_freeme = NULL;
}
static void
buffer_flushed_callback(struct buf *bp)
{
transaction *tr;
journal *jnl;
transaction *ctr, *prev=NULL, *next;
int i, bufsize;
bufsize = bp->b_bufsize;
tr = bp->b_transaction;
bp->b_iodone = NULL; bp->b_transaction = NULL;
if (ISSET(bp->b_flags, B_ASYNC)) {
brelse(bp);
} else {
CLR(bp->b_flags, B_WANTED);
wakeup(bp);
}
if (tr == NULL) {
return;
}
CHECK_TRANSACTION(tr);
jnl = tr->jnl;
if (jnl->flags & JOURNAL_INVALID) {
return;
}
CHECK_JOURNAL(jnl);
tr->num_flushed += bufsize;
if ((tr->num_flushed + tr->num_killed) < tr->total_bytes) {
return;
}
simple_lock(&jnl->old_start_lock);
for(i=0; i < sizeof(jnl->old_start)/sizeof(jnl->old_start[0]); i++) {
if ((jnl->old_start[i] & ~(0x8000000000000000LL)) == tr->journal_start) {
jnl->old_start[i] &= ~(0x8000000000000000LL);
break;
}
}
if (i >= sizeof(jnl->old_start)/sizeof(jnl->old_start[0])) {
panic("jnl: buffer_flushed: did not find tr w/start @ %lld (tr 0x%x, jnl 0x%x)\n",
tr->journal_start, tr, jnl);
}
simple_unlock(&jnl->old_start_lock);
if (tr->journal_start == jnl->active_start) {
jnl->active_start = tr->journal_end;
tr->journal_start = tr->journal_end = (off_t)0;
}
for(ctr=jnl->completed_trs; ctr; prev=ctr, ctr=next) {
if (ctr->journal_start == jnl->active_start) {
jnl->active_start = ctr->journal_end;
if (prev) {
prev->next = ctr->next;
}
if (ctr == jnl->completed_trs) {
jnl->completed_trs = ctr->next;
}
next = jnl->completed_trs; ctr->next = jnl->tr_freeme;
jnl->tr_freeme = ctr;
ctr = NULL;
} else if (tr->journal_end == ctr->journal_start) {
ctr->journal_start = tr->journal_start;
next = jnl->completed_trs; ctr = NULL;
tr->journal_start = tr->journal_end = (off_t)0;
} else if (tr->journal_start == ctr->journal_end) {
ctr->journal_end = tr->journal_end;
next = ctr->next;
tr->journal_start = tr->journal_end = (off_t)0;
} else {
next = ctr->next;
}
}
tr->total_bytes = 0xfbadc0de;
if (tr->journal_start != 0) {
prev = NULL;
for(ctr=jnl->completed_trs; ctr && tr->journal_start > ctr->journal_start; prev=ctr, ctr=ctr->next) {
}
if (ctr == NULL && prev == NULL) {
jnl->completed_trs = tr;
tr->next = NULL;
} else if (ctr == jnl->completed_trs) {
tr->next = jnl->completed_trs;
jnl->completed_trs = tr;
} else {
tr->next = prev->next;
prev->next = tr;
}
} else {
tr->next = jnl->tr_freeme;
jnl->tr_freeme = tr;
}
}
#include <libkern/OSByteOrder.h>
#define SWAP16(x) OSSwapInt16(x)
#define SWAP32(x) OSSwapInt32(x)
#define SWAP64(x) OSSwapInt64(x)
static void
swap_journal_header(journal *jnl)
{
jnl->jhdr->magic = SWAP32(jnl->jhdr->magic);
jnl->jhdr->endian = SWAP32(jnl->jhdr->endian);
jnl->jhdr->start = SWAP64(jnl->jhdr->start);
jnl->jhdr->end = SWAP64(jnl->jhdr->end);
jnl->jhdr->size = SWAP64(jnl->jhdr->size);
jnl->jhdr->blhdr_size = SWAP32(jnl->jhdr->blhdr_size);
jnl->jhdr->checksum = SWAP32(jnl->jhdr->checksum);
jnl->jhdr->jhdr_size = SWAP32(jnl->jhdr->jhdr_size);
}
static void
swap_block_list_header(journal *jnl, block_list_header *blhdr)
{
int i;
blhdr->max_blocks = SWAP16(blhdr->max_blocks);
blhdr->num_blocks = SWAP16(blhdr->num_blocks);
blhdr->bytes_used = SWAP32(blhdr->bytes_used);
blhdr->checksum = SWAP32(blhdr->checksum);
blhdr->pad = SWAP32(blhdr->pad);
if (blhdr->num_blocks * sizeof(blhdr->binfo[0]) > jnl->jhdr->blhdr_size) {
printf("jnl: blhdr num blocks looks suspicious (%d). not swapping.\n", blhdr->num_blocks);
return;
}
for(i=0; i < blhdr->num_blocks; i++) {
blhdr->binfo[i].bnum = SWAP64(blhdr->binfo[i].bnum);
blhdr->binfo[i].bsize = SWAP32(blhdr->binfo[i].bsize);
blhdr->binfo[i].bp = (void *)SWAP32((int)blhdr->binfo[i].bp);
}
}
static int
update_fs_block(journal *jnl, void *block_ptr, off_t fs_block, size_t bsize)
{
int ret;
struct buf *oblock_bp=NULL;
ret = meta_bread(jnl->fsdev, (daddr_t)fs_block, bsize, NOCRED, &oblock_bp);
if (ret != 0) {
printf("jnl: update_fs_block: error reading fs block # %lld! (ret %d)\n", fs_block, ret);
if (oblock_bp) {
brelse(oblock_bp);
oblock_bp = NULL;
}
oblock_bp = getblk(jnl->fsdev, (daddr_t)fs_block, bsize, 0, 0, BLK_META);
if (oblock_bp == NULL) {
printf("jnl: update_fs_block: getblk() for %lld failed! failing update.\n", fs_block);
return -1;
}
}
if (oblock_bp->b_bufsize != bsize) {
brelse(oblock_bp);
return -1;
}
memcpy(oblock_bp->b_data, block_ptr, bsize);
if ((ret = VOP_BWRITE(oblock_bp)) != 0) {
printf("jnl: update_fs_block: failed to update block %lld (ret %d)\n", fs_block,ret);
return ret;
}
ret = meta_bread(jnl->fsdev, (daddr_t)fs_block, bsize, NOCRED, &oblock_bp);
if (oblock_bp) {
oblock_bp->b_flags |= B_INVAL;
brelse(oblock_bp);
}
return 0;
}
static int
grow_table(struct bucket **buf_ptr, int num_buckets, int new_size)
{
struct bucket *newBuf;
int current_size = num_buckets, i;
if (new_size < num_buckets) {
return current_size;
}
if ((MALLOC(newBuf, struct bucket *, new_size*sizeof(struct bucket), M_TEMP, M_WAITOK)) == NULL) {
printf("jnl: grow_table: no memory to expand coalesce buffer!\n");
return -1;
}
bcopy(*buf_ptr, newBuf, num_buckets*sizeof(struct bucket));
for(i=num_buckets; i < new_size; i++) {
newBuf[i].block_num = (off_t)-1;
}
FREE(*buf_ptr, M_TEMP);
*buf_ptr = newBuf;
return new_size;
}
static int
lookup_bucket(struct bucket **buf_ptr, off_t block_num, int num_full)
{
int lo, hi, index, matches, i;
if (num_full == 0) {
return 0; }
lo = 0;
hi = num_full - 1;
index = -1;
do {
int mid = (hi - lo)/2 + lo;
off_t this_num = (*buf_ptr)[mid].block_num;
if (block_num == this_num) {
index = mid;
break;
}
if (block_num < this_num) {
hi = mid;
continue;
}
if (block_num > this_num) {
lo = mid + 1;
continue;
}
} while(lo < hi);
if (block_num == (*buf_ptr)[hi].block_num) {
index = hi;
}
if (index == -1) {
index = (block_num < (*buf_ptr)[hi].block_num) ? hi : hi + 1;
} else {
matches = 0;
i = index + 1;
while(i < num_full && block_num == (*buf_ptr)[i].block_num) {
matches++;
i++;
}
index += matches;
}
return index;
}
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)
{
if (!overwriting) {
if (*num_full_ptr >= *num_buckets_ptr) {
int new_size = *num_buckets_ptr * 2;
int grow_size = grow_table(buf_ptr, *num_buckets_ptr, new_size);
if (grow_size < new_size) {
printf("jnl: add_block: grow_table returned an error!\n");
return -1;
}
*num_buckets_ptr = grow_size; }
if (blk_index != *num_full_ptr) {
bcopy( (*buf_ptr)+(blk_index), (*buf_ptr)+(blk_index+1), (*num_full_ptr-blk_index)*sizeof(struct bucket) );
}
(*num_full_ptr)++; }
if (offset >= jnl->jhdr->size) {
offset = jnl->jhdr->jhdr_size + (offset - jnl->jhdr->size);
}
if (size <= 0) {
panic("jnl: insert_block: bad size in insert_block (%d)\n", size);
}
(*buf_ptr)[blk_index].block_num = num;
(*buf_ptr)[blk_index].block_size = size;
(*buf_ptr)[blk_index].jnl_offset = offset;
return blk_index;
}
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)
{
int num_to_remove, index, i, overwrite, err;
size_t jhdr_size = jnl->jhdr->jhdr_size, new_offset;
off_t overlap, block_start, block_end;
block_start = block_num*jhdr_size;
block_end = block_start + size;
overwrite = (block_num == (*buf_ptr)[blk_index].block_num && size >= (*buf_ptr)[blk_index].block_size);
if (blk_index != 0 && !overwrite) {
off_t prev_block_start = (*buf_ptr)[blk_index-1].block_num*jhdr_size;
off_t prev_block_end = prev_block_start + (*buf_ptr)[blk_index-1].block_size;
overlap = prev_block_end - block_start;
if (overlap > 0) {
if (overlap % jhdr_size != 0) {
panic("jnl: do_overlap: overlap with previous entry not a multiple of %d\n", jhdr_size);
}
if (prev_block_end > block_end) {
off_t new_num = block_end / jhdr_size;
size_t new_size = prev_block_end - block_end;
size_t new_offset = (*buf_ptr)[blk_index-1].jnl_offset + (block_end - prev_block_start);
err = insert_block(jnl, buf_ptr, blk_index, new_num, new_size, new_offset, num_buckets_ptr, num_full_ptr, 0);
if (err < 0) {
panic("jnl: do_overlap: error inserting during pre-overlap\n");
}
}
(*buf_ptr)[blk_index-1].block_size = block_start - prev_block_start;
}
}
if (!overwrite && block_end <= (*buf_ptr)[blk_index].block_num*jhdr_size) {
return 0; } else if (overwrite && (blk_index + 1 >= *num_full_ptr || block_end <= (*buf_ptr)[blk_index+1].block_num*jhdr_size)) {
return 1; }
index = blk_index;
num_to_remove = 0;
while(index < *num_full_ptr && block_end > (*buf_ptr)[index].block_num*jhdr_size) {
if (block_end >= ((*buf_ptr)[index].block_num*jhdr_size + (*buf_ptr)[index].block_size)) {
(*buf_ptr)[index].block_num = -2; num_to_remove++;
} else {
overlap = block_end - (*buf_ptr)[index].block_num*jhdr_size;
if (overlap > 0) {
if (overlap % jhdr_size != 0) {
panic("jnl: do_overlap: overlap of %d is not multiple of %d\n", overlap, jhdr_size);
}
(*buf_ptr)[index].block_num += (overlap / jhdr_size);
new_offset = (*buf_ptr)[index].jnl_offset + overlap; if (new_offset >= jnl->jhdr->size) {
new_offset = jhdr_size + (new_offset - jnl->jhdr->size);
}
(*buf_ptr)[index].jnl_offset = new_offset;
(*buf_ptr)[index].block_size -= overlap; if ((*buf_ptr)[index].block_size <= 0) {
panic("jnl: do_overlap: after overlap, new block size is invalid (%d)\n", (*buf_ptr)[index].block_size);
}
}
}
index++;
}
index--; while(index >= blk_index) {
if ((*buf_ptr)[index].block_num == -2) {
if (index == *num_full_ptr-1) {
(*buf_ptr)[index].block_num = -1; } else {
bcopy( (*buf_ptr)+(index+1), (*buf_ptr)+(index), (*num_full_ptr - (index + 1)) * sizeof(struct bucket) );
}
(*num_full_ptr)--;
}
index--;
}
for(i=*num_full_ptr; i < (*num_full_ptr + num_to_remove); i++) {
(*buf_ptr)[i].block_num = -1;
}
return 0; }
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)
{
int blk_index, overwriting;
size_t jhdr_size = jnl->jhdr->jhdr_size;
blk_index = lookup_bucket( buf_ptr, block_num, *num_full_ptr);
if (blk_index < 0 || blk_index > *num_full_ptr) {
return -1;
}
overwriting = do_overlap(jnl, buf_ptr, blk_index, block_num, size, offset, num_buckets_ptr, num_full_ptr);
if (overwriting < 0) {
return -1; }
blk_index = insert_block(jnl, buf_ptr, blk_index, block_num, size, offset, num_buckets_ptr, num_full_ptr, overwriting);
return blk_index;
}
static int
replay_journal(journal *jnl)
{
int i, ret, orig_checksum, checksum, max_bsize;
struct buf *oblock_bp;
block_list_header *blhdr;
off_t offset;
char *buf, *block_ptr=NULL;
struct bucket *co_buf;
int num_buckets = STARTING_BUCKETS, num_full;
if (jnl->jhdr->start == jnl->jhdr->size) {
jnl->jhdr->start = jnl->jhdr->jhdr_size;
}
if (jnl->jhdr->end == jnl->jhdr->size) {
jnl->jhdr->end = jnl->jhdr->jhdr_size;
}
if (jnl->jhdr->start == jnl->jhdr->end) {
return 0;
}
if (kmem_alloc(kernel_map, (vm_offset_t *)&buf, jnl->jhdr->blhdr_size)) {
printf("jnl: replay_journal: no memory for block buffer! (%d bytes)\n",
jnl->jhdr->blhdr_size);
return -1;
}
if ((MALLOC(co_buf, struct bucket *, num_buckets*sizeof(struct bucket), M_TEMP, M_WAITOK)) == NULL) {
printf("jnl: replay_journal: no memory for coalesce buffer!\n");
return -1;
}
for(i=0; i < num_buckets; i++) {
co_buf[i].block_num = -1;
}
num_full = 0;
printf("jnl: replay_journal: from: %lld to: %lld (joffset 0x%llx)\n",
jnl->jhdr->start, jnl->jhdr->end, jnl->jdev_offset);
while(jnl->jhdr->start != jnl->jhdr->end) {
offset = jnl->jhdr->start;
ret = read_journal_data(jnl, &offset, buf, jnl->jhdr->blhdr_size);
if (ret != jnl->jhdr->blhdr_size) {
printf("jnl: replay_journal: Could not read block list header block @ 0x%llx!\n", offset);
goto bad_replay;
}
blhdr = (block_list_header *)buf;
orig_checksum = blhdr->checksum;
blhdr->checksum = 0;
if (jnl->flags & JOURNAL_NEED_SWAP) {
orig_checksum = SWAP32(orig_checksum);
checksum = calc_checksum((char *)blhdr, BLHDR_CHECKSUM_SIZE);
swap_block_list_header(jnl, blhdr);
} else {
checksum = calc_checksum((char *)blhdr, BLHDR_CHECKSUM_SIZE);
}
if (checksum != orig_checksum) {
printf("jnl: replay_journal: bad block list header @ 0x%llx (checksum 0x%x != 0x%x)\n",
offset, orig_checksum, checksum);
goto bad_replay;
}
if ( blhdr->max_blocks <= 0 || blhdr->max_blocks > 2048
|| blhdr->num_blocks <= 0 || blhdr->num_blocks > blhdr->max_blocks) {
printf("jnl: replay_journal: bad looking journal entry: max: %d num: %d\n",
blhdr->max_blocks, blhdr->num_blocks);
goto bad_replay;
}
for(i=1; i < blhdr->num_blocks; i++) {
if (blhdr->binfo[i].bnum < 0 && blhdr->binfo[i].bnum != (off_t)-1) {
printf("jnl: replay_journal: bogus block number 0x%llx\n", blhdr->binfo[i].bnum);
goto bad_replay;
}
}
for(i=1; i < blhdr->num_blocks; i++) {
int size, ret_val;
off_t number;
size = blhdr->binfo[i].bsize;
number = blhdr->binfo[i].bnum;
if (number == (off_t)-1) {
} else {
ret_val = add_block(jnl, &co_buf, number, size, (size_t) offset, &num_buckets, &num_full);
if (ret_val == -1) {
printf("jnl: replay_journal: trouble adding block to co_buf\n");
goto bad_replay;
} }
offset += size;
if (offset >= jnl->jhdr->size) {
offset = jnl->jhdr->jhdr_size + (offset - jnl->jhdr->size);
}
}
jnl->jhdr->start += blhdr->bytes_used;
if (jnl->jhdr->start >= jnl->jhdr->size) {
jnl->jhdr->start = (jnl->jhdr->start % jnl->jhdr->size) + jnl->jhdr->jhdr_size;
}
}
for (i = 0, max_bsize = PAGE_SIZE; i < num_full; i++) {
if (co_buf[i].block_num == (off_t)-1)
continue;
if (co_buf[i].block_size > max_bsize)
max_bsize = co_buf[i].block_size;
}
if (max_bsize & (PAGE_SIZE - 1)) {
max_bsize = (max_bsize + PAGE_SIZE) & ~(PAGE_SIZE - 1);
}
if (kmem_alloc(kernel_map, (vm_offset_t *)&block_ptr, max_bsize)) {
goto bad_replay;
}
for(i=0; i < num_full; i++) {
size_t size = co_buf[i].block_size;
off_t jnl_offset = (off_t) co_buf[i].jnl_offset;
off_t number = co_buf[i].block_num;
if (number == (off_t)-1) {
} else {
ret = read_journal_data(jnl, &jnl_offset, block_ptr, size);
if (ret != size) {
printf("jnl: replay_journal: Could not read journal entry data @ offset 0x%llx!\n", offset);
goto bad_replay;
}
if (update_fs_block(jnl, block_ptr, number, size) != 0) {
goto bad_replay;
}
}
}
if (write_journal_header(jnl) != 0) {
goto bad_replay;
}
kmem_free(kernel_map, (vm_offset_t)block_ptr, max_bsize);
block_ptr = NULL;
FREE(co_buf, M_TEMP);
co_buf = NULL;
kmem_free(kernel_map, (vm_offset_t)buf, jnl->jhdr->blhdr_size);
return 0;
bad_replay:
if (block_ptr) {
kmem_free(kernel_map, (vm_offset_t)block_ptr, max_bsize);
}
if (co_buf) {
FREE(co_buf, M_TEMP);
}
kmem_free(kernel_map, (vm_offset_t)buf, jnl->jhdr->blhdr_size);
return -1;
}
#define DEFAULT_TRANSACTION_BUFFER_SIZE (128*1024)
#define MAX_TRANSACTION_BUFFER_SIZE (512*1024)
int def_tbuffer_size = 0;
static void
size_up_tbuffer(journal *jnl, int tbuffer_size, int phys_blksz)
{
if (def_tbuffer_size == 0) {
if (mem_size < (256*1024*1024)) {
def_tbuffer_size = DEFAULT_TRANSACTION_BUFFER_SIZE;
} else if (mem_size < (512*1024*1024)) {
def_tbuffer_size = DEFAULT_TRANSACTION_BUFFER_SIZE * 2;
} else if (mem_size < (1024*1024*1024)) {
def_tbuffer_size = DEFAULT_TRANSACTION_BUFFER_SIZE * 3;
} else if (mem_size >= (1024*1024*1024)) {
def_tbuffer_size = DEFAULT_TRANSACTION_BUFFER_SIZE * 4;
}
}
if (tbuffer_size == 0) {
jnl->tbuffer_size = def_tbuffer_size;
} else {
if (tbuffer_size < jnl->jhdr->blhdr_size * 2) {
tbuffer_size = jnl->jhdr->blhdr_size * 2;
}
if ((tbuffer_size % jnl->jhdr->jhdr_size) != 0) {
tbuffer_size -= (tbuffer_size % jnl->jhdr->jhdr_size);
}
jnl->tbuffer_size = tbuffer_size;
}
if (jnl->tbuffer_size > (jnl->jhdr->size / 2)) {
jnl->tbuffer_size = (jnl->jhdr->size / 2);
}
if (jnl->tbuffer_size > MAX_TRANSACTION_BUFFER_SIZE) {
jnl->tbuffer_size = MAX_TRANSACTION_BUFFER_SIZE;
}
jnl->jhdr->blhdr_size = (jnl->tbuffer_size / jnl->jhdr->jhdr_size) * sizeof(block_info);
if (jnl->jhdr->blhdr_size < phys_blksz) {
jnl->jhdr->blhdr_size = phys_blksz;
} else if ((jnl->jhdr->blhdr_size % phys_blksz) != 0) {
jnl->jhdr->blhdr_size = (jnl->jhdr->blhdr_size + (phys_blksz - 1)) & ~(phys_blksz - 1);
}
}
journal *
journal_create(struct vnode *jvp,
off_t offset,
off_t journal_size,
struct vnode *fsvp,
size_t min_fs_blksz,
int32_t flags,
int32_t tbuffer_size,
void (*flush)(void *arg),
void *arg)
{
journal *jnl;
int ret, phys_blksz;
if (VOP_IOCTL(jvp, DKIOCGETBLOCKSIZE, (caddr_t)&phys_blksz, 0, FSCRED, NULL)) {
return NULL;
}
if (phys_blksz > min_fs_blksz) {
printf("jnl: create: error: phys blksize %d bigger than min fs blksize %d\n",
phys_blksz, min_fs_blksz);
return NULL;
}
if ((journal_size % phys_blksz) != 0) {
printf("jnl: create: journal size 0x%llx is not an even multiple of block size 0x%x\n",
journal_size, phys_blksz);
return NULL;
}
MALLOC_ZONE(jnl, struct journal *, sizeof(struct journal), M_JNL_JNL, M_WAITOK);
memset(jnl, 0, sizeof(*jnl));
jnl->jdev = jvp;
jnl->jdev_offset = offset;
jnl->fsdev = fsvp;
jnl->flush = flush;
jnl->flush_arg = arg;
jnl->flags = (flags & JOURNAL_OPTION_FLAGS_MASK);
simple_lock_init(&jnl->old_start_lock);
if (kmem_alloc(kernel_map, (vm_offset_t *)&jnl->header_buf, phys_blksz)) {
printf("jnl: create: could not allocate space for header buffer (%d bytes)\n", phys_blksz);
goto bad_kmem_alloc;
}
memset(jnl->header_buf, 0, phys_blksz);
jnl->jhdr = (journal_header *)jnl->header_buf;
jnl->jhdr->magic = JOURNAL_HEADER_MAGIC;
jnl->jhdr->endian = ENDIAN_MAGIC;
jnl->jhdr->start = phys_blksz; jnl->jhdr->end = phys_blksz;
jnl->jhdr->size = journal_size;
jnl->jhdr->jhdr_size = phys_blksz;
size_up_tbuffer(jnl, tbuffer_size, phys_blksz);
jnl->active_start = jnl->jhdr->start;
lockinit(&jnl->jlock, PINOD, "journal", 0, 0);
if (write_journal_header(jnl) != 0) {
printf("jnl: journal_create: failed to write journal header.\n");
goto bad_write;
}
return jnl;
bad_write:
kmem_free(kernel_map, (vm_offset_t)jnl->header_buf, phys_blksz);
bad_kmem_alloc:
jnl->jhdr = NULL;
FREE_ZONE(jnl, sizeof(struct journal), M_JNL_JNL);
return NULL;
}
journal *
journal_open(struct vnode *jvp,
off_t offset,
off_t journal_size,
struct vnode *fsvp,
size_t min_fs_blksz,
int32_t flags,
int32_t tbuffer_size,
void (*flush)(void *arg),
void *arg)
{
journal *jnl;
int orig_blksz=0, phys_blksz, blhdr_size;
int orig_checksum, checksum;
if (VOP_IOCTL(jvp, DKIOCGETBLOCKSIZE, (caddr_t)&phys_blksz, 0, FSCRED, NULL)) {
return NULL;
}
if (phys_blksz > min_fs_blksz) {
printf("jnl: create: error: phys blksize %d bigger than min fs blksize %d\n",
phys_blksz, min_fs_blksz);
return NULL;
}
if ((journal_size % phys_blksz) != 0) {
printf("jnl: open: journal size 0x%llx is not an even multiple of block size 0x%x\n",
journal_size, phys_blksz);
return NULL;
}
MALLOC_ZONE(jnl, struct journal *, sizeof(struct journal), M_JNL_JNL, M_WAITOK);
memset(jnl, 0, sizeof(*jnl));
jnl->jdev = jvp;
jnl->jdev_offset = offset;
jnl->fsdev = fsvp;
jnl->flush = flush;
jnl->flush_arg = arg;
jnl->flags = (flags & JOURNAL_OPTION_FLAGS_MASK);
simple_lock_init(&jnl->old_start_lock);
if (kmem_alloc(kernel_map, (vm_offset_t *)&jnl->header_buf, phys_blksz)) {
printf("jnl: create: could not allocate space for header buffer (%d bytes)\n", phys_blksz);
goto bad_kmem_alloc;
}
jnl->jhdr = (journal_header *)jnl->header_buf;
memset(jnl->jhdr, 0, sizeof(journal_header)+4);
jnl->jhdr->jhdr_size = phys_blksz;
if (read_journal_header(jnl, jnl->jhdr, phys_blksz) != phys_blksz) {
printf("jnl: open: could not read %d bytes for the journal header.\n",
phys_blksz);
goto bad_journal;
}
orig_checksum = jnl->jhdr->checksum;
jnl->jhdr->checksum = 0;
if (jnl->jhdr->magic == SWAP32(JOURNAL_HEADER_MAGIC)) {
orig_checksum = SWAP32(orig_checksum);
checksum = calc_checksum((char *)jnl->jhdr, sizeof(struct journal_header));
swap_journal_header(jnl);
jnl->flags |= JOURNAL_NEED_SWAP;
} else {
checksum = calc_checksum((char *)jnl->jhdr, sizeof(struct journal_header));
}
if (jnl->jhdr->magic != JOURNAL_HEADER_MAGIC && jnl->jhdr->magic != OLD_JOURNAL_HEADER_MAGIC) {
printf("jnl: open: journal magic is bad (0x%x != 0x%x)\n",
jnl->jhdr->magic, JOURNAL_HEADER_MAGIC);
goto bad_journal;
}
if (jnl->jhdr->magic == JOURNAL_HEADER_MAGIC) {
if (orig_checksum != checksum) {
printf("jnl: open: journal checksum is bad (0x%x != 0x%x)\n",
orig_checksum, checksum);
}
}
if (jnl->jhdr->magic == OLD_JOURNAL_HEADER_MAGIC) {
jnl->jhdr->magic = JOURNAL_HEADER_MAGIC;
}
if (phys_blksz != jnl->jhdr->jhdr_size && jnl->jhdr->jhdr_size != 0) {
printf("jnl: open: phys_blksz %d does not match journal header size %d\n",
phys_blksz, jnl->jhdr->jhdr_size);
orig_blksz = phys_blksz;
phys_blksz = jnl->jhdr->jhdr_size;
if (VOP_IOCTL(jvp, DKIOCSETBLOCKSIZE, (caddr_t)&phys_blksz, FWRITE, FSCRED, NULL)) {
printf("jnl: could not set block size to %d bytes.\n", phys_blksz);
goto bad_journal;
}
}
if ( jnl->jhdr->start <= 0
|| jnl->jhdr->start > jnl->jhdr->size
|| jnl->jhdr->start > 1024*1024*1024) {
printf("jnl: open: jhdr start looks bad (0x%llx max size 0x%llx)\n",
jnl->jhdr->start, jnl->jhdr->size);
goto bad_journal;
}
if ( jnl->jhdr->end <= 0
|| jnl->jhdr->end > jnl->jhdr->size
|| jnl->jhdr->end > 1024*1024*1024) {
printf("jnl: open: jhdr end looks bad (0x%llx max size 0x%llx)\n",
jnl->jhdr->end, jnl->jhdr->size);
goto bad_journal;
}
if (jnl->jhdr->size > 1024*1024*1024) {
printf("jnl: open: jhdr size looks bad (0x%llx)\n", jnl->jhdr->size);
goto bad_journal;
}
if ((jnl->jhdr->start % 512) != 0) {
printf("jnl: open: journal start (0x%llx) not a multiple of 512?\n",
jnl->jhdr->start);
goto bad_journal;
}
if ((jnl->jhdr->end % 512) != 0) {
printf("jnl: open: journal end (0x%llx) not a multiple of block size (0x%x)?\n",
jnl->jhdr->end, jnl->jhdr->jhdr_size);
goto bad_journal;
}
if (flags & JOURNAL_RESET) {
printf("jnl: journal start/end pointers reset! (jnl 0x%x; s 0x%llx e 0x%llx)\n",
jnl, jnl->jhdr->start, jnl->jhdr->end);
jnl->jhdr->start = jnl->jhdr->end;
} else if (replay_journal(jnl) != 0) {
printf("jnl: journal_open: Error replaying the journal!\n");
goto bad_journal;
}
if (orig_blksz != 0) {
VOP_IOCTL(jvp, DKIOCSETBLOCKSIZE, (caddr_t)&orig_blksz, FWRITE, FSCRED, NULL);
phys_blksz = orig_blksz;
if (orig_blksz < jnl->jhdr->jhdr_size) {
printf("jnl: open: jhdr_size is %d but orig phys blk size is %d. switching.\n",
jnl->jhdr->jhdr_size, orig_blksz);
jnl->jhdr->jhdr_size = orig_blksz;
}
}
jnl->active_start = jnl->jhdr->start;
size_up_tbuffer(jnl, tbuffer_size, phys_blksz);
lockinit(&jnl->jlock, PINOD, "journal", 0, 0);
return jnl;
bad_journal:
if (orig_blksz != 0) {
phys_blksz = orig_blksz;
VOP_IOCTL(jvp, DKIOCSETBLOCKSIZE, (caddr_t)&orig_blksz, FWRITE, FSCRED, NULL);
}
kmem_free(kernel_map, (vm_offset_t)jnl->header_buf, phys_blksz);
bad_kmem_alloc:
FREE_ZONE(jnl, sizeof(struct journal), M_JNL_JNL);
return NULL;
}
void
journal_close(journal *jnl)
{
volatile off_t *start, *end;
int counter=0;
CHECK_JOURNAL(jnl);
jnl->flags |= JOURNAL_CLOSE_PENDING;
if (jnl->owner != current_act()) {
int ret;
ret = lockmgr(&jnl->jlock, LK_EXCLUSIVE|LK_RETRY, NULL, current_proc());
if (ret != 0) {
printf("jnl: close: locking the journal (0x%x) failed %d.\n", jnl, ret);
return;
}
}
if ((jnl->flags & JOURNAL_INVALID) == 0) {
if (jnl->active_tr) {
journal_end_transaction(jnl);
}
if (jnl->cur_tr) {
transaction *tr = jnl->cur_tr;
jnl->cur_tr = NULL;
end_transaction(tr, 1); }
start = &jnl->active_start;
end = &jnl->jhdr->end;
while (*start != *end && counter++ < 500) {
printf("jnl: close: flushing the buffer cache (start 0x%llx end 0x%llx)\n", *start, *end);
if (jnl->flush) {
jnl->flush(jnl->flush_arg);
}
tsleep((caddr_t)jnl, PRIBIO, "jnl_close", 1);
}
if (*start != *end) {
printf("jnl: close: buffer flushing didn't seem to flush out all the transactions! (0x%llx - 0x%llx)\n",
*start, *end);
}
jnl->jhdr->start = jnl->active_start;
write_journal_header(jnl);
} else {
printf("jnl: close: journal 0x%x, is invalid. aborting outstanding transactions\n", jnl);
if (jnl->active_tr || jnl->cur_tr) {
transaction *tr;
if (jnl->active_tr) {
tr = jnl->active_tr;
jnl->active_tr = NULL;
} else {
tr = jnl->cur_tr;
jnl->cur_tr = NULL;
}
abort_transaction(jnl, tr);
if (jnl->active_tr || jnl->cur_tr) {
panic("jnl: close: jnl @ 0x%x had both an active and cur tr\n", jnl);
}
}
}
free_old_stuff(jnl);
kmem_free(kernel_map, (vm_offset_t)jnl->header_buf, jnl->jhdr->jhdr_size);
jnl->jhdr = (void *)0xbeefbabe;
FREE_ZONE(jnl, sizeof(struct journal), M_JNL_JNL);
}
static void
dump_journal(journal *jnl)
{
transaction *ctr;
printf("journal:");
printf(" jdev_offset %.8llx\n", jnl->jdev_offset);
printf(" magic: 0x%.8x\n", jnl->jhdr->magic);
printf(" start: 0x%.8llx\n", jnl->jhdr->start);
printf(" end: 0x%.8llx\n", jnl->jhdr->end);
printf(" size: 0x%.8llx\n", jnl->jhdr->size);
printf(" blhdr size: %d\n", jnl->jhdr->blhdr_size);
printf(" jhdr size: %d\n", jnl->jhdr->jhdr_size);
printf(" chksum: 0x%.8x\n", jnl->jhdr->checksum);
printf(" completed transactions:\n");
for(ctr=jnl->completed_trs; ctr; ctr=ctr->next) {
printf(" 0x%.8llx - 0x%.8llx\n", ctr->journal_start, ctr->journal_end);
}
}
static off_t
free_space(journal *jnl)
{
off_t free_space;
if (jnl->jhdr->start < jnl->jhdr->end) {
free_space = jnl->jhdr->size - (jnl->jhdr->end - jnl->jhdr->start) - jnl->jhdr->jhdr_size;
} else if (jnl->jhdr->start > jnl->jhdr->end) {
free_space = jnl->jhdr->start - jnl->jhdr->end;
} else {
free_space = jnl->jhdr->size - jnl->jhdr->jhdr_size;
}
return free_space;
}
static int
check_free_space(journal *jnl, int desired_size)
{
int i, counter=0;
while (1) {
int old_start_empty;
if (counter++ == 5000) {
dump_journal(jnl);
panic("jnl: check_free_space: buffer flushing isn't working "
"(jnl @ 0x%x s %lld e %lld f %lld [active start %lld]).\n", jnl,
jnl->jhdr->start, jnl->jhdr->end, free_space(jnl), jnl->active_start);
}
if (counter > 7500) {
printf("jnl: check_free_space: giving up waiting for free space.\n");
return ENOSPC;
}
if (free_space(jnl) > desired_size) {
break;
}
old_start_empty = 1;
simple_lock(&jnl->old_start_lock);
for(i=0; i < sizeof(jnl->old_start)/sizeof(jnl->old_start[0]); i++) {
int counter;
counter = 0;
while (jnl->old_start[i] & 0x8000000000000000LL) {
if (counter++ > 100) {
panic("jnl: check_free_space: tr starting @ 0x%llx not flushing (jnl 0x%x).\n",
jnl->old_start[i], jnl);
}
simple_unlock(&jnl->old_start_lock);
if (jnl->flush) {
jnl->flush(jnl->flush_arg);
}
tsleep((caddr_t)jnl, PRIBIO, "check_free_space1", 1);
simple_lock(&jnl->old_start_lock);
}
if (jnl->old_start[i] == 0) {
continue;
}
old_start_empty = 0;
jnl->jhdr->start = jnl->old_start[i];
jnl->old_start[i] = 0;
if (free_space(jnl) > desired_size) {
write_journal_header(jnl);
break;
}
}
simple_unlock(&jnl->old_start_lock);
if (i < sizeof(jnl->old_start)/sizeof(jnl->old_start[0])) {
continue;
} else if (old_start_empty) {
jnl->jhdr->start = jnl->active_start;
write_journal_header(jnl);
continue;
}
if (jnl->flush) {
jnl->flush(jnl->flush_arg);
}
tsleep((caddr_t)jnl, PRIBIO, "check_free_space2", 1);
}
return 0;
}
int
journal_start_transaction(journal *jnl)
{
int ret;
transaction *tr;
int prev_priv;
CHECK_JOURNAL(jnl);
if (jnl->flags & JOURNAL_INVALID) {
return EINVAL;
}
if (jnl->owner == current_act()) {
if (jnl->active_tr == NULL) {
panic("jnl: start_tr: active_tr is NULL (jnl @ 0x%x, owner 0x%x, current_act 0x%x\n",
jnl, jnl->owner, current_act());
}
jnl->nested_count++;
return 0;
}
ret = lockmgr(&jnl->jlock, LK_EXCLUSIVE|LK_RETRY, NULL, current_proc());
if (ret != 0) {
printf("jnl: start_tr: locking the journal (0x%x) failed %d.\n", jnl, ret);
return EINVAL;
}
if (jnl->owner != NULL || jnl->nested_count != 0 || jnl->active_tr != NULL) {
panic("jnl: start_tr: owner 0x%x, nested count 0x%x, active_tr 0x%x jnl @ 0x%x\n",
jnl->owner, jnl->nested_count, jnl->active_tr, jnl);
}
jnl->owner = current_act();
jnl->nested_count = 1;
free_old_stuff(jnl);
if (check_free_space(jnl, jnl->tbuffer_size) != 0) {
printf("jnl: start transaction failed: no space\n");
ret = ENOSPC;
goto bad_start;
}
if (jnl->cur_tr) {
jnl->active_tr = jnl->cur_tr;
jnl->cur_tr = NULL;
return 0;
}
MALLOC_ZONE(tr, transaction *, sizeof(transaction), M_JNL_TR, M_WAITOK);
memset(tr, 0, sizeof(transaction));
tr->tbuffer_size = jnl->tbuffer_size;
thread_wire_internal(host_priv_self(), current_act(), TRUE, &prev_priv);
if (kmem_alloc(kernel_map, (vm_offset_t *)&tr->tbuffer, tr->tbuffer_size)) {
FREE_ZONE(tr, sizeof(transaction), M_JNL_TR);
printf("jnl: start transaction failed: no tbuffer mem\n");
ret = ENOMEM;
thread_wire_internal(host_priv_self(), current_act(), prev_priv, NULL);
goto bad_start;
}
thread_wire_internal(host_priv_self(), current_act(), prev_priv, NULL);
memset(tr->tbuffer, 0, BLHDR_CHECKSUM_SIZE);
tr->blhdr = (block_list_header *)tr->tbuffer;
tr->blhdr->max_blocks = (jnl->jhdr->blhdr_size / sizeof(block_info)) - 1;
tr->blhdr->num_blocks = 1; tr->blhdr->bytes_used = jnl->jhdr->blhdr_size;
tr->num_blhdrs = 1;
tr->total_bytes = jnl->jhdr->blhdr_size;
tr->jnl = jnl;
jnl->active_tr = tr;
return 0;
bad_start:
jnl->owner = NULL;
jnl->nested_count = 0;
lockmgr(&jnl->jlock, LK_RELEASE, NULL, current_proc());
return ret;
}
int
journal_modify_block_start(journal *jnl, struct buf *bp)
{
transaction *tr;
CHECK_JOURNAL(jnl);
if (jnl->flags & JOURNAL_INVALID) {
return EINVAL;
}
if ((bp->b_flags & B_META) == 0) {
panic("jnl: modify_block_start: bp @ 0x%x is not a meta-data block! (jnl 0x%x)\n", bp, jnl);
}
tr = jnl->active_tr;
CHECK_TRANSACTION(tr);
if (jnl->owner != current_act()) {
panic("jnl: modify_block_start: called w/out a transaction! jnl 0x%x, owner 0x%x, curact 0x%x\n",
jnl, jnl->owner, current_act());
}
free_old_stuff(jnl);
if ((bp->b_bufsize % jnl->jhdr->jhdr_size) != 0) {
panic("jnl: mod block start: bufsize %d not a multiple of block size %d\n",
bp->b_bufsize, jnl->jhdr->jhdr_size);
return -1;
}
if (tr->total_bytes+bp->b_bufsize >= (jnl->jhdr->size - jnl->jhdr->jhdr_size)) {
panic("jnl: transaction too big (%d >= %lld bytes, bufsize %d, tr 0x%x bp 0x%x)\n",
tr->total_bytes, (tr->jnl->jhdr->size - jnl->jhdr->jhdr_size), bp->b_bufsize, tr, bp);
return -1;
}
if ((bp->b_flags & B_DELWRI) && (bp->b_flags & B_LOCKED) == 0) {
bp->b_flags |= B_NORELSE;
VOP_BWRITE(bp);
}
bp->b_flags |= B_LOCKED;
return 0;
}
int
journal_modify_block_abort(journal *jnl, struct buf *bp)
{
transaction *tr;
block_list_header *blhdr;
int i, j;
CHECK_JOURNAL(jnl);
tr = jnl->active_tr;
if (tr == NULL) {
brelse(bp);
return 0;
}
if (jnl->flags & JOURNAL_INVALID) {
return EINVAL;
}
CHECK_TRANSACTION(tr);
if (jnl->owner != current_act()) {
panic("jnl: modify_block_abort: called w/out a transaction! jnl 0x%x, owner 0x%x, curact 0x%x\n",
jnl, jnl->owner, current_act());
}
free_old_stuff(jnl);
for(blhdr=tr->blhdr; blhdr; blhdr=(block_list_header *)((long)blhdr->binfo[0].bnum)) {
for(i=1; i < blhdr->num_blocks; i++) {
if (bp == blhdr->binfo[i].bp) {
if (bp->b_bufsize != blhdr->binfo[i].bsize) {
panic("jnl: bp @ 0x%x changed size on me! (%d vs. %d, jnl 0x%x)\n",
bp, bp->b_bufsize, blhdr->binfo[i].bsize, jnl);
}
break;
}
}
if (i < blhdr->num_blocks) {
break;
}
}
if (blhdr == NULL) {
bp->b_flags &= ~(B_LOCKED);
}
brelse(bp);
return 0;
}
int
journal_modify_block_end(journal *jnl, struct buf *bp)
{
int i, j, tbuffer_offset;
char *blkptr;
block_list_header *blhdr, *prev=NULL;
transaction *tr;
CHECK_JOURNAL(jnl);
if (jnl->flags & JOURNAL_INVALID) {
return EINVAL;
}
tr = jnl->active_tr;
CHECK_TRANSACTION(tr);
if (jnl->owner != current_act()) {
panic("jnl: modify_block_end: called w/out a transaction! jnl 0x%x, owner 0x%x, curact 0x%x\n",
jnl, jnl->owner, current_act());
}
free_old_stuff(jnl);
if ((bp->b_flags & B_LOCKED) == 0) {
panic("jnl: modify_block_end: bp 0x%x not locked! jnl @ 0x%x\n", bp, jnl);
bp->b_flags |= B_LOCKED;
}
for(blhdr=tr->blhdr; blhdr; prev=blhdr,blhdr=(block_list_header *)((long)blhdr->binfo[0].bnum)) {
tbuffer_offset = jnl->jhdr->blhdr_size;
for(i=1; i < blhdr->num_blocks; i++) {
if (bp == blhdr->binfo[i].bp) {
if (bp->b_bufsize != blhdr->binfo[i].bsize) {
panic("jnl: bp @ 0x%x changed size on me! (%d vs. %d, jnl 0x%x)\n",
bp, bp->b_bufsize, blhdr->binfo[i].bsize, jnl);
}
break;
}
tbuffer_offset += blhdr->binfo[i].bsize;
}
if (i < blhdr->num_blocks) {
break;
}
}
if (blhdr == NULL
&& prev
&& (prev->num_blocks+1) <= prev->max_blocks
&& (prev->bytes_used+bp->b_bufsize) <= tr->tbuffer_size) {
blhdr = prev;
} else if (blhdr == NULL) {
block_list_header *nblhdr;
int prev_priv;
if (prev == NULL) {
panic("jnl: modify block end: no way man, prev == NULL?!?, jnl 0x%x, bp 0x%x\n", jnl, bp);
}
thread_wire_internal(host_priv_self(), current_act(), TRUE, &prev_priv);
if (kmem_alloc(kernel_map, (vm_offset_t *)&nblhdr, tr->tbuffer_size)) {
panic("jnl: end_tr: no space for new block tr @ 0x%x (total bytes: %d)!\n",
tr, tr->total_bytes);
}
thread_wire_internal(host_priv_self(), current_act(), prev_priv, NULL);
memset(nblhdr, 0, BLHDR_CHECKSUM_SIZE);
nblhdr->max_blocks = (jnl->jhdr->blhdr_size / sizeof(block_info)) - 1;
nblhdr->num_blocks = 1; nblhdr->bytes_used = jnl->jhdr->blhdr_size;
tr->num_blhdrs++;
tr->total_bytes += jnl->jhdr->blhdr_size;
prev->binfo[0].bnum = (off_t)((long)nblhdr);
blhdr = nblhdr;
tbuffer_offset = jnl->jhdr->blhdr_size;
i = 1;
}
if ((i+1) > blhdr->max_blocks) {
panic("jnl: modify_block_end: i = %d, max_blocks %d\n", i, blhdr->max_blocks);
}
blkptr = (char *)&((char *)blhdr)[tbuffer_offset];
memcpy(blkptr, bp->b_data, bp->b_bufsize);
if (i >= blhdr->num_blocks) {
vget(bp->b_vp, 0, current_proc());
blhdr->binfo[i].bnum = (off_t)((unsigned)bp->b_blkno);
blhdr->binfo[i].bsize = bp->b_bufsize;
blhdr->binfo[i].bp = bp;
blhdr->bytes_used += bp->b_bufsize;
tr->total_bytes += bp->b_bufsize;
blhdr->num_blocks++;
}
bdwrite(bp);
return 0;
}
int
journal_kill_block(journal *jnl, struct buf *bp)
{
int i;
block_list_header *blhdr;
transaction *tr;
CHECK_JOURNAL(jnl);
if (jnl->flags & JOURNAL_INVALID) {
return EINVAL;
}
tr = jnl->active_tr;
CHECK_TRANSACTION(tr);
if (jnl->owner != current_act()) {
panic("jnl: modify_block_end: called w/out a transaction! jnl 0x%x, owner 0x%x, curact 0x%x\n",
jnl, jnl->owner, current_act());
}
free_old_stuff(jnl);
if ((bp->b_flags & B_LOCKED) == 0) {
panic("jnl: kill block: bp 0x%x not locked! jnl @ 0x%x\n", bp, jnl);
}
for(blhdr=tr->blhdr; blhdr; blhdr=(block_list_header *)((long)blhdr->binfo[0].bnum)) {
for(i=1; i < blhdr->num_blocks; i++) {
if (bp == blhdr->binfo[i].bp) {
bp->b_flags &= ~B_LOCKED;
vrele(bp->b_vp);
if ((bp->b_flags & B_DELWRI) && (bp->b_flags & B_CALL)) {
panic("jnl: kill block: this defies all logic! bp 0x%x\n", bp);
} else {
tr->num_killed += bp->b_bufsize;
}
if (bp->b_flags & B_BUSY) {
brelse(bp);
}
blhdr->binfo[i].bp = NULL;
blhdr->binfo[i].bnum = (off_t)-1;
break;
}
}
if (i < blhdr->num_blocks) {
break;
}
}
return 0;
}
static int
journal_binfo_cmp(void *a, void *b)
{
block_info *bi_a = (struct block_info *)a,
*bi_b = (struct block_info *)b;
daddr_t res;
if (bi_a->bp == NULL) {
return 1;
}
if (bi_b->bp == NULL) {
return -1;
}
res = (bi_a->bp->b_blkno - bi_b->bp->b_blkno);
return (int)res;
}
static int
end_transaction(transaction *tr, int force_it)
{
int i, j, ret, amt;
off_t end;
journal *jnl = tr->jnl;
struct buf *bp;
block_list_header *blhdr=NULL, *next=NULL;
if (jnl->cur_tr) {
panic("jnl: jnl @ 0x%x already has cur_tr 0x%x, new tr: 0x%x\n",
jnl, jnl->cur_tr, tr);
}
if (tr->total_bytes == jnl->jhdr->blhdr_size) {
jnl->cur_tr = tr;
return;
}
if ( force_it == 0
&& (jnl->flags & JOURNAL_NO_GROUP_COMMIT) == 0
&& tr->num_blhdrs < 3
&& (tr->total_bytes <= ((tr->tbuffer_size*tr->num_blhdrs) - tr->tbuffer_size/8))) {
jnl->cur_tr = tr;
return;
}
check_free_space(jnl, tr->total_bytes);
if (jnl->jhdr->end <= 0 || jnl->jhdr->end > jnl->jhdr->size) {
panic("jnl: end_transaction: end is bogus 0x%llx (sz 0x%llx)\n",
jnl->jhdr->end, jnl->jhdr->size);
}
tr->journal_start = jnl->jhdr->end;
end = jnl->jhdr->end;
i = 0;
simple_lock(&jnl->old_start_lock);
while ((jnl->old_start[0] & 0x8000000000000000LL) != 0) {
if (jnl->flush) {
simple_unlock(&jnl->old_start_lock);
if (jnl->flush) {
jnl->flush(jnl->flush_arg);
}
(void)tsleep((void *)jnl, PRIBIO, "jnl-old-start-sleep", 1);
simple_lock(&jnl->old_start_lock);
}
if (i++ >= 100) {
panic("jnl: transaction that started at 0x%llx is not completing! jnl 0x%x\n",
jnl->old_start[0] & (~0x8000000000000000LL), jnl);
}
}
memcpy(&jnl->old_start[0], &jnl->old_start[1], sizeof(jnl->old_start)-sizeof(jnl->old_start[0]));
jnl->old_start[sizeof(jnl->old_start)/sizeof(jnl->old_start[0]) - 1] = tr->journal_start | 0x8000000000000000LL;
simple_unlock(&jnl->old_start_lock);
for(blhdr=tr->blhdr; blhdr; blhdr=next) {
for(i=1; i < blhdr->num_blocks; i++) {
bp = blhdr->binfo[i].bp;
if (bp == NULL) { if (blhdr->binfo[i].bnum != (off_t)-1) {
panic("jnl: inconsistent binfo (NULL bp w/bnum %lld; jnl @ 0x%x, tr 0x%x)\n",
blhdr->binfo[i].bnum, jnl, tr);
}
continue;
}
if (bp->b_vp == NULL && bp->b_lblkno == bp->b_blkno) {
panic("jnl: end_tr: DANGER! bp @ 0x%x w/null vp and l/blkno = %d/%d\n",
bp, bp->b_lblkno, bp->b_blkno);
}
if ((bp->b_lblkno == bp->b_blkno) && (bp->b_vp != jnl->fsdev)) {
if (VOP_BMAP(bp->b_vp, bp->b_lblkno, NULL, &bp->b_blkno, NULL) != 0) {
printf("jnl: end_tr: can't bmap the bp @ 0x%x, jnl 0x%x\n", bp, jnl);
goto bad_journal;
}
}
blhdr->binfo[i].bnum = (off_t)((unsigned)bp->b_blkno);
}
next = (block_list_header *)((long)blhdr->binfo[0].bnum);
}
for(blhdr=tr->blhdr; blhdr; blhdr=(block_list_header *)((long)blhdr->binfo[0].bnum)) {
amt = blhdr->bytes_used;
blhdr->checksum = 0;
blhdr->checksum = calc_checksum((char *)blhdr, BLHDR_CHECKSUM_SIZE);
ret = write_journal_data(jnl, &end, blhdr, amt);
if (ret != amt) {
printf("jnl: end_transaction: only wrote %d of %d bytes to the journal!\n",
ret, amt);
goto bad_journal;
}
}
jnl->jhdr->end = end; tr->journal_end = end; if (tr->journal_start == 0 || tr->journal_end == 0) {
panic("jnl: end_transaction: bad tr journal start/end: 0x%llx 0x%llx\n",
tr->journal_start, tr->journal_end);
}
if (write_journal_header(jnl) != 0) {
goto bad_journal;
}
blhdr = tr->blhdr;
tr->tbuffer = NULL;
tr->blhdr = NULL;
tr->num_flushed = tr->num_blhdrs * jnl->jhdr->blhdr_size;
for(; blhdr; blhdr=next) {
qsort(&blhdr->binfo[1], blhdr->num_blocks-1, sizeof(block_info), journal_binfo_cmp);
for(i=1; i < blhdr->num_blocks; i++) {
if (blhdr->binfo[i].bp == NULL) {
continue;
}
ret = meta_bread(blhdr->binfo[i].bp->b_vp,
(daddr_t)blhdr->binfo[i].bp->b_lblkno,
blhdr->binfo[i].bp->b_bufsize,
NOCRED,
&bp);
if (ret == 0 && bp != NULL) {
struct vnode *save_vp;
if (bp != blhdr->binfo[i].bp) {
panic("jnl: end_tr: got back a different bp! (bp 0x%x should be 0x%x, jnl 0x%x\n",
bp, blhdr->binfo[i].bp, jnl);
}
if ((bp->b_flags & (B_LOCKED|B_DELWRI)) != (B_LOCKED|B_DELWRI)) {
if (jnl->flags & JOURNAL_CLOSE_PENDING) {
brelse(bp);
continue;
} else {
panic("jnl: end_tr: !!!DANGER!!! bp 0x%x flags (0x%x) not LOCKED & DELWRI\n", bp, bp->b_flags);
}
}
if (bp->b_iodone != NULL) {
panic("jnl: bp @ 0x%x (blkno %d, vp 0x%x) has non-null iodone (0x%x) buffflushcb 0x%x\n",
bp, bp->b_blkno, bp->b_vp, bp->b_iodone, buffer_flushed_callback);
}
save_vp = bp->b_vp;
bp->b_iodone = buffer_flushed_callback;
bp->b_transaction = tr;
bp->b_flags |= B_CALL;
bp->b_flags &= ~(B_LOCKED);
bawrite(bp);
vrele(save_vp);
} else {
printf("jnl: end_transaction: could not find block %Ld vp 0x%x!\n",
blhdr->binfo[i].bnum, blhdr->binfo[i].bp);
if (bp) {
brelse(bp);
}
}
}
next = (block_list_header *)((long)blhdr->binfo[0].bnum);
blhdr->binfo[0].bnum = 0xdeadc0de;
kmem_free(kernel_map, (vm_offset_t)blhdr, tr->tbuffer_size);
}
return 0;
bad_journal:
jnl->flags |= JOURNAL_INVALID;
abort_transaction(jnl, tr);
return -1;
}
static void
abort_transaction(journal *jnl, transaction *tr)
{
int i, ret;
block_list_header *blhdr, *next;
struct buf *bp;
struct vnode *save_vp;
for(blhdr=tr->blhdr; blhdr; blhdr=next) {
for(i=1; i < blhdr->num_blocks; i++) {
if (blhdr->binfo[i].bp == NULL) {
continue;
}
ret = meta_bread(blhdr->binfo[i].bp->b_vp,
(daddr_t)blhdr->binfo[i].bp->b_lblkno,
blhdr->binfo[i].bp->b_bufsize,
NOCRED,
&bp);
if (ret == 0) {
if (bp != blhdr->binfo[i].bp) {
panic("jnl: abort_tr: got back a different bp! (bp 0x%x should be 0x%x, jnl 0x%x\n",
bp, blhdr->binfo[i].bp, jnl);
}
bp->b_flags &= ~(B_LOCKED|B_DELWRI);
bp->b_flags |= B_INVAL;
save_vp = bp->b_vp;
brelse(bp);
vrele(save_vp);
} else {
printf("jnl: abort_tr: could not find block %Ld vp 0x%x!\n",
blhdr->binfo[i].bnum, blhdr->binfo[i].bp);
if (bp) {
brelse(bp);
}
}
}
next = (block_list_header *)((long)blhdr->binfo[0].bnum);
blhdr->binfo[0].bnum = 0xdeadc0de;
kmem_free(kernel_map, (vm_offset_t)blhdr, tr->tbuffer_size);
}
tr->tbuffer = NULL;
tr->blhdr = NULL;
tr->total_bytes = 0xdbadc0de;
FREE_ZONE(tr, sizeof(transaction), M_JNL_TR);
}
int
journal_end_transaction(journal *jnl)
{
int ret;
transaction *tr;
CHECK_JOURNAL(jnl);
if ((jnl->flags & JOURNAL_INVALID) && jnl->owner == NULL) {
return 0;
}
if (jnl->owner != current_act()) {
panic("jnl: end_tr: I'm not the owner! jnl 0x%x, owner 0x%x, curact 0x%x\n",
jnl, jnl->owner, current_act());
}
free_old_stuff(jnl);
jnl->nested_count--;
if (jnl->nested_count > 0) {
return 0;
} else if (jnl->nested_count < 0) {
panic("jnl: jnl @ 0x%x has negative nested count (%d). bad boy.\n", jnl, jnl->nested_count);
}
if (jnl->flags & JOURNAL_INVALID) {
if (jnl->active_tr) {
transaction *tr;
if (jnl->cur_tr != NULL) {
panic("jnl: journal @ 0x%x has active tr (0x%x) and cur tr (0x%x)\n",
jnl, jnl->active_tr, jnl->cur_tr);
}
tr = jnl->active_tr;
jnl->active_tr = NULL;
abort_transaction(jnl, tr);
}
jnl->owner = NULL;
lockmgr(&jnl->jlock, LK_RELEASE, NULL, current_proc());
return EINVAL;
}
tr = jnl->active_tr;
CHECK_TRANSACTION(tr);
jnl->active_tr = NULL;
ret = end_transaction(tr, 0);
jnl->owner = NULL;
lockmgr(&jnl->jlock, LK_RELEASE, NULL, current_proc());
return ret;
}
int
journal_flush(journal *jnl)
{
int need_signal = 0;
CHECK_JOURNAL(jnl);
if (jnl->flags & JOURNAL_INVALID) {
return -1;
}
if (jnl->owner != current_act()) {
int ret;
ret = lockmgr(&jnl->jlock, LK_EXCLUSIVE|LK_RETRY, NULL, current_proc());
if (ret != 0) {
printf("jnl: flush: locking the journal (0x%x) failed %d.\n", jnl, ret);
return -1;
}
need_signal = 1;
}
free_old_stuff(jnl);
if (jnl->active_tr == NULL && jnl->cur_tr) {
transaction *tr = jnl->cur_tr;
jnl->cur_tr = NULL;
end_transaction(tr, 1); }
if (need_signal) {
lockmgr(&jnl->jlock, LK_RELEASE, NULL, current_proc());
}
return 0;
}
int
journal_active(journal *jnl)
{
if (jnl->flags & JOURNAL_INVALID) {
return -1;
}
return (jnl->active_tr == NULL) ? 0 : 1;
}