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 & ST_READ) || (dev->state & ST_APPEND);
58 if (dev->state & ST_READ || dev->num_writers > 0) {
59 Jmsg1(jcr, M_FATAL, 0, _("Device %s is busy. Job canceled.\n"), dev_name(dev));
63 /* Find next Volume, if any */
66 Jmsg(jcr, M_FATAL, 0, _("No volumes specified. Job canceled.\n"));
70 for (i=1; i<jcr->CurVolume; i++) {
73 pm_strcpy(&jcr->VolumeName, vol->VolumeName);
76 if (job_canceled(jcr)) {
77 Mmsg0(&dev->errmsg, _("Job canceled.\n"));
78 goto get_out; /* error return */
81 * This code ensures that the device is ready for
82 * reading. If it is a file, it opens it.
83 * If it is a tape, it checks the volume name
85 for ( ; !(dev->state & ST_OPENED); ) {
86 Dmsg1(120, "bstored: open vol=%s\n", jcr->VolumeName);
87 if (open_dev(dev, jcr->VolumeName, READ_ONLY) < 0) {
88 Jmsg(jcr, M_FATAL, 0, _("Open device %s volume %s failed, ERR=%s\n"),
89 dev_name(dev), jcr->VolumeName, strerror_dev(dev));
92 Dmsg1(129, "open_dev %s OK\n", dev_name(dev));
94 dev->state &= ~ST_LABEL; /* force reread of label */
95 Dmsg0(200, "calling read-vol-label\n");
96 switch (read_dev_volume_label(jcr, dev, block)) {
102 * Send error message generated by read_dev_volume_label()
103 * only we really had a tape mounted. This supresses superfluous
104 * error messages when nothing is mounted.
106 if (tape_previously_mounted) {
107 Jmsg(jcr, M_WARNING, 0, "%s", jcr->errmsg);
111 Jmsg(jcr, M_WARNING, 0, "%s", jcr->errmsg);
113 tape_previously_mounted = 1;
114 Dmsg0(200, "dir_get_volume_info\n");
115 if (!dir_get_volume_info(jcr, GET_VOL_INFO_FOR_READ)) {
116 Jmsg1(jcr, M_WARNING, 0, "%s", jcr->errmsg);
118 /* Call autochanger only once unless ask_sysop called */
120 Dmsg2(200, "calling autoload Vol=%s Slot=%d\n",
121 jcr->VolumeName, jcr->VolCatInfo.Slot);
122 if ((autochanger=autoload_device(jcr, dev, 0, NULL))) {
126 /* Mount a specific volume and no other */
127 Dmsg0(200, "calling dir_ask_sysop\n");
128 if (!dir_ask_sysop_to_mount_volume(jcr, dev)) {
129 goto get_out; /* error return */
131 autochanger = 0; /* permit using autochanger again */
132 continue; /* try reading again */
137 Jmsg1(jcr, M_FATAL, 0, _("Too many errors trying to mount device \"%s\".\n"),
142 dev->state |= ST_READ;
143 attach_jcr_to_device(dev, jcr); /* attach jcr to device */
144 Jmsg(jcr, M_INFO, 0, _("Ready to read from volume \"%s\" on device %s.\n"),
145 jcr->VolumeName, dev_name(dev));
155 * Acquire device for writing. We permit multiple writers.
156 * If this is the first one, we read the label.
158 * Returns: NULL if failed for any reason
159 * dev if successful (may change if new dev opened)
160 * This routine must be single threaded because we may create
161 * multiple devices (for files), thus we have our own mutex
162 * on top of the device mutex.
164 DEVICE *acquire_device_for_append(JCR *jcr, DEVICE *dev, DEV_BLOCK *block)
167 bool recycle = false;
168 bool do_mount = false;
169 DEVICE *rtn_dev = NULL;
171 if (device_is_unmounted(dev)) {
172 Jmsg(jcr, M_WARNING, 0, _("device is BLOCKED due to user unmount.\n"));
175 block_device(dev, BST_DOING_ACQUIRE);
177 P(mutex); /* lock all devices */
178 Dmsg1(190, "acquire_append device is %s\n", dev_is_tape(dev)?"tape":"disk");
181 if (dev->state & ST_APPEND) {
183 * Device already in append mode
185 * Check if we have the right Volume mounted
186 * OK if current volume info OK
187 * OK if next volume matches current volume
188 * otherwise mount desired volume obtained from
189 * dir_find_next_appendable_volume
191 pm_strcpy(&jcr->VolumeName, dev->VolHdr.VolName);
192 if (!dir_get_volume_info(jcr, GET_VOL_INFO_FOR_WRITE) &&
193 !(dir_find_next_appendable_volume(jcr) &&
194 strcmp(dev->VolHdr.VolName, jcr->VolumeName) == 0)) { /* wrong tape mounted */
195 if (dev->num_writers != 0) {
196 DEVICE *d = ((DEVRES *)dev->device)->dev;
197 uint32_t open_vols = 0;
198 for ( ; d; d=d->next) {
201 if (dev->state & ST_FILE && dev->max_open_vols > open_vols) {
202 d = init_dev(NULL, (DEVRES *)dev->device); /* init new device */
203 d->prev = dev; /* chain in new device */
206 /* Release old device */
210 /* Make new device current device and lock it */
213 block_device(dev, BST_DOING_ACQUIRE);
216 Jmsg(jcr, M_FATAL, 0, _("Device %s is busy writing on another Volume.\n"), dev_name(dev));
220 /* Wrong tape mounted, release it, then fall through to get correct one */
225 * At this point, the correct tape is already mounted, so
226 * we do not need to do mount_next_write_volume(), unless
227 * we need to recycle the tape.
229 recycle = strcmp(jcr->VolCatInfo.VolCatStatus, "Recycle") == 0;
230 if (recycle && dev->num_writers != 0) {
231 Jmsg(jcr, M_FATAL, 0, _("Cannot recycle volume \"%s\""
232 " because it is in use by another job."));
237 /* Not already in append mode, so mount the device */
238 if (dev->state & ST_READ) {
239 Jmsg(jcr, M_FATAL, 0, _("Device %s is busy reading.\n"), dev_name(dev));
242 ASSERT(dev->num_writers == 0);
246 if (do_mount || recycle) {
247 if (!mount_next_write_volume(jcr, dev, block, release)) {
248 Jmsg(jcr, M_FATAL, 0, _("Could not ready device %s for append.\n"),
255 if (jcr->NumVolumes == 0) {
258 attach_jcr_to_device(dev, jcr); /* attach jcr to device */
259 rtn_dev = dev; /* return device */
262 * If we jump here, it is an error return because
263 * rtn_dev will still be NULL
269 V(mutex); /* unlock other threads */
274 * This job is done, so release the device. From a Unix standpoint,
275 * the device remains open.
278 int release_device(JCR *jcr, DEVICE *dev)
281 Dmsg1(100, "release_device device is %s\n", dev_is_tape(dev)?"tape":"disk");
282 if (dev->state & ST_READ) {
283 dev->state &= ~ST_READ; /* clear read bit */
284 if (!dev_is_tape(dev) || !dev_cap(dev, CAP_ALWAYSOPEN)) {
285 offline_or_rewind_dev(dev);
288 /******FIXME**** send read volume usage statistics to director */
290 } else if (dev->num_writers > 0) {
292 Dmsg1(100, "There are %d writers in release_device\n", dev->num_writers);
293 if (dev->num_writers == 0) {
294 /* If we are the only writer, write EOF after job */
295 if (dev->state & ST_LABEL) {
296 Dmsg0(100, "dir_create_jobmedia_record. Release\n");
297 dir_create_jobmedia_record(jcr);
298 if (dev_can_write(dev)) {
301 dev->VolCatInfo.VolCatFiles = dev->file; /* set number of files */
302 dev->VolCatInfo.VolCatJobs++; /* increment number of jobs */
303 /* Note! do volume update before close, which zaps VolCatInfo */
304 Dmsg0(200, "dir_update_vol_info. Release0\n");
305 dir_update_volume_info(jcr, &dev->VolCatInfo, 0); /* send Volume info to Director */
308 if (!dev_is_tape(dev) || !dev_cap(dev, CAP_ALWAYSOPEN)) {
309 offline_or_rewind_dev(dev);
312 } else if (dev->state & ST_LABEL) {
313 Dmsg0(100, "dir_create_jobmedia_record. Release\n");
314 dir_create_jobmedia_record(jcr);
315 Dmsg0(200, "dir_update_vol_info. Release1\n");
316 dev->VolCatInfo.VolCatFiles = dev->file; /* set number of files */
317 dev->VolCatInfo.VolCatJobs++; /* increment number of jobs */
318 dir_update_volume_info(jcr, &dev->VolCatInfo, 0); /* send Volume info to Director */
321 Jmsg2(jcr, M_ERROR, 0, _("BAD ERROR: release_device %s, Volume %s not in use.\n"),
322 dev_name(dev), NPRT(jcr->VolumeName));
324 detach_jcr_from_device(dev, jcr);
325 if (dev->prev && !(dev->state & ST_READ) && dev->num_writers == 0) {
328 dev->prev->next = dev->next; /* dechain */