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