]> git.sur5r.net Git - bacula/bacula/blob - bacula/src/stored/btape.c
- Add wait.c (oops forgot previously).
[bacula/bacula] / bacula / src / stored / btape.c
1 /*
2  *
3  *   Bacula Tape manipulation program
4  *
5  *    Has various tape manipulation commands -- mostly for
6  *    use in determining how tapes really work.
7  *
8  *     Kern Sibbald, April MM
9  *
10  *   Note, this program reads stored.conf, and will only
11  *     talk to devices that are configured.
12  *
13  *   Version $Id$
14  *
15  */
16 /*
17    Copyright (C) 2000-2005 Kern Sibbald
18
19    This program is free software; you can redistribute it and/or
20    modify it under the terms of the GNU General Public License as
21    published by the Free Software Foundation; either version 2 of
22    the License, or (at your option) any later version.
23
24    This program is distributed in the hope that it will be useful,
25    but WITHOUT ANY WARRANTY; without even the implied warranty of
26    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
27    General Public License for more details.
28
29    You should have received a copy of the GNU General Public
30    License along with this program; if not, write to the Free
31    Software Foundation, Inc., 59 Temple Place - Suite 330, Boston,
32    MA 02111-1307, USA.
33
34  */
35
36 #include "bacula.h"
37 #include "stored.h"
38
39
40 /* External subroutines */
41 extern void free_config_resources();
42
43 /* Exported variables */
44 int quit = 0;
45 char buf[100000];
46 int bsize = TAPE_BSIZE;
47 char VolName[MAX_NAME_LENGTH];
48 STORES *me = NULL;                    /* our Global resource */
49 bool forge_on = false;                /* proceed inspite of I/O errors */
50 pthread_mutex_t device_release_mutex = PTHREAD_MUTEX_INITIALIZER;
51 pthread_cond_t wait_device_release = PTHREAD_COND_INITIALIZER;
52
53 /*
54  * If you change the format of the state file,
55  *  increment this value
56  */
57 static uint32_t btape_state_level = 2;
58
59 DEVICE *dev = NULL;
60 DCR *dcr;
61 DEVRES *device = NULL;
62
63
64 /* Forward referenced subroutines */
65 static void do_tape_cmds();
66 static void helpcmd();
67 static void scancmd();
68 static void rewindcmd();
69 static void clearcmd();
70 static void wrcmd();
71 static void rrcmd();
72 static void eodcmd();
73 static void fillcmd();
74 static void qfillcmd();
75 static void statcmd();
76 static void unfillcmd();
77 static int flush_block(DEV_BLOCK *block, int dump);
78 static bool quickie_cb(DCR *dcr, DEV_RECORD *rec);
79 static bool compare_blocks(DEV_BLOCK *last_block, DEV_BLOCK *block);
80 static bool my_mount_next_read_volume(DCR *dcr);
81 static void scan_blocks();
82 static void set_volume_name(const char *VolName, int volnum);
83 static void rawfill_cmd();
84 static void bfill_cmd();
85 static bool open_the_device();
86 static void autochangercmd();
87 static void do_unfill();
88
89
90 /* Static variables */
91 #define CONFIG_FILE "bacula-sd.conf"
92 char *configfile;
93
94 #define MAX_CMD_ARGS 30
95 static POOLMEM *cmd;
96 static POOLMEM *args;
97 static char *argk[MAX_CMD_ARGS];
98 static char *argv[MAX_CMD_ARGS];
99 static int argc;
100
101 static int quickie_count = 0;
102 static BSR *bsr = NULL;
103 static int signals = TRUE;
104 static bool ok;
105 static int stop = 0;
106 static uint64_t vol_size;
107 static uint64_t VolBytes;
108 static time_t now;
109 static double kbs;
110 static int32_t file_index;
111 static int end_of_tape = 0;
112 static uint32_t LastBlock = 0;
113 static uint32_t eot_block;
114 static uint32_t eot_block_len;
115 static uint32_t eot_FileIndex;
116 static int dumped = 0;
117 static DEV_BLOCK *last_block1 = NULL;
118 static DEV_BLOCK *last_block2 = NULL;
119 static DEV_BLOCK *last_block = NULL;
120 static DEV_BLOCK *this_block = NULL;
121 static DEV_BLOCK *first_block = NULL;
122 static uint32_t last_file1 = 0;
123 static uint32_t last_file2 = 0;
124 static uint32_t last_file = 0;
125 static uint32_t last_block_num1 = 0;
126 static uint32_t last_block_num2 = 0;
127 static uint32_t last_block_num = 0;
128 static uint32_t BlockNumber = 0;
129 static bool simple = true;
130
131 static const char *VolumeName = NULL;
132 static int vol_num = 0;
133
134 static JCR *jcr = NULL;
135
136
137 static void usage();
138 static void terminate_btape(int sig);
139 int get_cmd(const char *prompt);
140
141
142 /*********************************************************************
143  *
144  *         Main Bacula Pool Creation Program
145  *
146  */
147 int main(int margc, char *margv[])
148 {
149    int ch, i;
150    uint32_t x32, y32;
151    uint64_t x64, y64;
152    char buf[1000];
153
154    /* Sanity checks */
155    if (TAPE_BSIZE % B_DEV_BSIZE != 0 || TAPE_BSIZE / B_DEV_BSIZE == 0) {
156       Emsg2(M_ABORT, 0, "Tape block size (%d) not multiple of system size (%d)\n",
157          TAPE_BSIZE, B_DEV_BSIZE);
158    }
159    if (TAPE_BSIZE != (1 << (ffs(TAPE_BSIZE)-1))) {
160       Emsg1(M_ABORT, 0, "Tape block size (%d) is not a power of 2\n", TAPE_BSIZE);
161    }
162    if (sizeof(off_t) < 8) {
163       Pmsg1(-1, "\n\n!!!! Warning large disk addressing disabled. off_t=%d should be 8 or more !!!!!\n\n\n",
164          sizeof(off_t));
165    }
166    x32 = 123456789;
167    bsnprintf(buf, sizeof(buf), "%u", x32);
168    i = bsscanf(buf, "%u", &y32);
169    if (i != 1 || x32 != y32) {
170       Pmsg3(-1, "32 bit printf/scanf problem. i=%d x32=%u y32=%u\n", i, x32, y32);
171       exit(1);
172    }
173    x64 = 123456789;
174    x64 = x64 << 32;
175    x64 += 123456789;
176    bsnprintf(buf, sizeof(buf), "%" llu, x64);
177    i = bsscanf(buf, "%llu", &y64);
178    if (i != 1 || x64 != y64) {
179       Pmsg3(-1, "64 bit printf/scanf problem. i=%d x64=%" llu " y64=%" llu "\n", i, x64, y64);
180       exit(1);
181    }
182
183    printf("Tape block granularity is %d bytes.\n", TAPE_BSIZE);
184
185    working_directory = "/tmp";
186    my_name_is(margc, margv, "btape");
187    init_msg(NULL, NULL);
188
189    while ((ch = getopt(margc, margv, "b:c:d:psv?")) != -1) {
190       switch (ch) {
191       case 'b':                    /* bootstrap file */
192          bsr = parse_bsr(NULL, optarg);
193 //       dump_bsr(bsr, true);
194          break;
195
196       case 'c':                    /* specify config file */
197          if (configfile != NULL) {
198             free(configfile);
199          }
200          configfile = bstrdup(optarg);
201          break;
202
203       case 'd':                    /* set debug level */
204          debug_level = atoi(optarg);
205          if (debug_level <= 0) {
206             debug_level = 1;
207          }
208          break;
209
210       case 'p':
211          forge_on = true;
212          break;
213
214       case 's':
215          signals = false;
216          break;
217
218       case 'v':
219          verbose++;
220          break;
221
222       case '?':
223       default:
224          helpcmd();
225          exit(0);
226
227       }
228    }
229    margc -= optind;
230    margv += optind;
231
232    cmd = get_pool_memory(PM_FNAME);
233    args = get_pool_memory(PM_FNAME);
234
235    if (signals) {
236       init_signals(terminate_btape);
237    }
238
239    if (configfile == NULL) {
240       configfile = bstrdup(CONFIG_FILE);
241    }
242
243    daemon_start_time = time(NULL);
244
245    parse_config(configfile);
246
247
248    /* See if we can open a device */
249    if (margc == 0) {
250       Pmsg0(000, "No archive name specified.\n");
251       usage();
252       exit(1);
253    } else if (margc != 1) {
254       Pmsg0(000, "Improper number of arguments specified.\n");
255       usage();
256       exit(1);
257    }
258
259    jcr = setup_jcr("btape", margv[0], bsr, NULL, 0); /* write */
260    if (!jcr) {
261       exit(1);
262    }
263    dev = jcr->dcr->dev;
264    if (!dev) {
265       exit(1);
266    }
267    dcr = jcr->dcr;
268    if (!open_the_device()) {
269       goto terminate;
270    }
271
272    Dmsg0(200, "Do tape commands\n");
273    do_tape_cmds();
274
275 terminate:
276    terminate_btape(0);
277    return 0;
278 }
279
280 static void terminate_btape(int stat)
281 {
282
283    sm_check(__FILE__, __LINE__, false);
284    if (configfile) {
285       free(configfile);
286    }
287    free_config_resources();
288    if (args) {
289       free_pool_memory(args);
290       args = NULL;
291    }
292    if (cmd) {
293       free_pool_memory(cmd);
294       cmd = NULL;
295    }
296
297    if (bsr) {
298       free_bsr(bsr);
299    }
300
301    free_jcr(jcr);
302    jcr = NULL;
303
304    if (dev) {
305       term_dev(dev);
306    }
307
308    if (debug_level > 10)
309       print_memory_pool_stats();
310
311    if (this_block) {
312       free_block(this_block);
313    }
314
315    stop_watchdog();
316    term_msg();
317    close_memory_pool();               /* free memory in pool */
318
319    sm_dump(false);
320    exit(stat);
321 }
322
323 static bool open_the_device()
324 {
325    DEV_BLOCK *block;
326
327    block = new_block(dev);
328    lock_device(dev);
329    if (!dev->is_open()) {
330       Dmsg1(200, "Opening device %s\n", dcr->VolumeName);
331       if (open_dev(dev, dcr->VolumeName, OPEN_READ_WRITE) < 0) {
332          Emsg1(M_FATAL, 0, _("dev open failed: %s\n"), dev->errmsg);
333          unlock_device(dev);
334          free_block(block);
335          return false;
336       }
337    }
338    Pmsg1(000, "open_dev %s OK\n", dev->print_name());
339    dev->set_append();                 /* put volume in append mode */
340    unlock_device(dev);
341    free_block(block);
342    return true;
343 }
344
345
346 void quitcmd()
347 {
348    quit = 1;
349 }
350
351 /*
352  * Write a label to the tape
353  */
354 static void labelcmd()
355 {
356    if (VolumeName) {
357       pm_strcpy(cmd, VolumeName);
358    } else {
359       if (!get_cmd("Enter Volume Name: ")) {
360          return;
361       }
362    }
363
364    if (!dev->is_open()) {
365       if (!first_open_device(dev)) {
366          Pmsg1(0, "Device open failed. ERR=%s\n", strerror_dev(dev));
367       }
368    }
369    rewind_dev(dev);
370    write_new_volume_label_to_dev(dcr, cmd, "Default");
371    Pmsg1(-1, "Wrote Volume label for volume \"%s\".\n", cmd);
372 }
373
374 /*
375  * Read the tape label
376  */
377 static void readlabelcmd()
378 {
379    int save_debug_level = debug_level;
380    int stat;
381
382    stat = read_dev_volume_label(dcr);
383    switch (stat) {
384    case VOL_NO_LABEL:
385       Pmsg0(0, "Volume has no label.\n");
386       break;
387    case VOL_OK:
388       Pmsg0(0, "Volume label read correctly.\n");
389       break;
390    case VOL_IO_ERROR:
391       Pmsg1(0, "I/O error on device: ERR=%s", strerror_dev(dev));
392       break;
393    case VOL_NAME_ERROR:
394       Pmsg0(0, "Volume name error\n");
395       break;
396    case VOL_CREATE_ERROR:
397       Pmsg1(0, "Error creating label. ERR=%s", strerror_dev(dev));
398       break;
399    case VOL_VERSION_ERROR:
400       Pmsg0(0, "Volume version error.\n");
401       break;
402    case VOL_LABEL_ERROR:
403       Pmsg0(0, "Bad Volume label type.\n");
404       break;
405    default:
406       Pmsg0(0, "Unknown error.\n");
407       break;
408    }
409
410    debug_level = 20;
411    dump_volume_label(dev);
412    debug_level = save_debug_level;
413 }
414
415
416 /*
417  * Load the tape should have prevously been taken
418  * off line, otherwise this command is not necessary.
419  */
420 static void loadcmd()
421 {
422
423    if (!load_dev(dev)) {
424       Pmsg1(0, "Bad status from load. ERR=%s\n", strerror_dev(dev));
425    } else
426       Pmsg1(0, "Loaded %s\n", dev->print_name());
427 }
428
429 /*
430  * Rewind the tape.
431  */
432 static void rewindcmd()
433 {
434    if (!rewind_dev(dev)) {
435       Pmsg1(0, "Bad status from rewind. ERR=%s\n", strerror_dev(dev));
436       clrerror_dev(dev, -1);
437    } else {
438       Pmsg1(0, "Rewound %s\n", dev->print_name());
439    }
440 }
441
442 /*
443  * Clear any tape error
444  */
445 static void clearcmd()
446 {
447    clrerror_dev(dev, -1);
448 }
449
450 /*
451  * Write and end of file on the tape
452  */
453 static void weofcmd()
454 {
455    int stat;
456    int num = 1;
457    if (argc > 1) {
458       num = atoi(argk[1]);
459    }
460    if (num <= 0) {
461       num = 1;
462    }
463
464    if ((stat = weof_dev(dev, num)) < 0) {
465       Pmsg2(0, "Bad status from weof %d. ERR=%s\n", stat, strerror_dev(dev));
466       return;
467    } else {
468       Pmsg3(0, "Wrote %d EOF%s to %s\n", num, num==1?"":"s", dev->print_name());
469    }
470 }
471
472
473 /* Go to the end of the medium -- raw command
474  * The idea was orginally that the end of the Bacula
475  * medium would be flagged differently. This is not
476  * currently the case. So, this is identical to the
477  * eodcmd().
478  */
479 static void eomcmd()
480 {
481    if (!eod_dev(dev)) {
482       Pmsg1(0, "%s", strerror_dev(dev));
483       return;
484    } else {
485       Pmsg0(0, _("Moved to end of medium.\n"));
486    }
487 }
488
489 /*
490  * Go to the end of the medium (either hardware determined
491  *  or defined by two eofs.
492  */
493 static void eodcmd()
494 {
495    eomcmd();
496 }
497
498 /*
499  * Backspace file
500  */
501 static void bsfcmd()
502 {
503    int num = 1;
504    if (argc > 1) {
505       num = atoi(argk[1]);
506    }
507    if (num <= 0) {
508       num = 1;
509    }
510
511    if (!bsf_dev(dev, num)) {
512       Pmsg1(0, _("Bad status from bsf. ERR=%s\n"), strerror_dev(dev));
513    } else {
514       Pmsg2(0, _("Backspaced %d file%s.\n"), num, num==1?"":"s");
515    }
516 }
517
518 /*
519  * Backspace record
520  */
521 static void bsrcmd()
522 {
523    int num = 1;
524    if (argc > 1) {
525       num = atoi(argk[1]);
526    }
527    if (num <= 0) {
528       num = 1;
529    }
530    if (!bsr_dev(dev, num)) {
531       Pmsg1(0, _("Bad status from bsr. ERR=%s\n"), strerror_dev(dev));
532    } else {
533       Pmsg2(0, _("Backspaced %d record%s.\n"), num, num==1?"":"s");
534    }
535 }
536
537 /*
538  * List device capabilities as defined in the
539  *  stored.conf file.
540  */
541 static void capcmd()
542 {
543    printf(_("Configured device capabilities:\n"));
544    printf("%sEOF ", dev->capabilities & CAP_EOF ? "" : "!");
545    printf("%sBSR ", dev->capabilities & CAP_BSR ? "" : "!");
546    printf("%sBSF ", dev->capabilities & CAP_BSF ? "" : "!");
547    printf("%sFSR ", dev->capabilities & CAP_FSR ? "" : "!");
548    printf("%sFSF ", dev->capabilities & CAP_FSF ? "" : "!");
549    printf("%sFASTFSF ", dev->capabilities & CAP_FASTFSF ? "" : "!");
550    printf("%sBSFATEOM ", dev->capabilities & CAP_BSFATEOM ? "" : "!");
551    printf("%sEOM ", dev->capabilities & CAP_EOM ? "" : "!");
552    printf("%sREM ", dev->capabilities & CAP_REM ? "" : "!");
553    printf("%sRACCESS ", dev->capabilities & CAP_RACCESS ? "" : "!");
554    printf("%sAUTOMOUNT ", dev->capabilities & CAP_AUTOMOUNT ? "" : "!");
555    printf("%sLABEL ", dev->capabilities & CAP_LABEL ? "" : "!");
556    printf("%sANONVOLS ", dev->capabilities & CAP_ANONVOLS ? "" : "!");
557    printf("%sALWAYSOPEN ", dev->capabilities & CAP_ALWAYSOPEN ? "" : "!");
558    printf("%sMTIOCGET ", dev->capabilities & CAP_MTIOCGET ? "" : "!");
559    printf("\n");
560
561    printf(_("Device status:\n"));
562    printf("%sOPENED ", dev->is_open() ? "" : "!");
563    printf("%sTAPE ", dev->is_tape() ? "" : "!");
564    printf("%sLABEL ", dev->is_labeled() ? "" : "!");
565    printf("%sMALLOC ", dev->state & ST_MALLOC ? "" : "!");
566    printf("%sAPPEND ", dev->can_append() ? "" : "!");
567    printf("%sREAD ", dev->can_read() ? "" : "!");
568    printf("%sEOT ", dev->at_eot() ? "" : "!");
569    printf("%sWEOT ", dev->state & ST_WEOT ? "" : "!");
570    printf("%sEOF ", dev->at_eof() ? "" : "!");
571    printf("%sNEXTVOL ", dev->state & ST_NEXTVOL ? "" : "!");
572    printf("%sSHORT ", dev->state & ST_SHORT ? "" : "!");
573    printf("\n");
574
575    printf(_("Device parameters:\n"));
576    printf("Device name: %s\n", dev->dev_name);
577    printf("File=%u block=%u\n", dev->file, dev->block_num);
578    printf("Min block=%u Max block=%u\n", dev->min_block_size, dev->max_block_size);
579
580    printf("Status:\n");
581    statcmd();
582
583 }
584
585 /*
586  * Test writting larger and larger records.
587  * This is a torture test for records.
588  */
589 static void rectestcmd()
590 {
591    DEV_BLOCK *block;
592    DEV_RECORD *rec;
593    int i, blkno = 0;
594
595    Pmsg0(0, "Test writting larger and larger records.\n"
596 "This is a torture test for records.\nI am going to write\n"
597 "larger and larger records. It will stop when the record size\n"
598 "plus the header exceeds the block size (by default about 64K)\n");
599
600
601    get_cmd("Do you want to continue? (y/n): ");
602    if (cmd[0] != 'y') {
603       Pmsg0(000, "Command aborted.\n");
604       return;
605    }
606
607    sm_check(__FILE__, __LINE__, false);
608    block = new_block(dev);
609    rec = new_record();
610
611    for (i=1; i<500000; i++) {
612       rec->data = check_pool_memory_size(rec->data, i);
613       memset(rec->data, i & 0xFF, i);
614       rec->data_len = i;
615       sm_check(__FILE__, __LINE__, false);
616       if (write_record_to_block(block, rec)) {
617          empty_block(block);
618          blkno++;
619          Pmsg2(0, "Block %d i=%d\n", blkno, i);
620       } else {
621          break;
622       }
623       sm_check(__FILE__, __LINE__, false);
624    }
625    free_record(rec);
626    free_block(block);
627    sm_check(__FILE__, __LINE__, false);
628 }
629
630 /*
631  * This test attempts to re-read a block written by Bacula
632  *   normally at the end of the tape. Bacula will then back up
633  *   over the two eof marks, backup over the record and reread
634  *   it to make sure it is valid.  Bacula can skip this validation
635  *   if you set "Backward space record = no"
636  */
637 static int re_read_block_test()
638 {
639    DEV_BLOCK *block = dcr->block;
640    DEV_RECORD *rec;
641    int stat = 0;
642    int len;
643
644    if (!(dev->capabilities & CAP_BSR)) {
645       Pmsg0(-1, _("Skipping read backwards test because BSR turned off.\n"));
646       return 0;
647    }
648
649    Pmsg0(-1, _("\n=== Write, backup, and re-read test ===\n\n"
650       "I'm going to write three records and an EOF\n"
651       "then backup over the EOF and re-read the last record.\n"
652       "Bacula does this after writing the last block on the\n"
653       "tape to verify that the block was written correctly.\n\n"
654       "This is not an *essential* feature ...\n\n"));
655    rewindcmd();
656    empty_block(block);
657    rec = new_record();
658    rec->data = check_pool_memory_size(rec->data, block->buf_len);
659    len = rec->data_len = block->buf_len-100;
660    memset(rec->data, 1, rec->data_len);
661    if (!write_record_to_block(block, rec)) {
662       Pmsg0(0, _("Error writing record to block.\n"));
663       goto bail_out;
664    }
665    if (!write_block_to_dev(dcr)) {
666       Pmsg0(0, _("Error writing block to device.\n"));
667       goto bail_out;
668    } else {
669       Pmsg1(0, _("Wrote first record of %d bytes.\n"), rec->data_len);
670    }
671    memset(rec->data, 2, rec->data_len);
672    if (!write_record_to_block(block, rec)) {
673       Pmsg0(0, _("Error writing record to block.\n"));
674       goto bail_out;
675    }
676    if (!write_block_to_dev(dcr)) {
677       Pmsg0(0, _("Error writing block to device.\n"));
678       goto bail_out;
679    } else {
680       Pmsg1(0, _("Wrote second record of %d bytes.\n"), rec->data_len);
681    }
682    memset(rec->data, 3, rec->data_len);
683    if (!write_record_to_block(block, rec)) {
684       Pmsg0(0, _("Error writing record to block.\n"));
685       goto bail_out;
686    }
687    if (!write_block_to_dev(dcr)) {
688       Pmsg0(0, _("Error writing block to device.\n"));
689       goto bail_out;
690    } else {
691       Pmsg1(0, _("Wrote third record of %d bytes.\n"), rec->data_len);
692    }
693    weofcmd();
694    if (dev_cap(dev, CAP_TWOEOF)) {
695       weofcmd();
696    }
697    if (!bsf_dev(dev, 1)) {
698       Pmsg1(0, _("Backspace file failed! ERR=%s\n"), strerror_dev(dev));
699       goto bail_out;
700    }
701    if (dev_cap(dev, CAP_TWOEOF)) {
702       if (!bsf_dev(dev, 1)) {
703          Pmsg1(0, _("Backspace file failed! ERR=%s\n"), strerror_dev(dev));
704          goto bail_out;
705       }
706    }
707    Pmsg0(0, "Backspaced over EOF OK.\n");
708    if (!bsr_dev(dev, 1)) {
709       Pmsg1(0, _("Backspace record failed! ERR=%s\n"), strerror_dev(dev));
710       goto bail_out;
711    }
712    Pmsg0(0, "Backspace record OK.\n");
713    if (!read_block_from_dev(dcr, NO_BLOCK_NUMBER_CHECK)) {
714       berrno be;
715       Pmsg1(0, _("Read block failed! ERR=%s\n"), be.strerror(dev->dev_errno));
716       goto bail_out;
717    }
718    memset(rec->data, 0, rec->data_len);
719    if (!read_record_from_block(block, rec)) {
720       berrno be;
721       Pmsg1(0, _("Read block failed! ERR=%s\n"), be.strerror(dev->dev_errno));
722       goto bail_out;
723    }
724    for (int i=0; i<len; i++) {
725       if (rec->data[i] != 3) {
726          Pmsg0(0, _("Bad data in record. Test failed!\n"));
727          goto bail_out;
728       }
729    }
730    Pmsg0(0, _("\nBlock re-read correct. Test succeeded!\n"));
731    Pmsg0(-1, _("=== End Write, backup, and re-read test ===\n\n"));
732
733    stat = 1;
734
735 bail_out:
736    free_record(rec);
737    if (stat == 0) {
738       Pmsg0(0, _("This is not terribly serious since Bacula only uses\n"
739                  "this function to verify the last block written to the\n"
740                  "tape. Bacula will skip the last block verification\n"
741                  "if you add:\n\n"
742                   "Backward Space Record = No\n\n"
743                   "to your Storage daemon's Device resource definition.\n"));
744    }
745    return stat;
746 }
747
748
749 /*
750  * This test writes Bacula blocks to the tape in
751  *   several files. It then rewinds the tape and attepts
752  *   to read these blocks back checking the data.
753  */
754 static int write_read_test()
755 {
756    DEV_BLOCK *block;
757    DEV_RECORD *rec;
758    int stat = 0;
759    int len, i, j;
760    int *p;
761
762    Pmsg0(-1, _("\n=== Write, rewind, and re-read test ===\n\n"
763       "I'm going to write 1000 records and an EOF\n"
764       "then write 1000 records and an EOF, then rewind,\n"
765       "and re-read the data to verify that it is correct.\n\n"
766       "This is an *essential* feature ...\n\n"));
767    block = dcr->block;
768    rec = new_record();
769    if (!rewind_dev(dev)) {
770       Pmsg1(0, "Bad status from rewind. ERR=%s\n", strerror_dev(dev));
771       goto bail_out;
772    }
773    rec->data = check_pool_memory_size(rec->data, block->buf_len);
774    rec->data_len = block->buf_len-100;
775    len = rec->data_len/sizeof(i);
776    for (i=1; i<=1000; i++) {
777       p = (int *)rec->data;
778       for (j=0; j<len; j++) {
779          *p++ = i;
780       }
781       if (!write_record_to_block(block, rec)) {
782          Pmsg0(0, _("Error writing record to block.\n"));
783          goto bail_out;
784       }
785       if (!write_block_to_dev(dcr)) {
786          Pmsg0(0, _("Error writing block to device.\n"));
787          goto bail_out;
788       }
789    }
790    Pmsg1(0, _("Wrote 1000 blocks of %d bytes.\n"), rec->data_len);
791    weofcmd();
792    for (i=1001; i<=2000; i++) {
793       p = (int *)rec->data;
794       for (j=0; j<len; j++) {
795          *p++ = i;
796       }
797       if (!write_record_to_block(block, rec)) {
798          Pmsg0(0, _("Error writing record to block.\n"));
799          goto bail_out;
800       }
801       if (!write_block_to_dev(dcr)) {
802          Pmsg0(0, _("Error writing block to device.\n"));
803          goto bail_out;
804       }
805    }
806    Pmsg1(0, _("Wrote 1000 blocks of %d bytes.\n"), rec->data_len);
807    weofcmd();
808    if (dev_cap(dev, CAP_TWOEOF)) {
809       weofcmd();
810    }
811    if (!rewind_dev(dev)) {
812       Pmsg1(0, "Bad status from rewind. ERR=%s\n", strerror_dev(dev));
813       goto bail_out;
814    } else {
815       Pmsg0(0, "Rewind OK.\n");
816    }
817    for (i=1; i<=2000; i++) {
818 read_again:
819       if (!read_block_from_dev(dcr, NO_BLOCK_NUMBER_CHECK)) {
820          berrno be;
821          if (dev_state(dev, ST_EOF)) {
822             Pmsg0(-1, _("Got EOF on tape.\n"));
823             if (i == 1001) {
824                goto read_again;
825             }
826          }
827          Pmsg2(0, _("Read block %d failed! ERR=%s\n"), i, be.strerror(dev->dev_errno));
828          goto bail_out;
829       }
830       memset(rec->data, 0, rec->data_len);
831       if (!read_record_from_block(block, rec)) {
832          berrno be;
833          Pmsg2(0, _("Read record failed. Block %d! ERR=%s\n"), i, be.strerror(dev->dev_errno));
834          goto bail_out;
835       }
836       p = (int *)rec->data;
837       for (j=0; j<len; j++) {
838          if (*p != i) {
839             Pmsg3(0, _("Bad data in record. Expected %d, got %d at byte %d. Test failed!\n"),
840                i, *p, j);
841             goto bail_out;
842          }
843          p++;
844       }
845       if (i == 1000 || i == 2000) {
846          Pmsg0(-1, _("1000 blocks re-read correctly.\n"));
847       }
848    }
849    Pmsg0(-1, _("=== Test Succeeded. End Write, rewind, and re-read test ===\n\n"));
850    stat = 1;
851
852 bail_out:
853    free_record(rec);
854    return stat;
855 }
856
857 /*
858  * This test writes Bacula blocks to the tape in
859  *   several files. It then rewinds the tape and attepts
860  *   to read these blocks back checking the data.
861  */
862 static int position_test()
863 {
864    DEV_BLOCK *block = dcr->block;
865    DEV_RECORD *rec;
866    int stat = 0;
867    int len, i, j;
868    bool ok = true;
869    int recno = 0;
870    int file = 0, blk = 0;
871    int *p;
872    bool got_eof = false;
873
874    Pmsg0(-1, _("\n=== Write, rewind, and position test ===\n\n"
875       "I'm going to write 1000 records and an EOF\n"
876       "then write 1000 records and an EOF, then rewind,\n"
877       "and position to a few blocks and verify that it is correct.\n\n"
878       "This is an *essential* feature ...\n\n"));
879    empty_block(block);
880    rec = new_record();
881    if (!rewind_dev(dev)) {
882       Pmsg1(0, "Bad status from rewind. ERR=%s\n", strerror_dev(dev));
883       goto bail_out;
884    }
885    rec->data = check_pool_memory_size(rec->data, block->buf_len);
886    rec->data_len = block->buf_len-100;
887    len = rec->data_len/sizeof(i);
888    for (i=1; i<=1000; i++) {
889       p = (int *)rec->data;
890       for (j=0; j<len; j++) {
891          *p++ = i;
892       }
893       if (!write_record_to_block(block, rec)) {
894          Pmsg0(0, _("Error writing record to block.\n"));
895          goto bail_out;
896       }
897       if (!write_block_to_dev(dcr)) {
898          Pmsg0(0, _("Error writing block to device.\n"));
899          goto bail_out;
900       }
901    }
902    Pmsg1(0, _("Wrote 1000 blocks of %d bytes.\n"), rec->data_len);
903    weofcmd();
904    for (i=1001; i<=2000; i++) {
905       p = (int *)rec->data;
906       for (j=0; j<len; j++) {
907          *p++ = i;
908       }
909       if (!write_record_to_block(block, rec)) {
910          Pmsg0(0, _("Error writing record to block.\n"));
911          goto bail_out;
912       }
913       if (!write_block_to_dev(dcr)) {
914          Pmsg0(0, _("Error writing block to device.\n"));
915          goto bail_out;
916       }
917    }
918    Pmsg1(0, _("Wrote 1000 blocks of %d bytes.\n"), rec->data_len);
919    weofcmd();
920    if (dev_cap(dev, CAP_TWOEOF)) {
921       weofcmd();
922    }
923    if (!rewind_dev(dev)) {
924       Pmsg1(0, "Bad status from rewind. ERR=%s\n", strerror_dev(dev));
925       goto bail_out;
926    } else {
927       Pmsg0(0, "Rewind OK.\n");
928    }
929
930    while(ok) {
931       /* Set up next item to read based on where we are */
932       switch (recno) {
933       case 0:
934          recno = 5;
935          file = 0;
936          blk = 4;
937          break;
938       case 5:
939          recno = 201;
940          file = 0;
941          blk = 200;
942          break;
943       case 201:
944          recno = 1000;
945          file = 0;
946          blk = 999;
947          break;
948       case 1000:
949          recno = 1001;
950          file = 1;
951          blk = 0;
952          break;
953       case 1001:
954          recno = 1601;
955          file = 1;
956          blk = 600;
957          break;
958       case 1601:
959          recno = 2000;
960          file = 1;
961          blk = 999;
962          break;
963       case 2000:
964          ok = false;
965          continue;
966       }
967       Pmsg2(-1, "Reposition to file:block %d:%d\n", file, blk);
968       if (!reposition_dev(dev, file, blk)) {
969          Pmsg0(0, "Reposition error.\n");
970          goto bail_out;
971       }
972 read_again:
973       if (!read_block_from_dev(dcr, NO_BLOCK_NUMBER_CHECK)) {
974          berrno be;
975          if (dev_state(dev, ST_EOF)) {
976             Pmsg0(-1, _("Got EOF on tape.\n"));
977             if (!got_eof) {
978                got_eof = true;
979                goto read_again;
980             }
981          }
982          Pmsg4(0, _("Read block %d failed! file=%d blk=%d. ERR=%s\n\n"),
983             recno, file, blk, be.strerror(dev->dev_errno));
984          Pmsg0(0, _("This may be because the tape drive block size is not\n"
985                     " set to variable blocking as normally used by Bacula.\n"
986                     " Please see the Tape Testing chapter in the manual and \n"
987                     " look for using mt with defblksize and setoptions\n"
988                     "If your tape drive block size is correct, then perhaps\n"
989                     " your SCSI driver is *really* stupid and does not\n"
990                     " correctly report the file:block after a FSF. In this\n"
991                     " case try setting:\n"
992                     "    Fast Forward Space File = no\n"
993                     " in your Device resource.\n"));
994
995          goto bail_out;
996       }
997       memset(rec->data, 0, rec->data_len);
998       if (!read_record_from_block(block, rec)) {
999          berrno be;
1000          Pmsg1(0, _("Read record failed! ERR=%s\n"), be.strerror(dev->dev_errno));
1001          goto bail_out;
1002       }
1003       p = (int *)rec->data;
1004       for (j=0; j<len; j++) {
1005          if (p[j] != recno) {
1006             Pmsg3(0, _("Bad data in record. Expected %d, got %d at byte %d. Test failed!\n"),
1007                recno, p[j], j);
1008             goto bail_out;
1009          }
1010       }
1011       Pmsg1(-1, _("Block %d re-read correctly.\n"), recno);
1012    }
1013    Pmsg0(-1, _("=== Test Succeeded. End Write, rewind, and re-read test ===\n\n"));
1014    stat = 1;
1015
1016 bail_out:
1017    free_record(rec);
1018    return stat;
1019 }
1020
1021
1022
1023
1024 /*
1025  * This test writes some records, then writes an end of file,
1026  *   rewinds the tape, moves to the end of the data and attepts
1027  *   to append to the tape.  This function is essential for
1028  *   Bacula to be able to write multiple jobs to the tape.
1029  */
1030 static int append_test()
1031 {
1032    Pmsg0(-1, _("\n\n=== Append files test ===\n\n"
1033                "This test is essential to Bacula.\n\n"
1034 "I'm going to write one record  in file 0,\n"
1035 "                   two records in file 1,\n"
1036 "             and three records in file 2\n\n"));
1037    argc = 1;
1038    rewindcmd();
1039    wrcmd();
1040    weofcmd();      /* end file 0 */
1041    wrcmd();
1042    wrcmd();
1043    weofcmd();      /* end file 1 */
1044    wrcmd();
1045    wrcmd();
1046    wrcmd();
1047    weofcmd();     /* end file 2 */
1048    if (dev_cap(dev, CAP_TWOEOF)) {
1049       weofcmd();
1050    }
1051    force_close_dev(dev);              /* release device */
1052    if (!open_the_device()) {
1053       return -1;
1054    }
1055    rewindcmd();
1056    Pmsg0(0, _("Now moving to end of medium.\n"));
1057    eodcmd();
1058    Pmsg2(-1, _("We should be in file 3. I am at file %d. This is %s\n"),
1059       dev->file, dev->file == 3 ? "correct!" : "NOT correct!!!!");
1060
1061    if (dev->file != 3) {
1062       return -1;
1063    }
1064
1065    Pmsg0(-1, _("\nNow the important part, I am going to attempt to append to the tape.\n\n"));
1066    wrcmd();
1067    weofcmd();
1068    if (dev_cap(dev, CAP_TWOEOF)) {
1069       weofcmd();
1070    }
1071    rewindcmd();
1072    Pmsg0(-1, _("Done appending, there should be no I/O errors\n\n"));
1073    Pmsg0(-1, "Doing Bacula scan of blocks:\n");
1074    scan_blocks();
1075    Pmsg0(-1, _("End scanning the tape.\n"));
1076    Pmsg2(-1, _("We should be in file 4. I am at file %d. This is %s\n"),
1077       dev->file, dev->file == 4 ? "correct!" : "NOT correct!!!!");
1078
1079    if (dev->file != 4) {
1080       return -2;
1081    }
1082    return 1;
1083 }
1084
1085
1086 /*
1087  * This test exercises the autochanger
1088  */
1089 static int autochanger_test()
1090 {
1091    POOLMEM *results, *changer;
1092    int slot, status, loaded;
1093    int timeout = dcr->device->max_changer_wait;
1094    int sleep_time = 0;
1095
1096    Dmsg1(100, "Max changer wait = %d sec\n", timeout);
1097    if (!dev_cap(dev, CAP_AUTOCHANGER)) {
1098       return 1;
1099    }
1100    if (!(dcr->device && dcr->device->changer_name && dcr->device->changer_command)) {
1101       Pmsg0(-1, "\nAutochanger enabled, but no name or no command device specified.\n");
1102       return 1;
1103    }
1104
1105    Pmsg0(-1, "\nAh, I see you have an autochanger configured.\n"
1106              "To test the autochanger you must have a blank tape\n"
1107              " that I can write on in Slot 1.\n");
1108    if (!get_cmd("\nDo you wish to continue with the Autochanger test? (y/n): ")) {
1109       return 0;
1110    }
1111    if (cmd[0] != 'y' && cmd[0] != 'Y') {
1112       return 0;
1113    }
1114
1115    Pmsg0(-1, _("\n\n=== Autochanger test ===\n\n"));
1116
1117    results = get_pool_memory(PM_MESSAGE);
1118    changer = get_pool_memory(PM_FNAME);
1119
1120 try_again:
1121    slot = 1;
1122    dcr->VolCatInfo.Slot = slot;
1123    /* Find out what is loaded, zero means device is unloaded */
1124    Pmsg0(-1, _("3301 Issuing autochanger \"loaded\" command.\n"));
1125    changer = edit_device_codes(dcr, changer, "loaded");
1126    status = run_program(changer, timeout, results);
1127    Dmsg3(100, "run_prog: %s stat=%d result=\"%s\"\n", changer, status, results);
1128    if (status == 0) {
1129       loaded = atoi(results);
1130    } else {
1131       berrno be;
1132       Pmsg1(-1, _("3991 Bad autochanger command: %s\n"), changer);
1133       Pmsg2(-1, _("3991 result=\"%s\": ERR=%s\n"), results, be.strerror(status));
1134       goto bail_out;
1135    }
1136    if (loaded) {
1137       Pmsg1(-1, "Slot %d loaded. I am going to unload it.\n", loaded);
1138    } else {
1139       Pmsg0(-1, "Nothing loaded in the drive. OK.\n");
1140    }
1141    Dmsg1(100, "Results from loaded query=%s\n", results);
1142    if (loaded) {
1143       dcr->VolCatInfo.Slot = loaded;
1144       offline_or_rewind_dev(dev);
1145       /* We are going to load a new tape, so close the device */
1146       force_close_dev(dev);
1147       Pmsg2(-1, _("3302 Issuing autochanger \"unload %d %d\" command.\n"),
1148          loaded, dev->drive_index);
1149       changer = edit_device_codes(dcr, changer, "unload");
1150       status = run_program(changer, timeout, results);
1151       Pmsg2(-1, "unload status=%s %d\n", status==0?"OK":"Bad", status);
1152       if (status != 0) {
1153          berrno be;
1154          Pmsg1(-1, _("3992 Bad autochanger command: %s\n"), changer);
1155          Pmsg2(-1, _("3992 result=\"%s\": ERR=%s\n"), results, be.strerror(status));
1156       }
1157    }
1158
1159    /*
1160     * Load the Slot 1
1161     */
1162
1163    slot = 1;
1164    dcr->VolCatInfo.Slot = slot;
1165    Pmsg2(-1, _("3303 Issuing autochanger \"load slot %d %d\" command.\n"),
1166       slot, dev->drive_index);
1167    changer = edit_device_codes(dcr, changer, "load");
1168    Dmsg1(100, "Changer=%s\n", changer);
1169    force_close_dev(dev);
1170    status = run_program(changer, timeout, results);
1171    if (status == 0) {
1172       Pmsg2(-1,  _("3303 Autochanger \"load slot %d %d\" status is OK.\n"),
1173          slot, dev->drive_index);
1174    } else {
1175       berrno be;
1176       Pmsg1(-1, _("3993 Bad autochanger command: %s\n"), changer);
1177       Pmsg2(-1, _("3993 result=\"%s\": ERR=%s\n"), results, be.strerror(status));
1178       goto bail_out;
1179    }
1180
1181    if (!open_the_device()) {
1182       goto bail_out;
1183    }
1184    /*
1185     * Start with sleep_time 0 then increment by 30 seconds if we get
1186     * a failure.
1187     */
1188    bmicrosleep(sleep_time, 0);
1189    if (!rewind_dev(dev) || weof_dev(dev,1) < 0) {
1190       Pmsg1(0, "Bad status from rewind. ERR=%s\n", strerror_dev(dev));
1191       clrerror_dev(dev, -1);
1192       Pmsg0(-1, "\nThe test failed, probably because you need to put\n"
1193                 "a longer sleep time in the mtx-script in the load) case.\n"
1194                 "Adding a 30 second sleep and trying again ...\n");
1195       sleep_time += 30;
1196       goto try_again;
1197    } else {
1198       Pmsg1(0, "Rewound %s\n", dev->print_name());
1199    }
1200
1201    if ((status = weof_dev(dev, 1)) < 0) {
1202       Pmsg2(0, "Bad status from weof %d. ERR=%s\n", status, strerror_dev(dev));
1203       goto bail_out;
1204    } else {
1205       Pmsg1(0, "Wrote EOF to %s\n", dev->print_name());
1206    }
1207
1208    if (sleep_time) {
1209       Pmsg1(-1, "\nThe test worked this time. Please add:\n\n"
1210                 "   sleep %d\n\n"
1211                 "to your mtx-changer script in the load) case.\n\n",
1212                 sleep_time);
1213    } else {
1214       Pmsg0(-1, "\nThe test autochanger worked!!\n\n");
1215    }
1216
1217    free_pool_memory(changer);
1218    free_pool_memory(results);
1219    return 1;
1220
1221
1222 bail_out:
1223    free_pool_memory(changer);
1224    free_pool_memory(results);
1225    Pmsg0(-1, "You must correct this error or the Autochanger will not work.\n");
1226    return -2;
1227 }
1228
1229 static void autochangercmd()
1230 {
1231    autochanger_test();
1232 }
1233
1234
1235 /*
1236  * This test assumes that the append test has been done,
1237  *   then it tests the fsf function.
1238  */
1239 static int fsf_test()
1240 {
1241    bool set_off = false;
1242
1243    Pmsg0(-1, _("\n\n=== Forward space files test ===\n\n"
1244                "This test is essential to Bacula.\n\n"
1245                "I'm going to write five files then test forward spacing\n\n"));
1246    argc = 1;
1247    rewindcmd();
1248    wrcmd();
1249    weofcmd();      /* end file 0 */
1250    wrcmd();
1251    wrcmd();
1252    weofcmd();      /* end file 1 */
1253    wrcmd();
1254    wrcmd();
1255    wrcmd();
1256    weofcmd();     /* end file 2 */
1257    wrcmd();
1258    wrcmd();
1259    weofcmd();     /* end file 3 */
1260    wrcmd();
1261    weofcmd();     /* end file 4 */
1262    if (dev_cap(dev, CAP_TWOEOF)) {
1263       weofcmd();
1264    }
1265
1266 test_again:
1267    rewindcmd();
1268    Pmsg0(0, _("Now forward spacing 1 file.\n"));
1269    if (!fsf_dev(dev, 1)) {
1270       Pmsg1(0, "Bad status from fsr. ERR=%s\n", strerror_dev(dev));
1271       goto bail_out;
1272    }
1273    Pmsg2(-1, _("We should be in file 1. I am at file %d. This is %s\n"),
1274       dev->file, dev->file == 1 ? "correct!" : "NOT correct!!!!");
1275
1276    if (dev->file != 1) {
1277       goto bail_out;
1278    }
1279
1280    Pmsg0(0, _("Now forward spacing 2 files.\n"));
1281    if (!fsf_dev(dev, 2)) {
1282       Pmsg1(0, "Bad status from fsr. ERR=%s\n", strerror_dev(dev));
1283       goto bail_out;
1284    }
1285    Pmsg2(-1, _("We should be in file 3. I am at file %d. This is %s\n"),
1286       dev->file, dev->file == 3 ? "correct!" : "NOT correct!!!!");
1287
1288    if (dev->file != 3) {
1289       goto bail_out;
1290    }
1291
1292    rewindcmd();
1293    Pmsg0(0, _("Now forward spacing 4 files.\n"));
1294    if (!fsf_dev(dev, 4)) {
1295       Pmsg1(0, "Bad status from fsr. ERR=%s\n", strerror_dev(dev));
1296       goto bail_out;
1297    }
1298    Pmsg2(-1, _("We should be in file 4. I am at file %d. This is %s\n"),
1299       dev->file, dev->file == 4 ? "correct!" : "NOT correct!!!!");
1300
1301    if (dev->file != 4) {
1302       goto bail_out;
1303    }
1304    if (set_off) {
1305       Pmsg0(-1, "The test worked this time. Please add:\n\n"
1306                 "   Fast Forward Space File = no\n\n"
1307                 "to your Device resource for this drive.\n");
1308    }
1309
1310    Pmsg0(-1, "\n");
1311    Pmsg0(0, _("Now forward spacing 1 more file.\n"));
1312    if (!fsf_dev(dev, 1)) {
1313       Pmsg1(0, "Bad status from fsr. ERR=%s\n", strerror_dev(dev));
1314    }
1315    Pmsg2(-1, _("We should be in file 5. I am at file %d. This is %s\n"),
1316       dev->file, dev->file == 5 ? "correct!" : "NOT correct!!!!");
1317    if (dev->file != 5) {
1318       goto bail_out;
1319    }
1320    Pmsg0(-1, _("\n=== End Forward space files test ===\n\n"));
1321    return 1;
1322
1323 bail_out:
1324    Pmsg0(-1, _("\nThe forward space file test failed.\n"));
1325    if (dev_cap(dev, CAP_FASTFSF)) {
1326       Pmsg0(-1, "You have Fast Forward Space File enabled.\n"
1327               "I am turning it off then retrying the test.\n");
1328       dev->capabilities &= ~CAP_FASTFSF;
1329       set_off = true;
1330       goto test_again;
1331    }
1332    Pmsg0(-1, "You must correct this error or Bacula will not work.\n"
1333             "Some systems, e.g. OpenBSD, require you to set\n"
1334             "   Use MTIOCGET= no\n"
1335             "in your device resource. Use with caution.\n");
1336    return -2;
1337 }
1338
1339
1340
1341
1342
1343 /*
1344  * This is a general test of Bacula's functions
1345  *   needed to read and write the tape.
1346  */
1347 static void testcmd()
1348 {
1349    int stat;
1350
1351    if (!write_read_test()) {
1352       return;
1353    }
1354    if (!position_test()) {
1355       return;
1356    }
1357
1358    stat = append_test();
1359    if (stat == 1) {                   /* OK get out */
1360       goto all_done;
1361    }
1362    if (stat == -1) {                  /* first test failed */
1363       if (dev_cap(dev, CAP_EOM) || dev_cap(dev, CAP_FASTFSF)) {
1364          Pmsg0(-1, "\nAppend test failed. Attempting again.\n"
1365                    "Setting \"Hardware End of Medium = no\n"
1366                    "    and \"Fast Forward Space File = no\n"
1367                    "and retrying append test.\n\n");
1368          dev->capabilities &= ~CAP_EOM; /* turn off eom */
1369          dev->capabilities &= ~CAP_FASTFSF; /* turn off fast fsf */
1370          stat = append_test();
1371          if (stat == 1) {
1372             Pmsg0(-1, "\n\nIt looks like the test worked this time, please add:\n\n"
1373                      "    Hardware End of Medium = No\n\n"
1374                      "    Fast Forward Space File = No\n"
1375                      "to your Device resource in the Storage conf file.\n");
1376             goto all_done;
1377          }
1378          if (stat == -1) {
1379             Pmsg0(-1, "\n\nThat appears *NOT* to have corrected the problem.\n");
1380             goto failed;
1381          }
1382          /* Wrong count after append */
1383          if (stat == -2) {
1384             Pmsg0(-1, "\n\nIt looks like the append failed. Attempting again.\n"
1385                      "Setting \"BSF at EOM = yes\" and retrying append test.\n");
1386             dev->capabilities |= CAP_BSFATEOM; /* backspace on eom */
1387             stat = append_test();
1388             if (stat == 1) {
1389                Pmsg0(-1, "\n\nIt looks like the test worked this time, please add:\n\n"
1390                      "    Hardware End of Medium = No\n"
1391                      "    Fast Forward Space File = No\n"
1392                      "    BSF at EOM = yes\n\n"
1393                      "to your Device resource in the Storage conf file.\n");
1394                goto all_done;
1395             }
1396          }
1397
1398       }
1399 failed:
1400       Pmsg0(-1, "\nAppend test failed.\n\n");
1401       Pmsg0(-1, "\n!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!\n"
1402             "Unable to correct the problem. You MUST fix this\n"
1403              "problem before Bacula can use your tape drive correctly\n");
1404       Pmsg0(-1, "\nPerhaps running Bacula in fixed block mode will work.\n"
1405             "Do so by setting:\n\n"
1406             "Minimum Block Size = nnn\n"
1407             "Maximum Block Size = nnn\n\n"
1408             "in your Storage daemon's Device definition.\n"
1409             "nnn must match your tape driver's block size, which\n"
1410             "can be determined by reading your tape manufacturers\n"
1411             "information, and the information on your kernel dirver.\n"
1412             "Fixed block sizes, however, are not normally an ideal solution.\n"
1413             "\n"
1414             "Some systems, e.g. OpenBSD, require you to set\n"
1415             "   Use MTIOCGET= no\n"
1416             "in your device resource. Use with caution.\n");
1417        return;
1418    }
1419
1420 all_done:
1421    Pmsg0(-1, _("\nThe above Bacula scan should have output identical to what follows.\n"
1422         "Please double check it ...\n"
1423         "=== Sample correct output ===\n"
1424         "1 block of 64448 bytes in file 1\n"
1425         "End of File mark.\n"
1426         "2 blocks of 64448 bytes in file 2\n"
1427         "End of File mark.\n"
1428         "3 blocks of 64448 bytes in file 3\n"
1429         "End of File mark.\n"
1430         "1 block of 64448 bytes in file 4\n"
1431         "End of File mark.\n"
1432         "Total files=4, blocks=7, bytes = 451,136\n"
1433         "=== End sample correct output ===\n\n"));
1434
1435    Pmsg0(-1, _("If the above scan output is not identical to the\n"
1436                "sample output, you MUST correct the problem\n"
1437                "or Bacula will not be able to write multiple Jobs to \n"
1438                "the tape.\n\n"));
1439
1440    if (stat == 1) {
1441       re_read_block_test();
1442    }
1443
1444    fsf_test();                        /* do fast forward space file test */
1445
1446    autochanger_test();                /* do autochanger test */
1447
1448 }
1449
1450 /* Forward space a file */
1451 static void fsfcmd()
1452 {
1453    int num = 1;
1454    if (argc > 1) {
1455       num = atoi(argk[1]);
1456    }
1457    if (num <= 0) {
1458       num = 1;
1459    }
1460    if (!fsf_dev(dev, num)) {
1461       Pmsg1(0, "Bad status from fsf. ERR=%s\n", strerror_dev(dev));
1462       return;
1463    }
1464    Pmsg2(0, "Forward spaced %d file%s.\n", num, num==1?"":"s");
1465 }
1466
1467 /* Forward space a record */
1468 static void fsrcmd()
1469 {
1470    int num = 1;
1471    if (argc > 1) {
1472       num = atoi(argk[1]);
1473    }
1474    if (num <= 0) {
1475       num = 1;
1476    }
1477    if (!fsr_dev(dev, num)) {
1478       Pmsg1(0, "Bad status from fsr. ERR=%s\n", strerror_dev(dev));
1479       return;
1480    }
1481    Pmsg2(0, "Forward spaced %d record%s.\n", num, num==1?"":"s");
1482 }
1483
1484
1485 /*
1486  * Write a Bacula block to the tape
1487  */
1488 static void wrcmd()
1489 {
1490    DEV_BLOCK *block = dcr->block;
1491    DEV_RECORD *rec = dcr->rec;
1492    int i;
1493
1494    sm_check(__FILE__, __LINE__, false);
1495    empty_block(block);
1496    if (verbose > 1) {
1497       dump_block(block, "test");
1498    }
1499
1500    i = block->buf_len - 100;
1501    ASSERT (i > 0);
1502    rec->data = check_pool_memory_size(rec->data, i);
1503    memset(rec->data, i & 0xFF, i);
1504    rec->data_len = i;
1505    sm_check(__FILE__, __LINE__, false);
1506    if (!write_record_to_block(block, rec)) {
1507       Pmsg0(0, _("Error writing record to block.\n"));
1508       goto bail_out;
1509    }
1510    if (!write_block_to_dev(dcr)) {
1511       Pmsg0(0, _("Error writing block to device.\n"));
1512       goto bail_out;
1513    } else {
1514       Pmsg1(0, _("Wrote one record of %d bytes.\n"), i);
1515    }
1516    Pmsg0(0, _("Wrote block to device.\n"));
1517
1518 bail_out:
1519    sm_check(__FILE__, __LINE__, false);
1520    sm_check(__FILE__, __LINE__, false);
1521 }
1522
1523 /*
1524  * Read a record from the tape
1525  */
1526 static void rrcmd()
1527 {
1528    char *buf;
1529    int stat, len;
1530
1531    if (!get_cmd("Enter length to read: ")) {
1532       return;
1533    }
1534    len = atoi(cmd);
1535    if (len < 0 || len > 1000000) {
1536       Pmsg0(0, _("Bad length entered, using default of 1024 bytes.\n"));
1537       len = 1024;
1538    }
1539    buf = (char *)malloc(len);
1540    stat = read(dev->fd, buf, len);
1541    if (stat > 0 && stat <= len) {
1542       errno = 0;
1543    }
1544    berrno be;
1545    Pmsg3(0, _("Read of %d bytes gives stat=%d. ERR=%s\n"),
1546       len, stat, be.strerror());
1547    free(buf);
1548 }
1549
1550
1551 /*
1552  * Scan tape by reading block by block. Report what is
1553  * on the tape.  Note, this command does raw reads, and as such
1554  * will not work with fixed block size devices.
1555  */
1556 static void scancmd()
1557 {
1558    int stat;
1559    int blocks, tot_blocks, tot_files;
1560    int block_size;
1561    uint64_t bytes;
1562    char ec1[50];
1563
1564
1565    blocks = block_size = tot_blocks = 0;
1566    bytes = 0;
1567    if (dev->state & ST_EOT) {
1568       Pmsg0(0, "End of tape\n");
1569       return;
1570    }
1571    update_pos_dev(dev);
1572    tot_files = dev->file;
1573    Pmsg1(0, _("Starting scan at file %u\n"), dev->file);
1574    for (;;) {
1575       if ((stat = read(dev->fd, buf, sizeof(buf))) < 0) {
1576          berrno be;
1577          clrerror_dev(dev, -1);
1578          Mmsg2(dev->errmsg, "read error on %s. ERR=%s.\n",
1579             dev->dev_name, be.strerror());
1580          Pmsg2(0, "Bad status from read %d. ERR=%s\n", stat, strerror_dev(dev));
1581          if (blocks > 0)
1582             printf("%d block%s of %d bytes in file %d\n",
1583                     blocks, blocks>1?"s":"", block_size, dev->file);
1584          return;
1585       }
1586       Dmsg1(200, "read status = %d\n", stat);
1587 /*    sleep(1); */
1588       if (stat != block_size) {
1589          update_pos_dev(dev);
1590          if (blocks > 0) {
1591             printf("%d block%s of %d bytes in file %d\n",
1592                  blocks, blocks>1?"s":"", block_size, dev->file);
1593             blocks = 0;
1594          }
1595          block_size = stat;
1596       }
1597       if (stat == 0) {                /* EOF */
1598          update_pos_dev(dev);
1599          printf("End of File mark.\n");
1600          /* Two reads of zero means end of tape */
1601          if (dev->state & ST_EOF)
1602             dev->state |= ST_EOT;
1603          else {
1604             dev->state |= ST_EOF;
1605             dev->file++;
1606          }
1607          if (dev->state & ST_EOT) {
1608             printf("End of tape\n");
1609             break;
1610          }
1611       } else {                        /* Got data */
1612          dev->state &= ~ST_EOF;
1613          blocks++;
1614          tot_blocks++;
1615          bytes += stat;
1616       }
1617    }
1618    update_pos_dev(dev);
1619    tot_files = dev->file - tot_files;
1620    printf("Total files=%d, blocks=%d, bytes = %s\n", tot_files, tot_blocks,
1621       edit_uint64_with_commas(bytes, ec1));
1622 }
1623
1624
1625 /*
1626  * Scan tape by reading Bacula block by block. Report what is
1627  * on the tape.  This function reads Bacula blocks, so if your
1628  * Device resource is correctly defined, it should work with
1629  * either variable or fixed block sizes.
1630  */
1631 static void scan_blocks()
1632 {
1633    int blocks, tot_blocks, tot_files;
1634    uint32_t block_size;
1635    uint64_t bytes;
1636    DEV_BLOCK *block = dcr->block;
1637    char ec1[50];
1638
1639    blocks = block_size = tot_blocks = 0;
1640    bytes = 0;
1641
1642    empty_block(block);
1643    update_pos_dev(dev);
1644    tot_files = dev->file;
1645    for (;;) {
1646       if (!read_block_from_device(dcr, NO_BLOCK_NUMBER_CHECK)) {
1647          Dmsg1(100, "!read_block(): ERR=%s\n", strerror_dev(dev));
1648          if (dev->state & ST_EOT) {
1649             if (blocks > 0) {
1650                printf("%d block%s of %d bytes in file %d\n",
1651                     blocks, blocks>1?"s":"", block_size, dev->file);
1652                blocks = 0;
1653             }
1654             goto bail_out;
1655          }
1656          if (dev->state & ST_EOF) {
1657             if (blocks > 0) {
1658                printf("%d block%s of %d bytes in file %d\n",
1659                        blocks, blocks>1?"s":"", block_size, dev->file);
1660                blocks = 0;
1661             }
1662             printf(_("End of File mark.\n"));
1663             continue;
1664          }
1665          if (dev->state & ST_SHORT) {
1666             if (blocks > 0) {
1667                printf("%d block%s of %d bytes in file %d\n",
1668                        blocks, blocks>1?"s":"", block_size, dev->file);
1669                blocks = 0;
1670             }
1671             printf(_("Short block read.\n"));
1672             continue;
1673          }
1674          printf(_("Error reading block. ERR=%s\n"), strerror_dev(dev));
1675          goto bail_out;
1676       }
1677       if (block->block_len != block_size) {
1678          if (blocks > 0) {
1679             printf("%d block%s of %d bytes in file %d\n",
1680                     blocks, blocks>1?"s":"", block_size, dev->file);
1681             blocks = 0;
1682          }
1683          block_size = block->block_len;
1684       }
1685       blocks++;
1686       tot_blocks++;
1687       bytes += block->block_len;
1688       Dmsg6(100, "Blk_blk=%u dev_blk=%u blen=%u bVer=%d SessId=%u SessTim=%u\n",
1689          block->BlockNumber, dev->block_num, block->block_len, block->BlockVer,
1690          block->VolSessionId, block->VolSessionTime);
1691       if (verbose == 1) {
1692          DEV_RECORD *rec = new_record();
1693          read_record_from_block(block, rec);
1694          Pmsg8(-1, "Blk_block: %u dev_blk=%u blen=%u First rec FI=%s SessId=%u SessTim=%u Strm=%s rlen=%d\n",
1695               block->BlockNumber, dev->block_num, block->block_len,
1696               FI_to_ascii(rec->FileIndex), rec->VolSessionId, rec->VolSessionTime,
1697               stream_to_ascii(rec->Stream, rec->FileIndex), rec->data_len);
1698          rec->remainder = 0;
1699          free_record(rec);
1700       } else if (verbose > 1) {
1701          dump_block(block, "");
1702       }
1703
1704    }
1705 bail_out:
1706    tot_files = dev->file - tot_files;
1707    printf("Total files=%d, blocks=%d, bytes = %s\n", tot_files, tot_blocks,
1708       edit_uint64_with_commas(bytes, ec1));
1709 }
1710
1711
1712 static void statcmd()
1713 {
1714    int debug = debug_level;
1715    debug_level = 30;
1716    Pmsg2(0, "Device status: %u. ERR=%s\n", status_dev(dev), strerror_dev(dev));
1717 #ifdef xxxx
1718    dump_volume_label(dev);
1719 #endif
1720    debug_level = debug;
1721 }
1722
1723
1724 /*
1725  * First we label the tape, then we fill
1726  *  it with data get a new tape and write a few blocks.
1727  */
1728 static void fillcmd()
1729 {
1730    DEV_RECORD rec;
1731    DEV_BLOCK  *block = dcr->block;
1732    char ec1[50];
1733    int fd;
1734    uint32_t i;
1735    uint32_t min_block_size;
1736
1737    ok = true;
1738    stop = 0;
1739    vol_num = 0;
1740    last_file = 0;
1741    last_block_num = 0;
1742    BlockNumber = 0;
1743
1744    Pmsg0(-1, "\n"
1745 "This command simulates Bacula writing to a tape.\n"
1746 "It requires either one or two blank tapes, which it\n"
1747 "will label and write.\n\n"
1748 "If you have an autochanger configured, it will use\n"
1749 "the tapes that are in slots 1 and 2, otherwise, you will\n"
1750 "be prompted to insert the tapes when necessary.\n\n"
1751 "It will print a status approximately\n"
1752 "every 322 MB, and write an EOF every 3.2 GB.  If you have\n"
1753 "selected the simple test option, after writing the first tape\n"
1754 "it will rewind it and re-read the last block written.\n\n"
1755 "If you have selected the multiple tape test, when the first tape\n"
1756 "fills, it will ask for a second, and after writing a few more \n"
1757 "blocks, it will stop.  Then it will begin re-reading the\n"
1758 "two tapes.\n\n"
1759 "This may take a long time -- hours! ...\n\n");
1760
1761    get_cmd("Do you want to run the simplified test (s) with one tape\n"
1762            "or the complete multiple tape (m) test: (s/m) ");
1763    if (cmd[0] == 's') {
1764       Pmsg0(-1, "Simple test (single tape) selected.\n");
1765       simple = true;
1766    } else if (cmd[0] == 'm') {
1767       Pmsg0(-1, "Multiple tape test selected.\n");
1768       simple = false;
1769    } else {
1770       Pmsg0(000, "Command aborted.\n");
1771       return;
1772    }
1773
1774    Dmsg1(20, "Begin append device=%s\n", dev->print_name());
1775    Dmsg1(20, "MaxVolSize=%s\n", edit_uint64(dev->max_volume_size, ec1));
1776
1777    /* Use fixed block size to simplify read back */
1778    min_block_size = dev->min_block_size;
1779    dev->min_block_size = dev->max_block_size;
1780    set_volume_name("TestVolume1", 1);
1781
1782    if (!dev->rewind()) {
1783       Pmsg0(000, "Rewind failed.\n");
1784    }
1785    if (!dev->weof()) {
1786       Pmsg0(000, "Write EOF failed.\n");
1787    }
1788    labelcmd();
1789    dev->set_append();                 /* force volume to be relabeled */
1790
1791    /*
1792     * Acquire output device for writing.  Note, after acquiring a
1793     *   device, we MUST release it, which is done at the end of this
1794     *   subroutine.
1795     */
1796    Dmsg0(100, "just before acquire_device\n");
1797    if (!acquire_device_for_append(dcr)) {
1798       set_jcr_job_status(jcr, JS_ErrorTerminated);
1799       return;
1800    }
1801    block = jcr->dcr->block;
1802
1803    Dmsg0(100, "Just after acquire_device_for_append\n");
1804    /*
1805     * Write Begin Session Record
1806     */
1807    if (!write_session_label(dcr, SOS_LABEL)) {
1808       set_jcr_job_status(jcr, JS_ErrorTerminated);
1809       Jmsg1(jcr, M_FATAL, 0, _("Write session label failed. ERR=%s\n"),
1810          strerror_dev(dev));
1811       ok = false;
1812    }
1813    Pmsg0(-1, "Wrote Start of Session label.\n");
1814
1815    memset(&rec, 0, sizeof(rec));
1816    rec.data = get_memory(100000);     /* max record size */
1817
1818 #define REC_SIZE 32768
1819    rec.data_len = REC_SIZE;
1820
1821    /*
1822     * Put some random data in the record
1823     */
1824    fd = open("/dev/urandom", O_RDONLY);
1825    if (fd) {
1826       read(fd, rec.data, rec.data_len);
1827       close(fd);
1828    } else {
1829       uint32_t *p = (uint32_t *)rec.data;
1830       srandom(time(NULL));
1831       for (i=0; i<rec.data_len/sizeof(uint32_t); i++) {
1832          p[i] = random();
1833       }
1834    }
1835
1836    /*
1837     * Generate data as if from File daemon, write to device
1838     */
1839    jcr->dcr->VolFirstIndex = 0;
1840    time(&jcr->run_time);              /* start counting time for rates */
1841    if (simple) {
1842       Pmsg0(-1, "Begin writing Bacula records to tape ...\n");
1843    } else {
1844       Pmsg0(-1, "Begin writing Bacula records to first tape ...\n");
1845    }
1846    for (file_index = 0; ok && !job_canceled(jcr); ) {
1847       rec.VolSessionId = jcr->VolSessionId;
1848       rec.VolSessionTime = jcr->VolSessionTime;
1849       rec.FileIndex = ++file_index;
1850       rec.Stream = STREAM_FILE_DATA;
1851
1852       /* Mix up the data just a bit */
1853       uint32_t *lp = (uint32_t *)rec.data;
1854       lp[0] += lp[13];
1855       for (i=1; i < (rec.data_len-sizeof(uint32_t))/sizeof(uint32_t)-1; i++) {
1856          lp[i] += lp[i-1];
1857       }
1858
1859       Dmsg4(250, "before write_rec FI=%d SessId=%d Strm=%s len=%d\n",
1860          rec.FileIndex, rec.VolSessionId, stream_to_ascii(rec.Stream, rec.FileIndex),
1861          rec.data_len);
1862
1863       while (!write_record_to_block(block, &rec)) {
1864          /*
1865           * When we get here we have just filled a block
1866           */
1867          Dmsg2(150, "!write_record_to_block data_len=%d rem=%d\n", rec.data_len,
1868                     rec.remainder);
1869
1870          /* Write block to tape */
1871          if (!flush_block(block, 1)) {
1872             break;
1873          }
1874
1875          /* Every 5000 blocks (approx 322MB) report where we are.
1876           */
1877          if ((block->BlockNumber % 5000) == 0) {
1878             now = time(NULL);
1879             now -= jcr->run_time;
1880             if (now <= 0) {
1881                now = 1;          /* prevent divide error */
1882             }
1883             kbs = (double)dev->VolCatInfo.VolCatBytes / (1000.0 * (double)now);
1884             Pmsg4(-1, "Wrote blk_block=%u, dev_blk_num=%u VolBytes=%s rate=%.1f KB/s\n",
1885                block->BlockNumber, dev->block_num,
1886                edit_uint64_with_commas(dev->VolCatInfo.VolCatBytes, ec1), (float)kbs);
1887          }
1888          /* Every 15000 blocks (approx 1GB) write an EOF.
1889           */
1890          if ((block->BlockNumber % 15000) == 0) {
1891             Pmsg0(-1, "Flush block, write EOF\n");
1892             flush_block(block, 0);
1893             weof_dev(dev, 1);
1894          }
1895
1896          /* Get out after writing 10 blocks to the second tape */
1897          if (++BlockNumber > 10 && stop != 0) {      /* get out */
1898             break;
1899          }
1900       }
1901       if (!ok) {
1902          Pmsg0(000, _("Not OK\n"));
1903          break;
1904       }
1905       jcr->JobBytes += rec.data_len;   /* increment bytes this job */
1906       Dmsg4(190, "write_record FI=%s SessId=%d Strm=%s len=%d\n",
1907          FI_to_ascii(rec.FileIndex), rec.VolSessionId,
1908          stream_to_ascii(rec.Stream, rec.FileIndex), rec.data_len);
1909
1910       /* Get out after writing 10 blocks to the second tape */
1911       if (BlockNumber > 10 && stop != 0) {      /* get out */
1912          Pmsg0(-1, "Done writing ...\n");
1913          break;
1914       }
1915    }
1916    if (vol_num > 1) {
1917       Dmsg0(100, "Write_end_session_label()\n");
1918       /* Create Job status for end of session label */
1919       if (!job_canceled(jcr) && ok) {
1920          set_jcr_job_status(jcr, JS_Terminated);
1921       } else if (!ok) {
1922          set_jcr_job_status(jcr, JS_ErrorTerminated);
1923       }
1924       if (!write_session_label(dcr, EOS_LABEL)) {
1925          Pmsg1(000, _("Error writting end session label. ERR=%s\n"), strerror_dev(dev));
1926          ok = false;
1927       }
1928       /* Write out final block of this session */
1929       if (!write_block_to_device(dcr)) {
1930          Pmsg0(-1, _("Set ok=false after write_block_to_device.\n"));
1931          ok = false;
1932       }
1933       Pmsg0(-1, _("Wrote End of Session label.\n"));
1934
1935       /* Save last block info for second tape */
1936       last_block_num2 = last_block_num;
1937       last_file2 = last_file;
1938       if (last_block2) {
1939          free_block(last_block2);
1940       }
1941       last_block2 = dup_block(last_block);
1942    }
1943
1944    sprintf(buf, "%s/btape.state", working_directory);
1945    fd = open(buf, O_CREAT|O_TRUNC|O_WRONLY, 0640);
1946    if (fd >= 0) {
1947       write(fd, &btape_state_level, sizeof(btape_state_level));
1948       write(fd, &simple, sizeof(simple));
1949       write(fd, &last_block_num1, sizeof(last_block_num1));
1950       write(fd, &last_block_num2, sizeof(last_block_num2));
1951       write(fd, &last_file1, sizeof(last_file1));
1952       write(fd, &last_file2, sizeof(last_file2));
1953       write(fd, last_block1->buf, last_block1->buf_len);
1954       write(fd, last_block2->buf, last_block2->buf_len);
1955       write(fd, first_block->buf, first_block->buf_len);
1956       close(fd);
1957       Pmsg2(-1, "Wrote state file last_block_num1=%d last_block_num2=%d\n",
1958          last_block_num1, last_block_num2);
1959    } else {
1960       berrno be;
1961       Pmsg2(-1, _("Could not create state file: %s ERR=%s\n"), buf,
1962                  be.strerror());
1963    }
1964
1965    Pmsg4(-1, _("\n\nDone filling tape%s at %d:%d. Now beginning re-read of %stape ...\n"),
1966       simple?"":"s", jcr->dcr->dev->file, jcr->dcr->dev->block_num, simple?"":"first ");
1967
1968    jcr->dcr->block = block;
1969    do_unfill();
1970
1971    dev->min_block_size = min_block_size;
1972    free_memory(rec.data);
1973 }
1974
1975 /*
1976  * Read two tapes written by the "fill" command and ensure
1977  *  that the data is valid.  If stop==1 we simulate full read back
1978  *  of two tapes.  If stop==-1 we simply read the last block and
1979  *  verify that it is correct.
1980  */
1981 static void unfillcmd()
1982 {
1983    int fd;
1984
1985    last_block1 = new_block(dev);
1986    last_block2 = new_block(dev);
1987    first_block = new_block(dev);
1988    sprintf(buf, "%s/btape.state", working_directory);
1989    fd = open(buf, O_RDONLY);
1990    if (fd >= 0) {
1991       uint32_t state_level;
1992       read(fd, &state_level, sizeof(btape_state_level));
1993       read(fd, &simple, sizeof(simple));
1994       read(fd, &last_block_num1, sizeof(last_block_num1));
1995       read(fd, &last_block_num2, sizeof(last_block_num2));
1996       read(fd, &last_file1, sizeof(last_file1));
1997       read(fd, &last_file2, sizeof(last_file2));
1998       read(fd, last_block1->buf, last_block1->buf_len);
1999       read(fd, last_block2->buf, last_block2->buf_len);
2000       read(fd, first_block->buf, first_block->buf_len);
2001       close(fd);
2002       if (state_level != btape_state_level) {
2003           Pmsg0(-1, "\nThe state file level has changed. You must redo\n"
2004                   "the fill command.\n");
2005           return;
2006        }
2007    } else {
2008       berrno be;
2009       Pmsg2(-1, "\nCould not find the state file: %s ERR=%s\n"
2010              "You must redo the fill command.\n", buf, be.strerror());
2011       return;
2012    }
2013    do_unfill();
2014    this_block = NULL;
2015 }
2016
2017 static void do_unfill()
2018 {
2019    DEV_BLOCK *block = dcr->block;
2020    bool autochanger;
2021
2022    dumped = 0;
2023    VolBytes = 0;
2024    LastBlock = 0;
2025
2026    Dmsg0(20, "Enter do_unfill\n");
2027    dev->capabilities |= CAP_ANONVOLS; /* allow reading any volume */
2028    dev->capabilities &= ~CAP_LABEL;   /* don't label anything here */
2029
2030    end_of_tape = 0;
2031
2032    time(&jcr->run_time);              /* start counting time for rates */
2033    stop = 0;
2034    file_index = 0;
2035    if (last_block) {
2036       free_block(last_block);
2037    }
2038    last_block_num = last_block_num1;
2039    last_file = last_file1;
2040    last_block = last_block1;
2041
2042    if (!simple) {
2043       /* Multiple Volume tape */
2044       /* Close device so user can use autochanger if desired */
2045       if (dev_cap(dev, CAP_OFFLINEUNMOUNT)) {
2046          offline_dev(dev);
2047       }
2048       autochanger = autoload_device(dcr, 1, NULL);
2049       if (!autochanger) {
2050          force_close_dev(dev);
2051          get_cmd(_("Mount first tape. Press enter when ready: "));
2052       }
2053       free_vol_list(jcr);
2054       jcr->dcr = new_dcr(jcr, dev);
2055       set_volume_name("TestVolume1", 1);
2056       jcr->bsr = NULL;
2057       create_vol_list(jcr);
2058       close_dev(dev);
2059       dev->state &= ~(ST_READ|ST_APPEND);
2060       dev->num_writers = 0;
2061       if (!acquire_device_for_read(dcr)) {
2062          Pmsg1(-1, "%s", dev->errmsg);
2063          goto bail_out;
2064       }
2065    }
2066    /*
2067     * We now have the first tape mounted.
2068     * Note, re-reading last block may have caused us to
2069     *   loose track of where we are (block number unknown).
2070     */
2071    if (!rewind_dev(dev)) {                /* get to a known place on tape */
2072       goto bail_out;
2073    }
2074    /* Read the first 10000 records */
2075    Pmsg0(-1, _("Reading the first 10000 records.\n"));
2076    quickie_count = 0;
2077    read_records(dcr, quickie_cb, my_mount_next_read_volume);
2078    Pmsg4(-1, _("Reposition from %u:%u to %u:%u\n"), dev->file, dev->block_num,
2079          last_file, last_block_num);
2080    if (!reposition_dev(dev, last_file, last_block_num)) {
2081       Pmsg1(-1, "Reposition error. ERR=%s\n", strerror_dev(dev));
2082       goto bail_out;
2083    }
2084    Pmsg1(-1, _("Reading block %u.\n"), last_block_num);
2085    if (!read_block_from_device(dcr, NO_BLOCK_NUMBER_CHECK)) {
2086       Pmsg1(-1, _("Error reading block: ERR=%s\n"), strerror_dev(dev));
2087       goto bail_out;
2088    }
2089    if (compare_blocks(last_block, block)) {
2090       if (simple) {
2091          Pmsg0(-1, _("\nThe last block on the tape matches. Test succeeded.\n\n"));
2092       } else {
2093          Pmsg0(-1, _("\nThe last block of the first tape matches.\n\n"));
2094       }
2095    }
2096    if (simple) {
2097       goto bail_out;
2098    }
2099
2100    /* restore info for last block on second Volume */
2101    last_block_num = last_block_num2;
2102    last_file = last_file2;
2103    last_block = last_block2;
2104
2105    /* Multiple Volume tape */
2106    /* Close device so user can use autochanger if desired */
2107    if (dev_cap(dev, CAP_OFFLINEUNMOUNT)) {
2108       offline_dev(dev);
2109    }
2110
2111    free_vol_list(jcr);
2112    set_volume_name("TestVolume2", 2);
2113    jcr->bsr = NULL;
2114    create_vol_list(jcr);
2115    autochanger = autoload_device(dcr, 1, NULL);
2116    if (!autochanger) {
2117       force_close_dev(dev);
2118       get_cmd(_("Mount second tape. Press enter when ready: "));
2119    }
2120
2121    dev->clear_read();
2122    if (!acquire_device_for_read(dcr)) {
2123       Pmsg1(-1, "%s", dev->errmsg);
2124       goto bail_out;
2125    }
2126
2127    /* Space to "first" block which is last block not written
2128     * on the previous tape.
2129     */
2130    Pmsg2(-1, _("Reposition from %u:%u to 0:1\n"), dev->file, dev->block_num);
2131    if (!reposition_dev(dev, 0, 1)) {
2132       Pmsg1(-1, "Reposition error. ERR=%s\n", strerror_dev(dev));
2133       goto bail_out;
2134    }
2135    Pmsg1(-1, _("Reading block %d.\n"), dev->block_num);
2136    if (!read_block_from_device(dcr, NO_BLOCK_NUMBER_CHECK)) {
2137       Pmsg1(-1, _("Error reading block: ERR=%s\n"), strerror_dev(dev));
2138       goto bail_out;
2139    }
2140    if (compare_blocks(first_block, block)) {
2141       Pmsg0(-1, _("\nThe first block on the second tape matches.\n\n"));
2142    }
2143
2144    /* Now find and compare the last block */
2145    Pmsg4(-1, _("Reposition from %u:%u to %u:%u\n"), dev->file, dev->block_num,
2146          last_file, last_block_num);
2147    if (!reposition_dev(dev, last_file, last_block_num)) {
2148       Pmsg1(-1, "Reposition error. ERR=%s\n", strerror_dev(dev));
2149       goto bail_out;
2150    }
2151    Pmsg1(-1, _("Reading block %d.\n"), dev->block_num);
2152    if (!read_block_from_device(dcr, NO_BLOCK_NUMBER_CHECK)) {
2153       Pmsg1(-1, _("Error reading block: ERR=%s\n"), strerror_dev(dev));
2154       goto bail_out;
2155    }
2156    if (compare_blocks(last_block, block)) {
2157       Pmsg0(-1, _("\nThe last block on the second tape matches. Test succeeded.\n\n"));
2158    }
2159
2160 bail_out:
2161    free_block(last_block1);
2162    free_block(last_block2);
2163    free_block(first_block);
2164 }
2165
2166 /* Read 10000 records then stop */
2167 static bool quickie_cb(DCR *dcr, DEV_RECORD *rec)
2168 {
2169    DEVICE *dev = dcr->dev;
2170    if (dev->file != 0) {
2171       Pmsg3(-1, "ERROR! device at %d:%d count=%d\n", dev->file, dev->block_num,
2172          quickie_count);
2173       return false;
2174    }
2175    quickie_count++;
2176    if (quickie_count == 10000) {
2177       Pmsg2(-1, "1000 records read now at %d:%d\n", dev->file, dev->block_num);
2178    }
2179    return quickie_count < 10000;
2180 }
2181
2182 static bool compare_blocks(DEV_BLOCK *last_block, DEV_BLOCK *block)
2183 {
2184    char *p, *q;
2185    uint32_t CheckSum, block_len;
2186    ser_declare;
2187
2188    p = last_block->buf;
2189    q = block->buf;
2190    unser_begin(q, BLKHDR2_LENGTH);
2191    unser_uint32(CheckSum);
2192    unser_uint32(block_len);
2193    while (q < (block->buf+block_len)) {
2194       if (*p == *q) {
2195          p++;
2196          q++;
2197          continue;
2198       }
2199       Pmsg0(-1, "\n");
2200       dump_block(last_block, _("Last block written"));
2201       Pmsg0(-1, "\n");
2202       dump_block(block, _("Block read back"));
2203       Pmsg1(-1, "\n\nThe blocks differ at byte %u\n", p - last_block->buf);
2204       Pmsg0(-1, "\n\n!!!! The last block written and the block\n"
2205                 "that was read back differ. The test FAILED !!!!\n"
2206                 "This must be corrected before you use Bacula\n"
2207                 "to write multi-tape Volumes.!!!!\n");
2208       return false;
2209    }
2210    if (verbose) {
2211       dump_block(last_block, _("Last block written"));
2212       dump_block(block, _("Block read back"));
2213    }
2214    return true;
2215 }
2216
2217
2218
2219
2220
2221 /*
2222  * Write current block to tape regardless of whether or
2223  *   not it is full. If the tape fills, attempt to
2224  *   acquire another tape.
2225  */
2226 static int flush_block(DEV_BLOCK *block, int dump)
2227 {
2228    char ec1[50];
2229    DEV_BLOCK *tblock;
2230    uint32_t this_file, this_block_num;
2231
2232    lock_device(dev);
2233    if (!this_block) {
2234       this_block = new_block(dev);
2235    }
2236    if (!last_block) {
2237       last_block = new_block(dev);
2238    }
2239    /* Copy block */
2240    this_file = dev->file;
2241    this_block_num = dev->block_num;
2242    if (!write_block_to_dev(dcr)) {
2243       Pmsg3(000, "Last block at: %u:%u this_dev_block_num=%d\n",
2244                   last_file, last_block_num, this_block_num);
2245       if (vol_num == 1) {
2246          /*
2247           * This is 1st tape, so save first tape info separate
2248           *  from second tape info
2249           */
2250          last_block_num1 = last_block_num;
2251          last_file1 = last_file;
2252          last_block1 = dup_block(last_block);
2253          last_block2 = dup_block(last_block);
2254          first_block = dup_block(block); /* first block second tape */
2255       }
2256       if (verbose) {
2257          Pmsg3(000, "Block not written: FileIndex=%u blk_block=%u Size=%u\n",
2258             (unsigned)file_index, block->BlockNumber, block->block_len);
2259          dump_block(last_block, "Last block written");
2260          Pmsg0(-1, "\n");
2261          dump_block(block, "Block not written");
2262       }
2263       if (stop == 0) {
2264          eot_block = block->BlockNumber;
2265          eot_block_len = block->block_len;
2266          eot_FileIndex = file_index;
2267          stop = 1;
2268       }
2269       now = time(NULL);
2270       now -= jcr->run_time;
2271       if (now <= 0) {
2272          now = 1;                     /* don't divide by zero */
2273       }
2274       kbs = (double)dev->VolCatInfo.VolCatBytes / (1000 * now);
2275       vol_size = dev->VolCatInfo.VolCatBytes;
2276       Pmsg4(000, "End of tape %d:%d. VolumeCapacity=%s. Write rate = %.1f KB/s\n",
2277          dev->file, dev->block_num,
2278          edit_uint64_with_commas(dev->VolCatInfo.VolCatBytes, ec1), kbs);
2279
2280       if (simple) {
2281          stop = -1;                   /* stop, but do simplified test */
2282       } else {
2283          /* Full test in progress */
2284          if (!fixup_device_block_write_error(jcr->dcr)) {
2285             Pmsg1(000, _("Cannot fixup device error. %s\n"), strerror_dev(dev));
2286             ok = false;
2287             unlock_device(dev);
2288             return 0;
2289          }
2290          BlockNumber = 0;             /* start counting for second tape */
2291       }
2292       unlock_device(dev);
2293       return 1;                       /* end of tape reached */
2294    }
2295
2296    /* Save contents after write so that the header is serialized */
2297    memcpy(this_block->buf, block->buf, this_block->buf_len);
2298
2299    /*
2300     * Note, we always read/write to block, but we toggle
2301     *  copying it to one or another of two allocated blocks.
2302     * Switch blocks so that the block just successfully written is
2303     *  always in last_block.
2304     */
2305    tblock = last_block;
2306    last_block = this_block;
2307    this_block = tblock;
2308    last_file = this_file;
2309    last_block_num = this_block_num;
2310
2311    unlock_device(dev);
2312    return 1;
2313 }
2314
2315
2316 /*
2317  * First we label the tape, then we fill
2318  *  it with data get a new tape and write a few blocks.
2319  */
2320 static void qfillcmd()
2321 {
2322    DEV_BLOCK *block = dcr->block;
2323    DEV_RECORD *rec = dcr->rec;
2324    int i, count;
2325
2326    Pmsg0(0, "Test writing blocks of 64512 bytes to tape.\n");
2327
2328    get_cmd("How many blocks do you want to write? (1000): ");
2329
2330    count = atoi(cmd);
2331    if (count <= 0) {
2332       count = 1000;
2333    }
2334
2335    sm_check(__FILE__, __LINE__, false);
2336
2337    i = block->buf_len - 100;
2338    ASSERT (i > 0);
2339    rec->data = check_pool_memory_size(rec->data, i);
2340    memset(rec->data, i & 0xFF, i);
2341    rec->data_len = i;
2342    rewindcmd();
2343    Pmsg1(0, "Begin writing %d Bacula blocks to tape ...\n", count);
2344    for (i=0; i < count; i++) {
2345       if (i % 100 == 0) {
2346          printf("+");
2347          fflush(stdout);
2348       }
2349       if (!write_record_to_block(block, rec)) {
2350          Pmsg0(0, _("Error writing record to block.\n"));
2351          goto bail_out;
2352       }
2353       if (!write_block_to_dev(dcr)) {
2354          Pmsg0(0, _("Error writing block to device.\n"));
2355          goto bail_out;
2356       }
2357    }
2358    printf("\n");
2359    weofcmd();
2360    if (dev_cap(dev, CAP_TWOEOF)) {
2361       weofcmd();
2362    }
2363    rewindcmd();
2364    scan_blocks();
2365
2366 bail_out:
2367    sm_check(__FILE__, __LINE__, false);
2368 }
2369
2370 /*
2371  * Fill a tape using raw write() command
2372  */
2373 static void rawfill_cmd()
2374 {
2375    DEV_BLOCK *block = dcr->block;
2376    int stat;
2377    int fd;
2378    uint32_t block_num = 0;
2379    uint32_t *p;
2380    int my_errno;
2381    uint32_t i;
2382
2383    fd = open("/dev/urandom", O_RDONLY);
2384    if (fd) {
2385       read(fd, block->buf, block->buf_len);
2386       close(fd);
2387    } else {
2388       uint32_t *p = (uint32_t *)block->buf;
2389       srandom(time(NULL));
2390       for (i=0; i<block->buf_len/sizeof(uint32_t); i++) {
2391          p[i] = random();
2392       }
2393    }
2394    p = (uint32_t *)block->buf;
2395    Pmsg1(0, "Begin writing raw blocks of %u bytes.\n", block->buf_len);
2396    for ( ;; ) {
2397       *p = block_num;
2398       stat = write(dev->fd, block->buf, block->buf_len);
2399       if (stat == (int)block->buf_len) {
2400          if ((block_num++ % 100) == 0) {
2401             printf("+");
2402             fflush(stdout);
2403          }
2404          p[0] += p[13];
2405          for (i=1; i<(block->buf_len-sizeof(uint32_t))/sizeof(uint32_t)-1; i++) {
2406             p[i] += p[i-1];
2407          }
2408          continue;
2409       }
2410       break;
2411    }
2412    my_errno = errno;
2413    printf("\n");
2414    berrno be;
2415    printf("Write failed at block %u. stat=%d ERR=%s\n", block_num, stat,
2416       be.strerror(my_errno));
2417    weofcmd();
2418 }
2419
2420
2421 /*
2422  * Fill a tape using Bacula block writes
2423  */
2424 static void bfill_cmd()
2425 {
2426    DEV_BLOCK *block = dcr->block;
2427    uint32_t block_num = 0;
2428    uint32_t *p;
2429    int my_errno;
2430    int fd;
2431    uint32_t i;
2432
2433    fd = open("/dev/urandom", O_RDONLY);
2434    if (fd) {
2435       read(fd, block->buf, block->buf_len);
2436       close(fd);
2437    } else {
2438       uint32_t *p = (uint32_t *)block->buf;
2439       srandom(time(NULL));
2440       for (i=0; i<block->buf_len/sizeof(uint32_t); i++) {
2441          p[i] = random();
2442       }
2443    }
2444    p = (uint32_t *)block->buf;
2445    Pmsg1(0, "Begin writing Bacula blocks of %u bytes.\n", block->buf_len);
2446    for ( ;; ) {
2447       *p = block_num;
2448       block->binbuf = block->buf_len;
2449       block->bufp = block->buf + block->binbuf;
2450       if (!write_block_to_dev(dcr)) {
2451          break;
2452       }
2453       if ((block_num++ % 100) == 0) {
2454          printf("+");
2455          fflush(stdout);
2456       }
2457       p[0] += p[13];
2458       for (i=1; i<(block->buf_len/sizeof(uint32_t)-1); i++) {
2459          p[i] += p[i-1];
2460       }
2461    }
2462    my_errno = errno;
2463    printf("\n");
2464    printf("Write failed at block %u.\n", block_num);
2465    weofcmd();
2466 }
2467
2468
2469 struct cmdstruct { const char *key; void (*func)(); const char *help; };
2470 static struct cmdstruct commands[] = {
2471  {"autochanger", autochangercmd, "test autochanger"},
2472  {"bsf",        bsfcmd,       "backspace file"},
2473  {"bsr",        bsrcmd,       "backspace record"},
2474  {"bfill",      bfill_cmd,    "fill tape using Bacula writes"},
2475  {"cap",        capcmd,       "list device capabilities"},
2476  {"clear",      clearcmd,     "clear tape errors"},
2477  {"eod",        eodcmd,       "go to end of Bacula data for append"},
2478  {"eom",        eomcmd,       "go to the physical end of medium"},
2479  {"fill",       fillcmd,      "fill tape, write onto second volume"},
2480  {"unfill",     unfillcmd,    "read filled tape"},
2481  {"fsf",        fsfcmd,       "forward space a file"},
2482  {"fsr",        fsrcmd,       "forward space a record"},
2483  {"help",       helpcmd,      "print this command"},
2484  {"label",      labelcmd,     "write a Bacula label to the tape"},
2485  {"load",       loadcmd,      "load a tape"},
2486  {"quit",       quitcmd,      "quit btape"},
2487  {"rawfill",    rawfill_cmd,  "use write() to fill tape"},
2488  {"readlabel",  readlabelcmd, "read and print the Bacula tape label"},
2489  {"rectest",    rectestcmd,   "test record handling functions"},
2490  {"rewind",     rewindcmd,    "rewind the tape"},
2491  {"scan",       scancmd,      "read() tape block by block to EOT and report"},
2492  {"scanblocks", scan_blocks,  "Bacula read block by block to EOT and report"},
2493  {"status",     statcmd,      "print tape status"},
2494  {"test",       testcmd,      "General test Bacula tape functions"},
2495  {"weof",       weofcmd,      "write an EOF on the tape"},
2496  {"wr",         wrcmd,        "write a single Bacula block"},
2497  {"rr",         rrcmd,        "read a single record"},
2498  {"qfill",      qfillcmd,     "quick fill command"},
2499              };
2500 #define comsize (sizeof(commands)/sizeof(struct cmdstruct))
2501
2502 static void
2503 do_tape_cmds()
2504 {
2505    unsigned int i;
2506    bool found;
2507
2508    while (get_cmd("*")) {
2509       sm_check(__FILE__, __LINE__, false);
2510       found = false;
2511       parse_args(cmd, &args, &argc, argk, argv, MAX_CMD_ARGS);
2512       for (i=0; i<comsize; i++)       /* search for command */
2513          if (argc > 0 && fstrsch(argk[0],  commands[i].key)) {
2514             (*commands[i].func)();    /* go execute command */
2515             found = true;
2516             break;
2517          }
2518       if (!found)
2519          Pmsg1(0, _("%s is an illegal command\n"), cmd);
2520       if (quit)
2521          break;
2522    }
2523 }
2524
2525 static void helpcmd()
2526 {
2527    unsigned int i;
2528    usage();
2529    printf(_("Interactive commands:\n"));
2530    printf(_("  Command    Description\n  =======    ===========\n"));
2531    for (i=0; i<comsize; i++)
2532       printf("  %-10s %s\n", commands[i].key, commands[i].help);
2533    printf("\n");
2534 }
2535
2536 static void usage()
2537 {
2538    fprintf(stderr, _(
2539 "Copyright (C) 2000-2005 Kern Sibbald.\n"
2540 "\nVersion: " VERSION " (" BDATE ")\n\n"
2541 "Usage: btape <options> <device_name>\n"
2542 "       -b <file>   specify bootstrap file\n"
2543 "       -c <file>   set configuration file to file\n"
2544 "       -d <nn>     set debug level to nn\n"
2545 "       -p          proceed inspite of I/O errors\n"
2546 "       -s          turn off signals\n"
2547 "       -v          be verbose\n"
2548 "       -?          print this message.\n"
2549 "\n"));
2550
2551 }
2552
2553 /*
2554  * Get next input command from terminal.  This
2555  * routine is REALLY primitive, and should be enhanced
2556  * to have correct backspacing, etc.
2557  */
2558 int
2559 get_cmd(const char *prompt)
2560 {
2561    int i = 0;
2562    int ch;
2563    fprintf(stdout, prompt);
2564
2565    /* We really should turn off echoing and pretty this
2566     * up a bit.
2567     */
2568    cmd[i] = 0;
2569    while ((ch = fgetc(stdin)) != EOF) {
2570       if (ch == '\n') {
2571          strip_trailing_junk(cmd);
2572          return 1;
2573       } else if (ch == 4 || ch == 0xd3 || ch == 0x8) {
2574          if (i > 0)
2575             cmd[--i] = 0;
2576          continue;
2577       }
2578
2579       cmd[i++] = ch;
2580       cmd[i] = 0;
2581    }
2582    quit = 1;
2583    return 0;
2584 }
2585
2586 /* Dummies to replace askdir.c */
2587 bool    dir_update_file_attributes(DCR *dcr, DEV_RECORD *rec) { return 1;}
2588 bool    dir_send_job_status(JCR *jcr) {return 1;}
2589
2590 bool dir_update_volume_info(DCR *dcr, bool relabel)
2591 {
2592    return 1;
2593 }
2594
2595
2596 bool dir_get_volume_info(DCR *dcr, enum get_vol_info_rw  writing)
2597 {
2598    Dmsg0(20, "Enter dir_get_volume_info\n");
2599    bstrncpy(dcr->VolCatInfo.VolCatName, dcr->VolumeName, sizeof(dcr->VolCatInfo.VolCatName));
2600    return 1;
2601 }
2602
2603 bool dir_create_jobmedia_record(DCR *dcr)
2604 {
2605    dcr->WroteVol = false;
2606    return 1;
2607 }
2608
2609
2610 bool dir_find_next_appendable_volume(DCR *dcr)
2611 {
2612    Dmsg1(20, "Enter dir_find_next_appendable_volume. stop=%d\n", stop);
2613    return dcr->VolumeName[0] != 0;
2614 }
2615
2616 bool dir_ask_sysop_to_mount_volume(DCR *dcr)
2617 {
2618    DEVICE *dev = dcr->dev;
2619    Dmsg0(20, "Enter dir_ask_sysop_to_mount_volume\n");
2620    if (dcr->VolumeName[0] == 0) {
2621       return dir_ask_sysop_to_create_appendable_volume(dcr);
2622    }
2623    /* Close device so user can use autochanger if desired */
2624    if (dev_cap(dev, CAP_OFFLINEUNMOUNT)) {
2625       offline_dev(dev);
2626    }
2627    force_close_dev(dev);
2628    Pmsg1(-1, "%s", dev->errmsg);           /* print reason */
2629    if (dcr->VolumeName[0] == 0 || strcmp(dcr->VolumeName, "TestVolume2") == 0) {
2630       fprintf(stderr, "Mount second Volume on device %s and press return when ready: ",
2631          dev->print_name());
2632    } else {
2633       fprintf(stderr, "Mount Volume \"%s\" on device %s and press return when ready: ",
2634          dcr->VolumeName, dev->print_name());
2635    }
2636    getchar();
2637    return true;
2638 }
2639
2640 bool dir_ask_sysop_to_create_appendable_volume(DCR *dcr)
2641 {
2642    bool autochanger;
2643    DEVICE *dev = dcr->dev;
2644    Dmsg0(20, "Enter dir_ask_sysop_to_create_appendable_volume\n");
2645    if (stop == 0) {
2646       set_volume_name("TestVolume1", 1);
2647    } else {
2648       set_volume_name("TestVolume2", 2);
2649    }
2650    /* Close device so user can use autochanger if desired */
2651    if (dev_cap(dev, CAP_OFFLINEUNMOUNT)) {
2652       offline_dev(dev);
2653    }
2654    autochanger = autoload_device(dcr, 1, NULL);
2655    if (!autochanger) {
2656       force_close_dev(dev);
2657       fprintf(stderr, "Mount blank Volume on device %s and press return when ready: ",
2658          dev->print_name());
2659       getchar();
2660    }
2661    open_device(dcr);
2662    labelcmd();
2663    VolumeName = NULL;
2664    BlockNumber = 0;
2665    return true;
2666 }
2667
2668 static bool my_mount_next_read_volume(DCR *dcr)
2669 {
2670    char ec1[50];
2671    JCR *jcr = dcr->jcr;
2672    DEV_BLOCK *block = dcr->block;
2673
2674    Dmsg0(20, "Enter my_mount_next_read_volume\n");
2675    Pmsg1(000, "End of Volume \"%s\"\n", dcr->VolumeName);
2676
2677    if (LastBlock != block->BlockNumber) {
2678       VolBytes += block->block_len;
2679    }
2680    LastBlock = block->BlockNumber;
2681    now = time(NULL);
2682    now -= jcr->run_time;
2683    if (now <= 0) {
2684       now = 1;
2685    }
2686    kbs = (double)VolBytes / (1000.0 * (double)now);
2687    Pmsg3(-1, "Read block=%u, VolBytes=%s rate=%.1f KB/s\n", block->BlockNumber,
2688             edit_uint64_with_commas(VolBytes, ec1), (float)kbs);
2689
2690    if (strcmp(dcr->VolumeName, "TestVolume2") == 0) {
2691       end_of_tape = 1;
2692       return false;
2693    }
2694
2695    free_vol_list(jcr);
2696    set_volume_name("TestVolume2", 2);
2697    jcr->bsr = NULL;
2698    create_vol_list(jcr);
2699    close_dev(dev);
2700    dev->clear_read();
2701    if (!acquire_device_for_read(dcr)) {
2702       Pmsg2(0, "Cannot open Dev=%s, Vol=%s\n", dev->print_name(), dcr->VolumeName);
2703       return false;
2704    }
2705    return true;                    /* next volume mounted */
2706 }
2707
2708 static void set_volume_name(const char *VolName, int volnum)
2709 {
2710    DCR *dcr = jcr->dcr;
2711    VolumeName = VolName;
2712    vol_num = volnum;
2713    bstrncpy(dev->VolCatInfo.VolCatName, VolName, sizeof(dev->VolCatInfo.VolCatName));
2714    bstrncpy(dcr->VolCatInfo.VolCatName, VolName, sizeof(dcr->VolCatInfo.VolCatName));
2715    bstrncpy(dcr->VolumeName, VolName, sizeof(dcr->VolumeName));
2716    dcr->VolCatInfo.Slot = volnum;
2717 }