]> git.sur5r.net Git - bacula/bacula/blob - bacula/src/stored/dircmd.c
Massive cleanup of recycling code
[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-2003 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
58
59 /* Imported functions */
60 extern void terminate_child();
61 extern int job_cmd(JCR *jcr);
62 extern int status_cmd(JCR *sjcr);
63
64 /* Forward referenced functions */
65 static int label_cmd(JCR *jcr);
66 static int relabel_cmd(JCR *jcr);
67 static int release_cmd(JCR *jcr);
68 static int setdebug_cmd(JCR *jcr);
69 static int cancel_cmd(JCR *cjcr);
70 static int mount_cmd(JCR *jcr);
71 static int unmount_cmd(JCR *jcr);
72 static int autochanger_cmd(JCR *sjcr);
73 static int do_label(JCR *jcr, int relabel);
74 static void label_volume_if_ok(JCR *jcr, DEVICE *dev, char *oldname,
75                                char *newname, char *poolname, 
76                                int Slot, int relabel);
77
78 struct s_cmds {
79    char *cmd;
80    int (*func)(JCR *jcr);
81 };
82
83 /*  
84  * The following are the recognized commands from the Director. 
85  */
86 static struct s_cmds cmds[] = {
87    {"JobId=",    job_cmd},            /* start Job */
88    {"setdebug=", setdebug_cmd},       /* set debug level */
89    {"cancel",    cancel_cmd},
90    {"label",     label_cmd},          /* label a tape */
91    {"relabel",   relabel_cmd},        /* relabel a tape */
92    {"mount",     mount_cmd},
93    {"unmount",   unmount_cmd},
94    {"status",    status_cmd},
95    {"autochanger", autochanger_cmd},
96    {"release",   release_cmd},
97    {NULL,        NULL}                /* list terminator */
98 };
99
100
101 /* 
102  * Connection request. We accept connections either from the 
103  *  Director or a Client.
104  * 
105  * Note, we are running as a seperate thread of the Storage daemon.
106  *  and it is because a Director has made a connection with
107  *  us on the "Message" channel.    
108  *
109  * Basic tasks done here:  
110  *  - Create a JCR record
111  *  - Authenticate the Director
112  *  - We wait for a command
113  *  - We execute the command
114  *  - We continue or exit depending on the return status
115  */
116 void *connection_request(void *arg)
117 {
118    BSOCK *bs = (BSOCK *)arg;
119    JCR *jcr;
120    int i, found, quit;
121    int bnet_stat = 0;
122    char name[MAX_NAME_LENGTH];
123
124    if (bnet_recv(bs) <= 0) {
125       Emsg0(M_ERROR, 0, _("Connection request failed.\n"));
126       return NULL;
127    }
128
129    /* 
130     * See if this is a File daemon connection
131     */
132    if (bs->msglen < 25 || bs->msglen > (int)sizeof(name)+25) {
133       Emsg1(M_ERROR, 0, _("Invalid Dir connection. Len=%d\n"), bs->msglen);
134    }
135    if (sscanf(bs->msg, "Hello Start Job %127s calling\n", name) == 1) {
136       handle_filed_connection(bs, name);
137       return NULL;
138    }
139    
140    jcr = new_jcr(sizeof(JCR), stored_free_jcr);     /* create Job Control Record */
141    jcr->dir_bsock = bs;               /* save Director bsock */
142    jcr->dir_bsock->jcr = jcr;
143
144    Dmsg0(1000, "stored in start_job\n");
145
146    /*
147     * Authenticate the Director
148     */
149    if (!authenticate_director(jcr)) {
150       Jmsg(jcr, M_FATAL, 0, _("Unable to authenticate Director\n"));
151       free_jcr(jcr);
152       return NULL;
153    }
154    Dmsg0(90, "Message channel init completed.\n");
155
156    for (quit=0; !quit;) {
157       /* Read command */
158       if ((bnet_stat = bnet_recv(bs)) <= 0) {
159          break;                       /* connection terminated */
160       }
161       Dmsg1(9, "<dird: %s\n", bs->msg);
162       found = FALSE;
163       for (i=0; cmds[i].cmd; i++) {
164          if (strncmp(cmds[i].cmd, bs->msg, strlen(cmds[i].cmd)) == 0) {
165             if (!cmds[i].func(jcr)) {    /* do command */
166                quit = TRUE;              /* error, get out */
167                Dmsg1(90, "Command %s requsts quit\n", cmds[i].cmd);
168             }
169             found = TRUE;            /* indicate command found */
170             break;
171          }
172       }
173       if (!found) {                   /* command not found */
174          bnet_fsend(bs, derrmsg);
175          quit = TRUE;
176          break;
177       }
178    }
179    bnet_sig(bs, BNET_TERMINATE);
180    free_jcr(jcr);
181    return NULL;
182 }
183
184 /*
185  * Set debug level as requested by the Director
186  *
187  */
188 static int setdebug_cmd(JCR *jcr)
189 {
190    BSOCK *dir = jcr->dir_bsock;
191    int level;
192
193    Dmsg1(10, "setdebug_cmd: %s", dir->msg);
194    if (sscanf(dir->msg, "setdebug=%d", &level) != 1 || level < 0) {
195       bnet_fsend(dir, "3991 Bad setdebug command: %s\n", dir->msg);
196       return 0;
197    }
198    debug_level = level;
199    return bnet_fsend(dir, OKsetdebug, level);
200 }
201
202
203 /*
204  * Cancel a Job
205  */
206 static int cancel_cmd(JCR *cjcr)
207 {
208    BSOCK *dir = cjcr->dir_bsock;
209    int oldStatus;
210    char Job[MAX_NAME_LENGTH];
211    JCR *jcr;
212
213    if (sscanf(dir->msg, "cancel Job=%127s", Job) == 1) {
214       if (!(jcr=get_jcr_by_full_name(Job))) {
215          bnet_fsend(dir, _("3992 Job %s not found.\n"), Job);
216       } else {
217          P(jcr->mutex);
218          oldStatus = jcr->JobStatus;
219          set_jcr_job_status(jcr, JS_Canceled);
220          if (!jcr->authenticated && oldStatus == JS_WaitFD) {
221             pthread_cond_signal(&jcr->job_start_wait); /* wake waiting thread */
222          }
223          V(jcr->mutex);
224          if (jcr->file_bsock) {
225             bnet_sig(jcr->file_bsock, BNET_TERMINATE);
226          }
227          /* If thread waiting on mount, wake him */
228          if (jcr->device && jcr->device->dev &&      
229               (jcr->device->dev->dev_blocked == BST_WAITING_FOR_SYSOP ||
230                jcr->device->dev->dev_blocked == BST_UNMOUNTED ||
231                jcr->device->dev->dev_blocked == BST_UNMOUNTED_WAITING_FOR_SYSOP)) {
232              pthread_cond_signal(&jcr->device->dev->wait_next_vol);
233          }
234          bnet_fsend(dir, _("3000 Job %s marked to be canceled.\n"), jcr->Job);
235          free_jcr(jcr);
236       }
237    } else {
238       bnet_fsend(dir, _("3993 Error scanning cancel command.\n"));
239    }
240    bnet_sig(dir, BNET_EOD);
241    return 1;
242 }
243
244 /*
245  * Label a tape
246  *
247  */
248 static int label_cmd(JCR *jcr) 
249 {
250    return do_label(jcr, 0);
251 }
252
253 static int relabel_cmd(JCR *jcr) 
254 {
255    return do_label(jcr, 1);
256 }
257
258 static int do_label(JCR *jcr, int relabel)  
259 {
260    POOLMEM *dname, *newname, *oldname, *poolname, *mtype;
261    BSOCK *dir = jcr->dir_bsock;
262    DEVRES *device;
263    DEVICE *dev;
264    int found = 0, ok = 0;
265    int slot;    
266
267    dname = get_memory(dir->msglen+1);
268    newname = get_memory(dir->msglen+1);
269    oldname = get_memory(dir->msglen+1);
270    poolname = get_memory(dir->msglen+1);
271    mtype = get_memory(dir->msglen+1);
272    if (relabel) {
273       if (sscanf(dir->msg, "relabel %s OldName=%s NewName=%s PoolName=%s MediaType=%s Slot=%d",
274           dname, oldname, newname, poolname, mtype, &slot) == 6) {
275          ok = 1;
276       }
277    } else {
278       *oldname = 0;
279       if (sscanf(dir->msg, "label %s VolumeName=%s PoolName=%s MediaType=%s Slot=%d",
280           dname, newname, poolname, mtype, &slot) == 5) {
281          ok = 1;
282       }
283    }
284    if (ok) {
285       unbash_spaces(dname);
286       unbash_spaces(newname);
287       unbash_spaces(oldname);
288       unbash_spaces(poolname);
289       unbash_spaces(mtype);
290       device = NULL;
291       LockRes();
292       while ((device=(DEVRES *)GetNextRes(R_DEVICE, (RES *)device))) {
293          /* Find resource, and make sure we were able to open it */
294          if (strcmp(device->hdr.name, dname) == 0 && device->dev) {
295             Dmsg1(20, "Found device %s\n", device->hdr.name);
296             found = 1;
297             break;
298          }
299       }
300       UnlockRes();
301       if (found) {
302          /******FIXME**** compare MediaTypes */
303          jcr->device = device;
304          dev = device->dev;
305
306          P(dev->mutex);               /* Use P to avoid indefinite block */
307          if (!(dev->state & ST_OPENED)) {
308             if (open_dev(dev, newname, READ_WRITE) < 0) {
309                bnet_fsend(dir, _("3994 Connot open device: %s\n"), strerror_dev(dev));
310             } else {
311                label_volume_if_ok(jcr, dev, oldname, newname, poolname, slot, relabel);
312                force_close_dev(dev);
313             }
314          /* Under certain "safe" conditions, we can steal the lock */
315          } else if (dev->dev_blocked && 
316                     (dev->dev_blocked == BST_UNMOUNTED ||
317                      dev->dev_blocked == BST_WAITING_FOR_SYSOP ||
318                      dev->dev_blocked == BST_UNMOUNTED_WAITING_FOR_SYSOP)) {
319             label_volume_if_ok(jcr, dev, oldname, newname, poolname, slot, relabel);
320          } else if (dev->state & ST_READ || dev->num_writers) {
321             if (dev->state & ST_READ) {
322                 bnet_fsend(dir, _("3901 Device %s is busy with 1 reader.\n"),
323                    dev_name(dev));
324             } else {
325                 bnet_fsend(dir, _("3902 Device %s is busy with %d writer(s).\n"),
326                    dev_name(dev), dev->num_writers);
327             }
328          } else {                     /* device not being used */
329             label_volume_if_ok(jcr, dev, oldname, newname, poolname, slot, relabel);
330          }
331          V(dev->mutex);
332       } else {
333          bnet_fsend(dir, _("3999 Device %s not found\n"), dname);
334       }
335    } else {
336       /* NB dir->msg gets clobbered in bnet_fsend, so save command */
337       pm_strcpy(&jcr->errmsg, dir->msg);
338       bnet_fsend(dir, _("3903 Error scanning label command: %s\n"), jcr->errmsg);
339    }
340    free_memory(dname);
341    free_memory(oldname);
342    free_memory(newname);
343    free_memory(poolname);
344    free_memory(mtype);
345    bnet_sig(dir, BNET_EOD);
346    return 1;
347 }
348
349 /* 
350  * Read the tape label and determine if we can safely
351  * label the tape (not a Bacula volume), then label it.
352  *
353  *  Enter with the mutex set
354  */
355 static void label_volume_if_ok(JCR *jcr, DEVICE *dev, char *oldname, 
356                                char *newname, char *poolname,
357                                int slot, int relabel)
358 {
359    BSOCK *dir = jcr->dir_bsock;
360    DEV_BLOCK *block;
361    bsteal_lock_t hold;
362    
363    steal_device_lock(dev, &hold, BST_WRITING_LABEL);
364    
365    pm_strcpy(&jcr->VolumeName, newname);
366    jcr->VolCatInfo.Slot = slot;
367    autoload_device(jcr, dev, 0, dir);      /* autoload if possible */
368    block = new_block(dev);
369
370    /* Ensure that the device is open -- autoload_device() closes it */
371    for ( ; !(dev->state & ST_OPENED); ) {
372       if (open_dev(dev, jcr->VolumeName, READ_WRITE) < 0) {
373          if (dev->dev_errno == EAGAIN || dev->dev_errno == EBUSY) {
374             bmicrosleep(30, 0);
375          }
376          bnet_fsend(dir, _("3910 Unable to open device %s. ERR=%s\n"), 
377             dev_name(dev), strerror_dev(dev));
378          goto bail_out;
379       }
380    }
381
382    /* See what we have for a Volume */
383    switch (read_dev_volume_label(jcr, dev, block)) {                
384    case VOL_NAME_ERROR:
385    case VOL_VERSION_ERROR:
386    case VOL_LABEL_ERROR:
387    case VOL_OK:
388       if (!relabel) {
389          bnet_fsend(dir, _(
390             "3911 Cannot label Volume because it is already labeled: \"%s\"\n"), 
391              dev->VolHdr.VolName);
392          break;
393       }
394       /* Relabel request. If oldname matches, continue */
395       if (strcmp(oldname, dev->VolHdr.VolName) != 0) {
396          bnet_fsend(dir, _("Wrong volume mounted.\n"));
397          break;
398       }
399       /* Fall through wanted! */
400    case VOL_IO_ERROR:
401    case VOL_NO_LABEL:
402       if (!write_volume_label_to_dev(jcr, jcr->device, newname, poolname)) {
403          bnet_fsend(dir, _("3912 Failed to label Volume: ERR=%s\n"), strerror_dev(dev));
404          break;
405       }
406       pm_strcpy(&jcr->VolumeName, newname);
407       bnet_fsend(dir, _("3000 OK label. Volume=%s Device=%s\n"), 
408          newname, dev->dev_name);
409       break;
410    case VOL_NO_MEDIA:
411       bnet_fsend(dir, _("3912 Failed to label Volume: ERR=%s\n"), strerror_dev(dev));
412       break;
413    default:
414       bnet_fsend(dir, _("3913 Cannot label Volume. \
415 Unknown status %d from read_volume_label()\n"), jcr->label_status);
416       break;
417    }
418 bail_out:
419    free_block(block);
420    give_back_device_lock(dev, &hold);
421
422    return;
423 }
424
425
426 /* 
427  * Read the tape label
428  *
429  *  Enter with the mutex set
430  */
431 static int read_label(JCR *jcr, DEVICE *dev)
432 {
433    int stat;
434    BSOCK *dir = jcr->dir_bsock;
435    DEV_BLOCK *block;
436    bsteal_lock_t hold;
437    
438    steal_device_lock(dev, &hold, BST_DOING_ACQUIRE);
439    
440    jcr->VolumeName[0] = 0;
441    block = new_block(dev);
442    dev->state &= ~ST_LABEL;           /* force read of label */
443    switch (read_dev_volume_label(jcr, dev, block)) {                
444    case VOL_OK:
445       bnet_fsend(dir, _("3001 Mounted Volume: %s\n"), dev->VolHdr.VolName);
446       stat = 1;
447       break;
448    default:
449       bnet_fsend(dir, _("3902 Cannot mount Volume on Storage Device \"%s\" because:\n%s\n"),
450          dev->dev_name, jcr->errmsg);
451       stat = 0;
452       break;
453    }
454    free_block(block);
455    give_back_device_lock(dev, &hold);
456    return stat;
457 }
458
459 /*
460  * Mount command from Director
461  */
462 static int mount_cmd(JCR *jcr)
463 {
464    POOLMEM *dev_name;
465    BSOCK *dir = jcr->dir_bsock;
466    DEVRES *device;
467    DEVICE *dev;
468    int found = 0;
469
470    dev_name = get_memory(dir->msglen+1);
471    if (sscanf(dir->msg, "mount %s", dev_name) == 1) {
472       unbash_spaces(dev_name);
473       device = NULL;
474       LockRes();
475       while ((device=(DEVRES *)GetNextRes(R_DEVICE, (RES *)device))) {
476          /* Find resource, and make sure we were able to open it */
477          if (strcmp(device->hdr.name, dev_name) == 0 && device->dev) {
478             Dmsg1(20, "Found device %s\n", device->hdr.name);
479             found = 1;
480             break;
481          }
482       }
483       UnlockRes();
484       if (found) {
485          jcr->device = device;
486          dev = device->dev;
487          P(dev->mutex);               /* Use P to avoid indefinite block */
488          switch (dev->dev_blocked) {         /* device blocked? */
489             DEV_BLOCK *block;
490          case BST_WAITING_FOR_SYSOP:
491             /* Someone is waiting, wake him */
492             Dmsg0(100, "Waiting for mount. Attempting to wake thread\n");
493             dev->dev_blocked = BST_MOUNT;
494             pthread_cond_signal(&dev->wait_next_vol);
495             bnet_fsend(dir, "3001 OK mount. Device=%s\n", dev->dev_name);
496             break;
497
498          case BST_UNMOUNTED_WAITING_FOR_SYSOP:
499          case BST_UNMOUNTED:
500             /* We freed the device, so reopen it and wake any waiting threads */
501             if (open_dev(dev, NULL, READ_WRITE) < 0) {
502                bnet_fsend(dir, _("3901 open device failed: ERR=%s\n"), 
503                   strerror_dev(dev));
504                break;
505             }
506             block = new_block(dev);
507             read_dev_volume_label(jcr, dev, block);
508             free_block(block);
509             if (dev->dev_blocked == BST_UNMOUNTED) {
510                Dmsg0(100, "Unmounted. Unblocking device\n");
511                read_label(jcr, dev);
512                unblock_device(dev);
513             } else {
514                Dmsg0(100, "Unmounted waiting for mount. Attempting to wake thread\n");
515                dev->dev_blocked = BST_MOUNT;
516                pthread_cond_signal(&dev->wait_next_vol);
517             }
518             if (dev->state & ST_LABEL) {
519                bnet_fsend(dir, _("3001 Device %s is mounted with Volume \"%s\"\n"), 
520                   dev->dev_name, dev->VolHdr.VolName);
521             } else {
522                bnet_fsend(dir, _("3905 Device %s open but no Bacula volume is mounted.\n"
523                                  "If this is not a blank tape, try unmounting and remounting the Volume.\n"),
524                           dev->dev_name);
525             }
526             break;
527
528          case BST_DOING_ACQUIRE:
529             bnet_fsend(dir, _("3001 Device %s is mounted; doing acquire.\n"), 
530                        dev->dev_name);
531             break;
532
533          case BST_WRITING_LABEL:
534             bnet_fsend(dir, _("3903 Device %s is being labeled.\n"), dev->dev_name);
535             break;
536
537          case BST_NOT_BLOCKED:
538             if (dev->state & ST_OPENED) {
539                if (dev->state & ST_LABEL) {
540                   bnet_fsend(dir, _("3001 Device %s is mounted with Volume \"%s\"\n"),
541                      dev->dev_name, dev->VolHdr.VolName);
542                } else {
543                   bnet_fsend(dir, _("3905 Device %s open but no Bacula volume is mounted.\n"   
544                                  "If this is not a blank tape, try unmounting and remounting the Volume.\n"),
545                              dev->dev_name);
546                }
547             } else {
548                if (!dev_is_tape(dev)) {
549                   bnet_fsend(dir, _("3906 cannot mount non-tape.\n"));
550                   break;
551                }
552                if (open_dev(dev, NULL, READ_WRITE) < 0) {
553                   bnet_fsend(dir, _("3901 open device failed: ERR=%s\n"), 
554                      strerror_dev(dev));
555                   break;
556                }
557                read_label(jcr, dev);
558                if (dev->state & ST_LABEL) {
559                   bnet_fsend(dir, _("3001 Device %s is mounted with Volume \"%s\"\n"), 
560                      dev->dev_name, dev->VolHdr.VolName);
561                } else {
562                   bnet_fsend(dir, _("3905 Device %s open but no Bacula volume is mounted.\n"
563                                     "If this is not a blank tape, try unmounting and remounting the Volume.\n"),
564                              dev->dev_name);
565                }
566             }
567             break;
568
569          default:
570             bnet_fsend(dir, _("3905 Bizarre wait state %d\n"), dev->dev_blocked);
571             break;
572          }
573          V(dev->mutex);
574       } else {
575          bnet_fsend(dir, _("3999 Device %s not found\n"), dev_name);
576       }
577    } else {
578       pm_strcpy(&jcr->errmsg, dir->msg);
579       bnet_fsend(dir, _("3906 Error scanning mount command: %s\n"), jcr->errmsg);
580    }
581    free_memory(dev_name);
582    bnet_sig(dir, BNET_EOD);
583    return 1;
584 }
585
586 /*
587  * unmount command from Director
588  */
589 static int unmount_cmd(JCR *jcr)
590 {
591    POOLMEM *dname;
592    BSOCK *dir = jcr->dir_bsock;
593    DEVRES *device;
594    DEVICE *dev;
595    int found = 0;
596
597    dname = get_memory(dir->msglen+1);
598    if (sscanf(dir->msg, "unmount %s", dname) == 1) {
599       unbash_spaces(dname);
600       device = NULL;
601       LockRes();
602       while ((device=(DEVRES *)GetNextRes(R_DEVICE, (RES *)device))) {
603          /* Find resource, and make sure we were able to open it */
604          if (strcmp(device->hdr.name, dname) == 0 && device->dev) {
605             Dmsg1(20, "Found device %s\n", device->hdr.name);
606             found = 1;
607             break;
608          }
609       }
610       UnlockRes();
611       if (found) {
612          jcr->device = device;
613          dev = device->dev;
614          P(dev->mutex);               /* Use P to avoid indefinite block */
615          if (!(dev->state & ST_OPENED)) {
616             Dmsg0(90, "Device already unmounted\n");
617             bnet_fsend(dir, _("3901 Device %s is already unmounted.\n"), dev_name(dev));
618
619          } else if (dev->dev_blocked == BST_WAITING_FOR_SYSOP) {
620             Dmsg2(90, "%d waiter dev_block=%d. doing unmount\n", dev->num_waiting,
621                dev->dev_blocked);
622             open_dev(dev, NULL, 0);     /* fake open for close */
623             offline_or_rewind_dev(dev);
624             force_close_dev(dev);
625             dev->dev_blocked = BST_UNMOUNTED_WAITING_FOR_SYSOP;
626             bnet_fsend(dir, _("3001 Device %s unmounted.\n"), dev_name(dev));
627
628          } else if (dev->dev_blocked == BST_DOING_ACQUIRE) {
629             bnet_fsend(dir, _("3902 Device %s is busy in acquire.\n"),
630                dev_name(dev));
631
632          } else if (dev->dev_blocked == BST_WRITING_LABEL) {
633             bnet_fsend(dir, _("3903 Device %s is being labeled.\n"),
634                dev_name(dev));
635
636          } else if (dev->state & ST_READ || dev->num_writers) {
637             if (dev->state & ST_READ) {
638                 Dmsg0(90, "Device in read mode\n");
639                 bnet_fsend(dir, _("3904 Device %s is busy with 1 reader.\n"),
640                    dev_name(dev));
641             } else {
642                 Dmsg1(90, "Device busy with %d writers\n", dev->num_writers);
643                 bnet_fsend(dir, _("3905 Device %s is busy with %d writer(s).\n"),
644                    dev_name(dev), dev->num_writers);
645             }
646
647          } else {                     /* device not being used */
648             Dmsg0(90, "Device not in use, unmounting\n");
649             /* On FreeBSD, I am having ASSERT() failures in block_device()
650              * and I can only imagine that the thread id that we are
651              * leaving in no_wait_id is being re-used. So here,
652              * we simply do it by hand.  Gross, but a solution.
653              */
654             /*  block_device(dev, BST_UNMOUNTED); replace with 2 lines below */
655             dev->dev_blocked = BST_UNMOUNTED;
656             dev->no_wait_id = 0;
657             open_dev(dev, NULL, 0);     /* fake open for close */
658             offline_or_rewind_dev(dev);
659             force_close_dev(dev);
660             bnet_fsend(dir, _("3002 Device %s unmounted.\n"), dev_name(dev));
661          }
662          V(dev->mutex);
663       } else {
664          bnet_fsend(dir, _("3999 Device %s not found\n"), dname);
665       }
666    } else {
667       /* NB dir->msg gets clobbered in bnet_fsend, so save command */
668       pm_strcpy(&jcr->errmsg, dir->msg);
669       bnet_fsend(dir, _("3907 Error scanning unmount command: %s\n"), jcr->errmsg);
670    }
671    free_memory(dname);
672    bnet_sig(dir, BNET_EOD);
673    return 1;
674 }
675
676 /*
677  * Release command from Director. This rewinds the device and if
678  *   configured does a offline and ensures that Bacula will
679  *   re-read the label of the tape before continuing. This gives
680  *   the operator the chance to change the tape anytime before the
681  *   next job starts.
682  */
683 static int release_cmd(JCR *jcr)
684 {
685    POOLMEM *dname;
686    BSOCK *dir = jcr->dir_bsock;
687    DEVRES *device;
688    DEVICE *dev;
689    int found = 0;
690
691    dname = get_memory(dir->msglen+1);
692    if (sscanf(dir->msg, "release %s", dname) == 1) {
693       unbash_spaces(dname);
694       device = NULL;
695       LockRes();
696       while ((device=(DEVRES *)GetNextRes(R_DEVICE, (RES *)device))) {
697          /* Find resource, and make sure we were able to open it */
698          if (strcmp(device->hdr.name, dname) == 0 && device->dev) {
699             Dmsg1(20, "Found device %s\n", device->hdr.name);
700             found = 1;
701             break;
702          }
703       }
704       UnlockRes();
705       if (found) {
706          jcr->device = device;
707          dev = device->dev;
708          P(dev->mutex);               /* Use P to avoid indefinite block */
709          if (!(dev->state & ST_OPENED)) {
710             Dmsg0(90, "Device already released\n");
711             bnet_fsend(dir, _("3911 Device %s already released.\n"), dev_name(dev));
712
713          } else if (dev->dev_blocked == BST_WAITING_FOR_SYSOP ||
714                     dev->dev_blocked == BST_UNMOUNTED_WAITING_FOR_SYSOP) {
715             Dmsg2(90, "%d waiter dev_block=%d. doing unmount\n", dev->num_waiting,
716                dev->dev_blocked);
717             bnet_fsend(dir, _("3912 Device %s waiting for mount.\n"), dev_name(dev));
718
719          } else if (dev->dev_blocked == BST_DOING_ACQUIRE) {
720             bnet_fsend(dir, _("3913 Device %s is busy in acquire.\n"),
721                dev_name(dev));
722
723          } else if (dev->dev_blocked == BST_WRITING_LABEL) {
724             bnet_fsend(dir, _("3914 Device %s is being labeled.\n"),
725                dev_name(dev));
726
727          } else if (dev->state & ST_READ || dev->num_writers) {
728             if (dev->state & ST_READ) {
729                 Dmsg0(90, "Device in read mode\n");
730                 bnet_fsend(dir, _("3915 Device %s is busy with 1 reader.\n"),
731                    dev_name(dev));
732             } else {
733                 Dmsg1(90, "Device busy with %d writers\n", dev->num_writers);
734                 bnet_fsend(dir, _("3916 Device %s is busy with %d writer(s).\n"),
735                    dev_name(dev), dev->num_writers);
736             }
737
738          } else {                     /* device not being used */
739             Dmsg0(90, "Device not in use, unmounting\n");
740             release_volume(jcr, dev);
741             bnet_fsend(dir, _("3012 Device %s released.\n"), dev_name(dev));
742          }
743          V(dev->mutex);
744       } else {
745          bnet_fsend(dir, _("3999 Device %s not found\n"), dname);
746       }
747    } else {
748       /* NB dir->msg gets clobbered in bnet_fsend, so save command */
749       pm_strcpy(&jcr->errmsg, dir->msg);
750       bnet_fsend(dir, _("3917 Error scanning release command: %s\n"), jcr->errmsg);
751    }
752    free_memory(dname);
753    bnet_sig(dir, BNET_EOD);
754    return 1;
755 }
756
757
758
759 /*
760  * Autochanger command from Director
761  */
762 static int autochanger_cmd(JCR *jcr)
763 {
764    POOLMEM *devname;
765    BSOCK *dir = jcr->dir_bsock;
766    DEVRES *device;
767    DEVICE *dev;
768    int found = 0;
769
770    devname = get_memory(dir->msglen+1);
771    if (sscanf(dir->msg, "autochanger list %s ", devname) == 1) {
772       unbash_spaces(devname);
773       device = NULL;
774       LockRes();
775       while ((device=(DEVRES *)GetNextRes(R_DEVICE, (RES *)device))) {
776          /* Find resource, and make sure we were able to open it */
777          if (strcmp(device->hdr.name, devname) == 0 && device->dev) {
778             Dmsg1(20, "Found device %s\n", device->hdr.name);
779             found = 1;
780             break;
781          }
782       }
783       UnlockRes();
784       if (found) {
785          jcr->device = device;
786          dev = device->dev;
787          P(dev->mutex);               /* Use P to avoid indefinite block */
788          if (!dev_is_tape(dev)) {
789             bnet_fsend(dir, _("3995 Device %s is not an autochanger.\n"), 
790                dev_name(dev));
791          } else if (!(dev->state & ST_OPENED)) {
792             if (open_dev(dev, NULL, READ_WRITE) < 0) {
793                bnet_fsend(dir, _("3994 Connot open device: %s\n"), strerror_dev(dev));
794             } else {
795                autochanger_list(jcr, dev, dir);
796                force_close_dev(dev);
797             }
798          /* Under certain "safe" conditions, we can steal the lock */
799          } else if (dev->dev_blocked && 
800                     (dev->dev_blocked == BST_UNMOUNTED ||
801                      dev->dev_blocked == BST_WAITING_FOR_SYSOP ||
802                      dev->dev_blocked == BST_UNMOUNTED_WAITING_FOR_SYSOP)) {
803             autochanger_list(jcr, dev, dir);
804          } else if (dev->state & ST_READ || dev->num_writers) {
805             if (dev->state & ST_READ) {
806                 bnet_fsend(dir, _("3901 Device %s is busy with 1 reader.\n"),
807                    dev_name(dev));
808             } else {
809                 bnet_fsend(dir, _("3902 Device %s is busy with %d writer(s).\n"),
810                    dev_name(dev), dev->num_writers);
811             }
812          } else {                     /* device not being used */
813             autochanger_list(jcr, dev, dir);
814          }
815          V(dev->mutex);
816       } else {
817          bnet_fsend(dir, _("3999 Device %s not found\n"), devname);
818       }
819    } else {  /* error on scanf */
820       pm_strcpy(&jcr->errmsg, dir->msg);
821       bnet_fsend(dir, _("3907 Error scanning autocharger list command: %s\n"),
822          jcr->errmsg);
823    }
824    free_memory(devname);
825    bnet_sig(dir, BNET_EOD);
826    return 1;
827 }