#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 <assert.h>
#include <sys/sysctl.h>
#include <sys/types.h>
#include <mach/mach.h>
#include <sys/disk.h>
#include <sys/kdebug.h>
#include "lf_hfs_locks.h"
#include "lf_hfs_journal.h"
#include "lf_hfs_vfsutils.h"
#include "lf_hfs_raw_read_write.h"
#include "lf_hfs_generic_buf.h"
#include "lf_hfs_logger.h"
#include "lf_hfs_vfsops.h"
#define CHECK_JOURNAL(jnl) \
do { \
if (jnl == NULL) { \
printf("%s:%d: null journal ptr?\n", __FILE__, __LINE__); \
panic("%s:%d: null journal ptr?\n", __FILE__, __LINE__); \
} \
if (jnl->jdev == NULL) { \
printf("%s:%d: jdev is null!\n", __FILE__, __LINE__); \
panic("%s:%d: jdev is null!\n", __FILE__, __LINE__); \
} \
if (jnl->fsdev == NULL) { \
printf("%s:%d: fsdev is null!\n", __FILE__, __LINE__); \
panic("%s:%d: fsdev is null!\n", __FILE__, __LINE__); \
} \
if (jnl->jhdr->magic != JOURNAL_HEADER_MAGIC) { \
printf("%s:%d: jhdr magic corrupted (0x%x != 0x%x)\n", \
__FILE__, __LINE__, 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) { \
printf("%s:%d: jhdr start looks bad (0x%llx max size 0x%llx)\n", \
__FILE__, __LINE__, jnl->jhdr->start, jnl->jhdr->size); \
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) { \
printf("%s:%d: jhdr end looks bad (0x%llx max size 0x%llx)\n", \
__FILE__, __LINE__, jnl->jhdr->end, jnl->jhdr->size); \
panic("%s:%d: jhdr end looks bad (0x%llx max size 0x%llx)\n", \
__FILE__, __LINE__, jnl->jhdr->end, jnl->jhdr->size); \
} \
} while(0)
#define CHECK_TRANSACTION(tr) \
do { \
if (tr == NULL) { \
printf("%s:%d: null transaction ptr?\n", __FILE__, __LINE__); \
panic("%s:%d: null transaction ptr?\n", __FILE__, __LINE__); \
} \
if (tr->jnl == NULL) { \
printf("%s:%d: null tr->jnl ptr?\n", __FILE__, __LINE__); \
panic("%s:%d: null tr->jnl ptr?\n", __FILE__, __LINE__); \
} \
if (tr->blhdr != (block_list_header *)tr->tbuffer) { \
printf("%s:%d: blhdr (%p) != tbuffer (%p)\n", __FILE__, __LINE__, tr->blhdr, tr->tbuffer); \
panic("%s:%d: blhdr (%p) != tbuffer (%p)\n", __FILE__, __LINE__, tr->blhdr, tr->tbuffer); \
} \
if (tr->total_bytes < 0) { \
printf("%s:%d: tr total_bytes looks bad: %d\n", __FILE__, __LINE__, tr->total_bytes); \
panic("%s:%d: tr total_bytes looks bad: %d\n", __FILE__, __LINE__, tr->total_bytes); \
} \
if (tr->journal_start < 0) { \
printf("%s:%d: tr journal start looks bad: 0x%llx\n", __FILE__, __LINE__, tr->journal_start); \
panic("%s:%d: tr journal start looks bad: 0x%llx\n", __FILE__, __LINE__, tr->journal_start); \
} \
if (tr->journal_end < 0) { \
printf("%s:%d: tr journal end looks bad: 0x%llx\n", __FILE__, __LINE__, tr->journal_end); \
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))) { \
printf("%s:%d: tr blhdr max_blocks looks bad: %d\n", __FILE__, __LINE__, tr->blhdr->max_blocks); \
panic("%s:%d: tr blhdr max_blocks looks bad: %d\n", __FILE__, __LINE__, tr->blhdr->max_blocks); \
} \
} while(0)
#define SWAP16(x) OSSwapInt16(x)
#define SWAP32(x) OSSwapInt32(x)
#define SWAP64(x) OSSwapInt64(x)
#define JNL_WRITE 0x0001
#define JNL_READ 0x0002
#define JNL_HEADER 0x8000
#define BLHDR_CHECKSUM_SIZE 32
#define MAX_JOURNAL_SIZE 0x80000000U
#define STARTING_BUCKETS 256
typedef struct bucket {
off_t block_num;
uint32_t jnl_offset;
uint32_t block_size;
int32_t cksum;
} bucket;
static int replay_journal(journal *jnl);
static void free_old_stuff(journal *jnl);
static errno_t journal_allocate_transaction(journal *jnl);
static void get_io_info(struct vnode *devvp, size_t phys_blksz, journal *jnl);
static size_t read_journal_header(journal *jnl, void *data, size_t len);
static size_t do_journal_io(journal *jnl, off_t *offset, void *data, size_t len, int direction);
static unsigned int calc_checksum(const char *ptr, int len);
static void swap_journal_header(journal *jnl);
static int end_transaction(transaction *tr,
int force_it,
errno_t (*callback)(void*),
void *callback_arg,
boolean_t drop_lock);
static void abort_transaction(journal *jnl, transaction *tr);
static void size_up_tbuffer(journal *jnl, uint32_t tbuffer_size, uint32_t phys_blksz);
static void lock_condition(journal *jnl, ConditionalFlag_S *psCondFlag, __unused const char *condition_name);
static void wait_condition(journal *jnl, ConditionalFlag_S *psCondFlag, __unused const char *condition_name);
static void unlock_condition(journal *jnl, ConditionalFlag_S *psCondFlag);
static int write_journal_header(journal *jnl, int updating_start, uint32_t sequence_num);
static size_t read_journal_data(journal *jnl, off_t *offset, void *data, size_t len);
static size_t write_journal_data(journal *jnl, off_t *offset, void *data, size_t len);
static __inline__ void lock_oldstart(journal *jnl) {
lf_lck_mtx_lock(&jnl->old_start_lock);
}
static __inline__ void unlock_oldstart(journal *jnl) {
lf_lck_mtx_unlock(&jnl->old_start_lock);
}
__inline__ void journal_lock(journal *jnl) {
lf_lck_mtx_lock(&jnl->jlock);
if (jnl->owner) {
panic ("jnl: owner is %p, expected NULL\n", jnl->owner);
}
jnl->owner = pthread_self();
}
__inline__ void journal_unlock(journal *jnl) {
jnl->owner = NULL;
lf_lck_mtx_unlock(&jnl->jlock);
}
static __inline__ void lock_flush(journal *jnl) {
lf_lck_mtx_lock(&jnl->flock);
}
static __inline__ void unlock_flush(journal *jnl) {
lf_lck_mtx_unlock(&jnl->flock);
}
lck_grp_attr_t *jnl_group_attr = NULL;
lck_attr_t *jnl_lock_attr = NULL;
lck_grp_t *jnl_mutex_group = NULL;
enum {
JOURNAL_DEFAULT_TRIM_BYTES = 4096,
JOURNAL_DEFAULT_TRIM_EXTENTS = JOURNAL_DEFAULT_TRIM_BYTES / sizeof(dk_extent_t),
JOURNAL_FLUSH_TRIM_EXTENTS = JOURNAL_DEFAULT_TRIM_EXTENTS * 15 / 16
};
unsigned int jnl_trim_flush_limit = JOURNAL_FLUSH_TRIM_EXTENTS;
#define DEFAULT_TRANSACTION_BUFFER_SIZE (128*1024)
#define MAX_TRANSACTION_BUFFER_SIZE (3072*1024)
uint32_t def_tbuffer_size = 0;
void journal_init(void) {
jnl_lock_attr = lf_lck_attr_alloc_init();
jnl_group_attr = lf_lck_grp_attr_alloc_init();
jnl_mutex_group = lf_lck_grp_alloc_init();
}
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,
struct mount *fsmount) {
journal *jnl;
uint32_t orig_blksz=0;
uint32_t phys_blksz;
u_int32_t min_size = 0;
int orig_checksum, checksum;
if (ioctl(jvp->psFSRecord->iFD, DKIOCGETBLOCKSIZE, (caddr_t)&phys_blksz)) {
goto cleanup_jdev_name;
}
if (phys_blksz > min_fs_blksz) {
LFHFS_LOG(LEVEL_ERROR, "jnl: open: error: phys blksize %u bigger than min fs blksize %zd\n",
phys_blksz, min_fs_blksz);
goto cleanup_jdev_name;
}
if (journal_size < (256*1024) || journal_size > (1024*1024*1024)) {
LFHFS_LOG(LEVEL_ERROR, "jnl: open: journal size %lld looks bogus.\n", journal_size);
goto cleanup_jdev_name;
}
min_size = phys_blksz * (phys_blksz / sizeof(block_info));
if (journal_size < min_size) {
LFHFS_LOG(LEVEL_ERROR, "jnl: open: journal size (%lld) too small given sector size of (%u)\n",
journal_size, phys_blksz);
goto cleanup_jdev_name;
}
if ((journal_size % phys_blksz) != 0) {
LFHFS_LOG(LEVEL_ERROR, "jnl: open: journal size 0x%llx is not an even multiple of block size 0x%x\n",
journal_size, phys_blksz);
goto cleanup_jdev_name;
}
jnl = hfs_mallocz(sizeof(struct journal));
jnl->jdev = jvp;
jnl->jdev_offset = offset;
jnl->jdev_blknum = (uint32_t)(offset / min_fs_blksz);
jnl->fsdev = fsvp;
jnl->flush = flush;
jnl->flush_arg = arg;
jnl->flags = (flags & JOURNAL_OPTION_FLAGS_MASK);
lf_lck_mtx_init(&jnl->old_start_lock);
lf_cond_init(&jnl->flushing.sCond);
lf_cond_init(&jnl->asyncIO.sCond);
lf_cond_init(&jnl->writing_header.sCond);
jnl->fsmount = fsmount;
get_io_info(jvp, phys_blksz, jnl);
jnl->header_buf = hfs_malloc(phys_blksz);
jnl->header_buf_size = phys_blksz;
jnl->jhdr = (journal_header *)jnl->header_buf;
memset(jnl->jhdr, 0, sizeof(journal_header));
jnl->jhdr->jhdr_size = phys_blksz;
if (read_journal_header(jnl, jnl->jhdr, phys_blksz) != phys_blksz) {
LFHFS_LOG(LEVEL_ERROR, "jnl: open: could not read %u bytes for the journal header.\n",
phys_blksz);
goto bad_journal;
}
if (jnl->jhdr->jhdr_size == 0) {
LFHFS_LOG(LEVEL_ERROR, "jnl: open: bad jhdr size (%d) \n", jnl->jhdr->jhdr_size);
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, JOURNAL_HEADER_CKSUM_SIZE);
swap_journal_header(jnl);
jnl->flags |= JOURNAL_NEED_SWAP;
} else {
checksum = calc_checksum((char *)jnl->jhdr, JOURNAL_HEADER_CKSUM_SIZE);
}
if (jnl->jhdr->magic != JOURNAL_HEADER_MAGIC && jnl->jhdr->magic != OLD_JOURNAL_HEADER_MAGIC) {
LFHFS_LOG(LEVEL_ERROR, "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) {
LFHFS_LOG(LEVEL_ERROR, "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 != (size_t)jnl->jhdr->jhdr_size && jnl->jhdr->jhdr_size != 0) {
LFHFS_LOG(LEVEL_ERROR,
"jnl: block size mismatch: phys_blksz=%llu, jhdr->jhdr_size=%llu -- COMPENSATING\n",
(unsigned long long)phys_blksz, (unsigned long long)jnl->jhdr->jhdr_size);
orig_blksz = phys_blksz;
}
if ( jnl->jhdr->start <= 0
|| jnl->jhdr->start > jnl->jhdr->size
|| jnl->jhdr->start > 1024*1024*1024) {
LFHFS_LOG(LEVEL_ERROR, "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) {
LFHFS_LOG(LEVEL_ERROR, "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 < (256*1024) || jnl->jhdr->size > 1024*1024*1024) {
LFHFS_LOG(LEVEL_ERROR, "jnl: open: jhdr size looks bad (0x%llx)\n", jnl->jhdr->size);
goto bad_journal;
}
if ((jnl->jhdr->start % 512) != 0) {
LFHFS_LOG(LEVEL_ERROR, "jnl: open: journal start (0x%llx) not a multiple of 512?\n",
jnl->jhdr->start);
goto bad_journal;
}
if ((jnl->jhdr->end % 512) != 0) {
LFHFS_LOG(LEVEL_ERROR, "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 (jnl->jhdr->blhdr_size < 0) {
LFHFS_LOG(LEVEL_ERROR, "jnl: open: blhdr size looks bogus! (%d) \n",
jnl->jhdr->blhdr_size);
goto bad_journal;
}
if (flags & JOURNAL_RESET) {
LFHFS_LOG(LEVEL_ERROR, "jnl: journal start/end pointers reset! (s 0x%llx e 0x%llx)\n",
jnl->jhdr->start, jnl->jhdr->end);
jnl->jhdr->start = jnl->jhdr->end;
} else if (replay_journal(jnl) != 0) {
LFHFS_LOG(LEVEL_ERROR, "jnl: journal_open: Error replaying the journal!\n");
goto bad_journal;
}
if (orig_blksz != 0) {
LFHFS_LOG(LEVEL_ERROR, "jnl: updating journal header with block size %llu\n",
(unsigned long long)phys_blksz);
jnl->jhdr->jhdr_size = phys_blksz;
jnl->jhdr->start = phys_blksz;
jnl->jhdr->end = phys_blksz;
jnl->jhdr->sequence_num = (jnl->jhdr->sequence_num +
(journal_size / phys_blksz) +
(random() % 16384)) & 0x00ffffff;
if (write_journal_header(jnl, 1, jnl->jhdr->sequence_num)) {
LFHFS_LOG(LEVEL_ERROR, "jnl: open: failed to update journal header size\n");
goto bad_journal;
}
}
jnl->active_start = jnl->jhdr->start;
jnl->sequence_num = jnl->jhdr->sequence_num;
size_up_tbuffer(jnl, tbuffer_size, phys_blksz);
if ((off_t)(jnl->jhdr->blhdr_size/sizeof(block_info)-1) > (jnl->jhdr->size/jnl->jhdr->jhdr_size)) {
LFHFS_LOG(LEVEL_ERROR, "jnl: open: jhdr size and blhdr size are not compatible (0x%llx, %d, %d)\n", jnl->jhdr->size,
jnl->jhdr->blhdr_size, jnl->jhdr->jhdr_size);
goto bad_journal;
}
lf_lck_mtx_init(&jnl->jlock);
lf_lck_mtx_init(&jnl->flock);
lf_lck_rw_init(&jnl->trim_lock);
goto journal_open_complete;
bad_journal:
hfs_free(jnl->header_buf);
hfs_free(jnl);
cleanup_jdev_name:
jnl = NULL;
journal_open_complete:
return jnl;
}
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,
struct mount *fsmount) {
journal *jnl;
uint32_t phys_blksz, new_txn_base;
u_int32_t min_size;
if (ioctl(jvp->psFSRecord->iFD, DKIOCGETBLOCKSIZE, (caddr_t)&phys_blksz)) {
goto cleanup_jdev_name;
}
if (journal_size < (256*1024) || journal_size > (MAX_JOURNAL_SIZE)) {
LFHFS_LOG(LEVEL_ERROR, "jnl: create: journal size %lld looks bogus.\n", journal_size);
goto cleanup_jdev_name;
}
min_size = phys_blksz * (phys_blksz / sizeof(block_info));
if (journal_size < min_size) {
LFHFS_LOG(LEVEL_ERROR, "jnl: create: journal size (%lld) too small given sector size of (%u)\n",
journal_size, phys_blksz);
goto cleanup_jdev_name;
}
if (phys_blksz > min_fs_blksz) {
LFHFS_LOG(LEVEL_ERROR, "jnl: create: error: phys blksize %u bigger than min fs blksize %zd\n",
phys_blksz, min_fs_blksz);
goto cleanup_jdev_name;
}
if ((journal_size % phys_blksz) != 0) {
LFHFS_LOG(LEVEL_ERROR, "jnl: create: journal size 0x%llx is not an even multiple of block size 0x%ux\n",
journal_size, phys_blksz);
goto cleanup_jdev_name;
}
jnl = hfs_mallocz(sizeof(struct journal));
jnl->jdev = jvp;
jnl->jdev_offset = offset;
jnl->jdev_blknum = (uint32_t)(offset / min_fs_blksz);
jnl->fsdev = fsvp;
jnl->flush = flush;
jnl->flush_arg = arg;
jnl->flags = (flags & JOURNAL_OPTION_FLAGS_MASK);
lf_lck_mtx_init(&jnl->old_start_lock);
jnl->fsmount = fsmount;
get_io_info(jvp, phys_blksz, jnl);
jnl->header_buf = hfs_malloc(phys_blksz);
jnl->header_buf_size = phys_blksz;
jnl->jhdr = (journal_header *)jnl->header_buf;
memset(jnl->jhdr, 0, sizeof(journal_header));
jnl->jhdr->jhdr_size = phys_blksz;
if ( read_journal_header(jnl, jnl->jhdr, phys_blksz) == phys_blksz
&& jnl->jhdr->magic == JOURNAL_HEADER_MAGIC
&& jnl->jhdr->sequence_num != 0) {
new_txn_base = (jnl->jhdr->sequence_num + (journal_size / phys_blksz) + (random() % 16384)) & 0x00ffffff;
LFHFS_LOG(LEVEL_ERROR, "jnl: create: avoiding old sequence number 0x%x (0x%x)\n", jnl->jhdr->sequence_num, new_txn_base);
} else {
new_txn_base = random() & 0x00ffffff;
}
memset(jnl->header_buf, 0, phys_blksz);
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;
jnl->jhdr->sequence_num = new_txn_base;
lf_lck_mtx_init(&jnl->jlock);
lf_lck_mtx_init(&jnl->flock);
lf_lck_rw_init(&jnl->trim_lock);
lf_cond_init(&jnl->flushing.sCond);
lf_cond_init(&jnl->asyncIO.sCond);
lf_cond_init(&jnl->writing_header.sCond);
jnl->flush_aborted = FALSE;
jnl->async_trim = NULL;
jnl->sequence_num = jnl->jhdr->sequence_num;
if (write_journal_header(jnl, 1, jnl->jhdr->sequence_num) != 0) {
LFHFS_LOG(LEVEL_ERROR, "jnl: journal_create: failed to write journal header.\n");
goto bad_write;
}
goto journal_create_complete;
bad_write:
hfs_free(jnl->header_buf);
jnl->jhdr = NULL;
hfs_free(jnl);
cleanup_jdev_name:
jnl = NULL;
journal_create_complete:
return jnl;
}
void *journal_owner(journal *jnl) {
return jnl->owner;
}
_Bool hfs_is_journal_file(struct hfsmount *hfsmp, struct cnode *cp) {
if (hfsmp->jnl != NULL &&
(cp->c_fileid == hfsmp->hfs_jnlinfoblkid ||
cp->c_fileid == hfsmp->hfs_jnlfileid)) {
return true;
} else {
return false;
}
}
bool is_journaled(UVFSFileNode *psRootNode) {
struct vnode *psRootVnode = *psRootNode;
if (!psRootNode) {
LFHFS_LOG(LEVEL_DEBUG, "is_journaled: psRootNode is NULL");
return false;
}
if (!psRootVnode->sFSParams.vnfs_mp) {
LFHFS_LOG(LEVEL_DEBUG, "is_journaled: psRootVnode->sFSParams.vnfs_mp is NULL");
return false;
}
if (psRootVnode->sFSParams.vnfs_mp->psHfsmount->jnl)
return true;
return false;
}
void journal_release(journal *jnl) {
if (jnl->owner != pthread_self()) {
journal_lock(jnl);
}
if (jnl->active_tr) {
abort_transaction(jnl, jnl->active_tr);
}
if (jnl->cur_tr) {
abort_transaction(jnl, jnl->cur_tr);
}
free_old_stuff(jnl);
hfs_free(jnl->header_buf);
jnl->jhdr = (void *)0xbeefbabe;
journal_unlock(jnl);
lf_lck_mtx_destroy(&jnl->old_start_lock);
lf_lck_mtx_destroy(&jnl->jlock);
lf_lck_mtx_destroy(&jnl->flock);
hfs_free(jnl);
}
void journal_close(journal *jnl) {
volatile off_t *start, *end;
int counter=0;
CHECK_JOURNAL(jnl);
jnl->flags |= JOURNAL_CLOSE_PENDING;
if (jnl->owner != pthread_self()) {
journal_lock(jnl);
}
wait_condition(jnl, &jnl->flushing, "journal_close");
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, NULL, NULL, FALSE);
}
wait_condition(jnl, &jnl->flushing, "journal_close");
start = &jnl->active_start;
end = &jnl->jhdr->end;
while (*start != *end && counter++ < 5000) {
if (jnl->flush) {
jnl->flush(jnl->flush_arg);
}
usleep(10000);
}
if (*start != *end) {
LFHFS_LOG(LEVEL_ERROR, "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, 1, jnl->sequence_num);
} else {
LFHFS_LOG(LEVEL_ERROR, "jnl: close: journal is invalid. aborting outstanding transactions\n");
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 @ %p had both an active and cur tr\n", jnl);
}
}
}
wait_condition(jnl, &jnl->asyncIO, "journal_close");
free_old_stuff(jnl);
hfs_free(jnl->header_buf);
jnl->jhdr = (void *)0xbeefbabe;
journal_unlock(jnl);
lf_lck_mtx_destroy(&jnl->old_start_lock);
lf_lck_mtx_destroy(&jnl->jlock);
lf_lck_mtx_destroy(&jnl->flock);
hfs_free(jnl);
}
int journal_start_transaction(journal *jnl) {
int ret;
#if JOURNAL_DEBUG
printf("journal_start_transaction (%u).\n", jnl->nested_count);
#endif
CHECK_JOURNAL(jnl);
free_old_stuff(jnl);
if (jnl->flags & JOURNAL_INVALID) {
return EINVAL;
}
if (jnl->owner == pthread_self()) {
if (jnl->active_tr == NULL) {
panic("jnl: start_tr: active_tr is NULL (jnl @ %p, owner %p, current_thread %p\n",
jnl, jnl->owner, pthread_self());
}
jnl->nested_count++;
return 0;
}
journal_lock(jnl);
if (jnl->nested_count != 0 || jnl->active_tr != NULL) {
panic("jnl: start_tr: owner %p, nested count %d, active_tr %p jnl @ %p\n",
jnl->owner, jnl->nested_count, jnl->active_tr, jnl);
}
jnl->nested_count = 1;
if (jnl->cur_tr) {
jnl->active_tr = jnl->cur_tr;
jnl->cur_tr = NULL;
return 0;
}
ret = journal_allocate_transaction(jnl);
if (ret) {
goto bad_start;
}
return 0;
bad_start:
jnl->nested_count = 0;
journal_unlock(jnl);
return ret;
}
int journal_end_transaction(journal *jnl) {
int ret;
transaction *tr;
#if JOURNAL_DEBUG
printf("journal_end_transaction (%u).\n", jnl->nested_count-1);
#endif
CHECK_JOURNAL(jnl);
free_old_stuff(jnl);
if ((jnl->flags & JOURNAL_INVALID) && jnl->owner == NULL) {
return 0;
}
if (jnl->owner != pthread_self()) {
panic("jnl: end_tr: I'm not the owner! jnl %p, owner %p, curact %p\n",
jnl, jnl->owner, pthread_self());
}
jnl->nested_count--;
if (jnl->nested_count > 0) {
return 0;
} else if (jnl->nested_count < 0) {
panic("jnl: jnl @ %p has negative nested count (%d). bad boy.\n", jnl, jnl->nested_count);
}
if (jnl->flags & JOURNAL_INVALID) {
if (jnl->active_tr) {
if (jnl->cur_tr != NULL) {
panic("jnl: journal @ %p has active tr (%p) and cur tr (%p)\n",
jnl, jnl->active_tr, jnl->cur_tr);
}
tr = jnl->active_tr;
jnl->active_tr = NULL;
abort_transaction(jnl, tr);
}
journal_unlock(jnl);
return EINVAL;
}
tr = jnl->active_tr;
CHECK_TRANSACTION(tr);
jnl->active_tr = NULL;
if (tr->flush_on_completion == TRUE) {
ret = end_transaction(tr, 1, NULL, NULL, TRUE);
}
else {
ret = end_transaction(tr, 0, NULL, NULL, TRUE);
}
return ret;
}
int journal_modify_block_start(journal *jnl, GenericLFBuf *psGenBuf) {
transaction *tr;
#if JOURNAL_DEBUG
printf("journal_modify_block_start: psGenBuf %p, psVnode %p, uBlockN %llu, uDataSize %u, uCacheFlags 0x%llx, uPhyCluster %llu, uLockCnt %u\n",
psGenBuf, psGenBuf->psVnode, psGenBuf->uBlockN, psGenBuf->uDataSize, psGenBuf->uCacheFlags ,psGenBuf->uPhyCluster, psGenBuf->uLockCnt);
#endif
CHECK_JOURNAL(jnl);
free_old_stuff(jnl);
if (jnl->flags & JOURNAL_INVALID) {
return EINVAL;
}
tr = jnl->active_tr;
CHECK_TRANSACTION(tr);
if (jnl->owner != pthread_self()) {
panic("jnl: modify_block_start: called w/out a transaction! jnl %p, owner %p, curact %p\n",
jnl, jnl->owner, pthread_self());
}
if ((psGenBuf->uDataSize % jnl->jhdr->jhdr_size) != 0) {
uint32_t bad=0;
uint32_t phys_blksz;
if (ioctl(jnl->jdev->psFSRecord->iFD, DKIOCGETBLOCKSIZE, (caddr_t)&phys_blksz)) {
bad = 1;
} else if (phys_blksz != (uint32_t)jnl->jhdr->jhdr_size) {
if (phys_blksz < 512) {
panic("jnl: mod block start: phys blksz %d is too small (%d, %d)\n",
phys_blksz, psGenBuf->uDataSize, jnl->jhdr->jhdr_size);
}
if ((psGenBuf->uDataSize % phys_blksz) != 0) {
bad = 1;
} else if (phys_blksz < (uint32_t)jnl->jhdr->jhdr_size) {
jnl->jhdr->jhdr_size = phys_blksz;
} else {
char *new_header_buf;
LFHFS_LOG(LEVEL_ERROR, "jnl: phys blksz got bigger (was: %d/%d now %d)\n",
jnl->header_buf_size, jnl->jhdr->jhdr_size, phys_blksz);
new_header_buf = hfs_malloc(phys_blksz);
memcpy(new_header_buf, jnl->header_buf, jnl->header_buf_size);
memset(&new_header_buf[jnl->header_buf_size], 0x18, (phys_blksz - jnl->header_buf_size));
hfs_free(jnl->header_buf);
jnl->header_buf = new_header_buf;
jnl->header_buf_size = phys_blksz;
jnl->jhdr = (journal_header *)jnl->header_buf;
jnl->jhdr->jhdr_size = phys_blksz;
}
} else {
bad = 1;
}
if (bad) {
panic("jnl: mod block start: bufsize %d not a multiple of block size %d\n",
psGenBuf->uDataSize, jnl->jhdr->jhdr_size);
return -1;
}
}
if ((tr->total_bytes+psGenBuf->uDataSize) >= (size_t)(jnl->jhdr->size - jnl->jhdr->jhdr_size)) {
panic("jnl: transaction too big (%d >= %lld bytes, bufsize %d, tr %p bp %p)\n",
tr->total_bytes, (tr->jnl->jhdr->size - jnl->jhdr->jhdr_size), psGenBuf->uDataSize, tr, psGenBuf->pvData);
return -1;
}
lf_hfs_generic_buf_set_cache_flag(psGenBuf, GEN_BUF_WRITE_LOCK);
return 0;
}
int journal_modify_block_end(journal *jnl, GenericLFBuf *psGenBuf,
void (*func)(GenericLFBuf *bp, void *arg), void *arg) {
int i = 1;
size_t tbuffer_offset=0;
block_list_header *blhdr, *prev=NULL;
transaction *tr = NULL;
#if JOURNAL_DEBUG
printf("journal_modify_block_end: psGenBuf %p, psVnode %p, uBlockN %llu, uDataSize %u, uPhyCluster %llu uLockCnt %u\n",
psGenBuf, psGenBuf->psVnode, psGenBuf->uBlockN, psGenBuf->uDataSize, psGenBuf->uPhyCluster, psGenBuf->uLockCnt);
#endif
CHECK_JOURNAL(jnl);
free_old_stuff(jnl);
if (func) {
psGenBuf->pfFunc = func;
psGenBuf->pvCallbackArgs = arg;
}
if (jnl->flags & JOURNAL_INVALID) {
lf_hfs_generic_buf_clear_cache_flag(psGenBuf, GEN_BUF_WRITE_LOCK);
lf_hfs_generic_buf_release(psGenBuf);
return EINVAL;
}
tr = jnl->active_tr;
CHECK_TRANSACTION(tr);
if (jnl->owner != pthread_self()) {
panic("jnl: modify_block_end: called w/out a transaction! jnl %p, owner %p, curact %p\n",
jnl, jnl->owner, pthread_self());
}
if ((psGenBuf->uCacheFlags & GEN_BUF_WRITE_LOCK) == 0) {
panic("jnl: modify_block_end: bp %p not locked! jnl @ %p\n", psGenBuf, jnl);
}
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++) {
GenericLFBuf *bp = (void*)blhdr->binfo[i].u.bp;
if (psGenBuf == bp) {
#if JOURNAL_DEBUG
printf("block_end, already in journal: psGenBuf %p, psVnode %p, uBlockN %llu, uDataSize %u, uPhyCluster %llu uLockCnt %u\n",
psGenBuf, psGenBuf->psVnode, psGenBuf->uBlockN, psGenBuf->uDataSize, psGenBuf->uPhyCluster, psGenBuf->uLockCnt);
#endif
break;
}
if (blhdr->binfo[i].bnum != (off_t)-1) {
off_t uSizeOfBuf = ((GenericLFBuf*)(blhdr->binfo[i].u.bp))->uDataSize;
tbuffer_offset += uSizeOfBuf;
} else {
tbuffer_offset += blhdr->binfo[i].u.bi.bsize;
}
}
if (i < blhdr->num_blocks) {
break;
}
}
if (blhdr == NULL
&& prev
&& (prev->num_blocks+1) <= prev->max_blocks
&& (prev->bytes_used+psGenBuf->uDataSize) <= (uint32_t)tr->tbuffer_size) {
blhdr = prev;
} else if (blhdr == NULL) {
block_list_header *nblhdr;
if (prev == NULL) {
panic("jnl: modify block end: no way man, prev == NULL?!?, jnl %p, psGenBuf %p\n", jnl, psGenBuf);
}
nblhdr = hfs_malloc(tr->tbuffer_size);
memset(nblhdr, 0, BLHDR_CHECKSUM_SIZE);
memset(nblhdr + BLHDR_CHECKSUM_SIZE, 0x5a, jnl->jhdr->blhdr_size - BLHDR_CHECKSUM_SIZE);
nblhdr->max_blocks = (jnl->jhdr->blhdr_size / sizeof(block_info)) - 1;
nblhdr->num_blocks = 1; nblhdr->bytes_used = (uint32_t)jnl->jhdr->blhdr_size;
nblhdr->flags = BLHDR_CHECK_CHECKSUMS;
tr->num_blhdrs++;
tr->total_bytes += jnl->jhdr->blhdr_size;
prev->binfo[0].bnum = (off_t)((long)nblhdr);
blhdr = nblhdr;
i = 1;
}
if ((i+1) > blhdr->max_blocks) {
panic("jnl: modify_block_end: i = %d, max_blocks %d\n", i, blhdr->max_blocks);
}
if (i >= blhdr->num_blocks) {
off_t bsize;
bsize = psGenBuf->uDataSize;
blhdr->binfo[i].bnum = (off_t)(psGenBuf->uBlockN);
blhdr->binfo[i].u.bp = (void*)psGenBuf;
blhdr->bytes_used += bsize;
tr->total_bytes += bsize;
blhdr->num_blocks++;
}
lf_hfs_generic_buf_release(psGenBuf);
return 0;
}
int journal_flush(journal *jnl, journal_flush_options_t options) {
boolean_t drop_lock = FALSE;
errno_t error = 0;
uint32_t flush_count = 0;
CHECK_JOURNAL(jnl);
free_old_stuff(jnl);
if (jnl->flags & JOURNAL_INVALID) {
return EINVAL;
}
if (jnl->owner != pthread_self()) {
journal_lock(jnl);
drop_lock = TRUE;
}
if (ISSET(options, JOURNAL_FLUSH_FULL))
flush_count = jnl->flush_counter;
if (jnl->active_tr == NULL && jnl->cur_tr) {
transaction *tr = jnl->cur_tr;
jnl->cur_tr = NULL;
if (ISSET(options, JOURNAL_WAIT_FOR_IO)) {
wait_condition(jnl, &jnl->flushing, "journal_flush");
wait_condition(jnl, &jnl->asyncIO, "journal_flush");
}
int lockflags = hfs_systemfile_lock(jnl->fsmount->psHfsmount, SFL_CATALOG | SFL_ATTRIBUTE | SFL_EXTENTS | SFL_BITMAP, HFS_EXCLUSIVE_LOCK);
end_transaction(tr, 1, NULL, NULL, drop_lock);
hfs_systemfile_unlock(jnl->fsmount->psHfsmount, lockflags);
} else {
if (drop_lock == TRUE) {
journal_unlock(jnl);
}
wait_condition(jnl, &jnl->flushing, "journal_flush");
}
if (ISSET(options, JOURNAL_WAIT_FOR_IO)) {
wait_condition(jnl, &jnl->asyncIO, "journal_flush");
}
if (ISSET(options, JOURNAL_FLUSH_FULL)) {
dk_synchronize_t sync_request = {
.options = 0,
};
if (flush_count == jnl->flush_counter)
error = ioctl(jnl->jdev->psFSRecord->iFD, DKIOCSYNCHRONIZE, (caddr_t)&sync_request);
if (jnl->jdev != jnl->fsdev)
error = ioctl(jnl->jdev->psFSRecord->iFD, DKIOCSYNCHRONIZE, (caddr_t)&sync_request);
}
return error;
}
static int update_fs_block(journal *jnl, void *block_ptr, off_t fs_block, size_t bsize) {
int iRet = 0;
GenericLFBuf *psGenBuf = NULL;
psGenBuf = lf_hfs_generic_buf_allocate(jnl->fsmount->psHfsmount->hfs_devvp,
fs_block,
(uint32_t)bsize,
GEN_BUF_PHY_BLOCK | GEN_BUF_NON_CACHED);
if (!psGenBuf) {
LFHFS_LOG(LEVEL_ERROR, "jnl: update_fs_block: error allocating fs block # %lld!\n", fs_block);
iRet = -1;
goto exit;
}
iRet = lf_hfs_generic_buf_read(psGenBuf);
if (iRet) {
LFHFS_LOG(LEVEL_ERROR, "jnl: update_fs_block: error reading fs block # %lld!\n", fs_block);
goto exit;
}
memcpy(psGenBuf->pvData, block_ptr, bsize);
iRet = lf_hfs_generic_buf_write(psGenBuf);
if (iRet) {
LFHFS_LOG(LEVEL_ERROR, "jnl: update_fs_block: failed to write block %lld (ret %d)\n", fs_block, iRet);
goto exit;
}
exit:
if (psGenBuf) {
lf_hfs_generic_buf_release(psGenBuf);
}
return iRet;
}
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;
}
newBuf = hfs_malloc(new_size*sizeof(struct bucket));
bcopy(*buf_ptr, newBuf, num_buckets*sizeof(struct bucket));
for(i = num_buckets; i < new_size; i++) {
newBuf[i].block_num = (off_t)-1;
}
hfs_free(*buf_ptr);
*buf_ptr = newBuf;
return new_size;
}
static int insert_block(journal *jnl, struct bucket **buf_ptr, int blk_index, off_t num, size_t size, size_t offset, int32_t cksum, int *num_buckets_ptr, int *num_full_ptr, int overwriting) {
if (!overwriting) {
if (*num_full_ptr >= (*num_buckets_ptr - 1)) {
int new_size = *num_buckets_ptr * 2;
int grow_size = grow_table(buf_ptr, *num_buckets_ptr, new_size);
if (grow_size < new_size) {
LFHFS_LOG(LEVEL_ERROR, "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 ((off_t)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 (%zd)\n", size);
}
(*buf_ptr)[blk_index].block_num = num;
(*buf_ptr)[blk_index].block_size = (uint32_t)size;
(*buf_ptr)[blk_index].jnl_offset = (uint32_t)offset;
(*buf_ptr)[blk_index].cksum = cksum;
return blk_index;
}
static int do_overlap(journal *jnl, struct bucket **buf_ptr, int blk_index, off_t block_num, size_t size, __unused size_t offset, int32_t cksum, int *num_buckets_ptr, int *num_full_ptr) {
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 %zd\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;
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, cksum, 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 = (uint32_t)(block_start - prev_block_start);
(*buf_ptr)[blk_index-1].cksum = 0; }
}
if (!overwrite && block_end <= (off_t)((*buf_ptr)[blk_index].block_num*jhdr_size)) {
return 0; } else if (overwrite && (blk_index + 1 >= *num_full_ptr || block_end <= (off_t)((*buf_ptr)[blk_index+1].block_num*jhdr_size))) {
(*buf_ptr)[blk_index].cksum = cksum; return 1; }
index = blk_index;
num_to_remove = 0;
while (index < *num_full_ptr && block_end > (off_t)((*buf_ptr)[index].block_num*jhdr_size)) {
if (block_end >= (off_t)(((*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 %lld is not multiple of %zd\n", overlap, jhdr_size);
}
(*buf_ptr)[index].block_num += (overlap / jhdr_size); (*buf_ptr)[index].cksum = 0;
new_offset = (*buf_ptr)[index].jnl_offset + overlap; if ((off_t)new_offset >= jnl->jhdr->size) {
new_offset = jhdr_size + (new_offset - jnl->jhdr->size);
}
(*buf_ptr)[index].jnl_offset = (uint32_t)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 (%u)\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 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 add_block(journal *jnl, struct bucket **buf_ptr, off_t block_num, size_t size, size_t offset, int32_t cksum, int *num_buckets_ptr, int *num_full_ptr) {
int blk_index, overwriting;
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, cksum, num_buckets_ptr, num_full_ptr);
if (overwriting < 0) {
return -1; }
blk_index = insert_block(jnl, buf_ptr, blk_index, block_num, size, offset, cksum, num_buckets_ptr, num_full_ptr, overwriting);
return blk_index;
}
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->flags = SWAP32(blhdr->flags);
if (blhdr->num_blocks >= ((jnl->jhdr->blhdr_size / sizeof(block_info)) - 1)) {
LFHFS_LOG(LEVEL_ERROR, "jnl: blhdr num blocks looks suspicious (%d / blhdr size %d). not swapping.\n", blhdr->num_blocks, jnl->jhdr->blhdr_size);
return;
}
for(i = 0; i < blhdr->num_blocks; i++) {
blhdr->binfo[i].bnum = SWAP64(blhdr->binfo[i].bnum);
blhdr->binfo[i].u.bi.bsize = SWAP32(blhdr->binfo[i].u.bi.bsize);
blhdr->binfo[i].u.bi.b.cksum = SWAP32(blhdr->binfo[i].u.bi.b.cksum);
}
}
static int replay_journal(journal *jnl) {
int i, bad_blocks=0;
unsigned int orig_checksum, checksum, check_block_checksums = 0;
size_t ret;
size_t max_bsize = 0;
block_list_header *blhdr;
off_t offset, txn_start_offset=0, blhdr_offset, orig_jnl_start;
char *buff, *block_ptr=NULL;
struct bucket *co_buf;
int num_buckets = STARTING_BUCKETS, num_full, check_past_jnl_end = 1, in_uncharted_territory = 0;
uint32_t last_sequence_num = 0;
int replay_retry_count = 0;
LFHFS_LOG(LEVEL_DEFAULT, "replay_journal: start.\n");
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) {
LFHFS_LOG(LEVEL_DEFAULT, "replay_journal: journal empty.\n");
goto success;
}
orig_jnl_start = jnl->jhdr->start;
buff = hfs_malloc(jnl->jhdr->blhdr_size);
co_buf = hfs_malloc(num_buckets*sizeof(struct bucket));
restart_replay:
for(i = 0; i < num_buckets; i++) {
co_buf[i].block_num = -1;
}
num_full = 0;
while (check_past_jnl_end || jnl->jhdr->start != jnl->jhdr->end) {
offset = blhdr_offset = jnl->jhdr->start;
ret = read_journal_data(jnl, &offset, buff, jnl->jhdr->blhdr_size);
if (ret != (size_t)jnl->jhdr->blhdr_size) {
LFHFS_LOG(LEVEL_ERROR, "jnl: replay_journal: Could not read block list header block @ 0x%llx!\n", offset);
goto bad_txn_handling;
}
blhdr = (block_list_header *)buff;
orig_checksum = blhdr->checksum;
blhdr->checksum = 0;
if (jnl->flags & JOURNAL_NEED_SWAP) {
orig_checksum = (unsigned int)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) {
if (check_past_jnl_end && in_uncharted_territory) {
if (blhdr_offset != jnl->jhdr->end) {
LFHFS_LOG(LEVEL_ERROR, "jnl: Extra txn replay stopped @ %lld / 0x%llx\n", blhdr_offset, blhdr_offset);
}
check_past_jnl_end = 0;
jnl->jhdr->end = blhdr_offset;
continue;
}
LFHFS_LOG(LEVEL_ERROR, "jnl: replay_journal: bad block list header @ 0x%llx (checksum 0x%x != 0x%x)\n",
blhdr_offset, orig_checksum, checksum);
if (blhdr_offset == orig_jnl_start) {
goto bad_replay;
}
goto bad_txn_handling;
}
if ( (last_sequence_num != 0)
&& (blhdr->binfo[0].u.bi.b.sequence_num != 0)
&& (blhdr->binfo[0].u.bi.b.sequence_num != last_sequence_num)
&& (blhdr->binfo[0].u.bi.b.sequence_num != last_sequence_num+1)) {
txn_start_offset = jnl->jhdr->end = blhdr_offset;
if (check_past_jnl_end) {
check_past_jnl_end = 0;
LFHFS_LOG(LEVEL_ERROR, "jnl: 2: extra replay stopped @ %lld / 0x%llx (seq %d < %d)\n",
blhdr_offset, blhdr_offset, blhdr->binfo[0].u.bi.b.sequence_num, last_sequence_num);
continue;
}
LFHFS_LOG(LEVEL_ERROR, "jnl: txn sequence numbers out of order in txn @ %lld / %llx! (%d < %d)\n",
blhdr_offset, blhdr_offset, blhdr->binfo[0].u.bi.b.sequence_num, last_sequence_num);
goto bad_txn_handling;
}
last_sequence_num = blhdr->binfo[0].u.bi.b.sequence_num;
if (blhdr_offset >= jnl->jhdr->end && jnl->jhdr->start <= jnl->jhdr->end) {
if (last_sequence_num == 0) {
check_past_jnl_end = 0;
LFHFS_LOG(LEVEL_ERROR, "jnl: pre-sequence-num-enabled txn's - can not go further than end (%lld %lld).\n",
jnl->jhdr->start, jnl->jhdr->end);
if (jnl->jhdr->start != jnl->jhdr->end) {
jnl->jhdr->start = jnl->jhdr->end;
}
continue;
}
LFHFS_LOG(LEVEL_ERROR, "jnl: examining extra transactions starting @ %lld / 0x%llx\n", blhdr_offset, blhdr_offset);
}
if ( blhdr->max_blocks <= 0 || blhdr->max_blocks > (jnl->jhdr->size/jnl->jhdr->jhdr_size)
|| blhdr->num_blocks <= 0 || blhdr->num_blocks > blhdr->max_blocks) {
LFHFS_LOG(LEVEL_ERROR, "jnl: replay_journal: bad looking journal entry: max: %d num: %d\n",
blhdr->max_blocks, blhdr->num_blocks);
goto bad_txn_handling;
}
max_bsize = 0;
for (i = 1; i < blhdr->num_blocks; i++) {
if (blhdr->binfo[i].bnum < 0 && blhdr->binfo[i].bnum != (off_t)-1) {
LFHFS_LOG(LEVEL_ERROR, "jnl: replay_journal: bogus block number 0x%llx\n", blhdr->binfo[i].bnum);
goto bad_txn_handling;
}
if ((size_t)blhdr->binfo[i].u.bi.bsize > max_bsize) {
max_bsize = blhdr->binfo[i].u.bi.bsize;
}
}
if (blhdr->flags & BLHDR_CHECK_CHECKSUMS) {
check_block_checksums = 1;
block_ptr = hfs_malloc(max_bsize);
} else {
block_ptr = NULL;
}
if (blhdr->flags & BLHDR_FIRST_HEADER) {
txn_start_offset = blhdr_offset;
}
bad_blocks = 0;
for (i = 1; i < blhdr->num_blocks; i++) {
int size, ret_val;
off_t number;
size = blhdr->binfo[i].u.bi.bsize;
number = blhdr->binfo[i].bnum;
if (number == (off_t)-1) {
} else {
if (check_block_checksums) {
int32_t disk_cksum;
off_t block_offset;
block_offset = offset;
ret = read_journal_data(jnl, &block_offset, block_ptr, size);
if (ret != (size_t)size) {
LFHFS_LOG(LEVEL_ERROR, "jnl: replay_journal: Could not read journal entry data @ offset 0x%llx!\n", offset);
goto bad_txn_handling;
}
disk_cksum = calc_checksum(block_ptr, size);
if (blhdr->binfo[i].u.bi.b.cksum != 0 && disk_cksum != blhdr->binfo[i].u.bi.b.cksum) {
LFHFS_LOG(LEVEL_ERROR, "jnl: txn starting at %lld (%lld) @ index %3d bnum %lld (%d) with disk cksum != blhdr cksum (0x%.8x 0x%.8x)\n",
txn_start_offset, blhdr_offset, i, number, size, disk_cksum, blhdr->binfo[i].u.bi.b.cksum);
LFHFS_LOG(LEVEL_ERROR, "jnl: 0x%.8x 0x%.8x 0x%.8x 0x%.8x 0x%.8x 0x%.8x 0x%.8x 0x%.8x\n",
*(int *)&block_ptr[0*sizeof(int)], *(int *)&block_ptr[1*sizeof(int)], *(int *)&block_ptr[2*sizeof(int)], *(int *)&block_ptr[3*sizeof(int)],
*(int *)&block_ptr[4*sizeof(int)], *(int *)&block_ptr[5*sizeof(int)], *(int *)&block_ptr[6*sizeof(int)], *(int *)&block_ptr[7*sizeof(int)]);
goto bad_txn_handling;
}
}
ret_val = add_block(jnl, &co_buf, number, size, (size_t) offset, blhdr->binfo[i].u.bi.b.cksum, &num_buckets, &num_full);
if (ret_val == -1) {
LFHFS_LOG(LEVEL_ERROR, "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);
}
}
if (block_ptr) {
hfs_free(block_ptr);
block_ptr = NULL;
}
if (bad_blocks) {
bad_txn_handling:
if (txn_start_offset == 0) {
LFHFS_LOG(LEVEL_ERROR, "jnl: no known good txn start offset! aborting journal replay.\n");
goto bad_replay;
}
if (replay_retry_count == 3) {
LFHFS_LOG(LEVEL_ERROR, "jnl: repeated errors replaying journal! aborting journal replay.\n");
goto bad_replay;
}
replay_retry_count++;
jnl->jhdr->start = orig_jnl_start;
jnl->jhdr->end = txn_start_offset;
check_past_jnl_end = 0;
last_sequence_num = 0;
LFHFS_LOG(LEVEL_ERROR, "jnl: restarting journal replay (%lld - %lld)!\n", jnl->jhdr->start, jnl->jhdr->end);
goto restart_replay;
}
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;
}
if (jnl->jhdr->start == jnl->jhdr->end) {
in_uncharted_territory = 1;
}
}
if (jnl->jhdr->start != jnl->jhdr->end) {
LFHFS_LOG(LEVEL_ERROR, "jnl: start %lld != end %lld. resetting end.\n", jnl->jhdr->start, jnl->jhdr->end);
jnl->jhdr->end = jnl->jhdr->start;
}
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);
}
block_ptr = hfs_malloc(max_bsize);
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) {
LFHFS_LOG(LEVEL_ERROR, "jnl: replay_journal: Could not read journal entry data @ offset 0x%llx!\n", jnl_offset);
goto bad_replay;
}
if (update_fs_block(jnl, block_ptr, number, size) != 0) {
goto bad_replay;
}
}
}
if (write_journal_header(jnl, 1, jnl->jhdr->sequence_num) != 0) {
goto bad_replay;
}
if (block_ptr) {
hfs_free(block_ptr);
block_ptr = NULL;
}
hfs_free(co_buf);
co_buf = NULL;
hfs_free(buff);
success:
LFHFS_LOG(LEVEL_DEFAULT, "replay_journal: success.\n");
return 0;
bad_replay:
hfs_free(block_ptr);
hfs_free(co_buf);
hfs_free(buff);
LFHFS_LOG(LEVEL_ERROR, "replay_journal: error.\n");
return -1;
}
static void buffer_written(transaction *tr, GenericLFBuf *bp) {
journal *jnl;
transaction *ctr, *prev=NULL, *next;
size_t i;
size_t bufsize, amt_flushed, total_bytes;
bufsize = bp->uDataSize;
if (tr == NULL) {
return;
}
CHECK_TRANSACTION(tr);
jnl = tr->jnl;
CHECK_JOURNAL(jnl);
amt_flushed = tr->num_killed;
total_bytes = tr->total_bytes;
amt_flushed += tr->num_flushed;
tr->num_flushed += bufsize;
if ((amt_flushed + bufsize) < total_bytes) {
return;
}
lock_oldstart(jnl);
if (tr->total_bytes == (int)0xfbadc0de) {
unlock_oldstart(jnl);
return;
}
tr->total_bytes = 0xfbadc0de;
if (jnl->flags & JOURNAL_INVALID)
goto transaction_done;
for(i = 0; i < sizeof(jnl->old_start)/sizeof(jnl->old_start[0]); i++) {
if ((off_t)(jnl->old_start[i] & ~(0x8000000000000000ULL)) == tr->journal_start) {
jnl->old_start[i] &= ~(0x8000000000000000ULL);
break;
}
}
if (i >= sizeof(jnl->old_start)/sizeof(jnl->old_start[0])) {
panic("jnl: buffer_flushed: did not find tr w/start @ %lld (tr %p, jnl %p)\n",
tr->journal_start, tr, jnl);
}
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 if (ctr->next && ctr->journal_end == ctr->next->journal_start) {
next = ctr->next; ctr->journal_end = next->journal_end;
ctr->next = next->next;
next->next = jnl->tr_freeme; jnl->tr_freeme = next;
next = jnl->completed_trs; ctr = NULL;
} else {
next = ctr->next;
}
}
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;
}
transaction_done:
unlock_oldstart(jnl);
unlock_condition(jnl, &jnl->asyncIO);
}
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 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 void size_up_tbuffer(journal *jnl, uint32_t tbuffer_size, uint32_t phys_blksz) {
if (def_tbuffer_size == 0) {
uint64_t memsize = 0;
size_t l = sizeof(memsize);
sysctlbyname("hw.memsize", &memsize, &l, NULL, 0);
if (memsize < (256*1024*1024)) {
def_tbuffer_size = DEFAULT_TRANSACTION_BUFFER_SIZE;
} else if (memsize < (512*1024*1024)) {
def_tbuffer_size = DEFAULT_TRANSACTION_BUFFER_SIZE * 2;
} else if (memsize < (1024*1024*1024)) {
def_tbuffer_size = DEFAULT_TRANSACTION_BUFFER_SIZE * 3;
} else {
def_tbuffer_size = (uint32_t)(DEFAULT_TRANSACTION_BUFFER_SIZE * (memsize / (256*1024*1024)));
}
}
if (!(jnl->jhdr->jhdr_size > 0)) {
panic("jnl->jhdr->jhdr_size is %d", jnl->jhdr->jhdr_size);
}
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 = (uint32_t)(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);
}
}
static int write_journal_header(journal *jnl, int updating_start, uint32_t sequence_num) {
static int num_err_prints = 0;
int ret=0;
off_t jhdr_offset = 0;
if (!updating_start && (jnl->flags & JOURNAL_DO_FUA_WRITES) == 0) {
dk_synchronize_t sync_request = {
.options = DK_SYNCHRONIZE_OPTION_BARRIER,
};
if (!(jnl->flags & JOURNAL_FEATURE_BARRIER) || (jnl->jdev != jnl->fsdev)) {
sync_request.options = 0;
jnl->flush_counter++;
}
ret = ioctl(jnl->jdev->psFSRecord->iFD, DKIOCSYNCHRONIZE, (caddr_t)&sync_request);
}
if (ret != 0) {
if ( ret != jnl->last_flush_err
|| (jnl->flags & JOURNAL_FLUSHCACHE_ERR) == 0
|| num_err_prints++ < 25) {
LFHFS_LOG(LEVEL_ERROR, "jnl: flushing fs disk buffer returned 0x%x\n", ret);
jnl->flags |= JOURNAL_FLUSHCACHE_ERR;
jnl->last_flush_err = ret;
}
}
jnl->jhdr->sequence_num = sequence_num;
jnl->jhdr->checksum = 0;
jnl->jhdr->checksum = calc_checksum((char *)jnl->jhdr, JOURNAL_HEADER_CKSUM_SIZE);
if (do_journal_io(jnl, &jhdr_offset, jnl->header_buf, jnl->jhdr->jhdr_size, JNL_WRITE|JNL_HEADER) != (size_t)jnl->jhdr->jhdr_size) {
LFHFS_LOG(LEVEL_ERROR, "jnl: write_journal_header: error writing the journal header!\n");
jnl->flags |= JOURNAL_INVALID;
return -1;
}
if (updating_start && (jnl->flags & JOURNAL_DO_FUA_WRITES) == 0) {
dk_synchronize_t sync_request = {
.options = DK_SYNCHRONIZE_OPTION_BARRIER,
};
if (!(jnl->flags & JOURNAL_FEATURE_BARRIER) || (jnl->jdev != jnl->fsdev)) {
sync_request.options = 0;
jnl->flush_counter++;
}
ioctl(jnl->jdev->psFSRecord->iFD, DKIOCSYNCHRONIZE, (caddr_t)&sync_request);
}
return 0;
}
static int journal_binfo_cmp(const void *a, const void *b) {
const block_info *bi_a = (const struct block_info *)a;
const block_info *bi_b = (const struct block_info *)b;
daddr64_t res;
if (bi_a->bnum == (off_t)-1) {
return 1;
}
if (bi_b->bnum == (off_t)-1) {
return -1;
}
GenericLFBuf *psGenBufA, *psGenBufB;
psGenBufA = (void*)bi_a->u.bp;
psGenBufB = (void*)bi_b->u.bp;
res = psGenBufA->uBlockN - psGenBufB->uBlockN;
return (int)res;
}
static int finish_end_transaction(transaction *tr, errno_t (*callback)(void*), void *callback_arg) {
int i;
size_t amt;
size_t ret = 0;
off_t end;
journal *jnl = tr->jnl;
GenericLFBuf *bp = NULL, **bparray = NULL;
block_list_header *blhdr=NULL, *next=NULL;
size_t tbuffer_offset;
int bufs_written = 0;
int ret_val = 0;
end = jnl->jhdr->end;
for (blhdr = tr->blhdr; blhdr; blhdr = (block_list_header *)((long)blhdr->binfo[0].bnum)) {
amt = blhdr->bytes_used;
blhdr->binfo[0].u.bi.b.sequence_num = tr->sequence_num;
blhdr->checksum = 0;
blhdr->checksum = calc_checksum((char *)blhdr, BLHDR_CHECKSUM_SIZE);
bparray = hfs_malloc(blhdr->num_blocks * sizeof(buf_t));
tbuffer_offset = jnl->jhdr->blhdr_size;
for (i = 1; i < blhdr->num_blocks; i++) {
size_t bsize;
if (blhdr->binfo[i].bnum != (off_t)-1) {
daddr64_t blkno;
bp = (void*)blhdr->binfo[i].u.bp;
blkno = bp->uPhyCluster;
blhdr->binfo[i].bnum = (off_t)(blkno);
bparray[i] = bp;
bsize = bp->uDataSize;
blhdr->binfo[i].u.bi.bsize = (uint32_t)bsize;
blhdr->binfo[i].u.bi.b.cksum = calc_checksum(&((char *)blhdr)[tbuffer_offset], (uint32_t)bsize);
} else {
bparray[i] = NULL;
bsize = blhdr->binfo[i].u.bi.bsize;
blhdr->binfo[i].u.bi.b.cksum = 0;
}
tbuffer_offset += bsize;
}
wait_condition(jnl, &jnl->writing_header, "finish_end_transaction");
if (jnl->write_header_failed == FALSE)
ret = write_journal_data(jnl, &end, blhdr, amt);
else
ret_val = -1;
#if HFS_CRASH_TEST
CRASH_ABORT(CRASH_ABORT_JOURNAL_AFTER_JOURNAL_DATA, jnl->fsmount->psHfsmount, NULL);
#endif
for (i = 1; i < blhdr->num_blocks; i++)
blhdr->binfo[i].u.bp = (void*)bparray[i];
hfs_free(bparray);
if (ret_val == -1)
goto bad_journal;
if (ret != amt) {
LFHFS_LOG(LEVEL_ERROR, "jnl: end_transaction: only wrote %zu of %zu bytes to the journal!\n",
ret, amt);
ret_val = -1;
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, jnl->saved_sequence_num) != 0) {
ret_val = -1;
goto bad_journal;
}
#if HFS_CRASH_TEST
CRASH_ABORT(CRASH_ABORT_JOURNAL_AFTER_JOURNAL_HEADER, jnl->fsmount->psHfsmount, NULL);
#endif
if (callback != NULL && callback(callback_arg) != 0) {
ret_val = -1;
goto bad_journal;
}
tr->num_flushed = tr->num_blhdrs * jnl->jhdr->blhdr_size;
lock_condition(jnl, &jnl->asyncIO, "finish_end_transaction");
for (blhdr = tr->blhdr; blhdr; blhdr = next) {
uint16_t num_blocks;
next = (block_list_header *)((long)blhdr->binfo[0].bnum);
num_blocks = blhdr->num_blocks;
qsort(&blhdr->binfo[1], num_blocks-1, sizeof(block_info), journal_binfo_cmp);
for (i = num_blocks-1; i >= 1; i--) {
if (blhdr->binfo[i].bnum != (off_t)-1)
break;
num_blocks--;
}
for (i = 1; i < num_blocks; i++) {
if ((bp = (void*)blhdr->binfo[i].u.bp)) {
errno_t ret_val = 0;
#if JOURNAL_DEBUG
printf("journal write physical: bp %p, psVnode %p, uBlockN %llu, uPhyCluster %llu uLockCnt %u\n",
bp, bp->psVnode, bp->uBlockN, bp->uPhyCluster, bp->uLockCnt);
#endif
lf_hfs_generic_buf_clear_cache_flag(bp, GEN_BUF_WRITE_LOCK);
ret_val = lf_hfs_generic_buf_write(bp);
#if HFS_CRASH_TEST
CRASH_ABORT(CRASH_ABORT_JOURNAL_IN_BLOCK_DATA, jnl->fsmount->psHfsmount, NULL);
#endif
if (ret_val) {
LFHFS_LOG(LEVEL_ERROR, "jnl: raw_readwrite_write_mount inside finish_end_transaction returned %d.\n", ret_val);
}
buffer_written(tr, bp);
lf_hfs_generic_buf_unlock(bp);
lf_hfs_generic_buf_release(bp);
bufs_written++;
}
}
}
#if HFS_CRASH_TEST
CRASH_ABORT(CRASH_ABORT_JOURNAL_AFTER_BLOCK_DATA, jnl->fsmount->psHfsmount, NULL);
#endif
if (bufs_written == 0) {
lock_oldstart(jnl);
tr->next = jnl->tr_freeme;
jnl->tr_freeme = tr;
unlock_oldstart(jnl);
unlock_condition(jnl, &jnl->asyncIO);
}
bad_journal:
if (ret_val == -1) {
abort_transaction(jnl, tr);
jnl->flush_aborted = TRUE;
unlock_condition(jnl, &jnl->flushing);
journal_lock(jnl);
jnl->flags |= JOURNAL_INVALID;
jnl->old_start[sizeof(jnl->old_start)/sizeof(jnl->old_start[0]) - 1] &= ~0x8000000000000000LL;
journal_unlock(jnl);
} else
unlock_condition(jnl, &jnl->flushing);
return (ret_val);
}
static off_t free_space(journal *jnl) {
off_t free_space_offset;
if (jnl->jhdr->start < jnl->jhdr->end) {
free_space_offset = jnl->jhdr->size - (jnl->jhdr->end - jnl->jhdr->start) - jnl->jhdr->jhdr_size;
} else if (jnl->jhdr->start > jnl->jhdr->end) {
free_space_offset = jnl->jhdr->start - jnl->jhdr->end;
} else {
free_space_offset = jnl->jhdr->size - jnl->jhdr->jhdr_size;
}
return free_space_offset;
}
static void dump_journal(journal *jnl) {
transaction *ctr;
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 int check_free_space( journal *jnl,
int desired_size,
boolean_t *delayed_header_write,
uint32_t sequence_num) {
size_t i;
int counter=0;
if (delayed_header_write)
*delayed_header_write = FALSE;
while (1) {
int old_start_empty;
if (free_space(jnl) > desired_size && jnl->old_start[0] == 0) {
break;
}
if (counter++ == 5000) {
dump_journal(jnl);
panic("jnl: check_free_space: buffer flushing isn't working "
"(jnl @ %p 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) {
return ENOSPC;
}
old_start_empty = 1;
lock_oldstart(jnl);
for (i = 0; i < sizeof(jnl->old_start)/sizeof(jnl->old_start[0]); i++) {
int lcl_counter;
lcl_counter = 0;
while (jnl->old_start[i] & 0x8000000000000000LL) {
if (lcl_counter++ > 10000) {
panic("jnl: check_free_space: tr starting @ 0x%llx not flushing (jnl %p).\n",
jnl->old_start[i], jnl);
}
unlock_oldstart(jnl);
if (jnl->flush) {
jnl->flush(jnl->flush_arg);
}
usleep(10000);
lock_oldstart(jnl);
}
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) {
if (delayed_header_write)
*delayed_header_write = TRUE;
else {
unlock_oldstart(jnl);
write_journal_header(jnl, 1, sequence_num);
lock_oldstart(jnl);
}
break;
}
}
unlock_oldstart(jnl);
if (i < sizeof(jnl->old_start)/sizeof(jnl->old_start[0])) {
continue;
} else if (old_start_empty) {
jnl->jhdr->start = jnl->active_start;
if (delayed_header_write)
*delayed_header_write = TRUE;
else
write_journal_header(jnl, 1, sequence_num);
continue;
}
if (jnl->flush) {
jnl->flush(jnl->flush_arg);
}
usleep(10000);
}
return 0;
}
static void lock_condition(journal *jnl, ConditionalFlag_S *psCondFlag, __unused const char *condition_name) {
lock_flush(jnl);
while (psCondFlag->uFlag) {
pthread_cond_wait(&psCondFlag->sCond, &jnl->flock);
}
psCondFlag->uFlag = TRUE;
unlock_flush(jnl);
}
static void wait_condition(journal *jnl, ConditionalFlag_S *psCondFlag, __unused const char *condition_name) {
if (!psCondFlag->uFlag)
return;
lock_flush(jnl);
while (psCondFlag->uFlag) {
pthread_cond_wait(&psCondFlag->sCond, &jnl->flock);
}
unlock_flush(jnl);
}
static void unlock_condition(journal *jnl, ConditionalFlag_S *psCondFlag) {
lock_flush(jnl);
psCondFlag->uFlag = FALSE;
pthread_cond_broadcast(&psCondFlag->sCond);
unlock_flush(jnl);
}
static int end_transaction(transaction *tr, int force_it, errno_t (*callback)(void*), void *callback_arg, boolean_t drop_lock) {
block_list_header *blhdr=NULL, *next=NULL;
int i, ret_val = 0;
journal *jnl = tr->jnl;
GenericLFBuf *bp;
size_t tbuffer_offset;
if (jnl->cur_tr) {
panic("jnl: jnl @ %p already has cur_tr %p, new tr: %p\n",
jnl, jnl->cur_tr, tr);
}
if (tr->total_bytes == (int)jnl->jhdr->blhdr_size) {
jnl->cur_tr = tr;
goto done;
}
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->flags & JOURNAL_USE_UNMAP) || (tr->trim.extent_count < jnl_trim_flush_limit))) {
jnl->cur_tr = tr;
goto done;
}
lock_condition(jnl, &jnl->flushing, "end_transaction");
if ((jnl->flags & JOURNAL_INVALID) || jnl->flush_aborted == TRUE) {
unlock_condition(jnl, &jnl->flushing);
abort_transaction(jnl, tr);
ret_val = -1;
goto done;
}
lf_lck_rw_lock_exclusive(&jnl->trim_lock);
if (jnl->async_trim != NULL)
panic("jnl: end_transaction: async_trim already non-NULL!");
if (tr->trim.extent_count > 0)
jnl->async_trim = &tr->trim;
lf_lck_rw_unlock_exclusive(&jnl->trim_lock);
jnl->saved_sequence_num = jnl->sequence_num;
check_free_space(jnl, tr->total_bytes, &tr->delayed_header_write, jnl->saved_sequence_num);
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;
lock_oldstart(jnl);
memmove(__CAST_AWAY_QUALIFIER(&jnl->old_start[0], volatile, void *), __CAST_AWAY_QUALIFIER(&jnl->old_start[1], volatile, void *), sizeof(jnl->old_start)-sizeof(jnl->old_start[0]));
jnl->old_start[sizeof(jnl->old_start)/sizeof(jnl->old_start[0]) - 1] = tr->journal_start | 0x8000000000000000LL;
unlock_oldstart(jnl);
for (blhdr = tr->blhdr; blhdr; blhdr = next) {
char *blkptr;
size_t bsize;
tbuffer_offset = jnl->jhdr->blhdr_size;
for (i = 1; i < blhdr->num_blocks; i++) {
if (blhdr->binfo[i].bnum != (off_t)-1) {
bp = (GenericLFBuf*)blhdr->binfo[i].u.bp;
if (bp == NULL) {
panic("jnl: inconsistent binfo (NULL bp w/bnum %lld; jnl @ %p, tr %p)\n",
blhdr->binfo[i].bnum, jnl, tr);
}
bsize = bp->uDataSize;
blkptr = (char *)&((char *)blhdr)[tbuffer_offset];
int iRet;
retry:
iRet = lf_hfs_generic_buf_take_ownership(bp, NULL);
if (iRet == EAGAIN) {
goto retry;
} else if (iRet) {
LFHFS_LOG(LEVEL_ERROR, "jnl: end_transaction: lf_hfs_generic_buf_take_ownership returned %d.\n", iRet);
ret_val = -1;
goto done;
}
if (!(bp->uCacheFlags & GEN_BUF_WRITE_LOCK)) {
panic("GEN_BUF_WRITE_LOCK should be set!");
}
if (bp->pfFunc) {
bp->pfFunc(bp, bp->pvCallbackArgs);
bp->pfFunc = NULL;
}
if (bp->uCacheFlags & GEN_BUF_LITTLE_ENDIAN) {
panic("We do not want to write a GEN_BUF_LITTLE_ENDIAN buffer to media!");
}
memcpy(blkptr, bp->pvData, bsize);
blhdr->binfo[i].u.bp = (void*)bp;
} else {
bsize = blhdr->binfo[i].u.bi.bsize;
}
tbuffer_offset += bsize;
}
next = (block_list_header *)((long)blhdr->binfo[0].bnum);
}
#if HFS_CRASH_TEST
CRASH_ABORT(CRASH_ABORT_JOURNAL_BEFORE_FINISH, jnl->fsmount->psHfsmount, NULL);
#endif
ret_val = finish_end_transaction(tr, callback, callback_arg);
done:
if (drop_lock == TRUE) {
journal_unlock(jnl);
}
return (ret_val);
}
static void abort_transaction(journal *jnl, transaction *tr) {
block_list_header *blhdr, *next;
for (blhdr = tr->blhdr; blhdr; blhdr = next) {
int i;
for (i = 1; i < blhdr->num_blocks; i++) {
GenericLFBufPtr bp;
if (blhdr->binfo[i].bnum == (off_t)-1)
continue;
bp = (void*)blhdr->binfo[i].u.bp;
lf_hfs_generic_buf_clear_cache_flag(bp, GEN_BUF_WRITE_LOCK);
if (lf_hfs_generic_buf_validate_owner(bp)) { lf_hfs_generic_buf_release(bp);
}
}
next = (block_list_header *)((long)blhdr->binfo[0].bnum);
blhdr->binfo[0].bnum = 0xdeadc0de;
hfs_free(blhdr);
}
lf_lck_rw_lock_exclusive(&jnl->trim_lock);
if (jnl->async_trim == &tr->trim)
jnl->async_trim = NULL;
lf_lck_rw_unlock_exclusive(&jnl->trim_lock);
if (tr->trim.extents) {
hfs_free(tr->trim.extents);
}
tr->trim.allocated_count = 0;
tr->trim.extent_count = 0;
tr->trim.extents = NULL;
tr->tbuffer = NULL;
tr->blhdr = NULL;
tr->total_bytes = 0xdbadc0de;
hfs_free(tr);
}
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);
jnl->jhdr->sequence_num = SWAP32(jnl->jhdr->sequence_num);
}
static unsigned int calc_checksum(const char *ptr, int len) {
int i;
unsigned int cksum=0;
for(i = 0; i < len; i++, ptr++) {
cksum = (cksum << 8) ^ (cksum + *(unsigned char *)ptr);
}
return (~cksum);
}
static size_t do_journal_io(journal *jnl, off_t *offset, void *data, size_t len, int direction) {
off_t curlen = len;
size_t io_sz = 0;
off_t max_iosize;
#if 0 // TBD
int err;
buf_t bp;
off_t accumulated_offset = 0;
ExtendedVCB *vcb = HFSTOVCB(jnl->fsmount->psHfsmount);
#endif
if (*offset < 0 || *offset > jnl->jhdr->size) {
panic("jnl: do_jnl_io: bad offset 0x%llx (max 0x%llx)\n", *offset, jnl->jhdr->size);
}
if (direction & JNL_WRITE)
max_iosize = jnl->max_write_size;
else if (direction & JNL_READ)
max_iosize = jnl->max_read_size;
else
max_iosize = 128 * 1024;
again:
if (*offset + curlen > jnl->jhdr->size && *offset != 0 && jnl->jhdr->size != 0) {
if (*offset == jnl->jhdr->size) {
*offset = jnl->jhdr->jhdr_size;
} else {
curlen = jnl->jhdr->size - *offset;
}
}
if (curlen > max_iosize) {
curlen = max_iosize;
}
if (curlen <= 0) {
panic("jnl: do_jnl_io: curlen == %lld, offset 0x%llx len %zd\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 %lld, data %p)\n", curlen, data);
}
uint64_t phyblksize = jnl->fsmount->psHfsmount->hfs_physical_block_size;
uint64_t uBlkNum = jnl->jdev_blknum+(*offset)/phyblksize;
if (direction & JNL_READ) {
raw_readwrite_read_mount(jnl->jdev, uBlkNum, phyblksize, data, curlen, NULL, NULL);
} else if (direction & JNL_WRITE) {
raw_readwrite_write_mount(jnl->jdev, uBlkNum, phyblksize, data, curlen, NULL, NULL);
}
*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_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 void get_io_info(struct vnode *devvp, size_t phys_blksz, journal *jnl) {
off_t readblockcnt;
off_t writeblockcnt;
off_t readmaxcnt=0, tmp_readmaxcnt;
off_t writemaxcnt=0, tmp_writemaxcnt;
off_t readsegcnt, writesegcnt;
ioctl(devvp->psFSRecord->iFD, DKIOCGETMAXBYTECOUNTREAD, (caddr_t)&readmaxcnt);
if (ioctl(devvp->psFSRecord->iFD, DKIOCGETMAXBLOCKCOUNTREAD, (caddr_t)&readblockcnt) == 0) {
tmp_readmaxcnt = readblockcnt * phys_blksz;
if (readmaxcnt == 0 || (readblockcnt > 0 && tmp_readmaxcnt < readmaxcnt)) {
readmaxcnt = tmp_readmaxcnt;
}
}
if (ioctl(devvp->psFSRecord->iFD, DKIOCGETMAXSEGMENTCOUNTREAD, (caddr_t)&readsegcnt)) {
readsegcnt = 0;
}
if (readsegcnt > 0 && (readsegcnt * PAGE_SIZE) < readmaxcnt) {
readmaxcnt = readsegcnt * PAGE_SIZE;
}
if (readmaxcnt == 0) {
readmaxcnt = 128 * 1024;
} else if (readmaxcnt > UINT32_MAX) {
readmaxcnt = UINT32_MAX;
}
ioctl(devvp->psFSRecord->iFD, DKIOCGETMAXBYTECOUNTWRITE, (caddr_t)&writemaxcnt);
if (ioctl(devvp->psFSRecord->iFD, DKIOCGETMAXBLOCKCOUNTWRITE, (caddr_t)&writeblockcnt) == 0) {
tmp_writemaxcnt = writeblockcnt * phys_blksz;
if (writemaxcnt == 0 || (writeblockcnt > 0 && tmp_writemaxcnt < writemaxcnt)) {
writemaxcnt = tmp_writemaxcnt;
}
}
if (ioctl(devvp->psFSRecord->iFD, DKIOCGETMAXSEGMENTCOUNTWRITE, (caddr_t)&writesegcnt)) {
writesegcnt = 0;
}
if (writesegcnt > 0 && (writesegcnt * PAGE_SIZE) < writemaxcnt) {
writemaxcnt = writesegcnt * PAGE_SIZE;
}
if (writemaxcnt == 0) {
writemaxcnt = 128 * 1024;
} else if (writemaxcnt > UINT32_MAX) {
writemaxcnt = UINT32_MAX;
}
jnl->max_read_size = readmaxcnt;
jnl->max_write_size = writemaxcnt;
}
static void free_old_stuff(journal *jnl) {
transaction *tr, *next;
block_list_header *blhdr=NULL, *next_blhdr=NULL;
if (jnl->tr_freeme == NULL)
return;
lock_oldstart(jnl);
tr = jnl->tr_freeme;
jnl->tr_freeme = NULL;
unlock_oldstart(jnl);
for(; tr; tr=next) {
for (blhdr = tr->blhdr; blhdr; blhdr = next_blhdr) {
next_blhdr = (block_list_header *)((long)blhdr->binfo[0].bnum);
blhdr->binfo[0].bnum = 0xdeadc0de;
hfs_free(blhdr);
KERNEL_DEBUG(0xbbbbc01c, jnl, tr, tr->tbuffer_size, 0, 0);
}
next = tr->next;
hfs_free(tr);
}
}
static errno_t journal_allocate_transaction(journal *jnl) {
transaction *tr;
tr = hfs_mallocz(sizeof(transaction));
tr->tbuffer_size = jnl->tbuffer_size;
tr->tbuffer = hfs_malloc(tr->tbuffer_size);
memset(tr->tbuffer, 0, BLHDR_CHECKSUM_SIZE);
memset(tr->tbuffer + BLHDR_CHECKSUM_SIZE, 0x5a, jnl->jhdr->blhdr_size - 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->blhdr->flags = BLHDR_CHECK_CHECKSUMS | BLHDR_FIRST_HEADER;
tr->sequence_num = ++jnl->sequence_num;
tr->num_blhdrs = 1;
tr->total_bytes = jnl->jhdr->blhdr_size;
tr->jnl = jnl;
jnl->active_tr = tr;
return 0;
}
int journal_kill_block(journal *jnl, GenericLFBuf *psGenBuf) {
int i;
uint64_t uflags;
block_list_header *blhdr;
transaction *tr;
#if JOURNAL_DEBUG
printf("journal_kill_block: psGenBuf %p, psVnode %p, uBlockN %llu, uDataSize %u, uPhyCluster %llu uLockCnt %u\n",
psGenBuf, psGenBuf->psVnode, psGenBuf->uBlockN, psGenBuf->uDataSize ,psGenBuf->uPhyCluster, psGenBuf->uLockCnt);
#endif
CHECK_JOURNAL(jnl);
free_old_stuff(jnl);
if (jnl->flags & JOURNAL_INVALID) {
lf_hfs_generic_buf_clear_cache_flag(psGenBuf, GEN_BUF_WRITE_LOCK);
lf_hfs_generic_buf_release(psGenBuf);
return 0;
}
tr = jnl->active_tr;
CHECK_TRANSACTION(tr);
if (jnl->owner != pthread_self()) {
panic("jnl: journal_kill_block: called w/out a transaction! jnl %p, owner %p, curact %p\n",
jnl, jnl->owner, pthread_self());
}
uflags = psGenBuf->uCacheFlags;
if ( !(uflags & GEN_BUF_WRITE_LOCK))
panic("jnl: journal_kill_block: called with bp not B_LOCKED");
for (blhdr = tr->blhdr; blhdr; blhdr = (block_list_header *)((long)blhdr->binfo[0].bnum)) {
for (i = 1; i < blhdr->num_blocks; i++) {
if (psGenBuf == (void*)blhdr->binfo[i].u.bp) {
tr->num_killed += psGenBuf->uDataSize;
blhdr->binfo[i].bnum = (off_t)-1;
blhdr->binfo[i].u.bp = NULL;
blhdr->binfo[i].u.bi.bsize = psGenBuf->uDataSize;
lf_hfs_generic_buf_clear_cache_flag(psGenBuf, GEN_BUF_WRITE_LOCK);
lf_hfs_generic_buf_release(psGenBuf);
return 0;
}
}
}
lf_hfs_generic_buf_clear_cache_flag(psGenBuf, GEN_BUF_WRITE_LOCK);
lf_hfs_generic_buf_release(psGenBuf);
return 0;
}
int journal_is_clean(struct vnode *jvp,
off_t offset,
off_t journal_size,
struct vnode *fsvp,
size_t min_fs_block_size,
struct mount *fsmount) {
journal jnl;
uint32_t phys_blksz;
int ret;
int orig_checksum, checksum;
if (ioctl(jvp->psFSRecord->iFD, DKIOCGETBLOCKSIZE, (caddr_t)&phys_blksz)) {
LFHFS_LOG(LEVEL_ERROR, "jnl: journal_is_clean: failed to get device block size.\n");
ret = EINVAL;
goto cleanup_jdev_name;
}
if (phys_blksz > (uint32_t)min_fs_block_size) {
LFHFS_LOG(LEVEL_ERROR, "jnl: journal_is_clean: error: phys blksize %d bigger than min fs blksize %zd\n",
phys_blksz, min_fs_block_size);
ret = EINVAL;
goto cleanup_jdev_name;
}
if (journal_size < (256*1024) || journal_size > (MAX_JOURNAL_SIZE)) {
LFHFS_LOG(LEVEL_ERROR, "jnl: journal_is_clean: journal size %lld looks bogus.\n", journal_size);
ret = EINVAL;
goto cleanup_jdev_name;
}
if ((journal_size % phys_blksz) != 0) {
LFHFS_LOG(LEVEL_ERROR, "jnl: journal_is_clean: journal size 0x%llx is not an even multiple of block size 0x%x\n",
journal_size, phys_blksz);
ret = EINVAL;
goto cleanup_jdev_name;
}
memset(&jnl, 0, sizeof(jnl));
jnl.header_buf = hfs_malloc(phys_blksz);
jnl.header_buf_size = phys_blksz;
jnl.fsmount = fsmount;
get_io_info(jvp, phys_blksz, &jnl);
jnl.jhdr = (journal_header *)jnl.header_buf;
memset(jnl.jhdr, 0, sizeof(journal_header));
jnl.jdev = jvp;
jnl.jdev_offset = offset;
jnl.jdev_blknum = (uint32_t)(offset / phys_blksz);
jnl.fsdev = fsvp;
jnl.jhdr->jhdr_size = phys_blksz;
if (read_journal_header(&jnl, jnl.jhdr, phys_blksz) != (unsigned)phys_blksz) {
LFHFS_LOG(LEVEL_ERROR, "jnl: journal_is_clean: could not read %d bytes for the journal header.\n",
phys_blksz);
ret = EINVAL;
goto get_out;
}
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, JOURNAL_HEADER_CKSUM_SIZE);
swap_journal_header(&jnl);
jnl.flags |= JOURNAL_NEED_SWAP;
} else {
checksum = calc_checksum((char *)jnl.jhdr, JOURNAL_HEADER_CKSUM_SIZE);
}
if (jnl.jhdr->magic != JOURNAL_HEADER_MAGIC && jnl.jhdr->magic != OLD_JOURNAL_HEADER_MAGIC) {
LFHFS_LOG(LEVEL_ERROR, "jnl: journal_is_clean: journal magic is bad (0x%x != 0x%x)\n",
jnl.jhdr->magic, JOURNAL_HEADER_MAGIC);
ret = EINVAL;
goto get_out;
}
if (orig_checksum != checksum) {
LFHFS_LOG(LEVEL_ERROR, "jnl: journal_is_clean: journal checksum is bad (0x%x != 0x%x)\n", orig_checksum, checksum);
ret = EINVAL;
goto get_out;
}
if (jnl.jhdr->start == jnl.jhdr->end) {
ret = 0;
} else {
ret = EBUSY; }
get_out:
hfs_free(jnl.header_buf);
cleanup_jdev_name:
return ret;
}
uint32_t journal_current_txn(journal *jnl) {
return jnl->sequence_num + (jnl->active_tr || jnl->cur_tr ? 0 : 1);
}