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 DCR *new_dcr(JCR *jcr, DEVICE *dev)
35 DCR *dcr = (DCR *)malloc(sizeof(DCR));
36 memset(dcr, 0, sizeof(DCR));
42 dcr->block = new_block(dev);
43 dcr->record = new_record();
48 void free_dcr(DCR *dcr)
51 free_block(dcr->block);
54 free_record(dcr->record);
63 /*********************************************************************
64 * Acquire device for reading. We permit (for the moment)
65 * only one reader. We read the Volume label from the block and
66 * leave the block pointers just after the label.
68 * Returns: 0 if failed for any reason
71 DCR *acquire_device_for_read(JCR *jcr)
74 bool tape_previously_mounted;
75 bool tape_initially_mounted;
82 /* Called for each volume */
84 dcr = new_dcr(jcr, jcr->device->dev);
87 if (device_is_unmounted(dev)) {
88 Jmsg(jcr, M_WARNING, 0, _("device %s is BLOCKED due to user unmount.\n"),
92 block_device(dev, BST_DOING_ACQUIRE);
95 init_dev_wait_timers(dev);
96 if (dev_state(dev, ST_READ) || dev->num_writers > 0) {
97 Jmsg2(jcr, M_FATAL, 0, _("Device %s is busy. Job %d canceled.\n"),
98 dev_name(dev), jcr->JobId);
102 tape_previously_mounted = dev_state(dev, ST_READ) ||
103 dev_state(dev, ST_APPEND) ||
104 dev_state(dev, ST_LABEL);
105 tape_initially_mounted = tape_previously_mounted;
107 /* Find next Volume, if any */
110 Jmsg(jcr, M_FATAL, 0, _("No volumes specified. Job %d canceled.\n"), jcr->JobId);
114 for (i=1; i<jcr->CurVolume; i++) {
117 pm_strcpy(&jcr->VolumeName, vol->VolumeName);
118 bstrncpy(dcr->VolumeName, vol->VolumeName, sizeof(dcr->VolumeName));
120 for (i=0; i<5; i++) {
121 if (job_canceled(jcr)) {
122 Mmsg1(&dev->errmsg, _("Job %d canceled.\n"), jcr->JobId);
123 goto get_out; /* error return */
126 * This code ensures that the device is ready for
127 * reading. If it is a file, it opens it.
128 * If it is a tape, it checks the volume name
130 for ( ; !(dev->state & ST_OPENED); ) {
131 Dmsg1(120, "bstored: open vol=%s\n", jcr->VolumeName);
132 if (open_dev(dev, dcr->VolumeName, READ_ONLY) < 0) {
133 Jmsg(jcr, M_FATAL, 0, _("Open device %s volume %s failed, ERR=%s\n"),
134 dev_name(dev), dcr->VolumeName, strerror_dev(dev));
137 Dmsg1(129, "open_dev %s OK\n", dev_name(dev));
139 /****FIXME***** do not reread label if ioctl() says we are
140 * correctly possitioned. Possibly have way user can turn
141 * this optimization (to be implemented) off.
143 dcr->dev->state &= ~ST_LABEL; /* force reread of label */
144 Dmsg0(200, "calling read-vol-label\n");
145 switch (read_dev_volume_label(jcr, dev, dcr->block)) {
151 * Send error message generated by read_dev_volume_label()
152 * only we really had a tape mounted. This supresses superfluous
153 * error messages when nothing is mounted.
155 if (tape_previously_mounted) {
156 Jmsg(jcr, M_WARNING, 0, "%s", jcr->errmsg);
160 if (tape_initially_mounted) {
161 tape_initially_mounted = false;
166 Jmsg(jcr, M_WARNING, 0, "%s", jcr->errmsg);
168 tape_previously_mounted = true;
169 Dmsg0(200, "dir_get_volume_info\n");
170 if (!dir_get_volume_info(jcr, GET_VOL_INFO_FOR_READ)) {
171 Jmsg1(jcr, M_WARNING, 0, "%s", jcr->errmsg);
173 /* Call autochanger only once unless ask_sysop called */
175 Dmsg2(200, "calling autoload Vol=%s Slot=%d\n",
176 jcr->VolumeName, jcr->VolCatInfo.Slot);
177 if ((autochanger=autoload_device(jcr, dev, 0, NULL))) {
181 /* Mount a specific volume and no other */
182 Dmsg0(200, "calling dir_ask_sysop\n");
183 if (!dir_ask_sysop_to_mount_volume(jcr, dev)) {
184 goto get_out; /* error return */
186 autochanger = 0; /* permit using autochanger again */
187 continue; /* try reading again */
192 Jmsg1(jcr, M_FATAL, 0, _("Too many errors trying to mount device \"%s\".\n"),
197 dev->state &= ~ST_APPEND; /* clear any previous append mode */
198 dev->state |= ST_READ; /* set read mode */
199 attach_jcr_to_device(dev, jcr); /* attach jcr to device */
200 Jmsg(jcr, M_INFO, 0, _("Ready to read from volume \"%s\" on device %s.\n"),
201 jcr->VolumeName, dev_name(dev));
215 * Acquire device for writing. We permit multiple writers.
216 * If this is the first one, we read the label.
218 * Returns: NULL if failed for any reason
219 * dev if successful (may change if new dev opened)
220 * This routine must be single threaded because we may create
221 * multiple devices (for files), thus we have our own mutex
222 * on top of the device mutex.
224 DCR *acquire_device_for_append(JCR *jcr)
227 bool recycle = false;
228 bool do_mount = false;
230 DEVICE *dev = jcr->device->dev;
232 dcr = new_dcr(jcr, dev);
233 if (device_is_unmounted(dev)) {
234 Jmsg(jcr, M_WARNING, 0, _("device %s is BLOCKED due to user unmount.\n"),
238 block_device(dev, BST_DOING_ACQUIRE);
240 P(mutex); /* lock all devices */
241 Dmsg1(190, "acquire_append device is %s\n", dev_is_tape(dev)?"tape":"disk");
244 if (dev_state(dev, ST_APPEND)) {
246 * Device already in append mode
248 * Check if we have the right Volume mounted
249 * OK if current volume info OK
250 * OK if next volume matches current volume
251 * otherwise mount desired volume obtained from
252 * dir_find_next_appendable_volume
254 pm_strcpy(&jcr->VolumeName, dev->VolHdr.VolName);
255 bstrncpy(dcr->VolumeName, dev->VolHdr.VolName, sizeof(dcr->VolumeName));
256 if (!dir_get_volume_info(jcr, GET_VOL_INFO_FOR_WRITE) &&
257 !(dir_find_next_appendable_volume(jcr) &&
258 strcmp(dev->VolHdr.VolName, jcr->VolumeName) == 0)) { /* wrong tape mounted */
259 if (dev->num_writers != 0) {
260 DEVICE *d = ((DEVRES *)dev->device)->dev;
261 uint32_t open_vols = 0;
262 for ( ; d; d=d->next) {
265 if (dev_state(dev, ST_FILE) && dev->max_open_vols > open_vols) {
266 d = init_dev(NULL, (DEVRES *)dev->device); /* init new device */
267 d->prev = dev; /* chain in new device */
270 /* Release old device */
274 /* Make new device current device and lock it */
277 block_device(dev, BST_DOING_ACQUIRE);
280 Jmsg(jcr, M_FATAL, 0, _("Device %s is busy writing on another Volume.\n"), dev_name(dev));
284 /* Wrong tape mounted, release it, then fall through to get correct one */
289 * At this point, the correct tape is already mounted, so
290 * we do not need to do mount_next_write_volume(), unless
291 * we need to recycle the tape.
293 recycle = strcmp(jcr->VolCatInfo.VolCatStatus, "Recycle") == 0;
294 if (recycle && dev->num_writers != 0) {
295 Jmsg(jcr, M_FATAL, 0, _("Cannot recycle volume \"%s\""
296 " because it is in use by another job.\n"));
301 /* Not already in append mode, so mount the device */
302 if (dev_state(dev, ST_READ)) {
303 Jmsg(jcr, M_FATAL, 0, _("Device %s is busy reading.\n"), dev_name(dev));
306 ASSERT(dev->num_writers == 0);
310 if (do_mount || recycle) {
311 if (!mount_next_write_volume(jcr, dev, dcr->block, release)) {
312 if (!job_canceled(jcr)) {
313 /* Reduce "noise" -- don't print if job canceled */
314 Jmsg(jcr, M_FATAL, 0, _("Could not ready device \"%s\" for append.\n"),
322 if (jcr->NumVolumes == 0) {
325 attach_jcr_to_device(dev, jcr); /* attach jcr to device */
329 * If we jump here, it is an error return because
330 * rtn_dev will still be NULL
339 V(mutex); /* unlock other threads */
344 * This job is done, so release the device. From a Unix standpoint,
345 * the device remains open.
348 int release_device(JCR *jcr)
350 DEVICE *dev = jcr->dcr->dev;
352 Dmsg1(100, "release_device device is %s\n", dev_is_tape(dev)?"tape":"disk");
353 if (dev_state(dev, ST_READ)) {
354 dev->state &= ~ST_READ; /* clear read bit */
355 if (!dev_is_tape(dev) || !dev_cap(dev, CAP_ALWAYSOPEN)) {
356 offline_or_rewind_dev(dev);
359 /******FIXME**** send read volume usage statistics to director */
361 } else if (dev->num_writers > 0) {
363 Dmsg1(100, "There are %d writers in release_device\n", dev->num_writers);
364 if (dev_state(dev, ST_LABEL)) {
365 Dmsg0(100, "dir_create_jobmedia_record. Release\n");
366 if (!dir_create_jobmedia_record(jcr)) {
367 Jmsg(jcr, M_ERROR, 0, _("Could not create JobMedia record for Volume=\"%s\" Job=%s\n"),
368 jcr->VolCatInfo.VolCatName, jcr->Job);
370 /* If no more writers, write an EOF */
371 if (!dev->num_writers && dev_can_write(dev)) {
374 dev->VolCatInfo.VolCatFiles = dev->file; /* set number of files */
375 dev->VolCatInfo.VolCatJobs++; /* increment number of jobs */
376 /* Note! do volume update before close, which zaps VolCatInfo */
377 Dmsg0(100, "dir_update_vol_info. Release0\n");
378 dir_update_volume_info(jcr, dev, 0); /* send Volume info to Director */
381 if (!dev->num_writers && (!dev_is_tape(dev) || !dev_cap(dev, CAP_ALWAYSOPEN))) {
382 offline_or_rewind_dev(dev);
386 Jmsg2(jcr, M_ERROR, 0, _("BAD ERROR: release_device %s, Volume \"%s\" not in use.\n"),
387 dev_name(dev), NPRT(jcr->VolumeName));
389 detach_jcr_from_device(dev, jcr);
390 if (dev->prev && !dev_state(dev, ST_READ) && !dev->num_writers) {
393 dev->prev->next = dev->next; /* dechain */