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;
49 block_device(dev, BST_DOING_ACQUIRE);
52 tape_previously_mounted = (dev->state & ST_READ) || (dev->state & ST_APPEND);
54 if (dev->state & ST_READ || dev->num_writers > 0) {
55 Jmsg1(jcr, M_FATAL, 0, _("Device %s is busy. Job canceled.\n"), dev_name(dev));
59 /* Find next Volume, if any */
62 Jmsg(jcr, M_FATAL, 0, _("No volumes specified. Job canceled.\n"));
66 for (int i=1; i<jcr->CurVolume; i++) {
69 pm_strcpy(&jcr->VolumeName, vol->VolumeName);
71 for (int i=0; i<5; i++) {
72 if (job_canceled(jcr)) {
73 Mmsg0(&dev->errmsg, _("Job canceled.\n"));
74 goto get_out; /* error return */
77 * This code ensures that the device is ready for
78 * reading. If it is a file, it opens it.
79 * If it is a tape, it checks the volume name
81 for ( ; !(dev->state & ST_OPENED); ) {
82 Dmsg1(120, "bstored: open vol=%s\n", jcr->VolumeName);
83 if (open_dev(dev, jcr->VolumeName, READ_ONLY) < 0) {
84 Jmsg(jcr, M_FATAL, 0, _("Open device %s volume %s failed, ERR=%s\n"),
85 dev_name(dev), jcr->VolumeName, strerror_dev(dev));
88 Dmsg1(129, "open_dev %s OK\n", dev_name(dev));
90 dev->state &= ~ST_LABEL; /* force reread of label */
91 Dmsg0(200, "calling read-vol-label\n");
92 switch (read_dev_volume_label(jcr, dev, block)) {
98 * Send error message generated by read_dev_volume_label()
99 * only we really had a tape mounted. This supresses superfluous
100 * error messages when nothing is mounted.
102 if (tape_previously_mounted) {
103 Jmsg(jcr, M_WARNING, 0, "%s", jcr->errmsg);
107 Jmsg(jcr, M_WARNING, 0, "%s", jcr->errmsg);
109 tape_previously_mounted = 1;
110 Dmsg0(200, "dir_get_volume_info\n");
111 if (!dir_get_volume_info(jcr, 0)) {
112 Jmsg1(jcr, M_WARNING, 0, "%s", jcr->errmsg);
114 /* Call autochanger only once unless ask_sysop called */
116 Dmsg2(200, "calling autoload Vol=%s Slot=%d\n",
117 jcr->VolumeName, jcr->VolCatInfo.Slot);
118 if ((autochanger=autoload_device(jcr, dev, 0, NULL))) {
122 /* Mount a specific volume and no other */
123 Dmsg0(200, "calling dir_ask_sysop\n");
124 if (!dir_ask_sysop_to_mount_volume(jcr, dev)) {
125 goto get_out; /* error return */
127 autochanger = 0; /* permit using autochanger again */
128 continue; /* try reading again */
133 Jmsg1(jcr, M_FATAL, 0, _("Too many errors trying to mount device \"%s\".\n"),
138 dev->state |= ST_READ;
139 attach_jcr_to_device(dev, jcr); /* attach jcr to device */
140 if ((dev->state & ST_TAPE) && vol->start_file > 0) {
141 Dmsg1(200, "====== Got start_file = %d\n", vol->start_file);
142 Jmsg(jcr, M_INFO, 0, _("Forward spacing to file %d.\n"), vol->start_file);
143 fsf_dev(dev, vol->start_file);
154 * Acquire device for writing. We permit multiple writers.
155 * If this is the first one, we read the label.
157 * Returns: NULL if failed for any reason
158 * dev if successful (may change if new dev opened)
159 * This routine must be single threaded because we may create
160 * multiple devices (for files), thus we have our own mutex
161 * on top of the device mutex.
163 DEVICE * acquire_device_for_append(JCR *jcr, DEVICE *dev, DEV_BLOCK *block)
167 DEVICE *rtn_dev = NULL;
170 block_device(dev, BST_DOING_ACQUIRE);
172 P(mutex); /* lock all devices */
173 Dmsg1(190, "acquire_append device is %s\n", dev_is_tape(dev)?"tape":"disk");
176 if (dev->state & ST_APPEND) {
178 * Device already in append mode
180 * Check if we have the right Volume mounted
181 * OK if current volume info OK
182 * OK if next volume matches current volume
183 * otherwise mount desired volume obtained from
184 * dir_find_next_appendable_volume
186 strcpy(jcr->VolumeName, dev->VolHdr.VolName);
187 if (!dir_get_volume_info(jcr, 1) &&
188 !(dir_find_next_appendable_volume(jcr) &&
189 strcmp(dev->VolHdr.VolName, jcr->VolumeName) == 0)) { /* wrong tape mounted */
190 if (dev->num_writers != 0) {
191 DEVICE *d = ((DEVRES *)dev->device)->dev;
192 uint32_t open_vols = 0;
193 for ( ; d; d=d->next) {
196 if (dev->state & ST_FILE && dev->max_open_vols > open_vols) {
197 d = init_dev(NULL, (DEVRES *)dev->device); /* init new device */
198 d->prev = dev; /* chain in new device */
201 /* Release old device */
205 /* Make new device current device and lock it */
208 block_device(dev, BST_DOING_ACQUIRE);
211 Jmsg(jcr, M_FATAL, 0, _("Device %s is busy writing on another Volume.\n"), dev_name(dev));
215 /* Wrong tape mounted, release it, then fall through to get correct one */
220 /* Not already in append mode, so mount the device */
221 if (dev->state & ST_READ) {
222 Jmsg(jcr, M_FATAL, 0, _("Device %s is busy reading.\n"), dev_name(dev));
225 ASSERT(dev->num_writers == 0);
230 if (!mount_next_write_volume(jcr, dev, block, release)) {
231 Jmsg(jcr, M_FATAL, 0, _("Could not ready device %s for append.\n"),
238 if (dev->num_writers > 1) {
239 Dmsg2(100, "Hey!!!! There are %d writers on device %s\n", dev->num_writers,
242 if (jcr->NumVolumes == 0) {
245 attach_jcr_to_device(dev, jcr); /* attach jcr to device */
246 rtn_dev = dev; /* return device */
252 V(mutex); /* unlock other threads */
257 * This job is done, so release the device. From a Unix standpoint,
258 * the device remains open.
261 int release_device(JCR *jcr, DEVICE *dev)
264 Dmsg1(100, "release_device device is %s\n", dev_is_tape(dev)?"tape":"disk");
265 if (dev->state & ST_READ) {
266 dev->state &= ~ST_READ; /* clear read bit */
267 if (!dev_is_tape(dev) || !dev_cap(dev, CAP_ALWAYSOPEN)) {
268 if (dev_cap(dev, CAP_OFFLINEUNMOUNT)) {
273 /******FIXME**** send read volume usage statistics to director */
275 } else if (dev->num_writers > 0) {
277 if (dev->state & ST_TAPE) {
278 jcr->EndBlock = dev->EndBlock;
279 jcr->EndFile = dev->EndFile;
280 Dmsg2(200, "Release device: EndFile=%u EndBlock=%u\n", jcr->EndFile, jcr->EndBlock);
282 jcr->EndBlock = (uint32_t)dev->file_addr;
283 jcr->EndFile = (uint32_t)(dev->file_addr >> 32);
285 Dmsg1(100, "There are %d writers in release_device\n", dev->num_writers);
286 if (dev->num_writers == 0) {
287 Dmsg0(100, "dir_create_jobmedia_record. Release\n");
288 dir_create_jobmedia_record(jcr);
289 if (dev_can_write(dev)) {
292 dev->VolCatInfo.VolCatFiles = dev->file; /* set number of files */
293 dev->VolCatInfo.VolCatJobs++; /* increment number of jobs */
294 /* Note! do volume update before close, which zaps VolCatInfo */
295 Dmsg0(200, "dir_update_vol_info. Release0\n");
296 dir_update_volume_info(jcr, &dev->VolCatInfo, 0); /* send Volume info to Director */
298 if (!dev_is_tape(dev) || !dev_cap(dev, CAP_ALWAYSOPEN)) {
299 if (dev_cap(dev, CAP_OFFLINEUNMOUNT)) {
305 Dmsg0(100, "dir_create_jobmedia_record. Release\n");
306 dir_create_jobmedia_record(jcr);
307 Dmsg0(200, "dir_update_vol_info. Release1\n");
308 dev->VolCatInfo.VolCatFiles = dev->file; /* set number of files */
309 dev->VolCatInfo.VolCatJobs++; /* increment number of jobs */
310 dir_update_volume_info(jcr, &dev->VolCatInfo, 0); /* send Volume info to Director */
313 Jmsg2(jcr, M_ERROR, 0, _("BAD ERROR: release_device %s, Volume %s not in use.\n"),
314 dev_name(dev), NPRT(jcr->VolumeName));
316 detach_jcr_from_device(dev, jcr);
317 if (dev->prev && !(dev->state & ST_READ) && dev->num_writers == 0) {
320 dev->prev->next = dev->next; /* dechain */