]> git.sur5r.net Git - bacula/bacula/blob - bacula/src/stored/dev.c
5333f91b5a0cbff948d4655daa5c9e71e71d72f7
[bacula/bacula] / bacula / src / stored / dev.c
1 /*
2  *
3  *   dev.c  -- low level operations on device (storage device)
4  *
5  *              Kern Sibbald
6  *
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).
14  *
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.
18  * 
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.
22  *
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).
28  */
29 /*
30    Copyright (C) 2000, 2001, 2002 Kern Sibbald and John Walker
31
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.
36
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.
41
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,
45    MA 02111-1307, USA.
46
47  */
48
49 /*
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).
67  *
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.
77  */
78
79
80 #include "bacula.h"
81 #include "stored.h"
82
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);
87
88 extern int debug_level;
89
90 /* 
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.
95  *
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.
99  *
100  */
101 DEVICE *
102 init_dev(DEVICE *dev, char *dev_name)
103 {
104    struct stat statp;
105    int tape;
106    int errstat;
107
108    /* Check that device is available */
109    if (stat(dev_name, &statp) < 0) {
110       if (dev) {
111          dev->dev_errno = errno;
112       } 
113       Emsg2(M_FATAL, 0, "Unable to stat device %s : %s\n", dev_name, strerror(errno));
114       return NULL;
115    }
116    tape = FALSE;
117    if (S_ISDIR(statp.st_mode)) {
118       tape = FALSE;
119    } else if (S_ISCHR(statp.st_mode)) {
120       tape = TRUE;
121    } else {
122       if (dev) {
123          dev->dev_errno = ENODEV;
124       }
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);
127       return NULL;
128    }
129    if (!dev) {
130       dev = (DEVICE *)get_memory(sizeof(DEVICE));
131       memset(dev, 0, sizeof(DEVICE));
132       dev->state = ST_MALLOC;
133    } else {
134       memset(dev, 0, sizeof(DEVICE));
135    }
136    if (tape) {
137       dev->state |= ST_TAPE;
138    }
139    dev->dev_name = (char *) get_memory(strlen(dev_name)+1);
140    strcpy(dev->dev_name, dev_name);
141
142    dev->errmsg = (char *) get_pool_memory(PM_EMSG);
143    *dev->errmsg = 0;
144
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);
149    }
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);
154    }
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);
159    }
160    dev->fd = -1;
161    Dmsg2(29, "init_dev: tape=%d dev_name=%s\n", dev_is_tape(dev), dev->dev_name);
162    return dev;
163 }
164
165 /* Open the device with the operating system and
166  * initialize buffer pointers.
167  *
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.
173  */
174 int
175 open_dev(DEVICE *dev, char *VolName, int mode)
176 {
177    char *archive_name;
178
179    if (dev->state & ST_OPENED) {
180       /*
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.
185        */
186       if (VolName && strcmp(dev->VolCatInfo.VolCatName, VolName) != 0) {
187          return -1;
188       }
189       dev->use_count++;
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);
193       return dev->fd;
194    }
195    if (VolName) {
196       strcpy(dev->VolCatInfo.VolCatName, VolName);
197    }
198
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;
206       } else {
207          dev->mode = O_RDONLY | O_BINARY;
208       }
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);
214       } else {
215          dev->dev_errno = 0;
216          dev->state |= ST_OPENED;
217          dev->use_count++;
218          update_pos_dev(dev);             /* update position */
219       }
220       Dmsg1(29, "open_dev: tape %d opened\n", dev->fd);
221    } else {
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, "/");
226       }
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;
231       } else {
232          dev->mode = O_RDONLY | O_BINARY;
233       }
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);
238       } else {
239          dev->dev_errno = 0;
240          dev->state |= ST_OPENED;
241          dev->use_count++;
242          update_pos_dev(dev);                /* update position */
243       }
244       Dmsg1(29, "open_dev: disk fd=%d opened\n", dev->fd);
245       free_pool_memory(archive_name);
246    }
247    return dev->fd;
248 }
249
250 /*
251  * Rewind the device.
252  *  Returns: 1 on success
253  *           0 on failure
254  */
255 int rewind_dev(DEVICE *dev)
256 {
257    struct mtop mt_com;
258    unsigned int i;
259
260    Dmsg0(29, "rewind_dev\n");
261    if (dev->fd < 0) {
262       dev->dev_errno = EBADF;
263       Mmsg1(&dev->errmsg, "Bad call to rewind_dev. Device %s not open\n",
264             dev->dev_name);
265       Emsg0(M_ABORT, 0, dev->errmsg);
266       return 0;
267    }
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;
272       mt_com.mt_count = 1;
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.
276        */
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));
281             }
282             clrerror_dev(dev, MTREW);
283             if (dev->dev_errno == EIO && i > 0) {
284                Dmsg0(200, "Sleeping 5 seconds.\n");
285                sleep(5);
286                continue;
287             }
288             Mmsg2(&dev->errmsg, "Rewind error on %s. ERR=%s.\n",
289                dev->dev_name, strerror(dev->dev_errno));
290             return 0;
291          }
292          break;
293       }
294    } else {
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));
299          return 0;
300       }
301    }
302    return 1;
303 }
304
305 /* 
306  * Position device to end of medium (end of data)
307  *  Returns: 1 on succes
308  *           0 on error
309  */
310 int 
311 eod_dev(DEVICE *dev)
312 {
313    struct mtop mt_com;
314    struct mtget mt_stat;
315    int stat = 0;
316    int32_t pos;
317
318    Dmsg0(29, "eod_dev\n");
319    if (dev->state & ST_EOT) {
320       return 1;
321    }
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);
326       if (pos > 0) {
327          update_pos_dev(dev);
328          dev->state |= ST_EOT;
329          return 1;
330       }
331       return 0;
332    }
333    if (dev->capabilities & CAP_EOM) {
334       mt_com.mt_op = MTEOM;
335       mt_com.mt_count = 1;
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);
339          update_pos_dev(dev);
340          Mmsg2(&dev->errmsg, "ioctl MTEOM error on %s. ERR=%s.\n",
341             dev->dev_name, strerror(dev->dev_errno));
342          return 0;
343       }
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));
348          return 0;
349       }
350       Dmsg2(200, "EOD file=%d block=%d\n", mt_stat.mt_fileno, mt_stat.mt_blkno);
351       dev->file = mt_stat.mt_fileno;
352
353    /*
354     * Rewind then use FSF until EOT reached
355     */
356    } else {
357       if (!rewind_dev(dev)) {
358          return 0;
359       }
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");
364             return 0;
365          }
366       }
367    }
368    update_pos_dev(dev);                      /* update position */
369    Dmsg1(200, "EOD dev->file=%d\n", dev->file);
370    return 1;
371 }
372
373 /*
374  * Set the position of the device.
375  *  Returns: 1 on succes
376  *           0 on error
377  */
378 int update_pos_dev(DEVICE *dev)
379 {
380 #ifdef xxxx
381    struct mtget mt_stat;
382 #endif
383    int32_t pos;
384    int stat = 0;
385
386    if (dev->fd < 0) {
387       dev->dev_errno = EBADF;
388       Mmsg0(&dev->errmsg, "Bad device call. Archive not open\n");
389       Emsg0(M_FATAL, 0, dev->errmsg);
390       return 0;
391    }
392
393    /* Find out where we are */
394    if (!(dev->state & ST_TAPE)) {
395       dev->file = 0;
396       pos = lseek(dev->fd, 0, SEEK_CUR);
397       if (pos < 0) {
398          Dmsg1(200, "Seek error: ERR=%s\n", strerror(dev->dev_errno));
399          pos = 0;
400          dev->dev_errno = errno;
401          Mmsg2(&dev->errmsg, "lseek error on %s. ERR=%s.\n",
402             dev->dev_name, strerror(dev->dev_errno));
403       } else {
404          stat = 1;
405       }
406       return stat;
407    }
408
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));
415    } else {
416       stat = 1;
417    }
418    return stat;
419 #endif
420    return 1;
421 }
422
423
424 /* 
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.
430  *
431  *   Returns: 1 on success
432  *            0 on error
433  */
434 int
435 status_dev(DEVICE *dev, uint32_t *status)
436 {
437    struct mtget mt_stat;
438    uint32_t stat = 0;
439
440    if (dev->state & (ST_EOT | ST_WEOT)) {
441       stat |= MT_EOD;
442       Dmsg0(-20, " EOD");
443    }
444    if (dev->state & ST_EOF) {
445       stat |= MT_EOF;
446       Dmsg0(-20, " EOF");
447    }
448    if (dev->state & ST_TAPE) {
449       stat |= MT_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));
456          return 0;
457       }
458       Dmsg0(-20, " Device status:");
459
460 #if defined(HAVE_LINUX_OS)
461       if (GMT_EOF(mt_stat.mt_gstat)) {
462          stat |= MT_EOF;
463          Dmsg0(-20, " EOF");
464       }
465       if (GMT_BOT(mt_stat.mt_gstat)) {
466          stat |= MT_BOT;
467          Dmsg0(-20, " BOT");
468       }
469       if (GMT_EOT(mt_stat.mt_gstat)) {
470          stat |= MT_EOT;
471          Dmsg0(-20, " EOT");
472       }
473       if (GMT_SM(mt_stat.mt_gstat)) {
474          stat |= MT_SM;
475          Dmsg0(-20, " SM");
476       }
477       if (GMT_EOD(mt_stat.mt_gstat)) {
478          stat |= MT_EOD;
479          Dmsg0(-20, " EOD");
480       }
481       if (GMT_WR_PROT(mt_stat.mt_gstat)) {
482          stat |= MT_WR_PROT;
483          Dmsg0(-20, " WR_PROT");
484       }
485       if (GMT_ONLINE(mt_stat.mt_gstat)) {
486          stat |= MT_ONLINE;
487          Dmsg0(-20, " ONLINE");
488       }
489       if (GMT_DR_OPEN(mt_stat.mt_gstat)) {
490          stat |= MT_DR_OPEN;
491          Dmsg0(-20, " DR_OPEN");       
492       }
493       if (GMT_IM_REP_EN(mt_stat.mt_gstat)) {
494          stat |= MT_IM_REP_EN;
495          Dmsg0(-20, " IM_REP_EN");
496       }
497 #endif /* !SunOS && !OSF */
498       Dmsg2(-20, " file=%d block=%d\n", mt_stat.mt_fileno, mt_stat.mt_blkno);
499    } else {
500       stat |= MT_ONLINE | MT_BOT;
501    }
502    *status = stat; 
503    return 1;
504 }
505
506
507 /*
508  * Load medium in device
509  *  Returns: 1 on success
510  *           0 on failure
511  */
512 int load_dev(DEVICE *dev)
513 {
514 #ifdef MTLOAD
515    struct mtop mt_com;
516 #endif
517
518    if (dev->fd < 0) {
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);
522       return 0;
523    }
524    if (!(dev->state & ST_TAPE)) {
525       return 1;
526    }
527 #ifndef MTLOAD
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;
532    return 0;
533 #else
534
535    dev->block_num = dev->file = 0;
536    mt_com.mt_op = MTLOAD;
537    mt_com.mt_count = 1;
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;
542    }
543    return 1;
544 #endif
545 }
546
547 /*
548  * Rewind device and put it offline
549  *  Returns: 1 on success
550  *           0 on failure
551  */
552 int offline_dev(DEVICE *dev)
553 {
554    struct mtop mt_com;
555
556    if (dev->fd < 0) {
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);
560       return 0;
561    }
562    if (!(dev->state & ST_TAPE)) {
563       return 1;
564    }
565
566    dev->block_num = dev->file = 0;
567    mt_com.mt_op = MTOFFL;
568    mt_com.mt_count = 1;
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));
573       return 0;
574    }
575    return 1;
576 }
577
578
579 /* 
580  * Foward space a file  
581  */
582 int
583 fsf_dev(DEVICE *dev, int num)
584
585    struct mtop mt_com;
586    int stat;
587    char rbuf[1024];
588
589    if (dev->fd < 0) {
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);
593       return -1;
594    }
595
596    if (!(dev->state & ST_TAPE)) {
597       return 0;
598    }
599    if (dev->state & ST_EOT) {
600       dev->dev_errno = 0;
601       Mmsg1(&dev->errmsg, "Device %s at End of Tape.\n", dev->dev_name);
602       return -1;
603    }
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");
608       
609    Dmsg0(29, "fsf_dev\n");
610    dev->block_num = 0;
611    if (dev->capabilities & CAP_FSF) {
612       Dmsg0(200, "FSF has cap_fsf\n");
613       mt_com.mt_op = MTFSF;
614       mt_com.mt_count = 1;
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 */
620             } else {
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);
627                break;
628             }
629          }
630          if (stat == 0) {                /* EOF */
631             update_pos_dev(dev);
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");
637                break;
638             } else {
639                dev->state |= ST_EOF;
640                dev->file++;
641                continue;
642             }
643          } else {                        /* Got data */
644             dev->state &= ~(ST_EOF|ST_EOT);
645          }
646
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);
657          } else {
658             dev->state |= ST_EOF;     /* just read EOF */
659             dev->file++;
660          }   
661       }
662    
663    /*
664     * No FSF, so use FSR to simulate it
665     */
666    } else {
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 */
670       }
671       if (dev->state & ST_EOT) {
672          dev->dev_errno = 0;
673          Mmsg1(&dev->errmsg, "Device %s at End of Tape.\n", dev->dev_name);
674          stat = -1;
675       } else {
676          stat = 0;
677       }
678    }
679    update_pos_dev(dev);
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);
686    return stat;
687 }
688
689 /* 
690  * Backward space a file  
691  */
692 int
693 bsf_dev(DEVICE *dev, int num)
694
695    struct mtop mt_com;
696    int stat;
697
698    if (dev->fd < 0) {
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);
702       return -1;
703    }
704
705    if (!(dev->state & ST_TAPE)) {
706       return 0;
707    }
708    Dmsg0(29, "bsf_dev\n");
709    dev->state &= ~(ST_EOT|ST_EOF);
710    dev->file -= num;
711    mt_com.mt_op = MTBSF;
712    mt_com.mt_count = num;
713    stat = ioctl(dev->fd, MTIOCTOP, (char *)&mt_com);
714    if (stat < 0) {
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));
718    }
719    update_pos_dev(dev);
720    return stat;
721 }
722
723
724 /* 
725  * Foward space a record
726  */
727 int
728 fsr_dev(DEVICE *dev, int num)
729
730    struct mtop mt_com;
731    int stat;
732
733    if (dev->fd < 0) {
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);
737       return -1;
738    }
739
740    if (!(dev->state & ST_TAPE)) {
741       return 0;
742    }
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);
748    if (stat == 0) {
749       dev->state &= ~ST_EOF;
750    } else {
751       if (dev->state & ST_EOF) {
752          dev->state |= ST_EOT;
753       } else {
754          dev->state |= ST_EOF;           /* assume EOF */
755          dev->file++;
756       }
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));
760    }
761    update_pos_dev(dev);
762    return stat;
763 }
764
765 /* 
766  * Backward space a record
767  */
768 int
769 bsr_dev(DEVICE *dev, int num)
770
771    struct mtop mt_com;
772    int stat;
773
774    if (dev->fd < 0) {
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);
778       return -1;
779    }
780
781    if (!(dev->state & ST_TAPE)) {
782       return 0;
783    }
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);
790    if (stat < 0) {
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));
794    }
795    update_pos_dev(dev);
796    return stat;
797 }
798
799
800
801 /*
802  * Write an end of file on the device
803  */
804 int 
805 weof_dev(DEVICE *dev, int num)
806
807    struct mtop mt_com;
808    int stat;
809
810    if (dev->fd < 0) {
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);
814       return -1;
815    }
816
817    if (!(dev->state & ST_TAPE)) {
818       return 0;
819    }
820    dev->state &= ~(ST_EOT | ST_EOF);  /* remove EOF/EOT flags */
821    dev->block_num = 0;
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);
826    if (stat == 0) {
827       dev->file++;
828    } else {
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));
832    }
833    return stat;
834 }
835
836 /*
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.
841  */
842 char *
843 strerror_dev(DEVICE *dev)
844 {
845    return dev->errmsg;
846 }
847
848
849 /*
850  * If implemented in system, clear the tape
851  * error status.
852  */
853 void
854 clrerror_dev(DEVICE *dev, int func)
855 {
856    char *msg = NULL;
857
858    dev->dev_errno = errno;         /* save errno */
859    if (errno == EIO) {
860       dev->VolCatInfo.VolCatErrors++;
861    }
862
863    if (!(dev->state & ST_TAPE)) {
864       return;
865    }
866    if (errno == ENOTTY || errno == ENOSYS) { /* Function not implemented */
867       switch (func) {
868          case -1:
869             Emsg0(M_ABORT, 0, "Got ENOTTY on read/write!\n");
870             break;
871          case MTWEOF:
872             msg = "WTWEOF";
873             dev->capabilities &= ~CAP_EOF; /* turn off feature */
874             break;
875          case MTEOM:
876             msg = "WTEOM";
877             dev->capabilities &= ~CAP_EOM; /* turn off feature */
878             break;
879          case MTFSF:
880             msg = "MTFSF";
881             dev->capabilities &= ~CAP_FSF; /* turn off feature */
882             break;
883          case MTBSF:
884             msg = "MTBSF";
885             dev->capabilities &= ~CAP_BSF; /* turn off feature */
886             break;
887          case MTFSR:
888             msg = "MTFSR";
889             dev->capabilities &= ~CAP_FSR; /* turn off feature */
890             break;
891          case MTBSR:
892             msg = "MTBSR";
893             dev->capabilities &= ~CAP_BSR; /* turn off feature */
894             break;
895          default:
896             msg = "Unknown";
897             break;
898       }
899       if (msg != NULL) {
900          dev->dev_errno = ENOSYS;
901          Mmsg1(&dev->errmsg, "This device does not support %s.\n", msg);
902          Emsg0(M_ERROR, 0, dev->errmsg);
903       }
904    }
905 #ifdef MTIOCLRERR
906 {
907    struct mtop mt_com;
908    int stat;
909    mt_com.mt_op = MTIOCLRERR;
910    mt_com.mt_count = 1;
911    stat = ioctl(dev->fd, MTIOCTOP, (char *)&mt_com);
912    Dmsg0(200, "Did MTIOCLRERR\n");
913 }
914 #endif
915 }
916
917 /*
918  * Flush buffer contents
919  *  No longer used.
920  */
921 int flush_dev(DEVICE *dev)
922 {
923    return 1;
924 }
925
926 static void do_close(DEVICE *dev)
927 {
928    Dmsg0(29, "really close_dev\n");
929    close(dev->fd);
930    /* Clean up device packet so it can be reused */
931    dev->fd = -1;
932    dev->state &= ~(ST_OPENED|ST_LABEL|ST_READ|ST_APPEND|ST_EOT|ST_WEOT|ST_EOF);
933    dev->block_num = 0;
934    dev->file = 0;
935    dev->LastBlockNumWritten = 0;
936    memset(&dev->VolCatInfo, 0, sizeof(dev->VolCatInfo));
937    memset(&dev->VolHdr, 0, sizeof(dev->VolHdr));
938 }
939
940 /* 
941  * Close the device
942  */
943 void
944 close_dev(DEVICE *dev)
945 {
946    if (!dev) {
947       Mmsg0(&dev->errmsg, "Bad call to fsf_dev. Archive not open\n");
948       Emsg0(M_FATAL, 0, dev->errmsg);
949       return;
950    }
951    if (dev->fd >= 0 && dev->use_count == 1) {
952       do_close(dev);
953    } else {    
954       Dmsg0(29, "close_dev but in use so leave open.\n");
955    }
956    dev->use_count--;
957 }
958
959 void force_close_dev(DEVICE *dev)
960 {
961    if (!dev) {
962       Mmsg0(&dev->errmsg, "Bad call to fsf_dev. Archive not open\n");
963       Emsg0(M_FATAL, 0, dev->errmsg);
964       return;
965    }
966    Dmsg0(29, "really close_dev\n");
967    do_close(dev);
968    dev->use_count--;
969 }
970
971 int 
972 dev_is_tape(DEVICE *dev)
973 {  
974    return (dev->state & ST_TAPE) ? 1 : 0;
975 }
976
977 char *
978 dev_name(DEVICE *dev)
979 {
980    return dev->dev_name;
981 }
982
983 char *
984 dev_vol_name(DEVICE *dev)
985 {
986    return dev->VolCatInfo.VolCatName;
987 }
988
989 uint32_t dev_block(DEVICE *dev)
990 {
991    update_pos_dev(dev);
992    return dev->block_num;
993 }
994
995 uint32_t dev_file(DEVICE *dev)
996 {
997    update_pos_dev(dev);
998    return dev->file;
999 }
1000
1001 /* 
1002  * Free memory allocated for the device
1003  */
1004 void
1005 term_dev(DEVICE *dev)
1006 {
1007    if (!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);
1011       return;
1012    }
1013    close_dev(dev);
1014    Dmsg0(29, "term_dev\n");
1015    if (dev->dev_name) {
1016       free_memory(dev->dev_name);
1017       dev->dev_name = NULL;
1018    }
1019    if (dev->errmsg) {
1020       free_memory(dev->errmsg);
1021       dev->errmsg = NULL;
1022    }
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) {
1027       free_memory(dev);
1028    }
1029 }