2 * Routines to acquire and release a device for read/write
4 * Kern Sibbald, August MMII
9 Copyright (C) 2002-2005 Kern Sibbald
11 This program is free software; you can redistribute it and/or
12 modify it under the terms of the GNU General Public License as
13 published by the Free Software Foundation; either version 2 of
14 the License, or (at your option) any later version.
16 This program is distributed in the hope that it will be useful,
17 but WITHOUT ANY WARRANTY; without even the implied warranty of
18 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
19 General Public License for more details.
21 You should have received a copy of the GNU General Public
22 License along with this program; if not, write to the Free
23 Software Foundation, Inc., 59 Temple Place - Suite 330, Boston,
28 #include "bacula.h" /* pull in global headers */
29 #include "stored.h" /* pull in Storage Deamon headers */
32 * Create a new Device Control Record and attach
33 * it to the device (if this is a real job).
35 DCR *new_dcr(JCR *jcr, DEVICE *dev)
37 if (jcr && jcr->dcr) {
40 DCR *dcr = (DCR *)malloc(sizeof(DCR));
41 memset(dcr, 0, sizeof(DCR));
48 dcr->device = dev->device;
50 dcr->block = new_block(dev);
51 dcr->rec = new_record();
53 dcr->max_spool_size = dev->device->max_spool_size;
54 /* Attach this dcr only if dev is initialized */
55 if (dev->fd != 0 && jcr && jcr->JobType != JT_SYSTEM) {
56 dev->attached_dcrs->append(dcr);
57 // jcr->dcrs->append(dcr);
63 * Search the dcrs list for the given dcr. If it is found,
64 * as it should be, then remove it. Also zap the jcr pointer
65 * to the dcr if it is the same one.
67 static void remove_dcr_from_dcrs(DCR *dcr)
73 int num = jcr->dcrs->size();
74 for (i=0; i < num; i++) {
75 ldcr = (DCR *)jcr->dcrs->get(i);
78 if (jcr->dcr == dcr) {
87 * Free up all aspects of the given dcr -- i.e. dechain it,
88 * release allocated memory, zap pointers, ...
90 void free_dcr(DCR *dcr)
93 DEVICE *dev = dcr->dev;
96 * If we reserved the device, we must decrement the
99 if (dcr->reserved_device) {
102 if (dev->num_writers < 0) {
103 Jmsg1(dcr->jcr, M_ERROR, 0, _("Hey! num_writers=%d!!!!\n"), dev->num_writers);
104 dev->num_writers = 0;
105 dcr->reserved_device = false;
110 /* Detach this dcr only if the dev is initialized */
111 if (dev->fd != 0 && jcr && jcr->JobType != JT_SYSTEM) {
112 dev->attached_dcrs->remove(dcr);
113 // remove_dcr_from_dcrs(dcr);
116 free_block(dcr->block);
119 free_record(dcr->rec);
122 dcr->jcr->dcr = NULL;
129 * We "reserve" the drive by setting the ST_READ bit. No one else
130 * should touch the drive until that is cleared.
131 * This allows the DIR to "reserve" the device before actually
132 * starting the job. If the device is not available, the DIR
133 * can wait (to be implemented 1/05).
135 bool reserve_device_for_read(JCR *jcr, DEVICE *dev)
141 if (device_is_unmounted(dev)) {
142 Jmsg(jcr, M_WARNING, 0, _("device %s is BLOCKED due to user unmount.\n"),
147 block_device(dev, BST_DOING_ACQUIRE);
150 if (dev->is_busy()) {
151 Jmsg2(jcr, M_FATAL, 0, _("Device %s is busy. Job %d canceled.\n"),
152 dev_name(dev), jcr->JobId);
156 dcr = new_dcr(jcr, dev);
170 /*********************************************************************
171 * Acquire device for reading.
172 * The drive should have previously been reserved by calling
173 * reserve_device_for_read(). We read the Volume label from the block and
174 * leave the block pointers just after the label.
176 * Returns: NULL if failed for any reason
179 DCR *acquire_device_for_read(JCR *jcr, DEVICE *dev)
182 bool tape_previously_mounted;
183 bool tape_initially_mounted;
185 bool try_autochanger = true;
188 int vol_label_status;
191 block_device(dev, BST_DOING_ACQUIRE);
194 init_dev_wait_timers(dev);
196 tape_previously_mounted = dev->can_read() ||
199 tape_initially_mounted = tape_previously_mounted;
201 if (dev->num_writers > 0) {
202 Jmsg2(jcr, M_FATAL, 0, _("Num_writers=%d not zero. Job %d canceled.\n"),
203 dev->num_writers, jcr->JobId);
207 /* Find next Volume, if any */
210 Jmsg(jcr, M_FATAL, 0, _("No volumes specified. Job %d canceled.\n"), jcr->JobId);
214 for (i=1; i<jcr->CurVolume; i++) {
217 bstrncpy(dcr->VolumeName, vol->VolumeName, sizeof(dcr->VolumeName));
219 /* Volume info is always needed because of VolParts */
220 Dmsg0(200, "dir_get_volume_info\n");
221 if (!dir_get_volume_info(dcr, GET_VOL_INFO_FOR_READ)) {
222 Jmsg1(jcr, M_WARNING, 0, "%s", jcr->errmsg);
225 dev->num_parts = dcr->VolCatInfo.VolCatParts;
227 for (i=0; i<5; i++) {
228 dev->clear_label(); /* force reread of label */
229 if (job_canceled(jcr)) {
230 Mmsg1(dev->errmsg, _("Job %d canceled.\n"), jcr->JobId);
231 goto get_out; /* error return */
234 * This code ensures that the device is ready for
235 * reading. If it is a file, it opens it.
236 * If it is a tape, it checks the volume name
238 for ( ; !dev->is_open(); ) {
239 Dmsg1(120, "bstored: open vol=%s\n", dcr->VolumeName);
240 if (open_dev(dev, dcr->VolumeName, OPEN_READ_ONLY) < 0) {
241 if (dev->dev_errno == EIO) { /* no tape loaded */
245 /* If we have a dvd that requires mount,
246 * we need to try to open the label, so the info can be reported
247 * if a wrong volume has been mounted. */
248 if (dev->is_dvd() && (dcr->VolCatInfo.VolCatParts > 0)) {
252 Jmsg(jcr, M_FATAL, 0, _("Open device %s volume %s failed, ERR=%s\n"),
253 dev_name(dev), dcr->VolumeName, strerror_dev(dev));
256 Dmsg1(129, "open_dev %s OK\n", dev_name(dev));
260 vol_label_status = read_dev_volume_label_guess(dcr, 0);
262 vol_label_status = read_dev_volume_label(dcr);
265 /****FIXME***** do not reread label if ioctl() says we are
266 * correctly possitioned. Possibly have way user can turn
267 * this optimization (to be implemented) off.
269 Dmsg0(200, "calling read-vol-label\n");
270 switch (vol_label_status) {
273 memcpy(&dev->VolCatInfo, &dcr->VolCatInfo, sizeof(dev->VolCatInfo));
277 * Send error message generated by read_dev_volume_label()
278 * only we really had a tape mounted. This supresses superfluous
279 * error messages when nothing is mounted.
281 if (tape_previously_mounted) {
282 Jmsg(jcr, M_WARNING, 0, "%s", jcr->errmsg);
286 if (tape_initially_mounted) {
287 tape_initially_mounted = false;
292 Jmsg(jcr, M_WARNING, 0, "%s", jcr->errmsg);
294 tape_previously_mounted = true;
296 /* If the device requires mount, close it, so the device can be ejected.
297 * FIXME: This should perhaps be done for all devices. */
298 if (dev_cap(dev, CAP_REQMOUNT)) {
299 force_close_dev(dev);
302 /* Call autochanger only once unless ask_sysop called */
303 if (try_autochanger) {
305 Dmsg2(200, "calling autoload Vol=%s Slot=%d\n",
306 dcr->VolumeName, dcr->VolCatInfo.Slot);
307 stat = autoload_device(dcr, 0, NULL);
309 try_autochanger = false;
314 /* Mount a specific volume and no other */
315 Dmsg0(200, "calling dir_ask_sysop\n");
316 if (!dir_ask_sysop_to_mount_volume(dcr)) {
317 goto get_out; /* error return */
319 try_autochanger = true; /* permit using autochanger again */
320 continue; /* try reading again */
325 Jmsg1(jcr, M_FATAL, 0, _("Too many errors trying to mount device \"%s\".\n"),
332 set_jcr_job_status(jcr, JS_Running);
333 dir_send_job_status(jcr);
334 Jmsg(jcr, M_INFO, 0, _("Ready to read from volume \"%s\" on device %s.\n"),
335 dcr->VolumeName, dev_name(dev));
349 * We reserve the device for appending by incrementing the
350 * reserved_device. We do virtually all the same work that
351 * is done in acquire_device_for_append(), but we do
352 * not attempt to mount the device. This routine allows
353 * the DIR to reserve multiple devices before *really*
354 * starting the job. It also permits the SD to refuse
355 * certain devices (not up, ...).
357 bool reserve_device_for_append(JCR *jcr, DEVICE *dev)
366 block_device(dev, BST_DOING_ACQUIRE);
368 if (device_is_unmounted(dev)) {
369 Jmsg(jcr, M_WARNING, 0, _("device %s is BLOCKED due to user unmount.\n"),
373 Dmsg1(190, "reserve_append device is %s\n", dev_is_tape(dev)?"tape":"disk");
374 if (dev->can_append() || dev->num_writers > 0 || dev->reserved_device) {
375 Dmsg0(190, "device already in append.\n");
377 * Device already in append mode or reserved for write
379 * Check if we have the right Volume mounted
380 * OK if current volume info OK
381 * OK if next volume matches current volume
383 bstrncpy(dcr->VolumeName, dev->VolHdr.VolName, sizeof(dcr->VolumeName));
384 if (!dir_get_volume_info(dcr, GET_VOL_INFO_FOR_WRITE) &&
385 !(dir_find_next_appendable_volume(dcr) &&
386 strcmp(dev->VolHdr.VolName, dcr->VolumeName) == 0)) { /* wrong tape mounted */
387 Dmsg0(190, "Wrong tape mounted.\n");
388 if (dev->num_writers != 0 || dev->reserved_device) {
389 Jmsg(jcr, M_FATAL, 0, _("Device %s is busy writing on another Volume.\n"), dev_name(dev));
394 * At this point, the correct tape is already mounted, so
395 * we do not need to do mount_next_write_volume(), unless
396 * we need to recycle the tape.
398 recycle = strcmp(dcr->VolCatInfo.VolCatStatus, "Recycle") == 0;
399 Dmsg1(190, "Correct tape mounted. recycle=%d\n", recycle);
400 if (recycle && dev->num_writers != 0) {
401 Jmsg(jcr, M_FATAL, 0, _("Cannot recycle volume \"%s\""
402 " because it is in use by another job.\n"));
405 if (dev->num_writers == 0) {
406 memcpy(&dev->VolCatInfo, &dcr->VolCatInfo, sizeof(dev->VolCatInfo));
410 if (dev->can_read()) {
411 Jmsg(jcr, M_FATAL, 0, _("Device %s is busy reading.\n"), dev_name(dev));
414 ASSERT(dev->num_writers == 0);
417 dev->reserved_device++;
418 dcr->reserved_device = true;
429 * Acquire device for writing. We permit multiple writers.
430 * If this is the first one, we read the label.
432 * Returns: NULL if failed for any reason
434 * Note, normally reserve_device_for_append() is called
435 * before this routine.
437 DCR *acquire_device_for_append(JCR *jcr, DEVICE *dev)
439 bool release = false;
440 bool recycle = false;
441 bool do_mount = false;
445 dcr = new_dcr(jcr, dev);
448 block_device(dev, BST_DOING_ACQUIRE);
450 Dmsg1(190, "acquire_append device is %s\n", dev_is_tape(dev)?"tape":"disk");
452 if (dcr->reserved_device) {
453 dev->reserved_device--;
454 dcr->reserved_device = false;
456 if (dev->can_append()) {
457 Dmsg0(190, "device already in append.\n");
459 * Device already in append mode
461 * Check if we have the right Volume mounted
462 * OK if current volume info OK
463 * OK if next volume matches current volume
464 * otherwise mount desired volume obtained from
465 * dir_find_next_appendable_volume
467 bstrncpy(dcr->VolumeName, dev->VolHdr.VolName, sizeof(dcr->VolumeName));
468 if (!dir_get_volume_info(dcr, GET_VOL_INFO_FOR_WRITE) &&
469 !(dir_find_next_appendable_volume(dcr) &&
470 strcmp(dev->VolHdr.VolName, dcr->VolumeName) == 0)) { /* wrong tape mounted */
471 Dmsg0(190, "Wrong tape mounted.\n");
472 if (dev->num_writers != 0 || dev->reserved_device) {
473 Jmsg(jcr, M_FATAL, 0, _("Device %s is busy writing on another Volume.\n"), dev_name(dev));
476 /* Wrong tape mounted, release it, then fall through to get correct one */
477 Dmsg0(190, "Wrong tape mounted, release and try mount.\n");
482 * At this point, the correct tape is already mounted, so
483 * we do not need to do mount_next_write_volume(), unless
484 * we need to recycle the tape.
486 recycle = strcmp(dcr->VolCatInfo.VolCatStatus, "Recycle") == 0;
487 Dmsg1(190, "Correct tape mounted. recycle=%d\n", recycle);
488 if (recycle && dev->num_writers != 0) {
489 Jmsg(jcr, M_FATAL, 0, _("Cannot recycle volume \"%s\""
490 " because it is in use by another job.\n"));
493 if (dev->num_writers == 0) {
494 memcpy(&dev->VolCatInfo, &dcr->VolCatInfo, sizeof(dev->VolCatInfo));
498 /* Not already in append mode, so mount the device */
499 Dmsg0(190, "Not in append mode, try mount.\n");
500 if (dev->can_read()) {
501 Jmsg(jcr, M_FATAL, 0, _("Device %s is busy reading.\n"), dev_name(dev));
504 ASSERT(dev->num_writers == 0);
508 if (do_mount || recycle) {
509 Dmsg0(190, "Do mount_next_write_vol\n");
510 bool mounted = mount_next_write_volume(dcr, release);
512 if (!job_canceled(jcr)) {
513 /* Reduce "noise" -- don't print if job canceled */
514 Jmsg(jcr, M_FATAL, 0, _("Could not ready device \"%s\" for append.\n"),
521 dev->num_writers++; /* we are now a writer */
522 if (jcr->NumVolumes == 0) {
528 * If we jump here, it is an error return because
529 * rtn_dev will still be NULL
542 * This job is done, so release the device. From a Unix standpoint,
543 * the device remains open.
546 bool release_device(DCR *dcr)
549 DEVICE *dev = dcr->dev;
551 Dmsg1(100, "release_device device is %s\n", dev_is_tape(dev)?"tape":"disk");
553 /* if device is reserved, job never started, so release the reserve here */
554 if (dcr->reserved_device) {
555 dev->reserved_device--;
556 dcr->reserved_device = false;
559 if (dev->can_read()) {
560 dev->clear_read(); /* clear read bit */
561 /* Close if file or !CAP_ALWAYSOPEN */
562 if (!dev->is_tape() || !dev_cap(dev, CAP_ALWAYSOPEN)) {
563 offline_or_rewind_dev(dev);
566 /******FIXME**** send read volume usage statistics to director */
568 } else if (dev->num_writers > 0) {
570 Dmsg1(100, "There are %d writers in release_device\n", dev->num_writers);
571 if (dev->is_labeled()) {
572 Dmsg0(100, "dir_create_jobmedia_record. Release\n");
573 if (!dir_create_jobmedia_record(dcr)) {
574 Jmsg(jcr, M_FATAL, 0, _("Could not create JobMedia record for Volume=\"%s\" Job=%s\n"),
575 dcr->VolCatInfo.VolCatName, jcr->Job);
577 /* If no more writers, write an EOF */
578 if (!dev->num_writers && dev_can_write(dev)) {
581 dev->VolCatInfo.VolCatFiles = dev->file; /* set number of files */
582 dev->VolCatInfo.VolCatJobs++; /* increment number of jobs */
583 /* Note! do volume update before close, which zaps VolCatInfo */
584 Dmsg0(100, "dir_update_vol_info. Release0\n");
585 dir_update_volume_info(dcr, false); /* send Volume info to Director */
588 /* If no writers, close if file or !CAP_ALWAYS_OPEN */
589 if (dev->num_writers == 0 && (!dev->is_tape() || !dev_cap(dev, CAP_ALWAYSOPEN))) {
590 offline_or_rewind_dev(dev);
594 Jmsg2(jcr, M_FATAL, 0, _("BAD ERROR: release_device %s, Volume \"%s\" not in use.\n"),
595 dev_name(dev), NPRT(dcr->VolumeName));
596 Jmsg2(jcr, M_ERROR, 0, _("num_writers=%d state=%x\n"), dev->num_writers, dev->state);
599 /* Fire off Alert command and include any output */
600 if (!job_canceled(jcr) && dcr->device->alert_command) {
604 char line[MAXSTRING];
605 alert = get_pool_memory(PM_FNAME);
606 alert = edit_device_codes(dcr, alert, "");
607 bpipe = open_bpipe(alert, 0, "r");
609 while (fgets(line, sizeof(line), bpipe->rfd)) {
610 Jmsg(jcr, M_ALERT, 0, _("Alert: %s"), line);
612 status = close_bpipe(bpipe);
618 Jmsg(jcr, M_ALERT, 0, _("3997 Bad alert command: %s: ERR=%s.\n"),
619 alert, be.strerror(status));
622 Dmsg1(400, "alert status=%d\n", status);
623 free_pool_memory(alert);