]> git.sur5r.net Git - bacula/bacula/blob - bacula/src/stored/block_util.c
Apply patches from bugs #2325 and #2326 to fix FIFO bugs
[bacula/bacula] / bacula / src / stored / block_util.c
1 /*
2    Bacula(R) - The Network Backup Solution
3
4    Copyright (C) 2000-2017 Kern Sibbald
5
6    The original author of Bacula is Kern Sibbald, with contributions
7    from many others, a complete list can be found in the file AUTHORS.
8
9    You may use this file and others of this release according to the
10    license defined in the LICENSE file, which includes the Affero General
11    Public License, v3.0 ("AGPLv3") and some additional permissions and
12    terms pursuant to its AGPLv3 Section 7.
13
14    This notice must be preserved when any source code is
15    conveyed and/or propagated.
16
17    Bacula(R) is a registered trademark of Kern Sibbald.
18 */
19 /*
20  *
21  *   block_util.c -- tape block utility functions
22  *
23  *              Kern Sibbald, split from block.c March MMXII
24  */
25
26
27 #include "bacula.h"
28 #include "stored.h"
29
30 static const int dbglvl = 160;
31
32 #ifdef DEBUG_BLOCK_CHECKSUM
33 static const bool debug_block_checksum = true;
34 #else
35 static const bool debug_block_checksum = false;
36 #endif
37
38 #ifdef NO_TAPE_WRITE_TEST
39 static const bool no_tape_write_test = true;
40 #else
41 static const bool no_tape_write_test = false;
42 #endif
43
44 /*
45  * Dump the block header, then walk through
46  * the block printing out the record headers.
47  */
48 void dump_block(DEVICE *dev, DEV_BLOCK *b, const char *msg, bool force)
49 {
50    ser_declare;
51    char *p;
52    char *bufp;
53    char Id[BLKHDR_ID_LENGTH+1];
54    uint32_t CheckSum, BlockCheckSum;
55    uint32_t block_len, reclen;
56    uint32_t BlockNumber;
57    uint32_t VolSessionId, VolSessionTime, data_len;
58    int32_t  FileIndex;
59    int32_t  Stream;
60    int bhl, rhl;
61    char buf1[100], buf2[100];
62
63    if (!force && ((debug_level & ~DT_ALL) < 250)) {
64       return;
65    }
66    if (b->adata) {
67       Dmsg0(20, "Dump block: adata=1 cannot dump.\n");
68       return;
69    }
70    bufp = b->bufp;
71    if (dev) {
72       if (dev->can_read()) {
73          bufp = b->buf + b->block_len;
74       }
75    }
76    unser_begin(b->buf, BLKHDR1_LENGTH);
77    unser_uint32(CheckSum);
78    unser_uint32(block_len);
79    unser_uint32(BlockNumber);
80    unser_bytes(Id, BLKHDR_ID_LENGTH);
81    ASSERT(unser_length(b->buf) == BLKHDR1_LENGTH);
82    Id[BLKHDR_ID_LENGTH] = 0;
83    if (Id[3] == '2') {
84       unser_uint32(VolSessionId);
85       unser_uint32(VolSessionTime);
86       bhl = BLKHDR2_LENGTH;
87       rhl = RECHDR2_LENGTH;
88    } else {
89       VolSessionId = VolSessionTime = 0;
90       bhl = BLKHDR1_LENGTH;
91       rhl = RECHDR1_LENGTH;
92    }
93
94    if (block_len > 4000000 || block_len < BLKHDR_CS_LENGTH) {
95       Dmsg3(20, "Will not dump blocksize too %s %lu msg: %s\n",
96             (block_len < BLKHDR_CS_LENGTH)?"small":"big",
97             block_len, msg);
98       return;
99    }
100
101    BlockCheckSum = bcrc32((uint8_t *)b->buf+BLKHDR_CS_LENGTH,
102                          block_len-BLKHDR_CS_LENGTH);
103    Pmsg7(000, _("Dump block %s %p: adata=%d size=%d BlkNum=%d\n"
104 "                           Hdrcksum=%x cksum=%x\n"),
105       msg, b, b->adata, block_len, BlockNumber, CheckSum, BlockCheckSum);
106    p = b->buf + bhl;
107    while (p < bufp) {
108       unser_begin(p, WRITE_RECHDR_LENGTH);
109       if (rhl == RECHDR1_LENGTH) {
110          unser_uint32(VolSessionId);
111          unser_uint32(VolSessionTime);
112       }
113       unser_int32(FileIndex);
114       unser_int32(Stream);
115       unser_uint32(data_len);
116       if (Stream == STREAM_ADATA_BLOCK_HEADER) {
117          reclen = 0;
118          p += WRITE_ADATA_BLKHDR_LENGTH;
119       } else if (Stream == STREAM_ADATA_RECORD_HEADER ||
120                  Stream == -STREAM_ADATA_RECORD_HEADER) {
121          unser_uint32(reclen);
122          unser_int32(Stream);
123          p += WRITE_ADATA_RECHDR_LENGTH;
124       } else {
125          reclen = 0;
126          p += data_len + rhl;
127       }
128       Pmsg6(000, _("   Rec: VId=%u VT=%u FI=%s Strm=%s len=%d reclen=%d\n"),
129            VolSessionId, VolSessionTime, FI_to_ascii(buf1, FileIndex),
130            stream_to_ascii(buf2, Stream, FileIndex), data_len, reclen);
131   }
132 }
133
134 /*
135  * Create a new block structure.
136  * We pass device so that the block can inherit the
137  * min and max block sizes.
138  */
139 void DEVICE::new_dcr_blocks(DCR *dcr)
140 {
141    dcr->block = dcr->ameta_block = new_block(dcr);
142 }
143
144 DEV_BLOCK *DEVICE::new_block(DCR *dcr, int size)
145 {
146    DEV_BLOCK *block = (DEV_BLOCK *)get_memory(sizeof(DEV_BLOCK));
147    int len;
148
149    memset(block, 0, sizeof(DEV_BLOCK));
150
151    /* If the user has specified a max_block_size, use it as the default */
152    if (max_block_size == 0) {
153       len = DEFAULT_BLOCK_SIZE;
154    } else {
155       len = max_block_size;
156    }
157    block->dev = this;
158    /* special size */
159    if (size) {
160       len = size;
161    }
162    block->buf_len = len;
163    block->buf = get_memory(block->buf_len);
164    block->rechdr_queue = get_memory(block->buf_len);
165    block->rechdr_items = 0;
166    Dmsg2(510, "Rechdr len=%d max_items=%d\n", sizeof_pool_memory(block->rechdr_queue),
167       sizeof_pool_memory(block->rechdr_queue)/WRITE_ADATA_RECHDR_LENGTH);
168    empty_block(block);
169    block->BlockVer = BLOCK_VER;       /* default write version */
170    Dmsg3(150, "New block adata=%d len=%d block=%p\n", block->adata, len, block);
171    return block;
172 }
173
174
175 /*
176  * Duplicate an existing block (eblock)
177  */
178 DEV_BLOCK *dup_block(DEV_BLOCK *eblock)
179 {
180    DEV_BLOCK *block = (DEV_BLOCK *)get_memory(sizeof(DEV_BLOCK));
181    int buf_len = sizeof_pool_memory(eblock->buf);
182    int rechdr_len = sizeof_pool_memory(eblock->rechdr_queue);
183
184    memcpy(block, eblock, sizeof(DEV_BLOCK));
185    block->buf = get_memory(buf_len);
186    memcpy(block->buf, eblock->buf, buf_len);
187
188    block->rechdr_queue = get_memory(rechdr_len);
189    memcpy(block->rechdr_queue, eblock->rechdr_queue, rechdr_len);
190
191    /* bufp might point inside buf */
192    if (eblock->bufp &&
193        eblock->bufp >= eblock->buf &&
194        eblock->bufp < (eblock->buf + buf_len))
195    {
196       block->bufp = (eblock->bufp - eblock->buf) + block->buf;
197
198    } else {
199       block->bufp = NULL;
200    }
201    return block;
202 }
203
204 /*
205  * Flush block to disk
206  */
207 bool DEVICE::flush_block(DCR *dcr)
208 {
209    if (!is_block_empty(dcr->block)) {
210       Dmsg0(dbglvl, "=== wpath 53 flush_ameta\n");
211       Dmsg4(190, "Call flush_ameta_block BlockAddr=%lld nbytes=%d adata=%d block=%x\n",
212          dcr->block->BlockAddr, dcr->block->binbuf, dcr->adata_block->adata, dcr->adata_block);
213       dump_block(dcr->dev, dcr->block, "Flush_ameta_block");
214       if (dcr->jcr->is_canceled() || !dcr->write_block_to_device()) {
215          Dmsg0(dbglvl, "=== wpath 54 flush_ameta\n");
216          Dmsg0(190, "Failed to write ameta block to device, return false.\n");
217          return false;
218       }
219       empty_block(dcr->block);
220    }
221    return true;
222 }
223
224
225 /*
226  * Only the first block checksum error was reported.
227  *   If there are more, report it now.
228  */
229 void print_block_read_errors(JCR *jcr, DEV_BLOCK *block)
230 {
231    if (block->read_errors > 1) {
232       Jmsg(jcr, M_ERROR, 0, _("%d block read errors not printed.\n"),
233          block->read_errors);
234    }
235 }
236
237 void DEVICE::free_dcr_blocks(DCR *dcr)
238 {
239    if (dcr->block == dcr->ameta_block) {
240       dcr->ameta_block = NULL;      /* do not free twice */
241    }
242    free_block(dcr->block);
243    dcr->block = NULL;
244    free_block(dcr->ameta_block);
245    dcr->ameta_block = NULL;
246 }
247
248 /*
249  * Free block
250  */
251 void free_block(DEV_BLOCK *block)
252 {
253    if (block) {
254       Dmsg1(999, "free_block buffer=%p\n", block->buf);
255       if (block->buf) {
256          free_memory(block->buf);
257       }
258       if (block->rechdr_queue) {
259          free_memory(block->rechdr_queue);
260       }
261       Dmsg1(999, "=== free_block block %p\n", block);
262       free_memory((POOLMEM *)block);
263    }
264 }
265
266 bool is_block_empty(DEV_BLOCK *block)
267 {
268    if (block->adata) {
269       Dmsg1(200, "=== adata=1 binbuf=%d\n", block->binbuf);
270       return block->binbuf <= 0;
271    } else {
272       Dmsg1(200, "=== adata=0 binbuf=%d\n", block->binbuf-WRITE_BLKHDR_LENGTH);
273       return block->binbuf <= WRITE_BLKHDR_LENGTH;
274    }
275 }
276
277 /* Empty the block -- for writing */
278 void empty_block(DEV_BLOCK *block)
279 {
280    if (block->adata) {
281       block->binbuf = 0;
282    } else {
283       block->binbuf = WRITE_BLKHDR_LENGTH;
284    }
285    Dmsg3(250, "empty_block: adata=%d len=%d set binbuf=%d\n",
286          block->adata, block->buf_len, block->binbuf);
287    block->bufp = block->buf + block->binbuf;
288    block->read_len = 0;
289    block->write_failed = false;
290    block->block_read = false;
291    block->needs_write = false;
292    block->FirstIndex = block->LastIndex = 0;
293    block->RecNum = 0;
294    block->BlockAddr = 0;
295 }
296
297 /*
298  * Create block header just before write. The space
299  * in the buffer should have already been reserved by
300  * init_block.
301  */
302 uint32_t ser_block_header(DEV_BLOCK *block, bool do_checksum)
303 {
304    ser_declare;
305    uint32_t block_len = block->binbuf;
306
307    block->CheckSum = 0;
308    if (block->adata) {
309       /* Checksum whole block */
310       if (do_checksum) {
311          block->CheckSum = bcrc32((uint8_t *)block->buf, block_len);
312       }
313    } else {
314       Dmsg1(160, "block_header: block_len=%d\n", block_len);
315       ser_begin(block->buf, BLKHDR2_LENGTH);
316       ser_uint32(block->CheckSum);
317       ser_uint32(block_len);
318       ser_uint32(block->BlockNumber);
319       ser_bytes(WRITE_BLKHDR_ID, BLKHDR_ID_LENGTH);
320       if (BLOCK_VER >= 2) {
321          ser_uint32(block->VolSessionId);
322          ser_uint32(block->VolSessionTime);
323       }
324
325       /* Checksum whole block except for the checksum */
326       if (do_checksum) {
327          block->CheckSum = bcrc32((uint8_t *)block->buf+BLKHDR_CS_LENGTH,
328                        block_len-BLKHDR_CS_LENGTH);
329       }
330       Dmsg2(160, "ser_block_header: adata=%d checksum=%x\n", block->adata, block->CheckSum);
331       ser_begin(block->buf, BLKHDR2_LENGTH);
332       ser_uint32(block->CheckSum);    /* now add checksum to block header */
333    }
334    return block->CheckSum;
335 }
336
337 /*
338  * Unserialize the block header for reading block.
339  *  This includes setting all the buffer pointers correctly.
340  *
341  *  Returns: false on failure (not a block)
342  *           true  on success
343  */
344 bool unser_block_header(DCR *dcr, DEVICE *dev, DEV_BLOCK *block)
345 {
346    ser_declare;
347    char Id[BLKHDR_ID_LENGTH+1];
348    uint32_t BlockCheckSum;
349    uint32_t block_len;
350    uint32_t block_end;
351    uint32_t BlockNumber;
352    JCR *jcr = dcr->jcr;
353    int bhl;
354
355    if (block->adata) {
356       /* Checksum the whole block */
357       if (block->block_len <= block->read_len && dev->do_checksum()) {
358          BlockCheckSum = bcrc32((uint8_t *)block->buf, block->block_len);
359          if (BlockCheckSum != block->CheckSum) {
360             dev->dev_errno = EIO;
361             Mmsg5(dev->errmsg, _("Volume data error at %lld!\n"
362                "Adata block checksum mismatch in block=%u len=%d: calc=%x blk=%x\n"),
363                block->BlockAddr, block->BlockNumber,
364                block->block_len, BlockCheckSum, block->CheckSum);
365             if (block->read_errors == 0 || verbose >= 2) {
366                Jmsg(jcr, M_ERROR, 0, "%s", dev->errmsg);
367                dump_block(dev, block, "with checksum error");
368             }
369             block->read_errors++;
370             if (!forge_on) {
371                return false;
372             }
373          }
374       }
375       return true;
376    }
377
378    if (block->no_header) {
379       return true;
380    }
381    unser_begin(block->buf, BLKHDR_LENGTH);
382    unser_uint32(block->CheckSum);
383    unser_uint32(block_len);
384    unser_uint32(BlockNumber);
385    unser_bytes(Id, BLKHDR_ID_LENGTH);
386    ASSERT(unser_length(block->buf) == BLKHDR1_LENGTH);
387    Id[BLKHDR_ID_LENGTH] = 0;
388
389    if (Id[3] == '1') {
390       bhl = BLKHDR1_LENGTH;
391       block->BlockVer = 1;
392       block->bufp = block->buf + bhl;
393       //Dmsg3(100, "Block=%p buf=%p bufp=%p\n", block, block->buf, block->bufp);
394       if (strncmp(Id, BLKHDR1_ID, BLKHDR_ID_LENGTH) != 0) {
395          dev->dev_errno = EIO;
396          Mmsg4(dev->errmsg, _("Volume data error at %u:%u! Wanted ID: \"%s\", got \"%s\". Buffer discarded.\n"),
397             dev->file, dev->block_num, BLKHDR1_ID, Id);
398          if (block->read_errors == 0 || verbose >= 2) {
399             Jmsg(jcr, M_ERROR, 0, "%s", dev->errmsg);
400          }
401          block->read_errors++;
402          return false;
403       }
404    } else if (Id[3] == '2') {
405       unser_uint32(block->VolSessionId);
406       unser_uint32(block->VolSessionTime);
407       bhl = BLKHDR2_LENGTH;
408       block->BlockVer = 2;
409       block->bufp = block->buf + bhl;
410       //Dmsg5(100, "Read-blkhdr Block=%p adata=%d buf=%p bufp=%p off=%d\n", block, block->adata,
411       //   block->buf, block->bufp, block->bufp-block->buf);
412       if (strncmp(Id, BLKHDR2_ID, BLKHDR_ID_LENGTH) != 0) {
413          dev->dev_errno = EIO;
414          Mmsg4(dev->errmsg, _("Volume data error at %u:%u! Wanted ID: \"%s\", got \"%s\". Buffer discarded.\n"),
415             dev->file, dev->block_num, BLKHDR2_ID, Id);
416          if (block->read_errors == 0 || verbose >= 2) {
417             Jmsg(jcr, M_ERROR, 0, "%s", dev->errmsg);
418          }
419          block->read_errors++;
420          return false;
421       }
422    } else {
423       dev->dev_errno = EIO;
424       Mmsg4(dev->errmsg, _("Volume data error at %u:%u! Wanted ID: \"%s\", got \"%s\". Buffer discarded.\n"),
425           dev->file, dev->block_num, BLKHDR2_ID, Id);
426       Dmsg1(50, "%s", dev->errmsg);
427       if (block->read_errors == 0 || verbose >= 2) {
428          Jmsg(jcr, M_FATAL, 0, "%s", dev->errmsg);
429       }
430       block->read_errors++;
431       unser_uint32(block->VolSessionId);
432       unser_uint32(block->VolSessionTime);
433       return false;
434    }
435
436    /* Sanity check */
437    if (block_len > MAX_BLOCK_SIZE) {
438       dev->dev_errno = EIO;
439       Mmsg3(dev->errmsg,  _("Volume data error at %u:%u! Block length %u is insane (too large), probably due to a bad archive.\n"),
440          dev->file, dev->block_num, block_len);
441       if (block->read_errors == 0 || verbose >= 2) {
442          Jmsg(jcr, M_ERROR, 0, "%s", dev->errmsg);
443       }
444       block->read_errors++;
445       return false;
446    }
447
448    Dmsg1(390, "unser_block_header block_len=%d\n", block_len);
449    /* Find end of block or end of buffer whichever is smaller */
450    if (block_len > block->read_len) {
451       block_end = block->read_len;
452    } else {
453       block_end = block_len;
454    }
455    block->binbuf = block_end - bhl;
456    Dmsg3(200, "set block=%p adata=%d binbuf=%d\n", block, block->adata, block->binbuf);
457    block->block_len = block_len;
458    block->BlockNumber = BlockNumber;
459    Dmsg3(390, "Read binbuf = %d %d block_len=%d\n", block->binbuf,
460       bhl, block_len);
461    if (block_len <= block->read_len && dev->do_checksum()) {
462       BlockCheckSum = bcrc32((uint8_t *)block->buf+BLKHDR_CS_LENGTH,
463                                  block_len-BLKHDR_CS_LENGTH);
464
465       if (BlockCheckSum != block->CheckSum) {
466          dev->dev_errno = EIO;
467          Mmsg6(dev->errmsg, _("Volume data error at %u:%u!\n"
468             "Block checksum mismatch in block=%u len=%d: calc=%x blk=%x\n"),
469             dev->file, dev->block_num, (unsigned)BlockNumber,
470             block_len, BlockCheckSum, block->CheckSum);
471          if (block->read_errors == 0 || verbose >= 2) {
472             Jmsg(jcr, M_ERROR, 0, "%s", dev->errmsg);
473             dump_block(dev, block, "with checksum error");
474          }
475          block->read_errors++;
476          if (!forge_on) {
477             return false;
478          }
479       }
480    }
481    return true;
482 }
483
484 /*
485  * Calculate how many bytes to write and then clear to end
486  *  of block.
487  */
488 uint32_t get_len_and_clear_block(DEV_BLOCK *block, DEVICE *dev, uint32_t &pad)
489 {
490    uint32_t wlen;
491    /*
492     * Clear to the end of the buffer if it is not full,
493     *  and on tape devices, apply min and fixed blocking.
494     */
495    wlen = block->binbuf;
496    if (wlen != block->buf_len) {
497       Dmsg2(250, "binbuf=%d buf_len=%d\n", block->binbuf, block->buf_len);
498
499       /* Adjust write size to min/max for tapes and aligned only */
500       if (dev->is_tape() || block->adata) {
501          /* check for fixed block size */
502          if (dev->min_block_size == dev->max_block_size) {
503             wlen = block->buf_len;    /* fixed block size already rounded */
504          /* Check for min block size */
505          } else if (wlen < dev->min_block_size) {
506             wlen =  ((dev->min_block_size + TAPE_BSIZE - 1) / TAPE_BSIZE) * TAPE_BSIZE;
507          /* Ensure size is rounded */
508          } else {
509             wlen = ((wlen + TAPE_BSIZE - 1) / TAPE_BSIZE) * TAPE_BSIZE;
510          }
511       }
512       if (block->adata && dev->padding_size > 0) {
513          /* Write to next aligned boundry */
514          wlen = ((wlen + dev->padding_size - 1) / dev->padding_size) * dev->padding_size;
515       }
516       ASSERT(wlen <= block->buf_len);
517       /* Clear from end of data to end of block */
518       if (wlen-block->binbuf > 0) {
519          memset(block->bufp, 0, wlen-block->binbuf); /* clear garbage */
520       }
521       pad = wlen - block->binbuf;        /* padding or zeros written */
522       Dmsg5(150, "Zero end blk: adata=%d cleared=%d buf_len=%d wlen=%d binbuf=%d\n",
523          block->adata, pad, block->buf_len, wlen, block->binbuf);
524    } else {
525       pad = 0;
526    }
527
528    return wlen;              /* bytes to write */
529 }
530
531 /*
532  * Determine if user defined volume size has been
533  *  reached, and if so, return true, otherwise
534  *  return false.
535  */
536 bool is_user_volume_size_reached(DCR *dcr, bool quiet)
537 {
538    bool hit_max1, hit_max2;
539    uint64_t size, max_size;
540    DEVICE *dev = dcr->ameta_dev;
541    char ed1[50];
542    bool rtn = false;
543
544    Enter(dbglvl);
545    if (dev->is_aligned()) {
546       /* Note, we reserve space for one ameta and one adata block */
547       size = dev->VolCatInfo.VolCatBytes + dcr->ameta_block->buf_len +
548          dcr->adata_block->buf_len;
549    } else {
550       size = dev->VolCatInfo.VolCatBytes + dcr->ameta_block->binbuf;
551    }
552    /* Limit maximum Volume size to value specified by user */
553    hit_max1 = (dev->max_volume_size > 0) && (size >= dev->max_volume_size);
554    hit_max2 = (dev->VolCatInfo.VolCatMaxBytes > 0) &&
555        (size >= dev->VolCatInfo.VolCatMaxBytes);
556    if (hit_max1) {
557       max_size = dev->max_volume_size;
558    } else {
559       max_size = dev->VolCatInfo.VolCatMaxBytes;
560    }
561    if (hit_max1 || hit_max2) {
562       if (!quiet) {
563          Jmsg(dcr->jcr, M_INFO, 0, _("User defined maximum volume size %s will be exceeded on device %s.\n"
564             "   Marking Volume \"%s\" as Full.\n"),
565             edit_uint64_with_commas(max_size, ed1),  dev->print_name(),
566             dev->getVolCatName());
567       }
568       Dmsg4(100, "Maximum volume size %s exceeded Vol=%s device=%s.\n"
569          "Marking Volume \"%s\" as Full.\n",
570          edit_uint64_with_commas(max_size, ed1), dev->getVolCatName(),
571          dev->print_name(), dev->getVolCatName());
572       rtn = true;
573    }
574    Dmsg1(dbglvl, "Return from is_user_volume_size_reached=%d\n", rtn);
575    Leave(dbglvl);
576    return rtn;
577 }
578
579
580 void reread_last_block(DCR *dcr)
581 {
582 #define CHECK_LAST_BLOCK
583 #ifdef  CHECK_LAST_BLOCK
584    bool ok = true;
585    DEVICE *dev = dcr->dev;
586    JCR *jcr = dcr->jcr;
587    DEV_BLOCK *ameta_block = dcr->ameta_block;
588    DEV_BLOCK *adata_block = dcr->adata_block;
589    DEV_BLOCK *block = dcr->block;
590    /*
591     * If the device is a tape and it supports backspace record,
592     *   we backspace over one or two eof marks depending on
593     *   how many we just wrote, then over the last record,
594     *   then re-read it and verify that the block number is
595     *   correct.
596     */
597    if (dev->is_tape() && dev->has_cap(CAP_BSR)) {
598       /* Now back up over what we wrote and read the last block */
599       if (!dev->bsf(1)) {
600          berrno be;
601          ok = false;
602          Jmsg(jcr, M_ERROR, 0, _("Backspace file at EOT failed. ERR=%s\n"),
603               be.bstrerror(dev->dev_errno));
604       }
605       if (ok && dev->has_cap(CAP_TWOEOF) && !dev->bsf(1)) {
606          berrno be;
607          ok = false;
608          Jmsg(jcr, M_ERROR, 0, _("Backspace file at EOT failed. ERR=%s\n"),
609               be.bstrerror(dev->dev_errno));
610       }
611       /* Backspace over record */
612       if (ok && !dev->bsr(1)) {
613          berrno be;
614          ok = false;
615          Jmsg(jcr, M_ERROR, 0, _("Backspace record at EOT failed. ERR=%s\n"),
616               be.bstrerror(dev->dev_errno));
617          /*
618           *  On FreeBSD systems, if the user got here, it is likely that his/her
619           *    tape drive is "frozen".  The correct thing to do is a
620           *    rewind(), but if we do that, higher levels in cleaning up, will
621           *    most likely write the EOS record over the beginning of the
622           *    tape.  The rewind *is* done later in mount.c when another
623           *    tape is requested. Note, the clrerror() call in bsr()
624           *    calls ioctl(MTCERRSTAT), which *should* fix the problem.
625           */
626       }
627       if (ok) {
628          dev->new_dcr_blocks(dcr);
629          /* Note, this can destroy dev->errmsg */
630          if (!dcr->read_block_from_dev(NO_BLOCK_NUMBER_CHECK)) {
631             Jmsg(jcr, M_ERROR, 0, _("Re-read last block at EOT failed. ERR=%s"),
632                  dev->errmsg);
633          } else {
634             /*
635              * If we wrote block and the block numbers don't agree
636              *  we have a possible problem.
637              */
638             if (dcr->block->BlockNumber != dev->LastBlock) {
639                 if (dev->LastBlock > (dcr->block->BlockNumber + 1)) {
640                    Jmsg(jcr, M_FATAL, 0, _(
641 "Re-read of last block: block numbers differ by more than one.\n"
642 "Probable tape misconfiguration and data loss. Read block=%u Want block=%u.\n"),
643                        dcr->block->BlockNumber, dev->LastBlock);
644                  } else {
645                    Jmsg(jcr, M_ERROR, 0, _(
646 "Re-read of last block OK, but block numbers differ. Read block=%u Want block=%u.\n"),
647                        dcr->block->BlockNumber, dev->LastBlock);
648                  }
649             } else {
650                Jmsg(jcr, M_INFO, 0, _("Re-read of last block succeeded.\n"));
651             }
652          }
653          dev->free_dcr_blocks(dcr);
654          dcr->ameta_block = ameta_block;
655          dcr->block = block;
656          dcr->adata_block = adata_block;
657       }
658    }
659 #endif
660 }
661
662 /*
663  * If this routine is called, we do our bookkeeping and
664  *   then assure that the volume will not be written any
665  *   more.
666  */
667 bool terminate_writing_volume(DCR *dcr)
668 {
669    DEVICE *dev = dcr->dev;
670    bool ok = true;
671    bool was_adata = false;
672
673    Enter(dbglvl);
674
675    if (dev->is_ateot()) {
676       return ok;          /* already been here return now */
677    }
678
679    /* Work with ameta device */
680    if (dev->adata) {
681       dev->set_ateot();       /* no more writing this Volume */
682       dcr->adata_block->write_failed = true;
683       dcr->set_ameta();
684       dev = dcr->ameta_dev;
685       was_adata = true;
686    }
687
688    /* Create a JobMedia record to indicated end of medium */
689    dev->VolCatInfo.VolCatFiles = dev->get_file();
690    dev->VolCatInfo.VolLastPartBytes = dev->part_size;
691    dev->VolCatInfo.VolCatParts = dev->part;
692    if (!dir_create_jobmedia_record(dcr)) {
693       Dmsg0(50, "Error from create JobMedia\n");
694       dev->dev_errno = EIO;
695         Mmsg2(dev->errmsg, _("Could not create JobMedia record for Volume=\"%s\" Job=%s\n"),
696             dev->getVolCatName(), dcr->jcr->Job);
697        Jmsg(dcr->jcr, M_FATAL, 0, "%s", dev->errmsg);
698        ok = false;
699    }
700    flush_jobmedia_queue(dcr->jcr);
701    bstrncpy(dev->LoadedVolName, dev->VolCatInfo.VolCatName, sizeof(dev->LoadedVolName));
702    dcr->block->write_failed = true;
703    if (dev->can_append() && !dev->weof(dcr, 1)) {     /* end the tape */
704       dev->VolCatInfo.VolCatErrors++;
705       Jmsg(dcr->jcr, M_ERROR, 0, _("Error writing final EOF to tape. Volume %s may not be readable.\n"
706            "%s"), dev->VolCatInfo.VolCatName, dev->errmsg);
707       ok = false;
708       Dmsg0(50, "Error writing final EOF to volume.\n");
709    }
710    if (ok) {
711       ok = dev->end_of_volume(dcr);
712    }
713
714    Dmsg3(100, "Set VolCatStatus Full adata=%d size=%lld vol=%s\n", dev->adata,
715       dev->VolCatInfo.VolCatBytes, dev->VolCatInfo.VolCatName);
716
717    /* If still in append mode mark volume Full */
718    if (bstrcmp(dev->VolCatInfo.VolCatStatus, "Append")) {
719       dev->setVolCatStatus("Full");
720    }
721
722    if (!dir_update_volume_info(dcr, false, true)) {
723       Mmsg(dev->errmsg, _("Error sending Volume info to Director.\n"));
724       ok = false;
725       Dmsg0(50, "Error updating volume info.\n");
726    }
727    Dmsg2(150, "dir_update_volume_info vol=%s to terminate writing -- %s\n",
728       dev->getVolCatName(), ok?"OK":"ERROR");
729
730    dev->notify_newvol_in_attached_dcrs(NULL);
731
732    /* Set new file/block parameters for current dcr */
733    set_new_file_parameters(dcr);
734
735    if (ok && dev->has_cap(CAP_TWOEOF) && dev->can_append() && !dev->weof(dcr, 1)) {  /* end the tape */
736       dev->VolCatInfo.VolCatErrors++;
737       /* This may not be fatal since we already wrote an EOF */
738       if (dev->errmsg[0]) {
739          Jmsg(dcr->jcr, M_ERROR, 0, "%s", dev->errmsg);
740       }
741       Dmsg0(50, "Writing second EOF failed.\n");
742    }
743
744    dev->set_ateot();                  /* no more writing this tape */
745    Dmsg2(150, "Leave terminate_writing_volume=%s -- %s\n",
746       dev->getVolCatName(), ok?"OK":"ERROR");
747    if (was_adata) {
748       dcr->set_adata();
749    }
750    Leave(dbglvl);
751    return ok;
752 }
753
754 /*
755  * If a new volume has been mounted since our last write
756  *   Create a JobMedia record for the previous volume written,
757  *   and set new parameters to write this volume
758  * The same applies for if we are in a new file.
759  */
760 bool check_for_newvol_or_newfile(DCR *dcr)
761 {
762    JCR *jcr = dcr->jcr;
763
764    if (dcr->NewVol || dcr->NewFile) {
765       if (job_canceled(jcr)) {
766          Dmsg0(100, "Canceled\n");
767          return false;
768       }
769       /* If we wrote on Volume create a last jobmedia record for this job */
770       if (!dcr->VolFirstIndex) {
771          Dmsg7(100, "Skip JobMedia Vol=%s wrote=%d MediaId=%lld FI=%lu LI=%lu StartAddr=%lld EndAddr=%lld\n",
772             dcr->VolumeName, dcr->WroteVol, dcr->VolMediaId,
773             dcr->VolFirstIndex, dcr->VolLastIndex, dcr->StartAddr, dcr->EndAddr);
774       }
775       if (dcr->VolFirstIndex && !dir_create_jobmedia_record(dcr)) {
776          dcr->dev->dev_errno = EIO;
777          Jmsg2(jcr, M_FATAL, 0, _("Could not create JobMedia record for Volume=\"%s\" Job=%s\n"),
778             dcr->getVolCatName(), jcr->Job);
779          set_new_volume_parameters(dcr);
780          Dmsg0(100, "cannot create media record\n");
781          return false;
782       }
783       if (dcr->NewVol) {
784          Dmsg0(250, "Process NewVol\n");
785          flush_jobmedia_queue(jcr);
786          /* Note, setting a new volume also handles any pending new file */
787          set_new_volume_parameters(dcr);
788       } else {
789          set_new_file_parameters(dcr);
790       }
791    }
792    return true;
793 }
794
795 /*
796  * Do bookkeeping when a new file is created on a Volume. This is
797  *  also done for disk files to generate the jobmedia records for
798  *  quick seeking.
799  */
800 bool do_new_file_bookkeeping(DCR *dcr)
801 {
802    DEVICE *dev = dcr->dev;
803    JCR *jcr = dcr->jcr;
804
805    /* Create a JobMedia record so restore can seek */
806    if (!dir_create_jobmedia_record(dcr)) {
807       Dmsg0(40, "Error from create_job_media.\n");
808       dev->dev_errno = EIO;
809       Jmsg2(jcr, M_FATAL, 0, _("Could not create JobMedia record for Volume=\"%s\" Job=%s\n"),
810            dcr->getVolCatName(), jcr->Job);
811       Dmsg0(40, "Call terminate_writing_volume\n");
812       terminate_writing_volume(dcr);
813       dev->dev_errno = EIO;
814       return false;
815    }
816    dev->VolCatInfo.VolCatFiles = dev->get_file();
817    dev->VolCatInfo.VolLastPartBytes = dev->part_size;
818    dev->VolCatInfo.VolCatParts = dev->part;
819    if (!dir_update_volume_info(dcr, false, false)) {
820       Dmsg0(50, "Error from update_vol_info.\n");
821       Dmsg0(40, "Call terminate_writing_volume\n");
822       terminate_writing_volume(dcr);
823       dev->dev_errno = EIO;
824       return false;
825    }
826    Dmsg0(100, "dir_update_volume_info max file size -- OK\n");
827
828    dev->notify_newfile_in_attached_dcrs();
829
830    /* Set new file/block parameters for current dcr */
831    set_new_file_parameters(dcr);
832    return true;
833 }