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;
50 block_device(dev, BST_DOING_ACQUIRE);
53 tape_previously_mounted = (dev->state & ST_READ) || (dev->state & ST_APPEND);
55 if (dev->state & ST_READ || dev->num_writers > 0) {
56 Jmsg1(jcr, M_FATAL, 0, _("Device %s is busy. Job canceled.\n"), dev_name(dev));
60 /* Find next Volume, if any */
63 Jmsg(jcr, M_FATAL, 0, _("No volumes specified. Job canceled.\n"));
67 for (i=1; i<jcr->CurVolume; i++) {
70 pm_strcpy(&jcr->VolumeName, vol->VolumeName);
73 if (job_canceled(jcr)) {
74 Mmsg0(&dev->errmsg, _("Job canceled.\n"));
75 goto get_out; /* error return */
78 * This code ensures that the device is ready for
79 * reading. If it is a file, it opens it.
80 * If it is a tape, it checks the volume name
82 for ( ; !(dev->state & ST_OPENED); ) {
83 Dmsg1(120, "bstored: open vol=%s\n", jcr->VolumeName);
84 if (open_dev(dev, jcr->VolumeName, READ_ONLY) < 0) {
85 Jmsg(jcr, M_FATAL, 0, _("Open device %s volume %s failed, ERR=%s\n"),
86 dev_name(dev), jcr->VolumeName, strerror_dev(dev));
89 Dmsg1(129, "open_dev %s OK\n", dev_name(dev));
91 dev->state &= ~ST_LABEL; /* force reread of label */
92 Dmsg0(200, "calling read-vol-label\n");
93 switch (read_dev_volume_label(jcr, dev, block)) {
99 * Send error message generated by read_dev_volume_label()
100 * only we really had a tape mounted. This supresses superfluous
101 * error messages when nothing is mounted.
103 if (tape_previously_mounted) {
104 Jmsg(jcr, M_WARNING, 0, "%s", jcr->errmsg);
108 Jmsg(jcr, M_WARNING, 0, "%s", jcr->errmsg);
110 tape_previously_mounted = 1;
111 Dmsg0(200, "dir_get_volume_info\n");
112 if (!dir_get_volume_info(jcr, GET_VOL_INFO_FOR_READ)) {
113 Jmsg1(jcr, M_WARNING, 0, "%s", jcr->errmsg);
115 /* Call autochanger only once unless ask_sysop called */
117 Dmsg2(200, "calling autoload Vol=%s Slot=%d\n",
118 jcr->VolumeName, jcr->VolCatInfo.Slot);
119 if ((autochanger=autoload_device(jcr, dev, 0, NULL))) {
123 /* Mount a specific volume and no other */
124 Dmsg0(200, "calling dir_ask_sysop\n");
125 if (!dir_ask_sysop_to_mount_volume(jcr, dev)) {
126 goto get_out; /* error return */
128 autochanger = 0; /* permit using autochanger again */
129 continue; /* try reading again */
134 Jmsg1(jcr, M_FATAL, 0, _("Too many errors trying to mount device \"%s\".\n"),
139 dev->state |= ST_READ;
140 attach_jcr_to_device(dev, jcr); /* attach jcr to device */
141 Jmsg(jcr, M_INFO, 0, _("Ready to read from volume \"%s\" on device %s.\n"),
142 jcr->VolumeName, dev_name(dev));
152 * Acquire device for writing. We permit multiple writers.
153 * If this is the first one, we read the label.
155 * Returns: NULL if failed for any reason
156 * dev if successful (may change if new dev opened)
157 * This routine must be single threaded because we may create
158 * multiple devices (for files), thus we have our own mutex
159 * on top of the device mutex.
161 DEVICE * acquire_device_for_append(JCR *jcr, DEVICE *dev, DEV_BLOCK *block)
165 DEVICE *rtn_dev = NULL;
168 block_device(dev, BST_DOING_ACQUIRE);
170 P(mutex); /* lock all devices */
171 Dmsg1(190, "acquire_append device is %s\n", dev_is_tape(dev)?"tape":"disk");
174 if (dev->state & ST_APPEND) {
176 * Device already in append mode
178 * Check if we have the right Volume mounted
179 * OK if current volume info OK
180 * OK if next volume matches current volume
181 * otherwise mount desired volume obtained from
182 * dir_find_next_appendable_volume
184 pm_strcpy(&jcr->VolumeName, dev->VolHdr.VolName);
185 if (!dir_get_volume_info(jcr, GET_VOL_INFO_FOR_WRITE) &&
186 !(dir_find_next_appendable_volume(jcr) &&
187 strcmp(dev->VolHdr.VolName, jcr->VolumeName) == 0)) { /* wrong tape mounted */
188 if (dev->num_writers != 0) {
189 DEVICE *d = ((DEVRES *)dev->device)->dev;
190 uint32_t open_vols = 0;
191 for ( ; d; d=d->next) {
194 if (dev->state & ST_FILE && dev->max_open_vols > open_vols) {
195 d = init_dev(NULL, (DEVRES *)dev->device); /* init new device */
196 d->prev = dev; /* chain in new device */
199 /* Release old device */
203 /* Make new device current device and lock it */
206 block_device(dev, BST_DOING_ACQUIRE);
209 Jmsg(jcr, M_FATAL, 0, _("Device %s is busy writing on another Volume.\n"), dev_name(dev));
213 /* Wrong tape mounted, release it, then fall through to get correct one */
218 /* Not already in append mode, so mount the device */
219 if (dev->state & ST_READ) {
220 Jmsg(jcr, M_FATAL, 0, _("Device %s is busy reading.\n"), dev_name(dev));
223 ASSERT(dev->num_writers == 0);
228 if (!mount_next_write_volume(jcr, dev, block, release)) {
229 Jmsg(jcr, M_FATAL, 0, _("Could not ready device %s for append.\n"),
236 if (dev->num_writers > 1) {
237 Dmsg2(100, "Hey!!!! There are %d writers on device %s\n", dev->num_writers,
240 if (jcr->NumVolumes == 0) {
243 attach_jcr_to_device(dev, jcr); /* attach jcr to device */
244 rtn_dev = dev; /* return device */
250 V(mutex); /* unlock other threads */
255 * This job is done, so release the device. From a Unix standpoint,
256 * the device remains open.
259 int release_device(JCR *jcr, DEVICE *dev)
262 Dmsg1(100, "release_device device is %s\n", dev_is_tape(dev)?"tape":"disk");
263 if (dev->state & ST_READ) {
264 dev->state &= ~ST_READ; /* clear read bit */
265 if (!dev_is_tape(dev) || !dev_cap(dev, CAP_ALWAYSOPEN)) {
266 offline_or_rewind_dev(dev);
269 /******FIXME**** send read volume usage statistics to director */
271 } else if (dev->num_writers > 0) {
273 Dmsg1(100, "There are %d writers in release_device\n", dev->num_writers);
274 if (dev->num_writers == 0) {
275 /* If we are the only writer, write EOF after job */
276 if (dev->state & ST_LABEL) {
277 Dmsg0(100, "dir_create_jobmedia_record. Release\n");
278 dir_create_jobmedia_record(jcr);
279 if (dev_can_write(dev)) {
282 dev->VolCatInfo.VolCatFiles = dev->file; /* set number of files */
283 dev->VolCatInfo.VolCatJobs++; /* increment number of jobs */
284 /* Note! do volume update before close, which zaps VolCatInfo */
285 Dmsg0(200, "dir_update_vol_info. Release0\n");
286 dir_update_volume_info(jcr, &dev->VolCatInfo, 0); /* send Volume info to Director */
289 if (!dev_is_tape(dev) || !dev_cap(dev, CAP_ALWAYSOPEN)) {
290 offline_or_rewind_dev(dev);
293 } else if (dev->state & ST_LABEL) {
294 Dmsg0(100, "dir_create_jobmedia_record. Release\n");
295 dir_create_jobmedia_record(jcr);
296 Dmsg0(200, "dir_update_vol_info. Release1\n");
297 dev->VolCatInfo.VolCatFiles = dev->file; /* set number of files */
298 dev->VolCatInfo.VolCatJobs++; /* increment number of jobs */
299 dir_update_volume_info(jcr, &dev->VolCatInfo, 0); /* send Volume info to Director */
302 Jmsg2(jcr, M_ERROR, 0, _("BAD ERROR: release_device %s, Volume %s not in use.\n"),
303 dev_name(dev), NPRT(jcr->VolumeName));
305 detach_jcr_from_device(dev, jcr);
306 if (dev->prev && !(dev->state & ST_READ) && dev->num_writers == 0) {
309 dev->prev->next = dev->next; /* dechain */