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