2 Bacula® - The Network Backup Solution
4 Copyright (C) 2000-2010 Free Software Foundation Europe e.V.
6 The main author of Bacula is Kern Sibbald, with contributions from
7 many others, a complete list can be found in the file AUTHORS.
8 This program is Free Software; you can redistribute it and/or
9 modify it under the terms of version three of the GNU Affero General Public
10 License as published by the Free Software Foundation and included
13 This program is distributed in the hope that it will be useful, but
14 WITHOUT ANY WARRANTY; without even the implied warranty of
15 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
16 General Public License for more details.
18 You should have received a copy of the GNU Affero General Public License
19 along with this program; if not, write to the Free Software
20 Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
23 Bacula® is a registered trademark of Kern Sibbald.
24 The licensor of Bacula is the Free Software Foundation Europe
25 (FSFE), Fiduciary Program, Sumatrastrasse 25, 8006 Zürich,
26 Switzerland, email:ftf@fsfeurope.org.
30 * dev.c -- low level operations on device (storage device)
34 * NOTE!!!! None of these routines are reentrant. You must
35 * use dev->r_dlock() and dev->unlock() at a higher level,
36 * or use the xxx_device() equivalents. By moving the
37 * thread synchronization to a higher level, we permit
38 * the higher level routines to "seize" the device and
39 * to carry out operations without worrying about who
40 * set what lock (i.e. race conditions).
42 * Note, this is the device dependent code, and may have
43 * to be modified for each system, but is meant to
44 * be as "generic" as possible.
46 * The purpose of this code is to develop a SIMPLE Storage
47 * daemon. More complicated coding (double buffering, writer
48 * thread, ...) is left for a later version.
53 * Handling I/O errors and end of tape conditions are a bit tricky.
54 * This is how it is currently done when writing.
55 * On either an I/O error or end of tape,
56 * we will stop writing on the physical device (no I/O recovery is
57 * attempted at least in this daemon). The state flag will be sent
58 * to include ST_EOT, which is ephemeral, and ST_WEOT, which is
59 * persistent. Lots of routines clear ST_EOT, but ST_WEOT is
60 * cleared only when the problem goes away. Now when ST_WEOT
61 * is set all calls to write_block_to_device() call the fix_up
62 * routine. In addition, all threads are blocked
63 * from writing on the tape by calling lock_dev(), and thread other
64 * than the first thread to hit the EOT will block on a condition
65 * variable. The first thread to hit the EOT will continue to
66 * be able to read and write the tape (he sort of tunnels through
67 * the locking mechanism -- see lock_dev() for details).
69 * Now presumably somewhere higher in the chain of command
70 * (device.c), someone will notice the EOT condition and
71 * get a new tape up, get the tape label read, and mark
72 * the label for rewriting. Then this higher level routine
73 * will write the unwritten buffer to the new volume.
74 * Finally, he will release
75 * any blocked threads by doing a broadcast on the condition
76 * variable. At that point, we should be totally back in
77 * business with no lost data.
88 /* Forward referenced functions */
89 void set_os_device_parameters(DCR *dcr);
90 static bool dev_get_os_pos(DEVICE *dev, struct mtget *mt_stat);
91 static const char *mode_to_str(int mode);
94 * Allocate and initialize the DEVICE structure
95 * Note, if dev is non-NULL, it is already allocated,
96 * thus we neither allocate it nor free it. This allows
97 * the caller to put the packet in shared memory.
99 * Note, for a tape, the device->device_name is the device name
100 * (e.g. /dev/nst0), and for a file, the device name
101 * is the directory in which the file will be placed.
105 init_dev(JCR *jcr, DEVRES *device)
114 /* If no device type specified, try to guess */
115 if (!device->dev_type) {
116 /* Check that device is available */
117 if (stat(device->device_name, &statp) < 0) {
119 Jmsg2(jcr, M_ERROR, 0, _("Unable to stat device %s: ERR=%s\n"),
120 device->device_name, be.bstrerror());
123 if (S_ISDIR(statp.st_mode)) {
124 device->dev_type = B_FILE_DEV;
125 } else if (S_ISCHR(statp.st_mode)) {
126 device->dev_type = B_TAPE_DEV;
127 } else if (S_ISFIFO(statp.st_mode)) {
128 device->dev_type = B_FIFO_DEV;
130 /* must set DeviceType = Vtape
131 * in normal mode, autodetection is disabled
133 } else if (S_ISREG(statp.st_mode)) {
134 device->dev_type = B_VTAPE_DEV;
136 } else if (!(device->cap_bits & CAP_REQMOUNT)) {
137 Jmsg2(jcr, M_ERROR, 0, _("%s is an unknown device type. Must be tape or directory\n"
138 " or have RequiresMount=yes for DVD. st_mode=%x\n"),
139 device->device_name, statp.st_mode);
142 device->dev_type = B_DVD_DEV;
145 switch (device->dev_type) {
147 Jmsg0(jcr, M_FATAL, 0, _("DVD support is now deprecated\n"));
154 dev = New(ftp_device);
158 /* TODO: defined in src/win32/stored/mtops.cpp */
160 dev = New(win32_tape_device);
163 dev = New(win32_file_device);
175 dev->clear_slot(); /* unknown */
177 /* Copy user supplied device parameters from Resource */
178 dev->dev_name = get_memory(strlen(device->device_name)+1);
179 pm_strcpy(dev->dev_name, device->device_name);
180 dev->prt_name = get_memory(strlen(device->device_name) + strlen(device->hdr.name) + 20);
181 /* We edit "Resource-name" (physical-name) */
182 Mmsg(dev->prt_name, "\"%s\" (%s)", device->hdr.name, device->device_name);
183 Dmsg1(400, "Allocate dev=%s\n", dev->print_name());
184 dev->capabilities = device->cap_bits;
185 dev->min_block_size = device->min_block_size;
186 dev->max_block_size = device->max_block_size;
187 dev->max_volume_size = device->max_volume_size;
188 dev->max_file_size = device->max_file_size;
189 dev->max_concurrent_jobs = device->max_concurrent_jobs;
190 dev->volume_capacity = device->volume_capacity;
191 dev->max_rewind_wait = device->max_rewind_wait;
192 dev->max_open_wait = device->max_open_wait;
193 dev->max_open_vols = device->max_open_vols;
194 dev->vol_poll_interval = device->vol_poll_interval;
195 dev->max_spool_size = device->max_spool_size;
196 dev->drive_index = device->drive_index;
197 dev->autoselect = device->autoselect;
198 dev->dev_type = device->dev_type;
199 if (dev->is_tape()) { /* No parts on tapes */
200 dev->max_part_size = 0;
202 dev->max_part_size = device->max_part_size;
205 if (dev->vol_poll_interval && dev->vol_poll_interval < 60) {
206 dev->vol_poll_interval = 60;
208 /* Link the dev and device structures together */
209 dev->device = device;
212 if (dev->is_fifo()) {
213 dev->capabilities |= CAP_STREAM; /* set stream device */
216 /* If the device requires mount :
217 * - Check that the mount point is available
218 * - Check that (un)mount commands are defined
220 if (dev->is_file() && dev->requires_mount()) {
221 if (!device->mount_point || stat(device->mount_point, &statp) < 0) {
223 dev->dev_errno = errno;
224 Jmsg2(jcr, M_ERROR_TERM, 0, _("Unable to stat mount point %s: ERR=%s\n"),
225 device->mount_point, be.bstrerror());
228 if (!device->mount_command || !device->unmount_command) {
229 Jmsg0(jcr, M_ERROR_TERM, 0, _("Mount and unmount commands must defined for a device which requires mount.\n"));
234 if (dev->max_block_size == 0) {
235 max_bs = DEFAULT_BLOCK_SIZE;
237 max_bs = dev->max_block_size;
239 if (dev->min_block_size > max_bs) {
240 Jmsg(jcr, M_ERROR_TERM, 0, _("Min block size > max on device %s\n"),
243 if (dev->max_block_size > 4096000) {
244 Jmsg3(jcr, M_ERROR, 0, _("Block size %u on device %s is too large, using default %u\n"),
245 dev->max_block_size, dev->print_name(), DEFAULT_BLOCK_SIZE);
246 dev->max_block_size = 0;
248 if (dev->max_block_size % TAPE_BSIZE != 0) {
249 Jmsg2(jcr, M_WARNING, 0, _("Max block size %u not multiple of device %s block size.\n"),
250 dev->max_block_size, dev->print_name());
252 if (dev->max_volume_size != 0 && dev->max_volume_size < (dev->max_block_size << 4)) {
253 Jmsg(jcr, M_ERROR_TERM, 0, _("Max Vol Size < 8 * Max Block Size on device %s\n"),
257 dev->errmsg = get_pool_memory(PM_EMSG);
260 if ((errstat = pthread_mutex_init(&dev->m_mutex, NULL)) != 0) {
262 dev->dev_errno = errstat;
263 Mmsg1(dev->errmsg, _("Unable to init mutex: ERR=%s\n"), be.bstrerror(errstat));
264 Jmsg0(jcr, M_ERROR_TERM, 0, dev->errmsg);
266 if ((errstat = pthread_cond_init(&dev->wait, NULL)) != 0) {
268 dev->dev_errno = errstat;
269 Mmsg1(dev->errmsg, _("Unable to init cond variable: ERR=%s\n"), be.bstrerror(errstat));
270 Jmsg0(jcr, M_ERROR_TERM, 0, dev->errmsg);
272 if ((errstat = pthread_cond_init(&dev->wait_next_vol, NULL)) != 0) {
274 dev->dev_errno = errstat;
275 Mmsg1(dev->errmsg, _("Unable to init cond variable: ERR=%s\n"), be.bstrerror(errstat));
276 Jmsg0(jcr, M_ERROR_TERM, 0, dev->errmsg);
278 if ((errstat = pthread_mutex_init(&dev->spool_mutex, NULL)) != 0) {
280 dev->dev_errno = errstat;
281 Mmsg1(dev->errmsg, _("Unable to init mutex: ERR=%s\n"), be.bstrerror(errstat));
282 Jmsg0(jcr, M_ERROR_TERM, 0, dev->errmsg);
284 if ((errstat = pthread_mutex_init(&dev->acquire_mutex, NULL)) != 0) {
286 dev->dev_errno = errstat;
287 Mmsg1(dev->errmsg, _("Unable to init mutex: ERR=%s\n"), be.bstrerror(errstat));
288 Jmsg0(jcr, M_ERROR_TERM, 0, dev->errmsg);
290 /* Ensure that we respect this order in P/V operations */
291 bthread_mutex_set_priority(&dev->m_mutex, PRIO_SD_DEV_ACCESS);
292 bthread_mutex_set_priority(&dev->spool_mutex, PRIO_SD_DEV_SPOOL);
293 bthread_mutex_set_priority(&dev->acquire_mutex, PRIO_SD_DEV_ACQUIRE);
295 if ((errstat = rwl_init(&dev->lock)) != 0) {
297 dev->dev_errno = errstat;
298 Mmsg1(dev->errmsg, _("Unable to init mutex: ERR=%s\n"), be.bstrerror(errstat));
299 Jmsg0(jcr, M_ERROR_TERM, 0, dev->errmsg);
304 dev->attached_dcrs = New(dlist(dcr, &dcr->dev_link));
305 Dmsg2(100, "init_dev: tape=%d dev_name=%s\n", dev->is_tape(), dev->dev_name);
306 dev->initiated = true;
311 /* default primitives are designed for file */
312 int DEVICE::d_open(const char *pathname, int flags)
314 return ::open(pathname, flags);
317 int DEVICE::d_close(int fd)
322 int DEVICE::d_ioctl(int fd, ioctl_req_t request, char *mt_com)
324 return ::ioctl(fd, request, mt_com);
327 ssize_t DEVICE::d_read(int fd, void *buffer, size_t count)
329 return ::read(fd, buffer, count);
332 ssize_t DEVICE::d_write(int fd, const void *buffer, size_t count)
334 return ::write(fd, buffer, count);
338 * Open the device with the operating system and
339 * initialize buffer pointers.
341 * Returns: -1 on error
344 * Note, for a tape, the VolName is the name we give to the
345 * volume (not really used here), but for a file, the
346 * VolName represents the name of the file to be created/opened.
347 * In the case of a file, the full name is the device name
348 * (archive_name) with the VolName concatenated.
351 DEVICE::open(DCR *dcr, int omode)
355 if (openmode == omode) {
360 Dmsg0(100, "Close fd for mode change.\n");
361 preserve = state & (ST_LABEL|ST_APPEND|ST_READ);
365 dcr->setVolCatName(dcr->VolumeName);
366 VolCatInfo = dcr->VolCatInfo; /* structure assign */
369 Dmsg4(100, "open dev: type=%d dev_name=%s vol=%s mode=%s\n", dev_type,
370 print_name(), getVolCatName(), mode_to_str(omode));
371 state &= ~(ST_LABEL|ST_APPEND|ST_READ|ST_EOT|ST_WEOT|ST_EOF);
372 label_type = B_BACULA_LABEL;
374 if (is_tape() || is_fifo()) {
375 open_tape_device(dcr, omode);
376 } else if (is_ftp()) {
377 open_device(dcr, omode);
379 Dmsg1(100, "call open_file_device mode=%s\n", mode_to_str(omode));
380 open_file_device(dcr, omode);
382 state |= preserve; /* reset any important state info */
383 Dmsg2(100, "preserve=0x%x fd=%d\n", preserve, m_fd);
387 void DEVICE::set_mode(int new_mode)
390 case CREATE_READ_WRITE:
391 mode = O_CREAT | O_RDWR | O_BINARY;
393 case OPEN_READ_WRITE:
394 mode = O_RDWR | O_BINARY;
397 mode = O_RDONLY | O_BINARY;
399 case OPEN_WRITE_ONLY:
400 mode = O_WRONLY | O_BINARY;
403 Emsg0(M_ABORT, 0, _("Illegal mode given to open dev.\n"));
409 void DEVICE::open_tape_device(DCR *dcr, int omode)
412 int timeout = max_open_wait;
413 #if !defined(HAVE_WIN32)
415 utime_t start_time = time(NULL);
418 mount(1); /* do mount if required */
420 Dmsg0(100, "Open dev: device is tape\n");
422 get_autochanger_loaded_slot(dcr);
431 if (is_fifo() && timeout) {
433 tid = start_thread_timer(dcr->jcr, pthread_self(), timeout);
435 Dmsg2(100, "Try open %s mode=%s\n", print_name(), mode_to_str(omode));
436 #if defined(HAVE_WIN32)
439 if ((m_fd = d_open(dev_name, mode)) < 0) {
446 /* If busy retry each second for max_open_wait seconds */
448 /* Try non-blocking open */
449 m_fd = d_open(dev_name, mode+O_NONBLOCK);
453 Dmsg5(100, "Open error on %s omode=%d mode=%x errno=%d: ERR=%s\n",
454 print_name(), omode, mode, errno, be.bstrerror());
456 /* Tape open, now rewind it */
457 Dmsg0(100, "Rewind after open\n");
458 mt_com.mt_op = MTREW;
460 /* rewind only if dev is a tape */
461 if (is_tape() && (d_ioctl(m_fd, MTIOCTOP, (char *)&mt_com) < 0)) {
463 dev_errno = errno; /* set error status from rewind */
466 Dmsg2(100, "Rewind error on %s close: ERR=%s\n", print_name(),
467 be.bstrerror(dev_errno));
468 /* If we get busy, device is probably rewinding, try again */
469 if (dev_errno != EBUSY) {
470 break; /* error -- no medium */
473 /* Got fd and rewind worked, so we must have medium in drive */
475 m_fd = d_open(dev_name, mode); /* open normally */
479 Dmsg5(100, "Open error on %s omode=%d mode=%x errno=%d: ERR=%s\n",
480 print_name(), omode, mode, errno, be.bstrerror());
485 set_os_device_parameters(dcr); /* do system dependent stuff */
486 break; /* Successfully opened and rewound */
490 /* Exceed wait time ? */
491 if (time(NULL) - start_time >= max_open_wait) {
492 break; /* yes, get out */
499 Mmsg2(errmsg, _("Unable to open device %s: ERR=%s\n"),
500 print_name(), be.bstrerror(dev_errno));
501 Dmsg1(100, "%s", errmsg);
504 /* Stop any open() timer we started */
506 stop_thread_timer(tid);
509 Dmsg1(100, "open dev: tape %d opened\n", m_fd);
512 void DEVICE::open_device(DCR *dcr, int omode)
514 /* do nothing waiting to split open_file/tape_device */
520 void DEVICE::open_file_device(DCR *dcr, int omode)
522 POOL_MEM archive_name(PM_FNAME);
524 get_autochanger_loaded_slot(dcr);
527 * Handle opening of File Archive (not a tape)
530 pm_strcpy(archive_name, dev_name);
532 * If this is a virtual autochanger (i.e. changer_res != NULL)
533 * we simply use the device name, assuming it has been
534 * appropriately setup by the "autochanger".
536 if (!device->changer_res || device->changer_command[0] == 0) {
537 if (VolCatInfo.VolCatName[0] == 0) {
538 Mmsg(errmsg, _("Could not open file device %s. No Volume name given.\n"),
544 if (!IsPathSeparator(archive_name.c_str()[strlen(archive_name.c_str())-1])) {
545 pm_strcat(archive_name, "/");
547 pm_strcat(archive_name, getVolCatName());
550 mount(1); /* do mount if required */
554 /* If creating file, give 0640 permissions */
555 Dmsg3(100, "open disk: mode=%s open(%s, 0x%x, 0640)\n", mode_to_str(omode),
556 archive_name.c_str(), mode);
557 /* Use system open() */
558 if ((m_fd = ::open(archive_name.c_str(), mode, 0640)) < 0) {
561 Mmsg2(errmsg, _("Could not open: %s, ERR=%s\n"), archive_name.c_str(),
563 Dmsg1(100, "open failed: %s", errmsg);
564 // Jmsg1(NULL, M_WARNING, 0, "%s", errmsg);
570 Dmsg4(100, "open dev: disk fd=%d opened, part=%d/%d, part_size=%u\n",
571 m_fd, part, num_dvd_parts, part_size);
576 * Returns: true on success
579 bool DEVICE::rewind(DCR *dcr)
585 Dmsg3(400, "rewind res=%d fd=%d %s\n", num_reserved(), m_fd, print_name());
586 state &= ~(ST_EOT|ST_EOF|ST_WEOT); /* remove EOF/EOT flags */
587 block_num = file = 0;
594 mt_com.mt_op = MTREW;
596 /* If we get an I/O error on rewind, it is probably because
597 * the drive is actually busy. We loop for (about 5 minutes)
598 * retrying every 5 seconds.
600 for (i=max_rewind_wait; ; i -= 5) {
601 if (d_ioctl(m_fd, MTIOCTOP, (char *)&mt_com) < 0) {
604 if (i == max_rewind_wait) {
605 Dmsg1(200, "Rewind error, %s. retrying ...\n", be.bstrerror());
608 * This is a gross hack, because if the user has the
609 * device mounted (i.e. open), then uses mtx to load
610 * a tape, the current open file descriptor is invalid.
611 * So, we close the drive and re-open it.
614 int open_mode = openmode;
617 open(dcr, open_mode);
625 if (dev_errno == EIO) {
626 Mmsg1(errmsg, _("No tape loaded or drive offline on %s.\n"), print_name());
630 if (dev_errno == EIO && i > 0) {
631 Dmsg0(200, "Sleeping 5 seconds.\n");
636 Mmsg2(errmsg, _("Rewind error on %s. ERR=%s.\n"),
637 print_name(), be.bstrerror());
642 } else if (is_file()) {
643 if (lseek(dcr, (boffset_t)0, SEEK_SET) < 0) {
646 Mmsg2(errmsg, _("lseek error on %s. ERR=%s.\n"),
647 print_name(), be.bstrerror());
656 * Called to indicate that we have just read an
657 * EOF from the device.
659 void DEVICE::set_ateof()
671 * Called to indicate we are now at the end of the tape, and
672 * writing is not possible.
674 void DEVICE::set_ateot()
676 /* Make tape effectively read-only */
677 state |= (ST_EOF|ST_EOT|ST_WEOT);
682 * Position device to end of medium (end of data)
683 * Returns: true on succes
686 bool DEVICE::eod(DCR *dcr)
695 Mmsg1(errmsg, _("Bad call to eod. Device %s not open\n"), print_name());
699 #if defined (__digital__) && defined (__unix__)
700 return fsf(VolCatInfo.VolCatFiles);
703 Dmsg0(100, "Enter eod\n");
707 clear_eof(); /* remove EOF flag */
708 block_num = file = 0;
715 pos = lseek(dcr, (boffset_t)0, SEEK_END);
716 Dmsg1(200, "====== Seek to %lld\n", pos);
724 Mmsg2(errmsg, _("lseek error on %s. ERR=%s.\n"),
725 print_name(), be.bstrerror());
730 if (has_cap(CAP_FASTFSF) && !has_cap(CAP_EOM)) {
731 Dmsg0(100,"Using FAST FSF for EOM\n");
732 /* If unknown position, rewind */
733 if (get_os_tape_file() < 0) {
735 Dmsg0(100, "Rewind error\n");
739 mt_com.mt_op = MTFSF;
741 * ***FIXME*** fix code to handle case that INT16_MAX is
744 mt_com.mt_count = INT16_MAX; /* use big positive number */
745 if (mt_com.mt_count < 0) {
746 mt_com.mt_count = INT16_MAX; /* brain damaged system */
750 if (has_cap(CAP_MTIOCGET) && (has_cap(CAP_FASTFSF) || has_cap(CAP_EOM))) {
751 if (has_cap(CAP_EOM)) {
752 Dmsg0(100,"Using EOM for EOM\n");
753 mt_com.mt_op = MTEOM;
757 if (d_ioctl(m_fd, MTIOCTOP, (char *)&mt_com) < 0) {
759 clrerror(mt_com.mt_op);
760 Dmsg1(50, "ioctl error: %s\n", be.bstrerror());
762 Mmsg2(errmsg, _("ioctl MTEOM error on %s. ERR=%s.\n"),
763 print_name(), be.bstrerror());
768 os_file = get_os_tape_file();
772 Mmsg2(errmsg, _("ioctl MTIOCGET error on %s. ERR=%s.\n"),
773 print_name(), be.bstrerror());
777 Dmsg1(100, "EOD file=%d\n", os_file);
785 * Rewind then use FSF until EOT reached
788 Dmsg0(100, "Rewind error.\n");
792 * Move file by file to the end of the tape
795 for (file_num=file; !at_eot(); file_num++) {
796 Dmsg0(200, "eod: doing fsf 1\n");
798 Dmsg0(100, "fsf error.\n");
802 * Avoid infinite loop by ensuring we advance.
804 if (!at_eot() && file_num == (int)file) {
805 Dmsg1(100, "fsf did not advance from file %d\n", file_num);
807 os_file = get_os_tape_file();
809 Dmsg2(100, "Adjust file from %d to %d\n", file_num, os_file);
817 * Some drivers leave us after second EOF when doing
818 * MTEOM, so we must backup so that appending overwrites
821 if (has_cap(CAP_BSFATEOM)) {
822 /* Backup over EOF */
824 /* If BSF worked and fileno is known (not -1), set file */
825 os_file = get_os_tape_file();
827 Dmsg2(100, "BSFATEOF adjust file from %d to %d\n", file , os_file);
830 file++; /* wing it -- not correct on all OSes */
833 update_pos(dcr); /* update position */
835 Dmsg1(200, "EOD dev->file=%d\n", file);
840 * Set the position of the device -- only for files and DVD
841 * For other devices, there is no generic way to do it.
842 * Returns: true on succes
845 bool DEVICE::update_pos(DCR *dcr)
852 Mmsg0(errmsg, _("Bad device call. Device not open\n"));
853 Emsg1(M_FATAL, 0, "%s", errmsg);
860 pos = lseek(dcr, (boffset_t)0, SEEK_CUR);
864 Pmsg1(000, _("Seek error: ERR=%s\n"), be.bstrerror());
865 Mmsg2(errmsg, _("lseek error on %s. ERR=%s.\n"),
866 print_name(), be.bstrerror());
870 block_num = (uint32_t)pos;
871 file = (uint32_t)(pos >> 32);
878 * Return the status of the device. This was meant
879 * to be a generic routine. Unfortunately, it doesn't
880 * seem possible (at least I do not know how to do it
881 * currently), which means that for the moment, this
882 * routine has very little value.
886 uint32_t status_dev(DEVICE *dev)
888 struct mtget mt_stat;
891 if (dev->state & (ST_EOT | ST_WEOT)) {
895 if (dev->state & ST_EOF) {
899 if (dev->is_tape()) {
901 Pmsg0(-20,_(" Bacula status:"));
902 Pmsg2(-20,_(" file=%d block=%d\n"), dev->file, dev->block_num);
903 if (dev->d_ioctl(dev->fd(), MTIOCGET, (char *)&mt_stat) < 0) {
905 dev->dev_errno = errno;
906 Mmsg2(dev->errmsg, _("ioctl MTIOCGET error on %s. ERR=%s.\n"),
907 dev->print_name(), be.bstrerror());
910 Pmsg0(-20, _(" Device status:"));
912 #if defined(HAVE_LINUX_OS)
913 if (GMT_EOF(mt_stat.mt_gstat)) {
917 if (GMT_BOT(mt_stat.mt_gstat)) {
921 if (GMT_EOT(mt_stat.mt_gstat)) {
925 if (GMT_SM(mt_stat.mt_gstat)) {
929 if (GMT_EOD(mt_stat.mt_gstat)) {
933 if (GMT_WR_PROT(mt_stat.mt_gstat)) {
935 Pmsg0(-20, " WR_PROT");
937 if (GMT_ONLINE(mt_stat.mt_gstat)) {
939 Pmsg0(-20, " ONLINE");
941 if (GMT_DR_OPEN(mt_stat.mt_gstat)) {
943 Pmsg0(-20, " DR_OPEN");
945 if (GMT_IM_REP_EN(mt_stat.mt_gstat)) {
946 stat |= BMT_IM_REP_EN;
947 Pmsg0(-20, " IM_REP_EN");
949 #elif defined(HAVE_WIN32)
950 if (GMT_EOF(mt_stat.mt_gstat)) {
954 if (GMT_BOT(mt_stat.mt_gstat)) {
958 if (GMT_EOT(mt_stat.mt_gstat)) {
962 if (GMT_EOD(mt_stat.mt_gstat)) {
966 if (GMT_WR_PROT(mt_stat.mt_gstat)) {
968 Pmsg0(-20, " WR_PROT");
970 if (GMT_ONLINE(mt_stat.mt_gstat)) {
972 Pmsg0(-20, " ONLINE");
974 if (GMT_DR_OPEN(mt_stat.mt_gstat)) {
976 Pmsg0(-20, " DR_OPEN");
978 if (GMT_IM_REP_EN(mt_stat.mt_gstat)) {
979 stat |= BMT_IM_REP_EN;
980 Pmsg0(-20, " IM_REP_EN");
983 #endif /* !SunOS && !OSF */
984 if (dev->has_cap(CAP_MTIOCGET)) {
985 Pmsg2(-20, _(" file=%d block=%d\n"), mt_stat.mt_fileno, mt_stat.mt_blkno);
987 Pmsg2(-20, _(" file=%d block=%d\n"), -1, -1);
990 stat |= BMT_ONLINE | BMT_BOT;
997 * Load medium in device
998 * Returns: true on success
1001 bool load_dev(DEVICE *dev)
1007 if (dev->fd() < 0) {
1008 dev->dev_errno = EBADF;
1009 Mmsg0(dev->errmsg, _("Bad call to load_dev. Device not open\n"));
1010 Emsg0(M_FATAL, 0, dev->errmsg);
1013 if (!(dev->is_tape())) {
1017 Dmsg0(200, "stored: MTLOAD command not available\n");
1019 dev->dev_errno = ENOTTY; /* function not available */
1020 Mmsg2(dev->errmsg, _("ioctl MTLOAD error on %s. ERR=%s.\n"),
1021 dev->print_name(), be.bstrerror());
1025 dev->block_num = dev->file = 0;
1028 mt_com.mt_op = MTLOAD;
1029 mt_com.mt_count = 1;
1030 if (dev->d_ioctl(dev->fd(), MTIOCTOP, (char *)&mt_com) < 0) {
1032 dev->dev_errno = errno;
1033 Mmsg2(dev->errmsg, _("ioctl MTLOAD error on %s. ERR=%s.\n"),
1034 dev->print_name(), be.bstrerror());
1042 * Rewind device and put it offline
1043 * Returns: true on success
1046 bool DEVICE::offline()
1051 return true; /* device not open */
1054 state &= ~(ST_APPEND|ST_READ|ST_EOT|ST_EOF|ST_WEOT); /* remove EOF/EOT flags */
1055 block_num = file = 0;
1059 mt_com.mt_op = MTOFFL;
1060 mt_com.mt_count = 1;
1061 if (d_ioctl(m_fd, MTIOCTOP, (char *)&mt_com) < 0) {
1064 Mmsg2(errmsg, _("ioctl MTOFFL error on %s. ERR=%s.\n"),
1065 print_name(), be.bstrerror());
1068 Dmsg1(100, "Offlined device %s\n", print_name());
1072 bool DEVICE::offline_or_rewind()
1077 if (has_cap(CAP_OFFLINEUNMOUNT)) {
1081 * Note, this rewind probably should not be here (it wasn't
1082 * in prior versions of Bacula), but on FreeBSD, this is
1083 * needed in the case the tape was "frozen" due to an error
1084 * such as backspacing after writing and EOF. If it is not
1085 * done, all future references to the drive get and I/O error.
1088 return rewind(NULL);
1093 * Foward space a file
1094 * Returns: true on success
1097 bool DEVICE::fsf(int num)
1099 int32_t os_file = 0;
1105 Mmsg0(errmsg, _("Bad call to fsf. Device not open\n"));
1106 Emsg0(M_FATAL, 0, errmsg);
1116 Mmsg1(errmsg, _("Device %s at End of Tape.\n"), print_name());
1120 Dmsg0(200, "ST_EOF set on entry to FSF\n");
1123 Dmsg0(100, "fsf\n");
1126 * If Fast forward space file is set, then we
1127 * use MTFSF to forward space and MTIOCGET
1128 * to get the file position. We assume that
1129 * the SCSI driver will ensure that we do not
1130 * forward space past the end of the medium.
1132 if (has_cap(CAP_FSF) && has_cap(CAP_MTIOCGET) && has_cap(CAP_FASTFSF)) {
1134 mt_com.mt_op = MTFSF;
1135 mt_com.mt_count = num;
1136 stat = d_ioctl(m_fd, MTIOCTOP, (char *)&mt_com);
1138 my_errno = errno; /* save errno */
1139 } else if ((os_file=get_os_tape_file()) < 0) {
1140 my_errno = errno; /* save errno */
1142 if (my_errno != 0) {
1145 Dmsg0(200, "Set ST_EOT\n");
1147 Mmsg2(errmsg, _("ioctl MTFSF error on %s. ERR=%s.\n"),
1148 print_name(), be.bstrerror(my_errno));
1149 Dmsg1(200, "%s", errmsg);
1153 Dmsg1(200, "fsf file=%d\n", os_file);
1159 * Here if CAP_FSF is set, and virtually all drives
1160 * these days support it, we read a record, then forward
1161 * space one file. Using this procedure, which is slow,
1162 * is the only way we can be sure that we don't read
1163 * two consecutive EOF marks, which means End of Data.
1165 } else if (has_cap(CAP_FSF)) {
1168 Dmsg0(200, "FSF has cap_fsf\n");
1169 if (max_block_size == 0) {
1170 rbuf_len = DEFAULT_BLOCK_SIZE;
1172 rbuf_len = max_block_size;
1174 rbuf = get_memory(rbuf_len);
1175 mt_com.mt_op = MTFSF;
1176 mt_com.mt_count = 1;
1177 while (num-- && !at_eot()) {
1178 Dmsg0(100, "Doing read before fsf\n");
1179 if ((stat = this->read((char *)rbuf, rbuf_len)) < 0) {
1180 if (errno == ENOMEM) { /* tape record exceeds buf len */
1181 stat = rbuf_len; /* This is OK */
1183 * On IBM drives, they return ENOSPC at EOM
1184 * instead of EOF status
1186 } else if (at_eof() && errno == ENOSPC) {
1192 Dmsg2(100, "Set ST_EOT read errno=%d. ERR=%s\n", dev_errno,
1194 Mmsg2(errmsg, _("read error on %s. ERR=%s.\n"),
1195 print_name(), be.bstrerror());
1196 Dmsg1(100, "%s", errmsg);
1200 if (stat == 0) { /* EOF */
1201 Dmsg1(100, "End of File mark from read. File=%d\n", file+1);
1202 /* Two reads of zero means end of tape */
1205 Dmsg0(100, "Set ST_EOT\n");
1211 } else { /* Got data */
1216 Dmsg0(100, "Doing MTFSF\n");
1217 stat = d_ioctl(m_fd, MTIOCTOP, (char *)&mt_com);
1218 if (stat < 0) { /* error => EOT */
1221 Dmsg0(100, "Set ST_EOT\n");
1223 Mmsg2(errmsg, _("ioctl MTFSF error on %s. ERR=%s.\n"),
1224 print_name(), be.bstrerror());
1225 Dmsg0(100, "Got < 0 for MTFSF\n");
1226 Dmsg1(100, "%s", errmsg);
1234 * No FSF, so use FSR to simulate it
1237 Dmsg0(200, "Doing FSR for FSF\n");
1238 while (num-- && !at_eot()) {
1239 fsr(INT32_MAX); /* returns -1 on EOF or EOT */
1243 Mmsg1(errmsg, _("Device %s at End of Tape.\n"), print_name());
1249 Dmsg1(200, "Return %d from FSF\n", stat);
1251 Dmsg0(200, "ST_EOF set on exit FSF\n");
1254 Dmsg0(200, "ST_EOT set on exit FSF\n");
1256 Dmsg1(200, "Return from FSF file=%d\n", file);
1261 * Backward space a file
1262 * Returns: false on failure
1265 bool DEVICE::bsf(int num)
1272 Mmsg0(errmsg, _("Bad call to bsf. Device not open\n"));
1273 Emsg0(M_FATAL, 0, errmsg);
1278 Mmsg1(errmsg, _("Device %s cannot BSF because it is not a tape.\n"),
1283 Dmsg0(100, "bsf\n");
1289 mt_com.mt_op = MTBSF;
1290 mt_com.mt_count = num;
1291 stat = d_ioctl(m_fd, MTIOCTOP, (char *)&mt_com);
1295 Mmsg2(errmsg, _("ioctl MTBSF error on %s. ERR=%s.\n"),
1296 print_name(), be.bstrerror());
1303 * Foward space num records
1304 * Returns: false on failure
1307 bool DEVICE::fsr(int num)
1314 Mmsg0(errmsg, _("Bad call to fsr. Device not open\n"));
1315 Emsg0(M_FATAL, 0, errmsg);
1323 if (!has_cap(CAP_FSR)) {
1324 Mmsg1(errmsg, _("ioctl MTFSR not permitted on %s.\n"), print_name());
1328 Dmsg1(100, "fsr %d\n", num);
1329 mt_com.mt_op = MTFSR;
1330 mt_com.mt_count = num;
1331 stat = d_ioctl(m_fd, MTIOCTOP, (char *)&mt_com);
1337 struct mtget mt_stat;
1339 Dmsg1(100, "FSF fail: ERR=%s\n", be.bstrerror());
1340 if (dev_get_os_pos(this, &mt_stat)) {
1341 Dmsg4(100, "Adjust from %d:%d to %d:%d\n", file,
1342 block_num, mt_stat.mt_fileno, mt_stat.mt_blkno);
1343 file = mt_stat.mt_fileno;
1344 block_num = mt_stat.mt_blkno;
1352 Mmsg3(errmsg, _("ioctl MTFSR %d error on %s. ERR=%s.\n"),
1353 num, print_name(), be.bstrerror());
1359 * Backward space a record
1360 * Returns: false on failure
1363 bool DEVICE::bsr(int num)
1370 Mmsg0(errmsg, _("Bad call to bsr_dev. Device not open\n"));
1371 Emsg0(M_FATAL, 0, errmsg);
1379 if (!has_cap(CAP_BSR)) {
1380 Mmsg1(errmsg, _("ioctl MTBSR not permitted on %s.\n"), print_name());
1384 Dmsg0(100, "bsr_dev\n");
1388 mt_com.mt_op = MTBSR;
1389 mt_com.mt_count = num;
1390 stat = d_ioctl(m_fd, MTIOCTOP, (char *)&mt_com);
1394 Mmsg2(errmsg, _("ioctl MTBSR error on %s. ERR=%s.\n"),
1395 print_name(), be.bstrerror());
1400 void DEVICE::lock_door()
1404 mt_com.mt_op = MTLOCK;
1405 mt_com.mt_count = 1;
1406 d_ioctl(m_fd, MTIOCTOP, (char *)&mt_com);
1410 void DEVICE::unlock_door()
1414 mt_com.mt_op = MTUNLOCK;
1415 mt_com.mt_count = 1;
1416 d_ioctl(m_fd, MTIOCTOP, (char *)&mt_com);
1420 void DEVICE::set_slot(int32_t slot)
1423 if (vol) vol->clear_slot();
1426 void DEVICE::clear_slot()
1429 if (vol) vol->set_slot(-1);
1435 * Reposition the device to file, block
1436 * Returns: false on failure
1439 bool DEVICE::reposition(DCR *dcr, uint32_t rfile, uint32_t rblock)
1443 Mmsg0(errmsg, _("Bad call to reposition. Device not open\n"));
1444 Emsg0(M_FATAL, 0, errmsg);
1449 boffset_t pos = (((boffset_t)rfile)<<32) | rblock;
1450 Dmsg1(100, "===== lseek to %d\n", (int)pos);
1451 if (lseek(dcr, pos, SEEK_SET) == (boffset_t)-1) {
1454 Mmsg2(errmsg, _("lseek error on %s. ERR=%s.\n"),
1455 print_name(), be.bstrerror());
1464 /* After this point, we are tape only */
1465 Dmsg4(100, "reposition from %u:%u to %u:%u\n", file, block_num, rfile, rblock);
1467 Dmsg0(100, "Rewind\n");
1468 if (!rewind(NULL)) {
1473 Dmsg1(100, "fsf %d\n", rfile-file);
1474 if (!fsf(rfile-file)) {
1475 Dmsg1(100, "fsf failed! ERR=%s\n", bstrerror());
1478 Dmsg2(100, "wanted_file=%d at_file=%d\n", rfile, file);
1480 if (rblock < block_num) {
1481 Dmsg2(100, "wanted_blk=%d at_blk=%d\n", rblock, block_num);
1482 Dmsg0(100, "bsf 1\n");
1484 Dmsg0(100, "fsf 1\n");
1486 Dmsg2(100, "wanted_blk=%d at_blk=%d\n", rblock, block_num);
1488 if (has_cap(CAP_POSITIONBLOCKS) && rblock > block_num) {
1489 /* Ignore errors as Bacula can read to the correct block */
1490 Dmsg1(100, "fsr %d\n", rblock-block_num);
1491 return fsr(rblock-block_num);
1493 while (rblock > block_num) {
1494 if (!read_block_from_dev(dcr, NO_BLOCK_NUMBER_CHECK)) {
1497 Dmsg2(30, "Failed to find requested block on %s: ERR=%s",
1498 print_name(), be.bstrerror());
1501 Dmsg2(300, "moving forward wanted_blk=%d at_blk=%d\n", rblock, block_num);
1510 * Write an end of file on the device
1511 * Returns: true on success
1514 bool DEVICE::weof(int num)
1518 Dmsg1(129, "=== weof_dev=%s\n", print_name());
1522 Mmsg0(errmsg, _("Bad call to weof_dev. Device not open\n"));
1523 Emsg0(M_FATAL, 0, errmsg);
1531 if (!can_append()) {
1532 Mmsg0(errmsg, _("Attempt to WEOF on non-appendable Volume\n"));
1533 Emsg0(M_FATAL, 0, errmsg);
1539 mt_com.mt_op = MTWEOF;
1540 mt_com.mt_count = num;
1541 stat = d_ioctl(m_fd, MTIOCTOP, (char *)&mt_com);
1550 Mmsg2(errmsg, _("ioctl MTWEOF error on %s. ERR=%s.\n"),
1551 print_name(), be.bstrerror());
1559 * If implemented in system, clear the tape
1562 void DEVICE::clrerror(int func)
1564 const char *msg = NULL;
1567 dev_errno = errno; /* save errno */
1569 VolCatInfo.VolCatErrors++;
1576 if (errno == ENOTTY || errno == ENOSYS) { /* Function not implemented */
1579 break; /* ignore message printed later */
1582 clear_cap(CAP_EOF); /* turn off feature */
1587 clear_cap(CAP_EOM); /* turn off feature */
1592 clear_cap(CAP_FSF); /* turn off feature */
1596 clear_cap(CAP_BSF); /* turn off feature */
1600 clear_cap(CAP_FSR); /* turn off feature */
1604 clear_cap(CAP_BSR); /* turn off feature */
1614 #ifdef MTSETDRVBUFFER
1615 case MTSETDRVBUFFER:
1616 msg = "MTSETDRVBUFFER";
1649 bsnprintf(buf, sizeof(buf), _("unknown func code %d"), func);
1655 Mmsg1(errmsg, _("I/O function \"%s\" not supported on this device.\n"), msg);
1656 Emsg0(M_ERROR, 0, errmsg);
1661 * Now we try different methods of clearing the error
1662 * status on the drive so that it is not locked for
1663 * further operations.
1666 /* On some systems such as NetBSD, this clears all errors */
1669 /* Found on Solaris */
1672 d_ioctl(m_fd, MTIOCLRERR);
1673 Dmsg0(200, "Did MTIOCLRERR\n");
1677 /* Typically on FreeBSD */
1681 /* Read and clear SCSI error status */
1682 union mterrstat mt_errstat;
1683 Dmsg2(200, "Doing MTIOCERRSTAT errno=%d ERR=%s\n", dev_errno,
1684 be.bstrerror(dev_errno));
1685 d_ioctl(m_fd, MTIOCERRSTAT, (char *)&mt_errstat);
1689 /* Clear Subsystem Exception TRU64 */
1693 mt_com.mt_op = MTCSE;
1694 mt_com.mt_count = 1;
1695 /* Clear any error condition on the tape */
1696 d_ioctl(m_fd, MTIOCTOP, (char *)&mt_com);
1697 Dmsg0(200, "Did MTCSE\n");
1704 * Set to unload the current volume in the drive
1706 void DEVICE::set_unload()
1708 if (!m_unload && VolHdr.VolumeName[0] != 0) {
1710 memcpy(UnloadVolName, VolHdr.VolumeName, sizeof(UnloadVolName));
1716 * Clear volume header
1718 void DEVICE::clear_volhdr()
1720 Dmsg1(100, "Clear volhdr vol=%s\n", VolHdr.VolumeName);
1721 memset(&VolHdr, 0, sizeof(VolHdr));
1722 setVolCatInfo(false);
1729 void DEVICE::close()
1731 Dmsg1(100, "close_dev %s\n", print_name());
1732 offline_or_rewind();
1735 Dmsg2(100, "device %s already closed vol=%s\n", print_name(),
1737 return; /* already closed */
1750 unmount(1); /* do unmount if required */
1752 /* Clean up device packet so it can be reused */
1755 * Be careful not to clear items needed by the DVD driver
1756 * when it is closing a single part.
1758 state &= ~(ST_LABEL|ST_READ|ST_APPEND|ST_EOT|ST_WEOT|ST_EOF|
1759 ST_MOUNTED|ST_MEDIA|ST_SHORT);
1760 label_type = B_BACULA_LABEL;
1761 file = block_num = 0;
1764 EndFile = EndBlock = 0;
1767 memset(&VolCatInfo, 0, sizeof(VolCatInfo));
1769 stop_thread_timer(tid);
1775 * This call closes the device, but it is used in DVD handling
1776 * where we close one part and then open the next part. The
1777 * difference between close_part() and close() is that close_part()
1778 * saves the state information of the device (e.g. the Volume lable,
1779 * the Volume Catalog record, ... This permits opening and closing
1780 * the Volume parts multiple times without losing track of what the
1781 * main Volume parameters are.
1783 void DEVICE::close_part(DCR * /*dcr*/)
1785 VOLUME_LABEL saveVolHdr;
1786 VOLUME_CAT_INFO saveVolCatInfo; /* Volume Catalog Information */
1789 saveVolHdr = VolHdr; /* structure assignment */
1790 saveVolCatInfo = VolCatInfo; /* structure assignment */
1791 close(); /* close current part */
1792 VolHdr = saveVolHdr; /* structure assignment */
1793 VolCatInfo = saveVolCatInfo; /* structure assignment */
1796 boffset_t DEVICE::lseek(DCR *dcr, boffset_t offset, int whence)
1800 #if defined(HAVE_WIN32)
1801 return ::_lseeki64(m_fd, (__int64)offset, whence);
1803 return ::lseek(m_fd, offset, whence);
1810 bool DEVICE::truncate(DCR *dcr) /* We need the DCR for DVD-writing */
1814 Dmsg1(100, "truncate %s\n", print_name());
1819 /* maybe we should rewind and write and eof ???? */
1820 return true; /* we don't really truncate tapes */
1822 if (ftruncate(m_fd, 0) != 0) {
1824 Mmsg2(errmsg, _("Unable to truncate device %s. ERR=%s\n"),
1825 print_name(), be.bstrerror());
1830 * Check for a successful ftruncate() and issue a work-around for devices
1831 * (mostly cheap NAS) that don't support truncation.
1832 * Workaround supplied by Martin Schmid as a solution to bug #1011.
1835 * 3. open new file with same mode
1836 * 4. change ownership to original
1839 if (fstat(m_fd, &st) != 0) {
1841 Mmsg2(errmsg, _("Unable to stat device %s. ERR=%s\n"),
1842 print_name(), be.bstrerror());
1846 if (st.st_size != 0) { /* ftruncate() didn't work */
1847 POOL_MEM archive_name(PM_FNAME);
1849 pm_strcpy(archive_name, dev_name);
1850 if (!IsPathSeparator(archive_name.c_str()[strlen(archive_name.c_str())-1])) {
1851 pm_strcat(archive_name, "/");
1853 pm_strcat(archive_name, dcr->VolumeName);
1855 Mmsg2(errmsg, _("Device %s doesn't support ftruncate(). Recreating file %s.\n"),
1856 print_name(), archive_name.c_str());
1858 /* Close file and blow it away */
1860 ::unlink(archive_name.c_str());
1862 /* Recreate the file -- of course, empty */
1863 set_mode(CREATE_READ_WRITE);
1864 if ((m_fd = ::open(archive_name.c_str(), mode, st.st_mode)) < 0) {
1867 Mmsg2(errmsg, _("Could not reopen: %s, ERR=%s\n"), archive_name.c_str(),
1869 Dmsg1(100, "reopen failed: %s", errmsg);
1870 Emsg0(M_FATAL, 0, errmsg);
1874 /* Reset proper owner */
1875 chown(archive_name.c_str(), st.st_uid, st.st_gid);
1885 * If timeout, wait until the mount command returns 0.
1886 * If !timeout, try to mount the device only once.
1888 bool DEVICE::mount(int timeout)
1890 Dmsg0(190, "Enter mount\n");
1900 if (device->mount_command) {
1901 return do_tape_mount(1, timeout);
1905 if (requires_mount() && device->mount_command) {
1906 return do_file_mount(1, timeout);
1917 * Unmount the device
1918 * If timeout, wait until the unmount command returns 0.
1919 * If !timeout, try to unmount the device only once.
1921 bool DEVICE::unmount(int timeout)
1923 Dmsg0(100, "Enter unmount\n");
1925 if (!is_mounted()) {
1933 if (device->unmount_command) {
1934 return do_tape_mount(0, timeout);
1939 if (requires_mount() && device->unmount_command) {
1940 return do_file_mount(0, timeout);
1951 * (Un)mount the device (for tape devices)
1953 bool DEVICE::do_tape_mount(int mount, int dotimeout)
1955 POOL_MEM ocmd(PM_FNAME);
1963 icmd = device->mount_command;
1965 icmd = device->unmount_command;
1968 edit_mount_codes(ocmd, icmd);
1970 Dmsg2(100, "do_tape_mount: cmd=%s mounted=%d\n", ocmd.c_str(), !!is_mounted());
1973 /* Try at most 10 times to (un)mount the device. This should perhaps be configurable. */
1978 results = get_memory(4000);
1980 /* If busy retry each second */
1981 Dmsg1(100, "do_tape_mount run_prog=%s\n", ocmd.c_str());
1982 while ((status = run_program_full_output(ocmd.c_str(), max_open_wait/2, results)) != 0) {
1987 Dmsg5(100, "Device %s cannot be %smounted. stat=%d result=%s ERR=%s\n", print_name(),
1988 (mount ? "" : "un"), status, results, be.bstrerror(status));
1989 Mmsg(errmsg, _("Device %s cannot be %smounted. ERR=%s\n"),
1990 print_name(), (mount ? "" : "un"), be.bstrerror(status));
1993 free_pool_memory(results);
1994 Dmsg0(200, "============ mount=0\n");
1999 set_mounted(mount); /* set/clear mounted flag */
2000 free_pool_memory(results);
2001 Dmsg1(200, "============ mount=%d\n", mount);
2006 * (Un)mount the device (either a FILE or DVD device)
2008 bool DEVICE::do_file_mount(int mount, int dotimeout)
2010 POOL_MEM ocmd(PM_FNAME);
2014 struct dirent *entry, *result;
2015 int status, tries, name_max, count;
2020 icmd = device->mount_command;
2022 icmd = device->unmount_command;
2025 clear_freespace_ok();
2026 edit_mount_codes(ocmd, icmd);
2028 Dmsg2(100, "do_file_mount: cmd=%s mounted=%d\n", ocmd.c_str(), !!is_mounted());
2031 /* Try at most 10 times to (un)mount the device. This should perhaps be configurable. */
2036 results = get_memory(4000);
2038 /* If busy retry each second */
2039 Dmsg1(100, "do_file_mount run_prog=%s\n", ocmd.c_str());
2040 while ((status = run_program_full_output(ocmd.c_str(), max_open_wait/2, results)) != 0) {
2041 /* Doesn't work with internationalization (This is not a problem) */
2042 if (mount && fnmatch("*is already mounted on*", results, 0) == 0) {
2045 if (!mount && fnmatch("* not mounted*", results, 0) == 0) {
2049 /* Sometimes the device cannot be mounted because it is already mounted.
2050 * Try to unmount it, then remount it */
2052 Dmsg1(400, "Trying to unmount the device %s...\n", print_name());
2053 do_file_mount(0, 0);
2058 Dmsg5(100, "Device %s cannot be %smounted. stat=%d result=%s ERR=%s\n", print_name(),
2059 (mount ? "" : "un"), status, results, be.bstrerror(status));
2060 Mmsg(errmsg, _("Device %s cannot be %smounted. ERR=%s\n"),
2061 print_name(), (mount ? "" : "un"), be.bstrerror(status));
2064 * Now, just to be sure it is not mounted, try to read the filesystem.
2066 name_max = pathconf(".", _PC_NAME_MAX);
2067 if (name_max < 1024) {
2071 if (!(dp = opendir(device->mount_point))) {
2074 Dmsg3(100, "do_file_mount: failed to open dir %s (dev=%s), ERR=%s\n",
2075 device->mount_point, print_name(), be.bstrerror());
2079 entry = (struct dirent *)malloc(sizeof(struct dirent) + name_max + 1000);
2082 if ((readdir_r(dp, entry, &result) != 0) || (result == NULL)) {
2084 Dmsg2(129, "do_file_mount: failed to find suitable file in dir %s (dev=%s)\n",
2085 device->mount_point, print_name());
2088 if ((strcmp(result->d_name, ".")) && (strcmp(result->d_name, "..")) && (strcmp(result->d_name, ".keep"))) {
2089 count++; /* result->d_name != ., .. or .keep (Gentoo-specific) */
2092 Dmsg2(129, "do_file_mount: ignoring %s in %s\n", result->d_name, device->mount_point);
2098 Dmsg1(100, "do_file_mount: got %d files in the mount point (not counting ., .. and .keep)\n", count);
2101 /* If we got more than ., .. and .keep */
2102 /* there must be something mounted */
2104 Dmsg1(100, "Did Mount by count=%d\n", count);
2107 /* An unmount request. We failed to unmount - report an error */
2109 free_pool_memory(results);
2110 Dmsg0(200, "== error mount=1 wanted unmount\n");
2116 free_pool_memory(results);
2117 Dmsg0(200, "============ mount=0\n");
2122 set_mounted(mount); /* set/clear mounted flag */
2123 free_pool_memory(results);
2124 /* Do not check free space when unmounting */
2125 if (mount && !update_freespace()) {
2128 Dmsg1(200, "============ mount=%d\n", mount);
2133 * Edit codes into (Un)MountCommand, Write(First)PartCommand
2135 * %a = archive device name
2136 * %e = erase (set if cannot mount and first part)
2139 * %v = last part name
2141 * omsg = edited output message
2142 * imsg = input string containing edit codes (%x)
2145 void DEVICE::edit_mount_codes(POOL_MEM &omsg, const char *imsg)
2151 POOL_MEM archive_name(PM_FNAME);
2153 omsg.c_str()[0] = 0;
2154 Dmsg1(800, "edit_mount_codes: %s\n", imsg);
2155 for (p=imsg; *p; p++) {
2165 if (num_dvd_parts == 0) {
2166 if (truncating || blank_dvd) {
2176 bsnprintf(add, sizeof(add), "%d", part);
2180 str = device->mount_point;
2183 make_spooled_dvd_filename(this, archive_name);
2184 str = archive_name.c_str();
2198 Dmsg1(1900, "add_str %s\n", str);
2199 pm_strcat(omsg, (char *)str);
2200 Dmsg1(1800, "omsg=%s\n", omsg.c_str());
2204 /* return the last timer interval (ms)
2205 * or 0 if something goes wrong
2207 btime_t DEVICE::get_timer_count()
2209 btime_t temp = last_timer;
2210 last_timer = get_current_btime();
2211 temp = last_timer - temp; /* get elapsed time */
2212 return (temp>0)?temp:0; /* take care of skewed clock */
2216 ssize_t DEVICE::read(void *buf, size_t len)
2222 read_len = d_read(m_fd, buf, len);
2224 last_tick = get_timer_count();
2226 DevReadTime += last_tick;
2227 VolCatInfo.VolReadTime += last_tick;
2229 if (read_len > 0) { /* skip error */
2230 DevReadBytes += read_len;
2237 ssize_t DEVICE::write(const void *buf, size_t len)
2243 write_len = d_write(m_fd, buf, len);
2245 last_tick = get_timer_count();
2247 DevWriteTime += last_tick;
2248 VolCatInfo.VolWriteTime += last_tick;
2250 if (write_len > 0) { /* skip error */
2251 DevWriteBytes += write_len;
2257 /* Return the resource name for the device */
2258 const char *DEVICE::name() const
2260 return device->hdr.name;
2263 /* Returns file position on tape or -1 */
2264 int32_t DEVICE::get_os_tape_file()
2266 struct mtget mt_stat;
2268 if (has_cap(CAP_MTIOCGET) &&
2269 d_ioctl(m_fd, MTIOCGET, (char *)&mt_stat) == 0) {
2270 return mt_stat.mt_fileno;
2276 dev_vol_name(DEVICE *dev)
2278 return dev->getVolCatName();
2283 * Free memory allocated for the device
2285 void DEVICE::term(void)
2287 Dmsg1(900, "term dev: %s\n", print_name());
2290 free_memory(dev_name);
2294 free_memory(prt_name);
2298 free_pool_memory(errmsg);
2301 pthread_mutex_destroy(&m_mutex);
2302 pthread_cond_destroy(&wait);
2303 pthread_cond_destroy(&wait_next_vol);
2304 pthread_mutex_destroy(&spool_mutex);
2305 // rwl_destroy(&lock);
2306 if (attached_dcrs) {
2307 delete attached_dcrs;
2308 attached_dcrs = NULL;
2317 * This routine initializes the device wait timers
2319 void init_device_wait_timers(DCR *dcr)
2321 DEVICE *dev = dcr->dev;
2322 JCR *jcr = dcr->jcr;
2324 /* ******FIXME******* put these on config variables */
2325 dev->min_wait = 60 * 60;
2326 dev->max_wait = 24 * 60 * 60;
2327 dev->max_num_wait = 9; /* 5 waits =~ 1 day, then 1 day at a time */
2328 dev->wait_sec = dev->min_wait;
2329 dev->rem_wait_sec = dev->wait_sec;
2333 jcr->min_wait = 60 * 60;
2334 jcr->max_wait = 24 * 60 * 60;
2335 jcr->max_num_wait = 9; /* 5 waits =~ 1 day, then 1 day at a time */
2336 jcr->wait_sec = jcr->min_wait;
2337 jcr->rem_wait_sec = jcr->wait_sec;
2342 void init_jcr_device_wait_timers(JCR *jcr)
2344 /* ******FIXME******* put these on config variables */
2345 jcr->min_wait = 60 * 60;
2346 jcr->max_wait = 24 * 60 * 60;
2347 jcr->max_num_wait = 9; /* 5 waits =~ 1 day, then 1 day at a time */
2348 jcr->wait_sec = jcr->min_wait;
2349 jcr->rem_wait_sec = jcr->wait_sec;
2355 * The dev timers are used for waiting on a particular device
2357 * Returns: true if time doubled
2358 * false if max time expired
2360 bool double_dev_wait_time(DEVICE *dev)
2362 dev->wait_sec *= 2; /* double wait time */
2363 if (dev->wait_sec > dev->max_wait) { /* but not longer than maxtime */
2364 dev->wait_sec = dev->max_wait;
2367 dev->rem_wait_sec = dev->wait_sec;
2368 if (dev->num_wait >= dev->max_num_wait) {
2375 void set_os_device_parameters(DCR *dcr)
2377 DEVICE *dev = dcr->dev;
2379 if (strcmp(dev->dev_name, "/dev/null") == 0) {
2380 return; /* no use trying to set /dev/null */
2383 #if defined(HAVE_LINUX_OS) || defined(HAVE_WIN32)
2386 Dmsg0(100, "In set_os_device_parameters\n");
2387 #if defined(MTSETBLK)
2388 if (dev->min_block_size == dev->max_block_size &&
2389 dev->min_block_size == 0) { /* variable block mode */
2390 mt_com.mt_op = MTSETBLK;
2391 mt_com.mt_count = 0;
2392 Dmsg0(100, "Set block size to zero\n");
2393 if (dev->d_ioctl(dev->fd(), MTIOCTOP, (char *)&mt_com) < 0) {
2394 dev->clrerror(MTSETBLK);
2398 #if defined(MTSETDRVBUFFER)
2399 if (getuid() == 0) { /* Only root can do this */
2400 mt_com.mt_op = MTSETDRVBUFFER;
2401 mt_com.mt_count = MT_ST_CLEARBOOLEANS;
2402 if (!dev->has_cap(CAP_TWOEOF)) {
2403 mt_com.mt_count |= MT_ST_TWO_FM;
2405 if (dev->has_cap(CAP_EOM)) {
2406 mt_com.mt_count |= MT_ST_FAST_MTEOM;
2408 Dmsg0(100, "MTSETDRVBUFFER\n");
2409 if (dev->d_ioctl(dev->fd(), MTIOCTOP, (char *)&mt_com) < 0) {
2410 dev->clrerror(MTSETDRVBUFFER);
2417 #ifdef HAVE_NETBSD_OS
2419 if (dev->min_block_size == dev->max_block_size &&
2420 dev->min_block_size == 0) { /* variable block mode */
2421 mt_com.mt_op = MTSETBSIZ;
2422 mt_com.mt_count = 0;
2423 if (dev->d_ioctl(dev->fd(), MTIOCTOP, (char *)&mt_com) < 0) {
2424 dev->clrerror(MTSETBSIZ);
2426 /* Get notified at logical end of tape */
2427 mt_com.mt_op = MTEWARN;
2428 mt_com.mt_count = 1;
2429 if (dev->d_ioctl(dev->fd(), MTIOCTOP, (char *)&mt_com) < 0) {
2430 dev->clrerror(MTEWARN);
2436 #if HAVE_FREEBSD_OS || HAVE_OPENBSD_OS
2438 if (dev->min_block_size == dev->max_block_size &&
2439 dev->min_block_size == 0) { /* variable block mode */
2440 mt_com.mt_op = MTSETBSIZ;
2441 mt_com.mt_count = 0;
2442 if (dev->d_ioctl(dev->fd(), MTIOCTOP, (char *)&mt_com) < 0) {
2443 dev->clrerror(MTSETBSIZ);
2446 #if defined(MTIOCSETEOTMODEL)
2448 if (dev->has_cap(CAP_TWOEOF)) {
2453 if (dev->d_ioctl(dev->fd(), MTIOCSETEOTMODEL, (caddr_t)&neof) < 0) {
2455 dev->dev_errno = errno; /* save errno */
2456 Mmsg2(dev->errmsg, _("Unable to set eotmodel on device %s: ERR=%s\n"),
2457 dev->print_name(), be.bstrerror(dev->dev_errno));
2458 Jmsg(dcr->jcr, M_FATAL, 0, dev->errmsg);
2466 if (dev->min_block_size == dev->max_block_size &&
2467 dev->min_block_size == 0) { /* variable block mode */
2468 mt_com.mt_op = MTSRSZ;
2469 mt_com.mt_count = 0;
2470 if (dev->d_ioctl(dev->fd(), MTIOCTOP, (char *)&mt_com) < 0) {
2471 dev->clrerror(MTSRSZ);
2478 static bool dev_get_os_pos(DEVICE *dev, struct mtget *mt_stat)
2480 Dmsg0(100, "dev_get_os_pos\n");
2481 return dev->has_cap(CAP_MTIOCGET) &&
2482 dev->d_ioctl(dev->fd(), MTIOCGET, (char *)mt_stat) == 0 &&
2483 mt_stat->mt_fileno >= 0;
2486 static const char *modes[] = {
2487 "CREATE_READ_WRITE",
2494 static const char *mode_to_str(int mode)
2496 static char buf[100];
2497 if (mode < 1 || mode > 4) {
2498 bsnprintf(buf, sizeof(buf), "BAD mode=%d", mode);
2501 return modes[mode-1];