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