Lines Matching refs:jnl

65  * Set sysctl vfs.generic.jnl.kdebug.trim=1 to enable KERNEL_DEBUG_CONSTANT
73 SYSCTL_NODE(_vfs_generic, OID_AUTO, jnl, CTLFLAG_RW|CTLFLAG_LOCKED, 0, "Journal");
155 static void lock_condition(journal *jnl, boolean_t *condition, const char *condition_name);
156 static void wait_condition(journal *jnl, boolean_t *condition, const char *condition_name);
157 static void unlock_condition(journal *jnl, boolean_t *condition);
159 static void write_header_thread(journal *jnl);
162 static void abort_transaction(journal *jnl, transaction *tr);
163 static void dump_journal(journal *jnl);
165 static __inline__ void lock_journal(journal *jnl);
166 static __inline__ void unlock_journal(journal *jnl);
167 static __inline__ void lock_oldstart(journal *jnl);
168 static __inline__ void unlock_oldstart(journal *jnl);
169 static __inline__ void lock_flush(journal *jnl);
170 static __inline__ void unlock_flush(journal *jnl);
186 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);
189 static int do_overlap(journal *jnl, struct bucket **buf_ptr, int blk_index, off_t block_num, size_t size, size_t offset, int32_t cksum, int *num_buckets_ptr, int *num_full_ptr);
190 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);
192 #define CHECK_JOURNAL(jnl) \
194 if (jnl == NULL) { \
197 if (jnl->jdev == NULL) { \
200 if (jnl->fsdev == NULL) { \
203 if (jnl->jhdr->magic != JOURNAL_HEADER_MAGIC) { \
205 __FILE__, __LINE__, jnl->jhdr->magic, JOURNAL_HEADER_MAGIC); \
207 if ( jnl->jhdr->start <= 0 \
208 || jnl->jhdr->start > jnl->jhdr->size) { \
210 __FILE__, __LINE__, jnl->jhdr->start, jnl->jhdr->size); \
212 if ( jnl->jhdr->end <= 0 \
213 || jnl->jhdr->end > jnl->jhdr->size) { \
215 __FILE__, __LINE__, jnl->jhdr->end, jnl->jhdr->size); \
224 if (tr->jnl == NULL) { \
225 panic("%s:%d: null tr->jnl ptr?\n", __FILE__, __LINE__); \
239 if (tr->blhdr && (tr->blhdr->max_blocks <= 0 || tr->blhdr->max_blocks > (tr->jnl->jhdr->size/tr->jnl->jhdr->jhdr_size))) { \
276 jnl_mutex_group = lck_grp_alloc_init("jnl-mutex", jnl_group_attr);
280 lock_journal(journal *jnl)
282 lck_mtx_lock(&jnl->jlock);
286 unlock_journal(journal *jnl)
288 lck_mtx_unlock(&jnl->jlock);
292 lock_flush(journal *jnl)
294 lck_mtx_lock(&jnl->flock);
298 unlock_flush(journal *jnl)
300 lck_mtx_unlock(&jnl->flock);
304 lock_oldstart(journal *jnl)
306 lck_mtx_lock(&jnl->old_start_lock);
310 unlock_oldstart(journal *jnl)
312 lck_mtx_unlock(&jnl->old_start_lock);
331 do_journal_io(journal *jnl, off_t *offset, void *data, size_t len, int direction)
338 if (*offset < 0 || *offset > jnl->jhdr->size) {
339 panic("jnl: do_jnl_io: bad offset 0x%llx (max 0x%llx)\n", *offset, jnl->jhdr->size);
343 max_iosize = jnl->max_write_size;
345 max_iosize = jnl->max_read_size;
350 bp = alloc_io_buf(jnl->jdev, 1);
352 if (*offset + (off_t)curlen > jnl->jhdr->size && *offset != 0 && jnl->jhdr->size != 0) {
353 if (*offset == jnl->jhdr->size) {
354 *offset = jnl->jhdr->jhdr_size;
356 curlen = (off_t)jnl->jhdr->size - *offset;
365 panic("jnl: do_jnl_io: curlen == %d, offset 0x%llx len %zd\n", curlen, *offset, len);
369 panic("jnl: request for i/o to jnl-header without JNL_HEADER flag set! (len %d, data %p)\n", curlen, data);
378 vnode_startwrite(jnl->jdev);
383 buf_setblkno(bp, (daddr64_t) ((jnl->jdev_offset + *offset) / (off_t)jnl->jhdr->jhdr_size));
384 buf_setlblkno(bp, (daddr64_t) ((jnl->jdev_offset + *offset) / (off_t)jnl->jhdr->jhdr_size));
386 if ((direction & JNL_WRITE) && (jnl->flags & JOURNAL_DO_FUA_WRITES)) {
399 printf("jnl: %s: do_jnl_io: strategy err 0x%x\n", jnl->jdev_name, err);
410 if (*offset >= jnl->jhdr->size) {
411 *offset = jnl->jhdr->jhdr_size;
420 read_journal_data(journal *jnl, off_t *offset, void *data, size_t len)
422 return do_journal_io(jnl, offset, data, len, JNL_READ);
426 write_journal_data(journal *jnl, off_t *offset, void *data, size_t len)
428 return do_journal_io(jnl, offset, data, len, JNL_WRITE);
433 read_journal_header(journal *jnl, void *data, size_t len)
437 return do_journal_io(jnl, &hdr_offset, data, len, JNL_READ|JNL_HEADER);
441 write_journal_header(journal *jnl, int updating_start, uint32_t sequence_num)
454 if (!updating_start && (jnl->flags & JOURNAL_DO_FUA_WRITES) == 0) {
455 ret = VNOP_IOCTL(jnl->jdev, DKIOCSYNCHRONIZECACHE, NULL, FWRITE, &context);
467 if ( ret != jnl->last_flush_err
468 || (jnl->flags & JOURNAL_FLUSHCACHE_ERR) == 0
471 printf("jnl: %s: flushing fs disk buffer returned 0x%x\n", jnl->jdev_name, ret);
473 jnl->flags |= JOURNAL_FLUSHCACHE_ERR;
474 jnl->last_flush_err = ret;
478 jnl->jhdr->sequence_num = sequence_num;
479 jnl->jhdr->checksum = 0;
480 jnl->jhdr->checksum = calc_checksum((char *)jnl->jhdr, JOURNAL_HEADER_CKSUM_SIZE);
482 if (do_journal_io(jnl, &jhdr_offset, jnl->header_buf, jnl->jhdr->jhdr_size, JNL_WRITE|JNL_HEADER) != (size_t)jnl->jhdr->jhdr_size) {
483 printf("jnl: %s: write_journal_header: error writing the journal header!\n", jnl->jdev_name);
484 jnl->flags |= JOURNAL_INVALID;
496 if (updating_start && (jnl->flags & JOURNAL_DO_FUA_WRITES) == 0) {
497 VNOP_IOCTL(jnl->jdev, DKIOCSYNCHRONIZECACHE, NULL, FWRITE, &context);
514 free_old_stuff(journal *jnl)
519 if (jnl->tr_freeme == NULL)
522 lock_oldstart(jnl);
523 tr = jnl->tr_freeme;
524 jnl->tr_freeme = NULL;
525 unlock_oldstart(jnl);
534 KERNEL_DEBUG(0xbbbbc01c, jnl, tr, tr->tbuffer_size, 0, 0);
553 journal *jnl;
559 //printf("jnl: buf flush: bp @ 0x%x l/blkno %qd/%qd vp 0x%x tr @ 0x%x\n",
573 jnl = tr->jnl;
574 if (jnl->flags & JOURNAL_INVALID) {
578 CHECK_JOURNAL(jnl);
605 lock_oldstart(jnl);
609 unlock_oldstart(jnl);
617 //printf("jnl: tr 0x%x (0x%llx 0x%llx) in jnl 0x%x completed.\n",
618 // tr, tr->journal_start, tr->journal_end, jnl);
621 for(i = 0; i < sizeof(jnl->old_start)/sizeof(jnl->old_start[0]); i++) {
623 if ((off_t)(jnl->old_start[i] & ~(0x8000000000000000ULL)) == tr->journal_start) {
624 jnl->old_start[i] &= ~(0x8000000000000000ULL);
629 if (i >= sizeof(jnl->old_start)/sizeof(jnl->old_start[0])) {
630 panic("jnl: buffer_flushed: did not find tr w/start @ %lld (tr %p, jnl %p)\n",
631 tr->journal_start, tr, jnl);
637 if (tr->journal_start == jnl->active_start) {
638 jnl->active_start = tr->journal_end;
644 for (ctr = jnl->completed_trs; ctr; prev=ctr, ctr=next) {
645 if (ctr->journal_start == jnl->active_start) {
646 jnl->active_start = ctr->journal_end;
650 if (ctr == jnl->completed_trs) {
651 jnl->completed_trs = ctr->next;
654 next = jnl->completed_trs; // this starts us over again
655 ctr->next = jnl->tr_freeme;
656 jnl->tr_freeme = ctr;
660 next = jnl->completed_trs; // this starts us over again
673 next->next = jnl->tr_freeme; // link in the next guy at the head of the tr_freeme list
674 jnl->tr_freeme = next;
676 next = jnl->completed_trs; // this starts us over again
692 for (ctr = jnl->completed_trs; ctr && tr->journal_start > ctr->journal_start; prev=ctr, ctr=ctr->next) {
697 jnl->completed_trs = tr;
699 } else if (ctr == jnl->completed_trs) {
700 tr->next = jnl->completed_trs;
701 jnl->completed_trs = tr;
709 tr->next = jnl->tr_freeme;
710 jnl->tr_freeme = tr;
712 unlock_oldstart(jnl);
714 unlock_condition(jnl, &jnl->asyncIO);
726 swap_journal_header(journal *jnl)
728 jnl->jhdr->magic = SWAP32(jnl->jhdr->magic);
729 jnl->jhdr->endian = SWAP32(jnl->jhdr->endian);
730 jnl->jhdr->start = SWAP64(jnl->jhdr->start);
731 jnl->jhdr->end = SWAP64(jnl->jhdr->end);
732 jnl->jhdr->size = SWAP64(jnl->jhdr->size);
733 jnl->jhdr->blhdr_size = SWAP32(jnl->jhdr->blhdr_size);
734 jnl->jhdr->checksum = SWAP32(jnl->jhdr->checksum);
735 jnl->jhdr->jhdr_size = SWAP32(jnl->jhdr->jhdr_size);
736 jnl->jhdr->sequence_num = SWAP32(jnl->jhdr->sequence_num);
740 swap_block_list_header(journal *jnl, block_list_header *blhdr)
750 if (blhdr->num_blocks >= ((jnl->jhdr->blhdr_size / sizeof(block_info)) - 1)) {
751 printf("jnl: %s: blhdr num blocks looks suspicious (%d / blhdr size %d). not swapping.\n", jnl->jdev_name, blhdr->num_blocks, jnl->jhdr->blhdr_size);
764 update_fs_block(journal *jnl, void *block_ptr, off_t fs_block, size_t bsize)
770 ret = buf_meta_bread(jnl->fsdev, (daddr64_t)fs_block, bsize, NOCRED, &oblock_bp);
772 printf("jnl: %s: update_fs_block: error reading fs block # %lld! (ret %d)\n", jnl->jdev_name, fs_block, ret);
780 oblock_bp = buf_getblk(jnl->fsdev, (daddr64_t)fs_block, bsize, 0, 0, BLK_META);
782 printf("jnl: %s: update_fs_block: buf_getblk() for %lld failed! failing update.\n", jnl->jdev_name, fs_block);
797 printf("jnl: %s: update_fs_block: failed to update block %lld (ret %d)\n", jnl->jdev_name, fs_block,ret);
803 ret = buf_meta_bread(jnl->fsdev, (daddr64_t)fs_block, bsize, NOCRED, &oblock_bp);
824 printf("jnl: grow_table: no memory to expand coalesce buffer!\n");
828 // printf("jnl: lookup_bucket: expanded co_buf to %d elems\n", new_size);
905 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)
914 printf("jnl: %s: add_block: grow_table returned an error!\n", jnl->jdev_name);
930 if ((off_t)offset >= jnl->jhdr->size) {
931 offset = jnl->jhdr->jhdr_size + (offset - jnl->jhdr->size);
934 panic("jnl: insert_block: bad size in insert_block (%zd)\n", size);
946 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)
949 size_t jhdr_size = jnl->jhdr->jhdr_size, new_offset;
963 panic("jnl: do_overlap: overlap with previous entry not a multiple of %zd\n", jhdr_size);
973 err = insert_block(jnl, buf_ptr, blk_index, new_num, new_size, new_offset, cksum, num_buckets_ptr, num_full_ptr, 0);
975 panic("jnl: do_overlap: error inserting during pre-overlap\n");
1008 panic("jnl: do_overlap: overlap of %lld is not multiple of %zd\n", overlap, jhdr_size);
1011 // if we partially overlap this entry, adjust its block number, jnl offset, and size
1016 if ((off_t)new_offset >= jnl->jhdr->size) {
1017 new_offset = jhdr_size + (new_offset - jnl->jhdr->size);
1023 panic("jnl: do_overlap: after overlap, new block size is invalid (%u)\n", (*buf_ptr)[index].block_size);
1064 add_block(journal *jnl, struct bucket **buf_ptr, off_t block_num, size_t size, __unused size_t offset, int32_t cksum, int *num_buckets_ptr, int *num_full_ptr)
1075 //printf("jnl: add_block: trouble adding block to co_buf\n");
1077 } // else printf("jnl: add_block: adding block 0x%llx at i=%d\n", block_num, blk_index);
1080 overwriting = do_overlap(jnl, buf_ptr, blk_index, block_num, size, offset, cksum, num_buckets_ptr, num_full_ptr);
1086 blk_index = insert_block(jnl, buf_ptr, blk_index, block_num, size, offset, cksum, num_buckets_ptr, num_full_ptr, overwriting);
1092 replay_journal(journal *jnl)
1106 if (jnl->jhdr->start == jnl->jhdr->size) {
1107 jnl->jhdr->start = jnl->jhdr->jhdr_size;
1109 if (jnl->jhdr->end == jnl->jhdr->size) {
1110 jnl->jhdr->end = jnl->jhdr->jhdr_size;
1113 if (jnl->jhdr->start == jnl->jhdr->end) {
1117 orig_jnl_start = jnl->jhdr->start;
1120 if (kmem_alloc_kobject(kernel_map, (vm_offset_t *)&buff, jnl->jhdr->blhdr_size)) {
1121 printf("jnl: %s: replay_journal: no memory for block buffer! (%d bytes)\n",
1122 jnl->jdev_name, jnl->jhdr->blhdr_size);
1128 printf("jnl: %s: replay_journal: no memory for coalesce buffer!\n", jnl->jdev_name);
1141 printf("jnl: %s: replay_journal: from: %lld to: %lld (joffset 0x%llx)\n",
1142 jnl->jdev_name, jnl->jhdr->start, jnl->jhdr->end, jnl->jdev_offset);
1144 while (check_past_jnl_end || jnl->jhdr->start != jnl->jhdr->end) {
1145 offset = blhdr_offset = jnl->jhdr->start;
1146 ret = read_journal_data(jnl, &offset, buff, jnl->jhdr->blhdr_size);
1147 if (ret != (size_t)jnl->jhdr->blhdr_size) {
1148 printf("jnl: %s: replay_journal: Could not read block list header block @ 0x%llx!\n", jnl->jdev_name, offset);
1157 if (jnl->flags & JOURNAL_NEED_SWAP) {
1162 swap_block_list_header(jnl, blhdr);
1177 if (blhdr_offset != jnl->jhdr->end) {
1178 printf("jnl: %s: Extra txn replay stopped @ %lld / 0x%llx\n", jnl->jdev_name, blhdr_offset, blhdr_offset);
1182 jnl->jhdr->end = blhdr_offset;
1186 printf("jnl: %s: replay_journal: bad block list header @ 0x%llx (checksum 0x%x != 0x%x)\n",
1187 jnl->jdev_name, blhdr_offset, orig_checksum, checksum);
1203 txn_start_offset = jnl->jhdr->end = blhdr_offset;
1207 printf("jnl: %s: 2: extra replay stopped @ %lld / 0x%llx (seq %d < %d)\n",
1208 jnl->jdev_name, blhdr_offset, blhdr_offset, blhdr->binfo[0].u.bi.b.sequence_num, last_sequence_num);
1212 printf("jnl: %s: txn sequence numbers out of order in txn @ %lld / %llx! (%d < %d)\n",
1213 jnl->jdev_name, blhdr_offset, blhdr_offset, blhdr->binfo[0].u.bi.b.sequence_num, last_sequence_num);
1219 if (blhdr_offset >= jnl->jhdr->end && jnl->jhdr->start <= jnl->jhdr->end) {
1222 printf("jnl: %s: pre-sequence-num-enabled txn's - can not go further than end (%lld %lld).\n",
1223 jnl->jdev_name, jnl->jhdr->start, jnl->jhdr->end);
1224 if (jnl->jhdr->start != jnl->jhdr->end) {
1225 jnl->jhdr->start = jnl->jhdr->end;
1229 printf("jnl: %s: examining extra transactions starting @ %lld / 0x%llx\n", jnl->jdev_name, blhdr_offset, blhdr_offset);
1232 if ( blhdr->max_blocks <= 0 || blhdr->max_blocks > (jnl->jhdr->size/jnl->jhdr->jhdr_size)
1234 printf("jnl: %s: replay_journal: bad looking journal entry: max: %d num: %d\n",
1235 jnl->jdev_name, blhdr->max_blocks, blhdr->num_blocks);
1243 printf("jnl: %s: replay_journal: bogus block number 0x%llx\n", jnl->jdev_name, blhdr->binfo[i].bnum);
1266 //printf("jnl: replay_journal: adding %d blocks in journal entry @ 0x%llx to co_buf\n",
1267 // blhdr->num_blocks-1, jnl->jhdr->start);
1278 //printf("jnl: replay_journal: skipping killed fs block (index %d)\n", i);
1288 ret = read_journal_data(jnl, &block_offset, block_ptr, size);
1290 printf("jnl: %s: replay_journal: Could not read journal entry data @ offset 0x%llx!\n", jnl->jdev_name, offset);
1300 printf("jnl: %s: txn starting at %lld (%lld) @ index %3d bnum %lld (%d) with disk cksum != blhdr cksum (0x%.8x 0x%.8x)\n",
1301 jnl->jdev_name, txn_start_offset, blhdr_offset, i, number, size, disk_cksum, blhdr->binfo[i].u.bi.b.cksum);
1302 printf("jnl: 0x%.8x 0x%.8x 0x%.8x 0x%.8x 0x%.8x 0x%.8x 0x%.8x 0x%.8x\n",
1313 // printf("jnl: replay_journal: adding block 0x%llx\n", number);
1314 ret_val = add_block(jnl, &co_buf, number, size, (size_t) offset, blhdr->binfo[i].u.bi.b.cksum, &num_buckets, &num_full);
1317 printf("jnl: %s: replay_journal: trouble adding block to co_buf\n", jnl->jdev_name);
1319 } // else printf("jnl: replay_journal: added block 0x%llx at i=%d\n", number);
1325 // check if the last block added puts us off the end of the jnl.
1329 if (offset >= jnl->jhdr->size) {
1330 offset = jnl->jhdr->jhdr_size + (offset - jnl->jhdr->size);
1344 printf("jnl: %s: no known good txn start offset! aborting journal replay.\n", jnl->jdev_name);
1350 printf("jnl: %s: repeated errors replaying journal! aborting journal replay.\n", jnl->jdev_name);
1360 jnl->jhdr->start = orig_jnl_start;
1361 jnl->jhdr->end = txn_start_offset;
1364 printf("jnl: %s: restarting journal replay (%lld - %lld)!\n", jnl->jdev_name, jnl->jhdr->start, jnl->jhdr->end);
1368 jnl->jhdr->start += blhdr->bytes_used;
1369 if (jnl->jhdr->start >= jnl->jhdr->size) {
1371 jnl->jhdr->start = (jnl->jhdr->start % jnl->jhdr->size) + jnl->jhdr->jhdr_size;
1374 if (jnl->jhdr->start == jnl->jhdr->end) {
1379 if (jnl->jhdr->start != jnl->jhdr->end) {
1380 printf("jnl: %s: start %lld != end %lld. resetting end.\n", jnl->jdev_name, jnl->jhdr->start, jnl->jhdr->end);
1381 jnl->jhdr->end = jnl->jhdr->start;
1384 //printf("jnl: replay_journal: replaying %d blocks\n", num_full);
1420 // printf("jnl: replay_journal: skipping killed fs block\n");
1424 ret = read_journal_data(jnl, &jnl_offset, block_ptr, size);
1426 printf("jnl: %s: replay_journal: Could not read journal entry data @ offset 0x%llx!\n", jnl->jdev_name, offset);
1430 if (update_fs_block(jnl, block_ptr, number, size) != 0) {
1437 // done replaying; update jnl header
1438 if (write_journal_header(jnl, 1, jnl->jhdr->sequence_num) != 0) {
1442 printf("jnl: %s: journal replay done.\n", jnl->jdev_name);
1454 kmem_free(kernel_map, (vm_offset_t)buff, jnl->jhdr->blhdr_size);
1464 kmem_free(kernel_map, (vm_offset_t)buff, jnl->jhdr->blhdr_size);
1479 // size of the blhdr. It assumes that jnl->jhdr->size
1480 // and jnl->jhdr->jhdr_size are already valid.
1483 size_up_tbuffer(journal *jnl, int tbuffer_size, int phys_blksz)
1504 jnl->tbuffer_size = def_tbuffer_size;
1507 if (tbuffer_size < jnl->jhdr->blhdr_size * 2) {
1508 tbuffer_size = jnl->jhdr->blhdr_size * 2;
1511 if ((tbuffer_size % jnl->jhdr->jhdr_size) != 0) {
1512 tbuffer_size -= (tbuffer_size % jnl->jhdr->jhdr_size);
1515 jnl->tbuffer_size = tbuffer_size;
1518 if (jnl->tbuffer_size > (jnl->jhdr->size / 2)) {
1519 jnl->tbuffer_size = (jnl->jhdr->size / 2);
1522 if (jnl->tbuffer_size > MAX_TRANSACTION_BUFFER_SIZE) {
1523 jnl->tbuffer_size = MAX_TRANSACTION_BUFFER_SIZE;
1526 jnl->jhdr->blhdr_size = (jnl->tbuffer_size / jnl->jhdr->jhdr_size) * sizeof(block_info);
1527 if (jnl->jhdr->blhdr_size < phys_blksz) {
1528 jnl->jhdr->blhdr_size = phys_blksz;
1529 } else if ((jnl->jhdr->blhdr_size % phys_blksz) != 0) {
1531 jnl->jhdr->blhdr_size = (jnl->jhdr->blhdr_size + (phys_blksz - 1)) & ~(phys_blksz - 1);
1538 get_io_info(struct vnode *devvp, size_t phys_blksz, journal *jnl, struct vfs_context *context)
1550 jnl->flags |= JOURNAL_DO_FUA_WRITES;
1551 printf("jnl: %s: enabling FUA writes (features 0x%x)\n", name ? name : "no-name-dev", features);
1554 jnl->flags |= JOURNAL_USE_UNMAP;
1611 jnl->max_read_size = readmaxcnt;
1612 jnl->max_write_size = writemaxcnt;
1613 // printf("jnl: %s: max read/write: %lld k / %lld k\n",
1614 // jnl->jdev_name ? jnl->jdev_name : "unknown",
1615 // jnl->max_read_size/1024, jnl->max_write_size/1024);
1647 journal *jnl;
1669 printf("jnl: create: journal size %lld looks bogus.\n", journal_size);
1676 printf("jnl: create: journal size (%lld) too small given sector size of (%u)\n",
1682 printf("jnl: %s: create: error: phys blksize %u bigger than min fs blksize %zd\n",
1688 printf("jnl: %s: create: journal size 0x%llx is not an even multiple of block size 0x%ux\n",
1694 MALLOC_ZONE(jnl, struct journal *, sizeof(struct journal), M_JNL_JNL, M_WAITOK);
1695 memset(jnl, 0, sizeof(*jnl));
1697 jnl->jdev = jvp;
1698 jnl->jdev_offset = offset;
1699 jnl->fsdev = fsvp;
1700 jnl->flush = flush;
1701 jnl->flush_arg = arg;
1702 jnl->flags = (flags & JOURNAL_OPTION_FLAGS_MASK);
1703 jnl->jdev_name = jdev_name;
1704 lck_mtx_init(&jnl->old_start_lock, jnl_mutex_group, jnl_lock_attr);
1706 get_io_info(jvp, phys_blksz, jnl, &context);
1708 if (kmem_alloc_kobject(kernel_map, (vm_offset_t *)&jnl->header_buf, phys_blksz)) {
1709 printf("jnl: %s: create: could not allocate space for header buffer (%u bytes)\n", jdev_name, phys_blksz);
1712 jnl->header_buf_size = phys_blksz;
1714 jnl->jhdr = (journal_header *)jnl->header_buf;
1715 memset(jnl->jhdr, 0, sizeof(journal_header));
1718 jnl->jhdr->jhdr_size = phys_blksz;
1736 if ( read_journal_header(jnl, jnl->jhdr, phys_blksz) == phys_blksz
1737 && jnl->jhdr->magic == JOURNAL_HEADER_MAGIC
1738 && jnl->jhdr->sequence_num != 0) {
1740 new_txn_base = (jnl->jhdr->sequence_num + (journal_size / phys_blksz) + (random() % 16384)) & 0x00ffffff;
1741 printf("jnl: create: avoiding old sequence number 0x%x (0x%x)\n", jnl->jhdr->sequence_num, new_txn_base);
1753 write_journal_data(jnl, &pos, jnl->header_buf, phys_blksz);
1755 printf("jnl: create: done clearing journal (i=%d)\n", i);
1761 memset(jnl->header_buf, 0, phys_blksz);
1763 jnl->jhdr->magic = JOURNAL_HEADER_MAGIC;
1764 jnl->jhdr->endian = ENDIAN_MAGIC;
1765 jnl->jhdr->start = phys_blksz; // start at block #1, block #0 is for the jhdr itself
1766 jnl->jhdr->end = phys_blksz;
1767 jnl->jhdr->size = journal_size;
1768 jnl->jhdr->jhdr_size = phys_blksz;
1769 size_up_tbuffer(jnl, tbuffer_size, phys_blksz);
1771 jnl->active_start = jnl->jhdr->start;
1774 // jnl->jhdr->start = jnl->jhdr->size - (phys_blksz*3);
1775 // jnl->jhdr->end = jnl->jhdr->size - (phys_blksz*3);
1777 jnl->jhdr->sequence_num = new_txn_base;
1779 lck_mtx_init(&jnl->jlock, jnl_mutex_group, jnl_lock_attr);
1780 lck_mtx_init(&jnl->flock, jnl_mutex_group, jnl_lock_attr);
1781 lck_rw_init(&jnl->trim_lock, jnl_mutex_group, jnl_lock_attr);
1784 jnl->flushing = FALSE;
1785 jnl->asyncIO = FALSE;
1786 jnl->flush_aborted = FALSE;
1787 jnl->writing_header = FALSE;
1788 jnl->async_trim = NULL;
1789 jnl->sequence_num = jnl->jhdr->sequence_num;
1791 if (write_journal_header(jnl, 1, jnl->jhdr->sequence_num) != 0) {
1792 printf("jnl: %s: journal_create: failed to write journal header.\n", jdev_name);
1796 return jnl;
1800 kmem_free(kernel_map, (vm_offset_t)jnl->header_buf, phys_blksz);
1805 jnl->jhdr = NULL;
1806 FREE_ZONE(jnl, sizeof(struct journal), M_JNL_JNL);
1823 journal *jnl;
1840 printf("jnl: %s: open: error: phys blksize %u bigger than min fs blksize %zd\n",
1846 printf("jnl: open: journal size %lld looks bogus.\n", journal_size);
1853 printf("jnl: open: journal size (%lld) too small given sector size of (%u)\n",
1859 printf("jnl: %s: open: journal size 0x%llx is not an even multiple of block size 0x%x\n",
1864 MALLOC_ZONE(jnl, struct journal *, sizeof(struct journal), M_JNL_JNL, M_WAITOK);
1865 memset(jnl, 0, sizeof(*jnl));
1867 jnl->jdev = jvp;
1868 jnl->jdev_offset = offset;
1869 jnl->fsdev = fsvp;
1870 jnl->flush = flush;
1871 jnl->flush_arg = arg;
1872 jnl->flags = (flags & JOURNAL_OPTION_FLAGS_MASK);
1873 jnl->jdev_name = jdev_name;
1874 lck_mtx_init(&jnl->old_start_lock, jnl_mutex_group, jnl_lock_attr);
1876 get_io_info(jvp, phys_blksz, jnl, &context);
1878 if (kmem_alloc_kobject(kernel_map, (vm_offset_t *)&jnl->header_buf, phys_blksz)) {
1879 printf("jnl: %s: create: could not allocate space for header buffer (%u bytes)\n", jdev_name, phys_blksz);
1882 jnl->header_buf_size = phys_blksz;
1884 jnl->jhdr = (journal_header *)jnl->header_buf;
1885 memset(jnl->jhdr, 0, sizeof(journal_header));
1888 jnl->jhdr->jhdr_size = phys_blksz;
1890 if (read_journal_header(jnl, jnl->jhdr, phys_blksz) != phys_blksz) {
1891 printf("jnl: %s: open: could not read %u bytes for the journal header.\n",
1896 orig_checksum = jnl->jhdr->checksum;
1897 jnl->jhdr->checksum = 0;
1899 if (jnl->jhdr->magic == SWAP32(JOURNAL_HEADER_MAGIC)) {
1902 checksum = calc_checksum((char *)jnl->jhdr, JOURNAL_HEADER_CKSUM_SIZE);
1903 swap_journal_header(jnl);
1904 jnl->flags |= JOURNAL_NEED_SWAP;
1906 checksum = calc_checksum((char *)jnl->jhdr, JOURNAL_HEADER_CKSUM_SIZE);
1909 if (jnl->jhdr->magic != JOURNAL_HEADER_MAGIC && jnl->jhdr->magic != OLD_JOURNAL_HEADER_MAGIC) {
1910 printf("jnl: %s: open: journal magic is bad (0x%x != 0x%x)\n",
1911 jnl->jdev_name, jnl->jhdr->magic, JOURNAL_HEADER_MAGIC);
1916 if (jnl->jhdr->magic == JOURNAL_HEADER_MAGIC) {
1919 printf("jnl: %s: open: journal checksum is bad (0x%x != 0x%x)\n",
1927 if (jnl->jhdr->magic == OLD_JOURNAL_HEADER_MAGIC) {
1928 jnl->jhdr->magic = JOURNAL_HEADER_MAGIC;
1931 if (phys_blksz != (size_t)jnl->jhdr->jhdr_size && jnl->jhdr->jhdr_size != 0) {
1943 phys_blksz = jnl->jhdr->jhdr_size;
1945 printf("jnl: %s: open: temporarily switched block size from %u to %u\n",
1949 if ( jnl->jhdr->start <= 0
1950 || jnl->jhdr->start > jnl->jhdr->size
1951 || jnl->jhdr->start > 1024*1024*1024) {
1952 printf("jnl: %s: open: jhdr start looks bad (0x%llx max size 0x%llx)\n",
1953 jdev_name, jnl->jhdr->start, jnl->jhdr->size);
1957 if ( jnl->jhdr->end <= 0
1958 || jnl->jhdr->end > jnl->jhdr->size
1959 || jnl->jhdr->end > 1024*1024*1024) {
1960 printf("jnl: %s: open: jhdr end looks bad (0x%llx max size 0x%llx)\n",
1961 jdev_name, jnl->jhdr->end, jnl->jhdr->size);
1965 if (jnl->jhdr->size < (256*1024) || jnl->jhdr->size > 1024*1024*1024) {
1966 printf("jnl: %s: open: jhdr size looks bad (0x%llx)\n", jdev_name, jnl->jhdr->size);
1975 // XXXdbg if ((jnl->jhdr->start % jnl->jhdr->jhdr_size) != 0) {
1976 if ((jnl->jhdr->start % 512) != 0) {
1977 printf("jnl: %s: open: journal start (0x%llx) not a multiple of 512?\n",
1978 jdev_name, jnl->jhdr->start);
1982 //XXXdbg if ((jnl->jhdr->end % jnl->jhdr->jhdr_size) != 0) {
1983 if ((jnl->jhdr->end % 512) != 0) {
1984 printf("jnl: %s: open: journal end (0x%llx) not a multiple of block size (0x%x)?\n",
1985 jdev_name, jnl->jhdr->end, jnl->jhdr->jhdr_size);
1991 printf("jnl: %s: journal start/end pointers reset! (jnl %p; s 0x%llx e 0x%llx)\n",
1992 jdev_name, jnl, jnl->jhdr->start, jnl->jhdr->end);
1993 jnl->jhdr->start = jnl->jhdr->end;
1994 } else if (replay_journal(jnl) != 0) {
1995 printf("jnl: %s: journal_open: Error replaying the journal!\n", jdev_name);
2000 * When we get here, we know that the journal is empty (jnl->jhdr->start ==
2001 * jnl->jhdr->end). If the device's logical block size was different from
2017 jnl->jhdr->jhdr_size = phys_blksz;
2018 jnl->jhdr->start = phys_blksz;
2019 jnl->jhdr->end = phys_blksz;
2020 jnl->jhdr->sequence_num = (jnl->jhdr->sequence_num +
2024 if (write_journal_header(jnl, 1, jnl->jhdr->sequence_num)) {
2025 printf("jnl: %s: open: failed to update journal header size\n", jdev_name);
2031 jnl->active_start = jnl->jhdr->start;
2032 jnl->sequence_num = jnl->jhdr->sequence_num;
2035 size_up_tbuffer(jnl, tbuffer_size, phys_blksz);
2038 if ((off_t)(jnl->jhdr->blhdr_size/sizeof(block_info)-1) > (jnl->jhdr->size/jnl->jhdr->jhdr_size)) {
2039 printf("jnl: %s: open: jhdr size and blhdr size are not compatible (0x%llx, %d, %d)\n", jdev_name, jnl->jhdr->size,
2040 jnl->jhdr->blhdr_size, jnl->jhdr->jhdr_size);
2044 lck_mtx_init(&jnl->jlock, jnl_mutex_group, jnl_lock_attr);
2045 lck_mtx_init(&jnl->flock, jnl_mutex_group, jnl_lock_attr);
2046 lck_rw_init(&jnl->trim_lock, jnl_mutex_group, jnl_lock_attr);
2048 return jnl;
2054 printf("jnl: %s: open: restored block size after error\n", jdev_name);
2056 kmem_free(kernel_map, (vm_offset_t)jnl->header_buf, phys_blksz);
2061 FREE_ZONE(jnl, sizeof(struct journal), M_JNL_JNL);
2073 journal jnl;
2085 printf("jnl: %s: is_clean: failed to get device block size.\n", jdev_name);
2090 printf("jnl: %s: is_clean: error: phys blksize %d bigger than min fs blksize %zd\n",
2096 printf("jnl: is_clean: journal size %lld looks bogus.\n", journal_size);
2101 printf("jnl: %s: is_clean: journal size 0x%llx is not an even multiple of block size 0x%x\n",
2106 memset(&jnl, 0, sizeof(jnl));
2108 if (kmem_alloc_kobject(kernel_map, (vm_offset_t *)&jnl.header_buf, phys_blksz)) {
2109 printf("jnl: %s: is_clean: could not allocate space for header buffer (%d bytes)\n", jdev_name, phys_blksz);
2112 jnl.header_buf_size = phys_blksz;
2114 get_io_info(jvp, phys_blksz, &jnl, &context);
2116 jnl.jhdr = (journal_header *)jnl.header_buf;
2117 memset(jnl.jhdr, 0, sizeof(journal_header));
2119 jnl.jdev = jvp;
2120 jnl.jdev_offset = offset;
2121 jnl.fsdev = fsvp;
2124 jnl.jhdr->jhdr_size = phys_blksz;
2126 if (read_journal_header(&jnl, jnl.jhdr, phys_blksz) != (unsigned)phys_blksz) {
2127 printf("jnl: %s: is_clean: could not read %d bytes for the journal header.\n",
2133 orig_checksum = jnl.jhdr->checksum;
2134 jnl.jhdr->checksum = 0;
2136 if (jnl.jhdr->magic == SWAP32(JOURNAL_HEADER_MAGIC)) {
2139 checksum = calc_checksum((char *)jnl.jhdr, JOURNAL_HEADER_CKSUM_SIZE);
2140 swap_journal_header(&jnl);
2141 jnl.flags |= JOURNAL_NEED_SWAP;
2143 checksum = calc_checksum((char *)jnl.jhdr, JOURNAL_HEADER_CKSUM_SIZE);
2146 if (jnl.jhdr->magic != JOURNAL_HEADER_MAGIC && jnl.jhdr->magic != OLD_JOURNAL_HEADER_MAGIC) {
2147 printf("jnl: %s: is_clean: journal magic is bad (0x%x != 0x%x)\n",
2148 jdev_name, jnl.jhdr->magic, JOURNAL_HEADER_MAGIC);
2154 printf("jnl: %s: is_clean: journal checksum is bad (0x%x != 0x%x)\n", jdev_name, orig_checksum, checksum);
2163 if (jnl.jhdr->start == jnl.jhdr->end) {
2170 kmem_free(kernel_map, (vm_offset_t)jnl.header_buf, phys_blksz);
2181 journal_close(journal *jnl)
2186 CHECK_JOURNAL(jnl);
2191 jnl->flags |= JOURNAL_CLOSE_PENDING;
2193 if (jnl->owner != current_thread()) {
2194 lock_journal(jnl);
2197 wait_condition(jnl, &jnl->flushing, "journal_close");
2202 if ((jnl->flags & JOURNAL_INVALID) == 0) {
2204 if (jnl->active_tr) {
2208 journal_end_transaction(jnl);
2212 if (jnl->cur_tr) {
2213 transaction *tr = jnl->cur_tr;
2215 jnl->cur_tr = NULL;
2226 wait_condition(jnl, &jnl->flushing, "journal_close");
2228 //start = &jnl->jhdr->start;
2229 start = &jnl->active_start;
2230 end = &jnl->jhdr->end;
2233 //printf("jnl: close: flushing the buffer cache (start 0x%llx end 0x%llx)\n", *start, *end);
2234 if (jnl->flush) {
2235 jnl->flush(jnl->flush_arg);
2237 tsleep((caddr_t)jnl, PRIBIO, "jnl_close", 2);
2241 printf("jnl: %s: close: buffer flushing didn't seem to flush out all the transactions! (0x%llx - 0x%llx)\n",
2242 jnl->jdev_name, *start, *end);
2246 jnl->jhdr->start = jnl->active_start;
2249 write_journal_header(jnl, 1, jnl->sequence_num);
2253 printf("jnl: %s: close: journal %p, is invalid. aborting outstanding transactions\n", jnl->jdev_name, jnl);
2255 if (jnl->active_tr || jnl->cur_tr) {
2258 if (jnl->active_tr) {
2259 tr = jnl->active_tr;
2260 jnl->active_tr = NULL;
2262 tr = jnl->cur_tr;
2263 jnl->cur_tr = NULL;
2265 abort_transaction(jnl, tr);
2267 if (jnl->active_tr || jnl->cur_tr) {
2268 panic("jnl: %s: close: jnl @ %p had both an active and cur tr\n", jnl->jdev_name, jnl);
2273 free_old_stuff(jnl);
2275 kmem_free(kernel_map, (vm_offset_t)jnl->header_buf, jnl->header_buf_size);
2276 jnl->jhdr = (void *)0xbeefbabe;
2278 if (jnl->jdev_name) {
2279 vfs_removename(jnl->jdev_name);
2282 FREE_ZONE(jnl, sizeof(struct journal), M_JNL_JNL);
2286 dump_journal(journal *jnl)
2290 printf("journal for dev %s:", jnl->jdev_name);
2291 printf(" jdev_offset %.8llx\n", jnl->jdev_offset);
2292 printf(" magic: 0x%.8x\n", jnl->jhdr->magic);
2293 printf(" start: 0x%.8llx\n", jnl->jhdr->start);
2294 printf(" end: 0x%.8llx\n", jnl->jhdr->end);
2295 printf(" size: 0x%.8llx\n", jnl->jhdr->size);
2296 printf(" blhdr size: %d\n", jnl->jhdr->blhdr_size);
2297 printf(" jhdr size: %d\n", jnl->jhdr->jhdr_size);
2298 printf(" chksum: 0x%.8x\n", jnl->jhdr->checksum);
2301 for (ctr = jnl->completed_trs; ctr; ctr = ctr->next) {
2309 free_space(journal *jnl)
2313 if (jnl->jhdr->start < jnl->jhdr->end) {
2314 free_space_offset = jnl->jhdr->size - (jnl->jhdr->end - jnl->jhdr->start) - jnl->jhdr->jhdr_size;
2315 } else if (jnl->jhdr->start > jnl->jhdr->end) {
2316 free_space_offset = jnl->jhdr->start - jnl->jhdr->end;
2319 free_space_offset = jnl->jhdr->size - jnl->jhdr->jhdr_size;
2331 check_free_space(journal *jnl, int desired_size, boolean_t *delayed_header_write, uint32_t sequence_num)
2336 //printf("jnl: check free space (desired 0x%x, avail 0x%Lx)\n",
2337 // desired_size, free_space(jnl));
2346 if (free_space(jnl) > desired_size && jnl->old_start[0] == 0) {
2350 dump_journal(jnl);
2351 panic("jnl: check_free_space: buffer flushing isn't working "
2352 "(jnl @ %p s %lld e %lld f %lld [active start %lld]).\n", jnl,
2353 jnl->jhdr->start, jnl->jhdr->end, free_space(jnl), jnl->active_start);
2356 printf("jnl: %s: check_free_space: giving up waiting for free space.\n", jnl->jdev_name);
2361 // here's where we lazily bump up jnl->jhdr->start. we'll consume
2365 lock_oldstart(jnl);
2367 for (i = 0; i < sizeof(jnl->old_start)/sizeof(jnl->old_start[0]); i++) {
2371 while (jnl->old_start[i] & 0x8000000000000000LL) {
2373 panic("jnl: check_free_space: tr starting @ 0x%llx not flushing (jnl %p).\n",
2374 jnl->old_start[i], jnl);
2377 unlock_oldstart(jnl);
2378 if (jnl->flush) {
2379 jnl->flush(jnl->flush_arg);
2381 tsleep((caddr_t)jnl, PRIBIO, "check_free_space1", 1);
2382 lock_oldstart(jnl);
2385 if (jnl->old_start[i] == 0) {
2390 jnl->jhdr->start = jnl->old_start[i];
2391 jnl->old_start[i] = 0;
2393 if (free_space(jnl) > desired_size) {
2398 unlock_oldstart(jnl);
2399 write_journal_header(jnl, 1, sequence_num);
2400 lock_oldstart(jnl);
2405 unlock_oldstart(jnl);
2408 if (i < sizeof(jnl->old_start)/sizeof(jnl->old_start[0])) {
2420 jnl->jhdr->start = jnl->active_start;
2425 write_journal_header(jnl, 1, sequence_num);
2433 if (jnl->flush) {
2434 jnl->flush(jnl->flush_arg);
2439 tsleep((caddr_t)jnl, PRIBIO, "check_free_space2", 1);
2449 journal_allocate_transaction(journal *jnl)
2456 tr->tbuffer_size = jnl->tbuffer_size;
2460 jnl->active_tr = NULL;
2467 memset(tr->tbuffer + BLHDR_CHECKSUM_SIZE, 0x5a, jnl->jhdr->blhdr_size - BLHDR_CHECKSUM_SIZE);
2470 tr->blhdr->max_blocks = (jnl->jhdr->blhdr_size / sizeof(block_info)) - 1;
2472 tr->blhdr->bytes_used = jnl->jhdr->blhdr_size;
2475 tr->sequence_num = ++jnl->sequence_num;
2477 tr->total_bytes = jnl->jhdr->blhdr_size;
2478 tr->jnl = jnl;
2480 jnl->active_tr = tr;
2486 journal_start_transaction(journal *jnl)
2490 CHECK_JOURNAL(jnl);
2492 free_old_stuff(jnl);
2494 if (jnl->flags & JOURNAL_INVALID) {
2497 if (jnl->owner == current_thread()) {
2498 if (jnl->active_tr == NULL) {
2499 panic("jnl: start_tr: active_tr is NULL (jnl @ %p, owner %p, current_thread %p\n",
2500 jnl, jnl->owner, current_thread());
2502 jnl->nested_count++;
2505 lock_journal(jnl);
2507 if (jnl->owner != NULL || jnl->nested_count != 0 || jnl->active_tr != NULL) {
2508 panic("jnl: start_tr: owner %p, nested count %d, active_tr %p jnl @ %p\n",
2509 jnl->owner, jnl->nested_count, jnl->active_tr, jnl);
2512 jnl->owner = current_thread();
2513 jnl->nested_count = 1;
2517 if (free_space(jnl) < jnl->tbuffer_size) {
2519 KERNEL_DEBUG(0xbbbbc030 | DBG_FUNC_START, jnl, 0, 0, 0, 0);
2522 // as well as updating jnl->jhdr->start
2523 if (check_free_space(jnl, jnl->tbuffer_size, NULL, jnl->sequence_num) != 0) {
2524 printf("jnl: %s: start transaction failed: no space\n", jnl->jdev_name);
2528 KERNEL_DEBUG(0xbbbbc030 | DBG_FUNC_END, jnl, 0, 0, 0, 0);
2533 if (jnl->cur_tr) {
2534 jnl->active_tr = jnl->cur_tr;
2535 jnl->cur_tr = NULL;
2540 ret = journal_allocate_transaction(jnl);
2545 // printf("jnl: start_tr: owner 0x%x new tr @ 0x%x\n", jnl->owner, jnl->active_tr);
2550 jnl->owner = NULL;
2551 jnl->nested_count = 0;
2552 unlock_journal(jnl);
2559 journal_modify_block_start(journal *jnl, struct buf *bp)
2563 CHECK_JOURNAL(jnl);
2566 free_old_stuff(jnl);
2568 if (jnl->flags & JOURNAL_INVALID) {
2575 panic("jnl: modify_block_start: bp @ %p is not a meta-data block! (jnl %p)\n", bp, jnl);
2578 tr = jnl->active_tr;
2581 if (jnl->owner != current_thread()) {
2582 panic("jnl: modify_block_start: called w/out a transaction! jnl %p, owner %p, curact %p\n",
2583 jnl, jnl->owner, current_thread());
2586 //printf("jnl: mod block start (bp 0x%x vp 0x%x l/blkno %qd/%qd bsz %d; total bytes %d)\n",
2591 if ((buf_size(bp) % jnl->jhdr->jhdr_size) != 0) {
2594 if (VNOP_IOCTL(jnl->jdev, DKIOCGETBLOCKSIZE, (caddr_t)&phys_blksz, 0, vfs_context_kernel())) {
2596 } else if (phys_blksz != (uint32_t)jnl->jhdr->jhdr_size) {
2598 panic("jnl: mod block start: phys blksz %d is too small (%d, %d)\n",
2599 phys_blksz, buf_size(bp), jnl->jhdr->jhdr_size);
2604 } else if (phys_blksz < (uint32_t)jnl->jhdr->jhdr_size) {
2605 jnl->jhdr->jhdr_size = phys_blksz;
2610 printf("jnl: %s: phys blksz got bigger (was: %d/%d now %d)\n",
2611 jnl->jdev_name, jnl->header_buf_size, jnl->jhdr->jhdr_size, phys_blksz);
2613 printf("jnl: modify_block_start: %s: create: phys blksz change (was %d, now %d) but could not allocate space for new header\n",
2614 jnl->jdev_name, jnl->jhdr->jhdr_size, phys_blksz);
2617 memcpy(new_header_buf, jnl->header_buf, jnl->header_buf_size);
2618 memset(&new_header_buf[jnl->header_buf_size], 0x18, (phys_blksz - jnl->header_buf_size));
2619 kmem_free(kernel_map, (vm_offset_t)jnl->header_buf, jnl->header_buf_size);
2620 jnl->header_buf = new_header_buf;
2621 jnl->header_buf_size = phys_blksz;
2623 jnl->jhdr = (journal_header *)jnl->header_buf;
2624 jnl->jhdr->jhdr_size = phys_blksz;
2632 panic("jnl: mod block start: bufsize %d not a multiple of block size %d\n",
2633 buf_size(bp), jnl->jhdr->jhdr_size);
2639 if (tr->total_bytes+buf_size(bp) >= (jnl->jhdr->size - jnl->jhdr->jhdr_size)) {
2640 panic("jnl: transaction too big (%d >= %lld bytes, bufsize %d, tr %p bp %p)\n",
2641 tr->total_bytes, (tr->jnl->jhdr->size - jnl->jhdr->jhdr_size), buf_size(bp), tr, bp);
2667 journal_modify_block_abort(journal *jnl, struct buf *bp)
2673 CHECK_JOURNAL(jnl);
2675 free_old_stuff(jnl);
2677 tr = jnl->active_tr;
2689 if (jnl->flags & JOURNAL_INVALID) {
2697 if (jnl->owner != current_thread()) {
2698 panic("jnl: modify_block_abort: called w/out a transaction! jnl %p, owner %p, curact %p\n",
2699 jnl, jnl->owner, current_thread());
2702 // printf("jnl: modify_block_abort: tr 0x%x bp 0x%x\n", jnl->active_tr, bp);
2734 journal_modify_block_end(journal *jnl, struct buf *bp, void (*func)(buf_t bp, void *arg), void *arg)
2741 CHECK_JOURNAL(jnl);
2743 free_old_stuff(jnl);
2745 if (jnl->flags & JOURNAL_INVALID) {
2751 tr = jnl->active_tr;
2754 if (jnl->owner != current_thread()) {
2755 panic("jnl: modify_block_end: called w/out a transaction! jnl %p, owner %p, curact %p\n",
2756 jnl, jnl->owner, current_thread());
2759 //printf("jnl: mod block end: (bp 0x%x vp 0x%x l/blkno %qd/%qd bsz %d, total bytes %d)\n",
2763 panic("jnl: modify_block_end: bp %p not locked! jnl @ %p\n", bp, jnl);
2768 tbuffer_offset = jnl->jhdr->blhdr_size;
2795 panic("jnl: modify block end: no way man, prev == NULL?!?, jnl %p, bp %p\n", jnl, bp);
2806 panic("jnl: end_tr: no space for new block tr @ %p (total bytes: %d)!\n",
2813 memset(nblhdr + BLHDR_CHECKSUM_SIZE, 0x5a, jnl->jhdr->blhdr_size - BLHDR_CHECKSUM_SIZE);
2816 nblhdr->max_blocks = (jnl->jhdr->blhdr_size / sizeof(block_info)) - 1;
2818 nblhdr->bytes_used = jnl->jhdr->blhdr_size;
2822 tr->total_bytes += jnl->jhdr->blhdr_size;
2829 tbuffer_offset = jnl->jhdr->blhdr_size;
2835 panic("jnl: modify_block_end: i = %d, max_blocks %d\n", i, blhdr->max_blocks);
2855 panic("jnl: modify_block_end: old func %p / arg %p (func %p)", old_func, old_arg, func);
2870 journal_kill_block(journal *jnl, struct buf *bp)
2877 CHECK_JOURNAL(jnl);
2879 free_old_stuff(jnl);
2881 if (jnl->flags & JOURNAL_INVALID) {
2885 tr = jnl->active_tr;
2888 if (jnl->owner != current_thread()) {
2889 panic("jnl: modify_block_end: called w/out a transaction! jnl %p, owner %p, curact %p\n",
2890 jnl, jnl->owner, current_thread());
2896 panic("jnl: modify_block_end: called with bp not B_LOCKED");
2920 // panic("jnl: kill block: this defies all logic! bp 0x%x\n", bp);
2959 ; jnl - The journal structure for the filesystem.
2965 journal_trim_set_callback(journal *jnl, jnl_trim_callback_t callback, void *arg)
2967 jnl->trim_callback = callback;
2968 jnl->trim_callback_arg = arg;
3007 printf("jnl: trim_realloc: unable to grow extent list!\n");
3098 ; jnl - The journal for the volume containing the byte range.
3104 journal_trim_add_extent(journal *jnl, uint64_t offset, uint64_t length)
3112 CHECK_JOURNAL(jnl);
3115 if (jnl->flags & JOURNAL_INVALID) {
3119 tr = jnl->active_tr;
3123 KERNEL_DEBUG_CONSTANT(DBG_JOURNAL_TRIM_ADD | DBG_FUNC_START, jnl, offset, length, tr->trim.extent_count, 0);
3125 if (jnl->owner != current_thread()) {
3126 panic("jnl: trim_add_extent: called w/out a transaction! jnl %p, owner %p, curact %p\n",
3127 jnl, jnl->owner, current_thread());
3130 free_old_stuff(jnl);
3160 printf("jnl: trim_add_extent: out of memory!");
3289 printf("jnl: trim_remove_extent: out of memory!");
3362 ; jnl - The journal for the volume containing the byte range.
3368 journal_trim_remove_extent(journal *jnl, uint64_t offset, uint64_t length)
3373 CHECK_JOURNAL(jnl);
3376 if (jnl->flags & JOURNAL_INVALID) {
3380 tr = jnl->active_tr;
3384 KERNEL_DEBUG_CONSTANT(DBG_JOURNAL_TRIM_REMOVE | DBG_FUNC_START, jnl, offset, length, tr->trim.extent_count, 0);
3386 if (jnl->owner != current_thread()) {
3387 panic("jnl: trim_remove_extent: called w/out a transaction! jnl %p, owner %p, curact %p\n",
3388 jnl, jnl->owner, current_thread());
3391 free_old_stuff(jnl);
3401 lck_rw_lock_shared(&jnl->trim_lock);
3402 if (jnl->async_trim != NULL)
3403 found = trim_search_extent(jnl->async_trim, offset, length);
3404 lck_rw_unlock_shared(&jnl->trim_lock);
3416 KERNEL_DEBUG_CONSTANT(DBG_JOURNAL_TRIM_REMOVE_PENDING | DBG_FUNC_START, jnl, offset, length, 0, 0);
3417 lck_rw_lock_exclusive(&jnl->trim_lock);
3418 if (jnl->async_trim != NULL) {
3419 error = trim_remove_extent(jnl->async_trim, offset, length);
3420 async_extent_count = jnl->async_trim->extent_count;
3422 lck_rw_unlock_exclusive(&jnl->trim_lock);
3435 journal_trim_flush(journal *jnl, transaction *tr)
3440 KERNEL_DEBUG_CONSTANT(DBG_JOURNAL_TRIM_FLUSH | DBG_FUNC_START, jnl, tr, 0, tr->trim.extent_count, 0);
3442 lck_rw_lock_shared(&jnl->trim_lock);
3447 if (CONFIG_HFS_TRIM && (jnl->flags & JOURNAL_USE_UNMAP)) {
3451 KERNEL_DEBUG_CONSTANT(DBG_JOURNAL_TRIM_UNMAP | DBG_FUNC_START, jnl, tr, 0, tr->trim.extent_count, 0);
3452 errno = VNOP_IOCTL(jnl->fsdev, DKIOCUNMAP, (caddr_t)&unmap, FWRITE, vfs_context_kernel());
3456 printf("jnl: error %d from DKIOCUNMAP (extents=%lx, count=%u); disabling trim for %s\n",
3458 jnl->jdev_name);
3459 jnl->flags &= ~JOURNAL_USE_UNMAP;
3472 if (jnl->trim_callback)
3473 jnl->trim_callback(jnl->trim_callback_arg, tr->trim.extent_count, tr->trim.extents);
3475 lck_rw_unlock_shared(&jnl->trim_lock);
3487 lck_rw_lock_exclusive(&jnl->trim_lock);
3488 if (jnl->async_trim == &tr->trim)
3489 jnl->async_trim = NULL;
3490 lck_rw_unlock_exclusive(&jnl->trim_lock);
3569 journal *jnl = tr->jnl;
3574 if (jnl->cur_tr) {
3575 panic("jnl: jnl @ %p already has cur_tr %p, new tr: %p\n",
3576 jnl, jnl->cur_tr, tr);
3581 if (tr->total_bytes == jnl->jhdr->blhdr_size) {
3582 jnl->cur_tr = tr;
3600 && (jnl->flags & JOURNAL_NO_GROUP_COMMIT) == 0
3603 && (!(jnl->flags & JOURNAL_USE_UNMAP) || (tr->trim.extent_count < jnl_trim_flush_limit))) {
3605 jnl->cur_tr = tr;
3609 KERNEL_DEBUG(0xbbbbc018|DBG_FUNC_START, jnl, tr, drop_lock, must_wait, 0);
3611 lock_condition(jnl, &jnl->flushing, "end_transaction");
3620 if ((jnl->flags & JOURNAL_INVALID) || jnl->flush_aborted == TRUE) {
3621 unlock_condition(jnl, &jnl->flushing);
3623 abort_transaction(jnl, tr);
3625 KERNEL_DEBUG(0xbbbbc018|DBG_FUNC_END, jnl, tr, ret_val, 0, 0);
3638 lck_rw_lock_exclusive(&jnl->trim_lock);
3639 if (jnl->async_trim != NULL)
3640 panic("jnl: end_transaction: async_trim already non-NULL!");
3642 jnl->async_trim = &tr->trim;
3643 lck_rw_unlock_exclusive(&jnl->trim_lock);
3654 jnl->saved_sequence_num = jnl->sequence_num;
3659 * space for this transaction in the journal and jnl->old_start[0]
3662 KERNEL_DEBUG(0xbbbbc030 | DBG_FUNC_START, jnl, 0, 0, 0, 0);
3664 check_free_space(jnl, tr->total_bytes, &tr->delayed_header_write, jnl->saved_sequence_num);
3666 KERNEL_DEBUG(0xbbbbc030 | DBG_FUNC_END, jnl, tr->delayed_header_write, 0, 0, 0);
3669 if (jnl->jhdr->end <= 0 || jnl->jhdr->end > jnl->jhdr->size) {
3670 panic("jnl: end_transaction: end is bogus 0x%llx (sz 0x%llx)\n",
3671 jnl->jhdr->end, jnl->jhdr->size);
3676 lock_condition(jnl, &jnl->writing_header, "end_transaction");
3683 kernel_thread_start((thread_continue_t)write_header_thread, jnl, &thread);
3685 jnl->write_header_failed = FALSE;
3689 tr->journal_start = jnl->jhdr->end;
3691 lock_oldstart(jnl);
3697 memcpy(__CAST_AWAY_QUALIFIER(&jnl->old_start[0], volatile, void *), __CAST_AWAY_QUALIFIER(&jnl->old_start[1], volatile, void *), sizeof(jnl->old_start)-sizeof(jnl->old_start[0]));
3698 jnl->old_start[sizeof(jnl->old_start)/sizeof(jnl->old_start[0]) - 1] = tr->journal_start | 0x8000000000000000LL;
3700 unlock_oldstart(jnl);
3708 tbuffer_offset = jnl->jhdr->blhdr_size;
3719 panic("jnl: inconsistent binfo (NULL bp w/bnum %lld; jnl @ %p, tr %p)\n",
3720 blhdr->binfo[i].bnum, jnl, tr);
3736 if (jnl->flags & JOURNAL_CLOSE_PENDING) {
3752 panic("jnl: end_tr: !!!DANGER!!! bp %p flags (0x%x) not LOCKED & DELWRI\n", bp, buf_flags(bp));
3764 panic("jnl: buf_create_shadow returned NULL");
3814 jnl->owner = NULL;
3815 unlock_journal(jnl);
3830 KERNEL_DEBUG(0xbbbbc018|DBG_FUNC_END, jnl, tr, ret_val, 0, 0);
3833 jnl->owner = NULL;
3834 unlock_journal(jnl);
3851 write_header_thread(journal *jnl)
3855 if (write_journal_header(jnl, 1, jnl->saved_sequence_num))
3856 jnl->write_header_failed = TRUE;
3858 jnl->write_header_failed = FALSE;
3859 unlock_condition(jnl, &jnl->writing_header);
3871 journal *jnl = tr->jnl;
3879 KERNEL_DEBUG(0xbbbbc028|DBG_FUNC_START, jnl, tr, 0, 0, 0);
3881 end = jnl->jhdr->end;
3895 tbuffer_offset = jnl->jhdr->blhdr_size;
3917 printf("jnl: %s: end_tr: bad news! bp @ %p w/null vp and l/blkno = %qd/%qd. aborting the transaction (tr %p jnl %p).\n",
3918 jnl->jdev_name, bp, lblkno, blkno, tr, jnl);
3927 if ((lblkno == blkno) && (vp != jnl->fsdev)) {
3932 printf("jnl: %s: end_tr: vnop_blktooff failed @ %p, jnl %p\n", jnl->jdev_name, bp, jnl);
3937 printf("jnl: %s: end_tr: can't blockmap the bp @ %p, jnl %p\n", jnl->jdev_name, bp, jnl);
3942 printf("jnl: %s: end_tr: blk not physically contiguous on disk@ %p, jnl %p\n", jnl->jdev_name, bp, jnl);
3981 wait_condition(jnl, &jnl->writing_header, "finish_end_transaction");
3983 if (jnl->write_header_failed == FALSE)
3984 ret = write_journal_data(jnl, &end, blhdr, amt);
4000 printf("jnl: %s: end_transaction: only wrote %d of %d bytes to the journal!\n",
4001 jnl->jdev_name, ret, amt);
4007 jnl->jhdr->end = end; // update where the journal now ends
4011 panic("jnl: end_transaction: bad tr journal start/end: 0x%llx 0x%llx\n",
4015 if (write_journal_header(jnl, 0, jnl->saved_sequence_num) != 0) {
4033 journal_trim_flush(jnl, tr);
4039 tr->num_flushed = tr->num_blhdrs * jnl->jhdr->blhdr_size;
4041 lock_condition(jnl, &jnl->asyncIO, "finish_end_transaction");
4097 lock_oldstart(jnl);
4099 tr->next = jnl->tr_freeme;
4100 jnl->tr_freeme = tr;
4102 unlock_oldstart(jnl);
4104 unlock_condition(jnl, &jnl->asyncIO);
4107 //printf("jnl: end_tr: tr @ 0x%x, jnl-blocks: 0x%llx - 0x%llx. exit!\n",
4122 jnl->flush_aborted = TRUE;
4124 unlock_condition(jnl, &jnl->flushing);
4125 lock_journal(jnl);
4127 jnl->flags |= JOURNAL_INVALID;
4128 jnl->old_start[sizeof(jnl->old_start)/sizeof(jnl->old_start[0]) - 1] &= ~0x8000000000000000LL;
4129 abort_transaction(jnl, tr); // cleans up list of extents to be trimmed
4131 unlock_journal(jnl);
4133 unlock_condition(jnl, &jnl->flushing);
4135 KERNEL_DEBUG(0xbbbbc028|DBG_FUNC_END, jnl, tr, bufs_written, ret_val, 0);
4142 lock_condition(journal *jnl, boolean_t *condition, const char *condition_name)
4145 KERNEL_DEBUG(0xbbbbc020|DBG_FUNC_START, jnl, condition, 0, 0, 0);
4147 lock_flush(jnl);
4150 msleep(condition, &jnl->flock, PRIBIO, condition_name, NULL);
4153 unlock_flush(jnl);
4155 KERNEL_DEBUG(0xbbbbc020|DBG_FUNC_END, jnl, condition, 0, 0, 0);
4159 wait_condition(journal *jnl, boolean_t *condition, const char *condition_name)
4165 KERNEL_DEBUG(0xbbbbc02c|DBG_FUNC_START, jnl, condition, 0, 0, 0);
4167 lock_flush(jnl);
4170 msleep(condition, &jnl->flock, PRIBIO, condition_name, NULL);
4172 unlock_flush(jnl);
4174 KERNEL_DEBUG(0xbbbbc02c|DBG_FUNC_END, jnl, condition, 0, 0, 0);
4178 unlock_condition(journal *jnl, boolean_t *condition)
4180 lock_flush(jnl);
4185 unlock_flush(jnl);
4189 abort_transaction(journal *jnl, transaction *tr)
4203 KERNEL_DEBUG(0xbbbbc034|DBG_FUNC_START, jnl, tr, 0, 0, 0);
4235 panic("jnl: abort_tr: got back a different bp! (bp %p should be %p, jnl %p\n",
4236 bp, tbp, jnl);
4248 KERNEL_DEBUG(0xbbbbc034|DBG_FUNC_NONE, jnl, tr, bp, 0, 0);
4255 printf("jnl: %s: abort_tr: could not find block %lld vp %p!\n",
4256 jnl->jdev_name, blhdr->binfo[i].bnum, tbp);
4277 lck_rw_lock_exclusive(&jnl->trim_lock);
4278 if (jnl->async_trim == &tr->trim)
4279 jnl->async_trim = NULL;
4280 lck_rw_unlock_exclusive(&jnl->trim_lock);
4294 KERNEL_DEBUG(0xbbbbc034|DBG_FUNC_END, jnl, tr, 0, 0, 0);
4299 journal_end_transaction(journal *jnl)
4304 CHECK_JOURNAL(jnl);
4306 free_old_stuff(jnl);
4308 if ((jnl->flags & JOURNAL_INVALID) && jnl->owner == NULL) {
4312 if (jnl->owner != current_thread()) {
4313 panic("jnl: end_tr: I'm not the owner! jnl %p, owner %p, curact %p\n",
4314 jnl, jnl->owner, current_thread());
4316 jnl->nested_count--;
4318 if (jnl->nested_count > 0) {
4320 } else if (jnl->nested_count < 0) {
4321 panic("jnl: jnl @ %p has negative nested count (%d). bad boy.\n", jnl, jnl->nested_count);
4324 if (jnl->flags & JOURNAL_INVALID) {
4325 if (jnl->active_tr) {
4326 if (jnl->cur_tr != NULL) {
4327 panic("jnl: journal @ %p has active tr (%p) and cur tr (%p)\n",
4328 jnl, jnl->active_tr, jnl->cur_tr);
4330 tr = jnl->active_tr;
4331 jnl->active_tr = NULL;
4333 abort_transaction(jnl, tr);
4335 jnl->owner = NULL;
4336 unlock_journal(jnl);
4341 tr = jnl->active_tr;
4349 jnl->active_tr = NULL;
4383 journal_flush(journal *jnl, boolean_t wait_for_IO)
4387 CHECK_JOURNAL(jnl);
4389 free_old_stuff(jnl);
4391 if (jnl->flags & JOURNAL_INVALID) {
4395 KERNEL_DEBUG(DBG_JOURNAL_FLUSH | DBG_FUNC_START, jnl, 0, 0, 0, 0);
4397 if (jnl->owner != current_thread()) {
4398 lock_journal(jnl);
4403 if (jnl->active_tr == NULL && jnl->cur_tr) {
4404 transaction *tr = jnl->cur_tr;
4406 jnl->cur_tr = NULL;
4409 wait_condition(jnl, &jnl->flushing, "journal_flush");
4410 wait_condition(jnl, &jnl->asyncIO, "journal_flush");
4422 unlock_journal(jnl);
4432 wait_condition(jnl, &jnl->flushing, "journal_flush");
4435 wait_condition(jnl, &jnl->asyncIO, "journal_flush");
4438 KERNEL_DEBUG(DBG_JOURNAL_FLUSH | DBG_FUNC_END, jnl, 0, 0, 0, 0);
4444 journal_active(journal *jnl)
4446 if (jnl->flags & JOURNAL_INVALID) {
4450 return (jnl->active_tr == NULL) ? 0 : 1;
4454 journal_owner(journal *jnl)
4456 return jnl->owner;
4459 int journal_uses_fua(journal *jnl)
4461 if (jnl->flags & JOURNAL_DO_FUA_WRITES)
4502 * jnl The (opened) journal to relocate.
4522 int journal_relocate(journal *jnl, off_t offset, off_t journal_size, int32_t tbuffer_size,
4532 if ((offset % jnl->jhdr->jhdr_size) != 0) {
4533 printf("jnl: %s: relocate: offset 0x%llx is not an even multiple of block size 0x%x\n",
4534 jnl->jdev_name, offset, jnl->jhdr->jhdr_size);
4537 if ((journal_size % jnl->jhdr->jhdr_size) != 0) {
4538 printf("jnl: %s: relocate: journal size 0x%llx is not an even multiple of block size 0x%x\n",
4539 jnl->jdev_name, journal_size, jnl->jhdr->jhdr_size);
4543 CHECK_JOURNAL(jnl);
4546 if (jnl->flags & JOURNAL_INVALID) {
4549 if (jnl->owner != current_thread()) {
4550 panic("jnl: relocate: Not the owner! jnl %p, owner %p, curact %p\n",
4551 jnl, jnl->owner, current_thread());
4555 tbuffer_size = jnl->tbuffer_size;
4556 size_up_tbuffer(jnl, tbuffer_size, jnl->jhdr->jhdr_size);
4563 tr = jnl->active_tr;
4565 jnl->active_tr = NULL;
4566 ret = journal_flush(jnl, TRUE);
4567 jnl->active_tr = tr;
4572 wait_condition(jnl, &jnl->flushing, "end_transaction");
4584 lock_oldstart(jnl);
4585 for (i = 0; i < sizeof (jnl->old_start) / sizeof(jnl->old_start[0]); i++) {
4586 jnl->old_start[i] = 0;
4588 unlock_oldstart(jnl);
4591 jnl->jdev_offset = offset;
4592 jnl->jhdr->start = jnl->jhdr->end = jnl->jhdr->jhdr_size;
4593 jnl->jhdr->size = journal_size;
4594 jnl->active_start = jnl->jhdr->start;
4601 jnl->active_tr = NULL;
4604 printf("jnl: %s: relocate: end_transaction failed (%d)\n", jnl->jdev_name, ret);
4612 ret = journal_allocate_transaction(jnl);
4614 printf("jnl: %s: relocate: could not allocate new transaction (%d)\n", jnl->jdev_name, ret);
4621 jnl->flags |= JOURNAL_INVALID;
4622 abort_transaction(jnl, tr);
4629 int journal_uses_fua(__unused journal *jnl)
4664 journal_modify_block_start(__unused journal *jnl, __unused struct buf *bp)
4670 journal_modify_block_end(__unused journal *jnl,
4679 journal_kill_block(__unused journal *jnl, __unused struct buf *bp)
4684 int journal_relocate(__unused journal *jnl,
4695 journal_close(__unused journal *jnl)
4700 journal_start_transaction(__unused journal *jnl)
4706 journal_end_transaction(__unused journal *jnl)
4712 journal_flush(__unused journal *jnl, __unused boolean_t wait_for_IO)
4729 journal_owner(__unused journal *jnl)