2 Bacula® - The Network Backup Solution
4 Copyright (C) 2000-2012 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 Jmsg3(jcr, M_WARNING, 0, _("Max block size %u not multiple of device %s block size=%d.\n"),
250 dev->max_block_size, dev->print_name(), TAPE_BSIZE);
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 for 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);
294 if ((errstat = pthread_mutex_init(&dev->read_acquire_mutex, NULL)) != 0) {
296 dev->dev_errno = errstat;
297 Mmsg1(dev->errmsg, _("Unable to init mutex: ERR=%s\n"), be.bstrerror(errstat));
298 Jmsg0(jcr, M_ERROR_TERM, 0, dev->errmsg);
301 if ((errstat = rwl_init(&dev->lock)) != 0) {
303 dev->dev_errno = errstat;
304 Mmsg1(dev->errmsg, _("Unable to init mutex: ERR=%s\n"), be.bstrerror(errstat));
305 Jmsg0(jcr, M_ERROR_TERM, 0, dev->errmsg);
310 dev->attached_dcrs = New(dlist(dcr, &dcr->dev_link));
311 Dmsg2(100, "init_dev: tape=%d dev_name=%s\n", dev->is_tape(), dev->dev_name);
312 dev->initiated = true;
317 /* default primitives are designed for file */
318 int DEVICE::d_open(const char *pathname, int flags)
320 return ::open(pathname, flags);
323 int DEVICE::d_close(int fd)
328 int DEVICE::d_ioctl(int fd, ioctl_req_t request, char *mt_com)
330 return ::ioctl(fd, request, mt_com);
333 ssize_t DEVICE::d_read(int fd, void *buffer, size_t count)
335 return ::read(fd, buffer, count);
338 ssize_t DEVICE::d_write(int fd, const void *buffer, size_t count)
340 return ::write(fd, buffer, count);
344 * Open the device with the operating system and
345 * initialize buffer pointers.
347 * Returns: true on success
350 * Note, for a tape, the VolName is the name we give to the
351 * volume (not really used here), but for a file, the
352 * VolName represents the name of the file to be created/opened.
353 * In the case of a file, the full name is the device name
354 * (archive_name) with the VolName concatenated.
357 DEVICE::open(DCR *dcr, int omode)
361 if (openmode == omode) {
366 Dmsg0(100, "Close fd for mode change.\n");
367 preserve = state & (ST_LABEL|ST_APPEND|ST_READ);
371 dcr->setVolCatName(dcr->VolumeName);
372 VolCatInfo = dcr->VolCatInfo; /* structure assign */
375 Dmsg4(100, "open dev: type=%d dev_name=%s vol=%s mode=%s\n", dev_type,
376 print_name(), getVolCatName(), mode_to_str(omode));
377 state &= ~(ST_LABEL|ST_APPEND|ST_READ|ST_EOT|ST_WEOT|ST_EOF);
378 label_type = B_BACULA_LABEL;
380 if (is_tape() || is_fifo()) {
381 open_tape_device(dcr, omode);
382 } else if (is_ftp()) {
383 open_device(dcr, omode);
385 Dmsg1(100, "call open_file_device mode=%s\n", mode_to_str(omode));
386 open_file_device(dcr, omode);
388 state |= preserve; /* reset any important state info */
389 Dmsg2(100, "preserve=0x%x fd=%d\n", preserve, m_fd);
393 void DEVICE::set_mode(int new_mode)
396 case CREATE_READ_WRITE:
397 mode = O_CREAT | O_RDWR | O_BINARY;
399 case OPEN_READ_WRITE:
400 mode = O_RDWR | O_BINARY;
403 mode = O_RDONLY | O_BINARY;
405 case OPEN_WRITE_ONLY:
406 mode = O_WRONLY | O_BINARY;
409 Emsg0(M_ABORT, 0, _("Illegal mode given to open dev.\n"));
415 void DEVICE::open_tape_device(DCR *dcr, int omode)
418 int timeout = max_open_wait;
419 #if !defined(HAVE_WIN32)
421 utime_t start_time = time(NULL);
424 mount(1); /* do mount if required */
426 Dmsg0(100, "Open dev: device is tape\n");
428 get_autochanger_loaded_slot(dcr);
437 if (is_fifo() && timeout) {
439 tid = start_thread_timer(dcr->jcr, pthread_self(), timeout);
441 Dmsg2(100, "Try open %s mode=%s\n", print_name(), mode_to_str(omode));
442 #if defined(HAVE_WIN32)
445 if ((m_fd = d_open(dev_name, mode)) < 0) {
452 /* If busy retry each second for max_open_wait seconds */
454 /* Try non-blocking open */
455 m_fd = d_open(dev_name, mode+O_NONBLOCK);
459 Dmsg5(100, "Open error on %s omode=%d mode=%x errno=%d: ERR=%s\n",
460 print_name(), omode, mode, errno, be.bstrerror());
462 /* Tape open, now rewind it */
463 Dmsg0(100, "Rewind after open\n");
464 mt_com.mt_op = MTREW;
466 /* rewind only if dev is a tape */
467 if (is_tape() && (d_ioctl(m_fd, MTIOCTOP, (char *)&mt_com) < 0)) {
469 dev_errno = errno; /* set error status from rewind */
472 Dmsg2(100, "Rewind error on %s close: ERR=%s\n", print_name(),
473 be.bstrerror(dev_errno));
474 /* If we get busy, device is probably rewinding, try again */
475 if (dev_errno != EBUSY) {
476 break; /* error -- no medium */
479 /* Got fd and rewind worked, so we must have medium in drive */
481 m_fd = d_open(dev_name, mode); /* open normally */
485 Dmsg5(100, "Open error on %s omode=%d mode=%x errno=%d: ERR=%s\n",
486 print_name(), omode, mode, errno, be.bstrerror());
491 set_os_device_parameters(dcr); /* do system dependent stuff */
492 break; /* Successfully opened and rewound */
496 /* Exceed wait time ? */
497 if (time(NULL) - start_time >= max_open_wait) {
498 break; /* yes, get out */
505 Mmsg2(errmsg, _("Unable to open device %s: ERR=%s\n"),
506 print_name(), be.bstrerror(dev_errno));
507 Dmsg1(100, "%s", errmsg);
510 /* Stop any open() timer we started */
512 stop_thread_timer(tid);
515 Dmsg1(100, "open dev: tape %d opened\n", m_fd);
518 void DEVICE::open_device(DCR *dcr, int omode)
520 /* do nothing waiting to split open_file/tape_device */
526 void DEVICE::open_file_device(DCR *dcr, int omode)
528 POOL_MEM archive_name(PM_FNAME);
530 get_autochanger_loaded_slot(dcr);
533 * Handle opening of File Archive (not a tape)
536 pm_strcpy(archive_name, dev_name);
538 * If this is a virtual autochanger (i.e. changer_res != NULL)
539 * we simply use the device name, assuming it has been
540 * appropriately setup by the "autochanger".
542 if (!device->changer_res || device->changer_command[0] == 0) {
543 if (VolCatInfo.VolCatName[0] == 0) {
544 Mmsg(errmsg, _("Could not open file device %s. No Volume name given.\n"),
550 if (!IsPathSeparator(archive_name.c_str()[strlen(archive_name.c_str())-1])) {
551 pm_strcat(archive_name, "/");
553 pm_strcat(archive_name, getVolCatName());
556 mount(1); /* do mount if required */
560 /* If creating file, give 0640 permissions */
561 Dmsg3(100, "open disk: mode=%s open(%s, 0x%x, 0640)\n", mode_to_str(omode),
562 archive_name.c_str(), mode);
563 /* Use system open() */
564 if ((m_fd = ::open(archive_name.c_str(), mode, 0640)) < 0) {
567 Mmsg2(errmsg, _("Could not open: %s, ERR=%s\n"), archive_name.c_str(),
569 Dmsg1(100, "open failed: %s", errmsg);
576 Dmsg4(100, "open dev: disk fd=%d opened, part=%d/%d, part_size=%u\n",
577 m_fd, part, num_dvd_parts, part_size);
582 * Returns: true on success
585 bool DEVICE::rewind(DCR *dcr)
591 Dmsg3(400, "rewind res=%d fd=%d %s\n", num_reserved(), m_fd, print_name());
592 state &= ~(ST_EOT|ST_EOF|ST_WEOT); /* remove EOF/EOT flags */
593 block_num = file = 0;
600 mt_com.mt_op = MTREW;
602 /* If we get an I/O error on rewind, it is probably because
603 * the drive is actually busy. We loop for (about 5 minutes)
604 * retrying every 5 seconds.
606 for (i=max_rewind_wait; ; i -= 5) {
607 if (d_ioctl(m_fd, MTIOCTOP, (char *)&mt_com) < 0) {
610 if (i == max_rewind_wait) {
611 Dmsg1(200, "Rewind error, %s. retrying ...\n", be.bstrerror());
614 * This is a gross hack, because if the user has the
615 * device mounted (i.e. open), then uses mtx to load
616 * a tape, the current open file descriptor is invalid.
617 * So, we close the drive and re-open it.
620 int open_mode = openmode;
623 open(dcr, open_mode);
631 if (dev_errno == EIO) {
632 Mmsg1(errmsg, _("No tape loaded or drive offline on %s.\n"), print_name());
636 if (dev_errno == EIO && i > 0) {
637 Dmsg0(200, "Sleeping 5 seconds.\n");
642 Mmsg2(errmsg, _("Rewind error on %s. ERR=%s.\n"),
643 print_name(), be.bstrerror());
648 } else if (is_file()) {
649 if (lseek(dcr, (boffset_t)0, SEEK_SET) < 0) {
652 Mmsg2(errmsg, _("lseek error on %s. ERR=%s.\n"),
653 print_name(), be.bstrerror());
662 * Called to indicate that we have just read an
663 * EOF from the device.
665 void DEVICE::set_ateof()
677 * Called to indicate we are now at the end of the tape, and
678 * writing is not possible.
680 void DEVICE::set_ateot()
682 /* Make tape effectively read-only */
683 state |= (ST_EOF|ST_EOT|ST_WEOT);
688 * Position device to end of medium (end of data)
689 * Returns: true on succes
692 bool DEVICE::eod(DCR *dcr)
701 Mmsg1(errmsg, _("Bad call to eod. Device %s not open\n"), print_name());
705 #if defined (__digital__) && defined (__unix__)
706 return fsf(VolCatInfo.VolCatFiles);
709 Dmsg0(100, "Enter eod\n");
713 clear_eof(); /* remove EOF flag */
714 block_num = file = 0;
721 pos = lseek(dcr, (boffset_t)0, SEEK_END);
722 Dmsg1(200, "====== Seek to %lld\n", pos);
730 Mmsg2(errmsg, _("lseek error on %s. ERR=%s.\n"),
731 print_name(), be.bstrerror());
736 if (has_cap(CAP_FASTFSF) && !has_cap(CAP_EOM)) {
737 Dmsg0(100,"Using FAST FSF for EOM\n");
738 /* If unknown position, rewind */
739 if (get_os_tape_file() < 0) {
741 Dmsg0(100, "Rewind error\n");
745 mt_com.mt_op = MTFSF;
747 * ***FIXME*** fix code to handle case that INT16_MAX is
750 mt_com.mt_count = INT16_MAX; /* use big positive number */
751 if (mt_com.mt_count < 0) {
752 mt_com.mt_count = INT16_MAX; /* brain damaged system */
756 if (has_cap(CAP_MTIOCGET) && (has_cap(CAP_FASTFSF) || has_cap(CAP_EOM))) {
757 if (has_cap(CAP_EOM)) {
758 Dmsg0(100,"Using EOM for EOM\n");
759 mt_com.mt_op = MTEOM;
763 if (d_ioctl(m_fd, MTIOCTOP, (char *)&mt_com) < 0) {
765 clrerror(mt_com.mt_op);
766 Dmsg1(50, "ioctl error: %s\n", be.bstrerror());
768 Mmsg2(errmsg, _("ioctl MTEOM error on %s. ERR=%s.\n"),
769 print_name(), be.bstrerror());
774 os_file = get_os_tape_file();
778 Mmsg2(errmsg, _("ioctl MTIOCGET error on %s. ERR=%s.\n"),
779 print_name(), be.bstrerror());
783 Dmsg1(100, "EOD file=%d\n", os_file);
791 * Rewind then use FSF until EOT reached
794 Dmsg0(100, "Rewind error.\n");
798 * Move file by file to the end of the tape
801 for (file_num=file; !at_eot(); file_num++) {
802 Dmsg0(200, "eod: doing fsf 1\n");
804 Dmsg0(100, "fsf error.\n");
808 * Avoid infinite loop by ensuring we advance.
810 if (!at_eot() && file_num == (int)file) {
811 Dmsg1(100, "fsf did not advance from file %d\n", file_num);
813 os_file = get_os_tape_file();
815 Dmsg2(100, "Adjust file from %d to %d\n", file_num, os_file);
823 * Some drivers leave us after second EOF when doing
824 * MTEOM, so we must backup so that appending overwrites
827 if (has_cap(CAP_BSFATEOM)) {
828 /* Backup over EOF */
830 /* If BSF worked and fileno is known (not -1), set file */
831 os_file = get_os_tape_file();
833 Dmsg2(100, "BSFATEOF adjust file from %d to %d\n", file , os_file);
836 file++; /* wing it -- not correct on all OSes */
839 update_pos(dcr); /* update position */
841 Dmsg1(200, "EOD dev->file=%d\n", file);
846 * Set the position of the device -- only for files and DVD
847 * For other devices, there is no generic way to do it.
848 * Returns: true on succes
851 bool DEVICE::update_pos(DCR *dcr)
858 Mmsg0(errmsg, _("Bad device call. Device not open\n"));
859 Emsg1(M_FATAL, 0, "%s", errmsg);
866 pos = lseek(dcr, (boffset_t)0, SEEK_CUR);
870 Pmsg1(000, _("Seek error: ERR=%s\n"), be.bstrerror());
871 Mmsg2(errmsg, _("lseek error on %s. ERR=%s.\n"),
872 print_name(), be.bstrerror());
876 block_num = (uint32_t)pos;
877 file = (uint32_t)(pos >> 32);
884 * Return the status of the device. This was meant
885 * to be a generic routine. Unfortunately, it doesn't
886 * seem possible (at least I do not know how to do it
887 * currently), which means that for the moment, this
888 * routine has very little value.
892 uint32_t status_dev(DEVICE *dev)
894 struct mtget mt_stat;
897 if (dev->state & (ST_EOT | ST_WEOT)) {
901 if (dev->state & ST_EOF) {
905 if (dev->is_tape()) {
907 Pmsg0(-20,_(" Bacula status:"));
908 Pmsg2(-20,_(" file=%d block=%d\n"), dev->file, dev->block_num);
909 if (dev->d_ioctl(dev->fd(), MTIOCGET, (char *)&mt_stat) < 0) {
911 dev->dev_errno = errno;
912 Mmsg2(dev->errmsg, _("ioctl MTIOCGET error on %s. ERR=%s.\n"),
913 dev->print_name(), be.bstrerror());
916 Pmsg0(-20, _(" Device status:"));
918 #if defined(HAVE_LINUX_OS)
919 if (GMT_EOF(mt_stat.mt_gstat)) {
923 if (GMT_BOT(mt_stat.mt_gstat)) {
927 if (GMT_EOT(mt_stat.mt_gstat)) {
931 if (GMT_SM(mt_stat.mt_gstat)) {
935 if (GMT_EOD(mt_stat.mt_gstat)) {
939 if (GMT_WR_PROT(mt_stat.mt_gstat)) {
941 Pmsg0(-20, " WR_PROT");
943 if (GMT_ONLINE(mt_stat.mt_gstat)) {
945 Pmsg0(-20, " ONLINE");
947 if (GMT_DR_OPEN(mt_stat.mt_gstat)) {
949 Pmsg0(-20, " DR_OPEN");
951 if (GMT_IM_REP_EN(mt_stat.mt_gstat)) {
952 stat |= BMT_IM_REP_EN;
953 Pmsg0(-20, " IM_REP_EN");
955 #elif defined(HAVE_WIN32)
956 if (GMT_EOF(mt_stat.mt_gstat)) {
960 if (GMT_BOT(mt_stat.mt_gstat)) {
964 if (GMT_EOT(mt_stat.mt_gstat)) {
968 if (GMT_EOD(mt_stat.mt_gstat)) {
972 if (GMT_WR_PROT(mt_stat.mt_gstat)) {
974 Pmsg0(-20, " WR_PROT");
976 if (GMT_ONLINE(mt_stat.mt_gstat)) {
978 Pmsg0(-20, " ONLINE");
980 if (GMT_DR_OPEN(mt_stat.mt_gstat)) {
982 Pmsg0(-20, " DR_OPEN");
984 if (GMT_IM_REP_EN(mt_stat.mt_gstat)) {
985 stat |= BMT_IM_REP_EN;
986 Pmsg0(-20, " IM_REP_EN");
989 #endif /* !SunOS && !OSF */
990 if (dev->has_cap(CAP_MTIOCGET)) {
991 Pmsg2(-20, _(" file=%d block=%d\n"), mt_stat.mt_fileno, mt_stat.mt_blkno);
993 Pmsg2(-20, _(" file=%d block=%d\n"), -1, -1);
996 stat |= BMT_ONLINE | BMT_BOT;
1003 * Load medium in device
1004 * Returns: true on success
1007 bool load_dev(DEVICE *dev)
1013 if (dev->fd() < 0) {
1014 dev->dev_errno = EBADF;
1015 Mmsg0(dev->errmsg, _("Bad call to load_dev. Device not open\n"));
1016 Emsg0(M_FATAL, 0, dev->errmsg);
1019 if (!(dev->is_tape())) {
1023 Dmsg0(200, "stored: MTLOAD command not available\n");
1025 dev->dev_errno = ENOTTY; /* function not available */
1026 Mmsg2(dev->errmsg, _("ioctl MTLOAD error on %s. ERR=%s.\n"),
1027 dev->print_name(), be.bstrerror());
1031 dev->block_num = dev->file = 0;
1034 mt_com.mt_op = MTLOAD;
1035 mt_com.mt_count = 1;
1036 if (dev->d_ioctl(dev->fd(), MTIOCTOP, (char *)&mt_com) < 0) {
1038 dev->dev_errno = errno;
1039 Mmsg2(dev->errmsg, _("ioctl MTLOAD error on %s. ERR=%s.\n"),
1040 dev->print_name(), be.bstrerror());
1048 * Rewind device and put it offline
1049 * Returns: true on success
1052 bool DEVICE::offline()
1057 return true; /* device not open */
1060 state &= ~(ST_APPEND|ST_READ|ST_EOT|ST_EOF|ST_WEOT); /* remove EOF/EOT flags */
1061 block_num = file = 0;
1065 mt_com.mt_op = MTOFFL;
1066 mt_com.mt_count = 1;
1067 if (d_ioctl(m_fd, MTIOCTOP, (char *)&mt_com) < 0) {
1070 Mmsg2(errmsg, _("ioctl MTOFFL error on %s. ERR=%s.\n"),
1071 print_name(), be.bstrerror());
1074 Dmsg1(100, "Offlined device %s\n", print_name());
1078 bool DEVICE::offline_or_rewind()
1083 if (has_cap(CAP_OFFLINEUNMOUNT)) {
1087 * Note, this rewind probably should not be here (it wasn't
1088 * in prior versions of Bacula), but on FreeBSD, this is
1089 * needed in the case the tape was "frozen" due to an error
1090 * such as backspacing after writing and EOF. If it is not
1091 * done, all future references to the drive get and I/O error.
1094 return rewind(NULL);
1099 * Foward space a file
1100 * Returns: true on success
1103 bool DEVICE::fsf(int num)
1105 int32_t os_file = 0;
1111 Mmsg0(errmsg, _("Bad call to fsf. Device not open\n"));
1112 Emsg0(M_FATAL, 0, errmsg);
1122 Mmsg1(errmsg, _("Device %s at End of Tape.\n"), print_name());
1126 Dmsg0(200, "ST_EOF set on entry to FSF\n");
1129 Dmsg0(100, "fsf\n");
1132 * If Fast forward space file is set, then we
1133 * use MTFSF to forward space and MTIOCGET
1134 * to get the file position. We assume that
1135 * the SCSI driver will ensure that we do not
1136 * forward space past the end of the medium.
1138 if (has_cap(CAP_FSF) && has_cap(CAP_MTIOCGET) && has_cap(CAP_FASTFSF)) {
1140 mt_com.mt_op = MTFSF;
1141 mt_com.mt_count = num;
1142 stat = d_ioctl(m_fd, MTIOCTOP, (char *)&mt_com);
1144 my_errno = errno; /* save errno */
1145 } else if ((os_file=get_os_tape_file()) < 0) {
1146 my_errno = errno; /* save errno */
1148 if (my_errno != 0) {
1151 Dmsg0(200, "Set ST_EOT\n");
1153 Mmsg2(errmsg, _("ioctl MTFSF error on %s. ERR=%s.\n"),
1154 print_name(), be.bstrerror(my_errno));
1155 Dmsg1(200, "%s", errmsg);
1159 Dmsg1(200, "fsf file=%d\n", os_file);
1165 * Here if CAP_FSF is set, and virtually all drives
1166 * these days support it, we read a record, then forward
1167 * space one file. Using this procedure, which is slow,
1168 * is the only way we can be sure that we don't read
1169 * two consecutive EOF marks, which means End of Data.
1171 } else if (has_cap(CAP_FSF)) {
1174 Dmsg0(200, "FSF has cap_fsf\n");
1175 if (max_block_size == 0) {
1176 rbuf_len = DEFAULT_BLOCK_SIZE;
1178 rbuf_len = max_block_size;
1180 rbuf = get_memory(rbuf_len);
1181 mt_com.mt_op = MTFSF;
1182 mt_com.mt_count = 1;
1183 while (num-- && !at_eot()) {
1184 Dmsg0(100, "Doing read before fsf\n");
1185 if ((stat = this->read((char *)rbuf, rbuf_len)) < 0) {
1186 if (errno == ENOMEM) { /* tape record exceeds buf len */
1187 stat = rbuf_len; /* This is OK */
1189 * On IBM drives, they return ENOSPC at EOM
1190 * instead of EOF status
1192 } else if (at_eof() && errno == ENOSPC) {
1198 Dmsg2(100, "Set ST_EOT read errno=%d. ERR=%s\n", dev_errno,
1200 Mmsg2(errmsg, _("read error on %s. ERR=%s.\n"),
1201 print_name(), be.bstrerror());
1202 Dmsg1(100, "%s", errmsg);
1206 if (stat == 0) { /* EOF */
1207 Dmsg1(100, "End of File mark from read. File=%d\n", file+1);
1208 /* Two reads of zero means end of tape */
1211 Dmsg0(100, "Set ST_EOT\n");
1217 } else { /* Got data */
1222 Dmsg0(100, "Doing MTFSF\n");
1223 stat = d_ioctl(m_fd, MTIOCTOP, (char *)&mt_com);
1224 if (stat < 0) { /* error => EOT */
1227 Dmsg0(100, "Set ST_EOT\n");
1229 Mmsg2(errmsg, _("ioctl MTFSF error on %s. ERR=%s.\n"),
1230 print_name(), be.bstrerror());
1231 Dmsg0(100, "Got < 0 for MTFSF\n");
1232 Dmsg1(100, "%s", errmsg);
1240 * No FSF, so use FSR to simulate it
1243 Dmsg0(200, "Doing FSR for FSF\n");
1244 while (num-- && !at_eot()) {
1245 fsr(INT32_MAX); /* returns -1 on EOF or EOT */
1249 Mmsg1(errmsg, _("Device %s at End of Tape.\n"), print_name());
1255 Dmsg1(200, "Return %d from FSF\n", stat);
1257 Dmsg0(200, "ST_EOF set on exit FSF\n");
1260 Dmsg0(200, "ST_EOT set on exit FSF\n");
1262 Dmsg1(200, "Return from FSF file=%d\n", file);
1267 * Backward space a file
1268 * Returns: false on failure
1271 bool DEVICE::bsf(int num)
1278 Mmsg0(errmsg, _("Bad call to bsf. Device not open\n"));
1279 Emsg0(M_FATAL, 0, errmsg);
1284 Mmsg1(errmsg, _("Device %s cannot BSF because it is not a tape.\n"),
1289 Dmsg0(100, "bsf\n");
1295 mt_com.mt_op = MTBSF;
1296 mt_com.mt_count = num;
1297 stat = d_ioctl(m_fd, MTIOCTOP, (char *)&mt_com);
1301 Mmsg2(errmsg, _("ioctl MTBSF error on %s. ERR=%s.\n"),
1302 print_name(), be.bstrerror());
1309 * Foward space num records
1310 * Returns: false on failure
1313 bool DEVICE::fsr(int num)
1320 Mmsg0(errmsg, _("Bad call to fsr. Device not open\n"));
1321 Emsg0(M_FATAL, 0, errmsg);
1329 if (!has_cap(CAP_FSR)) {
1330 Mmsg1(errmsg, _("ioctl MTFSR not permitted on %s.\n"), print_name());
1334 Dmsg1(100, "fsr %d\n", num);
1335 mt_com.mt_op = MTFSR;
1336 mt_com.mt_count = num;
1337 stat = d_ioctl(m_fd, MTIOCTOP, (char *)&mt_com);
1343 struct mtget mt_stat;
1345 Dmsg1(100, "FSF fail: ERR=%s\n", be.bstrerror());
1346 if (dev_get_os_pos(this, &mt_stat)) {
1347 Dmsg4(100, "Adjust from %d:%d to %d:%d\n", file,
1348 block_num, mt_stat.mt_fileno, mt_stat.mt_blkno);
1349 file = mt_stat.mt_fileno;
1350 block_num = mt_stat.mt_blkno;
1358 Mmsg3(errmsg, _("ioctl MTFSR %d error on %s. ERR=%s.\n"),
1359 num, print_name(), be.bstrerror());
1365 * Backward space a record
1366 * Returns: false on failure
1369 bool DEVICE::bsr(int num)
1376 Mmsg0(errmsg, _("Bad call to bsr_dev. Device not open\n"));
1377 Emsg0(M_FATAL, 0, errmsg);
1385 if (!has_cap(CAP_BSR)) {
1386 Mmsg1(errmsg, _("ioctl MTBSR not permitted on %s.\n"), print_name());
1390 Dmsg0(100, "bsr_dev\n");
1394 mt_com.mt_op = MTBSR;
1395 mt_com.mt_count = num;
1396 stat = d_ioctl(m_fd, MTIOCTOP, (char *)&mt_com);
1400 Mmsg2(errmsg, _("ioctl MTBSR error on %s. ERR=%s.\n"),
1401 print_name(), be.bstrerror());
1406 void DEVICE::lock_door()
1410 mt_com.mt_op = MTLOCK;
1411 mt_com.mt_count = 1;
1412 d_ioctl(m_fd, MTIOCTOP, (char *)&mt_com);
1416 void DEVICE::unlock_door()
1420 mt_com.mt_op = MTUNLOCK;
1421 mt_com.mt_count = 1;
1422 d_ioctl(m_fd, MTIOCTOP, (char *)&mt_com);
1426 void DEVICE::set_slot(int32_t slot)
1429 if (vol) vol->clear_slot();
1432 void DEVICE::clear_slot()
1435 if (vol) vol->set_slot(-1);
1441 * Reposition the device to file, block
1442 * Returns: false on failure
1445 bool DEVICE::reposition(DCR *dcr, uint32_t rfile, uint32_t rblock)
1449 Mmsg0(errmsg, _("Bad call to reposition. Device not open\n"));
1450 Emsg0(M_FATAL, 0, errmsg);
1455 boffset_t pos = (((boffset_t)rfile)<<32) | rblock;
1456 Dmsg1(100, "===== lseek to %d\n", (int)pos);
1457 if (lseek(dcr, pos, SEEK_SET) == (boffset_t)-1) {
1460 Mmsg2(errmsg, _("lseek error on %s. ERR=%s.\n"),
1461 print_name(), be.bstrerror());
1470 /* After this point, we are tape only */
1471 Dmsg4(100, "reposition from %u:%u to %u:%u\n", file, block_num, rfile, rblock);
1473 Dmsg0(100, "Rewind\n");
1474 if (!rewind(NULL)) {
1479 Dmsg1(100, "fsf %d\n", rfile-file);
1480 if (!fsf(rfile-file)) {
1481 Dmsg1(100, "fsf failed! ERR=%s\n", bstrerror());
1484 Dmsg2(100, "wanted_file=%d at_file=%d\n", rfile, file);
1486 if (rblock < block_num) {
1487 Dmsg2(100, "wanted_blk=%d at_blk=%d\n", rblock, block_num);
1488 Dmsg0(100, "bsf 1\n");
1490 Dmsg0(100, "fsf 1\n");
1492 Dmsg2(100, "wanted_blk=%d at_blk=%d\n", rblock, block_num);
1494 if (has_cap(CAP_POSITIONBLOCKS) && rblock > block_num) {
1495 /* Ignore errors as Bacula can read to the correct block */
1496 Dmsg1(100, "fsr %d\n", rblock-block_num);
1497 return fsr(rblock-block_num);
1499 while (rblock > block_num) {
1500 if (!read_block_from_dev(dcr, NO_BLOCK_NUMBER_CHECK)) {
1503 Dmsg2(30, "Failed to find requested block on %s: ERR=%s",
1504 print_name(), be.bstrerror());
1507 Dmsg2(300, "moving forward wanted_blk=%d at_blk=%d\n", rblock, block_num);
1516 * Write an end of file on the device
1517 * Returns: true on success
1520 bool DEVICE::weof(int num)
1524 Dmsg1(129, "=== weof_dev=%s\n", print_name());
1528 Mmsg0(errmsg, _("Bad call to weof_dev. Device not open\n"));
1529 Emsg0(M_FATAL, 0, errmsg);
1537 if (!can_append()) {
1538 Mmsg0(errmsg, _("Attempt to WEOF on non-appendable Volume\n"));
1539 Emsg0(M_FATAL, 0, errmsg);
1545 mt_com.mt_op = MTWEOF;
1546 mt_com.mt_count = num;
1547 stat = d_ioctl(m_fd, MTIOCTOP, (char *)&mt_com);
1556 Mmsg2(errmsg, _("ioctl MTWEOF error on %s. ERR=%s.\n"),
1557 print_name(), be.bstrerror());
1565 * If implemented in system, clear the tape
1568 void DEVICE::clrerror(int func)
1570 const char *msg = NULL;
1573 dev_errno = errno; /* save errno */
1575 VolCatInfo.VolCatErrors++;
1582 if (errno == ENOTTY || errno == ENOSYS) { /* Function not implemented */
1585 break; /* ignore message printed later */
1588 clear_cap(CAP_EOF); /* turn off feature */
1593 clear_cap(CAP_EOM); /* turn off feature */
1598 clear_cap(CAP_FSF); /* turn off feature */
1602 clear_cap(CAP_BSF); /* turn off feature */
1606 clear_cap(CAP_FSR); /* turn off feature */
1610 clear_cap(CAP_BSR); /* turn off feature */
1620 #ifdef MTSETDRVBUFFER
1621 case MTSETDRVBUFFER:
1622 msg = "MTSETDRVBUFFER";
1655 bsnprintf(buf, sizeof(buf), _("unknown func code %d"), func);
1661 Mmsg1(errmsg, _("I/O function \"%s\" not supported on this device.\n"), msg);
1662 Emsg0(M_ERROR, 0, errmsg);
1667 * Now we try different methods of clearing the error
1668 * status on the drive so that it is not locked for
1669 * further operations.
1672 /* On some systems such as NetBSD, this clears all errors */
1675 /* Found on Solaris */
1678 d_ioctl(m_fd, MTIOCLRERR);
1679 Dmsg0(200, "Did MTIOCLRERR\n");
1683 /* Typically on FreeBSD */
1687 /* Read and clear SCSI error status */
1688 union mterrstat mt_errstat;
1689 Dmsg2(200, "Doing MTIOCERRSTAT errno=%d ERR=%s\n", dev_errno,
1690 be.bstrerror(dev_errno));
1691 d_ioctl(m_fd, MTIOCERRSTAT, (char *)&mt_errstat);
1695 /* Clear Subsystem Exception TRU64 */
1699 mt_com.mt_op = MTCSE;
1700 mt_com.mt_count = 1;
1701 /* Clear any error condition on the tape */
1702 d_ioctl(m_fd, MTIOCTOP, (char *)&mt_com);
1703 Dmsg0(200, "Did MTCSE\n");
1710 * Set to unload the current volume in the drive
1712 void DEVICE::set_unload()
1714 if (!m_unload && VolHdr.VolumeName[0] != 0) {
1716 memcpy(UnloadVolName, VolHdr.VolumeName, sizeof(UnloadVolName));
1722 * Clear volume header
1724 void DEVICE::clear_volhdr()
1726 Dmsg1(100, "Clear volhdr vol=%s\n", VolHdr.VolumeName);
1727 memset(&VolHdr, 0, sizeof(VolHdr));
1728 setVolCatInfo(false);
1735 void DEVICE::close()
1737 Dmsg1(100, "close_dev %s\n", print_name());
1738 offline_or_rewind();
1741 Dmsg2(100, "device %s already closed vol=%s\n", print_name(),
1743 return; /* already closed */
1756 unmount(1); /* do unmount if required */
1758 /* Clean up device packet so it can be reused */
1761 * Be careful not to clear items needed by the DVD driver
1762 * when it is closing a single part.
1764 state &= ~(ST_LABEL|ST_READ|ST_APPEND|ST_EOT|ST_WEOT|ST_EOF|
1765 ST_MOUNTED|ST_MEDIA|ST_SHORT);
1766 label_type = B_BACULA_LABEL;
1767 file = block_num = 0;
1770 EndFile = EndBlock = 0;
1773 memset(&VolCatInfo, 0, sizeof(VolCatInfo));
1775 stop_thread_timer(tid);
1781 * This call closes the device, but it is used in DVD handling
1782 * where we close one part and then open the next part. The
1783 * difference between close_part() and close() is that close_part()
1784 * saves the state information of the device (e.g. the Volume lable,
1785 * the Volume Catalog record, ... This permits opening and closing
1786 * the Volume parts multiple times without losing track of what the
1787 * main Volume parameters are.
1789 void DEVICE::close_part(DCR * /*dcr*/)
1791 VOLUME_LABEL saveVolHdr;
1792 VOLUME_CAT_INFO saveVolCatInfo; /* Volume Catalog Information */
1795 saveVolHdr = VolHdr; /* structure assignment */
1796 saveVolCatInfo = VolCatInfo; /* structure assignment */
1797 close(); /* close current part */
1798 VolHdr = saveVolHdr; /* structure assignment */
1799 VolCatInfo = saveVolCatInfo; /* structure assignment */
1802 boffset_t DEVICE::lseek(DCR *dcr, boffset_t offset, int whence)
1806 #if defined(HAVE_WIN32)
1807 return ::_lseeki64(m_fd, (__int64)offset, whence);
1809 return ::lseek(m_fd, offset, whence);
1816 bool DEVICE::truncate(DCR *dcr) /* We need the DCR for DVD-writing */
1820 Dmsg1(100, "truncate %s\n", print_name());
1825 /* maybe we should rewind and write and eof ???? */
1826 return true; /* we don't really truncate tapes */
1828 if (ftruncate(m_fd, 0) != 0) {
1830 Mmsg2(errmsg, _("Unable to truncate device %s. ERR=%s\n"),
1831 print_name(), be.bstrerror());
1836 * Check for a successful ftruncate() and issue a work-around for devices
1837 * (mostly cheap NAS) that don't support truncation.
1838 * Workaround supplied by Martin Schmid as a solution to bug #1011.
1841 * 3. open new file with same mode
1842 * 4. change ownership to original
1845 if (fstat(m_fd, &st) != 0) {
1847 Mmsg2(errmsg, _("Unable to stat device %s. ERR=%s\n"),
1848 print_name(), be.bstrerror());
1852 if (st.st_size != 0) { /* ftruncate() didn't work */
1853 POOL_MEM archive_name(PM_FNAME);
1855 pm_strcpy(archive_name, dev_name);
1856 if (!IsPathSeparator(archive_name.c_str()[strlen(archive_name.c_str())-1])) {
1857 pm_strcat(archive_name, "/");
1859 pm_strcat(archive_name, dcr->VolumeName);
1861 Mmsg2(errmsg, _("Device %s doesn't support ftruncate(). Recreating file %s.\n"),
1862 print_name(), archive_name.c_str());
1864 /* Close file and blow it away */
1866 ::unlink(archive_name.c_str());
1868 /* Recreate the file -- of course, empty */
1869 set_mode(CREATE_READ_WRITE);
1870 if ((m_fd = ::open(archive_name.c_str(), mode, st.st_mode)) < 0) {
1873 Mmsg2(errmsg, _("Could not reopen: %s, ERR=%s\n"), archive_name.c_str(),
1875 Dmsg1(100, "reopen failed: %s", errmsg);
1876 Emsg0(M_FATAL, 0, errmsg);
1880 /* Reset proper owner */
1881 chown(archive_name.c_str(), st.st_uid, st.st_gid);
1891 * If timeout, wait until the mount command returns 0.
1892 * If !timeout, try to mount the device only once.
1894 bool DEVICE::mount(int timeout)
1896 Dmsg0(190, "Enter mount\n");
1906 if (device->mount_command) {
1907 return do_tape_mount(1, timeout);
1911 if (requires_mount() && device->mount_command) {
1912 return do_file_mount(1, timeout);
1923 * Unmount the device
1924 * If timeout, wait until the unmount command returns 0.
1925 * If !timeout, try to unmount the device only once.
1927 bool DEVICE::unmount(int timeout)
1929 Dmsg0(100, "Enter unmount\n");
1931 if (!is_mounted()) {
1939 if (device->unmount_command) {
1940 return do_tape_mount(0, timeout);
1945 if (requires_mount() && device->unmount_command) {
1946 return do_file_mount(0, timeout);
1957 * (Un)mount the device (for tape devices)
1959 bool DEVICE::do_tape_mount(int mount, int dotimeout)
1961 POOL_MEM ocmd(PM_FNAME);
1969 icmd = device->mount_command;
1971 icmd = device->unmount_command;
1974 edit_mount_codes(ocmd, icmd);
1976 Dmsg2(100, "do_tape_mount: cmd=%s mounted=%d\n", ocmd.c_str(), !!is_mounted());
1979 /* Try at most 10 times to (un)mount the device. This should perhaps be configurable. */
1984 results = get_memory(4000);
1986 /* If busy retry each second */
1987 Dmsg1(100, "do_tape_mount run_prog=%s\n", ocmd.c_str());
1988 while ((status = run_program_full_output(ocmd.c_str(), max_open_wait/2, results)) != 0) {
1993 Dmsg5(100, "Device %s cannot be %smounted. stat=%d result=%s ERR=%s\n", print_name(),
1994 (mount ? "" : "un"), status, results, be.bstrerror(status));
1995 Mmsg(errmsg, _("Device %s cannot be %smounted. ERR=%s\n"),
1996 print_name(), (mount ? "" : "un"), be.bstrerror(status));
1999 free_pool_memory(results);
2000 Dmsg0(200, "============ mount=0\n");
2005 set_mounted(mount); /* set/clear mounted flag */
2006 free_pool_memory(results);
2007 Dmsg1(200, "============ mount=%d\n", mount);
2012 * (Un)mount the device (either a FILE or DVD device)
2014 bool DEVICE::do_file_mount(int mount, int dotimeout)
2016 POOL_MEM ocmd(PM_FNAME);
2020 struct dirent *entry, *result;
2021 int status, tries, name_max, count;
2026 icmd = device->mount_command;
2028 icmd = device->unmount_command;
2031 clear_freespace_ok();
2032 edit_mount_codes(ocmd, icmd);
2034 Dmsg2(100, "do_file_mount: cmd=%s mounted=%d\n", ocmd.c_str(), !!is_mounted());
2037 /* Try at most 10 times to (un)mount the device. This should perhaps be configurable. */
2042 results = get_memory(4000);
2044 /* If busy retry each second */
2045 Dmsg1(100, "do_file_mount run_prog=%s\n", ocmd.c_str());
2046 while ((status = run_program_full_output(ocmd.c_str(), max_open_wait/2, results)) != 0) {
2047 /* Doesn't work with internationalization (This is not a problem) */
2048 if (mount && fnmatch("*is already mounted on*", results, 0) == 0) {
2051 if (!mount && fnmatch("* not mounted*", results, 0) == 0) {
2055 /* Sometimes the device cannot be mounted because it is already mounted.
2056 * Try to unmount it, then remount it */
2058 Dmsg1(400, "Trying to unmount the device %s...\n", print_name());
2059 do_file_mount(0, 0);
2064 Dmsg5(100, "Device %s cannot be %smounted. stat=%d result=%s ERR=%s\n", print_name(),
2065 (mount ? "" : "un"), status, results, be.bstrerror(status));
2066 Mmsg(errmsg, _("Device %s cannot be %smounted. ERR=%s\n"),
2067 print_name(), (mount ? "" : "un"), be.bstrerror(status));
2070 * Now, just to be sure it is not mounted, try to read the filesystem.
2072 name_max = pathconf(".", _PC_NAME_MAX);
2073 if (name_max < 1024) {
2077 if (!(dp = opendir(device->mount_point))) {
2080 Dmsg3(100, "do_file_mount: failed to open dir %s (dev=%s), ERR=%s\n",
2081 device->mount_point, print_name(), be.bstrerror());
2085 entry = (struct dirent *)malloc(sizeof(struct dirent) + name_max + 1000);
2088 if ((readdir_r(dp, entry, &result) != 0) || (result == NULL)) {
2090 Dmsg2(129, "do_file_mount: failed to find suitable file in dir %s (dev=%s)\n",
2091 device->mount_point, print_name());
2094 if ((strcmp(result->d_name, ".")) && (strcmp(result->d_name, "..")) && (strcmp(result->d_name, ".keep"))) {
2095 count++; /* result->d_name != ., .. or .keep (Gentoo-specific) */
2098 Dmsg2(129, "do_file_mount: ignoring %s in %s\n", result->d_name, device->mount_point);
2104 Dmsg1(100, "do_file_mount: got %d files in the mount point (not counting ., .. and .keep)\n", count);
2107 /* If we got more than ., .. and .keep */
2108 /* there must be something mounted */
2110 Dmsg1(100, "Did Mount by count=%d\n", count);
2113 /* An unmount request. We failed to unmount - report an error */
2115 free_pool_memory(results);
2116 Dmsg0(200, "== error mount=1 wanted unmount\n");
2122 free_pool_memory(results);
2123 Dmsg0(200, "============ mount=0\n");
2128 set_mounted(mount); /* set/clear mounted flag */
2129 free_pool_memory(results);
2130 /* Do not check free space when unmounting */
2131 if (mount && !update_freespace()) {
2134 Dmsg1(200, "============ mount=%d\n", mount);
2139 * Edit codes into (Un)MountCommand, Write(First)PartCommand
2141 * %a = archive device name
2142 * %e = erase (set if cannot mount and first part)
2145 * %v = last part name
2147 * omsg = edited output message
2148 * imsg = input string containing edit codes (%x)
2151 void DEVICE::edit_mount_codes(POOL_MEM &omsg, const char *imsg)
2157 POOL_MEM archive_name(PM_FNAME);
2159 omsg.c_str()[0] = 0;
2160 Dmsg1(800, "edit_mount_codes: %s\n", imsg);
2161 for (p=imsg; *p; p++) {
2171 if (num_dvd_parts == 0) {
2172 if (truncating || blank_dvd) {
2182 bsnprintf(add, sizeof(add), "%d", part);
2186 str = device->mount_point;
2189 make_spooled_dvd_filename(this, archive_name);
2190 str = archive_name.c_str();
2204 Dmsg1(1900, "add_str %s\n", str);
2205 pm_strcat(omsg, (char *)str);
2206 Dmsg1(1800, "omsg=%s\n", omsg.c_str());
2210 /* return the last timer interval (ms)
2211 * or 0 if something goes wrong
2213 btime_t DEVICE::get_timer_count()
2215 btime_t temp = last_timer;
2216 last_timer = get_current_btime();
2217 temp = last_timer - temp; /* get elapsed time */
2218 return (temp>0)?temp:0; /* take care of skewed clock */
2222 ssize_t DEVICE::read(void *buf, size_t len)
2228 read_len = d_read(m_fd, buf, len);
2230 last_tick = get_timer_count();
2232 DevReadTime += last_tick;
2233 VolCatInfo.VolReadTime += last_tick;
2235 if (read_len > 0) { /* skip error */
2236 DevReadBytes += read_len;
2243 ssize_t DEVICE::write(const void *buf, size_t len)
2249 write_len = d_write(m_fd, buf, len);
2251 last_tick = get_timer_count();
2253 DevWriteTime += last_tick;
2254 VolCatInfo.VolWriteTime += last_tick;
2256 if (write_len > 0) { /* skip error */
2257 DevWriteBytes += write_len;
2263 /* Return the resource name for the device */
2264 const char *DEVICE::name() const
2266 return device->hdr.name;
2269 /* Returns file position on tape or -1 */
2270 int32_t DEVICE::get_os_tape_file()
2272 struct mtget mt_stat;
2274 if (has_cap(CAP_MTIOCGET) &&
2275 d_ioctl(m_fd, MTIOCGET, (char *)&mt_stat) == 0) {
2276 return mt_stat.mt_fileno;
2282 dev_vol_name(DEVICE *dev)
2284 return dev->getVolCatName();
2289 * Free memory allocated for the device
2291 void DEVICE::term(void)
2293 Dmsg1(900, "term dev: %s\n", print_name());
2296 free_memory(dev_name);
2300 free_memory(prt_name);
2304 free_pool_memory(errmsg);
2307 pthread_mutex_destroy(&m_mutex);
2308 pthread_cond_destroy(&wait);
2309 pthread_cond_destroy(&wait_next_vol);
2310 pthread_mutex_destroy(&spool_mutex);
2311 // rwl_destroy(&lock);
2312 if (attached_dcrs) {
2313 delete attached_dcrs;
2314 attached_dcrs = NULL;
2323 * This routine initializes the device wait timers
2325 void init_device_wait_timers(DCR *dcr)
2327 DEVICE *dev = dcr->dev;
2328 JCR *jcr = dcr->jcr;
2330 /* ******FIXME******* put these on config variables */
2331 dev->min_wait = 60 * 60;
2332 dev->max_wait = 24 * 60 * 60;
2333 dev->max_num_wait = 9; /* 5 waits =~ 1 day, then 1 day at a time */
2334 dev->wait_sec = dev->min_wait;
2335 dev->rem_wait_sec = dev->wait_sec;
2339 jcr->min_wait = 60 * 60;
2340 jcr->max_wait = 24 * 60 * 60;
2341 jcr->max_num_wait = 9; /* 5 waits =~ 1 day, then 1 day at a time */
2342 jcr->wait_sec = jcr->min_wait;
2343 jcr->rem_wait_sec = jcr->wait_sec;
2348 void init_jcr_device_wait_timers(JCR *jcr)
2350 /* ******FIXME******* put these on config variables */
2351 jcr->min_wait = 60 * 60;
2352 jcr->max_wait = 24 * 60 * 60;
2353 jcr->max_num_wait = 9; /* 5 waits =~ 1 day, then 1 day at a time */
2354 jcr->wait_sec = jcr->min_wait;
2355 jcr->rem_wait_sec = jcr->wait_sec;
2361 * The dev timers are used for waiting on a particular device
2363 * Returns: true if time doubled
2364 * false if max time expired
2366 bool double_dev_wait_time(DEVICE *dev)
2368 dev->wait_sec *= 2; /* double wait time */
2369 if (dev->wait_sec > dev->max_wait) { /* but not longer than maxtime */
2370 dev->wait_sec = dev->max_wait;
2373 dev->rem_wait_sec = dev->wait_sec;
2374 if (dev->num_wait >= dev->max_num_wait) {
2381 void set_os_device_parameters(DCR *dcr)
2383 DEVICE *dev = dcr->dev;
2385 if (strcmp(dev->dev_name, "/dev/null") == 0) {
2386 return; /* no use trying to set /dev/null */
2389 #if defined(HAVE_LINUX_OS) || defined(HAVE_WIN32)
2392 Dmsg0(100, "In set_os_device_parameters\n");
2393 #if defined(MTSETBLK)
2394 if (dev->min_block_size == dev->max_block_size &&
2395 dev->min_block_size == 0) { /* variable block mode */
2396 mt_com.mt_op = MTSETBLK;
2397 mt_com.mt_count = 0;
2398 Dmsg0(100, "Set block size to zero\n");
2399 if (dev->d_ioctl(dev->fd(), MTIOCTOP, (char *)&mt_com) < 0) {
2400 dev->clrerror(MTSETBLK);
2404 #if defined(MTSETDRVBUFFER)
2405 if (getuid() == 0) { /* Only root can do this */
2406 mt_com.mt_op = MTSETDRVBUFFER;
2407 mt_com.mt_count = MT_ST_CLEARBOOLEANS;
2408 if (!dev->has_cap(CAP_TWOEOF)) {
2409 mt_com.mt_count |= MT_ST_TWO_FM;
2411 if (dev->has_cap(CAP_EOM)) {
2412 mt_com.mt_count |= MT_ST_FAST_MTEOM;
2414 Dmsg0(100, "MTSETDRVBUFFER\n");
2415 if (dev->d_ioctl(dev->fd(), MTIOCTOP, (char *)&mt_com) < 0) {
2416 dev->clrerror(MTSETDRVBUFFER);
2423 #ifdef HAVE_NETBSD_OS
2425 if (dev->min_block_size == dev->max_block_size &&
2426 dev->min_block_size == 0) { /* variable block mode */
2427 mt_com.mt_op = MTSETBSIZ;
2428 mt_com.mt_count = 0;
2429 if (dev->d_ioctl(dev->fd(), MTIOCTOP, (char *)&mt_com) < 0) {
2430 dev->clrerror(MTSETBSIZ);
2432 /* Get notified at logical end of tape */
2433 mt_com.mt_op = MTEWARN;
2434 mt_com.mt_count = 1;
2435 if (dev->d_ioctl(dev->fd(), MTIOCTOP, (char *)&mt_com) < 0) {
2436 dev->clrerror(MTEWARN);
2442 #if HAVE_FREEBSD_OS || HAVE_OPENBSD_OS
2444 if (dev->min_block_size == dev->max_block_size &&
2445 dev->min_block_size == 0) { /* variable block mode */
2446 mt_com.mt_op = MTSETBSIZ;
2447 mt_com.mt_count = 0;
2448 if (dev->d_ioctl(dev->fd(), MTIOCTOP, (char *)&mt_com) < 0) {
2449 dev->clrerror(MTSETBSIZ);
2452 #if defined(MTIOCSETEOTMODEL)
2454 if (dev->has_cap(CAP_TWOEOF)) {
2459 if (dev->d_ioctl(dev->fd(), MTIOCSETEOTMODEL, (caddr_t)&neof) < 0) {
2461 dev->dev_errno = errno; /* save errno */
2462 Mmsg2(dev->errmsg, _("Unable to set eotmodel on device %s: ERR=%s\n"),
2463 dev->print_name(), be.bstrerror(dev->dev_errno));
2464 Jmsg(dcr->jcr, M_FATAL, 0, dev->errmsg);
2472 if (dev->min_block_size == dev->max_block_size &&
2473 dev->min_block_size == 0) { /* variable block mode */
2474 mt_com.mt_op = MTSRSZ;
2475 mt_com.mt_count = 0;
2476 if (dev->d_ioctl(dev->fd(), MTIOCTOP, (char *)&mt_com) < 0) {
2477 dev->clrerror(MTSRSZ);
2484 static bool dev_get_os_pos(DEVICE *dev, struct mtget *mt_stat)
2486 Dmsg0(100, "dev_get_os_pos\n");
2487 return dev->has_cap(CAP_MTIOCGET) &&
2488 dev->d_ioctl(dev->fd(), MTIOCGET, (char *)mt_stat) == 0 &&
2489 mt_stat->mt_fileno >= 0;
2492 static const char *modes[] = {
2493 "CREATE_READ_WRITE",
2500 static const char *mode_to_str(int mode)
2502 static char buf[100];
2503 if (mode < 1 || mode > 4) {
2504 bsnprintf(buf, sizeof(buf), "BAD mode=%d", mode);
2507 return modes[mode-1];