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 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->record = new_record();
48 dcr->max_spool_size = dev->device->max_spool_size;
52 void free_dcr(DCR *dcr)
55 free_block(dcr->block);
58 free_record(dcr->record);
67 /*********************************************************************
68 * Acquire device for reading. We permit (for the moment)
69 * only one reader. We read the Volume label from the block and
70 * leave the block pointers just after the label.
72 * Returns: 0 if failed for any reason
75 DCR *acquire_device_for_read(JCR *jcr)
78 bool tape_previously_mounted;
79 bool tape_initially_mounted;
86 /* Called for each volume */
88 dcr = new_dcr(jcr, jcr->device->dev);
91 if (device_is_unmounted(dev)) {
92 Jmsg(jcr, M_WARNING, 0, _("device %s is BLOCKED due to user unmount.\n"),
96 block_device(dev, BST_DOING_ACQUIRE);
99 init_dev_wait_timers(dev);
100 if (dev_state(dev, ST_READ) || dev->num_writers > 0) {
101 Jmsg2(jcr, M_FATAL, 0, _("Device %s is busy. Job %d canceled.\n"),
102 dev_name(dev), jcr->JobId);
106 tape_previously_mounted = dev_state(dev, ST_READ) ||
107 dev_state(dev, ST_APPEND) ||
108 dev_state(dev, ST_LABEL);
109 tape_initially_mounted = tape_previously_mounted;
111 /* Find next Volume, if any */
114 Jmsg(jcr, M_FATAL, 0, _("No volumes specified. Job %d canceled.\n"), jcr->JobId);
118 for (i=1; i<jcr->CurVolume; i++) {
121 pm_strcpy(&jcr->VolumeName, vol->VolumeName);
122 bstrncpy(dcr->VolumeName, vol->VolumeName, sizeof(dcr->VolumeName));
124 for (i=0; i<5; i++) {
125 if (job_canceled(jcr)) {
126 Mmsg1(&dev->errmsg, _("Job %d canceled.\n"), jcr->JobId);
127 goto get_out; /* error return */
130 * This code ensures that the device is ready for
131 * reading. If it is a file, it opens it.
132 * If it is a tape, it checks the volume name
134 for ( ; !(dev->state & ST_OPENED); ) {
135 Dmsg1(120, "bstored: open vol=%s\n", jcr->VolumeName);
136 if (open_dev(dev, dcr->VolumeName, OPEN_READ_ONLY) < 0) {
137 Jmsg(jcr, M_FATAL, 0, _("Open device %s volume %s failed, ERR=%s\n"),
138 dev_name(dev), dcr->VolumeName, strerror_dev(dev));
141 Dmsg1(129, "open_dev %s OK\n", dev_name(dev));
143 /****FIXME***** do not reread label if ioctl() says we are
144 * correctly possitioned. Possibly have way user can turn
145 * this optimization (to be implemented) off.
147 dcr->dev->state &= ~ST_LABEL; /* force reread of label */
148 Dmsg0(200, "calling read-vol-label\n");
149 switch (read_dev_volume_label(jcr, dev, dcr->block)) {
155 * Send error message generated by read_dev_volume_label()
156 * only we really had a tape mounted. This supresses superfluous
157 * error messages when nothing is mounted.
159 if (tape_previously_mounted) {
160 Jmsg(jcr, M_WARNING, 0, "%s", jcr->errmsg);
164 if (tape_initially_mounted) {
165 tape_initially_mounted = false;
170 Jmsg(jcr, M_WARNING, 0, "%s", jcr->errmsg);
172 tape_previously_mounted = true;
173 Dmsg0(200, "dir_get_volume_info\n");
174 if (!dir_get_volume_info(jcr, GET_VOL_INFO_FOR_READ)) {
175 Jmsg1(jcr, M_WARNING, 0, "%s", jcr->errmsg);
177 /* Call autochanger only once unless ask_sysop called */
180 Dmsg2(200, "calling autoload Vol=%s Slot=%d\n",
181 jcr->VolumeName, jcr->VolCatInfo.Slot);
182 stat = autoload_device(jcr, dev, 0, NULL);
188 /* Mount a specific volume and no other */
189 Dmsg0(200, "calling dir_ask_sysop\n");
190 if (!dir_ask_sysop_to_mount_volume(jcr, dev)) {
191 goto get_out; /* error return */
193 autochanger = 0; /* permit using autochanger again */
194 continue; /* try reading again */
199 Jmsg1(jcr, M_FATAL, 0, _("Too many errors trying to mount device \"%s\".\n"),
204 dev->state &= ~ST_APPEND; /* clear any previous append mode */
205 dev->state |= ST_READ; /* set read mode */
206 attach_jcr_to_device(dev, jcr); /* attach jcr to device */
207 Jmsg(jcr, M_INFO, 0, _("Ready to read from volume \"%s\" on device %s.\n"),
208 jcr->VolumeName, dev_name(dev));
222 * Acquire device for writing. We permit multiple writers.
223 * If this is the first one, we read the label.
225 * Returns: NULL if failed for any reason
226 * dev if successful (may change if new dev opened)
227 * This routine must be single threaded because we may create
228 * multiple devices (for files), thus we have our own mutex
229 * on top of the device mutex.
231 DCR *acquire_device_for_append(JCR *jcr)
234 bool recycle = false;
235 bool do_mount = false;
237 DEVICE *dev = jcr->device->dev;
239 dcr = new_dcr(jcr, dev);
240 if (device_is_unmounted(dev)) {
241 Jmsg(jcr, M_WARNING, 0, _("device %s is BLOCKED due to user unmount.\n"),
245 block_device(dev, BST_DOING_ACQUIRE);
247 P(mutex); /* lock all devices */
248 Dmsg1(190, "acquire_append device is %s\n", dev_is_tape(dev)?"tape":"disk");
251 if (dev_state(dev, ST_APPEND)) {
253 * Device already in append mode
255 * Check if we have the right Volume mounted
256 * OK if current volume info OK
257 * OK if next volume matches current volume
258 * otherwise mount desired volume obtained from
259 * dir_find_next_appendable_volume
261 pm_strcpy(&jcr->VolumeName, dev->VolHdr.VolName);
262 bstrncpy(dcr->VolumeName, dev->VolHdr.VolName, sizeof(dcr->VolumeName));
263 if (!dir_get_volume_info(jcr, GET_VOL_INFO_FOR_WRITE) &&
264 !(dir_find_next_appendable_volume(jcr) &&
265 strcmp(dev->VolHdr.VolName, jcr->VolumeName) == 0)) { /* wrong tape mounted */
266 if (dev->num_writers != 0) {
267 DEVICE *d = ((DEVRES *)dev->device)->dev;
268 uint32_t open_vols = 0;
269 for ( ; d; d=d->next) {
272 if (dev_state(dev, ST_FILE) && dev->max_open_vols > open_vols) {
273 d = init_dev(NULL, (DEVRES *)dev->device); /* init new device */
274 d->prev = dev; /* chain in new device */
277 /* Release old device */
281 /* Make new device current device and lock it */
284 block_device(dev, BST_DOING_ACQUIRE);
287 Jmsg(jcr, M_FATAL, 0, _("Device %s is busy writing on another Volume.\n"), dev_name(dev));
291 /* Wrong tape mounted, release it, then fall through to get correct one */
296 * At this point, the correct tape is already mounted, so
297 * we do not need to do mount_next_write_volume(), unless
298 * we need to recycle the tape.
300 recycle = strcmp(jcr->VolCatInfo.VolCatStatus, "Recycle") == 0;
301 if (recycle && dev->num_writers != 0) {
302 Jmsg(jcr, M_FATAL, 0, _("Cannot recycle volume \"%s\""
303 " because it is in use by another job.\n"));
308 /* Not already in append mode, so mount the device */
309 if (dev_state(dev, ST_READ)) {
310 Jmsg(jcr, M_FATAL, 0, _("Device %s is busy reading.\n"), dev_name(dev));
313 ASSERT(dev->num_writers == 0);
317 if (do_mount || recycle) {
318 if (!mount_next_write_volume(jcr, dev, dcr->block, release)) {
319 if (!job_canceled(jcr)) {
320 /* Reduce "noise" -- don't print if job canceled */
321 Jmsg(jcr, M_FATAL, 0, _("Could not ready device \"%s\" for append.\n"),
329 if (jcr->NumVolumes == 0) {
332 attach_jcr_to_device(dev, jcr); /* attach jcr to device */
336 * If we jump here, it is an error return because
337 * rtn_dev will still be NULL
346 V(mutex); /* unlock other threads */
351 * This job is done, so release the device. From a Unix standpoint,
352 * the device remains open.
355 int release_device(JCR *jcr)
357 DEVICE *dev = jcr->dcr->dev;
359 Dmsg1(100, "release_device device is %s\n", dev_is_tape(dev)?"tape":"disk");
360 if (dev_state(dev, ST_READ)) {
361 dev->state &= ~ST_READ; /* clear read bit */
362 if (!dev_is_tape(dev) || !dev_cap(dev, CAP_ALWAYSOPEN)) {
363 offline_or_rewind_dev(dev);
366 /******FIXME**** send read volume usage statistics to director */
368 } else if (dev->num_writers > 0) {
370 Dmsg1(100, "There are %d writers in release_device\n", dev->num_writers);
371 if (dev_state(dev, ST_LABEL)) {
372 Dmsg0(100, "dir_create_jobmedia_record. Release\n");
373 if (!dir_create_jobmedia_record(jcr)) {
374 Jmsg(jcr, M_ERROR, 0, _("Could not create JobMedia record for Volume=\"%s\" Job=%s\n"),
375 jcr->VolCatInfo.VolCatName, jcr->Job);
377 /* If no more writers, write an EOF */
378 if (!dev->num_writers && dev_can_write(dev)) {
381 dev->VolCatInfo.VolCatFiles = dev->file; /* set number of files */
382 dev->VolCatInfo.VolCatJobs++; /* increment number of jobs */
383 /* Note! do volume update before close, which zaps VolCatInfo */
384 Dmsg0(100, "dir_update_vol_info. Release0\n");
385 dir_update_volume_info(jcr, dev, 0); /* send Volume info to Director */
388 if (!dev->num_writers && (!dev_is_tape(dev) || !dev_cap(dev, CAP_ALWAYSOPEN))) {
389 offline_or_rewind_dev(dev);
393 Jmsg2(jcr, M_ERROR, 0, _("BAD ERROR: release_device %s, Volume \"%s\" not in use.\n"),
394 dev_name(dev), NPRT(jcr->VolumeName));
396 detach_jcr_from_device(dev, jcr);
397 if (dev->prev && !dev_state(dev, ST_READ) && !dev->num_writers) {
400 dev->prev->next = dev->next; /* dechain */