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