]> git.sur5r.net Git - bacula/bacula/blob - bacula/src/stored/dev.c
80c6315de248a7267a9048fd0fe473ca38cee15c
[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 contorted than is
26  *     really necessary -- KES.  Note, this contortion has been
27  *     corrected to a large extent by a rewrite (Apr MMI).
28  *
29  *   Version $Id$
30  */
31 /*
32    Copyright (C) 2000, 2001, 2002 Kern Sibbald and John Walker
33
34    This program is free software; you can redistribute it and/or
35    modify it under the terms of the GNU General Public License as
36    published by the Free Software Foundation; either version 2 of
37    the License, or (at your option) any later version.
38
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 GNU
42    General Public License for more details.
43
44    You should have received a copy of the GNU General Public
45    License along with this program; if not, write to the Free
46    Software Foundation, Inc., 59 Temple Place - Suite 330, Boston,
47    MA 02111-1307, USA.
48
49  */
50
51 /*
52  * Handling I/O errors and end of tape conditions are a bit tricky.
53  * This is how it is currently done when writting.
54  * On either an I/O error or end of tape,
55  * we will stop writing on the physical device (no I/O recovery is
56  * attempted at least in this daemon). The state flag will be sent
57  * to include ST_EOT, which is ephimeral, and ST_WEOT, which is
58  * persistent. Lots of routines clear ST_EOT, but ST_WEOT is
59  * cleared only when the problem goes away.  Now when ST_WEOT
60  * is set all calls to write_dev() are handled as usual. However,
61  * in write_block() instead of attempting to write the block to
62  * the physical device, it is chained into a list of blocks written
63  * after the EOT condition.  In addition, all threads are blocked
64  * from writing on the tape by calling lock(), and thread other
65  * than the first thread to hit the EOT will block on a condition
66  * variable. The first thread to hit the EOT will continue to
67  * be able to read and write the tape (he sort of tunnels through
68  * the locking mechanism -- see lock() for details).
69  *
70  * Now presumably somewhere higher in the chain of command 
71  * (device.c), someone will notice the EOT condition and 
72  * get a new tape up, get the tape label read, and mark 
73  * the label for rewriting. Then this higher level routine 
74  * will write the unwritten buffer to the new volume.
75  * Finally, he will release
76  * any blocked threads by doing a broadcast on the condition
77  * variable.  At that point, we should be totally back in 
78  * business with no lost data.
79  */
80
81
82 #include "bacula.h"
83 #include "stored.h"
84
85 /* Forward referenced functions */
86 int dev_is_tape(DEVICE *dev);
87 void clrerror_dev(DEVICE *dev, int func);
88 int fsr_dev(DEVICE *dev, int num);
89
90 extern int debug_level;
91
92 /* 
93  * Allocate and initialize the DEVICE structure
94  * Note, if dev is non-NULL, it is already allocated,
95  * thus we neither allocate it nor free it. This allows
96  * the caller to put the packet in shared memory.
97  *
98  *  Note, for a tape, the dev_name is the device name
99  *     (e.g. /dev/nst0), and for a file, the device name
100  *     is the directory in which the file will be placed.
101  *
102  */
103 DEVICE *
104 init_dev(DEVICE *dev, char *dev_name)
105 {
106    struct stat statp;
107    int tape;
108    int errstat;
109
110    /* Check that device is available */
111    if (stat(dev_name, &statp) < 0) {
112       if (dev) {
113          dev->dev_errno = errno;
114       } 
115       Emsg2(M_FATAL, 0, "Unable to stat device %s : %s\n", dev_name, strerror(errno));
116       return NULL;
117    }
118    tape = FALSE;
119    if (S_ISDIR(statp.st_mode)) {
120       tape = FALSE;
121    } else if (S_ISCHR(statp.st_mode)) {
122       tape = TRUE;
123    } else {
124       if (dev) {
125          dev->dev_errno = ENODEV;
126       }
127       Emsg2(M_FATAL, 0, "%s is an unknown device type. Must be tape or directory. st_mode=%x\n", 
128          dev_name, statp.st_mode);
129       return NULL;
130    }
131    if (!dev) {
132       dev = (DEVICE *)get_memory(sizeof(DEVICE));
133       memset(dev, 0, sizeof(DEVICE));
134       dev->state = ST_MALLOC;
135    } else {
136       memset(dev, 0, sizeof(DEVICE));
137    }
138    if (tape) {
139       dev->state |= ST_TAPE;
140    }
141    dev->dev_name = get_memory(strlen(dev_name)+1);
142    strcpy(dev->dev_name, dev_name);
143
144    dev->errmsg = get_pool_memory(PM_EMSG);
145    *dev->errmsg = 0;
146
147 #ifdef NEW_LOCK
148    if ((errstat=rwl_init(&dev->lock)) != 0) {
149       Mmsg1(&dev->errmsg, _("Unable to initialize dev lock. ERR=%s\n"), strerror(errstat));
150       Emsg0(M_FATAL, 0, dev->errmsg);
151    }
152 #endif
153
154    if ((errstat = pthread_mutex_init(&dev->mutex, NULL)) != 0) {
155       dev->dev_errno = errstat;
156       Mmsg1(&dev->errmsg, _("Unable to init mutex: ERR=%s\n"), strerror(errstat));
157       Emsg0(M_FATAL, 0, dev->errmsg);
158    }
159    if ((errstat = pthread_cond_init(&dev->wait, NULL)) != 0) {
160       dev->dev_errno = errstat;
161       Mmsg1(&dev->errmsg, _("Unable to init cond variable: ERR=%s\n"), strerror(errstat));
162       Emsg0(M_FATAL, 0, dev->errmsg);
163    }
164    if ((errstat = pthread_cond_init(&dev->wait_next_vol, NULL)) != 0) {
165       dev->dev_errno = errstat;
166       Mmsg1(&dev->errmsg, _("Unable to init cond variable: ERR=%s\n"), strerror(errstat));
167       Emsg0(M_FATAL, 0, dev->errmsg);
168    }
169    dev->fd = -1;
170    Dmsg2(29, "init_dev: tape=%d dev_name=%s\n", dev_is_tape(dev), dev->dev_name);
171    return dev;
172 }
173
174 /* Open the device with the operating system and
175  * initialize buffer pointers.
176  *
177  * Note, for a tape, the VolName is the name we give to the
178  *    volume (not really used here), but for a file, the
179  *    VolName represents the name of the file to be created/opened.
180  *    In the case of a file, the full name is the device name
181  *    (archive_name) with the VolName concatenated.
182  */
183 int
184 open_dev(DEVICE *dev, char *VolName, int mode)
185 {
186    POOLMEM *archive_name;
187
188    if (dev->state & ST_OPENED) {
189       /*
190        *  *****FIXME***** how to handle two threads wanting
191        *  different volumes mounted???? E.g. one is waiting
192        *  for the next volume to be mounted, and a new job
193        *  starts and snatches up the device.
194        */
195       if (VolName && strcmp(dev->VolCatInfo.VolCatName, VolName) != 0) {
196          return -1;
197       }
198       dev->use_count++;
199       Mmsg2(&dev->errmsg, _("WARNING!!!! device %s opened %d times!!!\n"), 
200             dev->dev_name, dev->use_count);
201       Emsg0(M_WARNING, 0, dev->errmsg);
202       return dev->fd;
203    }
204    if (VolName) {
205       strcpy(dev->VolCatInfo.VolCatName, VolName);
206    }
207
208    Dmsg3(29, "open_dev: tape=%d dev_name=%s vol=%s\n", dev_is_tape(dev), 
209          dev->dev_name, dev->VolCatInfo.VolCatName);
210    dev->state &= ~(ST_LABEL|ST_APPEND|ST_READ|ST_EOT|ST_WEOT|ST_EOF);
211    if (dev->state & ST_TAPE) {
212       int timeout;
213       Dmsg0(29, "open_dev: device is tape\n");
214       if (mode == READ_WRITE) {
215          dev->mode = O_RDWR | O_BINARY;
216       } else {
217          dev->mode = O_RDONLY | O_BINARY;
218       }
219       timeout = dev->max_open_wait;
220       errno = 0;
221       while ((dev->fd = open(dev->dev_name, dev->mode, MODE_RW)) < 0) {
222          if (errno == EBUSY && timeout-- > 0) {
223             Dmsg2(100, "Device %s busy. ERR=%s\n", dev->dev_name, strerror(errno));
224             sleep(1);
225             continue;
226          }
227          dev->dev_errno = errno;
228          Mmsg2(&dev->errmsg, _("stored: unable to open device %s: ERR=%s\n"), 
229                dev->dev_name, strerror(dev->dev_errno));
230          Emsg0(M_FATAL, 0, dev->errmsg);
231          break;
232       }
233       if (dev->fd >= 0) {
234          dev->dev_errno = 0;
235          dev->state |= ST_OPENED;
236          dev->use_count++;
237          update_pos_dev(dev);             /* update position */
238       }
239       Dmsg1(29, "open_dev: tape %d opened\n", dev->fd);
240    } else {
241       /*
242        * Handle opening of file
243        */
244       archive_name = get_pool_memory(PM_FNAME);
245       strcpy(archive_name, dev->dev_name);
246       if (archive_name[strlen(archive_name)] != '/') {
247          strcat(archive_name, "/");
248       }
249       strcat(archive_name, VolName);
250       Dmsg1(29, "open_dev: device is disk %s\n", archive_name);
251       if (mode == READ_WRITE) {
252          dev->mode = O_CREAT | O_RDWR | O_BINARY;
253       } else {
254          dev->mode = O_RDONLY | O_BINARY;
255       }
256       if ((dev->fd = open(archive_name, dev->mode, MODE_RW)) < 0) {
257          dev->dev_errno = errno;
258          Mmsg2(&dev->errmsg, _("Could not open: %s, ERR=%s\n"), archive_name, strerror(dev->dev_errno));
259          Emsg0(M_FATAL, 0, dev->errmsg);
260       } else {
261          dev->dev_errno = 0;
262          dev->state |= ST_OPENED;
263          dev->use_count++;
264          update_pos_dev(dev);                /* update position */
265       }
266       Dmsg1(29, "open_dev: disk fd=%d opened\n", dev->fd);
267       free_pool_memory(archive_name);
268    }
269    return dev->fd;
270 }
271
272 /*
273  * Rewind the device.
274  *  Returns: 1 on success
275  *           0 on failure
276  */
277 int rewind_dev(DEVICE *dev)
278 {
279    struct mtop mt_com;
280    unsigned int i;
281
282    Dmsg0(29, "rewind_dev\n");
283    if (dev->fd < 0) {
284       dev->dev_errno = EBADF;
285       Mmsg1(&dev->errmsg, _("Bad call to rewind_dev. Device %s not open\n"),
286             dev->dev_name);
287       Emsg0(M_FATAL, 0, dev->errmsg);
288       return 0;
289    }
290    dev->state &= ~(ST_APPEND|ST_READ|ST_EOT | ST_EOF | ST_WEOT);  /* remove EOF/EOT flags */
291    dev->block_num = dev->file = 0;
292    dev->file_bytes = 0;
293    if (dev->state & ST_TAPE) {
294       mt_com.mt_op = MTREW;
295       mt_com.mt_count = 1;
296       /* If we get an I/O error on rewind, it is probably because
297        * the drive is actually busy. We loop for (about 5 minutes)
298        * retrying every 5 seconds.
299        */
300       for (i=dev->max_rewind_wait; ; i -= 5) {
301          if (ioctl(dev->fd, MTIOCTOP, (char *)&mt_com) < 0) {
302             if (i == dev->max_rewind_wait) {
303                Dmsg1(200, "Rewind error, %s. retrying ...\n", strerror(errno));
304             }
305             clrerror_dev(dev, MTREW);
306             if (dev->dev_errno == EIO && i > 0) {
307                Dmsg0(200, "Sleeping 5 seconds.\n");
308                sleep(5);
309                continue;
310             }
311             Mmsg2(&dev->errmsg, _("Rewind error on %s. ERR=%s.\n"),
312                dev->dev_name, strerror(dev->dev_errno));
313             return 0;
314          }
315          break;
316       }
317    } else {
318       if (lseek(dev->fd, 0, SEEK_SET) < 0) {
319          dev->dev_errno = errno;
320          Mmsg2(&dev->errmsg, _("lseek error on %s. ERR=%s.\n"),
321             dev->dev_name, strerror(dev->dev_errno));
322          return 0;
323       }
324    }
325    return 1;
326 }
327
328 /* 
329  * Position device to end of medium (end of data)
330  *  Returns: 1 on succes
331  *           0 on error
332  */
333 int 
334 eod_dev(DEVICE *dev)
335 {
336    struct mtop mt_com;
337    struct mtget mt_stat;
338    int stat = 0;
339    int32_t pos;
340
341    Dmsg0(29, "eod_dev\n");
342    if (dev->state & ST_EOT) {
343       return 1;
344    }
345    dev->state &= ~(ST_EOF);  /* remove EOF flags */
346    dev->block_num = dev->file = 0;
347    dev->file_bytes = 0;
348    if (!(dev->state & ST_TAPE)) {
349       pos = lseek(dev->fd, 0, SEEK_END);
350       if (pos > 0) {
351          update_pos_dev(dev);
352          dev->state |= ST_EOT;
353          return 1;
354       }
355       return 0;
356    }
357    if (dev->capabilities & CAP_EOM) {
358       mt_com.mt_op = MTEOM;
359       mt_com.mt_count = 1;
360       if ((stat=ioctl(dev->fd, MTIOCTOP, (char *)&mt_com)) < 0) {
361          Dmsg1(50, "ioctl error: %s\n", strerror(dev->dev_errno));
362          clrerror_dev(dev, mt_com.mt_op);
363          update_pos_dev(dev);
364          Mmsg2(&dev->errmsg, _("ioctl MTEOM error on %s. ERR=%s.\n"),
365             dev->dev_name, strerror(dev->dev_errno));
366          return 0;
367       }
368       if (ioctl(dev->fd, MTIOCGET, (char *)&mt_stat) < 0) {
369          dev->dev_errno = errno;
370          Mmsg2(&dev->errmsg, _("ioctl MTIOCGET error on %s. ERR=%s.\n"),
371             dev->dev_name, strerror(dev->dev_errno));
372          return 0;
373       }
374       Dmsg2(200, "EOD file=%d block=%d\n", mt_stat.mt_fileno, mt_stat.mt_blkno);
375       dev->file = mt_stat.mt_fileno;
376
377    /*
378     * Rewind then use FSF until EOT reached
379     */
380    } else {
381       if (!rewind_dev(dev)) {
382          return 0;
383       }
384       while (!(dev->state & ST_EOT)) {
385          Dmsg0(200, "Do fsf 1\n");
386          if (fsf_dev(dev, 1) < 0) {
387             Dmsg0(200, "fsf_dev return < 0\n");
388             return 0;
389          }
390       }
391    }
392    update_pos_dev(dev);                      /* update position */
393    Dmsg1(200, "EOD dev->file=%d\n", dev->file);
394    return 1;
395 }
396
397 /*
398  * Set the position of the device.
399  *  Returns: 1 on succes
400  *           0 on error
401  */
402 int update_pos_dev(DEVICE *dev)
403 {
404 #ifdef xxxx
405    struct mtget mt_stat;
406 #endif
407    int32_t pos;
408    int stat = 0;
409
410    if (dev->fd < 0) {
411       dev->dev_errno = EBADF;
412       Mmsg0(&dev->errmsg, _("Bad device call. Archive not open\n"));
413       Emsg0(M_FATAL, 0, dev->errmsg);
414       return 0;
415    }
416
417    /* Find out where we are */
418    if (!(dev->state & ST_TAPE)) {
419       dev->file = 0;
420       dev->file_bytes = 0;
421       pos = lseek(dev->fd, 0, SEEK_CUR);
422       if (pos < 0) {
423          Dmsg1(200, "Seek error: ERR=%s\n", strerror(dev->dev_errno));
424          pos = 0;
425          dev->dev_errno = errno;
426          Mmsg2(&dev->errmsg, _("lseek error on %s. ERR=%s.\n"),
427             dev->dev_name, strerror(dev->dev_errno));
428       } else {
429          stat = 1;
430       }
431       return stat;
432    }
433
434 #ifdef REALLY_IMPLEMENTED
435    if (ioctl(dev->fd, MTIOCGET, (char *)&mt_stat) < 0) {
436       Dmsg1(50, "MTIOCGET error: %s\n", strerror(dev->dev_errno));
437       dev->dev_errno = errno;
438       Mmsg2(&dev->errmsg, _("ioctl MTIOCGET error on %s. ERR=%s.\n"),
439          dev->dev_name, strerror(dev->dev_errno));
440    } else {
441       stat = 1;
442    }
443    return stat;
444 #endif
445    return 1;
446 }
447
448
449 /* 
450  * Return the status of the device.  This was meant
451  * to be a generic routine. Unfortunately, it doesn't
452  * seem possible (at least I do not know how to do it
453  * currently), which means that for the moment, this
454  * routine has very little value.
455  *
456  *   Returns: 1 on success
457  *            0 on error
458  */
459 int
460 status_dev(DEVICE *dev, uint32_t *status)
461 {
462    struct mtget mt_stat;
463    uint32_t stat = 0;
464
465    if (dev->state & (ST_EOT | ST_WEOT)) {
466       stat |= MT_EOD;
467       Dmsg0(-20, " EOD");
468    }
469    if (dev->state & ST_EOF) {
470       stat |= MT_EOF;
471       Dmsg0(-20, " EOF");
472    }
473    if (dev->state & ST_TAPE) {
474       stat |= MT_TAPE;
475       Dmsg0(-20," Driver status:");
476       Dmsg2(-20," file=%d block=%d\n", dev->file, dev->block_num);
477       if (ioctl(dev->fd, MTIOCGET, (char *)&mt_stat) < 0) {
478          dev->dev_errno = errno;
479          Mmsg2(&dev->errmsg, _("ioctl MTIOCGET error on %s. ERR=%s.\n"),
480             dev->dev_name, strerror(dev->dev_errno));
481          return 0;
482       }
483       Dmsg0(-20, " Device status:");
484
485 #if defined(HAVE_LINUX_OS)
486       if (GMT_EOF(mt_stat.mt_gstat)) {
487          stat |= MT_EOF;
488          Dmsg0(-20, " EOF");
489       }
490       if (GMT_BOT(mt_stat.mt_gstat)) {
491          stat |= MT_BOT;
492          Dmsg0(-20, " BOT");
493       }
494       if (GMT_EOT(mt_stat.mt_gstat)) {
495          stat |= MT_EOT;
496          Dmsg0(-20, " EOT");
497       }
498       if (GMT_SM(mt_stat.mt_gstat)) {
499          stat |= MT_SM;
500          Dmsg0(-20, " SM");
501       }
502       if (GMT_EOD(mt_stat.mt_gstat)) {
503          stat |= MT_EOD;
504          Dmsg0(-20, " EOD");
505       }
506       if (GMT_WR_PROT(mt_stat.mt_gstat)) {
507          stat |= MT_WR_PROT;
508          Dmsg0(-20, " WR_PROT");
509       }
510       if (GMT_ONLINE(mt_stat.mt_gstat)) {
511          stat |= MT_ONLINE;
512          Dmsg0(-20, " ONLINE");
513       }
514       if (GMT_DR_OPEN(mt_stat.mt_gstat)) {
515          stat |= MT_DR_OPEN;
516          Dmsg0(-20, " DR_OPEN");       
517       }
518       if (GMT_IM_REP_EN(mt_stat.mt_gstat)) {
519          stat |= MT_IM_REP_EN;
520          Dmsg0(-20, " IM_REP_EN");
521       }
522 #endif /* !SunOS && !OSF */
523       Dmsg2(-20, " file=%d block=%d\n", mt_stat.mt_fileno, mt_stat.mt_blkno);
524    } else {
525       stat |= MT_ONLINE | MT_BOT;
526    }
527    *status = stat; 
528    return 1;
529 }
530
531
532 /*
533  * Load medium in device
534  *  Returns: 1 on success
535  *           0 on failure
536  */
537 int load_dev(DEVICE *dev)
538 {
539 #ifdef MTLOAD
540    struct mtop mt_com;
541 #endif
542
543    if (dev->fd < 0) {
544       dev->dev_errno = EBADF;
545       Mmsg0(&dev->errmsg, _("Bad call to load_dev. Archive not open\n"));
546       Emsg0(M_FATAL, 0, dev->errmsg);
547       return 0;
548    }
549    if (!(dev->state & ST_TAPE)) {
550       return 1;
551    }
552 #ifndef MTLOAD
553    Dmsg0(200, "stored: MTLOAD command not available\n");
554    dev->dev_errno = ENOTTY;           /* function not available */
555    Mmsg2(&dev->errmsg, _("ioctl MTLOAD error on %s. ERR=%s.\n"),
556          dev->dev_name, strerror(dev->dev_errno));      return 0;
557    return 0;
558 #else
559
560    dev->block_num = dev->file = 0;
561    dev->file_bytes = 0;
562    mt_com.mt_op = MTLOAD;
563    mt_com.mt_count = 1;
564    if (ioctl(dev->fd, MTIOCTOP, (char *)&mt_com) < 0) {
565       dev->dev_errno = errno;
566       Mmsg2(&dev->errmsg, _("ioctl MTLOAD error on %s. ERR=%s.\n"),
567          dev->dev_name, strerror(dev->dev_errno));      return 0;
568    }
569    return 1;
570 #endif
571 }
572
573 /*
574  * Rewind device and put it offline
575  *  Returns: 1 on success
576  *           0 on failure
577  */
578 int offline_dev(DEVICE *dev)
579 {
580    struct mtop mt_com;
581
582    if (dev->fd < 0) {
583       dev->dev_errno = EBADF;
584       Mmsg0(&dev->errmsg, _("Bad call to load_dev. Archive not open\n"));
585       Emsg0(M_FATAL, 0, dev->errmsg);
586       return 0;
587    }
588    if (!(dev->state & ST_TAPE)) {
589       return 1;
590    }
591
592    dev->block_num = dev->file = 0;
593    dev->file_bytes = 0;
594 #ifdef MTUNLOCK
595    mt_com.mt_op = MTUNLOCK;
596    mt_com.mt_count = 1;
597    ioctl(dev->fd, MTIOCTOP, (char *)&mt_com);
598 #endif
599    mt_com.mt_op = MTOFFL;
600    mt_com.mt_count = 1;
601    if (ioctl(dev->fd, MTIOCTOP, (char *)&mt_com) < 0) {
602       dev->dev_errno = errno;
603       Mmsg2(&dev->errmsg, _("ioctl MTOFFL error on %s. ERR=%s.\n"),
604          dev->dev_name, strerror(dev->dev_errno));
605       return 0;
606    }
607    Dmsg1(100, "Offlined device %s\n", dev->dev_name);
608    return 1;
609 }
610
611
612 /* 
613  * Foward space a file  
614  */
615 int
616 fsf_dev(DEVICE *dev, int num)
617
618    struct mtop mt_com;
619    int stat = 0;
620    char rbuf[1024];
621
622    if (dev->fd < 0) {
623       dev->dev_errno = EBADF;
624       Mmsg0(&dev->errmsg, _("Bad call to fsf_dev. Archive not open\n"));
625       Emsg0(M_FATAL, 0, dev->errmsg);
626       return -1;
627    }
628
629    if (!(dev->state & ST_TAPE)) {
630       return 0;
631    }
632    if (dev->state & ST_EOT) {
633       dev->dev_errno = 0;
634       Mmsg1(&dev->errmsg, _("Device %s at End of Tape.\n"), dev->dev_name);
635       return -1;
636    }
637    if (dev->state & ST_EOF)
638       Dmsg0(200, "ST_EOF set on entry to FSF\n");
639    if (dev->state & ST_EOT)
640       Dmsg0(200, "ST_EOT set on entry to FSF\n");
641       
642    Dmsg0(29, "fsf_dev\n");
643    dev->block_num = 0;
644    if (dev->capabilities & CAP_FSF) {
645       Dmsg0(200, "FSF has cap_fsf\n");
646       mt_com.mt_op = MTFSF;
647       mt_com.mt_count = 1;
648       while (num-- && !(dev->state & ST_EOT)) {
649          Dmsg0(200, "Doing read for fsf\n");
650          if ((stat = read(dev->fd, rbuf, sizeof(rbuf))) < 0) {
651             if (errno == ENOMEM) {     /* tape record exceeds buf len */
652                stat = sizeof(rbuf);   /* This is OK */
653             } else {
654                dev->state |= ST_EOT;
655                clrerror_dev(dev, -1);
656                Dmsg1(200, "Set ST_EOT read error %d\n", dev->dev_errno);
657                Mmsg2(&dev->errmsg, _("read error on %s. ERR=%s.\n"),
658                   dev->dev_name, strerror(dev->dev_errno));
659                Dmsg1(200, "%s", dev->errmsg);
660                break;
661             }
662          }
663          if (stat == 0) {                /* EOF */
664             update_pos_dev(dev);
665             Dmsg1(200, "End of File mark from read. File=%d\n", dev->file+1);
666             /* Two reads of zero means end of tape */
667             if (dev->state & ST_EOF) {
668                dev->state |= ST_EOT;
669                Dmsg0(200, "Set ST_EOT\n");
670                break;
671             } else {
672                dev->state |= ST_EOF;
673                dev->file++;
674                dev->file_bytes = 0;
675                continue;
676             }
677          } else {                        /* Got data */
678             dev->state &= ~(ST_EOF|ST_EOT);
679          }
680
681          Dmsg0(200, "Doing MT_FSF\n");
682          stat = ioctl(dev->fd, MTIOCTOP, (char *)&mt_com);
683          if (stat < 0) {                 /* error => EOT */
684             dev->state |= ST_EOT;
685             Dmsg0(200, "Set ST_EOT\n");
686             clrerror_dev(dev, MTFSF);
687             Mmsg2(&dev->errmsg, _("ioctl MTFSF error on %s. ERR=%s.\n"),
688                dev->dev_name, strerror(dev->dev_errno));
689             Dmsg0(200, "Got < 0 for MT_FSF\n");
690             Dmsg1(200, "%s", dev->errmsg);
691          } else {
692             dev->state |= ST_EOF;     /* just read EOF */
693             dev->file++;
694             dev->file_bytes = 0;
695          }   
696       }
697    
698    /*
699     * No FSF, so use FSR to simulate it
700     */
701    } else {
702       Dmsg0(200, "Doing FSR for FSF\n");
703       while (num-- && !(dev->state & ST_EOT)) {
704          fsr_dev(dev, INT32_MAX);    /* returns -1 on EOF or EOT */
705       }
706       if (dev->state & ST_EOT) {
707          dev->dev_errno = 0;
708          Mmsg1(&dev->errmsg, _("Device %s at End of Tape.\n"), dev->dev_name);
709          stat = -1;
710       } else {
711          stat = 0;
712       }
713    }
714    update_pos_dev(dev);
715    Dmsg1(200, "Return %d from FSF\n", stat);
716    if (dev->state & ST_EOF)
717       Dmsg0(200, "ST_EOF set on exit FSF\n");
718    if (dev->state & ST_EOT)
719       Dmsg0(200, "ST_EOT set on exit FSF\n");
720    Dmsg1(200, "Return from FSF file=%d\n", dev->file);
721    return stat;
722 }
723
724 /* 
725  * Backward space a file  
726  */
727 int
728 bsf_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, "bsf_dev\n");
744    dev->state &= ~(ST_EOT|ST_EOF);
745    dev->file -= num;
746    dev->file_bytes = 0;
747    mt_com.mt_op = MTBSF;
748    mt_com.mt_count = num;
749    stat = ioctl(dev->fd, MTIOCTOP, (char *)&mt_com);
750    if (stat < 0) {
751       clrerror_dev(dev, MTBSF);
752       Mmsg2(&dev->errmsg, _("ioctl MTBSF error on %s. ERR=%s.\n"),
753          dev->dev_name, strerror(dev->dev_errno));
754    }
755    update_pos_dev(dev);
756    return stat;
757 }
758
759
760 /* 
761  * Foward space a record
762  */
763 int
764 fsr_dev(DEVICE *dev, int num)
765
766    struct mtop mt_com;
767    int stat;
768
769    if (dev->fd < 0) {
770       dev->dev_errno = EBADF;
771       Mmsg0(&dev->errmsg, _("Bad call to fsf_dev. Archive not open\n"));
772       Emsg0(M_FATAL, 0, dev->errmsg);
773       return -1;
774    }
775
776    if (!(dev->state & ST_TAPE)) {
777       return 0;
778    }
779    Dmsg0(29, "fsr_dev\n");
780    dev->block_num += num;
781    mt_com.mt_op = MTFSR;
782    mt_com.mt_count = num;
783    stat = ioctl(dev->fd, MTIOCTOP, (char *)&mt_com);
784    if (stat == 0) {
785       dev->state &= ~ST_EOF;
786    } else {
787       if (dev->state & ST_EOF) {
788          dev->state |= ST_EOT;
789       } else {
790          dev->state |= ST_EOF;           /* assume EOF */
791          dev->file++;
792          dev->file_bytes = 0;
793       }
794       clrerror_dev(dev, MTFSR);
795       Mmsg2(&dev->errmsg, _("ioctl MTFSR error on %s. ERR=%s.\n"),
796          dev->dev_name, strerror(dev->dev_errno));
797    }
798    update_pos_dev(dev);
799    return stat;
800 }
801
802 /* 
803  * Backward space a record
804  */
805 int
806 bsr_dev(DEVICE *dev, int num)
807
808    struct mtop mt_com;
809    int stat;
810
811    if (dev->fd < 0) {
812       dev->dev_errno = EBADF;
813       Mmsg0(&dev->errmsg, _("Bad call to fsf_dev. Archive not open\n"));
814       Emsg0(M_FATAL, 0, dev->errmsg);
815       return -1;
816    }
817
818    if (!(dev->state & ST_TAPE)) {
819       return 0;
820    }
821    Dmsg0(29, "bsr_dev\n");
822    dev->block_num -= num;
823    dev->state &= ~(ST_EOF|ST_EOT|ST_EOF);
824    mt_com.mt_op = MTBSR;
825    mt_com.mt_count = num;
826    stat = ioctl(dev->fd, MTIOCTOP, (char *)&mt_com);
827    if (stat < 0) {
828       clrerror_dev(dev, MTBSR);
829       Mmsg2(&dev->errmsg, _("ioctl MTBSR error on %s. ERR=%s.\n"),
830          dev->dev_name, strerror(dev->dev_errno));
831    }
832    update_pos_dev(dev);
833    return stat;
834 }
835
836
837
838 /*
839  * Write an end of file on the device
840  */
841 int 
842 weof_dev(DEVICE *dev, int num)
843
844    struct mtop mt_com;
845    int stat;
846
847    if (dev->fd < 0) {
848       dev->dev_errno = EBADF;
849       Mmsg0(&dev->errmsg, _("Bad call to fsf_dev. Archive not open\n"));
850       Emsg0(M_FATAL, 0, dev->errmsg);
851       return -1;
852    }
853
854    if (!(dev->state & ST_TAPE)) {
855       return 0;
856    }
857    dev->state &= ~(ST_EOT | ST_EOF);  /* remove EOF/EOT flags */
858    dev->block_num = 0;
859    Dmsg0(29, "weof_dev\n");
860    mt_com.mt_op = MTWEOF;
861    mt_com.mt_count = num;
862    stat = ioctl(dev->fd, MTIOCTOP, (char *)&mt_com);
863    if (stat == 0) {
864       dev->file++;
865       dev->file_bytes = 0;
866    } else {
867       clrerror_dev(dev, MTWEOF);
868       Mmsg2(&dev->errmsg, _("ioctl MTWEOF error on %s. ERR=%s.\n"),
869          dev->dev_name, strerror(dev->dev_errno));
870    }
871    return stat;
872 }
873
874 /*
875  * Return string message with last error in English
876  *  Be careful not to call this routine from within dev.c
877  *  while editing an Mmsg(&) or you will end up in a recursive
878  *  loop creating a Segmentation Violation.
879  */
880 char *
881 strerror_dev(DEVICE *dev)
882 {
883    return dev->errmsg;
884 }
885
886
887 /*
888  * If implemented in system, clear the tape
889  * error status.
890  */
891 void
892 clrerror_dev(DEVICE *dev, int func)
893 {
894    char *msg = NULL;
895
896    dev->dev_errno = errno;         /* save errno */
897    if (errno == EIO) {
898       dev->VolCatInfo.VolCatErrors++;
899    }
900
901    if (!(dev->state & ST_TAPE)) {
902       return;
903    }
904    if (errno == ENOTTY || errno == ENOSYS) { /* Function not implemented */
905       switch (func) {
906          case -1:
907             Emsg0(M_ABORT, 0, "Got ENOTTY on read/write!\n");
908             break;
909          case MTWEOF:
910             msg = "WTWEOF";
911             dev->capabilities &= ~CAP_EOF; /* turn off feature */
912             break;
913          case MTEOM:
914             msg = "WTEOM";
915             dev->capabilities &= ~CAP_EOM; /* turn off feature */
916             break;
917          case MTFSF:
918             msg = "MTFSF";
919             dev->capabilities &= ~CAP_FSF; /* turn off feature */
920             break;
921          case MTBSF:
922             msg = "MTBSF";
923             dev->capabilities &= ~CAP_BSF; /* turn off feature */
924             break;
925          case MTFSR:
926             msg = "MTFSR";
927             dev->capabilities &= ~CAP_FSR; /* turn off feature */
928             break;
929          case MTBSR:
930             msg = "MTBSR";
931             dev->capabilities &= ~CAP_BSR; /* turn off feature */
932             break;
933          default:
934             msg = "Unknown";
935             break;
936       }
937       if (msg != NULL) {
938          dev->dev_errno = ENOSYS;
939          Mmsg1(&dev->errmsg, _("This device does not support %s.\n"), msg);
940          Emsg0(M_ERROR, 0, dev->errmsg);
941       }
942    }
943 #ifdef MTIOCLRERR
944 {
945    struct mtop mt_com;
946    int stat;
947    mt_com.mt_op = MTIOCLRERR;
948    mt_com.mt_count = 1;
949    stat = ioctl(dev->fd, MTIOCTOP, (char *)&mt_com);
950    Dmsg0(200, "Did MTIOCLRERR\n");
951 }
952 #endif
953 }
954
955 /*
956  * Flush buffer contents
957  *  No longer used.
958  */
959 int flush_dev(DEVICE *dev)
960 {
961    return 1;
962 }
963
964 static void do_close(DEVICE *dev)
965 {
966
967    Dmsg0(29, "really close_dev\n");
968    close(dev->fd);
969    /* Clean up device packet so it can be reused */
970    dev->fd = -1;
971    dev->state &= ~(ST_OPENED|ST_LABEL|ST_READ|ST_APPEND|ST_EOT|ST_WEOT|ST_EOF);
972    dev->block_num = 0;
973    dev->file = 0;
974    dev->file_bytes = 0;
975    dev->LastBlockNumWritten = 0;
976    memset(&dev->VolCatInfo, 0, sizeof(dev->VolCatInfo));
977    memset(&dev->VolHdr, 0, sizeof(dev->VolHdr));
978    dev->use_count--;
979 }
980
981 /* 
982  * Close the device
983  */
984 void
985 close_dev(DEVICE *dev)
986 {
987    if (!dev) {
988       Mmsg0(&dev->errmsg, _("Bad call to fsf_dev. Archive not open\n"));
989       Emsg0(M_FATAL, 0, dev->errmsg);
990       return;
991    }
992    if (dev->fd >= 0 && dev->use_count == 1) {
993       do_close(dev);
994    } else {    
995       Dmsg0(29, "close_dev but in use so leave open.\n");
996       dev->use_count--;
997    }
998 }
999
1000 /*
1001  * Used when unmounting the device
1002  */
1003 void force_close_dev(DEVICE *dev)
1004 {
1005    if (!dev) {
1006       Mmsg0(&dev->errmsg, _("Bad call to fsf_dev. Archive not open\n"));
1007       Emsg0(M_FATAL, 0, dev->errmsg);
1008       return;
1009    }
1010    Dmsg0(29, "really close_dev\n");
1011    do_close(dev);
1012 }
1013
1014 int truncate_dev(DEVICE *dev)
1015 {
1016    if (dev->state & ST_TAPE) {
1017       return 1;
1018    }
1019    if (ftruncate(dev->fd, 0) != 0) {
1020       Mmsg1(&dev->errmsg, _("Unable to truncate device. ERR=%s\n"), strerror(errno));
1021       return 0;
1022    }
1023    return 1;
1024 }
1025
1026 int 
1027 dev_is_tape(DEVICE *dev)
1028 {  
1029    return (dev->state & ST_TAPE) ? 1 : 0;
1030 }
1031
1032 char *
1033 dev_name(DEVICE *dev)
1034 {
1035    return dev->dev_name;
1036 }
1037
1038 char *
1039 dev_vol_name(DEVICE *dev)
1040 {
1041    return dev->VolCatInfo.VolCatName;
1042 }
1043
1044 uint32_t dev_block(DEVICE *dev)
1045 {
1046    update_pos_dev(dev);
1047    return dev->block_num;
1048 }
1049
1050 uint32_t dev_file(DEVICE *dev)
1051 {
1052    update_pos_dev(dev);
1053    return dev->file;
1054 }
1055
1056 /* 
1057  * Free memory allocated for the device
1058  */
1059 void
1060 term_dev(DEVICE *dev)
1061 {
1062    if (!dev) {
1063       dev->dev_errno = EBADF;
1064       Mmsg0(&dev->errmsg, _("Bad call to fsf_dev. Archive not open\n"));
1065       Emsg0(M_FATAL, 0, dev->errmsg);
1066       return;
1067    }
1068    close_dev(dev);
1069    Dmsg0(29, "term_dev\n");
1070    if (dev->dev_name) {
1071       free_memory(dev->dev_name);
1072       dev->dev_name = NULL;
1073    }
1074    if (dev->errmsg) {
1075       free_pool_memory(dev->errmsg);
1076       dev->errmsg = NULL;
1077    }
1078 #ifdef NEW_LOCK
1079    rwl_destroy(&dev->lock);
1080 #endif
1081    pthread_mutex_destroy(&dev->mutex);
1082    pthread_cond_destroy(&dev->wait);
1083    pthread_cond_destroy(&dev->wait_next_vol);
1084    if (dev->state & ST_MALLOC) {
1085       free_pool_memory((POOLMEM *)dev);
1086    }
1087 }
1088
1089
1090 /* To make following two functions more readable */
1091
1092 #define attached_jcrs ((JCR *)(dev->attached_jcrs))
1093
1094 void attach_jcr_to_device(DEVICE *dev, JCR *jcr)
1095 {
1096    jcr->prev_dev = NULL;
1097    jcr->next_dev = attached_jcrs;
1098    if (attached_jcrs) {
1099       attached_jcrs->prev_dev = jcr;
1100    }
1101    attached_jcrs = jcr;
1102    Dmsg1(100, "Attached Job %s\n", jcr->Job);
1103 }
1104
1105 void detach_jcr_from_device(DEVICE *dev, JCR *jcr)
1106 {
1107    if (!jcr->prev_dev) {
1108       attached_jcrs = jcr->next_dev;
1109    } else {
1110       jcr->prev_dev->next_dev = jcr->next_dev;
1111    }
1112    if (jcr->next_dev) {
1113       jcr->next_dev->prev_dev = jcr->prev_dev; 
1114    }
1115    jcr->next_dev = jcr->prev_dev = NULL;
1116    Dmsg1(100, "Detached Job %s\n", jcr->Job);
1117 }
1118
1119 JCR *next_attached_jcr(DEVICE *dev, JCR *jcr)
1120 {
1121    if (jcr == NULL) {
1122       return attached_jcrs;
1123    }
1124    return jcr->next_dev;
1125 }