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