]> git.sur5r.net Git - bacula/bacula/blob - bacula/src/stored/acquire.c
- Comment out Multiple Connections in the document.
[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-2005 Kern Sibbald
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 /*
32  * Create a new Device Control Record and attach
33  *   it to the device (if this is a real job).
34  */
35 DCR *new_dcr(JCR *jcr, DEVICE *dev)
36 {
37    if (jcr && jcr->dcr) {
38       return jcr->dcr;
39    }
40    DCR *dcr = (DCR *)malloc(sizeof(DCR));
41    memset(dcr, 0, sizeof(DCR));
42    if (jcr) {
43       jcr->dcr = dcr;
44    }
45    dcr->jcr = jcr;
46    dcr->dev = dev;
47    if (dev) {
48       dcr->device = dev->device;
49    }
50    dcr->block = new_block(dev);
51    dcr->rec = new_record();
52    dcr->spool_fd = -1;
53    dcr->max_spool_size = dev->device->max_spool_size;
54    /* Attach this dcr only if dev is initialized */
55    if (dev->fd != 0 && jcr && jcr->JobType != JT_SYSTEM) {
56       dev->attached_dcrs->append(dcr);
57 //    jcr->dcrs->append(dcr);
58    }
59    return dcr;
60 }
61
62 /*
63  * Search the dcrs list for the given dcr. If it is found,
64  *  as it should be, then remove it. Also zap the jcr pointer
65  *  to the dcr if it is the same one.
66  */
67 #ifdef needed
68 static void remove_dcr_from_dcrs(DCR *dcr)
69 {
70    JCR *jcr = dcr->jcr;
71    if (jcr->dcrs) {
72       int i = 0;
73       DCR *ldcr;
74       int num = jcr->dcrs->size();
75       for (i=0; i < num; i++) {
76          ldcr = (DCR *)jcr->dcrs->get(i);
77          if (ldcr == dcr) {
78             jcr->dcrs->remove(i);
79             if (jcr->dcr == dcr) {
80                jcr->dcr = NULL;
81             }
82          }
83       }
84    }
85 }
86 #endif
87
88 /*
89  * Free up all aspects of the given dcr -- i.e. dechain it,
90  *  release allocated memory, zap pointers, ...
91  */
92 void free_dcr(DCR *dcr)
93 {
94    JCR *jcr = dcr->jcr;
95    DEVICE *dev = dcr->dev;
96
97    /*
98     * If we reserved the device, we must decrement the
99     *  number of writers.
100     */
101    if (dcr->reserved_device) {
102       lock_device(dev);
103       dev->num_writers--;
104       if (dev->num_writers < 0) {
105          Jmsg1(dcr->jcr, M_ERROR, 0, _("Hey! num_writers=%d!!!!\n"), dev->num_writers);
106          dev->num_writers = 0;
107          dcr->reserved_device = false;
108       }
109       unlock_device(dev);
110    }
111
112    /* Detach this dcr only if the dev is initialized */
113    if (dev->fd != 0 && jcr && jcr->JobType != JT_SYSTEM) {
114       dev->attached_dcrs->remove(dcr);
115 //    remove_dcr_from_dcrs(dcr);
116    }
117    if (dcr->block) {
118       free_block(dcr->block);
119    }
120    if (dcr->rec) {
121       free_record(dcr->rec);
122    }
123    if (dcr->jcr) {
124       dcr->jcr->dcr = NULL;
125    }
126    free(dcr);
127 }
128
129
130 /*
131  * We "reserve" the drive by setting the ST_READ bit. No one else
132  *  should touch the drive until that is cleared.
133  *  This allows the DIR to "reserve" the device before actually
134  *  starting the job. If the device is not available, the DIR
135  *  can wait (to be implemented 1/05).
136  */
137 bool reserve_device_for_read(JCR *jcr, DEVICE *dev)
138 {
139    DCR *dcr = jcr->dcr;
140    bool first;
141
142    ASSERT(dcr);
143
144    dev->block(BST_DOING_ACQUIRE);
145
146    Mmsg(jcr->errmsg, _("Device %s is BLOCKED due to user unmount.\n"),
147         dev->print_name());
148    for (first=true; device_is_unmounted(dev); first=false) {
149       dev->unblock();
150       if (!wait_for_device(dcr, jcr->errmsg, first))  {
151          return false;
152       }
153      dev->block(BST_DOING_ACQUIRE);
154    }
155
156    Mmsg2(jcr->errmsg, _("Device %s is busy. Job %d canceled.\n"),
157          dev->print_name(), jcr->JobId);
158    for (first=true; dev->is_busy(); first=false) {
159       dev->unblock();
160       if (!wait_for_device(dcr, jcr->errmsg, first)) {
161          return false;
162       }
163       dev->block(BST_DOING_ACQUIRE);
164    }
165
166    dev->clear_append();
167    dev->set_read();
168    dev->unblock();
169    return true;
170 }
171
172
173 /*********************************************************************
174  * Acquire device for reading. 
175  *  The drive should have previously been reserved by calling 
176  *  reserve_device_for_read(). We read the Volume label from the block and
177  *  leave the block pointers just after the label.
178  *
179  *  Returns: NULL if failed for any reason
180  *           dcr  if successful
181  */
182 DCR *acquire_device_for_read(JCR *jcr, DEVICE *dev)
183 {
184    bool vol_ok = false;
185    bool tape_previously_mounted;
186    bool tape_initially_mounted;
187    VOL_LIST *vol;
188    bool try_autochanger = true;
189    int i;
190    DCR *dcr = jcr->dcr;
191    int vol_label_status;
192    
193    dev->block(BST_DOING_ACQUIRE);
194
195    init_device_wait_timers(dcr);
196
197    tape_previously_mounted = dev->can_read() ||
198                              dev->can_append() ||
199                              dev->is_labeled();
200    tape_initially_mounted = tape_previously_mounted;
201
202    if (dev->num_writers > 0) {
203       Jmsg2(jcr, M_FATAL, 0, _("Num_writers=%d not zero. Job %d canceled.\n"), 
204          dev->num_writers, jcr->JobId);
205       goto get_out;
206    }
207
208    /* Find next Volume, if any */
209    vol = jcr->VolList;
210    if (!vol) {
211       Jmsg(jcr, M_FATAL, 0, _("No volumes specified. Job %d canceled.\n"), jcr->JobId);
212       goto get_out;
213    }
214    jcr->CurVolume++;
215    for (i=1; i<jcr->CurVolume; i++) {
216       vol = vol->next;
217    }
218    bstrncpy(dcr->VolumeName, vol->VolumeName, sizeof(dcr->VolumeName));
219
220    /* Volume info is always needed because of VolParts */
221    Dmsg0(200, "dir_get_volume_info\n");
222    if (!dir_get_volume_info(dcr, GET_VOL_INFO_FOR_READ)) {
223       Jmsg1(jcr, M_WARNING, 0, "%s", jcr->errmsg);
224    }
225    
226    dev->num_parts = dcr->VolCatInfo.VolCatParts;
227    
228    for (i=0; i<5; i++) {
229       dev->clear_labeled();              /* force reread of label */
230       if (job_canceled(jcr)) {
231          Mmsg1(dev->errmsg, _("Job %d canceled.\n"), jcr->JobId);
232          goto get_out;                /* error return */
233       }
234       /*
235        * This code ensures that the device is ready for
236        * reading. If it is a file, it opens it.
237        * If it is a tape, it checks the volume name
238        */
239       for ( ; !dev->is_open(); ) {
240          Dmsg1(120, "bstored: open vol=%s\n", dcr->VolumeName);
241          if (open_dev(dev, dcr->VolumeName, OPEN_READ_ONLY) < 0) {
242             if (dev->dev_errno == EIO) {   /* no tape loaded */
243                goto default_path;
244             }
245             
246             /* If we have a dvd that requires mount, 
247              * we need to try to open the label, so the info can be reported
248              * if a wrong volume has been mounted. */
249             if (dev->is_dvd() && (dcr->VolCatInfo.VolCatParts > 0)) {
250                break;
251             }
252             
253             Jmsg(jcr, M_FATAL, 0, _("Open device %s Volume \"%s\" failed: ERR=%s\n"),
254                 dev->print_name(), dcr->VolumeName, strerror_dev(dev));
255             goto get_out;
256          }
257          Dmsg1(129, "open_dev %s OK\n", dev->print_name());
258       }
259       
260       if (dev->is_dvd()) {
261          vol_label_status = read_dev_volume_label_guess(dcr, 0);
262       } else {
263          vol_label_status = read_dev_volume_label(dcr);
264       }
265       
266       Dmsg0(200, "calling read-vol-label\n");
267       switch (vol_label_status) {
268       case VOL_OK:
269          vol_ok = true;
270          memcpy(&dev->VolCatInfo, &dcr->VolCatInfo, sizeof(dev->VolCatInfo));
271          break;                    /* got it */
272       case VOL_IO_ERROR:
273          /*
274           * Send error message generated by read_dev_volume_label()
275           *  only we really had a tape mounted. This supresses superfluous
276           *  error messages when nothing is mounted.
277           */
278          if (tape_previously_mounted) {
279             Jmsg(jcr, M_WARNING, 0, "%s", jcr->errmsg);
280          }
281          goto default_path;
282       case VOL_NAME_ERROR:
283          if (tape_initially_mounted) {
284             tape_initially_mounted = false;
285             goto default_path;
286          }
287          /* Fall through */
288       default:
289          Jmsg(jcr, M_WARNING, 0, "%s", jcr->errmsg);
290 default_path:
291          tape_previously_mounted = true;
292          
293          /* If the device requires mount, close it, so the device can be ejected.
294           * FIXME: This should perhaps be done for all devices. */
295          if (dev_cap(dev, CAP_REQMOUNT)) {
296             force_close_dev(dev);
297          }
298          
299          /* Call autochanger only once unless ask_sysop called */
300          if (try_autochanger) {
301             int stat;
302             Dmsg2(200, "calling autoload Vol=%s Slot=%d\n",
303                dcr->VolumeName, dcr->VolCatInfo.Slot);
304             stat = autoload_device(dcr, 0, NULL);
305             if (stat > 0) {
306                try_autochanger = false;
307                continue;              /* try reading volume mounted */
308             }
309          }
310          
311          /* Mount a specific volume and no other */
312          Dmsg0(200, "calling dir_ask_sysop\n");
313          if (!dir_ask_sysop_to_mount_volume(dcr)) {
314             goto get_out;             /* error return */
315          }
316          try_autochanger = true;      /* permit using autochanger again */
317          continue;                    /* try reading again */
318       } /* end switch */
319       break;
320    } /* end for loop */
321    if (!vol_ok) {
322       Jmsg1(jcr, M_FATAL, 0, _("Too many errors trying to mount device %s.\n"),
323             dev->print_name());
324       goto get_out;
325    }
326
327    dev->clear_append();
328    dev->set_read();
329    set_jcr_job_status(jcr, JS_Running);
330    dir_send_job_status(jcr);
331    Jmsg(jcr, M_INFO, 0, _("Ready to read from volume \"%s\" on device %s.\n"),
332       dcr->VolumeName, dev->print_name());
333
334 get_out:
335    dev->unblock();
336    if (!vol_ok) {
337       free_dcr(dcr);
338       dcr = NULL;
339    }
340    return dcr;
341 }
342
343 /*
344  * We reserve the device for appending by incrementing the 
345  *  reserved_device. We do virtually all the same work that
346  *  is done in acquire_device_for_append(), but we do
347  *  not attempt to mount the device. This routine allows
348  *  the DIR to reserve multiple devices before *really* 
349  *  starting the job. It also permits the SD to refuse 
350  *  certain devices (not up, ...).
351  *
352  * Note, in reserving a device, if the device is for the
353  *  same pool and the same pool type, then it is acceptable.
354  *  The Media Type has already been checked. If we are
355  *  the first tor reserve the device, we put the pool
356  *  name and pool type in the device record.
357  */
358 bool reserve_device_for_append(JCR *jcr, DEVICE *dev)
359 {
360    DCR *dcr = jcr->dcr;
361    bool ok = false;
362
363    ASSERT(dcr);
364
365    dev->block(BST_DOING_ACQUIRE);
366    if (dev->can_read()) {
367       Jmsg(jcr, M_WARNING, 0, _("Device %s is busy reading.\n"), dev->print_name());
368       goto bail_out;
369    }
370    if (device_is_unmounted(dev)) {
371       Jmsg(jcr, M_WARNING, 0, _("device %s is BLOCKED due to user unmount.\n"),
372          dev->print_name());
373       goto bail_out;
374    }
375    Dmsg1(190, "reserve_append device is %s\n", dev_is_tape(dev)?"tape":"disk");
376    /*
377     * First handle the case that the drive is not yet in append mode
378     */
379    if (!dev->can_append() && dev->num_writers == 0) {
380       /* Now check if there are any reservations on the drive */
381       if (dev->reserved_device) {           
382          /* Yes, now check if we want the same Pool and pool type */
383          if (strcmp(dev->pool_name, dcr->pool_name) == 0 &&
384              strcmp(dev->pool_type, dcr->pool_type) == 0) {
385             /* OK, compatible device */
386          } else {
387             /* Drive not suitable for us */
388             Jmsg(jcr, M_WARNING, 0, _("Device %s is busy writing on another Volume.\n"), dev->print_name());
389             goto bail_out;
390          }
391       } else {
392          /* Device is available but not yet reserved, reserve it for us */
393          bstrncpy(dev->pool_name, dcr->pool_name, sizeof(dev->pool_name));
394          bstrncpy(dev->pool_type, dcr->pool_type, sizeof(dev->pool_type));
395          dev->PoolId = dcr->PoolId;
396       }
397       goto do_reserve;
398    }
399
400    /*
401     * Now check if the device is in append mode 
402     */
403    if (dev->can_append() || dev->num_writers > 0) {
404       Dmsg0(190, "device already in append.\n");
405       /* Yes, now check if we want the same Pool and pool type */
406       if (strcmp(dev->pool_name, dcr->pool_name) == 0 &&
407           strcmp(dev->pool_type, dcr->pool_type) == 0) {
408          /* OK, compatible device */
409       } else {
410          /* Drive not suitable for us */
411          Jmsg(jcr, M_WARNING, 0, _("Device %s is busy writing on another Volume.\n"), dev->print_name());
412          goto bail_out;
413       }
414    } else {
415       Pmsg0(000, "Logic error!!!! Should not get here.\n");
416       goto bail_out;                  /* should not get here */
417    }
418
419 do_reserve:
420    dev->reserved_device++;
421    dcr->reserved_device = true;
422    ok = true;
423
424 bail_out:
425    dev->unblock();
426    return ok;
427 }
428
429 /*
430  * Acquire device for writing. We permit multiple writers.
431  *  If this is the first one, we read the label.
432  *
433  *  Returns: NULL if failed for any reason
434  *           dcr if successful.
435  *   Note, normally reserve_device_for_append() is called
436  *   before this routine.
437  */
438 DCR *acquire_device_for_append(JCR *jcr, DEVICE *dev)
439 {
440    bool release = false;
441    bool recycle = false;
442    bool do_mount = false;
443    DCR *dcr = jcr->dcr;
444
445    if (!dcr) {
446       dcr = new_dcr(jcr, dev);
447    }
448    dev->block(BST_DOING_ACQUIRE);
449    Dmsg1(190, "acquire_append device is %s\n", dev_is_tape(dev)?"tape":"disk");
450
451    if (dcr->reserved_device) {
452       dev->reserved_device--;
453       dcr->reserved_device = false;
454    }
455
456    /*
457     * With the reservation system, this should not happen
458     */
459    if (dev->can_read()) {
460       Jmsg(jcr, M_FATAL, 0, _("Device %s is busy reading.\n"), dev->print_name());
461       goto get_out;
462    }
463
464    if (dev->can_append()) {
465       Dmsg0(190, "device already in append.\n");
466       /*
467        * Device already in append mode
468        *
469        * Check if we have the right Volume mounted
470        *   OK if current volume info OK
471        *   OK if next volume matches current volume
472        *   otherwise mount desired volume obtained from
473        *    dir_find_next_appendable_volume
474        */
475       bstrncpy(dcr->VolumeName, dev->VolHdr.VolName, sizeof(dcr->VolumeName));
476       if (!dir_get_volume_info(dcr, GET_VOL_INFO_FOR_WRITE) &&
477           !(dir_find_next_appendable_volume(dcr) &&
478             strcmp(dev->VolHdr.VolName, dcr->VolumeName) == 0)) { /* wrong tape mounted */
479          Dmsg0(190, "Wrong tape mounted.\n");
480          if (dev->num_writers != 0 || dev->reserved_device) {
481             Jmsg(jcr, M_FATAL, 0, _("Device %s is busy writing on another Volume.\n"), dev->print_name());
482             goto get_out;
483          }
484          /* Wrong tape mounted, release it, then fall through to get correct one */
485          Dmsg0(190, "Wrong tape mounted, release and try mount.\n");
486          release = true;
487          do_mount = true;
488       } else {
489          /*
490           * At this point, the correct tape is already mounted, so
491           *   we do not need to do mount_next_write_volume(), unless
492           *   we need to recycle the tape.
493           */
494           recycle = strcmp(dcr->VolCatInfo.VolCatStatus, "Recycle") == 0;
495           Dmsg1(190, "Correct tape mounted. recycle=%d\n", recycle);
496           if (recycle && dev->num_writers != 0) {
497              Jmsg(jcr, M_FATAL, 0, _("Cannot recycle volume \"%s\""
498                   " because it is in use by another job.\n"));
499              goto get_out;
500           }
501           if (dev->num_writers == 0) {
502              memcpy(&dev->VolCatInfo, &dcr->VolCatInfo, sizeof(dev->VolCatInfo));
503           }
504        }
505    } else {
506       /* Not already in append mode, so mount the device */
507       Dmsg0(190, "Not in append mode, try mount.\n");
508       ASSERT(dev->num_writers == 0);
509       do_mount = true;
510    }
511
512    if (do_mount || recycle) {
513       Dmsg0(190, "Do mount_next_write_vol\n");
514       bool mounted = mount_next_write_volume(dcr, release);
515       if (!mounted) {
516          if (!job_canceled(jcr)) {
517             /* Reduce "noise" -- don't print if job canceled */
518             Jmsg(jcr, M_FATAL, 0, _("Could not ready device %s for append.\n"),
519                dev->print_name());
520          }
521          goto get_out;
522       }
523    }
524
525    dev->num_writers++;                /* we are now a writer */
526    if (jcr->NumVolumes == 0) {
527       jcr->NumVolumes = 1;
528    }
529    goto ok_out;
530
531 /*
532  * If we jump here, it is an error return because
533  *  rtn_dev will still be NULL
534  */
535 get_out:
536    free_dcr(dcr);
537    dcr = NULL;
538 ok_out:
539    dev->unblock();
540    return dcr;
541 }
542
543 /*
544  * This job is done, so release the device. From a Unix standpoint,
545  *  the device remains open.
546  *
547  */
548 bool release_device(DCR *dcr)
549 {
550    bool ok = true;
551    JCR *jcr = dcr->jcr;
552    DEVICE *dev = dcr->dev;
553
554    lock_device(dev);
555    Dmsg1(100, "release_device device is %s\n", dev_is_tape(dev)?"tape":"disk");
556
557    /* if device is reserved, job never started, so release the reserve here */
558    if (dcr->reserved_device) {
559       dev->reserved_device--;
560       dcr->reserved_device = false;
561    }
562
563    if (dev->can_read()) {
564       dev->clear_read();              /* clear read bit */
565       /* Close if file or !CAP_ALWAYSOPEN */
566       if (!dev->is_tape() || !dev_cap(dev, CAP_ALWAYSOPEN)) {
567          offline_or_rewind_dev(dev);
568          close_dev(dev);
569       }
570       /******FIXME**** send read volume usage statistics to director */
571
572    } else if (dev->num_writers > 0) {
573       dev->num_writers--;
574       Dmsg1(100, "There are %d writers in release_device\n", dev->num_writers);
575       if (dev->is_labeled()) {
576          Dmsg0(100, "dir_create_jobmedia_record. Release\n");
577          if (!dir_create_jobmedia_record(dcr)) {
578             Jmsg(jcr, M_FATAL, 0, _("Could not create JobMedia record for Volume=\"%s\" Job=%s\n"),
579                dcr->VolCatInfo.VolCatName, jcr->Job);
580             ok = false;
581          }
582          /* If no more writers, write an EOF */
583          if (!dev->num_writers && dev_can_write(dev)) {
584             weof_dev(dev, 1);
585             write_ansi_ibm_labels(dcr, ANSI_EOF_LABEL, dev->VolHdr.VolName);
586          }
587          dev->VolCatInfo.VolCatFiles = dev->file;   /* set number of files */
588          dev->VolCatInfo.VolCatJobs++;              /* increment number of jobs */
589          /* Note! do volume update before close, which zaps VolCatInfo */
590          Dmsg0(100, "dir_update_vol_info. Release0\n");
591          dir_update_volume_info(dcr, false); /* send Volume info to Director */
592          Dmsg0(100, "==== write ansi eof label \n");
593       }
594
595       /* If no writers, close if file or !CAP_ALWAYS_OPEN */
596       if (dev->num_writers == 0 && (!dev->is_tape() || !dev_cap(dev, CAP_ALWAYSOPEN))) {
597          offline_or_rewind_dev(dev);
598          close_dev(dev);
599       }
600    } else {
601       Jmsg2(jcr, M_FATAL, 0, _("BAD ERROR: release_device %s, Volume \"%s\" not in use.\n"),
602             dev->print_name(), NPRT(dcr->VolumeName));
603       Jmsg2(jcr, M_ERROR, 0, _("num_writers=%d state=%x\n"), dev->num_writers, dev->state);
604       ok = false;
605    }
606
607    /* Fire off Alert command and include any output */
608    if (!job_canceled(jcr) && dcr->device->alert_command) {
609       POOLMEM *alert;
610       int status = 1;
611       BPIPE *bpipe;
612       char line[MAXSTRING];
613       alert = get_pool_memory(PM_FNAME);
614       alert = edit_device_codes(dcr, alert, "");
615       bpipe = open_bpipe(alert, 0, "r");
616       if (bpipe) {
617          while (fgets(line, sizeof(line), bpipe->rfd)) {
618             Jmsg(jcr, M_ALERT, 0, _("Alert: %s"), line);
619          }
620          status = close_bpipe(bpipe);
621       } else {
622          status = errno;
623       }
624       if (status != 0) {
625          berrno be;
626          Jmsg(jcr, M_ALERT, 0, _("3997 Bad alert command: %s: ERR=%s.\n"),
627               alert, be.strerror(status));
628       }
629
630       Dmsg1(400, "alert status=%d\n", status);
631       free_pool_memory(alert);
632    }
633    unlock_device(dev);
634    free_dcr(dcr);
635    jcr->dcr = NULL;
636    pthread_cond_broadcast(&wait_device_release);
637    return ok;
638 }