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