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