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