int subprocess_flush_io (struct subprocess *p) { zio_flush (p->zio_in); while (zio_read (p->zio_out) > 0) {}; while (zio_read (p->zio_err) > 0) {}; return (0); }
static int zio_read_cb_common (zio_t *zio) { int rc = zio_read (zio); if ((rc < 0) && (errno == EAGAIN)) rc = 0; return (rc); }
/* * Read in the data for the dmu_sync()ed block, and change the log * record to write this whole block. */ void zil_get_replay_data(zilog_t *zilog, lr_write_t *lr) { blkptr_t *wbp = &lr->lr_blkptr; char *wbuf = (char *)(lr + 1); /* data follows lr_write_t */ uint64_t blksz; if (BP_IS_HOLE(wbp)) { /* compressed to a hole */ blksz = BP_GET_LSIZE(&lr->lr_blkptr); /* * If the blksz is zero then we must be replaying a log * from an version prior to setting the blksize of null blocks. * So we just zero the actual write size reqeusted. */ if (blksz == 0) { bzero(wbuf, lr->lr_length); return; } bzero(wbuf, blksz); } else { /* * A subsequent write may have overwritten this block, in which * case wbp may have been been freed and reallocated, and our * read of wbp may fail with a checksum error. We can safely * ignore this because the later write will provide the * correct data. */ zbookmark_t zb; zb.zb_objset = dmu_objset_id(zilog->zl_os); zb.zb_object = lr->lr_foid; zb.zb_level = 0; zb.zb_blkid = -1; /* unknown */ blksz = BP_GET_LSIZE(&lr->lr_blkptr); (void) zio_wait(zio_read(NULL, zilog->zl_spa, wbp, wbuf, blksz, NULL, NULL, ZIO_PRIORITY_SYNC_READ, ZIO_FLAG_CANFAIL | ZIO_FLAG_SPECULATIVE, &zb)); } lr->lr_offset -= lr->lr_offset % blksz; lr->lr_length = blksz; }
/* * Set monochrome bitmap picture as the current binary data. */ EB_Error_Code eb_set_binary_gray_graphic(EB_Book *book, const EB_Position *position, int width, int height) { EB_Error_Code error_code; EB_Binary_Context *context; EB_Position real_position; unsigned char *buffer_p; size_t line_pad_length; size_t data_size; size_t file_size; eb_lock(&book->lock); LOG(("in: eb_set_binary_gray_graphic(book=%d, position={%d,%d}, \ width=%d, height=%d)", (int)book->code, position->page, position->offset, width, height)); eb_reset_binary_context(book); /* * Current subbook must have been set. */ if (book->subbook_current == NULL) { error_code = EB_ERR_NO_CUR_SUB; goto failed; } /* * Current subbook must have a graphic file. */ if (zio_file(&book->subbook_current->text_zio) < 0) { error_code = EB_ERR_NO_SUCH_BINARY; goto failed; } /* * If both width and height are 0, * we get real width, height and position of the graphic data. */ if (position->page <= 0 || position->offset < 0) { error_code = EB_ERR_FAIL_SEEK_BINARY; goto failed; } if (width == 0 && height == 0) { char buffer[22]; if (zio_lseek(&book->subbook_current->text_zio, ((off_t) position->page - 1) * EB_SIZE_PAGE + position->offset, SEEK_SET) < 0) { error_code = EB_ERR_FAIL_SEEK_BINARY; goto failed; } if (zio_read(&book->subbook_current->text_zio, buffer, 22) != 22) { error_code = EB_ERR_FAIL_READ_BINARY; goto failed; } if (eb_uint2(buffer) != 0x1f45 || eb_uint2(buffer + 4) != 0x1f31 || eb_uint2(buffer + 12) != 0x1f51 || eb_uint2(buffer + 20) != 0x1f65) { error_code = EB_ERR_UNEXP_BINARY; goto failed; } width = eb_bcd2(buffer + 8); height = eb_bcd2(buffer + 10); real_position.page = eb_bcd4(buffer + 14); real_position.offset = eb_bcd2(buffer + 18); position = &real_position; } if (width <= 0 || height <= 0) { error_code = EB_ERR_NO_SUCH_BINARY; goto failed; } /* * BMP requires that the number of bytes in a line must be multiple * of 4. If not, 0x00 must be padded to end of each line. * `line_pad_length' (0...3) is the number of bytes to be padded. * * In case of EB_BINARY_GRAY_GRAPHIC, a pixel is represented with * 4 bits. */ if (width % 8 == 0) line_pad_length = 0; else if (width % 8 <= 2) line_pad_length = 3; else if (width % 8 <= 4) line_pad_length = 2; else if (width % 8 <= 6) line_pad_length = 1; else line_pad_length = 0; data_size = (width / 2 + line_pad_length) * height; file_size = data_size + MONO_BMP_PREAMBLE_LENGTH; /* * Set binary context. */ context = &book->binary_context; context->code = EB_BINARY_GRAY_GRAPHIC; context->zio = &book->subbook_current->text_zio; context->location = ((off_t) position->page - 1) * EB_SIZE_PAGE + position->offset + (width + 1) / 2 * (height - 1); context->size = (width + 1) / 2 * height; context->offset = 0; context->cache_offset = 0; context->width = width; /* * Set BMP preamble. */ context->cache_length = GRAY_BMP_PREAMBLE_LENGTH; memcpy(context->cache_buffer, gray_bmp_preamble, GRAY_BMP_PREAMBLE_LENGTH); buffer_p = (unsigned char *)context->cache_buffer + 2; *buffer_p++ = file_size & 0xff; *buffer_p++ = (file_size >> 8) & 0xff; *buffer_p++ = (file_size >> 16) & 0xff; *buffer_p++ = (file_size >> 24) & 0xff; buffer_p = (unsigned char *)context->cache_buffer + 18; *buffer_p++ = width & 0xff; *buffer_p++ = (width >> 8) & 0xff; *buffer_p++ = (width >> 16) & 0xff; *buffer_p++ = (width >> 24) & 0xff; *buffer_p++ = height & 0xff; *buffer_p++ = (height >> 8) & 0xff; *buffer_p++ = (height >> 16) & 0xff; *buffer_p++ = (height >> 24) & 0xff; buffer_p = (unsigned char *)context->cache_buffer + 34; *buffer_p++ = data_size & 0xff; *buffer_p++ = (data_size >> 8) & 0xff; *buffer_p++ = (data_size >> 16) & 0xff; *buffer_p++ = (data_size >> 24) & 0xff; /* * Seek graphic file. */ if (zio_lseek(context->zio, context->location, SEEK_SET) < 0) { error_code = EB_ERR_FAIL_SEEK_BINARY; goto failed; } LOG(("out: eb_set_binary_gray_graphic() = %s", eb_error_string(EB_SUCCESS))); eb_unlock(&book->lock); return EB_SUCCESS; /* * An error occurs... */ failed: eb_reset_binary_context(book); LOG(("out: eb_set_binary_gray_graphic() = %s", eb_error_string(error_code))); eb_unlock(&book->lock); return error_code; }
/* ARGSUSED */ static void zil_prt_rec_write(zilog_t *zilog, int txtype, lr_write_t *lr) { char *data, *dlimit; blkptr_t *bp = &lr->lr_blkptr; zbookmark_phys_t zb; char buf[SPA_MAXBLOCKSIZE]; int verbose = MAX(dump_opt['d'], dump_opt['i']); int error; (void) printf("%sfoid %llu, offset %llx, length %llx\n", prefix, (u_longlong_t)lr->lr_foid, (u_longlong_t)lr->lr_offset, (u_longlong_t)lr->lr_length); if (txtype == TX_WRITE2 || verbose < 5) return; if (lr->lr_common.lrc_reclen == sizeof (lr_write_t)) { (void) printf("%shas blkptr, %s\n", prefix, !BP_IS_HOLE(bp) && bp->blk_birth >= spa_first_txg(zilog->zl_spa) ? "will claim" : "won't claim"); print_log_bp(bp, prefix); if (BP_IS_HOLE(bp)) { (void) printf("\t\t\tLSIZE 0x%llx\n", (u_longlong_t)BP_GET_LSIZE(bp)); bzero(buf, sizeof (buf)); (void) printf("%s<hole>\n", prefix); return; } if (bp->blk_birth < zilog->zl_header->zh_claim_txg) { (void) printf("%s<block already committed>\n", prefix); return; } SET_BOOKMARK(&zb, dmu_objset_id(zilog->zl_os), lr->lr_foid, ZB_ZIL_LEVEL, lr->lr_offset / BP_GET_LSIZE(bp)); error = zio_wait(zio_read(NULL, zilog->zl_spa, bp, buf, BP_GET_LSIZE(bp), NULL, NULL, ZIO_PRIORITY_SYNC_READ, ZIO_FLAG_CANFAIL, &zb)); if (error) return; data = buf; } else { data = (char *)(lr + 1); } dlimit = data + MIN(lr->lr_length, (verbose < 6 ? 20 : SPA_MAXBLOCKSIZE)); (void) printf("%s", prefix); while (data < dlimit) { if (isprint(*data)) (void) printf("%c ", *data); else (void) printf("%2hhX", *data); data++; } (void) printf("\n"); }
/* * Internal function for ebzip_unzip_file() and ebzip_unzip_sebxa_start(). * If it succeeds, 0 is returned. Otherwise -1 is returned. */ static int ebzip_unzip_file_internal(const char *out_file_name, const char *in_file_name, Zio_Code in_zio_code, int index_page) { Zio in_zio; unsigned char *buffer = NULL; off_t total_length; int out_file = -1; ssize_t length; struct stat in_status, out_status; unsigned int crc = 1; int progress_interval; int total_slices; int i; zio_initialize(&in_zio); /* * Simply copy a file, when an input file is not compressed. */ if (in_zio_code == ZIO_PLAIN) return ebzip_copy_file(out_file_name, in_file_name); /* * Output file name information. */ if (!ebzip_quiet_flag) { fprintf(stderr, _("==> uncompress %s <==\n"), in_file_name); fprintf(stderr, _("output to %s\n"), out_file_name); fflush(stderr); } /* * Get status of the input file. */ if (stat(in_file_name, &in_status) < 0 || !S_ISREG(in_status.st_mode)) { fprintf(stderr, _("%s: no such file: %s\n"), invoked_name, in_file_name); goto failed; } /* * Do nothing if the `in_file_name' and `out_file_name' are the same. */ if (is_same_file(out_file_name, in_file_name)) { if (!ebzip_quiet_flag) { fprintf(stderr, _("the input and output files are the same, skipped.\n\n")); fflush(stderr); } return 0; } /* * Allocate memories for in/out buffers. */ buffer = (unsigned char *)malloc(EB_SIZE_PAGE << ZIO_MAX_EBZIP_LEVEL); if (buffer == NULL) { fprintf(stderr, _("%s: memory exhausted\n"), invoked_name); goto failed; } /* * If the file `out_file_name' already exists, confirm and unlink it. */ if (!ebzip_test_flag && stat(out_file_name, &out_status) == 0 && S_ISREG(out_status.st_mode)) { if (ebzip_overwrite_mode == EBZIP_OVERWRITE_NO) { if (!ebzip_quiet_flag) { fputs(_("already exists, skip the file\n\n"), stderr); fflush(stderr); } return 0; } else if (ebzip_overwrite_mode == EBZIP_OVERWRITE_CONFIRM) { int y_or_n; fprintf(stderr, _("\nthe file already exists: %s\n"), out_file_name); y_or_n = query_y_or_n(_("do you wish to overwrite (y or n)? ")); fputc('\n', stderr); fflush(stderr); if (!y_or_n) return 0; } if (unlink(out_file_name) < 0) { fprintf(stderr, _("%s: failed to unlink the file: %s\n"), invoked_name, out_file_name); goto failed; } } /* * Open files. */ if (zio_open(&in_zio, in_file_name, in_zio_code) < 0) { fprintf(stderr, _("%s: failed to open the file: %s\n"), invoked_name, in_file_name); goto failed; } if (in_zio_code == ZIO_SEBXA) { off_t index_location; off_t index_base; off_t zio_start_location; off_t zio_end_location; if (get_sebxa_indexes(in_file_name, index_page, &index_location, &index_base, &zio_start_location, &zio_end_location) < 0) { goto failed; } zio_set_sebxa_mode(&in_zio, index_location, index_base, zio_start_location, zio_end_location); } if (!ebzip_test_flag) { trap_file_name = out_file_name; #ifdef SIGHUP signal(SIGHUP, trap); #endif signal(SIGINT, trap); #ifdef SIGQUIT signal(SIGQUIT, trap); #endif #ifdef SIGTERM signal(SIGTERM, trap); #endif #ifdef O_CREAT out_file = open(out_file_name, O_CREAT | O_TRUNC | O_WRONLY | O_BINARY, 0666 ^ get_umask()); #else out_file = creat(out_file_name, 0666 ^ get_umask()); #endif if (out_file < 0) { fprintf(stderr, _("%s: failed to open the file: %s\n"), invoked_name, out_file_name); goto failed; } trap_file = out_file; } /* * Read a slice from the input file, uncompress it if required, * and then write it to the output file. */ total_length = 0; total_slices = (in_zio.file_size + in_zio.slice_size - 1) / in_zio.slice_size; progress_interval = EBZIP_PROGRESS_INTERVAL_FACTOR; if (((total_slices + 999) / 1000) > progress_interval) progress_interval = ((total_slices + 999) / 1000); for (i = 0; i < total_slices; i++) { /* * Read a slice. */ if (zio_lseek(&in_zio, total_length, SEEK_SET) < 0) { fprintf(stderr, _("%s: failed to seek the file: %s\n"), invoked_name, in_file_name); goto failed; } length = zio_read(&in_zio, (char *)buffer, in_zio.slice_size); if (length < 0) { fprintf(stderr, _("%s: failed to read from the file: %s\n"), invoked_name, in_file_name); goto failed; } else if (length == 0) { fprintf(stderr, _("%s: unexpected EOF: %s\n"), invoked_name, in_file_name); goto failed; } else if (length != in_zio.slice_size && total_length + length != in_zio.file_size) { fprintf(stderr, _("%s: unexpected EOF: %s\n"), invoked_name, in_file_name); goto failed; } /* * Update CRC. (Calculate adler32 again.) */ if (in_zio.code == ZIO_EBZIP1) crc = adler32((uLong)crc, (Bytef *)buffer, (uInt)length); /* * Write the slice to `out_file'. */ if (!ebzip_test_flag) { if (write(out_file, buffer, length) != length) { fprintf(stderr, _("%s: failed to write to the file, %s: %s\n"), invoked_name, strerror(errno), out_file_name); goto failed; } } total_length += length; /* * Output status information unless `quiet' mode. */ if (!ebzip_quiet_flag && (i + 1) % progress_interval == 0) { #if defined(PRINTF_LL_MODIFIER) fprintf(stderr, _("%4.1f%% done (%llu / %llu bytes)\n"), (double) (i + 1) * 100.0 / (double) total_slices, (unsigned long long) total_length, (unsigned long long) in_zio.file_size); #elif defined(PRINTF_I64_MODIFIER) fprintf(stderr, _("%4.1f%% done (%I64u / %I64u bytes)\n"), (double) (i + 1) * 100.0 / (double) total_slices, (unsigned __int64) total_length, (unsigned __int64) in_zio.file_size); #else fprintf(stderr, _("%4.1f%% done (%lu / %lu bytes)\n"), (double) (i + 1) * 100.0 / (double) total_slices, (unsigned long) total_length, (unsigned long) in_zio.file_size); #endif fflush(stderr); } } /* * Output the result unless quiet mode. */ if (!ebzip_quiet_flag) { #if defined(PRINTF_LL_MODIFIER) fprintf(stderr, _("completed (%llu / %llu bytes)\n"), (unsigned long long) in_zio.file_size, (unsigned long long) in_zio.file_size); #elif defined(PRINTF_I64_MODIFIER) fprintf(stderr, _("completed (%I64u / %I64u bytes)\n"), (unsigned __int64) in_zio.file_size, (unsigned __int64) in_zio.file_size); #else fprintf(stderr, _("%lu -> %lu bytes\n\n"), (unsigned long) in_status.st_size, (unsigned long) total_length); #endif fflush(stderr); } /* * Close files. */ zio_close(&in_zio); zio_finalize(&in_zio); if (!ebzip_test_flag) { close(out_file); out_file = -1; trap_file = -1; trap_file_name = NULL; #ifdef SIGHUP signal(SIGHUP, SIG_DFL); #endif signal(SIGINT, SIG_DFL); #ifdef SIGQUIT signal(SIGQUIT, SIG_DFL); #endif #ifdef SIGTERM signal(SIGTERM, SIG_DFL); #endif } /* * Check for CRC. */ if (in_zio.code == ZIO_EBZIP1 && in_zio.crc != crc) { fprintf(stderr, _("%s: CRC error: %s\n"), invoked_name, out_file_name); goto failed; } /* * Delete an original file unless the keep flag is set. */ if (!ebzip_test_flag && !ebzip_keep_flag) unlink_files_add(in_file_name); /* * Set owner, group, permission, atime and mtime of `out_file'. * We ignore return values of `chown', `chmod' and `utime'. */ if (!ebzip_test_flag) { struct utimbuf utim; utim.actime = in_status.st_atime; utim.modtime = in_status.st_mtime; utime(out_file_name, &utim); } /* * Dispose memories. */ free(buffer); return 0; /* * An error occurs... */ failed: if (buffer != NULL) free(buffer); zio_close(&in_zio); zio_finalize(&in_zio); if (0 <= out_file) { close(out_file); trap_file = -1; trap_file_name = NULL; #ifdef SIGHUP signal(SIGHUP, SIG_DFL); #endif signal(SIGINT, SIG_DFL); #ifdef SIGQUIT signal(SIGQUIT, SIG_DFL); #endif #ifdef SIGTERM signal(SIGTERM, SIG_DFL); #endif } fputc('\n', stderr); fflush(stderr); return -1; }
static void zil_replay_log_record(zilog_t *zilog, lr_t *lr, void *zra, uint64_t claim_txg) { zil_replay_arg_t *zr = zra; const zil_header_t *zh = zilog->zl_header; uint64_t reclen = lr->lrc_reclen; uint64_t txtype = lr->lrc_txtype; char *name; int pass, error; if (!zilog->zl_replay) /* giving up */ return; if (lr->lrc_txg < claim_txg) /* already committed */ return; if (lr->lrc_seq <= zh->zh_replay_seq) /* already replayed */ return; /* Strip case-insensitive bit, still present in log record */ txtype &= ~TX_CI; if (txtype == 0 || txtype >= TX_MAX_TYPE) { error = EINVAL; goto bad; } /* * Make a copy of the data so we can revise and extend it. */ bcopy(lr, zr->zr_lrbuf, reclen); /* * The log block containing this lr may have been byteswapped * so that we can easily examine common fields like lrc_txtype. * However, the log is a mix of different data types, and only the * replay vectors know how to byteswap their records. Therefore, if * the lr was byteswapped, undo it before invoking the replay vector. */ if (zr->zr_byteswap) byteswap_uint64_array(zr->zr_lrbuf, reclen); /* * If this is a TX_WRITE with a blkptr, suck in the data. */ if (txtype == TX_WRITE && reclen == sizeof (lr_write_t)) { lr_write_t *lrw = (lr_write_t *)lr; blkptr_t *wbp = &lrw->lr_blkptr; uint64_t wlen = lrw->lr_length; char *wbuf = zr->zr_lrbuf + reclen; if (BP_IS_HOLE(wbp)) { /* compressed to a hole */ bzero(wbuf, wlen); } else { /* * A subsequent write may have overwritten this block, * in which case wbp may have been been freed and * reallocated, and our read of wbp may fail with a * checksum error. We can safely ignore this because * the later write will provide the correct data. */ zbookmark_t zb; zb.zb_objset = dmu_objset_id(zilog->zl_os); zb.zb_object = lrw->lr_foid; zb.zb_level = -1; zb.zb_blkid = lrw->lr_offset / BP_GET_LSIZE(wbp); (void) zio_wait(zio_read(NULL, zilog->zl_spa, wbp, wbuf, BP_GET_LSIZE(wbp), NULL, NULL, ZIO_PRIORITY_SYNC_READ, ZIO_FLAG_CANFAIL | ZIO_FLAG_SPECULATIVE, &zb)); (void) memmove(wbuf, wbuf + lrw->lr_blkoff, wlen); } } /* * We must now do two things atomically: replay this log record, * and update the log header sequence number to reflect the fact that * we did so. At the end of each replay function the sequence number * is updated if we are in replay mode. */ for (pass = 1; pass <= 2; pass++) { zilog->zl_replaying_seq = lr->lrc_seq; /* Only byteswap (if needed) on the 1st pass. */ error = zr->zr_replay[txtype](zr->zr_arg, zr->zr_lrbuf, zr->zr_byteswap && pass == 1); if (!error) return; /* * The DMU's dnode layer doesn't see removes until the txg * commits, so a subsequent claim can spuriously fail with * EEXIST. So if we receive any error we try syncing out * any removes then retry the transaction. */ if (pass == 1) txg_wait_synced(spa_get_dsl(zilog->zl_spa), 0); } bad: ASSERT(error); name = kmem_alloc(MAXNAMELEN, KM_SLEEP); dmu_objset_name(zr->zr_os, name); cmn_err(CE_WARN, "ZFS replay transaction error %d, " "dataset %s, seq 0x%llx, txtype %llu %s\n", error, name, (u_longlong_t)lr->lrc_seq, (u_longlong_t)txtype, (lr->lrc_txtype & TX_CI) ? "CI" : ""); zilog->zl_replay = B_FALSE; kmem_free(name, MAXNAMELEN); }