2 Bacula(R) - The Network Backup Solution
4 Copyright (C) 2000-2017 Kern Sibbald
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.
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.
14 This notice must be preserved when any source code is
15 conveyed and/or propagated.
17 Bacula(R) is a registered trademark of Kern Sibbald.
20 * Initialize a single device.
22 * This code was split from dev.c in January 2016
24 * written by, Kern Sibbald, MM
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"
44 /* Forward referenced functions */
46 typedef DEVICE *(*newDriver_t)(JCR *jcr, DEVRES *device);
49 static DEVICE *load_driver(JCR *jcr, DEVRES *device);
52 * Driver item for driver table
57 newDriver_t newDriver;
63 * Driver table. Must be in same order as the B_xxx_DEV type
64 * name handle, builtin loaded
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}
86 static pthread_mutex_t mutex = PTHREAD_MUTEX_INITIALIZER;
88 /* The alist should be created with not_owned_by_alist argument */
89 void sd_list_loaded_drivers(alist *list)
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);
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.
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.
109 DEVICE *init_dev(JCR *jcr, DEVRES *device, bool adata)
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) {
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());
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;
133 /* must set DeviceType = Vtape
134 * in normal mode, autodetection is disabled
136 } else if (S_ISREG(statp.st_mode)) {
137 device->dev_type = B_VTAPE_DEV;
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."
142 device->device_name, statp.st_mode);
145 if (strcmp(device->device_name, "/dev/null") == 0) {
146 device->dev_type = B_NULL_DEV;
151 for (n_drivers=0; driver_tab[n_drivers].name; n_drivers++) { };
152 Dmsg1(100, "Num drivers=%d\n", n_drivers);
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);
160 Dmsg5(100, "loadable=%d type=%d loaded=%d name=%s handle=%p\n",
161 !driver_tab[device->dev_type-1].builtin,
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) {
171 // dev = New(win_tape_dev);
176 dev = New(win_file_dev);
177 dev->capabilities |= CAP_LSEEK;
180 dev = New(win_file_dev);
191 dev->capabilities |= CAP_LSEEK;
201 Jmsg2(jcr, M_FATAL, 0, _("[SF0002] Unknown device type=%d device=\"%s\"\n"),
202 device->dev_type, device->hdr.name);
206 /* Loadable driver */
207 dev = load_driver(jcr, device);
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;
220 dev->device_generic_init(jcr, device);
222 /* Do device specific initialization */
223 dev->device_specific_init(jcr, device);
225 /* ***FIXME*** move to fifo driver */
226 if (dev->is_fifo()) {
227 dev->capabilities |= CAP_STREAM; /* set stream device */
234 * Do all the generic initialization here. Same for all devices.
236 void DEVICE::device_generic_init(JCR *jcr, DEVRES *device)
244 dev->clear_slot(); /* unknown */
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;
276 dev->max_part_size = device->max_part_size;
279 if (dev->vol_poll_interval && dev->vol_poll_interval < 60) {
280 dev->vol_poll_interval = 60;
287 /* If the device requires mount :
288 * - Check that the mount point is available
289 * - Check that (un)mount commands are defined
291 if (dev->is_file() && dev->requires_mount()) {
292 if (!device->mount_point || stat(device->mount_point, &statp) < 0) {
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());
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"));
306 if (dev->max_block_size == 0) {
307 max_bs = DEFAULT_BLOCK_SIZE;
309 max_bs = dev->max_block_size;
311 if (dev->min_block_size > max_bs) {
312 Jmsg(jcr, M_ERROR_TERM, 0, _("[SA0005] Min block size > max on device %s\n"),
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;
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);
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"),
329 dev->errmsg = get_pool_memory(PM_EMSG);
332 if ((errstat = dev->init_mutex()) != 0) {
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);
338 if ((errstat = pthread_cond_init(&dev->wait, NULL)) != 0) {
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);
344 if ((errstat = pthread_cond_init(&dev->wait_next_vol, NULL)) != 0) {
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);
350 if ((errstat = pthread_mutex_init(&dev->spool_mutex, NULL)) != 0) {
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);
356 if ((errstat = dev->init_acquire_mutex()) != 0) {
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);
362 if ((errstat = dev->init_freespace_mutex()) != 0) {
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);
368 if ((errstat = dev->init_read_acquire_mutex()) != 0) {
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);
374 if ((errstat = dev->init_volcat_mutex()) != 0) {
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);
380 if ((errstat = dev->init_dcrs_mutex()) != 0) {
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);
387 dev->set_mutex_priorities();
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;
395 static DEVICE *load_driver(JCR *jcr, DEVRES *device)
397 POOL_MEM fname(PM_FNAME);
403 newDriver_t newDriver;
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);
412 len = strlen(me->plugin_directory);
414 Jmsg0(jcr, M_FATAL, 0, _("[SF0019] Plugin directory not defined. Cannot load drivers.\n"));
419 if (IsPathSeparator(me->plugin_directory[len - 1])) {
425 Dmsg5(100, "loadable=%d type=%d loaded=%d name=%s handle=%p\n",
426 !driver_tab[device->dev_type-1].builtin,
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);
435 Dmsg1(10, "Open SD driver at %s\n", fname.c_str());
436 pHandle = dlopen(fname.c_str(), RTLD_NOW);
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);
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));
453 drv->handle = pHandle;
455 drv->newDriver = newDriver;
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(),
467 Dmsg1(10, "SD driver=%s is already loaded.\n", drv->name);
470 /* Call driver initialization */
471 dev = drv->newDriver(jcr, device);