]> git.sur5r.net Git - bacula/bacula/blob - bacula/src/stored/autochanger.c
342619ad304eaa00b6c9e497f8172dc27c825109
[bacula/bacula] / bacula / src / stored / autochanger.c
1 /*
2  *
3  *  Routines for handling the autochanger.
4  *
5  *   Kern Sibbald, August MMII
6  *                            
7  *   Version $Id$
8  */
9 /*
10    Copyright (C) 2000-2004 Kern Sibbald and John Walker
11
12    This program is free software; you can redistribute it and/or
13    modify it under the terms of the GNU General Public License as
14    published by the Free Software Foundation; either version 2 of
15    the License, or (at your option) any later version.
16
17    This program is distributed in the hope that it will be useful,
18    but WITHOUT ANY WARRANTY; without even the implied warranty of
19    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
20    General Public License for more details.
21
22    You should have received a copy of the GNU General Public
23    License along with this program; if not, write to the Free
24    Software Foundation, Inc., 59 Temple Place - Suite 330, Boston,
25    MA 02111-1307, USA.
26
27  */
28
29 #include "bacula.h"                   /* pull in global headers */
30 #include "stored.h"                   /* pull in Storage Deamon headers */
31
32 /* Forward referenced functions */
33 char *edit_device_codes(JCR *jcr, char *omsg, const char *imsg, const char *cmd);
34 static int get_autochanger_loaded_slot(JCR *jcr);
35
36
37 /*
38  * Called here to do an autoload using the autochanger, if
39  *  configured, and if a Slot has been defined for this Volume.
40  *  On success this routine loads the indicated tape, but the
41  *  label is not read, so it must be verified.
42  *
43  *  Note if dir is not NULL, it is the console requesting the 
44  *   autoload for labeling, so we respond directly to the
45  *   dir bsock.
46  *
47  *  Returns: 1 on success
48  *           0 on failure (no changer available) 
49  *          -1 on error on autochanger
50  */
51 int autoload_device(DCR *dcr, int writing, BSOCK *dir)
52 {
53    JCR *jcr = dcr->jcr;
54    DEVICE *dev = dcr->dev;
55    int slot = jcr->VolCatInfo.Slot;
56    int drive = jcr->device->drive_index;
57    int rtn_stat = -1;                 /* error status */
58      
59    /*
60     * Handle autoloaders here.  If we cannot autoload it, we
61     *  will return FALSE to ask the sysop.
62     */
63    if (writing && dev_cap(dev, CAP_AUTOCHANGER) && slot <= 0) {
64       if (dir) {
65          return 0;                    /* For user, bail out right now */
66       }
67       if (dir_find_next_appendable_volume(dcr)) {
68          slot = jcr->VolCatInfo.Slot; 
69       } else {
70          slot = 0;
71       }
72    }
73    Dmsg1(400, "Want changer slot=%d\n", slot);
74
75    if (slot > 0 && jcr->device->changer_name && jcr->device->changer_command) {
76       uint32_t timeout = jcr->device->max_changer_wait;
77       POOLMEM *changer;
78       int loaded, status;     
79
80       changer = get_pool_memory(PM_FNAME);
81
82       loaded = get_autochanger_loaded_slot(jcr);
83
84       /* If tape we want is not loaded, load it. */
85       if (loaded != slot) { 
86          offline_or_rewind_dev(dev);
87          /* We are going to load a new tape, so close the device */
88          force_close_dev(dev);
89          if (loaded != 0 && loaded != -1) {        /* must unload drive */
90             Dmsg0(400, "Doing changer unload.\n");
91             Jmsg(jcr, M_INFO, 0, 
92                  _("3303 Issuing autochanger \"unload slot %d, drive %d\" command.\n"),
93                  loaded, drive);
94             jcr->VolCatInfo.Slot = loaded;   /* slot to be unloaded */
95             changer = edit_device_codes(jcr, changer, 
96                         jcr->device->changer_command, "unload");
97             status = run_program(changer, timeout, NULL);
98             if (status != 0) {
99                berrno be;
100                be.set_errno(status);
101                Jmsg(jcr, M_INFO, 0, _("3992 Bad autochanger \"unload slot %d, drive %d\": ERR=%s.\n"),
102                     slot, drive, be.strerror());
103             }
104
105             Dmsg1(400, "unload status=%d\n", status);
106          }
107          /*
108           * Load the desired cassette    
109           */
110          Dmsg1(400, "Doing changer load slot %d\n", slot);
111          Jmsg(jcr, M_INFO, 0, 
112               _("3304 Issuing autochanger \"load slot %d, drive %d\" command.\n"), 
113               slot, drive);
114          jcr->VolCatInfo.Slot = slot;    /* slot to be loaded */
115          changer = edit_device_codes(jcr, changer, 
116                       jcr->device->changer_command, "load");
117          status = run_program(changer, timeout, NULL);
118          if (status == 0) {
119             Jmsg(jcr, M_INFO, 0, _("3305 Autochanger \"load slot %d, drive %d\", status is OK.\n"),
120                     slot, drive);
121          } else {
122            berrno be;
123            be.set_errno(status);
124             Jmsg(jcr, M_INFO, 0, _("3992 Bad autochanger \"load slot %d, drive %d\": ERR=%s.\n"),
125                     slot, drive, be.strerror());
126          }
127          Dmsg2(400, "load slot %d status=%d\n", slot, status);
128       } else { 
129          status = 0;                  /* we got what we want */
130       }
131       free_pool_memory(changer);
132       Dmsg1(400, "After changer, status=%d\n", status);
133       if (status == 0) {              /* did we succeed? */
134          rtn_stat = 1;                /* tape loaded by changer */
135       }
136    } else {
137       rtn_stat = 0;                   /* no changer found */
138    }
139    return rtn_stat;
140 }
141
142 static int get_autochanger_loaded_slot(JCR *jcr)
143 {
144    POOLMEM *changer, *results;
145    int status, loaded;
146    uint32_t timeout = jcr->device->max_changer_wait;
147    int drive = jcr->device->drive_index;
148
149    results = get_pool_memory(PM_MESSAGE);
150    changer = get_pool_memory(PM_FNAME);
151
152    /* Find out what is loaded, zero means device is unloaded */
153    Jmsg(jcr, M_INFO, 0, _("3301 Issuing autochanger \"loaded drive %d\" command.\n"),
154         drive);
155    changer = edit_device_codes(jcr, changer, jcr->device->changer_command, 
156                 "loaded");
157    status = run_program(changer, timeout, results);
158    Dmsg3(100, "run_prog: %s stat=%d result=%s\n", changer, status, results);
159    if (status == 0) {
160       loaded = atoi(results);
161       if (loaded > 0) {
162          Jmsg(jcr, M_INFO, 0, _("3302 Autochanger \"loaded drive %d\", result is Slot %d.\n"),
163               drive, loaded);
164       } else {
165          Jmsg(jcr, M_INFO, 0, _("3302 Autochanger \"loaded drive %d\", result: nothing loaded.\n"),
166               drive);
167       }
168    } else {
169       berrno be;
170       be.set_errno(status);
171       Jmsg(jcr, M_INFO, 0, _("3991 Bad autochanger \"loaded drive %d\" command: ERR=%s.\n"), 
172            drive, be.strerror());
173       loaded = -1;              /* force unload */
174    }
175    free_pool_memory(changer);
176    free_pool_memory(results);
177    return loaded;
178 }
179
180 /*
181  * The Volume is not in the correct slot, so mark this 
182  *   Volume as not being in the Changer.
183  */
184 void invalid_slot_in_catalog(DCR *dcr)
185 {
186    JCR *jcr = dcr->jcr;
187    DEVICE *dev = dcr->dev;
188    Jmsg(jcr, M_ERROR, 0, _("Autochanger Volume \"%s\" not found in slot %d.\n"
189 "    Setting slot to zero in catalog.\n"),
190         jcr->VolCatInfo.VolCatName, jcr->VolCatInfo.Slot);
191    jcr->VolCatInfo.InChanger = false;
192    dev->VolCatInfo.InChanger = false;
193    Dmsg0(100, "update vol info in mount\n");
194    dir_update_volume_info(dcr, true);  /* set new status */
195 }
196
197 /*
198  * List the Volumes that are in the autoloader possibly
199  *   with their barcodes.
200  *   We assume that it is always the Console that is calling us.
201  */
202 bool autochanger_list(DCR *dcr, BSOCK *dir)
203 {
204    DEVICE *dev = dcr->dev;
205    JCR *jcr = dcr->jcr;
206    uint32_t timeout = jcr->device->max_changer_wait;
207    POOLMEM *changer;
208    BPIPE *bpipe;
209    int slot, loaded;
210    int len = sizeof_pool_memory(dir->msg) - 1;
211
212    if (!dev_cap(dev, CAP_AUTOCHANGER) || !jcr->device->changer_name ||
213        !jcr->device->changer_command) {
214       bnet_fsend(dir, _("3993 Not a autochanger device.\n"));
215       return false;
216    }
217
218    changer = get_pool_memory(PM_FNAME);
219    offline_or_rewind_dev(dev);
220    /* We are going to load a new tape, so close the device */
221    force_close_dev(dev);
222
223    /* First unload any tape */
224    loaded = get_autochanger_loaded_slot(jcr);
225    if (loaded > 0) {
226       bnet_fsend(dir, _("3305 Issuing autochanger \"unload slot %d\" command.\n"), loaded);
227       slot = jcr->VolCatInfo.Slot; 
228       jcr->VolCatInfo.Slot = loaded;
229       changer = edit_device_codes(jcr, changer, jcr->device->changer_command, "unload");
230       int stat = run_program(changer, timeout, NULL);
231       if (stat != 0) {
232          berrno be;
233          be.set_errno(stat);
234          Jmsg(jcr, M_INFO, 0, _("3995 Bad autochanger \"unload slot %d\" command: ERR=%s.\n"), 
235               loaded, be.strerror());
236       }
237       jcr->VolCatInfo.Slot = slot;
238    }
239
240    /* Now list slots occupied */
241    changer = edit_device_codes(jcr, changer, jcr->device->changer_command, "list");
242    bnet_fsend(dir, _("3306 Issuing autochanger \"list\" command.\n"));
243    bpipe = open_bpipe(changer, timeout, "r");
244    if (!bpipe) {
245       bnet_fsend(dir, _("3993 Open bpipe failed.\n"));
246       free_pool_memory(changer);
247       return false;
248    }
249    /* Get output from changer */
250    while (fgets(dir->msg, len, bpipe->rfd)) { 
251       dir->msglen = strlen(dir->msg);
252       bnet_send(dir);
253    }
254    int stat = close_bpipe(bpipe);
255    if (stat != 0) {
256       berrno be;
257       be.set_errno(stat);
258       bnet_fsend(dir, "Autochanger error: ERR=%s\n", be.strerror());
259    }
260    bnet_sig(dir, BNET_EOD);
261
262    free_pool_memory(changer);
263    return true;
264 }
265
266
267 /*
268  * Edit codes into ChangerCommand
269  *  %% = %
270  *  %a = archive device name
271  *  %c = changer device name
272  *  %d = changer drive index        
273  *  %f = Client's name
274  *  %j = Job name
275  *  %o = command
276  *  %s = Slot base 0
277  *  %S = Slot base 1
278  *  %v = Volume name
279  *
280  *
281  *  omsg = edited output message
282  *  imsg = input string containing edit codes (%x)
283  *  cmd = command string (load, unload, ...) 
284  *
285  */
286 char *edit_device_codes(JCR *jcr, char *omsg, const char *imsg, const char *cmd) 
287 {
288    const char *p;
289    const char *str;
290    char add[20];
291
292    *omsg = 0;
293    Dmsg1(400, "edit_device_codes: %s\n", imsg);
294    for (p=imsg; *p; p++) {
295       if (*p == '%') {
296          switch (*++p) {
297          case '%':
298             str = "%";
299             break;
300          case 'a':
301             str = dev_name(jcr->device->dev);
302             break;
303          case 'c':
304             str = NPRT(jcr->device->changer_name);
305             break;
306          case 'd':
307             sprintf(add, "%d", jcr->device->dev->drive_index);
308             str = add;
309             break;
310          case 'o':
311             str = NPRT(cmd);
312             break;
313          case 's':
314             sprintf(add, "%d", jcr->VolCatInfo.Slot - 1);
315             str = add;
316             break;
317          case 'S':
318             sprintf(add, "%d", jcr->VolCatInfo.Slot);
319             str = add;
320             break;
321          case 'j':                    /* Job name */
322             str = jcr->Job;
323             break;
324          case 'v':
325             str = NPRT(jcr->VolumeName);
326             break;
327          case 'f':
328             str = NPRT(jcr->client_name);
329             break;
330
331          default:
332             add[0] = '%';
333             add[1] = *p;
334             add[2] = 0;
335             str = add;
336             break;
337          }
338       } else {
339          add[0] = *p;
340          add[1] = 0;
341          str = add;
342       }
343       Dmsg1(400, "add_str %s\n", str);
344       pm_strcat(&omsg, (char *)str);
345       Dmsg1(400, "omsg=%s\n", omsg);
346    }
347    return omsg;
348 }