]> git.sur5r.net Git - bacula/bacula/blob - bacula/src/stored/mount.c
SetIP command + CloseOnPoll + Full,Inc,Diff Pools + more access control checks
[bacula/bacula] / bacula / src / stored / mount.c
1 /*
2  *
3  *  Routines for handling mounting tapes for reading and for
4  *    writing.
5  *
6  *   Kern Sibbald, August MMII
7  *                            
8  *   Version $Id$
9  */
10 /*
11    Copyright (C) 2000-2004 Kern Sibbald and John Walker
12
13    This program is free software; you can redistribute it and/or
14    modify it under the terms of the GNU General Public License as
15    published by the Free Software Foundation; either version 2 of
16    the License, or (at your option) any later version.
17
18    This program is distributed in the hope that it will be useful,
19    but WITHOUT ANY WARRANTY; without even the implied warranty of
20    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
21    General Public License for more details.
22
23    You should have received a copy of the GNU General Public
24    License along with this program; if not, write to the Free
25    Software Foundation, Inc., 59 Temple Place - Suite 330, Boston,
26    MA 02111-1307, USA.
27
28  */
29
30 #include "bacula.h"                   /* pull in global headers */
31 #include "stored.h"                   /* pull in Storage Deamon headers */
32
33 /*
34  * If release is set, we rewind the current volume, 
35  * which we no longer want, and ask the user (console) 
36  * to mount the next volume.
37  *
38  *  Continue trying until we get it, and then ensure
39  *  that we can write on it.
40  *
41  * This routine returns a 0 only if it is REALLY
42  *  impossible to get the requested Volume.
43  *
44  */
45 int mount_next_write_volume(JCR *jcr, DEVICE *dev, DEV_BLOCK *block, int release)
46 {
47    int retry = 0;
48    bool ask = false, recycle, autochanger;
49    int vol_label_status;
50
51    Dmsg0(100, "Enter mount_next_volume()\n");
52
53    init_dev_wait_timers(dev);
54
55    /* 
56     * Attempt to mount the next volume. If something non-fatal goes
57     *  wrong, we come back here to re-try (new op messages, re-read
58     *  Volume, ...)
59     */
60 mount_next_vol:
61    /* Ignore retry if this is poll request */
62    if (!dev->poll && retry++ > 8) {
63       Jmsg(jcr, M_FATAL, 0, _("Too many errors trying to mount device %s.\n"), 
64            dev_name(dev));
65       return 0;
66    }
67    if (job_canceled(jcr)) {
68       Jmsg(jcr, M_FATAL, 0, _("Job %d canceled.\n"), jcr->JobId);
69       return 0;
70    }
71    autochanger = false;                /* Assume no autochanger */
72    recycle = false;
73    if (release) {
74       Dmsg0(100, "mount_next_volume release=1\n");
75       release_volume(jcr, dev);
76       ask = true;                     /* ask operator to mount tape */
77    }
78
79    /* 
80     * Get Director's idea of what tape we should have mounted. 
81     *    in jcr->VolCatInfo
82     */
83    Dmsg0(200, "Before dir_find_next_appendable_volume.\n");
84    while (!dir_find_next_appendable_volume(jcr)) {
85        Dmsg0(200, "not dir_find_next\n");
86        if (!dir_ask_sysop_to_create_appendable_volume(jcr, dev)) {
87          return 0;
88        }
89        Dmsg0(200, "Again dir_find_next_append...\n");
90    }
91    if (job_canceled(jcr)) {
92       return 0;
93    }
94    Dmsg2(100, "After find_next_append. Vol=%s Slot=%d\n",
95          jcr->VolCatInfo.VolCatName, jcr->VolCatInfo.Slot);
96
97    /* 
98     * Get next volume and ready it for append
99     * This code ensures that the device is ready for
100     * writing. We start from the assumption that there
101     * may not be a tape mounted. 
102     *
103     * If the device is a file, we create the output
104     * file. If it is a tape, we check the volume name
105     * and move the tape to the end of data.
106     *
107     * It assumes that the device is not already in use!
108     *
109     */
110    dev->state &= ~(ST_APPEND|ST_READ|ST_EOT|ST_WEOT|ST_EOF);
111
112    autochanger = autoload_device(jcr, dev, 1, NULL);
113    Dmsg1(100, "autoload_dev returns %d\n", autochanger);
114    /*
115     * If we autochanged to correct Volume or (we have not just
116     *   released the Volume AND we can automount) we go ahead 
117     *   and read the label. If there is no tape in the drive,
118     *   we will err, recurse and ask the operator the next time.
119     */
120    if (autochanger || (!release && dev_is_tape(dev) && dev_cap(dev, CAP_AUTOMOUNT))) {
121       ask = false;                 /* don't ask SYSOP this time */
122    }
123    Dmsg2(100, "Ask=%d autochanger=%d\n", ask, autochanger);
124    release = true;                /* release next time if we "recurse" */
125
126    if (ask && !dir_ask_sysop_to_mount_volume(jcr, dev)) {
127       Dmsg0(100, "Error return ask_sysop ...\n");
128       return 0;              /* error return */
129    }
130    if (job_canceled(jcr)) {
131       return 0;
132    }
133    Dmsg1(100, "want vol=%s\n", jcr->VolumeName);
134
135    if (dev->poll && dev_cap(dev, CAP_CLOSEONPOLL)) {
136       force_close_dev(dev);
137    }
138
139    /* Open device */
140    if  (!(dev_state(dev, ST_OPENED))) {
141        int mode;
142        if (dev_cap(dev, CAP_STREAM)) {
143           mode = OPEN_WRITE_ONLY;
144        } else {
145           mode = OPEN_READ_WRITE;
146        }
147        if (open_dev(dev, jcr->VolCatInfo.VolCatName, mode) < 0) {
148           Jmsg2(jcr, M_FATAL, 0, _("Unable to open device %s. ERR=%s\n"), 
149              dev_name(dev), strerror_dev(dev));
150           return 0;
151        }
152    }
153
154    /*
155     * Now make sure we have the right tape mounted
156     */
157 read_volume:
158    /* 
159     * If we are writing to a stream device, ASSUME the volume label
160     *  is correct.
161     */
162    if (dev_cap(dev, CAP_STREAM)) {
163       vol_label_status = VOL_OK;
164       create_volume_label(dev, jcr->VolumeName, "Default");
165       dev->VolHdr.LabelType = PRE_LABEL;
166    } else {
167       vol_label_status = read_dev_volume_label(jcr, dev, block);
168    }
169
170    Dmsg2(100, "dirVol=%s dirStat=%s\n", jcr->VolumeName,
171       jcr->VolCatInfo.VolCatStatus);
172    /*
173     * At this point, dev->VolCatInfo has what is in the drive, if anything,
174     *          and   jcr->VolCatInfo has what the Director wants.
175     */
176    switch (vol_label_status) {
177    case VOL_OK:
178       Dmsg1(100, "Vol OK name=%s\n", jcr->VolumeName);
179       memcpy(&dev->VolCatInfo, &jcr->VolCatInfo, sizeof(jcr->VolCatInfo));
180       recycle = strcmp(dev->VolCatInfo.VolCatStatus, "Recycle") == 0;
181       break;                    /* got a Volume */
182    case VOL_NAME_ERROR:
183       VOLUME_CAT_INFO VolCatInfo;
184
185       Dmsg1(100, "Vol NAME Error Name=%s\n", jcr->VolumeName);
186       /* If polling and got a previous bad name, ignore it */
187       if (dev->poll && strcmp(dev->BadVolName, dev->VolHdr.VolName) == 0) {
188          ask = true;
189          Dmsg1(200, "Vol Name error supress due to poll. Name=%s\n", 
190             jcr->VolumeName);
191          goto mount_next_vol;
192       }
193       /* 
194        * OK, we got a different volume mounted. First save the
195        *  requested Volume info (jcr) structure, then query if
196        *  this volume is really OK. If not, put back the desired
197        *  volume name and continue.
198        */
199       memcpy(&VolCatInfo, &jcr->VolCatInfo, sizeof(VolCatInfo));
200       /* Check if this is a valid Volume in the pool */
201       pm_strcpy(&jcr->VolumeName, dev->VolHdr.VolName);                         
202       if (!dir_get_volume_info(jcr, GET_VOL_INFO_FOR_WRITE)) {
203          bstrncpy(dev->BadVolName, dev->VolHdr.VolName, sizeof(dev->BadVolName));
204          Jmsg(jcr, M_WARNING, 0, _("Director wanted Volume \"%s\".\n"
205               "    Current Volume \"%s\" not acceptable because:\n"
206               "    %s"),
207              VolCatInfo.VolCatName, dev->VolHdr.VolName,
208              jcr->dir_bsock->msg);
209          /* Restore desired volume name, note device info out of sync */
210          memcpy(&jcr->VolCatInfo, &VolCatInfo, sizeof(jcr->VolCatInfo));
211          ask = true;
212          goto mount_next_vol;
213       }
214       Dmsg1(100, "want new name=%s\n", jcr->VolumeName);
215       memcpy(&dev->VolCatInfo, &jcr->VolCatInfo, sizeof(dev->VolCatInfo));
216       recycle = strcmp(dev->VolCatInfo.VolCatStatus, "Recycle") == 0;
217       break;                /* got a Volume */
218    /*
219     * At this point, we assume we have a blank tape mounted.
220     */
221    case VOL_NO_LABEL:
222    case VOL_IO_ERROR:
223       /* 
224        * If permitted, we label the device, make sure we can do
225        *   it by checking that the VolCatBytes is zero => not labeled, 
226        *   once the Volume is labeled we don't want to label another
227        *   blank tape with the same name.  For disk, we go ahead and
228        *   label it anyway, because the OS insures that there is only
229        *   one Volume with that name.
230        * As noted above, at this point jcr->VolCatInfo has what
231        *   the Director wants and dev->VolCatInfo has info on the
232        *   previous tape (or nothing).
233        */
234       if (dev_cap(dev, CAP_LABEL) && (jcr->VolCatInfo.VolCatBytes == 0 ||
235             (!dev_is_tape(dev) && strcmp(jcr->VolCatInfo.VolCatStatus, 
236                                    "Recycle") == 0))) {
237          Dmsg0(100, "Create volume label\n");
238          if (!write_volume_label_to_dev(jcr, (DEVRES *)dev->device, jcr->VolumeName,
239                 jcr->pool_name)) {
240             Dmsg0(100, "!write_vol_label\n");
241             goto mount_next_vol;
242          }
243          Dmsg0(100, "dir_update_vol_info. Set Append\n");
244          /* Copy Director's info into the device info */
245          memcpy(&dev->VolCatInfo, &jcr->VolCatInfo, sizeof(dev->VolCatInfo));
246          dir_update_volume_info(jcr, dev, 1);  /* indicate tape labeled */
247          Jmsg(jcr, M_INFO, 0, _("Labeled new Volume \"%s\" on device %s.\n"),
248             jcr->VolumeName, dev_name(dev));
249          goto read_volume;      /* read label we just wrote */
250       } 
251       /* NOTE! Fall-through wanted. */
252    case VOL_NO_MEDIA:
253    default:
254       /* Send error message */
255       if (!dev->poll) {
256          Jmsg(jcr, M_WARNING, 0, "%s", jcr->errmsg);                         
257       } else {
258          Dmsg1(200, "Msg suppressed by poll: %s\n", jcr->errmsg);
259       }
260       ask = true;
261       goto mount_next_vol;
262    }
263
264    /* 
265     * See if we have a fresh tape or a tape with data.
266     *
267     * Note, if the LabelType is PRE_LABEL, it was labeled
268     *  but never written. If so, rewrite the label but set as
269     *  VOL_LABEL.  We rewind and return the label (reconstructed)
270     *  in the block so that in the case of a new tape, data can
271     *  be appended just after the block label.  If we are writing
272     *  a second volume, the calling routine will write the label
273     *  before writing the overflow block.
274     *
275     *  If the tape is marked as Recycle, we rewrite the label.
276     */
277    if (dev->VolHdr.LabelType == PRE_LABEL || recycle) {
278       Dmsg1(190, "ready_for_append found freshly labeled volume. dev=%x\n", dev);
279       dev->VolHdr.LabelType = VOL_LABEL; /* set Volume label */
280       write_volume_label_to_block(jcr, dev, block);
281       /*
282        * If we are not dealing with a streaming device,
283        *  write the block now to ensure we have write permission.
284        *  It is better to find out now rather than later.
285        */
286       if (!dev_cap(dev, CAP_STREAM)) {
287          if (!rewind_dev(dev)) {
288             Jmsg2(jcr, M_WARNING, 0, _("Rewind error on device \"%s\". ERR=%s\n"), 
289                   dev_name(dev), strerror_dev(dev));
290          }
291          if (recycle) {
292             if (!truncate_dev(dev)) {
293                Jmsg2(jcr, M_WARNING, 0, _("Truncate error on device \"%s\". ERR=%s\n"), 
294                      dev_name(dev), strerror_dev(dev));
295             }
296          }
297          /* Attempt write to check write permission */
298          if (!write_block_to_dev(jcr, dev, block)) {
299             Jmsg2(jcr, M_ERROR, 0, _("Unable to write device \"%s\". ERR=%s\n"),
300                dev_name(dev), strerror_dev(dev));
301             goto mount_next_vol;
302          }
303          /* 
304           * We do not return the label in the block, because if we are
305           *  running multiple simultaneous jobs, once we release the lock
306           *  some other thread may write his block over the label. So, 
307           *  we simply write it definitively now.
308           */
309 #ifdef needed
310          if (!rewind_dev(dev)) {
311             Jmsg2(jcr, M_ERROR, 0, _("Unable to rewind device %s. ERR=%s\n"),
312                dev_name(dev), strerror_dev(dev));
313             goto mount_next_vol;
314          }
315
316          /* Recreate a correct volume label and return it in the block */
317          write_volume_label_to_block(jcr, dev, block);
318 #endif
319       }
320       /* Set or reset Volume statistics */
321       dev->VolCatInfo.VolCatJobs = 0;
322       dev->VolCatInfo.VolCatFiles = 0;
323       dev->VolCatInfo.VolCatBytes = 1;
324       dev->VolCatInfo.VolCatErrors = 0;
325       dev->VolCatInfo.VolCatBlocks = 0;
326       dev->VolCatInfo.VolCatRBytes = 0;
327       if (recycle) {
328          dev->VolCatInfo.VolCatMounts++;  
329          dev->VolCatInfo.VolCatRecycles++;
330       } else {
331          dev->VolCatInfo.VolCatMounts = 1;
332          dev->VolCatInfo.VolCatRecycles = 0;
333          dev->VolCatInfo.VolCatWrites = 1;
334          dev->VolCatInfo.VolCatReads = 1;
335       }
336       Dmsg0(100, "dir_update_vol_info. Set Append\n");
337       bstrncpy(dev->VolCatInfo.VolCatStatus, "Append", sizeof(dev->VolCatInfo.VolCatStatus));
338       dir_update_volume_info(jcr, dev, 1);  /* indicate doing relabel */
339       if (recycle) {
340          Jmsg(jcr, M_INFO, 0, _("Recycled volume \"%s\" on device \"%s\", all previous data lost.\n"),
341             jcr->VolumeName, dev_name(dev));
342       } else {
343          Jmsg(jcr, M_INFO, 0, _("Wrote label to prelabeled Volume \"%s\" on device \"%s\"\n"),
344             jcr->VolumeName, dev_name(dev));
345       }
346       /*
347        * End writing real Volume label (from pre-labeled tape), or recycling
348        *  the volume.
349        */
350
351    } else {
352       /*
353        * OK, at this point, we have a valid Bacula label, but
354        * we need to position to the end of the volume, since we are
355        * just now putting it into append mode.
356        */
357       Dmsg0(200, "Device previously written, moving to end of data\n");
358       Jmsg(jcr, M_INFO, 0, _("Volume \"%s\" previously written, moving to end of data.\n"),
359          jcr->VolumeName);
360       if (!eod_dev(dev)) {
361          Jmsg(jcr, M_ERROR, 0, _("Unable to position to end of data on device \"%s\". ERR=%s\n"),
362             dev_name(dev), strerror_dev(dev));
363          mark_volume_in_error(jcr, dev);
364          goto mount_next_vol;
365       }
366       /* *****FIXME**** we should do some checking for files too */
367       if (dev_is_tape(dev)) {
368          /*
369           * Check if we are positioned on the tape at the same place
370           * that the database says we should be.
371           */
372          if (dev->VolCatInfo.VolCatFiles == dev_file(dev)) {
373             Jmsg(jcr, M_INFO, 0, _("Ready to append to end of Volume \"%s\" at file=%d.\n"), 
374                  jcr->VolumeName, dev_file(dev));
375          } else {
376             Jmsg(jcr, M_ERROR, 0, _("I canot write on Volume \"%s\" because:\n\
377 The number of files mismatch! Volume=%u Catalog=%u\n"), 
378                  jcr->VolumeName, dev_file(dev), dev->VolCatInfo.VolCatFiles);
379             mark_volume_in_error(jcr, dev);
380             goto mount_next_vol;
381          }
382       }
383       dev->VolCatInfo.VolCatMounts++;      /* Update mounts */
384       Dmsg1(100, "update volinfo mounts=%d\n", dev->VolCatInfo.VolCatMounts);
385       dir_update_volume_info(jcr, dev, 0);
386       /* Return an empty block */
387       empty_block(block);             /* we used it for reading so set for write */
388    }
389    dev->state |= ST_APPEND;
390    Dmsg0(100, "Normal return from read_dev_for_append\n");
391    return 1; 
392 }
393
394 /*
395  * Mark volume in error in catalog 
396  */
397 void mark_volume_in_error(JCR *jcr, DEVICE *dev)
398 {
399    Jmsg(jcr, M_INFO, 0, _("Marking Volume \"%s\" in Error in Catalog.\n"),
400         jcr->VolumeName);
401    bstrncpy(dev->VolCatInfo.VolCatStatus, "Error", sizeof(dev->VolCatInfo.VolCatStatus));
402    Dmsg0(100, "dir_update_vol_info. Set Error.\n");
403    dir_update_volume_info(jcr, dev, 0);
404 }
405
406 /* 
407  * If we are reading, we come here at the end of the tape
408  *  and see if there are more volumes to be mounted.
409  */
410 int mount_next_read_volume(JCR *jcr, DEVICE *dev, DEV_BLOCK *block)
411 {
412    Dmsg2(90, "NumVolumes=%d CurVolume=%d\n", jcr->NumVolumes, jcr->CurVolume);
413    /*
414     * End Of Tape -- mount next Volume (if another specified)
415     */
416    if (jcr->NumVolumes > 1 && jcr->CurVolume < jcr->NumVolumes) {
417       close_dev(dev);
418       dev->state &= ~ST_READ; 
419       if (!acquire_device_for_read(jcr, dev, block)) {
420          Jmsg2(jcr, M_FATAL, 0, "Cannot open Dev=%s, Vol=%s\n", dev_name(dev),
421                jcr->VolumeName);
422          return 0;
423       }
424       return 1;                       /* next volume mounted */
425    }
426    Dmsg0(90, "End of Device reached.\n");
427    return 0;
428 }
429
430 /*
431  * Either because we are going to hang a new volume, or because
432  *  of explicit user request, we release the current volume.
433  */
434 void release_volume(JCR *jcr, DEVICE *dev)
435 {
436    if (jcr->WroteVol) {
437       Jmsg0(jcr, M_ERROR, 0, "Hey!!!!! WroteVol non-zero !!!!!\n");
438    }
439    /* 
440     * First erase all memory of the current volume   
441     */
442    dev->block_num = dev->file = 0;
443    dev->EndBlock = dev->EndFile = 0;
444    memset(&dev->VolCatInfo, 0, sizeof(dev->VolCatInfo));
445    memset(&jcr->VolCatInfo, 0, sizeof(jcr->VolCatInfo));
446    memset(&dev->VolHdr, 0, sizeof(dev->VolHdr));
447    /* Force re-read of label */
448    dev->state &= ~(ST_LABEL|ST_READ|ST_APPEND);
449    jcr->VolumeName[0] = 0;
450
451    if ((dev->state & ST_OPENED) && 
452        (!dev_is_tape(dev) || !dev_cap(dev, CAP_ALWAYSOPEN))) {
453       offline_or_rewind_dev(dev);
454       close_dev(dev);
455    }
456
457    /* If we have not closed the device, then at least rewind the tape */
458    if (dev->state & ST_OPENED) {
459       offline_or_rewind_dev(dev);
460    }
461 }