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 is BLOCKED due to user unmount.\n"));
53 block_device(dev, BST_DOING_ACQUIRE);
56 tape_previously_mounted = dev_state(dev, ST_READ) || dev_state(dev, ST_APPEND);
58 if (dev_state(dev, ST_READ) || dev->num_writers > 0) {
59 Jmsg2(jcr, M_FATAL, 0, _("Device %s is busy. Job %d canceled.\n"),
60 dev_name(dev), jcr->JobId);
64 /* Find next Volume, if any */
67 Jmsg(jcr, M_FATAL, 0, _("No volumes specified. Job %d canceled.\n"), jcr->JobId);
71 for (i=1; i<jcr->CurVolume; i++) {
74 pm_strcpy(&jcr->VolumeName, vol->VolumeName);
77 if (job_canceled(jcr)) {
78 Mmsg1(&dev->errmsg, _("Job %d canceled.\n"), jcr->JobId);
79 goto get_out; /* error return */
82 * This code ensures that the device is ready for
83 * reading. If it is a file, it opens it.
84 * If it is a tape, it checks the volume name
86 for ( ; !(dev->state & ST_OPENED); ) {
87 Dmsg1(120, "bstored: open vol=%s\n", jcr->VolumeName);
88 if (open_dev(dev, jcr->VolumeName, READ_ONLY) < 0) {
89 Jmsg(jcr, M_FATAL, 0, _("Open device %s volume %s failed, ERR=%s\n"),
90 dev_name(dev), jcr->VolumeName, strerror_dev(dev));
93 Dmsg1(129, "open_dev %s OK\n", dev_name(dev));
95 dev->state &= ~ST_LABEL; /* force reread of label */
96 Dmsg0(200, "calling read-vol-label\n");
97 switch (read_dev_volume_label(jcr, dev, block)) {
103 * Send error message generated by read_dev_volume_label()
104 * only we really had a tape mounted. This supresses superfluous
105 * error messages when nothing is mounted.
107 if (tape_previously_mounted) {
108 Jmsg(jcr, M_WARNING, 0, "%s", jcr->errmsg);
112 Jmsg(jcr, M_WARNING, 0, "%s", jcr->errmsg);
114 tape_previously_mounted = 1;
115 Dmsg0(200, "dir_get_volume_info\n");
116 if (!dir_get_volume_info(jcr, GET_VOL_INFO_FOR_READ)) {
117 Jmsg1(jcr, M_WARNING, 0, "%s", jcr->errmsg);
119 /* Call autochanger only once unless ask_sysop called */
121 Dmsg2(200, "calling autoload Vol=%s Slot=%d\n",
122 jcr->VolumeName, jcr->VolCatInfo.Slot);
123 if ((autochanger=autoload_device(jcr, dev, 0, NULL))) {
127 /* Mount a specific volume and no other */
128 Dmsg0(200, "calling dir_ask_sysop\n");
129 if (!dir_ask_sysop_to_mount_volume(jcr, dev)) {
130 goto get_out; /* error return */
132 autochanger = 0; /* permit using autochanger again */
133 continue; /* try reading again */
138 Jmsg1(jcr, M_FATAL, 0, _("Too many errors trying to mount device \"%s\".\n"),
143 dev->state |= ST_READ;
144 attach_jcr_to_device(dev, jcr); /* attach jcr to device */
145 Jmsg(jcr, M_INFO, 0, _("Ready to read from volume \"%s\" on device %s.\n"),
146 jcr->VolumeName, dev_name(dev));
156 * Acquire device for writing. We permit multiple writers.
157 * If this is the first one, we read the label.
159 * Returns: NULL if failed for any reason
160 * dev if successful (may change if new dev opened)
161 * This routine must be single threaded because we may create
162 * multiple devices (for files), thus we have our own mutex
163 * on top of the device mutex.
165 DEVICE *acquire_device_for_append(JCR *jcr, DEVICE *dev, DEV_BLOCK *block)
168 bool recycle = false;
169 bool do_mount = false;
170 DEVICE *rtn_dev = NULL;
172 if (device_is_unmounted(dev)) {
173 Jmsg(jcr, M_WARNING, 0, _("device is BLOCKED due to user unmount.\n"));
176 block_device(dev, BST_DOING_ACQUIRE);
178 P(mutex); /* lock all devices */
179 Dmsg1(190, "acquire_append device is %s\n", dev_is_tape(dev)?"tape":"disk");
182 if (dev_state(dev, ST_APPEND)) {
184 * Device already in append mode
186 * Check if we have the right Volume mounted
187 * OK if current volume info OK
188 * OK if next volume matches current volume
189 * otherwise mount desired volume obtained from
190 * dir_find_next_appendable_volume
192 pm_strcpy(&jcr->VolumeName, dev->VolHdr.VolName);
193 if (!dir_get_volume_info(jcr, GET_VOL_INFO_FOR_WRITE) &&
194 !(dir_find_next_appendable_volume(jcr) &&
195 strcmp(dev->VolHdr.VolName, jcr->VolumeName) == 0)) { /* wrong tape mounted */
196 if (dev->num_writers != 0) {
197 DEVICE *d = ((DEVRES *)dev->device)->dev;
198 uint32_t open_vols = 0;
199 for ( ; d; d=d->next) {
202 if (dev_state(dev, ST_FILE) && dev->max_open_vols > open_vols) {
203 d = init_dev(NULL, (DEVRES *)dev->device); /* init new device */
204 d->prev = dev; /* chain in new device */
207 /* Release old device */
211 /* Make new device current device and lock it */
214 block_device(dev, BST_DOING_ACQUIRE);
217 Jmsg(jcr, M_FATAL, 0, _("Device %s is busy writing on another Volume.\n"), dev_name(dev));
221 /* Wrong tape mounted, release it, then fall through to get correct one */
226 * At this point, the correct tape is already mounted, so
227 * we do not need to do mount_next_write_volume(), unless
228 * we need to recycle the tape.
230 recycle = strcmp(jcr->VolCatInfo.VolCatStatus, "Recycle") == 0;
231 if (recycle && dev->num_writers != 0) {
232 Jmsg(jcr, M_FATAL, 0, _("Cannot recycle volume \"%s\""
233 " because it is in use by another job."));
238 /* Not already in append mode, so mount the device */
239 if (dev_state(dev, ST_READ)) {
240 Jmsg(jcr, M_FATAL, 0, _("Device %s is busy reading.\n"), dev_name(dev));
243 ASSERT(dev->num_writers == 0);
247 if (do_mount || recycle) {
248 if (!mount_next_write_volume(jcr, dev, block, release)) {
249 if (!job_canceled(jcr)) {
250 /* Reduce "noise" -- don't print if job canceled */
251 Jmsg(jcr, M_FATAL, 0, _("Could not ready device %s for append.\n"),
259 if (jcr->NumVolumes == 0) {
262 attach_jcr_to_device(dev, jcr); /* attach jcr to device */
263 rtn_dev = dev; /* return device */
266 * If we jump here, it is an error return because
267 * rtn_dev will still be NULL
273 V(mutex); /* unlock other threads */
278 * This job is done, so release the device. From a Unix standpoint,
279 * the device remains open.
282 int release_device(JCR *jcr, DEVICE *dev)
285 Dmsg1(100, "release_device device is %s\n", dev_is_tape(dev)?"tape":"disk");
286 if (dev_state(dev, ST_READ)) {
287 dev->state &= ~ST_READ; /* clear read bit */
288 if (!dev_is_tape(dev) || !dev_cap(dev, CAP_ALWAYSOPEN)) {
289 offline_or_rewind_dev(dev);
292 /******FIXME**** send read volume usage statistics to director */
294 } else if (dev->num_writers > 0) {
296 Dmsg1(100, "There are %d writers in release_device\n", dev->num_writers);
297 if (dev->num_writers == 0) {
298 /* If we are the only writer, write EOF after job */
299 if (dev_state(dev, ST_LABEL)) {
300 Dmsg0(100, "dir_create_jobmedia_record. Release\n");
301 if (!dir_create_jobmedia_record(jcr)) {
302 Jmsg(jcr, M_ERROR, 0, _("Could not create JobMedia record for Volume=\"%s\" Job=%s\n"),
303 jcr->VolCatInfo.VolCatName, jcr->Job);
305 if (dev_can_write(dev)) {
308 dev->VolCatInfo.VolCatJobs++; /* increment number of jobs */
309 /* Note! do volume update before close, which zaps VolCatInfo */
310 Dmsg0(100, "dir_update_vol_info. Release0\n");
311 dir_update_volume_info(jcr, dev, 0); /* send Volume info to Director */
314 if (!dev_is_tape(dev) || !dev_cap(dev, CAP_ALWAYSOPEN)) {
315 offline_or_rewind_dev(dev);
318 } else if (dev_state(dev, ST_LABEL)) {
319 Dmsg0(100, "dir_create_jobmedia_record. Release\n");
320 if (!dir_create_jobmedia_record(jcr)) {
321 Jmsg(jcr, M_ERROR, 0, _("Could not create JobMedia record for Volume=\"%s\" Job=%s\n"),
322 jcr->VolCatInfo.VolCatName, jcr->Job);
324 Dmsg0(100, "dir_update_vol_info. Release1\n");
325 dev->VolCatInfo.VolCatJobs++; /* increment number of jobs */
326 dir_update_volume_info(jcr, dev, 0); /* send Volume info to Director */
329 Jmsg2(jcr, M_ERROR, 0, _("BAD ERROR: release_device %s, Volume \"%s\" not in use.\n"),
330 dev_name(dev), NPRT(jcr->VolumeName));
332 detach_jcr_from_device(dev, jcr);
333 if (dev->prev && !dev_state(dev, ST_READ) && dev->num_writers == 0) {
336 dev->prev->next = dev->next; /* dechain */