]> git.sur5r.net Git - bacula/bacula/blob - bacula/src/stored/dircmd.c
edd11821535874d6d6e89ed37019c1d0c006ecb0
[bacula/bacula] / bacula / src / stored / dircmd.c
1 /*
2  *  This file handles accepting Director Commands
3  *
4  *    Most Director commands are handled here, with the 
5  *    exception of the Job command command and subsequent 
6  *    subcommands that are handled
7  *    in job.c.  
8  *
9  *    N.B. in this file, in general we must use P(dev->mutex) rather
10  *      than lock_device(dev) so that we can examine the blocked
11  *      state rather than blocking ourselves. In some "safe" cases,
12  *      we can do things to a blocked device. CAREFUL!!!!
13  *
14  *    File daemon commands are handled in fdcmd.c
15  *
16  *     Kern Sibbald, May MMI
17  *
18  *   Version $Id$
19  *  
20  */
21 /*
22    Copyright (C) 2000-2004 Kern Sibbald and John Walker
23
24    This program is free software; you can redistribute it and/or
25    modify it under the terms of the GNU General Public License as
26    published by the Free Software Foundation; either version 2 of
27    the License, or (at your option) any later version.
28
29    This program is distributed in the hope that it will be useful,
30    but WITHOUT ANY WARRANTY; without even the implied warranty of
31    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
32    General Public License for more details.
33
34    You should have received a copy of the GNU General Public
35    License along with this program; if not, write to the Free
36    Software Foundation, Inc., 59 Temple Place - Suite 330, Boston,
37    MA 02111-1307, USA.
38
39  */
40
41 #include "bacula.h"
42 #include "stored.h"
43
44 /* Exported variables */
45
46 /* Imported variables */
47 extern BSOCK *filed_chan;
48 extern int r_first, r_last;
49 extern struct s_res resources[];
50 extern char my_name[];
51 extern time_t daemon_start_time;
52 extern struct s_last_job last_job;
53
54 /* Static variables */
55 static char derrmsg[]       = "3900 Invalid command\n";
56 static char OKsetdebug[]   = "3000 OK setdebug=%d\n";
57 static char illegal_cmd[] = "3997 Illegal command for a Director with Monitor directive enabled\n";
58
59 /* Imported functions */
60 extern void terminate_child();
61 extern bool job_cmd(JCR *jcr);
62 extern bool status_cmd(JCR *sjcr);
63 extern bool qstatus_cmd(JCR *jcr);
64
65 /* Forward referenced functions */
66 static bool label_cmd(JCR *jcr);
67 static bool relabel_cmd(JCR *jcr);
68 static bool readlabel_cmd(JCR *jcr);
69 static bool release_cmd(JCR *jcr);
70 static bool setdebug_cmd(JCR *jcr);
71 static bool cancel_cmd(JCR *cjcr);
72 static bool mount_cmd(JCR *jcr);
73 static bool unmount_cmd(JCR *jcr);
74 static bool autochanger_cmd(JCR *sjcr);
75 static bool do_label(JCR *jcr, int relabel);
76 static DEVICE *find_device(JCR *jcr, POOL_MEM &dname);
77 static void read_volume_label(JCR *jcr, DEVICE *dev, int Slot);
78 static void label_volume_if_ok(JCR *jcr, DEVICE *dev, char *oldname,
79                                char *newname, char *poolname, 
80                                int Slot, int relabel);
81
82 struct s_cmds {
83    const char *cmd;
84    bool (*func)(JCR *jcr);
85    int monitoraccess; /* specify if monitors have access to this function */
86 };
87
88 /*  
89  * The following are the recognized commands from the Director. 
90  */
91 static struct s_cmds cmds[] = {
92    {"JobId=",      job_cmd,         0},     /* start Job */
93    {"autochanger", autochanger_cmd, 0},
94    {"bootstrap",   bootstrap_cmd,   0},
95    {"cancel",      cancel_cmd,      0},
96    {"label",       label_cmd,       0},     /* label a tape */
97    {"mount",       mount_cmd,       0},
98    {"readlabel",   readlabel_cmd,   0},
99    {"release",     release_cmd,     0},
100    {"relabel",     relabel_cmd,     0},     /* relabel a tape */
101    {"setdebug=",   setdebug_cmd,    0},     /* set debug level */
102    {"status",      status_cmd,      1},
103    {".status",     qstatus_cmd,     1},
104    {"unmount",     unmount_cmd,     0},
105    {NULL,        NULL}                      /* list terminator */
106 };
107
108
109 /* 
110  * Connection request. We accept connections either from the 
111  *  Director or a Client (File daemon).
112  * 
113  * Note, we are running as a seperate thread of the Storage daemon.
114  *  and it is because a Director has made a connection with
115  *  us on the "Message" channel.    
116  *
117  * Basic tasks done here:  
118  *  - Create a JCR record
119  *  - If it was from the FD, call handle_filed_connection()
120  *  - Authenticate the Director
121  *  - We wait for a command
122  *  - We execute the command
123  *  - We continue or exit depending on the return status
124  */
125 void *handle_connection_request(void *arg)
126 {
127    BSOCK *bs = (BSOCK *)arg;
128    JCR *jcr;
129    int i;
130    bool found, quit;
131    int bnet_stat = 0;
132    char name[MAX_NAME_LENGTH];
133
134    if (bnet_recv(bs) <= 0) {
135       Emsg0(M_ERROR, 0, _("Connection request failed.\n"));
136       bnet_close(bs);
137       return NULL;
138    }
139
140    /* 
141     * Do a sanity check on the message received
142     */
143    if (bs->msglen < 25 || bs->msglen > (int)sizeof(name)-25) {
144       Emsg1(M_ERROR, 0, _("Invalid connection. Len=%d\n"), bs->msglen);
145       bnet_close(bs);
146       return NULL;
147    }
148    /* 
149     * See if this is a File daemon connection. If so
150     *   call FD handler.
151     */
152    Dmsg1(110, "Conn: %s", bs->msg);
153    if (sscanf(bs->msg, "Hello Start Job %127s", name) == 1) {
154       handle_filed_connection(bs, name);
155       return NULL;
156    }
157    
158    Dmsg0(110, "Start Dir Job\n");
159    jcr = new_jcr(sizeof(JCR), stored_free_jcr); /* create Job Control Record */
160    jcr->dir_bsock = bs;               /* save Director bsock */
161    jcr->dir_bsock->jcr = jcr;
162    /* Initialize FD start condition variable */
163    int errstat = pthread_cond_init(&jcr->job_start_wait, NULL);
164    if (errstat != 0) {
165       Jmsg1(jcr, M_FATAL, 0, _("Unable to init job cond variable: ERR=%s\n"), strerror(errstat));
166       goto bail_out;
167    }
168
169    Dmsg0(1000, "stored in start_job\n");
170
171    /*
172     * Authenticate the Director
173     */
174    if (!authenticate_director(jcr)) {
175       Jmsg(jcr, M_FATAL, 0, _("Unable to authenticate Director\n"));
176       goto bail_out;
177    }
178    Dmsg0(90, "Message channel init completed.\n");
179
180    for (quit=0; !quit;) {
181       /* Read command */
182       if ((bnet_stat = bnet_recv(bs)) <= 0) {
183          break;               /* connection terminated */
184       }
185       Dmsg1(9, "<dird: %s\n", bs->msg);
186       found = false;
187       for (i=0; cmds[i].cmd; i++) {
188         if (strncmp(cmds[i].cmd, bs->msg, strlen(cmds[i].cmd)) == 0) {
189            if ((!cmds[i].monitoraccess) && (jcr->director->monitor)) {
190               Dmsg1(100, "Command %s illegal.\n", cmds[i].cmd);
191               bnet_fsend(bs, illegal_cmd);
192               bnet_sig(bs, BNET_EOD);
193               break;
194            }
195            if (!cmds[i].func(jcr)) { /* do command */
196               quit = true; /* error, get out */
197               Dmsg1(90, "Command %s requsts quit\n", cmds[i].cmd);
198            }
199            found = true;             /* indicate command found */
200            break;
201         }
202       }
203       if (!found) {                   /* command not found */
204         bnet_fsend(bs, derrmsg);
205         break;
206       }
207    }
208 bail_out:
209    dequeue_messages(jcr);             /* send any queued messages */
210    bnet_sig(bs, BNET_TERMINATE);
211    free_jcr(jcr);
212    return NULL;
213 }
214
215 /*
216  * Set debug level as requested by the Director
217  *
218  */
219 static bool setdebug_cmd(JCR *jcr)
220 {
221    BSOCK *dir = jcr->dir_bsock;
222    int level, trace_flag;
223
224    Dmsg1(10, "setdebug_cmd: %s", dir->msg);
225    if (sscanf(dir->msg, "setdebug=%d trace=%d", &level, &trace_flag) != 2 || level < 0) {
226       bnet_fsend(dir, "3991 Bad setdebug command: %s\n", dir->msg);
227       return 0;
228    }
229    debug_level = level;
230    set_trace(trace_flag);
231    return bnet_fsend(dir, OKsetdebug, level);
232 }
233
234
235 /*
236  * Cancel a Job
237  */
238 static bool cancel_cmd(JCR *cjcr)
239 {
240    BSOCK *dir = cjcr->dir_bsock;
241    int oldStatus;
242    char Job[MAX_NAME_LENGTH];
243    JCR *jcr;
244
245    if (sscanf(dir->msg, "cancel Job=%127s", Job) == 1) {
246       if (!(jcr=get_jcr_by_full_name(Job))) {
247          bnet_fsend(dir, _("3992 Job %s not found.\n"), Job);
248       } else {
249          P(jcr->mutex);
250          oldStatus = jcr->JobStatus;
251          set_jcr_job_status(jcr, JS_Canceled);
252          if (!jcr->authenticated && oldStatus == JS_WaitFD) {
253             pthread_cond_signal(&jcr->job_start_wait); /* wake waiting thread */
254          }
255          V(jcr->mutex);
256          if (jcr->file_bsock) {
257             bnet_sig(jcr->file_bsock, BNET_TERMINATE);
258          }
259          /* If thread waiting on mount, wake him */
260          if (jcr->dcr && jcr->dcr->dev &&
261               (jcr->dcr->dev->dev_blocked == BST_WAITING_FOR_SYSOP ||
262                jcr->dcr->dev->dev_blocked == BST_UNMOUNTED ||
263                jcr->dcr->dev->dev_blocked == BST_UNMOUNTED_WAITING_FOR_SYSOP)) {
264              pthread_cond_signal(&jcr->dcr->dev->wait_next_vol);
265          }
266          bnet_fsend(dir, _("3000 Job %s marked to be canceled.\n"), jcr->Job);
267          free_jcr(jcr);
268       }
269    } else {
270       bnet_fsend(dir, _("3993 Error scanning cancel command.\n"));
271    }
272    bnet_sig(dir, BNET_EOD);
273    return 1;
274 }
275
276 /*
277  * Label a tape
278  *
279  */
280 static bool label_cmd(JCR *jcr) 
281 {
282    return do_label(jcr, 0);
283 }
284
285 static bool relabel_cmd(JCR *jcr) 
286 {
287    return do_label(jcr, 1);
288 }
289
290 static bool do_label(JCR *jcr, int relabel)  
291 {
292    POOLMEM *newname, *oldname, *poolname, *mtype;
293    POOL_MEM dname;
294    BSOCK *dir = jcr->dir_bsock;
295    DEVICE *dev;
296    bool ok = false;
297    int slot;    
298
299    newname = get_memory(dir->msglen+1);
300    oldname = get_memory(dir->msglen+1);
301    poolname = get_memory(dir->msglen+1);
302    mtype = get_memory(dir->msglen+1);
303    if (relabel) {
304       if (sscanf(dir->msg, "relabel %127s OldName=%127s NewName=%127s PoolName=127%s MediaType=%127s Slot=%d",
305           dname.c_str(), oldname, newname, poolname, mtype, &slot) == 6) {
306          ok = true;
307       }
308    } else {
309       *oldname = 0;
310       if (sscanf(dir->msg, "label %127s VolumeName=%127s PoolName=%127s MediaType=%127s Slot=%d",
311           dname.c_str(), newname, poolname, mtype, &slot) == 5) {
312          ok = true;
313       }
314    }
315    if (ok) {
316       unbash_spaces(newname);
317       unbash_spaces(oldname);
318       unbash_spaces(poolname);
319       unbash_spaces(mtype);
320       dev = find_device(jcr, dname);
321       if (dev) {
322          /******FIXME**** compare MediaTypes */
323          P(dev->mutex);               /* Use P to avoid indefinite block */
324          if (!(dev->state & ST_OPENED)) {
325             label_volume_if_ok(jcr, dev, oldname, newname, poolname, slot, relabel);
326             force_close_dev(dev);
327          /* Under certain "safe" conditions, we can steal the lock */
328          } else if (dev->dev_blocked && 
329                     (dev->dev_blocked == BST_UNMOUNTED ||
330                      dev->dev_blocked == BST_WAITING_FOR_SYSOP ||
331                      dev->dev_blocked == BST_UNMOUNTED_WAITING_FOR_SYSOP)) {
332             label_volume_if_ok(jcr, dev, oldname, newname, poolname, slot, relabel);
333          } else if (dev_state(dev, ST_READ) || dev->num_writers) {
334             if (dev_state(dev, ST_READ)) {
335                 bnet_fsend(dir, _("3911 Device %s is busy with 1 reader.\n"),
336                    dev_name(dev));
337             } else {
338                 bnet_fsend(dir, _("3912 Device %s is busy with %d writer(s).\n"),
339                    dev_name(dev), dev->num_writers);
340             }
341          } else {                     /* device not being used */
342             label_volume_if_ok(jcr, dev, oldname, newname, poolname, slot, relabel);
343          }
344          V(dev->mutex);
345       } else {
346          bnet_fsend(dir, _("3999 Device \"%s\" not found\n"), dname.c_str());
347       }
348    } else {
349       /* NB dir->msg gets clobbered in bnet_fsend, so save command */
350       pm_strcpy(jcr->errmsg, dir->msg);
351       bnet_fsend(dir, _("3903 Error scanning label command: %s\n"), jcr->errmsg);
352    }
353    free_memory(oldname);
354    free_memory(newname);
355    free_memory(poolname);
356    free_memory(mtype);
357    bnet_sig(dir, BNET_EOD);
358    return true;
359 }
360
361 /* 
362  * Read the tape label and determine if we can safely
363  * label the tape (not a Bacula volume), then label it.
364  *
365  *  Enter with the mutex set
366  */
367 static void label_volume_if_ok(JCR *jcr, DEVICE *dev, char *oldname, 
368                                char *newname, char *poolname,
369                                int slot, int relabel)
370 {
371    BSOCK *dir = jcr->dir_bsock;
372    bsteal_lock_t hold;
373    DCR *dcr = jcr->dcr;
374    int label_status;
375    
376    steal_device_lock(dev, &hold, BST_WRITING_LABEL);
377    
378    bstrncpy(dcr->VolumeName, newname, sizeof(dcr->VolumeName));
379    dcr->VolCatInfo.Slot = slot;
380    if (autoload_device(dcr, 0, dir) < 0) {    /* autoload if possible */
381       goto bail_out;
382    }
383
384    /* Ensure that the device is open -- autoload_device() closes it */
385    for ( ; !(dev->state & ST_OPENED); ) {
386       if (open_dev(dev, dcr->VolumeName, OPEN_READ_WRITE) < 0) {
387          bnet_fsend(dir, _("3910 Unable to open device %s. ERR=%s\n"), 
388             dev_name(dev), strerror_dev(dev));
389          goto bail_out;
390       }
391    }
392
393    /* See what we have for a Volume */
394    label_status = read_dev_volume_label(dcr);                  
395    switch(label_status) {
396    case VOL_NAME_ERROR:
397    case VOL_VERSION_ERROR:
398    case VOL_LABEL_ERROR:
399    case VOL_OK:
400       if (!relabel) {
401          bnet_fsend(dir, _(
402             "3911 Cannot label Volume because it is already labeled: \"%s\"\n"), 
403              dev->VolHdr.VolName);
404          break;
405       }
406       /* Relabel request. If oldname matches, continue */
407       if (strcmp(oldname, dev->VolHdr.VolName) != 0) {
408          bnet_fsend(dir, _("Wrong volume mounted.\n"));
409          break;
410       }
411       /* Fall through wanted! */
412    case VOL_IO_ERROR:
413    case VOL_NO_LABEL:
414       if (!write_new_volume_label_to_dev(dcr, newname, poolname)) {
415          bnet_fsend(dir, _("3912 Failed to label Volume: ERR=%s\n"), strerror_dev(dev));
416          break;
417       }
418       bstrncpy(dcr->VolumeName, newname, sizeof(dcr->VolumeName));
419       /* The following 3000 OK label. string is scanned in ua_label.c */
420       bnet_fsend(dir, "3000 OK label. Volume=%s Device=%s\n", 
421          newname, dev_name(dev));
422       break;
423    case VOL_NO_MEDIA:
424       bnet_fsend(dir, _("3912 Failed to label Volume: ERR=%s\n"), strerror_dev(dev));
425       break;
426    default:
427       bnet_fsend(dir, _("3913 Cannot label Volume. \
428 Unknown status %d from read_volume_label()\n"), label_status);
429       break;
430    }
431
432 bail_out:
433    give_back_device_lock(dev, &hold);
434    return;
435 }
436
437
438 /* 
439  * Read the tape label
440  *
441  *  Enter with the mutex set
442  */
443 static bool read_label(DCR *dcr)
444 {
445    int ok;
446    JCR *jcr = dcr->jcr;
447    BSOCK *dir = jcr->dir_bsock;
448    bsteal_lock_t hold;
449    DEVICE *dev = dcr->dev;
450    
451    steal_device_lock(dev, &hold, BST_DOING_ACQUIRE);
452    
453    dcr->VolumeName[0] = 0;
454    dev->state &= ~ST_LABEL;           /* force read of label */
455    switch (read_dev_volume_label(dcr)) {                
456    case VOL_OK:
457       bnet_fsend(dir, _("3001 Mounted Volume: %s\n"), dev->VolHdr.VolName);
458       ok = true;
459       break;
460    default:
461       bnet_fsend(dir, _("3902 Cannot mount Volume on Storage Device \"%s\" because:\n%s"),
462          dev_name(dev), jcr->errmsg);
463       ok = false;
464       break;
465    }
466    give_back_device_lock(dev, &hold);
467    return ok;
468 }
469
470 static DEVICE *find_device(JCR *jcr, POOL_MEM &dname)
471 {
472    DEVRES *device;
473    bool found = false;
474
475    unbash_spaces(dname);
476    LockRes();
477    foreach_res(device, R_DEVICE) {
478       /* Find resource, and make sure we were able to open it */
479       if (strcmp(device->hdr.name, dname.c_str()) == 0 && device->dev) {
480          Dmsg1(20, "Found device %s\n", device->hdr.name);
481          jcr->device = device;
482          found = true;
483          break;
484       }
485    }
486    if (found) {
487       /*
488        * ****FIXME*****  device->dev may not point to right device
489        *  if there are multiple devices open
490        */
491       jcr->dcr = new_dcr(jcr, device->dev);
492       UnlockRes();
493       return jcr->dcr->dev;
494    }
495    UnlockRes();
496    return NULL;
497 }
498
499
500 /*
501  * Mount command from Director
502  */
503 static bool mount_cmd(JCR *jcr)
504 {
505    POOL_MEM dname;
506    BSOCK *dir = jcr->dir_bsock;
507    DEVICE *dev;
508    DCR *dcr;
509
510    if (sscanf(dir->msg, "mount %127s", dname.c_str()) == 1) {
511       dev = find_device(jcr, dname);
512       dcr = jcr->dcr;
513       if (dev) {
514          P(dev->mutex);               /* Use P to avoid indefinite block */
515          switch (dev->dev_blocked) {         /* device blocked? */
516          case BST_WAITING_FOR_SYSOP:
517             /* Someone is waiting, wake him */
518             Dmsg0(100, "Waiting for mount. Attempting to wake thread\n");
519             dev->dev_blocked = BST_MOUNT;
520             bnet_fsend(dir, "3001 OK mount. Device=%s\n", dev_name(dev));
521             pthread_cond_signal(&dev->wait_next_vol);
522             break;
523
524          /* In both of these two cases, we (the user) unmounted the Volume */
525          case BST_UNMOUNTED_WAITING_FOR_SYSOP:
526          case BST_UNMOUNTED:
527             /* We freed the device, so reopen it and wake any waiting threads */
528             if (open_dev(dev, NULL, OPEN_READ_WRITE) < 0) {
529                bnet_fsend(dir, _("3901 open device failed: ERR=%s\n"), 
530                   strerror_dev(dev));
531                break;
532             }
533             read_dev_volume_label(dcr);
534             if (dev->dev_blocked == BST_UNMOUNTED) {
535                /* We blocked the device, so unblock it */
536                Dmsg0(100, "Unmounted. Unblocking device\n");
537                read_label(dcr);       /* this should not be necessary */
538                unblock_device(dev);
539             } else {
540                Dmsg0(100, "Unmounted waiting for mount. Attempting to wake thread\n");
541                dev->dev_blocked = BST_MOUNT;
542             }
543             if (dev_state(dev, ST_LABEL)) {
544                bnet_fsend(dir, _("3001 Device %s is mounted with Volume \"%s\"\n"), 
545                   dev_name(dev), dev->VolHdr.VolName);
546             } else {
547                bnet_fsend(dir, _("3905 Device %s open but no Bacula volume is mounted.\n"
548                                  "If this is not a blank tape, try unmounting and remounting the Volume.\n"),
549                           dev_name(dev));
550             }
551             pthread_cond_signal(&dev->wait_next_vol);
552             break;
553
554          case BST_DOING_ACQUIRE:
555             bnet_fsend(dir, _("3001 Device %s is mounted; doing acquire.\n"), 
556                        dev_name(dev));
557             break;
558
559          case BST_WRITING_LABEL:
560             bnet_fsend(dir, _("3903 Device %s is being labeled.\n"), dev_name(dev));
561             break;
562
563          case BST_NOT_BLOCKED:
564             if (dev_state(dev, ST_OPENED)) {
565                if (dev_state(dev, ST_LABEL)) {
566                   bnet_fsend(dir, _("3001 Device %s is mounted with Volume \"%s\"\n"),
567                      dev_name(dev), dev->VolHdr.VolName);
568                } else {
569                   bnet_fsend(dir, _("3905 Device %s open but no Bacula volume is mounted.\n"   
570                                  "If this is not a blank tape, try unmounting and remounting the Volume.\n"),
571                              dev_name(dev));
572                }
573             } else {
574                if (!dev_is_tape(dev)) {
575                   bnet_fsend(dir, _("3906 cannot mount non-tape.\n"));
576                   break;
577                }
578                if (open_dev(dev, NULL, OPEN_READ_WRITE) < 0) {
579                   bnet_fsend(dir, _("3901 open device failed: ERR=%s\n"), 
580                      strerror_dev(dev));
581                   break;
582                }
583                read_label(dcr);
584                if (dev_state(dev, ST_LABEL)) {
585                   bnet_fsend(dir, _("3001 Device %s is already mounted with Volume \"%s\"\n"), 
586                      dev_name(dev), dev->VolHdr.VolName);
587                } else {
588                   bnet_fsend(dir, _("3905 Device %s open but no Bacula volume is mounted.\n"
589                                     "If this is not a blank tape, try unmounting and remounting the Volume.\n"),
590                              dev_name(dev));
591                }
592             }
593             break;
594
595          default:
596             bnet_fsend(dir, _("3905 Bizarre wait state %d\n"), dev->dev_blocked);
597             break;
598          }
599          V(dev->mutex);
600       } else {
601          bnet_fsend(dir, _("3999 Device \"%s\" not found\n"), dname.c_str());
602       }
603    } else {
604       pm_strcpy(jcr->errmsg, dir->msg);
605       bnet_fsend(dir, _("3909 Error scanning mount command: %s\n"), jcr->errmsg);
606    }
607    bnet_sig(dir, BNET_EOD);
608    return true;
609 }
610
611 /*
612  * unmount command from Director
613  */
614 static bool unmount_cmd(JCR *jcr)
615 {
616    POOL_MEM dname;
617    BSOCK *dir = jcr->dir_bsock;
618    DEVICE *dev;
619
620    if (sscanf(dir->msg, "unmount %127s", dname.c_str()) == 1) {
621       dev = find_device(jcr, dname);
622       if (dev) {
623          P(dev->mutex);               /* Use P to avoid indefinite block */
624          if (!(dev->state & ST_OPENED)) {
625             Dmsg0(90, "Device already unmounted\n");
626             bnet_fsend(dir, _("3901 Device \"%s\" is already unmounted.\n"), dev_name(dev));
627
628          } else if (dev->dev_blocked == BST_WAITING_FOR_SYSOP) {
629             Dmsg2(90, "%d waiter dev_block=%d. doing unmount\n", dev->num_waiting,
630                dev->dev_blocked);
631             open_dev(dev, NULL, 0);     /* fake open for close */
632             offline_or_rewind_dev(dev);
633             force_close_dev(dev);
634             dev->dev_blocked = BST_UNMOUNTED_WAITING_FOR_SYSOP;
635             bnet_fsend(dir, _("3001 Device \"%s\" unmounted.\n"), dev_name(dev));
636
637          } else if (dev->dev_blocked == BST_DOING_ACQUIRE) {
638             bnet_fsend(dir, _("3902 Device \"%s\" is busy in acquire.\n"), dev_name(dev));
639
640          } else if (dev->dev_blocked == BST_WRITING_LABEL) {
641             bnet_fsend(dir, _("3903 Device \"%s\" is being labeled.\n"), dev_name(dev));
642
643          } else if (dev_state(dev, ST_READ) || dev->num_writers) {
644             if (dev_state(dev, ST_READ)) {
645                 Dmsg0(90, "Device in read mode\n");
646                 bnet_fsend(dir, _("3904 Device \"%s\" is busy reading.\n"), dev_name(dev));
647             } else {
648                 Dmsg1(90, "Device busy with %d writers\n", dev->num_writers);
649                 bnet_fsend(dir, _("3905 Device %s is busy with %d writer(s).\n"),
650                    dev_name(dev), dev->num_writers);
651             }
652
653          } else {                     /* device not being used */
654             Dmsg0(90, "Device not in use, unmounting\n");
655             /* On FreeBSD, I am having ASSERT() failures in block_device()
656              * and I can only imagine that the thread id that we are
657              * leaving in no_wait_id is being re-used. So here,
658              * we simply do it by hand.  Gross, but a solution.
659              */
660             /*  block_device(dev, BST_UNMOUNTED); replace with 2 lines below */
661             dev->dev_blocked = BST_UNMOUNTED;
662             dev->no_wait_id = 0;
663             open_dev(dev, NULL, 0);     /* fake open for close */
664             offline_or_rewind_dev(dev);
665             force_close_dev(dev);
666             bnet_fsend(dir, _("3002 Device %s unmounted.\n"), dev_name(dev));
667          }
668          V(dev->mutex);
669       } else {
670          bnet_fsend(dir, _("3999 Device \"%s\" not found\n"), dname.c_str());
671       }
672    } else {
673       /* NB dir->msg gets clobbered in bnet_fsend, so save command */
674       pm_strcpy(jcr->errmsg, dir->msg);
675       bnet_fsend(dir, _("3907 Error scanning unmount command: %s\n"), jcr->errmsg);
676    }
677    bnet_sig(dir, BNET_EOD);
678    return true;
679 }
680
681 /*
682  * Release command from Director. This rewinds the device and if
683  *   configured does a offline and ensures that Bacula will
684  *   re-read the label of the tape before continuing. This gives
685  *   the operator the chance to change the tape anytime before the
686  *   next job starts.
687  */
688 static bool release_cmd(JCR *jcr)
689 {
690    POOL_MEM dname;
691    BSOCK *dir = jcr->dir_bsock;
692    DEVICE *dev;
693
694    if (sscanf(dir->msg, "release %127s", dname.c_str()) == 1) {
695       dev = find_device(jcr, dname);
696       if (dev) {
697          P(dev->mutex);               /* Use P to avoid indefinite block */
698          if (!(dev->state & ST_OPENED)) {
699             Dmsg0(90, "Device already released\n");
700             bnet_fsend(dir, _("3911 Device %s already released.\n"), dev_name(dev));
701
702          } else if (dev->dev_blocked == BST_WAITING_FOR_SYSOP ||
703                     dev->dev_blocked == BST_UNMOUNTED_WAITING_FOR_SYSOP) {
704             Dmsg2(90, "%d waiter dev_block=%d. doing unmount\n", dev->num_waiting,
705                dev->dev_blocked);
706             bnet_fsend(dir, _("3912 Device %s waiting for mount.\n"), dev_name(dev));
707
708          } else if (dev->dev_blocked == BST_DOING_ACQUIRE) {
709             bnet_fsend(dir, _("3913 Device %s is busy in acquire.\n"), dev_name(dev));
710
711          } else if (dev->dev_blocked == BST_WRITING_LABEL) {
712             bnet_fsend(dir, _("3914 Device %s is being labeled.\n"), dev_name(dev));
713
714          } else if (dev_state(dev, ST_READ) || dev->num_writers) {
715             if (dev_state(dev, ST_READ)) {
716                 Dmsg0(90, "Device in read mode\n");
717                 bnet_fsend(dir, _("3915 Device %s is busy with 1 reader.\n"), dev_name(dev));
718             } else {
719                 Dmsg1(90, "Device busy with %d writers\n", dev->num_writers);
720                 bnet_fsend(dir, _("3916 Device %s is busy with %d writer(s).\n"),
721                    dev_name(dev), dev->num_writers);
722             }
723
724          } else {                     /* device not being used */
725             Dmsg0(90, "Device not in use, unmounting\n");
726             release_volume(jcr->dcr);
727             bnet_fsend(dir, _("3012 Device %s released.\n"), dev_name(dev));
728          }
729          V(dev->mutex);
730       } else {
731          bnet_fsend(dir, _("3999 Device \"%s\" not found\n"), dname.c_str());
732       }
733    } else {
734       /* NB dir->msg gets clobbered in bnet_fsend, so save command */
735       pm_strcpy(jcr->errmsg, dir->msg);
736       bnet_fsend(dir, _("3917 Error scanning release command: %s\n"), jcr->errmsg);
737    }
738    bnet_sig(dir, BNET_EOD);
739    return true;
740 }
741
742
743
744 /*
745  * Autochanger command from Director
746  */
747 static bool autochanger_cmd(JCR *jcr)
748 {
749    POOL_MEM dname;
750    BSOCK *dir = jcr->dir_bsock;
751    DEVICE *dev;
752    DCR *dcr;
753
754    if (sscanf(dir->msg, "autochanger list %127s ", dname.c_str()) == 1) {
755       dev = find_device(jcr, dname);
756       dcr = jcr->dcr;
757       if (dev) {
758          P(dev->mutex);               /* Use P to avoid indefinite block */
759          if (!dev_is_tape(dev)) {
760             bnet_fsend(dir, _("3995 Device %s is not an autochanger.\n"), dev_name(dev));
761          } else if (!(dev->state & ST_OPENED)) {
762             autochanger_list(dcr, dir);
763          /* Under certain "safe" conditions, we can steal the lock */
764          } else if (dev->dev_blocked && 
765                     (dev->dev_blocked == BST_UNMOUNTED ||
766                      dev->dev_blocked == BST_WAITING_FOR_SYSOP ||
767                      dev->dev_blocked == BST_UNMOUNTED_WAITING_FOR_SYSOP)) {
768             autochanger_list(dcr, dir);
769          } else if (dev_state(dev, ST_READ) || dev->num_writers) {
770             if (dev_state(dev, ST_READ)) {
771                 bnet_fsend(dir, _("3901 Device %s is busy with 1 reader.\n"), dev_name(dev));
772             } else {
773                 bnet_fsend(dir, _("3902 Device %s is busy with %d writer(s).\n"),
774                    dev_name(dev), dev->num_writers);
775             }
776          } else {                     /* device not being used */
777             autochanger_list(dcr, dir);
778          }
779          V(dev->mutex);
780       } else {
781          bnet_fsend(dir, _("3999 Device \"%s\" not found\n"), dname.c_str());
782       }
783    } else {  /* error on scanf */
784       pm_strcpy(jcr->errmsg, dir->msg);
785       bnet_fsend(dir, _("3908 Error scanning autocharger list command: %s\n"),
786          jcr->errmsg);
787    }
788    bnet_sig(dir, BNET_EOD);
789    return true;
790 }
791
792 /*
793  * Read and return the Volume label
794  */
795 static bool readlabel_cmd(JCR *jcr)
796 {
797    POOL_MEM dname;
798    BSOCK *dir = jcr->dir_bsock;
799    DEVICE *dev;
800    int Slot;
801
802    if (sscanf(dir->msg, "readlabel %127s Slot=%d", dname.c_str(), &Slot) == 2) {
803       dev = find_device(jcr, dname);
804       if (dev) {
805          P(dev->mutex);               /* Use P to avoid indefinite block */
806          if (!dev_state(dev, ST_OPENED)) {
807             read_volume_label(jcr, dev, Slot);
808             force_close_dev(dev);
809          /* Under certain "safe" conditions, we can steal the lock */
810          } else if (dev->dev_blocked && 
811                     (dev->dev_blocked == BST_UNMOUNTED ||
812                      dev->dev_blocked == BST_WAITING_FOR_SYSOP ||
813                      dev->dev_blocked == BST_UNMOUNTED_WAITING_FOR_SYSOP)) {
814             read_volume_label(jcr, dev, Slot);
815          } else if (dev_state(dev, ST_READ) || dev->num_writers) {
816             if (dev_state(dev, ST_READ)) {
817                 bnet_fsend(dir, _("3911 Device %s is busy with 1 reader.\n"),
818                             dev_name(dev));
819             } else {
820                 bnet_fsend(dir, _("3912 Device %s is busy with %d writer(s).\n"),
821                    dev_name(dev), dev->num_writers);
822             }
823          } else {                     /* device not being used */
824             read_volume_label(jcr, dev, Slot);
825          }
826          V(dev->mutex);
827       } else {
828          bnet_fsend(dir, _("3999 Device \"%s\" not found\n"), dname.c_str());
829       }
830    } else {
831       pm_strcpy(jcr->errmsg, dir->msg);
832       bnet_fsend(dir, _("3909 Error scanning readlabel command: %s\n"), jcr->errmsg);
833    }
834    bnet_sig(dir, BNET_EOD);
835    return true;
836 }
837
838 /* 
839  * Read the tape label
840  *
841  *  Enter with the mutex set
842  */
843 static void read_volume_label(JCR *jcr, DEVICE *dev, int Slot)
844 {
845    BSOCK *dir = jcr->dir_bsock;
846    bsteal_lock_t hold;
847    DCR *dcr = jcr->dcr;
848    
849    steal_device_lock(dev, &hold, BST_WRITING_LABEL);
850    
851    dcr->VolumeName[0] = 0;
852    dcr->VolCatInfo.Slot = Slot;
853    if (autoload_device(dcr, 0, dir) < 0) {    /* autoload if possible */
854       goto bail_out;
855    }
856
857    /* Ensure that the device is open -- autoload_device() closes it */
858    for ( ; !dev_state(dev, ST_OPENED); ) {
859       if (open_dev(dev, dcr->VolumeName, OPEN_READ_WRITE) < 0) {
860          bnet_fsend(dir, _("3910 Unable to open device \"%s\". ERR=%s\n"), 
861             dev_name(dev), strerror_dev(dev));
862          goto bail_out;
863       }
864    }
865
866    dev->state &= ~ST_LABEL;           /* force read of label */
867    switch (read_dev_volume_label(dcr)) {                
868    case VOL_OK:
869       /* DO NOT add quotes around the Volume name. It is scanned in the DIR */
870       bnet_fsend(dir, _("3001 Volume=%s Slot=%d\n"), dev->VolHdr.VolName, Slot);
871       Dmsg1(100, "Volume: %s\n", dev->VolHdr.VolName);
872       break;
873    default:
874       bnet_fsend(dir, _("3902 Cannot mount Volume on Storage Device \"%s\" because:\n%s"),
875                  dev_name(dev), jcr->errmsg);
876       break;
877    }
878
879 bail_out:
880    give_back_device_lock(dev, &hold);
881    return;
882 }