]> git.sur5r.net Git - bacula/bacula/blob - bacula/src/stored/block.c
Fix compiler warning in previous patch
[bacula/bacula] / bacula / src / stored / block.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.c -- tape block handling functions
22  *
23  *              Kern Sibbald, March MMI
24  *                 added BB02 format October MMII
25  */
26
27
28 #include "bacula.h"
29 #include "stored.h"
30
31 #ifdef DEBUG_BLOCK_CHECKSUM
32 static const bool debug_block_checksum = true;
33 #else
34 static const bool debug_block_checksum = false;
35 #endif
36
37 static int debug_io_error = 0;    /* # blocks to write before creating I/O error */
38
39 #ifdef NO_TAPE_WRITE_TEST
40 static const bool no_tape_write_test = true;
41 #else
42 static const bool no_tape_write_test = false;
43 #endif
44
45
46 uint32_t get_len_and_clear_block(DEV_BLOCK *block, DEVICE *dev, uint32_t &pad);
47 uint32_t ser_block_header(DEV_BLOCK *block, bool do_checksum);
48 bool unser_block_header(DCR *dcr, DEVICE *dev, DEV_BLOCK *block);
49
50 /*
51  * Write a block to the device, with locking and unlocking
52  *
53  * Returns: true  on success
54  *        : false on failure
55  *
56  */
57 bool DCR::write_block_to_device(bool final)
58 {
59    bool ok = true;
60    DCR *dcr = this;
61
62    if (dcr->spooling) {
63       Dmsg0(250, "Write to spool\n");
64       ok = write_block_to_spool_file(dcr);
65       return ok;
66    }
67
68    if (!is_dev_locked()) {        /* device already locked? */
69       /* note, do not change this to dcr->rLock */
70       dev->rLock(false);          /* no, lock it */
71    }
72
73    if (!check_for_newvol_or_newfile(dcr)) {
74       ok = false;
75       goto bail_out;   /* fatal error */
76    }
77
78    Dmsg1(500, "Write block to dev=%p\n", dcr->dev);
79    if (!write_block_to_dev()) {
80       Dmsg2(40, "*** Failed write_block_to_dev adata=%d block=%p\n",
81          block->adata, block);
82       if (job_canceled(jcr) || jcr->getJobType() == JT_SYSTEM) {
83          ok = false;
84          Dmsg2(40, "cancel=%d or SYSTEM=%d\n", job_canceled(jcr),
85             jcr->getJobType() == JT_SYSTEM);
86       } else {
87          bool was_adata = false;
88          if (was_adata) {
89             dcr->set_ameta();
90             was_adata = true;
91          }
92          /* Flush any existing JobMedia info */
93          if (!(ok = dir_create_jobmedia_record(dcr))) {
94             Jmsg(jcr, M_FATAL, 0, _("Error writing JobMedia record to catalog.\n"));
95          } else {
96             Dmsg1(40, "Calling fixup_device was_adata=%d...\n", was_adata);
97             ok = fixup_device_block_write_error(dcr);
98          }
99          if (was_adata) {
100             dcr->set_adata();
101          }
102       }
103    }
104    if (ok && final && !dir_create_jobmedia_record(dcr)) {
105       Jmsg(jcr, M_FATAL, 0, _("Error writing final JobMedia record to catalog.\n"));
106    }
107
108 bail_out:
109    if (!dcr->is_dev_locked()) {        /* did we lock dev above? */
110       /* note, do not change this to dcr->dunlock */
111       dev->Unlock();                  /* unlock it now */
112    }
113    return ok;
114 }
115
116 /*
117  * Write a block to the device
118  *
119  *  Returns: true  on success or EOT
120  *           false on hard error
121  */
122 bool DCR::write_block_to_dev()
123 {
124    ssize_t stat = 0;
125    uint32_t wlen;                     /* length to write */
126    bool ok = true;
127    DCR *dcr = this;
128    uint32_t checksum;
129    uint32_t pad;                      /* padding or zeros written */
130    boffset_t pos;
131    char ed1[50];
132
133    if (no_tape_write_test) {
134       empty_block(block);
135       return true;
136    }
137    if (job_canceled(jcr)) {
138       return false;
139    }
140    if (!dev->enabled) {
141       Jmsg1(jcr, M_FATAL, 0,  _("Cannot write block. Device is disabled. dev=%s\n"), dev->print_name());
142       return false;
143    }
144
145    ASSERT2(block->adata == dev->adata, "Block and dev adata not same");
146    Dmsg4(200, "fd=%d adata=%d bufp-buf=%d binbuf=%d\n", dev->fd(), block->adata,
147       block->bufp-block->buf, block->binbuf);
148    ASSERT2(block->binbuf == ((uint32_t)(block->bufp - block->buf)), "binbuf badly set");
149
150    if (is_block_empty(block)) {  /* Does block have data in it? */
151       Dmsg1(50, "return write_block_to_dev no adata=%d data to write\n", block->adata);
152       return true;
153    }
154
155    if (dev->at_weot()) {
156       Dmsg1(50, "==== FATAL: At EOM with ST_WEOT. adata=%d.\n", dev->adata);
157       dev->dev_errno = ENOSPC;
158       Jmsg1(jcr, M_FATAL, 0,  _("Cannot write block. Device at EOM. dev=%s\n"), dev->print_name());
159       return false;
160    }
161    if (!dev->can_append()) {
162       dev->dev_errno = EIO;
163       Jmsg1(jcr, M_FATAL, 0, _("Attempt to write on read-only Volume. dev=%s\n"), dev->print_name());
164       Dmsg1(50, "Attempt to write on read-only Volume. dev=%s\n", dev->print_name());
165       return false;
166    }
167
168    if (!dev->is_open()) {
169       Jmsg1(jcr, M_FATAL, 0, _("Attempt to write on closed device=%s\n"), dev->print_name());
170       Dmsg1(50, "Attempt to write on closed device=%s\n", dev->print_name());
171       return false;
172    }
173
174    wlen = get_len_and_clear_block(block, dev, pad);
175    block->block_len = wlen;
176    dev->updateVolCatPadding(pad);
177
178    checksum = ser_block_header(block, dev->do_checksum());
179
180    if (!dev->do_size_checks(dcr, block)) {
181       Dmsg0(50, "Size check triggered.  Cannot write block.\n");
182       return false;
183    }
184
185    dev->updateVolCatWrites(1);
186
187    dump_block(dev, block, "before write");
188
189 #ifdef DEBUG_BLOCK_ZEROING
190    uint32_t *bp = (uint32_t *)block->buf;
191    if (bp[0] == 0 && bp[1] == 0 && bp[2] == 0 && block->buf[12] == 0) {
192       Jmsg0(jcr, M_ABORT, 0, _("Write block header zeroed.\n"));
193    }
194 #endif
195
196    /*
197     * If Adata block, we must seek to the correct address
198     */
199    if (block->adata) {
200       ASSERT(dcr->dev->adata);
201       uint64_t cur = dev->lseek(dcr, 0, SEEK_CUR);
202       /* If we are going to create a hole, record it */
203       if (block->BlockAddr != cur) {
204          dev->lseek(dcr, block->BlockAddr, SEEK_SET);
205          Dmsg4(100, "Adata seek BlockAddr from %lld to %lld = %lld bytes adata_addr=%lld\n",
206             cur, block->BlockAddr, block->BlockAddr - cur, dev->adata_addr);
207          /* Insanity check */
208          if (block->BlockAddr > cur) {
209             dev->updateVolCatHoleBytes(block->BlockAddr - cur);
210          } else if (block->BlockAddr < cur) {
211            Pmsg5(000, "Vol=%s cur=%lld BlockAddr=%lld adata=%d block=%p\n",
212               dev->getVolCatName(), cur, block->BlockAddr, block->adata, block);
213            Jmsg3(jcr, M_FATAL, 0, "Bad seek on adata Vol=%s BlockAddr=%lld DiskAddr=%lld. Multiple simultaneous Jobs?\n",
214               dev->getVolCatName(), block->BlockAddr, cur);
215            //Pmsg2(000, "HoleBytes would go negative cur=%lld blkaddr=%lld\n", cur, block->BlockAddr);
216          }
217       }
218    }
219
220    /*
221     * Do write here, make a somewhat feeble attempt to recover from
222     *  I/O errors, or from the OS telling us it is busy.
223     */
224    int retry = 0;
225    errno = 0;
226    stat = 0;
227    /* ***FIXME**** remove next line debug */
228    pos =  dev->lseek(dcr, 0, SEEK_CUR);
229    do {
230       if (retry > 0 && stat == -1 && errno == EBUSY) {
231          berrno be;
232          Dmsg4(100, "===== write retry=%d stat=%d errno=%d: ERR=%s\n",
233                retry, stat, errno, be.bstrerror());
234          bmicrosleep(5, 0);    /* pause a bit if busy or lots of errors */
235          dev->clrerror(-1);
236       }
237       stat = dev->write(block->buf, (size_t)wlen);
238       Dmsg4(100, "%s write() BlockAddr=%lld wlen=%d Vol=%s wlen=%d\n",
239          block->adata?"Adata":"Ameta", block->BlockAddr, wlen,
240          dev->VolHdr.VolumeName);
241    } while (stat == -1 && (errno == EBUSY || errno == EIO) && retry++ < 3);
242
243    /* ***FIXME*** remove 2 lines debug */
244    Dmsg2(100, "Wrote %d bytes at %s\n", wlen, dev->print_addr(ed1, sizeof(ed1), pos));
245    dump_block(dev, block, "After write");
246
247    if (debug_block_checksum) {
248       uint32_t achecksum = ser_block_header(block, dev->do_checksum());
249       if (checksum != achecksum) {
250          Jmsg2(jcr, M_ERROR, 0, _("Block checksum changed during write: before=%u after=%u\n"),
251             checksum, achecksum);
252          dump_block(dev, block, "with checksum error");
253       }
254    }
255
256 #ifdef DEBUG_BLOCK_ZEROING
257    if (bp[0] == 0 && bp[1] == 0 && bp[2] == 0 && block->buf[12] == 0) {
258       Jmsg0(jcr, M_ABORT, 0, _("Write block header zeroed.\n"));
259    }
260 #endif
261
262    if (debug_io_error) {
263       debug_io_error--;
264       if (debug_io_error == 1) {  /* trigger error */
265          stat = -1;
266          dev->dev_errno = EIO;
267          errno = EIO;
268          debug_io_error = 0;      /* turn off trigger */
269       }
270    }
271
272    if (stat != (ssize_t)wlen) {
273       /* Some devices simply report EIO when the volume is full.
274        * With a little more thought we may be able to check
275        * capacity and distinguish real errors and EOT
276        * conditions.  In any case, we probably want to
277        * simulate an End of Medium.
278        */
279       if (stat == -1) {
280          berrno be;
281          dev->clrerror(-1);                 /* saves errno in dev->dev_errno */
282          if (dev->dev_errno == 0) {
283             dev->dev_errno = ENOSPC;        /* out of space */
284          }
285          if (dev->dev_errno != ENOSPC) {
286             int etype = M_ERROR;
287             if (block->adata) {
288                etype = M_FATAL;
289             }
290             dev->VolCatInfo.VolCatErrors++;
291             Jmsg4(jcr, etype, 0, _("Write error at %s on device %s Vol=%s. ERR=%s.\n"),
292                dev->print_addr(ed1, sizeof(ed1)), dev->print_name(),
293                dev->getVolCatName(), be.bstrerror());
294             if (dev->get_tape_alerts(this)) {
295                dev->show_tape_alerts(this, list_long, list_last, alert_callback);
296             }
297          }
298       } else {
299         dev->dev_errno = ENOSPC;            /* out of space */
300       }
301       if (dev->dev_errno == ENOSPC) {
302          dev->update_freespace();
303          if (dev->is_freespace_ok() && dev->free_space < dev->min_free_space) {
304             dev->set_nospace();
305             Jmsg(jcr, M_FATAL, 0, _("Out of freespace caused End of Volume \"%s\" at %s on device %s. Write of %u bytes got %d.\n"),
306                dev->getVolCatName(),
307                dev->print_addr(ed1, sizeof(ed1)), dev->print_name(), wlen, stat);
308          } else {
309             dev->clear_nospace();
310             Jmsg(jcr, M_INFO, 0, _("End of Volume \"%s\" at %s on device %s. Write of %u bytes got %d.\n"),
311                dev->getVolCatName(),
312                dev->print_addr(ed1, sizeof(ed1)), dev->print_name(), wlen, stat);
313          }
314       }
315       if (chk_dbglvl(100)) {
316          berrno be;
317          Dmsg7(90, "==== Write error. fd=%d size=%u rtn=%d dev_blk=%d blk_blk=%d errno=%d: ERR=%s\n",
318             dev->fd(), wlen, stat, dev->block_num, block->BlockNumber,
319             dev->dev_errno, be.bstrerror(dev->dev_errno));
320       }
321
322       Dmsg0(40, "Calling terminate_writing_volume\n");
323       ok = terminate_writing_volume(dcr);
324       if (ok) {
325          reread_last_block(dcr);
326       }
327       return false;
328    }
329
330    /* We successfully wrote the block, now do housekeeping */
331    Dmsg2(1300, "VolCatBytes=%lld newVolCatBytes=%lld\n", dev->VolCatInfo.VolCatBytes,
332       (dev->VolCatInfo.VolCatBytes+wlen));
333    if (!dev->setVolCatAdataBytes(block->BlockAddr + wlen)) {
334       dev->updateVolCatBytes(wlen);
335       Dmsg3(200, "AmetaBytes=%lld AdataBytes=%lld Bytes=%lld\n",
336          dev->VolCatInfo.VolCatAmetaBytes, dev->VolCatInfo.VolCatAdataBytes, dev->VolCatInfo.VolCatBytes);
337    }
338    dev->updateVolCatBlocks(1);
339    dev->LastBlock = block->BlockNumber;
340    block->BlockNumber++;
341
342    /* Update dcr values */
343    if (dev->is_tape()) {
344       dev->EndAddr = dev->get_full_addr();
345       if (dcr->EndAddr < dev->EndAddr) {
346          dcr->EndAddr = dev->EndAddr;
347       }
348       dev->block_num++;
349    } else {
350       /* Save address of byte just written */
351       uint64_t addr = dev->file_addr + wlen - 1;
352       if (dev->is_indexed()) {
353          uint64_t full_addr = dev->get_full_addr(addr);
354          if (full_addr < dcr->EndAddr) {
355             Pmsg2(000, "Possible incorrect EndAddr oldEndAddr=%llu newEndAddr=%llu\n",
356                dcr->EndAddr, full_addr);
357          }
358          dcr->EndAddr = full_addr;
359       }
360
361       if (dev->adata) {
362          /* We really should use file_addr, but I am not sure it is correctly set */
363          Dmsg3(100, "Set BlockAddr from %lld to %lld adata_addr=%lld\n",
364             block->BlockAddr, block->BlockAddr + wlen, dev->adata_addr);
365          block->BlockAddr += wlen;
366          dev->adata_addr = block->BlockAddr;
367       } else {
368          block->BlockAddr = dev->get_full_addr() + wlen;
369       }
370    }
371    if (dev->is_indexed()) {
372       if (dcr->VolMediaId != dev->VolCatInfo.VolMediaId) {
373          Dmsg7(100, "JobMedia Vol=%s wrote=%d MediaId=%lld FI=%lu LI=%lu StartAddr=%lld EndAddr=%lld Wrote\n",
374             dcr->VolumeName, dcr->WroteVol, dcr->VolMediaId,
375             dcr->VolFirstIndex, dcr->VolLastIndex, dcr->StartAddr, dcr->EndAddr);
376       }
377       dcr->VolMediaId = dev->VolCatInfo.VolMediaId;
378       Dmsg3(150, "VolFirstIndex=%d blockFirstIndex=%d Vol=%s\n",
379          dcr->VolFirstIndex, block->FirstIndex, dcr->VolumeName);
380       if (dcr->VolFirstIndex == 0 && block->FirstIndex > 0) {
381          dcr->VolFirstIndex = block->FirstIndex;
382       }
383       if (block->LastIndex > (int32_t)dcr->VolLastIndex) {
384          dcr->VolLastIndex = block->LastIndex;
385       }
386       dcr->WroteVol = true;
387    }
388
389    dev->file_addr += wlen;            /* update file address */
390    dev->file_size += wlen;
391    dev->usage += wlen;                /* update usage counter */
392    if (dev->part > 0) {
393       dev->part_size += wlen;
394    }
395    dev->setVolCatInfo(false);         /* Needs update */
396
397    Dmsg2(1300, "write_block: wrote block %d bytes=%d\n", dev->block_num, wlen);
398    empty_block(block);
399    return true;
400 }
401
402
403 /*
404  * Read block with locking
405  *
406  */
407 bool DCR::read_block_from_device(bool check_block_numbers)
408 {
409    bool ok;
410
411    Dmsg0(250, "Enter read_block_from_device\n");
412    dev->rLock(false);
413    ok = read_block_from_dev(check_block_numbers);
414    dev->rUnlock();
415    Dmsg1(250, "Leave read_block_from_device. ok=%d\n", ok);
416    return ok;
417 }
418
419 static void set_block_position(DCR *dcr, DEVICE *dev, DEV_BLOCK *block)
420 {
421    /* Used also by the Single Item Restore code to locate a particular block */
422    if (dev->is_tape()) {
423       block->BlockAddr = dev->get_full_addr();
424
425    } else if (!dev->adata) {    /* TODO: See if we just use !dev->adata for tapes */
426       /* Note: we only update the DCR values for ameta blocks
427        * because all the indexing (JobMedia) is done with
428        * ameta blocks/records, which may point to adata.
429        */
430       block->BlockAddr = dev->get_full_addr();
431    }
432    block->RecNum = 0;
433 }
434
435 /*
436  * Read the next block into the block structure and unserialize
437  *  the block header.  For a file, the block may be partially
438  *  or completely in the current buffer.
439  * Note: in order for bscan to generate correct JobMedia records
440  *  we must be careful to update the EndAddr of the last byte read.
441  */
442 bool DCR::read_block_from_dev(bool check_block_numbers)
443 {
444    ssize_t stat;
445    int looping;
446    int retry;
447    DCR *dcr = this;
448    boffset_t pos;
449    char ed1[50];
450    uint32_t data_len;
451
452    if (job_canceled(jcr)) {
453       Mmsg(dev->errmsg, _("Job failed or canceled.\n"));
454       Dmsg1(000, "%s", dev->errmsg);
455       block->read_len = 0;
456       return false;
457    }
458    if (!dev->enabled) {
459       Mmsg(dev->errmsg, _("Cannot write block. Device is disabled. dev=%s\n"), dev->print_name());
460       Jmsg1(jcr, M_FATAL, 0, "%s", dev->errmsg);
461       return false;
462    }
463
464    if (dev->at_eot()) {
465       Mmsg(dev->errmsg, _("At EOT: attempt to read past end of Volume.\n"));
466       Dmsg1(000, "%s", dev->errmsg);
467       block->read_len = 0;
468       return false;
469    }
470    looping = 0;
471
472    if (!dev->is_open()) {
473       Mmsg4(dev->errmsg, _("Attempt to read closed device: fd=%d at file:blk %u:%u on device %s\n"),
474          dev->fd(), dev->file, dev->block_num, dev->print_name());
475       Jmsg(dcr->jcr, M_FATAL, 0, "%s", dev->errmsg);
476       Pmsg4(000, "Fatal: dev=%p dcr=%p adata=%d bytes=%lld\n", dev, dcr, dev->adata,
477          VolCatInfo.VolCatAdataRBytes);
478       Pmsg1(000, "%s", dev->errmsg);
479       block->read_len = 0;
480       return false;
481    }
482
483    set_block_position(dcr, dev, block);
484
485 reread:
486    if (looping > 1) {
487       dev->dev_errno = EIO;
488       Mmsg1(dev->errmsg, _("Block buffer size looping problem on device %s\n"),
489          dev->print_name());
490       Dmsg1(000, "%s", dev->errmsg);
491       Jmsg(jcr, M_ERROR, 0, "%s", dev->errmsg);
492       block->read_len = 0;
493       return false;
494    }
495
496    /* See if we must open another part */
497    if (dev->at_eof() && !dev->open_next_part(dcr)) {
498       if (dev->at_eof()) {        /* EOF just seen? */
499          dev->set_eot();          /* yes, error => EOT */
500       }
501       return false;
502    }
503
504    retry = 0;
505    errno = 0;
506    stat = 0;
507
508    if (dev->adata) {
509       dev->lseek(dcr, dcr->block->BlockAddr, SEEK_SET);
510    }
511    {  /* debug block */
512       pos = dev->lseek(dcr, (boffset_t)0, SEEK_CUR); /* get curr pos */
513       Dmsg2(200, "Pos for read=%s %lld\n",
514          dev->print_addr(ed1, sizeof(ed1), pos), pos);
515    }
516
517    data_len = 0;
518
519    do {
520       retry = 0;
521
522       do {
523          if ((retry > 0 && stat == -1 && errno == EBUSY)) {
524             berrno be;
525             Dmsg4(100, "===== read retry=%d stat=%d errno=%d: ERR=%s\n",
526                   retry, stat, errno, be.bstrerror());
527             bmicrosleep(10, 0);    /* pause a bit if busy or lots of errors */
528             dev->clrerror(-1);
529          }
530          stat = dev->read(block->buf + data_len, (size_t)(block->buf_len - data_len));
531          if (stat > 0)
532             data_len += stat;
533
534       } while (stat == -1 && (errno == EBUSY || errno == EINTR || errno == EIO) && retry++ < 3);
535
536    } while (data_len < block->buf_len && stat > 0 && dev->dev_type == B_FIFO_DEV);
537
538    Dmsg4(110, "Read() adata=%d vol=%s nbytes=%d pos=%lld\n",
539       block->adata, dev->VolHdr.VolumeName, stat < 0 ? stat : data_len, pos);
540    if (stat < 0) {
541       berrno be;
542       dev->clrerror(-1);
543       Dmsg2(90, "Read device fd=%d got: ERR=%s\n", dev->fd(), be.bstrerror());
544       block->read_len = 0;
545       if (reading_label) {      /* Trying to read Volume label */
546          Mmsg(dev->errmsg, _("The %sVolume=%s on device=%s appears to be unlabeled.%s\n"),
547             dev->adata?"adata ":"", VolumeName, dev->print_name(),
548             dev->is_fs_nearly_full(1048576)?" Notice that the filesystem is nearly full.":"");
549       } else {
550          Mmsg4(dev->errmsg, _("Read error on fd=%d at addr=%s on device %s. ERR=%s.\n"),
551             dev->fd(), dev->print_addr(ed1, sizeof(ed1)), dev->print_name(), be.bstrerror());
552       }
553       Jmsg(jcr, M_ERROR, 0, "%s", dev->errmsg);
554       if (dev->get_tape_alerts(this)) {
555          dev->show_tape_alerts(this, list_long, list_last, alert_callback);
556       }
557       if (dev->at_eof()) {        /* EOF just seen? */
558          dev->set_eot();          /* yes, error => EOT */
559       }
560       return false;
561    }
562
563    stat = data_len;
564
565    if (stat == 0) {             /* Got EOF ! */
566       pos = dev->lseek(dcr, (boffset_t)0, SEEK_CUR); /* get curr pos */
567       pos = dev->get_full_addr(pos);
568       if (reading_label) {      /* Trying to read Volume label */
569          Mmsg4(dev->errmsg, _("The %sVolume=%s on device=%s appears to be unlabeled.%s\n"),
570             dev->adata?"adata ":"", VolumeName, dev->print_name(),
571             dev->is_fs_nearly_full(1048576)?" Notice tha the filesystem is nearly full.":"");
572       } else {
573          Mmsg4(dev->errmsg, _("Read zero %sbytes Vol=%s at %s on device %s.\n"),
574                dev->adata?"adata ":"", dev->VolCatInfo.VolCatName,
575                dev->print_addr(ed1, sizeof(ed1), pos), dev->print_name());
576       }
577       block->read_len = 0;
578       Dmsg1(100, "%s", dev->errmsg);
579       if (dev->at_eof()) {        /* EOF just seen? */
580          dev->set_eot();          /* yes, error => EOT */
581       }
582       dev->set_ateof();
583       dev->file_addr = 0;
584       dev->EndAddr = pos;
585       if (dcr->EndAddr < dev->EndAddr) {
586          dcr->EndAddr = dev->EndAddr;
587       }
588       Dmsg3(150, "==== Read zero bytes. adata=%d vol=%s at %s\n", dev->adata,
589          dev->VolCatInfo.VolCatName, dev->print_addr(ed1, sizeof(ed1), pos));
590       return false;             /* return eof */
591    }
592
593    /* Continue here for successful read */
594
595    block->read_len = stat;      /* save length read */
596    if (block->adata) {
597       block->binbuf = block->read_len;
598       block->block_len = block->read_len;
599    } else {
600       if (block->read_len == 80 &&
601            (dcr->VolCatInfo.LabelType != B_BACULA_LABEL ||
602             dcr->device->label_type != B_BACULA_LABEL)) {
603          /* ***FIXME*** should check label */
604          Dmsg2(100, "Ignore 80 byte ANSI label at %u:%u\n", dev->file, dev->block_num);
605          dev->clear_eof();
606          goto reread;             /* skip ANSI/IBM label */
607       }
608
609       if (block->read_len < BLKHDR2_LENGTH) {
610          dev->dev_errno = EIO;
611          Mmsg3(dev->errmsg, _("Volume data error at %s! Very short block of %d bytes on device %s discarded.\n"),
612             dev->print_addr(ed1, sizeof(ed1)), block->read_len, dev->print_name());
613          Jmsg(jcr, M_ERROR, 0, "%s", dev->errmsg);
614          dev->set_short_block();
615          block->read_len = block->binbuf = 0;
616          Dmsg2(50, "set block=%p binbuf=%d\n", block, block->binbuf);
617          return false;             /* return error */
618       }
619
620       if (!unser_block_header(this, dev, block)) {
621          if (forge_on) {
622             dev->file_addr += block->read_len;
623             dev->file_size += block->read_len;
624             goto reread;
625          }
626          return false;
627       }
628    }
629
630    /*
631     * If the block is bigger than the buffer, we reposition for
632     *  re-reading the block, allocate a buffer of the correct size,
633     *  and go re-read.
634     */
635    Dmsg3(150, "adata=%d block_len=%d buf_len=%d\n", block->adata, block->block_len, block->buf_len);
636    if (block->block_len > block->buf_len) {
637       dev->dev_errno = EIO;
638       Mmsg2(dev->errmsg,  _("Block length %u is greater than buffer %u. Attempting recovery.\n"),
639          block->block_len, block->buf_len);
640       Jmsg(jcr, M_ERROR, 0, "%s", dev->errmsg);
641       Pmsg1(000, "%s", dev->errmsg);
642       /* Attempt to reposition to re-read the block */
643       if (dev->is_tape()) {
644          Dmsg0(250, "BSR for reread; block too big for buffer.\n");
645          if (!dev->bsr(1)) {
646             Mmsg(dev->errmsg, "%s", dev->bstrerror());
647             if (dev->errmsg[0]) {
648                Jmsg(jcr, M_ERROR, 0, "%s", dev->errmsg);
649             }
650             block->read_len = 0;
651             return false;
652          }
653       } else {
654          Dmsg0(250, "Seek to beginning of block for reread.\n");
655          boffset_t pos = dev->lseek(dcr, (boffset_t)0, SEEK_CUR); /* get curr pos */
656          pos -= block->read_len;
657          dev->lseek(dcr, pos, SEEK_SET);
658          dev->file_addr = pos;
659       }
660       Mmsg1(dev->errmsg, _("Setting block buffer size to %u bytes.\n"), block->block_len);
661       Jmsg(jcr, M_INFO, 0, "%s", dev->errmsg);
662       Pmsg1(000, "%s", dev->errmsg);
663       /* Set new block length */
664       dev->max_block_size = block->block_len;
665       block->buf_len = block->block_len;
666       free_memory(block->buf);
667       block->buf = get_memory(block->buf_len);
668       empty_block(block);
669       looping++;
670       goto reread;                    /* re-read block with correct block size */
671    }
672
673    if (block->block_len > block->read_len) {
674       dev->dev_errno = EIO;
675       Mmsg4(dev->errmsg, _("Volume data error at %u:%u! Short block of %d bytes on device %s discarded.\n"),
676          dev->file, dev->block_num, block->read_len, dev->print_name());
677       Jmsg(jcr, M_ERROR, 0, "%s", dev->errmsg);
678       dev->set_short_block();
679       block->read_len = block->binbuf = 0;
680       return false;             /* return error */
681    }
682
683    dev->clear_short_block();
684    dev->clear_eof();
685    dev->updateVolCatReads(1);
686    dev->updateVolCatReadBytes(block->read_len);
687
688    /* Update dcr values */
689    if (dev->is_tape()) {
690       dev->EndAddr = dev->get_full_addr();
691       if (dcr->EndAddr <  dev->EndAddr) {
692          dcr->EndAddr = dev->EndAddr;
693       }
694       dev->block_num++;
695    } else {
696       /* We need to take care about a short block in EndBlock/File
697        * computation
698        */
699       uint32_t len = MIN(block->read_len, block->block_len);
700       uint64_t addr = dev->get_full_addr() + len - 1;
701       if (dev->is_indexed()) {
702          if (addr > dcr->EndAddr) {
703             dcr->EndAddr = addr;
704          }
705       }
706       dev->EndAddr = addr;
707    }
708    if (dev->is_indexed()) {
709       dcr->VolMediaId = dev->VolCatInfo.VolMediaId;
710    }
711    dev->file_addr += block->read_len;
712    dev->file_size += block->read_len;
713    dev->usage     += block->read_len;      /* update usage counter */
714
715    /*
716     * If we read a short block on disk,
717     * seek to beginning of next block. This saves us
718     * from shuffling blocks around in the buffer. Take a
719     * look at this from an efficiency stand point later, but
720     * it should only happen once at the end of each job.
721     *
722     * I've been lseek()ing negative relative to SEEK_CUR for 30
723     *   years now. However, it seems that with the new off_t definition,
724     *   it is not possible to seek negative amounts, so we use two
725     *   lseek(). One to get the position, then the second to do an
726     *   absolute positioning -- so much for efficiency.  KES Sep 02.
727     */
728    Dmsg0(250, "At end of read block\n");
729    if (block->read_len > block->block_len && !dev->is_tape()) {
730       char ed1[50];
731       boffset_t pos = dev->lseek(dcr, (boffset_t)0, SEEK_CUR); /* get curr pos */
732       Dmsg1(250, "Current lseek pos=%s\n", edit_int64(pos, ed1));
733       pos -= (block->read_len - block->block_len);
734       dev->lseek(dcr, pos, SEEK_SET);
735       Dmsg3(250, "Did lseek pos=%s blk_size=%d rdlen=%d\n",
736          edit_int64(pos, ed1), block->block_len,
737             block->read_len);
738       dev->file_addr = pos;
739       dev->file_size = pos;
740    }
741    Dmsg3(150, "Exit read_block read_len=%d block_len=%d binbuf=%d\n",
742       block->read_len, block->block_len, block->binbuf);
743    block->block_read = true;
744    return true;
745 }