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