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 contored than is
26 * really necessary -- KES. Note, this contortion has been
27 * corrected to a large extent by a rewrite (Apr MMI).
30 Copyright (C) 2000, 2001, 2002 Kern Sibbald and John Walker
32 This program is free software; you can redistribute it and/or
33 modify it under the terms of the GNU General Public License as
34 published by the Free Software Foundation; either version 2 of
35 the License, or (at your option) any later version.
37 This program is distributed in the hope that it will be useful,
38 but WITHOUT ANY WARRANTY; without even the implied warranty of
39 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
40 General Public License for more details.
42 You should have received a copy of the GNU General Public
43 License along with this program; if not, write to the Free
44 Software Foundation, Inc., 59 Temple Place - Suite 330, Boston,
50 * Handling I/O errors and end of tape conditions is a bit tricky.
51 * This is how it is currently done when writting.
52 * On either an I/O error or end of tape,
53 * we will stop writing on the physical device (no I/O recovery is
54 * attempted at least in this daemon). The state flag will be sent
55 * to include ST_EOT, which is ephimeral, and ST_WEOT, which is
56 * persistent. Lots of routines clear ST_EOT, but ST_WEOT is
57 * cleared only when the problem goes away. Now when ST_WEOT
58 * is set all calls to write_dev() are handled as usual. However,
59 * in write_block() instead of attempting to write the block to
60 * the physical device, it is chained into a list of blocks written
61 * after the EOT condition. In addition, all threads are blocked
62 * from writing on the tape by calling lock(), and thread other
63 * than the first thread to hit the EOT will block on a condition
64 * variable. The first thread to hit the EOT will continue to
65 * be able to read and write the tape (he sort of tunnels through
66 * the locking mechanism -- see lock() for details).
68 * Now presumably somewhere higher in the chain of command
69 * (device.c), someone will notice the EOT condition and
70 * get a new tape up, get the tape label read, and mark
71 * the label for rewriting. Then this higher level routine
72 * will write the unwritten buffer to the new volume.
73 * Finally, he will release
74 * any blocked threads by doing a broadcast on the condition
75 * variable. At that point, we should be totally back in
76 * business with no lost data.
83 /* Forward referenced functions */
84 int dev_is_tape(DEVICE *dev);
85 void clrerror_dev(DEVICE *dev, int func);
86 int fsr_dev(DEVICE *dev, int num);
88 extern int debug_level;
91 * Allocate and initialize the DEVICE structure
92 * Note, if dev is non-NULL, it is already allocated,
93 * thus we neither allocate it nor free it. This allows
94 * the caller to put the packet in shared memory.
96 * Note, for a tape, the dev_name is the device name
97 * (e.g. /dev/nst0), and for a file, the device name
98 * is the directory in which the file will be placed.
102 init_dev(DEVICE *dev, char *dev_name)
108 /* Check that device is available */
109 if (stat(dev_name, &statp) < 0) {
111 dev->dev_errno = errno;
113 Emsg2(M_FATAL, 0, "Unable to stat device %s : %s\n", dev_name, strerror(errno));
117 if (S_ISDIR(statp.st_mode)) {
119 } else if (S_ISCHR(statp.st_mode)) {
123 dev->dev_errno = ENODEV;
125 Emsg2(M_FATAL, 0, "%s is an unknown device type. Must be tape or directory. st_mode=%x\n",
126 dev_name, statp.st_mode);
130 dev = (DEVICE *)get_memory(sizeof(DEVICE));
131 memset(dev, 0, sizeof(DEVICE));
132 dev->state = ST_MALLOC;
134 memset(dev, 0, sizeof(DEVICE));
137 dev->state |= ST_TAPE;
139 dev->dev_name = (char *) get_memory(strlen(dev_name)+1);
140 strcpy(dev->dev_name, dev_name);
142 dev->errmsg = (char *) get_pool_memory(PM_EMSG);
145 if ((errstat = pthread_mutex_init(&dev->mutex, NULL)) != 0) {
146 dev->dev_errno = errstat;
147 Mmsg1(&dev->errmsg, "Unable to init mutex: ERR=%s\n", strerror(errstat));
148 Emsg0(M_FATAL, 0, dev->errmsg);
150 if ((errstat = pthread_cond_init(&dev->wait, NULL)) != 0) {
151 dev->dev_errno = errstat;
152 Mmsg1(&dev->errmsg, "Unable to init cond variable: ERR=%s\n", strerror(errstat));
153 Emsg0(M_FATAL, 0, dev->errmsg);
155 if ((errstat = pthread_cond_init(&dev->wait_next_vol, NULL)) != 0) {
156 dev->dev_errno = errstat;
157 Mmsg1(&dev->errmsg, "Unable to init cond variable: ERR=%s\n", strerror(errstat));
158 Emsg0(M_FATAL, 0, dev->errmsg);
161 Dmsg2(29, "init_dev: tape=%d dev_name=%s\n", dev_is_tape(dev), dev->dev_name);
165 /* Open the device with the operating system and
166 * initialize buffer pointers.
168 * Note, for a tape, the VolName is the name we give to the
169 * volume (not really used here), but for a file, the
170 * VolName represents the name of the file to be created/opened.
171 * In the case of a file, the full name is the device name
172 * (archive_name) with the VolName concatenated.
175 open_dev(DEVICE *dev, char *VolName, int mode)
179 if (dev->state & ST_OPENED) {
181 * *****FIXME***** how to handle two threads wanting
182 * different volumes mounted???? E.g. one is waiting
183 * for the next volume to be mounted, and a new job
184 * starts and snatches up the device.
186 if (VolName && strcmp(dev->VolCatInfo.VolCatName, VolName) != 0) {
190 Mmsg2(&dev->errmsg, "WARNING!!!! device %s opened %d times!!!\n",
191 dev->dev_name, dev->use_count);
192 Emsg0(M_WARNING, 0, dev->errmsg);
196 strcpy(dev->VolCatInfo.VolCatName, VolName);
199 Dmsg3(29, "open_dev: tape=%d dev_name=%s vol=%s\n", dev_is_tape(dev),
200 dev->dev_name, dev->VolCatInfo.VolCatName);
201 dev->state &= ~(ST_LABEL|ST_APPEND|ST_READ|ST_EOT|ST_WEOT|ST_EOF);
202 if (dev->state & ST_TAPE) {
203 Dmsg0(29, "open_dev: device is tape\n");
204 if (mode == READ_WRITE) {
205 dev->mode = O_RDWR | O_BINARY;
207 dev->mode = O_RDONLY | O_BINARY;
209 if ((dev->fd = open(dev->dev_name, dev->mode, MODE_RW)) < 0) {
210 dev->dev_errno = errno;
211 Mmsg2(&dev->errmsg, "stored: unable to open device %s: ERR=%s\n",
212 dev->dev_name, strerror(dev->dev_errno));
213 Emsg0(M_FATAL, 0, dev->errmsg);
216 dev->state |= ST_OPENED;
218 update_pos_dev(dev); /* update position */
220 Dmsg1(29, "open_dev: tape %d opened\n", dev->fd);
222 archive_name = (char *) get_pool_memory(PM_FNAME);
223 strcpy(archive_name, dev->dev_name);
224 if (archive_name[strlen(archive_name)] != '/') {
225 strcat(archive_name, "/");
227 strcat(archive_name, VolName);
228 Dmsg1(29, "open_dev: device is disk %s\n", archive_name);
229 if (mode == READ_WRITE) {
230 dev->mode = O_CREAT | O_RDWR | O_BINARY;
232 dev->mode = O_RDONLY | O_BINARY;
234 if ((dev->fd = open(archive_name, dev->mode, MODE_RW)) < 0) {
235 dev->dev_errno = errno;
236 Mmsg2(&dev->errmsg, "Could not open: %s, ERR=%s\n", archive_name, strerror(dev->dev_errno));
237 Emsg0(M_FATAL, 0, dev->errmsg);
240 dev->state |= ST_OPENED;
242 update_pos_dev(dev); /* update position */
244 Dmsg1(29, "open_dev: disk fd=%d opened\n", dev->fd);
245 free_pool_memory(archive_name);
252 * Returns: 1 on success
255 int rewind_dev(DEVICE *dev)
260 Dmsg0(29, "rewind_dev\n");
262 dev->dev_errno = EBADF;
263 Mmsg1(&dev->errmsg, "Bad call to rewind_dev. Device %s not open\n",
265 Emsg0(M_ABORT, 0, dev->errmsg);
268 dev->state &= ~(ST_APPEND|ST_READ|ST_EOT | ST_EOF | ST_WEOT); /* remove EOF/EOT flags */
269 dev->block_num = dev->file = 0;
270 if (dev->state & ST_TAPE) {
271 mt_com.mt_op = MTREW;
273 /* If we get an I/O error on rewind, it is probably because
274 * the drive is actually busy. We loop for (about 5 minutes)
275 * retrying every 5 seconds.
277 for (i=dev->max_rewind_wait; ; i -= 5) {
278 if (ioctl(dev->fd, MTIOCTOP, (char *)&mt_com) < 0) {
279 if (i == dev->max_rewind_wait) {
280 Dmsg1(200, "Rewind error, %s. retrying ...\n", strerror(errno));
282 clrerror_dev(dev, MTREW);
283 if (dev->dev_errno == EIO && i > 0) {
284 Dmsg0(200, "Sleeping 5 seconds.\n");
288 Mmsg2(&dev->errmsg, "Rewind error on %s. ERR=%s.\n",
289 dev->dev_name, strerror(dev->dev_errno));
295 if (lseek(dev->fd, 0, SEEK_SET) < 0) {
296 dev->dev_errno = errno;
297 Mmsg2(&dev->errmsg, "lseek error on %s. ERR=%s.\n",
298 dev->dev_name, strerror(dev->dev_errno));
306 * Position device to end of medium (end of data)
307 * Returns: 1 on succes
314 struct mtget mt_stat;
318 Dmsg0(29, "eod_dev\n");
319 if (dev->state & ST_EOT) {
322 dev->state &= ~(ST_EOF); /* remove EOF flags */
323 dev->block_num = dev->file = 0;
324 if (!(dev->state & ST_TAPE)) {
325 pos = lseek(dev->fd, 0, SEEK_END);
328 dev->state |= ST_EOT;
333 if (dev->capabilities & CAP_EOM) {
334 mt_com.mt_op = MTEOM;
336 if ((stat=ioctl(dev->fd, MTIOCTOP, (char *)&mt_com)) < 0) {
337 Dmsg1(50, "ioctl error: %s\n", strerror(dev->dev_errno));
338 clrerror_dev(dev, mt_com.mt_op);
340 Mmsg2(&dev->errmsg, "ioctl MTEOM error on %s. ERR=%s.\n",
341 dev->dev_name, strerror(dev->dev_errno));
344 if (ioctl(dev->fd, MTIOCGET, (char *)&mt_stat) < 0) {
345 dev->dev_errno = errno;
346 Mmsg2(&dev->errmsg, "ioctl MTIOCGET error on %s. ERR=%s.\n",
347 dev->dev_name, strerror(dev->dev_errno));
350 Dmsg2(200, "EOD file=%d block=%d\n", mt_stat.mt_fileno, mt_stat.mt_blkno);
351 dev->file = mt_stat.mt_fileno;
354 * Rewind then use FSF until EOT reached
357 if (!rewind_dev(dev)) {
360 while (!(dev->state & ST_EOT)) {
361 Dmsg0(200, "Do fsf 1\n");
362 if (fsf_dev(dev, 1) < 0) {
363 Dmsg0(200, "fsf_dev return < 0\n");
368 update_pos_dev(dev); /* update position */
369 Dmsg1(200, "EOD dev->file=%d\n", dev->file);
374 * Set the position of the device.
375 * Returns: 1 on succes
378 int update_pos_dev(DEVICE *dev)
381 struct mtget mt_stat;
387 dev->dev_errno = EBADF;
388 Mmsg0(&dev->errmsg, "Bad device call. Archive not open\n");
389 Emsg0(M_FATAL, 0, dev->errmsg);
393 /* Find out where we are */
394 if (!(dev->state & ST_TAPE)) {
396 pos = lseek(dev->fd, 0, SEEK_CUR);
398 Dmsg1(200, "Seek error: ERR=%s\n", strerror(dev->dev_errno));
400 dev->dev_errno = errno;
401 Mmsg2(&dev->errmsg, "lseek error on %s. ERR=%s.\n",
402 dev->dev_name, strerror(dev->dev_errno));
409 #ifdef REALLY_IMPLEMENTED
410 if (ioctl(dev->fd, MTIOCGET, (char *)&mt_stat) < 0) {
411 Dmsg1(50, "MTIOCGET error: %s\n", strerror(dev->dev_errno));
412 dev->dev_errno = errno;
413 Mmsg2(&dev->errmsg, "ioctl MTIOCGET error on %s. ERR=%s.\n",
414 dev->dev_name, strerror(dev->dev_errno));
425 * Return the status of the device. This was meant
426 * to be a generic routine. Unfortunately, it doesn't
427 * seem possible (at least I do not know how to do it
428 * currently), which means that for the moment, this
429 * routine has very little value.
431 * Returns: 1 on success
435 status_dev(DEVICE *dev, uint32_t *status)
437 struct mtget mt_stat;
440 if (dev->state & (ST_EOT | ST_WEOT)) {
444 if (dev->state & ST_EOF) {
448 if (dev->state & ST_TAPE) {
450 Dmsg0(-20," Driver status:");
451 Dmsg2(-20," file=%d block=%d\n", dev->file, dev->block_num);
452 if (ioctl(dev->fd, MTIOCGET, (char *)&mt_stat) < 0) {
453 dev->dev_errno = errno;
454 Mmsg2(&dev->errmsg, "ioctl MTIOCGET error on %s. ERR=%s.\n",
455 dev->dev_name, strerror(dev->dev_errno));
458 Dmsg0(-20, " Device status:");
460 #if defined(HAVE_LINUX_OS)
461 if (GMT_EOF(mt_stat.mt_gstat)) {
465 if (GMT_BOT(mt_stat.mt_gstat)) {
469 if (GMT_EOT(mt_stat.mt_gstat)) {
473 if (GMT_SM(mt_stat.mt_gstat)) {
477 if (GMT_EOD(mt_stat.mt_gstat)) {
481 if (GMT_WR_PROT(mt_stat.mt_gstat)) {
483 Dmsg0(-20, " WR_PROT");
485 if (GMT_ONLINE(mt_stat.mt_gstat)) {
487 Dmsg0(-20, " ONLINE");
489 if (GMT_DR_OPEN(mt_stat.mt_gstat)) {
491 Dmsg0(-20, " DR_OPEN");
493 if (GMT_IM_REP_EN(mt_stat.mt_gstat)) {
494 stat |= MT_IM_REP_EN;
495 Dmsg0(-20, " IM_REP_EN");
497 #endif /* !SunOS && !OSF */
498 Dmsg2(-20, " file=%d block=%d\n", mt_stat.mt_fileno, mt_stat.mt_blkno);
500 stat |= MT_ONLINE | MT_BOT;
508 * Load medium in device
509 * Returns: 1 on success
512 int load_dev(DEVICE *dev)
519 dev->dev_errno = EBADF;
520 Mmsg0(&dev->errmsg, "Bad call to load_dev. Archive not open\n");
521 Emsg0(M_FATAL, 0, dev->errmsg);
524 if (!(dev->state & ST_TAPE)) {
528 Dmsg0(200, "stored: MTLOAD command not available\n");
529 dev->dev_errno = ENOTTY; /* function not available */
530 Mmsg2(&dev->errmsg, "ioctl MTLOAD error on %s. ERR=%s.\n",
531 dev->dev_name, strerror(dev->dev_errno)); return 0;
535 dev->block_num = dev->file = 0;
536 mt_com.mt_op = MTLOAD;
538 if (ioctl(dev->fd, MTIOCTOP, (char *)&mt_com) < 0) {
539 dev->dev_errno = errno;
540 Mmsg2(&dev->errmsg, "ioctl MTLOAD error on %s. ERR=%s.\n",
541 dev->dev_name, strerror(dev->dev_errno)); return 0;
548 * Rewind device and put it offline
549 * Returns: 1 on success
552 int offline_dev(DEVICE *dev)
557 dev->dev_errno = EBADF;
558 Mmsg0(&dev->errmsg, "Bad call to load_dev. Archive not open\n");
559 Emsg0(M_FATAL, 0, dev->errmsg);
562 if (!(dev->state & ST_TAPE)) {
566 dev->block_num = dev->file = 0;
567 mt_com.mt_op = MTOFFL;
569 if (ioctl(dev->fd, MTIOCTOP, (char *)&mt_com) < 0) {
570 dev->dev_errno = errno;
571 Mmsg2(&dev->errmsg, "ioctl MTOFFL error on %s. ERR=%s.\n",
572 dev->dev_name, strerror(dev->dev_errno));
580 * Foward space a file
583 fsf_dev(DEVICE *dev, int num)
590 dev->dev_errno = EBADF;
591 Mmsg0(&dev->errmsg, "Bad call to fsf_dev. Archive not open\n");
592 Emsg0(M_FATAL, 0, dev->errmsg);
596 if (!(dev->state & ST_TAPE)) {
599 if (dev->state & ST_EOT) {
601 Mmsg1(&dev->errmsg, "Device %s at End of Tape.\n", dev->dev_name);
604 if (dev->state & ST_EOF)
605 Dmsg0(200, "ST_EOF set on entry to FSF\n");
606 if (dev->state & ST_EOT)
607 Dmsg0(200, "ST_EOT set on entry to FSF\n");
609 Dmsg0(29, "fsf_dev\n");
611 if (dev->capabilities & CAP_FSF) {
612 Dmsg0(200, "FSF has cap_fsf\n");
613 mt_com.mt_op = MTFSF;
615 while (num-- && !(dev->state & ST_EOT)) {
616 Dmsg0(200, "Doing read for fsf\n");
617 if ((stat = read(dev->fd, rbuf, sizeof(rbuf))) < 0) {
618 if (errno == ENOMEM) { /* tape record exceeds buf len */
619 stat = sizeof(rbuf); /* This is OK */
621 dev->state |= ST_EOT;
622 clrerror_dev(dev, -1);
623 Dmsg1(200, "Set ST_EOT read error %d\n", dev->dev_errno);
624 Mmsg2(&dev->errmsg, "read error on %s. ERR=%s.\n",
625 dev->dev_name, strerror(dev->dev_errno));
626 Dmsg1(200, "%s", dev->errmsg);
630 if (stat == 0) { /* EOF */
632 Dmsg1(200, "End of File mark from read. File=%d\n", dev->file+1);
633 /* Two reads of zero means end of tape */
634 if (dev->state & ST_EOF) {
635 dev->state |= ST_EOT;
636 Dmsg0(200, "Set ST_EOT\n");
639 dev->state |= ST_EOF;
643 } else { /* Got data */
644 dev->state &= ~(ST_EOF|ST_EOT);
647 Dmsg0(200, "Doing MT_FSF\n");
648 stat = ioctl(dev->fd, MTIOCTOP, (char *)&mt_com);
649 if (stat < 0) { /* error => EOT */
650 dev->state |= ST_EOT;
651 Dmsg0(200, "Set ST_EOT\n");
652 clrerror_dev(dev, MTFSF);
653 Mmsg2(&dev->errmsg, "ioctl MTFSF error on %s. ERR=%s.\n",
654 dev->dev_name, strerror(dev->dev_errno));
655 Dmsg0(200, "Got < 0 for MT_FSF\n");
656 Dmsg1(200, "%s", dev->errmsg);
658 dev->state |= ST_EOF; /* just read EOF */
664 * No FSF, so use FSR to simulate it
667 Dmsg0(200, "Doing FSR for FSF\n");
668 while (num-- && !(dev->state & ST_EOT)) {
669 fsr_dev(dev, INT32_MAX); /* returns -1 on EOF or EOT */
671 if (dev->state & ST_EOT) {
673 Mmsg1(&dev->errmsg, "Device %s at End of Tape.\n", dev->dev_name);
680 Dmsg1(200, "Return %d from FSF\n", stat);
681 if (dev->state & ST_EOF)
682 Dmsg0(200, "ST_EOF set on exit FSF\n");
683 if (dev->state & ST_EOT)
684 Dmsg0(200, "ST_EOT set on exit FSF\n");
685 Dmsg1(200, "Return from FSF file=%d\n", dev->file);
690 * Backward space a file
693 bsf_dev(DEVICE *dev, int num)
699 dev->dev_errno = EBADF;
700 Mmsg0(&dev->errmsg, "Bad call to fsf_dev. Archive not open\n");
701 Emsg0(M_FATAL, 0, dev->errmsg);
705 if (!(dev->state & ST_TAPE)) {
708 Dmsg0(29, "bsf_dev\n");
709 dev->state &= ~(ST_EOT|ST_EOF);
711 mt_com.mt_op = MTBSF;
712 mt_com.mt_count = num;
713 stat = ioctl(dev->fd, MTIOCTOP, (char *)&mt_com);
715 clrerror_dev(dev, MTBSF);
716 Mmsg2(&dev->errmsg, "ioctl MTBSF error on %s. ERR=%s.\n",
717 dev->dev_name, strerror(dev->dev_errno));
725 * Foward space a record
728 fsr_dev(DEVICE *dev, int num)
734 dev->dev_errno = EBADF;
735 Mmsg0(&dev->errmsg, "Bad call to fsf_dev. Archive not open\n");
736 Emsg0(M_FATAL, 0, dev->errmsg);
740 if (!(dev->state & ST_TAPE)) {
743 Dmsg0(29, "fsr_dev\n");
744 dev->block_num += num;
745 mt_com.mt_op = MTFSR;
746 mt_com.mt_count = num;
747 stat = ioctl(dev->fd, MTIOCTOP, (char *)&mt_com);
749 dev->state &= ~ST_EOF;
751 if (dev->state & ST_EOF) {
752 dev->state |= ST_EOT;
754 dev->state |= ST_EOF; /* assume EOF */
757 clrerror_dev(dev, MTFSR);
758 Mmsg2(&dev->errmsg, "ioctl MTFSR error on %s. ERR=%s.\n",
759 dev->dev_name, strerror(dev->dev_errno));
766 * Backward space a record
769 bsr_dev(DEVICE *dev, int num)
775 dev->dev_errno = EBADF;
776 Mmsg0(&dev->errmsg, "Bad call to fsf_dev. Archive not open\n");
777 Emsg0(M_FATAL, 0, dev->errmsg);
781 if (!(dev->state & ST_TAPE)) {
784 Dmsg0(29, "bsr_dev\n");
785 dev->block_num -= num;
786 dev->state &= ~(ST_EOF|ST_EOT|ST_EOF);
787 mt_com.mt_op = MTBSR;
788 mt_com.mt_count = num;
789 stat = ioctl(dev->fd, MTIOCTOP, (char *)&mt_com);
791 clrerror_dev(dev, MTBSR);
792 Mmsg2(&dev->errmsg, "ioctl MTBSR error on %s. ERR=%s.\n",
793 dev->dev_name, strerror(dev->dev_errno));
802 * Write an end of file on the device
805 weof_dev(DEVICE *dev, int num)
811 dev->dev_errno = EBADF;
812 Mmsg0(&dev->errmsg, "Bad call to fsf_dev. Archive not open\n");
813 Emsg0(M_FATAL, 0, dev->errmsg);
817 if (!(dev->state & ST_TAPE)) {
820 dev->state &= ~(ST_EOT | ST_EOF); /* remove EOF/EOT flags */
822 Dmsg0(29, "weof_dev\n");
823 mt_com.mt_op = MTWEOF;
824 mt_com.mt_count = num;
825 stat = ioctl(dev->fd, MTIOCTOP, (char *)&mt_com);
829 clrerror_dev(dev, MTWEOF);
830 Mmsg2(&dev->errmsg, "ioctl MTWEOF error on %s. ERR=%s.\n",
831 dev->dev_name, strerror(dev->dev_errno));
837 * Return string message with last error in English
838 * Be careful not to call this routine from within dev.c
839 * while editing an Mmsg(&) or you will end up in a recursive
840 * loop creating a Segmentation Violation.
843 strerror_dev(DEVICE *dev)
850 * If implemented in system, clear the tape
854 clrerror_dev(DEVICE *dev, int func)
858 dev->dev_errno = errno; /* save errno */
860 dev->VolCatInfo.VolCatErrors++;
863 if (!(dev->state & ST_TAPE)) {
866 if (errno == ENOTTY || errno == ENOSYS) { /* Function not implemented */
869 Emsg0(M_ABORT, 0, "Got ENOTTY on read/write!\n");
873 dev->capabilities &= ~CAP_EOF; /* turn off feature */
877 dev->capabilities &= ~CAP_EOM; /* turn off feature */
881 dev->capabilities &= ~CAP_FSF; /* turn off feature */
885 dev->capabilities &= ~CAP_BSF; /* turn off feature */
889 dev->capabilities &= ~CAP_FSR; /* turn off feature */
893 dev->capabilities &= ~CAP_BSR; /* turn off feature */
900 dev->dev_errno = ENOSYS;
901 Mmsg1(&dev->errmsg, "This device does not support %s.\n", msg);
902 Emsg0(M_ERROR, 0, dev->errmsg);
909 mt_com.mt_op = MTIOCLRERR;
911 stat = ioctl(dev->fd, MTIOCTOP, (char *)&mt_com);
912 Dmsg0(200, "Did MTIOCLRERR\n");
918 * Flush buffer contents
921 int flush_dev(DEVICE *dev)
926 static void do_close(DEVICE *dev)
928 Dmsg0(29, "really close_dev\n");
930 /* Clean up device packet so it can be reused */
932 dev->state &= ~(ST_OPENED|ST_LABEL|ST_READ|ST_APPEND|ST_EOT|ST_WEOT|ST_EOF);
935 dev->LastBlockNumWritten = 0;
936 memset(&dev->VolCatInfo, 0, sizeof(dev->VolCatInfo));
937 memset(&dev->VolHdr, 0, sizeof(dev->VolHdr));
944 close_dev(DEVICE *dev)
947 Mmsg0(&dev->errmsg, "Bad call to fsf_dev. Archive not open\n");
948 Emsg0(M_FATAL, 0, dev->errmsg);
951 if (dev->fd >= 0 && dev->use_count == 1) {
954 Dmsg0(29, "close_dev but in use so leave open.\n");
959 void force_close_dev(DEVICE *dev)
962 Mmsg0(&dev->errmsg, "Bad call to fsf_dev. Archive not open\n");
963 Emsg0(M_FATAL, 0, dev->errmsg);
966 Dmsg0(29, "really close_dev\n");
972 dev_is_tape(DEVICE *dev)
974 return (dev->state & ST_TAPE) ? 1 : 0;
978 dev_name(DEVICE *dev)
980 return dev->dev_name;
984 dev_vol_name(DEVICE *dev)
986 return dev->VolCatInfo.VolCatName;
989 uint32_t dev_block(DEVICE *dev)
992 return dev->block_num;
995 uint32_t dev_file(DEVICE *dev)
1002 * Free memory allocated for the device
1005 term_dev(DEVICE *dev)
1008 dev->dev_errno = EBADF;
1009 Mmsg0(&dev->errmsg, "Bad call to fsf_dev. Archive not open\n");
1010 Emsg0(M_FATAL, 0, dev->errmsg);
1014 Dmsg0(29, "term_dev\n");
1015 if (dev->dev_name) {
1016 free_memory(dev->dev_name);
1017 dev->dev_name = NULL;
1020 free_memory(dev->errmsg);
1023 pthread_mutex_destroy(&dev->mutex);
1024 pthread_cond_destroy(&dev->wait);
1025 pthread_cond_destroy(&dev->wait_next_vol);
1026 if (dev->state & ST_MALLOC) {