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