]> git.sur5r.net Git - bacula/bacula/blob - bacula/src/stored/mount.c
First cut 1.27 see kes23Oct02
[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, 2001, 2002 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 /* Forward referenced functions */
34 static char *edit_device_codes(JCR *jcr, char *omsg, char *imsg, char *cmd);
35
36
37 /*
38  * If release is set, we rewind the current volume, 
39  * which we no longer want, and ask the user (console) 
40  * to mount the next volume.
41  *
42  *  Continue trying until we get it, and then ensure
43  *  that we can write on it.
44  *
45  * This routine returns a 0 only if it is REALLY
46  *  impossible to get the requested Volume.
47  */
48 int mount_next_write_volume(JCR *jcr, DEVICE *dev, DEV_BLOCK *block, int release)
49 {
50    int recycle, ask, retry = 0, autochanger;
51
52    Dmsg0(100, "Enter mount_next_volume()\n");
53
54 mount_next_vol:
55    if (retry++ > 5) {
56       Jmsg(jcr, M_FATAL, 0, _("Too many errors on device %s.\n"), dev_name(dev));
57       return 0;
58    }
59    if (job_cancelled(jcr)) {
60       Jmsg(jcr, M_FATAL, 0, _("Job cancelled.\n"));
61       return 0;
62    }
63    recycle = ask = autochanger = 0;
64    if (release) {
65       Dmsg0(100, "mount_next_volume release=1\n");
66       /* 
67        * First erase all memory of the current volume   
68        */
69       dev->block_num = 0;
70       dev->file = 0;
71       dev->LastBlockNumWritten = 0;
72       memset(&dev->VolCatInfo, 0, sizeof(dev->VolCatInfo));
73       memset(&jcr->VolCatInfo, 0, sizeof(jcr->VolCatInfo));
74       memset(&dev->VolHdr, 0, sizeof(dev->VolHdr));
75       dev->state &= ~ST_LABEL;        /* label not yet read */
76       jcr->VolumeName[0] = 0;
77
78       if (!dev_is_tape(dev) || !(dev->capabilities & CAP_ALWAYSOPEN)) {
79          if (dev->capabilities & CAP_OFFLINEUNMOUNT) {
80             offline_dev(dev);
81          }
82          close_dev(dev);
83       }
84
85       /* If we have not closed the device, then at least rewind the tape */
86       if (dev->state & ST_OPENED) {
87          if (dev->capabilities & CAP_OFFLINEUNMOUNT) {
88             offline_dev(dev);
89          }
90          if (!rewind_dev(dev)) {
91             Jmsg2(jcr, M_WARNING, 0, _("Rewind error on device %s. ERR=%s\n"), 
92                   dev_name(dev), strerror_dev(dev));
93          }
94       }
95       ask = 1;                        /* ask operator to mount tape */
96    }
97
98    /* 
99     * Get Director's idea of what tape we should have mounted. 
100     */
101    if (!dir_find_next_appendable_volume(jcr)) {
102       ask = 1;                     /* we must ask */
103       Dmsg0(100, "did not find next volume. Must ask.\n");
104    }
105    Dmsg2(100, "After find_next_append. Vol=%s Slot=%d\n",
106       jcr->VolCatInfo.VolCatName, jcr->VolCatInfo.Slot);
107    release = 1;                       /* release if we "recurse" */
108
109    /* 
110     * Get next volume and ready it for append
111     * This code ensures that the device is ready for
112     * writing. We start from the assumption that there
113     * may not be a tape mounted. 
114     *
115     * If the device is a file, we create the output
116     * file. If it is a tape, we check the volume name
117     * and move the tape to the end of data.
118     *
119     * It assumes that the device is not already in use!
120     *
121     */
122
123    Dmsg0(100, "Enter ready_dev_for_append\n");
124
125    dev->state &= ~(ST_APPEND|ST_READ|ST_EOT|ST_WEOT|ST_EOF);
126
127    jcr->VolFirstFile = jcr->JobFiles; /* first update of Vol FileIndex */
128    for ( ;; ) {
129       autochanger = autoload_device(jcr, dev, 1, NULL);
130       if (autochanger) {
131          ask = 0;                     /* if autochange no need to ask sysop */
132       }
133
134       if (ask && !dir_ask_sysop_to_mount_next_volume(jcr, dev)) {
135          return 0;              /* error return */
136       }
137       Dmsg1(100, "want vol=%s\n", jcr->VolumeName);
138
139       /* Open device */
140       for ( ; !(dev->state & ST_OPENED); ) {
141           if (open_dev(dev, jcr->VolCatInfo.VolCatName, READ_WRITE) < 0) {
142              if (dev->dev_errno == EAGAIN || dev->dev_errno == EBUSY) {
143                 sleep(30);
144              }
145              Jmsg2(jcr, M_FATAL, 0, _("Unable to open device %s. ERR=%s\n"), 
146                 dev_name(dev), strerror_dev(dev));
147              return 0;
148           }
149       }
150
151       /*
152        * Now make sure we have the right tape mounted
153        */
154 read_volume:
155       switch (read_dev_volume_label(jcr, dev, block)) {
156          case VOL_OK:
157             Dmsg1(500, "Vol OK name=%s\n", jcr->VolumeName);
158             memcpy(&dev->VolCatInfo, &jcr->VolCatInfo, sizeof(jcr->VolCatInfo));
159             if (strcmp(dev->VolCatInfo.VolCatStatus, "Recycle") == 0) {
160                recycle = 1;
161             }
162             break;                    /* got it */
163          case VOL_NAME_ERROR:
164             Dmsg1(500, "Vol NAME Error Name=%s\n", jcr->VolumeName);
165             /* Check if this is a valid Volume in the pool */
166             strcpy(jcr->VolumeName, dev->VolHdr.VolName);
167             if (!dir_get_volume_info(jcr, 1)) {
168                goto mount_next_vol;
169             }
170             Dmsg1(200, "want new name=%s\n", jcr->VolumeName);
171             memcpy(&dev->VolCatInfo, &jcr->VolCatInfo, sizeof(jcr->VolCatInfo));
172             break;
173
174          case VOL_NO_LABEL:
175          case VOL_IO_ERROR:
176             Dmsg1(500, "Vol NO_LABEL or IO_ERROR name=%s\n", jcr->VolumeName);
177             /* If permitted, create a label */
178             if (dev->capabilities & CAP_LABEL) {
179                Dmsg0(190, "Create volume label\n");
180                if (!write_volume_label_to_dev(jcr, (DEVRES *)dev->device, jcr->VolumeName,
181                       jcr->pool_name)) {
182                   goto mount_next_vol;
183                }
184                Jmsg(jcr, M_INFO, 0, _("Created Volume label %s on device %s.\n"),
185                   jcr->VolumeName, dev_name(dev));
186                goto read_volume;      /* read label we just wrote */
187             } 
188             /* NOTE! Fall-through wanted. */
189          default:
190             /* Send error message */
191             Jmsg1(jcr, M_WARNING, 0, "%s", jcr->errmsg);                         
192             if (autochanger) {
193                Jmsg(jcr, M_ERROR, 0, _("Autochanger Volume %s not found in slot %d.\n\
194     Setting slot to zero in catalog.\n"),
195                   jcr->VolumeName, jcr->VolCatInfo.Slot);
196                jcr->VolCatInfo.Slot = 0; /* invalidate slot */
197                dir_update_volume_info(jcr, &jcr->VolCatInfo, 1);  /* set slot */
198             }
199             goto mount_next_vol;
200       }
201       break;
202    }
203
204    /* 
205     * See if we have a fresh tape or tape with data.
206     *
207     * Note, if the LabelType is PRE_LABEL, it was labeled
208     *  but never written. If so, rewrite the label but set as
209     *  VOL_LABEL.  We rewind and return the label (reconstructed)
210     *  in the block so that in the case of a new tape, data can
211     *  be appended just after the block label.  If we are writing
212     *  an second volume, the calling routine will write the label
213     *  before writing the overflow block.
214     *
215     *  If the tape is marked as Recycle, we rewrite the label.
216     */
217    if (dev->VolHdr.LabelType == PRE_LABEL || recycle) {
218       Dmsg1(190, "ready_for_append found freshly labeled volume. dev=%x\n", dev);
219       dev->VolHdr.LabelType = VOL_LABEL; /* set Volume label */
220       write_volume_label_to_block(jcr, dev, block);
221       /*
222        * Write the block now to ensure we have write permission.
223        *  It is better to find out now rather than later.
224        */
225       dev->VolCatInfo.VolCatBytes = 0;
226       if (!rewind_dev(dev)) {
227          Jmsg2(jcr, M_WARNING, 0, _("Rewind error on device %s. ERR=%s\n"), 
228                dev_name(dev), strerror_dev(dev));
229       }
230       if (recycle) {
231          if (!truncate_dev(dev)) {
232             Jmsg2(jcr, M_WARNING, 0, _("Truncate error on device %s. ERR=%s\n"), 
233                   dev_name(dev), strerror_dev(dev));
234          }
235       }
236       if (!write_block_to_dev(dev, block)) {
237          Jmsg2(jcr, M_ERROR, 0, _("Unable to write device %s. ERR=%s\n"),
238             dev_name(dev), strerror_dev(dev));
239          goto mount_next_vol;
240       }
241       if (!rewind_dev(dev)) {
242          Jmsg2(jcr, M_ERROR, 0, _("Unable to rewind device %s. ERR=%s\n"),
243             dev_name(dev), strerror_dev(dev));
244          goto mount_next_vol;
245       }
246       /* Recreate a correct volume label and return it in the block */
247       write_volume_label_to_block(jcr, dev, block);
248       dev->VolCatInfo.VolCatJobs = 1;
249       dev->VolCatInfo.VolCatFiles = 1;
250       dev->VolCatInfo.VolCatErrors = 0;
251       dev->VolCatInfo.VolCatBlocks = 1;
252       if (recycle) {
253          dev->VolCatInfo.VolCatMounts++;  
254          dev->VolCatInfo.VolCatRecycles++;
255       } else {
256          dev->VolCatInfo.VolCatMounts = 1;
257          dev->VolCatInfo.VolCatRecycles = 0;
258          dev->VolCatInfo.VolCatWrites = 1;
259          dev->VolCatInfo.VolCatReads = 1;
260       }
261       strcpy(dev->VolCatInfo.VolCatStatus, "Append");
262       Dmsg0(100, "dir_update_vol_info. Set Append\n");
263       dir_update_volume_info(jcr, &dev->VolCatInfo, 1);  /* indicate doing relabel */
264       if (recycle) {
265          Jmsg(jcr, M_INFO, 0, _("Recycled volume %s on device %s, all previous data lost.\n"),
266             jcr->VolumeName, dev_name(dev));
267       } else {
268          Jmsg(jcr, M_INFO, 0, _("Wrote label to prelabeled Volume %s on device %s\n"),
269             jcr->VolumeName, dev_name(dev));
270       }
271
272    } else {
273       /*
274        * OK, at this point, we have a valid Bacula label, but
275        * we need to position to the end of the volume, since we are
276        * just now putting it into append mode.
277        */
278       Dmsg0(200, "Device previously written, moving to end of data\n");
279       Jmsg(jcr, M_INFO, 0, _("Volume %s previously written, moving to end of data.\n"),
280          jcr->VolumeName);
281       if (!eod_dev(dev)) {
282          Jmsg(jcr, M_ERROR, 0, _("Unable to position to end of data %s. ERR=%s\n"),
283             dev_name(dev), strerror_dev(dev));
284          Jmsg(jcr, M_INFO, 0, _("Marking Volume %s in Error in Catalog.\n"),
285             jcr->VolumeName);
286          strcpy(dev->VolCatInfo.VolCatStatus, "Error");
287          Dmsg0(100, "dir_update_vol_info. Set Error.\n");
288          dir_update_volume_info(jcr, &dev->VolCatInfo, 0);
289          goto mount_next_vol;
290       }
291       /* *****FIXME**** we should do some checking for files too */
292       if (dev_is_tape(dev)) {
293          Jmsg(jcr, M_INFO, 0, _("Ready to append to end of Volume at file=%d.\n"), dev_file(dev));
294          /*
295           * Check if we are positioned on the tape at the same place
296           * that the database says we should be.
297           */
298          if (dev->VolCatInfo.VolCatFiles != dev_file(dev) + 1) {
299             /* ****FIXME**** this should refuse to write on tape */
300             Jmsg(jcr, M_ERROR, 0, _("Hey! Num files mismatch! Volume=%d Catalog=%d\n"), 
301                dev_file(dev)+1, dev->VolCatInfo.VolCatFiles);
302          }
303       }
304       /* Update Volume Info -- will be written at end of Job */
305       dev->VolCatInfo.VolCatMounts++;      /* Update mounts */
306       dev->VolCatInfo.VolCatJobs++;
307       /* Return an empty block */
308       empty_block(block);             /* we used it for reading so set for write */
309    }
310    dev->state |= ST_APPEND;
311    Dmsg0(100, "Normal return from read_dev_for_append\n");
312    return 1; 
313 }
314
315
316 int mount_next_read_volume(JCR *jcr, DEVICE *dev, DEV_BLOCK *block)
317 {
318    Dmsg2(90, "NumVolumes=%d CurVolume=%d\n", jcr->NumVolumes, jcr->CurVolume);
319    /*
320     * End Of Tape -- mount next Volume (if another specified)
321     */
322    if (jcr->NumVolumes > 1 && jcr->CurVolume < jcr->NumVolumes) {
323       VOL_LIST *vol = jcr->VolList;
324       /* Find next Volume */
325       jcr->CurVolume++;
326       for (int i=1; i<jcr->CurVolume; i++) {
327          vol = vol->next;
328       }
329       pm_strcpy(&jcr->VolumeName, vol->VolumeName);
330       Dmsg1(400, "There is another volume %s.\n", jcr->VolumeName);
331
332       close_dev(dev);
333       dev->state &= ~ST_READ; 
334       if (!acquire_device_for_read(jcr, dev, block)) {
335          Emsg2(M_FATAL, 0, "Cannot open Dev=%s, Vol=%s\n", dev_name(dev),
336                jcr->VolumeName);
337          return 0;
338       }
339       return 1;                       /* next volume mounted */
340    }
341    Dmsg0(90, "End of Device reached.\n");
342    return 0;
343 }
344
345 /*
346  * Called here to do an autoload using the autochanger, if
347  *  configured, and if a Slot has been defined for this Volume.
348  *  On success this routine loads the indicated tape, but the
349  *  label is not read, so it must be verified.
350  *
351  *  Note if dir is not NULL, it is the console requesting the 
352  *   autoload for labeling, so we respond directly to the
353  *   dir bsock.
354  *
355  *  Returns: 1 on success
356  *           0 on failure
357  */
358 int autoload_device(JCR *jcr, DEVICE *dev, int writing, BSOCK *dir)
359 {
360    int slot = jcr->VolCatInfo.Slot;
361    int rtn_stat = 0;
362      
363    /*
364     * Handle autoloaders here.  If we cannot autoload it, we
365     *  will return FALSE to ask the sysop.
366     */
367    if (writing && dev->capabilities && CAP_AUTOCHANGER && slot <= 0) {
368       if (dir) {
369          return 0;                    /* For user, bail out right now */
370       }
371       if (dir_find_next_appendable_volume(jcr)) {
372          slot = jcr->VolCatInfo.Slot; 
373       }
374    }
375    Dmsg1(100, "Want changer slot=%d\n", slot);
376
377    if (slot > 0 && jcr->device->changer_name && jcr->device->changer_command) {
378       uint32_t timeout = jcr->device->max_changer_wait;
379       POOLMEM *changer, *results;
380       int status, loaded;
381
382       results = get_pool_memory(PM_MESSAGE);
383       changer = get_pool_memory(PM_FNAME);
384
385       /* Find out what is loaded, zero means device is unloaded */
386       changer = edit_device_codes(jcr, changer, jcr->device->changer_command, 
387                    "loaded");
388       status = run_program(changer, timeout, results);
389       if (status == 0) {
390          loaded = atoi(results);
391       } else {
392          loaded = -1;              /* force unload */
393       }
394       Dmsg1(100, "loaded=%s\n", results);
395
396       /* If bad status or tape we want is not loaded, load it. */
397       if (status != 0 || loaded != slot) { 
398          if (dev->capabilities & CAP_OFFLINEUNMOUNT) {
399             offline_dev(dev);
400          }
401          /* We are going to load a new tape, so close the device */
402          force_close_dev(dev);
403          if (loaded != 0) {        /* must unload drive */
404             Dmsg0(100, "Doing changer unload.\n");
405             if (dir) {
406                bnet_fsend(dir, _("3902 Issuing autochanger \"unload\" command.\n"));
407             } else {
408                Jmsg(jcr, M_INFO, 0, _("Issuing autochanger \"unload\" command.\n"));
409             }
410             changer = edit_device_codes(jcr, changer, 
411                         jcr->device->changer_command, "unload");
412             status = run_program(changer, timeout, NULL);
413             Dmsg1(100, "unload status=%d\n", status);
414          }
415          /*
416           * Load the desired cassette    
417           */
418          Dmsg1(100, "Doing changer load slot %d\n", slot);
419          if (dir) {
420             bnet_fsend(dir, _("3903 Issuing autochanger \"load slot %d\" command.\n"),
421                slot);
422          } else {
423             Jmsg(jcr, M_INFO, 0, _("Issuing autochanger \"load slot %d\" command.\n"),
424                slot);
425          }
426          changer = edit_device_codes(jcr, changer, 
427                       jcr->device->changer_command, "load");
428          status = run_program(changer, timeout, NULL);
429          Dmsg2(100, "load slot %d status=%d\n", slot, status);
430       }
431       free_pool_memory(changer);
432       free_pool_memory(results);
433       Dmsg1(100, "After changer, status=%d\n", status);
434       if (status == 0) {           /* did we succeed? */
435          rtn_stat = 1;             /* tape loaded by changer */
436       }
437    }
438    return rtn_stat;
439 }
440
441
442
443 /*
444  * Edit codes into ChangerCommand
445  *  %% = %
446  *  %a = archive device name
447  *  %c = changer device name
448  *  %f = Client's name
449  *  %j = Job name
450  *  %o = command
451  *  %s = Slot base 0
452  *  %S = Slot base 1
453  *  %v = Volume name
454  *
455  *
456  *  omsg = edited output message
457  *  imsg = input string containing edit codes (%x)
458  *  cmd = command string (load, unload, ...) 
459  *
460  */
461 static char *edit_device_codes(JCR *jcr, char *omsg, char *imsg, char *cmd) 
462 {
463    char *p;
464    const char *str;
465    char add[20];
466
467    *omsg = 0;
468    Dmsg1(200, "edit_device_codes: %s\n", imsg);
469    for (p=imsg; *p; p++) {
470       if (*p == '%') {
471          switch (*++p) {
472          case '%':
473             str = "%";
474             break;
475          case 'a':
476             str = jcr->device->dev->dev_name;
477             break;
478          case 'c':
479             str = NPRT(jcr->device->changer_name);
480             break;
481          case 'o':
482             str = NPRT(cmd);
483             break;
484          case 's':
485             sprintf(add, "%d", jcr->VolCatInfo.Slot - 1);
486             str = add;
487             break;
488          case 'S':
489             sprintf(add, "%d", jcr->VolCatInfo.Slot);
490             str = add;
491             break;
492          case 'j':                    /* Job name */
493             str = jcr->Job;
494             break;
495          case 'v':
496             str = NPRT(jcr->VolumeName);
497             break;
498          case 'f':
499             str = NPRT(jcr->client_name);
500             break;
501
502          default:
503             add[0] = '%';
504             add[1] = *p;
505             add[2] = 0;
506             str = add;
507             break;
508          }
509       } else {
510          add[0] = *p;
511          add[1] = 0;
512          str = add;
513       }
514       Dmsg1(200, "add_str %s\n", str);
515       pm_strcat(&omsg, (char *)str);
516       Dmsg1(200, "omsg=%s\n", omsg);
517    }
518    return omsg;
519 }