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