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