]> git.sur5r.net Git - bacula/bacula/blob - bacula/src/stored/block.c
Backport from Bacula Enterprise
[bacula/bacula] / bacula / src / stored / block.c
1 /*
2    Bacula(R) - The Network Backup Solution
3
4    Copyright (C) 2000-2015 Kern Sibbald
5    Copyright (C) 2001-2014 Free Software Foundation Europe e.V.
6
7    The original author of Bacula is Kern Sibbald, with contributions
8    from many others, a complete list can be found in the file AUTHORS.
9
10    You may use this file and others of this release according to the
11    license defined in the LICENSE file, which includes the Affero General
12    Public License, v3.0 ("AGPLv3") and some additional permissions and
13    terms pursuant to its AGPLv3 Section 7.
14
15    This notice must be preserved when any source code is 
16    conveyed and/or propagated.
17
18    Bacula(R) is a registered trademark of Kern Sibbald.
19 */
20 /*
21  *
22  *   block.c -- tape block handling functions
23  *
24  *              Kern Sibbald, March MMI
25  *                 added BB02 format October MMII
26  *
27  */
28
29
30 #include "bacula.h"
31 #include "stored.h"
32
33 #ifdef DEBUG_BLOCK_CHECKSUM
34 static const bool debug_block_checksum = true;
35 #else
36 static const bool debug_block_checksum = false;
37 #endif
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 bool do_new_file_bookkeeping(DCR *dcr);
47 //bool do_dvd_size_checks(DCR *dcr);
48 void reread_last_block(DCR *dcr);
49 uint32_t get_len_and_clear_block(DEV_BLOCK *block, DEVICE *dev, uint32_t &pad);
50 uint32_t ser_block_header(DEV_BLOCK *block, bool do_checksum);
51 bool unser_block_header(JCR *jcr, DEVICE *dev, DEV_BLOCK *block);
52
53 /*
54  * Write a block to the device, with locking and unlocking
55  *
56  * Returns: true  on success
57  *        : false on failure
58  *
59  */
60 bool DCR::write_block_to_device(bool final)
61 {
62    bool ok = true;
63    DCR *dcr = this;
64
65    if (dcr->spooling) {
66       Dmsg0(200, "Write to spool\n");
67       ok = write_block_to_spool_file(dcr);
68       return ok;
69    }
70
71    if (!is_dev_locked()) {        /* device already locked? */
72       /* note, do not change this to dcr->rLock */
73       dev->rLock(false);          /* no, lock it */
74    }
75
76    if (!check_for_newvol_or_newfile(dcr)) {
77       ok = false;
78       goto bail_out;   /* fatal error */
79    }
80
81    Dmsg1(500, "Write block to dev=%p\n", dcr->dev);
82    if (!write_block_to_dev()) {
83       Dmsg1(40, "*** Failed write_block_to_dev block=%p\n", block);
84       if (job_canceled(jcr) || jcr->getJobType() == JT_SYSTEM) {
85          ok = false;
86          Dmsg2(40, "cancel=%d or SYSTEM=%d\n", job_canceled(jcr),
87             jcr->getJobType() == JT_SYSTEM);
88       } else {
89          ok = fixup_device_block_write_error(dcr);
90       }
91    }
92    if (ok && final && !dir_create_jobmedia_record(dcr)) {
93       Jmsg(jcr, M_FATAL, 0, _("Error writing final JobMedia record to catalog.\n"));
94    }
95
96 bail_out:
97    if (!dcr->is_dev_locked()) {        /* did we lock dev above? */
98       /* note, do not change this to dcr->dunlock */
99       dev->Unlock();                  /* unlock it now */
100    }
101    return ok;
102 }
103
104 /*
105  * Write a block to the device
106  *
107  *  Returns: true  on success or EOT
108  *           false on hard error
109  */
110 bool DCR::write_block_to_dev()
111 {
112    ssize_t stat = 0;
113    uint32_t wlen;                     /* length to write */
114    bool ok = true;
115    DCR *dcr = this;
116    uint32_t checksum;
117    uint32_t pad;                      /* padding or zeros written */
118
119    if (no_tape_write_test) {
120       empty_block(block);
121       return true;
122    }
123    if (job_canceled(jcr)) {
124       return false;
125    }
126
127    Dmsg3(200, "fd=%d bufp-buf=%d binbuf=%d\n", dev->fd(),
128       block->bufp-block->buf, block->binbuf);
129    ASSERT2(block->binbuf == ((uint32_t)(block->bufp - block->buf)), "binbuf badly set");
130
131    if (is_block_empty(block)) {  /* Does block have data in it? */
132       Dmsg0(150, "return write_block_to_dev no data to write\n");
133       return true;
134    }
135
136    dump_block(block, "before write");
137    if (dev->at_weot()) {
138       Dmsg0(50, "==== FATAL: At EOM with ST_WEOT.\n");
139       dev->dev_errno = ENOSPC;
140       Jmsg1(jcr, M_FATAL, 0,  _("Cannot write block. Device at EOM. dev=%s\n"), dev->print_name());
141       return false;
142    }
143    if (!dev->can_append()) {
144       dev->dev_errno = EIO;
145       Jmsg1(jcr, M_FATAL, 0, _("Attempt to write on read-only Volume. dev=%s\n"), dev->print_name());
146       Dmsg1(50, "Attempt to write on read-only Volume. dev=%s\n", dev->print_name());
147       return false;
148    }
149
150    if (!dev->is_open()) {
151       Jmsg1(jcr, M_FATAL, 0, _("Attempt to write on closed device=%s\n"), dev->print_name());
152       Dmsg1(50, "Attempt to write on closed device=%s\n", dev->print_name());
153       return false;
154    }
155
156    wlen = get_len_and_clear_block(block, dev, pad);
157    block->block_len = wlen;
158
159    checksum = ser_block_header(block, dev->do_checksum());
160
161    if (user_volume_size_reached(dcr, true)) {
162       Dmsg0(40, "Calling terminate_writing_volume\n");
163       terminate_writing_volume(dcr);
164       reread_last_block(dcr);   /* Only used on tapes */
165       dev->dev_errno = ENOSPC;
166       return false;
167    }
168
169    /*
170     * Limit maximum File size on volume to user specified value.
171     *  In practical terms, this means to put an EOF mark on
172     *  a tape after every X bytes. This effectively determines
173     *  how many index records we have (JobMedia). If you set
174     *  max_file_size too small, it will cause a lot of shoe-shine
175     *  on very fast modern tape (LTO-3 and above).
176     */
177    if ((dev->max_file_size > 0) &&
178        (dev->file_size+block->binbuf) >= dev->max_file_size) {
179       dev->file_size = 0;             /* reset file size */
180
181       if (!dev->weof(1)) {            /* write eof */
182          Dmsg0(50, "WEOF error in max file size.\n");
183          Jmsg(jcr, M_FATAL, 0, _("Unable to write EOF. ERR=%s\n"),
184             dev->bstrerror());
185          Dmsg0(40, "Calling terminate_writing_volume\n");
186          terminate_writing_volume(dcr);
187          dev->dev_errno = ENOSPC;
188          return false;
189       }
190       if (!write_ansi_ibm_labels(dcr, ANSI_EOF_LABEL, dev->VolHdr.VolumeName)) {
191          return false;
192       }
193
194       if (!do_new_file_bookkeeping(dcr)) {
195          /* Error message already sent */
196          return false;
197       }
198    }
199
200    dev->updateVolCatWrites(1);
201
202 #ifdef DEBUG_BLOCK_ZEROING
203    uint32_t *bp = (uint32_t *)block->buf;
204    if (bp[0] == 0 && bp[1] == 0 && bp[2] == 0 && block->buf[12] == 0) {
205       Jmsg0(jcr, M_ABORT, 0, _("Write block header zeroed.\n"));
206    }
207 #endif
208
209    /*
210     * Do write here, make a somewhat feeble attempt to recover from
211     *  I/O errors, or from the OS telling us it is busy.
212     */
213    int retry = 0;
214    errno = 0;
215    stat = 0;
216    do {
217       if (retry > 0 && stat == -1 && errno == EBUSY) {
218          berrno be;
219          Dmsg4(100, "===== write retry=%d stat=%d errno=%d: ERR=%s\n",
220                retry, stat, errno, be.bstrerror());
221          bmicrosleep(5, 0);    /* pause a bit if busy or lots of errors */
222          dev->clrerror(-1);
223       }
224       stat = dev->write(block->buf, (size_t)wlen);
225       Dmsg4(110, "Write() BlockAddr=%lld NextAddr=%lld Vol=%s wlen=%d\n",
226          block->BlockAddr, dev->lseek(dcr, 0, SEEK_CUR),
227          dev->VolHdr.VolumeName, wlen);
228    } while (stat == -1 && (errno == EBUSY || errno == EIO) && retry++ < 3);
229
230    if (debug_block_checksum) {
231       uint32_t achecksum = ser_block_header(block, dev->do_checksum());
232       if (checksum != achecksum) {
233          Jmsg2(jcr, M_ERROR, 0, _("Block checksum changed during write: before=%ud after=%ud\n"),
234             checksum, achecksum);
235          dump_block(block, "with checksum error");
236       }
237    }
238
239 #ifdef DEBUG_BLOCK_ZEROING
240    if (bp[0] == 0 && bp[1] == 0 && bp[2] == 0 && block->buf[12] == 0) {
241       Jmsg0(jcr, M_ABORT, 0, _("Write block header zeroed.\n"));
242    }
243 #endif
244
245    if (stat != (ssize_t)wlen) {
246       /* Some devices simply report EIO when the volume is full.
247        * With a little more thought we may be able to check
248        * capacity and distinguish real errors and EOT
249        * conditions.  In any case, we probably want to
250        * simulate an End of Medium.
251        */
252       if (stat == -1) {
253          berrno be;
254          dev->clrerror(-1);                 /* saves errno in dev->dev_errno */
255          if (dev->dev_errno == 0) {
256             dev->dev_errno = ENOSPC;        /* out of space */
257          }
258          if (dev->dev_errno != ENOSPC) {
259             dev->VolCatInfo.VolCatErrors++;
260             Jmsg4(jcr, M_ERROR, 0, _("Write error at %u:%u on device %s. ERR=%s.\n"),
261                dev->file, dev->block_num, dev->print_name(), be.bstrerror());
262          }
263       } else {
264         dev->dev_errno = ENOSPC;            /* out of space */
265       }
266       if (dev->dev_errno == ENOSPC) {
267          dev->update_freespace();
268          if (dev->is_freespace_ok() && dev->free_space < dev->min_free_space) {
269             dev->set_nospace();
270             Jmsg(jcr, M_WARNING, 0, _("Out of freespace caused End of Volume \"%s\" at %u:%u on device %s. Write of %u bytes got %d.\n"),
271                dev->getVolCatName(),
272                dev->file, dev->block_num, dev->print_name(), wlen, stat);
273          } else {
274             dev->clear_nospace();
275             Jmsg(jcr, M_INFO, 0, _("End of Volume \"%s\" at %u:%u on device %s. Write of %u bytes got %d.\n"),
276                dev->getVolCatName(),
277                dev->file, dev->block_num, dev->print_name(), wlen, stat);
278          }
279       }
280       if (chk_dbglvl(100)) {
281          berrno be;
282          Dmsg7(90, "==== Write error. fd=%d size=%u rtn=%d dev_blk=%d blk_blk=%d errno=%d: ERR=%s\n",
283             dev->fd(), wlen, stat, dev->block_num, block->BlockNumber,
284             dev->dev_errno, be.bstrerror(dev->dev_errno));
285       }
286
287       Dmsg0(40, "Calling terminate_writing_volume\n");
288       ok = terminate_writing_volume(dcr);
289       if (ok) {
290          reread_last_block(dcr);
291       }
292       return false;
293    }
294
295    /* We successfully wrote the block, now do housekeeping */
296    Dmsg2(1300, "VolCatBytes=%lld newVolCatBytes=%lld\n", dev->VolCatInfo.VolCatBytes,
297       (dev->VolCatInfo.VolCatBytes+wlen));
298    dev->updateVolCatBytes(wlen);
299    dev->updateVolCatBlocks(1);
300    dev->EndBlock = dev->block_num;
301    dev->EndFile  = dev->file;
302    dev->LastBlock = block->BlockNumber;
303    block->BlockNumber++;
304
305    /* Update dcr values */
306    if (dev->is_tape()) {
307       if (dcr->EndFile != dev->EndFile || dev->EndBlock > dcr->EndBlock) {
308          dcr->EndBlock = dev->EndBlock;
309          dcr->EndFile  = dev->EndFile;
310       }
311       dev->block_num++;
312    } else {
313       /* Save address of block just written */
314       uint64_t addr = dev->file_addr + wlen - 1;
315       if (dcr->EndFile == (uint32_t)(addr >> 32) &&
316           (uint32_t)addr < dcr->EndBlock) {
317          Pmsg4(000, "Possible incorrect EndBlock oldEndBlock=%d newEndBlock=%d oldEndFile=%d newEndFile=%d\n",
318             dcr->EndBlock, (uint32_t)addr, dcr->EndFile, (uint32_t)(addr >> 32));
319       } else {
320          dcr->EndBlock = (uint32_t)addr;
321          dcr->EndFile = (uint32_t)(addr >> 32);
322       }
323       dev->block_num = (uint32_t)addr;
324       dev->file = (uint32_t)(addr >> 32);
325       block->BlockAddr = dev->file_addr + wlen;
326       Dmsg3(150, "Set block->BlockAddr=%lld wlen=%d block=%x\n",
327          block->BlockAddr, wlen, block);
328    }
329    dcr->VolMediaId = dev->VolCatInfo.VolMediaId;
330    Dmsg3(150, "VolFirstIndex=%d blockFirstIndex=%d Vol=%s\n",
331       dcr->VolFirstIndex, block->FirstIndex, dcr->VolumeName);
332    if (dcr->VolFirstIndex == 0 && block->FirstIndex > 0) {
333       dcr->VolFirstIndex = block->FirstIndex;
334    }
335    if (block->LastIndex > (int32_t)dcr->VolLastIndex) {
336       dcr->VolLastIndex = block->LastIndex;
337    }
338    dcr->WroteVol = true;
339    dev->file_addr += wlen;            /* update file address */
340    dev->file_size += wlen;
341    dev->part_size += wlen;
342    dev->setVolCatInfo(false);         /* Needs update */
343
344    Dmsg2(1300, "write_block: wrote block %d bytes=%d\n", dev->block_num, wlen);
345    empty_block(block);
346    return true;
347 }
348
349
350 /*
351  * Read block with locking
352  *
353  */
354 bool DCR::read_block_from_device(bool check_block_numbers)
355 {
356    bool ok;
357
358    Dmsg0(250, "Enter read_block_from_device\n");
359    dev->rLock(false);
360    ok = read_block_from_dev(check_block_numbers);
361    dev->rUnlock();
362    Dmsg0(250, "Leave read_block_from_device\n");
363    return ok;
364 }
365
366 static void set_block_position(DCR *dcr, DEVICE *dev, DEV_BLOCK *block)
367 {
368    if (dev->is_tape()) {
369       block->Block = dev->block_num;
370       block->File = dev->file;
371    } else {
372       block->Block = (uint32_t)dev->file_addr;
373       block->File  = (uint32_t)(dev->file_addr >> 32);
374    }
375    block->RecNum = 0;
376 }
377
378 /*
379  * Read the next block into the block structure and unserialize
380  *  the block header.  For a file, the block may be partially
381  *  or completely in the current buffer.
382  */
383 bool DCR::read_block_from_dev(bool check_block_numbers)
384 {
385    ssize_t stat;
386    int looping;
387    int retry;
388    DCR *dcr = this;
389
390    if (job_canceled(jcr)) {
391       Mmsg(dev->errmsg, _("Job failed or canceled.\n"));
392       block->read_len = 0;
393       return false;
394    }
395
396    if (dev->at_eot()) {
397       Mmsg(dev->errmsg, _("Attempt to read past end of tape or file.\n"));
398       block->read_len = 0;
399       return false;
400    }
401    looping = 0;
402    Dmsg1(250, "Full read in read_block_from_device() len=%d\n", block->buf_len);
403
404    if (!dev->is_open()) {
405       Mmsg4(dev->errmsg, _("Attempt to read closed device: fd=%d at file:blk %u:%u on device %s\n"),
406          dev->fd(), dev->file, dev->block_num, dev->print_name());
407       Jmsg(dcr->jcr, M_FATAL, 0, "%s", dev->errmsg);
408       Pmsg1(000, "%s", dev->errmsg);
409       block->read_len = 0;
410       return false;
411    }
412
413    set_block_position(dcr, dev, block);
414
415 reread:
416    if (looping > 1) {
417       dev->dev_errno = EIO;
418       Mmsg1(dev->errmsg, _("Block buffer size looping problem on device %s\n"),
419          dev->print_name());
420       Jmsg(jcr, M_ERROR, 0, "%s", dev->errmsg);
421       block->read_len = 0;
422       return false;
423    }
424
425    dump_block(block, "before read");
426
427 #ifdef xxxBUILD_DVD
428    /* Check for DVD part file end */
429    if (dev->at_eof() && dev->is_dvd() && dev->num_dvd_parts > 0 &&
430         dev->part <= dev->num_dvd_parts) {
431       Dmsg0(400, "Call dvd_open_next_part\n");
432       if (dvd_open_next_part(dcr) < 0) {
433          Mmsg3(dev->errmsg, _("Unable to open device part=%d %s: ERR=%s\n"),
434                dev->part, dev->print_name(), dev->bstrerror());
435          Jmsg(dcr->jcr, M_FATAL, 0, "%s", dev->errmsg);
436          dev->dev_errno = EIO;
437          return false;
438       }
439    }
440 #endif /* xxxBUILD_DVD */
441
442    retry = 0;
443    errno = 0;
444    stat = 0;
445
446    boffset_t pos = dev->lseek(dcr, (boffset_t)0, SEEK_CUR); /* get curr pos */
447    do {
448       if ((retry > 0 && stat == -1 && errno == EBUSY)) {
449          berrno be;
450          Dmsg4(100, "===== read retry=%d stat=%d errno=%d: ERR=%s\n",
451                retry, stat, errno, be.bstrerror());
452          bmicrosleep(10, 0);    /* pause a bit if busy or lots of errors */
453          dev->clrerror(-1);
454       }
455       stat = dev->read(block->buf, (size_t)block->buf_len);
456
457    } while (stat == -1 && (errno == EBUSY || errno == EINTR || errno == EIO) && retry++ < 3);
458    Dmsg3(110, "Read() vol=%s nbytes=%d addr=%lld\n",
459       dev->VolHdr.VolumeName, stat, pos);
460    if (stat < 0) {
461       berrno be;
462       dev->clrerror(-1);
463       Dmsg2(90, "Read device fd=%d got: ERR=%s\n", dev->fd(), be.bstrerror());
464       block->read_len = 0;
465       if (dev->file == 0 && dev->block_num == 0) { /* Attempt to read label */
466          Mmsg(dev->errmsg, _("The Volume=%s on device=%s appears to be unlabeled.\n"),
467             "", dev->VolCatInfo.VolCatName, dev->print_name());
468       } else {
469          Mmsg5(dev->errmsg, _("Read error on fd=%d at file:blk %u:%u on device %s. ERR=%s.\n"),
470             dev->fd(), dev->file, dev->block_num, dev->print_name(), be.bstrerror());
471       }
472       Jmsg(jcr, M_ERROR, 0, "%s", dev->errmsg);
473       if (dev->at_eof()) {        /* EOF just seen? */
474          dev->set_eot();          /* yes, error => EOT */
475       }
476       return false;
477    }
478    if (stat == 0) {             /* Got EOF ! */
479       if (dev->file == 0 && dev->block_num == 0) { /* Attempt to read label */
480          Mmsg3(dev->errmsg, _("The %sVolume=%s on device=%s appears to be unlabeled.\n"),
481             "", dev->VolCatInfo.VolCatName, dev->print_name());
482       } else {
483          Mmsg4(dev->errmsg, _("Read zero %sbytes Vol=%s at %lld on device %s.\n"),
484                "", dev->VolCatInfo.VolCatName, pos, dev->print_name());
485       }
486       dev->block_num = 0;
487       block->read_len = 0;
488       Dmsg1(100, "%s", dev->errmsg);
489       if (dev->at_eof()) {       /* EOF already read? */
490          dev->set_eot();         /* yes, 2 EOFs => EOT */
491          return false;
492       }
493       dev->set_ateof();
494       Dmsg2(150, "==== Read zero bytes. vol=%s at %lld\n", dev->VolCatInfo.VolCatName, pos);
495       return false;             /* return eof */
496    }
497
498    /* Continue here for successful read */
499
500    block->read_len = stat;      /* save length read */
501    if (block->read_len == 80 &&
502         (dcr->VolCatInfo.LabelType != B_BACULA_LABEL ||
503          dcr->device->label_type != B_BACULA_LABEL)) {
504       /* ***FIXME*** should check label */
505       Dmsg2(100, "Ignore 80 byte ANSI label at %u:%u\n", dev->file, dev->block_num);
506       dev->clear_eof();
507       goto reread;             /* skip ANSI/IBM label */
508    }
509
510    if (block->read_len < BLKHDR2_LENGTH) {
511       dev->dev_errno = EIO;
512       Mmsg4(dev->errmsg, _("Volume data error at %u:%u! Very short block of %d bytes on device %s discarded.\n"),
513          dev->file, dev->block_num, block->read_len, dev->print_name());
514       Jmsg(jcr, M_ERROR, 0, "%s", dev->errmsg);
515       dev->set_short_block();
516       block->read_len = block->binbuf = 0;
517       Dmsg2(50, "set block=%p binbuf=%d\n", block, block->binbuf);
518       return false;             /* return error */
519    }
520
521    //BlockNumber = block->BlockNumber + 1;
522    if (!unser_block_header(jcr, dev, block)) {
523       if (forge_on) {
524          dev->file_addr += block->read_len;
525          dev->file_size += block->read_len;
526          goto reread;
527       }
528       return false;
529    }
530
531    /*
532     * If the block is bigger than the buffer, we reposition for
533     *  re-reading the block, allocate a buffer of the correct size,
534     *  and go re-read.
535     */
536    Dmsg2(150, "block_len=%d buf_len=%d\n", block->block_len, block->buf_len);
537    if (block->block_len > block->buf_len) {
538       dev->dev_errno = EIO;
539       Mmsg2(dev->errmsg,  _("Block length %u is greater than buffer %u. Attempting recovery.\n"),
540          block->block_len, block->buf_len);
541       Jmsg(jcr, M_ERROR, 0, "%s", dev->errmsg);
542       Pmsg1(000, "%s", dev->errmsg);
543       /* Attempt to reposition to re-read the block */
544       if (dev->is_tape()) {
545          Dmsg0(250, "BSR for reread; block too big for buffer.\n");
546          if (!dev->bsr(1)) {
547             Mmsg(dev->errmsg, "%s", dev->bstrerror());
548             Jmsg(jcr, M_ERROR, 0, "%s", dev->errmsg);
549             block->read_len = 0;
550             return false;
551          }
552       } else {
553          Dmsg0(250, "Seek to beginning of block for reread.\n");
554          boffset_t pos = dev->lseek(dcr, (boffset_t)0, SEEK_CUR); /* get curr pos */
555          pos -= block->read_len;
556          dev->lseek(dcr, pos, SEEK_SET);
557          dev->file_addr = pos;
558       }
559       Mmsg1(dev->errmsg, _("Setting block buffer size to %u bytes.\n"), block->block_len);
560       Jmsg(jcr, M_INFO, 0, "%s", dev->errmsg);
561       Pmsg1(000, "%s", dev->errmsg);
562       /* Set new block length */
563       dev->max_block_size = block->block_len;
564       block->buf_len = block->block_len;
565       free_memory(block->buf);
566       block->buf = get_memory(block->buf_len);
567       empty_block(block);
568       looping++;
569       goto reread;                    /* re-read block with correct block size */
570    }
571
572    if (block->block_len > block->read_len) {
573       dev->dev_errno = EIO;
574       Mmsg4(dev->errmsg, _("Volume data error at %u:%u! Short block of %d bytes on device %s discarded.\n"),
575          dev->file, dev->block_num, block->read_len, dev->print_name());
576       Jmsg(jcr, M_ERROR, 0, "%s", dev->errmsg);
577       dev->set_short_block();
578       block->read_len = block->binbuf = 0;
579       return false;             /* return error */
580    }
581
582    dev->clear_short_block();
583    dev->clear_eof();
584    dev->updateVolCatReads(1);
585    dev->updateVolCatReadBytes(block->read_len);
586
587    dev->EndBlock = dev->block_num;
588    dev->EndFile  = dev->file;
589    dev->block_num++;
590
591    /* Update dcr values */
592    if (dev->is_tape()) {
593       if (dcr->EndFile != dev->EndFile || dev->EndBlock > dcr->EndBlock) {
594          dcr->EndBlock = dev->EndBlock;
595          dcr->EndFile  = dev->EndFile;
596       }
597    } else {
598       /* We need to take care about a short block in EndBlock/File
599        * computation
600        */
601       uint32_t len = MIN(block->read_len, block->block_len);
602       uint64_t addr = dev->file_addr + len - 1;
603       if ((uint32_t)(addr >> 32) != dcr->EndFile || (uint32_t)addr > dcr->EndBlock) {
604          dcr->EndBlock = (uint32_t)addr;
605          dcr->EndFile = (uint32_t)(addr >> 32);
606       }
607       dev->block_num = dev->EndBlock = (uint32_t)addr;
608       dev->file = dev->EndFile = (uint32_t)(addr >> 32);
609    }
610    dcr->VolMediaId = dev->VolCatInfo.VolMediaId;
611    dev->file_addr += block->read_len;
612    dev->file_size += block->read_len;
613
614    /*
615     * If we read a short block on disk,
616     * seek to beginning of next block. This saves us
617     * from shuffling blocks around in the buffer. Take a
618     * look at this from an efficiency stand point later, but
619     * it should only happen once at the end of each job.
620     *
621     * I've been lseek()ing negative relative to SEEK_CUR for 30
622     *   years now. However, it seems that with the new off_t definition,
623     *   it is not possible to seek negative amounts, so we use two
624     *   lseek(). One to get the position, then the second to do an
625     *   absolute positioning -- so much for efficiency.  KES Sep 02.
626     */
627    Dmsg0(250, "At end of read block\n");
628    if (block->read_len > block->block_len && !dev->is_tape()) {
629       char ed1[50];
630       boffset_t pos = dev->lseek(dcr, (boffset_t)0, SEEK_CUR); /* get curr pos */
631       Dmsg1(250, "Current lseek pos=%s\n", edit_int64(pos, ed1));
632       pos -= (block->read_len - block->block_len);
633       dev->lseek(dcr, pos, SEEK_SET);
634       Dmsg3(250, "Did lseek pos=%s blk_size=%d rdlen=%d\n",
635          edit_int64(pos, ed1), block->block_len,
636             block->read_len);
637       dev->file_addr = pos;
638       dev->file_size = pos;
639    }
640    Dmsg3(150, "Exit read_block read_len=%d block_len=%d binbuf=%d\n",
641       block->read_len, block->block_len, block->binbuf);
642    block->block_read = true;
643    return true;
644 }