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 my 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 Copyright (C) 2000-2005 Kern Sibbald
34 This program is free software; you can redistribute it and/or
35 modify it under the terms of the GNU General Public License as
36 published by the Free Software Foundation; either version 2 of
37 the License, or (at your option) any later version.
39 This program is distributed in the hope that it will be useful,
40 but WITHOUT ANY WARRANTY; without even the implied warranty of
41 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
42 General Public License for more details.
44 You should have received a copy of the GNU General Public
45 License along with this program; if not, write to the Free
46 Software Foundation, Inc., 59 Temple Place - Suite 330, Boston,
52 * Handling I/O errors and end of tape conditions are a bit tricky.
53 * This is how it is currently done when writting.
54 * On either an I/O error or end of tape,
55 * we will stop writing on the physical device (no I/O recovery is
56 * attempted at least in this daemon). The state flag will be sent
57 * to include ST_EOT, which is ephimeral, and ST_WEOT, which is
58 * persistent. Lots of routines clear ST_EOT, but ST_WEOT is
59 * cleared only when the problem goes away. Now when ST_WEOT
60 * is set all calls to write_block_to_device() call the fix_up
61 * routine. In addition, all threads are blocked
62 * from writing on the tape by calling lock_dev(), and thread other
63 * than the first thread to hit the EOT will block on a condition
64 * variable. The first thread to hit the EOT will continue to
65 * be able to read and write the tape (he sort of tunnels through
66 * the locking mechanism -- see lock_dev() for details).
68 * Now presumably somewhere higher in the chain of command
69 * (device.c), someone will notice the EOT condition and
70 * get a new tape up, get the tape label read, and mark
71 * the label for rewriting. Then this higher level routine
72 * will write the unwritten buffer to the new volume.
73 * Finally, he will release
74 * any blocked threads by doing a broadcast on the condition
75 * variable. At that point, we should be totally back in
76 * business with no lost data.
83 /* Functions in dvd.c */
84 void get_filename(DEVICE *dev, char *VolName, POOL_MEM& archive_name);
85 int mount_dev(DEVICE* dev, int timeout);
86 int unmount_dev(DEVICE *dev, int timeout);
87 void update_free_space_dev(DEVICE* dev);
90 /* Forward referenced functions */
91 void set_os_device_parameters(DEVICE *dev);
92 static bool dev_get_os_pos(DEVICE *dev, struct mtget *mt_stat);
95 * Allocate and initialize the DEVICE structure
96 * Note, if dev is non-NULL, it is already allocated,
97 * thus we neither allocate it nor free it. This allows
98 * the caller to put the packet in shared memory.
100 * Note, for a tape, the device->device_name is the device name
101 * (e.g. /dev/nst0), and for a file, the device name
102 * is the directory in which the file will be placed.
106 init_dev(JCR *jcr, DEVICE *dev, DEVRES *device)
113 /* Check that device is available */
114 if (stat(device->device_name, &statp) < 0) {
117 dev->dev_errno = errno;
119 Jmsg2(jcr, M_FATAL, 0, _("Unable to stat device %s: ERR=%s\n"),
120 device->device_name, be.strerror());
127 if (S_ISDIR(statp.st_mode)) {
129 } else if (S_ISCHR(statp.st_mode)) {
131 } else if (S_ISFIFO(statp.st_mode)) {
135 dev->dev_errno = ENODEV;
137 Jmsg2(jcr, M_FATAL, 0, _("%s is an unknown device type. Must be tape or directory. st_mode=%x\n"),
138 device->device_name, statp.st_mode);
142 dev = (DEVICE *)get_memory(sizeof(DEVICE));
143 memset(dev, 0, sizeof(DEVICE));
144 dev->state = ST_MALLOC;
146 memset(dev, 0, sizeof(DEVICE));
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->capabilities = device->cap_bits;
153 dev->min_block_size = device->min_block_size;
154 dev->max_block_size = device->max_block_size;
155 dev->max_volume_size = device->max_volume_size;
156 dev->max_file_size = device->max_file_size;
157 dev->volume_capacity = device->volume_capacity;
158 dev->max_rewind_wait = device->max_rewind_wait;
159 dev->max_open_wait = device->max_open_wait;
160 dev->max_open_vols = device->max_open_vols;
161 dev->vol_poll_interval = device->vol_poll_interval;
162 dev->max_spool_size = device->max_spool_size;
163 dev->drive_index = device->drive_index;
164 if (tape) { /* No parts on tapes */
165 dev->max_part_size = 0;
168 dev->max_part_size = device->max_part_size;
171 if (dev->vol_poll_interval && dev->vol_poll_interval < 60) {
172 dev->vol_poll_interval = 60;
174 dev->device = device;
177 dev->state |= ST_TAPE;
179 dev->state |= ST_FIFO;
180 dev->capabilities |= CAP_STREAM; /* set stream device */
182 dev->state |= ST_FILE;
185 /* If the device requires mount :
186 * - Check that the mount point is available
187 * - Check that (un)mount commands are defined
189 if (dev->is_file() && device->cap_bits & CAP_REQMOUNT) {
190 if (stat(device->mount_point, &statp) < 0) {
192 dev->dev_errno = errno;
193 Jmsg2(jcr, M_FATAL, 0, _("Unable to stat mount point %s: ERR=%s\n"),
194 device->mount_point, be.strerror());
197 if (!device->mount_command || !device->unmount_command) {
198 Jmsg0(jcr, M_ERROR_TERM, 0, _("Mount and unmount commands must defined for a device which requires mount.\n"));
200 if (!device->write_part_command) {
201 Jmsg0(jcr, M_ERROR_TERM, 0, _("Write part command must be defined for a device which requires mount.\n"));
203 dev->state |= ST_DVD;
206 if (dev->max_block_size > 1000000) {
207 Jmsg3(jcr, M_ERROR, 0, _("Block size %u on device %s is too large, using default %u\n"),
208 dev->max_block_size, dev->dev_name, DEFAULT_BLOCK_SIZE);
209 dev->max_block_size = 0;
211 if (dev->max_block_size % TAPE_BSIZE != 0) {
212 Jmsg2(jcr, M_WARNING, 0, _("Max block size %u not multiple of device %s block size.\n"),
213 dev->max_block_size, dev->dev_name);
216 dev->errmsg = get_pool_memory(PM_EMSG);
219 if ((errstat = pthread_mutex_init(&dev->mutex, NULL)) != 0) {
221 dev->dev_errno = errstat;
222 Mmsg1(dev->errmsg, _("Unable to init mutex: ERR=%s\n"), be.strerror(errstat));
223 Jmsg0(jcr, M_ERROR_TERM, 0, dev->errmsg);
225 if ((errstat = pthread_cond_init(&dev->wait, NULL)) != 0) {
227 dev->dev_errno = errstat;
228 Mmsg1(dev->errmsg, _("Unable to init cond variable: ERR=%s\n"), be.strerror(errstat));
229 Jmsg0(jcr, M_ERROR_TERM, 0, dev->errmsg);
231 if ((errstat = pthread_cond_init(&dev->wait_next_vol, NULL)) != 0) {
233 dev->dev_errno = errstat;
234 Mmsg1(dev->errmsg, _("Unable to init cond variable: ERR=%s\n"), be.strerror(errstat));
235 Jmsg0(jcr, M_ERROR_TERM, 0, dev->errmsg);
237 if ((errstat = pthread_mutex_init(&dev->spool_mutex, NULL)) != 0) {
239 dev->dev_errno = errstat;
240 Mmsg1(dev->errmsg, _("Unable to init mutex: ERR=%s\n"), be.strerror(errstat));
241 Jmsg0(jcr, M_ERROR_TERM, 0, dev->errmsg);
243 if ((errstat = rwl_init(&dev->lock)) != 0) {
245 dev->dev_errno = errstat;
246 Mmsg1(dev->errmsg, _("Unable to init mutex: ERR=%s\n"), be.strerror(errstat));
247 Jmsg0(jcr, M_ERROR_TERM, 0, dev->errmsg);
251 dev->attached_dcrs = New(dlist(dcr, &dcr->dev_link));
252 Dmsg2(29, "init_dev: tape=%d dev_name=%s\n", dev_is_tape(dev), dev->dev_name);
258 * Open the device with the operating system and
259 * initialize buffer pointers.
261 * Returns: -1 on error
264 * Note, for a tape, the VolName is the name we give to the
265 * volume (not really used here), but for a file, the
266 * VolName represents the name of the file to be created/opened.
267 * In the case of a file, the full name is the device name
268 * (archive_name) with the VolName concatenated.
271 open_dev(DEVICE *dev, char *VolName, int mode)
273 if (dev->is_open()) {
275 * *****FIXME***** how to handle two threads wanting
276 * different volumes mounted???? E.g. one is waiting
277 * for the next volume to be mounted, and a new job
278 * starts and snatches up the device.
280 if (VolName && strcmp(dev->VolCatInfo.VolCatName, VolName) != 0) {
284 Mmsg2(&dev->errmsg, _("WARNING!!!! device %s opened %d times!!!\n"),
285 dev->dev_name, dev->use_count);
286 Emsg1(M_WARNING, 0, "%s", dev->errmsg);
290 bstrncpy(dev->VolCatInfo.VolCatName, VolName, sizeof(dev->VolCatInfo.VolCatName));
293 Dmsg3(29, "open_dev: tape=%d dev_name=%s vol=%s\n", dev_is_tape(dev),
294 dev->dev_name, dev->VolCatInfo.VolCatName);
295 dev->state &= ~(ST_LABEL|ST_APPEND|ST_READ|ST_EOT|ST_WEOT|ST_EOF);
296 dev->label_type = B_BACULA_LABEL;
297 if (dev->is_tape() || dev->is_fifo()) {
300 Dmsg0(29, "open_dev: device is tape\n");
301 if (mode == OPEN_READ_WRITE) {
302 dev->mode = O_RDWR | O_BINARY;
303 } else if (mode == OPEN_READ_ONLY) {
304 dev->mode = O_RDONLY | O_BINARY;
305 } else if (mode == OPEN_WRITE_ONLY) {
306 dev->mode = O_WRONLY | O_BINARY;
308 Emsg0(M_ABORT, 0, _("Illegal mode given to open_dev.\n"));
310 timeout = dev->max_open_wait;
312 if (dev->is_fifo() && timeout) {
314 dev->tid = start_thread_timer(pthread_self(), timeout);
316 /* If busy retry each second for max_open_wait seconds */
317 while ((dev->fd = open(dev->dev_name, dev->mode, MODE_RW)) < 0) {
319 if (errno == EINTR || errno == EAGAIN) {
322 if (errno == EBUSY && timeout-- > 0) {
323 Dmsg2(100, "Device %s busy. ERR=%s\n", dev->dev_name, be.strerror());
327 dev->dev_errno = errno;
328 Mmsg2(&dev->errmsg, _("stored: unable to open device %s: ERR=%s\n"),
329 dev->dev_name, be.strerror());
330 /* Stop any open timer we set */
332 stop_thread_timer(dev->tid);
335 Emsg0(M_FATAL, 0, dev->errmsg);
340 dev->state |= ST_OPENED;
342 update_pos_dev(dev); /* update position */
343 set_os_device_parameters(dev); /* do system dependent stuff */
345 /* Stop any open() timer we started */
347 stop_thread_timer(dev->tid);
350 Dmsg1(29, "open_dev: tape %d opened\n", dev->fd);
352 POOL_MEM archive_name(PM_FNAME);
353 struct stat filestat;
355 * Handle opening of File Archive (not a tape)
357 if (dev->part == 0) {
362 /* if num_parts has not been set, but VolCatInfo is available, copy
363 * it from the VolCatInfo.VolCatParts */
364 if (dev->num_parts < dev->VolCatInfo.VolCatParts) {
365 dev->num_parts = dev->VolCatInfo.VolCatParts;
368 if (VolName == NULL || *VolName == 0) {
369 Mmsg(dev->errmsg, _("Could not open file device %s. No Volume name given.\n"),
373 get_filename(dev, VolName, archive_name);
375 if (mount_dev(dev, 1) < 0) {
376 Mmsg(dev->errmsg, _("Could not mount archive device %s.\n"),
378 Emsg0(M_FATAL, 0, dev->errmsg);
383 Dmsg2(29, "open_dev: device is disk %s (mode:%d)\n", archive_name.c_str(), mode);
384 dev->openmode = mode;
387 * If we are not trying to access the last part, set mode to
388 * OPEN_READ_ONLY as writing would be an error.
390 if (dev->part < dev->num_parts) {
391 mode = OPEN_READ_ONLY;
394 if (mode == OPEN_READ_WRITE) {
395 dev->mode = O_CREAT | O_RDWR | O_BINARY;
396 } else if (mode == OPEN_READ_ONLY) {
397 dev->mode = O_RDONLY | O_BINARY;
398 } else if (mode == OPEN_WRITE_ONLY) {
399 dev->mode = O_WRONLY | O_BINARY;
401 Emsg0(M_ABORT, 0, _("Illegal mode given to open_dev.\n"));
403 /* If creating file, give 0640 permissions */
404 if ((dev->fd = open(archive_name.c_str(), dev->mode, 0640)) < 0) {
406 dev->dev_errno = errno;
407 Mmsg2(&dev->errmsg, _("Could not open: %s, ERR=%s\n"), archive_name.c_str(), be.strerror());
408 Emsg0(M_FATAL, 0, dev->errmsg);
411 dev->state |= ST_OPENED;
413 update_pos_dev(dev); /* update position */
414 if (fstat(dev->fd, &filestat) < 0) {
416 dev->dev_errno = errno;
417 Mmsg2(&dev->errmsg, _("Could not fstat: %s, ERR=%s\n"), archive_name.c_str(), be.strerror());
418 Emsg0(M_FATAL, 0, dev->errmsg);
420 dev->part_size = filestat.st_size;
423 Dmsg4(29, "open_dev: disk fd=%d opened, part=%d/%d, part_size=%u\n", dev->fd, dev->part, dev->num_parts, dev->part_size);
424 if (dev->is_dvd() && (dev->mode != OPEN_READ_ONLY) &&
425 (dev->free_space_errno == 0 || dev->num_parts == dev->part)) {
426 update_free_space_dev(dev);
434 bool _rewind_dev(char *file, int line, DEVICE *dev)
436 Dmsg2(100, "rewind_dev called from %s:%d\n", file, line);
437 return rewind_dev(dev);
443 * Returns: true on success
446 bool rewind_dev(DEVICE *dev)
451 Dmsg1(29, "rewind_dev %s\n", dev->dev_name);
453 dev->dev_errno = EBADF;
454 Mmsg1(&dev->errmsg, _("Bad call to rewind_dev. Device %s not open\n"),
456 Emsg0(M_ABORT, 0, dev->errmsg);
459 dev->state &= ~(ST_EOT|ST_EOF|ST_WEOT); /* remove EOF/EOT flags */
460 dev->block_num = dev->file = 0;
463 if (dev->is_tape()) {
464 mt_com.mt_op = MTREW;
466 /* If we get an I/O error on rewind, it is probably because
467 * the drive is actually busy. We loop for (about 5 minutes)
468 * retrying every 5 seconds.
470 for (i=dev->max_rewind_wait; ; i -= 5) {
471 if (ioctl(dev->fd, MTIOCTOP, (char *)&mt_com) < 0) {
473 clrerror_dev(dev, MTREW);
474 if (i == dev->max_rewind_wait) {
475 Dmsg1(200, "Rewind error, %s. retrying ...\n", be.strerror());
477 if (dev->dev_errno == EIO && i > 0) {
478 Dmsg0(200, "Sleeping 5 seconds.\n");
482 Mmsg2(&dev->errmsg, _("Rewind error on %s. ERR=%s.\n"),
483 dev->dev_name, be.strerror());
488 } else if (dev->is_file()) {
489 if (lseek_dev(dev, (off_t)0, SEEK_SET) < 0) {
491 dev->dev_errno = errno;
492 Mmsg2(&dev->errmsg, _("lseek_dev error on %s. ERR=%s.\n"),
493 dev->dev_name, be.strerror());
501 * Called to indicate that we have just read an
502 * EOF from the device.
504 void DEVICE::set_eof()
514 * Called to indicate we are now at the end of the tape, and
515 * writing is not possible.
517 void DEVICE::set_eot()
519 state |= (ST_EOF|ST_EOT|ST_WEOT);
520 state &= ~ST_APPEND; /* make tape read-only */
524 * Position device to end of medium (end of data)
525 * Returns: 1 on succes
532 struct mtget mt_stat;
536 Dmsg0(29, "eod_dev\n");
540 dev->state &= ~(ST_EOF); /* remove EOF flags */
541 dev->block_num = dev->file = 0;
544 if (dev->state & (ST_FIFO | ST_PROG)) {
547 if (!dev->is_tape()) {
548 pos = lseek_dev(dev, (off_t)0, SEEK_END);
549 // Dmsg1(100, "====== Seek to %lld\n", pos);
552 dev->state |= ST_EOT;
555 dev->dev_errno = errno;
557 Mmsg2(&dev->errmsg, _("lseek_dev error on %s. ERR=%s.\n"),
558 dev->dev_name, be.strerror());
562 if (dev_cap(dev, CAP_FASTFSF) && !dev_cap(dev, CAP_EOM)) {
563 Dmsg0(100,"Using FAST FSF for EOM\n");
564 /* If unknown position, rewind */
565 if (!dev_get_os_pos(dev, &mt_stat)) {
566 if (!rewind_dev(dev)) {
570 mt_com.mt_op = MTFSF;
572 * ***FIXME*** fix code to handle case that INT16_MAX is
575 mt_com.mt_count = INT16_MAX; /* use big positive number */
576 if (mt_com.mt_count < 0) {
577 mt_com.mt_count = INT16_MAX; /* brain damaged system */
581 if (dev_cap(dev, CAP_MTIOCGET) && (dev_cap(dev, CAP_FASTFSF) || dev_cap(dev, CAP_EOM))) {
582 if (dev_cap(dev, CAP_EOM)) {
583 Dmsg0(100,"Using EOM for EOM\n");
584 mt_com.mt_op = MTEOM;
588 if ((stat=ioctl(dev->fd, MTIOCTOP, (char *)&mt_com)) < 0) {
590 clrerror_dev(dev, mt_com.mt_op);
591 Dmsg1(50, "ioctl error: %s\n", be.strerror());
593 Mmsg2(&dev->errmsg, _("ioctl MTEOM error on %s. ERR=%s.\n"),
594 dev->dev_name, be.strerror());
598 if (!dev_get_os_pos(dev, &mt_stat)) {
600 clrerror_dev(dev, -1);
601 Mmsg2(&dev->errmsg, _("ioctl MTIOCGET error on %s. ERR=%s.\n"),
602 dev->dev_name, be.strerror());
605 Dmsg2(100, "EOD file=%d block=%d\n", mt_stat.mt_fileno, mt_stat.mt_blkno);
607 dev->file = mt_stat.mt_fileno;
614 * Rewind then use FSF until EOT reached
616 if (!rewind_dev(dev)) {
620 * Move file by file to the end of the tape
623 for (file_num=dev->file; !dev->at_eot(); file_num++) {
624 Dmsg0(200, "eod_dev: doing fsf 1\n");
625 if (!fsf_dev(dev, 1)) {
626 Dmsg0(200, "fsf_dev error.\n");
630 * Avoid infinite loop. ***FIXME*** possibly add code
631 * to set EOD or to turn off CAP_FASTFSF if on.
633 if (file_num == (int)dev->file) {
634 struct mtget mt_stat;
635 Dmsg1(100, "fsf_dev did not advance from file %d\n", file_num);
636 if (dev_get_os_pos(dev, &mt_stat)) {
637 Dmsg2(100, "Adjust file from %d to %d\n", dev->file , mt_stat.mt_fileno);
639 dev->file = mt_stat.mt_fileno;
642 break; /* we are not progressing, bail out */
647 * Some drivers leave us after second EOF when doing
648 * MTEOM, so we must backup so that appending overwrites
651 if (dev_cap(dev, CAP_BSFATEOM)) {
652 struct mtget mt_stat;
653 /* Backup over EOF */
654 stat = bsf_dev(dev, 1);
655 /* If BSF worked and fileno is known (not -1), set file */
656 if (dev_get_os_pos(dev, &mt_stat)) {
657 Dmsg2(100, "BSFATEOF adjust file from %d to %d\n", dev->file , mt_stat.mt_fileno);
658 dev->file = mt_stat.mt_fileno;
660 dev->file++; /* wing it -- not correct on all OSes */
663 update_pos_dev(dev); /* update position */
666 Dmsg1(200, "EOD dev->file=%d\n", dev->file);
671 * Set the position of the device -- only for files
672 * For other devices, there is no generic way to do it.
673 * Returns: true on succes
676 bool update_pos_dev(DEVICE *dev)
682 dev->dev_errno = EBADF;
683 Mmsg0(&dev->errmsg, _("Bad device call. Archive not open\n"));
684 Emsg0(M_FATAL, 0, dev->errmsg);
688 /* Find out where we are */
689 if (dev->is_file()) {
692 pos = lseek_dev(dev, (off_t)0, SEEK_CUR);
695 dev->dev_errno = errno;
696 Pmsg1(000, "Seek error: ERR=%s\n", be.strerror());
697 Mmsg2(&dev->errmsg, _("lseek_dev error on %s. ERR=%s.\n"),
698 dev->dev_name, be.strerror());
701 dev->file_addr = pos;
709 * Return the status of the device. This was meant
710 * to be a generic routine. Unfortunately, it doesn't
711 * seem possible (at least I do not know how to do it
712 * currently), which means that for the moment, this
713 * routine has very little value.
717 uint32_t status_dev(DEVICE *dev)
719 struct mtget mt_stat;
722 if (dev->state & (ST_EOT | ST_WEOT)) {
726 if (dev->state & ST_EOF) {
730 if (dev->is_tape()) {
732 Dmsg0(-20," Bacula status:");
733 Dmsg2(-20," file=%d block=%d\n", dev->file, dev->block_num);
734 if (ioctl(dev->fd, MTIOCGET, (char *)&mt_stat) < 0) {
736 dev->dev_errno = errno;
737 Mmsg2(&dev->errmsg, _("ioctl MTIOCGET error on %s. ERR=%s.\n"),
738 dev->dev_name, be.strerror());
741 Dmsg0(-20, " Device status:");
743 #if defined(HAVE_LINUX_OS)
744 if (GMT_EOF(mt_stat.mt_gstat)) {
748 if (GMT_BOT(mt_stat.mt_gstat)) {
752 if (GMT_EOT(mt_stat.mt_gstat)) {
756 if (GMT_SM(mt_stat.mt_gstat)) {
760 if (GMT_EOD(mt_stat.mt_gstat)) {
764 if (GMT_WR_PROT(mt_stat.mt_gstat)) {
766 Dmsg0(-20, " WR_PROT");
768 if (GMT_ONLINE(mt_stat.mt_gstat)) {
770 Dmsg0(-20, " ONLINE");
772 if (GMT_DR_OPEN(mt_stat.mt_gstat)) {
774 Dmsg0(-20, " DR_OPEN");
776 if (GMT_IM_REP_EN(mt_stat.mt_gstat)) {
777 stat |= BMT_IM_REP_EN;
778 Dmsg0(-20, " IM_REP_EN");
780 #endif /* !SunOS && !OSF */
781 if (dev_cap(dev, CAP_MTIOCGET)) {
782 Dmsg2(-20, " file=%d block=%d\n", mt_stat.mt_fileno, mt_stat.mt_blkno);
784 Dmsg2(-20, " file=%d block=%d\n", -1, -1);
787 stat |= BMT_ONLINE | BMT_BOT;
794 * Load medium in device
795 * Returns: true on success
798 bool load_dev(DEVICE *dev)
805 dev->dev_errno = EBADF;
806 Mmsg0(&dev->errmsg, _("Bad call to load_dev. Archive not open\n"));
807 Emsg0(M_FATAL, 0, dev->errmsg);
810 if (!(dev->is_tape())) {
814 Dmsg0(200, "stored: MTLOAD command not available\n");
816 dev->dev_errno = ENOTTY; /* function not available */
817 Mmsg2(&dev->errmsg, _("ioctl MTLOAD error on %s. ERR=%s.\n"),
818 dev->dev_name, be.strerror());
822 dev->block_num = dev->file = 0;
825 mt_com.mt_op = MTLOAD;
827 if (ioctl(dev->fd, MTIOCTOP, (char *)&mt_com) < 0) {
829 dev->dev_errno = errno;
830 Mmsg2(&dev->errmsg, _("ioctl MTLOAD error on %s. ERR=%s.\n"),
831 dev->dev_name, be.strerror());
839 * Rewind device and put it offline
840 * Returns: true on success
843 bool offline_dev(DEVICE *dev)
848 dev->dev_errno = EBADF;
849 Mmsg0(&dev->errmsg, _("Bad call to offline_dev. Archive not open\n"));
850 Emsg0(M_FATAL, 0, dev->errmsg);
853 if (!(dev->is_tape())) {
857 dev->state &= ~(ST_APPEND|ST_READ|ST_EOT|ST_EOF|ST_WEOT); /* remove EOF/EOT flags */
858 dev->block_num = dev->file = 0;
863 mt_com.mt_op = MTUNLOCK;
865 ioctl(dev->fd, MTIOCTOP, (char *)&mt_com);
867 mt_com.mt_op = MTOFFL;
869 if (ioctl(dev->fd, MTIOCTOP, (char *)&mt_com) < 0) {
871 dev->dev_errno = errno;
872 Mmsg2(&dev->errmsg, _("ioctl MTOFFL error on %s. ERR=%s.\n"),
873 dev->dev_name, be.strerror());
876 Dmsg1(100, "Offlined device %s\n", dev->dev_name);
880 bool offline_or_rewind_dev(DEVICE *dev)
885 if (dev_cap(dev, CAP_OFFLINEUNMOUNT)) {
886 return offline_dev(dev);
889 * Note, this rewind probably should not be here (it wasn't
890 * in prior versions of Bacula), but on FreeBSD, this is
891 * needed in the case the tape was "frozen" due to an error
892 * such as backspacing after writing and EOF. If it is not
893 * done, all future references to the drive get and I/O error.
895 clrerror_dev(dev, MTREW);
896 return rewind_dev(dev);
901 * Foward space a file
902 * Returns: true on success
906 fsf_dev(DEVICE *dev, int num)
908 struct mtget mt_stat;
913 dev->dev_errno = EBADF;
914 Mmsg0(&dev->errmsg, _("Bad call to fsf_dev. Archive not open\n"));
915 Emsg0(M_FATAL, 0, dev->errmsg);
919 if (!dev->is_tape()) {
922 if (dev->state & ST_EOT) {
924 Mmsg1(dev->errmsg, _("Device %s at End of Tape.\n"), dev->dev_name);
927 if (dev->state & ST_EOF) {
928 Dmsg0(200, "ST_EOF set on entry to FSF\n");
931 Dmsg0(100, "fsf_dev\n");
934 * If Fast forward space file is set, then we
935 * use MTFSF to forward space and MTIOCGET
936 * to get the file position. We assume that
937 * the SCSI driver will ensure that we do not
938 * forward space past the end of the medium.
940 if (dev_cap(dev, CAP_FSF) && dev_cap(dev, CAP_MTIOCGET) && dev_cap(dev, CAP_FASTFSF)) {
941 mt_com.mt_op = MTFSF;
942 mt_com.mt_count = num;
943 stat = ioctl(dev->fd, MTIOCTOP, (char *)&mt_com);
944 if (stat < 0 || !dev_get_os_pos(dev, &mt_stat)) {
946 dev->state |= ST_EOT;
947 Dmsg0(200, "Set ST_EOT\n");
948 clrerror_dev(dev, MTFSF);
949 Mmsg2(dev->errmsg, _("ioctl MTFSF error on %s. ERR=%s.\n"),
950 dev->dev_name, be.strerror());
951 Dmsg1(200, "%s", dev->errmsg);
954 Dmsg2(200, "fsf file=%d block=%d\n", mt_stat.mt_fileno, mt_stat.mt_blkno);
956 dev->file = mt_stat.mt_fileno;
960 * Here if CAP_FSF is set, and virtually all drives
961 * these days support it, we read a record, then forward
962 * space one file. Using this procedure, which is slow,
963 * is the only way we can be sure that we don't read
964 * two consecutive EOF marks, which means End of Data.
966 } else if (dev_cap(dev, CAP_FSF)) {
969 Dmsg0(200, "FSF has cap_fsf\n");
970 if (dev->max_block_size == 0) {
971 rbuf_len = DEFAULT_BLOCK_SIZE;
973 rbuf_len = dev->max_block_size;
975 rbuf = get_memory(rbuf_len);
976 mt_com.mt_op = MTFSF;
978 while (num-- && !(dev->state & ST_EOT)) {
979 Dmsg0(100, "Doing read before fsf\n");
980 if ((stat = read(dev->fd, (char *)rbuf, rbuf_len)) < 0) {
981 if (errno == ENOMEM) { /* tape record exceeds buf len */
982 stat = rbuf_len; /* This is OK */
985 dev->state |= ST_EOT;
986 clrerror_dev(dev, -1);
987 Dmsg2(100, "Set ST_EOT read errno=%d. ERR=%s\n", dev->dev_errno,
989 Mmsg2(dev->errmsg, _("read error on %s. ERR=%s.\n"),
990 dev->dev_name, be.strerror());
991 Dmsg1(100, "%s", dev->errmsg);
995 if (stat == 0) { /* EOF */
997 Dmsg1(100, "End of File mark from read. File=%d\n", dev->file+1);
998 /* Two reads of zero means end of tape */
999 if (dev->state & ST_EOF) {
1000 dev->state |= ST_EOT;
1001 Dmsg0(100, "Set ST_EOT\n");
1007 } else { /* Got data */
1008 dev->state &= ~(ST_EOF|ST_EOT);
1011 Dmsg0(100, "Doing MTFSF\n");
1012 stat = ioctl(dev->fd, MTIOCTOP, (char *)&mt_com);
1013 if (stat < 0) { /* error => EOT */
1015 dev->state |= ST_EOT;
1016 Dmsg0(100, "Set ST_EOT\n");
1017 clrerror_dev(dev, MTFSF);
1018 Mmsg2(&dev->errmsg, _("ioctl MTFSF error on %s. ERR=%s.\n"),
1019 dev->dev_name, be.strerror());
1020 Dmsg0(100, "Got < 0 for MTFSF\n");
1021 Dmsg1(100, "%s", dev->errmsg);
1029 * No FSF, so use FSR to simulate it
1032 Dmsg0(200, "Doing FSR for FSF\n");
1033 while (num-- && !(dev->state & ST_EOT)) {
1034 fsr_dev(dev, INT32_MAX); /* returns -1 on EOF or EOT */
1036 if (dev->state & ST_EOT) {
1038 Mmsg1(dev->errmsg, _("Device %s at End of Tape.\n"), dev->dev_name);
1044 update_pos_dev(dev);
1045 Dmsg1(200, "Return %d from FSF\n", stat);
1046 if (dev->state & ST_EOF)
1047 Dmsg0(200, "ST_EOF set on exit FSF\n");
1048 if (dev->state & ST_EOT)
1049 Dmsg0(200, "ST_EOT set on exit FSF\n");
1050 Dmsg1(200, "Return from FSF file=%d\n", dev->file);
1055 * Backward space a file
1056 * Returns: false on failure
1060 bsf_dev(DEVICE *dev, int num)
1066 dev->dev_errno = EBADF;
1067 Mmsg0(dev->errmsg, _("Bad call to bsf_dev. Archive device not open\n"));
1068 Emsg0(M_FATAL, 0, dev->errmsg);
1072 if (!dev->is_tape()) {
1073 Mmsg1(dev->errmsg, _("Device %s cannot BSF because it is not a tape.\n"),
1077 Dmsg0(29, "bsf_dev\n");
1078 dev->state &= ~(ST_EOT|ST_EOF);
1082 mt_com.mt_op = MTBSF;
1083 mt_com.mt_count = num;
1084 stat = ioctl(dev->fd, MTIOCTOP, (char *)&mt_com);
1087 clrerror_dev(dev, MTBSF);
1088 Mmsg2(dev->errmsg, _("ioctl MTBSF error on %s. ERR=%s.\n"),
1089 dev->dev_name, be.strerror());
1091 update_pos_dev(dev);
1097 * Foward space a record
1098 * Returns: false on failure
1102 fsr_dev(DEVICE *dev, int num)
1108 dev->dev_errno = EBADF;
1109 Mmsg0(dev->errmsg, _("Bad call to fsr_dev. Archive not open\n"));
1110 Emsg0(M_FATAL, 0, dev->errmsg);
1114 if (!dev->is_tape()) {
1117 if (!dev_cap(dev, CAP_FSR)) {
1118 Mmsg1(dev->errmsg, _("ioctl MTFSR not permitted on %s.\n"), dev->dev_name);
1122 Dmsg1(29, "fsr_dev %d\n", num);
1123 mt_com.mt_op = MTFSR;
1124 mt_com.mt_count = num;
1125 stat = ioctl(dev->fd, MTIOCTOP, (char *)&mt_com);
1127 dev->state &= ~ST_EOF;
1128 dev->block_num += num;
1131 struct mtget mt_stat;
1132 clrerror_dev(dev, MTFSR);
1133 Dmsg1(100, "FSF fail: ERR=%s\n", be.strerror());
1134 if (dev_get_os_pos(dev, &mt_stat)) {
1135 Dmsg4(100, "Adjust from %d:%d to %d:%d\n", dev->file,
1136 dev->block_num, mt_stat.mt_fileno, mt_stat.mt_blkno);
1137 dev->file = mt_stat.mt_fileno;
1138 dev->block_num = mt_stat.mt_blkno;
1140 if (dev->state & ST_EOF) {
1141 dev->state |= ST_EOT;
1146 Mmsg2(dev->errmsg, _("ioctl MTFSR error on %s. ERR=%s.\n"),
1147 dev->dev_name, be.strerror());
1149 update_pos_dev(dev);
1154 * Backward space a record
1155 * Returns: false on failure
1159 bsr_dev(DEVICE *dev, int num)
1165 dev->dev_errno = EBADF;
1166 Mmsg0(dev->errmsg, _("Bad call to bsr_dev. Archive not open\n"));
1167 Emsg0(M_FATAL, 0, dev->errmsg);
1171 if (!dev->is_tape()) {
1175 if (!dev_cap(dev, CAP_BSR)) {
1176 Mmsg1(dev->errmsg, _("ioctl MTBSR not permitted on %s.\n"), dev->dev_name);
1180 Dmsg0(29, "bsr_dev\n");
1181 dev->block_num -= num;
1182 dev->state &= ~(ST_EOF|ST_EOT|ST_EOF);
1183 mt_com.mt_op = MTBSR;
1184 mt_com.mt_count = num;
1185 stat = ioctl(dev->fd, MTIOCTOP, (char *)&mt_com);
1188 clrerror_dev(dev, MTBSR);
1189 Mmsg2(dev->errmsg, _("ioctl MTBSR error on %s. ERR=%s.\n"),
1190 dev->dev_name, be.strerror());
1192 update_pos_dev(dev);
1197 * Reposition the device to file, block
1198 * Returns: false on failure
1202 reposition_dev(DEVICE *dev, uint32_t file, uint32_t block)
1205 dev->dev_errno = EBADF;
1206 Mmsg0(dev->errmsg, _("Bad call to reposition_dev. Archive not open\n"));
1207 Emsg0(M_FATAL, 0, dev->errmsg);
1211 if (!dev->is_tape()) {
1212 off_t pos = (((off_t)file)<<32) + block;
1213 Dmsg1(100, "===== lseek_dev to %d\n", (int)pos);
1214 if (lseek_dev(dev, pos, SEEK_SET) == (off_t)-1) {
1216 dev->dev_errno = errno;
1217 Mmsg2(dev->errmsg, _("lseek_dev error on %s. ERR=%s.\n"),
1218 dev->dev_name, be.strerror());
1222 dev->block_num = block;
1223 dev->file_addr = pos;
1226 Dmsg4(100, "reposition_dev from %u:%u to %u:%u\n",
1227 dev->file, dev->block_num, file, block);
1228 if (file < dev->file) {
1229 Dmsg0(100, "Rewind_dev\n");
1230 if (!rewind_dev(dev)) {
1234 if (file > dev->file) {
1235 Dmsg1(100, "fsf %d\n", file-dev->file);
1236 if (!fsf_dev(dev, file-dev->file)) {
1237 Dmsg1(100, "fsf failed! ERR=%s\n", strerror_dev(dev));
1240 Dmsg2(100, "wanted_file=%d at_file=%d\n", file, dev->file);
1242 if (block < dev->block_num) {
1243 Dmsg2(100, "wanted_blk=%d at_blk=%d\n", block, dev->block_num);
1244 Dmsg0(100, "bsf_dev 1\n");
1246 Dmsg0(100, "fsf_dev 1\n");
1248 Dmsg2(100, "wanted_blk=%d at_blk=%d\n", block, dev->block_num);
1250 if (dev_cap(dev, CAP_POSITIONBLOCKS) && block > dev->block_num) {
1251 /* Ignore errors as Bacula can read to the correct block */
1252 Dmsg1(100, "fsr %d\n", block-dev->block_num);
1253 return fsr_dev(dev, block-dev->block_num);
1261 * Write an end of file on the device
1262 * Returns: 0 on success
1263 * non-zero on failure
1266 weof_dev(DEVICE *dev, int num)
1270 Dmsg0(29, "weof_dev\n");
1273 dev->dev_errno = EBADF;
1274 Mmsg0(dev->errmsg, _("Bad call to weof_dev. Archive drive not open\n"));
1275 Emsg0(M_FATAL, 0, dev->errmsg);
1280 if (!dev->is_tape()) {
1283 if (!dev->can_append()) {
1284 Mmsg0(dev->errmsg, _("Attempt to WEOF on non-appendable Volume\n"));
1285 Emsg0(M_FATAL, 0, dev->errmsg);
1289 dev->state &= ~(ST_EOT | ST_EOF); /* remove EOF/EOT flags */
1290 mt_com.mt_op = MTWEOF;
1291 mt_com.mt_count = num;
1292 stat = ioctl(dev->fd, MTIOCTOP, (char *)&mt_com);
1299 clrerror_dev(dev, MTWEOF);
1301 Mmsg2(dev->errmsg, _("ioctl MTWEOF error on %s. ERR=%s.\n"),
1302 dev->dev_name, be.strerror());
1309 * Return string message with last error in English
1310 * Be careful not to call this routine from within dev.c
1311 * while editing an Mmsg() or you will end up in a recursive
1312 * loop creating a Segmentation Violation.
1315 strerror_dev(DEVICE *dev)
1322 * If implemented in system, clear the tape
1326 clrerror_dev(DEVICE *dev, int func)
1328 const char *msg = NULL;
1329 struct mtget mt_stat;
1332 dev->dev_errno = errno; /* save errno */
1334 dev->VolCatInfo.VolCatErrors++;
1337 if (!dev->is_tape()) {
1340 if (errno == ENOTTY || errno == ENOSYS) { /* Function not implemented */
1343 Emsg0(M_ABORT, 0, "Got ENOTTY on read/write!\n");
1347 dev->capabilities &= ~CAP_EOF; /* turn off feature */
1352 dev->capabilities &= ~CAP_EOM; /* turn off feature */
1357 dev->capabilities &= ~CAP_FSF; /* turn off feature */
1361 dev->capabilities &= ~CAP_BSF; /* turn off feature */
1365 dev->capabilities &= ~CAP_FSR; /* turn off feature */
1369 dev->capabilities &= ~CAP_BSR; /* turn off feature */
1390 bsnprintf(buf, sizeof(buf), "unknown func code %d", func);
1395 dev->dev_errno = ENOSYS;
1396 Mmsg1(dev->errmsg, _("I/O function \"%s\" not supported on this device.\n"), msg);
1397 Emsg0(M_ERROR, 0, dev->errmsg);
1400 /* On some systems such as NetBSD, this clears all errors */
1401 ioctl(dev->fd, MTIOCGET, (char *)&mt_stat);
1403 /* Found on Linux */
1407 mt_com.mt_op = MTIOCLRERR;
1408 mt_com.mt_count = 1;
1409 /* Clear any error condition on the tape */
1410 ioctl(dev->fd, MTIOCTOP, (char *)&mt_com);
1411 Dmsg0(200, "Did MTIOCLRERR\n");
1415 /* Typically on FreeBSD */
1418 /* Read and clear SCSI error status */
1419 union mterrstat mt_errstat;
1420 Dmsg2(200, "Doing MTIOCERRSTAT errno=%d ERR=%s\n", dev->dev_errno,
1421 strerror(dev->dev_errno));
1422 ioctl(dev->fd, MTIOCERRSTAT, (char *)&mt_errstat);
1426 /* Clear Subsystem Exception OSF1 */
1430 mt_com.mt_op = MTCSE;
1431 mt_com.mt_count = 1;
1432 /* Clear any error condition on the tape */
1433 ioctl(dev->fd, MTIOCTOP, (char *)&mt_com);
1434 Dmsg0(200, "Did MTCSE\n");
1440 * Flush buffer contents
1443 int flush_dev(DEVICE *dev)
1448 static void do_close(DEVICE *dev)
1451 Dmsg1(29, "really close_dev %s\n", dev->dev_name);
1456 if (unmount_dev(dev, 1) < 0) {
1457 Dmsg1(0, "Cannot unmount device %s.\n", dev->dev_name);
1460 /* Remove the last part file if it is empty */
1461 if (dev->can_append() && (dev->num_parts > 0)) {
1463 POOL_MEM archive_name(PM_FNAME);
1464 dev->part = dev->num_parts;
1465 get_filename(dev, dev->VolCatInfo.VolCatName, archive_name);
1466 /* Check that the part file is empty */
1467 if ((stat(archive_name.c_str(), &statp) == 0) && (statp.st_size == 0)) {
1468 unlink(archive_name.c_str());
1472 /* Clean up device packet so it can be reused */
1474 dev->state &= ~(ST_OPENED|ST_LABEL|ST_READ|ST_APPEND|ST_EOT|ST_WEOT|ST_EOF);
1475 dev->label_type = B_BACULA_LABEL;
1476 dev->file = dev->block_num = 0;
1481 dev->part_start = 0;
1482 dev->EndFile = dev->EndBlock = 0;
1483 memset(&dev->VolCatInfo, 0, sizeof(dev->VolCatInfo));
1484 memset(&dev->VolHdr, 0, sizeof(dev->VolHdr));
1486 stop_thread_timer(dev->tid);
1496 close_dev(DEVICE *dev)
1499 Mmsg0(&dev->errmsg, _("Bad call to close_dev. Archive not open\n"));
1500 Emsg0(M_FATAL, 0, dev->errmsg);
1503 /*if (dev->fd >= 0 && dev->use_count == 1) {*/
1504 /* No need to check if dev->fd >= 0: it is checked again
1505 * in do_close, and do_close MUST be called for volumes
1506 * splitted in parts, even if dev->fd == -1. */
1507 if (dev->use_count == 1) {
1509 } else if (dev->use_count > 0) {
1514 ASSERT(dev->use_count >= 0);
1519 * Used when unmounting the device, ignore use_count
1521 void force_close_dev(DEVICE *dev)
1524 Mmsg0(&dev->errmsg, _("Bad call to force_close_dev. Archive not open\n"));
1525 Emsg0(M_FATAL, 0, dev->errmsg);
1528 Dmsg1(29, "Force close_dev %s\n", dev->dev_name);
1532 ASSERT(dev->use_count >= 0);
1536 bool truncate_dev(DEVICE *dev)
1538 Dmsg1(100, "truncate_dev %s\n", dev->dev_name);
1539 if (dev->is_tape()) {
1540 return true; /* we don't really truncate tapes */
1541 /* maybe we should rewind and write and eof ???? */
1544 /* If there is more than one part, open the first one, and then truncate it. */
1545 if (dev->num_parts > 0) {
1547 dev->VolCatInfo.VolCatParts = 0;
1548 if (open_first_part(dev) < 0) {
1550 Mmsg1(&dev->errmsg, "Unable to truncate device, because I'm unable to open the first part. ERR=%s\n", be.strerror());
1554 if (ftruncate(dev->fd, 0) != 0) {
1556 Mmsg1(&dev->errmsg, _("Unable to truncate device. ERR=%s\n"), be.strerror());
1563 dev_is_tape(DEVICE *dev)
1565 return dev->is_tape() ? true : false;
1570 * return 1 if the device is read for write, and 0 otherwise
1571 * This is meant for checking at the end of a job to see
1572 * if we still have a tape (perhaps not if at end of tape
1573 * and the job is canceled).
1576 dev_can_write(DEVICE *dev)
1578 if (dev->is_open() && dev->can_append() &&
1579 dev->is_labeled() && !(dev->state & ST_WEOT)) {
1587 dev_name(DEVICE *dev)
1589 return dev->dev_name;
1593 dev_vol_name(DEVICE *dev)
1595 return dev->VolCatInfo.VolCatName;
1598 uint32_t dev_block(DEVICE *dev)
1600 update_pos_dev(dev);
1601 return dev->block_num;
1604 uint32_t dev_file(DEVICE *dev)
1606 update_pos_dev(dev);
1611 * Free memory allocated for the device
1614 term_dev(DEVICE *dev)
1617 dev->dev_errno = EBADF;
1618 Mmsg0(&dev->errmsg, _("Bad call to term_dev. Archive not open\n"));
1619 Emsg0(M_FATAL, 0, dev->errmsg);
1623 Dmsg0(29, "term_dev\n");
1624 if (dev->dev_name) {
1625 free_memory(dev->dev_name);
1626 dev->dev_name = NULL;
1629 free_pool_memory(dev->errmsg);
1632 pthread_mutex_destroy(&dev->mutex);
1633 pthread_cond_destroy(&dev->wait);
1634 pthread_cond_destroy(&dev->wait_next_vol);
1635 pthread_mutex_destroy(&dev->spool_mutex);
1636 rwl_destroy(&dev->lock);
1637 if (dev->attached_dcrs) {
1638 delete dev->attached_dcrs;
1639 dev->attached_dcrs = NULL;
1641 if (dev->state & ST_MALLOC) {
1642 free_pool_memory((POOLMEM *)dev);
1647 * This routine initializes the device wait timers
1649 void init_dev_wait_timers(DEVICE *dev)
1651 /* ******FIXME******* put these on config variables */
1652 dev->min_wait = 60 * 60;
1653 dev->max_wait = 24 * 60 * 60;
1654 dev->max_num_wait = 9; /* 5 waits =~ 1 day, then 1 day at a time */
1655 dev->wait_sec = dev->min_wait;
1656 dev->rem_wait_sec = dev->wait_sec;
1659 dev->BadVolName[0] = 0;
1663 * Returns: true if time doubled
1664 * false if max time expired
1666 bool double_dev_wait_time(DEVICE *dev)
1668 dev->wait_sec *= 2; /* double wait time */
1669 if (dev->wait_sec > dev->max_wait) { /* but not longer than maxtime */
1670 dev->wait_sec = dev->max_wait;
1673 dev->rem_wait_sec = dev->wait_sec;
1674 if (dev->num_wait >= dev->max_num_wait) {
1680 void set_os_device_parameters(DEVICE *dev)
1682 #ifdef HAVE_LINUX_OS
1684 if (dev->min_block_size == dev->max_block_size &&
1685 dev->min_block_size == 0) { /* variable block mode */
1686 mt_com.mt_op = MTSETBLK;
1687 mt_com.mt_count = 0;
1688 if (ioctl(dev->fd, MTIOCTOP, (char *)&mt_com) < 0) {
1689 clrerror_dev(dev, MTSETBLK);
1691 mt_com.mt_op = MTSETDRVBUFFER;
1692 mt_com.mt_count = MT_ST_CLEARBOOLEANS;
1693 if (!dev_cap(dev, CAP_TWOEOF)) {
1694 mt_com.mt_count |= MT_ST_TWO_FM;
1696 if (dev_cap(dev, CAP_EOM)) {
1697 mt_com.mt_count |= MT_ST_FAST_MTEOM;
1699 if (ioctl(dev->fd, MTIOCTOP, (char *)&mt_com) < 0) {
1700 clrerror_dev(dev, MTSETBLK);
1706 #ifdef HAVE_NETBSD_OS
1708 if (dev->min_block_size == dev->max_block_size &&
1709 dev->min_block_size == 0) { /* variable block mode */
1710 mt_com.mt_op = MTSETBSIZ;
1711 mt_com.mt_count = 0;
1712 if (ioctl(dev->fd, MTIOCTOP, (char *)&mt_com) < 0) {
1713 clrerror_dev(dev, MTSETBSIZ);
1715 /* Get notified at logical end of tape */
1716 mt_com.mt_op = MTEWARN;
1717 mt_com.mt_count = 1;
1718 if (ioctl(dev->fd, MTIOCTOP, (char *)&mt_com) < 0) {
1719 clrerror_dev(dev, MTEWARN);
1725 #if HAVE_FREEBSD_OS || HAVE_OPENBSD_OS
1727 if (dev->min_block_size == dev->max_block_size &&
1728 dev->min_block_size == 0) { /* variable block mode */
1729 mt_com.mt_op = MTSETBSIZ;
1730 mt_com.mt_count = 0;
1731 if (ioctl(dev->fd, MTIOCTOP, (char *)&mt_com) < 0) {
1732 clrerror_dev(dev, MTSETBSIZ);
1740 if (dev->min_block_size == dev->max_block_size &&
1741 dev->min_block_size == 0) { /* variable block mode */
1742 mt_com.mt_op = MTSRSZ;
1743 mt_com.mt_count = 0;
1744 if (ioctl(dev->fd, MTIOCTOP, (char *)&mt_com) < 0) {
1745 clrerror_dev(dev, MTSRSZ);
1752 static bool dev_get_os_pos(DEVICE *dev, struct mtget *mt_stat)
1754 return dev_cap(dev, CAP_MTIOCGET) &&
1755 ioctl(dev->fd, MTIOCGET, (char *)mt_stat) == 0 &&
1756 mt_stat->mt_fileno >= 0;