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