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