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 /*********************************************************************
34 * Acquire device for reading. We permit (for the moment)
35 * only one reader. We read the Volume label from the block and
36 * leave the block pointers just after the label.
38 * Returns: 0 if failed for any reason
41 int acquire_device_for_read(JCR *jcr, DEVICE *dev, DEV_BLOCK *block)
44 int tape_previously_mounted;
49 if (device_is_unmounted(dev)) {
50 Jmsg(jcr, M_WARNING, 0, _("device %s is BLOCKED due to user unmount.\n"),
54 block_device(dev, BST_DOING_ACQUIRE);
57 tape_previously_mounted = dev_state(dev, ST_READ) || dev_state(dev, ST_APPEND);
59 if (dev_state(dev, ST_READ) || dev->num_writers > 0) {
60 Jmsg2(jcr, M_FATAL, 0, _("Device %s is busy. Job %d canceled.\n"),
61 dev_name(dev), jcr->JobId);
65 /* Find next Volume, if any */
68 Jmsg(jcr, M_FATAL, 0, _("No volumes specified. Job %d canceled.\n"), jcr->JobId);
72 for (i=1; i<jcr->CurVolume; i++) {
75 pm_strcpy(&jcr->VolumeName, vol->VolumeName);
78 if (job_canceled(jcr)) {
79 Mmsg1(&dev->errmsg, _("Job %d canceled.\n"), jcr->JobId);
80 goto get_out; /* error return */
83 * This code ensures that the device is ready for
84 * reading. If it is a file, it opens it.
85 * If it is a tape, it checks the volume name
87 for ( ; !(dev->state & ST_OPENED); ) {
88 Dmsg1(120, "bstored: open vol=%s\n", jcr->VolumeName);
89 if (open_dev(dev, jcr->VolumeName, READ_ONLY) < 0) {
90 Jmsg(jcr, M_FATAL, 0, _("Open device %s volume %s failed, ERR=%s\n"),
91 dev_name(dev), jcr->VolumeName, strerror_dev(dev));
94 Dmsg1(129, "open_dev %s OK\n", dev_name(dev));
96 dev->state &= ~ST_LABEL; /* force reread of label */
97 Dmsg0(200, "calling read-vol-label\n");
98 switch (read_dev_volume_label(jcr, dev, block)) {
104 * Send error message generated by read_dev_volume_label()
105 * only we really had a tape mounted. This supresses superfluous
106 * error messages when nothing is mounted.
108 if (tape_previously_mounted) {
109 Jmsg(jcr, M_WARNING, 0, "%s", jcr->errmsg);
113 Jmsg(jcr, M_WARNING, 0, "%s", jcr->errmsg);
115 tape_previously_mounted = 1;
116 Dmsg0(200, "dir_get_volume_info\n");
117 if (!dir_get_volume_info(jcr, GET_VOL_INFO_FOR_READ)) {
118 Jmsg1(jcr, M_WARNING, 0, "%s", jcr->errmsg);
120 /* Call autochanger only once unless ask_sysop called */
122 Dmsg2(200, "calling autoload Vol=%s Slot=%d\n",
123 jcr->VolumeName, jcr->VolCatInfo.Slot);
124 if ((autochanger=autoload_device(jcr, dev, 0, NULL))) {
128 /* Mount a specific volume and no other */
129 Dmsg0(200, "calling dir_ask_sysop\n");
130 if (!dir_ask_sysop_to_mount_volume(jcr, dev)) {
131 goto get_out; /* error return */
133 autochanger = 0; /* permit using autochanger again */
134 continue; /* try reading again */
139 Jmsg1(jcr, M_FATAL, 0, _("Too many errors trying to mount device \"%s\".\n"),
144 dev->state |= ST_READ;
145 attach_jcr_to_device(dev, jcr); /* attach jcr to device */
146 Jmsg(jcr, M_INFO, 0, _("Ready to read from volume \"%s\" on device %s.\n"),
147 jcr->VolumeName, dev_name(dev));
157 * Acquire device for writing. We permit multiple writers.
158 * If this is the first one, we read the label.
160 * Returns: NULL if failed for any reason
161 * dev if successful (may change if new dev opened)
162 * This routine must be single threaded because we may create
163 * multiple devices (for files), thus we have our own mutex
164 * on top of the device mutex.
166 DEVICE *acquire_device_for_append(JCR *jcr, DEVICE *dev, DEV_BLOCK *block)
169 bool recycle = false;
170 bool do_mount = false;
171 DEVICE *rtn_dev = NULL;
173 if (device_is_unmounted(dev)) {
174 Jmsg(jcr, M_WARNING, 0, _("device %s is BLOCKED due to user unmount.\n"),
178 block_device(dev, BST_DOING_ACQUIRE);
180 P(mutex); /* lock all devices */
181 Dmsg1(190, "acquire_append device is %s\n", dev_is_tape(dev)?"tape":"disk");
184 if (dev_state(dev, ST_APPEND)) {
186 * Device already in append mode
188 * Check if we have the right Volume mounted
189 * OK if current volume info OK
190 * OK if next volume matches current volume
191 * otherwise mount desired volume obtained from
192 * dir_find_next_appendable_volume
194 pm_strcpy(&jcr->VolumeName, dev->VolHdr.VolName);
195 if (!dir_get_volume_info(jcr, GET_VOL_INFO_FOR_WRITE) &&
196 !(dir_find_next_appendable_volume(jcr) &&
197 strcmp(dev->VolHdr.VolName, jcr->VolumeName) == 0)) { /* wrong tape mounted */
198 if (dev->num_writers != 0) {
199 DEVICE *d = ((DEVRES *)dev->device)->dev;
200 uint32_t open_vols = 0;
201 for ( ; d; d=d->next) {
204 if (dev_state(dev, ST_FILE) && dev->max_open_vols > open_vols) {
205 d = init_dev(NULL, (DEVRES *)dev->device); /* init new device */
206 d->prev = dev; /* chain in new device */
209 /* Release old device */
213 /* Make new device current device and lock it */
216 block_device(dev, BST_DOING_ACQUIRE);
219 Jmsg(jcr, M_FATAL, 0, _("Device %s is busy writing on another Volume.\n"), dev_name(dev));
223 /* Wrong tape mounted, release it, then fall through to get correct one */
228 * At this point, the correct tape is already mounted, so
229 * we do not need to do mount_next_write_volume(), unless
230 * we need to recycle the tape.
232 recycle = strcmp(jcr->VolCatInfo.VolCatStatus, "Recycle") == 0;
233 if (recycle && dev->num_writers != 0) {
234 Jmsg(jcr, M_FATAL, 0, _("Cannot recycle volume \"%s\""
235 " because it is in use by another job."));
240 /* Not already in append mode, so mount the device */
241 if (dev_state(dev, ST_READ)) {
242 Jmsg(jcr, M_FATAL, 0, _("Device %s is busy reading.\n"), dev_name(dev));
245 ASSERT(dev->num_writers == 0);
249 if (do_mount || recycle) {
250 if (!mount_next_write_volume(jcr, dev, block, release)) {
251 if (!job_canceled(jcr)) {
252 /* Reduce "noise" -- don't print if job canceled */
253 Jmsg(jcr, M_FATAL, 0, _("Could not ready device %s for append.\n"),
261 if (jcr->NumVolumes == 0) {
264 attach_jcr_to_device(dev, jcr); /* attach jcr to device */
265 rtn_dev = dev; /* return device */
268 * If we jump here, it is an error return because
269 * rtn_dev will still be NULL
275 V(mutex); /* unlock other threads */
280 * This job is done, so release the device. From a Unix standpoint,
281 * the device remains open.
284 int release_device(JCR *jcr, DEVICE *dev)
287 Dmsg1(100, "release_device device is %s\n", dev_is_tape(dev)?"tape":"disk");
288 if (dev_state(dev, ST_READ)) {
289 dev->state &= ~ST_READ; /* clear read bit */
290 if (!dev_is_tape(dev) || !dev_cap(dev, CAP_ALWAYSOPEN)) {
291 offline_or_rewind_dev(dev);
294 /******FIXME**** send read volume usage statistics to director */
296 } else if (dev->num_writers > 0) {
298 Dmsg1(100, "There are %d writers in release_device\n", dev->num_writers);
299 if (dev->num_writers == 0) {
300 /* If we are the only writer, write EOF after job */
301 if (dev_state(dev, ST_LABEL)) {
302 Dmsg0(100, "dir_create_jobmedia_record. Release\n");
303 if (!dir_create_jobmedia_record(jcr)) {
304 Jmsg(jcr, M_ERROR, 0, _("Could not create JobMedia record for Volume=\"%s\" Job=%s\n"),
305 jcr->VolCatInfo.VolCatName, jcr->Job);
307 if (dev_can_write(dev)) {
310 dev->VolCatInfo.VolCatFiles = dev->file; /* set number of files */
311 dev->VolCatInfo.VolCatJobs++; /* increment number of jobs */
312 /* Note! do volume update before close, which zaps VolCatInfo */
313 Dmsg0(100, "dir_update_vol_info. Release0\n");
314 dir_update_volume_info(jcr, dev, 0); /* send Volume info to Director */
317 if (!dev_is_tape(dev) || !dev_cap(dev, CAP_ALWAYSOPEN)) {
318 offline_or_rewind_dev(dev);
321 } else if (dev_state(dev, ST_LABEL)) {
322 Dmsg0(100, "dir_create_jobmedia_record. Release\n");
323 if (!dir_create_jobmedia_record(jcr)) {
324 Jmsg(jcr, M_ERROR, 0, _("Could not create JobMedia record for Volume=\"%s\" Job=%s\n"),
325 jcr->VolCatInfo.VolCatName, jcr->Job);
327 Dmsg0(100, "dir_update_vol_info. Release1\n");
328 dev->VolCatInfo.VolCatFiles = dev->file; /* set number of files */
329 dev->VolCatInfo.VolCatJobs++; /* increment number of jobs */
330 dir_update_volume_info(jcr, dev, 0); /* send Volume info to Director */
333 Jmsg2(jcr, M_ERROR, 0, _("BAD ERROR: release_device %s, Volume \"%s\" not in use.\n"),
334 dev_name(dev), NPRT(jcr->VolumeName));
336 detach_jcr_from_device(dev, jcr);
337 if (dev->prev && !dev_state(dev, ST_READ) && dev->num_writers == 0) {
340 dev->prev->next = dev->next; /* dechain */