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