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_ERROR, 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_ERROR, 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->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 dev->capabilities = device->cap_bits;
156 dev->min_block_size = device->min_block_size;
157 dev->max_block_size = device->max_block_size;
158 dev->max_volume_size = device->max_volume_size;
159 dev->max_file_size = device->max_file_size;
160 dev->volume_capacity = device->volume_capacity;
161 dev->max_rewind_wait = device->max_rewind_wait;
162 dev->max_open_wait = device->max_open_wait;
163 dev->max_open_vols = device->max_open_vols;
164 dev->vol_poll_interval = device->vol_poll_interval;
165 dev->max_spool_size = device->max_spool_size;
166 dev->drive_index = device->drive_index;
167 dev->autoselect = device->autoselect;
168 if (tape) { /* No parts on tapes */
169 dev->max_part_size = 0;
172 dev->max_part_size = device->max_part_size;
175 if (dev->vol_poll_interval && dev->vol_poll_interval < 60) {
176 dev->vol_poll_interval = 60;
178 dev->device = device;
181 dev->state |= ST_TAPE;
183 dev->state |= ST_FIFO;
184 dev->capabilities |= CAP_STREAM; /* set stream device */
186 dev->state |= ST_FILE;
189 /* If the device requires mount :
190 * - Check that the mount point is available
191 * - Check that (un)mount commands are defined
193 if (dev->is_file() && device->cap_bits & CAP_REQMOUNT) {
194 if (stat(device->mount_point, &statp) < 0) {
196 dev->dev_errno = errno;
197 Jmsg2(jcr, M_ERROR, 0, _("Unable to stat mount point %s: ERR=%s\n"),
198 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"));
207 dev->state |= ST_DVD;
210 if (dev->max_block_size > 1000000) {
211 Jmsg3(jcr, M_ERROR, 0, _("Block size %u on device %s is too large, using default %u\n"),
212 dev->max_block_size, dev->dev_name, DEFAULT_BLOCK_SIZE);
213 dev->max_block_size = 0;
215 if (dev->max_block_size % TAPE_BSIZE != 0) {
216 Jmsg2(jcr, M_WARNING, 0, _("Max block size %u not multiple of device %s block size.\n"),
217 dev->max_block_size, dev->dev_name);
220 dev->errmsg = get_pool_memory(PM_EMSG);
223 if ((errstat = pthread_mutex_init(&dev->mutex, NULL)) != 0) {
225 dev->dev_errno = errstat;
226 Mmsg1(dev->errmsg, _("Unable to init mutex: ERR=%s\n"), be.strerror(errstat));
227 Jmsg0(jcr, M_ERROR_TERM, 0, dev->errmsg);
229 if ((errstat = pthread_cond_init(&dev->wait, NULL)) != 0) {
231 dev->dev_errno = errstat;
232 Mmsg1(dev->errmsg, _("Unable to init cond variable: ERR=%s\n"), be.strerror(errstat));
233 Jmsg0(jcr, M_ERROR_TERM, 0, dev->errmsg);
235 if ((errstat = pthread_cond_init(&dev->wait_next_vol, NULL)) != 0) {
237 dev->dev_errno = errstat;
238 Mmsg1(dev->errmsg, _("Unable to init cond variable: ERR=%s\n"), be.strerror(errstat));
239 Jmsg0(jcr, M_ERROR_TERM, 0, dev->errmsg);
241 if ((errstat = pthread_mutex_init(&dev->spool_mutex, NULL)) != 0) {
243 dev->dev_errno = errstat;
244 Mmsg1(dev->errmsg, _("Unable to init mutex: ERR=%s\n"), be.strerror(errstat));
245 Jmsg0(jcr, M_ERROR_TERM, 0, dev->errmsg);
247 if ((errstat = rwl_init(&dev->lock)) != 0) {
249 dev->dev_errno = errstat;
250 Mmsg1(dev->errmsg, _("Unable to init mutex: ERR=%s\n"), be.strerror(errstat));
251 Jmsg0(jcr, M_ERROR_TERM, 0, dev->errmsg);
255 dev->attached_dcrs = New(dlist(dcr, &dcr->dev_link));
256 Dmsg2(29, "init_dev: tape=%d dev_name=%s\n", dev_is_tape(dev), dev->dev_name);
262 * Open the device with the operating system and
263 * initialize buffer pointers.
265 * Returns: -1 on error
268 * Note, for a tape, the VolName is the name we give to the
269 * volume (not really used here), but for a file, the
270 * VolName represents the name of the file to be created/opened.
271 * In the case of a file, the full name is the device name
272 * (archive_name) with the VolName concatenated.
275 open_dev(DEVICE *dev, char *VolName, int mode)
277 if (dev->is_open()) {
279 * *****FIXME***** how to handle two threads wanting
280 * different volumes mounted???? E.g. one is waiting
281 * for the next volume to be mounted, and a new job
282 * starts and snatches up the device.
284 if (VolName && strcmp(dev->VolCatInfo.VolCatName, VolName) != 0) {
288 Mmsg2(&dev->errmsg, _("WARNING!!!! device %s opened %d times!!!\n"),
289 dev->dev_name, dev->use_count);
290 Emsg1(M_WARNING, 0, "%s", dev->errmsg);
294 bstrncpy(dev->VolCatInfo.VolCatName, VolName, sizeof(dev->VolCatInfo.VolCatName));
297 Dmsg3(29, "open_dev: tape=%d dev_name=%s vol=%s\n", dev_is_tape(dev),
298 dev->dev_name, dev->VolCatInfo.VolCatName);
299 dev->state &= ~(ST_LABEL|ST_APPEND|ST_READ|ST_EOT|ST_WEOT|ST_EOF);
300 dev->label_type = B_BACULA_LABEL;
301 if (dev->is_tape() || dev->is_fifo()) {
304 Dmsg0(29, "open_dev: device is tape\n");
305 if (mode == OPEN_READ_WRITE) {
306 dev->mode = O_RDWR | O_BINARY;
307 } else if (mode == OPEN_READ_ONLY) {
308 dev->mode = O_RDONLY | O_BINARY;
309 } else if (mode == OPEN_WRITE_ONLY) {
310 dev->mode = O_WRONLY | O_BINARY;
312 Emsg0(M_ABORT, 0, _("Illegal mode given to open_dev.\n"));
314 timeout = dev->max_open_wait;
316 if (dev->is_fifo() && timeout) {
318 dev->tid = start_thread_timer(pthread_self(), timeout);
320 /* If busy retry each second for max_open_wait seconds */
321 while ((dev->fd = open(dev->dev_name, dev->mode, MODE_RW)) < 0) {
323 if (errno == EINTR || errno == EAGAIN) {
326 if (errno == EBUSY && timeout-- > 0) {
327 Dmsg2(100, "Device %s busy. ERR=%s\n", dev->dev_name, be.strerror());
331 dev->dev_errno = errno;
332 Mmsg2(&dev->errmsg, _("stored: unable to open device %s: ERR=%s\n"),
333 dev->dev_name, be.strerror());
334 /* Stop any open timer we set */
336 stop_thread_timer(dev->tid);
339 Emsg0(M_FATAL, 0, dev->errmsg);
344 dev->state |= ST_OPENED;
346 update_pos_dev(dev); /* update position */
347 set_os_device_parameters(dev); /* do system dependent stuff */
349 /* Stop any open() timer we started */
351 stop_thread_timer(dev->tid);
354 Dmsg1(29, "open_dev: tape %d opened\n", dev->fd);
356 POOL_MEM archive_name(PM_FNAME);
357 struct stat filestat;
359 * Handle opening of File Archive (not a tape)
361 if (dev->part == 0) {
366 /* if num_parts has not been set, but VolCatInfo is available, copy
367 * it from the VolCatInfo.VolCatParts */
368 if (dev->num_parts < dev->VolCatInfo.VolCatParts) {
369 dev->num_parts = dev->VolCatInfo.VolCatParts;
372 if (VolName == NULL || *VolName == 0) {
373 Mmsg(dev->errmsg, _("Could not open file device %s. No Volume name given.\n"),
377 get_filename(dev, VolName, archive_name);
379 if (mount_dev(dev, 1) < 0) {
380 Mmsg(dev->errmsg, _("Could not mount archive device %s.\n"),
382 Emsg0(M_FATAL, 0, dev->errmsg);
387 Dmsg2(29, "open_dev: device is disk %s (mode:%d)\n", archive_name.c_str(), mode);
388 dev->openmode = mode;
391 * If we are not trying to access the last part, set mode to
392 * OPEN_READ_ONLY as writing would be an error.
394 if (dev->part < dev->num_parts) {
395 mode = OPEN_READ_ONLY;
398 if (mode == OPEN_READ_WRITE) {
399 dev->mode = O_CREAT | O_RDWR | O_BINARY;
400 } else if (mode == OPEN_READ_ONLY) {
401 dev->mode = O_RDONLY | O_BINARY;
402 } else if (mode == OPEN_WRITE_ONLY) {
403 dev->mode = O_WRONLY | O_BINARY;
405 Emsg0(M_ABORT, 0, _("Illegal mode given to open_dev.\n"));
407 /* If creating file, give 0640 permissions */
408 if ((dev->fd = open(archive_name.c_str(), dev->mode, 0640)) < 0) {
410 dev->dev_errno = errno;
411 Mmsg2(&dev->errmsg, _("Could not open: %s, ERR=%s\n"), archive_name.c_str(), be.strerror());
412 Emsg0(M_FATAL, 0, dev->errmsg);
415 dev->state |= ST_OPENED;
417 update_pos_dev(dev); /* update position */
418 if (fstat(dev->fd, &filestat) < 0) {
420 dev->dev_errno = errno;
421 Mmsg2(&dev->errmsg, _("Could not fstat: %s, ERR=%s\n"), archive_name.c_str(), be.strerror());
422 Emsg0(M_FATAL, 0, dev->errmsg);
424 dev->part_size = filestat.st_size;
427 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);
428 if (dev->is_dvd() && (dev->mode != OPEN_READ_ONLY) &&
429 (dev->free_space_errno == 0 || dev->num_parts == dev->part)) {
430 update_free_space_dev(dev);
438 bool _rewind_dev(char *file, int line, DEVICE *dev)
440 Dmsg2(100, "rewind_dev called from %s:%d\n", file, line);
441 return rewind_dev(dev);
447 * Returns: true on success
450 bool rewind_dev(DEVICE *dev)
455 Dmsg1(29, "rewind_dev %s\n", dev->dev_name);
457 dev->dev_errno = EBADF;
458 Mmsg1(&dev->errmsg, _("Bad call to rewind_dev. Device %s not open\n"),
460 Emsg0(M_ABORT, 0, dev->errmsg);
463 dev->state &= ~(ST_EOT|ST_EOF|ST_WEOT); /* remove EOF/EOT flags */
464 dev->block_num = dev->file = 0;
467 if (dev->is_tape()) {
468 mt_com.mt_op = MTREW;
470 /* If we get an I/O error on rewind, it is probably because
471 * the drive is actually busy. We loop for (about 5 minutes)
472 * retrying every 5 seconds.
474 for (i=dev->max_rewind_wait; ; i -= 5) {
475 if (ioctl(dev->fd, MTIOCTOP, (char *)&mt_com) < 0) {
477 clrerror_dev(dev, MTREW);
478 if (i == dev->max_rewind_wait) {
479 Dmsg1(200, "Rewind error, %s. retrying ...\n", be.strerror());
481 if (dev->dev_errno == EIO && i > 0) {
482 Dmsg0(200, "Sleeping 5 seconds.\n");
486 Mmsg2(&dev->errmsg, _("Rewind error on %s. ERR=%s.\n"),
487 dev->dev_name, be.strerror());
492 } else if (dev->is_file()) {
493 if (lseek_dev(dev, (off_t)0, SEEK_SET) < 0) {
495 dev->dev_errno = errno;
496 Mmsg2(&dev->errmsg, _("lseek_dev error on %s. ERR=%s.\n"),
497 dev->dev_name, be.strerror());
505 * Called to indicate that we have just read an
506 * EOF from the device.
508 void DEVICE::set_eof()
518 * Called to indicate we are now at the end of the tape, and
519 * writing is not possible.
521 void DEVICE::set_eot()
523 state |= (ST_EOF|ST_EOT|ST_WEOT);
524 state &= ~ST_APPEND; /* make tape read-only */
528 * Position device to end of medium (end of data)
529 * Returns: 1 on succes
536 struct mtget mt_stat;
540 Dmsg0(29, "eod_dev\n");
544 dev->state &= ~(ST_EOF); /* remove EOF flags */
545 dev->block_num = dev->file = 0;
548 if (dev->state & (ST_FIFO | ST_PROG)) {
551 if (!dev->is_tape()) {
552 pos = lseek_dev(dev, (off_t)0, SEEK_END);
553 // Dmsg1(100, "====== Seek to %lld\n", pos);
556 dev->state |= ST_EOT;
559 dev->dev_errno = errno;
561 Mmsg2(&dev->errmsg, _("lseek_dev error on %s. ERR=%s.\n"),
562 dev->dev_name, be.strerror());
566 if (dev_cap(dev, CAP_FASTFSF) && !dev_cap(dev, CAP_EOM)) {
567 Dmsg0(100,"Using FAST FSF for EOM\n");
568 /* If unknown position, rewind */
569 if (!dev_get_os_pos(dev, &mt_stat)) {
570 if (!rewind_dev(dev)) {
574 mt_com.mt_op = MTFSF;
576 * ***FIXME*** fix code to handle case that INT16_MAX is
579 mt_com.mt_count = INT16_MAX; /* use big positive number */
580 if (mt_com.mt_count < 0) {
581 mt_com.mt_count = INT16_MAX; /* brain damaged system */
585 if (dev_cap(dev, CAP_MTIOCGET) && (dev_cap(dev, CAP_FASTFSF) || dev_cap(dev, CAP_EOM))) {
586 if (dev_cap(dev, CAP_EOM)) {
587 Dmsg0(100,"Using EOM for EOM\n");
588 mt_com.mt_op = MTEOM;
592 if ((stat=ioctl(dev->fd, MTIOCTOP, (char *)&mt_com)) < 0) {
594 clrerror_dev(dev, mt_com.mt_op);
595 Dmsg1(50, "ioctl error: %s\n", be.strerror());
597 Mmsg2(&dev->errmsg, _("ioctl MTEOM error on %s. ERR=%s.\n"),
598 dev->dev_name, be.strerror());
602 if (!dev_get_os_pos(dev, &mt_stat)) {
604 clrerror_dev(dev, -1);
605 Mmsg2(&dev->errmsg, _("ioctl MTIOCGET error on %s. ERR=%s.\n"),
606 dev->dev_name, be.strerror());
609 Dmsg2(100, "EOD file=%d block=%d\n", mt_stat.mt_fileno, mt_stat.mt_blkno);
611 dev->file = mt_stat.mt_fileno;
618 * Rewind then use FSF until EOT reached
620 if (!rewind_dev(dev)) {
624 * Move file by file to the end of the tape
627 for (file_num=dev->file; !dev->at_eot(); file_num++) {
628 Dmsg0(200, "eod_dev: doing fsf 1\n");
629 if (!fsf_dev(dev, 1)) {
630 Dmsg0(200, "fsf_dev error.\n");
634 * Avoid infinite loop. ***FIXME*** possibly add code
635 * to set EOD or to turn off CAP_FASTFSF if on.
637 if (file_num == (int)dev->file) {
638 struct mtget mt_stat;
639 Dmsg1(100, "fsf_dev did not advance from file %d\n", file_num);
640 if (dev_get_os_pos(dev, &mt_stat)) {
641 Dmsg2(100, "Adjust file from %d to %d\n", dev->file , mt_stat.mt_fileno);
643 dev->file = mt_stat.mt_fileno;
646 break; /* we are not progressing, bail out */
651 * Some drivers leave us after second EOF when doing
652 * MTEOM, so we must backup so that appending overwrites
655 if (dev_cap(dev, CAP_BSFATEOM)) {
656 struct mtget mt_stat;
657 /* Backup over EOF */
658 stat = bsf_dev(dev, 1);
659 /* If BSF worked and fileno is known (not -1), set file */
660 if (dev_get_os_pos(dev, &mt_stat)) {
661 Dmsg2(100, "BSFATEOF adjust file from %d to %d\n", dev->file , mt_stat.mt_fileno);
662 dev->file = mt_stat.mt_fileno;
664 dev->file++; /* wing it -- not correct on all OSes */
667 update_pos_dev(dev); /* update position */
670 Dmsg1(200, "EOD dev->file=%d\n", dev->file);
675 * Set the position of the device -- only for files
676 * For other devices, there is no generic way to do it.
677 * Returns: true on succes
680 bool update_pos_dev(DEVICE *dev)
686 dev->dev_errno = EBADF;
687 Mmsg0(&dev->errmsg, _("Bad device call. Archive not open\n"));
688 Emsg0(M_FATAL, 0, dev->errmsg);
692 /* Find out where we are */
693 if (dev->is_file()) {
696 pos = lseek_dev(dev, (off_t)0, SEEK_CUR);
699 dev->dev_errno = errno;
700 Pmsg1(000, "Seek error: ERR=%s\n", be.strerror());
701 Mmsg2(&dev->errmsg, _("lseek_dev error on %s. ERR=%s.\n"),
702 dev->dev_name, be.strerror());
705 dev->file_addr = pos;
713 * Return the status of the device. This was meant
714 * to be a generic routine. Unfortunately, it doesn't
715 * seem possible (at least I do not know how to do it
716 * currently), which means that for the moment, this
717 * routine has very little value.
721 uint32_t status_dev(DEVICE *dev)
723 struct mtget mt_stat;
726 if (dev->state & (ST_EOT | ST_WEOT)) {
730 if (dev->state & ST_EOF) {
734 if (dev->is_tape()) {
736 Dmsg0(-20," Bacula status:");
737 Dmsg2(-20," file=%d block=%d\n", dev->file, dev->block_num);
738 if (ioctl(dev->fd, MTIOCGET, (char *)&mt_stat) < 0) {
740 dev->dev_errno = errno;
741 Mmsg2(&dev->errmsg, _("ioctl MTIOCGET error on %s. ERR=%s.\n"),
742 dev->dev_name, be.strerror());
745 Dmsg0(-20, " Device status:");
747 #if defined(HAVE_LINUX_OS)
748 if (GMT_EOF(mt_stat.mt_gstat)) {
752 if (GMT_BOT(mt_stat.mt_gstat)) {
756 if (GMT_EOT(mt_stat.mt_gstat)) {
760 if (GMT_SM(mt_stat.mt_gstat)) {
764 if (GMT_EOD(mt_stat.mt_gstat)) {
768 if (GMT_WR_PROT(mt_stat.mt_gstat)) {
770 Dmsg0(-20, " WR_PROT");
772 if (GMT_ONLINE(mt_stat.mt_gstat)) {
774 Dmsg0(-20, " ONLINE");
776 if (GMT_DR_OPEN(mt_stat.mt_gstat)) {
778 Dmsg0(-20, " DR_OPEN");
780 if (GMT_IM_REP_EN(mt_stat.mt_gstat)) {
781 stat |= BMT_IM_REP_EN;
782 Dmsg0(-20, " IM_REP_EN");
784 #endif /* !SunOS && !OSF */
785 if (dev_cap(dev, CAP_MTIOCGET)) {
786 Dmsg2(-20, " file=%d block=%d\n", mt_stat.mt_fileno, mt_stat.mt_blkno);
788 Dmsg2(-20, " file=%d block=%d\n", -1, -1);
791 stat |= BMT_ONLINE | BMT_BOT;
798 * Load medium in device
799 * Returns: true on success
802 bool load_dev(DEVICE *dev)
809 dev->dev_errno = EBADF;
810 Mmsg0(&dev->errmsg, _("Bad call to load_dev. Archive not open\n"));
811 Emsg0(M_FATAL, 0, dev->errmsg);
814 if (!(dev->is_tape())) {
818 Dmsg0(200, "stored: MTLOAD command not available\n");
820 dev->dev_errno = ENOTTY; /* function not available */
821 Mmsg2(&dev->errmsg, _("ioctl MTLOAD error on %s. ERR=%s.\n"),
822 dev->dev_name, be.strerror());
826 dev->block_num = dev->file = 0;
829 mt_com.mt_op = MTLOAD;
831 if (ioctl(dev->fd, MTIOCTOP, (char *)&mt_com) < 0) {
833 dev->dev_errno = errno;
834 Mmsg2(&dev->errmsg, _("ioctl MTLOAD error on %s. ERR=%s.\n"),
835 dev->dev_name, be.strerror());
843 * Rewind device and put it offline
844 * Returns: true on success
847 bool offline_dev(DEVICE *dev)
852 dev->dev_errno = EBADF;
853 Mmsg0(&dev->errmsg, _("Bad call to offline_dev. Archive not open\n"));
854 Emsg0(M_FATAL, 0, dev->errmsg);
857 if (!(dev->is_tape())) {
861 dev->state &= ~(ST_APPEND|ST_READ|ST_EOT|ST_EOF|ST_WEOT); /* remove EOF/EOT flags */
862 dev->block_num = dev->file = 0;
867 mt_com.mt_op = MTUNLOCK;
869 ioctl(dev->fd, MTIOCTOP, (char *)&mt_com);
871 mt_com.mt_op = MTOFFL;
873 if (ioctl(dev->fd, MTIOCTOP, (char *)&mt_com) < 0) {
875 dev->dev_errno = errno;
876 Mmsg2(&dev->errmsg, _("ioctl MTOFFL error on %s. ERR=%s.\n"),
877 dev->dev_name, be.strerror());
880 Dmsg1(100, "Offlined device %s\n", dev->dev_name);
884 bool offline_or_rewind_dev(DEVICE *dev)
889 if (dev_cap(dev, CAP_OFFLINEUNMOUNT)) {
890 return offline_dev(dev);
893 * Note, this rewind probably should not be here (it wasn't
894 * in prior versions of Bacula), but on FreeBSD, this is
895 * needed in the case the tape was "frozen" due to an error
896 * such as backspacing after writing and EOF. If it is not
897 * done, all future references to the drive get and I/O error.
899 clrerror_dev(dev, MTREW);
900 return rewind_dev(dev);
905 * Foward space a file
906 * Returns: true on success
910 fsf_dev(DEVICE *dev, int num)
912 struct mtget mt_stat;
917 dev->dev_errno = EBADF;
918 Mmsg0(&dev->errmsg, _("Bad call to fsf_dev. Archive not open\n"));
919 Emsg0(M_FATAL, 0, dev->errmsg);
923 if (!dev->is_tape()) {
926 if (dev->state & ST_EOT) {
928 Mmsg1(dev->errmsg, _("Device %s at End of Tape.\n"), dev->dev_name);
931 if (dev->state & ST_EOF) {
932 Dmsg0(200, "ST_EOF set on entry to FSF\n");
935 Dmsg0(100, "fsf_dev\n");
938 * If Fast forward space file is set, then we
939 * use MTFSF to forward space and MTIOCGET
940 * to get the file position. We assume that
941 * the SCSI driver will ensure that we do not
942 * forward space past the end of the medium.
944 if (dev_cap(dev, CAP_FSF) && dev_cap(dev, CAP_MTIOCGET) && dev_cap(dev, CAP_FASTFSF)) {
945 mt_com.mt_op = MTFSF;
946 mt_com.mt_count = num;
947 stat = ioctl(dev->fd, MTIOCTOP, (char *)&mt_com);
948 if (stat < 0 || !dev_get_os_pos(dev, &mt_stat)) {
950 dev->state |= ST_EOT;
951 Dmsg0(200, "Set ST_EOT\n");
952 clrerror_dev(dev, MTFSF);
953 Mmsg2(dev->errmsg, _("ioctl MTFSF error on %s. ERR=%s.\n"),
954 dev->dev_name, be.strerror());
955 Dmsg1(200, "%s", dev->errmsg);
958 Dmsg2(200, "fsf file=%d block=%d\n", mt_stat.mt_fileno, mt_stat.mt_blkno);
960 dev->file = mt_stat.mt_fileno;
964 * Here if CAP_FSF is set, and virtually all drives
965 * these days support it, we read a record, then forward
966 * space one file. Using this procedure, which is slow,
967 * is the only way we can be sure that we don't read
968 * two consecutive EOF marks, which means End of Data.
970 } else if (dev_cap(dev, CAP_FSF)) {
973 Dmsg0(200, "FSF has cap_fsf\n");
974 if (dev->max_block_size == 0) {
975 rbuf_len = DEFAULT_BLOCK_SIZE;
977 rbuf_len = dev->max_block_size;
979 rbuf = get_memory(rbuf_len);
980 mt_com.mt_op = MTFSF;
982 while (num-- && !(dev->state & ST_EOT)) {
983 Dmsg0(100, "Doing read before fsf\n");
984 if ((stat = read(dev->fd, (char *)rbuf, rbuf_len)) < 0) {
985 if (errno == ENOMEM) { /* tape record exceeds buf len */
986 stat = rbuf_len; /* This is OK */
989 dev->state |= ST_EOT;
990 clrerror_dev(dev, -1);
991 Dmsg2(100, "Set ST_EOT read errno=%d. ERR=%s\n", dev->dev_errno,
993 Mmsg2(dev->errmsg, _("read error on %s. ERR=%s.\n"),
994 dev->dev_name, be.strerror());
995 Dmsg1(100, "%s", dev->errmsg);
999 if (stat == 0) { /* EOF */
1000 update_pos_dev(dev);
1001 Dmsg1(100, "End of File mark from read. File=%d\n", dev->file+1);
1002 /* Two reads of zero means end of tape */
1003 if (dev->state & ST_EOF) {
1004 dev->state |= ST_EOT;
1005 Dmsg0(100, "Set ST_EOT\n");
1011 } else { /* Got data */
1012 dev->state &= ~(ST_EOF|ST_EOT);
1015 Dmsg0(100, "Doing MTFSF\n");
1016 stat = ioctl(dev->fd, MTIOCTOP, (char *)&mt_com);
1017 if (stat < 0) { /* error => EOT */
1019 dev->state |= ST_EOT;
1020 Dmsg0(100, "Set ST_EOT\n");
1021 clrerror_dev(dev, MTFSF);
1022 Mmsg2(&dev->errmsg, _("ioctl MTFSF error on %s. ERR=%s.\n"),
1023 dev->dev_name, be.strerror());
1024 Dmsg0(100, "Got < 0 for MTFSF\n");
1025 Dmsg1(100, "%s", dev->errmsg);
1033 * No FSF, so use FSR to simulate it
1036 Dmsg0(200, "Doing FSR for FSF\n");
1037 while (num-- && !(dev->state & ST_EOT)) {
1038 fsr_dev(dev, INT32_MAX); /* returns -1 on EOF or EOT */
1040 if (dev->state & ST_EOT) {
1042 Mmsg1(dev->errmsg, _("Device %s at End of Tape.\n"), dev->dev_name);
1048 update_pos_dev(dev);
1049 Dmsg1(200, "Return %d from FSF\n", stat);
1050 if (dev->state & ST_EOF)
1051 Dmsg0(200, "ST_EOF set on exit FSF\n");
1052 if (dev->state & ST_EOT)
1053 Dmsg0(200, "ST_EOT set on exit FSF\n");
1054 Dmsg1(200, "Return from FSF file=%d\n", dev->file);
1059 * Backward space a file
1060 * Returns: false on failure
1064 bsf_dev(DEVICE *dev, int num)
1070 dev->dev_errno = EBADF;
1071 Mmsg0(dev->errmsg, _("Bad call to bsf_dev. Archive device not open\n"));
1072 Emsg0(M_FATAL, 0, dev->errmsg);
1076 if (!dev->is_tape()) {
1077 Mmsg1(dev->errmsg, _("Device %s cannot BSF because it is not a tape.\n"),
1081 Dmsg0(29, "bsf_dev\n");
1082 dev->state &= ~(ST_EOT|ST_EOF);
1086 mt_com.mt_op = MTBSF;
1087 mt_com.mt_count = num;
1088 stat = ioctl(dev->fd, MTIOCTOP, (char *)&mt_com);
1091 clrerror_dev(dev, MTBSF);
1092 Mmsg2(dev->errmsg, _("ioctl MTBSF error on %s. ERR=%s.\n"),
1093 dev->dev_name, be.strerror());
1095 update_pos_dev(dev);
1101 * Foward space a record
1102 * Returns: false on failure
1106 fsr_dev(DEVICE *dev, int num)
1112 dev->dev_errno = EBADF;
1113 Mmsg0(dev->errmsg, _("Bad call to fsr_dev. Archive not open\n"));
1114 Emsg0(M_FATAL, 0, dev->errmsg);
1118 if (!dev->is_tape()) {
1121 if (!dev_cap(dev, CAP_FSR)) {
1122 Mmsg1(dev->errmsg, _("ioctl MTFSR not permitted on %s.\n"), dev->dev_name);
1126 Dmsg1(29, "fsr_dev %d\n", num);
1127 mt_com.mt_op = MTFSR;
1128 mt_com.mt_count = num;
1129 stat = ioctl(dev->fd, MTIOCTOP, (char *)&mt_com);
1131 dev->state &= ~ST_EOF;
1132 dev->block_num += num;
1135 struct mtget mt_stat;
1136 clrerror_dev(dev, MTFSR);
1137 Dmsg1(100, "FSF fail: ERR=%s\n", be.strerror());
1138 if (dev_get_os_pos(dev, &mt_stat)) {
1139 Dmsg4(100, "Adjust from %d:%d to %d:%d\n", dev->file,
1140 dev->block_num, mt_stat.mt_fileno, mt_stat.mt_blkno);
1141 dev->file = mt_stat.mt_fileno;
1142 dev->block_num = mt_stat.mt_blkno;
1144 if (dev->state & ST_EOF) {
1145 dev->state |= ST_EOT;
1150 Mmsg2(dev->errmsg, _("ioctl MTFSR error on %s. ERR=%s.\n"),
1151 dev->dev_name, be.strerror());
1153 update_pos_dev(dev);
1158 * Backward space a record
1159 * Returns: false on failure
1163 bsr_dev(DEVICE *dev, int num)
1169 dev->dev_errno = EBADF;
1170 Mmsg0(dev->errmsg, _("Bad call to bsr_dev. Archive not open\n"));
1171 Emsg0(M_FATAL, 0, dev->errmsg);
1175 if (!dev->is_tape()) {
1179 if (!dev_cap(dev, CAP_BSR)) {
1180 Mmsg1(dev->errmsg, _("ioctl MTBSR not permitted on %s.\n"), dev->dev_name);
1184 Dmsg0(29, "bsr_dev\n");
1185 dev->block_num -= num;
1186 dev->state &= ~(ST_EOF|ST_EOT|ST_EOF);
1187 mt_com.mt_op = MTBSR;
1188 mt_com.mt_count = num;
1189 stat = ioctl(dev->fd, MTIOCTOP, (char *)&mt_com);
1192 clrerror_dev(dev, MTBSR);
1193 Mmsg2(dev->errmsg, _("ioctl MTBSR error on %s. ERR=%s.\n"),
1194 dev->dev_name, be.strerror());
1196 update_pos_dev(dev);
1201 * Reposition the device to file, block
1202 * Returns: false on failure
1206 reposition_dev(DEVICE *dev, uint32_t file, uint32_t block)
1209 dev->dev_errno = EBADF;
1210 Mmsg0(dev->errmsg, _("Bad call to reposition_dev. Archive not open\n"));
1211 Emsg0(M_FATAL, 0, dev->errmsg);
1215 if (!dev->is_tape()) {
1216 off_t pos = (((off_t)file)<<32) + block;
1217 Dmsg1(100, "===== lseek_dev to %d\n", (int)pos);
1218 if (lseek_dev(dev, pos, SEEK_SET) == (off_t)-1) {
1220 dev->dev_errno = errno;
1221 Mmsg2(dev->errmsg, _("lseek_dev error on %s. ERR=%s.\n"),
1222 dev->dev_name, be.strerror());
1226 dev->block_num = block;
1227 dev->file_addr = pos;
1230 Dmsg4(100, "reposition_dev from %u:%u to %u:%u\n",
1231 dev->file, dev->block_num, file, block);
1232 if (file < dev->file) {
1233 Dmsg0(100, "Rewind_dev\n");
1234 if (!rewind_dev(dev)) {
1238 if (file > dev->file) {
1239 Dmsg1(100, "fsf %d\n", file-dev->file);
1240 if (!fsf_dev(dev, file-dev->file)) {
1241 Dmsg1(100, "fsf failed! ERR=%s\n", strerror_dev(dev));
1244 Dmsg2(100, "wanted_file=%d at_file=%d\n", file, dev->file);
1246 if (block < dev->block_num) {
1247 Dmsg2(100, "wanted_blk=%d at_blk=%d\n", block, dev->block_num);
1248 Dmsg0(100, "bsf_dev 1\n");
1250 Dmsg0(100, "fsf_dev 1\n");
1252 Dmsg2(100, "wanted_blk=%d at_blk=%d\n", block, dev->block_num);
1254 if (dev_cap(dev, CAP_POSITIONBLOCKS) && block > dev->block_num) {
1255 /* Ignore errors as Bacula can read to the correct block */
1256 Dmsg1(100, "fsr %d\n", block-dev->block_num);
1257 return fsr_dev(dev, block-dev->block_num);
1265 * Write an end of file on the device
1266 * Returns: 0 on success
1267 * non-zero on failure
1270 weof_dev(DEVICE *dev, int num)
1274 Dmsg0(29, "weof_dev\n");
1277 dev->dev_errno = EBADF;
1278 Mmsg0(dev->errmsg, _("Bad call to weof_dev. Archive drive not open\n"));
1279 Emsg0(M_FATAL, 0, dev->errmsg);
1284 if (!dev->is_tape()) {
1287 if (!dev->can_append()) {
1288 Mmsg0(dev->errmsg, _("Attempt to WEOF on non-appendable Volume\n"));
1289 Emsg0(M_FATAL, 0, dev->errmsg);
1293 dev->state &= ~(ST_EOT | ST_EOF); /* remove EOF/EOT flags */
1294 mt_com.mt_op = MTWEOF;
1295 mt_com.mt_count = num;
1296 stat = ioctl(dev->fd, MTIOCTOP, (char *)&mt_com);
1303 clrerror_dev(dev, MTWEOF);
1305 Mmsg2(dev->errmsg, _("ioctl MTWEOF error on %s. ERR=%s.\n"),
1306 dev->dev_name, be.strerror());
1313 * Return string message with last error in English
1314 * Be careful not to call this routine from within dev.c
1315 * while editing an Mmsg() or you will end up in a recursive
1316 * loop creating a Segmentation Violation.
1319 strerror_dev(DEVICE *dev)
1326 * If implemented in system, clear the tape
1330 clrerror_dev(DEVICE *dev, int func)
1332 const char *msg = NULL;
1333 struct mtget mt_stat;
1336 dev->dev_errno = errno; /* save errno */
1338 dev->VolCatInfo.VolCatErrors++;
1341 if (!dev->is_tape()) {
1344 if (errno == ENOTTY || errno == ENOSYS) { /* Function not implemented */
1347 Emsg0(M_ABORT, 0, "Got ENOTTY on read/write!\n");
1351 dev->capabilities &= ~CAP_EOF; /* turn off feature */
1356 dev->capabilities &= ~CAP_EOM; /* turn off feature */
1361 dev->capabilities &= ~CAP_FSF; /* turn off feature */
1365 dev->capabilities &= ~CAP_BSF; /* turn off feature */
1369 dev->capabilities &= ~CAP_FSR; /* turn off feature */
1373 dev->capabilities &= ~CAP_BSR; /* turn off feature */
1394 bsnprintf(buf, sizeof(buf), "unknown func code %d", func);
1399 dev->dev_errno = ENOSYS;
1400 Mmsg1(dev->errmsg, _("I/O function \"%s\" not supported on this device.\n"), msg);
1401 Emsg0(M_ERROR, 0, dev->errmsg);
1404 /* On some systems such as NetBSD, this clears all errors */
1405 ioctl(dev->fd, MTIOCGET, (char *)&mt_stat);
1407 /* Found on Linux */
1411 mt_com.mt_op = MTIOCLRERR;
1412 mt_com.mt_count = 1;
1413 /* Clear any error condition on the tape */
1414 ioctl(dev->fd, MTIOCTOP, (char *)&mt_com);
1415 Dmsg0(200, "Did MTIOCLRERR\n");
1419 /* Typically on FreeBSD */
1422 /* Read and clear SCSI error status */
1423 union mterrstat mt_errstat;
1424 Dmsg2(200, "Doing MTIOCERRSTAT errno=%d ERR=%s\n", dev->dev_errno,
1425 strerror(dev->dev_errno));
1426 ioctl(dev->fd, MTIOCERRSTAT, (char *)&mt_errstat);
1430 /* Clear Subsystem Exception OSF1 */
1434 mt_com.mt_op = MTCSE;
1435 mt_com.mt_count = 1;
1436 /* Clear any error condition on the tape */
1437 ioctl(dev->fd, MTIOCTOP, (char *)&mt_com);
1438 Dmsg0(200, "Did MTCSE\n");
1444 * Flush buffer contents
1447 int flush_dev(DEVICE *dev)
1452 static void do_close(DEVICE *dev)
1455 Dmsg1(29, "really close_dev %s\n", dev->dev_name);
1460 if (unmount_dev(dev, 1) < 0) {
1461 Dmsg1(0, "Cannot unmount device %s.\n", dev->dev_name);
1464 /* Remove the last part file if it is empty */
1465 if (dev->can_append() && (dev->num_parts > 0)) {
1467 POOL_MEM archive_name(PM_FNAME);
1468 dev->part = dev->num_parts;
1469 get_filename(dev, dev->VolCatInfo.VolCatName, archive_name);
1470 /* Check that the part file is empty */
1471 if ((stat(archive_name.c_str(), &statp) == 0) && (statp.st_size == 0)) {
1472 unlink(archive_name.c_str());
1476 /* Clean up device packet so it can be reused */
1478 dev->state &= ~(ST_OPENED|ST_LABEL|ST_READ|ST_APPEND|ST_EOT|ST_WEOT|ST_EOF);
1479 dev->label_type = B_BACULA_LABEL;
1480 dev->file = dev->block_num = 0;
1485 dev->part_start = 0;
1486 dev->EndFile = dev->EndBlock = 0;
1487 memset(&dev->VolCatInfo, 0, sizeof(dev->VolCatInfo));
1488 memset(&dev->VolHdr, 0, sizeof(dev->VolHdr));
1490 stop_thread_timer(dev->tid);
1500 close_dev(DEVICE *dev)
1503 Mmsg0(&dev->errmsg, _("Bad call to close_dev. Archive not open\n"));
1504 Emsg0(M_FATAL, 0, dev->errmsg);
1507 /*if (dev->fd >= 0 && dev->use_count == 1) {*/
1508 /* No need to check if dev->fd >= 0: it is checked again
1509 * in do_close, and do_close MUST be called for volumes
1510 * splitted in parts, even if dev->fd == -1. */
1511 if (dev->use_count == 1) {
1513 } else if (dev->use_count > 0) {
1518 ASSERT(dev->use_count >= 0);
1523 * Used when unmounting the device, ignore use_count
1525 void force_close_dev(DEVICE *dev)
1528 Mmsg0(&dev->errmsg, _("Bad call to force_close_dev. Archive not open\n"));
1529 Emsg0(M_FATAL, 0, dev->errmsg);
1532 Dmsg1(29, "Force close_dev %s\n", dev->dev_name);
1536 ASSERT(dev->use_count >= 0);
1540 bool truncate_dev(DEVICE *dev)
1542 Dmsg1(100, "truncate_dev %s\n", dev->dev_name);
1543 if (dev->is_tape()) {
1544 return true; /* we don't really truncate tapes */
1545 /* maybe we should rewind and write and eof ???? */
1548 /* If there is more than one part, open the first one, and then truncate it. */
1549 if (dev->num_parts > 0) {
1551 dev->VolCatInfo.VolCatParts = 0;
1552 if (open_first_part(dev) < 0) {
1554 Mmsg1(&dev->errmsg, "Unable to truncate device, because I'm unable to open the first part. ERR=%s\n", be.strerror());
1558 if (ftruncate(dev->fd, 0) != 0) {
1560 Mmsg1(&dev->errmsg, _("Unable to truncate device. ERR=%s\n"), be.strerror());
1567 dev_is_tape(DEVICE *dev)
1569 return dev->is_tape() ? true : false;
1574 * return 1 if the device is read for write, and 0 otherwise
1575 * This is meant for checking at the end of a job to see
1576 * if we still have a tape (perhaps not if at end of tape
1577 * and the job is canceled).
1580 dev_can_write(DEVICE *dev)
1582 if (dev->is_open() && dev->can_append() &&
1583 dev->is_labeled() && !(dev->state & ST_WEOT)) {
1590 /* Return the resource name for the device */
1591 const char *DEVICE::name() const
1593 return device->hdr.name;
1597 dev_vol_name(DEVICE *dev)
1599 return dev->VolCatInfo.VolCatName;
1602 uint32_t dev_block(DEVICE *dev)
1604 update_pos_dev(dev);
1605 return dev->block_num;
1608 uint32_t dev_file(DEVICE *dev)
1610 update_pos_dev(dev);
1615 * Free memory allocated for the device
1618 term_dev(DEVICE *dev)
1621 dev->dev_errno = EBADF;
1622 Mmsg0(&dev->errmsg, _("Bad call to term_dev. Archive not open\n"));
1623 Emsg0(M_FATAL, 0, dev->errmsg);
1627 Dmsg0(29, "term_dev\n");
1628 if (dev->dev_name) {
1629 free_memory(dev->dev_name);
1630 dev->dev_name = NULL;
1632 if (dev->prt_name) {
1633 free_memory(dev->prt_name);
1634 dev->prt_name = NULL;
1637 free_pool_memory(dev->errmsg);
1640 pthread_mutex_destroy(&dev->mutex);
1641 pthread_cond_destroy(&dev->wait);
1642 pthread_cond_destroy(&dev->wait_next_vol);
1643 pthread_mutex_destroy(&dev->spool_mutex);
1644 rwl_destroy(&dev->lock);
1645 if (dev->attached_dcrs) {
1646 delete dev->attached_dcrs;
1647 dev->attached_dcrs = NULL;
1649 if (dev->state & ST_MALLOC) {
1650 free_pool_memory((POOLMEM *)dev);
1655 * This routine initializes the device wait timers
1657 void init_dev_wait_timers(DEVICE *dev)
1659 /* ******FIXME******* put these on config variables */
1660 dev->min_wait = 60 * 60;
1661 dev->max_wait = 24 * 60 * 60;
1662 dev->max_num_wait = 9; /* 5 waits =~ 1 day, then 1 day at a time */
1663 dev->wait_sec = dev->min_wait;
1664 dev->rem_wait_sec = dev->wait_sec;
1667 dev->BadVolName[0] = 0;
1671 * Returns: true if time doubled
1672 * false if max time expired
1674 bool double_dev_wait_time(DEVICE *dev)
1676 dev->wait_sec *= 2; /* double wait time */
1677 if (dev->wait_sec > dev->max_wait) { /* but not longer than maxtime */
1678 dev->wait_sec = dev->max_wait;
1681 dev->rem_wait_sec = dev->wait_sec;
1682 if (dev->num_wait >= dev->max_num_wait) {
1688 void set_os_device_parameters(DEVICE *dev)
1690 #ifdef HAVE_LINUX_OS
1692 if (dev->min_block_size == dev->max_block_size &&
1693 dev->min_block_size == 0) { /* variable block mode */
1694 mt_com.mt_op = MTSETBLK;
1695 mt_com.mt_count = 0;
1696 if (ioctl(dev->fd, MTIOCTOP, (char *)&mt_com) < 0) {
1697 clrerror_dev(dev, MTSETBLK);
1699 mt_com.mt_op = MTSETDRVBUFFER;
1700 mt_com.mt_count = MT_ST_CLEARBOOLEANS;
1701 if (!dev_cap(dev, CAP_TWOEOF)) {
1702 mt_com.mt_count |= MT_ST_TWO_FM;
1704 if (dev_cap(dev, CAP_EOM)) {
1705 mt_com.mt_count |= MT_ST_FAST_MTEOM;
1707 if (ioctl(dev->fd, MTIOCTOP, (char *)&mt_com) < 0) {
1708 clrerror_dev(dev, MTSETBLK);
1714 #ifdef HAVE_NETBSD_OS
1716 if (dev->min_block_size == dev->max_block_size &&
1717 dev->min_block_size == 0) { /* variable block mode */
1718 mt_com.mt_op = MTSETBSIZ;
1719 mt_com.mt_count = 0;
1720 if (ioctl(dev->fd, MTIOCTOP, (char *)&mt_com) < 0) {
1721 clrerror_dev(dev, MTSETBSIZ);
1723 /* Get notified at logical end of tape */
1724 mt_com.mt_op = MTEWARN;
1725 mt_com.mt_count = 1;
1726 if (ioctl(dev->fd, MTIOCTOP, (char *)&mt_com) < 0) {
1727 clrerror_dev(dev, MTEWARN);
1733 #if HAVE_FREEBSD_OS || HAVE_OPENBSD_OS
1735 if (dev->min_block_size == dev->max_block_size &&
1736 dev->min_block_size == 0) { /* variable block mode */
1737 mt_com.mt_op = MTSETBSIZ;
1738 mt_com.mt_count = 0;
1739 if (ioctl(dev->fd, MTIOCTOP, (char *)&mt_com) < 0) {
1740 clrerror_dev(dev, MTSETBSIZ);
1748 if (dev->min_block_size == dev->max_block_size &&
1749 dev->min_block_size == 0) { /* variable block mode */
1750 mt_com.mt_op = MTSRSZ;
1751 mt_com.mt_count = 0;
1752 if (ioctl(dev->fd, MTIOCTOP, (char *)&mt_com) < 0) {
1753 clrerror_dev(dev, MTSRSZ);
1760 static bool dev_get_os_pos(DEVICE *dev, struct mtget *mt_stat)
1762 return dev_cap(dev, CAP_MTIOCGET) &&
1763 ioctl(dev->fd, MTIOCGET, (char *)mt_stat) == 0 &&
1764 mt_stat->mt_fileno >= 0;