X-Git-Url: https://git.sur5r.net/?a=blobdiff_plain;f=bacula%2Fsrc%2Fstored%2Fbtape.c;h=60ea8d3c5eced9fc8f6eec66f9fe73f3fac02f59;hb=6fc016ea3c4f5698462494319733199e2de9a9af;hp=c59f8dfef2d50eea56d4c6dc9f257acaf732b996;hpb=31324722fa6a602c0f97fe47deb3ed2b69dead4b;p=bacula%2Fbacula diff --git a/bacula/src/stored/btape.c b/bacula/src/stored/btape.c index c59f8dfef2..60ea8d3c5e 100644 --- a/bacula/src/stored/btape.c +++ b/bacula/src/stored/btape.c @@ -36,6 +36,8 @@ #include "bacula.h" #include "stored.h" +/* Dummy functions */ +int generate_daemon_event(JCR *jcr, const char *event) { return 1; } /* External subroutines */ extern void free_config_resources(); @@ -45,7 +47,10 @@ int quit = 0; char buf[100000]; int bsize = TAPE_BSIZE; char VolName[MAX_NAME_LENGTH]; -bool forge_on = false; +STORES *me = NULL; /* our Global resource */ +bool forge_on = false; /* proceed inspite of I/O errors */ +pthread_mutex_t device_release_mutex = PTHREAD_MUTEX_INITIALIZER; +pthread_cond_t wait_device_release = PTHREAD_COND_INITIALIZER; /* * If you change the format of the state file, @@ -138,7 +143,7 @@ int get_cmd(const char *prompt); /********************************************************************* * - * Main Bacula Pool Creation Program + * Main Bacula Pool Creation Program * */ int main(int margc, char *margv[]) @@ -149,16 +154,16 @@ int main(int margc, char *margv[]) char buf[1000]; /* Sanity checks */ - if (TAPE_BSIZE % DEV_BSIZE != 0 || TAPE_BSIZE / DEV_BSIZE == 0) { + if (TAPE_BSIZE % B_DEV_BSIZE != 0 || TAPE_BSIZE / B_DEV_BSIZE == 0) { Emsg2(M_ABORT, 0, "Tape block size (%d) not multiple of system size (%d)\n", - TAPE_BSIZE, DEV_BSIZE); + TAPE_BSIZE, B_DEV_BSIZE); } if (TAPE_BSIZE != (1 << (ffs(TAPE_BSIZE)-1))) { Emsg1(M_ABORT, 0, "Tape block size (%d) is not a power of 2\n", TAPE_BSIZE); } if (sizeof(off_t) < 8) { Pmsg1(-1, "\n\n!!!! Warning large disk addressing disabled. off_t=%d should be 8 or more !!!!!\n\n\n", - sizeof(off_t)); + sizeof(off_t)); } x32 = 123456789; bsnprintf(buf, sizeof(buf), "%u", x32); @@ -186,40 +191,40 @@ int main(int margc, char *margv[]) while ((ch = getopt(margc, margv, "b:c:d:psv?")) != -1) { switch (ch) { case 'b': /* bootstrap file */ - bsr = parse_bsr(NULL, optarg); -// dump_bsr(bsr, true); - break; + bsr = parse_bsr(NULL, optarg); +// dump_bsr(bsr, true); + break; case 'c': /* specify config file */ - if (configfile != NULL) { - free(configfile); - } - configfile = bstrdup(optarg); - break; + if (configfile != NULL) { + free(configfile); + } + configfile = bstrdup(optarg); + break; case 'd': /* set debug level */ - debug_level = atoi(optarg); - if (debug_level <= 0) { - debug_level = 1; - } - break; + debug_level = atoi(optarg); + if (debug_level <= 0) { + debug_level = 1; + } + break; case 'p': - forge_on = true; - break; + forge_on = true; + break; case 's': - signals = false; - break; + signals = false; + break; case 'v': - verbose++; - break; + verbose++; + break; case '?': default: - helpcmd(); - exit(0); + helpcmd(); + exit(0); } } @@ -311,7 +316,7 @@ static void terminate_btape(int stat) stop_watchdog(); term_msg(); - close_memory_pool(); /* free memory in pool */ + close_memory_pool(); /* free memory in pool */ sm_dump(false); exit(stat); @@ -327,13 +332,13 @@ static bool open_the_device() Dmsg1(200, "Opening device %s\n", dcr->VolumeName); if (open_dev(dev, dcr->VolumeName, OPEN_READ_WRITE) < 0) { Emsg1(M_FATAL, 0, _("dev open failed: %s\n"), dev->errmsg); - unlock_device(dev); - free_block(block); - return false; + unlock_device(dev); + free_block(block); + return false; } } - Pmsg1(000, "open_dev %s OK\n", dev_name(dev)); - dev->state |= ST_APPEND; + Pmsg1(000, "open_dev %s OK\n", dev->print_name()); + dev->set_append(); /* put volume in append mode */ unlock_device(dev); free_block(block); return true; @@ -354,7 +359,7 @@ static void labelcmd() pm_strcpy(cmd, VolumeName); } else { if (!get_cmd("Enter Volume Name: ")) { - return; + return; } } @@ -420,7 +425,7 @@ static void loadcmd() if (!load_dev(dev)) { Pmsg1(0, "Bad status from load. ERR=%s\n", strerror_dev(dev)); } else - Pmsg1(0, "Loaded %s\n", dev_name(dev)); + Pmsg1(0, "Loaded %s\n", dev->print_name()); } /* @@ -432,7 +437,7 @@ static void rewindcmd() Pmsg1(0, "Bad status from rewind. ERR=%s\n", strerror_dev(dev)); clrerror_dev(dev, -1); } else { - Pmsg1(0, "Rewound %s\n", dev_name(dev)); + Pmsg1(0, "Rewound %s\n", dev->print_name()); } } @@ -462,7 +467,7 @@ static void weofcmd() Pmsg2(0, "Bad status from weof %d. ERR=%s\n", stat, strerror_dev(dev)); return; } else { - Pmsg3(0, "Wrote %d EOF%s to %s\n", num, num==1?"":"s", dev_name(dev)); + Pmsg3(0, "Wrote %d EOF%s to %s\n", num, num==1?"":"s", dev->print_name()); } } @@ -611,11 +616,11 @@ static void rectestcmd() rec->data_len = i; sm_check(__FILE__, __LINE__, false); if (write_record_to_block(block, rec)) { - empty_block(block); - blkno++; + empty_block(block); + blkno++; Pmsg2(0, "Block %d i=%d\n", blkno, i); } else { - break; + break; } sm_check(__FILE__, __LINE__, false); } @@ -698,7 +703,7 @@ static int re_read_block_test() if (dev_cap(dev, CAP_TWOEOF)) { if (!bsf_dev(dev, 1)) { Pmsg1(0, _("Backspace file failed! ERR=%s\n"), strerror_dev(dev)); - goto bail_out; + goto bail_out; } } Pmsg0(0, "Backspaced over EOF OK.\n"); @@ -721,7 +726,7 @@ static int re_read_block_test() for (int i=0; idata[i] != 3) { Pmsg0(0, _("Bad data in record. Test failed!\n")); - goto bail_out; + goto bail_out; } } Pmsg0(0, _("\nBlock re-read correct. Test succeeded!\n")); @@ -773,15 +778,15 @@ static int write_read_test() for (i=1; i<=1000; i++) { p = (int *)rec->data; for (j=0; jdata_len); @@ -789,15 +794,15 @@ static int write_read_test() for (i=1001; i<=2000; i++) { p = (int *)rec->data; for (j=0; jdata_len); @@ -814,30 +819,30 @@ static int write_read_test() for (i=1; i<=2000; i++) { read_again: if (!read_block_from_dev(dcr, NO_BLOCK_NUMBER_CHECK)) { - berrno be; - if (dev_state(dev, ST_EOF)) { + berrno be; + if (dev_state(dev, ST_EOF)) { Pmsg0(-1, _("Got EOF on tape.\n")); - if (i == 1001) { - goto read_again; - } - } + if (i == 1001) { + goto read_again; + } + } Pmsg2(0, _("Read block %d failed! ERR=%s\n"), i, be.strerror(dev->dev_errno)); - goto bail_out; + goto bail_out; } memset(rec->data, 0, rec->data_len); if (!read_record_from_block(block, rec)) { - berrno be; + berrno be; Pmsg2(0, _("Read record failed. Block %d! ERR=%s\n"), i, be.strerror(dev->dev_errno)); - goto bail_out; + goto bail_out; } p = (int *)rec->data; for (j=0; jdata; for (j=0; jdata_len); @@ -901,15 +906,15 @@ static int position_test() for (i=1001; i<=2000; i++) { p = (int *)rec->data; for (j=0; jdata_len); @@ -928,56 +933,56 @@ static int position_test() /* Set up next item to read based on where we are */ switch (recno) { case 0: - recno = 5; - file = 0; - blk = 4; - break; + recno = 5; + file = 0; + blk = 4; + break; case 5: - recno = 201; - file = 0; - blk = 200; - break; + recno = 201; + file = 0; + blk = 200; + break; case 201: - recno = 1000; - file = 0; - blk = 999; - break; + recno = 1000; + file = 0; + blk = 999; + break; case 1000: - recno = 1001; - file = 1; - blk = 0; - break; + recno = 1001; + file = 1; + blk = 0; + break; case 1001: - recno = 1601; - file = 1; - blk = 600; - break; + recno = 1601; + file = 1; + blk = 600; + break; case 1601: - recno = 2000; - file = 1; - blk = 999; - break; + recno = 2000; + file = 1; + blk = 999; + break; case 2000: - ok = false; - continue; + ok = false; + continue; } Pmsg2(-1, "Reposition to file:block %d:%d\n", file, blk); if (!reposition_dev(dev, file, blk)) { Pmsg0(0, "Reposition error.\n"); - goto bail_out; + goto bail_out; } read_again: if (!read_block_from_dev(dcr, NO_BLOCK_NUMBER_CHECK)) { - berrno be; - if (dev_state(dev, ST_EOF)) { + berrno be; + if (dev_state(dev, ST_EOF)) { Pmsg0(-1, _("Got EOF on tape.\n")); - if (!got_eof) { - got_eof = true; - goto read_again; - } - } + if (!got_eof) { + got_eof = true; + goto read_again; + } + } Pmsg4(0, _("Read block %d failed! file=%d blk=%d. ERR=%s\n\n"), - recno, file, blk, be.strerror(dev->dev_errno)); + recno, file, blk, be.strerror(dev->dev_errno)); Pmsg0(0, _("This may be because the tape drive block size is not\n" " set to variable blocking as normally used by Bacula.\n" " Please see the Tape Testing chapter in the manual and \n" @@ -989,21 +994,21 @@ read_again: " Fast Forward Space File = no\n" " in your Device resource.\n")); - goto bail_out; + goto bail_out; } memset(rec->data, 0, rec->data_len); if (!read_record_from_block(block, rec)) { - berrno be; + berrno be; Pmsg1(0, _("Read record failed! ERR=%s\n"), be.strerror(dev->dev_errno)); - goto bail_out; + goto bail_out; } p = (int *)rec->data; for (j=0; jdrive_index); + loaded, dev->drive_index); changer = edit_device_codes(dcr, changer, "unload"); status = run_program(changer, timeout, results); Pmsg2(-1, "unload status=%s %d\n", status==0?"OK":"Bad", status); if (status != 0) { - berrno be; + berrno be; Pmsg1(-1, _("3992 Bad autochanger command: %s\n"), changer); Pmsg2(-1, _("3992 result=\"%s\": ERR=%s\n"), results, be.strerror(status)); } @@ -1167,7 +1172,7 @@ try_again: status = run_program(changer, timeout, results); if (status == 0) { Pmsg2(-1, _("3303 Autochanger \"load slot %d %d\" status is OK.\n"), - slot, dev->drive_index); + slot, dev->drive_index); } else { berrno be; Pmsg1(-1, _("3993 Bad autochanger command: %s\n"), changer); @@ -1192,21 +1197,21 @@ try_again: sleep_time += 30; goto try_again; } else { - Pmsg1(0, "Rewound %s\n", dev_name(dev)); + Pmsg1(0, "Rewound %s\n", dev->print_name()); } if ((status = weof_dev(dev, 1)) < 0) { Pmsg2(0, "Bad status from weof %d. ERR=%s\n", status, strerror_dev(dev)); goto bail_out; } else { - Pmsg1(0, "Wrote EOF to %s\n", dev_name(dev)); + Pmsg1(0, "Wrote EOF to %s\n", dev->print_name()); } if (sleep_time) { Pmsg1(-1, "\nThe test worked this time. Please add:\n\n" " sleep %d\n\n" "to your mtx-changer script in the load) case.\n\n", - sleep_time); + sleep_time); } else { Pmsg0(-1, "\nThe test autochanger worked!!\n\n"); } @@ -1243,19 +1248,19 @@ static int fsf_test() argc = 1; rewindcmd(); wrcmd(); - weofcmd(); /* end file 0 */ + weofcmd(); /* end file 0 */ wrcmd(); wrcmd(); - weofcmd(); /* end file 1 */ + weofcmd(); /* end file 1 */ wrcmd(); wrcmd(); wrcmd(); - weofcmd(); /* end file 2 */ + weofcmd(); /* end file 2 */ wrcmd(); wrcmd(); - weofcmd(); /* end file 3 */ + weofcmd(); /* end file 3 */ wrcmd(); - weofcmd(); /* end file 4 */ + weofcmd(); /* end file 4 */ if (dev_cap(dev, CAP_TWOEOF)) { weofcmd(); } @@ -1353,44 +1358,44 @@ static void testcmd() } stat = append_test(); - if (stat == 1) { /* OK get out */ + if (stat == 1) { /* OK get out */ goto all_done; } - if (stat == -1) { /* first test failed */ + if (stat == -1) { /* first test failed */ if (dev_cap(dev, CAP_EOM) || dev_cap(dev, CAP_FASTFSF)) { Pmsg0(-1, "\nAppend test failed. Attempting again.\n" "Setting \"Hardware End of Medium = no\n" " and \"Fast Forward Space File = no\n" "and retrying append test.\n\n"); - dev->capabilities &= ~CAP_EOM; /* turn off eom */ - dev->capabilities &= ~CAP_FASTFSF; /* turn off fast fsf */ - stat = append_test(); - if (stat == 1) { + dev->capabilities &= ~CAP_EOM; /* turn off eom */ + dev->capabilities &= ~CAP_FASTFSF; /* turn off fast fsf */ + stat = append_test(); + if (stat == 1) { Pmsg0(-1, "\n\nIt looks like the test worked this time, please add:\n\n" " Hardware End of Medium = No\n\n" " Fast Forward Space File = No\n" "to your Device resource in the Storage conf file.\n"); - goto all_done; - } - if (stat == -1) { + goto all_done; + } + if (stat == -1) { Pmsg0(-1, "\n\nThat appears *NOT* to have corrected the problem.\n"); - goto failed; - } - /* Wrong count after append */ - if (stat == -2) { + goto failed; + } + /* Wrong count after append */ + if (stat == -2) { Pmsg0(-1, "\n\nIt looks like the append failed. Attempting again.\n" "Setting \"BSF at EOM = yes\" and retrying append test.\n"); - dev->capabilities |= CAP_BSFATEOM; /* backspace on eom */ - stat = append_test(); - if (stat == 1) { + dev->capabilities |= CAP_BSFATEOM; /* backspace on eom */ + stat = append_test(); + if (stat == 1) { Pmsg0(-1, "\n\nIt looks like the test worked this time, please add:\n\n" " Hardware End of Medium = No\n" " Fast Forward Space File = No\n" " BSF at EOM = yes\n\n" "to your Device resource in the Storage conf file.\n"); - goto all_done; - } - } + goto all_done; + } + } } failed: @@ -1438,9 +1443,9 @@ all_done: re_read_block_test(); } - fsf_test(); /* do fast forward space file test */ + fsf_test(); /* do fast forward space file test */ - autochanger_test(); /* do autochanger test */ + autochanger_test(); /* do autochanger test */ } @@ -1471,7 +1476,7 @@ static void fsrcmd() if (num <= 0) { num = 1; } - if (!fsr_dev(dev, num)) { + if (!dev->fsr(num)) { Pmsg1(0, "Bad status from fsr. ERR=%s\n", strerror_dev(dev)); return; } @@ -1570,46 +1575,46 @@ static void scancmd() Pmsg1(0, _("Starting scan at file %u\n"), dev->file); for (;;) { if ((stat = read(dev->fd, buf, sizeof(buf))) < 0) { - berrno be; - clrerror_dev(dev, -1); + berrno be; + clrerror_dev(dev, -1); Mmsg2(dev->errmsg, "read error on %s. ERR=%s.\n", - dev->dev_name, be.strerror()); + dev->dev_name, be.strerror()); Pmsg2(0, "Bad status from read %d. ERR=%s\n", stat, strerror_dev(dev)); - if (blocks > 0) + if (blocks > 0) printf("%d block%s of %d bytes in file %d\n", blocks, blocks>1?"s":"", block_size, dev->file); - return; + return; } Dmsg1(200, "read status = %d\n", stat); /* sleep(1); */ if (stat != block_size) { - update_pos_dev(dev); - if (blocks > 0) { + update_pos_dev(dev); + if (blocks > 0) { printf("%d block%s of %d bytes in file %d\n", blocks, blocks>1?"s":"", block_size, dev->file); - blocks = 0; - } - block_size = stat; + blocks = 0; + } + block_size = stat; } - if (stat == 0) { /* EOF */ - update_pos_dev(dev); + if (stat == 0) { /* EOF */ + update_pos_dev(dev); printf("End of File mark.\n"); - /* Two reads of zero means end of tape */ - if (dev->state & ST_EOF) - dev->state |= ST_EOT; - else { - dev->state |= ST_EOF; - dev->file++; - } - if (dev->state & ST_EOT) { + /* Two reads of zero means end of tape */ + if (dev->state & ST_EOF) + dev->state |= ST_EOT; + else { + dev->state |= ST_EOF; + dev->file++; + } + if (dev->state & ST_EOT) { printf("End of tape\n"); - break; - } - } else { /* Got data */ - dev->state &= ~ST_EOF; - blocks++; - tot_blocks++; - bytes += stat; + break; + } + } else { /* Got data */ + dev->state &= ~ST_EOF; + blocks++; + tot_blocks++; + bytes += stat; } } update_pos_dev(dev); @@ -1642,58 +1647,58 @@ static void scan_blocks() for (;;) { if (!read_block_from_device(dcr, NO_BLOCK_NUMBER_CHECK)) { Dmsg1(100, "!read_block(): ERR=%s\n", strerror_dev(dev)); - if (dev->state & ST_EOT) { - if (blocks > 0) { + if (dev->state & ST_EOT) { + if (blocks > 0) { printf("%d block%s of %d bytes in file %d\n", blocks, blocks>1?"s":"", block_size, dev->file); - blocks = 0; - } - goto bail_out; - } - if (dev->state & ST_EOF) { - if (blocks > 0) { + blocks = 0; + } + goto bail_out; + } + if (dev->state & ST_EOF) { + if (blocks > 0) { printf("%d block%s of %d bytes in file %d\n", blocks, blocks>1?"s":"", block_size, dev->file); - blocks = 0; - } + blocks = 0; + } printf(_("End of File mark.\n")); - continue; - } - if (dev->state & ST_SHORT) { - if (blocks > 0) { + continue; + } + if (dev->state & ST_SHORT) { + if (blocks > 0) { printf("%d block%s of %d bytes in file %d\n", blocks, blocks>1?"s":"", block_size, dev->file); - blocks = 0; - } + blocks = 0; + } printf(_("Short block read.\n")); - continue; - } + continue; + } printf(_("Error reading block. ERR=%s\n"), strerror_dev(dev)); - goto bail_out; + goto bail_out; } if (block->block_len != block_size) { - if (blocks > 0) { + if (blocks > 0) { printf("%d block%s of %d bytes in file %d\n", blocks, blocks>1?"s":"", block_size, dev->file); - blocks = 0; - } - block_size = block->block_len; + blocks = 0; + } + block_size = block->block_len; } blocks++; tot_blocks++; bytes += block->block_len; Dmsg6(100, "Blk_blk=%u dev_blk=%u blen=%u bVer=%d SessId=%u SessTim=%u\n", - block->BlockNumber, dev->block_num, block->block_len, block->BlockVer, - block->VolSessionId, block->VolSessionTime); + block->BlockNumber, dev->block_num, block->block_len, block->BlockVer, + block->VolSessionId, block->VolSessionTime); if (verbose == 1) { - DEV_RECORD *rec = new_record(); - read_record_from_block(block, rec); + DEV_RECORD *rec = new_record(); + read_record_from_block(block, rec); Pmsg8(-1, "Blk_block: %u dev_blk=%u blen=%u First rec FI=%s SessId=%u SessTim=%u Strm=%s rlen=%d\n", - block->BlockNumber, dev->block_num, block->block_len, - FI_to_ascii(rec->FileIndex), rec->VolSessionId, rec->VolSessionTime, - stream_to_ascii(rec->Stream, rec->FileIndex), rec->data_len); - rec->remainder = 0; - free_record(rec); + block->BlockNumber, dev->block_num, block->block_len, + FI_to_ascii(rec->FileIndex), rec->VolSessionId, rec->VolSessionTime, + stream_to_ascii(rec->Stream, rec->FileIndex), rec->data_len); + rec->remainder = 0; + free_record(rec); } else if (verbose > 1) { dump_block(block, ""); } @@ -1768,7 +1773,7 @@ static void fillcmd() return; } - Dmsg1(20, "Begin append device=%s\n", dev_name(dev)); + Dmsg1(20, "Begin append device=%s\n", dev->print_name()); Dmsg1(20, "MaxVolSize=%s\n", edit_uint64(dev->max_volume_size, ec1)); /* Use fixed block size to simplify read back */ @@ -1776,18 +1781,22 @@ static void fillcmd() dev->min_block_size = dev->max_block_size; set_volume_name("TestVolume1", 1); - rewind_dev(dev); - weof_dev(dev, 1); + if (!dev->rewind()) { + Pmsg0(000, "Rewind failed.\n"); + } + if (!dev->weof()) { + Pmsg0(000, "Write EOF failed.\n"); + } labelcmd(); - dev->state &= ~ST_APPEND; /* force volume to be relabeled */ + dev->set_append(); /* force volume to be relabeled */ /* * Acquire output device for writing. Note, after acquiring a - * device, we MUST release it, which is done at the end of this - * subroutine. + * device, we MUST release it, which is done at the end of this + * subroutine. */ Dmsg0(100, "just before acquire_device\n"); - if (!acquire_device_for_append(jcr, dev)) { + if (!acquire_device_for_append(dcr)) { set_jcr_job_status(jcr, JS_ErrorTerminated); return; } @@ -1800,7 +1809,7 @@ static void fillcmd() if (!write_session_label(dcr, SOS_LABEL)) { set_jcr_job_status(jcr, JS_ErrorTerminated); Jmsg1(jcr, M_FATAL, 0, _("Write session label failed. ERR=%s\n"), - strerror_dev(dev)); + strerror_dev(dev)); ok = false; } Pmsg0(-1, "Wrote Start of Session label.\n"); @@ -1822,7 +1831,7 @@ static void fillcmd() uint32_t *p = (uint32_t *)rec.data; srandom(time(NULL)); for (i=0; idcr->VolFirstIndex = 0; - time(&jcr->run_time); /* start counting time for rates */ + time(&jcr->run_time); /* start counting time for rates */ if (simple) { Pmsg0(-1, "Begin writing Bacula records to tape ...\n"); } else { @@ -1846,82 +1855,82 @@ static void fillcmd() uint32_t *lp = (uint32_t *)rec.data; lp[0] += lp[13]; for (i=1; i < (rec.data_len-sizeof(uint32_t))/sizeof(uint32_t)-1; i++) { - lp[i] += lp[i-1]; + lp[i] += lp[i-1]; } Dmsg4(250, "before write_rec FI=%d SessId=%d Strm=%s len=%d\n", - rec.FileIndex, rec.VolSessionId, stream_to_ascii(rec.Stream, rec.FileIndex), - rec.data_len); + rec.FileIndex, rec.VolSessionId, stream_to_ascii(rec.Stream, rec.FileIndex), + rec.data_len); while (!write_record_to_block(block, &rec)) { - /* - * When we get here we have just filled a block - */ + /* + * When we get here we have just filled a block + */ Dmsg2(150, "!write_record_to_block data_len=%d rem=%d\n", rec.data_len, - rec.remainder); - - /* Write block to tape */ - if (!flush_block(block, 1)) { - break; - } - - /* Every 5000 blocks (approx 322MB) report where we are. - */ - if ((block->BlockNumber % 5000) == 0) { - now = time(NULL); - now -= jcr->run_time; - if (now <= 0) { - now = 1; /* prevent divide error */ - } - kbs = (double)dev->VolCatInfo.VolCatBytes / (1000.0 * (double)now); + rec.remainder); + + /* Write block to tape */ + if (!flush_block(block, 1)) { + break; + } + + /* Every 5000 blocks (approx 322MB) report where we are. + */ + if ((block->BlockNumber % 5000) == 0) { + now = time(NULL); + now -= jcr->run_time; + if (now <= 0) { + now = 1; /* prevent divide error */ + } + kbs = (double)dev->VolCatInfo.VolCatBytes / (1000.0 * (double)now); Pmsg4(-1, "Wrote blk_block=%u, dev_blk_num=%u VolBytes=%s rate=%.1f KB/s\n", - block->BlockNumber, dev->block_num, - edit_uint64_with_commas(dev->VolCatInfo.VolCatBytes, ec1), (float)kbs); - } - /* Every 15000 blocks (approx 1GB) write an EOF. - */ - if ((block->BlockNumber % 15000) == 0) { + block->BlockNumber, dev->block_num, + edit_uint64_with_commas(dev->VolCatInfo.VolCatBytes, ec1), (float)kbs); + } + /* Every 15000 blocks (approx 1GB) write an EOF. + */ + if ((block->BlockNumber % 15000) == 0) { Pmsg0(-1, "Flush block, write EOF\n"); - flush_block(block, 0); - weof_dev(dev, 1); - } + flush_block(block, 0); + weof_dev(dev, 1); + } - /* Get out after writing 10 blocks to the second tape */ - if (++BlockNumber > 10 && stop != 0) { /* get out */ - break; - } + /* Get out after writing 10 blocks to the second tape */ + if (++BlockNumber > 10 && stop != 0) { /* get out */ + break; + } } if (!ok) { Pmsg0(000, _("Not OK\n")); - break; + break; } jcr->JobBytes += rec.data_len; /* increment bytes this job */ Dmsg4(190, "write_record FI=%s SessId=%d Strm=%s len=%d\n", - FI_to_ascii(rec.FileIndex), rec.VolSessionId, - stream_to_ascii(rec.Stream, rec.FileIndex), rec.data_len); + FI_to_ascii(rec.FileIndex), rec.VolSessionId, + stream_to_ascii(rec.Stream, rec.FileIndex), rec.data_len); /* Get out after writing 10 blocks to the second tape */ - if (BlockNumber > 10 && stop != 0) { /* get out */ + if (BlockNumber > 10 && stop != 0) { /* get out */ Pmsg0(-1, "Done writing ...\n"); - break; + break; } } if (vol_num > 1) { Dmsg0(100, "Write_end_session_label()\n"); /* Create Job status for end of session label */ if (!job_canceled(jcr) && ok) { - set_jcr_job_status(jcr, JS_Terminated); + set_jcr_job_status(jcr, JS_Terminated); } else if (!ok) { - set_jcr_job_status(jcr, JS_ErrorTerminated); + set_jcr_job_status(jcr, JS_ErrorTerminated); } if (!write_session_label(dcr, EOS_LABEL)) { Pmsg1(000, _("Error writting end session label. ERR=%s\n"), strerror_dev(dev)); - ok = false; + ok = false; } /* Write out final block of this session */ if (!write_block_to_device(dcr)) { Pmsg0(-1, _("Set ok=false after write_block_to_device.\n")); - ok = false; + ok = false; } Pmsg0(-1, _("Wrote End of Session label.\n")); @@ -1929,7 +1938,7 @@ static void fillcmd() last_block_num2 = last_block_num; last_file2 = last_file; if (last_block2) { - free_block(last_block2); + free_block(last_block2); } last_block2 = dup_block(last_block); } @@ -1948,23 +1957,17 @@ static void fillcmd() write(fd, first_block->buf, first_block->buf_len); close(fd); Pmsg2(-1, "Wrote state file last_block_num1=%d last_block_num2=%d\n", - last_block_num1, last_block_num2); + last_block_num1, last_block_num2); } else { berrno be; Pmsg2(-1, _("Could not create state file: %s ERR=%s\n"), buf, - be.strerror()); + be.strerror()); } Pmsg4(-1, _("\n\nDone filling tape%s at %d:%d. Now beginning re-read of %stape ...\n"), simple?"":"s", jcr->dcr->dev->file, jcr->dcr->dev->block_num, simple?"":"first "); jcr->dcr->block = block; - /* Release the device if multiple tapes being used */ -// if (!simple && !release_device(jcr)) { -// Pmsg0(-1, _("Error in release_device\n")); -// ok = false; -// } - do_unfill(); dev->min_block_size = min_block_size; @@ -2001,7 +2004,7 @@ static void unfillcmd() if (state_level != btape_state_level) { Pmsg0(-1, "\nThe state file level has changed. You must redo\n" "the fill command.\n"); - return; + return; } } else { berrno be; @@ -2028,7 +2031,7 @@ static void do_unfill() end_of_tape = 0; - time(&jcr->run_time); /* start counting time for rates */ + time(&jcr->run_time); /* start counting time for rates */ stop = 0; file_index = 0; if (last_block) { @@ -2042,11 +2045,11 @@ static void do_unfill() /* Multiple Volume tape */ /* Close device so user can use autochanger if desired */ if (dev_cap(dev, CAP_OFFLINEUNMOUNT)) { - offline_dev(dev); + offline_dev(dev); } autochanger = autoload_device(dcr, 1, NULL); if (!autochanger) { - force_close_dev(dev); + force_close_dev(dev); get_cmd(_("Mount first tape. Press enter when ready: ")); } free_vol_list(jcr); @@ -2057,25 +2060,25 @@ static void do_unfill() close_dev(dev); dev->state &= ~(ST_READ|ST_APPEND); dev->num_writers = 0; - if (!acquire_device_for_read(jcr, dev)) { + if (!acquire_device_for_read(dcr)) { Pmsg1(-1, "%s", dev->errmsg); - goto bail_out; + goto bail_out; } } /* * We now have the first tape mounted. * Note, re-reading last block may have caused us to - * loose track of where we are (block number unknown). + * loose track of where we are (block number unknown). */ - if (!rewind_dev(dev)) { /* get to a known place on tape */ + if (!rewind_dev(dev)) { /* get to a known place on tape */ goto bail_out; } - /* Read the first 1000 records */ - Pmsg0(-1, _("Reading the first 1000 records.\n")); + /* Read the first 10000 records */ + Pmsg0(-1, _("Reading the first 10000 records.\n")); quickie_count = 0; read_records(dcr, quickie_cb, my_mount_next_read_volume); Pmsg4(-1, _("Reposition from %u:%u to %u:%u\n"), dev->file, dev->block_num, - last_file, last_block_num); + last_file, last_block_num); if (!reposition_dev(dev, last_file, last_block_num)) { Pmsg1(-1, "Reposition error. ERR=%s\n", strerror_dev(dev)); goto bail_out; @@ -2117,8 +2120,8 @@ static void do_unfill() get_cmd(_("Mount second tape. Press enter when ready: ")); } - dev->state &= ~ST_READ; - if (!acquire_device_for_read(jcr, dev)) { + dev->clear_read(); + if (!acquire_device_for_read(dcr)) { Pmsg1(-1, "%s", dev->errmsg); goto bail_out; } @@ -2142,7 +2145,7 @@ static void do_unfill() /* Now find and compare the last block */ Pmsg4(-1, _("Reposition from %u:%u to %u:%u\n"), dev->file, dev->block_num, - last_file, last_block_num); + last_file, last_block_num); if (!reposition_dev(dev, last_file, last_block_num)) { Pmsg1(-1, "Reposition error. ERR=%s\n", strerror_dev(dev)); goto bail_out; @@ -2162,20 +2165,20 @@ bail_out: free_block(first_block); } -/* Read 1000 records then stop */ +/* Read 10000 records then stop */ static bool quickie_cb(DCR *dcr, DEV_RECORD *rec) { DEVICE *dev = dcr->dev; if (dev->file != 0) { Pmsg3(-1, "ERROR! device at %d:%d count=%d\n", dev->file, dev->block_num, - quickie_count); + quickie_count); return false; } quickie_count++; - if (quickie_count == 1000) { + if (quickie_count == 10000) { Pmsg2(-1, "1000 records read now at %d:%d\n", dev->file, dev->block_num); } - return quickie_count < 1000; + return quickie_count < 10000; } static bool compare_blocks(DEV_BLOCK *last_block, DEV_BLOCK *block) @@ -2191,9 +2194,9 @@ static bool compare_blocks(DEV_BLOCK *last_block, DEV_BLOCK *block) unser_uint32(block_len); while (q < (block->buf+block_len)) { if (*p == *q) { - p++; - q++; - continue; + p++; + q++; + continue; } Pmsg0(-1, "\n"); dump_block(last_block, _("Last block written")); @@ -2240,30 +2243,30 @@ static int flush_block(DEV_BLOCK *block, int dump) this_block_num = dev->block_num; if (!write_block_to_dev(dcr)) { Pmsg3(000, "Last block at: %u:%u this_dev_block_num=%d\n", - last_file, last_block_num, this_block_num); + last_file, last_block_num, this_block_num); if (vol_num == 1) { - /* - * This is 1st tape, so save first tape info separate - * from second tape info - */ - last_block_num1 = last_block_num; - last_file1 = last_file; - last_block1 = dup_block(last_block); - last_block2 = dup_block(last_block); - first_block = dup_block(block); /* first block second tape */ + /* + * This is 1st tape, so save first tape info separate + * from second tape info + */ + last_block_num1 = last_block_num; + last_file1 = last_file; + last_block1 = dup_block(last_block); + last_block2 = dup_block(last_block); + first_block = dup_block(block); /* first block second tape */ } if (verbose) { Pmsg3(000, "Block not written: FileIndex=%u blk_block=%u Size=%u\n", - (unsigned)file_index, block->BlockNumber, block->block_len); + (unsigned)file_index, block->BlockNumber, block->block_len); dump_block(last_block, "Last block written"); Pmsg0(-1, "\n"); dump_block(block, "Block not written"); } if (stop == 0) { - eot_block = block->BlockNumber; - eot_block_len = block->block_len; - eot_FileIndex = file_index; - stop = 1; + eot_block = block->BlockNumber; + eot_block_len = block->block_len; + eot_FileIndex = file_index; + stop = 1; } now = time(NULL); now -= jcr->run_time; @@ -2273,23 +2276,23 @@ static int flush_block(DEV_BLOCK *block, int dump) kbs = (double)dev->VolCatInfo.VolCatBytes / (1000 * now); vol_size = dev->VolCatInfo.VolCatBytes; Pmsg4(000, "End of tape %d:%d. VolumeCapacity=%s. Write rate = %.1f KB/s\n", - dev->file, dev->block_num, - edit_uint64_with_commas(dev->VolCatInfo.VolCatBytes, ec1), kbs); + dev->file, dev->block_num, + edit_uint64_with_commas(dev->VolCatInfo.VolCatBytes, ec1), kbs); if (simple) { - stop = -1; /* stop, but do simplified test */ + stop = -1; /* stop, but do simplified test */ } else { - /* Full test in progress */ - if (!fixup_device_block_write_error(jcr->dcr)) { + /* Full test in progress */ + if (!fixup_device_block_write_error(jcr->dcr)) { Pmsg1(000, _("Cannot fixup device error. %s\n"), strerror_dev(dev)); - ok = false; - unlock_device(dev); - return 0; - } - BlockNumber = 0; /* start counting for second tape */ + ok = false; + unlock_device(dev); + return 0; + } + BlockNumber = 0; /* start counting for second tape */ } unlock_device(dev); - return 1; /* end of tape reached */ + return 1; /* end of tape reached */ } /* Save contents after write so that the header is serialized */ @@ -2343,15 +2346,15 @@ static void qfillcmd() for (i=0; i < count; i++) { if (i % 100 == 0) { printf("+"); - fflush(stdout); + fflush(stdout); } if (!write_record_to_block(block, rec)) { Pmsg0(0, _("Error writing record to block.\n")); - goto bail_out; + goto bail_out; } if (!write_block_to_dev(dcr)) { Pmsg0(0, _("Error writing block to device.\n")); - goto bail_out; + goto bail_out; } } printf("\n"); @@ -2387,7 +2390,7 @@ static void rawfill_cmd() uint32_t *p = (uint32_t *)block->buf; srandom(time(NULL)); for (i=0; ibuf_len/sizeof(uint32_t); i++) { - p[i] = random(); + p[i] = random(); } } p = (uint32_t *)block->buf; @@ -2396,15 +2399,15 @@ static void rawfill_cmd() *p = block_num; stat = write(dev->fd, block->buf, block->buf_len); if (stat == (int)block->buf_len) { - if ((block_num++ % 100) == 0) { + if ((block_num++ % 100) == 0) { printf("+"); - fflush(stdout); - } - p[0] += p[13]; - for (i=1; i<(block->buf_len-sizeof(uint32_t))/sizeof(uint32_t)-1; i++) { - p[i] += p[i-1]; - } - continue; + fflush(stdout); + } + p[0] += p[13]; + for (i=1; i<(block->buf_len-sizeof(uint32_t))/sizeof(uint32_t)-1; i++) { + p[i] += p[i-1]; + } + continue; } break; } @@ -2418,7 +2421,7 @@ static void rawfill_cmd() /* - * Fill a tape using raw write() command + * Fill a tape using Bacula block writes */ static void bfill_cmd() { @@ -2437,7 +2440,7 @@ static void bfill_cmd() uint32_t *p = (uint32_t *)block->buf; srandom(time(NULL)); for (i=0; ibuf_len/sizeof(uint32_t); i++) { - p[i] = random(); + p[i] = random(); } } p = (uint32_t *)block->buf; @@ -2447,15 +2450,15 @@ static void bfill_cmd() block->binbuf = block->buf_len; block->bufp = block->buf + block->binbuf; if (!write_block_to_dev(dcr)) { - break; + break; } if ((block_num++ % 100) == 0) { printf("+"); - fflush(stdout); + fflush(stdout); } p[0] += p[13]; for (i=1; i<(block->buf_len/sizeof(uint32_t)-1); i++) { - p[i] += p[i-1]; + p[i] += p[i-1]; } } my_errno = errno; @@ -2495,7 +2498,7 @@ static struct cmdstruct commands[] = { {"wr", wrcmd, "write a single Bacula block"}, {"rr", rrcmd, "read a single record"}, {"qfill", qfillcmd, "quick fill command"}, - }; + }; #define comsize (sizeof(commands)/sizeof(struct cmdstruct)) static void @@ -2509,15 +2512,15 @@ do_tape_cmds() found = false; parse_args(cmd, &args, &argc, argk, argv, MAX_CMD_ARGS); for (i=0; i 0 && fstrsch(argk[0], commands[i].key)) { - (*commands[i].func)(); /* go execute command */ - found = true; - break; - } + if (argc > 0 && fstrsch(argk[0], commands[i].key)) { + (*commands[i].func)(); /* go execute command */ + found = true; + break; + } if (!found) Pmsg1(0, _("%s is an illegal command\n"), cmd); if (quit) - break; + break; } } @@ -2567,12 +2570,12 @@ get_cmd(const char *prompt) cmd[i] = 0; while ((ch = fgetc(stdin)) != EOF) { if (ch == '\n') { - strip_trailing_junk(cmd); - return 1; + strip_trailing_junk(cmd); + return 1; } else if (ch == 4 || ch == 0xd3 || ch == 0x8) { - if (i > 0) - cmd[--i] = 0; - continue; + if (i > 0) + cmd[--i] = 0; + continue; } cmd[i++] = ch; @@ -2583,8 +2586,8 @@ get_cmd(const char *prompt) } /* Dummies to replace askdir.c */ -bool dir_update_file_attributes(DCR *dcr, DEV_RECORD *rec) { return 1;} -bool dir_send_job_status(JCR *jcr) {return 1;} +bool dir_update_file_attributes(DCR *dcr, DEV_RECORD *rec) { return 1;} +bool dir_send_job_status(JCR *jcr) {return 1;} bool dir_update_volume_info(DCR *dcr, bool relabel) { @@ -2627,10 +2630,10 @@ bool dir_ask_sysop_to_mount_volume(DCR *dcr) Pmsg1(-1, "%s", dev->errmsg); /* print reason */ if (dcr->VolumeName[0] == 0 || strcmp(dcr->VolumeName, "TestVolume2") == 0) { fprintf(stderr, "Mount second Volume on device %s and press return when ready: ", - dev_name(dev)); + dev->print_name()); } else { fprintf(stderr, "Mount Volume \"%s\" on device %s and press return when ready: ", - dcr->VolumeName, dev_name(dev)); + dcr->VolumeName, dev->print_name()); } getchar(); return true; @@ -2654,7 +2657,7 @@ bool dir_ask_sysop_to_create_appendable_volume(DCR *dcr) if (!autochanger) { force_close_dev(dev); fprintf(stderr, "Mount blank Volume on device %s and press return when ready: ", - dev_name(dev)); + dev->print_name()); getchar(); } open_device(dcr); @@ -2684,7 +2687,7 @@ static bool my_mount_next_read_volume(DCR *dcr) } kbs = (double)VolBytes / (1000.0 * (double)now); Pmsg3(-1, "Read block=%u, VolBytes=%s rate=%.1f KB/s\n", block->BlockNumber, - edit_uint64_with_commas(VolBytes, ec1), (float)kbs); + edit_uint64_with_commas(VolBytes, ec1), (float)kbs); if (strcmp(dcr->VolumeName, "TestVolume2") == 0) { end_of_tape = 1; @@ -2696,12 +2699,12 @@ static bool my_mount_next_read_volume(DCR *dcr) jcr->bsr = NULL; create_vol_list(jcr); close_dev(dev); - dev->state &= ~ST_READ; - if (!acquire_device_for_read(jcr, dev)) { - Pmsg2(0, "Cannot open Dev=%s, Vol=%s\n", dev_name(dev), dcr->VolumeName); + dev->clear_read(); + if (!acquire_device_for_read(dcr)) { + Pmsg2(0, "Cannot open Dev=%s, Vol=%s\n", dev->print_name(), dcr->VolumeName); return false; } - return true; /* next volume mounted */ + return true; /* next volume mounted */ } static void set_volume_name(const char *VolName, int volnum)