]> git.sur5r.net Git - bacula/bacula/blob - bacula/src/stored/autochanger.c
- Lock autochanger script when running.
[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\n", 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_list(DCR *dcr, BSOCK *dir)
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
236    if (!dev_cap(dev, CAP_AUTOCHANGER) || !dcr->device->changer_name ||
237        !dcr->device->changer_command) {
238       bnet_fsend(dir, _("3993 Not a autochanger device.\n"));
239       return false;
240    }
241
242    changer = get_pool_memory(PM_FNAME);
243    offline_or_rewind_dev(dev);
244    /* We are going to load a new tape, so close the device */
245    force_close_dev(dev);
246
247    /* First unload any tape */
248    loaded = get_autochanger_loaded_slot(dcr);
249    if (loaded > 0) {
250       bnet_fsend(dir, _("3305 Issuing autochanger \"unload slot %d\" command.\n"), loaded);
251       slot = dcr->VolCatInfo.Slot;
252       dcr->VolCatInfo.Slot = loaded;
253       changer = edit_device_codes(dcr, changer, "unload");
254       int stat = run_program(changer, timeout, NULL);
255       if (stat != 0) {
256          berrno be;
257          be.set_errno(stat);
258          Jmsg(jcr, M_INFO, 0, _("3995 Bad autochanger \"unload slot %d\" command: ERR=%s.\n"),
259               loaded, be.strerror());
260       }
261       dcr->VolCatInfo.Slot = slot;
262    }
263
264    /* Now list slots occupied */
265    changer = edit_device_codes(dcr, changer, "list");
266    bnet_fsend(dir, _("3306 Issuing autochanger \"list\" command.\n"));
267    bpipe = open_bpipe(changer, timeout, "r");
268    if (!bpipe) {
269       bnet_fsend(dir, _("3993 Open bpipe failed.\n"));
270       free_pool_memory(changer);
271       return false;
272    }
273    /* Get output from changer */
274    while (fgets(dir->msg, len, bpipe->rfd)) {
275       dir->msglen = strlen(dir->msg);
276       bnet_send(dir);
277    }
278    int stat = close_bpipe(bpipe);
279    if (stat != 0) {
280       berrno be;
281       be.set_errno(stat);
282       bnet_fsend(dir, "Autochanger error: ERR=%s\n", be.strerror());
283    }
284    bnet_sig(dir, BNET_EOD);
285
286    free_pool_memory(changer);
287    return true;
288 }
289
290
291 /*
292  * Edit codes into ChangerCommand
293  *  %% = %
294  *  %a = archive device name
295  *  %c = changer device name
296  *  %d = changer drive index
297  *  %f = Client's name
298  *  %j = Job name
299  *  %o = command
300  *  %s = Slot base 0
301  *  %S = Slot base 1
302  *  %v = Volume name
303  *
304  *
305  *  omsg = edited output message
306  *  imsg = input string containing edit codes (%x)
307  *  cmd = command string (load, unload, ...)
308  *
309  */
310 char *edit_device_codes(DCR *dcr, char *omsg, const char *cmd)
311 {
312    const char *p;
313    const char *str;
314    char add[20];
315    const char *imsg = dcr->device->changer_command;
316
317    *omsg = 0;
318    Dmsg1(800, "edit_device_codes: %s\n", imsg);
319    for (p=imsg; *p; p++) {
320       if (*p == '%') {
321          switch (*++p) {
322          case '%':
323             str = "%";
324             break;
325          case 'a':
326             str = dev_name(dcr->dev);
327             break;
328          case 'c':
329             str = NPRT(dcr->device->changer_name);
330             break;
331          case 'd':
332             sprintf(add, "%d", dcr->dev->drive_index);
333             str = add;
334             break;
335          case 'o':
336             str = NPRT(cmd);
337             break;
338          case 's':
339             sprintf(add, "%d", dcr->VolCatInfo.Slot - 1);
340             str = add;
341             break;
342          case 'S':
343             sprintf(add, "%d", dcr->VolCatInfo.Slot);
344             str = add;
345             break;
346          case 'j':                    /* Job name */
347             str = dcr->jcr->Job;
348             break;
349          case 'v':
350             str = NPRT(dcr->VolumeName);
351             break;
352          case 'f':
353             str = NPRT(dcr->jcr->client_name);
354             break;
355
356          default:
357             add[0] = '%';
358             add[1] = *p;
359             add[2] = 0;
360             str = add;
361             break;
362          }
363       } else {
364          add[0] = *p;
365          add[1] = 0;
366          str = add;
367       }
368       Dmsg1(900, "add_str %s\n", str);
369       pm_strcat(&omsg, (char *)str);
370       Dmsg1(800, "omsg=%s\n", omsg);
371    }
372    return omsg;
373 }