2 * Routines to acquire and release a device for read/write
4 * Kern Sibbald, August MMII
9 Copyright (C) 2002-2004 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->rec = new_record();
48 dcr->max_spool_size = dev->device->max_spool_size;
49 /* Attach this dcr only if dev is initialized */
50 if (dev->fd != 0 && jcr && jcr->JobType != JT_SYSTEM) {
51 dev->attached_dcrs->append(dcr);
56 void free_dcr(DCR *dcr)
59 DEVICE *dev = dcr->dev;
61 /* Detach this dcr only if the dev is initialized */
62 if (dev->fd != 0 && jcr && jcr->JobType != JT_SYSTEM) {
63 dcr->dev->attached_dcrs->remove(dcr);
66 free_block(dcr->block);
69 free_record(dcr->rec);
78 /*********************************************************************
79 * Acquire device for reading. We permit (for the moment)
80 * only one reader. We read the Volume label from the block and
81 * leave the block pointers just after the label.
83 * Returns: 0 if failed for any reason
86 DCR *acquire_device_for_read(JCR *jcr)
89 bool tape_previously_mounted;
90 bool tape_initially_mounted;
92 bool try_autochanger = true;
97 /* Called for each volume */
99 dcr = new_dcr(jcr, jcr->device->dev);
102 if (device_is_unmounted(dev)) {
103 Jmsg(jcr, M_WARNING, 0, _("device %s is BLOCKED due to user unmount.\n"),
107 block_device(dev, BST_DOING_ACQUIRE);
110 init_dev_wait_timers(dev);
111 if (dev_state(dev, ST_READ) || dev->num_writers > 0) {
112 Jmsg2(jcr, M_FATAL, 0, _("Device %s is busy. Job %d canceled.\n"),
113 dev_name(dev), jcr->JobId);
117 tape_previously_mounted = dev_state(dev, ST_READ) ||
118 dev_state(dev, ST_APPEND) ||
119 dev_state(dev, ST_LABEL);
120 tape_initially_mounted = tape_previously_mounted;
122 /* Find next Volume, if any */
125 Jmsg(jcr, M_FATAL, 0, _("No volumes specified. Job %d canceled.\n"), jcr->JobId);
129 for (i=1; i<jcr->CurVolume; i++) {
132 pm_strcpy(&jcr->VolumeName, vol->VolumeName);
133 bstrncpy(dcr->VolumeName, vol->VolumeName, sizeof(dcr->VolumeName));
135 for (i=0; i<5; i++) {
136 if (job_canceled(jcr)) {
137 Mmsg1(&dev->errmsg, _("Job %d canceled.\n"), jcr->JobId);
138 goto get_out; /* error return */
141 * This code ensures that the device is ready for
142 * reading. If it is a file, it opens it.
143 * If it is a tape, it checks the volume name
145 for ( ; !(dev->state & ST_OPENED); ) {
146 Dmsg1(120, "bstored: open vol=%s\n", jcr->VolumeName);
147 if (open_dev(dev, dcr->VolumeName, OPEN_READ_ONLY) < 0) {
148 Jmsg(jcr, M_FATAL, 0, _("Open device %s volume %s failed, ERR=%s\n"),
149 dev_name(dev), dcr->VolumeName, strerror_dev(dev));
152 Dmsg1(129, "open_dev %s OK\n", dev_name(dev));
154 /****FIXME***** do not reread label if ioctl() says we are
155 * correctly possitioned. Possibly have way user can turn
156 * this optimization (to be implemented) off.
158 dcr->dev->state &= ~ST_LABEL; /* force reread of label */
159 Dmsg0(200, "calling read-vol-label\n");
160 switch (read_dev_volume_label(dcr, dcr->block)) {
166 * Send error message generated by read_dev_volume_label()
167 * only we really had a tape mounted. This supresses superfluous
168 * error messages when nothing is mounted.
170 if (tape_previously_mounted) {
171 Jmsg(jcr, M_WARNING, 0, "%s", jcr->errmsg);
175 if (tape_initially_mounted) {
176 tape_initially_mounted = false;
181 Jmsg(jcr, M_WARNING, 0, "%s", jcr->errmsg);
183 tape_previously_mounted = true;
184 Dmsg0(200, "dir_get_volume_info\n");
185 if (!dir_get_volume_info(dcr, GET_VOL_INFO_FOR_READ)) {
186 Jmsg1(jcr, M_WARNING, 0, "%s", jcr->errmsg);
188 /* Call autochanger only once unless ask_sysop called */
189 if (try_autochanger) {
191 Dmsg2(200, "calling autoload Vol=%s Slot=%d\n",
192 jcr->VolumeName, jcr->VolCatInfo.Slot);
193 stat = autoload_device(dcr, 0, NULL);
195 try_autochanger = false;
199 /* Mount a specific volume and no other */
200 Dmsg0(200, "calling dir_ask_sysop\n");
201 if (!dir_ask_sysop_to_mount_volume(dcr)) {
202 goto get_out; /* error return */
204 try_autochanger = true; /* permit using autochanger again */
205 continue; /* try reading again */
210 Jmsg1(jcr, M_FATAL, 0, _("Too many errors trying to mount device \"%s\".\n"),
215 dev->state &= ~ST_APPEND; /* clear any previous append mode */
216 dev->state |= ST_READ; /* set read mode */
217 attach_jcr_to_device(dev, jcr); /* attach jcr to device */
218 set_jcr_job_status(jcr, JS_Running);
219 dir_send_job_status(jcr);
220 Jmsg(jcr, M_INFO, 0, _("Ready to read from volume \"%s\" on device %s.\n"),
221 jcr->VolumeName, dev_name(dev));
235 * Acquire device for writing. We permit multiple writers.
236 * If this is the first one, we read the label.
238 * Returns: NULL if failed for any reason
239 * dev if successful (may change if new dev opened)
240 * This routine must be single threaded because we may create
241 * multiple devices (for files), thus we have our own mutex
242 * on top of the device mutex.
244 DCR *acquire_device_for_append(JCR *jcr)
246 bool release = false;
247 bool recycle = false;
248 bool do_mount = false;
250 DEVICE *dev = jcr->device->dev;
252 dcr = new_dcr(jcr, dev);
253 if (device_is_unmounted(dev)) {
254 Jmsg(jcr, M_WARNING, 0, _("device %s is BLOCKED due to user unmount.\n"),
258 block_device(dev, BST_DOING_ACQUIRE);
260 P(mutex); /* lock all devices */
261 Dmsg1(190, "acquire_append device is %s\n", dev_is_tape(dev)?"tape":"disk");
264 if (dev_state(dev, ST_APPEND)) {
266 * Device already in append mode
268 * Check if we have the right Volume mounted
269 * OK if current volume info OK
270 * OK if next volume matches current volume
271 * otherwise mount desired volume obtained from
272 * dir_find_next_appendable_volume
274 pm_strcpy(&jcr->VolumeName, dev->VolHdr.VolName);
275 bstrncpy(dcr->VolumeName, dev->VolHdr.VolName, sizeof(dcr->VolumeName));
276 if (!dir_get_volume_info(dcr, GET_VOL_INFO_FOR_WRITE) &&
277 !(dir_find_next_appendable_volume(dcr) &&
278 strcmp(dev->VolHdr.VolName, jcr->VolumeName) == 0)) { /* wrong tape mounted */
279 if (dev->num_writers != 0) {
280 DEVICE *d = ((DEVRES *)dev->device)->dev;
281 uint32_t open_vols = 0;
282 for ( ; d; d=d->next) {
285 if (dev_state(dev, ST_FILE) && dev->max_open_vols > open_vols) {
286 d = init_dev(NULL, (DEVRES *)dev->device); /* init new device */
287 d->prev = dev; /* chain in new device */
290 /* Release old device */
294 free_dcr(dcr); /* release dcr pointing to old dev */
295 /* Make new device current device and lock it */
297 dcr = new_dcr(jcr, dev); /* get new dcr for new device */
299 block_device(dev, BST_DOING_ACQUIRE);
302 Jmsg(jcr, M_FATAL, 0, _("Device %s is busy writing on another Volume.\n"), dev_name(dev));
306 /* Wrong tape mounted, release it, then fall through to get correct one */
311 * At this point, the correct tape is already mounted, so
312 * we do not need to do mount_next_write_volume(), unless
313 * we need to recycle the tape.
315 recycle = strcmp(jcr->VolCatInfo.VolCatStatus, "Recycle") == 0;
316 if (recycle && dev->num_writers != 0) {
317 Jmsg(jcr, M_FATAL, 0, _("Cannot recycle volume \"%s\""
318 " because it is in use by another job.\n"));
323 /* Not already in append mode, so mount the device */
324 if (dev_state(dev, ST_READ)) {
325 Jmsg(jcr, M_FATAL, 0, _("Device %s is busy reading.\n"), dev_name(dev));
328 ASSERT(dev->num_writers == 0);
332 if (do_mount || recycle) {
333 if (!mount_next_write_volume(dcr, dcr->block, release)) {
334 if (!job_canceled(jcr)) {
335 /* Reduce "noise" -- don't print if job canceled */
336 Jmsg(jcr, M_FATAL, 0, _("Could not ready device \"%s\" for append.\n"),
344 if (jcr->NumVolumes == 0) {
347 attach_jcr_to_device(dev, jcr); /* attach jcr to device */
348 set_jcr_job_status(jcr, JS_Running);
349 dir_send_job_status(jcr);
353 * If we jump here, it is an error return because
354 * rtn_dev will still be NULL
363 V(mutex); /* unlock other threads */
368 * This job is done, so release the device. From a Unix standpoint,
369 * the device remains open.
372 bool release_device(JCR *jcr)
375 DEVICE *dev = dcr->dev;
377 Dmsg1(100, "release_device device is %s\n", dev_is_tape(dev)?"tape":"disk");
378 if (dev_state(dev, ST_READ)) {
379 dev->state &= ~ST_READ; /* clear read bit */
380 if (!dev_is_tape(dev) || !dev_cap(dev, CAP_ALWAYSOPEN)) {
381 offline_or_rewind_dev(dev);
384 /******FIXME**** send read volume usage statistics to director */
386 } else if (dev->num_writers > 0) {
388 Dmsg1(100, "There are %d writers in release_device\n", dev->num_writers);
389 if (dev_state(dev, ST_LABEL)) {
390 Dmsg0(100, "dir_create_jobmedia_record. Release\n");
391 if (!dir_create_jobmedia_record(dcr)) {
392 Jmsg(jcr, M_ERROR, 0, _("Could not create JobMedia record for Volume=\"%s\" Job=%s\n"),
393 jcr->VolCatInfo.VolCatName, jcr->Job);
395 /* If no more writers, write an EOF */
396 if (!dev->num_writers && dev_can_write(dev)) {
399 dev->VolCatInfo.VolCatFiles = dev->file; /* set number of files */
400 dev->VolCatInfo.VolCatJobs++; /* increment number of jobs */
401 /* Note! do volume update before close, which zaps VolCatInfo */
402 Dmsg0(100, "dir_update_vol_info. Release0\n");
403 dir_update_volume_info(dcr, false); /* send Volume info to Director */
406 if (!dev->num_writers && (!dev_is_tape(dev) || !dev_cap(dev, CAP_ALWAYSOPEN))) {
407 offline_or_rewind_dev(dev);
411 Jmsg2(jcr, M_ERROR, 0, _("BAD ERROR: release_device %s, Volume \"%s\" not in use.\n"),
412 dev_name(dev), NPRT(jcr->VolumeName));
414 detach_jcr_from_device(dev, jcr);
415 if (dev->prev && !dev_state(dev, ST_READ) && !dev->num_writers) {
418 dev->prev->next = dev->next; /* dechain */