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