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