]> git.sur5r.net Git - bacula/bacula/blob - bacula/src/stored/dev.c
Make 5 min Heartbeat the default
[bacula/bacula] / bacula / src / stored / dev.c
1 /*
2    Bacula® - The Network Backup Solution
3
4    Copyright (C) 2000-2014 Free Software Foundation Europe e.V.
5
6    The main author of Bacula is Kern Sibbald, with contributions from many
7    others, a complete list can be found in the file AUTHORS.
8
9    You may use this file and others of this release according to the
10    license defined in the LICENSE file, which includes the Affero General
11    Public License, v3.0 ("AGPLv3") and some additional permissions and
12    terms pursuant to its AGPLv3 Section 7.
13
14    Bacula® is a registered trademark of Kern Sibbald.
15 */
16 /*
17  *
18  *   dev.c  -- low level operations on device (storage device)
19  *
20  *     written by, Kern Sibbald, MM
21  *
22  *     NOTE!!!! None of these routines are reentrant. You must
23  *        use dev->rLock() and dev->Unlock() at a higher level,
24  *        or use the xxx_device() equivalents.  By moving the
25  *        thread synchronization to a higher level, we permit
26  *        the higher level routines to "seize" the device and
27  *        to carry out operations without worrying about who
28  *        set what lock (i.e. race conditions).
29  *
30  *     Note, this is the device dependent code, and may have
31  *           to be modified for each system, but is meant to
32  *           be as "generic" as possible.
33  *
34  *     The purpose of this code is to develop a SIMPLE Storage
35  *     daemon. More complicated coding (double buffering, writer
36  *     thread, ...) is left for a later version.
37  *
38  */
39
40 /*
41  * Handling I/O errors and end of tape conditions are a bit tricky.
42  * This is how it is currently done when writing.
43  * On either an I/O error or end of tape,
44  * we will stop writing on the physical device (no I/O recovery is
45  * attempted at least in this daemon). The state flag will be sent
46  * to include ST_EOT, which is ephemeral, and ST_WEOT, which is
47  * persistent. Lots of routines clear ST_EOT, but ST_WEOT is
48  * cleared only when the problem goes away.  Now when ST_WEOT
49  * is set all calls to write_block_to_device() call the fix_up
50  * routine. In addition, all threads are blocked
51  * from writing on the tape by calling lock_dev(), and thread other
52  * than the first thread to hit the EOT will block on a condition
53  * variable. The first thread to hit the EOT will continue to
54  * be able to read and write the tape (he sort of tunnels through
55  * the locking mechanism -- see lock_dev() for details).
56  *
57  * Now presumably somewhere higher in the chain of command
58  * (device.c), someone will notice the EOT condition and
59  * get a new tape up, get the tape label read, and mark
60  * the label for rewriting. Then this higher level routine
61  * will write the unwritten buffer to the new volume.
62  * Finally, he will release
63  * any blocked threads by doing a broadcast on the condition
64  * variable.  At that point, we should be totally back in
65  * business with no lost data.
66  */
67
68 #include "bacula.h"
69 #include "stored.h"
70
71 #ifndef O_NONBLOCK
72 #define O_NONBLOCK 0
73 #endif
74
75 /* Imported functions */
76 extern void set_os_device_parameters(DCR *dcr);
77 extern bool dev_get_os_pos(DEVICE *dev, struct mtget *mt_stat);
78 extern uint32_t status_dev(DEVICE *dev);
79
80 /* Forward referenced functions */
81 const char *mode_to_str(int mode);
82 DEVICE *m_init_dev(JCR *jcr, DEVRES *device, bool alt);
83 /*
84  * Device types for printing
85  */
86 static const char *prt_dev_types[] = {
87    "*none*",
88    "file",
89    "tape",
90    "DVD",
91    "FIFO",
92    "Vtape",
93    "FTP",
94    "VTL",
95    "virtual"
96 };
97
98 /*
99  * Allocate and initialize the DEVICE structure
100  * Note, if dev is non-NULL, it is already allocated,
101  * thus we neither allocate it nor free it. This allows
102  * the caller to put the packet in shared memory.
103  *
104  *  Note, for a tape, the device->device_name is the device name
105  *     (e.g. /dev/nst0), and for a file, the device name
106  *     is the directory in which the file will be placed.
107  *
108  */
109 DEVICE *init_dev(JCR *jcr, DEVRES *device)
110 {
111    generate_global_plugin_event(bsdGlobalEventDeviceInit, device);
112    DEVICE *dev = m_init_dev(jcr, device, false);
113    return dev;
114 }
115
116 DEVICE *m_init_dev(JCR *jcr, DEVRES *device, bool alt)
117 {
118    struct stat statp;
119    int errstat;
120    DCR *dcr = NULL;
121    DEVICE *dev;
122    uint32_t max_bs;
123
124    /* If no device type specified, try to guess */
125    if (!device->dev_type) {
126       /* Check that device is available */
127       if (stat(device->device_name, &statp) < 0) {
128          berrno be;
129          Jmsg2(jcr, M_ERROR, 0, _("Unable to stat device %s: ERR=%s\n"),
130             device->device_name, be.bstrerror());
131          return NULL;
132       }
133       if (S_ISDIR(statp.st_mode)) {
134          device->dev_type = B_FILE_DEV;
135       } else if (S_ISCHR(statp.st_mode)) {
136          device->dev_type = B_TAPE_DEV;
137       } else if (S_ISFIFO(statp.st_mode)) {
138          device->dev_type = B_FIFO_DEV;
139 #ifdef USE_VTAPE
140       /* must set DeviceType = Vtape
141        * in normal mode, autodetection is disabled
142        */
143       } else if (S_ISREG(statp.st_mode)) {
144          device->dev_type = B_VTAPE_DEV;
145 #endif
146       } else if (!(device->cap_bits & CAP_REQMOUNT)) {
147          Jmsg2(jcr, M_ERROR, 0, _("%s is an unknown device type. Must be tape or directory\n"
148                " or have RequiresMount=yes for DVD. st_mode=%x\n"),
149             device->device_name, statp.st_mode);
150          return NULL;
151       } else {
152          device->dev_type = B_DVD_DEV;
153       }
154    }
155    switch (device->dev_type) {
156    case B_DVD_DEV:
157       Jmsg0(jcr, M_FATAL, 0, _("DVD support is now deprecated.\n"));
158       return NULL;
159    case B_VIRTUAL_DEV:
160       Jmsg0(jcr, M_FATAL, 0, _("Aligned device not supported. Please use \"DeviceType = File\"\n"));
161       return NULL;
162    case B_VTAPE_DEV:
163       dev = New(vtape);
164       break;
165 #ifdef USE_FTP
166    case B_FTP_DEV:
167       dev = New(ftp_device);
168       break;
169 #endif
170 #ifdef xHAVE_WIN32
171 /* TODO: defined in src/win32/stored/mtops.cpp */
172    case B_TAPE_DEV:
173       dev = New(win_tape_dev);
174       break;
175    case B_FILE_DEV:
176       dev = New(win_file_dev);
177       break;
178 #else
179    case B_TAPE_DEV:
180       dev = New(tape_dev);
181       break;
182    case B_FILE_DEV:
183    case B_FIFO_DEV:
184       dev = New(file_dev);
185       break;
186       break;
187 #endif
188    default:
189          return NULL;
190    }
191    dev->clear_slot();         /* unknown */
192
193    /* Copy user supplied device parameters from Resource */
194    dev->dev_name = get_memory(strlen(device->device_name)+1);
195    pm_strcpy(dev->dev_name, device->device_name);
196    dev->prt_name = get_memory(strlen(device->device_name) + strlen(device->hdr.name) + 20);
197    /* We edit "Resource-name" (physical-name) */
198    Mmsg(dev->prt_name, "\"%s\" (%s)", device->hdr.name, device->device_name);
199    Dmsg1(400, "Allocate dev=%s\n", dev->print_name());
200    dev->capabilities = device->cap_bits;
201    dev->min_block_size = device->min_block_size;
202    dev->max_block_size = device->max_block_size;
203    dev->max_volume_size = device->max_volume_size;
204    dev->max_file_size = device->max_file_size;
205    dev->max_concurrent_jobs = device->max_concurrent_jobs;
206    dev->volume_capacity = device->volume_capacity;
207    dev->max_rewind_wait = device->max_rewind_wait;
208    dev->max_open_wait = device->max_open_wait;
209    dev->vol_poll_interval = device->vol_poll_interval;
210    dev->max_spool_size = device->max_spool_size;
211    dev->drive_index = device->drive_index;
212    dev->autoselect = device->autoselect;
213    dev->read_only = device->read_only;
214    dev->dev_type = device->dev_type;
215    dev->device = device;
216    if (dev->is_tape()) { /* No parts on tapes */
217       dev->max_part_size = 0;
218    } else {
219       dev->max_part_size = device->max_part_size;
220    }
221    /* Sanity check */
222    if (dev->vol_poll_interval && dev->vol_poll_interval < 60) {
223       dev->vol_poll_interval = 60;
224    }
225    /*
226     * ***FIXME*** remove this when we implement write device
227     *  switching.
228     *
229     * We limit aligned Jobs to run one at a time.
230     * This is required because:
231     *  1. BlockAddr must be local for each JCR, today it
232     *     is global.
233     *  2. When flushing buffers, all other processes must
234     *     be locked out, and they currently are not.
235     *  3. We need to reserve the full space for a job, but
236     *     currently space for only one block is reserved.
237     *  4. In the case that the file grows, we must be able
238     *     to create multiple references to the correct data
239     *     blocks, if they are not contiguous.
240     */
241
242    device->dev = dev;
243
244    if (dev->is_fifo()) {
245       dev->capabilities |= CAP_STREAM; /* set stream device */
246    }
247
248    /* If the device requires mount :
249     * - Check that the mount point is available
250     * - Check that (un)mount commands are defined
251     */
252    if (dev->is_file() && dev->requires_mount()) {
253       if (!device->mount_point || stat(device->mount_point, &statp) < 0) {
254          berrno be;
255          dev->dev_errno = errno;
256          Jmsg2(jcr, M_ERROR_TERM, 0, _("Unable to stat mount point %s: ERR=%s\n"),
257             device->mount_point, be.bstrerror());
258       }
259
260       if (!device->mount_command || !device->unmount_command) {
261          Jmsg0(jcr, M_ERROR_TERM, 0, _("Mount and unmount commands must defined for a device which requires mount.\n"));
262       }
263    }
264
265    /* Sanity check */
266    if (dev->max_block_size == 0) {
267       max_bs = DEFAULT_BLOCK_SIZE;
268    } else {
269       max_bs = dev->max_block_size;
270    }
271    if (dev->min_block_size > max_bs) {
272       Jmsg(jcr, M_ERROR_TERM, 0, _("Min block size > max on device %s\n"),
273            dev->print_name());
274    }
275    if (dev->max_block_size > 4096000) {
276       Jmsg3(jcr, M_ERROR, 0, _("Block size %u on device %s is too large, using default %u\n"),
277          dev->max_block_size, dev->print_name(), DEFAULT_BLOCK_SIZE);
278       dev->max_block_size = 0;
279    }
280    if (dev->max_block_size % TAPE_BSIZE != 0) {
281       Jmsg3(jcr, M_WARNING, 0, _("Max block size %u not multiple of device %s block size=%d.\n"),
282          dev->max_block_size, dev->print_name(), TAPE_BSIZE);
283    }
284    if (dev->max_volume_size != 0 && dev->max_volume_size < (dev->max_block_size << 4)) {
285       Jmsg(jcr, M_ERROR_TERM, 0, _("Max Vol Size < 8 * Max Block Size for device %s\n"),
286            dev->print_name());
287    }
288
289    dev->errmsg = get_pool_memory(PM_EMSG);
290    *dev->errmsg = 0;
291
292    if ((errstat = dev->init_mutex()) != 0) {
293       berrno be;
294       dev->dev_errno = errstat;
295       Mmsg1(dev->errmsg, _("Unable to init mutex: ERR=%s\n"), be.bstrerror(errstat));
296       Jmsg0(jcr, M_ERROR_TERM, 0, dev->errmsg);
297    }
298    if ((errstat = pthread_cond_init(&dev->wait, NULL)) != 0) {
299       berrno be;
300       dev->dev_errno = errstat;
301       Mmsg1(dev->errmsg, _("Unable to init cond variable: ERR=%s\n"), be.bstrerror(errstat));
302       Jmsg0(jcr, M_ERROR_TERM, 0, dev->errmsg);
303    }
304    if ((errstat = pthread_cond_init(&dev->wait_next_vol, NULL)) != 0) {
305       berrno be;
306       dev->dev_errno = errstat;
307       Mmsg1(dev->errmsg, _("Unable to init cond variable: ERR=%s\n"), be.bstrerror(errstat));
308       Jmsg0(jcr, M_ERROR_TERM, 0, dev->errmsg);
309    }
310    if ((errstat = pthread_mutex_init(&dev->spool_mutex, NULL)) != 0) {
311       berrno be;
312       dev->dev_errno = errstat;
313       Mmsg1(dev->errmsg, _("Unable to init spool mutex: ERR=%s\n"), be.bstrerror(errstat));
314       Jmsg0(jcr, M_ERROR_TERM, 0, dev->errmsg);
315    }
316    if ((errstat = dev->init_acquire_mutex()) != 0) {
317       berrno be;
318       dev->dev_errno = errstat;
319       Mmsg1(dev->errmsg, _("Unable to init acquire mutex: ERR=%s\n"), be.bstrerror(errstat));
320       Jmsg0(jcr, M_ERROR_TERM, 0, dev->errmsg);
321    }
322    if ((errstat = dev->init_read_acquire_mutex()) != 0) {
323       berrno be;
324       dev->dev_errno = errstat;
325       Mmsg1(dev->errmsg, _("Unable to init read acquire mutex: ERR=%s\n"), be.bstrerror(errstat));
326       Jmsg0(jcr, M_ERROR_TERM, 0, dev->errmsg);
327    }
328    if ((errstat = dev->init_volcat_mutex()) != 0) {
329       berrno be;
330       dev->dev_errno = errstat;
331       Mmsg1(dev->errmsg, _("Unable to init volcat mutex: ERR=%s\n"), be.bstrerror(errstat));
332       Jmsg0(jcr, M_ERROR_TERM, 0, dev->errmsg);
333    }
334    if ((errstat = dev->init_dcrs_mutex()) != 0) {
335       berrno be;
336       dev->dev_errno = errstat;
337       Mmsg1(dev->errmsg, _("Unable to init dcrs mutex: ERR=%s\n"), be.bstrerror(errstat));
338       Jmsg0(jcr, M_ERROR_TERM, 0, dev->errmsg);
339    }
340
341    dev->set_mutex_priorities();
342
343 #ifdef xxx
344    if ((errstat = rwl_init(&dev->lock)) != 0) {
345       berrno be;
346       dev->dev_errno = errstat;
347       Mmsg1(dev->errmsg, _("Unable to init mutex: ERR=%s\n"), be.bstrerror(errstat));
348       Jmsg0(jcr, M_ERROR_TERM, 0, dev->errmsg);
349    }
350 #endif
351
352    dev->clear_opened();
353    dev->attached_dcrs = New(dlist(dcr, &dcr->dev_link));
354    Dmsg2(100, "init_dev: tape=%d dev_name=%s\n", dev->is_tape(), dev->dev_name);
355    dev->initiated = true;
356
357    return dev;
358 }
359
360 /*
361  * Open the device with the operating system and
362  * initialize buffer pointers.
363  *
364  * Returns:  true on success
365  *           false on error
366  *
367  * Note, for a tape, the VolName is the name we give to the
368  *    volume (not really used here), but for a file, the
369  *    VolName represents the name of the file to be created/opened.
370  *    In the case of a file, the full name is the device name
371  *    (archive_name) with the VolName concatenated.
372  */
373 bool DEVICE::open(DCR *dcr, int omode)
374 {
375    int preserve = 0;
376    if (is_open()) {
377       if (openmode == omode) {
378          return true;
379       } else {
380          Dmsg1(200, "Close fd=%d for mode change in open().\n", m_fd);
381          d_close(m_fd);
382          clear_opened();
383          preserve = state & (ST_LABEL|ST_APPEND|ST_READ);
384       }
385    }
386    if (dcr) {
387       dcr->setVolCatName(dcr->VolumeName);
388       VolCatInfo = dcr->VolCatInfo;    /* structure assign */
389    }
390
391    state &= ~(ST_NOSPACE|ST_LABEL|ST_APPEND|ST_READ|ST_EOT|ST_WEOT|ST_EOF);
392    label_type = B_BACULA_LABEL;
393
394    if (is_tape() || is_fifo()) {
395       open_tape_device(dcr, omode);
396    } else if (is_ftp()) {
397       this->open_device(dcr, omode);
398    } else {
399       Dmsg1(100, "call open_file_device mode=%s\n", mode_to_str(omode));
400       open_file_device(dcr, omode);
401    }
402    state |= preserve;                 /* reset any important state info */
403    Dmsg2(100, "preserve=0x%x fd=%d\n", preserve, m_fd);
404
405    Dmsg7(100, "open dev: fd=%d dev=%p dcr=%p vol=%s type=%d dev_name=%s mode=%s\n",
406          m_fd, getVolCatName(), this, dcr, dev_type, print_name(), mode_to_str(omode));
407    return m_fd >= 0;
408 }
409
410 void DEVICE::set_mode(int new_mode)
411 {
412    switch (new_mode) {
413    case CREATE_READ_WRITE:
414       mode = O_CREAT | O_RDWR | O_BINARY;
415       break;
416    case OPEN_READ_WRITE:
417       mode = O_RDWR | O_BINARY;
418       break;
419    case OPEN_READ_ONLY:
420       mode = O_RDONLY | O_BINARY;
421       break;
422    case OPEN_WRITE_ONLY:
423       mode = O_WRONLY | O_BINARY;
424       break;
425    default:
426       Emsg0(M_ABORT, 0, _("Illegal mode given to open dev.\n"));
427    }
428 }
429
430
431 void DEVICE::open_device(DCR *dcr, int omode)
432 {
433    /* do nothing waiting to split open_file/tape_device */
434 }
435
436
437 /*
438  * Called to indicate that we have just read an
439  *  EOF from the device.
440  */
441 void DEVICE::set_ateof()
442 {
443    set_eof();
444    if (is_tape()) {
445       file++;
446    }
447    file_addr = 0;
448    file_size = 0;
449    block_num = 0;
450 }
451
452 /*
453  * Called to indicate we are now at the end of the tape, and
454  *   writing is not possible.
455  */
456 void DEVICE::set_ateot()
457 {
458    /* Make tape effectively read-only */
459    Dmsg0(200, "==== Set AtEof\n");
460    state |= (ST_EOF|ST_EOT|ST_WEOT);
461    clear_append();
462 }
463
464
465 /*
466  * Set the position of the device -- only for files and DVD
467  *   For other devices, there is no generic way to do it.
468  *  Returns: true  on succes
469  *           false on error
470  */
471 bool DEVICE::update_pos(DCR *dcr)
472 {
473    boffset_t pos;
474    bool ok = true;
475
476    if (!is_open()) {
477       dev_errno = EBADF;
478       Mmsg0(errmsg, _("Bad device call. Device not open\n"));
479       Emsg1(M_FATAL, 0, "%s", errmsg);
480       return false;
481    }
482
483    if (is_file()) {
484       file = 0;
485       file_addr = 0;
486       pos = lseek(dcr, (boffset_t)0, SEEK_CUR);
487       if (pos < 0) {
488          berrno be;
489          dev_errno = errno;
490          Pmsg1(000, _("Seek error: ERR=%s\n"), be.bstrerror());
491          Mmsg2(errmsg, _("lseek error on %s. ERR=%s.\n"),
492                print_name(), be.bstrerror());
493          ok = false;
494       } else {
495          file_addr = pos;
496          block_num = (uint32_t)pos;
497          file = (uint32_t)(pos >> 32);
498       }
499    }
500    return ok;
501 }
502
503 void DEVICE::set_slot(int32_t slot)
504 {
505    m_slot = slot;
506    if (vol) vol->clear_slot();
507 }
508
509 void DEVICE::clear_slot()
510 {
511    m_slot = -1;
512    if (vol) vol->set_slot(-1);
513 }
514
515 const char *DEVICE::print_type() const
516 {
517    return prt_dev_types[device->dev_type];
518 }
519
520 /*
521  * Set to unload the current volume in the drive
522  */
523 void DEVICE::set_unload()
524 {
525    if (!m_unload && VolHdr.VolumeName[0] != 0) {
526        m_unload = true;
527        memcpy(UnloadVolName, VolHdr.VolumeName, sizeof(UnloadVolName));
528        notify_newvol_in_attached_dcrs(NULL);
529    }
530 }
531
532 /*
533  * Clear volume header
534  */
535 void DEVICE::clear_volhdr()
536 {
537    Dmsg1(100, "Clear volhdr vol=%s\n", VolHdr.VolumeName);
538    memset(&VolHdr, 0, sizeof(VolHdr));
539    setVolCatInfo(false);
540 }
541
542
543 /*
544  * Close the device
545  */
546 void DEVICE::close()
547 {
548    Dmsg4(40, "close_dev vol=%s fd=%d dev=%p dev=%s\n",
549       VolHdr.VolumeName, m_fd, this, print_name());
550    offline_or_rewind();
551
552    if (!is_open()) {
553       Dmsg2(200, "device %s already closed vol=%s\n", print_name(),
554          VolHdr.VolumeName);
555       return;                         /* already closed */
556    }
557
558    switch (dev_type) {
559    case B_VTL_DEV:
560    case B_VTAPE_DEV:
561    case B_TAPE_DEV:
562       unlock_door();
563       /* Fall through wanted */
564    default:
565       d_close(m_fd);
566       break;
567    }
568
569    unmount(1);                        /* do unmount if required */
570
571    /* Clean up device packet so it can be reused */
572    clear_opened();
573
574    /*
575     * Be careful not to clear items needed by the DVD driver
576     *    when it is closing a single part.
577     */
578    state &= ~(ST_LABEL|ST_READ|ST_APPEND|ST_EOT|ST_WEOT|ST_EOF|
579               ST_NOSPACE|ST_MOUNTED|ST_MEDIA|ST_SHORT);
580    label_type = B_BACULA_LABEL;
581    file = block_num = 0;
582    file_size = 0;
583    file_addr = 0;
584    EndFile = EndBlock = 0;
585    openmode = 0;
586    clear_volhdr();
587    memset(&VolCatInfo, 0, sizeof(VolCatInfo));
588    if (tid) {
589       stop_thread_timer(tid);
590       tid = 0;
591    }
592 }
593
594 /*
595  * This call closes the device, but it is used in DVD handling
596  *  where we close one part and then open the next part. The
597  *  difference between close_part() and close() is that close_part()
598  *  saves the state information of the device (e.g. the Volume lable,
599  *  the Volume Catalog record, ...  This permits opening and closing
600  *  the Volume parts multiple times without losing track of what the
601  *  main Volume parameters are.
602  */
603 void DEVICE::close_part(DCR * /*dcr*/)
604 {
605    VOLUME_LABEL saveVolHdr;
606    VOLUME_CAT_INFO saveVolCatInfo;     /* Volume Catalog Information */
607
608
609    saveVolHdr = VolHdr;               /* structure assignment */
610    saveVolCatInfo = VolCatInfo;       /* structure assignment */
611    close();                           /* close current part */
612    VolHdr = saveVolHdr;               /* structure assignment */
613    VolCatInfo = saveVolCatInfo;       /* structure assignment */
614 }
615
616 /*
617  * Mount the device.
618  * If timeout, wait until the mount command returns 0.
619  * If !timeout, try to mount the device only once.
620  */
621 bool DEVICE::mount(int timeout)
622 {
623    Dmsg0(190, "Enter mount\n");
624
625    if (is_mounted()) {
626       return true;
627    }
628
629    switch (dev_type) {
630    case B_VTL_DEV:
631    case B_VTAPE_DEV:
632    case B_TAPE_DEV:
633       if (device->mount_command) {
634          return do_tape_mount(1, timeout);
635       }
636       break;
637    case B_FILE_DEV:
638       if (requires_mount() && device->mount_command) {
639          return do_file_mount(1, timeout);
640       }
641       break;
642    default:
643       break;
644    }
645
646    return true;
647 }
648
649 /*
650  * Unmount the device
651  * If timeout, wait until the unmount command returns 0.
652  * If !timeout, try to unmount the device only once.
653  */
654 bool DEVICE::unmount(int timeout)
655 {
656    Dmsg0(100, "Enter unmount\n");
657
658    if (!is_mounted()) {
659       return true;
660    }
661
662    switch (dev_type) {
663    case B_VTL_DEV:
664    case B_VTAPE_DEV:
665    case B_TAPE_DEV:
666       if (device->unmount_command) {
667          return do_tape_mount(0, timeout);
668       }
669       break;
670    case B_FILE_DEV:
671    case B_DVD_DEV:
672       if (requires_mount() && device->unmount_command) {
673          return do_file_mount(0, timeout);
674       }
675       break;
676    default:
677       break;
678    }
679
680    return true;
681 }
682
683
684 /*
685  * Edit codes into (Un)MountCommand, Write(First)PartCommand
686  *  %% = %
687  *  %a = archive device name
688  *  %e = erase (set if cannot mount and first part)
689  *  %n = part number
690  *  %m = mount point
691  *  %v = last part name
692  *
693  *  omsg = edited output message
694  *  imsg = input string containing edit codes (%x)
695  *
696  */
697 void DEVICE::edit_mount_codes(POOL_MEM &omsg, const char *imsg)
698 {
699    const char *p;
700    const char *str;
701    char add[20];
702
703    POOL_MEM archive_name(PM_FNAME);
704
705    omsg.c_str()[0] = 0;
706    Dmsg1(800, "edit_mount_codes: %s\n", imsg);
707    for (p=imsg; *p; p++) {
708       if (*p == '%') {
709          switch (*++p) {
710          case '%':
711             str = "%";
712             break;
713          case 'a':
714             str = dev_name;
715             break;
716          case 'e':
717             if (num_dvd_parts == 0) {
718                if (truncating || blank_dvd) {
719                   str = "2";
720                } else {
721                   str = "1";
722                }
723             } else {
724                str = "0";
725             }
726             break;
727          case 'n':
728             bsnprintf(add, sizeof(add), "%d", part);
729             str = add;
730             break;
731          case 'm':
732             str = device->mount_point;
733             break;
734          default:
735             add[0] = '%';
736             add[1] = *p;
737             add[2] = 0;
738             str = add;
739             break;
740          }
741       } else {
742          add[0] = *p;
743          add[1] = 0;
744          str = add;
745       }
746       Dmsg1(1900, "add_str %s\n", str);
747       pm_strcat(omsg, (char *)str);
748       Dmsg1(1800, "omsg=%s\n", omsg.c_str());
749    }
750 }
751
752 /* return the last timer interval (ms)
753  * or 0 if something goes wrong
754  */
755 btime_t DEVICE::get_timer_count()
756 {
757    btime_t temp = last_timer;
758    last_timer = get_current_btime();
759    temp = last_timer - temp;   /* get elapsed time */
760    return (temp>0)?temp:0;     /* take care of skewed clock */
761 }
762
763 /* read from fd */
764 ssize_t DEVICE::read(void *buf, size_t len)
765 {
766    ssize_t read_len ;
767
768    get_timer_count();
769
770    read_len = d_read(m_fd, buf, len);
771
772    last_tick = get_timer_count();
773
774    DevReadTime += last_tick;
775    VolCatInfo.VolReadTime += last_tick;
776
777    if (read_len > 0) {          /* skip error */
778       DevReadBytes += read_len;
779    }
780
781    return read_len;
782 }
783
784 /* write to fd */
785 ssize_t DEVICE::write(const void *buf, size_t len)
786 {
787    ssize_t write_len ;
788
789    get_timer_count();
790
791    write_len = d_write(m_fd, buf, len);
792
793    last_tick = get_timer_count();
794
795    DevWriteTime += last_tick;
796    VolCatInfo.VolWriteTime += last_tick;
797
798    if (write_len > 0) {         /* skip error */
799       DevWriteBytes += write_len;
800    }
801
802    return write_len;
803 }
804
805 /* Return the resource name for the device */
806 const char *DEVICE::name() const
807 {
808    return device->hdr.name;
809 }
810
811 uint32_t DEVICE::get_file()
812 {
813    if (is_dvd() || is_tape()) {
814       return file;
815    } else {
816       uint64_t bytes = VolCatInfo.VolCatAmetaBytes;
817       return (uint32_t)(bytes >> 32);
818    }
819 }
820
821 uint32_t DEVICE::get_block_num()
822 {
823    if (is_dvd() || is_tape()) {
824       return block_num;
825    } else {
826       return  VolCatInfo.VolCatAmetaBlocks;
827    }
828 }
829
830 /*
831  * Walk through all attached jcrs indicating the volume has changed
832  *   Note: If you have the new VolumeName, it is passed here,
833  *     otherwise pass a NULL.
834  */
835 void
836 DEVICE::notify_newvol_in_attached_dcrs(const char *newVolumeName)
837 {
838    Dmsg2(140, "Notify dcrs of vol change. oldVolume=%s NewVolume=%s\n",
839       getVolCatName(), newVolumeName?"*None*":newVolumeName);
840    Lock_dcrs();
841    DCR *mdcr;
842    foreach_dlist(mdcr, attached_dcrs) {
843       if (mdcr->jcr->JobId == 0) {
844          continue;                 /* ignore console */
845       }
846       mdcr->NewVol = true;
847       mdcr->NewFile = true;
848       if (newVolumeName && mdcr->VolumeName != newVolumeName) {
849          bstrncpy(mdcr->VolumeName, newVolumeName, sizeof(mdcr->VolumeName));
850          Dmsg2(140, "Set NewVol=%s in JobId=%d\n", mdcr->VolumeName, mdcr->jcr->JobId);
851       }
852    }
853    Unlock_dcrs();
854 }
855
856 /*
857  * Walk through all attached jcrs indicating the File has changed
858  */
859 void
860 DEVICE::notify_newfile_in_attached_dcrs()
861 {
862    Dmsg1(140, "Notify dcrs of file change. Volume=%s\n", getVolCatName());
863    Lock_dcrs();
864    DCR *mdcr;
865    foreach_dlist(mdcr, attached_dcrs) {
866       if (mdcr->jcr->JobId == 0) {
867          continue;                 /* ignore console */
868       }
869       Dmsg1(140, "Notify JobI=%d\n", mdcr->jcr->JobId);
870       mdcr->NewFile = true;
871    }
872    Unlock_dcrs();
873 }
874
875
876
877 /*
878  * Free memory allocated for the device
879  */
880 void DEVICE::term(void)
881 {
882    DEVICE *dev = NULL;
883    Dmsg1(900, "term dev: %s\n", print_name());
884    close();
885    if (dev_name) {
886       free_memory(dev_name);
887       dev_name = NULL;
888    }
889    if (prt_name) {
890       free_memory(prt_name);
891       prt_name = NULL;
892    }
893    if (errmsg) {
894       free_pool_memory(errmsg);
895       errmsg = NULL;
896    }
897    pthread_mutex_destroy(&m_mutex);
898    pthread_cond_destroy(&wait);
899    pthread_cond_destroy(&wait_next_vol);
900    pthread_mutex_destroy(&spool_mutex);
901    if (attached_dcrs) {
902       delete attached_dcrs;
903       attached_dcrs = NULL;
904    }
905    if (device) {
906       device->dev = NULL;
907    }
908    delete this;
909    if (dev) {
910       dev->term();
911    }
912 }
913
914 /*
915  * This routine initializes the device wait timers
916  */
917 void init_device_wait_timers(DCR *dcr)
918 {
919    DEVICE *dev = dcr->dev;
920    JCR *jcr = dcr->jcr;
921
922    /* ******FIXME******* put these on config variables */
923    dev->min_wait = 60 * 60;
924    dev->max_wait = 24 * 60 * 60;
925    dev->max_num_wait = 9;              /* 5 waits =~ 1 day, then 1 day at a time */
926    dev->wait_sec = dev->min_wait;
927    dev->rem_wait_sec = dev->wait_sec;
928    dev->num_wait = 0;
929    dev->poll = false;
930
931    jcr->min_wait = 60 * 60;
932    jcr->max_wait = 24 * 60 * 60;
933    jcr->max_num_wait = 9;              /* 5 waits =~ 1 day, then 1 day at a time */
934    jcr->wait_sec = jcr->min_wait;
935    jcr->rem_wait_sec = jcr->wait_sec;
936    jcr->num_wait = 0;
937
938 }
939
940 void init_jcr_device_wait_timers(JCR *jcr)
941 {
942    /* ******FIXME******* put these on config variables */
943    jcr->min_wait = 60 * 60;
944    jcr->max_wait = 24 * 60 * 60;
945    jcr->max_num_wait = 9;              /* 5 waits =~ 1 day, then 1 day at a time */
946    jcr->wait_sec = jcr->min_wait;
947    jcr->rem_wait_sec = jcr->wait_sec;
948    jcr->num_wait = 0;
949 }
950
951
952 /*
953  * The dev timers are used for waiting on a particular device
954  *
955  * Returns: true if time doubled
956  *          false if max time expired
957  */
958 bool double_dev_wait_time(DEVICE *dev)
959 {
960    dev->wait_sec *= 2;               /* double wait time */
961    if (dev->wait_sec > dev->max_wait) {   /* but not longer than maxtime */
962       dev->wait_sec = dev->max_wait;
963    }
964    dev->num_wait++;
965    dev->rem_wait_sec = dev->wait_sec;
966    if (dev->num_wait >= dev->max_num_wait) {
967       return false;
968    }
969    return true;
970 }
971
972 static const char *modes[] = {
973    "CREATE_READ_WRITE",
974    "OPEN_READ_WRITE",
975    "OPEN_READ_ONLY",
976    "OPEN_WRITE_ONLY"
977 };
978
979
980 const char *mode_to_str(int mode)
981 {
982    static char buf[100];
983    if (mode < 1 || mode > 4) {
984       bsnprintf(buf, sizeof(buf), "BAD mode=%d", mode);
985       return buf;
986     }
987    return modes[mode-1];
988 }