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