]> git.sur5r.net Git - bacula/bacula/blob - bacula/src/stored/dev.c
187a083af1eccebe6ce444f7c0a18fb8829c65d0
[bacula/bacula] / bacula / src / stored / dev.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  *   dev.c  -- low level operations on device (storage device)
22  *
23  *     written by, Kern Sibbald, MM
24  *
25  *     NOTE!!!! None of these routines are reentrant. You must
26  *        use dev->rLock() and dev->Unlock() at a higher level,
27  *        or use the xxx_device() equivalents.  By moving the
28  *        thread synchronization to a higher level, we permit
29  *        the higher level routines to "seize" the device and
30  *        to carry out operations without worrying about who
31  *        set what lock (i.e. race conditions).
32  *
33  *     Note, this is the device dependent code, and may have
34  *           to be modified for each system, but is meant to
35  *           be as "generic" as possible.
36  *
37  *     The purpose of this code is to develop a SIMPLE Storage
38  *     daemon. More complicated coding (double buffering, writer
39  *     thread, ...) is left for a later version.
40  */
41
42 /*
43  * Handling I/O errors and end of tape conditions are a bit tricky.
44  * This is how it is currently done when writing.
45  * On either an I/O error or end of tape,
46  * we will stop writing on the physical device (no I/O recovery is
47  * attempted at least in this daemon). The state flag will be sent
48  * to include ST_EOT, which is ephemeral, and ST_WEOT, which is
49  * persistent. Lots of routines clear ST_EOT, but ST_WEOT is
50  * cleared only when the problem goes away.  Now when ST_WEOT
51  * is set all calls to write_block_to_device() call the fix_up
52  * routine. In addition, all threads are blocked
53  * from writing on the tape by calling lock_dev(), and thread other
54  * than the first thread to hit the EOT will block on a condition
55  * variable. The first thread to hit the EOT will continue to
56  * be able to read and write the tape (he sort of tunnels through
57  * the locking mechanism -- see lock_dev() for details).
58  *
59  * Now presumably somewhere higher in the chain of command
60  * (device.c), someone will notice the EOT condition and
61  * get a new tape up, get the tape label read, and mark
62  * the label for rewriting. Then this higher level routine
63  * will write the unwritten buffer to the new volume.
64  * Finally, he will release
65  * any blocked threads by doing a broadcast on the condition
66  * variable.  At that point, we should be totally back in
67  * business with no lost data.
68  */
69
70 #include "bacula.h"
71 #include "stored.h"
72
73 #ifndef O_NONBLOCK
74 #define O_NONBLOCK 0
75 #endif
76
77 static const int dbglvl = 150;
78
79 /* Imported functions */
80 extern void set_os_device_parameters(DCR *dcr);
81 extern bool dev_get_os_pos(DEVICE *dev, struct mtget *mt_stat);
82 extern uint32_t status_dev(DEVICE *dev);
83
84 /* Forward referenced functions */
85 const char *mode_to_str(int mode);
86 DEVICE *m_init_dev(JCR *jcr, DEVRES *device, bool adata);
87
88 /*
89  * Device specific initialization.
90  */
91 void DEVICE::device_specific_init(JCR *jcr, DEVRES *device)
92 {
93 }
94
95 /*
96  * Initialize the device with the operating system and
97  * initialize buffer pointers.
98  *
99  * Returns:  true  device already open
100  *           false device setup but not open
101  *
102  * Note, for a tape, the VolName is the name we give to the
103  *    volume (not really used here), but for a file, the
104  *    VolName represents the name of the file to be created/opened.
105  *    In the case of a file, the full name is the device name
106  *    (archive_name) with the VolName concatenated.
107  *
108  * This is generic common code. It should be called prior to any
109  *     device specific code.  Note! This does not open anything.
110  */
111 bool DEVICE::open_device(DCR *dcr, int omode)
112 {
113    Enter(dbglvl);
114    preserve = 0;
115    ASSERT2(!adata, "Attempt to open adata dev");
116    if (is_open()) {
117       if (openmode == omode) {
118          return true;
119       } else {
120          Dmsg1(200, "Close fd=%d for mode change in open().\n", m_fd);
121          d_close(m_fd);
122          clear_opened();
123          preserve = state & (ST_LABEL|ST_APPEND|ST_READ);
124       }
125    }
126    openmode = omode;
127    if (dcr) {
128       dcr->setVolCatName(dcr->VolumeName);
129       VolCatInfo = dcr->VolCatInfo;    /* structure assign */
130    }
131
132    state &= ~(ST_NOSPACE|ST_LABEL|ST_APPEND|ST_READ|ST_EOT|ST_WEOT|ST_EOF);
133    label_type = B_BACULA_LABEL;
134
135    if (openmode == OPEN_READ_WRITE && has_cap(CAP_STREAM)) {
136       openmode = OPEN_WRITE_ONLY;
137    }
138    return false;
139 }
140
141 void DEVICE::set_mode(int new_mode)
142 {
143    switch (new_mode) {
144    case CREATE_READ_WRITE:
145       mode = O_CREAT | O_RDWR | O_BINARY;
146       break;
147    case OPEN_READ_WRITE:
148       mode = O_RDWR | O_BINARY;
149       break;
150    case OPEN_READ_ONLY:
151       mode = O_RDONLY | O_BINARY;
152       break;
153    case OPEN_WRITE_ONLY:
154       mode = O_WRONLY | O_BINARY;
155       break;
156    default:
157       Jmsg0(NULL, M_ABORT, 0, _("Illegal mode given to open dev.\n"));
158    }
159 }
160
161 /*
162  * Called to indicate that we have just read an
163  *  EOF from the device.
164  */
165 void DEVICE::set_ateof()
166 {
167    set_eof();
168    file_addr = 0;
169    file_size = 0;
170    block_num = 0;
171 }
172
173 /*
174  * Called to indicate we are now at the end of the tape, and
175  *   writing is not possible.
176  */
177 void DEVICE::set_ateot()
178 {
179    /* Make tape effectively read-only */
180    Dmsg0(200, "==== Set AtEof\n");
181    state |= (ST_EOF|ST_EOT|ST_WEOT);
182    clear_append();
183 }
184
185
186 /*
187  * Set the position of the device -- only for files
188  *   For other devices, there is no generic way to do it.
189  *  Returns: true  on succes
190  *           false on error
191  */
192 bool DEVICE::update_pos(DCR *dcr)
193 {
194    boffset_t pos;
195    bool ok = true;
196
197    if (!is_open()) {
198       dev_errno = EBADF;
199       Mmsg0(errmsg, _("Bad device call. Device not open\n"));
200       Emsg1(M_FATAL, 0, "%s", errmsg);
201       return false;
202    }
203
204    if (is_file()) {
205       file = 0;
206       file_addr = 0;
207       pos = lseek(dcr, (boffset_t)0, SEEK_CUR);
208       if (pos < 0) {
209          berrno be;
210          dev_errno = errno;
211          Pmsg1(000, _("Seek error: ERR=%s\n"), be.bstrerror());
212          Mmsg2(errmsg, _("lseek error on %s. ERR=%s.\n"),
213                print_name(), be.bstrerror());
214          ok = false;
215       } else {
216          file_addr = pos;
217          block_num = (uint32_t)pos;
218          file = (uint32_t)(pos >> 32);
219       }
220    }
221    return ok;
222 }
223
224 void DEVICE::set_slot(int32_t slot)
225 {
226    m_slot = slot;
227    if (vol) vol->clear_slot();
228 }
229
230 void DEVICE::clear_slot()
231 {
232    m_slot = -1;
233    if (vol) vol->set_slot(-1);
234 }
235
236 /*
237  * Set to unload the current volume in the drive
238  */
239 void DEVICE::set_unload()
240 {
241    if (!m_unload && VolHdr.VolumeName[0] != 0) {
242        m_unload = true;
243        notify_newvol_in_attached_dcrs(NULL);
244    }
245 }
246
247 /*
248  * Clear volume header
249  */
250 void DEVICE::clear_volhdr()
251 {
252    Dmsg1(100, "Clear volhdr vol=%s\n", VolHdr.VolumeName);
253    memset(&VolHdr, 0, sizeof(VolHdr));
254    setVolCatInfo(false);
255 }
256
257 void DEVICE::set_volcatinfo_from_dcr(DCR *dcr)
258 {
259    VolCatInfo = dcr->VolCatInfo;
260 }
261
262 /*
263  * Close the device
264  *   Can enter with dcr==NULL
265  */
266 bool DEVICE::close(DCR *dcr)
267 {
268    bool ok = true;
269
270    Dmsg5(40, "close_dev vol=%s fd=%d dev=%p adata=%d dev=%s\n",
271       VolHdr.VolumeName, m_fd, this, adata, print_name());
272    offline_or_rewind(dcr);
273
274    if (!is_open()) {
275       Dmsg2(200, "device %s already closed vol=%s\n", print_name(),
276          VolHdr.VolumeName);
277       return true;                    /* already closed */
278    }
279
280    switch (dev_type) {
281    case B_VTL_DEV:
282    case B_VTAPE_DEV:
283    case B_TAPE_DEV:
284       unlock_door();
285       /* Fall through wanted */
286    default:
287       if (d_close(m_fd) != 0) {
288          berrno be;
289          dev_errno = errno;
290          Mmsg2(errmsg, _("Error closing device %s. ERR=%s.\n"),
291                print_name(), be.bstrerror());
292          ok = false;
293       }
294       break;
295    }
296
297    unmount(1);                       /* do unmount if required */
298
299    /* Clean up device packet so it can be reused */
300    clear_opened();
301
302    state &= ~(ST_LABEL|ST_READ|ST_APPEND|ST_EOT|ST_WEOT|ST_EOF|
303               ST_NOSPACE|ST_MOUNTED|ST_MEDIA|ST_SHORT);
304    label_type = B_BACULA_LABEL;
305    file = block_num = 0;
306    file_size = 0;
307    file_addr = 0;
308    EndFile = EndBlock = 0;
309    openmode = 0;
310    clear_volhdr();
311    memset(&VolCatInfo, 0, sizeof(VolCatInfo));
312    if (tid) {
313       stop_thread_timer(tid);
314       tid = 0;
315    }
316    return ok;
317 }
318
319 /*
320  * If timeout, wait until the mount command returns 0.
321  * If !timeout, try to mount the device only once.
322  */
323 bool DEVICE::mount(int timeout)
324 {
325    Enter(dbglvl);
326    if (!is_mounted() && device->mount_command) {
327       return mount_file(1, timeout);
328    }
329    return true;
330 }
331
332 /*
333  * Unmount the device
334  * If timeout, wait until the unmount command returns 0.
335  * If !timeout, try to unmount the device only once.
336  */
337 bool DEVICE::unmount(int timeout)
338 {
339    Enter(dbglvl);
340    if (is_mounted() && requires_mount() && device->unmount_command) {
341       return mount_file(0, timeout);
342    }
343    return true;
344 }
345
346
347 /*
348  * Edit codes into (Un)MountCommand, Write(First)PartCommand
349  *  %% = %
350  *  %a = archive device name
351  *  %e = erase (set if cannot mount and first part)
352  *  %n = part number
353  *  %m = mount point
354  *  %v = last part name
355  *
356  *  omsg = edited output message
357  *  imsg = input string containing edit codes (%x)
358  *
359  */
360 void DEVICE::edit_mount_codes(POOL_MEM &omsg, const char *imsg)
361 {
362    const char *p;
363    const char *str;
364    char add[20];
365
366    POOL_MEM archive_name(PM_FNAME);
367
368    omsg.c_str()[0] = 0;
369    Dmsg1(800, "edit_mount_codes: %s\n", imsg);
370    for (p=imsg; *p; p++) {
371       if (*p == '%') {
372          switch (*++p) {
373          case '%':
374             str = "%";
375             break;
376          case 'a':
377             str = dev_name;
378             break;
379          case 'e':
380             str = "0";
381             /* ***FIXME*** this may be useful for Cloud */
382 #ifdef xxx
383             if (num_dvd_parts == 0) {
384                if (truncating || blank_dvd) {
385                   str = "2";
386                } else {
387                   str = "1";
388                }
389             } else {
390                str = "0";
391             }
392 #endif
393             break;
394          case 'n':
395             bsnprintf(add, sizeof(add), "%d", part);
396             str = add;
397             break;
398          case 'm':
399             str = device->mount_point;
400             break;
401          default:
402             add[0] = '%';
403             add[1] = *p;
404             add[2] = 0;
405             str = add;
406             break;
407          }
408       } else {
409          add[0] = *p;
410          add[1] = 0;
411          str = add;
412       }
413       Dmsg1(1900, "add_str %s\n", str);
414       pm_strcat(omsg, (char *)str);
415       Dmsg1(1800, "omsg=%s\n", omsg.c_str());
416    }
417 }
418
419 /* return the last timer interval (ms)
420  * or 0 if something goes wrong
421  */
422 btime_t DEVICE::get_timer_count()
423 {
424    btime_t temp = last_timer;
425    last_timer = get_current_btime();
426    temp = last_timer - temp;   /* get elapsed time */
427    return (temp>0)?temp:0;     /* take care of skewed clock */
428 }
429
430 /* read from fd */
431 ssize_t DEVICE::read(void *buf, size_t len)
432 {
433    ssize_t read_len ;
434
435    get_timer_count();
436
437    read_len = d_read(m_fd, buf, len);
438
439    last_tick = get_timer_count();
440
441    DevReadTime += last_tick;
442    VolCatInfo.VolReadTime += last_tick;
443
444    if (read_len > 0) {          /* skip error */
445       DevReadBytes += read_len;
446    }
447
448    return read_len;
449 }
450
451 /* write to fd */
452 ssize_t DEVICE::write(const void *buf, size_t len)
453 {
454    ssize_t write_len;
455
456    get_timer_count();
457
458    write_len = d_write(m_fd, buf, len);
459
460    last_tick = get_timer_count();
461
462    DevWriteTime += last_tick;
463    VolCatInfo.VolWriteTime += last_tick;
464
465    if (write_len > 0) {         /* skip error */
466       DevWriteBytes += write_len;
467    }
468
469    return write_len;
470 }
471
472 /* Return the resource name for the device */
473 const char *DEVICE::name() const
474 {
475    return device->hdr.name;
476 }
477
478 uint32_t DEVICE::get_file()
479 {
480    if (is_tape()) {
481       return file;
482    } else {
483       uint64_t bytes = VolCatInfo.VolCatAdataBytes + VolCatInfo.VolCatAmetaBytes;
484       return (uint32_t)(bytes >> 32);
485    }
486 }
487
488 uint32_t DEVICE::get_block_num()
489 {
490    if (is_tape()) {
491       return block_num;
492    } else {
493       return  VolCatInfo.VolCatAdataBlocks + VolCatInfo.VolCatAmetaBlocks;
494    }
495 }
496
497 /*
498  * Walk through all attached jcrs indicating the volume has changed
499  *   Note: If you have the new VolumeName, it is passed here,
500  *     otherwise pass a NULL.
501  */
502 void
503 DEVICE::notify_newvol_in_attached_dcrs(const char *newVolumeName)
504 {
505    Dmsg2(140, "Notify dcrs of vol change. oldVolume=%s NewVolume=%s\n",
506       getVolCatName(), newVolumeName?newVolumeName:"*None*");
507    Lock_dcrs();
508    DCR *mdcr;
509    foreach_dlist(mdcr, attached_dcrs) {
510       if (mdcr->jcr->JobId == 0) {
511          continue;                 /* ignore console */
512       }
513       mdcr->NewVol = true;
514       mdcr->NewFile = true;
515       if (newVolumeName && mdcr->VolumeName != newVolumeName) {
516          bstrncpy(mdcr->VolumeName, newVolumeName, sizeof(mdcr->VolumeName));
517          Dmsg2(140, "Set NewVol=%s in JobId=%d\n", mdcr->VolumeName, mdcr->jcr->JobId);
518       }
519    }
520    Unlock_dcrs();
521 }
522
523 /*
524  * Walk through all attached jcrs indicating the File has changed
525  */
526 void
527 DEVICE::notify_newfile_in_attached_dcrs()
528 {
529    Dmsg1(140, "Notify dcrs of file change. Volume=%s\n", getVolCatName());
530    Lock_dcrs();
531    DCR *mdcr;
532    foreach_dlist(mdcr, attached_dcrs) {
533       if (mdcr->jcr->JobId == 0) {
534          continue;                 /* ignore console */
535       }
536       Dmsg1(140, "Notify JobI=%d\n", mdcr->jcr->JobId);
537       mdcr->NewFile = true;
538    }
539    Unlock_dcrs();
540 }
541
542
543
544 /*
545  * Free memory allocated for the device
546  *  Can enter with dcr==NULL
547  */
548 void DEVICE::term(DCR *dcr)
549 {
550    Dmsg1(900, "term dev: %s\n", print_name());
551    if (!dcr) {
552       d_close(m_fd);
553    } else {
554       close(dcr);
555    }
556    if (dev_name) {
557       free_memory(dev_name);
558       dev_name = NULL;
559    }
560    if (adev_name) {
561       free_memory(adev_name);
562       adev_name = NULL;
563    }
564    if (prt_name) {
565       free_memory(prt_name);
566       prt_name = NULL;
567    }
568    if (errmsg) {
569       free_pool_memory(errmsg);
570       errmsg = NULL;
571    }
572    pthread_mutex_destroy(&m_mutex);
573    pthread_cond_destroy(&wait);
574    pthread_cond_destroy(&wait_next_vol);
575    pthread_mutex_destroy(&spool_mutex);
576    pthread_mutex_destroy(&freespace_mutex);
577    if (attached_dcrs) {
578       delete attached_dcrs;
579       attached_dcrs = NULL;
580    }
581    /* We let the DEVRES pointer if not our device */
582    if (device && device->dev == this) {
583       device->dev = NULL;
584    }
585    delete this;
586 }
587
588 /* Get freespace values */
589 void DEVICE::get_freespace(uint64_t *freeval, uint64_t *totalval)
590 {
591    get_os_device_freespace();
592    P(freespace_mutex);
593    if (is_freespace_ok()) {
594       *freeval = free_space;
595       *totalval = total_space;
596    } else {
597       *freeval = *totalval = 0;
598    }
599    V(freespace_mutex);
600 }
601
602 /* Set freespace values */
603 void DEVICE::set_freespace(uint64_t freeval, uint64_t totalval, int errnoval, bool valid)
604 {
605    P(freespace_mutex);
606    free_space = freeval;
607    total_space = totalval;
608    free_space_errno = errnoval;
609    if (valid) {
610       set_freespace_ok();
611    } else {
612       clear_freespace_ok();
613    }
614    V(freespace_mutex);
615 }
616
617 /* Convenient function that return true only  if the device back-end is a
618  * filesystem that its nearly full. (the free space is below the given threshold)
619  */
620 bool DEVICE::is_fs_nearly_full(uint64_t threshold)
621 {
622    uint64_t freeval, totalval;
623    if (is_file()) {
624       get_freespace(&freeval, &totalval);
625       if (totalval > 0) {
626          if (freeval < threshold) {
627             return true;
628          }
629       }
630    }
631    return false;
632 }
633
634 static const char *modes[] = {
635    "CREATE_READ_WRITE",
636    "OPEN_READ_WRITE",
637    "OPEN_READ_ONLY",
638    "OPEN_WRITE_ONLY"
639 };
640
641
642 const char *mode_to_str(int mode)
643 {
644    static char buf[100];
645    if (mode < 1 || mode > 4) {
646       bsnprintf(buf, sizeof(buf), "BAD mode=%d", mode);
647       return buf;
648     }
649    return modes[mode-1];
650 }
651
652 void DEVICE::setVolCatName(const char *name)
653 {
654    bstrncpy(VolCatInfo.VolCatName, name, sizeof(VolCatInfo.VolCatName));
655    setVolCatInfo(false);
656 }
657
658 void DEVICE::setVolCatStatus(const char *status)
659 {
660    bstrncpy(VolCatInfo.VolCatStatus, status, sizeof(VolCatInfo.VolCatStatus));
661    setVolCatInfo(false);
662 }
663
664 void DEVICE::updateVolCatBytes(uint64_t bytes)
665 {
666    Lock_VolCatInfo();
667    VolCatInfo.VolCatAmetaBytes += bytes;
668    VolCatInfo.VolCatBytes += bytes;
669    setVolCatInfo(false);
670    Unlock_VolCatInfo();
671 }
672
673 void DEVICE::updateVolCatHoleBytes(uint64_t hole)
674 {
675    return;
676 }
677
678 void DEVICE::updateVolCatPadding(uint64_t padding)
679 {
680    Lock_VolCatInfo();
681    VolCatInfo.VolCatAmetaPadding += padding;
682    VolCatInfo.VolCatPadding += padding;
683    setVolCatInfo(false);
684    Unlock_VolCatInfo();
685 }
686
687
688 void DEVICE::updateVolCatBlocks(uint32_t blocks)
689 {
690    Lock_VolCatInfo();
691    VolCatInfo.VolCatAmetaBlocks += blocks;
692    VolCatInfo.VolCatBlocks += blocks;
693    setVolCatInfo(false);
694    Unlock_VolCatInfo();
695 }
696
697 void DEVICE::updateVolCatWrites(uint32_t writes)
698 {
699    Lock_VolCatInfo();
700    VolCatInfo.VolCatAmetaWrites += writes;
701    VolCatInfo.VolCatWrites += writes;
702    setVolCatInfo(false);
703    Unlock_VolCatInfo();
704 }
705
706 void DEVICE::updateVolCatReads(uint32_t reads)
707 {
708    Lock_VolCatInfo();
709    VolCatInfo.VolCatAmetaReads += reads;
710    VolCatInfo.VolCatReads += reads;
711    setVolCatInfo(false);
712    Unlock_VolCatInfo();
713 }
714
715 void DEVICE::updateVolCatReadBytes(uint64_t bytes)
716 {
717    Lock_VolCatInfo();
718    VolCatInfo.VolCatAmetaRBytes += bytes;
719    VolCatInfo.VolCatRBytes += bytes;
720    setVolCatInfo(false);
721    Unlock_VolCatInfo();
722 }
723
724 void DEVICE::set_nospace()
725 {
726    state |= ST_NOSPACE;
727 }
728
729 void DEVICE::clear_nospace()
730 {
731    state &= ~ST_NOSPACE;
732 }
733
734 /* Put device in append mode */
735 void DEVICE::set_append()
736 {
737    state &= ~(ST_NOSPACE|ST_READ|ST_EOT|ST_EOF|ST_WEOT);  /* remove EOF/EOT flags */
738    state |= ST_APPEND;
739 }
740
741 /* Clear append mode */
742 void DEVICE::clear_append()
743 {
744    state &= ~ST_APPEND;
745 }
746
747 /* Put device in read mode */
748 void DEVICE::set_read()
749 {
750    state &= ~(ST_APPEND|ST_EOT|ST_EOF|ST_WEOT);  /* remove EOF/EOT flags */
751    state |= ST_READ;
752 }
753
754 /* Clear read mode */
755 void DEVICE::clear_read()
756 {
757    state &= ~ST_READ;
758 }
759
760 /*
761  * Get freespace using OS calls
762  * TODO: See if it's working with mount commands
763  */
764 bool DEVICE::get_os_device_freespace()
765 {
766    int64_t freespace, totalspace;
767
768    if (!is_file()) {
769       return true;
770    }
771    if (fs_get_free_space(dev_name, &freespace, &totalspace) == 0) {
772       set_freespace(freespace,  totalspace, 0, true);
773       Mmsg(errmsg, "");
774       return true;
775
776    } else {
777       set_freespace(0, 0, 0, false); /* No valid freespace */
778    }
779    return false;
780 }
781
782 /* Update the free space on the device */
783 bool DEVICE::update_freespace()
784 {
785    POOL_MEM ocmd(PM_FNAME);
786    POOLMEM* results;
787    char* icmd;
788    char* p;
789    uint64_t free, total;
790    char ed1[50];
791    bool ok = false;
792    int status;
793    berrno be;
794
795    if (!is_file()) {
796       Mmsg(errmsg, "");
797       return true;
798    }
799
800    /* The device must be mounted in order for freespace to work */
801    if (requires_mount()) {
802       mount(1);
803    }
804
805    if (get_os_device_freespace()) {
806       Dmsg4(20, "get_os_device_freespace: free_space=%s freespace_ok=%d free_space_errno=%d have_media=%d\n",
807          edit_uint64(free_space, ed1), !!is_freespace_ok(), free_space_errno, !!have_media());
808       return true;
809    }
810
811    icmd = device->free_space_command;
812
813    if (!icmd) {
814       set_freespace(0, 0, 0, false);
815       Dmsg2(20, "ERROR: update_free_space_dev: free_space=%s, free_space_errno=%d (!icmd)\n",
816             edit_uint64(free_space, ed1), free_space_errno);
817       Mmsg(errmsg, _("No FreeSpace command defined.\n"));
818       return false;
819    }
820
821    edit_mount_codes(ocmd, icmd);
822
823    Dmsg1(20, "update_freespace: cmd=%s\n", ocmd.c_str());
824
825    results = get_pool_memory(PM_MESSAGE);
826
827    Dmsg1(20, "Run freespace prog=%s\n", ocmd.c_str());
828    status = run_program_full_output(ocmd.c_str(), max_open_wait/2, results);
829    Dmsg2(20, "Freespace status=%d result=%s\n", status, results);
830    /* Should report "1223232 12323232\n"  "free  total\n" */
831    if (status == 0) {
832       free = str_to_int64(results) * 1024;
833       p = results;
834
835       if (skip_nonspaces(&p)) {
836          total = str_to_int64(p) * 1024;
837
838       } else {
839          total = 0;
840       }
841
842       Dmsg1(400, "Free space program run: Freespace=%s\n", results);
843       if (free >= 0) {
844          set_freespace(free, total, 0, true); /* have valid freespace */
845          Mmsg(errmsg, "");
846          ok = true;
847       }
848    } else {
849       set_freespace(0, 0, EPIPE, false); /* no valid freespace */
850       Mmsg2(errmsg, _("Cannot run free space command. Results=%s ERR=%s\n"),
851             results, be.bstrerror(status));
852
853       dev_errno = free_space_errno;
854       Dmsg4(20, "Cannot get free space on device %s. free_space=%s, "
855          "free_space_errno=%d ERR=%s\n",
856             print_name(), edit_uint64(free_space, ed1),
857             free_space_errno, errmsg);
858    }
859    free_pool_memory(results);
860    Dmsg4(20, "leave update_freespace: free_space=%s freespace_ok=%d free_space_errno=%d have_media=%d\n",
861       edit_uint64(free_space, ed1), !!is_freespace_ok(), free_space_errno, !!have_media());
862    return ok;
863 }
864
865 bool DEVICE::weof(DCR */*dcr*/, int num)
866 {
867    Dmsg1(129, "=== weof_dev=%s\n", print_name());
868
869    if (!is_open()) {
870       dev_errno = EBADF;
871       Mmsg0(errmsg, _("Bad call to weof_dev. Device not open\n"));
872       Emsg0(M_FATAL, 0, errmsg);
873       return false;
874    }
875
876    if (!can_append()) {
877       Mmsg0(errmsg, _("Attempt to WEOF on non-appendable Volume\n"));
878       Emsg0(M_FATAL, 0, errmsg);
879       return false;
880    }
881
882    file_size = 0;
883    return true;
884 }
885
886 /*
887  * Very basic functions -- no device specific code.
888  *  Returns: true  on succes
889  *           false on error
890  */
891 bool DEVICE::eod(DCR *dcr)
892 {
893    bool ok = true;
894
895    Enter(dbglvl);
896    if (m_fd < 0) {
897       dev_errno = EBADF;
898       Mmsg1(errmsg, _("Bad call to eod. Device %s not open\n"), print_name());
899       Dmsg1(100, "%s", errmsg);
900       return false;
901    }
902
903    if (at_eot()) {
904       Leave(100);
905       return true;
906    }
907    clear_eof();         /* remove EOF flag */
908    block_num = file = 0;
909    file_size = 0;
910    file_addr = 0;
911    Leave(100);
912    return ok;
913 }
914
915 bool DEVICE::is_eod_valid(DCR *dcr)
916 {
917    return true;
918 }
919
920 bool DEVICE::open_next_part(DCR */*dcr*/)
921 {
922    return true;
923 }
924
925 bool DEVICE::close_part(DCR */*dcr*/)
926 {
927    return true;
928 }
929
930 DEVICE *DEVICE::get_dev(DCR */*dcr*/)
931 {
932    return this;
933 }
934
935 uint32_t DEVICE::get_hi_addr()
936 {
937    return (uint32_t)(file_addr >> 32);
938 }
939
940 uint32_t DEVICE::get_hi_addr(boffset_t addr)
941 {
942    return (uint32_t)(addr >> 32);
943 }
944
945 uint32_t DEVICE::get_low_addr()
946 {
947    return (uint32_t)(file_addr);
948 }
949
950 uint32_t DEVICE::get_low_addr(boffset_t addr)
951 {
952    return (uint32_t)(addr);
953 }
954
955
956 uint64_t DEVICE::get_full_addr()
957 {
958    return file_addr;
959 }
960
961 uint64_t DEVICE::get_full_addr(boffset_t addr)
962 {
963    return addr;
964 }
965
966
967 uint64_t DEVICE::get_full_addr(uint32_t hi, uint32_t low)
968 {
969    return ((uint64_t)hi)<<32 | (uint64_t)low;
970 }
971
972 /* Note: this subroutine is not in the class */
973 uint64_t get_full_addr(uint32_t hi, uint32_t low)
974 {
975    return ((uint64_t)hi)<<32 | (uint64_t)low;
976 }
977
978 /* Print the file address */
979 char *DEVICE::print_addr(char *buf, int32_t buf_len)
980 {
981    buf[0] = 0;
982    bsnprintf(buf, buf_len, "%llu", get_full_addr());
983    return buf;
984 }
985
986 char *DEVICE::print_addr(char *buf, int32_t buf_len, boffset_t addr)
987 {
988    buf[0] = 0;
989    bsnprintf(buf, buf_len, "%llu", addr);
990    return buf;
991 }
992
993
994 bool DEVICE::do_size_checks(DCR *dcr, DEV_BLOCK *block)
995 {
996    JCR *jcr = dcr->jcr;
997
998    if (is_user_volume_size_reached(dcr, true)) {
999       Dmsg0(40, "Calling terminate_writing_volume\n");
1000       terminate_writing_volume(dcr);
1001       reread_last_block(dcr);   /* Only used on tapes */
1002       dev_errno = ENOSPC;
1003       return false;
1004    }
1005
1006    /*
1007     * Limit maximum File size on volume to user specified value.
1008     *  In practical terms, this means to put an EOF mark on
1009     *  a tape after every X bytes. This effectively determines
1010     *  how many index records we have (JobMedia). If you set
1011     *  max_file_size too small, it will cause a lot of shoe-shine
1012     *  on very fast modern tape (LTO-3 and above).
1013     */
1014    if ((max_file_size > 0) &&
1015        (file_size+block->binbuf) >= max_file_size) {
1016       file_size = 0;             /* reset file size */
1017
1018       if (!weof(dcr, 1)) {            /* write eof */
1019          Dmsg0(50, "WEOF error in max file size.\n");
1020          Jmsg(jcr, M_FATAL, 0, _("Unable to write EOF. ERR=%s\n"),
1021             bstrerror());
1022          Dmsg0(40, "Calling terminate_writing_volume\n");
1023          terminate_writing_volume(dcr);
1024          dev_errno = ENOSPC;
1025          return false;
1026       }
1027
1028       if (!do_new_file_bookkeeping(dcr)) {
1029          /* Error message already sent */
1030          return false;
1031       }
1032    }
1033    return true;
1034 }
1035
1036 bool DEVICE::get_tape_alerts(DCR *dcr)
1037 {
1038    return true;
1039 }
1040
1041 void DEVICE::show_tape_alerts(DCR *dcr, alert_list_type type,
1042        alert_list_which which, alert_cb alert_callback)
1043 {
1044    return;
1045 }
1046
1047 int DEVICE::delete_alerts()
1048 {
1049    return 0;
1050 }