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