3 * dev.c -- low level operations on device (storage device)
7 * NOTE!!!! None of these routines are reentrant. You must
8 * use lock_device() and unlock_device() at a higher level,
9 * or use the xxx_device() equivalents. By moving the
10 * thread synchronization to a higher level, we permit
11 * the higher level routines to "seize" the device and
12 * to carry out operations without worrying about who
13 * set what lock (i.e. race conditions).
15 * Note, this is the device dependent code, and may have
16 * to be modified for each system, but is meant to
17 * be as "generic" as possible.
19 * The purpose of this code is to develop a SIMPLE Storage
20 * daemon. More complicated coding (double buffering, writer
21 * thread, ...) is left for a later version.
23 * Unfortunately, I have had to add more and more complication
24 * to this code. This was not foreseen as noted above, and as
25 * a consequence has lead to something more contorted than is
26 * really necessary -- KES. Note, this contortion has been
27 * corrected to a large extent by a rewrite (Apr MMI).
32 Bacula® - The Network Backup Solution
34 Copyright (C) 2000-2006 Free Software Foundation Europe e.V.
36 The main author of Bacula is Kern Sibbald, with contributions from
37 many others, a complete list can be found in the file AUTHORS.
38 This program is Free Software; you can redistribute it and/or
39 modify it under the terms of version two of the GNU General Public
40 License as published by the Free Software Foundation plus additions
41 that are listed in the file LICENSE.
43 This program is distributed in the hope that it will be useful, but
44 WITHOUT ANY WARRANTY; without even the implied warranty of
45 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
46 General Public License for more details.
48 You should have received a copy of the GNU General Public License
49 along with this program; if not, write to the Free Software
50 Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
53 Bacula® is a registered trademark of John Walker.
54 The licensor of Bacula is the Free Software Foundation Europe
55 (FSFE), Fiduciary Program, Sumatrastrasse 25, 8006 Zürich,
56 Switzerland, email:ftf@fsfeurope.org.
60 * Handling I/O errors and end of tape conditions are a bit tricky.
61 * This is how it is currently done when writting.
62 * On either an I/O error or end of tape,
63 * we will stop writing on the physical device (no I/O recovery is
64 * attempted at least in this daemon). The state flag will be sent
65 * to include ST_EOT, which is ephemeral, and ST_WEOT, which is
66 * persistent. Lots of routines clear ST_EOT, but ST_WEOT is
67 * cleared only when the problem goes away. Now when ST_WEOT
68 * is set all calls to write_block_to_device() call the fix_up
69 * routine. In addition, all threads are blocked
70 * from writing on the tape by calling lock_dev(), and thread other
71 * than the first thread to hit the EOT will block on a condition
72 * variable. The first thread to hit the EOT will continue to
73 * be able to read and write the tape (he sort of tunnels through
74 * the locking mechanism -- see lock_dev() for details).
76 * Now presumably somewhere higher in the chain of command
77 * (device.c), someone will notice the EOT condition and
78 * get a new tape up, get the tape label read, and mark
79 * the label for rewriting. Then this higher level routine
80 * will write the unwritten buffer to the new volume.
81 * Finally, he will release
82 * any blocked threads by doing a broadcast on the condition
83 * variable. At that point, we should be totally back in
84 * business with no lost data.
95 /* Forward referenced functions */
96 void set_os_device_parameters(DCR *dcr);
97 static bool dev_get_os_pos(DEVICE *dev, struct mtget *mt_stat);
98 static char *mode_to_str(int mode);
101 * Allocate and initialize the DEVICE structure
102 * Note, if dev is non-NULL, it is already allocated,
103 * thus we neither allocate it nor free it. This allows
104 * the caller to put the packet in shared memory.
106 * Note, for a tape, the device->device_name is the device name
107 * (e.g. /dev/nst0), and for a file, the device name
108 * is the directory in which the file will be placed.
112 init_dev(JCR *jcr, DEVRES *device)
120 /* If no device type specified, try to guess */
121 if (!device->dev_type) {
122 /* Check that device is available */
123 if (stat(device->device_name, &statp) < 0) {
125 Jmsg2(jcr, M_ERROR, 0, _("Unable to stat device %s: ERR=%s\n"),
126 device->device_name, be.strerror());
129 if (S_ISDIR(statp.st_mode)) {
130 device->dev_type = B_FILE_DEV;
131 } else if (S_ISCHR(statp.st_mode)) {
132 device->dev_type = B_TAPE_DEV;
133 } else if (S_ISFIFO(statp.st_mode)) {
134 device->dev_type = B_FIFO_DEV;
135 } else if (!(device->cap_bits & CAP_REQMOUNT)) {
136 Jmsg2(jcr, M_ERROR, 0, _("%s is an unknown device type. Must be tape or directory\n"
137 " or have RequiresMount=yes for DVD. st_mode=%x\n"),
138 device->device_name, statp.st_mode);
141 device->dev_type = B_DVD_DEV;
145 dev = (DEVICE *)malloc(sizeof(DEVICE));
146 memset(dev, 0, sizeof(DEVICE));
147 dev->Slot = -1; /* unknown */
149 /* Copy user supplied device parameters from Resource */
150 dev->dev_name = get_memory(strlen(device->device_name)+1);
151 pm_strcpy(dev->dev_name, device->device_name);
152 dev->prt_name = get_memory(strlen(device->device_name) + strlen(device->hdr.name) + 20);
153 /* We edit "Resource-name" (physical-name) */
154 Mmsg(dev->prt_name, "\"%s\" (%s)", device->hdr.name, device->device_name);
155 Dmsg1(400, "Allocate dev=%s\n", dev->print_name());
156 dev->capabilities = device->cap_bits;
157 dev->min_block_size = device->min_block_size;
158 dev->max_block_size = device->max_block_size;
159 dev->max_volume_size = device->max_volume_size;
160 dev->max_file_size = device->max_file_size;
161 dev->volume_capacity = device->volume_capacity;
162 dev->max_rewind_wait = device->max_rewind_wait;
163 dev->max_open_wait = device->max_open_wait;
164 dev->max_open_vols = device->max_open_vols;
165 dev->vol_poll_interval = device->vol_poll_interval;
166 dev->max_spool_size = device->max_spool_size;
167 dev->drive_index = device->drive_index;
168 dev->autoselect = device->autoselect;
169 dev->dev_type = device->dev_type;
170 if (dev->is_tape()) { /* No parts on tapes */
171 dev->max_part_size = 0;
173 dev->max_part_size = device->max_part_size;
176 if (dev->vol_poll_interval && dev->vol_poll_interval < 60) {
177 dev->vol_poll_interval = 60;
179 /* Link the dev and device structures together */
180 dev->device = device;
183 if (dev->is_fifo()) {
184 dev->capabilities |= CAP_STREAM; /* set stream device */
187 /* If the device requires mount :
188 * - Check that the mount point is available
189 * - Check that (un)mount commands are defined
191 if ((dev->is_file() || dev->is_dvd()) && dev->requires_mount()) {
192 if (!device->mount_point || stat(device->mount_point, &statp) < 0) {
194 dev->dev_errno = errno;
195 Jmsg2(jcr, M_ERROR, 0, _("Unable to stat mount point %s: ERR=%s\n"),
196 device->mount_point, be.strerror());
201 if (!device->mount_command || !device->unmount_command) {
202 Jmsg0(jcr, M_ERROR_TERM, 0, _("Mount and unmount commands must defined for a device which requires mount.\n"));
204 if (!device->write_part_command) {
205 Jmsg0(jcr, M_ERROR_TERM, 0, _("Write part command must be defined for a device which requires mount.\n"));
209 if (dev->max_block_size > 1000000) {
210 Jmsg3(jcr, M_ERROR, 0, _("Block size %u on device %s is too large, using default %u\n"),
211 dev->max_block_size, dev->print_name(), DEFAULT_BLOCK_SIZE);
212 dev->max_block_size = 0;
214 if (dev->max_block_size % TAPE_BSIZE != 0) {
215 Jmsg2(jcr, M_WARNING, 0, _("Max block size %u not multiple of device %s block size.\n"),
216 dev->max_block_size, dev->print_name());
219 dev->errmsg = get_pool_memory(PM_EMSG);
222 if ((errstat = pthread_mutex_init(&dev->mutex, NULL)) != 0) {
224 dev->dev_errno = errstat;
225 Mmsg1(dev->errmsg, _("Unable to init mutex: ERR=%s\n"), be.strerror(errstat));
226 Jmsg0(jcr, M_ERROR_TERM, 0, dev->errmsg);
228 if ((errstat = pthread_cond_init(&dev->wait, NULL)) != 0) {
230 dev->dev_errno = errstat;
231 Mmsg1(dev->errmsg, _("Unable to init cond variable: ERR=%s\n"), be.strerror(errstat));
232 Jmsg0(jcr, M_ERROR_TERM, 0, dev->errmsg);
234 if ((errstat = pthread_cond_init(&dev->wait_next_vol, NULL)) != 0) {
236 dev->dev_errno = errstat;
237 Mmsg1(dev->errmsg, _("Unable to init cond variable: ERR=%s\n"), be.strerror(errstat));
238 Jmsg0(jcr, M_ERROR_TERM, 0, dev->errmsg);
240 if ((errstat = pthread_mutex_init(&dev->spool_mutex, NULL)) != 0) {
242 dev->dev_errno = errstat;
243 Mmsg1(dev->errmsg, _("Unable to init mutex: ERR=%s\n"), be.strerror(errstat));
244 Jmsg0(jcr, M_ERROR_TERM, 0, dev->errmsg);
246 if ((errstat = rwl_init(&dev->lock)) != 0) {
248 dev->dev_errno = errstat;
249 Mmsg1(dev->errmsg, _("Unable to init mutex: ERR=%s\n"), be.strerror(errstat));
250 Jmsg0(jcr, M_ERROR_TERM, 0, dev->errmsg);
254 dev->attached_dcrs = New(dlist(dcr, &dcr->dev_link));
255 Dmsg2(29, "init_dev: tape=%d dev_name=%s\n", dev->is_tape(), dev->dev_name);
261 * Open the device with the operating system and
262 * initialize buffer pointers.
264 * Returns: -1 on error
267 * Note, for a tape, the VolName is the name we give to the
268 * volume (not really used here), but for a file, the
269 * VolName represents the name of the file to be created/opened.
270 * In the case of a file, the full name is the device name
271 * (archive_name) with the VolName concatenated.
274 DEVICE::open(DCR *dcr, int omode)
278 if (openmode == omode) {
287 Dmsg0(100, "Close fd for mode change.\n");
288 preserve = state & (ST_LABEL|ST_APPEND|ST_READ);
292 bstrncpy(VolCatInfo.VolCatName, dcr->VolumeName, sizeof(VolCatInfo.VolCatName));
295 Dmsg4(29, "open dev: type=%d dev_name=%s vol=%s mode=%s\n", dev_type,
296 print_name(), VolCatInfo.VolCatName, mode_to_str(omode));
297 state &= ~(ST_LABEL|ST_APPEND|ST_READ|ST_EOT|ST_WEOT|ST_EOF);
298 Slot = -1; /* unknown slot */
299 label_type = B_BACULA_LABEL;
300 if (is_tape() || is_fifo()) {
301 open_tape_device(dcr, omode);
302 } else if (is_dvd()) {
303 Dmsg1(100, "call open_dvd_device mode=%s\n", mode_to_str(omode));
304 open_dvd_device(dcr, omode);
306 Dmsg1(100, "call open_file_device mode=%s\n", mode_to_str(omode));
307 open_file_device(dcr, omode);
309 state |= preserve; /* reset any important state info */
310 Dmsg2(100, "preserve=0x%x fd=%d\n", preserve, fd);
314 void DEVICE::set_mode(int new_mode)
317 case CREATE_READ_WRITE:
318 mode = O_CREAT | O_RDWR | O_BINARY;
320 case OPEN_READ_WRITE:
321 mode = O_RDWR | O_BINARY;
324 mode = O_RDONLY | O_BINARY;
326 case OPEN_WRITE_ONLY:
327 mode = O_WRONLY | O_BINARY;
330 Emsg0(M_ABORT, 0, _("Illegal mode given to open dev.\n"));
336 void DEVICE::open_tape_device(DCR *dcr, int omode)
339 int timeout = max_open_wait;
341 utime_t start_time = time(NULL);
344 Dmsg0(29, "Open dev: device is tape\n");
346 get_autochanger_loaded_slot(dcr);
355 if (is_fifo() && timeout) {
357 tid = start_thread_timer(pthread_self(), timeout);
359 Dmsg2(100, "Try open %s mode=%s\n", print_name(), mode_to_str(omode));
360 #if defined(HAVE_WIN32)
363 if ((fd = tape_open(dev_name, mode)) < 0) {
370 /* If busy retry each second for max_open_wait seconds */
372 /* Try non-blocking open */
373 fd = ::open(dev_name, mode+O_NONBLOCK);
377 Dmsg5(050, "Open error on %s omode=%d mode=%x errno=%d: ERR=%s\n",
378 print_name(), omode, mode, errno, be.strerror());
380 /* Tape open, now rewind it */
381 Dmsg0(050, "Rewind after open\n");
382 mt_com.mt_op = MTREW;
384 if (ioctl(fd, MTIOCTOP, (char *)&mt_com) < 0) {
386 dev_errno = errno; /* set error status from rewind */
389 Dmsg2(100, "Rewind error on %s close: ERR=%s\n", print_name(),
390 be.strerror(dev_errno));
391 /* If we get busy, device is probably rewinding, try again */
392 if (dev_errno != EBUSY) {
393 break; /* error -- no medium */
396 /* Got fd and rewind worked, so we must have medium in drive */
398 fd = ::open(dev_name, mode); /* open normally */
402 Dmsg5(050, "Open error on %s omode=%d mode=%x errno=%d: ERR=%s\n",
403 print_name(), omode, mode, errno, be.strerror());
408 set_os_device_parameters(dcr); /* do system dependent stuff */
409 break; /* Successfully opened and rewound */
413 /* Exceed wait time ? */
414 if (time(NULL) - start_time >= max_open_wait) {
415 break; /* yes, get out */
422 Mmsg2(errmsg, _("Unable to open device %s: ERR=%s\n"),
423 print_name(), be.strerror(dev_errno));
424 Dmsg1(100, "%s", errmsg);
427 /* Stop any open() timer we started */
429 stop_thread_timer(tid);
432 Dmsg1(29, "open dev: tape %d opened\n", fd);
439 void DEVICE::open_file_device(DCR *dcr, int omode)
441 POOL_MEM archive_name(PM_FNAME);
443 get_autochanger_loaded_slot(dcr);
446 * Handle opening of File Archive (not a tape)
449 pm_strcpy(archive_name, dev_name);
451 * If this is a virtual autochanger (i.e. changer_res != NULL)
452 * we simply use the device name, assuming it has been
453 * appropriately setup by the "autochanger".
455 if (!device->changer_res) {
456 if (VolCatInfo.VolCatName[0] == 0) {
457 Mmsg(errmsg, _("Could not open file device %s. No Volume name given.\n"),
463 if (!IsPathSeparator(archive_name.c_str()[strlen(archive_name.c_str())-1])) {
464 pm_strcat(archive_name, "/");
466 pm_strcat(archive_name, VolCatInfo.VolCatName);
469 mount(1); /* do mount if required */
473 /* If creating file, give 0640 permissions */
474 Dmsg3(29, "open disk: mode=%s open(%s, 0x%x, 0640)\n", mode_to_str(omode),
475 archive_name.c_str(), mode);
476 /* Use system open() */
477 if ((fd = ::open(archive_name.c_str(), mode, 0640)) < 0) {
480 Mmsg2(errmsg, _("Could not open: %s, ERR=%s\n"), archive_name.c_str(),
482 Dmsg1(29, "open failed: %s", errmsg);
483 Emsg0(M_FATAL, 0, errmsg);
489 Dmsg4(29, "open dev: disk fd=%d opened, part=%d/%d, part_size=%u\n",
490 fd, part, num_dvd_parts, part_size);
494 * Open a DVD device. N.B. at this point, dcr->VolCatInfo.VolCatName
495 * (NB:??? I think it's VolCatInfo.VolCatName that is right)
496 * has the desired Volume name, but there is NO assurance that
497 * any other field of VolCatInfo is correct.
499 void DEVICE::open_dvd_device(DCR *dcr, int omode)
501 POOL_MEM archive_name(PM_FNAME);
502 struct stat filestat;
505 * Handle opening of DVD Volume
507 Dmsg2(29, "Enter: open_dvd_dev: DVD vol=%s mode=%s\n",
508 &dcr->VolCatInfo, mode_to_str(omode));
511 * For a DVD we must always pull the state info from dcr->VolCatInfo
512 * This is a bit ugly, but is necessary because we need to open/close/re-open
513 * the dvd file in order to properly mount/unmount and access the
514 * DVD. So we store the state of the DVD as far as is known in the
515 * catalog in dcr->VolCatInfo, and thus we refresh the dev->VolCatInfo
516 * copy here, when opening.
518 VolCatInfo = dcr->VolCatInfo; /* structure assignment */
519 Dmsg1(100, "Volume=%s\n", VolCatInfo.VolCatName);
521 if (VolCatInfo.VolCatName[0] == 0) {
522 Dmsg1(10, "Could not open file device %s. No Volume name given.\n",
524 Mmsg(errmsg, _("Could not open file device %s. No Volume name given.\n"),
531 Dmsg0(100, "Set part=1\n");
532 part = 1; /* count from 1 */
536 if (num_dvd_parts != VolCatInfo.VolCatParts) {
537 num_dvd_parts = VolCatInfo.VolCatParts;
541 * If we are not trying to access the last part, set mode to
542 * OPEN_READ_ONLY as writing would be an error.
544 Dmsg2(29, "open DVD part=%d num_dvd_parts=%d\n", part, num_dvd_parts);
545 /* Now find the name of the part that we want to access */
546 if (part <= num_dvd_parts) {
547 omode = OPEN_READ_ONLY;
548 make_mounted_dvd_filename(this, archive_name);
549 set_part_spooled(false);
551 omode = OPEN_READ_WRITE;
552 make_spooled_dvd_filename(this, archive_name);
553 set_part_spooled(true);
557 // Clear any previous blank_dvd status - we will recalculate it here
560 Dmsg3(99, "open_dvd_device: part=%d num_dvd_parts=%d, VolCatInfo.VolCatParts=%d\n",
561 part, num_dvd_parts, dcr->VolCatInfo.VolCatParts);
564 Dmsg0(99, "DVD device mounted.\n");
565 if (num_dvd_parts == 0 && !truncating) {
567 * If we can mount the device, and we are not truncating the DVD,
568 * we usually want to abort. There is one exception, if there is
569 * only one 0-sized file on the DVD, with the right volume name,
570 * we continue (it's the method used by truncate_dvd to truncate a volume).
572 if (!check_can_write_on_non_blank_dvd(dcr)) {
573 Mmsg(errmsg, _("The DVD in device %s contains data, please blank it before writing.\n"), print_name());
574 Emsg0(M_FATAL, 0, errmsg);
575 unmount(1); /* Unmount the device, so the operator can change it. */
582 * Ensure that we have the correct DVD loaded by looking for part1.
583 * We only succeed the open if it exists. Failure to do this could
584 * leave us trying to add a part to a different DVD!
586 uint32_t oldpart = part;
588 POOL_MEM part1_name(PM_FNAME);
590 make_mounted_dvd_filename(this, part1_name);
592 if (stat(part1_name.c_str(), &statp) < 0) {
594 Mmsg(errmsg, _("Unable to stat DVD part 1 file %s: ERR=%s\n"),
595 part1_name.c_str(), be.strerror());
596 Emsg0(M_FATAL, 0, errmsg);
600 if (!S_ISREG(statp.st_mode)) {
601 /* It is not a regular file */
602 Mmsg(errmsg, _("DVD part 1 is not a regular file %s.\n"),
604 Emsg0(M_FATAL, 0, errmsg);
610 Dmsg0(99, "DVD device mount failed.\n");
611 /* We cannot mount the device */
612 if (num_dvd_parts == 0) {
613 /* Run free space, check there is a media. */
614 if (!update_freespace()) {
615 Emsg0(M_FATAL, 0, errmsg);
620 Dmsg1(29, "Could not mount device %s, this is not a problem (num_dvd_parts == 0), and have media.\n", print_name());
622 Mmsg(errmsg, _("There is no valid DVD in device %s.\n"), print_name());
623 Emsg0(M_FATAL, 0, errmsg);
628 Mmsg(errmsg, _("Could not mount DVD device %s.\n"), print_name());
629 Emsg0(M_FATAL, 0, errmsg);
635 Dmsg5(29, "open dev: DVD dev=%s mode=%s part=%d npart=%d volcatnparts=%d\n",
636 archive_name.c_str(), mode_to_str(omode),
637 part, num_dvd_parts, dcr->VolCatInfo.VolCatParts);
639 Dmsg2(100, "openmode=%d %s\n", openmode, mode_to_str(openmode));
642 /* If creating file, give 0640 permissions */
643 Dmsg3(29, "mode=%s open(%s, 0x%x, 0640)\n", mode_to_str(omode),
644 archive_name.c_str(), mode);
645 /* Use system open() */
646 if ((fd = ::open(archive_name.c_str(), mode, 0640)) < 0) {
648 Mmsg2(errmsg, _("Could not open: %s, ERR=%s\n"), archive_name.c_str(),
650 // Should this be set if we try the create/open below
651 dev_errno = EIO; /* Interpreted as no device present by acquire.c:acquire_device_for_read(). */
652 Dmsg1(29, "open failed: %s", errmsg);
654 /* Previous open failed. See if we can recover */
655 if ((omode == OPEN_READ_ONLY || omode == OPEN_READ_WRITE) &&
656 (part > num_dvd_parts)) {
657 /* If the last part (on spool), doesn't exist when accessing,
658 * create it. In read/write mode a write will be allowed (higher
659 * level software thinks that we are extending a pre-existing
660 * media. Reads for READ_ONLY will report immediately an EOF
661 * Sometimes it is better to finish with an EOF than with an error. */
662 Dmsg1(29, "Creating last part on spool: %s\n", archive_name.c_str());
663 omode = CREATE_READ_WRITE;
664 set_mode(CREATE_READ_WRITE);
665 fd = ::open(archive_name.c_str(), mode, 0640);
669 Dmsg1(100, "after open fd=%d\n", fd);
671 if (omode == OPEN_READ_WRITE || omode == CREATE_READ_WRITE) {
674 /* Get size of file */
675 if (fstat(fd, &filestat) < 0) {
678 Mmsg2(errmsg, _("Could not fstat: %s, ERR=%s\n"), archive_name.c_str(),
680 Dmsg1(29, "open failed: %s", errmsg);
681 /* Use system close() */
685 part_size = filestat.st_size;
687 update_pos(dcr); /* update position */
695 * Returns: true on success
698 bool DEVICE::rewind(DCR *dcr)
704 Dmsg3(400, "rewind res=%d fd=%d %s\n", reserved_device, fd, print_name());
705 state &= ~(ST_EOT|ST_EOF|ST_WEOT); /* remove EOF/EOT flags */
706 block_num = file = 0;
710 if (!is_dvd()) { /* In case of major error, the fd is not open on DVD, so we don't want to abort. */
712 Mmsg1(errmsg, _("Bad call to rewind. Device %s not open\n"),
714 Emsg0(M_ABORT, 0, errmsg);
719 mt_com.mt_op = MTREW;
721 /* If we get an I/O error on rewind, it is probably because
722 * the drive is actually busy. We loop for (about 5 minutes)
723 * retrying every 5 seconds.
725 for (i=max_rewind_wait; ; i -= 5) {
726 if (tape_ioctl(fd, MTIOCTOP, (char *)&mt_com) < 0) {
729 if (i == max_rewind_wait) {
730 Dmsg1(200, "Rewind error, %s. retrying ...\n", be.strerror());
733 * This is a gross hack, because if the user has the
734 * device mounted (i.e. open), then uses mtx to load
735 * a tape, the current open file descriptor is invalid.
736 * So, we close the drive and re-open it.
739 int open_mode = openmode;
742 open(dcr, open_mode);
750 if (dev_errno == EIO) {
751 Mmsg1(errmsg, _("No tape loaded or drive offline on %s.\n"), print_name());
755 if (dev_errno == EIO && i > 0) {
756 Dmsg0(200, "Sleeping 5 seconds.\n");
761 Mmsg2(errmsg, _("Rewind error on %s. ERR=%s.\n"),
762 print_name(), be.strerror());
767 } else if (is_file() || is_dvd()) {
768 if (lseek(dcr, (boffset_t)0, SEEK_SET) < 0) {
771 Mmsg2(errmsg, _("lseek error on %s. ERR=%s.\n"),
772 print_name(), be.strerror());
779 void DEVICE::block(int why)
782 block_device(this, why);
786 void DEVICE::unblock()
789 unblock_device(this);
793 const char *DEVICE::print_blocked() const
795 switch (dev_blocked) {
796 case BST_NOT_BLOCKED:
797 return "BST_NOT_BLOCKED";
799 return "BST_UNMOUNTED";
800 case BST_WAITING_FOR_SYSOP:
801 return "BST_WAITING_FOR_SYSOP";
802 case BST_DOING_ACQUIRE:
803 return "BST_DOING_ACQUIRE";
804 case BST_WRITING_LABEL:
805 return "BST_WRITING_LABEL";
806 case BST_UNMOUNTED_WAITING_FOR_SYSOP:
807 return "BST_UNMOUNTED_WAITING_FOR_SYSOP";
811 return _("unknown blocked code");
816 * Called to indicate that we have just read an
817 * EOF from the device.
819 void DEVICE::set_ateof()
831 * Called to indicate we are now at the end of the tape, and
832 * writing is not possible.
834 void DEVICE::set_ateot()
836 /* Make tape effectively read-only */
837 state |= (ST_EOF|ST_EOT|ST_WEOT);
842 * Position device to end of medium (end of data)
843 * Returns: true on succes
846 bool DEVICE::eod(DCR *dcr)
849 struct mtget mt_stat;
855 Mmsg1(errmsg, _("Bad call to eod. Device %s not open\n"), print_name());
859 #if defined (__digital__) && defined (__unix__)
860 return fsf(VolCatInfo.VolCatFiles);
867 clear_eof(); /* remove EOF flag */
868 block_num = file = 0;
871 if (is_fifo() || is_prog()) {
875 pos = lseek(dcr, (boffset_t)0, SEEK_END);
876 // Dmsg1(100, "====== Seek to %lld\n", pos);
884 Mmsg2(errmsg, _("lseek error on %s. ERR=%s.\n"),
885 print_name(), be.strerror());
889 if (has_cap(CAP_FASTFSF) && !has_cap(CAP_EOM)) {
890 Dmsg0(100,"Using FAST FSF for EOM\n");
891 /* If unknown position, rewind */
892 if (!dev_get_os_pos(this, &mt_stat)) {
897 mt_com.mt_op = MTFSF;
899 * ***FIXME*** fix code to handle case that INT16_MAX is
902 mt_com.mt_count = INT16_MAX; /* use big positive number */
903 if (mt_com.mt_count < 0) {
904 mt_com.mt_count = INT16_MAX; /* brain damaged system */
908 if (has_cap(CAP_MTIOCGET) && (has_cap(CAP_FASTFSF) || has_cap(CAP_EOM))) {
909 if (has_cap(CAP_EOM)) {
910 Dmsg0(100,"Using EOM for EOM\n");
911 mt_com.mt_op = MTEOM;
915 if (tape_ioctl(fd, MTIOCTOP, (char *)&mt_com) < 0) {
917 clrerror(mt_com.mt_op);
918 Dmsg1(50, "ioctl error: %s\n", be.strerror());
920 Mmsg2(errmsg, _("ioctl MTEOM error on %s. ERR=%s.\n"),
921 print_name(), be.strerror());
925 if (!dev_get_os_pos(this, &mt_stat)) {
928 Mmsg2(errmsg, _("ioctl MTIOCGET error on %s. ERR=%s.\n"),
929 print_name(), be.strerror());
932 Dmsg2(100, "EOD file=%d block=%d\n", mt_stat.mt_fileno, mt_stat.mt_blkno);
934 file = mt_stat.mt_fileno;
940 * Rewind then use FSF until EOT reached
946 * Move file by file to the end of the tape
949 for (file_num=file; !at_eot(); file_num++) {
950 Dmsg0(200, "eod: doing fsf 1\n");
952 Dmsg0(200, "fsf error.\n");
956 * Avoid infinite loop by ensuring we advance.
958 if (!at_eot() && file_num == (int)file) {
959 struct mtget mt_stat;
960 Dmsg1(100, "fsf did not advance from file %d\n", file_num);
962 if (dev_get_os_pos(this, &mt_stat)) {
963 Dmsg2(100, "Adjust file from %d to %d\n", file_num, mt_stat.mt_fileno);
964 file = mt_stat.mt_fileno;
971 * Some drivers leave us after second EOF when doing
972 * MTEOM, so we must backup so that appending overwrites
975 if (has_cap(CAP_BSFATEOM)) {
976 struct mtget mt_stat;
977 /* Backup over EOF */
979 /* If BSF worked and fileno is known (not -1), set file */
980 if (dev_get_os_pos(this, &mt_stat)) {
981 Dmsg2(100, "BSFATEOF adjust file from %d to %d\n", file , mt_stat.mt_fileno);
982 file = mt_stat.mt_fileno;
984 file++; /* wing it -- not correct on all OSes */
987 update_pos(dcr); /* update position */
989 Dmsg1(200, "EOD dev->file=%d\n", file);
994 * Set the position of the device -- only for files and DVD
995 * For other devices, there is no generic way to do it.
996 * Returns: true on succes
999 bool DEVICE::update_pos(DCR *dcr)
1006 Mmsg0(errmsg, _("Bad device call. Device not open\n"));
1007 Emsg1(M_FATAL, 0, "%s", errmsg);
1011 /* Find out where we are */
1012 if (is_file() || is_dvd()) {
1015 pos = lseek(dcr, (boffset_t)0, SEEK_CUR);
1019 Pmsg1(000, _("Seek error: ERR=%s\n"), be.strerror());
1020 Mmsg2(errmsg, _("lseek error on %s. ERR=%s.\n"),
1021 print_name(), be.strerror());
1025 block_num = (uint32_t)pos;
1026 file = (uint32_t)(pos >> 32);
1033 * Return the status of the device. This was meant
1034 * to be a generic routine. Unfortunately, it doesn't
1035 * seem possible (at least I do not know how to do it
1036 * currently), which means that for the moment, this
1037 * routine has very little value.
1041 uint32_t status_dev(DEVICE *dev)
1043 struct mtget mt_stat;
1046 if (dev->state & (ST_EOT | ST_WEOT)) {
1050 if (dev->state & ST_EOF) {
1054 if (dev->is_tape()) {
1056 Pmsg0(-20,_(" Bacula status:"));
1057 Pmsg2(-20,_(" file=%d block=%d\n"), dev->file, dev->block_num);
1058 if (tape_ioctl(dev->fd, MTIOCGET, (char *)&mt_stat) < 0) {
1060 dev->dev_errno = errno;
1061 Mmsg2(dev->errmsg, _("ioctl MTIOCGET error on %s. ERR=%s.\n"),
1062 dev->print_name(), be.strerror());
1065 Pmsg0(-20, _(" Device status:"));
1067 #if defined(HAVE_LINUX_OS)
1068 if (GMT_EOF(mt_stat.mt_gstat)) {
1072 if (GMT_BOT(mt_stat.mt_gstat)) {
1076 if (GMT_EOT(mt_stat.mt_gstat)) {
1080 if (GMT_SM(mt_stat.mt_gstat)) {
1084 if (GMT_EOD(mt_stat.mt_gstat)) {
1088 if (GMT_WR_PROT(mt_stat.mt_gstat)) {
1089 stat |= BMT_WR_PROT;
1090 Pmsg0(-20, " WR_PROT");
1092 if (GMT_ONLINE(mt_stat.mt_gstat)) {
1094 Pmsg0(-20, " ONLINE");
1096 if (GMT_DR_OPEN(mt_stat.mt_gstat)) {
1097 stat |= BMT_DR_OPEN;
1098 Pmsg0(-20, " DR_OPEN");
1100 if (GMT_IM_REP_EN(mt_stat.mt_gstat)) {
1101 stat |= BMT_IM_REP_EN;
1102 Pmsg0(-20, " IM_REP_EN");
1104 #elif defined(HAVE_WIN32)
1105 if (GMT_EOF(mt_stat.mt_gstat)) {
1109 if (GMT_BOT(mt_stat.mt_gstat)) {
1113 if (GMT_EOT(mt_stat.mt_gstat)) {
1117 if (GMT_EOD(mt_stat.mt_gstat)) {
1121 if (GMT_WR_PROT(mt_stat.mt_gstat)) {
1122 stat |= BMT_WR_PROT;
1123 Pmsg0(-20, " WR_PROT");
1125 if (GMT_ONLINE(mt_stat.mt_gstat)) {
1127 Pmsg0(-20, " ONLINE");
1129 if (GMT_DR_OPEN(mt_stat.mt_gstat)) {
1130 stat |= BMT_DR_OPEN;
1131 Pmsg0(-20, " DR_OPEN");
1133 if (GMT_IM_REP_EN(mt_stat.mt_gstat)) {
1134 stat |= BMT_IM_REP_EN;
1135 Pmsg0(-20, " IM_REP_EN");
1138 #endif /* !SunOS && !OSF */
1139 if (dev->has_cap(CAP_MTIOCGET)) {
1140 Pmsg2(-20, _(" file=%d block=%d\n"), mt_stat.mt_fileno, mt_stat.mt_blkno);
1142 Pmsg2(-20, _(" file=%d block=%d\n"), -1, -1);
1145 stat |= BMT_ONLINE | BMT_BOT;
1152 * Load medium in device
1153 * Returns: true on success
1156 bool load_dev(DEVICE *dev)
1163 dev->dev_errno = EBADF;
1164 Mmsg0(dev->errmsg, _("Bad call to load_dev. Device not open\n"));
1165 Emsg0(M_FATAL, 0, dev->errmsg);
1168 if (!(dev->is_tape())) {
1172 Dmsg0(200, "stored: MTLOAD command not available\n");
1174 dev->dev_errno = ENOTTY; /* function not available */
1175 Mmsg2(dev->errmsg, _("ioctl MTLOAD error on %s. ERR=%s.\n"),
1176 dev->print_name(), be.strerror());
1180 dev->block_num = dev->file = 0;
1183 mt_com.mt_op = MTLOAD;
1184 mt_com.mt_count = 1;
1185 if (tape_ioctl(dev->fd, MTIOCTOP, (char *)&mt_com) < 0) {
1187 dev->dev_errno = errno;
1188 Mmsg2(dev->errmsg, _("ioctl MTLOAD error on %s. ERR=%s.\n"),
1189 dev->print_name(), be.strerror());
1197 * Rewind device and put it offline
1198 * Returns: true on success
1201 bool DEVICE::offline()
1206 return true; /* device not open */
1209 state &= ~(ST_APPEND|ST_READ|ST_EOT|ST_EOF|ST_WEOT); /* remove EOF/EOT flags */
1210 block_num = file = 0;
1214 mt_com.mt_op = MTOFFL;
1215 mt_com.mt_count = 1;
1216 if (tape_ioctl(fd, MTIOCTOP, (char *)&mt_com) < 0) {
1219 Mmsg2(errmsg, _("ioctl MTOFFL error on %s. ERR=%s.\n"),
1220 print_name(), be.strerror());
1223 Dmsg1(100, "Offlined device %s\n", print_name());
1227 bool DEVICE::offline_or_rewind()
1232 if (has_cap(CAP_OFFLINEUNMOUNT)) {
1236 * Note, this rewind probably should not be here (it wasn't
1237 * in prior versions of Bacula), but on FreeBSD, this is
1238 * needed in the case the tape was "frozen" due to an error
1239 * such as backspacing after writing and EOF. If it is not
1240 * done, all future references to the drive get and I/O error.
1243 return rewind(NULL);
1248 * Foward space a file
1249 * Returns: true on success
1252 bool DEVICE::fsf(int num)
1254 struct mtget mt_stat;
1260 Mmsg0(errmsg, _("Bad call to fsf. Device not open\n"));
1261 Emsg0(M_FATAL, 0, errmsg);
1271 Mmsg1(errmsg, _("Device %s at End of Tape.\n"), print_name());
1275 Dmsg0(200, "ST_EOF set on entry to FSF\n");
1278 Dmsg0(100, "fsf\n");
1281 * If Fast forward space file is set, then we
1282 * use MTFSF to forward space and MTIOCGET
1283 * to get the file position. We assume that
1284 * the SCSI driver will ensure that we do not
1285 * forward space past the end of the medium.
1287 if (has_cap(CAP_FSF) && has_cap(CAP_MTIOCGET) && has_cap(CAP_FASTFSF)) {
1288 mt_com.mt_op = MTFSF;
1289 mt_com.mt_count = num;
1290 stat = tape_ioctl(fd, MTIOCTOP, (char *)&mt_com);
1291 if (stat < 0 || !dev_get_os_pos(this, &mt_stat)) {
1294 Dmsg0(200, "Set ST_EOT\n");
1296 Mmsg2(errmsg, _("ioctl MTFSF error on %s. ERR=%s.\n"),
1297 print_name(), be.strerror());
1298 Dmsg1(200, "%s", errmsg);
1301 Dmsg2(200, "fsf file=%d block=%d\n", mt_stat.mt_fileno, mt_stat.mt_blkno);
1303 file = mt_stat.mt_fileno;
1307 * Here if CAP_FSF is set, and virtually all drives
1308 * these days support it, we read a record, then forward
1309 * space one file. Using this procedure, which is slow,
1310 * is the only way we can be sure that we don't read
1311 * two consecutive EOF marks, which means End of Data.
1313 } else if (has_cap(CAP_FSF)) {
1316 Dmsg0(200, "FSF has cap_fsf\n");
1317 if (max_block_size == 0) {
1318 rbuf_len = DEFAULT_BLOCK_SIZE;
1320 rbuf_len = max_block_size;
1322 rbuf = get_memory(rbuf_len);
1323 mt_com.mt_op = MTFSF;
1324 mt_com.mt_count = 1;
1325 while (num-- && !at_eot()) {
1326 Dmsg0(100, "Doing read before fsf\n");
1327 if ((stat = tape_read(fd, (char *)rbuf, rbuf_len)) < 0) {
1328 if (errno == ENOMEM) { /* tape record exceeds buf len */
1329 stat = rbuf_len; /* This is OK */
1331 * On IBM drives, they return ENOSPC at EOM
1332 * instead of EOF status
1334 } else if (at_eof() && errno == ENOSPC) {
1340 Dmsg2(100, "Set ST_EOT read errno=%d. ERR=%s\n", dev_errno,
1342 Mmsg2(errmsg, _("read error on %s. ERR=%s.\n"),
1343 print_name(), be.strerror());
1344 Dmsg1(100, "%s", errmsg);
1348 if (stat == 0) { /* EOF */
1349 Dmsg1(100, "End of File mark from read. File=%d\n", file+1);
1350 /* Two reads of zero means end of tape */
1353 Dmsg0(100, "Set ST_EOT\n");
1359 } else { /* Got data */
1364 Dmsg0(100, "Doing MTFSF\n");
1365 stat = tape_ioctl(fd, MTIOCTOP, (char *)&mt_com);
1366 if (stat < 0) { /* error => EOT */
1369 Dmsg0(100, "Set ST_EOT\n");
1371 Mmsg2(errmsg, _("ioctl MTFSF error on %s. ERR=%s.\n"),
1372 print_name(), be.strerror());
1373 Dmsg0(100, "Got < 0 for MTFSF\n");
1374 Dmsg1(100, "%s", errmsg);
1382 * No FSF, so use FSR to simulate it
1385 Dmsg0(200, "Doing FSR for FSF\n");
1386 while (num-- && !at_eot()) {
1387 fsr(INT32_MAX); /* returns -1 on EOF or EOT */
1391 Mmsg1(errmsg, _("Device %s at End of Tape.\n"), print_name());
1397 Dmsg1(200, "Return %d from FSF\n", stat);
1399 Dmsg0(200, "ST_EOF set on exit FSF\n");
1402 Dmsg0(200, "ST_EOT set on exit FSF\n");
1404 Dmsg1(200, "Return from FSF file=%d\n", file);
1409 * Backward space a file
1410 * Returns: false on failure
1413 bool DEVICE::bsf(int num)
1420 Mmsg0(errmsg, _("Bad call to bsf. Device not open\n"));
1421 Emsg0(M_FATAL, 0, errmsg);
1426 Mmsg1(errmsg, _("Device %s cannot BSF because it is not a tape.\n"),
1437 mt_com.mt_op = MTBSF;
1438 mt_com.mt_count = num;
1439 stat = tape_ioctl(fd, MTIOCTOP, (char *)&mt_com);
1443 Mmsg2(errmsg, _("ioctl MTBSF error on %s. ERR=%s.\n"),
1444 print_name(), be.strerror());
1451 * Foward space num records
1452 * Returns: false on failure
1455 bool DEVICE::fsr(int num)
1462 Mmsg0(errmsg, _("Bad call to fsr. Device not open\n"));
1463 Emsg0(M_FATAL, 0, errmsg);
1471 if (!has_cap(CAP_FSR)) {
1472 Mmsg1(errmsg, _("ioctl MTFSR not permitted on %s.\n"), print_name());
1476 Dmsg1(29, "fsr %d\n", num);
1477 mt_com.mt_op = MTFSR;
1478 mt_com.mt_count = num;
1479 stat = tape_ioctl(fd, MTIOCTOP, (char *)&mt_com);
1485 struct mtget mt_stat;
1487 Dmsg1(100, "FSF fail: ERR=%s\n", be.strerror());
1488 if (dev_get_os_pos(this, &mt_stat)) {
1489 Dmsg4(100, "Adjust from %d:%d to %d:%d\n", file,
1490 block_num, mt_stat.mt_fileno, mt_stat.mt_blkno);
1491 file = mt_stat.mt_fileno;
1492 block_num = mt_stat.mt_blkno;
1500 Mmsg3(errmsg, _("ioctl MTFSR %d error on %s. ERR=%s.\n"),
1501 num, print_name(), be.strerror());
1507 * Backward space a record
1508 * Returns: false on failure
1511 bool DEVICE::bsr(int num)
1518 Mmsg0(errmsg, _("Bad call to bsr_dev. Device not open\n"));
1519 Emsg0(M_FATAL, 0, errmsg);
1527 if (!has_cap(CAP_BSR)) {
1528 Mmsg1(errmsg, _("ioctl MTBSR not permitted on %s.\n"), print_name());
1532 Dmsg0(29, "bsr_dev\n");
1536 mt_com.mt_op = MTBSR;
1537 mt_com.mt_count = num;
1538 stat = tape_ioctl(fd, MTIOCTOP, (char *)&mt_com);
1542 Mmsg2(errmsg, _("ioctl MTBSR error on %s. ERR=%s.\n"),
1543 print_name(), be.strerror());
1548 void DEVICE::lock_door()
1552 mt_com.mt_op = MTLOCK;
1553 mt_com.mt_count = 1;
1554 tape_ioctl(fd, MTIOCTOP, (char *)&mt_com);
1558 void DEVICE::unlock_door()
1562 mt_com.mt_op = MTUNLOCK;
1563 mt_com.mt_count = 1;
1564 tape_ioctl(fd, MTIOCTOP, (char *)&mt_com);
1570 * Reposition the device to file, block
1571 * Returns: false on failure
1574 bool DEVICE::reposition(DCR *dcr, uint32_t rfile, uint32_t rblock)
1578 Mmsg0(errmsg, _("Bad call to reposition. Device not open\n"));
1579 Emsg0(M_FATAL, 0, errmsg);
1584 boffset_t pos = (((boffset_t)rfile)<<32) | rblock;
1585 Dmsg1(100, "===== lseek to %d\n", (int)pos);
1586 if (lseek(dcr, pos, SEEK_SET) == (boffset_t)-1) {
1589 Mmsg2(errmsg, _("lseek error on %s. ERR=%s.\n"),
1590 print_name(), be.strerror());
1599 /* After this point, we are tape only */
1600 Dmsg4(100, "reposition from %u:%u to %u:%u\n", file, block_num, rfile, rblock);
1602 Dmsg0(100, "Rewind\n");
1603 if (!rewind(NULL)) {
1608 Dmsg1(100, "fsf %d\n", rfile-file);
1609 if (!fsf(rfile-file)) {
1610 Dmsg1(100, "fsf failed! ERR=%s\n", bstrerror());
1613 Dmsg2(100, "wanted_file=%d at_file=%d\n", rfile, file);
1615 if (rblock < block_num) {
1616 Dmsg2(100, "wanted_blk=%d at_blk=%d\n", rblock, block_num);
1617 Dmsg0(100, "bsf 1\n");
1619 Dmsg0(100, "fsf 1\n");
1621 Dmsg2(100, "wanted_blk=%d at_blk=%d\n", rblock, block_num);
1623 if (has_cap(CAP_POSITIONBLOCKS) && rblock > block_num) {
1624 /* Ignore errors as Bacula can read to the correct block */
1625 Dmsg1(100, "fsr %d\n", rblock-block_num);
1626 return fsr(rblock-block_num);
1628 while (rblock > block_num) {
1629 if (!read_block_from_dev(dcr, NO_BLOCK_NUMBER_CHECK)) {
1632 Dmsg2(30, "Failed to find requested block on %s: ERR=%s",
1633 print_name(), be.strerror());
1636 Dmsg2(300, "moving forward wanted_blk=%d at_blk=%d\n", rblock, block_num);
1645 * Write an end of file on the device
1646 * Returns: true on success
1649 bool DEVICE::weof(int num)
1653 Dmsg0(129, "weof_dev\n");
1657 Mmsg0(errmsg, _("Bad call to weof_dev. Device not open\n"));
1658 Emsg0(M_FATAL, 0, errmsg);
1666 if (!can_append()) {
1667 Mmsg0(errmsg, _("Attempt to WEOF on non-appendable Volume\n"));
1668 Emsg0(M_FATAL, 0, errmsg);
1674 mt_com.mt_op = MTWEOF;
1675 mt_com.mt_count = num;
1676 stat = tape_ioctl(fd, MTIOCTOP, (char *)&mt_com);
1685 Mmsg2(errmsg, _("ioctl MTWEOF error on %s. ERR=%s.\n"),
1686 print_name(), be.strerror());
1694 * If implemented in system, clear the tape
1697 void DEVICE::clrerror(int func)
1699 const char *msg = NULL;
1700 struct mtget mt_stat;
1703 dev_errno = errno; /* save errno */
1705 VolCatInfo.VolCatErrors++;
1712 if (errno == ENOTTY || errno == ENOSYS) { /* Function not implemented */
1715 break; /* ignore message printed later */
1718 capabilities &= ~CAP_EOF; /* turn off feature */
1723 capabilities &= ~CAP_EOM; /* turn off feature */
1728 capabilities &= ~CAP_FSF; /* turn off feature */
1732 capabilities &= ~CAP_BSF; /* turn off feature */
1736 capabilities &= ~CAP_FSR; /* turn off feature */
1740 capabilities &= ~CAP_BSR; /* turn off feature */
1750 #ifdef MTSETDRVBUFFER
1751 case MTSETDRVBUFFER:
1752 msg = "MTSETDRVBUFFER";
1785 bsnprintf(buf, sizeof(buf), _("unknown func code %d"), func);
1791 Mmsg1(errmsg, _("I/O function \"%s\" not supported on this device.\n"), msg);
1792 Emsg0(M_ERROR, 0, errmsg);
1797 * Now we try different methods of clearing the error
1798 * status on the drive so that it is not locked for
1799 * further operations.
1802 /* On some systems such as NetBSD, this clears all errors */
1803 tape_ioctl(fd, MTIOCGET, (char *)&mt_stat);
1805 /* Found on Linux */
1809 mt_com.mt_op = MTIOCLRERR;
1810 mt_com.mt_count = 1;
1811 /* Clear any error condition on the tape */
1812 tape_ioctl(fd, MTIOCTOP, (char *)&mt_com);
1813 Dmsg0(200, "Did MTIOCLRERR\n");
1817 /* Typically on FreeBSD */
1821 /* Read and clear SCSI error status */
1822 union mterrstat mt_errstat;
1823 Dmsg2(200, "Doing MTIOCERRSTAT errno=%d ERR=%s\n", dev_errno,
1824 be.strerror(dev_errno));
1825 tape_ioctl(fd, MTIOCERRSTAT, (char *)&mt_errstat);
1829 /* Clear Subsystem Exception OSF1 */
1833 mt_com.mt_op = MTCSE;
1834 mt_com.mt_count = 1;
1835 /* Clear any error condition on the tape */
1836 tape_ioctl(fd, MTIOCTOP, (char *)&mt_com);
1837 Dmsg0(200, "Did MTCSE\n");
1845 void DEVICE::close()
1847 Dmsg1(100, "close_dev %s\n", print_name());
1848 if (has_cap(CAP_OFFLINEUNMOUNT)) {
1853 Dmsg2(100, "device %s already closed vol=%s\n", print_name(),
1855 return; /* already closed */
1867 /* Clean up device packet so it can be reused */
1869 state &= ~(ST_LABEL|ST_READ|ST_APPEND|ST_EOT|ST_WEOT|ST_EOF);
1870 label_type = B_BACULA_LABEL;
1871 file = block_num = 0;
1874 EndFile = EndBlock = 0;
1876 Slot = -1; /* unknown slot */
1878 memset(&VolCatInfo, 0, sizeof(VolCatInfo));
1879 memset(&VolHdr, 0, sizeof(VolHdr));
1881 stop_thread_timer(tid);
1887 * This call closes the device, but it is used in DVD handling
1888 * where we close one part and then open the next part. The
1889 * difference between close_part() and close() is that close_part()
1890 * saves the state information of the device (e.g. the Volume lable,
1891 * the Volume Catalog record, ... This permits opening and closing
1892 * the Volume parts multiple times without losing track of what the
1893 * main Volume parameters are.
1895 void DEVICE::close_part(DCR *dcr)
1897 VOLUME_LABEL saveVolHdr;
1898 VOLUME_CAT_INFO saveVolCatInfo; /* Volume Catalog Information */
1901 saveVolHdr = VolHdr; /* structure assignment */
1902 saveVolCatInfo = VolCatInfo; /* structure assignment */
1903 close(); /* close current part */
1904 VolHdr = saveVolHdr; /* structure assignment */
1905 VolCatInfo = saveVolCatInfo; /* structure assignment */
1906 dcr->VolCatInfo = saveVolCatInfo; /* structure assignment */
1909 boffset_t DEVICE::lseek(DCR *dcr, boffset_t offset, int whence)
1913 return lseek_dvd(dcr, offset, whence);
1915 #if defined(HAVE_WIN32)
1916 return ::_lseeki64(fd, (__int64)offset, whence);
1918 return ::lseek(fd, (off_t)offset, whence);
1925 bool DEVICE::truncate(DCR *dcr) /* We need the DCR for DVD-writing */
1927 Dmsg1(100, "truncate %s\n", print_name());
1930 /* maybe we should rewind and write and eof ???? */
1931 return true; /* we don't really truncate tapes */
1933 return truncate_dvd(dcr);
1935 /* ***FIXME*** we really need to unlink() the file so that
1936 * its name can be changed for a relabel.
1938 if (ftruncate(fd, 0) != 0) {
1940 Mmsg2(errmsg, _("Unable to truncate device %s. ERR=%s\n"),
1941 print_name(), be.strerror());
1949 /* Mount the device.
1950 * If timeout, wait until the mount command returns 0.
1951 * If !timeout, try to mount the device only once.
1953 bool DEVICE::mount(int timeout)
1955 Dmsg0(190, "Enter mount\n");
1958 } else if (requires_mount()) {
1959 return do_mount(1, timeout);
1964 /* Unmount the device
1965 * If timeout, wait until the unmount command returns 0.
1966 * If !timeout, try to unmount the device only once.
1968 bool DEVICE::unmount(int timeout)
1970 Dmsg0(90, "Enter unmount\n");
1972 return do_mount(0, timeout);
1977 /* (Un)mount the device */
1978 bool DEVICE::do_mount(int mount, int dotimeout)
1980 POOL_MEM ocmd(PM_FNAME);
1983 int status, timeout;
1985 sm_check(__FILE__, __LINE__, false);
1988 Dmsg0(200, "======= mount=1\n");
1991 icmd = device->mount_command;
1993 if (!is_mounted()) {
1994 Dmsg0(200, "======= mount=0\n");
1997 icmd = device->unmount_command;
2000 clear_freespace_ok();
2001 edit_mount_codes(ocmd, icmd);
2003 Dmsg2(100, "do_mount: cmd=%s mounted=%d\n", ocmd.c_str(), !!is_mounted());
2006 /* Try at most 1 time to (un)mount the device. This should perhaps be configurable. */
2011 results = get_memory(4000);
2014 /* If busy retry each second */
2015 Dmsg1(20, "do_mount run_prog=%s\n", ocmd.c_str());
2016 while ((status = run_program_full_output(ocmd.c_str(),
2017 max_open_wait/2, results)) != 0) {
2018 /* Doesn't work with internationalization (This is not a problem) */
2019 if (mount && fnmatch("*is already mounted on*", results, 0) == 0) {
2022 if (!mount && fnmatch("* not mounted*", results, 0) == 0) {
2025 if (timeout-- > 0) {
2026 /* Sometimes the device cannot be mounted because it is already mounted.
2027 * Try to unmount it, then remount it */
2029 Dmsg1(400, "Trying to unmount the device %s...\n", print_name());
2037 Dmsg5(40, "Device %s cannot be %smounted. stat=%d result=%s ERR=%s\n", print_name(),
2038 (mount ? "" : "un"), status, results, be.strerror(status));
2039 Mmsg(errmsg, _("Device %s cannot be %smounted. ERR=%s\n"),
2040 print_name(), (mount ? "" : "un"), be.strerror(status));
2042 Dmsg4(40, "Device %s cannot be %smounted. stat=%d ERR=%s\n", print_name(),
2043 (mount ? "" : "un"), status, results);
2044 Mmsg(errmsg, _("Device %s cannot be %smounted. ERR=%s\n"),
2045 print_name(), (mount ? "" : "un"), results);
2048 * Now, just to be sure it is not mounted, try to read the
2052 struct dirent *entry, *result;
2056 name_max = pathconf(".", _PC_NAME_MAX);
2057 if (name_max < 1024) {
2061 if (!(dp = opendir(device->mount_point))) {
2064 Dmsg3(29, "do_mount: failed to open dir %s (dev=%s), ERR=%s\n",
2065 device->mount_point, print_name(), be.strerror());
2069 entry = (struct dirent *)malloc(sizeof(struct dirent) + name_max + 1000);
2072 if ((readdir_r(dp, entry, &result) != 0) || (result == NULL)) {
2074 Dmsg2(129, "do_mount: failed to find suitable file in dir %s (dev=%s)\n",
2075 device->mount_point, print_name());
2078 if ((strcmp(result->d_name, ".")) && (strcmp(result->d_name, "..")) && (strcmp(result->d_name, ".keep"))) {
2079 count++; /* result->d_name != ., .. or .keep (Gentoo-specific) */
2082 Dmsg2(129, "do_mount: ignoring %s in %s\n", result->d_name, device->mount_point);
2088 Dmsg1(29, "do_mount: got %d files in the mount point (not counting ., .. and .keep)\n", count);
2091 /* If we got more than ., .. and .keep */
2092 /* there must be something mounted */
2094 Dmsg1(100, "Did Mount by count=%d\n", count);
2097 /* An unmount request. We failed to unmount - report an error */
2099 free_pool_memory(results);
2100 Dmsg0(200, "== error mount=1 wanted unmount\n");
2106 sm_check(__FILE__, __LINE__, false);
2107 free_pool_memory(results);
2108 Dmsg0(200, "============ mount=0\n");
2112 set_mounted(mount); /* set/clear mounted flag */
2113 free_pool_memory(results);
2114 /* Do not check free space when unmounting */
2115 if (mount && !update_freespace()) {
2118 Dmsg1(200, "============ mount=%d\n", mount);
2123 * Edit codes into (Un)MountCommand, Write(First)PartCommand
2125 * %a = archive device name
2126 * %e = erase (set if cannot mount and first part)
2129 * %v = last part name
2131 * omsg = edited output message
2132 * imsg = input string containing edit codes (%x)
2135 void DEVICE::edit_mount_codes(POOL_MEM &omsg, const char *imsg)
2141 POOL_MEM archive_name(PM_FNAME);
2143 omsg.c_str()[0] = 0;
2144 Dmsg1(800, "edit_mount_codes: %s\n", imsg);
2145 for (p=imsg; *p; p++) {
2155 if (num_dvd_parts == 0) {
2156 if (truncating || blank_dvd) {
2166 bsnprintf(add, sizeof(add), "%d", part);
2170 str = device->mount_point;
2173 make_spooled_dvd_filename(this, archive_name);
2174 str = archive_name.c_str();
2188 Dmsg1(1900, "add_str %s\n", str);
2189 pm_strcat(omsg, (char *)str);
2190 Dmsg1(1800, "omsg=%s\n", omsg.c_str());
2195 /* Return the resource name for the device */
2196 const char *DEVICE::name() const
2198 return device->hdr.name;
2202 dev_vol_name(DEVICE *dev)
2204 return dev->VolCatInfo.VolCatName;
2209 * Free memory allocated for the device
2211 void DEVICE::term(void)
2213 Dmsg1(900, "term dev: %s\n", print_name());
2216 free_memory(dev_name);
2220 free_memory(prt_name);
2224 free_pool_memory(errmsg);
2227 pthread_mutex_destroy(&mutex);
2228 pthread_cond_destroy(&wait);
2229 pthread_cond_destroy(&wait_next_vol);
2230 pthread_mutex_destroy(&spool_mutex);
2232 if (attached_dcrs) {
2233 delete attached_dcrs;
2234 attached_dcrs = NULL;
2243 * This routine initializes the device wait timers
2245 void init_device_wait_timers(DCR *dcr)
2247 DEVICE *dev = dcr->dev;
2248 JCR *jcr = dcr->jcr;
2250 /* ******FIXME******* put these on config variables */
2251 dev->min_wait = 60 * 60;
2252 dev->max_wait = 24 * 60 * 60;
2253 dev->max_num_wait = 9; /* 5 waits =~ 1 day, then 1 day at a time */
2254 dev->wait_sec = dev->min_wait;
2255 dev->rem_wait_sec = dev->wait_sec;
2258 dev->BadVolName[0] = 0;
2260 jcr->min_wait = 60 * 60;
2261 jcr->max_wait = 24 * 60 * 60;
2262 jcr->max_num_wait = 9; /* 5 waits =~ 1 day, then 1 day at a time */
2263 jcr->wait_sec = jcr->min_wait;
2264 jcr->rem_wait_sec = jcr->wait_sec;
2269 void init_jcr_device_wait_timers(JCR *jcr)
2271 /* ******FIXME******* put these on config variables */
2272 jcr->min_wait = 60 * 60;
2273 jcr->max_wait = 24 * 60 * 60;
2274 jcr->max_num_wait = 9; /* 5 waits =~ 1 day, then 1 day at a time */
2275 jcr->wait_sec = jcr->min_wait;
2276 jcr->rem_wait_sec = jcr->wait_sec;
2282 * The dev timers are used for waiting on a particular device
2284 * Returns: true if time doubled
2285 * false if max time expired
2287 bool double_dev_wait_time(DEVICE *dev)
2289 dev->wait_sec *= 2; /* double wait time */
2290 if (dev->wait_sec > dev->max_wait) { /* but not longer than maxtime */
2291 dev->wait_sec = dev->max_wait;
2294 dev->rem_wait_sec = dev->wait_sec;
2295 if (dev->num_wait >= dev->max_num_wait) {
2302 void set_os_device_parameters(DCR *dcr)
2304 DEVICE *dev = dcr->dev;
2306 #if defined(HAVE_LINUX_OS) || defined(HAVE_WIN32)
2309 Dmsg0(050, "In set_os_device_parameters\n");
2310 #if defined(MTSETBLK)
2311 if (dev->min_block_size == dev->max_block_size &&
2312 dev->min_block_size == 0) { /* variable block mode */
2313 mt_com.mt_op = MTSETBLK;
2314 mt_com.mt_count = 0;
2315 Dmsg0(050, "Set block size to zero\n");
2316 if (tape_ioctl(dev->fd, MTIOCTOP, (char *)&mt_com) < 0) {
2317 dev->clrerror(MTSETBLK);
2321 #if defined(MTSETDRVBUFFER)
2322 if (getpid() == 0) { /* Only root can do this */
2323 mt_com.mt_op = MTSETDRVBUFFER;
2324 mt_com.mt_count = MT_ST_CLEARBOOLEANS;
2325 if (!dev->has_cap(CAP_TWOEOF)) {
2326 mt_com.mt_count |= MT_ST_TWO_FM;
2328 if (dev->has_cap(CAP_EOM)) {
2329 mt_com.mt_count |= MT_ST_FAST_MTEOM;
2331 Dmsg0(050, "MTSETDRVBUFFER\n");
2332 if (tape_ioctl(dev->fd, MTIOCTOP, (char *)&mt_com) < 0) {
2333 dev->clrerror(MTSETDRVBUFFER);
2340 #ifdef HAVE_NETBSD_OS
2342 if (dev->min_block_size == dev->max_block_size &&
2343 dev->min_block_size == 0) { /* variable block mode */
2344 mt_com.mt_op = MTSETBSIZ;
2345 mt_com.mt_count = 0;
2346 if (tape_ioctl(dev->fd, MTIOCTOP, (char *)&mt_com) < 0) {
2347 dev->clrerror(MTSETBSIZ);
2349 /* Get notified at logical end of tape */
2350 mt_com.mt_op = MTEWARN;
2351 mt_com.mt_count = 1;
2352 if (tape_ioctl(dev->fd, MTIOCTOP, (char *)&mt_com) < 0) {
2353 dev->clrerror(MTEWARN);
2359 #if HAVE_FREEBSD_OS || HAVE_OPENBSD_OS
2361 if (dev->min_block_size == dev->max_block_size &&
2362 dev->min_block_size == 0) { /* variable block mode */
2363 mt_com.mt_op = MTSETBSIZ;
2364 mt_com.mt_count = 0;
2365 if (tape_ioctl(dev->fd, MTIOCTOP, (char *)&mt_com) < 0) {
2366 dev->clrerror(MTSETBSIZ);
2369 /* Turn this on later when fully tested */
2370 #if defined(xxxMTIOCSETEOTMODEL)
2372 if (dev_cap(dev, CAP_TWOEOF)) {
2377 if (ioctl(dev->fd, MTIOCSETEOTMODEL, (caddr_t)&neof) < 0) {
2379 dev->dev_errno = errno; /* save errno */
2380 Mmsg2(dev->errmsg, _("Unable to set eotmodel on device %s: ERR=%s\n"),
2381 dev->print_name(), be.strerror(dev->dev_errno));
2382 Jmsg(dcr->jcr, M_FATAL, 0, dev->errmsg);
2390 if (dev->min_block_size == dev->max_block_size &&
2391 dev->min_block_size == 0) { /* variable block mode */
2392 mt_com.mt_op = MTSRSZ;
2393 mt_com.mt_count = 0;
2394 if (tape_ioctl(dev->fd, MTIOCTOP, (char *)&mt_com) < 0) {
2395 dev->clrerror(MTSRSZ);
2402 static bool dev_get_os_pos(DEVICE *dev, struct mtget *mt_stat)
2404 Dmsg0(050, "dev_get_os_pos\n");
2405 return dev->has_cap(CAP_MTIOCGET) &&
2406 tape_ioctl(dev->fd, MTIOCGET, (char *)mt_stat) == 0 &&
2407 mt_stat->mt_fileno >= 0;
2410 static char *modes[] = {
2411 "CREATE_READ_WRITE",
2418 static char *mode_to_str(int mode)
2420 static char buf[100];
2421 if (mode < 1 || mode > 4) {
2422 bsnprintf(buf, sizeof(buf), "BAD mode=%d", mode);
2425 return modes[mode-1];