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));
40 dcr->block = new_block(dev);
41 dcr->record = new_record();
46 void free_dcr(DCR *dcr)
49 free_block(dcr->block);
52 free_record(dcr->record);
59 /*********************************************************************
60 * Acquire device for reading. We permit (for the moment)
61 * only one reader. We read the Volume label from the block and
62 * leave the block pointers just after the label.
64 * Returns: 0 if failed for any reason
67 DCR *acquire_device_for_read(JCR *jcr)
70 bool tape_previously_mounted;
71 bool tape_initially_mounted;
78 /* Called for each volume */
80 dcr = new_dcr(jcr, jcr->device->dev);
83 if (device_is_unmounted(dev)) {
84 Jmsg(jcr, M_WARNING, 0, _("device %s is BLOCKED due to user unmount.\n"),
88 block_device(dev, BST_DOING_ACQUIRE);
91 if (dev_state(dev, ST_READ) || dev->num_writers > 0) {
92 Jmsg2(jcr, M_FATAL, 0, _("Device %s is busy. Job %d canceled.\n"),
93 dev_name(dev), jcr->JobId);
97 tape_previously_mounted = dev_state(dev, ST_READ) ||
98 dev_state(dev, ST_APPEND) ||
99 dev_state(dev, ST_LABEL);
100 tape_initially_mounted = tape_previously_mounted;
102 /* Find next Volume, if any */
105 Jmsg(jcr, M_FATAL, 0, _("No volumes specified. Job %d canceled.\n"), jcr->JobId);
109 for (i=1; i<jcr->CurVolume; i++) {
112 pm_strcpy(&jcr->VolumeName, vol->VolumeName);
113 bstrncpy(dcr->VolumeName, vol->VolumeName, sizeof(dcr->VolumeName));
115 for (i=0; i<5; i++) {
116 if (job_canceled(jcr)) {
117 Mmsg1(&dev->errmsg, _("Job %d canceled.\n"), jcr->JobId);
118 goto get_out; /* error return */
121 * This code ensures that the device is ready for
122 * reading. If it is a file, it opens it.
123 * If it is a tape, it checks the volume name
125 for ( ; !(dev->state & ST_OPENED); ) {
126 Dmsg1(120, "bstored: open vol=%s\n", jcr->VolumeName);
127 if (open_dev(dev, dcr->VolumeName, READ_ONLY) < 0) {
128 Jmsg(jcr, M_FATAL, 0, _("Open device %s volume %s failed, ERR=%s\n"),
129 dev_name(dev), dcr->VolumeName, strerror_dev(dev));
132 Dmsg1(129, "open_dev %s OK\n", dev_name(dev));
134 /****FIXME***** do not reread label if ioctl() says we are
135 * correctly possitioned. Possibly have way user can turn
136 * this optimization (to be implemented) off.
138 dcr->dev->state &= ~ST_LABEL; /* force reread of label */
139 Dmsg0(200, "calling read-vol-label\n");
140 switch (read_dev_volume_label(jcr, dev, dcr->block)) {
146 * Send error message generated by read_dev_volume_label()
147 * only we really had a tape mounted. This supresses superfluous
148 * error messages when nothing is mounted.
150 if (tape_previously_mounted) {
151 Jmsg(jcr, M_WARNING, 0, "%s", jcr->errmsg);
155 if (tape_initially_mounted) {
156 tape_initially_mounted = false;
161 Jmsg(jcr, M_WARNING, 0, "%s", jcr->errmsg);
163 tape_previously_mounted = true;
164 Dmsg0(200, "dir_get_volume_info\n");
165 if (!dir_get_volume_info(jcr, GET_VOL_INFO_FOR_READ)) {
166 Jmsg1(jcr, M_WARNING, 0, "%s", jcr->errmsg);
168 /* Call autochanger only once unless ask_sysop called */
170 Dmsg2(200, "calling autoload Vol=%s Slot=%d\n",
171 jcr->VolumeName, jcr->VolCatInfo.Slot);
172 if ((autochanger=autoload_device(jcr, dev, 0, NULL))) {
176 /* Mount a specific volume and no other */
177 Dmsg0(200, "calling dir_ask_sysop\n");
178 if (!dir_ask_sysop_to_mount_volume(jcr, dev)) {
179 goto get_out; /* error return */
181 autochanger = 0; /* permit using autochanger again */
182 continue; /* try reading again */
187 Jmsg1(jcr, M_FATAL, 0, _("Too many errors trying to mount device \"%s\".\n"),
192 dev->state &= ~ST_APPEND; /* clear any previous append mode */
193 dev->state |= ST_READ; /* set read mode */
194 attach_jcr_to_device(dev, jcr); /* attach jcr to device */
195 Jmsg(jcr, M_INFO, 0, _("Ready to read from volume \"%s\" on device %s.\n"),
196 jcr->VolumeName, dev_name(dev));
210 * Acquire device for writing. We permit multiple writers.
211 * If this is the first one, we read the label.
213 * Returns: NULL if failed for any reason
214 * dev if successful (may change if new dev opened)
215 * This routine must be single threaded because we may create
216 * multiple devices (for files), thus we have our own mutex
217 * on top of the device mutex.
219 DCR *acquire_device_for_append(JCR *jcr)
222 bool recycle = false;
223 bool do_mount = false;
225 DEVICE *dev = jcr->device->dev;
227 dcr = new_dcr(jcr, dev);
228 if (device_is_unmounted(dev)) {
229 Jmsg(jcr, M_WARNING, 0, _("device %s is BLOCKED due to user unmount.\n"),
233 block_device(dev, BST_DOING_ACQUIRE);
235 P(mutex); /* lock all devices */
236 Dmsg1(190, "acquire_append device is %s\n", dev_is_tape(dev)?"tape":"disk");
239 if (dev_state(dev, ST_APPEND)) {
241 * Device already in append mode
243 * Check if we have the right Volume mounted
244 * OK if current volume info OK
245 * OK if next volume matches current volume
246 * otherwise mount desired volume obtained from
247 * dir_find_next_appendable_volume
249 pm_strcpy(&jcr->VolumeName, dev->VolHdr.VolName);
250 bstrncpy(dcr->VolumeName, dev->VolHdr.VolName, sizeof(dcr->VolumeName));
251 if (!dir_get_volume_info(jcr, GET_VOL_INFO_FOR_WRITE) &&
252 !(dir_find_next_appendable_volume(jcr) &&
253 strcmp(dev->VolHdr.VolName, jcr->VolumeName) == 0)) { /* wrong tape mounted */
254 if (dev->num_writers != 0) {
255 DEVICE *d = ((DEVRES *)dev->device)->dev;
256 uint32_t open_vols = 0;
257 for ( ; d; d=d->next) {
260 if (dev_state(dev, ST_FILE) && dev->max_open_vols > open_vols) {
261 d = init_dev(NULL, (DEVRES *)dev->device); /* init new device */
262 d->prev = dev; /* chain in new device */
265 /* Release old device */
269 /* Make new device current device and lock it */
272 block_device(dev, BST_DOING_ACQUIRE);
275 Jmsg(jcr, M_FATAL, 0, _("Device %s is busy writing on another Volume.\n"), dev_name(dev));
279 /* Wrong tape mounted, release it, then fall through to get correct one */
284 * At this point, the correct tape is already mounted, so
285 * we do not need to do mount_next_write_volume(), unless
286 * we need to recycle the tape.
288 recycle = strcmp(jcr->VolCatInfo.VolCatStatus, "Recycle") == 0;
289 if (recycle && dev->num_writers != 0) {
290 Jmsg(jcr, M_FATAL, 0, _("Cannot recycle volume \"%s\""
291 " because it is in use by another job.\n"));
296 /* Not already in append mode, so mount the device */
297 if (dev_state(dev, ST_READ)) {
298 Jmsg(jcr, M_FATAL, 0, _("Device %s is busy reading.\n"), dev_name(dev));
301 ASSERT(dev->num_writers == 0);
305 if (do_mount || recycle) {
306 if (!mount_next_write_volume(jcr, dev, dcr->block, release)) {
307 if (!job_canceled(jcr)) {
308 /* Reduce "noise" -- don't print if job canceled */
309 Jmsg(jcr, M_FATAL, 0, _("Could not ready device \"%s\" for append.\n"),
317 if (jcr->NumVolumes == 0) {
320 attach_jcr_to_device(dev, jcr); /* attach jcr to device */
324 * If we jump here, it is an error return because
325 * rtn_dev will still be NULL
334 V(mutex); /* unlock other threads */
339 * This job is done, so release the device. From a Unix standpoint,
340 * the device remains open.
343 int release_device(JCR *jcr)
345 DEVICE *dev = jcr->dcr->dev;
347 Dmsg1(100, "release_device device is %s\n", dev_is_tape(dev)?"tape":"disk");
348 if (dev_state(dev, ST_READ)) {
349 dev->state &= ~ST_READ; /* clear read bit */
350 if (!dev_is_tape(dev) || !dev_cap(dev, CAP_ALWAYSOPEN)) {
351 offline_or_rewind_dev(dev);
354 /******FIXME**** send read volume usage statistics to director */
356 } else if (dev->num_writers > 0) {
358 Dmsg1(100, "There are %d writers in release_device\n", dev->num_writers);
359 if (dev_state(dev, ST_LABEL)) {
360 Dmsg0(100, "dir_create_jobmedia_record. Release\n");
361 if (!dir_create_jobmedia_record(jcr)) {
362 Jmsg(jcr, M_ERROR, 0, _("Could not create JobMedia record for Volume=\"%s\" Job=%s\n"),
363 jcr->VolCatInfo.VolCatName, jcr->Job);
365 /* If no more writers, write an EOF */
366 if (!dev->num_writers && dev_can_write(dev)) {
369 dev->VolCatInfo.VolCatFiles = dev->file; /* set number of files */
370 dev->VolCatInfo.VolCatJobs++; /* increment number of jobs */
371 /* Note! do volume update before close, which zaps VolCatInfo */
372 Dmsg0(100, "dir_update_vol_info. Release0\n");
373 dir_update_volume_info(jcr, dev, 0); /* send Volume info to Director */
376 if (!dev->num_writers && (!dev_is_tape(dev) || !dev_cap(dev, CAP_ALWAYSOPEN))) {
377 offline_or_rewind_dev(dev);
381 Jmsg2(jcr, M_ERROR, 0, _("BAD ERROR: release_device %s, Volume \"%s\" not in use.\n"),
382 dev_name(dev), NPRT(jcr->VolumeName));
384 detach_jcr_from_device(dev, jcr);
385 if (dev->prev && !dev_state(dev, ST_READ) && !dev->num_writers) {
388 dev->prev->next = dev->next; /* dechain */