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 amended 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 update_free_space_dev(DEVICE* dev);
86 /* Forward referenced functions */
87 void set_os_device_parameters(DEVICE *dev);
88 static bool dev_get_os_pos(DEVICE *dev, struct mtget *mt_stat);
89 static void open_tape_device(DCR *dcr, int mode);
90 static void open_file_device(DCR *dcr, int mode);
91 static void open_dvd_device(DCR *dcr, int mode);
92 static char *mode_to_str(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\n"
133 " or have RequiresMount=yes for DVD. 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() && dev->requires_mount()) {
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 DEVICE::open(DCR *dcr, int mode)
271 if (openmode == mode) {
274 ::close(fd); /* use system close so correct mode will be used on open */
278 bstrncpy(VolCatInfo.VolCatName, dcr->VolumeName, sizeof(VolCatInfo.VolCatName));
281 Dmsg4(29, "open dev: tape=%d dev_name=%s vol=%s mode=%s\n", is_tape(),
282 dev_name, VolCatInfo.VolCatName, mode_to_str(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(dcr, mode);
287 } else if (is_dvd()) {
288 Dmsg1(100, "call open_dvd_device mode=%s\n", mode_to_str(mode));
289 open_dvd_device(dcr, mode);
291 Dmsg1(100, "call open_file_device mode=%d\n", mode_to_str(mode));
292 open_file_device(dcr, mode);
297 void DEVICE::set_mode(int new_mode)
300 case CREATE_READ_WRITE:
301 mode = O_CREAT | O_RDWR | O_BINARY;
303 case OPEN_READ_WRITE:
304 if (is_dvd() || is_file()) {
305 mode = O_CREAT | O_RDWR | O_BINARY;
307 mode = O_RDWR | O_BINARY;
311 mode = O_RDONLY | O_BINARY;
313 case OPEN_WRITE_ONLY:
314 mode = O_WRONLY | O_BINARY;
317 Emsg0(M_ABORT, 0, _("Illegal mode given to open dev.\n"));
321 static void open_tape_device(DCR *dcr, int mode)
323 DEVICE *dev = dcr->dev;
324 int nonblocking = 0;;
328 Dmsg0(29, "open dev: device is tape\n");
331 timeout = dev->max_open_wait;
333 if (dev->open_nowait) {
334 /* Set wait counters to zero for no wait */
335 timeout = ioerrcnt = 0;
336 /* Open drive in non-block mode */
337 nonblocking = O_NONBLOCK;
339 if (dev->is_fifo() && timeout) {
341 dev->tid = start_thread_timer(pthread_self(), timeout);
343 /* If busy retry each second for max_open_wait seconds */
345 Dmsg1(500, "Try open %s\n", dev->dev_name);
346 while ((dev->fd = open(dev->dev_name, dev->mode, MODE_RW+nonblocking)) < 0) {
348 Dmsg2(500, "Open error errno=%d ERR=%s\n", errno, be.strerror());
349 if (errno == EINTR || errno == EAGAIN) {
350 Dmsg0(500, "Continue open\n");
353 /* Busy wait for specified time (default = 5 mins) */
354 if (errno == EBUSY && timeout-- > 0) {
355 Dmsg2(100, "Device %s busy. ERR=%s\n", dev->print_name(), be.strerror());
359 /* IO error (no volume) try 10 times every 6 seconds */
360 if (errno == EIO && ioerrcnt-- > 0) {
362 Dmsg0(500, "Continue open\n");
365 dev->dev_errno = errno;
366 Mmsg2(dev->errmsg, _("Unable to open device %s: ERR=%s\n"),
367 dev->print_name(), be.strerror(dev->dev_errno));
368 /* Stop any open timer we set */
370 stop_thread_timer(dev->tid);
373 Emsg0(M_FATAL, 0, dev->errmsg);
377 /* If opened in non-block mode, close it an open it normally */
383 dev->openmode = mode; /* save open mode */
385 dev->state |= ST_OPENED;
387 update_pos_dev(dev); /* update position */
388 set_os_device_parameters(dev); /* do system dependent stuff */
389 Dmsg0(500, "Open OK\n");
391 /* Stop any open() timer we started */
393 stop_thread_timer(dev->tid);
396 Dmsg1(29, "open dev: tape %d opened\n", dev->fd);
402 static void open_file_device(DCR *dcr, int mode)
404 DEVICE *dev = dcr->dev;
405 POOL_MEM archive_name(PM_FNAME);
408 * Handle opening of File Archive (not a tape)
410 Dmsg3(29, "Enter: open_file_dev: %s dev=%s mode=%s\n", dev->is_dvd()?"DVD":"disk",
411 archive_name.c_str(), mode_to_str(mode));
413 if (dev->VolCatInfo.VolCatName[0] == 0) {
414 Mmsg(dev->errmsg, _("Could not open file device %s. No Volume name given.\n"),
420 pm_strcpy(archive_name, dev->dev_name);
421 if (archive_name.c_str()[strlen(archive_name.c_str())-1] != '/') {
422 pm_strcat(archive_name, "/");
424 pm_strcat(archive_name, dev->VolCatInfo.VolCatName);
426 Dmsg3(29, "open dev: %s dev=%s mode=%s\n", dev->is_dvd()?"DVD":"disk",
427 archive_name.c_str(), mode_to_str(mode));
428 dev->openmode = mode;
431 /* If creating file, give 0640 permissions */
432 Dmsg3(29, "mode=%s open(%s, 0x%x, 0640)\n", mode, archive_name.c_str(),
433 mode_to_str(dev->mode));
434 if ((dev->fd = open(archive_name.c_str(), dev->mode, 0640)) < 0) {
436 dev->dev_errno = errno;
437 Mmsg2(dev->errmsg, _("Could not open: %s, ERR=%s\n"), archive_name.c_str(),
439 Dmsg1(29, "open failed: %s", dev->errmsg);
440 Emsg0(M_FATAL, 0, dev->errmsg);
443 dev->state |= ST_OPENED;
445 update_pos_dev(dev); /* update position */
447 Dmsg5(29, "open dev: %s fd=%d opened, part=%d/%d, part_size=%u\n",
448 dev->is_dvd()?"DVD":"disk", dev->fd, dev->part, dev->num_parts,
453 * Open a DVD device. N.B. at this point, dcr->VolCatInfo.VolCatName
454 * has the desired Volume name, but there is NO assurance that
455 * any other field of VolCatInfo is correct.
457 static void open_dvd_device(DCR *dcr, int mode)
459 DEVICE *dev = dcr->dev;
460 POOL_MEM archive_name(PM_FNAME);
461 struct stat filestat;
464 * Handle opening of DVD Volume
466 Dmsg3(29, "Enter: open_dvd_dev: %s dev=%s mode=%s\n", dev->is_dvd()?"DVD":"disk",
467 archive_name.c_str(), mode_to_str(mode));
469 if (dev->VolCatInfo.VolCatName[0] == 0) {
470 Mmsg(dev->errmsg, _("Could not open file device %s. No Volume name given.\n"),
476 if (dev->part == 0) {
482 if (mount_dev(dev, 1) < 0) {
483 Mmsg(dev->errmsg, _("Could not mount device %s.\n"),
485 Emsg0(M_FATAL, 0, dev->errmsg);
490 Dmsg3(29, "open dev: %s dev=%s mode=%s\n", dev->is_dvd()?"DVD":"disk",
491 archive_name.c_str(), mode_to_str(mode));
492 dev->openmode = mode;
495 * If we are not trying to access the last part, set mode to
496 * OPEN_READ_ONLY as writing would be an error.
498 if (dev->part < dev->num_parts) {
499 mode = OPEN_READ_ONLY;
504 * If we are opening it read-only, it is *probably* on the
505 * DVD, so try the DVD first, otherwise look in the spool dir.
507 if (mode == OPEN_READ_ONLY) {
508 make_mounted_dvd_filename(dev, archive_name);
510 make_spooled_dvd_filename(dev, archive_name);
513 /* If creating file, give 0640 permissions */
514 Dmsg3(29, "mode=%s open(%s, 0x%x, 0640)\n", mode_to_str(mode),
515 archive_name.c_str(), dev->mode);
516 if ((dev->fd = open(archive_name.c_str(), dev->mode, 0640)) < 0) {
518 dev->dev_errno = errno;
519 Mmsg2(dev->errmsg, _("Could not open: %s, ERR=%s\n"), archive_name.c_str(),
521 Dmsg1(29, "open failed: %s", dev->errmsg);
522 if (mode == OPEN_READ_ONLY) {
523 make_spooled_dvd_filename(dev, archive_name);
524 dev->fd = open(archive_name.c_str(), dev->mode, 0640); /* try on spool */
528 /* Get size of file */
529 if (fstat(dev->fd, &filestat) < 0) {
531 dev->dev_errno = errno;
532 Mmsg2(dev->errmsg, _("Could not fstat: %s, ERR=%s\n"), archive_name.c_str(),
534 Dmsg1(29, "open failed: %s", dev->errmsg);
538 dev->part_size = filestat.st_size;
540 dev->state |= ST_OPENED;
542 update_pos_dev(dev); /* update position */
545 Dmsg4(29, "open dev: DVD fd=%d opened, part=%d/%d, part_size=%u\n",
546 dev->fd, dev->part, dev->num_parts, dev->part_size);
547 if (dev->is_open() && dev->is_dvd() && (mode != OPEN_READ_ONLY) &&
548 (dev->free_space_errno == 0 || dev->num_parts == dev->part)) {
549 update_free_space_dev(dev);
556 bool _rewind_dev(char *file, int line, DEVICE *dev)
558 Dmsg2(100, "rewind_dev called from %s:%d\n", file, line);
559 return rewind_dev(dev);
565 * Returns: true on success
568 bool rewind_dev(DEVICE *dev)
573 Dmsg1(29, "rewind_dev %s\n", dev->print_name());
575 dev->dev_errno = EBADF;
576 Mmsg1(dev->errmsg, _("Bad call to rewind_dev. Device %s not open\n"),
578 Emsg0(M_ABORT, 0, dev->errmsg);
581 dev->state &= ~(ST_EOT|ST_EOF|ST_WEOT); /* remove EOF/EOT flags */
582 dev->block_num = dev->file = 0;
585 if (dev->is_tape()) {
586 mt_com.mt_op = MTREW;
588 /* If we get an I/O error on rewind, it is probably because
589 * the drive is actually busy. We loop for (about 5 minutes)
590 * retrying every 5 seconds.
592 for (i=dev->max_rewind_wait; ; i -= 5) {
593 if (ioctl(dev->fd, MTIOCTOP, (char *)&mt_com) < 0) {
595 clrerror_dev(dev, MTREW);
596 if (i == dev->max_rewind_wait) {
597 Dmsg1(200, "Rewind error, %s. retrying ...\n", be.strerror());
599 if (dev->dev_errno == EIO && i > 0) {
600 Dmsg0(200, "Sleeping 5 seconds.\n");
604 Mmsg2(dev->errmsg, _("Rewind error on %s. ERR=%s.\n"),
605 dev->print_name(), be.strerror());
610 } else if (dev->is_file()) {
611 if (lseek_dev(dev, (off_t)0, SEEK_SET) < 0) {
613 dev->dev_errno = errno;
614 Mmsg2(dev->errmsg, _("lseek_dev error on %s. ERR=%s.\n"),
615 dev->print_name(), be.strerror());
622 void DEVICE::block(int why)
625 block_device(this, why);
629 void DEVICE::unblock()
632 unblock_device(this);
636 const char *DEVICE::print_blocked() const
638 switch (dev_blocked) {
639 case BST_NOT_BLOCKED:
640 return "BST_NOT_BLOCKED";
642 return "BST_UNMOUNTED";
643 case BST_WAITING_FOR_SYSOP:
644 return "BST_WAITING_FOR_SYSOP";
645 case BST_DOING_ACQUIRE:
646 return "BST_DOING_ACQUIRE";
647 case BST_WRITING_LABEL:
648 return "BST_WRITING_LABEL";
649 case BST_UNMOUNTED_WAITING_FOR_SYSOP:
650 return "BST_UNMOUNTED_WAITING_FOR_SYSOP";
654 return "unknown blocked code";
659 * Called to indicate that we have just read an
660 * EOF from the device.
662 void DEVICE::set_ateof()
672 * Called to indicate we are now at the end of the tape, and
673 * writing is not possible.
675 void DEVICE::set_ateot()
677 /* Make tape effectively read-only */
678 state |= (ST_EOF|ST_EOT|ST_WEOT);
683 * Position device to end of medium (end of data)
684 * Returns: true on succes
691 struct mtget mt_stat;
696 dev->dev_errno = EBADF;
697 Mmsg1(dev->errmsg, _("Bad call to eod_dev. Device %s not open\n"),
702 #if defined (__digital__) && defined (__unix__)
703 return dev->fsf(dev->VolCatInfo.VolCatFiles);
706 Dmsg0(29, "eod_dev\n");
710 dev->state &= ~(ST_EOF); /* remove EOF flags */
711 dev->block_num = dev->file = 0;
714 if (dev->state & (ST_FIFO | ST_PROG)) {
717 if (!dev->is_tape()) {
718 pos = lseek_dev(dev, (off_t)0, SEEK_END);
719 // Dmsg1(100, "====== Seek to %lld\n", pos);
722 dev->state |= ST_EOT;
725 dev->dev_errno = errno;
727 Mmsg2(dev->errmsg, _("lseek_dev error on %s. ERR=%s.\n"),
728 dev->print_name(), be.strerror());
732 if (dev_cap(dev, CAP_FASTFSF) && !dev_cap(dev, CAP_EOM)) {
733 Dmsg0(100,"Using FAST FSF for EOM\n");
734 /* If unknown position, rewind */
735 if (!dev_get_os_pos(dev, &mt_stat)) {
736 if (!rewind_dev(dev)) {
740 mt_com.mt_op = MTFSF;
742 * ***FIXME*** fix code to handle case that INT16_MAX is
745 mt_com.mt_count = INT16_MAX; /* use big positive number */
746 if (mt_com.mt_count < 0) {
747 mt_com.mt_count = INT16_MAX; /* brain damaged system */
751 if (dev_cap(dev, CAP_MTIOCGET) && (dev_cap(dev, CAP_FASTFSF) || dev_cap(dev, CAP_EOM))) {
752 if (dev_cap(dev, CAP_EOM)) {
753 Dmsg0(100,"Using EOM for EOM\n");
754 mt_com.mt_op = MTEOM;
758 if (ioctl(dev->fd, MTIOCTOP, (char *)&mt_com) < 0) {
760 clrerror_dev(dev, mt_com.mt_op);
761 Dmsg1(50, "ioctl error: %s\n", be.strerror());
763 Mmsg2(dev->errmsg, _("ioctl MTEOM error on %s. ERR=%s.\n"),
764 dev->print_name(), be.strerror());
768 if (!dev_get_os_pos(dev, &mt_stat)) {
770 clrerror_dev(dev, -1);
771 Mmsg2(dev->errmsg, _("ioctl MTIOCGET error on %s. ERR=%s.\n"),
772 dev->print_name(), be.strerror());
775 Dmsg2(100, "EOD file=%d block=%d\n", mt_stat.mt_fileno, mt_stat.mt_blkno);
777 dev->file = mt_stat.mt_fileno;
783 * Rewind then use FSF until EOT reached
785 if (!rewind_dev(dev)) {
789 * Move file by file to the end of the tape
792 for (file_num=dev->file; !dev->at_eot(); file_num++) {
793 Dmsg0(200, "eod_dev: doing fsf 1\n");
795 Dmsg0(200, "fsf error.\n");
799 * Avoid infinite loop. ***FIXME*** possibly add code
800 * to set EOD or to turn off CAP_FASTFSF if on.
802 if (file_num == (int)dev->file) {
803 struct mtget mt_stat;
804 Dmsg1(100, "fsf did not advance from file %d\n", file_num);
805 if (dev_get_os_pos(dev, &mt_stat)) {
806 Dmsg2(100, "Adjust file from %d to %d\n", dev->file , mt_stat.mt_fileno);
808 dev->file = mt_stat.mt_fileno;
815 * Some drivers leave us after second EOF when doing
816 * MTEOM, so we must backup so that appending overwrites
819 if (dev_cap(dev, CAP_BSFATEOM)) {
820 struct mtget mt_stat;
821 /* Backup over EOF */
822 ok = bsf_dev(dev, 1);
823 /* If BSF worked and fileno is known (not -1), set file */
824 if (dev_get_os_pos(dev, &mt_stat)) {
825 Dmsg2(100, "BSFATEOF adjust file from %d to %d\n", dev->file , mt_stat.mt_fileno);
826 dev->file = mt_stat.mt_fileno;
828 dev->file++; /* wing it -- not correct on all OSes */
831 update_pos_dev(dev); /* update position */
833 Dmsg1(200, "EOD dev->file=%d\n", dev->file);
838 * Set the position of the device -- only for files
839 * For other devices, there is no generic way to do it.
840 * Returns: true on succes
843 bool update_pos_dev(DEVICE *dev)
849 dev->dev_errno = EBADF;
850 Mmsg0(dev->errmsg, _("Bad device call. Device not open\n"));
851 Emsg0(M_FATAL, 0, dev->errmsg);
855 /* Find out where we are */
856 if (dev->is_file()) {
859 pos = lseek_dev(dev, (off_t)0, SEEK_CUR);
862 dev->dev_errno = errno;
863 Pmsg1(000, "Seek error: ERR=%s\n", be.strerror());
864 Mmsg2(dev->errmsg, _("lseek_dev error on %s. ERR=%s.\n"),
865 dev->print_name(), be.strerror());
868 dev->file_addr = pos;
876 * Return the status of the device. This was meant
877 * to be a generic routine. Unfortunately, it doesn't
878 * seem possible (at least I do not know how to do it
879 * currently), which means that for the moment, this
880 * routine has very little value.
884 uint32_t status_dev(DEVICE *dev)
886 struct mtget mt_stat;
889 if (dev->state & (ST_EOT | ST_WEOT)) {
893 if (dev->state & ST_EOF) {
897 if (dev->is_tape()) {
899 Pmsg0(-20," Bacula status:");
900 Pmsg2(-20," file=%d block=%d\n", dev->file, dev->block_num);
901 if (ioctl(dev->fd, MTIOCGET, (char *)&mt_stat) < 0) {
903 dev->dev_errno = errno;
904 Mmsg2(dev->errmsg, _("ioctl MTIOCGET error on %s. ERR=%s.\n"),
905 dev->print_name(), be.strerror());
908 Pmsg0(-20, " Device status:");
910 #if defined(HAVE_LINUX_OS)
911 if (GMT_EOF(mt_stat.mt_gstat)) {
915 if (GMT_BOT(mt_stat.mt_gstat)) {
919 if (GMT_EOT(mt_stat.mt_gstat)) {
923 if (GMT_SM(mt_stat.mt_gstat)) {
927 if (GMT_EOD(mt_stat.mt_gstat)) {
931 if (GMT_WR_PROT(mt_stat.mt_gstat)) {
933 Pmsg0(-20, " WR_PROT");
935 if (GMT_ONLINE(mt_stat.mt_gstat)) {
937 Pmsg0(-20, " ONLINE");
939 if (GMT_DR_OPEN(mt_stat.mt_gstat)) {
941 Pmsg0(-20, " DR_OPEN");
943 if (GMT_IM_REP_EN(mt_stat.mt_gstat)) {
944 stat |= BMT_IM_REP_EN;
945 Pmsg0(-20, " IM_REP_EN");
947 #endif /* !SunOS && !OSF */
948 if (dev_cap(dev, CAP_MTIOCGET)) {
949 Pmsg2(-20, " file=%d block=%d\n", mt_stat.mt_fileno, mt_stat.mt_blkno);
951 Pmsg2(-20, " file=%d block=%d\n", -1, -1);
954 stat |= BMT_ONLINE | BMT_BOT;
961 * Load medium in device
962 * Returns: true on success
965 bool load_dev(DEVICE *dev)
972 dev->dev_errno = EBADF;
973 Mmsg0(dev->errmsg, _("Bad call to load_dev. Device not open\n"));
974 Emsg0(M_FATAL, 0, dev->errmsg);
977 if (!(dev->is_tape())) {
981 Dmsg0(200, "stored: MTLOAD command not available\n");
983 dev->dev_errno = ENOTTY; /* function not available */
984 Mmsg2(dev->errmsg, _("ioctl MTLOAD error on %s. ERR=%s.\n"),
985 dev->print_name(), be.strerror());
989 dev->block_num = dev->file = 0;
992 mt_com.mt_op = MTLOAD;
994 if (ioctl(dev->fd, MTIOCTOP, (char *)&mt_com) < 0) {
996 dev->dev_errno = errno;
997 Mmsg2(dev->errmsg, _("ioctl MTLOAD error on %s. ERR=%s.\n"),
998 dev->print_name(), be.strerror());
1006 * Rewind device and put it offline
1007 * Returns: true on success
1010 bool offline_dev(DEVICE *dev)
1015 dev->dev_errno = EBADF;
1016 Mmsg0(dev->errmsg, _("Bad call to offline_dev. Device not open\n"));
1017 Emsg0(M_FATAL, 0, dev->errmsg);
1020 if (!(dev->is_tape())) {
1024 dev->state &= ~(ST_APPEND|ST_READ|ST_EOT|ST_EOF|ST_WEOT); /* remove EOF/EOT flags */
1025 dev->block_num = dev->file = 0;
1030 mt_com.mt_op = MTUNLOCK;
1031 mt_com.mt_count = 1;
1032 ioctl(dev->fd, MTIOCTOP, (char *)&mt_com);
1034 mt_com.mt_op = MTOFFL;
1035 mt_com.mt_count = 1;
1036 if (ioctl(dev->fd, MTIOCTOP, (char *)&mt_com) < 0) {
1038 dev->dev_errno = errno;
1039 Mmsg2(dev->errmsg, _("ioctl MTOFFL error on %s. ERR=%s.\n"),
1040 dev->print_name(), be.strerror());
1043 Dmsg1(100, "Offlined device %s\n", dev->print_name());
1047 bool offline_or_rewind_dev(DEVICE *dev)
1052 if (dev_cap(dev, CAP_OFFLINEUNMOUNT)) {
1053 return offline_dev(dev);
1056 * Note, this rewind probably should not be here (it wasn't
1057 * in prior versions of Bacula), but on FreeBSD, this is
1058 * needed in the case the tape was "frozen" due to an error
1059 * such as backspacing after writing and EOF. If it is not
1060 * done, all future references to the drive get and I/O error.
1062 clrerror_dev(dev, MTREW);
1063 return rewind_dev(dev);
1068 * Foward space a file
1069 * Returns: true on success
1072 bool DEVICE::fsf(int num)
1074 struct mtget mt_stat;
1080 Mmsg0(errmsg, _("Bad call to fsf_dev. Device not open\n"));
1081 Emsg0(M_FATAL, 0, errmsg);
1090 Mmsg1(errmsg, _("Device %s at End of Tape.\n"), print_name());
1094 Dmsg0(200, "ST_EOF set on entry to FSF\n");
1097 Dmsg0(100, "fsf\n");
1100 * If Fast forward space file is set, then we
1101 * use MTFSF to forward space and MTIOCGET
1102 * to get the file position. We assume that
1103 * the SCSI driver will ensure that we do not
1104 * forward space past the end of the medium.
1106 if (dev_cap(this, CAP_FSF) && dev_cap(this, CAP_MTIOCGET) && dev_cap(this, CAP_FASTFSF)) {
1107 mt_com.mt_op = MTFSF;
1108 mt_com.mt_count = num;
1109 stat = ioctl(fd, MTIOCTOP, (char *)&mt_com);
1110 if (stat < 0 || !dev_get_os_pos(this, &mt_stat)) {
1113 Dmsg0(200, "Set ST_EOT\n");
1114 clrerror_dev(this, MTFSF);
1115 Mmsg2(errmsg, _("ioctl MTFSF error on %s. ERR=%s.\n"),
1116 print_name(), be.strerror());
1117 Dmsg1(200, "%s", errmsg);
1120 Dmsg2(200, "fsf file=%d block=%d\n", mt_stat.mt_fileno, mt_stat.mt_blkno);
1122 file = mt_stat.mt_fileno;
1126 * Here if CAP_FSF is set, and virtually all drives
1127 * these days support it, we read a record, then forward
1128 * space one file. Using this procedure, which is slow,
1129 * is the only way we can be sure that we don't read
1130 * two consecutive EOF marks, which means End of Data.
1132 } else if (dev_cap(this, CAP_FSF)) {
1135 Dmsg0(200, "FSF has cap_fsf\n");
1136 if (max_block_size == 0) {
1137 rbuf_len = DEFAULT_BLOCK_SIZE;
1139 rbuf_len = max_block_size;
1141 rbuf = get_memory(rbuf_len);
1142 mt_com.mt_op = MTFSF;
1143 mt_com.mt_count = 1;
1144 while (num-- && !at_eot()) {
1145 Dmsg0(100, "Doing read before fsf\n");
1146 if ((stat = read(fd, (char *)rbuf, rbuf_len)) < 0) {
1147 if (errno == ENOMEM) { /* tape record exceeds buf len */
1148 stat = rbuf_len; /* This is OK */
1150 * On IBM drives, they return ENOSPC at EOM
1151 * instead of EOF status
1153 } else if (at_eof() && errno == ENOSPC) {
1158 clrerror_dev(this, -1);
1159 Dmsg2(100, "Set ST_EOT read errno=%d. ERR=%s\n", dev_errno,
1161 Mmsg2(errmsg, _("read error on %s. ERR=%s.\n"),
1162 print_name(), be.strerror());
1163 Dmsg1(100, "%s", errmsg);
1167 if (stat == 0) { /* EOF */
1168 update_pos_dev(this);
1169 Dmsg1(100, "End of File mark from read. File=%d\n", file+1);
1170 /* Two reads of zero means end of tape */
1173 Dmsg0(100, "Set ST_EOT\n");
1179 } else { /* Got data */
1184 Dmsg0(100, "Doing MTFSF\n");
1185 stat = ioctl(fd, MTIOCTOP, (char *)&mt_com);
1186 if (stat < 0) { /* error => EOT */
1189 Dmsg0(100, "Set ST_EOT\n");
1190 clrerror_dev(this, MTFSF);
1191 Mmsg2(errmsg, _("ioctl MTFSF error on %s. ERR=%s.\n"),
1192 print_name(), be.strerror());
1193 Dmsg0(100, "Got < 0 for MTFSF\n");
1194 Dmsg1(100, "%s", errmsg);
1202 * No FSF, so use FSR to simulate it
1205 Dmsg0(200, "Doing FSR for FSF\n");
1206 while (num-- && !at_eot()) {
1207 fsr(INT32_MAX); /* returns -1 on EOF or EOT */
1211 Mmsg1(errmsg, _("Device %s at End of Tape.\n"), print_name());
1217 update_pos_dev(this);
1218 Dmsg1(200, "Return %d from FSF\n", stat);
1220 Dmsg0(200, "ST_EOF set on exit FSF\n");
1222 Dmsg0(200, "ST_EOT set on exit FSF\n");
1223 Dmsg1(200, "Return from FSF file=%d\n", file);
1228 * Backward space a file
1229 * Returns: false on failure
1233 bsf_dev(DEVICE *dev, int num)
1239 dev->dev_errno = EBADF;
1240 Mmsg0(dev->errmsg, _("Bad call to bsf_dev. Device not open\n"));
1241 Emsg0(M_FATAL, 0, dev->errmsg);
1245 if (!dev->is_tape()) {
1246 Mmsg1(dev->errmsg, _("Device %s cannot BSF because it is not a tape.\n"),
1250 Dmsg0(29, "bsf_dev\n");
1251 dev->state &= ~(ST_EOT|ST_EOF);
1255 mt_com.mt_op = MTBSF;
1256 mt_com.mt_count = num;
1257 stat = ioctl(dev->fd, MTIOCTOP, (char *)&mt_com);
1260 clrerror_dev(dev, MTBSF);
1261 Mmsg2(dev->errmsg, _("ioctl MTBSF error on %s. ERR=%s.\n"),
1262 dev->print_name(), be.strerror());
1264 update_pos_dev(dev);
1270 * Foward space num records
1271 * Returns: false on failure
1274 bool DEVICE::fsr(int num)
1281 Mmsg0(errmsg, _("Bad call to fsr. Device not open\n"));
1282 Emsg0(M_FATAL, 0, errmsg);
1289 if (!dev_cap(this, CAP_FSR)) {
1290 Mmsg1(errmsg, _("ioctl MTFSR not permitted on %s.\n"), print_name());
1294 Dmsg1(29, "fsr %d\n", num);
1295 mt_com.mt_op = MTFSR;
1296 mt_com.mt_count = num;
1297 stat = ioctl(fd, MTIOCTOP, (char *)&mt_com);
1303 struct mtget mt_stat;
1304 clrerror_dev(this, MTFSR);
1305 Dmsg1(100, "FSF fail: ERR=%s\n", be.strerror());
1306 if (dev_get_os_pos(this, &mt_stat)) {
1307 Dmsg4(100, "Adjust from %d:%d to %d:%d\n", file,
1308 block_num, mt_stat.mt_fileno, mt_stat.mt_blkno);
1309 file = mt_stat.mt_fileno;
1310 block_num = mt_stat.mt_blkno;
1318 Mmsg3(errmsg, _("ioctl MTFSR %d error on %s. ERR=%s.\n"),
1319 num, print_name(), be.strerror());
1321 update_pos_dev(this);
1326 * Backward space a record
1327 * Returns: false on failure
1331 bsr_dev(DEVICE *dev, int num)
1337 dev->dev_errno = EBADF;
1338 Mmsg0(dev->errmsg, _("Bad call to bsr_dev. Device not open\n"));
1339 Emsg0(M_FATAL, 0, dev->errmsg);
1343 if (!dev->is_tape()) {
1347 if (!dev_cap(dev, CAP_BSR)) {
1348 Mmsg1(dev->errmsg, _("ioctl MTBSR not permitted on %s.\n"), dev->print_name());
1352 Dmsg0(29, "bsr_dev\n");
1353 dev->block_num -= num;
1354 dev->state &= ~(ST_EOF|ST_EOT|ST_EOF);
1355 mt_com.mt_op = MTBSR;
1356 mt_com.mt_count = num;
1357 stat = ioctl(dev->fd, MTIOCTOP, (char *)&mt_com);
1360 clrerror_dev(dev, MTBSR);
1361 Mmsg2(dev->errmsg, _("ioctl MTBSR error on %s. ERR=%s.\n"),
1362 dev->print_name(), be.strerror());
1364 update_pos_dev(dev);
1369 * Reposition the device to file, block
1370 * Returns: false on failure
1374 reposition_dev(DEVICE *dev, uint32_t file, uint32_t block)
1377 dev->dev_errno = EBADF;
1378 Mmsg0(dev->errmsg, _("Bad call to reposition_dev. Device not open\n"));
1379 Emsg0(M_FATAL, 0, dev->errmsg);
1383 if (!dev->is_tape()) {
1384 off_t pos = (((off_t)file)<<32) + block;
1385 Dmsg1(100, "===== lseek_dev to %d\n", (int)pos);
1386 if (lseek_dev(dev, pos, SEEK_SET) == (off_t)-1) {
1388 dev->dev_errno = errno;
1389 Mmsg2(dev->errmsg, _("lseek_dev error on %s. ERR=%s.\n"),
1390 dev->print_name(), be.strerror());
1394 dev->block_num = block;
1395 dev->file_addr = pos;
1398 Dmsg4(100, "reposition_dev from %u:%u to %u:%u\n",
1399 dev->file, dev->block_num, file, block);
1400 if (file < dev->file) {
1401 Dmsg0(100, "Rewind_dev\n");
1402 if (!rewind_dev(dev)) {
1406 if (file > dev->file) {
1407 Dmsg1(100, "fsf %d\n", file-dev->file);
1408 if (!dev->fsf(file-dev->file)) {
1409 Dmsg1(100, "fsf failed! ERR=%s\n", strerror_dev(dev));
1412 Dmsg2(100, "wanted_file=%d at_file=%d\n", file, dev->file);
1414 if (block < dev->block_num) {
1415 Dmsg2(100, "wanted_blk=%d at_blk=%d\n", block, dev->block_num);
1416 Dmsg0(100, "bsf_dev 1\n");
1418 Dmsg0(100, "fsf_dev 1\n");
1420 Dmsg2(100, "wanted_blk=%d at_blk=%d\n", block, dev->block_num);
1422 if (dev_cap(dev, CAP_POSITIONBLOCKS) && block > dev->block_num) {
1423 /* Ignore errors as Bacula can read to the correct block */
1424 Dmsg1(100, "fsr %d\n", block-dev->block_num);
1425 return dev->fsr(block-dev->block_num);
1433 * Write an end of file on the device
1434 * Returns: 0 on success
1435 * non-zero on failure
1438 weof_dev(DEVICE *dev, int num)
1442 Dmsg0(29, "weof_dev\n");
1445 dev->dev_errno = EBADF;
1446 Mmsg0(dev->errmsg, _("Bad call to weof_dev. Device not open\n"));
1447 Emsg0(M_FATAL, 0, dev->errmsg);
1452 if (!dev->is_tape()) {
1455 if (!dev->can_append()) {
1456 Mmsg0(dev->errmsg, _("Attempt to WEOF on non-appendable Volume\n"));
1457 Emsg0(M_FATAL, 0, dev->errmsg);
1461 dev->state &= ~(ST_EOT | ST_EOF); /* remove EOF/EOT flags */
1462 mt_com.mt_op = MTWEOF;
1463 mt_com.mt_count = num;
1464 stat = ioctl(dev->fd, MTIOCTOP, (char *)&mt_com);
1471 clrerror_dev(dev, MTWEOF);
1473 Mmsg2(dev->errmsg, _("ioctl MTWEOF error on %s. ERR=%s.\n"),
1474 dev->print_name(), be.strerror());
1481 * Return string message with last error in English
1482 * Be careful not to call this routine from within dev.c
1483 * while editing an Mmsg() or you will end up in a recursive
1484 * loop creating a Segmentation Violation.
1487 strerror_dev(DEVICE *dev)
1494 * If implemented in system, clear the tape
1498 clrerror_dev(DEVICE *dev, int func)
1500 const char *msg = NULL;
1501 struct mtget mt_stat;
1504 dev->dev_errno = errno; /* save errno */
1506 dev->VolCatInfo.VolCatErrors++;
1509 if (!dev->is_tape()) {
1512 if (errno == ENOTTY || errno == ENOSYS) { /* Function not implemented */
1515 Emsg0(M_ABORT, 0, "Got ENOTTY on read/write!\n");
1519 dev->capabilities &= ~CAP_EOF; /* turn off feature */
1524 dev->capabilities &= ~CAP_EOM; /* turn off feature */
1529 dev->capabilities &= ~CAP_FSF; /* turn off feature */
1533 dev->capabilities &= ~CAP_BSF; /* turn off feature */
1537 dev->capabilities &= ~CAP_FSR; /* turn off feature */
1541 dev->capabilities &= ~CAP_BSR; /* turn off feature */
1562 bsnprintf(buf, sizeof(buf), "unknown func code %d", func);
1567 dev->dev_errno = ENOSYS;
1568 Mmsg1(dev->errmsg, _("I/O function \"%s\" not supported on this device.\n"), msg);
1569 Emsg0(M_ERROR, 0, dev->errmsg);
1572 /* On some systems such as NetBSD, this clears all errors */
1573 ioctl(dev->fd, MTIOCGET, (char *)&mt_stat);
1575 /* Found on Linux */
1579 mt_com.mt_op = MTIOCLRERR;
1580 mt_com.mt_count = 1;
1581 /* Clear any error condition on the tape */
1582 ioctl(dev->fd, MTIOCTOP, (char *)&mt_com);
1583 Dmsg0(200, "Did MTIOCLRERR\n");
1587 /* Typically on FreeBSD */
1590 /* Read and clear SCSI error status */
1591 union mterrstat mt_errstat;
1592 Dmsg2(200, "Doing MTIOCERRSTAT errno=%d ERR=%s\n", dev->dev_errno,
1593 strerror(dev->dev_errno));
1594 ioctl(dev->fd, MTIOCERRSTAT, (char *)&mt_errstat);
1598 /* Clear Subsystem Exception OSF1 */
1602 mt_com.mt_op = MTCSE;
1603 mt_com.mt_count = 1;
1604 /* Clear any error condition on the tape */
1605 ioctl(dev->fd, MTIOCTOP, (char *)&mt_com);
1606 Dmsg0(200, "Did MTCSE\n");
1612 * Flush buffer contents
1615 int flush_dev(DEVICE *dev)
1620 static void do_close(DEVICE *dev)
1623 Dmsg1(29, "really close_dev %s\n", dev->print_name());
1628 if (unmount_dev(dev, 1) < 0) {
1629 Dmsg1(0, "Cannot unmount device %s.\n", dev->print_name());
1632 /* Remove the last part file if it is empty */
1633 if (dev->can_append() && (dev->num_parts > 0)) {
1635 POOL_MEM archive_name(PM_FNAME);
1636 dev->part = dev->num_parts;
1637 Dmsg1(100, "Call make_dvd_filename. Vol=%s\n", dev->VolCatInfo.VolCatName);
1638 make_spooled_dvd_filename(dev, archive_name);
1639 /* Check that the part file is empty */
1640 if ((stat(archive_name.c_str(), &statp) == 0) && (statp.st_size == 0)) {
1641 Dmsg1(100, "unlink(%s)\n", archive_name.c_str());
1642 unlink(archive_name.c_str());
1646 /* Clean up device packet so it can be reused */
1648 dev->state &= ~(ST_OPENED|ST_LABEL|ST_READ|ST_APPEND|ST_EOT|ST_WEOT|ST_EOF);
1649 dev->label_type = B_BACULA_LABEL;
1650 dev->file = dev->block_num = 0;
1655 dev->part_start = 0;
1656 dev->EndFile = dev->EndBlock = 0;
1657 memset(&dev->VolCatInfo, 0, sizeof(dev->VolCatInfo));
1658 memset(&dev->VolHdr, 0, sizeof(dev->VolHdr));
1660 stop_thread_timer(dev->tid);
1670 void DEVICE::close()
1672 /*if (fd >= 0 && use_count == 1) {*/
1673 /* No need to check if fd >= 0: it is checked again
1674 * in do_close, and do_close MUST be called for volumes
1675 * splitted in parts, even if fd == -1. */
1676 if (use_count == 1) {
1678 } else if (use_count > 0) {
1683 ASSERT(use_count >= 0);
1688 bool truncate_dev(DEVICE *dev)
1690 Dmsg1(100, "truncate_dev %s\n", dev->print_name());
1691 if (dev->is_tape()) {
1692 return true; /* we don't really truncate tapes */
1693 /* maybe we should rewind and write and eof ???? */
1696 if (dev->is_dvd()) {
1697 Mmsg1(dev->errmsg, _("Truncate DVD %s not supported.\n"), dev->print_name());
1698 return false; /* we cannot truncate DVDs */
1701 if (ftruncate(dev->fd, 0) != 0) {
1703 Mmsg2(dev->errmsg, _("Unable to truncate device %s. ERR=%s\n"),
1704 dev->print_name(), be.strerror());
1710 /* Return the resource name for the device */
1711 const char *DEVICE::name() const
1713 return device->hdr.name;
1717 dev_vol_name(DEVICE *dev)
1719 return dev->VolCatInfo.VolCatName;
1722 uint32_t dev_block(DEVICE *dev)
1724 update_pos_dev(dev);
1725 return dev->block_num;
1728 uint32_t dev_file(DEVICE *dev)
1730 update_pos_dev(dev);
1735 * Free memory allocated for the device
1738 term_dev(DEVICE *dev)
1741 dev->dev_errno = EBADF;
1742 Mmsg0(dev->errmsg, _("Bad call to term_dev. Device not open\n"));
1743 Emsg0(M_FATAL, 0, dev->errmsg);
1746 Dmsg1(29, "term_dev: %s\n", dev->print_name());
1748 if (dev->dev_name) {
1749 free_memory(dev->dev_name);
1750 dev->dev_name = NULL;
1752 if (dev->prt_name) {
1753 free_memory(dev->prt_name);
1754 dev->prt_name = NULL;
1757 free_pool_memory(dev->errmsg);
1760 pthread_mutex_destroy(&dev->mutex);
1761 pthread_cond_destroy(&dev->wait);
1762 pthread_cond_destroy(&dev->wait_next_vol);
1763 pthread_mutex_destroy(&dev->spool_mutex);
1764 rwl_destroy(&dev->lock);
1765 if (dev->attached_dcrs) {
1766 delete dev->attached_dcrs;
1767 dev->attached_dcrs = NULL;
1769 if (dev->state & ST_MALLOC) {
1770 free_pool_memory((POOLMEM *)dev);
1775 * This routine initializes the device wait timers
1777 void init_device_wait_timers(DCR *dcr)
1779 DEVICE *dev = dcr->dev;
1780 JCR *jcr = dcr->jcr;
1782 /* ******FIXME******* put these on config variables */
1783 dev->min_wait = 60 * 60;
1784 dev->max_wait = 24 * 60 * 60;
1785 dev->max_num_wait = 9; /* 5 waits =~ 1 day, then 1 day at a time */
1786 dev->wait_sec = dev->min_wait;
1787 dev->rem_wait_sec = dev->wait_sec;
1790 dev->BadVolName[0] = 0;
1792 jcr->min_wait = 60 * 60;
1793 jcr->max_wait = 24 * 60 * 60;
1794 jcr->max_num_wait = 9; /* 5 waits =~ 1 day, then 1 day at a time */
1795 jcr->wait_sec = jcr->min_wait;
1796 jcr->rem_wait_sec = jcr->wait_sec;
1801 void init_jcr_device_wait_timers(JCR *jcr)
1803 /* ******FIXME******* put these on config variables */
1804 jcr->min_wait = 60 * 60;
1805 jcr->max_wait = 24 * 60 * 60;
1806 jcr->max_num_wait = 9; /* 5 waits =~ 1 day, then 1 day at a time */
1807 jcr->wait_sec = jcr->min_wait;
1808 jcr->rem_wait_sec = jcr->wait_sec;
1814 * The dev timers are used for waiting on a particular device
1816 * Returns: true if time doubled
1817 * false if max time expired
1819 bool double_dev_wait_time(DEVICE *dev)
1821 dev->wait_sec *= 2; /* double wait time */
1822 if (dev->wait_sec > dev->max_wait) { /* but not longer than maxtime */
1823 dev->wait_sec = dev->max_wait;
1826 dev->rem_wait_sec = dev->wait_sec;
1827 if (dev->num_wait >= dev->max_num_wait) {
1834 void set_os_device_parameters(DEVICE *dev)
1836 #ifdef HAVE_LINUX_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 = MTSETBLK;
1841 mt_com.mt_count = 0;
1842 if (ioctl(dev->fd, MTIOCTOP, (char *)&mt_com) < 0) {
1843 clrerror_dev(dev, MTSETBLK);
1845 mt_com.mt_op = MTSETDRVBUFFER;
1846 mt_com.mt_count = MT_ST_CLEARBOOLEANS;
1847 if (!dev_cap(dev, CAP_TWOEOF)) {
1848 mt_com.mt_count |= MT_ST_TWO_FM;
1850 if (dev_cap(dev, CAP_EOM)) {
1851 mt_com.mt_count |= MT_ST_FAST_MTEOM;
1853 if (ioctl(dev->fd, MTIOCTOP, (char *)&mt_com) < 0) {
1854 clrerror_dev(dev, MTSETBLK);
1860 #ifdef HAVE_NETBSD_OS
1862 if (dev->min_block_size == dev->max_block_size &&
1863 dev->min_block_size == 0) { /* variable block mode */
1864 mt_com.mt_op = MTSETBSIZ;
1865 mt_com.mt_count = 0;
1866 if (ioctl(dev->fd, MTIOCTOP, (char *)&mt_com) < 0) {
1867 clrerror_dev(dev, MTSETBSIZ);
1869 /* Get notified at logical end of tape */
1870 mt_com.mt_op = MTEWARN;
1871 mt_com.mt_count = 1;
1872 if (ioctl(dev->fd, MTIOCTOP, (char *)&mt_com) < 0) {
1873 clrerror_dev(dev, MTEWARN);
1879 #if HAVE_FREEBSD_OS || HAVE_OPENBSD_OS
1881 if (dev->min_block_size == dev->max_block_size &&
1882 dev->min_block_size == 0) { /* variable block mode */
1883 mt_com.mt_op = MTSETBSIZ;
1884 mt_com.mt_count = 0;
1885 if (ioctl(dev->fd, MTIOCTOP, (char *)&mt_com) < 0) {
1886 clrerror_dev(dev, MTSETBSIZ);
1894 if (dev->min_block_size == dev->max_block_size &&
1895 dev->min_block_size == 0) { /* variable block mode */
1896 mt_com.mt_op = MTSRSZ;
1897 mt_com.mt_count = 0;
1898 if (ioctl(dev->fd, MTIOCTOP, (char *)&mt_com) < 0) {
1899 clrerror_dev(dev, MTSRSZ);
1906 static bool dev_get_os_pos(DEVICE *dev, struct mtget *mt_stat)
1908 return dev_cap(dev, CAP_MTIOCGET) &&
1909 ioctl(dev->fd, MTIOCGET, (char *)mt_stat) == 0 &&
1910 mt_stat->mt_fileno >= 0;
1913 static char *modes[] = {
1914 "CREATE_READ_WRITE",
1921 static char *mode_to_str(int mode)
1923 return modes[mode-1];