2 * Routines to acquire and release a device for read/write
4 * Kern Sibbald, August MMII
9 Copyright (C) 2002-2004 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 if (jcr && jcr->dcr) {
38 DCR *dcr = (DCR *)malloc(sizeof(DCR));
39 memset(dcr, 0, sizeof(DCR));
45 dcr->block = new_block(dev);
46 dcr->rec = new_record();
48 dcr->max_spool_size = dev->device->max_spool_size;
49 // dev->attached_dcrs->append(dcr);
53 void free_dcr(DCR *dcr)
55 // dcr->dev->attached_dcrs->remove(dcr);
57 free_block(dcr->block);
60 free_record(dcr->rec);
69 /*********************************************************************
70 * Acquire device for reading. We permit (for the moment)
71 * only one reader. We read the Volume label from the block and
72 * leave the block pointers just after the label.
74 * Returns: 0 if failed for any reason
77 DCR *acquire_device_for_read(JCR *jcr)
80 bool tape_previously_mounted;
81 bool tape_initially_mounted;
83 bool try_autochanger = true;
88 /* Called for each volume */
90 dcr = new_dcr(jcr, jcr->device->dev);
93 if (device_is_unmounted(dev)) {
94 Jmsg(jcr, M_WARNING, 0, _("device %s is BLOCKED due to user unmount.\n"),
98 block_device(dev, BST_DOING_ACQUIRE);
101 init_dev_wait_timers(dev);
102 if (dev_state(dev, ST_READ) || dev->num_writers > 0) {
103 Jmsg2(jcr, M_FATAL, 0, _("Device %s is busy. Job %d canceled.\n"),
104 dev_name(dev), jcr->JobId);
108 tape_previously_mounted = dev_state(dev, ST_READ) ||
109 dev_state(dev, ST_APPEND) ||
110 dev_state(dev, ST_LABEL);
111 tape_initially_mounted = tape_previously_mounted;
113 /* Find next Volume, if any */
116 Jmsg(jcr, M_FATAL, 0, _("No volumes specified. Job %d canceled.\n"), jcr->JobId);
120 for (i=1; i<jcr->CurVolume; i++) {
123 pm_strcpy(&jcr->VolumeName, vol->VolumeName);
124 bstrncpy(dcr->VolumeName, vol->VolumeName, sizeof(dcr->VolumeName));
126 for (i=0; i<5; i++) {
127 if (job_canceled(jcr)) {
128 Mmsg1(&dev->errmsg, _("Job %d canceled.\n"), jcr->JobId);
129 goto get_out; /* error return */
132 * This code ensures that the device is ready for
133 * reading. If it is a file, it opens it.
134 * If it is a tape, it checks the volume name
136 for ( ; !(dev->state & ST_OPENED); ) {
137 Dmsg1(120, "bstored: open vol=%s\n", jcr->VolumeName);
138 if (open_dev(dev, dcr->VolumeName, OPEN_READ_ONLY) < 0) {
139 Jmsg(jcr, M_FATAL, 0, _("Open device %s volume %s failed, ERR=%s\n"),
140 dev_name(dev), dcr->VolumeName, strerror_dev(dev));
143 Dmsg1(129, "open_dev %s OK\n", dev_name(dev));
145 /****FIXME***** do not reread label if ioctl() says we are
146 * correctly possitioned. Possibly have way user can turn
147 * this optimization (to be implemented) off.
149 dcr->dev->state &= ~ST_LABEL; /* force reread of label */
150 Dmsg0(200, "calling read-vol-label\n");
151 switch (read_dev_volume_label(dcr, dcr->block)) {
157 * Send error message generated by read_dev_volume_label()
158 * only we really had a tape mounted. This supresses superfluous
159 * error messages when nothing is mounted.
161 if (tape_previously_mounted) {
162 Jmsg(jcr, M_WARNING, 0, "%s", jcr->errmsg);
166 if (tape_initially_mounted) {
167 tape_initially_mounted = false;
172 Jmsg(jcr, M_WARNING, 0, "%s", jcr->errmsg);
174 tape_previously_mounted = true;
175 Dmsg0(200, "dir_get_volume_info\n");
176 if (!dir_get_volume_info(dcr, GET_VOL_INFO_FOR_READ)) {
177 Jmsg1(jcr, M_WARNING, 0, "%s", jcr->errmsg);
179 /* Call autochanger only once unless ask_sysop called */
180 if (try_autochanger) {
182 Dmsg2(200, "calling autoload Vol=%s Slot=%d\n",
183 jcr->VolumeName, jcr->VolCatInfo.Slot);
184 stat = autoload_device(dcr, 0, NULL);
186 try_autochanger = false;
190 /* Mount a specific volume and no other */
191 Dmsg0(200, "calling dir_ask_sysop\n");
192 if (!dir_ask_sysop_to_mount_volume(dcr)) {
193 goto get_out; /* error return */
195 try_autochanger = true; /* permit using autochanger again */
196 continue; /* try reading again */
201 Jmsg1(jcr, M_FATAL, 0, _("Too many errors trying to mount device \"%s\".\n"),
206 dev->state &= ~ST_APPEND; /* clear any previous append mode */
207 dev->state |= ST_READ; /* set read mode */
208 attach_jcr_to_device(dev, jcr); /* attach jcr to device */
209 set_jcr_job_status(jcr, JS_Running);
210 dir_send_job_status(jcr);
211 Jmsg(jcr, M_INFO, 0, _("Ready to read from volume \"%s\" on device %s.\n"),
212 jcr->VolumeName, dev_name(dev));
226 * Acquire device for writing. We permit multiple writers.
227 * If this is the first one, we read the label.
229 * Returns: NULL if failed for any reason
230 * dev if successful (may change if new dev opened)
231 * This routine must be single threaded because we may create
232 * multiple devices (for files), thus we have our own mutex
233 * on top of the device mutex.
235 DCR *acquire_device_for_append(JCR *jcr)
237 bool release = false;
238 bool recycle = false;
239 bool do_mount = false;
241 DEVICE *dev = jcr->device->dev;
243 dcr = new_dcr(jcr, dev);
244 if (device_is_unmounted(dev)) {
245 Jmsg(jcr, M_WARNING, 0, _("device %s is BLOCKED due to user unmount.\n"),
249 block_device(dev, BST_DOING_ACQUIRE);
251 P(mutex); /* lock all devices */
252 Dmsg1(190, "acquire_append device is %s\n", dev_is_tape(dev)?"tape":"disk");
255 if (dev_state(dev, ST_APPEND)) {
257 * Device already in append mode
259 * Check if we have the right Volume mounted
260 * OK if current volume info OK
261 * OK if next volume matches current volume
262 * otherwise mount desired volume obtained from
263 * dir_find_next_appendable_volume
265 pm_strcpy(&jcr->VolumeName, dev->VolHdr.VolName);
266 bstrncpy(dcr->VolumeName, dev->VolHdr.VolName, sizeof(dcr->VolumeName));
267 if (!dir_get_volume_info(dcr, GET_VOL_INFO_FOR_WRITE) &&
268 !(dir_find_next_appendable_volume(dcr) &&
269 strcmp(dev->VolHdr.VolName, jcr->VolumeName) == 0)) { /* wrong tape mounted */
270 if (dev->num_writers != 0) {
271 DEVICE *d = ((DEVRES *)dev->device)->dev;
272 uint32_t open_vols = 0;
273 for ( ; d; d=d->next) {
276 if (dev_state(dev, ST_FILE) && dev->max_open_vols > open_vols) {
277 d = init_dev(NULL, (DEVRES *)dev->device); /* init new device */
278 d->prev = dev; /* chain in new device */
281 /* Release old device */
285 free_dcr(dcr); /* release dcr pointing to old dev */
286 /* Make new device current device and lock it */
288 dcr = new_dcr(jcr, dev); /* get new dcr for new device */
290 block_device(dev, BST_DOING_ACQUIRE);
293 Jmsg(jcr, M_FATAL, 0, _("Device %s is busy writing on another Volume.\n"), dev_name(dev));
297 /* Wrong tape mounted, release it, then fall through to get correct one */
302 * At this point, the correct tape is already mounted, so
303 * we do not need to do mount_next_write_volume(), unless
304 * we need to recycle the tape.
306 recycle = strcmp(jcr->VolCatInfo.VolCatStatus, "Recycle") == 0;
307 if (recycle && dev->num_writers != 0) {
308 Jmsg(jcr, M_FATAL, 0, _("Cannot recycle volume \"%s\""
309 " because it is in use by another job.\n"));
314 /* Not already in append mode, so mount the device */
315 if (dev_state(dev, ST_READ)) {
316 Jmsg(jcr, M_FATAL, 0, _("Device %s is busy reading.\n"), dev_name(dev));
319 ASSERT(dev->num_writers == 0);
323 if (do_mount || recycle) {
324 if (!mount_next_write_volume(dcr, dcr->block, release)) {
325 if (!job_canceled(jcr)) {
326 /* Reduce "noise" -- don't print if job canceled */
327 Jmsg(jcr, M_FATAL, 0, _("Could not ready device \"%s\" for append.\n"),
335 if (jcr->NumVolumes == 0) {
338 attach_jcr_to_device(dev, jcr); /* attach jcr to device */
339 set_jcr_job_status(jcr, JS_Running);
340 dir_send_job_status(jcr);
344 * If we jump here, it is an error return because
345 * rtn_dev will still be NULL
354 V(mutex); /* unlock other threads */
359 * This job is done, so release the device. From a Unix standpoint,
360 * the device remains open.
363 bool release_device(JCR *jcr)
366 DEVICE *dev = dcr->dev;
368 Dmsg1(100, "release_device device is %s\n", dev_is_tape(dev)?"tape":"disk");
369 if (dev_state(dev, ST_READ)) {
370 dev->state &= ~ST_READ; /* clear read bit */
371 if (!dev_is_tape(dev) || !dev_cap(dev, CAP_ALWAYSOPEN)) {
372 offline_or_rewind_dev(dev);
375 /******FIXME**** send read volume usage statistics to director */
377 } else if (dev->num_writers > 0) {
379 Dmsg1(100, "There are %d writers in release_device\n", dev->num_writers);
380 if (dev_state(dev, ST_LABEL)) {
381 Dmsg0(100, "dir_create_jobmedia_record. Release\n");
382 if (!dir_create_jobmedia_record(dcr)) {
383 Jmsg(jcr, M_ERROR, 0, _("Could not create JobMedia record for Volume=\"%s\" Job=%s\n"),
384 jcr->VolCatInfo.VolCatName, jcr->Job);
386 /* If no more writers, write an EOF */
387 if (!dev->num_writers && dev_can_write(dev)) {
390 dev->VolCatInfo.VolCatFiles = dev->file; /* set number of files */
391 dev->VolCatInfo.VolCatJobs++; /* increment number of jobs */
392 /* Note! do volume update before close, which zaps VolCatInfo */
393 Dmsg0(100, "dir_update_vol_info. Release0\n");
394 dir_update_volume_info(dcr, false); /* send Volume info to Director */
397 if (!dev->num_writers && (!dev_is_tape(dev) || !dev_cap(dev, CAP_ALWAYSOPEN))) {
398 offline_or_rewind_dev(dev);
402 Jmsg2(jcr, M_ERROR, 0, _("BAD ERROR: release_device %s, Volume \"%s\" not in use.\n"),
403 dev_name(dev), NPRT(jcr->VolumeName));
405 detach_jcr_from_device(dev, jcr);
406 if (dev->prev && !dev_state(dev, ST_READ) && !dev->num_writers) {
409 dev->prev->next = dev->next; /* dechain */