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