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);
92 static void open_dvd_device(DEVICE *dev, int mode);
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, DEVRES *device)
114 /* Check that device is available */
115 if (stat(device->device_name, &statp) < 0) {
117 Jmsg2(jcr, M_ERROR, 0, _("Unable to stat device %s: ERR=%s\n"),
118 device->device_name, be.strerror());
125 if (S_ISDIR(statp.st_mode)) {
127 } else if (S_ISCHR(statp.st_mode)) {
129 } else if (S_ISFIFO(statp.st_mode)) {
131 } else if (!(device->cap_bits & CAP_REQMOUNT)) {
132 Jmsg2(jcr, M_ERROR, 0, _("%s is an unknown device type. Must be tape or directory. st_mode=%x\n"),
133 device->device_name, statp.st_mode);
137 dev = (DEVICE *)get_memory(sizeof(DEVICE));
138 memset(dev, 0, sizeof(DEVICE));
139 dev->state = ST_MALLOC;
141 /* Copy user supplied device parameters from Resource */
142 dev->dev_name = get_memory(strlen(device->device_name)+1);
143 pm_strcpy(dev->dev_name, device->device_name);
144 dev->prt_name = get_memory(strlen(device->device_name) + strlen(device->hdr.name) + 20);
145 /* We edit "Resource-name" (physical-name) */
146 Mmsg(dev->prt_name, "\"%s\" (%s)", device->hdr.name, device->device_name);
147 dev->capabilities = device->cap_bits;
148 dev->min_block_size = device->min_block_size;
149 dev->max_block_size = device->max_block_size;
150 dev->max_volume_size = device->max_volume_size;
151 dev->max_file_size = device->max_file_size;
152 dev->volume_capacity = device->volume_capacity;
153 dev->max_rewind_wait = device->max_rewind_wait;
154 dev->max_open_wait = device->max_open_wait;
155 dev->max_open_vols = device->max_open_vols;
156 dev->vol_poll_interval = device->vol_poll_interval;
157 dev->max_spool_size = device->max_spool_size;
158 dev->drive_index = device->drive_index;
159 dev->autoselect = device->autoselect;
160 if (tape) { /* No parts on tapes */
161 dev->max_part_size = 0;
164 dev->max_part_size = device->max_part_size;
167 if (dev->vol_poll_interval && dev->vol_poll_interval < 60) {
168 dev->vol_poll_interval = 60;
170 dev->device = device;
173 dev->state |= ST_TAPE;
175 dev->state |= ST_FIFO;
176 dev->capabilities |= CAP_STREAM; /* set stream device */
178 dev->state |= ST_FILE;
181 /* If the device requires mount :
182 * - Check that the mount point is available
183 * - Check that (un)mount commands are defined
185 if (dev->is_file() && dev->requires_mount()) {
186 if (stat(device->mount_point, &statp) < 0) {
188 dev->dev_errno = errno;
189 Jmsg2(jcr, M_ERROR, 0, _("Unable to stat mount point %s: ERR=%s\n"),
190 device->mount_point, be.strerror());
193 if (!device->mount_command || !device->unmount_command) {
194 Jmsg0(jcr, M_ERROR_TERM, 0, _("Mount and unmount commands must defined for a device which requires mount.\n"));
196 if (!device->write_part_command) {
197 Jmsg0(jcr, M_ERROR_TERM, 0, _("Write part command must be defined for a device which requires mount.\n"));
199 dev->state |= ST_DVD;
202 if (dev->max_block_size > 1000000) {
203 Jmsg3(jcr, M_ERROR, 0, _("Block size %u on device %s is too large, using default %u\n"),
204 dev->max_block_size, dev->print_name(), DEFAULT_BLOCK_SIZE);
205 dev->max_block_size = 0;
207 if (dev->max_block_size % TAPE_BSIZE != 0) {
208 Jmsg2(jcr, M_WARNING, 0, _("Max block size %u not multiple of device %s block size.\n"),
209 dev->max_block_size, dev->print_name());
212 dev->errmsg = get_pool_memory(PM_EMSG);
215 if ((errstat = pthread_mutex_init(&dev->mutex, NULL)) != 0) {
217 dev->dev_errno = errstat;
218 Mmsg1(dev->errmsg, _("Unable to init mutex: ERR=%s\n"), be.strerror(errstat));
219 Jmsg0(jcr, M_ERROR_TERM, 0, dev->errmsg);
221 if ((errstat = pthread_cond_init(&dev->wait, NULL)) != 0) {
223 dev->dev_errno = errstat;
224 Mmsg1(dev->errmsg, _("Unable to init cond variable: ERR=%s\n"), be.strerror(errstat));
225 Jmsg0(jcr, M_ERROR_TERM, 0, dev->errmsg);
227 if ((errstat = pthread_cond_init(&dev->wait_next_vol, NULL)) != 0) {
229 dev->dev_errno = errstat;
230 Mmsg1(dev->errmsg, _("Unable to init cond variable: ERR=%s\n"), be.strerror(errstat));
231 Jmsg0(jcr, M_ERROR_TERM, 0, dev->errmsg);
233 if ((errstat = pthread_mutex_init(&dev->spool_mutex, NULL)) != 0) {
235 dev->dev_errno = errstat;
236 Mmsg1(dev->errmsg, _("Unable to init mutex: ERR=%s\n"), be.strerror(errstat));
237 Jmsg0(jcr, M_ERROR_TERM, 0, dev->errmsg);
239 if ((errstat = rwl_init(&dev->lock)) != 0) {
241 dev->dev_errno = errstat;
242 Mmsg1(dev->errmsg, _("Unable to init mutex: ERR=%s\n"), be.strerror(errstat));
243 Jmsg0(jcr, M_ERROR_TERM, 0, dev->errmsg);
247 dev->attached_dcrs = New(dlist(dcr, &dcr->dev_link));
248 Dmsg2(29, "init_dev: tape=%d dev_name=%s\n", dev->is_tape(), dev->dev_name);
254 * Open the device with the operating system and
255 * initialize buffer pointers.
257 * Returns: -1 on error
260 * Note, for a tape, the VolName is the name we give to the
261 * volume (not really used here), but for a file, the
262 * VolName represents the name of the file to be created/opened.
263 * In the case of a file, the full name is the device name
264 * (archive_name) with the VolName concatenated.
267 DEVICE::open(char *VolName, int mode)
270 if (openmode == mode) {
273 ::close(fd); /* use system close so correct mode will be used on open */
277 bstrncpy(VolCatInfo.VolCatName, VolName, sizeof(VolCatInfo.VolCatName));
279 VolCatInfo.VolCatName[0] = 0;
282 Dmsg4(29, "open dev: tape=%d dev_name=%s vol=%s mode=%d\n", is_tape(),
283 dev_name, VolCatInfo.VolCatName, mode);
284 state &= ~(ST_LABEL|ST_APPEND|ST_READ|ST_EOT|ST_WEOT|ST_EOF);
285 label_type = B_BACULA_LABEL;
286 if (is_tape() || is_fifo()) {
287 open_tape_device(this, mode);
288 } else if (is_dvd()) {
289 Dmsg1(100, "call open_dvd_device mode=%d\n", mode);
290 open_dvd_device(this, mode);
292 Dmsg1(100, "call open_file_device mode=%d\n", mode);
293 open_file_device(this, mode);
298 static void open_tape_device(DEVICE *dev, int mode)
300 int nonblocking = 0;;
304 Dmsg0(29, "open dev: device is tape\n");
306 if (mode == OPEN_READ_WRITE) {
307 dev->mode = O_RDWR | O_BINARY;
308 } else if (mode == OPEN_READ_ONLY) {
309 dev->mode = O_RDONLY | O_BINARY;
310 } else if (mode == OPEN_WRITE_ONLY) {
311 dev->mode = O_WRONLY | O_BINARY;
313 Emsg0(M_ABORT, 0, _("Illegal mode given to open dev.\n"));
315 timeout = dev->max_open_wait;
317 if (dev->open_nowait) {
318 /* Set wait counters to zero for no wait */
319 timeout = ioerrcnt = 0;
320 /* Open drive in non-block mode */
321 nonblocking = O_NONBLOCK;
323 if (dev->is_fifo() && timeout) {
325 dev->tid = start_thread_timer(pthread_self(), timeout);
327 /* If busy retry each second for max_open_wait seconds */
329 Dmsg1(500, "Try open %s\n", dev->dev_name);
330 while ((dev->fd = open(dev->dev_name, dev->mode, MODE_RW+nonblocking)) < 0) {
332 Dmsg2(500, "Open error errno=%d ERR=%s\n", errno, be.strerror());
333 if (errno == EINTR || errno == EAGAIN) {
334 Dmsg0(500, "Continue open\n");
337 /* Busy wait for specified time (default = 5 mins) */
338 if (errno == EBUSY && timeout-- > 0) {
339 Dmsg2(100, "Device %s busy. ERR=%s\n", dev->print_name(), be.strerror());
343 /* IO error (no volume) try 10 times every 6 seconds */
344 if (errno == EIO && ioerrcnt-- > 0) {
346 Dmsg0(500, "Continue open\n");
349 dev->dev_errno = errno;
350 Mmsg2(dev->errmsg, _("Unable to open device %s: ERR=%s\n"),
351 dev->print_name(), be.strerror(dev->dev_errno));
352 /* Stop any open timer we set */
354 stop_thread_timer(dev->tid);
357 Emsg0(M_FATAL, 0, dev->errmsg);
361 /* If opened in non-block mode, close it an open it normally */
367 dev->openmode = mode; /* save open mode */
369 dev->state |= ST_OPENED;
371 update_pos_dev(dev); /* update position */
372 set_os_device_parameters(dev); /* do system dependent stuff */
373 Dmsg0(500, "Open OK\n");
375 /* Stop any open() timer we started */
377 stop_thread_timer(dev->tid);
380 Dmsg1(29, "open dev: tape %d opened\n", dev->fd);
386 static void open_file_device(DEVICE *dev, int mode)
388 POOL_MEM archive_name(PM_FNAME);
391 * Handle opening of File Archive (not a tape)
393 Dmsg3(29, "Enter: open_file_dev: %s dev=%s mode=%d\n", dev->is_dvd()?"DVD":"disk",
394 archive_name.c_str(), mode);
396 if (dev->VolCatInfo.VolCatName[0] == 0) {
397 Mmsg(dev->errmsg, _("Could not open file device %s. No Volume name given.\n"),
403 get_filename(dev, dev->VolCatInfo.VolCatName, archive_name);
405 Dmsg3(29, "open dev: %s dev=%s mode=%d\n", dev->is_dvd()?"DVD":"disk",
406 archive_name.c_str(), mode);
407 dev->openmode = mode;
409 if (mode == OPEN_READ_WRITE) {
410 dev->mode = O_CREAT | O_RDWR | O_BINARY;
411 } else if (mode == OPEN_READ_ONLY) {
412 dev->mode = O_RDONLY | O_BINARY;
413 } else if (mode == OPEN_WRITE_ONLY) {
414 dev->mode = O_WRONLY | O_BINARY;
416 Emsg0(M_ABORT, 0, _("Illegal mode given to open dev.\n"));
418 /* If creating file, give 0640 permissions */
419 Dmsg3(29, "mode=%d open(%s, 0x%x, 0640)\n", mode, archive_name.c_str(), dev->mode);
420 if ((dev->fd = open(archive_name.c_str(), dev->mode, 0640)) < 0) {
422 dev->dev_errno = errno;
423 Mmsg2(dev->errmsg, _("Could not open: %s, ERR=%s\n"), archive_name.c_str(),
425 Dmsg1(29, "open failed: %s", dev->errmsg);
426 Emsg0(M_FATAL, 0, dev->errmsg);
429 dev->state |= ST_OPENED;
431 update_pos_dev(dev); /* update position */
433 Dmsg5(29, "open dev: %s fd=%d opened, part=%d/%d, part_size=%u\n",
434 dev->is_dvd()?"DVD":"disk", dev->fd, dev->part, dev->num_parts,
441 static void open_dvd_device(DEVICE *dev, int mode)
443 POOL_MEM archive_name(PM_FNAME);
444 struct stat filestat;
447 * Handle opening of File Archive (not a tape)
449 Dmsg3(29, "Enter: open_file_dev: %s dev=%s mode=%d\n", dev->is_dvd()?"DVD":"disk",
450 archive_name.c_str(), mode);
452 if (dev->VolCatInfo.VolCatName[0] == 0) {
453 Mmsg(dev->errmsg, _("Could not open file device %s. No Volume name given.\n"),
459 if (dev->part == 0) {
464 /* if num_parts has not been set, but VolCatInfo is available, copy
465 * it from the VolCatInfo.VolCatParts */
466 if (dev->num_parts < dev->VolCatInfo.VolCatParts) {
467 dev->num_parts = dev->VolCatInfo.VolCatParts;
470 get_filename(dev, dev->VolCatInfo.VolCatName, archive_name);
472 if (mount_dev(dev, 1) < 0) {
473 Mmsg(dev->errmsg, _("Could not mount device %s.\n"),
475 Emsg0(M_FATAL, 0, dev->errmsg);
480 Dmsg3(29, "open dev: %s dev=%s mode=%d\n", dev->is_dvd()?"DVD":"disk",
481 archive_name.c_str(), mode);
482 dev->openmode = mode;
485 * If we are not trying to access the last part, set mode to
486 * OPEN_READ_ONLY as writing would be an error.
488 if (dev->part < dev->num_parts) {
489 mode = OPEN_READ_ONLY;
492 if (mode == OPEN_READ_WRITE) {
493 dev->mode = O_CREAT | O_RDWR | O_BINARY;
494 } else if (mode == OPEN_READ_ONLY) {
495 dev->mode = O_RDONLY | O_BINARY;
496 } else if (mode == OPEN_WRITE_ONLY) {
497 dev->mode = O_WRONLY | O_BINARY;
499 Emsg0(M_ABORT, 0, _("Illegal mode given to open dev.\n"));
501 /* If creating file, give 0640 permissions */
502 Dmsg3(29, "mode=%d open(%s, 0x%x, 0640)\n", mode, archive_name.c_str(), dev->mode);
503 if ((dev->fd = open(archive_name.c_str(), dev->mode, 0640)) < 0) {
505 dev->dev_errno = errno;
506 Mmsg2(dev->errmsg, _("Could not open: %s, ERR=%s\n"), archive_name.c_str(),
508 Dmsg1(29, "open failed: %s", dev->errmsg);
510 /* Get size of file */
511 if (fstat(dev->fd, &filestat) < 0) {
513 dev->dev_errno = errno;
514 Mmsg2(dev->errmsg, _("Could not fstat: %s, ERR=%s\n"), archive_name.c_str(),
516 Dmsg1(29, "open failed: %s", dev->errmsg);
520 dev->part_size = filestat.st_size;
522 dev->state |= ST_OPENED;
524 update_pos_dev(dev); /* update position */
527 Dmsg5(29, "open dev: %s fd=%d opened, part=%d/%d, part_size=%u\n",
528 dev->is_dvd()?"DVD":"disk", dev->fd, dev->part, dev->num_parts,
530 if (dev->is_open() && dev->is_dvd() && (mode != OPEN_READ_ONLY) &&
531 (dev->free_space_errno == 0 || dev->num_parts == dev->part)) {
532 update_free_space_dev(dev);
539 bool _rewind_dev(char *file, int line, DEVICE *dev)
541 Dmsg2(100, "rewind_dev called from %s:%d\n", file, line);
542 return rewind_dev(dev);
548 * Returns: true on success
551 bool rewind_dev(DEVICE *dev)
556 Dmsg1(29, "rewind_dev %s\n", dev->print_name());
558 dev->dev_errno = EBADF;
559 Mmsg1(dev->errmsg, _("Bad call to rewind_dev. Device %s not open\n"),
561 Emsg0(M_ABORT, 0, dev->errmsg);
564 dev->state &= ~(ST_EOT|ST_EOF|ST_WEOT); /* remove EOF/EOT flags */
565 dev->block_num = dev->file = 0;
568 if (dev->is_tape()) {
569 mt_com.mt_op = MTREW;
571 /* If we get an I/O error on rewind, it is probably because
572 * the drive is actually busy. We loop for (about 5 minutes)
573 * retrying every 5 seconds.
575 for (i=dev->max_rewind_wait; ; i -= 5) {
576 if (ioctl(dev->fd, MTIOCTOP, (char *)&mt_com) < 0) {
578 clrerror_dev(dev, MTREW);
579 if (i == dev->max_rewind_wait) {
580 Dmsg1(200, "Rewind error, %s. retrying ...\n", be.strerror());
582 if (dev->dev_errno == EIO && i > 0) {
583 Dmsg0(200, "Sleeping 5 seconds.\n");
587 Mmsg2(dev->errmsg, _("Rewind error on %s. ERR=%s.\n"),
588 dev->print_name(), be.strerror());
593 } else if (dev->is_file()) {
594 if (lseek_dev(dev, (off_t)0, SEEK_SET) < 0) {
596 dev->dev_errno = errno;
597 Mmsg2(dev->errmsg, _("lseek_dev error on %s. ERR=%s.\n"),
598 dev->print_name(), be.strerror());
605 void DEVICE::block(int why)
608 block_device(this, why);
612 void DEVICE::unblock()
615 unblock_device(this);
620 * Called to indicate that we have just read an
621 * EOF from the device.
623 void DEVICE::set_ateof()
633 * Called to indicate we are now at the end of the tape, and
634 * writing is not possible.
636 void DEVICE::set_ateot()
638 /* Make tape effectively read-only */
639 state |= (ST_EOF|ST_EOT|ST_WEOT);
644 * Position device to end of medium (end of data)
645 * Returns: true on succes
652 struct mtget mt_stat;
657 dev->dev_errno = EBADF;
658 Mmsg1(dev->errmsg, _("Bad call to eod_dev. Device %s not open\n"),
663 #if defined (__digital__) && defined (__unix__)
664 return fsf_dev(dev, dev->VolCatInfo.VolCatFiles);
667 Dmsg0(29, "eod_dev\n");
671 dev->state &= ~(ST_EOF); /* remove EOF flags */
672 dev->block_num = dev->file = 0;
675 if (dev->state & (ST_FIFO | ST_PROG)) {
678 if (!dev->is_tape()) {
679 pos = lseek_dev(dev, (off_t)0, SEEK_END);
680 // Dmsg1(100, "====== Seek to %lld\n", pos);
683 dev->state |= ST_EOT;
686 dev->dev_errno = errno;
688 Mmsg2(dev->errmsg, _("lseek_dev error on %s. ERR=%s.\n"),
689 dev->print_name(), be.strerror());
693 if (dev_cap(dev, CAP_FASTFSF) && !dev_cap(dev, CAP_EOM)) {
694 Dmsg0(100,"Using FAST FSF for EOM\n");
695 /* If unknown position, rewind */
696 if (!dev_get_os_pos(dev, &mt_stat)) {
697 if (!rewind_dev(dev)) {
701 mt_com.mt_op = MTFSF;
703 * ***FIXME*** fix code to handle case that INT16_MAX is
706 mt_com.mt_count = INT16_MAX; /* use big positive number */
707 if (mt_com.mt_count < 0) {
708 mt_com.mt_count = INT16_MAX; /* brain damaged system */
712 if (dev_cap(dev, CAP_MTIOCGET) && (dev_cap(dev, CAP_FASTFSF) || dev_cap(dev, CAP_EOM))) {
713 if (dev_cap(dev, CAP_EOM)) {
714 Dmsg0(100,"Using EOM for EOM\n");
715 mt_com.mt_op = MTEOM;
719 if (ioctl(dev->fd, MTIOCTOP, (char *)&mt_com) < 0) {
721 clrerror_dev(dev, mt_com.mt_op);
722 Dmsg1(50, "ioctl error: %s\n", be.strerror());
724 Mmsg2(dev->errmsg, _("ioctl MTEOM error on %s. ERR=%s.\n"),
725 dev->print_name(), be.strerror());
729 if (!dev_get_os_pos(dev, &mt_stat)) {
731 clrerror_dev(dev, -1);
732 Mmsg2(dev->errmsg, _("ioctl MTIOCGET error on %s. ERR=%s.\n"),
733 dev->print_name(), be.strerror());
736 Dmsg2(100, "EOD file=%d block=%d\n", mt_stat.mt_fileno, mt_stat.mt_blkno);
738 dev->file = mt_stat.mt_fileno;
744 * Rewind then use FSF until EOT reached
746 if (!rewind_dev(dev)) {
750 * Move file by file to the end of the tape
753 for (file_num=dev->file; !dev->at_eot(); file_num++) {
754 Dmsg0(200, "eod_dev: doing fsf 1\n");
755 if (!fsf_dev(dev, 1)) {
756 Dmsg0(200, "fsf_dev error.\n");
760 * Avoid infinite loop. ***FIXME*** possibly add code
761 * to set EOD or to turn off CAP_FASTFSF if on.
763 if (file_num == (int)dev->file) {
764 struct mtget mt_stat;
765 Dmsg1(100, "fsf_dev did not advance from file %d\n", file_num);
766 if (dev_get_os_pos(dev, &mt_stat)) {
767 Dmsg2(100, "Adjust file from %d to %d\n", dev->file , mt_stat.mt_fileno);
769 dev->file = mt_stat.mt_fileno;
776 * Some drivers leave us after second EOF when doing
777 * MTEOM, so we must backup so that appending overwrites
780 if (dev_cap(dev, CAP_BSFATEOM)) {
781 struct mtget mt_stat;
782 /* Backup over EOF */
783 ok = bsf_dev(dev, 1);
784 /* If BSF worked and fileno is known (not -1), set file */
785 if (dev_get_os_pos(dev, &mt_stat)) {
786 Dmsg2(100, "BSFATEOF adjust file from %d to %d\n", dev->file , mt_stat.mt_fileno);
787 dev->file = mt_stat.mt_fileno;
789 dev->file++; /* wing it -- not correct on all OSes */
792 update_pos_dev(dev); /* update position */
794 Dmsg1(200, "EOD dev->file=%d\n", dev->file);
799 * Set the position of the device -- only for files
800 * For other devices, there is no generic way to do it.
801 * Returns: true on succes
804 bool update_pos_dev(DEVICE *dev)
810 dev->dev_errno = EBADF;
811 Mmsg0(dev->errmsg, _("Bad device call. Device not open\n"));
812 Emsg0(M_FATAL, 0, dev->errmsg);
816 /* Find out where we are */
817 if (dev->is_file()) {
820 pos = lseek_dev(dev, (off_t)0, SEEK_CUR);
823 dev->dev_errno = errno;
824 Pmsg1(000, "Seek error: ERR=%s\n", be.strerror());
825 Mmsg2(dev->errmsg, _("lseek_dev error on %s. ERR=%s.\n"),
826 dev->print_name(), be.strerror());
829 dev->file_addr = pos;
837 * Return the status of the device. This was meant
838 * to be a generic routine. Unfortunately, it doesn't
839 * seem possible (at least I do not know how to do it
840 * currently), which means that for the moment, this
841 * routine has very little value.
845 uint32_t status_dev(DEVICE *dev)
847 struct mtget mt_stat;
850 if (dev->state & (ST_EOT | ST_WEOT)) {
854 if (dev->state & ST_EOF) {
858 if (dev->is_tape()) {
860 Pmsg0(-20," Bacula status:");
861 Pmsg2(-20," file=%d block=%d\n", dev->file, dev->block_num);
862 if (ioctl(dev->fd, MTIOCGET, (char *)&mt_stat) < 0) {
864 dev->dev_errno = errno;
865 Mmsg2(dev->errmsg, _("ioctl MTIOCGET error on %s. ERR=%s.\n"),
866 dev->print_name(), be.strerror());
869 Pmsg0(-20, " Device status:");
871 #if defined(HAVE_LINUX_OS)
872 if (GMT_EOF(mt_stat.mt_gstat)) {
876 if (GMT_BOT(mt_stat.mt_gstat)) {
880 if (GMT_EOT(mt_stat.mt_gstat)) {
884 if (GMT_SM(mt_stat.mt_gstat)) {
888 if (GMT_EOD(mt_stat.mt_gstat)) {
892 if (GMT_WR_PROT(mt_stat.mt_gstat)) {
894 Pmsg0(-20, " WR_PROT");
896 if (GMT_ONLINE(mt_stat.mt_gstat)) {
898 Pmsg0(-20, " ONLINE");
900 if (GMT_DR_OPEN(mt_stat.mt_gstat)) {
902 Pmsg0(-20, " DR_OPEN");
904 if (GMT_IM_REP_EN(mt_stat.mt_gstat)) {
905 stat |= BMT_IM_REP_EN;
906 Pmsg0(-20, " IM_REP_EN");
908 #endif /* !SunOS && !OSF */
909 if (dev_cap(dev, CAP_MTIOCGET)) {
910 Pmsg2(-20, " file=%d block=%d\n", mt_stat.mt_fileno, mt_stat.mt_blkno);
912 Pmsg2(-20, " file=%d block=%d\n", -1, -1);
915 stat |= BMT_ONLINE | BMT_BOT;
922 * Load medium in device
923 * Returns: true on success
926 bool load_dev(DEVICE *dev)
933 dev->dev_errno = EBADF;
934 Mmsg0(dev->errmsg, _("Bad call to load_dev. Device not open\n"));
935 Emsg0(M_FATAL, 0, dev->errmsg);
938 if (!(dev->is_tape())) {
942 Dmsg0(200, "stored: MTLOAD command not available\n");
944 dev->dev_errno = ENOTTY; /* function not available */
945 Mmsg2(dev->errmsg, _("ioctl MTLOAD error on %s. ERR=%s.\n"),
946 dev->print_name(), be.strerror());
950 dev->block_num = dev->file = 0;
953 mt_com.mt_op = MTLOAD;
955 if (ioctl(dev->fd, MTIOCTOP, (char *)&mt_com) < 0) {
957 dev->dev_errno = errno;
958 Mmsg2(dev->errmsg, _("ioctl MTLOAD error on %s. ERR=%s.\n"),
959 dev->print_name(), be.strerror());
967 * Rewind device and put it offline
968 * Returns: true on success
971 bool offline_dev(DEVICE *dev)
976 dev->dev_errno = EBADF;
977 Mmsg0(dev->errmsg, _("Bad call to offline_dev. Device not open\n"));
978 Emsg0(M_FATAL, 0, dev->errmsg);
981 if (!(dev->is_tape())) {
985 dev->state &= ~(ST_APPEND|ST_READ|ST_EOT|ST_EOF|ST_WEOT); /* remove EOF/EOT flags */
986 dev->block_num = dev->file = 0;
991 mt_com.mt_op = MTUNLOCK;
993 ioctl(dev->fd, MTIOCTOP, (char *)&mt_com);
995 mt_com.mt_op = MTOFFL;
997 if (ioctl(dev->fd, MTIOCTOP, (char *)&mt_com) < 0) {
999 dev->dev_errno = errno;
1000 Mmsg2(dev->errmsg, _("ioctl MTOFFL error on %s. ERR=%s.\n"),
1001 dev->print_name(), be.strerror());
1004 Dmsg1(100, "Offlined device %s\n", dev->print_name());
1008 bool offline_or_rewind_dev(DEVICE *dev)
1013 if (dev_cap(dev, CAP_OFFLINEUNMOUNT)) {
1014 return offline_dev(dev);
1017 * Note, this rewind probably should not be here (it wasn't
1018 * in prior versions of Bacula), but on FreeBSD, this is
1019 * needed in the case the tape was "frozen" due to an error
1020 * such as backspacing after writing and EOF. If it is not
1021 * done, all future references to the drive get and I/O error.
1023 clrerror_dev(dev, MTREW);
1024 return rewind_dev(dev);
1029 * Foward space a file
1030 * Returns: true on success
1034 fsf_dev(DEVICE *dev, int num)
1036 struct mtget mt_stat;
1041 dev->dev_errno = EBADF;
1042 Mmsg0(dev->errmsg, _("Bad call to fsf_dev. Device not open\n"));
1043 Emsg0(M_FATAL, 0, dev->errmsg);
1047 if (!dev->is_tape()) {
1050 if (dev->state & ST_EOT) {
1052 Mmsg1(dev->errmsg, _("Device %s at End of Tape.\n"), dev->print_name());
1055 if (dev->state & ST_EOF) {
1056 Dmsg0(200, "ST_EOF set on entry to FSF\n");
1059 Dmsg0(100, "fsf_dev\n");
1062 * If Fast forward space file is set, then we
1063 * use MTFSF to forward space and MTIOCGET
1064 * to get the file position. We assume that
1065 * the SCSI driver will ensure that we do not
1066 * forward space past the end of the medium.
1068 if (dev_cap(dev, CAP_FSF) && dev_cap(dev, CAP_MTIOCGET) && dev_cap(dev, CAP_FASTFSF)) {
1069 mt_com.mt_op = MTFSF;
1070 mt_com.mt_count = num;
1071 stat = ioctl(dev->fd, MTIOCTOP, (char *)&mt_com);
1072 if (stat < 0 || !dev_get_os_pos(dev, &mt_stat)) {
1074 dev->state |= ST_EOT;
1075 Dmsg0(200, "Set ST_EOT\n");
1076 clrerror_dev(dev, MTFSF);
1077 Mmsg2(dev->errmsg, _("ioctl MTFSF error on %s. ERR=%s.\n"),
1078 dev->print_name(), be.strerror());
1079 Dmsg1(200, "%s", dev->errmsg);
1082 Dmsg2(200, "fsf file=%d block=%d\n", mt_stat.mt_fileno, mt_stat.mt_blkno);
1084 dev->file = mt_stat.mt_fileno;
1088 * Here if CAP_FSF is set, and virtually all drives
1089 * these days support it, we read a record, then forward
1090 * space one file. Using this procedure, which is slow,
1091 * is the only way we can be sure that we don't read
1092 * two consecutive EOF marks, which means End of Data.
1094 } else if (dev_cap(dev, CAP_FSF)) {
1097 Dmsg0(200, "FSF has cap_fsf\n");
1098 if (dev->max_block_size == 0) {
1099 rbuf_len = DEFAULT_BLOCK_SIZE;
1101 rbuf_len = dev->max_block_size;
1103 rbuf = get_memory(rbuf_len);
1104 mt_com.mt_op = MTFSF;
1105 mt_com.mt_count = 1;
1106 while (num-- && !(dev->state & ST_EOT)) {
1107 Dmsg0(100, "Doing read before fsf\n");
1108 if ((stat = read(dev->fd, (char *)rbuf, rbuf_len)) < 0) {
1109 if (errno == ENOMEM) { /* tape record exceeds buf len */
1110 stat = rbuf_len; /* This is OK */
1113 dev->state |= ST_EOT;
1114 clrerror_dev(dev, -1);
1115 Dmsg2(100, "Set ST_EOT read errno=%d. ERR=%s\n", dev->dev_errno,
1117 Mmsg2(dev->errmsg, _("read error on %s. ERR=%s.\n"),
1118 dev->print_name(), be.strerror());
1119 Dmsg1(100, "%s", dev->errmsg);
1123 if (stat == 0) { /* EOF */
1124 update_pos_dev(dev);
1125 Dmsg1(100, "End of File mark from read. File=%d\n", dev->file+1);
1126 /* Two reads of zero means end of tape */
1127 if (dev->state & ST_EOF) {
1128 dev->state |= ST_EOT;
1129 Dmsg0(100, "Set ST_EOT\n");
1135 } else { /* Got data */
1136 dev->state &= ~(ST_EOF|ST_EOT);
1139 Dmsg0(100, "Doing MTFSF\n");
1140 stat = ioctl(dev->fd, MTIOCTOP, (char *)&mt_com);
1141 if (stat < 0) { /* error => EOT */
1143 dev->state |= ST_EOT;
1144 Dmsg0(100, "Set ST_EOT\n");
1145 clrerror_dev(dev, MTFSF);
1146 Mmsg2(dev->errmsg, _("ioctl MTFSF error on %s. ERR=%s.\n"),
1147 dev->print_name(), be.strerror());
1148 Dmsg0(100, "Got < 0 for MTFSF\n");
1149 Dmsg1(100, "%s", dev->errmsg);
1157 * No FSF, so use FSR to simulate it
1160 Dmsg0(200, "Doing FSR for FSF\n");
1161 while (num-- && !(dev->state & ST_EOT)) {
1162 dev->fsr(INT32_MAX); /* returns -1 on EOF or EOT */
1164 if (dev->state & ST_EOT) {
1166 Mmsg1(dev->errmsg, _("Device %s at End of Tape.\n"), dev->print_name());
1172 update_pos_dev(dev);
1173 Dmsg1(200, "Return %d from FSF\n", stat);
1174 if (dev->state & ST_EOF)
1175 Dmsg0(200, "ST_EOF set on exit FSF\n");
1176 if (dev->state & ST_EOT)
1177 Dmsg0(200, "ST_EOT set on exit FSF\n");
1178 Dmsg1(200, "Return from FSF file=%d\n", dev->file);
1183 * Backward space a file
1184 * Returns: false on failure
1188 bsf_dev(DEVICE *dev, int num)
1194 dev->dev_errno = EBADF;
1195 Mmsg0(dev->errmsg, _("Bad call to bsf_dev. Device not open\n"));
1196 Emsg0(M_FATAL, 0, dev->errmsg);
1200 if (!dev->is_tape()) {
1201 Mmsg1(dev->errmsg, _("Device %s cannot BSF because it is not a tape.\n"),
1205 Dmsg0(29, "bsf_dev\n");
1206 dev->state &= ~(ST_EOT|ST_EOF);
1210 mt_com.mt_op = MTBSF;
1211 mt_com.mt_count = num;
1212 stat = ioctl(dev->fd, MTIOCTOP, (char *)&mt_com);
1215 clrerror_dev(dev, MTBSF);
1216 Mmsg2(dev->errmsg, _("ioctl MTBSF error on %s. ERR=%s.\n"),
1217 dev->print_name(), be.strerror());
1219 update_pos_dev(dev);
1225 * Foward space num records
1226 * Returns: false on failure
1229 bool DEVICE::fsr(int num)
1236 Mmsg0(errmsg, _("Bad call to fsr. Device not open\n"));
1237 Emsg0(M_FATAL, 0, errmsg);
1244 if (!dev_cap(this, CAP_FSR)) {
1245 Mmsg1(errmsg, _("ioctl MTFSR not permitted on %s.\n"), print_name());
1249 Dmsg1(29, "fsr %d\n", num);
1250 mt_com.mt_op = MTFSR;
1251 mt_com.mt_count = num;
1252 stat = ioctl(fd, MTIOCTOP, (char *)&mt_com);
1258 struct mtget mt_stat;
1259 clrerror_dev(this, MTFSR);
1260 Dmsg1(100, "FSF fail: ERR=%s\n", be.strerror());
1261 if (dev_get_os_pos(this, &mt_stat)) {
1262 Dmsg4(100, "Adjust from %d:%d to %d:%d\n", file,
1263 block_num, mt_stat.mt_fileno, mt_stat.mt_blkno);
1264 file = mt_stat.mt_fileno;
1265 block_num = mt_stat.mt_blkno;
1273 Mmsg3(errmsg, _("ioctl MTFSR %d error on %s. ERR=%s.\n"),
1274 num, print_name(), be.strerror());
1276 update_pos_dev(this);
1281 * Backward space a record
1282 * Returns: false on failure
1286 bsr_dev(DEVICE *dev, int num)
1292 dev->dev_errno = EBADF;
1293 Mmsg0(dev->errmsg, _("Bad call to bsr_dev. Device not open\n"));
1294 Emsg0(M_FATAL, 0, dev->errmsg);
1298 if (!dev->is_tape()) {
1302 if (!dev_cap(dev, CAP_BSR)) {
1303 Mmsg1(dev->errmsg, _("ioctl MTBSR not permitted on %s.\n"), dev->print_name());
1307 Dmsg0(29, "bsr_dev\n");
1308 dev->block_num -= num;
1309 dev->state &= ~(ST_EOF|ST_EOT|ST_EOF);
1310 mt_com.mt_op = MTBSR;
1311 mt_com.mt_count = num;
1312 stat = ioctl(dev->fd, MTIOCTOP, (char *)&mt_com);
1315 clrerror_dev(dev, MTBSR);
1316 Mmsg2(dev->errmsg, _("ioctl MTBSR error on %s. ERR=%s.\n"),
1317 dev->print_name(), be.strerror());
1319 update_pos_dev(dev);
1324 * Reposition the device to file, block
1325 * Returns: false on failure
1329 reposition_dev(DEVICE *dev, uint32_t file, uint32_t block)
1332 dev->dev_errno = EBADF;
1333 Mmsg0(dev->errmsg, _("Bad call to reposition_dev. Device not open\n"));
1334 Emsg0(M_FATAL, 0, dev->errmsg);
1338 if (!dev->is_tape()) {
1339 off_t pos = (((off_t)file)<<32) + block;
1340 Dmsg1(100, "===== lseek_dev to %d\n", (int)pos);
1341 if (lseek_dev(dev, pos, SEEK_SET) == (off_t)-1) {
1343 dev->dev_errno = errno;
1344 Mmsg2(dev->errmsg, _("lseek_dev error on %s. ERR=%s.\n"),
1345 dev->print_name(), be.strerror());
1349 dev->block_num = block;
1350 dev->file_addr = pos;
1353 Dmsg4(100, "reposition_dev from %u:%u to %u:%u\n",
1354 dev->file, dev->block_num, file, block);
1355 if (file < dev->file) {
1356 Dmsg0(100, "Rewind_dev\n");
1357 if (!rewind_dev(dev)) {
1361 if (file > dev->file) {
1362 Dmsg1(100, "fsf %d\n", file-dev->file);
1363 if (!fsf_dev(dev, file-dev->file)) {
1364 Dmsg1(100, "fsf failed! ERR=%s\n", strerror_dev(dev));
1367 Dmsg2(100, "wanted_file=%d at_file=%d\n", file, dev->file);
1369 if (block < dev->block_num) {
1370 Dmsg2(100, "wanted_blk=%d at_blk=%d\n", block, dev->block_num);
1371 Dmsg0(100, "bsf_dev 1\n");
1373 Dmsg0(100, "fsf_dev 1\n");
1375 Dmsg2(100, "wanted_blk=%d at_blk=%d\n", block, dev->block_num);
1377 if (dev_cap(dev, CAP_POSITIONBLOCKS) && block > dev->block_num) {
1378 /* Ignore errors as Bacula can read to the correct block */
1379 Dmsg1(100, "fsr %d\n", block-dev->block_num);
1380 return dev->fsr(block-dev->block_num);
1388 * Write an end of file on the device
1389 * Returns: 0 on success
1390 * non-zero on failure
1393 weof_dev(DEVICE *dev, int num)
1397 Dmsg0(29, "weof_dev\n");
1400 dev->dev_errno = EBADF;
1401 Mmsg0(dev->errmsg, _("Bad call to weof_dev. Device not open\n"));
1402 Emsg0(M_FATAL, 0, dev->errmsg);
1407 if (!dev->is_tape()) {
1410 if (!dev->can_append()) {
1411 Mmsg0(dev->errmsg, _("Attempt to WEOF on non-appendable Volume\n"));
1412 Emsg0(M_FATAL, 0, dev->errmsg);
1416 dev->state &= ~(ST_EOT | ST_EOF); /* remove EOF/EOT flags */
1417 mt_com.mt_op = MTWEOF;
1418 mt_com.mt_count = num;
1419 stat = ioctl(dev->fd, MTIOCTOP, (char *)&mt_com);
1426 clrerror_dev(dev, MTWEOF);
1428 Mmsg2(dev->errmsg, _("ioctl MTWEOF error on %s. ERR=%s.\n"),
1429 dev->print_name(), be.strerror());
1436 * Return string message with last error in English
1437 * Be careful not to call this routine from within dev.c
1438 * while editing an Mmsg() or you will end up in a recursive
1439 * loop creating a Segmentation Violation.
1442 strerror_dev(DEVICE *dev)
1449 * If implemented in system, clear the tape
1453 clrerror_dev(DEVICE *dev, int func)
1455 const char *msg = NULL;
1456 struct mtget mt_stat;
1459 dev->dev_errno = errno; /* save errno */
1461 dev->VolCatInfo.VolCatErrors++;
1464 if (!dev->is_tape()) {
1467 if (errno == ENOTTY || errno == ENOSYS) { /* Function not implemented */
1470 Emsg0(M_ABORT, 0, "Got ENOTTY on read/write!\n");
1474 dev->capabilities &= ~CAP_EOF; /* turn off feature */
1479 dev->capabilities &= ~CAP_EOM; /* turn off feature */
1484 dev->capabilities &= ~CAP_FSF; /* turn off feature */
1488 dev->capabilities &= ~CAP_BSF; /* turn off feature */
1492 dev->capabilities &= ~CAP_FSR; /* turn off feature */
1496 dev->capabilities &= ~CAP_BSR; /* turn off feature */
1517 bsnprintf(buf, sizeof(buf), "unknown func code %d", func);
1522 dev->dev_errno = ENOSYS;
1523 Mmsg1(dev->errmsg, _("I/O function \"%s\" not supported on this device.\n"), msg);
1524 Emsg0(M_ERROR, 0, dev->errmsg);
1527 /* On some systems such as NetBSD, this clears all errors */
1528 ioctl(dev->fd, MTIOCGET, (char *)&mt_stat);
1530 /* Found on Linux */
1534 mt_com.mt_op = MTIOCLRERR;
1535 mt_com.mt_count = 1;
1536 /* Clear any error condition on the tape */
1537 ioctl(dev->fd, MTIOCTOP, (char *)&mt_com);
1538 Dmsg0(200, "Did MTIOCLRERR\n");
1542 /* Typically on FreeBSD */
1545 /* Read and clear SCSI error status */
1546 union mterrstat mt_errstat;
1547 Dmsg2(200, "Doing MTIOCERRSTAT errno=%d ERR=%s\n", dev->dev_errno,
1548 strerror(dev->dev_errno));
1549 ioctl(dev->fd, MTIOCERRSTAT, (char *)&mt_errstat);
1553 /* Clear Subsystem Exception OSF1 */
1557 mt_com.mt_op = MTCSE;
1558 mt_com.mt_count = 1;
1559 /* Clear any error condition on the tape */
1560 ioctl(dev->fd, MTIOCTOP, (char *)&mt_com);
1561 Dmsg0(200, "Did MTCSE\n");
1567 * Flush buffer contents
1570 int flush_dev(DEVICE *dev)
1575 static void do_close(DEVICE *dev)
1578 Dmsg1(29, "really close_dev %s\n", dev->print_name());
1583 if (unmount_dev(dev, 1) < 0) {
1584 Dmsg1(0, "Cannot unmount device %s.\n", dev->print_name());
1587 /* Remove the last part file if it is empty */
1588 if (dev->can_append() && (dev->num_parts > 0)) {
1590 POOL_MEM archive_name(PM_FNAME);
1591 dev->part = dev->num_parts;
1592 get_filename(dev, dev->VolCatInfo.VolCatName, archive_name);
1593 /* Check that the part file is empty */
1594 if ((stat(archive_name.c_str(), &statp) == 0) && (statp.st_size == 0)) {
1595 unlink(archive_name.c_str());
1599 /* Clean up device packet so it can be reused */
1601 dev->state &= ~(ST_OPENED|ST_LABEL|ST_READ|ST_APPEND|ST_EOT|ST_WEOT|ST_EOF);
1602 dev->label_type = B_BACULA_LABEL;
1603 dev->file = dev->block_num = 0;
1608 dev->part_start = 0;
1609 dev->EndFile = dev->EndBlock = 0;
1610 memset(&dev->VolCatInfo, 0, sizeof(dev->VolCatInfo));
1611 memset(&dev->VolHdr, 0, sizeof(dev->VolHdr));
1613 stop_thread_timer(dev->tid);
1623 void DEVICE::close()
1625 /*if (fd >= 0 && use_count == 1) {*/
1626 /* No need to check if fd >= 0: it is checked again
1627 * in do_close, and do_close MUST be called for volumes
1628 * splitted in parts, even if fd == -1. */
1629 if (use_count == 1) {
1631 } else if (use_count > 0) {
1636 ASSERT(use_count >= 0);
1641 bool truncate_dev(DEVICE *dev)
1643 Dmsg1(100, "truncate_dev %s\n", dev->print_name());
1644 if (dev->is_tape()) {
1645 return true; /* we don't really truncate tapes */
1646 /* maybe we should rewind and write and eof ???? */
1649 /* If there is more than one part, open the first one, and then truncate it. */
1650 if (dev->num_parts > 0) {
1652 dev->VolCatInfo.VolCatParts = 0;
1653 if (open_first_part(dev, OPEN_READ_WRITE) < 0) {
1655 Mmsg1(dev->errmsg, "Unable to truncate device, because I'm unable to open the first part. ERR=%s\n", be.strerror());
1659 if (ftruncate(dev->fd, 0) != 0) {
1661 Mmsg1(dev->errmsg, _("Unable to truncate device. ERR=%s\n"), be.strerror());
1667 /* Return the resource name for the device */
1668 const char *DEVICE::name() const
1670 return device->hdr.name;
1674 dev_vol_name(DEVICE *dev)
1676 return dev->VolCatInfo.VolCatName;
1679 uint32_t dev_block(DEVICE *dev)
1681 update_pos_dev(dev);
1682 return dev->block_num;
1685 uint32_t dev_file(DEVICE *dev)
1687 update_pos_dev(dev);
1692 * Free memory allocated for the device
1695 term_dev(DEVICE *dev)
1698 dev->dev_errno = EBADF;
1699 Mmsg0(dev->errmsg, _("Bad call to term_dev. Device not open\n"));
1700 Emsg0(M_FATAL, 0, dev->errmsg);
1704 Dmsg0(29, "term_dev\n");
1705 if (dev->dev_name) {
1706 free_memory(dev->dev_name);
1707 dev->dev_name = NULL;
1709 if (dev->prt_name) {
1710 free_memory(dev->prt_name);
1711 dev->prt_name = NULL;
1714 free_pool_memory(dev->errmsg);
1717 pthread_mutex_destroy(&dev->mutex);
1718 pthread_cond_destroy(&dev->wait);
1719 pthread_cond_destroy(&dev->wait_next_vol);
1720 pthread_mutex_destroy(&dev->spool_mutex);
1721 rwl_destroy(&dev->lock);
1722 if (dev->attached_dcrs) {
1723 delete dev->attached_dcrs;
1724 dev->attached_dcrs = NULL;
1726 if (dev->state & ST_MALLOC) {
1727 free_pool_memory((POOLMEM *)dev);
1732 * This routine initializes the device wait timers
1734 void init_device_wait_timers(DCR *dcr)
1736 DEVICE *dev = dcr->dev;
1737 JCR *jcr = dcr->jcr;
1739 /* ******FIXME******* put these on config variables */
1740 dev->min_wait = 60 * 60;
1741 dev->max_wait = 24 * 60 * 60;
1742 dev->max_num_wait = 9; /* 5 waits =~ 1 day, then 1 day at a time */
1743 dev->wait_sec = dev->min_wait;
1744 dev->rem_wait_sec = dev->wait_sec;
1747 dev->BadVolName[0] = 0;
1749 jcr->min_wait = 60 * 60;
1750 jcr->max_wait = 24 * 60 * 60;
1751 jcr->max_num_wait = 9; /* 5 waits =~ 1 day, then 1 day at a time */
1752 jcr->wait_sec = jcr->min_wait;
1753 jcr->rem_wait_sec = jcr->wait_sec;
1758 void init_jcr_device_wait_timers(JCR *jcr)
1760 /* ******FIXME******* put these on config variables */
1761 jcr->min_wait = 60 * 60;
1762 jcr->max_wait = 24 * 60 * 60;
1763 jcr->max_num_wait = 9; /* 5 waits =~ 1 day, then 1 day at a time */
1764 jcr->wait_sec = jcr->min_wait;
1765 jcr->rem_wait_sec = jcr->wait_sec;
1771 * The dev timers are used for waiting on a particular device
1773 * Returns: true if time doubled
1774 * false if max time expired
1776 bool double_dev_wait_time(DEVICE *dev)
1778 dev->wait_sec *= 2; /* double wait time */
1779 if (dev->wait_sec > dev->max_wait) { /* but not longer than maxtime */
1780 dev->wait_sec = dev->max_wait;
1783 dev->rem_wait_sec = dev->wait_sec;
1784 if (dev->num_wait >= dev->max_num_wait) {
1791 void set_os_device_parameters(DEVICE *dev)
1793 #ifdef HAVE_LINUX_OS
1795 if (dev->min_block_size == dev->max_block_size &&
1796 dev->min_block_size == 0) { /* variable block mode */
1797 mt_com.mt_op = MTSETBLK;
1798 mt_com.mt_count = 0;
1799 if (ioctl(dev->fd, MTIOCTOP, (char *)&mt_com) < 0) {
1800 clrerror_dev(dev, MTSETBLK);
1802 mt_com.mt_op = MTSETDRVBUFFER;
1803 mt_com.mt_count = MT_ST_CLEARBOOLEANS;
1804 if (!dev_cap(dev, CAP_TWOEOF)) {
1805 mt_com.mt_count |= MT_ST_TWO_FM;
1807 if (dev_cap(dev, CAP_EOM)) {
1808 mt_com.mt_count |= MT_ST_FAST_MTEOM;
1810 if (ioctl(dev->fd, MTIOCTOP, (char *)&mt_com) < 0) {
1811 clrerror_dev(dev, MTSETBLK);
1817 #ifdef HAVE_NETBSD_OS
1819 if (dev->min_block_size == dev->max_block_size &&
1820 dev->min_block_size == 0) { /* variable block mode */
1821 mt_com.mt_op = MTSETBSIZ;
1822 mt_com.mt_count = 0;
1823 if (ioctl(dev->fd, MTIOCTOP, (char *)&mt_com) < 0) {
1824 clrerror_dev(dev, MTSETBSIZ);
1826 /* Get notified at logical end of tape */
1827 mt_com.mt_op = MTEWARN;
1828 mt_com.mt_count = 1;
1829 if (ioctl(dev->fd, MTIOCTOP, (char *)&mt_com) < 0) {
1830 clrerror_dev(dev, MTEWARN);
1836 #if HAVE_FREEBSD_OS || HAVE_OPENBSD_OS
1838 if (dev->min_block_size == dev->max_block_size &&
1839 dev->min_block_size == 0) { /* variable block mode */
1840 mt_com.mt_op = MTSETBSIZ;
1841 mt_com.mt_count = 0;
1842 if (ioctl(dev->fd, MTIOCTOP, (char *)&mt_com) < 0) {
1843 clrerror_dev(dev, MTSETBSIZ);
1851 if (dev->min_block_size == dev->max_block_size &&
1852 dev->min_block_size == 0) { /* variable block mode */
1853 mt_com.mt_op = MTSRSZ;
1854 mt_com.mt_count = 0;
1855 if (ioctl(dev->fd, MTIOCTOP, (char *)&mt_com) < 0) {
1856 clrerror_dev(dev, MTSRSZ);
1863 static bool dev_get_os_pos(DEVICE *dev, struct mtget *mt_stat)
1865 return dev_cap(dev, CAP_MTIOCGET) &&
1866 ioctl(dev->fd, MTIOCGET, (char *)mt_stat) == 0 &&
1867 mt_stat->mt_fileno >= 0;