2 Bacula(R) - The Network Backup Solution
4 Copyright (C) 2000-2017 Kern Sibbald
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.
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.
14 This notice must be preserved when any source code is
15 conveyed and/or propagated.
17 Bacula(R) is a registered trademark of Kern Sibbald.
21 * dev.c -- low level operations on device (storage device)
23 * written by, Kern Sibbald, MM
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).
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.
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.
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).
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.
77 static const int dbglvl = 150;
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);
84 /* Forward referenced functions */
85 const char *mode_to_str(int mode);
86 DEVICE *m_init_dev(JCR *jcr, DEVRES *device, bool adata);
89 * Device specific initialization.
91 void DEVICE::device_specific_init(JCR *jcr, DEVRES *device)
96 * Initialize the device with the operating system and
97 * initialize buffer pointers.
99 * Returns: true device already open
100 * false device setup but not open
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.
108 * This is generic common code. It should be called prior to any
109 * device specific code. Note! This does not open anything.
111 bool DEVICE::open_device(DCR *dcr, int omode)
115 ASSERT2(!adata, "Attempt to open adata dev");
117 if (openmode == omode) {
120 Dmsg1(200, "Close fd=%d for mode change in open().\n", m_fd);
123 preserve = state & (ST_LABEL|ST_APPEND|ST_READ);
128 dcr->setVolCatName(dcr->VolumeName);
129 VolCatInfo = dcr->VolCatInfo; /* structure assign */
132 state &= ~(ST_NOSPACE|ST_LABEL|ST_APPEND|ST_READ|ST_EOT|ST_WEOT|ST_EOF);
133 label_type = B_BACULA_LABEL;
135 if (openmode == OPEN_READ_WRITE && has_cap(CAP_STREAM)) {
136 openmode = OPEN_WRITE_ONLY;
141 void DEVICE::set_mode(int new_mode)
144 case CREATE_READ_WRITE:
145 mode = O_CREAT | O_RDWR | O_BINARY;
147 case OPEN_READ_WRITE:
148 mode = O_RDWR | O_BINARY;
151 mode = O_RDONLY | O_BINARY;
153 case OPEN_WRITE_ONLY:
154 mode = O_WRONLY | O_BINARY;
157 Jmsg0(NULL, M_ABORT, 0, _("Illegal mode given to open dev.\n"));
162 * Called to indicate that we have just read an
163 * EOF from the device.
165 void DEVICE::set_ateof()
174 * Called to indicate we are now at the end of the tape, and
175 * writing is not possible.
177 void DEVICE::set_ateot()
179 /* Make tape effectively read-only */
180 Dmsg0(200, "==== Set AtEof\n");
181 state |= (ST_EOF|ST_EOT|ST_WEOT);
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
192 bool DEVICE::update_pos(DCR *dcr)
199 Mmsg0(errmsg, _("Bad device call. Device not open\n"));
200 Emsg1(M_FATAL, 0, "%s", errmsg);
207 pos = lseek(dcr, (boffset_t)0, SEEK_CUR);
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());
217 block_num = (uint32_t)pos;
218 file = (uint32_t)(pos >> 32);
224 void DEVICE::set_slot(int32_t slot)
227 if (vol) vol->clear_slot();
230 void DEVICE::clear_slot()
233 if (vol) vol->set_slot(-1);
237 * Set to unload the current volume in the drive
239 void DEVICE::set_unload()
241 if (!m_unload && VolHdr.VolumeName[0] != 0) {
243 notify_newvol_in_attached_dcrs(NULL);
248 * Clear volume header
250 void DEVICE::clear_volhdr()
252 Dmsg1(100, "Clear volhdr vol=%s\n", VolHdr.VolumeName);
253 memset(&VolHdr, 0, sizeof(VolHdr));
254 setVolCatInfo(false);
257 void DEVICE::set_volcatinfo_from_dcr(DCR *dcr)
259 VolCatInfo = dcr->VolCatInfo;
264 * Can enter with dcr==NULL
266 bool DEVICE::close(DCR *dcr)
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);
275 Dmsg2(200, "device %s already closed vol=%s\n", print_name(),
277 return true; /* already closed */
285 /* Fall through wanted */
287 if (d_close(m_fd) != 0) {
290 Mmsg2(errmsg, _("Error closing device %s. ERR=%s.\n"),
291 print_name(), be.bstrerror());
297 unmount(1); /* do unmount if required */
299 /* Clean up device packet so it can be reused */
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;
308 EndFile = EndBlock = 0;
311 memset(&VolCatInfo, 0, sizeof(VolCatInfo));
313 stop_thread_timer(tid);
320 * If timeout, wait until the mount command returns 0.
321 * If !timeout, try to mount the device only once.
323 bool DEVICE::mount(int timeout)
326 if (!is_mounted() && device->mount_command) {
327 return mount_file(1, timeout);
334 * If timeout, wait until the unmount command returns 0.
335 * If !timeout, try to unmount the device only once.
337 bool DEVICE::unmount(int timeout)
340 if (is_mounted() && requires_mount() && device->unmount_command) {
341 return mount_file(0, timeout);
348 * Edit codes into (Un)MountCommand, Write(First)PartCommand
350 * %a = archive device name
351 * %e = erase (set if cannot mount and first part)
354 * %v = last part name
356 * omsg = edited output message
357 * imsg = input string containing edit codes (%x)
360 void DEVICE::edit_mount_codes(POOL_MEM &omsg, const char *imsg)
366 POOL_MEM archive_name(PM_FNAME);
369 Dmsg1(800, "edit_mount_codes: %s\n", imsg);
370 for (p=imsg; *p; p++) {
381 /* ***FIXME*** this may be useful for Cloud */
383 if (num_dvd_parts == 0) {
384 if (truncating || blank_dvd) {
395 bsnprintf(add, sizeof(add), "%d", part);
399 str = device->mount_point;
413 Dmsg1(1900, "add_str %s\n", str);
414 pm_strcat(omsg, (char *)str);
415 Dmsg1(1800, "omsg=%s\n", omsg.c_str());
419 /* return the last timer interval (ms)
420 * or 0 if something goes wrong
422 btime_t DEVICE::get_timer_count()
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 */
431 ssize_t DEVICE::read(void *buf, size_t len)
437 read_len = d_read(m_fd, buf, len);
439 last_tick = get_timer_count();
441 DevReadTime += last_tick;
442 VolCatInfo.VolReadTime += last_tick;
444 if (read_len > 0) { /* skip error */
445 DevReadBytes += read_len;
452 ssize_t DEVICE::write(const void *buf, size_t len)
458 write_len = d_write(m_fd, buf, len);
460 last_tick = get_timer_count();
462 DevWriteTime += last_tick;
463 VolCatInfo.VolWriteTime += last_tick;
465 if (write_len > 0) { /* skip error */
466 DevWriteBytes += write_len;
472 /* Return the resource name for the device */
473 const char *DEVICE::name() const
475 return device->hdr.name;
478 uint32_t DEVICE::get_file()
483 uint64_t bytes = VolCatInfo.VolCatAdataBytes + VolCatInfo.VolCatAmetaBytes;
484 return (uint32_t)(bytes >> 32);
488 uint32_t DEVICE::get_block_num()
493 return VolCatInfo.VolCatAdataBlocks + VolCatInfo.VolCatAmetaBlocks;
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.
503 DEVICE::notify_newvol_in_attached_dcrs(const char *newVolumeName)
505 Dmsg2(140, "Notify dcrs of vol change. oldVolume=%s NewVolume=%s\n",
506 getVolCatName(), newVolumeName?newVolumeName:"*None*");
509 foreach_dlist(mdcr, attached_dcrs) {
510 if (mdcr->jcr->JobId == 0) {
511 continue; /* ignore console */
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);
524 * Walk through all attached jcrs indicating the File has changed
527 DEVICE::notify_newfile_in_attached_dcrs()
529 Dmsg1(140, "Notify dcrs of file change. Volume=%s\n", getVolCatName());
532 foreach_dlist(mdcr, attached_dcrs) {
533 if (mdcr->jcr->JobId == 0) {
534 continue; /* ignore console */
536 Dmsg1(140, "Notify JobI=%d\n", mdcr->jcr->JobId);
537 mdcr->NewFile = true;
545 * Free memory allocated for the device
546 * Can enter with dcr==NULL
548 void DEVICE::term(DCR *dcr)
550 Dmsg1(900, "term dev: %s\n", print_name());
557 free_memory(dev_name);
561 free_memory(adev_name);
565 free_memory(prt_name);
569 free_pool_memory(errmsg);
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);
578 delete attached_dcrs;
579 attached_dcrs = NULL;
581 /* We let the DEVRES pointer if not our device */
582 if (device && device->dev == this) {
588 /* Get freespace values */
589 void DEVICE::get_freespace(uint64_t *freeval, uint64_t *totalval)
591 get_os_device_freespace();
593 if (is_freespace_ok()) {
594 *freeval = free_space;
595 *totalval = total_space;
597 *freeval = *totalval = 0;
602 /* Set freespace values */
603 void DEVICE::set_freespace(uint64_t freeval, uint64_t totalval, int errnoval, bool valid)
606 free_space = freeval;
607 total_space = totalval;
608 free_space_errno = errnoval;
612 clear_freespace_ok();
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)
620 bool DEVICE::is_fs_nearly_full(uint64_t threshold)
622 uint64_t freeval, totalval;
624 get_freespace(&freeval, &totalval);
626 if (freeval < threshold) {
634 static const char *modes[] = {
642 const char *mode_to_str(int mode)
644 static char buf[100];
645 if (mode < 1 || mode > 4) {
646 bsnprintf(buf, sizeof(buf), "BAD mode=%d", mode);
649 return modes[mode-1];
652 void DEVICE::setVolCatName(const char *name)
654 bstrncpy(VolCatInfo.VolCatName, name, sizeof(VolCatInfo.VolCatName));
655 setVolCatInfo(false);
658 void DEVICE::setVolCatStatus(const char *status)
660 bstrncpy(VolCatInfo.VolCatStatus, status, sizeof(VolCatInfo.VolCatStatus));
661 setVolCatInfo(false);
664 void DEVICE::updateVolCatBytes(uint64_t bytes)
667 VolCatInfo.VolCatAmetaBytes += bytes;
668 VolCatInfo.VolCatBytes += bytes;
669 setVolCatInfo(false);
673 void DEVICE::updateVolCatHoleBytes(uint64_t hole)
678 void DEVICE::updateVolCatPadding(uint64_t padding)
681 VolCatInfo.VolCatAmetaPadding += padding;
682 VolCatInfo.VolCatPadding += padding;
683 setVolCatInfo(false);
688 void DEVICE::updateVolCatBlocks(uint32_t blocks)
691 VolCatInfo.VolCatAmetaBlocks += blocks;
692 VolCatInfo.VolCatBlocks += blocks;
693 setVolCatInfo(false);
697 void DEVICE::updateVolCatWrites(uint32_t writes)
700 VolCatInfo.VolCatAmetaWrites += writes;
701 VolCatInfo.VolCatWrites += writes;
702 setVolCatInfo(false);
706 void DEVICE::updateVolCatReads(uint32_t reads)
709 VolCatInfo.VolCatAmetaReads += reads;
710 VolCatInfo.VolCatReads += reads;
711 setVolCatInfo(false);
715 void DEVICE::updateVolCatReadBytes(uint64_t bytes)
718 VolCatInfo.VolCatAmetaRBytes += bytes;
719 VolCatInfo.VolCatRBytes += bytes;
720 setVolCatInfo(false);
724 void DEVICE::set_nospace()
729 void DEVICE::clear_nospace()
731 state &= ~ST_NOSPACE;
734 /* Put device in append mode */
735 void DEVICE::set_append()
737 state &= ~(ST_NOSPACE|ST_READ|ST_EOT|ST_EOF|ST_WEOT); /* remove EOF/EOT flags */
741 /* Clear append mode */
742 void DEVICE::clear_append()
747 /* Put device in read mode */
748 void DEVICE::set_read()
750 state &= ~(ST_APPEND|ST_EOT|ST_EOF|ST_WEOT); /* remove EOF/EOT flags */
754 /* Clear read mode */
755 void DEVICE::clear_read()
761 * Get freespace using OS calls
762 * TODO: See if it's working with mount commands
764 bool DEVICE::get_os_device_freespace()
766 int64_t freespace, totalspace;
771 if (fs_get_free_space(dev_name, &freespace, &totalspace) == 0) {
772 set_freespace(freespace, totalspace, 0, true);
777 set_freespace(0, 0, 0, false); /* No valid freespace */
782 /* Update the free space on the device */
783 bool DEVICE::update_freespace()
785 POOL_MEM ocmd(PM_FNAME);
789 uint64_t free, total;
800 /* The device must be mounted in order for freespace to work */
801 if (requires_mount()) {
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());
811 icmd = device->free_space_command;
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"));
821 edit_mount_codes(ocmd, icmd);
823 Dmsg1(20, "update_freespace: cmd=%s\n", ocmd.c_str());
825 results = get_pool_memory(PM_MESSAGE);
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" */
832 free = str_to_int64(results) * 1024;
835 if (skip_nonspaces(&p)) {
836 total = str_to_int64(p) * 1024;
842 Dmsg1(400, "Free space program run: Freespace=%s\n", results);
844 set_freespace(free, total, 0, true); /* have valid freespace */
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));
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);
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());
865 bool DEVICE::weof(DCR */*dcr*/, int num)
867 Dmsg1(129, "=== weof_dev=%s\n", print_name());
871 Mmsg1(errmsg, _("Bad call to weof_dev. Device %s not open\n"), print_name());
872 Emsg0(M_FATAL, 0, errmsg);
877 Mmsg1(errmsg, _("Attempt to WEOF on non-appendable Volume %s\n"), VolHdr.VolumeName);
878 Emsg0(M_FATAL, 0, errmsg);
887 * Very basic functions -- no device specific code.
888 * Returns: true on succes
891 bool DEVICE::eod(DCR *dcr)
898 Mmsg1(errmsg, _("Bad call to eod. Device %s not open\n"), print_name());
899 Dmsg1(100, "%s", errmsg);
907 clear_eof(); /* remove EOF flag */
908 block_num = file = 0;
915 bool DEVICE::is_eod_valid(DCR *dcr)
920 bool DEVICE::open_next_part(DCR */*dcr*/)
925 bool DEVICE::close_part(DCR */*dcr*/)
930 DEVICE *DEVICE::get_dev(DCR */*dcr*/)
935 uint32_t DEVICE::get_hi_addr()
937 return (uint32_t)(file_addr >> 32);
940 uint32_t DEVICE::get_hi_addr(boffset_t addr)
942 return (uint32_t)(addr >> 32);
945 uint32_t DEVICE::get_low_addr()
947 return (uint32_t)(file_addr);
950 uint32_t DEVICE::get_low_addr(boffset_t addr)
952 return (uint32_t)(addr);
956 uint64_t DEVICE::get_full_addr()
961 uint64_t DEVICE::get_full_addr(boffset_t addr)
967 uint64_t DEVICE::get_full_addr(uint32_t hi, uint32_t low)
969 return ((uint64_t)hi)<<32 | (uint64_t)low;
972 /* Note: this subroutine is not in the class */
973 uint64_t get_full_addr(uint32_t hi, uint32_t low)
975 return ((uint64_t)hi)<<32 | (uint64_t)low;
978 /* Print the file address */
979 char *DEVICE::print_addr(char *buf, int32_t buf_len)
982 bsnprintf(buf, buf_len, "%llu", get_full_addr());
986 char *DEVICE::print_addr(char *buf, int32_t buf_len, boffset_t addr)
989 bsnprintf(buf, buf_len, "%llu", addr);
994 bool DEVICE::do_size_checks(DCR *dcr, DEV_BLOCK *block)
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 */
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).
1014 if ((max_file_size > 0) &&
1015 (file_size+block->binbuf) >= max_file_size) {
1016 file_size = 0; /* reset file size */
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"),
1022 Dmsg0(40, "Calling terminate_writing_volume\n");
1023 terminate_writing_volume(dcr);
1028 if (!do_new_file_bookkeeping(dcr)) {
1029 /* Error message already sent */
1036 bool DEVICE::get_tape_alerts(DCR *dcr)
1041 void DEVICE::show_tape_alerts(DCR *dcr, alert_list_type type,
1042 alert_list_which which, alert_cb alert_callback)
1047 int DEVICE::delete_alerts()