]> git.sur5r.net Git - bacula/bacula/blob - bacula/src/stored/acquire.c
See kes02Dec02
[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) 2000, 2001, 2002 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
32 /********************************************************************* 
33  * Acquire device for reading.  We permit (for the moment)
34  *  only one reader.  We read the Volume label from the block and
35  *  leave the block pointers just after the label.
36  *
37  *  Returns: 0 if failed for any reason
38  *           1 if successful
39  */
40 int acquire_device_for_read(JCR *jcr, DEVICE *dev, DEV_BLOCK *block)
41 {
42    int stat = 0;
43    int tape_previously_mounted;
44
45    lock_device(dev);
46    block_device(dev, BST_DOING_ACQUIRE);
47    unlock_device(dev);
48
49    tape_previously_mounted = (dev->state & ST_READ) || (dev->state & ST_APPEND);
50
51    if (dev->state & ST_READ || dev->num_writers > 0) {
52       Jmsg1(jcr, M_FATAL, 0, _("Device %s is busy. Job cancelled.\n"), dev_name(dev));
53       goto get_out;
54    }
55
56    for (;;) {
57       if (job_cancelled(jcr)) {
58          Mmsg0(&dev->errmsg, _("Job cancelled.\n"));
59          goto get_out;                /* error return */
60       }
61       /*
62        * This code ensures that the device is ready for
63        * reading. If it is a file, it opens it.
64        * If it is a tape, it checks the volume name 
65        */
66       for ( ; !(dev->state & ST_OPENED); ) {
67           Dmsg1(120, "bstored: open vol=%s\n", jcr->VolumeName);
68           if (open_dev(dev, jcr->VolumeName, READ_ONLY) < 0) {
69              Jmsg(jcr, M_FATAL, 0, _("Open device %s volume %s failed, ERR=%s\n"), 
70                  dev_name(dev), jcr->VolumeName, strerror_dev(dev));
71              goto get_out;
72           }
73           Dmsg1(129, "open_dev %s OK\n", dev_name(dev));
74       }
75       /* ***FIXME*** this is probably not necessary */
76       dev->state &= ~ST_LABEL;           /* force reread of label */
77       Dmsg0(200, "calling read-vol-label\n");
78       switch (read_dev_volume_label(jcr, dev, block)) {
79          case VOL_OK:
80             break;                    /* got it */
81          case VOL_IO_ERROR:
82             /*
83              * Send error message generated by read_dev_volume_label()
84              *  only we really had a tape mounted. This supresses superfluous
85              *  error messages when nothing is mounted.
86              */
87             if (tape_previously_mounted) {
88                Jmsg1(jcr, M_WARNING, 0, "%s", jcr->errmsg);                         
89             }
90             goto default_path;
91          default:
92             Jmsg1(jcr, M_WARNING, 0, "%s", jcr->errmsg);
93 default_path:
94             tape_previously_mounted = 0;
95             Dmsg0(200, "dir_get_volume_info\n");
96             if (!dir_get_volume_info(jcr, 0)) { 
97                Jmsg1(jcr, M_WARNING, 0, "%s", jcr->errmsg);
98             }
99             Dmsg2(200, "calling autoload Vol=%s Slot=%d\n",
100                jcr->VolumeName, jcr->VolCatInfo.Slot);                         
101             if (autoload_device(jcr, dev, 0, NULL)) {
102                continue;
103             }
104             /* Mount a specific volume and no other */
105             Dmsg0(200, "calling dir_ask_sysop\n");
106             if (!dir_ask_sysop_to_mount_volume(jcr, dev)) {
107                goto get_out;          /* error return */
108             }
109             continue;                 /* try reading again */
110       }
111       break;
112    }
113
114    dev->state |= ST_READ;
115    attach_jcr_to_device(dev, jcr);    /* attach jcr to device */
116    stat = 1;                          /* good return */
117
118 get_out:
119    P(dev->mutex); 
120    unblock_device(dev);
121    V(dev->mutex);
122    return stat;
123 }
124
125 /*
126  * Acquire device for writing. We permit multiple writers.
127  *  If this is the first one, we read the label.
128  *
129  *  Returns: 0 if failed for any reason
130  *           1 if successful
131  */
132 int acquire_device_for_append(JCR *jcr, DEVICE *dev, DEV_BLOCK *block)
133 {
134    int release = 0;
135    int do_mount = 0;
136    int stat = 0;
137
138    lock_device(dev);
139    block_device(dev, BST_DOING_ACQUIRE);
140    unlock_device(dev);
141    Dmsg1(190, "acquire_append device is %s\n", dev_is_tape(dev)?"tape":"disk");
142
143
144    if (dev->state & ST_APPEND) {
145       /* 
146        * Device already in append mode   
147        *
148        * Check if we have the right Volume mounted   
149        *   OK if current volume info OK
150        *   OK if next volume matches current volume
151        *   otherwise mount desired volume obtained from
152        *    dir_find_next_appendable_volume
153        */
154       strcpy(jcr->VolumeName, dev->VolHdr.VolName);
155       if (!dir_get_volume_info(jcr, 1) ||
156           !(dir_find_next_appendable_volume(jcr) &&
157             strcmp(dev->VolHdr.VolName, jcr->VolumeName) == 0)) { /* wrong tape mounted */
158          if (dev->num_writers != 0) {
159             Jmsg(jcr, M_FATAL, 0, _("Device %s is busy writing on another Volume.\n"), dev_name(dev));
160             goto get_out;
161          }
162          /* Wrong tape mounted, release it, then fall through to get correct one */
163          release = 1;
164          do_mount = 1;
165       }
166    } else { 
167       /* Not already in append mode, so mount the device */
168       if (dev->state & ST_READ) {
169          Jmsg(jcr, M_FATAL, 0, _("Device %s is busy reading.\n"), dev_name(dev));
170          goto get_out;
171       } 
172       ASSERT(dev->num_writers == 0);
173       do_mount = 1;
174    }
175
176    if (do_mount) {
177       if (!mount_next_write_volume(jcr, dev, block, release)) {
178          Jmsg(jcr, M_FATAL, 0, _("Could not ready device %s for append.\n"),
179             dev_name(dev));
180          goto get_out;
181       }
182    }
183
184    dev->num_writers++;
185    if (dev->num_writers > 1) {
186       Dmsg2(100, "Hey!!!! There are %d writers on device %s\n", dev->num_writers,
187          dev_name(dev));
188    }
189    if (jcr->NumVolumes == 0) {
190       jcr->NumVolumes = 1;
191    }
192    attach_jcr_to_device(dev, jcr);    /* attach jcr to device */
193    stat = 1;                          /* good return */
194
195 get_out:
196    P(dev->mutex); 
197    unblock_device(dev);
198    V(dev->mutex);
199    return stat;
200 }
201
202 /*
203  * This job is done, so release the device. From a Unix standpoint,
204  *  the device remains open.
205  *
206  */
207 int release_device(JCR *jcr, DEVICE *dev)
208 {
209    lock_device(dev);
210    Dmsg1(100, "release_device device is %s\n", dev_is_tape(dev)?"tape":"disk");
211    if (dev->state & ST_READ) {
212       dev->state &= ~ST_READ;         /* clear read bit */
213       if (!dev_is_tape(dev) || !(dev->capabilities & CAP_ALWAYSOPEN)) {
214          if (dev->capabilities & CAP_OFFLINEUNMOUNT) {
215             offline_dev(dev);
216          }
217          close_dev(dev);
218       }
219       /******FIXME**** send read volume usage statistics to director */
220
221    } else if (dev->num_writers > 0) {
222       dev->num_writers--;
223       Dmsg1(100, "There are %d writers in release_device\n", dev->num_writers);
224       if (dev->num_writers == 0) {
225          weof_dev(dev, 1);
226          Dmsg0(100, "dir_create_jobmedia_record. Release\n");
227          dir_create_jobmedia_record(jcr);
228          dev->VolCatInfo.VolCatFiles++;             /* increment number of files */
229          dev->VolCatInfo.VolCatJobs++;              /* increment number of jobs */
230          /* Note! do volume update before close, which zaps VolCatInfo */
231          Dmsg0(100, "dir_update_vol_info. Release\n");
232          dir_update_volume_info(jcr, &dev->VolCatInfo, 0); /* send Volume info to Director */
233
234          if (!dev_is_tape(dev) || !(dev->capabilities & CAP_ALWAYSOPEN)) {
235             if (dev->capabilities & CAP_OFFLINEUNMOUNT) {
236                offline_dev(dev);
237             }
238             close_dev(dev);
239          }
240       } else {
241          Dmsg0(100, "dir_create_jobmedia_record. Release\n");
242          dir_create_jobmedia_record(jcr);
243          Dmsg0(100, "dir_update_vol_info. Release\n");
244          dev->VolCatInfo.VolCatJobs++;              /* increment number of jobs */
245          dir_update_volume_info(jcr, &dev->VolCatInfo, 0); /* send Volume info to Director */
246       }
247    } else {
248       Jmsg2(jcr, M_ERROR, 0, _("BAD ERROR: release_device %s, Volume %s not in use.\n"), 
249             dev_name(dev), NPRT(jcr->VolumeName));
250    }
251    detach_jcr_from_device(dev, jcr);
252    unlock_device(dev);
253    return 1;
254 }