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