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
36 version 2 as ammended with additional clauses defined in the
37 file LICENSE in the main source directory.
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
42 the file LICENSE for additional details.
47 * Handling I/O errors and end of tape conditions are a bit tricky.
48 * This is how it is currently done when writting.
49 * On either an I/O error or end of tape,
50 * we will stop writing on the physical device (no I/O recovery is
51 * attempted at least in this daemon). The state flag will be sent
52 * to include ST_EOT, which is ephimeral, and ST_WEOT, which is
53 * persistent. Lots of routines clear ST_EOT, but ST_WEOT is
54 * cleared only when the problem goes away. Now when ST_WEOT
55 * is set all calls to write_block_to_device() call the fix_up
56 * routine. In addition, all threads are blocked
57 * from writing on the tape by calling lock_dev(), and thread other
58 * than the first thread to hit the EOT will block on a condition
59 * variable. The first thread to hit the EOT will continue to
60 * be able to read and write the tape (he sort of tunnels through
61 * the locking mechanism -- see lock_dev() for details).
63 * Now presumably somewhere higher in the chain of command
64 * (device.c), someone will notice the EOT condition and
65 * get a new tape up, get the tape label read, and mark
66 * the label for rewriting. Then this higher level routine
67 * will write the unwritten buffer to the new volume.
68 * Finally, he will release
69 * any blocked threads by doing a broadcast on the condition
70 * variable. At that point, we should be totally back in
71 * business with no lost data.
82 /* Functions in dvd.c */
83 void get_filename(DEVICE *dev, char *VolName, POOL_MEM& archive_name);
84 void update_free_space_dev(DEVICE* dev);
87 /* Forward referenced functions */
88 void set_os_device_parameters(DEVICE *dev);
89 static bool dev_get_os_pos(DEVICE *dev, struct mtget *mt_stat);
90 static void open_tape_device(DEVICE *dev, int mode);
91 static void open_file_device(DEVICE *dev, int mode);
94 * Allocate and initialize the DEVICE structure
95 * Note, if dev is non-NULL, it is already allocated,
96 * thus we neither allocate it nor free it. This allows
97 * the caller to put the packet in shared memory.
99 * Note, for a tape, the device->device_name is the device name
100 * (e.g. /dev/nst0), and for a file, the device name
101 * is the directory in which the file will be placed.
105 init_dev(JCR *jcr, DEVRES *device)
113 /* Check that device is available */
114 if (stat(device->device_name, &statp) < 0) {
116 Jmsg2(jcr, M_ERROR, 0, _("Unable to stat device %s: ERR=%s\n"),
117 device->device_name, be.strerror());
124 if (S_ISDIR(statp.st_mode)) {
126 } else if (S_ISCHR(statp.st_mode)) {
128 } else if (S_ISFIFO(statp.st_mode)) {
130 } else if (!(device->cap_bits & CAP_REQMOUNT)) {
131 Jmsg2(jcr, M_ERROR, 0, _("%s is an unknown device type. Must be tape or directory. st_mode=%x\n"),
132 device->device_name, statp.st_mode);
136 dev = (DEVICE *)get_memory(sizeof(DEVICE));
137 memset(dev, 0, sizeof(DEVICE));
138 dev->state = ST_MALLOC;
140 /* Copy user supplied device parameters from Resource */
141 dev->dev_name = get_memory(strlen(device->device_name)+1);
142 pm_strcpy(dev->dev_name, device->device_name);
143 dev->prt_name = get_memory(strlen(device->device_name) + strlen(device->hdr.name) + 20);
144 /* We edit "Resource-name" (physical-name) */
145 Mmsg(dev->prt_name, "\"%s\" (%s)", device->hdr.name, device->device_name);
146 dev->capabilities = device->cap_bits;
147 dev->min_block_size = device->min_block_size;
148 dev->max_block_size = device->max_block_size;
149 dev->max_volume_size = device->max_volume_size;
150 dev->max_file_size = device->max_file_size;
151 dev->volume_capacity = device->volume_capacity;
152 dev->max_rewind_wait = device->max_rewind_wait;
153 dev->max_open_wait = device->max_open_wait;
154 dev->max_open_vols = device->max_open_vols;
155 dev->vol_poll_interval = device->vol_poll_interval;
156 dev->max_spool_size = device->max_spool_size;
157 dev->drive_index = device->drive_index;
158 dev->autoselect = device->autoselect;
159 if (tape) { /* No parts on tapes */
160 dev->max_part_size = 0;
163 dev->max_part_size = device->max_part_size;
166 if (dev->vol_poll_interval && dev->vol_poll_interval < 60) {
167 dev->vol_poll_interval = 60;
169 dev->device = device;
172 dev->state |= ST_TAPE;
174 dev->state |= ST_FIFO;
175 dev->capabilities |= CAP_STREAM; /* set stream device */
177 dev->state |= ST_FILE;
180 /* If the device requires mount :
181 * - Check that the mount point is available
182 * - Check that (un)mount commands are defined
184 if (dev->is_file() && dev->requires_mount()) {
185 if (stat(device->mount_point, &statp) < 0) {
187 dev->dev_errno = errno;
188 Jmsg2(jcr, M_ERROR, 0, _("Unable to stat mount point %s: ERR=%s\n"),
189 device->mount_point, be.strerror());
192 if (!device->mount_command || !device->unmount_command) {
193 Jmsg0(jcr, M_ERROR_TERM, 0, _("Mount and unmount commands must defined for a device which requires mount.\n"));
195 if (!device->write_part_command) {
196 Jmsg0(jcr, M_ERROR_TERM, 0, _("Write part command must be defined for a device which requires mount.\n"));
198 dev->state |= ST_DVD;
201 if (dev->max_block_size > 1000000) {
202 Jmsg3(jcr, M_ERROR, 0, _("Block size %u on device %s is too large, using default %u\n"),
203 dev->max_block_size, dev->print_name(), DEFAULT_BLOCK_SIZE);
204 dev->max_block_size = 0;
206 if (dev->max_block_size % TAPE_BSIZE != 0) {
207 Jmsg2(jcr, M_WARNING, 0, _("Max block size %u not multiple of device %s block size.\n"),
208 dev->max_block_size, dev->print_name());
211 dev->errmsg = get_pool_memory(PM_EMSG);
214 if ((errstat = pthread_mutex_init(&dev->mutex, NULL)) != 0) {
216 dev->dev_errno = errstat;
217 Mmsg1(dev->errmsg, _("Unable to init mutex: ERR=%s\n"), be.strerror(errstat));
218 Jmsg0(jcr, M_ERROR_TERM, 0, dev->errmsg);
220 if ((errstat = pthread_cond_init(&dev->wait, NULL)) != 0) {
222 dev->dev_errno = errstat;
223 Mmsg1(dev->errmsg, _("Unable to init cond variable: ERR=%s\n"), be.strerror(errstat));
224 Jmsg0(jcr, M_ERROR_TERM, 0, dev->errmsg);
226 if ((errstat = pthread_cond_init(&dev->wait_next_vol, NULL)) != 0) {
228 dev->dev_errno = errstat;
229 Mmsg1(dev->errmsg, _("Unable to init cond variable: ERR=%s\n"), be.strerror(errstat));
230 Jmsg0(jcr, M_ERROR_TERM, 0, dev->errmsg);
232 if ((errstat = pthread_mutex_init(&dev->spool_mutex, NULL)) != 0) {
234 dev->dev_errno = errstat;
235 Mmsg1(dev->errmsg, _("Unable to init mutex: ERR=%s\n"), be.strerror(errstat));
236 Jmsg0(jcr, M_ERROR_TERM, 0, dev->errmsg);
238 if ((errstat = rwl_init(&dev->lock)) != 0) {
240 dev->dev_errno = errstat;
241 Mmsg1(dev->errmsg, _("Unable to init mutex: ERR=%s\n"), be.strerror(errstat));
242 Jmsg0(jcr, M_ERROR_TERM, 0, dev->errmsg);
246 dev->attached_dcrs = New(dlist(dcr, &dcr->dev_link));
247 Dmsg2(29, "init_dev: tape=%d dev_name=%s\n", dev->is_tape(), dev->dev_name);
253 * Open the device with the operating system and
254 * initialize buffer pointers.
256 * Returns: -1 on error
259 * Note, for a tape, the VolName is the name we give to the
260 * volume (not really used here), but for a file, the
261 * VolName represents the name of the file to be created/opened.
262 * In the case of a file, the full name is the device name
263 * (archive_name) with the VolName concatenated.
266 DEVICE::open(char *VolName, int mode)
269 if (openmode == mode) {
272 ::close(fd); /* use system close so correct mode will be used on open */
276 bstrncpy(VolCatInfo.VolCatName, VolName, sizeof(VolCatInfo.VolCatName));
278 VolCatInfo.VolCatName[0] = 0;
281 Dmsg4(29, "open dev: tape=%d dev_name=%s vol=%s mode=%d\n", is_tape(),
282 dev_name, VolCatInfo.VolCatName, mode);
283 state &= ~(ST_LABEL|ST_APPEND|ST_READ|ST_EOT|ST_WEOT|ST_EOF);
284 label_type = B_BACULA_LABEL;
285 if (is_tape() || is_fifo()) {
286 open_tape_device(this, mode);
288 Dmsg1(100, "call open_file_device mode=%d\n", mode);
289 open_file_device(this, mode);
294 static void open_tape_device(DEVICE *dev, int mode)
296 int nonblocking = 0;;
300 Dmsg0(29, "open dev: device is tape\n");
302 if (mode == OPEN_READ_WRITE) {
303 dev->mode = O_RDWR | O_BINARY;
304 } else if (mode == OPEN_READ_ONLY) {
305 dev->mode = O_RDONLY | O_BINARY;
306 } else if (mode == OPEN_WRITE_ONLY) {
307 dev->mode = O_WRONLY | O_BINARY;
309 Emsg0(M_ABORT, 0, _("Illegal mode given to open dev.\n"));
311 timeout = dev->max_open_wait;
313 if (dev->open_nowait) {
314 /* Set wait counters to zero for no wait */
315 timeout = ioerrcnt = 0;
316 /* Open drive in non-block mode */
317 nonblocking = O_NONBLOCK;
319 if (dev->is_fifo() && timeout) {
321 dev->tid = start_thread_timer(pthread_self(), timeout);
323 /* If busy retry each second for max_open_wait seconds */
325 Dmsg1(500, "Try open %s\n", dev->dev_name);
326 while ((dev->fd = open(dev->dev_name, dev->mode, MODE_RW+nonblocking)) < 0) {
328 Dmsg2(500, "Open error errno=%d ERR=%s\n", errno, be.strerror());
329 if (errno == EINTR || errno == EAGAIN) {
330 Dmsg0(500, "Continue open\n");
333 /* Busy wait for specified time (default = 5 mins) */
334 if (errno == EBUSY && timeout-- > 0) {
335 Dmsg2(100, "Device %s busy. ERR=%s\n", dev->print_name(), be.strerror());
339 /* IO error (no volume) try 10 times every 6 seconds */
340 if (errno == EIO && ioerrcnt-- > 0) {
342 Dmsg0(500, "Continue open\n");
345 dev->dev_errno = errno;
346 Mmsg2(dev->errmsg, _("Unable to open device %s: ERR=%s\n"),
347 dev->print_name(), be.strerror(dev->dev_errno));
348 /* Stop any open timer we set */
350 stop_thread_timer(dev->tid);
353 Emsg0(M_FATAL, 0, dev->errmsg);
357 /* If opened in non-block mode, close it an open it normally */
363 dev->openmode = mode; /* save open mode */
365 dev->state |= ST_OPENED;
367 update_pos_dev(dev); /* update position */
368 set_os_device_parameters(dev); /* do system dependent stuff */
369 Dmsg0(500, "Open OK\n");
371 /* Stop any open() timer we started */
373 stop_thread_timer(dev->tid);
376 Dmsg1(29, "open dev: tape %d opened\n", dev->fd);
380 * Open a file or DVD device
382 static void open_file_device(DEVICE *dev, int mode)
384 POOL_MEM archive_name(PM_FNAME);
385 struct stat filestat;
388 * Handle opening of File Archive (not a tape)
390 Dmsg3(29, "Enter: open_file_dev: %s dev=%s mode=%d\n", dev->is_dvd()?"DVD":"disk",
391 archive_name.c_str(), mode);
393 if (dev->VolCatInfo.VolCatName[0] == 0) {
394 Mmsg(dev->errmsg, _("Could not open file device %s. No Volume name given.\n"),
401 if (dev->part == 0) {
406 /* if num_parts has not been set, but VolCatInfo is available, copy
407 * it from the VolCatInfo.VolCatParts */
408 if (dev->num_parts < dev->VolCatInfo.VolCatParts) {
409 dev->num_parts = dev->VolCatInfo.VolCatParts;
412 get_filename(dev, dev->VolCatInfo.VolCatName, archive_name);
414 if (mount_dev(dev, 1) < 0) {
415 Mmsg(dev->errmsg, _("Could not mount device %s.\n"),
417 Emsg0(M_FATAL, 0, dev->errmsg);
422 get_filename(dev, dev->VolCatInfo.VolCatName, archive_name);
425 Dmsg3(29, "open dev: %s dev=%s mode=%d\n", dev->is_dvd()?"DVD":"disk",
426 archive_name.c_str(), mode);
427 dev->openmode = mode;
430 * If we are not trying to access the last part, set mode to
431 * OPEN_READ_ONLY as writing would be an error.
433 if (dev->part < dev->num_parts) {
434 mode = OPEN_READ_ONLY;
437 if (mode == OPEN_READ_WRITE) {
438 dev->mode = O_CREAT | O_RDWR | O_BINARY;
439 } else if (mode == OPEN_READ_ONLY) {
440 dev->mode = O_RDONLY | O_BINARY;
441 } else if (mode == OPEN_WRITE_ONLY) {
442 dev->mode = O_WRONLY | O_BINARY;
444 Emsg0(M_ABORT, 0, _("Illegal mode given to open dev.\n"));
446 /* If creating file, give 0640 permissions */
447 Dmsg3(29, "mode=%d open(%s, 0x%x, 0640)\n", mode, archive_name.c_str(), dev->mode);
448 if ((dev->fd = open(archive_name.c_str(), dev->mode, 0640)) < 0) {
450 dev->dev_errno = errno;
451 Mmsg2(dev->errmsg, _("Could not open: %s, ERR=%s\n"), archive_name.c_str(),
453 Dmsg1(29, "open failed: %s", dev->errmsg);
454 Emsg0(M_FATAL, 0, dev->errmsg);
457 dev->state |= ST_OPENED;
459 update_pos_dev(dev); /* update position */
460 if (fstat(dev->fd, &filestat) < 0) {
462 dev->dev_errno = errno;
463 Mmsg2(dev->errmsg, _("Could not fstat: %s, ERR=%s\n"), archive_name.c_str(),
465 Emsg0(M_FATAL, 0, dev->errmsg);
466 Dmsg1(29, "open failed: %s", dev->errmsg);
468 dev->part_size = filestat.st_size;
471 Dmsg5(29, "open dev: %s fd=%d opened, part=%d/%d, part_size=%u\n",
472 dev->is_dvd()?"DVD":"disk", dev->fd, dev->part, dev->num_parts,
474 if (dev->is_open() && dev->is_dvd() && (mode != OPEN_READ_ONLY) &&
475 (dev->free_space_errno == 0 || dev->num_parts == dev->part)) {
476 update_free_space_dev(dev);
482 bool _rewind_dev(char *file, int line, DEVICE *dev)
484 Dmsg2(100, "rewind_dev called from %s:%d\n", file, line);
485 return rewind_dev(dev);
491 * Returns: true on success
494 bool rewind_dev(DEVICE *dev)
499 Dmsg1(29, "rewind_dev %s\n", dev->print_name());
501 dev->dev_errno = EBADF;
502 Mmsg1(dev->errmsg, _("Bad call to rewind_dev. Device %s not open\n"),
504 Emsg0(M_ABORT, 0, dev->errmsg);
507 dev->state &= ~(ST_EOT|ST_EOF|ST_WEOT); /* remove EOF/EOT flags */
508 dev->block_num = dev->file = 0;
511 if (dev->is_tape()) {
512 mt_com.mt_op = MTREW;
514 /* If we get an I/O error on rewind, it is probably because
515 * the drive is actually busy. We loop for (about 5 minutes)
516 * retrying every 5 seconds.
518 for (i=dev->max_rewind_wait; ; i -= 5) {
519 if (ioctl(dev->fd, MTIOCTOP, (char *)&mt_com) < 0) {
521 clrerror_dev(dev, MTREW);
522 if (i == dev->max_rewind_wait) {
523 Dmsg1(200, "Rewind error, %s. retrying ...\n", be.strerror());
525 if (dev->dev_errno == EIO && i > 0) {
526 Dmsg0(200, "Sleeping 5 seconds.\n");
530 Mmsg2(dev->errmsg, _("Rewind error on %s. ERR=%s.\n"),
531 dev->print_name(), be.strerror());
536 } else if (dev->is_file()) {
537 if (lseek_dev(dev, (off_t)0, SEEK_SET) < 0) {
539 dev->dev_errno = errno;
540 Mmsg2(dev->errmsg, _("lseek_dev error on %s. ERR=%s.\n"),
541 dev->print_name(), be.strerror());
548 void DEVICE::block(int why)
551 block_device(this, why);
555 void DEVICE::unblock()
558 unblock_device(this);
563 * Called to indicate that we have just read an
564 * EOF from the device.
566 void DEVICE::set_ateof()
576 * Called to indicate we are now at the end of the tape, and
577 * writing is not possible.
579 void DEVICE::set_ateot()
581 /* Make tape effectively read-only */
582 state |= (ST_EOF|ST_EOT|ST_WEOT);
587 * Position device to end of medium (end of data)
588 * Returns: true on succes
595 struct mtget mt_stat;
600 dev->dev_errno = EBADF;
601 Mmsg1(dev->errmsg, _("Bad call to eod_dev. Device %s not open\n"),
606 #if defined (__digital__) && defined (__unix__)
607 return fsf_dev(dev, dev->VolCatInfo.VolCatFiles);
610 Dmsg0(29, "eod_dev\n");
614 dev->state &= ~(ST_EOF); /* remove EOF flags */
615 dev->block_num = dev->file = 0;
618 if (dev->state & (ST_FIFO | ST_PROG)) {
621 if (!dev->is_tape()) {
622 pos = lseek_dev(dev, (off_t)0, SEEK_END);
623 // Dmsg1(100, "====== Seek to %lld\n", pos);
626 dev->state |= ST_EOT;
629 dev->dev_errno = errno;
631 Mmsg2(dev->errmsg, _("lseek_dev error on %s. ERR=%s.\n"),
632 dev->print_name(), be.strerror());
636 if (dev_cap(dev, CAP_FASTFSF) && !dev_cap(dev, CAP_EOM)) {
637 Dmsg0(100,"Using FAST FSF for EOM\n");
638 /* If unknown position, rewind */
639 if (!dev_get_os_pos(dev, &mt_stat)) {
640 if (!rewind_dev(dev)) {
644 mt_com.mt_op = MTFSF;
646 * ***FIXME*** fix code to handle case that INT16_MAX is
649 mt_com.mt_count = INT16_MAX; /* use big positive number */
650 if (mt_com.mt_count < 0) {
651 mt_com.mt_count = INT16_MAX; /* brain damaged system */
655 if (dev_cap(dev, CAP_MTIOCGET) && (dev_cap(dev, CAP_FASTFSF) || dev_cap(dev, CAP_EOM))) {
656 if (dev_cap(dev, CAP_EOM)) {
657 Dmsg0(100,"Using EOM for EOM\n");
658 mt_com.mt_op = MTEOM;
662 if (ioctl(dev->fd, MTIOCTOP, (char *)&mt_com) < 0) {
664 clrerror_dev(dev, mt_com.mt_op);
665 Dmsg1(50, "ioctl error: %s\n", be.strerror());
667 Mmsg2(dev->errmsg, _("ioctl MTEOM error on %s. ERR=%s.\n"),
668 dev->print_name(), be.strerror());
672 if (!dev_get_os_pos(dev, &mt_stat)) {
674 clrerror_dev(dev, -1);
675 Mmsg2(dev->errmsg, _("ioctl MTIOCGET error on %s. ERR=%s.\n"),
676 dev->print_name(), be.strerror());
679 Dmsg2(100, "EOD file=%d block=%d\n", mt_stat.mt_fileno, mt_stat.mt_blkno);
681 dev->file = mt_stat.mt_fileno;
687 * Rewind then use FSF until EOT reached
689 if (!rewind_dev(dev)) {
693 * Move file by file to the end of the tape
696 for (file_num=dev->file; !dev->at_eot(); file_num++) {
697 Dmsg0(200, "eod_dev: doing fsf 1\n");
698 if (!fsf_dev(dev, 1)) {
699 Dmsg0(200, "fsf_dev error.\n");
703 * Avoid infinite loop. ***FIXME*** possibly add code
704 * to set EOD or to turn off CAP_FASTFSF if on.
706 if (file_num == (int)dev->file) {
707 struct mtget mt_stat;
708 Dmsg1(100, "fsf_dev did not advance from file %d\n", file_num);
709 if (dev_get_os_pos(dev, &mt_stat)) {
710 Dmsg2(100, "Adjust file from %d to %d\n", dev->file , mt_stat.mt_fileno);
712 dev->file = mt_stat.mt_fileno;
719 * Some drivers leave us after second EOF when doing
720 * MTEOM, so we must backup so that appending overwrites
723 if (dev_cap(dev, CAP_BSFATEOM)) {
724 struct mtget mt_stat;
725 /* Backup over EOF */
726 ok = bsf_dev(dev, 1);
727 /* If BSF worked and fileno is known (not -1), set file */
728 if (dev_get_os_pos(dev, &mt_stat)) {
729 Dmsg2(100, "BSFATEOF adjust file from %d to %d\n", dev->file , mt_stat.mt_fileno);
730 dev->file = mt_stat.mt_fileno;
732 dev->file++; /* wing it -- not correct on all OSes */
735 update_pos_dev(dev); /* update position */
737 Dmsg1(200, "EOD dev->file=%d\n", dev->file);
742 * Set the position of the device -- only for files
743 * For other devices, there is no generic way to do it.
744 * Returns: true on succes
747 bool update_pos_dev(DEVICE *dev)
753 dev->dev_errno = EBADF;
754 Mmsg0(dev->errmsg, _("Bad device call. Device not open\n"));
755 Emsg0(M_FATAL, 0, dev->errmsg);
759 /* Find out where we are */
760 if (dev->is_file()) {
763 pos = lseek_dev(dev, (off_t)0, SEEK_CUR);
766 dev->dev_errno = errno;
767 Pmsg1(000, "Seek error: ERR=%s\n", be.strerror());
768 Mmsg2(dev->errmsg, _("lseek_dev error on %s. ERR=%s.\n"),
769 dev->print_name(), be.strerror());
772 dev->file_addr = pos;
780 * Return the status of the device. This was meant
781 * to be a generic routine. Unfortunately, it doesn't
782 * seem possible (at least I do not know how to do it
783 * currently), which means that for the moment, this
784 * routine has very little value.
788 uint32_t status_dev(DEVICE *dev)
790 struct mtget mt_stat;
793 if (dev->state & (ST_EOT | ST_WEOT)) {
797 if (dev->state & ST_EOF) {
801 if (dev->is_tape()) {
803 Pmsg0(-20," Bacula status:");
804 Pmsg2(-20," file=%d block=%d\n", dev->file, dev->block_num);
805 if (ioctl(dev->fd, MTIOCGET, (char *)&mt_stat) < 0) {
807 dev->dev_errno = errno;
808 Mmsg2(dev->errmsg, _("ioctl MTIOCGET error on %s. ERR=%s.\n"),
809 dev->print_name(), be.strerror());
812 Pmsg0(-20, " Device status:");
814 #if defined(HAVE_LINUX_OS)
815 if (GMT_EOF(mt_stat.mt_gstat)) {
819 if (GMT_BOT(mt_stat.mt_gstat)) {
823 if (GMT_EOT(mt_stat.mt_gstat)) {
827 if (GMT_SM(mt_stat.mt_gstat)) {
831 if (GMT_EOD(mt_stat.mt_gstat)) {
835 if (GMT_WR_PROT(mt_stat.mt_gstat)) {
837 Pmsg0(-20, " WR_PROT");
839 if (GMT_ONLINE(mt_stat.mt_gstat)) {
841 Pmsg0(-20, " ONLINE");
843 if (GMT_DR_OPEN(mt_stat.mt_gstat)) {
845 Pmsg0(-20, " DR_OPEN");
847 if (GMT_IM_REP_EN(mt_stat.mt_gstat)) {
848 stat |= BMT_IM_REP_EN;
849 Pmsg0(-20, " IM_REP_EN");
851 #endif /* !SunOS && !OSF */
852 if (dev_cap(dev, CAP_MTIOCGET)) {
853 Pmsg2(-20, " file=%d block=%d\n", mt_stat.mt_fileno, mt_stat.mt_blkno);
855 Pmsg2(-20, " file=%d block=%d\n", -1, -1);
858 stat |= BMT_ONLINE | BMT_BOT;
865 * Load medium in device
866 * Returns: true on success
869 bool load_dev(DEVICE *dev)
876 dev->dev_errno = EBADF;
877 Mmsg0(dev->errmsg, _("Bad call to load_dev. Device not open\n"));
878 Emsg0(M_FATAL, 0, dev->errmsg);
881 if (!(dev->is_tape())) {
885 Dmsg0(200, "stored: MTLOAD command not available\n");
887 dev->dev_errno = ENOTTY; /* function not available */
888 Mmsg2(dev->errmsg, _("ioctl MTLOAD error on %s. ERR=%s.\n"),
889 dev->print_name(), be.strerror());
893 dev->block_num = dev->file = 0;
896 mt_com.mt_op = MTLOAD;
898 if (ioctl(dev->fd, MTIOCTOP, (char *)&mt_com) < 0) {
900 dev->dev_errno = errno;
901 Mmsg2(dev->errmsg, _("ioctl MTLOAD error on %s. ERR=%s.\n"),
902 dev->print_name(), be.strerror());
910 * Rewind device and put it offline
911 * Returns: true on success
914 bool offline_dev(DEVICE *dev)
919 dev->dev_errno = EBADF;
920 Mmsg0(dev->errmsg, _("Bad call to offline_dev. Device not open\n"));
921 Emsg0(M_FATAL, 0, dev->errmsg);
924 if (!(dev->is_tape())) {
928 dev->state &= ~(ST_APPEND|ST_READ|ST_EOT|ST_EOF|ST_WEOT); /* remove EOF/EOT flags */
929 dev->block_num = dev->file = 0;
934 mt_com.mt_op = MTUNLOCK;
936 ioctl(dev->fd, MTIOCTOP, (char *)&mt_com);
938 mt_com.mt_op = MTOFFL;
940 if (ioctl(dev->fd, MTIOCTOP, (char *)&mt_com) < 0) {
942 dev->dev_errno = errno;
943 Mmsg2(dev->errmsg, _("ioctl MTOFFL error on %s. ERR=%s.\n"),
944 dev->print_name(), be.strerror());
947 Dmsg1(100, "Offlined device %s\n", dev->print_name());
951 bool offline_or_rewind_dev(DEVICE *dev)
956 if (dev_cap(dev, CAP_OFFLINEUNMOUNT)) {
957 return offline_dev(dev);
960 * Note, this rewind probably should not be here (it wasn't
961 * in prior versions of Bacula), but on FreeBSD, this is
962 * needed in the case the tape was "frozen" due to an error
963 * such as backspacing after writing and EOF. If it is not
964 * done, all future references to the drive get and I/O error.
966 clrerror_dev(dev, MTREW);
967 return rewind_dev(dev);
972 * Foward space a file
973 * Returns: true on success
977 fsf_dev(DEVICE *dev, int num)
979 struct mtget mt_stat;
984 dev->dev_errno = EBADF;
985 Mmsg0(dev->errmsg, _("Bad call to fsf_dev. Device not open\n"));
986 Emsg0(M_FATAL, 0, dev->errmsg);
990 if (!dev->is_tape()) {
993 if (dev->state & ST_EOT) {
995 Mmsg1(dev->errmsg, _("Device %s at End of Tape.\n"), dev->print_name());
998 if (dev->state & ST_EOF) {
999 Dmsg0(200, "ST_EOF set on entry to FSF\n");
1002 Dmsg0(100, "fsf_dev\n");
1005 * If Fast forward space file is set, then we
1006 * use MTFSF to forward space and MTIOCGET
1007 * to get the file position. We assume that
1008 * the SCSI driver will ensure that we do not
1009 * forward space past the end of the medium.
1011 if (dev_cap(dev, CAP_FSF) && dev_cap(dev, CAP_MTIOCGET) && dev_cap(dev, CAP_FASTFSF)) {
1012 mt_com.mt_op = MTFSF;
1013 mt_com.mt_count = num;
1014 stat = ioctl(dev->fd, MTIOCTOP, (char *)&mt_com);
1015 if (stat < 0 || !dev_get_os_pos(dev, &mt_stat)) {
1017 dev->state |= ST_EOT;
1018 Dmsg0(200, "Set ST_EOT\n");
1019 clrerror_dev(dev, MTFSF);
1020 Mmsg2(dev->errmsg, _("ioctl MTFSF error on %s. ERR=%s.\n"),
1021 dev->print_name(), be.strerror());
1022 Dmsg1(200, "%s", dev->errmsg);
1025 Dmsg2(200, "fsf file=%d block=%d\n", mt_stat.mt_fileno, mt_stat.mt_blkno);
1027 dev->file = mt_stat.mt_fileno;
1031 * Here if CAP_FSF is set, and virtually all drives
1032 * these days support it, we read a record, then forward
1033 * space one file. Using this procedure, which is slow,
1034 * is the only way we can be sure that we don't read
1035 * two consecutive EOF marks, which means End of Data.
1037 } else if (dev_cap(dev, CAP_FSF)) {
1040 Dmsg0(200, "FSF has cap_fsf\n");
1041 if (dev->max_block_size == 0) {
1042 rbuf_len = DEFAULT_BLOCK_SIZE;
1044 rbuf_len = dev->max_block_size;
1046 rbuf = get_memory(rbuf_len);
1047 mt_com.mt_op = MTFSF;
1048 mt_com.mt_count = 1;
1049 while (num-- && !(dev->state & ST_EOT)) {
1050 Dmsg0(100, "Doing read before fsf\n");
1051 if ((stat = read(dev->fd, (char *)rbuf, rbuf_len)) < 0) {
1052 if (errno == ENOMEM) { /* tape record exceeds buf len */
1053 stat = rbuf_len; /* This is OK */
1056 dev->state |= ST_EOT;
1057 clrerror_dev(dev, -1);
1058 Dmsg2(100, "Set ST_EOT read errno=%d. ERR=%s\n", dev->dev_errno,
1060 Mmsg2(dev->errmsg, _("read error on %s. ERR=%s.\n"),
1061 dev->print_name(), be.strerror());
1062 Dmsg1(100, "%s", dev->errmsg);
1066 if (stat == 0) { /* EOF */
1067 update_pos_dev(dev);
1068 Dmsg1(100, "End of File mark from read. File=%d\n", dev->file+1);
1069 /* Two reads of zero means end of tape */
1070 if (dev->state & ST_EOF) {
1071 dev->state |= ST_EOT;
1072 Dmsg0(100, "Set ST_EOT\n");
1078 } else { /* Got data */
1079 dev->state &= ~(ST_EOF|ST_EOT);
1082 Dmsg0(100, "Doing MTFSF\n");
1083 stat = ioctl(dev->fd, MTIOCTOP, (char *)&mt_com);
1084 if (stat < 0) { /* error => EOT */
1086 dev->state |= ST_EOT;
1087 Dmsg0(100, "Set ST_EOT\n");
1088 clrerror_dev(dev, MTFSF);
1089 Mmsg2(dev->errmsg, _("ioctl MTFSF error on %s. ERR=%s.\n"),
1090 dev->print_name(), be.strerror());
1091 Dmsg0(100, "Got < 0 for MTFSF\n");
1092 Dmsg1(100, "%s", dev->errmsg);
1100 * No FSF, so use FSR to simulate it
1103 Dmsg0(200, "Doing FSR for FSF\n");
1104 while (num-- && !(dev->state & ST_EOT)) {
1105 dev->fsr(INT32_MAX); /* returns -1 on EOF or EOT */
1107 if (dev->state & ST_EOT) {
1109 Mmsg1(dev->errmsg, _("Device %s at End of Tape.\n"), dev->print_name());
1115 update_pos_dev(dev);
1116 Dmsg1(200, "Return %d from FSF\n", stat);
1117 if (dev->state & ST_EOF)
1118 Dmsg0(200, "ST_EOF set on exit FSF\n");
1119 if (dev->state & ST_EOT)
1120 Dmsg0(200, "ST_EOT set on exit FSF\n");
1121 Dmsg1(200, "Return from FSF file=%d\n", dev->file);
1126 * Backward space a file
1127 * Returns: false on failure
1131 bsf_dev(DEVICE *dev, int num)
1137 dev->dev_errno = EBADF;
1138 Mmsg0(dev->errmsg, _("Bad call to bsf_dev. Device not open\n"));
1139 Emsg0(M_FATAL, 0, dev->errmsg);
1143 if (!dev->is_tape()) {
1144 Mmsg1(dev->errmsg, _("Device %s cannot BSF because it is not a tape.\n"),
1148 Dmsg0(29, "bsf_dev\n");
1149 dev->state &= ~(ST_EOT|ST_EOF);
1153 mt_com.mt_op = MTBSF;
1154 mt_com.mt_count = num;
1155 stat = ioctl(dev->fd, MTIOCTOP, (char *)&mt_com);
1158 clrerror_dev(dev, MTBSF);
1159 Mmsg2(dev->errmsg, _("ioctl MTBSF error on %s. ERR=%s.\n"),
1160 dev->print_name(), be.strerror());
1162 update_pos_dev(dev);
1168 * Foward space num records
1169 * Returns: false on failure
1172 bool DEVICE::fsr(int num)
1179 Mmsg0(errmsg, _("Bad call to fsr. Device not open\n"));
1180 Emsg0(M_FATAL, 0, errmsg);
1187 if (!dev_cap(this, CAP_FSR)) {
1188 Mmsg1(errmsg, _("ioctl MTFSR not permitted on %s.\n"), print_name());
1192 Dmsg1(29, "fsr %d\n", num);
1193 mt_com.mt_op = MTFSR;
1194 mt_com.mt_count = num;
1195 stat = ioctl(fd, MTIOCTOP, (char *)&mt_com);
1201 struct mtget mt_stat;
1202 clrerror_dev(this, MTFSR);
1203 Dmsg1(100, "FSF fail: ERR=%s\n", be.strerror());
1204 if (dev_get_os_pos(this, &mt_stat)) {
1205 Dmsg4(100, "Adjust from %d:%d to %d:%d\n", file,
1206 block_num, mt_stat.mt_fileno, mt_stat.mt_blkno);
1207 file = mt_stat.mt_fileno;
1208 block_num = mt_stat.mt_blkno;
1216 Mmsg3(errmsg, _("ioctl MTFSR %d error on %s. ERR=%s.\n"),
1217 num, print_name(), be.strerror());
1219 update_pos_dev(this);
1224 * Backward space a record
1225 * Returns: false on failure
1229 bsr_dev(DEVICE *dev, int num)
1235 dev->dev_errno = EBADF;
1236 Mmsg0(dev->errmsg, _("Bad call to bsr_dev. Device not open\n"));
1237 Emsg0(M_FATAL, 0, dev->errmsg);
1241 if (!dev->is_tape()) {
1245 if (!dev_cap(dev, CAP_BSR)) {
1246 Mmsg1(dev->errmsg, _("ioctl MTBSR not permitted on %s.\n"), dev->print_name());
1250 Dmsg0(29, "bsr_dev\n");
1251 dev->block_num -= num;
1252 dev->state &= ~(ST_EOF|ST_EOT|ST_EOF);
1253 mt_com.mt_op = MTBSR;
1254 mt_com.mt_count = num;
1255 stat = ioctl(dev->fd, MTIOCTOP, (char *)&mt_com);
1258 clrerror_dev(dev, MTBSR);
1259 Mmsg2(dev->errmsg, _("ioctl MTBSR error on %s. ERR=%s.\n"),
1260 dev->print_name(), be.strerror());
1262 update_pos_dev(dev);
1267 * Reposition the device to file, block
1268 * Returns: false on failure
1272 reposition_dev(DEVICE *dev, uint32_t file, uint32_t block)
1275 dev->dev_errno = EBADF;
1276 Mmsg0(dev->errmsg, _("Bad call to reposition_dev. Device not open\n"));
1277 Emsg0(M_FATAL, 0, dev->errmsg);
1281 if (!dev->is_tape()) {
1282 off_t pos = (((off_t)file)<<32) + block;
1283 Dmsg1(100, "===== lseek_dev to %d\n", (int)pos);
1284 if (lseek_dev(dev, pos, SEEK_SET) == (off_t)-1) {
1286 dev->dev_errno = errno;
1287 Mmsg2(dev->errmsg, _("lseek_dev error on %s. ERR=%s.\n"),
1288 dev->print_name(), be.strerror());
1292 dev->block_num = block;
1293 dev->file_addr = pos;
1296 Dmsg4(100, "reposition_dev from %u:%u to %u:%u\n",
1297 dev->file, dev->block_num, file, block);
1298 if (file < dev->file) {
1299 Dmsg0(100, "Rewind_dev\n");
1300 if (!rewind_dev(dev)) {
1304 if (file > dev->file) {
1305 Dmsg1(100, "fsf %d\n", file-dev->file);
1306 if (!fsf_dev(dev, file-dev->file)) {
1307 Dmsg1(100, "fsf failed! ERR=%s\n", strerror_dev(dev));
1310 Dmsg2(100, "wanted_file=%d at_file=%d\n", file, dev->file);
1312 if (block < dev->block_num) {
1313 Dmsg2(100, "wanted_blk=%d at_blk=%d\n", block, dev->block_num);
1314 Dmsg0(100, "bsf_dev 1\n");
1316 Dmsg0(100, "fsf_dev 1\n");
1318 Dmsg2(100, "wanted_blk=%d at_blk=%d\n", block, dev->block_num);
1320 if (dev_cap(dev, CAP_POSITIONBLOCKS) && block > dev->block_num) {
1321 /* Ignore errors as Bacula can read to the correct block */
1322 Dmsg1(100, "fsr %d\n", block-dev->block_num);
1323 return dev->fsr(block-dev->block_num);
1331 * Write an end of file on the device
1332 * Returns: 0 on success
1333 * non-zero on failure
1336 weof_dev(DEVICE *dev, int num)
1340 Dmsg0(29, "weof_dev\n");
1343 dev->dev_errno = EBADF;
1344 Mmsg0(dev->errmsg, _("Bad call to weof_dev. Device not open\n"));
1345 Emsg0(M_FATAL, 0, dev->errmsg);
1350 if (!dev->is_tape()) {
1353 if (!dev->can_append()) {
1354 Mmsg0(dev->errmsg, _("Attempt to WEOF on non-appendable Volume\n"));
1355 Emsg0(M_FATAL, 0, dev->errmsg);
1359 dev->state &= ~(ST_EOT | ST_EOF); /* remove EOF/EOT flags */
1360 mt_com.mt_op = MTWEOF;
1361 mt_com.mt_count = num;
1362 stat = ioctl(dev->fd, MTIOCTOP, (char *)&mt_com);
1369 clrerror_dev(dev, MTWEOF);
1371 Mmsg2(dev->errmsg, _("ioctl MTWEOF error on %s. ERR=%s.\n"),
1372 dev->print_name(), be.strerror());
1379 * Return string message with last error in English
1380 * Be careful not to call this routine from within dev.c
1381 * while editing an Mmsg() or you will end up in a recursive
1382 * loop creating a Segmentation Violation.
1385 strerror_dev(DEVICE *dev)
1392 * If implemented in system, clear the tape
1396 clrerror_dev(DEVICE *dev, int func)
1398 const char *msg = NULL;
1399 struct mtget mt_stat;
1402 dev->dev_errno = errno; /* save errno */
1404 dev->VolCatInfo.VolCatErrors++;
1407 if (!dev->is_tape()) {
1410 if (errno == ENOTTY || errno == ENOSYS) { /* Function not implemented */
1413 Emsg0(M_ABORT, 0, "Got ENOTTY on read/write!\n");
1417 dev->capabilities &= ~CAP_EOF; /* turn off feature */
1422 dev->capabilities &= ~CAP_EOM; /* turn off feature */
1427 dev->capabilities &= ~CAP_FSF; /* turn off feature */
1431 dev->capabilities &= ~CAP_BSF; /* turn off feature */
1435 dev->capabilities &= ~CAP_FSR; /* turn off feature */
1439 dev->capabilities &= ~CAP_BSR; /* turn off feature */
1460 bsnprintf(buf, sizeof(buf), "unknown func code %d", func);
1465 dev->dev_errno = ENOSYS;
1466 Mmsg1(dev->errmsg, _("I/O function \"%s\" not supported on this device.\n"), msg);
1467 Emsg0(M_ERROR, 0, dev->errmsg);
1470 /* On some systems such as NetBSD, this clears all errors */
1471 ioctl(dev->fd, MTIOCGET, (char *)&mt_stat);
1473 /* Found on Linux */
1477 mt_com.mt_op = MTIOCLRERR;
1478 mt_com.mt_count = 1;
1479 /* Clear any error condition on the tape */
1480 ioctl(dev->fd, MTIOCTOP, (char *)&mt_com);
1481 Dmsg0(200, "Did MTIOCLRERR\n");
1485 /* Typically on FreeBSD */
1488 /* Read and clear SCSI error status */
1489 union mterrstat mt_errstat;
1490 Dmsg2(200, "Doing MTIOCERRSTAT errno=%d ERR=%s\n", dev->dev_errno,
1491 strerror(dev->dev_errno));
1492 ioctl(dev->fd, MTIOCERRSTAT, (char *)&mt_errstat);
1496 /* Clear Subsystem Exception OSF1 */
1500 mt_com.mt_op = MTCSE;
1501 mt_com.mt_count = 1;
1502 /* Clear any error condition on the tape */
1503 ioctl(dev->fd, MTIOCTOP, (char *)&mt_com);
1504 Dmsg0(200, "Did MTCSE\n");
1510 * Flush buffer contents
1513 int flush_dev(DEVICE *dev)
1518 static void do_close(DEVICE *dev)
1521 Dmsg1(29, "really close_dev %s\n", dev->print_name());
1526 if (unmount_dev(dev, 1) < 0) {
1527 Dmsg1(0, "Cannot unmount device %s.\n", dev->print_name());
1530 /* Remove the last part file if it is empty */
1531 if (dev->can_append() && (dev->num_parts > 0)) {
1533 POOL_MEM archive_name(PM_FNAME);
1534 dev->part = dev->num_parts;
1535 get_filename(dev, dev->VolCatInfo.VolCatName, archive_name);
1536 /* Check that the part file is empty */
1537 if ((stat(archive_name.c_str(), &statp) == 0) && (statp.st_size == 0)) {
1538 unlink(archive_name.c_str());
1542 /* Clean up device packet so it can be reused */
1544 dev->state &= ~(ST_OPENED|ST_LABEL|ST_READ|ST_APPEND|ST_EOT|ST_WEOT|ST_EOF);
1545 dev->label_type = B_BACULA_LABEL;
1546 dev->file = dev->block_num = 0;
1551 dev->part_start = 0;
1552 dev->EndFile = dev->EndBlock = 0;
1553 memset(&dev->VolCatInfo, 0, sizeof(dev->VolCatInfo));
1554 memset(&dev->VolHdr, 0, sizeof(dev->VolHdr));
1556 stop_thread_timer(dev->tid);
1566 void DEVICE::close()
1568 /*if (fd >= 0 && use_count == 1) {*/
1569 /* No need to check if fd >= 0: it is checked again
1570 * in do_close, and do_close MUST be called for volumes
1571 * splitted in parts, even if fd == -1. */
1572 if (use_count == 1) {
1574 } else if (use_count > 0) {
1579 ASSERT(use_count >= 0);
1584 bool truncate_dev(DEVICE *dev)
1586 Dmsg1(100, "truncate_dev %s\n", dev->print_name());
1587 if (dev->is_tape()) {
1588 return true; /* we don't really truncate tapes */
1589 /* maybe we should rewind and write and eof ???? */
1592 /* If there is more than one part, open the first one, and then truncate it. */
1593 if (dev->num_parts > 0) {
1595 dev->VolCatInfo.VolCatParts = 0;
1596 if (open_first_part(dev, OPEN_READ_WRITE) < 0) {
1598 Mmsg1(dev->errmsg, "Unable to truncate device, because I'm unable to open the first part. ERR=%s\n", be.strerror());
1602 if (ftruncate(dev->fd, 0) != 0) {
1604 Mmsg1(dev->errmsg, _("Unable to truncate device. ERR=%s\n"), be.strerror());
1610 /* Return the resource name for the device */
1611 const char *DEVICE::name() const
1613 return device->hdr.name;
1617 dev_vol_name(DEVICE *dev)
1619 return dev->VolCatInfo.VolCatName;
1622 uint32_t dev_block(DEVICE *dev)
1624 update_pos_dev(dev);
1625 return dev->block_num;
1628 uint32_t dev_file(DEVICE *dev)
1630 update_pos_dev(dev);
1635 * Free memory allocated for the device
1638 term_dev(DEVICE *dev)
1641 dev->dev_errno = EBADF;
1642 Mmsg0(dev->errmsg, _("Bad call to term_dev. Device not open\n"));
1643 Emsg0(M_FATAL, 0, dev->errmsg);
1647 Dmsg0(29, "term_dev\n");
1648 if (dev->dev_name) {
1649 free_memory(dev->dev_name);
1650 dev->dev_name = NULL;
1652 if (dev->prt_name) {
1653 free_memory(dev->prt_name);
1654 dev->prt_name = NULL;
1657 free_pool_memory(dev->errmsg);
1660 pthread_mutex_destroy(&dev->mutex);
1661 pthread_cond_destroy(&dev->wait);
1662 pthread_cond_destroy(&dev->wait_next_vol);
1663 pthread_mutex_destroy(&dev->spool_mutex);
1664 rwl_destroy(&dev->lock);
1665 if (dev->attached_dcrs) {
1666 delete dev->attached_dcrs;
1667 dev->attached_dcrs = NULL;
1669 if (dev->state & ST_MALLOC) {
1670 free_pool_memory((POOLMEM *)dev);
1675 * This routine initializes the device wait timers
1677 void init_device_wait_timers(DCR *dcr)
1679 DEVICE *dev = dcr->dev;
1680 JCR *jcr = dcr->jcr;
1682 /* ******FIXME******* put these on config variables */
1683 dev->min_wait = 60 * 60;
1684 dev->max_wait = 24 * 60 * 60;
1685 dev->max_num_wait = 9; /* 5 waits =~ 1 day, then 1 day at a time */
1686 dev->wait_sec = dev->min_wait;
1687 dev->rem_wait_sec = dev->wait_sec;
1690 dev->BadVolName[0] = 0;
1692 jcr->min_wait = 60 * 60;
1693 jcr->max_wait = 24 * 60 * 60;
1694 jcr->max_num_wait = 9; /* 5 waits =~ 1 day, then 1 day at a time */
1695 jcr->wait_sec = jcr->min_wait;
1696 jcr->rem_wait_sec = jcr->wait_sec;
1701 void init_jcr_device_wait_timers(JCR *jcr)
1703 /* ******FIXME******* put these on config variables */
1704 jcr->min_wait = 60 * 60;
1705 jcr->max_wait = 24 * 60 * 60;
1706 jcr->max_num_wait = 9; /* 5 waits =~ 1 day, then 1 day at a time */
1707 jcr->wait_sec = jcr->min_wait;
1708 jcr->rem_wait_sec = jcr->wait_sec;
1714 * The dev timers are used for waiting on a particular device
1716 * Returns: true if time doubled
1717 * false if max time expired
1719 bool double_dev_wait_time(DEVICE *dev)
1721 dev->wait_sec *= 2; /* double wait time */
1722 if (dev->wait_sec > dev->max_wait) { /* but not longer than maxtime */
1723 dev->wait_sec = dev->max_wait;
1726 dev->rem_wait_sec = dev->wait_sec;
1727 if (dev->num_wait >= dev->max_num_wait) {
1734 void set_os_device_parameters(DEVICE *dev)
1736 #ifdef HAVE_LINUX_OS
1738 if (dev->min_block_size == dev->max_block_size &&
1739 dev->min_block_size == 0) { /* variable block mode */
1740 mt_com.mt_op = MTSETBLK;
1741 mt_com.mt_count = 0;
1742 if (ioctl(dev->fd, MTIOCTOP, (char *)&mt_com) < 0) {
1743 clrerror_dev(dev, MTSETBLK);
1745 mt_com.mt_op = MTSETDRVBUFFER;
1746 mt_com.mt_count = MT_ST_CLEARBOOLEANS;
1747 if (!dev_cap(dev, CAP_TWOEOF)) {
1748 mt_com.mt_count |= MT_ST_TWO_FM;
1750 if (dev_cap(dev, CAP_EOM)) {
1751 mt_com.mt_count |= MT_ST_FAST_MTEOM;
1753 if (ioctl(dev->fd, MTIOCTOP, (char *)&mt_com) < 0) {
1754 clrerror_dev(dev, MTSETBLK);
1760 #ifdef HAVE_NETBSD_OS
1762 if (dev->min_block_size == dev->max_block_size &&
1763 dev->min_block_size == 0) { /* variable block mode */
1764 mt_com.mt_op = MTSETBSIZ;
1765 mt_com.mt_count = 0;
1766 if (ioctl(dev->fd, MTIOCTOP, (char *)&mt_com) < 0) {
1767 clrerror_dev(dev, MTSETBSIZ);
1769 /* Get notified at logical end of tape */
1770 mt_com.mt_op = MTEWARN;
1771 mt_com.mt_count = 1;
1772 if (ioctl(dev->fd, MTIOCTOP, (char *)&mt_com) < 0) {
1773 clrerror_dev(dev, MTEWARN);
1779 #if HAVE_FREEBSD_OS || HAVE_OPENBSD_OS
1781 if (dev->min_block_size == dev->max_block_size &&
1782 dev->min_block_size == 0) { /* variable block mode */
1783 mt_com.mt_op = MTSETBSIZ;
1784 mt_com.mt_count = 0;
1785 if (ioctl(dev->fd, MTIOCTOP, (char *)&mt_com) < 0) {
1786 clrerror_dev(dev, MTSETBSIZ);
1794 if (dev->min_block_size == dev->max_block_size &&
1795 dev->min_block_size == 0) { /* variable block mode */
1796 mt_com.mt_op = MTSRSZ;
1797 mt_com.mt_count = 0;
1798 if (ioctl(dev->fd, MTIOCTOP, (char *)&mt_com) < 0) {
1799 clrerror_dev(dev, MTSRSZ);
1806 static bool dev_get_os_pos(DEVICE *dev, struct mtget *mt_stat)
1808 return dev_cap(dev, CAP_MTIOCGET) &&
1809 ioctl(dev->fd, MTIOCGET, (char *)mt_stat) == 0 &&
1810 mt_stat->mt_fileno >= 0;