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