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 int mount_dev(DEVICE* dev, int timeout);
85 int unmount_dev(DEVICE *dev, int timeout);
86 void update_free_space_dev(DEVICE* dev);
89 /* Forward referenced functions */
90 void set_os_device_parameters(DEVICE *dev);
91 static bool dev_get_os_pos(DEVICE *dev, struct mtget *mt_stat);
92 static void open_tape_device(DEVICE *dev, int mode);
93 static void open_file_device(DEVICE *dev, int mode);
96 * Allocate and initialize the DEVICE structure
97 * Note, if dev is non-NULL, it is already allocated,
98 * thus we neither allocate it nor free it. This allows
99 * the caller to put the packet in shared memory.
101 * Note, for a tape, the device->device_name is the device name
102 * (e.g. /dev/nst0), and for a file, the device name
103 * is the directory in which the file will be placed.
107 init_dev(JCR *jcr, DEVRES *device)
115 /* Check that device is available */
116 if (stat(device->device_name, &statp) < 0) {
118 Jmsg2(jcr, M_ERROR, 0, _("Unable to stat device %s: ERR=%s\n"),
119 device->device_name, be.strerror());
126 if (S_ISDIR(statp.st_mode)) {
128 } else if (S_ISCHR(statp.st_mode)) {
130 } else if (S_ISFIFO(statp.st_mode)) {
132 } else if (!(device->cap_bits & CAP_REQMOUNT)) {
133 Jmsg2(jcr, M_ERROR, 0, _("%s is an unknown device type. Must be tape or directory. st_mode=%x\n"),
134 device->device_name, statp.st_mode);
138 dev = (DEVICE *)get_memory(sizeof(DEVICE));
139 memset(dev, 0, sizeof(DEVICE));
140 dev->state = ST_MALLOC;
142 /* Copy user supplied device parameters from Resource */
143 dev->dev_name = get_memory(strlen(device->device_name)+1);
144 pm_strcpy(dev->dev_name, device->device_name);
145 dev->prt_name = get_memory(strlen(device->device_name) + strlen(device->hdr.name) + 20);
146 /* We edit "Resource-name" (physical-name) */
147 Mmsg(dev->prt_name, "\"%s\" (%s)", device->hdr.name, device->device_name);
148 dev->capabilities = device->cap_bits;
149 dev->min_block_size = device->min_block_size;
150 dev->max_block_size = device->max_block_size;
151 dev->max_volume_size = device->max_volume_size;
152 dev->max_file_size = device->max_file_size;
153 dev->volume_capacity = device->volume_capacity;
154 dev->max_rewind_wait = device->max_rewind_wait;
155 dev->max_open_wait = device->max_open_wait;
156 dev->max_open_vols = device->max_open_vols;
157 dev->vol_poll_interval = device->vol_poll_interval;
158 dev->max_spool_size = device->max_spool_size;
159 dev->drive_index = device->drive_index;
160 dev->autoselect = device->autoselect;
161 if (tape) { /* No parts on tapes */
162 dev->max_part_size = 0;
165 dev->max_part_size = device->max_part_size;
168 if (dev->vol_poll_interval && dev->vol_poll_interval < 60) {
169 dev->vol_poll_interval = 60;
171 dev->device = device;
174 dev->state |= ST_TAPE;
176 dev->state |= ST_FIFO;
177 dev->capabilities |= CAP_STREAM; /* set stream device */
179 dev->state |= ST_FILE;
182 /* If the device requires mount :
183 * - Check that the mount point is available
184 * - Check that (un)mount commands are defined
186 if (dev->is_file() && device->cap_bits & CAP_REQMOUNT) {
187 if (stat(device->mount_point, &statp) < 0) {
189 dev->dev_errno = errno;
190 Jmsg2(jcr, M_ERROR, 0, _("Unable to stat mount point %s: ERR=%s\n"),
191 device->mount_point, be.strerror());
194 if (!device->mount_command || !device->unmount_command) {
195 Jmsg0(jcr, M_ERROR_TERM, 0, _("Mount and unmount commands must defined for a device which requires mount.\n"));
197 if (!device->write_part_command) {
198 Jmsg0(jcr, M_ERROR_TERM, 0, _("Write part command must be defined for a device which requires mount.\n"));
200 dev->state |= ST_DVD;
203 if (dev->max_block_size > 1000000) {
204 Jmsg3(jcr, M_ERROR, 0, _("Block size %u on device %s is too large, using default %u\n"),
205 dev->max_block_size, dev->print_name(), DEFAULT_BLOCK_SIZE);
206 dev->max_block_size = 0;
208 if (dev->max_block_size % TAPE_BSIZE != 0) {
209 Jmsg2(jcr, M_WARNING, 0, _("Max block size %u not multiple of device %s block size.\n"),
210 dev->max_block_size, dev->print_name());
213 dev->errmsg = get_pool_memory(PM_EMSG);
216 if ((errstat = pthread_mutex_init(&dev->mutex, NULL)) != 0) {
218 dev->dev_errno = errstat;
219 Mmsg1(dev->errmsg, _("Unable to init mutex: ERR=%s\n"), be.strerror(errstat));
220 Jmsg0(jcr, M_ERROR_TERM, 0, dev->errmsg);
222 if ((errstat = pthread_cond_init(&dev->wait, NULL)) != 0) {
224 dev->dev_errno = errstat;
225 Mmsg1(dev->errmsg, _("Unable to init cond variable: ERR=%s\n"), be.strerror(errstat));
226 Jmsg0(jcr, M_ERROR_TERM, 0, dev->errmsg);
228 if ((errstat = pthread_cond_init(&dev->wait_next_vol, NULL)) != 0) {
230 dev->dev_errno = errstat;
231 Mmsg1(dev->errmsg, _("Unable to init cond variable: ERR=%s\n"), be.strerror(errstat));
232 Jmsg0(jcr, M_ERROR_TERM, 0, dev->errmsg);
234 if ((errstat = pthread_mutex_init(&dev->spool_mutex, NULL)) != 0) {
236 dev->dev_errno = errstat;
237 Mmsg1(dev->errmsg, _("Unable to init mutex: ERR=%s\n"), be.strerror(errstat));
238 Jmsg0(jcr, M_ERROR_TERM, 0, dev->errmsg);
240 if ((errstat = rwl_init(&dev->lock)) != 0) {
242 dev->dev_errno = errstat;
243 Mmsg1(dev->errmsg, _("Unable to init mutex: ERR=%s\n"), be.strerror(errstat));
244 Jmsg0(jcr, M_ERROR_TERM, 0, dev->errmsg);
248 dev->attached_dcrs = New(dlist(dcr, &dcr->dev_link));
249 Dmsg2(29, "init_dev: tape=%d dev_name=%s\n", dev->is_tape(), dev->dev_name);
255 * Open the device with the operating system and
256 * initialize buffer pointers.
258 * Returns: -1 on error
261 * Note, for a tape, the VolName is the name we give to the
262 * volume (not really used here), but for a file, the
263 * VolName represents the name of the file to be created/opened.
264 * In the case of a file, the full name is the device name
265 * (archive_name) with the VolName concatenated.
268 open_dev(DEVICE *dev, char *VolName, int mode)
270 if (dev->is_open()) {
271 if (dev->openmode == mode) {
274 close(dev->fd); /* close so correct mode will be used */
278 bstrncpy(dev->VolCatInfo.VolCatName, VolName, sizeof(dev->VolCatInfo.VolCatName));
280 dev->VolCatInfo.VolCatName[0] = 0;
283 Dmsg3(29, "open_dev: tape=%d dev_name=%s vol=%s\n", dev->is_tape(),
284 dev->dev_name, dev->VolCatInfo.VolCatName);
285 dev->state &= ~(ST_LABEL|ST_APPEND|ST_READ|ST_EOT|ST_WEOT|ST_EOF);
286 dev->label_type = B_BACULA_LABEL;
287 if (dev->is_tape() || dev->is_fifo()) {
288 open_tape_device(dev, mode);
290 open_file_device(dev, mode);
295 static void open_tape_device(DEVICE *dev, int mode)
297 int nonblocking = 0;;
301 Dmsg0(29, "open_dev: device is tape\n");
303 if (mode == OPEN_READ_WRITE) {
304 dev->mode = O_RDWR | O_BINARY;
305 } else if (mode == OPEN_READ_ONLY) {
306 dev->mode = O_RDONLY | O_BINARY;
307 } else if (mode == OPEN_WRITE_ONLY) {
308 dev->mode = O_WRONLY | O_BINARY;
310 Emsg0(M_ABORT, 0, _("Illegal mode given to open_dev.\n"));
312 timeout = dev->max_open_wait;
314 if (dev->open_nowait) {
315 /* Set wait counters to zero for no wait */
316 timeout = ioerrcnt = 0;
317 /* Open drive in non-block mode */
318 nonblocking = O_NONBLOCK;
320 if (dev->is_fifo() && timeout) {
322 dev->tid = start_thread_timer(pthread_self(), timeout);
324 /* If busy retry each second for max_open_wait seconds */
326 Dmsg1(500, "Try open %s\n", dev->dev_name);
327 while ((dev->fd = open(dev->dev_name, dev->mode, MODE_RW+nonblocking)) < 0) {
329 Dmsg2(500, "Open error errno=%d ERR=%s\n", errno, be.strerror());
330 if (errno == EINTR || errno == EAGAIN) {
331 Dmsg0(500, "Continue open\n");
334 /* Busy wait for specified time (default = 5 mins) */
335 if (errno == EBUSY && timeout-- > 0) {
336 Dmsg2(100, "Device %s busy. ERR=%s\n", dev->print_name(), be.strerror());
340 /* IO error (no volume) try 10 times every 6 seconds */
341 if (errno == EIO && ioerrcnt-- > 0) {
343 Dmsg0(500, "Continue open\n");
346 dev->dev_errno = errno;
347 Mmsg2(dev->errmsg, _("Unable to open device %s: ERR=%s\n"),
348 dev->print_name(), be.strerror(dev->dev_errno));
349 /* Stop any open timer we set */
351 stop_thread_timer(dev->tid);
354 Emsg0(M_FATAL, 0, dev->errmsg);
358 /* If opened in non-block mode, close it an open it normally */
364 dev->openmode = mode; /* save open mode */
366 dev->state |= ST_OPENED;
368 update_pos_dev(dev); /* update position */
369 set_os_device_parameters(dev); /* do system dependent stuff */
370 Dmsg0(500, "Open OK\n");
372 /* Stop any open() timer we started */
374 stop_thread_timer(dev->tid);
377 Dmsg1(29, "open_dev: tape %d opened\n", dev->fd);
381 * Open a file or DVD device
383 static void open_file_device(DEVICE *dev, int mode)
385 POOL_MEM archive_name(PM_FNAME);
386 struct stat filestat;
388 * Handle opening of File Archive (not a tape)
390 if (dev->part == 0) {
395 /* if num_parts has not been set, but VolCatInfo is available, copy
396 * it from the VolCatInfo.VolCatParts */
397 if (dev->num_parts < dev->VolCatInfo.VolCatParts) {
398 dev->num_parts = dev->VolCatInfo.VolCatParts;
401 if (dev->VolCatInfo.VolCatName[0] == 0) {
402 Mmsg(dev->errmsg, _("Could not open file device %s. No Volume name given.\n"),
407 get_filename(dev, dev->VolCatInfo.VolCatName, archive_name);
409 if (mount_dev(dev, 1) < 0) {
410 Mmsg(dev->errmsg, _("Could not mount device %s.\n"),
412 Emsg0(M_FATAL, 0, dev->errmsg);
417 Dmsg3(29, "open_dev: device is %s (mode:%d)\n", dev->is_dvd()?"DVD":"disk",
418 archive_name.c_str(), mode);
419 dev->openmode = mode;
422 * If we are not trying to access the last part, set mode to
423 * OPEN_READ_ONLY as writing would be an error.
425 if (dev->part < dev->num_parts) {
426 mode = OPEN_READ_ONLY;
429 if (mode == OPEN_READ_WRITE) {
430 dev->mode = O_CREAT | O_RDWR | O_BINARY;
431 } else if (mode == OPEN_READ_ONLY) {
432 dev->mode = O_RDONLY | O_BINARY;
433 } else if (mode == OPEN_WRITE_ONLY) {
434 dev->mode = O_WRONLY | O_BINARY;
436 Emsg0(M_ABORT, 0, _("Illegal mode given to open_dev.\n"));
438 /* If creating file, give 0640 permissions */
439 Dmsg2(29, "open(%s, 0x%x, 0640)\n", archive_name.c_str(), dev->mode);
440 if ((dev->fd = open(archive_name.c_str(), dev->mode, 0640)) < 0) {
442 dev->dev_errno = errno;
443 Mmsg2(dev->errmsg, _("Could not open: %s, ERR=%s\n"), archive_name.c_str(),
445 Emsg0(M_FATAL, 0, dev->errmsg);
448 dev->state |= ST_OPENED;
450 update_pos_dev(dev); /* update position */
451 if (fstat(dev->fd, &filestat) < 0) {
453 dev->dev_errno = errno;
454 Mmsg2(dev->errmsg, _("Could not fstat: %s, ERR=%s\n"), archive_name.c_str(),
456 Emsg0(M_FATAL, 0, dev->errmsg);
458 dev->part_size = filestat.st_size;
461 Dmsg5(29, "open_dev: %s fd=%d opened, part=%d/%d, part_size=%u\n",
462 dev->is_dvd()?"DVD":"disk", dev->fd, dev->part, dev->num_parts,
464 if (dev->is_dvd() && (dev->mode != OPEN_READ_ONLY) &&
465 (dev->free_space_errno == 0 || dev->num_parts == dev->part)) {
466 update_free_space_dev(dev);
472 bool _rewind_dev(char *file, int line, DEVICE *dev)
474 Dmsg2(100, "rewind_dev called from %s:%d\n", file, line);
475 return rewind_dev(dev);
481 * Returns: true on success
484 bool rewind_dev(DEVICE *dev)
489 Dmsg1(29, "rewind_dev %s\n", dev->print_name());
491 dev->dev_errno = EBADF;
492 Mmsg1(dev->errmsg, _("Bad call to rewind_dev. Device %s not open\n"),
494 Emsg0(M_ABORT, 0, dev->errmsg);
497 dev->state &= ~(ST_EOT|ST_EOF|ST_WEOT); /* remove EOF/EOT flags */
498 dev->block_num = dev->file = 0;
501 if (dev->is_tape()) {
502 mt_com.mt_op = MTREW;
504 /* If we get an I/O error on rewind, it is probably because
505 * the drive is actually busy. We loop for (about 5 minutes)
506 * retrying every 5 seconds.
508 for (i=dev->max_rewind_wait; ; i -= 5) {
509 if (ioctl(dev->fd, MTIOCTOP, (char *)&mt_com) < 0) {
511 clrerror_dev(dev, MTREW);
512 if (i == dev->max_rewind_wait) {
513 Dmsg1(200, "Rewind error, %s. retrying ...\n", be.strerror());
515 if (dev->dev_errno == EIO && i > 0) {
516 Dmsg0(200, "Sleeping 5 seconds.\n");
520 Mmsg2(dev->errmsg, _("Rewind error on %s. ERR=%s.\n"),
521 dev->print_name(), be.strerror());
526 } else if (dev->is_file()) {
527 if (lseek_dev(dev, (off_t)0, SEEK_SET) < 0) {
529 dev->dev_errno = errno;
530 Mmsg2(dev->errmsg, _("lseek_dev error on %s. ERR=%s.\n"),
531 dev->print_name(), be.strerror());
538 void DEVICE::block(int why)
541 block_device(this, why);
545 void DEVICE::unblock()
548 unblock_device(this);
553 * Called to indicate that we have just read an
554 * EOF from the device.
556 void DEVICE::set_ateof()
566 * Called to indicate we are now at the end of the tape, and
567 * writing is not possible.
569 void DEVICE::set_ateot()
571 /* Make tape effectively read-only */
572 state |= (ST_EOF|ST_EOT|ST_WEOT);
577 * Position device to end of medium (end of data)
578 * Returns: true on succes
585 struct mtget mt_stat;
590 dev->dev_errno = EBADF;
591 Mmsg1(dev->errmsg, _("Bad call to eod_dev. Device %s not open\n"),
596 #if defined (__digital__) && defined (__unix__)
597 return fsf_dev(dev, dev->VolCatInfo.VolCatFiles);
600 Dmsg0(29, "eod_dev\n");
604 dev->state &= ~(ST_EOF); /* remove EOF flags */
605 dev->block_num = dev->file = 0;
608 if (dev->state & (ST_FIFO | ST_PROG)) {
611 if (!dev->is_tape()) {
612 pos = lseek_dev(dev, (off_t)0, SEEK_END);
613 // Dmsg1(100, "====== Seek to %lld\n", pos);
616 dev->state |= ST_EOT;
619 dev->dev_errno = errno;
621 Mmsg2(dev->errmsg, _("lseek_dev error on %s. ERR=%s.\n"),
622 dev->print_name(), be.strerror());
626 if (dev_cap(dev, CAP_FASTFSF) && !dev_cap(dev, CAP_EOM)) {
627 Dmsg0(100,"Using FAST FSF for EOM\n");
628 /* If unknown position, rewind */
629 if (!dev_get_os_pos(dev, &mt_stat)) {
630 if (!rewind_dev(dev)) {
634 mt_com.mt_op = MTFSF;
636 * ***FIXME*** fix code to handle case that INT16_MAX is
639 mt_com.mt_count = INT16_MAX; /* use big positive number */
640 if (mt_com.mt_count < 0) {
641 mt_com.mt_count = INT16_MAX; /* brain damaged system */
645 if (dev_cap(dev, CAP_MTIOCGET) && (dev_cap(dev, CAP_FASTFSF) || dev_cap(dev, CAP_EOM))) {
646 if (dev_cap(dev, CAP_EOM)) {
647 Dmsg0(100,"Using EOM for EOM\n");
648 mt_com.mt_op = MTEOM;
652 if (ioctl(dev->fd, MTIOCTOP, (char *)&mt_com) < 0) {
654 clrerror_dev(dev, mt_com.mt_op);
655 Dmsg1(50, "ioctl error: %s\n", be.strerror());
657 Mmsg2(dev->errmsg, _("ioctl MTEOM error on %s. ERR=%s.\n"),
658 dev->print_name(), be.strerror());
662 if (!dev_get_os_pos(dev, &mt_stat)) {
664 clrerror_dev(dev, -1);
665 Mmsg2(dev->errmsg, _("ioctl MTIOCGET error on %s. ERR=%s.\n"),
666 dev->print_name(), be.strerror());
669 Dmsg2(100, "EOD file=%d block=%d\n", mt_stat.mt_fileno, mt_stat.mt_blkno);
671 dev->file = mt_stat.mt_fileno;
677 * Rewind then use FSF until EOT reached
679 if (!rewind_dev(dev)) {
683 * Move file by file to the end of the tape
686 for (file_num=dev->file; !dev->at_eot(); file_num++) {
687 Dmsg0(200, "eod_dev: doing fsf 1\n");
688 if (!fsf_dev(dev, 1)) {
689 Dmsg0(200, "fsf_dev error.\n");
693 * Avoid infinite loop. ***FIXME*** possibly add code
694 * to set EOD or to turn off CAP_FASTFSF if on.
696 if (file_num == (int)dev->file) {
697 struct mtget mt_stat;
698 Dmsg1(100, "fsf_dev did not advance from file %d\n", file_num);
699 if (dev_get_os_pos(dev, &mt_stat)) {
700 Dmsg2(100, "Adjust file from %d to %d\n", dev->file , mt_stat.mt_fileno);
702 dev->file = mt_stat.mt_fileno;
709 * Some drivers leave us after second EOF when doing
710 * MTEOM, so we must backup so that appending overwrites
713 if (dev_cap(dev, CAP_BSFATEOM)) {
714 struct mtget mt_stat;
715 /* Backup over EOF */
716 ok = bsf_dev(dev, 1);
717 /* If BSF worked and fileno is known (not -1), set file */
718 if (dev_get_os_pos(dev, &mt_stat)) {
719 Dmsg2(100, "BSFATEOF adjust file from %d to %d\n", dev->file , mt_stat.mt_fileno);
720 dev->file = mt_stat.mt_fileno;
722 dev->file++; /* wing it -- not correct on all OSes */
725 update_pos_dev(dev); /* update position */
727 Dmsg1(200, "EOD dev->file=%d\n", dev->file);
732 * Set the position of the device -- only for files
733 * For other devices, there is no generic way to do it.
734 * Returns: true on succes
737 bool update_pos_dev(DEVICE *dev)
743 dev->dev_errno = EBADF;
744 Mmsg0(dev->errmsg, _("Bad device call. Device not open\n"));
745 Emsg0(M_FATAL, 0, dev->errmsg);
749 /* Find out where we are */
750 if (dev->is_file()) {
753 pos = lseek_dev(dev, (off_t)0, SEEK_CUR);
756 dev->dev_errno = errno;
757 Pmsg1(000, "Seek error: ERR=%s\n", be.strerror());
758 Mmsg2(dev->errmsg, _("lseek_dev error on %s. ERR=%s.\n"),
759 dev->print_name(), be.strerror());
762 dev->file_addr = pos;
770 * Return the status of the device. This was meant
771 * to be a generic routine. Unfortunately, it doesn't
772 * seem possible (at least I do not know how to do it
773 * currently), which means that for the moment, this
774 * routine has very little value.
778 uint32_t status_dev(DEVICE *dev)
780 struct mtget mt_stat;
783 if (dev->state & (ST_EOT | ST_WEOT)) {
787 if (dev->state & ST_EOF) {
791 if (dev->is_tape()) {
793 Pmsg0(-20," Bacula status:");
794 Pmsg2(-20," file=%d block=%d\n", dev->file, dev->block_num);
795 if (ioctl(dev->fd, MTIOCGET, (char *)&mt_stat) < 0) {
797 dev->dev_errno = errno;
798 Mmsg2(dev->errmsg, _("ioctl MTIOCGET error on %s. ERR=%s.\n"),
799 dev->print_name(), be.strerror());
802 Pmsg0(-20, " Device status:");
804 #if defined(HAVE_LINUX_OS)
805 if (GMT_EOF(mt_stat.mt_gstat)) {
809 if (GMT_BOT(mt_stat.mt_gstat)) {
813 if (GMT_EOT(mt_stat.mt_gstat)) {
817 if (GMT_SM(mt_stat.mt_gstat)) {
821 if (GMT_EOD(mt_stat.mt_gstat)) {
825 if (GMT_WR_PROT(mt_stat.mt_gstat)) {
827 Pmsg0(-20, " WR_PROT");
829 if (GMT_ONLINE(mt_stat.mt_gstat)) {
831 Pmsg0(-20, " ONLINE");
833 if (GMT_DR_OPEN(mt_stat.mt_gstat)) {
835 Pmsg0(-20, " DR_OPEN");
837 if (GMT_IM_REP_EN(mt_stat.mt_gstat)) {
838 stat |= BMT_IM_REP_EN;
839 Pmsg0(-20, " IM_REP_EN");
841 #endif /* !SunOS && !OSF */
842 if (dev_cap(dev, CAP_MTIOCGET)) {
843 Pmsg2(-20, " file=%d block=%d\n", mt_stat.mt_fileno, mt_stat.mt_blkno);
845 Pmsg2(-20, " file=%d block=%d\n", -1, -1);
848 stat |= BMT_ONLINE | BMT_BOT;
855 * Load medium in device
856 * Returns: true on success
859 bool load_dev(DEVICE *dev)
866 dev->dev_errno = EBADF;
867 Mmsg0(dev->errmsg, _("Bad call to load_dev. Device not open\n"));
868 Emsg0(M_FATAL, 0, dev->errmsg);
871 if (!(dev->is_tape())) {
875 Dmsg0(200, "stored: MTLOAD command not available\n");
877 dev->dev_errno = ENOTTY; /* function not available */
878 Mmsg2(dev->errmsg, _("ioctl MTLOAD error on %s. ERR=%s.\n"),
879 dev->print_name(), be.strerror());
883 dev->block_num = dev->file = 0;
886 mt_com.mt_op = MTLOAD;
888 if (ioctl(dev->fd, MTIOCTOP, (char *)&mt_com) < 0) {
890 dev->dev_errno = errno;
891 Mmsg2(dev->errmsg, _("ioctl MTLOAD error on %s. ERR=%s.\n"),
892 dev->print_name(), be.strerror());
900 * Rewind device and put it offline
901 * Returns: true on success
904 bool offline_dev(DEVICE *dev)
909 dev->dev_errno = EBADF;
910 Mmsg0(dev->errmsg, _("Bad call to offline_dev. Device not open\n"));
911 Emsg0(M_FATAL, 0, dev->errmsg);
914 if (!(dev->is_tape())) {
918 dev->state &= ~(ST_APPEND|ST_READ|ST_EOT|ST_EOF|ST_WEOT); /* remove EOF/EOT flags */
919 dev->block_num = dev->file = 0;
924 mt_com.mt_op = MTUNLOCK;
926 ioctl(dev->fd, MTIOCTOP, (char *)&mt_com);
928 mt_com.mt_op = MTOFFL;
930 if (ioctl(dev->fd, MTIOCTOP, (char *)&mt_com) < 0) {
932 dev->dev_errno = errno;
933 Mmsg2(dev->errmsg, _("ioctl MTOFFL error on %s. ERR=%s.\n"),
934 dev->print_name(), be.strerror());
937 Dmsg1(100, "Offlined device %s\n", dev->print_name());
941 bool offline_or_rewind_dev(DEVICE *dev)
946 if (dev_cap(dev, CAP_OFFLINEUNMOUNT)) {
947 return offline_dev(dev);
950 * Note, this rewind probably should not be here (it wasn't
951 * in prior versions of Bacula), but on FreeBSD, this is
952 * needed in the case the tape was "frozen" due to an error
953 * such as backspacing after writing and EOF. If it is not
954 * done, all future references to the drive get and I/O error.
956 clrerror_dev(dev, MTREW);
957 return rewind_dev(dev);
962 * Foward space a file
963 * Returns: true on success
967 fsf_dev(DEVICE *dev, int num)
969 struct mtget mt_stat;
974 dev->dev_errno = EBADF;
975 Mmsg0(dev->errmsg, _("Bad call to fsf_dev. Device not open\n"));
976 Emsg0(M_FATAL, 0, dev->errmsg);
980 if (!dev->is_tape()) {
983 if (dev->state & ST_EOT) {
985 Mmsg1(dev->errmsg, _("Device %s at End of Tape.\n"), dev->print_name());
988 if (dev->state & ST_EOF) {
989 Dmsg0(200, "ST_EOF set on entry to FSF\n");
992 Dmsg0(100, "fsf_dev\n");
995 * If Fast forward space file is set, then we
996 * use MTFSF to forward space and MTIOCGET
997 * to get the file position. We assume that
998 * the SCSI driver will ensure that we do not
999 * forward space past the end of the medium.
1001 if (dev_cap(dev, CAP_FSF) && dev_cap(dev, CAP_MTIOCGET) && dev_cap(dev, CAP_FASTFSF)) {
1002 mt_com.mt_op = MTFSF;
1003 mt_com.mt_count = num;
1004 stat = ioctl(dev->fd, MTIOCTOP, (char *)&mt_com);
1005 if (stat < 0 || !dev_get_os_pos(dev, &mt_stat)) {
1007 dev->state |= ST_EOT;
1008 Dmsg0(200, "Set ST_EOT\n");
1009 clrerror_dev(dev, MTFSF);
1010 Mmsg2(dev->errmsg, _("ioctl MTFSF error on %s. ERR=%s.\n"),
1011 dev->print_name(), be.strerror());
1012 Dmsg1(200, "%s", dev->errmsg);
1015 Dmsg2(200, "fsf file=%d block=%d\n", mt_stat.mt_fileno, mt_stat.mt_blkno);
1017 dev->file = mt_stat.mt_fileno;
1021 * Here if CAP_FSF is set, and virtually all drives
1022 * these days support it, we read a record, then forward
1023 * space one file. Using this procedure, which is slow,
1024 * is the only way we can be sure that we don't read
1025 * two consecutive EOF marks, which means End of Data.
1027 } else if (dev_cap(dev, CAP_FSF)) {
1030 Dmsg0(200, "FSF has cap_fsf\n");
1031 if (dev->max_block_size == 0) {
1032 rbuf_len = DEFAULT_BLOCK_SIZE;
1034 rbuf_len = dev->max_block_size;
1036 rbuf = get_memory(rbuf_len);
1037 mt_com.mt_op = MTFSF;
1038 mt_com.mt_count = 1;
1039 while (num-- && !(dev->state & ST_EOT)) {
1040 Dmsg0(100, "Doing read before fsf\n");
1041 if ((stat = read(dev->fd, (char *)rbuf, rbuf_len)) < 0) {
1042 if (errno == ENOMEM) { /* tape record exceeds buf len */
1043 stat = rbuf_len; /* This is OK */
1046 dev->state |= ST_EOT;
1047 clrerror_dev(dev, -1);
1048 Dmsg2(100, "Set ST_EOT read errno=%d. ERR=%s\n", dev->dev_errno,
1050 Mmsg2(dev->errmsg, _("read error on %s. ERR=%s.\n"),
1051 dev->print_name(), be.strerror());
1052 Dmsg1(100, "%s", dev->errmsg);
1056 if (stat == 0) { /* EOF */
1057 update_pos_dev(dev);
1058 Dmsg1(100, "End of File mark from read. File=%d\n", dev->file+1);
1059 /* Two reads of zero means end of tape */
1060 if (dev->state & ST_EOF) {
1061 dev->state |= ST_EOT;
1062 Dmsg0(100, "Set ST_EOT\n");
1068 } else { /* Got data */
1069 dev->state &= ~(ST_EOF|ST_EOT);
1072 Dmsg0(100, "Doing MTFSF\n");
1073 stat = ioctl(dev->fd, MTIOCTOP, (char *)&mt_com);
1074 if (stat < 0) { /* error => EOT */
1076 dev->state |= ST_EOT;
1077 Dmsg0(100, "Set ST_EOT\n");
1078 clrerror_dev(dev, MTFSF);
1079 Mmsg2(dev->errmsg, _("ioctl MTFSF error on %s. ERR=%s.\n"),
1080 dev->print_name(), be.strerror());
1081 Dmsg0(100, "Got < 0 for MTFSF\n");
1082 Dmsg1(100, "%s", dev->errmsg);
1090 * No FSF, so use FSR to simulate it
1093 Dmsg0(200, "Doing FSR for FSF\n");
1094 while (num-- && !(dev->state & ST_EOT)) {
1095 dev->fsr(INT32_MAX); /* returns -1 on EOF or EOT */
1097 if (dev->state & ST_EOT) {
1099 Mmsg1(dev->errmsg, _("Device %s at End of Tape.\n"), dev->print_name());
1105 update_pos_dev(dev);
1106 Dmsg1(200, "Return %d from FSF\n", stat);
1107 if (dev->state & ST_EOF)
1108 Dmsg0(200, "ST_EOF set on exit FSF\n");
1109 if (dev->state & ST_EOT)
1110 Dmsg0(200, "ST_EOT set on exit FSF\n");
1111 Dmsg1(200, "Return from FSF file=%d\n", dev->file);
1116 * Backward space a file
1117 * Returns: false on failure
1121 bsf_dev(DEVICE *dev, int num)
1127 dev->dev_errno = EBADF;
1128 Mmsg0(dev->errmsg, _("Bad call to bsf_dev. Device not open\n"));
1129 Emsg0(M_FATAL, 0, dev->errmsg);
1133 if (!dev->is_tape()) {
1134 Mmsg1(dev->errmsg, _("Device %s cannot BSF because it is not a tape.\n"),
1138 Dmsg0(29, "bsf_dev\n");
1139 dev->state &= ~(ST_EOT|ST_EOF);
1143 mt_com.mt_op = MTBSF;
1144 mt_com.mt_count = num;
1145 stat = ioctl(dev->fd, MTIOCTOP, (char *)&mt_com);
1148 clrerror_dev(dev, MTBSF);
1149 Mmsg2(dev->errmsg, _("ioctl MTBSF error on %s. ERR=%s.\n"),
1150 dev->print_name(), be.strerror());
1152 update_pos_dev(dev);
1158 * Foward space num records
1159 * Returns: false on failure
1162 bool DEVICE::fsr(int num)
1169 Mmsg0(errmsg, _("Bad call to fsr. Device not open\n"));
1170 Emsg0(M_FATAL, 0, errmsg);
1177 if (!dev_cap(this, CAP_FSR)) {
1178 Mmsg1(errmsg, _("ioctl MTFSR not permitted on %s.\n"), print_name());
1182 Dmsg1(29, "fsr %d\n", num);
1183 mt_com.mt_op = MTFSR;
1184 mt_com.mt_count = num;
1185 stat = ioctl(fd, MTIOCTOP, (char *)&mt_com);
1191 struct mtget mt_stat;
1192 clrerror_dev(this, MTFSR);
1193 Dmsg1(100, "FSF fail: ERR=%s\n", be.strerror());
1194 if (dev_get_os_pos(this, &mt_stat)) {
1195 Dmsg4(100, "Adjust from %d:%d to %d:%d\n", file,
1196 block_num, mt_stat.mt_fileno, mt_stat.mt_blkno);
1197 file = mt_stat.mt_fileno;
1198 block_num = mt_stat.mt_blkno;
1206 Mmsg3(errmsg, _("ioctl MTFSR %d error on %s. ERR=%s.\n"),
1207 num, print_name(), be.strerror());
1209 update_pos_dev(this);
1214 * Backward space a record
1215 * Returns: false on failure
1219 bsr_dev(DEVICE *dev, int num)
1225 dev->dev_errno = EBADF;
1226 Mmsg0(dev->errmsg, _("Bad call to bsr_dev. Device not open\n"));
1227 Emsg0(M_FATAL, 0, dev->errmsg);
1231 if (!dev->is_tape()) {
1235 if (!dev_cap(dev, CAP_BSR)) {
1236 Mmsg1(dev->errmsg, _("ioctl MTBSR not permitted on %s.\n"), dev->print_name());
1240 Dmsg0(29, "bsr_dev\n");
1241 dev->block_num -= num;
1242 dev->state &= ~(ST_EOF|ST_EOT|ST_EOF);
1243 mt_com.mt_op = MTBSR;
1244 mt_com.mt_count = num;
1245 stat = ioctl(dev->fd, MTIOCTOP, (char *)&mt_com);
1248 clrerror_dev(dev, MTBSR);
1249 Mmsg2(dev->errmsg, _("ioctl MTBSR error on %s. ERR=%s.\n"),
1250 dev->print_name(), be.strerror());
1252 update_pos_dev(dev);
1257 * Reposition the device to file, block
1258 * Returns: false on failure
1262 reposition_dev(DEVICE *dev, uint32_t file, uint32_t block)
1265 dev->dev_errno = EBADF;
1266 Mmsg0(dev->errmsg, _("Bad call to reposition_dev. Device not open\n"));
1267 Emsg0(M_FATAL, 0, dev->errmsg);
1271 if (!dev->is_tape()) {
1272 off_t pos = (((off_t)file)<<32) + block;
1273 Dmsg1(100, "===== lseek_dev to %d\n", (int)pos);
1274 if (lseek_dev(dev, pos, SEEK_SET) == (off_t)-1) {
1276 dev->dev_errno = errno;
1277 Mmsg2(dev->errmsg, _("lseek_dev error on %s. ERR=%s.\n"),
1278 dev->print_name(), be.strerror());
1282 dev->block_num = block;
1283 dev->file_addr = pos;
1286 Dmsg4(100, "reposition_dev from %u:%u to %u:%u\n",
1287 dev->file, dev->block_num, file, block);
1288 if (file < dev->file) {
1289 Dmsg0(100, "Rewind_dev\n");
1290 if (!rewind_dev(dev)) {
1294 if (file > dev->file) {
1295 Dmsg1(100, "fsf %d\n", file-dev->file);
1296 if (!fsf_dev(dev, file-dev->file)) {
1297 Dmsg1(100, "fsf failed! ERR=%s\n", strerror_dev(dev));
1300 Dmsg2(100, "wanted_file=%d at_file=%d\n", file, dev->file);
1302 if (block < dev->block_num) {
1303 Dmsg2(100, "wanted_blk=%d at_blk=%d\n", block, dev->block_num);
1304 Dmsg0(100, "bsf_dev 1\n");
1306 Dmsg0(100, "fsf_dev 1\n");
1308 Dmsg2(100, "wanted_blk=%d at_blk=%d\n", block, dev->block_num);
1310 if (dev_cap(dev, CAP_POSITIONBLOCKS) && block > dev->block_num) {
1311 /* Ignore errors as Bacula can read to the correct block */
1312 Dmsg1(100, "fsr %d\n", block-dev->block_num);
1313 return dev->fsr(block-dev->block_num);
1321 * Write an end of file on the device
1322 * Returns: 0 on success
1323 * non-zero on failure
1326 weof_dev(DEVICE *dev, int num)
1330 Dmsg0(29, "weof_dev\n");
1333 dev->dev_errno = EBADF;
1334 Mmsg0(dev->errmsg, _("Bad call to weof_dev. Device not open\n"));
1335 Emsg0(M_FATAL, 0, dev->errmsg);
1340 if (!dev->is_tape()) {
1343 if (!dev->can_append()) {
1344 Mmsg0(dev->errmsg, _("Attempt to WEOF on non-appendable Volume\n"));
1345 Emsg0(M_FATAL, 0, dev->errmsg);
1349 dev->state &= ~(ST_EOT | ST_EOF); /* remove EOF/EOT flags */
1350 mt_com.mt_op = MTWEOF;
1351 mt_com.mt_count = num;
1352 stat = ioctl(dev->fd, MTIOCTOP, (char *)&mt_com);
1359 clrerror_dev(dev, MTWEOF);
1361 Mmsg2(dev->errmsg, _("ioctl MTWEOF error on %s. ERR=%s.\n"),
1362 dev->print_name(), be.strerror());
1369 * Return string message with last error in English
1370 * Be careful not to call this routine from within dev.c
1371 * while editing an Mmsg() or you will end up in a recursive
1372 * loop creating a Segmentation Violation.
1375 strerror_dev(DEVICE *dev)
1382 * If implemented in system, clear the tape
1386 clrerror_dev(DEVICE *dev, int func)
1388 const char *msg = NULL;
1389 struct mtget mt_stat;
1392 dev->dev_errno = errno; /* save errno */
1394 dev->VolCatInfo.VolCatErrors++;
1397 if (!dev->is_tape()) {
1400 if (errno == ENOTTY || errno == ENOSYS) { /* Function not implemented */
1403 Emsg0(M_ABORT, 0, "Got ENOTTY on read/write!\n");
1407 dev->capabilities &= ~CAP_EOF; /* turn off feature */
1412 dev->capabilities &= ~CAP_EOM; /* turn off feature */
1417 dev->capabilities &= ~CAP_FSF; /* turn off feature */
1421 dev->capabilities &= ~CAP_BSF; /* turn off feature */
1425 dev->capabilities &= ~CAP_FSR; /* turn off feature */
1429 dev->capabilities &= ~CAP_BSR; /* turn off feature */
1450 bsnprintf(buf, sizeof(buf), "unknown func code %d", func);
1455 dev->dev_errno = ENOSYS;
1456 Mmsg1(dev->errmsg, _("I/O function \"%s\" not supported on this device.\n"), msg);
1457 Emsg0(M_ERROR, 0, dev->errmsg);
1460 /* On some systems such as NetBSD, this clears all errors */
1461 ioctl(dev->fd, MTIOCGET, (char *)&mt_stat);
1463 /* Found on Linux */
1467 mt_com.mt_op = MTIOCLRERR;
1468 mt_com.mt_count = 1;
1469 /* Clear any error condition on the tape */
1470 ioctl(dev->fd, MTIOCTOP, (char *)&mt_com);
1471 Dmsg0(200, "Did MTIOCLRERR\n");
1475 /* Typically on FreeBSD */
1478 /* Read and clear SCSI error status */
1479 union mterrstat mt_errstat;
1480 Dmsg2(200, "Doing MTIOCERRSTAT errno=%d ERR=%s\n", dev->dev_errno,
1481 strerror(dev->dev_errno));
1482 ioctl(dev->fd, MTIOCERRSTAT, (char *)&mt_errstat);
1486 /* Clear Subsystem Exception OSF1 */
1490 mt_com.mt_op = MTCSE;
1491 mt_com.mt_count = 1;
1492 /* Clear any error condition on the tape */
1493 ioctl(dev->fd, MTIOCTOP, (char *)&mt_com);
1494 Dmsg0(200, "Did MTCSE\n");
1500 * Flush buffer contents
1503 int flush_dev(DEVICE *dev)
1508 static void do_close(DEVICE *dev)
1511 Dmsg1(29, "really close_dev %s\n", dev->print_name());
1516 if (unmount_dev(dev, 1) < 0) {
1517 Dmsg1(0, "Cannot unmount device %s.\n", dev->print_name());
1520 /* Remove the last part file if it is empty */
1521 if (dev->can_append() && (dev->num_parts > 0)) {
1523 POOL_MEM archive_name(PM_FNAME);
1524 dev->part = dev->num_parts;
1525 get_filename(dev, dev->VolCatInfo.VolCatName, archive_name);
1526 /* Check that the part file is empty */
1527 if ((stat(archive_name.c_str(), &statp) == 0) && (statp.st_size == 0)) {
1528 unlink(archive_name.c_str());
1532 /* Clean up device packet so it can be reused */
1534 dev->state &= ~(ST_OPENED|ST_LABEL|ST_READ|ST_APPEND|ST_EOT|ST_WEOT|ST_EOF);
1535 dev->label_type = B_BACULA_LABEL;
1536 dev->file = dev->block_num = 0;
1541 dev->part_start = 0;
1542 dev->EndFile = dev->EndBlock = 0;
1543 memset(&dev->VolCatInfo, 0, sizeof(dev->VolCatInfo));
1544 memset(&dev->VolHdr, 0, sizeof(dev->VolHdr));
1546 stop_thread_timer(dev->tid);
1556 void DEVICE::close()
1558 /*if (fd >= 0 && use_count == 1) {*/
1559 /* No need to check if fd >= 0: it is checked again
1560 * in do_close, and do_close MUST be called for volumes
1561 * splitted in parts, even if fd == -1. */
1562 if (use_count == 1) {
1564 } else if (use_count > 0) {
1569 ASSERT(use_count >= 0);
1574 bool truncate_dev(DEVICE *dev)
1576 Dmsg1(100, "truncate_dev %s\n", dev->print_name());
1577 if (dev->is_tape()) {
1578 return true; /* we don't really truncate tapes */
1579 /* maybe we should rewind and write and eof ???? */
1582 /* If there is more than one part, open the first one, and then truncate it. */
1583 if (dev->num_parts > 0) {
1585 dev->VolCatInfo.VolCatParts = 0;
1586 if (open_first_part(dev) < 0) {
1588 Mmsg1(dev->errmsg, "Unable to truncate device, because I'm unable to open the first part. ERR=%s\n", be.strerror());
1592 if (ftruncate(dev->fd, 0) != 0) {
1594 Mmsg1(dev->errmsg, _("Unable to truncate device. ERR=%s\n"), be.strerror());
1600 /* Return the resource name for the device */
1601 const char *DEVICE::name() const
1603 return device->hdr.name;
1607 dev_vol_name(DEVICE *dev)
1609 return dev->VolCatInfo.VolCatName;
1612 uint32_t dev_block(DEVICE *dev)
1614 update_pos_dev(dev);
1615 return dev->block_num;
1618 uint32_t dev_file(DEVICE *dev)
1620 update_pos_dev(dev);
1625 * Free memory allocated for the device
1628 term_dev(DEVICE *dev)
1631 dev->dev_errno = EBADF;
1632 Mmsg0(dev->errmsg, _("Bad call to term_dev. Device not open\n"));
1633 Emsg0(M_FATAL, 0, dev->errmsg);
1637 Dmsg0(29, "term_dev\n");
1638 if (dev->dev_name) {
1639 free_memory(dev->dev_name);
1640 dev->dev_name = NULL;
1642 if (dev->prt_name) {
1643 free_memory(dev->prt_name);
1644 dev->prt_name = NULL;
1647 free_pool_memory(dev->errmsg);
1650 pthread_mutex_destroy(&dev->mutex);
1651 pthread_cond_destroy(&dev->wait);
1652 pthread_cond_destroy(&dev->wait_next_vol);
1653 pthread_mutex_destroy(&dev->spool_mutex);
1654 rwl_destroy(&dev->lock);
1655 if (dev->attached_dcrs) {
1656 delete dev->attached_dcrs;
1657 dev->attached_dcrs = NULL;
1659 if (dev->state & ST_MALLOC) {
1660 free_pool_memory((POOLMEM *)dev);
1665 * This routine initializes the device wait timers
1667 void init_device_wait_timers(DCR *dcr)
1669 DEVICE *dev = dcr->dev;
1670 JCR *jcr = dcr->jcr;
1672 /* ******FIXME******* put these on config variables */
1673 dev->min_wait = 60 * 60;
1674 dev->max_wait = 24 * 60 * 60;
1675 dev->max_num_wait = 9; /* 5 waits =~ 1 day, then 1 day at a time */
1676 dev->wait_sec = dev->min_wait;
1677 dev->rem_wait_sec = dev->wait_sec;
1680 dev->BadVolName[0] = 0;
1682 jcr->min_wait = 60 * 60;
1683 jcr->max_wait = 24 * 60 * 60;
1684 jcr->max_num_wait = 9; /* 5 waits =~ 1 day, then 1 day at a time */
1685 jcr->wait_sec = jcr->min_wait;
1686 jcr->rem_wait_sec = jcr->wait_sec;
1691 void init_jcr_device_wait_timers(JCR *jcr)
1693 /* ******FIXME******* put these on config variables */
1694 jcr->min_wait = 60 * 60;
1695 jcr->max_wait = 24 * 60 * 60;
1696 jcr->max_num_wait = 9; /* 5 waits =~ 1 day, then 1 day at a time */
1697 jcr->wait_sec = jcr->min_wait;
1698 jcr->rem_wait_sec = jcr->wait_sec;
1704 * The dev timers are used for waiting on a particular device
1706 * Returns: true if time doubled
1707 * false if max time expired
1709 bool double_dev_wait_time(DEVICE *dev)
1711 dev->wait_sec *= 2; /* double wait time */
1712 if (dev->wait_sec > dev->max_wait) { /* but not longer than maxtime */
1713 dev->wait_sec = dev->max_wait;
1716 dev->rem_wait_sec = dev->wait_sec;
1717 if (dev->num_wait >= dev->max_num_wait) {
1724 void set_os_device_parameters(DEVICE *dev)
1726 #ifdef HAVE_LINUX_OS
1728 if (dev->min_block_size == dev->max_block_size &&
1729 dev->min_block_size == 0) { /* variable block mode */
1730 mt_com.mt_op = MTSETBLK;
1731 mt_com.mt_count = 0;
1732 if (ioctl(dev->fd, MTIOCTOP, (char *)&mt_com) < 0) {
1733 clrerror_dev(dev, MTSETBLK);
1735 mt_com.mt_op = MTSETDRVBUFFER;
1736 mt_com.mt_count = MT_ST_CLEARBOOLEANS;
1737 if (!dev_cap(dev, CAP_TWOEOF)) {
1738 mt_com.mt_count |= MT_ST_TWO_FM;
1740 if (dev_cap(dev, CAP_EOM)) {
1741 mt_com.mt_count |= MT_ST_FAST_MTEOM;
1743 if (ioctl(dev->fd, MTIOCTOP, (char *)&mt_com) < 0) {
1744 clrerror_dev(dev, MTSETBLK);
1750 #ifdef HAVE_NETBSD_OS
1752 if (dev->min_block_size == dev->max_block_size &&
1753 dev->min_block_size == 0) { /* variable block mode */
1754 mt_com.mt_op = MTSETBSIZ;
1755 mt_com.mt_count = 0;
1756 if (ioctl(dev->fd, MTIOCTOP, (char *)&mt_com) < 0) {
1757 clrerror_dev(dev, MTSETBSIZ);
1759 /* Get notified at logical end of tape */
1760 mt_com.mt_op = MTEWARN;
1761 mt_com.mt_count = 1;
1762 if (ioctl(dev->fd, MTIOCTOP, (char *)&mt_com) < 0) {
1763 clrerror_dev(dev, MTEWARN);
1769 #if HAVE_FREEBSD_OS || HAVE_OPENBSD_OS
1771 if (dev->min_block_size == dev->max_block_size &&
1772 dev->min_block_size == 0) { /* variable block mode */
1773 mt_com.mt_op = MTSETBSIZ;
1774 mt_com.mt_count = 0;
1775 if (ioctl(dev->fd, MTIOCTOP, (char *)&mt_com) < 0) {
1776 clrerror_dev(dev, MTSETBSIZ);
1784 if (dev->min_block_size == dev->max_block_size &&
1785 dev->min_block_size == 0) { /* variable block mode */
1786 mt_com.mt_op = MTSRSZ;
1787 mt_com.mt_count = 0;
1788 if (ioctl(dev->fd, MTIOCTOP, (char *)&mt_com) < 0) {
1789 clrerror_dev(dev, MTSRSZ);
1796 static bool dev_get_os_pos(DEVICE *dev, struct mtget *mt_stat)
1798 return dev_cap(dev, CAP_MTIOCGET) &&
1799 ioctl(dev->fd, MTIOCGET, (char *)mt_stat) == 0 &&
1800 mt_stat->mt_fileno >= 0;