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 bool tape_previously_mounted;
45 bool tape_initially_mounted;
50 if (device_is_unmounted(dev)) {
51 Jmsg(jcr, M_WARNING, 0, _("device %s is BLOCKED due to user unmount.\n"),
55 block_device(dev, BST_DOING_ACQUIRE);
58 if (dev_state(dev, ST_READ) || dev->num_writers > 0) {
59 Jmsg2(jcr, M_FATAL, 0, _("Device %s is busy. Job %d canceled.\n"),
60 dev_name(dev), jcr->JobId);
64 tape_previously_mounted = dev_state(dev, ST_READ) ||
65 dev_state(dev, ST_APPEND) ||
66 dev_state(dev, ST_LABEL);
67 tape_initially_mounted = tape_previously_mounted;
69 /* Find next Volume, if any */
72 Jmsg(jcr, M_FATAL, 0, _("No volumes specified. Job %d canceled.\n"), jcr->JobId);
76 for (i=1; i<jcr->CurVolume; i++) {
79 pm_strcpy(&jcr->VolumeName, vol->VolumeName);
82 if (job_canceled(jcr)) {
83 Mmsg1(&dev->errmsg, _("Job %d canceled.\n"), jcr->JobId);
84 goto get_out; /* error return */
87 * This code ensures that the device is ready for
88 * reading. If it is a file, it opens it.
89 * If it is a tape, it checks the volume name
91 for ( ; !(dev->state & ST_OPENED); ) {
92 Dmsg1(120, "bstored: open vol=%s\n", jcr->VolumeName);
93 if (open_dev(dev, jcr->VolumeName, READ_ONLY) < 0) {
94 Jmsg(jcr, M_FATAL, 0, _("Open device %s volume %s failed, ERR=%s\n"),
95 dev_name(dev), jcr->VolumeName, strerror_dev(dev));
98 Dmsg1(129, "open_dev %s OK\n", dev_name(dev));
100 /****FIXME***** do not reread label if ioctl() says we are
101 * correctly possitioned. Possibly have way user can turn
102 * this optimization (to be implemented) off.
104 dev->state &= ~ST_LABEL; /* force reread of label */
105 Dmsg0(200, "calling read-vol-label\n");
106 switch (read_dev_volume_label(jcr, dev, block)) {
112 * Send error message generated by read_dev_volume_label()
113 * only we really had a tape mounted. This supresses superfluous
114 * error messages when nothing is mounted.
116 if (tape_previously_mounted) {
117 Jmsg(jcr, M_WARNING, 0, "%s", jcr->errmsg);
121 if (tape_initially_mounted) {
122 tape_initially_mounted = false;
127 Jmsg(jcr, M_WARNING, 0, "%s", jcr->errmsg);
129 tape_previously_mounted = true;
130 Dmsg0(200, "dir_get_volume_info\n");
131 if (!dir_get_volume_info(jcr, GET_VOL_INFO_FOR_READ)) {
132 Jmsg1(jcr, M_WARNING, 0, "%s", jcr->errmsg);
134 /* Call autochanger only once unless ask_sysop called */
136 Dmsg2(200, "calling autoload Vol=%s Slot=%d\n",
137 jcr->VolumeName, jcr->VolCatInfo.Slot);
138 if ((autochanger=autoload_device(jcr, dev, 0, NULL))) {
142 /* Mount a specific volume and no other */
143 Dmsg0(200, "calling dir_ask_sysop\n");
144 if (!dir_ask_sysop_to_mount_volume(jcr, dev)) {
145 goto get_out; /* error return */
147 autochanger = 0; /* permit using autochanger again */
148 continue; /* try reading again */
153 Jmsg1(jcr, M_FATAL, 0, _("Too many errors trying to mount device \"%s\".\n"),
158 dev->state &= ~ST_APPEND; /* clear any previous append mode */
159 dev->state |= ST_READ; /* set reed mode */
160 attach_jcr_to_device(dev, jcr); /* attach jcr to device */
161 Jmsg(jcr, M_INFO, 0, _("Ready to read from volume \"%s\" on device %s.\n"),
162 jcr->VolumeName, dev_name(dev));
172 * Acquire device for writing. We permit multiple writers.
173 * If this is the first one, we read the label.
175 * Returns: NULL if failed for any reason
176 * dev if successful (may change if new dev opened)
177 * This routine must be single threaded because we may create
178 * multiple devices (for files), thus we have our own mutex
179 * on top of the device mutex.
181 DEVICE *acquire_device_for_append(JCR *jcr, DEVICE *dev, DEV_BLOCK *block)
184 bool recycle = false;
185 bool do_mount = false;
186 DEVICE *rtn_dev = NULL;
188 if (device_is_unmounted(dev)) {
189 Jmsg(jcr, M_WARNING, 0, _("device %s is BLOCKED due to user unmount.\n"),
193 block_device(dev, BST_DOING_ACQUIRE);
195 P(mutex); /* lock all devices */
196 Dmsg1(190, "acquire_append device is %s\n", dev_is_tape(dev)?"tape":"disk");
199 if (dev_state(dev, ST_APPEND)) {
201 * Device already in append mode
203 * Check if we have the right Volume mounted
204 * OK if current volume info OK
205 * OK if next volume matches current volume
206 * otherwise mount desired volume obtained from
207 * dir_find_next_appendable_volume
209 pm_strcpy(&jcr->VolumeName, dev->VolHdr.VolName);
210 if (!dir_get_volume_info(jcr, GET_VOL_INFO_FOR_WRITE) &&
211 !(dir_find_next_appendable_volume(jcr) &&
212 strcmp(dev->VolHdr.VolName, jcr->VolumeName) == 0)) { /* wrong tape mounted */
213 if (dev->num_writers != 0) {
214 DEVICE *d = ((DEVRES *)dev->device)->dev;
215 uint32_t open_vols = 0;
216 for ( ; d; d=d->next) {
219 if (dev_state(dev, ST_FILE) && dev->max_open_vols > open_vols) {
220 d = init_dev(NULL, (DEVRES *)dev->device); /* init new device */
221 d->prev = dev; /* chain in new device */
224 /* Release old device */
228 /* Make new device current device and lock it */
231 block_device(dev, BST_DOING_ACQUIRE);
234 Jmsg(jcr, M_FATAL, 0, _("Device %s is busy writing on another Volume.\n"), dev_name(dev));
238 /* Wrong tape mounted, release it, then fall through to get correct one */
243 * At this point, the correct tape is already mounted, so
244 * we do not need to do mount_next_write_volume(), unless
245 * we need to recycle the tape.
247 recycle = strcmp(jcr->VolCatInfo.VolCatStatus, "Recycle") == 0;
248 if (recycle && dev->num_writers != 0) {
249 Jmsg(jcr, M_FATAL, 0, _("Cannot recycle volume \"%s\""
250 " because it is in use by another job.\n"));
255 /* Not already in append mode, so mount the device */
256 if (dev_state(dev, ST_READ)) {
257 Jmsg(jcr, M_FATAL, 0, _("Device %s is busy reading.\n"), dev_name(dev));
260 ASSERT(dev->num_writers == 0);
264 if (do_mount || recycle) {
265 if (!mount_next_write_volume(jcr, dev, block, release)) {
266 if (!job_canceled(jcr)) {
267 /* Reduce "noise" -- don't print if job canceled */
268 Jmsg(jcr, M_FATAL, 0, _("Could not ready device \"%s\" for append.\n"),
276 if (jcr->NumVolumes == 0) {
279 attach_jcr_to_device(dev, jcr); /* attach jcr to device */
280 rtn_dev = dev; /* return device */
283 * If we jump here, it is an error return because
284 * rtn_dev will still be NULL
290 V(mutex); /* unlock other threads */
295 * This job is done, so release the device. From a Unix standpoint,
296 * the device remains open.
299 int release_device(JCR *jcr, DEVICE *dev)
302 Dmsg1(100, "release_device device is %s\n", dev_is_tape(dev)?"tape":"disk");
303 if (dev_state(dev, ST_READ)) {
304 dev->state &= ~ST_READ; /* clear read bit */
305 if (!dev_is_tape(dev) || !dev_cap(dev, CAP_ALWAYSOPEN)) {
306 offline_or_rewind_dev(dev);
309 /******FIXME**** send read volume usage statistics to director */
311 } else if (dev->num_writers > 0) {
313 Dmsg1(100, "There are %d writers in release_device\n", dev->num_writers);
314 if (dev_state(dev, ST_LABEL)) {
315 Dmsg0(100, "dir_create_jobmedia_record. Release\n");
316 if (!dir_create_jobmedia_record(jcr)) {
317 Jmsg(jcr, M_ERROR, 0, _("Could not create JobMedia record for Volume=\"%s\" Job=%s\n"),
318 jcr->VolCatInfo.VolCatName, jcr->Job);
320 /* If no more writers, write an EOF */
321 if (!dev->num_writers && dev_can_write(dev)) {
324 dev->VolCatInfo.VolCatFiles = dev->file; /* set number of files */
325 dev->VolCatInfo.VolCatJobs++; /* increment number of jobs */
326 /* Note! do volume update before close, which zaps VolCatInfo */
327 Dmsg0(100, "dir_update_vol_info. Release0\n");
328 dir_update_volume_info(jcr, dev, 0); /* send Volume info to Director */
331 if (!dev->num_writers && (!dev_is_tape(dev) || !dev_cap(dev, CAP_ALWAYSOPEN))) {
332 offline_or_rewind_dev(dev);
336 Jmsg2(jcr, M_ERROR, 0, _("BAD ERROR: release_device %s, Volume \"%s\" not in use.\n"),
337 dev_name(dev), NPRT(jcr->VolumeName));
339 detach_jcr_from_device(dev, jcr);
340 if (dev->prev && !dev_state(dev, ST_READ) && !dev->num_writers) {
343 dev->prev->next = dev->next; /* dechain */