1 /*
2 BAREOS® - Backup Archiving REcovery Open Sourced
3
4 Copyright (C) 2000-2012 Free Software Foundation Europe e.V.
5 Copyright (C) 2011-2012 Planets Communications B.V.
6 Copyright (C) 2013-2019 Bareos GmbH & Co. KG
7
8 This program is Free Software; you can redistribute it and/or
9 modify it under the terms of version three of the GNU Affero General Public
10 License as published by the Free Software Foundation and included
11 in the file LICENSE.
12
13 This program is distributed in the hope that it will be useful, but
14 WITHOUT ANY WARRANTY; without even the implied warranty of
15 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
16 Affero General Public License for more details.
17
18 You should have received a copy of the GNU Affero General Public License
19 along with this program; if not, write to the Free Software
20 Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
21 02110-1301, USA.
22 */
23 /*
24 * Kern Sibbald, MM
25 */
26 /**
27 * @file
28 * low level operations on device (storage device)
29 *
30 * NOTE!!!! None of these routines are reentrant. You must
31 * use dev->rLock() and dev->Unlock() at a higher level,
32 * or use the xxx_device() equivalents. By moving the
33 * thread synchronization to a higher level, we permit
34 * the higher level routines to "seize" the device and
35 * to carry out operations without worrying about who
36 * set what lock (i.e. race conditions).
37 *
38 * Note, this is the device dependent code, and may have
39 * to be modified for each system, but is meant to
40 * be as "generic" as possible.
41 *
42 * The purpose of this code is to develop a SIMPLE Storage
43 * daemon. More complicated coding (double buffering, writer
44 * thread, ...) is left for a later version.
45 */
46
47 /*
48 * Handling I/O errors and end of tape conditions are a bit tricky.
49 * This is how it is currently done when writing.
50 * On either an I/O error or end of tape,
51 * we will stop writing on the physical device (no I/O recovery is
52 * attempted at least in this daemon). The state flag will be sent
53 * to include ST_EOT, which is ephemeral, and ST_WEOT, which is
54 * persistent. Lots of routines clear ST_EOT, but ST_WEOT is
55 * cleared only when the problem goes away. Now when ST_WEOT
56 * is set all calls to WriteBlockToDevice() call the fix_up
57 * routine. In addition, all threads are blocked
58 * from writing on the tape by calling lock_dev(), and thread other
59 * than the first thread to hit the EOT will block on a condition
60 * variable. The first thread to hit the EOT will continue to
61 * be able to read and write the tape (he sort of tunnels through
62 * the locking mechanism -- see lock_dev() for details).
63 *
64 * Now presumably somewhere higher in the chain of command
65 * (device.c), someone will notice the EOT condition and
66 * get a new tape up, get the tape label read, and mark
67 * the label for rewriting. Then this higher level routine
68 * will write the unwritten buffer to the new volume.
69 * Finally, he will release
70 * any blocked threads by doing a broadcast on the condition
71 * variable. At that point, we should be totally back in
72 * business with no lost data.
73 */
74
75 #include "include/bareos.h"
76 #include "stored/block.h"
77 #include "stored/stored.h"
78 #include "stored/autochanger.h"
79 #include "stored/bsr.h"
80 #include "stored/jcr_private.h"
81 #include "stored/sd_backends.h"
82 #include "lib/btimers.h"
83 #include "include/jcr.h"
84 #include "lib/berrno.h"
85
86 #ifndef HAVE_DYNAMIC_SD_BACKENDS
87 #ifdef HAVE_GFAPI
88 #include "backends/gfapi_device.h"
89 #endif
90 #ifdef HAVE_DROPLET
91 #include "backends/chunked_device.h"
92 #include "backends/droplet_device.h"
93 #endif
94 #ifdef HAVE_RADOS
95 #include "backends/rados_device.h"
96 #endif
97 #ifdef HAVE_CEPHFS
98 #include "backends/cephfs_device.h"
99 #endif
100 #ifdef HAVE_ELASTO
101 #include "backends/elasto_device.h"
102 #endif
103 #include "backends/generic_tape_device.h"
104 #ifdef HAVE_WIN32
105 #include "backends/win32_tape_device.h"
106 #include "backends/win32_fifo_device.h"
107 #else
108 #include "backends/unix_tape_device.h"
109 #include "backends/unix_fifo_device.h"
110 #endif
111 #endif /* HAVE_DYNAMIC_SD_BACKENDS */
112
113 #ifdef HAVE_WIN32
114 #include "backends/win32_file_device.h"
115 #else
116 #include "backends/unix_file_device.h"
117 #endif
118
119 #ifndef O_NONBLOCK
120 #define O_NONBLOCK 0
121 #endif
122
123 namespace storagedaemon {
124
mode_to_str(int mode)125 const char* Device::mode_to_str(int mode)
126 {
127 static const char* modes[] = {"CREATE_READ_WRITE", "OPEN_READ_WRITE",
128 "OPEN_READ_ONLY", "OPEN_WRITE_ONLY"};
129
130 static char buf[100];
131
132 if (mode < 1 || mode > 4) {
133 Bsnprintf(buf, sizeof(buf), "BAD mode=%d", mode);
134 return buf;
135 }
136
137 return modes[mode - 1];
138 }
139
init_dev(JobControlRecord * jcr,DeviceResource * device,bool new_init)140 static inline Device* init_dev(JobControlRecord* jcr,
141 DeviceResource* device,
142 bool new_init)
143 {
144 struct stat statp;
145 int errstat;
146 DeviceControlRecord* dcr = NULL;
147 Device* dev = NULL;
148 uint32_t max_bs;
149
150 Dmsg1(400, "max_block_size in device res is %u\n", device->max_block_size);
151
152 /*
153 * If no device type specified, try to guess
154 */
155 if (!device->dev_type) {
156 /*
157 * Check that device is available
158 */
159 if (stat(device->device_name, &statp) < 0) {
160 BErrNo be;
161 Jmsg2(jcr, M_ERROR, 0, _("Unable to stat device %s: ERR=%s\n"),
162 device->device_name, be.bstrerror());
163 return NULL;
164 }
165 if (S_ISDIR(statp.st_mode)) {
166 device->dev_type = B_FILE_DEV;
167 } else if (S_ISCHR(statp.st_mode)) {
168 device->dev_type = B_TAPE_DEV;
169 } else if (S_ISFIFO(statp.st_mode)) {
170 device->dev_type = B_FIFO_DEV;
171 } else if (!BitIsSet(CAP_REQMOUNT, device->cap_bits)) {
172 Jmsg2(jcr, M_ERROR, 0,
173 _("%s is an unknown device type. Must be tape or directory, "
174 "st_mode=%04o\n"),
175 device->device_name, (statp.st_mode & ~S_IFMT));
176 return NULL;
177 }
178 }
179
180 /*
181 * See what type of device is wanted.
182 */
183 switch (device->dev_type) {
184 /*
185 * When using dynamic loading use the init_backend_dev() function
186 * for any type of device not being of the type file.
187 */
188 #ifndef HAVE_DYNAMIC_SD_BACKENDS
189 #ifdef HAVE_GFAPI
190 case B_GFAPI_DEV:
191 dev = new gfapi_device;
192 break;
193 #endif
194 #ifdef HAVE_DROPLET
195 case B_DROPLET_DEV:
196 dev = new droplet_device;
197 break;
198 #endif
199 #ifdef HAVE_RADOS
200 case B_RADOS_DEV:
201 dev = new rados_device;
202 break;
203 #endif
204 #ifdef HAVE_CEPHFS
205 case B_CEPHFS_DEV:
206 dev = new cephfs_device;
207 break;
208 #endif
209 #ifdef HAVE_ELASTO
210 case B_ELASTO_DEV:
211 dev = new elasto_device;
212 break;
213 #endif
214 #ifdef HAVE_WIN32
215 case B_TAPE_DEV:
216 dev = new win32_tape_device;
217 break;
218 case B_FIFO_DEV:
219 dev = new win32_fifo_device;
220 break;
221 #else
222 case B_TAPE_DEV:
223 dev = new unix_tape_device;
224 break;
225 case B_FIFO_DEV:
226 dev = new unix_fifo_device;
227 break;
228 #endif
229 #endif /* HAVE_DYNAMIC_SD_BACKENDS */
230 #ifdef HAVE_WIN32
231 case B_FILE_DEV:
232 dev = new win32_file_device;
233 break;
234 #else
235 case B_FILE_DEV:
236 dev = new unix_file_device;
237 break;
238 #endif
239 default:
240 #ifdef HAVE_DYNAMIC_SD_BACKENDS
241 dev = init_backend_dev(jcr, device->dev_type);
242 #endif
243 break;
244 }
245
246 if (!dev) {
247 Jmsg2(jcr, M_ERROR, 0, _("%s has an unknown device type %d\n"),
248 device->device_name, device->dev_type);
249 return NULL;
250 }
251 dev->InvalidateSlotNumber(); /* unknown */
252
253 /*
254 * Copy user supplied device parameters from Resource
255 */
256 dev->dev_name = GetMemory(strlen(device->device_name) + 1);
257 PmStrcpy(dev->dev_name, device->device_name);
258 if (device->device_options) {
259 dev->dev_options = GetMemory(strlen(device->device_options) + 1);
260 PmStrcpy(dev->dev_options, device->device_options);
261 }
262 dev->prt_name = GetMemory(strlen(device->device_name) +
263 strlen(device->resource_name_) + 20);
264
265 /*
266 * We edit "Resource-name" (physical-name)
267 */
268 Mmsg(dev->prt_name, "\"%s\" (%s)", device->resource_name_,
269 device->device_name);
270 Dmsg1(400, "Allocate dev=%s\n", dev->print_name());
271 CopySetBits(CAP_MAX, device->cap_bits, dev->capabilities);
272
273 /*
274 * current block sizes
275 */
276 dev->min_block_size = device->min_block_size;
277 dev->max_block_size = device->max_block_size;
278 dev->max_volume_size = device->max_volume_size;
279 dev->max_file_size = device->max_file_size;
280 dev->max_concurrent_jobs = device->max_concurrent_jobs;
281 dev->volume_capacity = device->volume_capacity;
282 dev->max_rewind_wait = device->max_rewind_wait;
283 dev->max_open_wait = device->max_open_wait;
284 dev->max_open_vols = device->max_open_vols;
285 dev->vol_poll_interval = device->vol_poll_interval;
286 dev->max_spool_size = device->max_spool_size;
287 dev->drive = device->drive;
288 dev->drive_index = device->drive_index;
289 dev->autoselect = device->autoselect;
290 dev->norewindonclose = device->norewindonclose;
291 dev->dev_type = device->dev_type;
292 dev->device = device;
293
294 /*
295 * Sanity check
296 */
297 if (dev->vol_poll_interval && dev->vol_poll_interval < 60) {
298 dev->vol_poll_interval = 60;
299 }
300 device->dev = dev;
301
302 if (dev->IsFifo()) { dev->SetCap(CAP_STREAM); /* set stream device */ }
303
304 /*
305 * If the device requires mount :
306 * - Check that the mount point is available
307 * - Check that (un)mount commands are defined
308 */
309 if (dev->IsFile() && dev->RequiresMount()) {
310 if (!device->mount_point || stat(device->mount_point, &statp) < 0) {
311 BErrNo be;
312 dev->dev_errno = errno;
313 Jmsg2(jcr, M_ERROR_TERM, 0, _("Unable to stat mount point %s: ERR=%s\n"),
314 device->mount_point, be.bstrerror());
315 }
316
317 if (!device->mount_command || !device->unmount_command) {
318 Jmsg0(jcr, M_ERROR_TERM, 0,
319 _("Mount and unmount commands must defined for a device which "
320 "requires mount.\n"));
321 }
322 }
323
324 /*
325 * Sanity check
326 */
327 if (dev->max_block_size == 0) {
328 max_bs = DEFAULT_BLOCK_SIZE;
329 } else {
330 max_bs = dev->max_block_size;
331 }
332 if (dev->min_block_size > max_bs) {
333 Jmsg(jcr, M_ERROR_TERM, 0, _("Min block size > max on device %s\n"),
334 dev->print_name());
335 }
336 if (dev->max_block_size > MAX_BLOCK_LENGTH) {
337 Jmsg3(jcr, M_ERROR, 0,
338 _("Block size %u on device %s is too large, using default %u\n"),
339 dev->max_block_size, dev->print_name(), DEFAULT_BLOCK_SIZE);
340 dev->max_block_size = 0;
341 }
342 if (dev->max_block_size % TAPE_BSIZE != 0) {
343 Jmsg3(jcr, M_WARNING, 0,
344 _("Max block size %u not multiple of device %s block size=%d.\n"),
345 dev->max_block_size, dev->print_name(), TAPE_BSIZE);
346 }
347 if (dev->max_volume_size != 0 &&
348 dev->max_volume_size < (dev->max_block_size << 4)) {
349 Jmsg(jcr, M_ERROR_TERM, 0,
350 _("Max Vol Size < 8 * Max Block Size for device %s\n"),
351 dev->print_name());
352 }
353
354 dev->errmsg = GetPoolMemory(PM_EMSG);
355 *dev->errmsg = 0;
356
357 if ((errstat = dev->InitMutex()) != 0) {
358 BErrNo be;
359 dev->dev_errno = errstat;
360 Mmsg1(dev->errmsg, _("Unable to init mutex: ERR=%s\n"),
361 be.bstrerror(errstat));
362 Jmsg0(jcr, M_ERROR_TERM, 0, dev->errmsg);
363 }
364
365 if ((errstat = pthread_cond_init(&dev->wait, NULL)) != 0) {
366 BErrNo be;
367 dev->dev_errno = errstat;
368 Mmsg1(dev->errmsg, _("Unable to init cond variable: ERR=%s\n"),
369 be.bstrerror(errstat));
370 Jmsg0(jcr, M_ERROR_TERM, 0, dev->errmsg);
371 }
372
373 if ((errstat = pthread_cond_init(&dev->wait_next_vol, NULL)) != 0) {
374 BErrNo be;
375 dev->dev_errno = errstat;
376 Mmsg1(dev->errmsg, _("Unable to init cond variable: ERR=%s\n"),
377 be.bstrerror(errstat));
378 Jmsg0(jcr, M_ERROR_TERM, 0, dev->errmsg);
379 }
380
381 if ((errstat = pthread_mutex_init(&dev->spool_mutex, NULL)) != 0) {
382 BErrNo be;
383 dev->dev_errno = errstat;
384 Mmsg1(dev->errmsg, _("Unable to init spool mutex: ERR=%s\n"),
385 be.bstrerror(errstat));
386 Jmsg0(jcr, M_ERROR_TERM, 0, dev->errmsg);
387 }
388
389 if ((errstat = dev->InitAcquireMutex()) != 0) {
390 BErrNo be;
391 dev->dev_errno = errstat;
392 Mmsg1(dev->errmsg, _("Unable to init acquire mutex: ERR=%s\n"),
393 be.bstrerror(errstat));
394 Jmsg0(jcr, M_ERROR_TERM, 0, dev->errmsg);
395 }
396
397 if ((errstat = dev->InitReadAcquireMutex()) != 0) {
398 BErrNo be;
399 dev->dev_errno = errstat;
400 Mmsg1(dev->errmsg, _("Unable to init read acquire mutex: ERR=%s\n"),
401 be.bstrerror(errstat));
402 Jmsg0(jcr, M_ERROR_TERM, 0, dev->errmsg);
403 }
404
405 dev->ClearOpened();
406 dev->attached_dcrs = new dlist(dcr, &dcr->dev_link);
407 Dmsg2(100, "InitDev: tape=%d dev_name=%s\n", dev->IsTape(), dev->dev_name);
408 dev->initiated = true;
409 Dmsg3(100, "dev=%s dev_max_bs=%u max_bs=%u\n", dev->dev_name,
410 dev->device->max_block_size, dev->max_block_size);
411
412 return dev;
413 }
414
415 /**
416 * Allocate and initialize the Device structure
417 * Note, if dev is non-NULL, it is already allocated,
418 * thus we neither allocate it nor free it. This allows
419 * the caller to put the packet in shared memory.
420 *
421 * Note, for a tape, the device->device_name is the device name
422 * (e.g. /dev/nst0), and for a file, the device name
423 * is the directory in which the file will be placed.
424 */
InitDev(JobControlRecord * jcr,DeviceResource * device)425 Device* InitDev(JobControlRecord* jcr, DeviceResource* device)
426 {
427 Device* dev;
428
429 dev = init_dev(jcr, device, false);
430 return dev;
431 }
432
433 /**
434 * This routine initializes the device wait timers
435 */
InitDeviceWaitTimers(DeviceControlRecord * dcr)436 void InitDeviceWaitTimers(DeviceControlRecord* dcr)
437 {
438 Device* dev = dcr->dev;
439 JobControlRecord* jcr = dcr->jcr;
440
441 /* ******FIXME******* put these on config variables */
442 dev->min_wait = 60 * 60;
443 dev->max_wait = 24 * 60 * 60;
444 dev->max_num_wait = 9; /* 5 waits =~ 1 day, then 1 day at a time */
445 dev->wait_sec = dev->min_wait;
446 dev->rem_wait_sec = dev->wait_sec;
447 dev->num_wait = 0;
448 dev->poll = false;
449
450 jcr->impl->device_wait_times.min_wait = 60 * 60;
451 jcr->impl->device_wait_times.max_wait = 24 * 60 * 60;
452 jcr->impl->device_wait_times.max_num_wait =
453 9; /* 5 waits =~ 1 day, then 1 day at a time */
454 jcr->impl->device_wait_times.wait_sec = jcr->impl->device_wait_times.min_wait;
455 jcr->impl->device_wait_times.rem_wait_sec =
456 jcr->impl->device_wait_times.wait_sec;
457 jcr->impl->device_wait_times.num_wait = 0;
458 }
459
InitJcrDeviceWaitTimers(JobControlRecord * jcr)460 void InitJcrDeviceWaitTimers(JobControlRecord* jcr)
461 {
462 /* ******FIXME******* put these on config variables */
463 jcr->impl->device_wait_times.min_wait = 60 * 60;
464 jcr->impl->device_wait_times.max_wait = 24 * 60 * 60;
465 jcr->impl->device_wait_times.max_num_wait =
466 9; /* 5 waits =~ 1 day, then 1 day at a time */
467 jcr->impl->device_wait_times.wait_sec = jcr->impl->device_wait_times.min_wait;
468 jcr->impl->device_wait_times.rem_wait_sec =
469 jcr->impl->device_wait_times.wait_sec;
470 jcr->impl->device_wait_times.num_wait = 0;
471 }
472
473 /**
474 * The dev timers are used for waiting on a particular device
475 *
476 * Returns: true if time doubled
477 * false if max time expired
478 */
DoubleDevWaitTime(Device * dev)479 bool DoubleDevWaitTime(Device* dev)
480 {
481 dev->wait_sec *= 2; /* Double wait time */
482 if (dev->wait_sec > dev->max_wait) { /* But not longer than maxtime */
483 dev->wait_sec = dev->max_wait;
484 }
485 dev->num_wait++;
486 dev->rem_wait_sec = dev->wait_sec;
487 if (dev->num_wait >= dev->max_num_wait) { return false; }
488 return true;
489 }
490
491 /**
492 * Set the block size of the device.
493 * If the volume block size is zero, we set the max block size to what is
494 * configured in the device resource i.e. dev->device->max_block_size.
495 *
496 * If dev->device->max_block_size is zero, do nothing and leave
497 * dev->max_block_size as it is.
498 */
SetBlocksizes(DeviceControlRecord * dcr)499 void Device::SetBlocksizes(DeviceControlRecord* dcr)
500 {
501 Device* dev = this;
502 JobControlRecord* jcr = dcr->jcr;
503 uint32_t max_bs;
504
505 Dmsg4(100,
506 "Device %s has dev->device->max_block_size of %u and "
507 "dev->max_block_size of %u, dcr->VolMaxBlocksize is %u\n",
508 dev->print_name(), dev->device->max_block_size, dev->max_block_size,
509 dcr->VolMaxBlocksize);
510
511 if (dcr->VolMaxBlocksize == 0 && dev->device->max_block_size != 0) {
512 Dmsg2(100,
513 "setting dev->max_block_size to dev->device->max_block_size=%u "
514 "on device %s because dcr->VolMaxBlocksize is 0\n",
515 dev->device->max_block_size, dev->print_name());
516 dev->min_block_size = dev->device->min_block_size;
517 dev->max_block_size = dev->device->max_block_size;
518 } else if (dcr->VolMaxBlocksize != 0) {
519 dev->min_block_size = dcr->VolMinBlocksize;
520 dev->max_block_size = dcr->VolMaxBlocksize;
521 }
522
523 /*
524 * Sanity check
525 */
526 if (dev->max_block_size == 0) {
527 max_bs = DEFAULT_BLOCK_SIZE;
528 } else {
529 max_bs = dev->max_block_size;
530 }
531
532 if (dev->min_block_size > max_bs) {
533 Jmsg(jcr, M_ERROR_TERM, 0, _("Min block size > max on device %s\n"),
534 dev->print_name());
535 }
536
537 if (dev->max_block_size > MAX_BLOCK_LENGTH) {
538 Jmsg3(jcr, M_ERROR, 0,
539 _("Block size %u on device %s is too large, using default %u\n"),
540 dev->max_block_size, dev->print_name(), DEFAULT_BLOCK_SIZE);
541 dev->max_block_size = 0;
542 }
543
544 if (dev->max_block_size % TAPE_BSIZE != 0) {
545 Jmsg3(jcr, M_WARNING, 0,
546 _("Max block size %u not multiple of device %s block size=%d.\n"),
547 dev->max_block_size, dev->print_name(), TAPE_BSIZE);
548 }
549
550 if (dev->max_volume_size != 0 &&
551 dev->max_volume_size < (dev->max_block_size << 4)) {
552 Jmsg(jcr, M_ERROR_TERM, 0,
553 _("Max Vol Size < 8 * Max Block Size for device %s\n"),
554 dev->print_name());
555 }
556
557 Dmsg3(100, "set minblocksize to %d, maxblocksize to %d on device %s\n",
558 dev->min_block_size, dev->max_block_size, dev->print_name());
559
560 /*
561 * If blocklen is not dev->max_block_size create a new block with the right
562 * size. (as header is always dev->label_block_size which is preset with
563 * DEFAULT_BLOCK_SIZE)
564 */
565 if (dcr->block) {
566 if (dcr->block->buf_len != dev->max_block_size) {
567 Dmsg2(100, "created new block of buf_len: %u on device %s\n",
568 dev->max_block_size, dev->print_name());
569 FreeBlock(dcr->block);
570 dcr->block = new_block(dev);
571 Dmsg2(100,
572 "created new block of buf_len: %u on device %s, freeing block\n",
573 dcr->block->buf_len, dev->print_name());
574 }
575 }
576 }
577
578 /**
579 * Set the block size of the device to the label_block_size
580 * to read labels as we want to always use that blocksize when
581 * writing volume labels
582 */
SetLabelBlocksize(DeviceControlRecord * dcr)583 void Device::SetLabelBlocksize(DeviceControlRecord* dcr)
584 {
585 Device* dev = this;
586 Dmsg3(100,
587 "setting minblocksize to %u, "
588 "maxblocksize to label_block_size=%u, on device %s\n",
589 dev->device->label_block_size, dev->device->label_block_size,
590 dev->print_name());
591
592 dev->min_block_size = dev->device->label_block_size;
593 dev->max_block_size = dev->device->label_block_size;
594 /*
595 * If blocklen is not dev->max_block_size create a new block with the right
596 * size (as header is always label_block_size)
597 */
598 if (dcr->block) {
599 if (dcr->block->buf_len != dev->max_block_size) {
600 FreeBlock(dcr->block);
601 dcr->block = new_block(dev);
602 Dmsg2(100, "created new block of buf_len: %u on device %s\n",
603 dcr->block->buf_len, dev->print_name());
604 }
605 }
606 }
607
608 /**
609 * Open the device with the operating system and
610 * initialize buffer pointers.
611 *
612 * Returns: true on success
613 * false on error
614 *
615 * Note, for a tape, the VolName is the name we give to the
616 * volume (not really used here), but for a file, the
617 * VolName represents the name of the file to be created/opened.
618 * In the case of a file, the full name is the device name
619 * (archive_name) with the VolName concatenated.
620 */
open(DeviceControlRecord * dcr,int omode)621 bool Device::open(DeviceControlRecord* dcr, int omode)
622 {
623 char preserve[ST_BYTES];
624
625 ClearAllBits(ST_MAX, preserve);
626 if (IsOpen()) {
627 if (open_mode == omode) {
628 return true;
629 } else {
630 d_close(fd_);
631 ClearOpened();
632 Dmsg0(100, "Close fd for mode change.\n");
633
634 if (BitIsSet(ST_LABEL, state)) SetBit(ST_LABEL, preserve);
635 if (BitIsSet(ST_APPENDREADY, state)) SetBit(ST_APPENDREADY, preserve);
636 if (BitIsSet(ST_READREADY, state)) SetBit(ST_READREADY, preserve);
637 }
638 }
639
640 if (dcr) {
641 dcr->setVolCatName(dcr->VolumeName);
642 VolCatInfo = dcr->VolCatInfo; /* structure assign */
643 }
644
645 Dmsg4(100, "open dev: type=%d dev_name=%s vol=%s mode=%s\n", dev_type,
646 print_name(), getVolCatName(), mode_to_str(omode));
647
648 ClearBit(ST_LABEL, state);
649 ClearBit(ST_APPENDREADY, state);
650 ClearBit(ST_READREADY, state);
651 ClearBit(ST_EOT, state);
652 ClearBit(ST_WEOT, state);
653 ClearBit(ST_EOF, state);
654
655 label_type = B_BAREOS_LABEL;
656
657 /*
658 * We are about to open the device so let any plugin know we are.
659 */
660 if (dcr && GeneratePluginEvent(dcr->jcr, bsdEventDeviceOpen, dcr) != bRC_OK) {
661 Dmsg0(100, "open_dev: bsdEventDeviceOpen failed\n");
662 return false;
663 }
664
665 Dmsg1(100, "call OpenDevice mode=%s\n", mode_to_str(omode));
666 OpenDevice(dcr, omode);
667
668 /*
669 * Reset any important state info
670 */
671 CopySetBits(ST_MAX, preserve, state);
672
673 Dmsg2(100, "preserve=%08o fd=%d\n", preserve, fd_);
674
675 return fd_ >= 0;
676 }
677
set_mode(int mode)678 void Device::set_mode(int mode)
679 {
680 switch (mode) {
681 case CREATE_READ_WRITE:
682 oflags = O_CREAT | O_RDWR | O_BINARY;
683 break;
684 case OPEN_READ_WRITE:
685 oflags = O_RDWR | O_BINARY;
686 break;
687 case OPEN_READ_ONLY:
688 oflags = O_RDONLY | O_BINARY;
689 break;
690 case OPEN_WRITE_ONLY:
691 oflags = O_WRONLY | O_BINARY;
692 break;
693 default:
694 Emsg0(M_ABORT, 0, _("Illegal mode given to open dev.\n"));
695 }
696 }
697
698 /**
699 * Open a device.
700 */
OpenDevice(DeviceControlRecord * dcr,int omode)701 void Device::OpenDevice(DeviceControlRecord* dcr, int omode)
702 {
703 PoolMem archive_name(PM_FNAME);
704
705 GetAutochangerLoadedSlot(dcr);
706
707 /*
708 * Handle opening of File Archive (not a tape)
709 */
710 PmStrcpy(archive_name, dev_name);
711
712 /*
713 * If this is a virtual autochanger (i.e. changer_res != NULL) we simply use
714 * the device name, assuming it has been appropriately setup by the
715 * "autochanger".
716 */
717 if (!device->changer_res || device->changer_command[0] == 0) {
718 if (VolCatInfo.VolCatName[0] == 0) {
719 Mmsg(errmsg, _("Could not open file device %s. No Volume name given.\n"),
720 print_name());
721 ClearOpened();
722 return;
723 }
724
725 if (!IsPathSeparator(
726 archive_name.c_str()[strlen(archive_name.c_str()) - 1])) {
727 PmStrcat(archive_name, "/");
728 }
729 PmStrcat(archive_name, getVolCatName());
730 }
731
732 mount(dcr, 1); /* do mount if required */
733
734 open_mode = omode;
735 set_mode(omode);
736
737 /*
738 * If creating file, give 0640 permissions
739 */
740 Dmsg3(100, "open disk: mode=%s open(%s, %08o, 0640)\n", mode_to_str(omode),
741 archive_name.c_str(), oflags);
742
743 if ((fd_ = d_open(archive_name.c_str(), oflags, 0640)) < 0) {
744 BErrNo be;
745 dev_errno = errno;
746 Mmsg2(errmsg, _("Could not open: %s, ERR=%s\n"), archive_name.c_str(),
747 be.bstrerror());
748 Dmsg1(100, "open failed: %s", errmsg);
749 }
750
751 if (fd_ >= 0) {
752 dev_errno = 0;
753 file = 0;
754 file_addr = 0;
755 }
756
757 Dmsg1(100, "open dev: disk fd=%d opened\n", fd_);
758 }
759
760 /**
761 * Rewind the device.
762 *
763 * Returns: true on success
764 * false on failure
765 */
rewind(DeviceControlRecord * dcr)766 bool Device::rewind(DeviceControlRecord* dcr)
767 {
768 Dmsg3(400, "rewind res=%d fd=%d %s\n", NumReserved(), fd_, print_name());
769
770 /*
771 * Remove EOF/EOT flags
772 */
773 ClearBit(ST_EOT, state);
774 ClearBit(ST_EOF, state);
775 ClearBit(ST_WEOT, state);
776
777 block_num = file = 0;
778 file_size = 0;
779 file_addr = 0;
780
781 if (fd_ < 0) { return false; }
782
783 if (IsFifo() || IsVtl()) { return true; }
784
785 if (lseek(dcr, (boffset_t)0, SEEK_SET) < 0) {
786 BErrNo be;
787 dev_errno = errno;
788 Mmsg2(errmsg, _("lseek error on %s. ERR=%s"), print_name(), be.bstrerror());
789 return false;
790 }
791
792 return true;
793 }
794
795 /**
796 * Called to indicate that we have just read an EOF from the device.
797 */
SetAteof()798 void Device::SetAteof()
799 {
800 SetEof();
801 file_addr = 0;
802 file_size = 0;
803 block_num = 0;
804 }
805
806 /**
807 * Called to indicate we are now at the end of the volume, and writing is not
808 * possible.
809 */
SetAteot()810 void Device::SetAteot()
811 {
812 /*
813 * Make volume effectively read-only
814 */
815 SetBit(ST_EOF, state);
816 SetBit(ST_EOT, state);
817 SetBit(ST_WEOT, state);
818 ClearAppend();
819 }
820
821 /**
822 * Position device to end of medium (end of data)
823 *
824 * Returns: true on succes
825 * false on error
826 */
eod(DeviceControlRecord * dcr)827 bool Device::eod(DeviceControlRecord* dcr)
828 {
829 boffset_t pos;
830
831 if (fd_ < 0) {
832 dev_errno = EBADF;
833 Mmsg1(errmsg, _("Bad call to eod. Device %s not open\n"), print_name());
834 return false;
835 }
836
837 if (IsVtl()) { return true; }
838
839 Dmsg0(100, "Enter eod\n");
840 if (AtEot()) { return true; }
841
842 ClearEof(); /* remove EOF flag */
843
844 block_num = file = 0;
845 file_size = 0;
846 file_addr = 0;
847
848 pos = lseek(dcr, (boffset_t)0, SEEK_END);
849 Dmsg1(200, "====== Seek to %lld\n", pos);
850
851 if (pos >= 0) {
852 UpdatePos(dcr);
853 SetEot();
854 return true;
855 }
856
857 dev_errno = errno;
858 BErrNo be;
859 Mmsg2(errmsg, _("lseek error on %s. ERR=%s.\n"), print_name(),
860 be.bstrerror());
861 Dmsg0(100, errmsg);
862
863 return false;
864 }
865
866 /**
867 * Set the position of the device.
868 *
869 * Returns: true on succes
870 * false on error
871 */
UpdatePos(DeviceControlRecord * dcr)872 bool Device::UpdatePos(DeviceControlRecord* dcr)
873 {
874 boffset_t pos;
875 bool ok = true;
876
877 if (!IsOpen()) {
878 dev_errno = EBADF;
879 Mmsg0(errmsg, _("Bad device call. Device not open\n"));
880 Emsg1(M_FATAL, 0, "%s", errmsg);
881 return false;
882 }
883
884 if (IsFifo() || IsVtl()) { return true; }
885
886 file = 0;
887 file_addr = 0;
888 pos = lseek(dcr, (boffset_t)0, SEEK_CUR);
889 if (pos < 0) {
890 BErrNo be;
891 dev_errno = errno;
892 Pmsg1(000, _("Seek error: ERR=%s\n"), be.bstrerror());
893 Mmsg2(errmsg, _("lseek error on %s. ERR=%s.\n"), print_name(),
894 be.bstrerror());
895 ok = false;
896 } else {
897 file_addr = pos;
898 block_num = (uint32_t)pos;
899 file = (uint32_t)(pos >> 32);
900 }
901
902 return ok;
903 }
904
StatusDev()905 char* Device::StatusDev()
906 {
907 char* status;
908
909 status = (char*)malloc(BMT_BYTES);
910 ClearAllBits(BMT_MAX, status);
911
912 if (BitIsSet(ST_EOT, state) || BitIsSet(ST_WEOT, state)) {
913 SetBit(BMT_EOD, status);
914 Pmsg0(-20, " EOD");
915 }
916
917 if (BitIsSet(ST_EOF, state)) {
918 SetBit(BMT_EOF, status);
919 Pmsg0(-20, " EOF");
920 }
921
922 SetBit(BMT_ONLINE, status);
923 SetBit(BMT_BOT, status);
924
925 return status;
926 }
927
OfflineOrRewind()928 bool Device::OfflineOrRewind()
929 {
930 if (fd_ < 0) { return false; }
931 if (HasCap(CAP_OFFLINEUNMOUNT)) {
932 return offline();
933 } else {
934 /*
935 * Note, this rewind probably should not be here (it wasn't
936 * in prior versions of Bareos), but on FreeBSD, this is
937 * needed in the case the tape was "frozen" due to an error
938 * such as backspacing after writing and EOF. If it is not
939 * done, all future references to the drive get and I/O error.
940 */
941 clrerror(MTREW);
942 return rewind(NULL);
943 }
944 }
945
SetSlotNumber(slot_number_t slot)946 void Device::SetSlotNumber(slot_number_t slot)
947 {
948 slot_ = slot;
949 if (vol) { vol->InvalidateSlotNumber(); }
950 }
951
InvalidateSlotNumber()952 void Device::InvalidateSlotNumber()
953 {
954 slot_ = kInvalidSlotNumber;
955 if (vol) { vol->SetSlotNumber(kInvalidSlotNumber); }
956 }
957
958 /**
959 * Reposition the device to file, block
960 *
961 * Returns: false on failure
962 * true on success
963 */
Reposition(DeviceControlRecord * dcr,uint32_t rfile,uint32_t rblock)964 bool Device::Reposition(DeviceControlRecord* dcr,
965 uint32_t rfile,
966 uint32_t rblock)
967 {
968 if (!IsOpen()) {
969 dev_errno = EBADF;
970 Mmsg0(errmsg, _("Bad call to Reposition. Device not open\n"));
971 Emsg0(M_FATAL, 0, errmsg);
972 return false;
973 }
974
975 if (IsFifo() || IsVtl()) { return true; }
976
977 boffset_t pos = (((boffset_t)rfile) << 32) | rblock;
978 Dmsg1(100, "===== lseek to %d\n", (int)pos);
979 if (lseek(dcr, pos, SEEK_SET) == (boffset_t)-1) {
980 BErrNo be;
981 dev_errno = errno;
982 Mmsg2(errmsg, _("lseek error on %s. ERR=%s.\n"), print_name(),
983 be.bstrerror());
984 return false;
985 }
986 file = rfile;
987 block_num = rblock;
988 file_addr = pos;
989 return true;
990 }
991
992 /**
993 * Set to unload the current volume in the drive.
994 */
SetUnload()995 void Device::SetUnload()
996 {
997 if (!unload_ && VolHdr.VolumeName[0] != 0) {
998 unload_ = true;
999 memcpy(UnloadVolName, VolHdr.VolumeName, sizeof(UnloadVolName));
1000 }
1001 }
1002
1003 /**
1004 * Clear volume header.
1005 */
ClearVolhdr()1006 void Device::ClearVolhdr()
1007 {
1008 Dmsg1(100, "Clear volhdr vol=%s\n", VolHdr.VolumeName);
1009 VolHdr = Volume_Label{};
1010 setVolCatInfo(false);
1011 }
1012
1013 /**
1014 * Close the device.
1015 */
close(DeviceControlRecord * dcr)1016 bool Device::close(DeviceControlRecord* dcr)
1017 {
1018 bool retval = true;
1019 int status;
1020 Dmsg1(100, "close_dev %s\n", print_name());
1021
1022 if (!IsOpen()) {
1023 Dmsg2(100, "device %s already closed vol=%s\n", print_name(),
1024 VolHdr.VolumeName);
1025 goto bail_out; /* already closed */
1026 }
1027
1028 if (!norewindonclose) { OfflineOrRewind(); }
1029
1030 switch (dev_type) {
1031 case B_VTL_DEV:
1032 case B_TAPE_DEV:
1033 UnlockDoor();
1034 /*
1035 * Fall through wanted
1036 */
1037 default:
1038 status = d_close(fd_);
1039 if (status < 0) {
1040 BErrNo be;
1041
1042 Mmsg2(errmsg, _("Unable to close device %s. ERR=%s\n"), print_name(),
1043 be.bstrerror());
1044 dev_errno = errno;
1045 retval = false;
1046 }
1047 break;
1048 }
1049
1050 unmount(dcr, 1); /* do unmount if required */
1051
1052 /*
1053 * Clean up device packet so it can be reused.
1054 */
1055 ClearOpened();
1056
1057 ClearBit(ST_LABEL, state);
1058 ClearBit(ST_READREADY, state);
1059 ClearBit(ST_APPENDREADY, state);
1060 ClearBit(ST_EOT, state);
1061 ClearBit(ST_WEOT, state);
1062 ClearBit(ST_EOF, state);
1063 ClearBit(ST_MOUNTED, state);
1064 ClearBit(ST_MEDIA, state);
1065 ClearBit(ST_SHORT, state);
1066
1067 label_type = B_BAREOS_LABEL;
1068 file = block_num = 0;
1069 file_size = 0;
1070 file_addr = 0;
1071 EndFile = EndBlock = 0;
1072 open_mode = 0;
1073 ClearVolhdr();
1074 VolCatInfo = VolumeCatalogInfo{};
1075 if (tid) {
1076 StopThreadTimer(tid);
1077 tid = 0;
1078 }
1079
1080 /*
1081 * We closed the device so let any plugin know we did.
1082 */
1083 if (dcr) { GeneratePluginEvent(dcr->jcr, bsdEventDeviceClose, dcr); }
1084
1085 bail_out:
1086 return retval;
1087 }
1088
1089 /**
1090 * Mount the device.
1091 * If timeout, wait until the mount command returns 0.
1092 * If !timeout, try to mount the device only once.
1093 */
mount(DeviceControlRecord * dcr,int timeout)1094 bool Device::mount(DeviceControlRecord* dcr, int timeout)
1095 {
1096 bool retval = true;
1097 Dmsg0(190, "Enter mount\n");
1098
1099 if (IsMounted()) { return true; }
1100
1101 retval = MountBackend(dcr, timeout);
1102
1103 /*
1104 * When the mount command succeeded sent a
1105 * bsdEventDeviceMount plugin event so any plugin
1106 * that want to do something can do things now.
1107 */
1108 if (retval &&
1109 GeneratePluginEvent(dcr->jcr, bsdEventDeviceMount, dcr) != bRC_OK) {
1110 retval = false;
1111 }
1112
1113 /*
1114 * Mark the device mounted if we succeed.
1115 */
1116 if (retval) { SetMounted(); }
1117
1118 return retval;
1119 }
1120
1121 /**
1122 * Unmount the device
1123 * If timeout, wait until the unmount command returns 0.
1124 * If !timeout, try to unmount the device only once.
1125 */
unmount(DeviceControlRecord * dcr,int timeout)1126 bool Device::unmount(DeviceControlRecord* dcr, int timeout)
1127 {
1128 bool retval = true;
1129 Dmsg0(100, "Enter unmount\n");
1130
1131 /*
1132 * See if the device is mounted.
1133 */
1134 if (!IsMounted()) { return true; }
1135
1136 /*
1137 * Before running the unmount program sent a
1138 * bsdEventDeviceUnmount plugin event so any plugin
1139 * that want to do something can do things now.
1140 */
1141 if (dcr &&
1142 GeneratePluginEvent(dcr->jcr, bsdEventDeviceUnmount, dcr) != bRC_OK) {
1143 retval = false;
1144 goto bail_out;
1145 }
1146
1147 retval = UnmountBackend(dcr, timeout);
1148
1149 /*
1150 * Mark the device unmounted if we succeed.
1151 */
1152 if (retval) { ClearMounted(); }
1153
1154 bail_out:
1155 return retval;
1156 }
1157
1158 /**
1159 * Edit codes into (Un)MountCommand
1160 * %% = %
1161 * %a = archive device name
1162 * %m = mount point
1163 *
1164 * omsg = edited output message
1165 * imsg = input string containing edit codes (%x)
1166 *
1167 */
EditMountCodes(PoolMem & omsg,const char * imsg)1168 void Device::EditMountCodes(PoolMem& omsg, const char* imsg)
1169 {
1170 const char* p;
1171 const char* str;
1172 char add[20];
1173
1174 PoolMem archive_name(PM_FNAME);
1175
1176 omsg.c_str()[0] = 0;
1177 Dmsg1(800, "EditMountCodes: %s\n", imsg);
1178 for (p = imsg; *p; p++) {
1179 if (*p == '%') {
1180 switch (*++p) {
1181 case '%':
1182 str = "%";
1183 break;
1184 case 'a':
1185 str = dev_name;
1186 break;
1187 case 'm':
1188 str = device->mount_point;
1189 break;
1190 default:
1191 add[0] = '%';
1192 add[1] = *p;
1193 add[2] = 0;
1194 str = add;
1195 break;
1196 }
1197 } else {
1198 add[0] = *p;
1199 add[1] = 0;
1200 str = add;
1201 }
1202 Dmsg1(1900, "add_str %s\n", str);
1203 PmStrcat(omsg, (char*)str);
1204 Dmsg1(1800, "omsg=%s\n", omsg.c_str());
1205 }
1206 }
1207
1208 /**
1209 * Return the last timer interval (ms) or 0 if something goes wrong
1210 */
GetTimerCount()1211 btime_t Device::GetTimerCount()
1212 {
1213 btime_t temp = last_timer;
1214 last_timer = GetCurrentBtime();
1215 temp = last_timer - temp; /* get elapsed time */
1216
1217 return (temp > 0) ? temp : 0; /* take care of skewed clock */
1218 }
1219
1220 /**
1221 * Read from device.
1222 */
read(void * buf,size_t len)1223 ssize_t Device::read(void* buf, size_t len)
1224 {
1225 ssize_t read_len;
1226
1227 GetTimerCount();
1228
1229 read_len = d_read(fd_, buf, len);
1230
1231 last_tick = GetTimerCount();
1232
1233 DevReadTime += last_tick;
1234 VolCatInfo.VolReadTime += last_tick;
1235
1236 if (read_len > 0) { /* skip error */
1237 DevReadBytes += read_len;
1238 }
1239
1240 return read_len;
1241 }
1242
1243 /**
1244 * Write to device.
1245 */
write(const void * buf,size_t len)1246 ssize_t Device::write(const void* buf, size_t len)
1247 {
1248 ssize_t write_len;
1249
1250 GetTimerCount();
1251
1252 write_len = d_write(fd_, buf, len);
1253
1254 last_tick = GetTimerCount();
1255
1256 DevWriteTime += last_tick;
1257 VolCatInfo.VolWriteTime += last_tick;
1258
1259 if (write_len > 0) { /* skip error */
1260 DevWriteBytes += write_len;
1261 }
1262
1263 return write_len;
1264 }
1265
1266 /**
1267 * Return the resource name for the device
1268 */
name() const1269 const char* Device::name() const { return device->resource_name_; }
1270
1271 /**
1272 * Free memory allocated for the device
1273 */
term()1274 void Device::term()
1275 {
1276 Dmsg1(900, "term dev: %s\n", print_name());
1277
1278 /*
1279 * On termination we don't have any DCRs left
1280 * so we call close with a NULL argument as
1281 * the dcr argument is only used in the unmount
1282 * method to generate a plugin_event we just check
1283 * there if the dcr is not NULL and otherwise skip
1284 * the plugin event generation.
1285 */
1286 close(NULL);
1287
1288 if (dev_name) {
1289 FreeMemory(dev_name);
1290 dev_name = NULL;
1291 }
1292 if (dev_options) {
1293 FreeMemory(dev_options);
1294 dev_options = NULL;
1295 }
1296 if (prt_name) {
1297 FreeMemory(prt_name);
1298 prt_name = NULL;
1299 }
1300 if (errmsg) {
1301 FreePoolMemory(errmsg);
1302 errmsg = NULL;
1303 }
1304 pthread_mutex_destroy(&mutex_);
1305 pthread_cond_destroy(&wait);
1306 pthread_cond_destroy(&wait_next_vol);
1307 pthread_mutex_destroy(&spool_mutex);
1308 // RwlDestroy(&lock);
1309 if (attached_dcrs) {
1310 delete attached_dcrs;
1311 attached_dcrs = NULL;
1312 }
1313 if (device) { device->dev = NULL; }
1314 delete this;
1315 }
1316
CanStealLock() const1317 bool Device::CanStealLock() const
1318 {
1319 return blocked_ &&
1320 (blocked_ == BST_UNMOUNTED || blocked_ == BST_WAITING_FOR_SYSOP ||
1321 blocked_ == BST_UNMOUNTED_WAITING_FOR_SYSOP);
1322 }
1323
waiting_for_mount() const1324 bool Device::waiting_for_mount() const
1325 {
1326 return (blocked_ == BST_UNMOUNTED || blocked_ == BST_WAITING_FOR_SYSOP ||
1327 blocked_ == BST_UNMOUNTED_WAITING_FOR_SYSOP);
1328 }
1329
IsBlocked() const1330 bool Device::IsBlocked() const { return blocked_ != BST_NOT_BLOCKED; }
1331
1332 } /* namespace storagedaemon */
1333