]> git.sur5r.net Git - bacula/bacula/blob - bacula/src/stored/autochanger.c
f127e96fe20bbafb14c3b1d6d308e2b2641c992c
[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-2005 Kern Sibbald
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(DCR *dcr, char *omsg, const char *cmd);
34 static int get_autochanger_loaded_slot(DCR *dcr);
35 static void lock_changer(DCR *dcr);
36 static void unlock_changer(DCR *dcr);
37
38 /*
39  * Called here to do an autoload using the autochanger, if
40  *  configured, and if a Slot has been defined for this Volume.
41  *  On success this routine loads the indicated tape, but the
42  *  label is not read, so it must be verified.
43  *
44  *  Note if dir is not NULL, it is the console requesting the
45  *   autoload for labeling, so we respond directly to the
46  *   dir bsock.
47  *
48  *  Returns: 1 on success
49  *           0 on failure (no changer available)
50  *          -1 on error on autochanger
51  */
52 int autoload_device(DCR *dcr, int writing, BSOCK *dir)
53 {
54    JCR *jcr = dcr->jcr;
55    DEVICE *dev = dcr->dev;
56    int slot;
57    int drive = dev->device->drive_index;
58    int rtn_stat = -1;                 /* error status */
59
60    slot = dcr->VolCatInfo.InChanger ? dcr->VolCatInfo.Slot : 0;
61    /*
62     * Handle autoloaders here.  If we cannot autoload it, we
63     *  will return FALSE to ask the sysop.
64     */
65    if (writing && dev_cap(dev, CAP_AUTOCHANGER) && slot <= 0) {
66       if (dir) {
67          return 0;                    /* For user, bail out right now */
68       }
69       if (dir_find_next_appendable_volume(dcr)) {
70          slot = dcr->VolCatInfo.InChanger ? dcr->VolCatInfo.Slot : 0;
71       } else {
72          slot = 0;
73       }
74    }
75    Dmsg1(400, "Want changer slot=%d\n", slot);
76
77    if (slot > 0 && dcr->device->changer_name && dcr->device->changer_command) {
78       uint32_t timeout = dcr->device->max_changer_wait;
79       POOLMEM *changer;
80       int loaded, status;
81
82       changer = get_pool_memory(PM_FNAME);
83
84       loaded = get_autochanger_loaded_slot(dcr);
85
86       /* If tape we want is not loaded, load it. */
87       if (loaded != slot) {
88          offline_or_rewind_dev(dev);
89          /* We are going to load a new tape, so close the device */
90          force_close_dev(dev);
91          lock_changer(dcr);
92          if (loaded != 0 && loaded != -1) {        /* must unload drive */
93             Dmsg0(400, "Doing changer unload.\n");
94             Jmsg(jcr, M_INFO, 0,
95                  _("3303 Issuing autochanger \"unload slot %d, drive %d\" command.\n"),
96                  loaded, drive);
97             dcr->VolCatInfo.Slot = loaded;   /* slot to be unloaded */
98             changer = edit_device_codes(dcr, changer, "unload");
99             status = run_program(changer, timeout, NULL);
100             if (status != 0) {
101                berrno be;
102                be.set_errno(status);
103                Jmsg(jcr, M_INFO, 0, _("3992 Bad autochanger \"unload slot %d, drive %d\": ERR=%s.\n"),
104                     slot, drive, be.strerror());
105             }
106
107             Dmsg1(400, "unload status=%d\n", status);
108          }
109          /*
110           * Load the desired cassette
111           */
112          Dmsg1(400, "Doing changer load slot %d\n", slot);
113          Jmsg(jcr, M_INFO, 0,
114               _("3304 Issuing autochanger \"load slot %d, drive %d\" command.\n"),
115               slot, drive);
116          dcr->VolCatInfo.Slot = slot;    /* slot to be loaded */
117          changer = edit_device_codes(dcr, changer, "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          unlock_changer(dcr);
129          Dmsg2(400, "load slot %d status=%d\n", slot, status);
130       } else {
131          status = 0;                  /* we got what we want */
132       }
133       free_pool_memory(changer);
134       Dmsg1(400, "After changer, status=%d\n", status);
135       if (status == 0) {              /* did we succeed? */
136          rtn_stat = 1;                /* tape loaded by changer */
137       }
138    } else {
139       rtn_stat = 0;                   /* no changer found */
140    }
141    return rtn_stat;
142 }
143
144 static int get_autochanger_loaded_slot(DCR *dcr)
145 {
146    JCR *jcr = dcr->jcr;
147    POOLMEM *changer, *results;
148    int status, loaded;
149    uint32_t timeout = dcr->device->max_changer_wait;
150    int drive = dcr->device->drive_index;
151
152    results = get_pool_memory(PM_MESSAGE);
153    changer = get_pool_memory(PM_FNAME);
154
155    lock_changer(dcr);
156
157    /* Find out what is loaded, zero means device is unloaded */
158    Jmsg(jcr, M_INFO, 0, _("3301 Issuing autochanger \"loaded drive %d\" command.\n"),
159         drive);
160    changer = edit_device_codes(dcr, changer, "loaded");
161    status = run_program(changer, timeout, results);
162    Dmsg3(50, "run_prog: %s stat=%d result=%s", changer, status, results);
163    if (status == 0) {
164       loaded = atoi(results);
165       if (loaded > 0) {
166          Jmsg(jcr, M_INFO, 0, _("3302 Autochanger \"loaded drive %d\", result is Slot %d.\n"),
167               drive, loaded);
168       } else {
169          Jmsg(jcr, M_INFO, 0, _("3302 Autochanger \"loaded drive %d\", result: nothing loaded.\n"),
170               drive);
171       }
172    } else {
173       berrno be;
174       be.set_errno(status);
175       Jmsg(jcr, M_INFO, 0, _("3991 Bad autochanger \"loaded drive %d\" command: ERR=%s.\n"),
176            drive, be.strerror());
177       loaded = -1;              /* force unload */
178    }
179    unlock_changer(dcr);
180    free_pool_memory(changer);
181    free_pool_memory(results);
182    return loaded;
183 }
184
185 static void lock_changer(DCR *dcr)
186 {
187    AUTOCHANGER *changer_res = dcr->device->changer_res;
188    if (changer_res) {
189       Dmsg1(100, "Locking changer %s\n", changer_res->hdr.name);
190       P(changer_res->changer_mutex);  /* Lock changer script */
191    }
192 }
193
194 static void unlock_changer(DCR *dcr)
195 {
196    AUTOCHANGER *changer_res = dcr->device->changer_res;
197    if (changer_res) {
198       Dmsg1(100, "Unlocking changer %s\n", changer_res->hdr.name);
199       V(changer_res->changer_mutex);  /* Unlock changer script */
200    }
201 }
202
203
204 /*
205  * The Volume is not in the correct slot, so mark this
206  *   Volume as not being in the Changer.
207  */
208 void mark_volume_not_inchanger(DCR *dcr)
209 {
210    JCR *jcr = dcr->jcr;
211    DEVICE *dev = dcr->dev;
212    Jmsg(jcr, M_ERROR, 0, _("Autochanger Volume \"%s\" not found in slot %d.\n"
213 "    Setting InChanger to zero in catalog.\n"),
214         dcr->VolCatInfo.VolCatName, dcr->VolCatInfo.Slot);
215    dcr->VolCatInfo.InChanger = false;
216    dev->VolCatInfo.InChanger = false;
217    Dmsg0(400, "update vol info in mount\n");
218    dir_update_volume_info(dcr, true);  /* set new status */
219 }
220
221 /*
222  * List the Volumes that are in the autoloader possibly
223  *   with their barcodes.
224  *   We assume that it is always the Console that is calling us.
225  */
226 bool autochanger_cmd(DCR *dcr, BSOCK *dir, const char *cmd)  
227 {
228    DEVICE *dev = dcr->dev;
229    JCR *jcr = dcr->jcr;
230    uint32_t timeout = dcr->device->max_changer_wait;
231    POOLMEM *changer;
232    BPIPE *bpipe;
233    int slot, loaded;
234    int len = sizeof_pool_memory(dir->msg) - 1;
235    bool ok = false;
236    int stat;
237
238    if (!dev_cap(dev, CAP_AUTOCHANGER) || !dcr->device->changer_name ||
239        !dcr->device->changer_command) {
240       bnet_fsend(dir, _("3993 Not a autochanger device.\n"));
241       return false;
242    }
243
244    changer = get_pool_memory(PM_FNAME);
245    /* List command? */
246    if (strcmp(cmd, "list") == 0) {
247       int drive = dev->device->drive_index;
248       /* Yes, to get a good listing, we unload any volumes */
249       offline_or_rewind_dev(dev);
250       /* We are going to load a new tape, so close the device */
251       force_close_dev(dev);
252
253       /* First unload any tape */
254       loaded = get_autochanger_loaded_slot(dcr);
255       if (loaded > 0) {
256          bnet_fsend(dir,
257             _("3305 Issuing autochanger \"unload slot %d, drive %d\" command.\n"),
258             loaded, drive);
259          slot = dcr->VolCatInfo.Slot;
260          dcr->VolCatInfo.Slot = loaded;
261          changer = edit_device_codes(dcr, changer, "unload");
262          lock_changer(dcr);
263          int stat = run_program(changer, timeout, NULL);
264          unlock_changer(dcr);
265          if (stat != 0) {
266             berrno be;
267             be.set_errno(stat);
268             Jmsg(jcr, M_INFO, 0, _("3995 Bad autochanger \"unload slot %d, drive %d\": ERR=%s.\n"),
269                     slot, drive, be.strerror());
270          }
271          dcr->VolCatInfo.Slot = slot;
272       }
273    }
274
275    /* Now issue the command */
276    changer = edit_device_codes(dcr, changer, cmd);
277    bnet_fsend(dir, _("3306 Issuing autochanger \"%s\" command.\n"), cmd);
278    lock_changer(dcr);
279    bpipe = open_bpipe(changer, timeout, "r");
280    if (!bpipe) {
281       unlock_changer(dcr);
282       bnet_fsend(dir, _("3993 Open bpipe failed.\n"));
283       goto bail_out;
284    }
285    if (strcmp(cmd, "list") == 0) {
286       /* Get output from changer */
287       while (fgets(dir->msg, len, bpipe->rfd)) {
288          dir->msglen = strlen(dir->msg);
289          Dmsg1(100, "<stored: %s\n", dir->msg);
290          bnet_send(dir);
291       }
292    } else {
293       /* For slots command, read a single line */
294       bstrncpy(dir->msg, "slots=", len);
295       fgets(dir->msg+6, len-6, bpipe->rfd);
296       dir->msglen = strlen(dir->msg);
297       Dmsg1(100, "<stored: %s", dir->msg);
298       bnet_send(dir);
299    }
300                  
301    stat = close_bpipe(bpipe);
302    unlock_changer(dcr);
303    if (stat != 0) {
304       berrno be;
305       be.set_errno(stat);
306       bnet_fsend(dir, "Autochanger error: ERR=%s\n", be.strerror());
307    }
308    bnet_sig(dir, BNET_EOD);
309    ok = true;
310
311 bail_out:
312    free_pool_memory(changer);
313    return true;
314 }
315
316
317 /*
318  * Edit codes into ChangerCommand
319  *  %% = %
320  *  %a = archive device name
321  *  %c = changer device name
322  *  %d = changer drive index
323  *  %f = Client's name
324  *  %j = Job name
325  *  %o = command
326  *  %s = Slot base 0
327  *  %S = Slot base 1
328  *  %v = Volume name
329  *
330  *
331  *  omsg = edited output message
332  *  imsg = input string containing edit codes (%x)
333  *  cmd = command string (load, unload, ...)
334  *
335  */
336 char *edit_device_codes(DCR *dcr, char *omsg, const char *cmd)
337 {
338    const char *p;
339    const char *str;
340    char add[20];
341    const char *imsg = dcr->device->changer_command;
342
343    *omsg = 0;
344    Dmsg1(1800, "edit_device_codes: %s\n", imsg);
345    for (p=imsg; *p; p++) {
346       if (*p == '%') {
347          switch (*++p) {
348          case '%':
349             str = "%";
350             break;
351          case 'a':
352             str = dcr->dev->archive_name();
353             break;
354          case 'c':
355             str = NPRT(dcr->device->changer_name);
356             break;
357          case 'd':
358             sprintf(add, "%d", dcr->dev->drive_index);
359             str = add;
360             break;
361          case 'o':
362             str = NPRT(cmd);
363             break;
364          case 's':
365             sprintf(add, "%d", dcr->VolCatInfo.Slot - 1);
366             str = add;
367             break;
368          case 'S':
369             sprintf(add, "%d", dcr->VolCatInfo.Slot);
370             str = add;
371             break;
372          case 'j':                    /* Job name */
373             str = dcr->jcr->Job;
374             break;
375          case 'v':
376             str = NPRT(dcr->VolumeName);
377             break;
378          case 'f':
379             str = NPRT(dcr->jcr->client_name);
380             break;
381
382          default:
383             add[0] = '%';
384             add[1] = *p;
385             add[2] = 0;
386             str = add;
387             break;
388          }
389       } else {
390          add[0] = *p;
391          add[1] = 0;
392          str = add;
393       }
394       Dmsg1(1900, "add_str %s\n", str);
395       pm_strcat(&omsg, (char *)str);
396       Dmsg1(1800, "omsg=%s\n", omsg);
397    }
398    Dmsg1(800, "omsg=%s\n", omsg);
399    return omsg;
400 }