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