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