]> git.sur5r.net Git - bacula/bacula/blob - bacula/src/stored/autochanger.c
67cca5bad6ebbbe8f7e74ec4844b8d2d1348a4f0
[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-2003 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(JCR *jcr, DEVICE *dev, int writing, BSOCK *dir)
52 {
53    int slot = jcr->VolCatInfo.Slot;
54    int drive = jcr->device->drive_index;
55    int rtn_stat = -1;                 /* error status */
56      
57    /*
58     * Handle autoloaders here.  If we cannot autoload it, we
59     *  will return FALSE to ask the sysop.
60     */
61    if (writing && dev_cap(dev, CAP_AUTOCHANGER) && slot <= 0) {
62       if (dir) {
63          return 0;                    /* For user, bail out right now */
64       }
65       if (dir_find_next_appendable_volume(jcr)) {
66          slot = jcr->VolCatInfo.Slot; 
67       } else {
68          slot = 0;
69       }
70    }
71    Dmsg1(400, "Want changer slot=%d\n", slot);
72
73    if (slot > 0 && jcr->device->changer_name && jcr->device->changer_command) {
74       uint32_t timeout = jcr->device->max_changer_wait;
75       POOLMEM *changer, *results;
76       int loaded, status;     
77
78       results = get_pool_memory(PM_MESSAGE);
79       changer = get_pool_memory(PM_FNAME);
80
81       loaded = get_autochanger_loaded_slot(jcr);
82
83       Dmsg1(400, "loaded=%s\n", results);
84
85       /* If tape we want is not loaded, load it. */
86       if (loaded != slot) { 
87          offline_or_rewind_dev(dev);
88          /* We are going to load a new tape, so close the device */
89          force_close_dev(dev);
90          if (loaded != 0 && loaded != -1) {        /* must unload drive */
91             Dmsg0(400, "Doing changer unload.\n");
92             Jmsg(jcr, M_INFO, 0, 
93                  _("3303 Issuing autochanger \"unload slot %d, drive %d\" command.\n"),
94                  loaded, drive);
95             jcr->VolCatInfo.Slot = loaded;   /* slot to be unloaded */
96             changer = edit_device_codes(jcr, changer, 
97                         jcr->device->changer_command, "unload");
98             status = run_program(changer, timeout, NULL);
99             Dmsg1(400, "unload status=%d\n", status);
100          }
101          /*
102           * Load the desired cassette    
103           */
104          Dmsg1(400, "Doing changer load slot %d\n", slot);
105          Jmsg(jcr, M_INFO, 0, 
106               _("3304 Issuing autochanger \"load slot %d, drive %d\" command.\n"), 
107               slot, drive);
108          jcr->VolCatInfo.Slot = slot;    /* slot to be loaded */
109          changer = edit_device_codes(jcr, changer, 
110                       jcr->device->changer_command, "load");
111          status = run_program(changer, timeout, NULL);
112          if (status == 0) {
113             Jmsg(jcr, M_INFO, 0, _("3305 Autochanger \"load slot %d, drive %d\", status is OK.\n"),
114                     slot, drive);
115          } else {
116             Jmsg(jcr, M_INFO, 0, _("3992 Bad autochanger \"load slot %d, drive %d\", status=%d.\n"),
117                     slot, drive, status);
118          }
119          Dmsg2(400, "load slot %d status=%d\n", slot, status);
120       } else { 
121          status = 0;                  /* we got what we want */
122       }
123       free_pool_memory(changer);
124       free_pool_memory(results);
125       Dmsg1(400, "After changer, status=%d\n", status);
126       if (status == 0) {              /* did we succeed? */
127          rtn_stat = 1;                /* tape loaded by changer */
128       }
129    } else {
130       rtn_stat = 0;                   /* no changer found */
131    }
132    return rtn_stat;
133 }
134
135 static int get_autochanger_loaded_slot(JCR *jcr)
136 {
137    POOLMEM *changer, *results;
138    int status, loaded;
139    uint32_t timeout = jcr->device->max_changer_wait;
140    int drive = jcr->device->drive_index;
141
142    results = get_pool_memory(PM_MESSAGE);
143    changer = get_pool_memory(PM_FNAME);
144
145    /* Find out what is loaded, zero means device is unloaded */
146    Jmsg(jcr, M_INFO, 0, _("3301 Issuing autochanger \"loaded drive %d\" command.\n"),
147         drive);
148    changer = edit_device_codes(jcr, changer, jcr->device->changer_command, 
149                 "loaded");
150    status = run_program(changer, timeout, results);
151    Dmsg3(100, "run_prog: %s stat=%d result=%s\n", changer, status, results);
152    if (status == 0) {
153       loaded = atoi(results);
154       if (loaded > 0) {
155          Jmsg(jcr, M_INFO, 0, _("3302 Autochanger \"loaded drive %d\", result is Slot %d.\n"),
156               drive, loaded);
157       } else {
158          Jmsg(jcr, M_INFO, 0, _("3302 Autochanger \"loaded drive %d\", result: nothing loaded.\n"),
159               drive);
160       }
161    } else {
162       Jmsg(jcr, M_INFO, 0, _("3991 Bad autochanger \"loaded drive %d\" command, status=%d.\n"), 
163            drive, status);
164       loaded = -1;              /* force unload */
165    }
166    free_pool_memory(changer);
167    free_pool_memory(results);
168    return loaded;
169 }
170
171 /*
172  * The Volume is not in the correct slot, so mark this 
173  *   Volume as not being in the Changer.
174  */
175 void invalid_slot_in_catalog(JCR *jcr, DEVICE *dev)
176 {
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(jcr, dev, 1);  /* 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 int autochanger_list(JCR *jcr, DEVICE *dev, BSOCK *dir)
192 {
193    uint32_t timeout = jcr->device->max_changer_wait;
194    POOLMEM *changer;
195    BPIPE *bpipe;
196    int slot, loaded;
197    int len = sizeof_pool_memory(dir->msg) - 1;
198
199    if (!dev_cap(dev, CAP_AUTOCHANGER) || !jcr->device->changer_name ||
200        !jcr->device->changer_command) {
201       bnet_fsend(dir, _("3993 Not a autochanger device.\n"));
202       return 0;
203    }
204
205    changer = get_pool_memory(PM_FNAME);
206    offline_or_rewind_dev(dev);
207    /* We are going to load a new tape, so close the device */
208    force_close_dev(dev);
209
210    /* First unload any tape */
211    loaded = get_autochanger_loaded_slot(jcr);
212    if (loaded > 0) {
213       bnet_fsend(dir, _("3305 Issuing autochanger \"unload slot %d\" command.\n"), loaded);
214       slot = jcr->VolCatInfo.Slot; 
215       jcr->VolCatInfo.Slot = loaded;
216       changer = edit_device_codes(jcr, changer, jcr->device->changer_command, "unload");
217       run_program(changer, timeout, NULL);
218       jcr->VolCatInfo.Slot = slot;
219    }
220
221    /* Now list slots occupied */
222    changer = edit_device_codes(jcr, changer, jcr->device->changer_command, "list");
223    bnet_fsend(dir, _("3306 Issuing autochanger \"list\" command.\n"));
224    bpipe = open_bpipe(changer, timeout, "r");
225    if (!bpipe) {
226       bnet_fsend(dir, _("3993 Open bpipe failed.\n"));
227       goto bail_out;
228    }
229    /* Get output from changer */
230    while (fgets(dir->msg, len, bpipe->rfd)) { 
231       dir->msglen = strlen(dir->msg);
232       bnet_send(dir);
233    }
234    bnet_sig(dir, BNET_EOD);
235    close_bpipe(bpipe);
236
237 bail_out:
238    free_pool_memory(changer);
239    return 1;
240 }
241
242
243
244 /*
245  * Edit codes into ChangerCommand
246  *  %% = %
247  *  %a = archive device name
248  *  %c = changer device name
249  *  %d = changer drive index        
250  *  %f = Client's name
251  *  %j = Job name
252  *  %o = command
253  *  %s = Slot base 0
254  *  %S = Slot base 1
255  *  %v = Volume name
256  *
257  *
258  *  omsg = edited output message
259  *  imsg = input string containing edit codes (%x)
260  *  cmd = command string (load, unload, ...) 
261  *
262  */
263 char *edit_device_codes(JCR *jcr, char *omsg, const char *imsg, const char *cmd) 
264 {
265    const char *p;
266    const char *str;
267    char add[20];
268
269    *omsg = 0;
270    Dmsg1(400, "edit_device_codes: %s\n", imsg);
271    for (p=imsg; *p; p++) {
272       if (*p == '%') {
273          switch (*++p) {
274          case '%':
275             str = "%";
276             break;
277          case 'a':
278             str = dev_name(jcr->device->dev);
279             break;
280          case 'c':
281             str = NPRT(jcr->device->changer_name);
282             break;
283          case 'd':
284             sprintf(add, "%d", jcr->device->dev->drive_index);
285             str = add;
286             break;
287          case 'o':
288             str = NPRT(cmd);
289             break;
290          case 's':
291             sprintf(add, "%d", jcr->VolCatInfo.Slot - 1);
292             str = add;
293             break;
294          case 'S':
295             sprintf(add, "%d", jcr->VolCatInfo.Slot);
296             str = add;
297             break;
298          case 'j':                    /* Job name */
299             str = jcr->Job;
300             break;
301          case 'v':
302             str = NPRT(jcr->VolumeName);
303             break;
304          case 'f':
305             str = NPRT(jcr->client_name);
306             break;
307
308          default:
309             add[0] = '%';
310             add[1] = *p;
311             add[2] = 0;
312             str = add;
313             break;
314          }
315       } else {
316          add[0] = *p;
317          add[1] = 0;
318          str = add;
319       }
320       Dmsg1(400, "add_str %s\n", str);
321       pm_strcat(&omsg, (char *)str);
322       Dmsg1(400, "omsg=%s\n", omsg);
323    }
324    return omsg;
325 }