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