]> git.sur5r.net Git - bacula/bacula/blob - bacula/src/stored/acquire.c
Fix positioning error on second tape
[bacula/bacula] / bacula / src / stored / acquire.c
1 /*
2  *  Routines to acquire and release a device for read/write
3  *
4  *   Kern Sibbald, August MMII
5  *                            
6  *   Version $Id$
7  */
8 /*
9    Copyright (C) 2002-2003 Kern Sibbald and John Walker
10
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.
15
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.
20
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,
24    MA 02111-1307, USA.
25
26  */
27
28 #include "bacula.h"                   /* pull in global headers */
29 #include "stored.h"                   /* pull in Storage Deamon headers */
30
31 static pthread_mutex_t mutex = PTHREAD_MUTEX_INITIALIZER;
32
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.
37  *
38  *  Returns: 0 if failed for any reason
39  *           1 if successful
40  */
41 int acquire_device_for_read(JCR *jcr, DEVICE *dev, DEV_BLOCK *block)
42 {
43    int stat = 0;
44    int tape_previously_mounted;
45    VOL_LIST *vol;
46    int autochanger = 0;
47    int i;
48
49    lock_device(dev);
50    block_device(dev, BST_DOING_ACQUIRE);
51    unlock_device(dev);
52
53    tape_previously_mounted = (dev->state & ST_READ) || (dev->state & ST_APPEND);
54
55    if (dev->state & ST_READ || dev->num_writers > 0) {
56       Jmsg1(jcr, M_FATAL, 0, _("Device %s is busy. Job canceled.\n"), dev_name(dev));
57       goto get_out;
58    }
59
60    /* Find next Volume, if any */
61    vol = jcr->VolList;
62    if (!vol) {
63       Jmsg(jcr, M_FATAL, 0, _("No volumes specified. Job canceled.\n"));
64       goto get_out;
65    }
66    jcr->CurVolume++;
67    for (i=1; i<jcr->CurVolume; i++) {
68       vol = vol->next;
69    }
70    pm_strcpy(&jcr->VolumeName, vol->VolumeName);
71
72    for (i=0; i<5; i++) {
73       if (job_canceled(jcr)) {
74          Mmsg0(&dev->errmsg, _("Job canceled.\n"));
75          goto get_out;                /* error return */
76       }
77       /*
78        * This code ensures that the device is ready for
79        * reading. If it is a file, it opens it.
80        * If it is a tape, it checks the volume name 
81        */
82       for ( ; !(dev->state & ST_OPENED); ) {
83          Dmsg1(120, "bstored: open vol=%s\n", jcr->VolumeName);
84          if (open_dev(dev, jcr->VolumeName, READ_ONLY) < 0) {
85             Jmsg(jcr, M_FATAL, 0, _("Open device %s volume %s failed, ERR=%s\n"), 
86                 dev_name(dev), jcr->VolumeName, strerror_dev(dev));
87             goto get_out;
88          }
89          Dmsg1(129, "open_dev %s OK\n", dev_name(dev));
90       }
91       dev->state &= ~ST_LABEL;           /* force reread of label */
92       Dmsg0(200, "calling read-vol-label\n");
93       switch (read_dev_volume_label(jcr, dev, block)) {
94       case VOL_OK:
95          stat = 1;
96          break;                    /* got it */
97       case VOL_IO_ERROR:
98          /*
99           * Send error message generated by read_dev_volume_label()
100           *  only we really had a tape mounted. This supresses superfluous
101           *  error messages when nothing is mounted.
102           */
103          if (tape_previously_mounted) {
104             Jmsg(jcr, M_WARNING, 0, "%s", jcr->errmsg);                         
105          }
106          goto default_path;
107       default:
108          Jmsg(jcr, M_WARNING, 0, "%s", jcr->errmsg);
109 default_path:
110          tape_previously_mounted = 1;
111          Dmsg0(200, "dir_get_volume_info\n");
112          if (!dir_get_volume_info(jcr, GET_VOL_INFO_FOR_READ)) { 
113             Jmsg1(jcr, M_WARNING, 0, "%s", jcr->errmsg);
114          }
115          /* Call autochanger only once unless ask_sysop called */
116          if (!autochanger) {
117             Dmsg2(200, "calling autoload Vol=%s Slot=%d\n",
118                jcr->VolumeName, jcr->VolCatInfo.Slot);                         
119             if ((autochanger=autoload_device(jcr, dev, 0, NULL))) {
120                continue;
121             }
122          }
123          /* Mount a specific volume and no other */
124          Dmsg0(200, "calling dir_ask_sysop\n");
125          if (!dir_ask_sysop_to_mount_volume(jcr, dev)) {
126             goto get_out;             /* error return */
127          }
128          autochanger = 0;             /* permit using autochanger again */
129          continue;                    /* try reading again */
130       } /* end switch */
131       break;
132    } /* end for loop */
133    if (stat == 0) {
134       Jmsg1(jcr, M_FATAL, 0, _("Too many errors trying to mount device \"%s\".\n"),
135             dev_name(dev));
136       goto get_out;
137    }
138
139    dev->state |= ST_READ;
140    attach_jcr_to_device(dev, jcr);    /* attach jcr to device */
141    Jmsg(jcr, M_INFO, 0, _("Ready to read from volume \"%s\" on device %s.\n"),
142       jcr->VolumeName, dev_name(dev));
143
144 get_out:
145    P(dev->mutex); 
146    unblock_device(dev);
147    V(dev->mutex);
148    return stat;
149 }
150
151 /*
152  * Acquire device for writing. We permit multiple writers.
153  *  If this is the first one, we read the label.
154  *
155  *  Returns: NULL if failed for any reason
156  *           dev if successful (may change if new dev opened)
157  *  This routine must be single threaded because we may create
158  *   multiple devices (for files), thus we have our own mutex 
159  *   on top of the device mutex.
160  */
161 DEVICE * acquire_device_for_append(JCR *jcr, DEVICE *dev, DEV_BLOCK *block)
162 {
163    int release = 0;
164    int do_mount = 0;
165    DEVICE *rtn_dev = NULL;
166
167    lock_device(dev);
168    block_device(dev, BST_DOING_ACQUIRE);
169    unlock_device(dev);
170    P(mutex);                         /* lock all devices */
171    Dmsg1(190, "acquire_append device is %s\n", dev_is_tape(dev)?"tape":"disk");
172              
173
174    if (dev->state & ST_APPEND) {
175       /* 
176        * Device already in append mode   
177        *
178        * Check if we have the right Volume mounted   
179        *   OK if current volume info OK
180        *   OK if next volume matches current volume
181        *   otherwise mount desired volume obtained from
182        *    dir_find_next_appendable_volume
183        */
184       pm_strcpy(&jcr->VolumeName, dev->VolHdr.VolName);
185       if (!dir_get_volume_info(jcr, GET_VOL_INFO_FOR_WRITE) &&
186           !(dir_find_next_appendable_volume(jcr) &&
187             strcmp(dev->VolHdr.VolName, jcr->VolumeName) == 0)) { /* wrong tape mounted */
188          if (dev->num_writers != 0) {
189             DEVICE *d = ((DEVRES *)dev->device)->dev;
190             uint32_t open_vols = 0;
191             for ( ; d; d=d->next) {
192                open_vols++;
193             }
194             if (dev->state & ST_FILE && dev->max_open_vols > open_vols) {
195                d = init_dev(NULL, (DEVRES *)dev->device); /* init new device */
196                d->prev = dev;                   /* chain in new device */
197                d->next = dev->next;
198                dev->next = d;
199                /* Release old device */
200                P(dev->mutex); 
201                unblock_device(dev);
202                V(dev->mutex);
203                /* Make new device current device and lock it */
204                dev = d;
205                lock_device(dev);
206                block_device(dev, BST_DOING_ACQUIRE);
207                unlock_device(dev);
208             } else {
209                Jmsg(jcr, M_FATAL, 0, _("Device %s is busy writing on another Volume.\n"), dev_name(dev));
210                goto get_out;
211             }
212          }
213          /* Wrong tape mounted, release it, then fall through to get correct one */
214          release = 1;
215          do_mount = 1;
216       }
217    } else { 
218       /* Not already in append mode, so mount the device */
219       if (dev->state & ST_READ) {
220          Jmsg(jcr, M_FATAL, 0, _("Device %s is busy reading.\n"), dev_name(dev));
221          goto get_out;
222       } 
223       ASSERT(dev->num_writers == 0);
224       do_mount = 1;
225    }
226
227    if (do_mount) {
228       if (!mount_next_write_volume(jcr, dev, block, release)) {
229          Jmsg(jcr, M_FATAL, 0, _("Could not ready device %s for append.\n"),
230             dev_name(dev));
231          goto get_out;
232       }
233    }
234
235    dev->num_writers++;
236    if (dev->num_writers > 1) {
237       Dmsg2(100, "Hey!!!! There are %d writers on device %s\n", dev->num_writers,
238          dev_name(dev));
239    }
240    if (jcr->NumVolumes == 0) {
241       jcr->NumVolumes = 1;
242    }
243    attach_jcr_to_device(dev, jcr);    /* attach jcr to device */
244    rtn_dev = dev;                     /* return device */
245
246 get_out:
247    P(dev->mutex); 
248    unblock_device(dev);
249    V(dev->mutex);
250    V(mutex);                          /* unlock other threads */
251    return rtn_dev;
252 }
253
254 /*
255  * This job is done, so release the device. From a Unix standpoint,
256  *  the device remains open.
257  *
258  */
259 int release_device(JCR *jcr, DEVICE *dev)
260 {
261    lock_device(dev);
262    Dmsg1(100, "release_device device is %s\n", dev_is_tape(dev)?"tape":"disk");
263    if (dev->state & ST_READ) {
264       dev->state &= ~ST_READ;         /* clear read bit */
265       if (!dev_is_tape(dev) || !dev_cap(dev, CAP_ALWAYSOPEN)) {
266          offline_or_rewind_dev(dev);
267          close_dev(dev);
268       }
269       /******FIXME**** send read volume usage statistics to director */
270
271    } else if (dev->num_writers > 0) {
272       dev->num_writers--;
273       Dmsg1(100, "There are %d writers in release_device\n", dev->num_writers);
274       if (dev->num_writers == 0) {
275          /* If we are the only writer, write EOF after job */
276          if (dev->state & ST_LABEL) {
277             Dmsg0(100, "dir_create_jobmedia_record. Release\n");
278             dir_create_jobmedia_record(jcr);
279             if (dev_can_write(dev)) {
280                weof_dev(dev, 1);
281             }
282             dev->VolCatInfo.VolCatFiles = dev->file;   /* set number of files */
283             dev->VolCatInfo.VolCatJobs++;              /* increment number of jobs */
284             /* Note! do volume update before close, which zaps VolCatInfo */
285             Dmsg0(200, "dir_update_vol_info. Release0\n");
286             dir_update_volume_info(jcr, &dev->VolCatInfo, 0); /* send Volume info to Director */
287          }
288
289          if (!dev_is_tape(dev) || !dev_cap(dev, CAP_ALWAYSOPEN)) {
290             offline_or_rewind_dev(dev);
291             close_dev(dev);
292          }
293       } else if (dev->state & ST_LABEL) {
294          Dmsg0(100, "dir_create_jobmedia_record. Release\n");
295          dir_create_jobmedia_record(jcr);
296          Dmsg0(200, "dir_update_vol_info. Release1\n");
297          dev->VolCatInfo.VolCatFiles = dev->file;   /* set number of files */
298          dev->VolCatInfo.VolCatJobs++;              /* increment number of jobs */
299          dir_update_volume_info(jcr, &dev->VolCatInfo, 0); /* send Volume info to Director */
300       }
301    } else {
302       Jmsg2(jcr, M_ERROR, 0, _("BAD ERROR: release_device %s, Volume %s not in use.\n"), 
303             dev_name(dev), NPRT(jcr->VolumeName));
304    }
305    detach_jcr_from_device(dev, jcr);
306    if (dev->prev && !(dev->state & ST_READ) && dev->num_writers == 0) {
307       P(mutex);
308       unlock_device(dev);
309       dev->prev->next = dev->next;    /* dechain */
310       term_dev(dev);
311       V(mutex);
312    } else {
313       unlock_device(dev);
314    }
315    return 1;
316 }