2 * Routines to acquire and release a device for read/write
4 * Kern Sibbald, August MMII
9 Copyright (C) 2002-2003 Kern Sibbald and John Walker
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 pthread_mutex_t mutex = PTHREAD_MUTEX_INITIALIZER;
33 DCR *new_dcr(JCR *jcr, DEVICE *dev)
35 DCR *dcr = (DCR *)malloc(sizeof(DCR));
36 memset(dcr, 0, sizeof(DCR));
42 dcr->block = new_block(dev);
43 dcr->record = new_record();
45 dcr->max_spool_size = dev->device->max_spool_size;
49 void free_dcr(DCR *dcr)
52 free_block(dcr->block);
55 free_record(dcr->record);
64 /*********************************************************************
65 * Acquire device for reading. We permit (for the moment)
66 * only one reader. We read the Volume label from the block and
67 * leave the block pointers just after the label.
69 * Returns: 0 if failed for any reason
72 DCR *acquire_device_for_read(JCR *jcr)
75 bool tape_previously_mounted;
76 bool tape_initially_mounted;
83 /* Called for each volume */
85 dcr = new_dcr(jcr, jcr->device->dev);
88 if (device_is_unmounted(dev)) {
89 Jmsg(jcr, M_WARNING, 0, _("device %s is BLOCKED due to user unmount.\n"),
93 block_device(dev, BST_DOING_ACQUIRE);
96 init_dev_wait_timers(dev);
97 if (dev_state(dev, ST_READ) || dev->num_writers > 0) {
98 Jmsg2(jcr, M_FATAL, 0, _("Device %s is busy. Job %d canceled.\n"),
99 dev_name(dev), jcr->JobId);
103 tape_previously_mounted = dev_state(dev, ST_READ) ||
104 dev_state(dev, ST_APPEND) ||
105 dev_state(dev, ST_LABEL);
106 tape_initially_mounted = tape_previously_mounted;
108 /* Find next Volume, if any */
111 Jmsg(jcr, M_FATAL, 0, _("No volumes specified. Job %d canceled.\n"), jcr->JobId);
115 for (i=1; i<jcr->CurVolume; i++) {
118 pm_strcpy(&jcr->VolumeName, vol->VolumeName);
119 bstrncpy(dcr->VolumeName, vol->VolumeName, sizeof(dcr->VolumeName));
121 for (i=0; i<5; i++) {
122 if (job_canceled(jcr)) {
123 Mmsg1(&dev->errmsg, _("Job %d canceled.\n"), jcr->JobId);
124 goto get_out; /* error return */
127 * This code ensures that the device is ready for
128 * reading. If it is a file, it opens it.
129 * If it is a tape, it checks the volume name
131 for ( ; !(dev->state & ST_OPENED); ) {
132 Dmsg1(120, "bstored: open vol=%s\n", jcr->VolumeName);
133 if (open_dev(dev, dcr->VolumeName, OPEN_READ_ONLY) < 0) {
134 Jmsg(jcr, M_FATAL, 0, _("Open device %s volume %s failed, ERR=%s\n"),
135 dev_name(dev), dcr->VolumeName, strerror_dev(dev));
138 Dmsg1(129, "open_dev %s OK\n", dev_name(dev));
140 /****FIXME***** do not reread label if ioctl() says we are
141 * correctly possitioned. Possibly have way user can turn
142 * this optimization (to be implemented) off.
144 dcr->dev->state &= ~ST_LABEL; /* force reread of label */
145 Dmsg0(200, "calling read-vol-label\n");
146 switch (read_dev_volume_label(jcr, dev, dcr->block)) {
152 * Send error message generated by read_dev_volume_label()
153 * only we really had a tape mounted. This supresses superfluous
154 * error messages when nothing is mounted.
156 if (tape_previously_mounted) {
157 Jmsg(jcr, M_WARNING, 0, "%s", jcr->errmsg);
161 if (tape_initially_mounted) {
162 tape_initially_mounted = false;
167 Jmsg(jcr, M_WARNING, 0, "%s", jcr->errmsg);
169 tape_previously_mounted = true;
170 Dmsg0(200, "dir_get_volume_info\n");
171 if (!dir_get_volume_info(jcr, GET_VOL_INFO_FOR_READ)) {
172 Jmsg1(jcr, M_WARNING, 0, "%s", jcr->errmsg);
174 /* Call autochanger only once unless ask_sysop called */
177 Dmsg2(200, "calling autoload Vol=%s Slot=%d\n",
178 jcr->VolumeName, jcr->VolCatInfo.Slot);
179 stat = autoload_device(jcr, dev, 0, NULL);
185 /* Mount a specific volume and no other */
186 Dmsg0(200, "calling dir_ask_sysop\n");
187 if (!dir_ask_sysop_to_mount_volume(jcr, dev)) {
188 goto get_out; /* error return */
190 autochanger = 0; /* permit using autochanger again */
191 continue; /* try reading again */
196 Jmsg1(jcr, M_FATAL, 0, _("Too many errors trying to mount device \"%s\".\n"),
201 dev->state &= ~ST_APPEND; /* clear any previous append mode */
202 dev->state |= ST_READ; /* set read mode */
203 attach_jcr_to_device(dev, jcr); /* attach jcr to device */
204 Jmsg(jcr, M_INFO, 0, _("Ready to read from volume \"%s\" on device %s.\n"),
205 jcr->VolumeName, dev_name(dev));
219 * Acquire device for writing. We permit multiple writers.
220 * If this is the first one, we read the label.
222 * Returns: NULL if failed for any reason
223 * dev if successful (may change if new dev opened)
224 * This routine must be single threaded because we may create
225 * multiple devices (for files), thus we have our own mutex
226 * on top of the device mutex.
228 DCR *acquire_device_for_append(JCR *jcr)
231 bool recycle = false;
232 bool do_mount = false;
234 DEVICE *dev = jcr->device->dev;
236 dcr = new_dcr(jcr, dev);
237 if (device_is_unmounted(dev)) {
238 Jmsg(jcr, M_WARNING, 0, _("device %s is BLOCKED due to user unmount.\n"),
242 block_device(dev, BST_DOING_ACQUIRE);
244 P(mutex); /* lock all devices */
245 Dmsg1(190, "acquire_append device is %s\n", dev_is_tape(dev)?"tape":"disk");
248 if (dev_state(dev, ST_APPEND)) {
250 * Device already in append mode
252 * Check if we have the right Volume mounted
253 * OK if current volume info OK
254 * OK if next volume matches current volume
255 * otherwise mount desired volume obtained from
256 * dir_find_next_appendable_volume
258 pm_strcpy(&jcr->VolumeName, dev->VolHdr.VolName);
259 bstrncpy(dcr->VolumeName, dev->VolHdr.VolName, sizeof(dcr->VolumeName));
260 if (!dir_get_volume_info(jcr, GET_VOL_INFO_FOR_WRITE) &&
261 !(dir_find_next_appendable_volume(jcr) &&
262 strcmp(dev->VolHdr.VolName, jcr->VolumeName) == 0)) { /* wrong tape mounted */
263 if (dev->num_writers != 0) {
264 DEVICE *d = ((DEVRES *)dev->device)->dev;
265 uint32_t open_vols = 0;
266 for ( ; d; d=d->next) {
269 if (dev_state(dev, ST_FILE) && dev->max_open_vols > open_vols) {
270 d = init_dev(NULL, (DEVRES *)dev->device); /* init new device */
271 d->prev = dev; /* chain in new device */
274 /* Release old device */
278 /* Make new device current device and lock it */
281 block_device(dev, BST_DOING_ACQUIRE);
284 Jmsg(jcr, M_FATAL, 0, _("Device %s is busy writing on another Volume.\n"), dev_name(dev));
288 /* Wrong tape mounted, release it, then fall through to get correct one */
293 * At this point, the correct tape is already mounted, so
294 * we do not need to do mount_next_write_volume(), unless
295 * we need to recycle the tape.
297 recycle = strcmp(jcr->VolCatInfo.VolCatStatus, "Recycle") == 0;
298 if (recycle && dev->num_writers != 0) {
299 Jmsg(jcr, M_FATAL, 0, _("Cannot recycle volume \"%s\""
300 " because it is in use by another job.\n"));
305 /* Not already in append mode, so mount the device */
306 if (dev_state(dev, ST_READ)) {
307 Jmsg(jcr, M_FATAL, 0, _("Device %s is busy reading.\n"), dev_name(dev));
310 ASSERT(dev->num_writers == 0);
314 if (do_mount || recycle) {
315 if (!mount_next_write_volume(jcr, dev, dcr->block, release)) {
316 if (!job_canceled(jcr)) {
317 /* Reduce "noise" -- don't print if job canceled */
318 Jmsg(jcr, M_FATAL, 0, _("Could not ready device \"%s\" for append.\n"),
326 if (jcr->NumVolumes == 0) {
329 attach_jcr_to_device(dev, jcr); /* attach jcr to device */
333 * If we jump here, it is an error return because
334 * rtn_dev will still be NULL
343 V(mutex); /* unlock other threads */
348 * This job is done, so release the device. From a Unix standpoint,
349 * the device remains open.
352 int release_device(JCR *jcr)
354 DEVICE *dev = jcr->dcr->dev;
356 Dmsg1(100, "release_device device is %s\n", dev_is_tape(dev)?"tape":"disk");
357 if (dev_state(dev, ST_READ)) {
358 dev->state &= ~ST_READ; /* clear read bit */
359 if (!dev_is_tape(dev) || !dev_cap(dev, CAP_ALWAYSOPEN)) {
360 offline_or_rewind_dev(dev);
363 /******FIXME**** send read volume usage statistics to director */
365 } else if (dev->num_writers > 0) {
367 Dmsg1(100, "There are %d writers in release_device\n", dev->num_writers);
368 if (dev_state(dev, ST_LABEL)) {
369 Dmsg0(100, "dir_create_jobmedia_record. Release\n");
370 if (!dir_create_jobmedia_record(jcr)) {
371 Jmsg(jcr, M_ERROR, 0, _("Could not create JobMedia record for Volume=\"%s\" Job=%s\n"),
372 jcr->VolCatInfo.VolCatName, jcr->Job);
374 /* If no more writers, write an EOF */
375 if (!dev->num_writers && dev_can_write(dev)) {
378 dev->VolCatInfo.VolCatFiles = dev->file; /* set number of files */
379 dev->VolCatInfo.VolCatJobs++; /* increment number of jobs */
380 /* Note! do volume update before close, which zaps VolCatInfo */
381 Dmsg0(100, "dir_update_vol_info. Release0\n");
382 dir_update_volume_info(jcr, dev, 0); /* send Volume info to Director */
385 if (!dev->num_writers && (!dev_is_tape(dev) || !dev_cap(dev, CAP_ALWAYSOPEN))) {
386 offline_or_rewind_dev(dev);
390 Jmsg2(jcr, M_ERROR, 0, _("BAD ERROR: release_device %s, Volume \"%s\" not in use.\n"),
391 dev_name(dev), NPRT(jcr->VolumeName));
393 detach_jcr_from_device(dev, jcr);
394 if (dev->prev && !dev_state(dev, ST_READ) && !dev->num_writers) {
397 dev->prev->next = dev->next; /* dechain */