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