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 */
31 static int can_reserve_drive(DCR *dcr);
34 * Create a new Device Control Record and attach
35 * it to the device (if this is a real job).
37 DCR *new_dcr(JCR *jcr, DEVICE *dev)
39 if (jcr && jcr->dcr) {
42 DCR *dcr = (DCR *)malloc(sizeof(DCR));
43 memset(dcr, 0, sizeof(DCR));
50 dcr->device = dev->device;
52 dcr->block = new_block(dev);
53 dcr->rec = new_record();
55 dcr->max_spool_size = dev->device->max_spool_size;
56 /* Attach this dcr only if dev is initialized */
57 if (dev->fd != 0 && jcr && jcr->JobType != JT_SYSTEM) {
58 dev->attached_dcrs->append(dcr); /* attach dcr to device */
59 // jcr->dcrs->append(dcr); /* put dcr in list for Job */
65 * Search the dcrs list for the given dcr. If it is found,
66 * as it should be, then remove it. Also zap the jcr pointer
67 * to the dcr if it is the same one.
70 static void remove_dcr_from_dcrs(DCR *dcr)
76 int num = jcr->dcrs->size();
77 for (i=0; i < num; i++) {
78 ldcr = (DCR *)jcr->dcrs->get(i);
81 if (jcr->dcr == dcr) {
91 * Free up all aspects of the given dcr -- i.e. dechain it,
92 * release allocated memory, zap pointers, ...
94 void free_dcr(DCR *dcr)
97 DEVICE *dev = dcr->dev;
100 * If we reserved the device, we must decrement the
103 if (dcr->reserved_device) {
106 if (dev->num_writers < 0) {
107 Jmsg1(dcr->jcr, M_ERROR, 0, _("Hey! num_writers=%d!!!!\n"), dev->num_writers);
108 dev->num_writers = 0;
109 dcr->reserved_device = false;
114 /* Detach this dcr only if the dev is initialized */
115 if (dev->fd != 0 && jcr && jcr->JobType != JT_SYSTEM) {
116 dev->attached_dcrs->remove(dcr); /* detach dcr from device */
117 // remove_dcr_from_dcrs(dcr); /* remove dcr from jcr list */
120 free_block(dcr->block);
123 free_record(dcr->rec);
126 dcr->jcr->dcr = NULL;
133 * We "reserve" the drive by setting the ST_READ bit. No one else
134 * should touch the drive until that is cleared.
135 * This allows the DIR to "reserve" the device before actually
136 * starting the job. If the device is not available, the DIR
137 * can wait (to be implemented 1/05).
139 bool reserve_device_for_read(DCR *dcr)
141 DEVICE *dev = dcr->dev;
147 dev->block(BST_DOING_ACQUIRE);
149 Mmsg(jcr->errmsg, _("Device %s is BLOCKED due to user unmount.\n"),
151 for (first=true; device_is_unmounted(dev); first=false) {
153 if (!wait_for_device(dcr, jcr->errmsg, first)) {
156 dev->block(BST_DOING_ACQUIRE);
159 Mmsg2(jcr->errmsg, _("Device %s is busy. Job %d canceled.\n"),
160 dev->print_name(), jcr->JobId);
161 for (first=true; dev->is_busy(); first=false) {
163 if (!wait_for_device(dcr, jcr->errmsg, first)) {
166 dev->block(BST_DOING_ACQUIRE);
176 /*********************************************************************
177 * Acquire device for reading.
178 * The drive should have previously been reserved by calling
179 * reserve_device_for_read(). We read the Volume label from the block and
180 * leave the block pointers just after the label.
182 * Returns: NULL if failed for any reason
185 DCR *acquire_device_for_read(DCR *dcr)
187 DEVICE *dev = dcr->dev;
190 bool tape_previously_mounted;
191 bool tape_initially_mounted;
193 bool try_autochanger = true;
195 int vol_label_status;
197 dev->block(BST_DOING_ACQUIRE);
199 if (dev->num_writers > 0) {
200 Jmsg2(jcr, M_FATAL, 0, _("Num_writers=%d not zero. Job %d canceled.\n"),
201 dev->num_writers, jcr->JobId);
205 /* Find next Volume, if any */
208 Jmsg(jcr, M_FATAL, 0, _("No volumes specified. Job %d canceled.\n"), jcr->JobId);
212 for (i=1; i<jcr->CurVolume; i++) {
216 goto get_out; /* should not happen */
218 bstrncpy(dcr->VolumeName, vol->VolumeName, sizeof(dcr->VolumeName));
220 init_device_wait_timers(dcr);
222 tape_previously_mounted = dev->can_read() ||
225 tape_initially_mounted = tape_previously_mounted;
228 /* Volume info is always needed because of VolParts */
229 Dmsg0(200, "dir_get_volume_info\n");
230 if (!dir_get_volume_info(dcr, GET_VOL_INFO_FOR_READ)) {
231 Jmsg1(jcr, M_WARNING, 0, "%s", jcr->errmsg);
234 dev->num_parts = dcr->VolCatInfo.VolCatParts;
236 for (i=0; i<5; i++) {
237 dev->clear_labeled(); /* force reread of label */
238 if (job_canceled(jcr)) {
239 Mmsg1(dev->errmsg, _("Job %d canceled.\n"), jcr->JobId);
240 goto get_out; /* error return */
243 * This code ensures that the device is ready for
244 * reading. If it is a file, it opens it.
245 * If it is a tape, it checks the volume name
247 for ( ; !dev->is_open(); ) {
248 Dmsg1(120, "bstored: open vol=%s\n", dcr->VolumeName);
249 if (open_dev(dev, dcr->VolumeName, OPEN_READ_ONLY) < 0) {
250 if (dev->dev_errno == EIO) { /* no tape loaded */
251 Jmsg3(jcr, M_WARNING, 0, _("Open device %s Volume \"%s\" failed: ERR=%s\n"),
252 dev->print_name(), dcr->VolumeName, strerror_dev(dev));
256 /* If we have a dvd that requires mount,
257 * we need to try to open the label, so the info can be reported
258 * if a wrong volume has been mounted. */
259 if (dev->is_dvd() && (dcr->VolCatInfo.VolCatParts > 0)) {
263 Jmsg3(jcr, M_FATAL, 0, _("Open device %s Volume \"%s\" failed: ERR=%s\n"),
264 dev->print_name(), dcr->VolumeName, strerror_dev(dev));
267 Dmsg1(129, "open_dev %s OK\n", dev->print_name());
271 vol_label_status = read_dev_volume_label_guess(dcr, 0);
273 vol_label_status = read_dev_volume_label(dcr);
276 Dmsg0(200, "calling read-vol-label\n");
277 switch (vol_label_status) {
280 memcpy(&dev->VolCatInfo, &dcr->VolCatInfo, sizeof(dev->VolCatInfo));
284 * Send error message generated by read_dev_volume_label()
285 * only we really had a tape mounted. This supresses superfluous
286 * error messages when nothing is mounted.
288 if (tape_previously_mounted) {
289 Jmsg(jcr, M_WARNING, 0, "%s", jcr->errmsg);
293 if (tape_initially_mounted) {
294 tape_initially_mounted = false;
299 Jmsg1(jcr, M_WARNING, 0, "%s", jcr->errmsg);
301 tape_previously_mounted = true;
303 /* If the device requires mount, close it, so the device can be ejected.
304 * FIXME: This should perhaps be done for all devices. */
305 if (dev_cap(dev, CAP_REQMOUNT)) {
306 force_close_dev(dev);
309 /* Call autochanger only once unless ask_sysop called */
310 if (try_autochanger) {
312 Dmsg2(200, "calling autoload Vol=%s Slot=%d\n",
313 dcr->VolumeName, dcr->VolCatInfo.Slot);
314 stat = autoload_device(dcr, 0, NULL);
316 try_autochanger = false;
317 continue; /* try reading volume mounted */
321 /* Mount a specific volume and no other */
322 Dmsg0(200, "calling dir_ask_sysop\n");
323 if (!dir_ask_sysop_to_mount_volume(dcr)) {
324 goto get_out; /* error return */
326 try_autochanger = true; /* permit using autochanger again */
327 continue; /* try reading again */
332 Jmsg1(jcr, M_FATAL, 0, _("Too many errors trying to mount device %s.\n"),
339 set_jcr_job_status(jcr, JS_Running);
340 dir_send_job_status(jcr);
341 Jmsg(jcr, M_INFO, 0, _("Ready to read from volume \"%s\" on device %s.\n"),
342 dcr->VolumeName, dev->print_name());
354 * We reserve the device for appending by incrementing the
355 * reserved_device. We do virtually all the same work that
356 * is done in acquire_device_for_append(), but we do
357 * not attempt to mount the device. This routine allows
358 * the DIR to reserve multiple devices before *really*
359 * starting the job. It also permits the SD to refuse
360 * certain devices (not up, ...).
362 * Note, in reserving a device, if the device is for the
363 * same pool and the same pool type, then it is acceptable.
364 * The Media Type has already been checked. If we are
365 * the first tor reserve the device, we put the pool
366 * name and pool type in the device record.
368 bool reserve_device_for_append(DCR *dcr)
371 DEVICE *dev = dcr->dev;
376 dev->block(BST_DOING_ACQUIRE);
378 Mmsg2(jcr->errmsg, _("Device %s is busy reading. Job %d canceled.\n"),
379 dev->print_name(), jcr->JobId);
380 for (first=true; dev->can_read(); first=false) {
382 if (!wait_for_device(dcr, jcr->errmsg, first)) {
385 dev->block(BST_DOING_ACQUIRE);
389 Mmsg(jcr->errmsg, _("Device %s is BLOCKED due to user unmount.\n"),
391 for (first=true; device_is_unmounted(dev); first=false) {
393 if (!wait_for_device(dcr, jcr->errmsg, first)) {
396 dev->block(BST_DOING_ACQUIRE);
399 Dmsg1(190, "reserve_append device is %s\n", dev_is_tape(dev)?"tape":"disk");
402 switch (can_reserve_drive(dcr)) {
404 /* ****FIXME**** Make wait */
407 goto bail_out; /* error */
409 break; /* OK, reserve drive */
415 dev->reserved_device++;
416 dcr->reserved_device = true;
425 * Returns: 1 if drive can be reserved
426 * 0 if we should wait
429 static int can_reserve_drive(DCR *dcr)
431 DEVICE *dev = dcr->dev;
434 * First handle the case that the drive is not yet in append mode
436 if (!dev->can_append() && dev->num_writers == 0) {
437 /* Now check if there are any reservations on the drive */
438 if (dev->reserved_device) {
439 /* Yes, now check if we want the same Pool and pool type */
440 if (strcmp(dev->pool_name, dcr->pool_name) == 0 &&
441 strcmp(dev->pool_type, dcr->pool_type) == 0) {
442 /* OK, compatible device */
444 /* Drive not suitable for us */
445 Jmsg(jcr, M_WARNING, 0, _("Device %s is busy writing on another Volume.\n"), dev->print_name());
449 /* Device is available but not yet reserved, reserve it for us */
450 bstrncpy(dev->pool_name, dcr->pool_name, sizeof(dev->pool_name));
451 bstrncpy(dev->pool_type, dcr->pool_type, sizeof(dev->pool_type));
452 dev->PoolId = dcr->PoolId;
454 return 1; /* reserve drive */
458 * Now check if the device is in append mode
460 if (dev->can_append() || dev->num_writers > 0) {
461 Dmsg0(190, "device already in append.\n");
462 /* Yes, now check if we want the same Pool and pool type */
463 if (strcmp(dev->pool_name, dcr->pool_name) == 0 &&
464 strcmp(dev->pool_type, dcr->pool_type) == 0) {
465 /* OK, compatible device */
467 /* Drive not suitable for us */
468 Jmsg(jcr, M_WARNING, 0, _("Device %s is busy writing on another Volume.\n"), dev->print_name());
472 Pmsg0(000, "Logic error!!!! Should not get here.\n");
473 Jmsg0(jcr, M_FATAL, 0, _("Logic error!!!! Should not get here.\n"));
474 return -1; /* error, should not get here */
476 return 1; /* reserve drive */
480 * Acquire device for writing. We permit multiple writers.
481 * If this is the first one, we read the label.
483 * Returns: NULL if failed for any reason
485 * Note, normally reserve_device_for_append() is called
486 * before this routine.
488 DCR *acquire_device_for_append(DCR *dcr)
490 bool release = false;
491 bool recycle = false;
492 bool do_mount = false;
493 DEVICE *dev = dcr->dev;
496 dev->block(BST_DOING_ACQUIRE);
497 Dmsg1(190, "acquire_append device is %s\n", dev_is_tape(dev)?"tape":"disk");
499 if (dcr->reserved_device) {
500 dev->reserved_device--;
501 dcr->reserved_device = false;
505 * With the reservation system, this should not happen
507 if (dev->can_read()) {
508 Jmsg(jcr, M_FATAL, 0, _("Device %s is busy reading.\n"), dev->print_name());
512 if (dev->can_append()) {
513 Dmsg0(190, "device already in append.\n");
515 * Device already in append mode
517 * Check if we have the right Volume mounted
518 * OK if current volume info OK
519 * OK if next volume matches current volume
520 * otherwise mount desired volume obtained from
521 * dir_find_next_appendable_volume
523 bstrncpy(dcr->VolumeName, dev->VolHdr.VolName, sizeof(dcr->VolumeName));
524 if (!dir_get_volume_info(dcr, GET_VOL_INFO_FOR_WRITE) &&
525 !(dir_find_next_appendable_volume(dcr) &&
526 strcmp(dev->VolHdr.VolName, dcr->VolumeName) == 0)) { /* wrong tape mounted */
527 Dmsg0(190, "Wrong tape mounted.\n");
528 if (dev->num_writers != 0 || dev->reserved_device) {
529 Jmsg(jcr, M_FATAL, 0, _("Device %s is busy writing on another Volume.\n"), dev->print_name());
532 /* Wrong tape mounted, release it, then fall through to get correct one */
533 Dmsg0(190, "Wrong tape mounted, release and try mount.\n");
538 * At this point, the correct tape is already mounted, so
539 * we do not need to do mount_next_write_volume(), unless
540 * we need to recycle the tape.
542 recycle = strcmp(dcr->VolCatInfo.VolCatStatus, "Recycle") == 0;
543 Dmsg1(190, "Correct tape mounted. recycle=%d\n", recycle);
544 if (recycle && dev->num_writers != 0) {
545 Jmsg(jcr, M_FATAL, 0, _("Cannot recycle volume \"%s\""
546 " because it is in use by another job.\n"));
549 if (dev->num_writers == 0) {
550 memcpy(&dev->VolCatInfo, &dcr->VolCatInfo, sizeof(dev->VolCatInfo));
554 /* Not already in append mode, so mount the device */
555 Dmsg0(190, "Not in append mode, try mount.\n");
556 ASSERT(dev->num_writers == 0);
560 if (do_mount || recycle) {
561 Dmsg0(190, "Do mount_next_write_vol\n");
562 bool mounted = mount_next_write_volume(dcr, release);
564 if (!job_canceled(jcr)) {
565 /* Reduce "noise" -- don't print if job canceled */
566 Jmsg(jcr, M_FATAL, 0, _("Could not ready device %s for append.\n"),
573 dev->num_writers++; /* we are now a writer */
574 if (jcr->NumVolumes == 0) {
580 * If we jump here, it is an error return because
581 * rtn_dev will still be NULL
592 * This job is done, so release the device. From a Unix standpoint,
593 * the device remains open.
596 bool release_device(DCR *dcr)
600 DEVICE *dev = dcr->dev;
603 Dmsg1(100, "release_device device is %s\n", dev_is_tape(dev)?"tape":"disk");
605 /* if device is reserved, job never started, so release the reserve here */
606 if (dcr->reserved_device) {
607 dev->reserved_device--;
608 dcr->reserved_device = false;
611 if (dev->can_read()) {
612 dev->clear_read(); /* clear read bit */
614 /******FIXME**** send read volume usage statistics to director */
616 } else if (dev->num_writers > 0) {
618 Dmsg1(100, "There are %d writers in release_device\n", dev->num_writers);
619 if (dev->is_labeled()) {
620 Dmsg0(100, "dir_create_jobmedia_record. Release\n");
621 if (!dir_create_jobmedia_record(dcr)) {
622 Jmsg(jcr, M_FATAL, 0, _("Could not create JobMedia record for Volume=\"%s\" Job=%s\n"),
623 dcr->VolCatInfo.VolCatName, jcr->Job);
626 /* If no more writers, write an EOF */
627 if (!dev->num_writers && dev_can_write(dev)) {
629 write_ansi_ibm_labels(dcr, ANSI_EOF_LABEL, dev->VolHdr.VolName);
631 dev->VolCatInfo.VolCatFiles = dev->file; /* set number of files */
632 dev->VolCatInfo.VolCatJobs++; /* increment number of jobs */
633 /* Note! do volume update before close, which zaps VolCatInfo */
634 Dmsg0(100, "dir_update_vol_info. Release0\n");
635 dir_update_volume_info(dcr, false); /* send Volume info to Director */
636 Dmsg0(100, "==== write ansi eof label \n");
641 * If we reach here, it is most likely because the
642 * has failed, since the device is not in read mode and
643 * there are no writers.
647 /* If no writers, close if file or !CAP_ALWAYS_OPEN */
648 if (dev->num_writers == 0 && (!dev->is_tape() || !dev_cap(dev, CAP_ALWAYSOPEN))) {
649 offline_or_rewind_dev(dev);
653 /* Fire off Alert command and include any output */
654 if (!job_canceled(jcr) && dcr->device->alert_command) {
658 char line[MAXSTRING];
659 alert = get_pool_memory(PM_FNAME);
660 alert = edit_device_codes(dcr, alert, "");
661 bpipe = open_bpipe(alert, 0, "r");
663 while (fgets(line, sizeof(line), bpipe->rfd)) {
664 Jmsg(jcr, M_ALERT, 0, _("Alert: %s"), line);
666 status = close_bpipe(bpipe);
672 Jmsg(jcr, M_ALERT, 0, _("3997 Bad alert command: %s: ERR=%s.\n"),
673 alert, be.strerror(status));
676 Dmsg1(400, "alert status=%d\n", status);
677 free_pool_memory(alert);
682 pthread_cond_broadcast(&wait_device_release);