]> git.sur5r.net Git - bacula/bacula/blob - bacula/src/stored/init_dev.c
01d6eb6f5f575fa6aac22f1a853c3ac0e0ac5d87
[bacula/bacula] / bacula / src / stored / init_dev.c
1 /*
2    Bacula(R) - The Network Backup Solution
3
4    Copyright (C) 2000-2017 Kern Sibbald
5
6    The original author of Bacula is Kern Sibbald, with contributions
7    from many 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    This notice must be preserved when any source code is
15    conveyed and/or propagated.
16
17    Bacula(R) is a registered trademark of Kern Sibbald.
18 */
19 /*
20  * Initialize a single device.
21  *
22  * This code was split from dev.c in January 2016
23  *
24  *     written by, Kern Sibbald, MM
25  */
26
27 #include "bacula.h"
28 #include "stored.h"
29 #include <dlfcn.h>
30
31 /* Define possible extensions */
32 #if defined(HAVE_WIN32)
33 #define DRV_EXT ".dll"
34 #elif defined(HAVE_DARWIN_OS)
35 #define DRV_EXT ".dylib"
36 #else
37 #define DRV_EXT ".so"
38 #endif
39
40 #ifndef RTLD_NOW
41 #define RTLD_NOW 2
42 #endif
43
44 /* Forward referenced functions */
45 extern "C" {
46 typedef DEVICE *(*newDriver_t)(JCR *jcr, DEVRES *device);
47 }
48
49 static DEVICE *load_driver(JCR *jcr, DEVRES *device);
50
51 /*
52  * Driver item for driver table
53 */
54 struct driver_item {
55    const char *name;
56    void *handle;
57    newDriver_t newDriver;
58    bool builtin;
59    bool loaded;
60 };
61
62 /*
63  * Driver table. Must be in same order as the B_xxx_DEV type
64  *   name   handle, builtin  loaded
65  */
66 static driver_item driver_tab[] = {
67 /*   name   handle, newDriver builtin  loaded */
68    {"file",    NULL, NULL,    true,  true},
69    {"tape",    NULL, NULL,    true,  true},
70    {"none",    NULL, NULL,    true,  true}, /* deprecated was DVD */
71    {"fifo",    NULL, NULL,    true,  true},
72    {"vtape",   NULL, NULL,    true,  true},
73    {"ftp",     NULL, NULL,    true,  true},
74    {"vtl",     NULL, NULL,    true,  true},
75    {"none",    NULL, NULL,    true,  true}, /* B_ADATA_DEV */
76    {"aligned", NULL, NULL,    false, false},
77    {"none",    NULL, NULL,    true,  true}, /* deprecated was old dedup */
78    {"null",    NULL, NULL,    true,  true},
79    {"none",    NULL, NULL,    true,  true}, /* deprecated B_VALIGNED_DEV */
80    {"none",    NULL, NULL,    true,  true}, /* deprecated B_VDEDUP_DEV */
81    {"cloud",   NULL, NULL,    false, false},
82    {"none",    NULL, NULL,    false, false},
83    {NULL,      NULL, NULL,    false, false}
84 };
85
86 static pthread_mutex_t mutex = PTHREAD_MUTEX_INITIALIZER;
87
88 /* The alist should be created with not_owned_by_alist argument */
89 void sd_list_loaded_drivers(alist *list)
90 {
91    for(int i=0 ; driver_tab[i].name != NULL; i++) {
92       if (driver_tab[i].loaded && !driver_tab[i].builtin) {
93          list->append((void*)driver_tab[i].name);
94       }
95    }
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, bool adata)
110 {
111    struct stat statp;
112    DEVICE *dev = NULL;
113    uint32_t n_drivers;
114
115    generate_global_plugin_event(bsdGlobalEventDeviceInit, device);
116    Dmsg1(150, "init_dev dev_type=%d\n", device->dev_type);
117    /* If no device type specified, try to guess */
118    if (!device->dev_type) {
119       /* Check that device is available */
120       if (stat(device->device_name, &statp) < 0) {
121          berrno be;
122          Jmsg3(jcr, M_ERROR, 0, _("[SE0001] Unable to stat device %s at %s: ERR=%s\n"),
123             device->hdr.name, device->device_name, be.bstrerror());
124          return NULL;
125       }
126       if (S_ISDIR(statp.st_mode)) {
127          device->dev_type = B_FILE_DEV;
128       } else if (S_ISCHR(statp.st_mode)) {
129          device->dev_type = B_TAPE_DEV;
130       } else if (S_ISFIFO(statp.st_mode)) {
131          device->dev_type = B_FIFO_DEV;
132 #ifdef USE_VTAPE
133       /* must set DeviceType = Vtape
134        * in normal mode, autodetection is disabled
135        */
136       } else if (S_ISREG(statp.st_mode)) {
137          device->dev_type = B_VTAPE_DEV;
138 #endif
139       } else if (!(device->cap_bits & CAP_REQMOUNT)) {
140          Jmsg2(jcr, M_ERROR, 0, _("[SE0002] %s is an unknown device type. Must be tape or directory."
141                " st_mode=%x\n"),
142             device->device_name, statp.st_mode);
143          return NULL;
144       }
145       if (strcmp(device->device_name, "/dev/null") == 0) {
146          device->dev_type = B_NULL_DEV;
147       }
148    }
149
150    /* Count drivers */
151    for (n_drivers=0; driver_tab[n_drivers].name; n_drivers++) { };
152    Dmsg1(100, "Num drivers=%d\n", n_drivers);
153
154    /* If invalid dev_type get out */
155    if (device->dev_type < 0 || device->dev_type > n_drivers) {
156       Jmsg2(jcr, M_FATAL, 0, _("[SF0001] Invalid device type=%d name=\"%s\"\n"),
157          device->dev_type, device->hdr.name);
158       return NULL;
159    }
160    Dmsg5(100, "loadable=%d type=%d loaded=%d name=%s handle=%p\n",
161       !driver_tab[device->dev_type-1].builtin,
162       device->dev_type,
163       driver_tab[device->dev_type-1].loaded,
164       driver_tab[device->dev_type-1].name,
165       driver_tab[device->dev_type-1].handle);
166    if (driver_tab[device->dev_type-1].builtin) {
167       /* Built-in driver */
168       switch (device->dev_type) {
169 #ifdef HAVE_WIN32
170       case B_TAPE_DEV:
171 //       dev = New(win_tape_dev);
172          break;
173       case B_ADATA_DEV:
174       case B_ALIGNED_DEV:
175       case B_FILE_DEV:
176          dev = New(win_file_dev);
177          dev->capabilities |= CAP_LSEEK;
178          break;
179       case B_NULL_DEV:
180          dev = New(win_file_dev);
181          break;
182 #else
183       case B_VTAPE_DEV:
184          dev = New(vtape);
185          break;
186       case B_TAPE_DEV:
187          dev = New(tape_dev);
188          break;
189       case B_FILE_DEV:
190          dev = New(file_dev);
191          dev->capabilities |= CAP_LSEEK;
192          break;
193       case B_NULL_DEV:
194          dev = New(null_dev);
195          break;
196       case B_FIFO_DEV:
197          dev = New(fifo_dev);
198          break;
199 #endif
200       default:
201          Jmsg2(jcr, M_FATAL, 0, _("[SF0002] Unknown device type=%d device=\"%s\"\n"),
202             device->dev_type, device->hdr.name);
203          return NULL;
204       }
205    } else {
206       /* Loadable driver */
207       dev = load_driver(jcr, device);
208    }
209    if (!dev) {
210       return NULL;
211    }
212
213    dev->adata = adata;
214
215    /* Keep the device ID in the DEVICE struct to identify the hardware */
216    if (dev->is_file() && stat(dev->archive_name(), &statp) == 0) {
217       dev->devno = statp.st_dev;
218    }
219
220    dev->device_generic_init(jcr, device);
221
222    /* Do device specific initialization */
223    dev->device_specific_init(jcr, device);
224
225    /* ***FIXME*** move to fifo driver */
226    if (dev->is_fifo()) {
227       dev->capabilities |= CAP_STREAM; /* set stream device */
228    }
229
230    return dev;
231 }
232
233 /*
234  * Do all the generic initialization here. Same for all devices.
235  */
236 void DEVICE::device_generic_init(JCR *jcr, DEVRES *device)
237 {
238    struct stat statp;
239    DEVICE *dev = this;
240    DCR *dcr = NULL;
241    int errstat;
242    uint32_t max_bs;
243
244    dev->clear_slot();         /* unknown */
245
246    /* Copy user supplied device parameters from Resource */
247    dev->dev_name = get_memory(strlen(device->device_name)+1);
248    pm_strcpy(dev->dev_name, device->device_name);
249    dev->prt_name = get_memory(strlen(device->device_name) + strlen(device->hdr.name) + 20);
250    /* We edit "Resource-name" (physical-name) */
251    Mmsg(dev->prt_name, "\"%s\" (%s)", device->hdr.name, device->device_name);
252    Dmsg1(400, "Allocate dev=%s\n", dev->print_name());
253    dev->capabilities = device->cap_bits;
254    dev->min_free_space = device->min_free_space;
255    dev->min_block_size = device->min_block_size;
256    dev->max_block_size = device->max_block_size;
257    dev->max_volume_size = device->max_volume_size;
258    dev->max_file_size = device->max_file_size;
259    dev->padding_size = device->padding_size;
260    dev->file_alignment = device->file_alignment;
261    dev->max_concurrent_jobs = device->max_concurrent_jobs;
262    dev->volume_capacity = device->volume_capacity;
263    dev->max_rewind_wait = device->max_rewind_wait;
264    dev->max_open_wait = device->max_open_wait;
265    dev->vol_poll_interval = device->vol_poll_interval;
266    dev->max_spool_size = device->max_spool_size;
267    dev->drive_index = device->drive_index;
268    dev->enabled = device->enabled;
269    dev->autoselect = device->autoselect;
270    dev->read_only = device->read_only;
271    dev->dev_type = device->dev_type;
272    dev->device = device;
273    if (dev->is_tape()) { /* No parts on tapes */
274       dev->max_part_size = 0;
275    } else {
276       dev->max_part_size = device->max_part_size;
277    }
278    /* Sanity check */
279    if (dev->vol_poll_interval && dev->vol_poll_interval < 60) {
280       dev->vol_poll_interval = 60;
281    }
282
283    if (!device->dev) {
284       device->dev = dev;
285    }
286
287    /* If the device requires mount :
288     * - Check that the mount point is available
289     * - Check that (un)mount commands are defined
290     */
291    if (dev->is_file() && dev->requires_mount()) {
292       if (!device->mount_point || stat(device->mount_point, &statp) < 0) {
293          berrno be;
294          dev->dev_errno = errno;
295          Jmsg2(jcr, M_ERROR_TERM, 0, _("[SA0003] Unable to stat mount point %s: ERR=%s\n"),
296             device->mount_point, be.bstrerror());
297       }
298
299       if (!device->mount_command || !device->unmount_command) {
300          Jmsg0(jcr, M_ERROR_TERM, 0, _("[SA0004] Mount and unmount commands must defined for a device which requires mount.\n"));
301       }
302    }
303
304
305    /* Sanity check */
306    if (dev->max_block_size == 0) {
307       max_bs = DEFAULT_BLOCK_SIZE;
308    } else {
309       max_bs = dev->max_block_size;
310    }
311    if (dev->min_block_size > max_bs) {
312       Jmsg(jcr, M_ERROR_TERM, 0, _("[SA0005] Min block size > max on device %s\n"),
313            dev->print_name());
314    }
315    if (dev->max_block_size > 4096000) {
316       Jmsg3(jcr, M_ERROR, 0, _("[SA0006] Block size %u on device %s is too large, using default %u\n"),
317          dev->max_block_size, dev->print_name(), DEFAULT_BLOCK_SIZE);
318       dev->max_block_size = 0;
319    }
320    if (dev->max_block_size % TAPE_BSIZE != 0) {
321       Jmsg3(jcr, M_WARNING, 0, _("[SW0007] Max block size %u not multiple of device %s block size=%d.\n"),
322          dev->max_block_size, dev->print_name(), TAPE_BSIZE);
323    }
324    if (dev->max_volume_size != 0 && dev->max_volume_size < (dev->max_block_size << 4)) {
325       Jmsg(jcr, M_ERROR_TERM, 0, _("[SA0008] Max Vol Size < 8 * Max Block Size for device %s\n"),
326            dev->print_name());
327    }
328
329    dev->errmsg = get_pool_memory(PM_EMSG);
330    *dev->errmsg = 0;
331
332    if ((errstat = dev->init_mutex()) != 0) {
333       berrno be;
334       dev->dev_errno = errstat;
335       Mmsg1(dev->errmsg, _("[SA0009] Unable to init mutex: ERR=%s\n"), be.bstrerror(errstat));
336       Jmsg0(jcr, M_ERROR_TERM, 0, dev->errmsg);
337    }
338    if ((errstat = pthread_cond_init(&dev->wait, NULL)) != 0) {
339       berrno be;
340       dev->dev_errno = errstat;
341       Mmsg1(dev->errmsg, _("[SA0010] Unable to init cond variable: ERR=%s\n"), be.bstrerror(errstat));
342       Jmsg0(jcr, M_ERROR_TERM, 0, dev->errmsg);
343    }
344    if ((errstat = pthread_cond_init(&dev->wait_next_vol, NULL)) != 0) {
345       berrno be;
346       dev->dev_errno = errstat;
347       Mmsg1(dev->errmsg, _("[SA0011] Unable to init cond variable: ERR=%s\n"), be.bstrerror(errstat));
348       Jmsg0(jcr, M_ERROR_TERM, 0, dev->errmsg);
349    }
350    if ((errstat = pthread_mutex_init(&dev->spool_mutex, NULL)) != 0) {
351       berrno be;
352       dev->dev_errno = errstat;
353       Mmsg1(dev->errmsg, _("[SA0012] Unable to init spool mutex: ERR=%s\n"), be.bstrerror(errstat));
354       Jmsg0(jcr, M_ERROR_TERM, 0, dev->errmsg);
355    }
356    if ((errstat = dev->init_acquire_mutex()) != 0) {
357       berrno be;
358       dev->dev_errno = errstat;
359       Mmsg1(dev->errmsg, _("[SA0013] Unable to init acquire mutex: ERR=%s\n"), be.bstrerror(errstat));
360       Jmsg0(jcr, M_ERROR_TERM, 0, dev->errmsg);
361    }
362    if ((errstat = dev->init_freespace_mutex()) != 0) {
363       berrno be;
364       dev->dev_errno = errstat;
365       Mmsg1(dev->errmsg, _("[SA0014] Unable to init freespace mutex: ERR=%s\n"), be.bstrerror(errstat));
366       Jmsg0(jcr, M_ERROR_TERM, 0, dev->errmsg);
367    }
368    if ((errstat = dev->init_read_acquire_mutex()) != 0) {
369       berrno be;
370       dev->dev_errno = errstat;
371       Mmsg1(dev->errmsg, _("[SA0015] Unable to init read acquire mutex: ERR=%s\n"), be.bstrerror(errstat));
372       Jmsg0(jcr, M_ERROR_TERM, 0, dev->errmsg);
373    }
374    if ((errstat = dev->init_volcat_mutex()) != 0) {
375       berrno be;
376       dev->dev_errno = errstat;
377       Mmsg1(dev->errmsg, _("[SA0016] Unable to init volcat mutex: ERR=%s\n"), be.bstrerror(errstat));
378       Jmsg0(jcr, M_ERROR_TERM, 0, dev->errmsg);
379    }
380    if ((errstat = dev->init_dcrs_mutex()) != 0) {
381       berrno be;
382       dev->dev_errno = errstat;
383       Mmsg1(dev->errmsg, _("[SA0017] Unable to init dcrs mutex: ERR=%s\n"), be.bstrerror(errstat));
384       Jmsg0(jcr, M_ERROR_TERM, 0, dev->errmsg);
385    }
386
387    dev->set_mutex_priorities();
388
389    dev->clear_opened();
390    dev->attached_dcrs = New(dlist(dcr, &dcr->dev_link));
391    Dmsg2(100, "init_dev: tape=%d dev_name=%s\n", dev->is_tape(), dev->dev_name);
392    dev->initiated = true;
393 }
394
395 static DEVICE *load_driver(JCR *jcr, DEVRES *device)
396 {
397    POOL_MEM fname(PM_FNAME);
398    DEVICE *dev;
399    driver_item *drv;
400    const char *slash;
401    void *pHandle;
402    int len;
403    newDriver_t newDriver;
404
405    P(mutex);
406    if (!me->plugin_directory) {
407       Jmsg2(jcr, M_FATAL, 0,  _("[SF0018] Plugin directory not defined. Cannot load SD %s driver for device %s.\n"),
408          driver_tab[device->dev_type - 1], device->hdr.name);
409       V(mutex);
410       return NULL;
411    }
412    len = strlen(me->plugin_directory);
413    if (len == 0) {
414       Jmsg0(jcr, M_FATAL, 0,  _("[SF0019] Plugin directory not defined. Cannot load drivers.\n"));
415       V(mutex);
416       return NULL;
417    }
418
419    if (IsPathSeparator(me->plugin_directory[len - 1])) {
420       slash = "";
421    } else {
422       slash = "/";
423    }
424
425    Dmsg5(100, "loadable=%d type=%d loaded=%d name=%s handle=%p\n",
426       !driver_tab[device->dev_type-1].builtin,
427       device->dev_type,
428       driver_tab[device->dev_type-1].loaded,
429       driver_tab[device->dev_type-1].name,
430       driver_tab[device->dev_type-1].handle);
431    drv = &driver_tab[device->dev_type - 1];
432    Mmsg(fname, "%s%sbacula-sd-%s-driver%s%s", me->plugin_directory, slash,
433         drv->name, "-" VERSION, DRV_EXT);
434    if (!drv->loaded) {
435       Dmsg1(10, "Open SD driver at %s\n", fname.c_str());
436       pHandle = dlopen(fname.c_str(), RTLD_NOW);
437       if (pHandle) {
438          Dmsg2(100, "Driver=%s handle=%p\n", drv->name, pHandle);
439          /* Get global entry point */
440          Dmsg1(10, "Lookup \"BaculaSDdriver\" in driver=%s\n", drv->name);
441          newDriver = (newDriver_t)dlsym(pHandle, "BaculaSDdriver");
442          Dmsg2(10, "Driver=%s entry point=%p\n", drv->name, newDriver);
443          if (!newDriver) {
444             const char *error = dlerror();
445             Jmsg(NULL, M_ERROR, 0, _("[SE0003] Lookup of symbol \"BaculaSDdriver\" in driver %s for device %s failed: ERR=%s\n"),
446                device->hdr.name, fname.c_str(), NPRT(error));
447             Dmsg2(10, "Lookup of symbol \"BaculaSDdriver\" driver=%s failed: ERR=%s\n",
448                fname.c_str(), NPRT(error));
449             dlclose(pHandle);
450             V(mutex);
451             return NULL;
452          }
453          drv->handle = pHandle;
454          drv->loaded = true;
455          drv->newDriver = newDriver;
456       } else {
457          /* dlopen failed */
458          const char *error = dlerror();
459          Jmsg3(jcr, M_FATAL, 0, _("[SF0020] dlopen of SD driver=%s at %s failed: ERR=%s\n"),
460               drv->name, fname.c_str(), NPRT(error));
461          Dmsg2(000, "dlopen plugin %s failed: ERR=%s\n", fname.c_str(),
462                NPRT(error));
463          V(mutex);
464          return NULL;
465       }
466    } else {
467       Dmsg1(10, "SD driver=%s is already loaded.\n", drv->name);
468    }
469
470    /* Call driver initialization */
471    dev = drv->newDriver(jcr, device);
472    V(mutex);
473    return dev;
474 }