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.
68 static void remove_dcr_from_dcrs(DCR *dcr)
74 int num = jcr->dcrs->size();
75 for (i=0; i < num; i++) {
76 ldcr = (DCR *)jcr->dcrs->get(i);
79 if (jcr->dcr == dcr) {
89 * Free up all aspects of the given dcr -- i.e. dechain it,
90 * release allocated memory, zap pointers, ...
92 void free_dcr(DCR *dcr)
95 DEVICE *dev = dcr->dev;
98 * If we reserved the device, we must decrement the
101 if (dcr->reserved_device) {
104 if (dev->num_writers < 0) {
105 Jmsg1(dcr->jcr, M_ERROR, 0, _("Hey! num_writers=%d!!!!\n"), dev->num_writers);
106 dev->num_writers = 0;
107 dcr->reserved_device = false;
112 /* Detach this dcr only if the dev is initialized */
113 if (dev->fd != 0 && jcr && jcr->JobType != JT_SYSTEM) {
114 dev->attached_dcrs->remove(dcr);
115 // remove_dcr_from_dcrs(dcr);
118 free_block(dcr->block);
121 free_record(dcr->rec);
124 dcr->jcr->dcr = NULL;
131 * We "reserve" the drive by setting the ST_READ bit. No one else
132 * should touch the drive until that is cleared.
133 * This allows the DIR to "reserve" the device before actually
134 * starting the job. If the device is not available, the DIR
135 * can wait (to be implemented 1/05).
137 bool reserve_device_for_read(JCR *jcr, DEVICE *dev)
143 if (device_is_unmounted(dev)) {
144 Jmsg(jcr, M_WARNING, 0, _("device %s is BLOCKED due to user unmount.\n"),
149 block_device(dev, BST_DOING_ACQUIRE);
152 if (dev->is_busy()) {
153 Jmsg2(jcr, M_FATAL, 0, _("Device %s is busy. Job %d canceled.\n"),
154 dev->print_name(), jcr->JobId);
158 dcr = new_dcr(jcr, dev);
172 /*********************************************************************
173 * Acquire device for reading.
174 * The drive should have previously been reserved by calling
175 * reserve_device_for_read(). We read the Volume label from the block and
176 * leave the block pointers just after the label.
178 * Returns: NULL if failed for any reason
181 DCR *acquire_device_for_read(JCR *jcr, DEVICE *dev)
184 bool tape_previously_mounted;
185 bool tape_initially_mounted;
187 bool try_autochanger = true;
190 int vol_label_status;
193 block_device(dev, BST_DOING_ACQUIRE);
196 init_dev_wait_timers(dev);
198 tape_previously_mounted = dev->can_read() ||
201 tape_initially_mounted = tape_previously_mounted;
203 if (dev->num_writers > 0) {
204 Jmsg2(jcr, M_FATAL, 0, _("Num_writers=%d not zero. Job %d canceled.\n"),
205 dev->num_writers, jcr->JobId);
209 /* Find next Volume, if any */
212 Jmsg(jcr, M_FATAL, 0, _("No volumes specified. Job %d canceled.\n"), jcr->JobId);
216 for (i=1; i<jcr->CurVolume; i++) {
219 bstrncpy(dcr->VolumeName, vol->VolumeName, sizeof(dcr->VolumeName));
221 /* Volume info is always needed because of VolParts */
222 Dmsg0(200, "dir_get_volume_info\n");
223 if (!dir_get_volume_info(dcr, GET_VOL_INFO_FOR_READ)) {
224 Jmsg1(jcr, M_WARNING, 0, "%s", jcr->errmsg);
227 dev->num_parts = dcr->VolCatInfo.VolCatParts;
229 for (i=0; i<5; i++) {
230 dev->clear_labeled(); /* force reread of label */
231 if (job_canceled(jcr)) {
232 Mmsg1(dev->errmsg, _("Job %d canceled.\n"), jcr->JobId);
233 goto get_out; /* error return */
236 * This code ensures that the device is ready for
237 * reading. If it is a file, it opens it.
238 * If it is a tape, it checks the volume name
240 for ( ; !dev->is_open(); ) {
241 Dmsg1(120, "bstored: open vol=%s\n", dcr->VolumeName);
242 if (open_dev(dev, dcr->VolumeName, OPEN_READ_ONLY) < 0) {
243 if (dev->dev_errno == EIO) { /* no tape loaded */
247 /* If we have a dvd that requires mount,
248 * we need to try to open the label, so the info can be reported
249 * if a wrong volume has been mounted. */
250 if (dev->is_dvd() && (dcr->VolCatInfo.VolCatParts > 0)) {
254 Jmsg(jcr, M_FATAL, 0, _("Open device %s Volume \"%s\" failed: ERR=%s\n"),
255 dev->print_name(), dcr->VolumeName, strerror_dev(dev));
258 Dmsg1(129, "open_dev %s OK\n", dev->print_name());
262 vol_label_status = read_dev_volume_label_guess(dcr, 0);
264 vol_label_status = read_dev_volume_label(dcr);
267 Dmsg0(200, "calling read-vol-label\n");
268 switch (vol_label_status) {
271 memcpy(&dev->VolCatInfo, &dcr->VolCatInfo, sizeof(dev->VolCatInfo));
275 * Send error message generated by read_dev_volume_label()
276 * only we really had a tape mounted. This supresses superfluous
277 * error messages when nothing is mounted.
279 if (tape_previously_mounted) {
280 Jmsg(jcr, M_WARNING, 0, "%s", jcr->errmsg);
284 if (tape_initially_mounted) {
285 tape_initially_mounted = false;
290 Jmsg(jcr, M_WARNING, 0, "%s", jcr->errmsg);
292 tape_previously_mounted = true;
294 /* If the device requires mount, close it, so the device can be ejected.
295 * FIXME: This should perhaps be done for all devices. */
296 if (dev_cap(dev, CAP_REQMOUNT)) {
297 force_close_dev(dev);
300 /* Call autochanger only once unless ask_sysop called */
301 if (try_autochanger) {
303 Dmsg2(200, "calling autoload Vol=%s Slot=%d\n",
304 dcr->VolumeName, dcr->VolCatInfo.Slot);
305 stat = autoload_device(dcr, 0, NULL);
307 try_autochanger = false;
308 continue; /* try reading volume mounted */
312 /* Mount a specific volume and no other */
313 Dmsg0(200, "calling dir_ask_sysop\n");
314 if (!dir_ask_sysop_to_mount_volume(dcr)) {
315 goto get_out; /* error return */
317 try_autochanger = true; /* permit using autochanger again */
318 continue; /* try reading again */
323 Jmsg1(jcr, M_FATAL, 0, _("Too many errors trying to mount device %s.\n"),
330 set_jcr_job_status(jcr, JS_Running);
331 dir_send_job_status(jcr);
332 Jmsg(jcr, M_INFO, 0, _("Ready to read from volume \"%s\" on device %s.\n"),
333 dcr->VolumeName, dev->print_name());
347 * We reserve the device for appending by incrementing the
348 * reserved_device. We do virtually all the same work that
349 * is done in acquire_device_for_append(), but we do
350 * not attempt to mount the device. This routine allows
351 * the DIR to reserve multiple devices before *really*
352 * starting the job. It also permits the SD to refuse
353 * certain devices (not up, ...).
355 * Note, in reserving a device, if the device is for the
356 * same pool and the same pool type, then it is acceptable.
357 * The Media Type has already been checked. If we are
358 * the first tor reserve the device, we put the pool
359 * name and pool type in the device record.
361 bool reserve_device_for_append(JCR *jcr, DEVICE *dev)
369 block_device(dev, BST_DOING_ACQUIRE);
371 if (dev->can_read()) {
372 Jmsg(jcr, M_WARNING, 0, _("Device %s is busy reading.\n"), dev->print_name());
375 if (device_is_unmounted(dev)) {
376 Jmsg(jcr, M_WARNING, 0, _("device %s is BLOCKED due to user unmount.\n"),
380 Dmsg1(190, "reserve_append device is %s\n", dev_is_tape(dev)?"tape":"disk");
382 * First handle the case that the drive is not yet in append mode
384 if (!dev->can_append() && dev->num_writers == 0) {
385 /* Now check if there are any reservations on the drive */
386 if (dev->reserved_device) {
387 /* Yes, now check if we want the same Pool and pool type */
388 if (strcmp(dev->pool_name, dcr->pool_name) == 0 &&
389 strcmp(dev->pool_type, dcr->pool_type) == 0) {
390 /* OK, compatible device */
392 /* Drive not suitable for us */
393 Jmsg(jcr, M_WARNING, 0, _("Device %s is busy writing on another Volume.\n"), dev->print_name());
397 /* Device is available but not yet reserved, reserve it for us */
398 bstrncpy(dev->pool_name, dcr->pool_name, sizeof(dev->pool_name));
399 bstrncpy(dev->pool_type, dcr->pool_type, sizeof(dev->pool_type));
400 dev->PoolId = dcr->PoolId;
406 * Now check if the device is in append mode
408 if (dev->can_append() || dev->num_writers > 0) {
409 Dmsg0(190, "device already in append.\n");
410 /* Yes, now check if we want the same Pool and pool type */
411 if (strcmp(dev->pool_name, dcr->pool_name) == 0 &&
412 strcmp(dev->pool_type, dcr->pool_type) == 0) {
413 /* OK, compatible device */
415 /* Drive not suitable for us */
416 Jmsg(jcr, M_WARNING, 0, _("Device %s is busy writing on another Volume.\n"), dev->print_name());
420 Pmsg0(000, "Logic error!!!! Should not get here.\n");
421 goto bail_out; /* should not get here */
425 dev->reserved_device++;
426 dcr->reserved_device = true;
437 * Acquire device for writing. We permit multiple writers.
438 * If this is the first one, we read the label.
440 * Returns: NULL if failed for any reason
442 * Note, normally reserve_device_for_append() is called
443 * before this routine.
445 DCR *acquire_device_for_append(JCR *jcr, DEVICE *dev)
447 bool release = false;
448 bool recycle = false;
449 bool do_mount = false;
453 dcr = new_dcr(jcr, dev);
456 block_device(dev, BST_DOING_ACQUIRE);
458 Dmsg1(190, "acquire_append device is %s\n", dev_is_tape(dev)?"tape":"disk");
460 if (dcr->reserved_device) {
461 dev->reserved_device--;
462 dcr->reserved_device = false;
466 * With the reservation system, this should not happen
468 if (dev->can_read()) {
469 Jmsg(jcr, M_FATAL, 0, _("Device %s is busy reading.\n"), dev->print_name());
473 if (dev->can_append()) {
474 Dmsg0(190, "device already in append.\n");
476 * Device already in append mode
478 * Check if we have the right Volume mounted
479 * OK if current volume info OK
480 * OK if next volume matches current volume
481 * otherwise mount desired volume obtained from
482 * dir_find_next_appendable_volume
484 bstrncpy(dcr->VolumeName, dev->VolHdr.VolName, sizeof(dcr->VolumeName));
485 if (!dir_get_volume_info(dcr, GET_VOL_INFO_FOR_WRITE) &&
486 !(dir_find_next_appendable_volume(dcr) &&
487 strcmp(dev->VolHdr.VolName, dcr->VolumeName) == 0)) { /* wrong tape mounted */
488 Dmsg0(190, "Wrong tape mounted.\n");
489 if (dev->num_writers != 0 || dev->reserved_device) {
490 Jmsg(jcr, M_FATAL, 0, _("Device %s is busy writing on another Volume.\n"), dev->print_name());
493 /* Wrong tape mounted, release it, then fall through to get correct one */
494 Dmsg0(190, "Wrong tape mounted, release and try mount.\n");
499 * At this point, the correct tape is already mounted, so
500 * we do not need to do mount_next_write_volume(), unless
501 * we need to recycle the tape.
503 recycle = strcmp(dcr->VolCatInfo.VolCatStatus, "Recycle") == 0;
504 Dmsg1(190, "Correct tape mounted. recycle=%d\n", recycle);
505 if (recycle && dev->num_writers != 0) {
506 Jmsg(jcr, M_FATAL, 0, _("Cannot recycle volume \"%s\""
507 " because it is in use by another job.\n"));
510 if (dev->num_writers == 0) {
511 memcpy(&dev->VolCatInfo, &dcr->VolCatInfo, sizeof(dev->VolCatInfo));
515 /* Not already in append mode, so mount the device */
516 Dmsg0(190, "Not in append mode, try mount.\n");
517 ASSERT(dev->num_writers == 0);
521 if (do_mount || recycle) {
522 Dmsg0(190, "Do mount_next_write_vol\n");
523 bool mounted = mount_next_write_volume(dcr, release);
525 if (!job_canceled(jcr)) {
526 /* Reduce "noise" -- don't print if job canceled */
527 Jmsg(jcr, M_FATAL, 0, _("Could not ready device %s for append.\n"),
534 dev->num_writers++; /* we are now a writer */
535 if (jcr->NumVolumes == 0) {
541 * If we jump here, it is an error return because
542 * rtn_dev will still be NULL
555 * This job is done, so release the device. From a Unix standpoint,
556 * the device remains open.
559 bool release_device(DCR *dcr)
563 DEVICE *dev = dcr->dev;
566 Dmsg1(100, "release_device device is %s\n", dev_is_tape(dev)?"tape":"disk");
568 /* if device is reserved, job never started, so release the reserve here */
569 if (dcr->reserved_device) {
570 dev->reserved_device--;
571 dcr->reserved_device = false;
574 if (dev->can_read()) {
575 dev->clear_read(); /* clear read bit */
576 /* Close if file or !CAP_ALWAYSOPEN */
577 if (!dev->is_tape() || !dev_cap(dev, CAP_ALWAYSOPEN)) {
578 offline_or_rewind_dev(dev);
581 /******FIXME**** send read volume usage statistics to director */
583 } else if (dev->num_writers > 0) {
585 Dmsg1(100, "There are %d writers in release_device\n", dev->num_writers);
586 if (dev->is_labeled()) {
587 Dmsg0(100, "dir_create_jobmedia_record. Release\n");
588 if (!dir_create_jobmedia_record(dcr)) {
589 Jmsg(jcr, M_FATAL, 0, _("Could not create JobMedia record for Volume=\"%s\" Job=%s\n"),
590 dcr->VolCatInfo.VolCatName, jcr->Job);
593 /* If no more writers, write an EOF */
594 if (!dev->num_writers && dev_can_write(dev)) {
596 write_ansi_ibm_labels(dcr, ANSI_EOF_LABEL, dev->VolHdr.VolName);
598 dev->VolCatInfo.VolCatFiles = dev->file; /* set number of files */
599 dev->VolCatInfo.VolCatJobs++; /* increment number of jobs */
600 /* Note! do volume update before close, which zaps VolCatInfo */
601 Dmsg0(100, "dir_update_vol_info. Release0\n");
602 dir_update_volume_info(dcr, false); /* send Volume info to Director */
603 Dmsg0(100, "==== write ansi eof label \n");
606 /* If no writers, close if file or !CAP_ALWAYS_OPEN */
607 if (dev->num_writers == 0 && (!dev->is_tape() || !dev_cap(dev, CAP_ALWAYSOPEN))) {
608 offline_or_rewind_dev(dev);
612 Jmsg2(jcr, M_FATAL, 0, _("BAD ERROR: release_device %s, Volume \"%s\" not in use.\n"),
613 dev->print_name(), NPRT(dcr->VolumeName));
614 Jmsg2(jcr, M_ERROR, 0, _("num_writers=%d state=%x\n"), dev->num_writers, dev->state);
618 /* Fire off Alert command and include any output */
619 if (!job_canceled(jcr) && dcr->device->alert_command) {
623 char line[MAXSTRING];
624 alert = get_pool_memory(PM_FNAME);
625 alert = edit_device_codes(dcr, alert, "");
626 bpipe = open_bpipe(alert, 0, "r");
628 while (fgets(line, sizeof(line), bpipe->rfd)) {
629 Jmsg(jcr, M_ALERT, 0, _("Alert: %s"), line);
631 status = close_bpipe(bpipe);
637 Jmsg(jcr, M_ALERT, 0, _("3997 Bad alert command: %s: ERR=%s.\n"),
638 alert, be.strerror(status));
641 Dmsg1(400, "alert status=%d\n", status);
642 free_pool_memory(alert);