]> git.sur5r.net Git - bacula/bacula/blob - bacula/src/stored/autochanger.c
d73c564092c8c7cfa69da1e1f0cf824399207000
[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             Dmsg1(400, "unload status=%d\n", status);
99          }
100          /*
101           * Load the desired cassette    
102           */
103          Dmsg1(400, "Doing changer load slot %d\n", slot);
104          Jmsg(jcr, M_INFO, 0, 
105               _("3304 Issuing autochanger \"load slot %d, drive %d\" command.\n"), 
106               slot, drive);
107          jcr->VolCatInfo.Slot = slot;    /* slot to be loaded */
108          changer = edit_device_codes(jcr, changer, 
109                       jcr->device->changer_command, "load");
110          status = run_program(changer, timeout, NULL);
111          if (status == 0) {
112             Jmsg(jcr, M_INFO, 0, _("3305 Autochanger \"load slot %d, drive %d\", status is OK.\n"),
113                     slot, drive);
114          } else {
115             Jmsg(jcr, M_INFO, 0, _("3992 Bad autochanger \"load slot %d, drive %d\", status=%d.\n"),
116                     slot, drive, status);
117          }
118          Dmsg2(400, "load slot %d status=%d\n", slot, status);
119       } else { 
120          status = 0;                  /* we got what we want */
121       }
122       free_pool_memory(changer);
123       Dmsg1(400, "After changer, status=%d\n", status);
124       if (status == 0) {              /* did we succeed? */
125          rtn_stat = 1;                /* tape loaded by changer */
126       }
127    } else {
128       rtn_stat = 0;                   /* no changer found */
129    }
130    return rtn_stat;
131 }
132
133 static int get_autochanger_loaded_slot(JCR *jcr)
134 {
135    POOLMEM *changer, *results;
136    int status, loaded;
137    uint32_t timeout = jcr->device->max_changer_wait;
138    int drive = jcr->device->drive_index;
139
140    results = get_pool_memory(PM_MESSAGE);
141    changer = get_pool_memory(PM_FNAME);
142
143    /* Find out what is loaded, zero means device is unloaded */
144    Jmsg(jcr, M_INFO, 0, _("3301 Issuing autochanger \"loaded drive %d\" command.\n"),
145         drive);
146    changer = edit_device_codes(jcr, changer, jcr->device->changer_command, 
147                 "loaded");
148    status = run_program(changer, timeout, results);
149    Dmsg3(100, "run_prog: %s stat=%d result=%s\n", changer, status, results);
150    if (status == 0) {
151       loaded = atoi(results);
152       if (loaded > 0) {
153          Jmsg(jcr, M_INFO, 0, _("3302 Autochanger \"loaded drive %d\", result is Slot %d.\n"),
154               drive, loaded);
155       } else {
156          Jmsg(jcr, M_INFO, 0, _("3302 Autochanger \"loaded drive %d\", result: nothing loaded.\n"),
157               drive);
158       }
159    } else {
160       Jmsg(jcr, M_INFO, 0, _("3991 Bad autochanger \"loaded drive %d\" command, status=%d.\n"), 
161            drive, status);
162       loaded = -1;              /* force unload */
163    }
164    free_pool_memory(changer);
165    free_pool_memory(results);
166    return loaded;
167 }
168
169 /*
170  * The Volume is not in the correct slot, so mark this 
171  *   Volume as not being in the Changer.
172  */
173 void invalid_slot_in_catalog(DCR *dcr)
174 {
175    JCR *jcr = dcr->jcr;
176    DEVICE *dev = dcr->dev;
177    Jmsg(jcr, M_ERROR, 0, _("Autochanger Volume \"%s\" not found in slot %d.\n"
178 "    Setting slot to zero in catalog.\n"),
179         jcr->VolCatInfo.VolCatName, jcr->VolCatInfo.Slot);
180    jcr->VolCatInfo.InChanger = false;
181    dev->VolCatInfo.InChanger = false;
182    Dmsg0(100, "update vol info in mount\n");
183    dir_update_volume_info(dcr, true);  /* set new status */
184 }
185
186 /*
187  * List the Volumes that are in the autoloader possibly
188  *   with their barcodes.
189  *   We assume that it is always the Console that is calling us.
190  */
191 bool autochanger_list(DCR *dcr, BSOCK *dir)
192 {
193    DEVICE *dev = dcr->dev;
194    JCR *jcr = dcr->jcr;
195    uint32_t timeout = jcr->device->max_changer_wait;
196    POOLMEM *changer;
197    BPIPE *bpipe;
198    int slot, loaded;
199    int len = sizeof_pool_memory(dir->msg) - 1;
200
201    if (!dev_cap(dev, CAP_AUTOCHANGER) || !jcr->device->changer_name ||
202        !jcr->device->changer_command) {
203       bnet_fsend(dir, _("3993 Not a autochanger device.\n"));
204       return false;
205    }
206
207    changer = get_pool_memory(PM_FNAME);
208    offline_or_rewind_dev(dev);
209    /* We are going to load a new tape, so close the device */
210    force_close_dev(dev);
211
212    /* First unload any tape */
213    loaded = get_autochanger_loaded_slot(jcr);
214    if (loaded > 0) {
215       bnet_fsend(dir, _("3305 Issuing autochanger \"unload slot %d\" command.\n"), loaded);
216       slot = jcr->VolCatInfo.Slot; 
217       jcr->VolCatInfo.Slot = loaded;
218       changer = edit_device_codes(jcr, changer, jcr->device->changer_command, "unload");
219       run_program(changer, timeout, NULL);
220       jcr->VolCatInfo.Slot = slot;
221    }
222
223    /* Now list slots occupied */
224    changer = edit_device_codes(jcr, changer, jcr->device->changer_command, "list");
225    bnet_fsend(dir, _("3306 Issuing autochanger \"list\" command.\n"));
226    bpipe = open_bpipe(changer, timeout, "r");
227    if (!bpipe) {
228       bnet_fsend(dir, _("3993 Open bpipe failed.\n"));
229       free_pool_memory(changer);
230       return false;
231    }
232    /* Get output from changer */
233    while (fgets(dir->msg, len, bpipe->rfd)) { 
234       dir->msglen = strlen(dir->msg);
235       bnet_send(dir);
236    }
237    bnet_sig(dir, BNET_EOD);
238    close_bpipe(bpipe);
239
240    free_pool_memory(changer);
241    return true;
242 }
243
244
245 /*
246  * Edit codes into ChangerCommand
247  *  %% = %
248  *  %a = archive device name
249  *  %c = changer device name
250  *  %d = changer drive index        
251  *  %f = Client's name
252  *  %j = Job name
253  *  %o = command
254  *  %s = Slot base 0
255  *  %S = Slot base 1
256  *  %v = Volume name
257  *
258  *
259  *  omsg = edited output message
260  *  imsg = input string containing edit codes (%x)
261  *  cmd = command string (load, unload, ...) 
262  *
263  */
264 char *edit_device_codes(JCR *jcr, char *omsg, const char *imsg, const char *cmd) 
265 {
266    const char *p;
267    const char *str;
268    char add[20];
269
270    *omsg = 0;
271    Dmsg1(400, "edit_device_codes: %s\n", imsg);
272    for (p=imsg; *p; p++) {
273       if (*p == '%') {
274          switch (*++p) {
275          case '%':
276             str = "%";
277             break;
278          case 'a':
279             str = dev_name(jcr->device->dev);
280             break;
281          case 'c':
282             str = NPRT(jcr->device->changer_name);
283             break;
284          case 'd':
285             sprintf(add, "%d", jcr->device->dev->drive_index);
286             str = add;
287             break;
288          case 'o':
289             str = NPRT(cmd);
290             break;
291          case 's':
292             sprintf(add, "%d", jcr->VolCatInfo.Slot - 1);
293             str = add;
294             break;
295          case 'S':
296             sprintf(add, "%d", jcr->VolCatInfo.Slot);
297             str = add;
298             break;
299          case 'j':                    /* Job name */
300             str = jcr->Job;
301             break;
302          case 'v':
303             str = NPRT(jcr->VolumeName);
304             break;
305          case 'f':
306             str = NPRT(jcr->client_name);
307             break;
308
309          default:
310             add[0] = '%';
311             add[1] = *p;
312             add[2] = 0;
313             str = add;
314             break;
315          }
316       } else {
317          add[0] = *p;
318          add[1] = 0;
319          str = add;
320       }
321       Dmsg1(400, "add_str %s\n", str);
322       pm_strcat(&omsg, (char *)str);
323       Dmsg1(400, "omsg=%s\n", omsg);
324    }
325    return omsg;
326 }