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