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 int tape_previously_mounted;
48 block_device(dev, BST_DOING_ACQUIRE);
51 tape_previously_mounted = (dev->state & ST_READ) || (dev->state & ST_APPEND);
53 if (dev->state & ST_READ || dev->num_writers > 0) {
54 Jmsg1(jcr, M_FATAL, 0, _("Device %s is busy. Job cancelled.\n"), dev_name(dev));
58 /* Find next Volume, if any */
61 Jmsg(jcr, M_FATAL, 0, _("No volumes specified. Job cancelled.\n"));
65 for (int i=1; i<jcr->CurVolume; i++) {
68 pm_strcpy(&jcr->VolumeName, vol->VolumeName);
71 if (job_cancelled(jcr)) {
72 Mmsg0(&dev->errmsg, _("Job cancelled.\n"));
73 goto get_out; /* error return */
76 * This code ensures that the device is ready for
77 * reading. If it is a file, it opens it.
78 * If it is a tape, it checks the volume name
80 for ( ; !(dev->state & ST_OPENED); ) {
81 Dmsg1(120, "bstored: open vol=%s\n", jcr->VolumeName);
82 if (open_dev(dev, jcr->VolumeName, READ_ONLY) < 0) {
83 Jmsg(jcr, M_FATAL, 0, _("Open device %s volume %s failed, ERR=%s\n"),
84 dev_name(dev), jcr->VolumeName, strerror_dev(dev));
87 Dmsg1(129, "open_dev %s OK\n", dev_name(dev));
89 dev->state &= ~ST_LABEL; /* force reread of label */
90 Dmsg0(200, "calling read-vol-label\n");
91 switch (read_dev_volume_label(jcr, dev, block)) {
96 * Send error message generated by read_dev_volume_label()
97 * only we really had a tape mounted. This supresses superfluous
98 * error messages when nothing is mounted.
100 if (tape_previously_mounted) {
101 Jmsg1(jcr, M_WARNING, 0, "%s", jcr->errmsg);
105 Jmsg1(jcr, M_WARNING, 0, "%s", jcr->errmsg);
107 tape_previously_mounted = 0;
108 Dmsg0(200, "dir_get_volume_info\n");
109 if (!dir_get_volume_info(jcr, 0)) {
110 Jmsg1(jcr, M_WARNING, 0, "%s", jcr->errmsg);
112 Dmsg2(200, "calling autoload Vol=%s Slot=%d\n",
113 jcr->VolumeName, jcr->VolCatInfo.Slot);
114 if (autoload_device(jcr, dev, 0, NULL)) {
117 /* Mount a specific volume and no other */
118 Dmsg0(200, "calling dir_ask_sysop\n");
119 if (!dir_ask_sysop_to_mount_volume(jcr, dev)) {
120 goto get_out; /* error return */
122 continue; /* try reading again */
127 dev->state |= ST_READ;
128 attach_jcr_to_device(dev, jcr); /* attach jcr to device */
129 stat = 1; /* good return */
130 if ((dev->state & ST_TAPE) && vol->start_file > 0) {
131 Dmsg1(200, "====== Got start_file = %d\n", vol->start_file);
132 Jmsg(jcr, M_INFO, 0, _("Forward spacing to file %d.\n"), vol->start_file);
133 fsf_dev(dev, vol->start_file);
144 * Acquire device for writing. We permit multiple writers.
145 * If this is the first one, we read the label.
147 * Returns: NULL if failed for any reason
148 * dev if successful (may change if new dev opened)
149 * This routine must be single threaded because we may create
150 * multiple devices (for files), thus we have our own mutex
151 * on top of the device mutex.
153 DEVICE * acquire_device_for_append(JCR *jcr, DEVICE *dev, DEV_BLOCK *block)
157 DEVICE *rtn_dev = NULL;
160 block_device(dev, BST_DOING_ACQUIRE);
162 P(mutex); /* lock all devices */
163 Dmsg1(190, "acquire_append device is %s\n", dev_is_tape(dev)?"tape":"disk");
166 if (dev->state & ST_APPEND) {
168 * Device already in append mode
170 * Check if we have the right Volume mounted
171 * OK if current volume info OK
172 * OK if next volume matches current volume
173 * otherwise mount desired volume obtained from
174 * dir_find_next_appendable_volume
176 strcpy(jcr->VolumeName, dev->VolHdr.VolName);
177 if (!dir_get_volume_info(jcr, 1) &&
178 !(dir_find_next_appendable_volume(jcr) &&
179 strcmp(dev->VolHdr.VolName, jcr->VolumeName) == 0)) { /* wrong tape mounted */
180 if (dev->num_writers != 0) {
181 DEVICE *d = ((DEVRES *)dev->device)->dev;
182 uint32_t open_vols = 0;
183 for ( ; d; d=d->next) {
186 if (dev->state & ST_FILE && dev->max_open_vols > open_vols) {
187 d = init_dev(NULL, (DEVRES *)dev->device); /* init new device */
188 d->prev = dev; /* chain in new device */
191 /* Release old device */
195 /* Make new device current device and lock it */
198 block_device(dev, BST_DOING_ACQUIRE);
201 Jmsg(jcr, M_FATAL, 0, _("Device %s is busy writing on another Volume.\n"), dev_name(dev));
205 /* Wrong tape mounted, release it, then fall through to get correct one */
210 /* Not already in append mode, so mount the device */
211 if (dev->state & ST_READ) {
212 Jmsg(jcr, M_FATAL, 0, _("Device %s is busy reading.\n"), dev_name(dev));
215 ASSERT(dev->num_writers == 0);
220 if (!mount_next_write_volume(jcr, dev, block, release)) {
221 Jmsg(jcr, M_FATAL, 0, _("Could not ready device %s for append.\n"),
228 if (dev->num_writers > 1) {
229 Dmsg2(100, "Hey!!!! There are %d writers on device %s\n", dev->num_writers,
232 if (jcr->NumVolumes == 0) {
235 attach_jcr_to_device(dev, jcr); /* attach jcr to device */
236 rtn_dev = dev; /* return device */
242 V(mutex); /* unlock other threads */
247 * This job is done, so release the device. From a Unix standpoint,
248 * the device remains open.
251 int release_device(JCR *jcr, DEVICE *dev)
254 Dmsg1(100, "release_device device is %s\n", dev_is_tape(dev)?"tape":"disk");
255 if (dev->state & ST_READ) {
256 dev->state &= ~ST_READ; /* clear read bit */
257 if (!dev_is_tape(dev) || !dev_cap(dev, CAP_ALWAYSOPEN)) {
258 if (dev_cap(dev, CAP_OFFLINEUNMOUNT)) {
263 /******FIXME**** send read volume usage statistics to director */
265 } else if (dev->num_writers > 0) {
267 if (dev->state & ST_TAPE) {
268 jcr->EndBlock = dev->EndBlock;
269 jcr->EndFile = dev->EndFile;
270 Dmsg2(200, "Release device: EndFile=%u EndBlock=%u\n", jcr->EndFile, jcr->EndBlock);
272 jcr->EndBlock = (uint32_t)dev->file_addr;
273 jcr->EndFile = (uint32_t)(dev->file_addr >> 32);
275 Dmsg1(100, "There are %d writers in release_device\n", dev->num_writers);
276 if (dev->num_writers == 0) {
277 Dmsg0(100, "dir_create_jobmedia_record. Release\n");
278 dir_create_jobmedia_record(jcr);
280 dev->VolCatInfo.VolCatFiles = dev->file; /* set number of files */
281 dev->VolCatInfo.VolCatJobs++; /* increment number of jobs */
282 /* Note! do volume update before close, which zaps VolCatInfo */
283 Dmsg0(200, "dir_update_vol_info. Release0\n");
284 dir_update_volume_info(jcr, &dev->VolCatInfo, 0); /* send Volume info to Director */
286 if (!dev_is_tape(dev) || !dev_cap(dev, CAP_ALWAYSOPEN)) {
287 if (dev_cap(dev, CAP_OFFLINEUNMOUNT)) {
293 Dmsg0(100, "dir_create_jobmedia_record. Release\n");
294 dir_create_jobmedia_record(jcr);
295 Dmsg0(200, "dir_update_vol_info. Release1\n");
296 dev->VolCatInfo.VolCatFiles = dev->file; /* set number of files */
297 dev->VolCatInfo.VolCatJobs++; /* increment number of jobs */
298 dir_update_volume_info(jcr, &dev->VolCatInfo, 0); /* send Volume info to Director */
301 Jmsg2(jcr, M_ERROR, 0, _("BAD ERROR: release_device %s, Volume %s not in use.\n"),
302 dev_name(dev), NPRT(jcr->VolumeName));
304 detach_jcr_from_device(dev, jcr);
305 if (dev->prev && !(dev->state & ST_READ) && dev->num_writers == 0) {
308 dev->prev->next = dev->next; /* dechain */