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