2 * This file handles accepting Director Commands
4 * Most Director commands are handled here, with the
5 * exception of the Job command command and subsequent
6 * subcommands that are handled
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 because a Job
12 * thread has the device blocked. In some "safe" cases,
13 * we can do things to a blocked device. CAREFUL!!!!
15 * File daemon commands are handled in fdcmd.c
17 * Kern Sibbald, May MMI
23 Copyright (C) 2001-2006 Kern Sibbald
25 This program is free software; you can redistribute it and/or
26 modify it under the terms of the GNU General Public License
27 version 2 as amended with additional clauses defined in the
28 file LICENSE in the main source directory.
30 This program is distributed in the hope that it will be useful,
31 but WITHOUT ANY WARRANTY; without even the implied warranty of
32 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
33 the file LICENSE for additional details.
40 /* Exported variables */
42 /* Imported variables */
43 extern BSOCK *filed_chan;
44 extern int r_first, r_last;
45 extern struct s_res resources[];
46 extern char my_name[];
47 extern time_t daemon_start_time;
48 extern struct s_last_job last_job;
49 extern bool init_done;
51 /* Static variables */
52 static char derrmsg[] = "3900 Invalid command\n";
53 static char OKsetdebug[] = "3000 OK setdebug=%d\n";
54 static char illegal_cmd[] = "3997 Illegal command for a Director with Monitor directive enabled\n";
56 /* Imported functions */
57 extern void terminate_child();
58 extern bool job_cmd(JCR *jcr);
59 extern bool use_cmd(JCR *jcr);
60 extern bool run_cmd(JCR *jcr);
61 extern bool status_cmd(JCR *sjcr);
62 extern bool qstatus_cmd(JCR *jcr);
63 //extern bool query_cmd(JCR *jcr);
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 bootstrap_cmd(JCR *jcr);
75 static bool changer_cmd(JCR *sjcr);
76 static bool do_label(JCR *jcr, int relabel);
77 static DCR *find_device(JCR *jcr, POOL_MEM &dev_name, int drive);
78 static void read_volume_label(JCR *jcr, DEVICE *dev, int Slot);
79 static void label_volume_if_ok(DCR *dcr, char *oldname,
80 char *newname, char *poolname,
81 int Slot, int relabel);
82 static bool try_autoload_device(JCR *jcr, int slot, const char *VolName);
83 static void send_dir_busy_message(BSOCK *dir, DEVICE *dev);
87 bool (*func)(JCR *jcr);
88 int monitoraccess; /* specify if monitors have access to this function */
92 * The following are the recognized commands from the Director.
94 static struct s_cmds cmds[] = {
95 {"JobId=", job_cmd, 0}, /* start Job */
96 {"autochanger", changer_cmd, 0},
97 {"bootstrap", bootstrap_cmd, 0},
98 {"cancel", cancel_cmd, 0},
99 {"label", label_cmd, 0}, /* label a tape */
100 {"mount", mount_cmd, 0},
101 {"readlabel", readlabel_cmd, 0},
102 {"release", release_cmd, 0},
103 {"relabel", relabel_cmd, 0}, /* relabel a tape */
104 {"setdebug=", setdebug_cmd, 0}, /* set debug level */
105 {"status", status_cmd, 1},
106 {".status", qstatus_cmd, 1},
107 {"unmount", unmount_cmd, 0},
108 {"use storage=", use_cmd, 0},
110 // {"query", query_cmd, 0},
111 {NULL, NULL} /* list terminator */
116 * Connection request. We accept connections either from the
117 * Director or a Client (File daemon).
119 * Note, we are running as a seperate thread of the Storage daemon.
120 * and it is because a Director has made a connection with
121 * us on the "Message" channel.
123 * Basic tasks done here:
124 * - Create a JCR record
125 * - If it was from the FD, call handle_filed_connection()
126 * - Authenticate the Director
127 * - We wait for a command
128 * - We execute the command
129 * - We continue or exit depending on the return status
131 void *handle_connection_request(void *arg)
133 BSOCK *bs = (BSOCK *)arg;
138 char name[MAX_NAME_LENGTH];
140 if (bnet_recv(bs) <= 0) {
141 Emsg0(M_ERROR, 0, _("Connection request failed.\n"));
147 * Do a sanity check on the message received
149 if (bs->msglen < 25 || bs->msglen > (int)sizeof(name)-25) {
150 Emsg1(M_ERROR, 0, _("Invalid connection. Len=%d\n"), bs->msglen);
155 * See if this is a File daemon connection. If so
158 Dmsg1(110, "Conn: %s", bs->msg);
159 if (sscanf(bs->msg, "Hello Start Job %127s", name) == 1) {
160 handle_filed_connection(bs, name);
165 * This is a connection from the Director, so setup a JCR
167 Dmsg0(110, "Start Dir Job\n");
168 jcr = new_jcr(sizeof(JCR), stored_free_jcr); /* create Job Control Record */
169 jcr->dir_bsock = bs; /* save Director bsock */
170 jcr->dir_bsock->jcr = jcr;
171 jcr->dcrs = New(alist(10, not_owned_by_alist));
172 /* Initialize FD start condition variable */
173 int errstat = pthread_cond_init(&jcr->job_start_wait, NULL);
175 Jmsg1(jcr, M_FATAL, 0, _("Unable to init job cond variable: ERR=%s\n"), strerror(errstat));
179 Dmsg0(1000, "stored in start_job\n");
182 * Authenticate the Director
184 if (!authenticate_director(jcr)) {
185 Jmsg(jcr, M_FATAL, 0, _("Unable to authenticate Director\n"));
188 Dmsg0(90, "Message channel init completed.\n");
190 for (quit=false; !quit;) {
192 if ((bnet_stat = bnet_recv(bs)) <= 0) {
193 break; /* connection terminated */
195 Dmsg1(199, "<dird: %s\n", bs->msg);
196 /* Ensure that device initialization is complete */
201 for (i=0; cmds[i].cmd; i++) {
202 if (strncmp(cmds[i].cmd, bs->msg, strlen(cmds[i].cmd)) == 0) {
203 if ((!cmds[i].monitoraccess) && (jcr->director->monitor)) {
204 Dmsg1(100, "Command %s illegal.\n", cmds[i].cmd);
205 bnet_fsend(bs, illegal_cmd);
206 bnet_sig(bs, BNET_EOD);
209 Dmsg1(200, "Do command: %s\n", cmds[i].cmd);
210 if (!cmds[i].func(jcr)) { /* do command */
211 quit = true; /* error, get out */
212 Dmsg1(190, "Command %s requsts quit\n", cmds[i].cmd);
214 found = true; /* indicate command found */
218 if (!found) { /* command not found */
219 bnet_fsend(bs, derrmsg);
224 generate_daemon_event(jcr, "JobEnd");
225 dequeue_messages(jcr); /* send any queued messages */
226 bnet_sig(bs, BNET_TERMINATE);
232 * Set debug level as requested by the Director
235 static bool setdebug_cmd(JCR *jcr)
237 BSOCK *dir = jcr->dir_bsock;
238 int level, trace_flag;
240 Dmsg1(10, "setdebug_cmd: %s", dir->msg);
241 if (sscanf(dir->msg, "setdebug=%d trace=%d", &level, &trace_flag) != 2 || level < 0) {
242 bnet_fsend(dir, _("3991 Bad setdebug command: %s\n"), dir->msg);
246 set_trace(trace_flag);
247 return bnet_fsend(dir, OKsetdebug, level);
254 static bool cancel_cmd(JCR *cjcr)
256 BSOCK *dir = cjcr->dir_bsock;
258 char Job[MAX_NAME_LENGTH];
261 if (sscanf(dir->msg, "cancel Job=%127s", Job) == 1) {
262 if (!(jcr=get_jcr_by_full_name(Job))) {
263 bnet_fsend(dir, _("3904 Job %s not found.\n"), Job);
266 oldStatus = jcr->JobStatus;
267 set_jcr_job_status(jcr, JS_Canceled);
268 if (!jcr->authenticated && oldStatus == JS_WaitFD) {
269 pthread_cond_signal(&jcr->job_start_wait); /* wake waiting thread */
272 if (jcr->file_bsock) {
273 bnet_sig(jcr->file_bsock, BNET_TERMINATE);
275 /* If thread waiting on mount, wake him */
276 if (jcr->dcr && jcr->dcr->dev && jcr->dcr->dev->waiting_for_mount()) {
277 pthread_cond_broadcast(&jcr->dcr->dev->wait_next_vol);
278 pthread_cond_broadcast(&wait_device_release);
280 if (jcr->read_dcr && jcr->read_dcr->dev && jcr->read_dcr->dev->waiting_for_mount()) {
281 pthread_cond_broadcast(&jcr->read_dcr->dev->wait_next_vol);
282 pthread_cond_broadcast(&wait_device_release);
284 bnet_fsend(dir, _("3000 Job %s marked to be canceled.\n"), jcr->Job);
288 bnet_fsend(dir, _("3903 Error scanning cancel command.\n"));
290 bnet_sig(dir, BNET_EOD);
298 static bool label_cmd(JCR *jcr)
300 return do_label(jcr, 0);
303 static bool relabel_cmd(JCR *jcr)
305 return do_label(jcr, 1);
308 static bool do_label(JCR *jcr, int relabel)
310 POOLMEM *newname, *oldname, *poolname, *mtype;
312 BSOCK *dir = jcr->dir_bsock;
319 newname = get_memory(dir->msglen+1);
320 oldname = get_memory(dir->msglen+1);
321 poolname = get_memory(dir->msglen+1);
322 mtype = get_memory(dir->msglen+1);
324 if (sscanf(dir->msg, "relabel %127s OldName=%127s NewName=%127s PoolName=%127s "
325 "MediaType=%127s Slot=%d drive=%d",
326 dev_name.c_str(), oldname, newname, poolname, mtype,
327 &slot, &drive) == 7) {
332 if (sscanf(dir->msg, "label %127s VolumeName=%127s PoolName=%127s "
333 "MediaType=%127s Slot=%d drive=%d",
334 dev_name.c_str(), newname, poolname, mtype, &slot, &drive) == 6) {
339 unbash_spaces(newname);
340 unbash_spaces(oldname);
341 unbash_spaces(poolname);
342 unbash_spaces(mtype);
343 dcr = find_device(jcr, dev_name, drive);
346 P(dev->mutex); /* Use P to avoid indefinite block */
347 if (!dev->is_open()) {
348 Dmsg1(400, "Can %slabel. Device is not open\n", relabel?"re":"");
349 label_volume_if_ok(dcr, oldname, newname, poolname, slot, relabel);
351 /* Under certain "safe" conditions, we can steal the lock */
352 } else if (dev->can_steal_lock()) {
353 Dmsg0(400, "Can relabel. can_steal_lock\n");
354 label_volume_if_ok(dcr, oldname, newname, poolname, slot, relabel);
355 } else if (dev->is_busy() || dev->is_blocked()) {
356 send_dir_busy_message(dir, dev);
357 } else { /* device not being used */
358 Dmsg0(400, "Can relabel. device not used\n");
359 label_volume_if_ok(dcr, oldname, newname, poolname, slot, relabel);
365 bnet_fsend(dir, _("3999 Device \"%s\" not found or could not be opened.\n"), dev_name.c_str());
368 /* NB dir->msg gets clobbered in bnet_fsend, so save command */
369 pm_strcpy(jcr->errmsg, dir->msg);
370 bnet_fsend(dir, _("3903 Error scanning label command: %s\n"), jcr->errmsg);
372 free_memory(oldname);
373 free_memory(newname);
374 free_memory(poolname);
376 bnet_sig(dir, BNET_EOD);
381 * Read the tape label and determine if we can safely
382 * label the tape (not a Bacula volume), then label it.
384 * Enter with the mutex set
386 static void label_volume_if_ok(DCR *dcr, char *oldname,
387 char *newname, char *poolname,
388 int slot, int relabel)
390 BSOCK *dir = dcr->jcr->dir_bsock;
392 DEVICE *dev = dcr->dev;
396 steal_device_lock(dev, &hold, BST_WRITING_LABEL);
397 Dmsg1(100, "Stole device %s lock, writing label.\n", dev->print_name());
399 if (!try_autoload_device(dcr->jcr, slot, newname)) {
400 goto bail_out; /* error */
403 /* Ensure that the device is open -- autoload_device() closes it */
404 if (dev->is_tape()) {
405 mode = OPEN_READ_WRITE;
407 mode = CREATE_READ_WRITE;
409 if (dev->open(dcr, mode) < 0) {
410 bnet_fsend(dir, _("3910 Unable to open device %s: ERR=%s\n"),
411 dev->print_name(), dev->strerror());
415 /* See what we have for a Volume */
416 label_status = read_dev_volume_label(dcr);
418 switch(label_status) {
420 case VOL_VERSION_ERROR:
421 case VOL_LABEL_ERROR:
425 "3920 Cannot label Volume because it is already labeled: \"%s\"\n"),
426 dev->VolHdr.VolumeName);
430 /* Relabel request. If oldname matches, continue */
431 if (strcmp(oldname, dev->VolHdr.VolumeName) != 0) {
432 bnet_fsend(dir, _("3921 Wrong volume mounted.\n"));
435 if (dev->label_type != B_BACULA_LABEL) {
436 bnet_fsend(dir, _("3922 Cannot relabel an ANSI/IBM labeled Volume.\n"));
439 free_volume(dev); /* release old volume name */
440 /* Fall through wanted! */
443 if (!write_new_volume_label_to_dev(dcr, newname, poolname)) {
444 bnet_fsend(dir, _("3912 Failed to label Volume: ERR=%s\n"), dev->bstrerror());
447 bstrncpy(dcr->VolumeName, newname, sizeof(dcr->VolumeName));
448 /* The following 3000 OK label. string is scanned in ua_label.c */
449 bnet_fsend(dir, "3000 OK label. Volume=%s Device=%s\n",
450 newname, dev->print_name());
453 bnet_fsend(dir, _("3912 Failed to label Volume: ERR=%s\n"), dev->bstrerror());
456 bnet_fsend(dir, _("3913 Cannot label Volume. "
457 "Unknown status %d from read_volume_label()\n"), label_status);
462 if (!dev->is_open()) {
465 give_back_device_lock(dev, &hold);
471 * Read the tape label
473 * Enter with the mutex set
475 static bool read_label(DCR *dcr)
479 BSOCK *dir = jcr->dir_bsock;
481 DEVICE *dev = dcr->dev;
483 steal_device_lock(dev, &hold, BST_DOING_ACQUIRE);
485 dcr->VolumeName[0] = 0;
486 dev->clear_labeled(); /* force read of label */
487 switch (read_dev_volume_label(dcr)) {
489 bnet_fsend(dir, _("3001 Mounted Volume: %s\n"), dev->VolHdr.VolumeName);
493 bnet_fsend(dir, _("3902 Cannot mount Volume on Storage Device %s because:\n%s"),
494 dev->print_name(), jcr->errmsg);
498 give_back_device_lock(dev, &hold);
503 * Searches for device by name, and if found, creates a dcr and
506 static DCR *find_device(JCR *jcr, POOL_MEM &devname, int drive)
509 AUTOCHANGER *changer;
513 unbash_spaces(devname);
514 foreach_res(device, R_DEVICE) {
515 /* Find resource, and make sure we were able to open it */
516 if (fnmatch(device->hdr.name, devname.c_str(), 0) == 0) {
518 device->dev = init_dev(jcr, device);
521 Jmsg(jcr, M_WARNING, 0, _("\n"
522 " Device \"%s\" requested by DIR could not be opened or does not exist.\n"),
526 Dmsg1(20, "Found device %s\n", device->hdr.name);
532 foreach_res(changer, R_AUTOCHANGER) {
533 /* Find resource, and make sure we were able to open it */
534 if (fnmatch(devname.c_str(), changer->hdr.name, 0) == 0) {
535 /* Try each device in this AutoChanger */
536 foreach_alist(device, changer->device) {
537 Dmsg1(100, "Try changer device %s\n", device->hdr.name);
539 device->dev = init_dev(jcr, device);
542 Dmsg1(100, "Device %s could not be opened. Skipped\n", devname.c_str());
543 Jmsg(jcr, M_WARNING, 0, _("\n"
544 " Device \"%s\" in changer \"%s\" requested by DIR could not be opened or does not exist.\n"),
545 device->hdr.name, devname.c_str());
548 if (!device->dev->autoselect) {
549 Dmsg1(100, "Device %s not autoselect skipped.\n", devname.c_str());
550 continue; /* device is not available */
552 if (drive < 0 || drive == (int)device->dev->drive_index) {
553 Dmsg1(20, "Found changer device %s\n", device->hdr.name);
557 Dmsg3(100, "Device %s drive wrong: want=%d got=%d skipping\n",
558 devname.c_str(), drive, (int)device->dev->drive_index);
560 break; /* we found it but could not open a device */
566 Dmsg1(100, "Found device %s\n", device->hdr.name);
567 dcr = new_dcr(jcr, device->dev);
568 dcr->device = device;
576 * Mount command from Director
578 static bool mount_cmd(JCR *jcr)
581 BSOCK *dir = jcr->dir_bsock;
586 if (sscanf(dir->msg, "mount %127s drive=%d", devname.c_str(), &drive) == 2) {
587 dcr = find_device(jcr, devname, drive);
590 P(dev->mutex); /* Use P to avoid indefinite block */
591 Dmsg1(100, "mount cmd blocked=%d\n", dev->dev_blocked);
592 switch (dev->dev_blocked) { /* device blocked? */
593 case BST_WAITING_FOR_SYSOP:
594 /* Someone is waiting, wake him */
595 Dmsg0(100, "Waiting for mount. Attempting to wake thread\n");
596 dev->dev_blocked = BST_MOUNT;
597 bnet_fsend(dir, "3001 OK mount. Device=%s\n",
599 pthread_cond_broadcast(&dev->wait_next_vol);
600 pthread_cond_broadcast(&wait_device_release);
603 /* In both of these two cases, we (the user) unmounted the Volume */
604 case BST_UNMOUNTED_WAITING_FOR_SYSOP:
606 /* We freed the device, so reopen it and wake any waiting threads */
607 if (dev->open(dcr, OPEN_READ_ONLY) < 0) {
608 bnet_fsend(dir, _("3901 open device failed: ERR=%s\n"),
610 if (dev->dev_blocked == BST_UNMOUNTED) {
611 /* We blocked the device, so unblock it */
612 Dmsg0(100, "Unmounted. Unblocking device\n");
617 read_dev_volume_label(dcr);
618 if (dev->dev_blocked == BST_UNMOUNTED) {
619 /* We blocked the device, so unblock it */
620 Dmsg0(100, "Unmounted. Unblocking device\n");
621 read_label(dcr); /* this should not be necessary */
624 Dmsg0(100, "Unmounted waiting for mount. Attempting to wake thread\n");
625 dev->dev_blocked = BST_MOUNT;
627 if (dev->is_labeled()) {
628 bnet_fsend(dir, _("3001 Device %s is mounted with Volume \"%s\"\n"),
629 dev->print_name(), dev->VolHdr.VolumeName);
631 bnet_fsend(dir, _("3905 Device %s open but no Bacula volume is mounted.\n"
632 "If this is not a blank tape, try unmounting and remounting the Volume.\n"),
635 pthread_cond_broadcast(&dev->wait_next_vol);
636 pthread_cond_broadcast(&wait_device_release);
639 case BST_DOING_ACQUIRE:
640 bnet_fsend(dir, _("3001 Device %s is doing acquire.\n"),
644 case BST_WRITING_LABEL:
645 bnet_fsend(dir, _("3903 Device %s is being labeled.\n"),
649 case BST_NOT_BLOCKED:
650 if (dev->is_open()) {
651 if (dev->is_labeled()) {
652 bnet_fsend(dir, _("3001 Device %s is mounted with Volume \"%s\"\n"),
653 dev->print_name(), dev->VolHdr.VolumeName);
655 bnet_fsend(dir, _("3905 Device %s open but no Bacula volume is mounted.\n"
656 "If this is not a blank tape, try unmounting and remounting the Volume.\n"),
659 } else if (dev->is_tape()) {
660 if (dev->open(dcr, OPEN_READ_ONLY) < 0) {
661 bnet_fsend(dir, _("3901 open device failed: ERR=%s\n"),
666 if (dev->is_labeled()) {
667 bnet_fsend(dir, _("3001 Device %s is already mounted with Volume \"%s\"\n"),
668 dev->print_name(), dev->VolHdr.VolumeName);
670 bnet_fsend(dir, _("3905 Device %s open but no Bacula volume is mounted.\n"
671 "If this is not a blank tape, try unmounting and remounting the Volume.\n"),
674 } else if (dev->is_dvd()) {
675 if (mount_dvd(dev, 1)) {
676 bnet_fsend(dir, _("3002 Device %s is mounted.\n"),
679 bnet_fsend(dir, _("3907 %s"), dev->bstrerror());
681 } else { /* must be file */
682 bnet_fsend(dir, _("3906 File device %s is always mounted.\n"),
688 bnet_fsend(dir, _("3905 Bizarre wait state %d\n"), dev->dev_blocked);
695 bnet_fsend(dir, _("3999 Device \"%s\" not found or could not be opened.\n"), devname.c_str());
698 pm_strcpy(jcr->errmsg, dir->msg);
699 bnet_fsend(dir, _("3909 Error scanning mount command: %s\n"), jcr->errmsg);
701 bnet_sig(dir, BNET_EOD);
706 * unmount command from Director
708 static bool unmount_cmd(JCR *jcr)
711 BSOCK *dir = jcr->dir_bsock;
716 if (sscanf(dir->msg, "unmount %127s drive=%d", devname.c_str(), &drive) == 2) {
717 dcr = find_device(jcr, devname, drive);
720 P(dev->mutex); /* Use P to avoid indefinite block */
721 if (!dev->is_open()) {
722 if (!dev->is_busy()) {
723 unload_autochanger(jcr->dcr, -1);
725 Dmsg0(90, "Device already unmounted\n");
726 bnet_fsend(dir, _("3901 Device %s is already unmounted.\n"),
729 } else if (dev->dev_blocked == BST_WAITING_FOR_SYSOP) {
730 Dmsg2(90, "%d waiter dev_block=%d. doing unmount\n", dev->num_waiting,
732 if (!unload_autochanger(jcr->dcr, -1)) {
735 dev->dev_blocked = BST_UNMOUNTED_WAITING_FOR_SYSOP;
736 bnet_fsend(dir, _("3001 Device %s unmounted.\n"),
739 } else if (dev->dev_blocked == BST_DOING_ACQUIRE) {
740 bnet_fsend(dir, _("3902 Device %s is busy in acquire.\n"),
743 } else if (dev->dev_blocked == BST_WRITING_LABEL) {
744 bnet_fsend(dir, _("3903 Device %s is being labeled.\n"),
747 } else if (dev->is_busy()) {
748 send_dir_busy_message(dir, dev);
749 } else { /* device not being used */
750 Dmsg0(90, "Device not in use, unmounting\n");
751 /* On FreeBSD, I am having ASSERT() failures in block_device()
752 * and I can only imagine that the thread id that we are
753 * leaving in no_wait_id is being re-used. So here,
754 * we simply do it by hand. Gross, but a solution.
756 /* block_device(dev, BST_UNMOUNTED); replace with 2 lines below */
757 dev->dev_blocked = BST_UNMOUNTED;
759 if (!unload_autochanger(jcr->dcr, -1)) {
762 bnet_fsend(dir, _("3002 Device %s unmounted.\n"),
769 bnet_fsend(dir, _("3999 Device \"%s\" not found or could not be opened.\n"), devname.c_str());
772 /* NB dir->msg gets clobbered in bnet_fsend, so save command */
773 pm_strcpy(jcr->errmsg, dir->msg);
774 bnet_fsend(dir, _("3907 Error scanning unmount command: %s\n"), jcr->errmsg);
776 bnet_sig(dir, BNET_EOD);
781 * Release command from Director. This rewinds the device and if
782 * configured does a offline and ensures that Bacula will
783 * re-read the label of the tape before continuing. This gives
784 * the operator the chance to change the tape anytime before the
787 static bool release_cmd(JCR *jcr)
790 BSOCK *dir = jcr->dir_bsock;
795 if (sscanf(dir->msg, "release %127s drive=%d", devname.c_str(), &drive) == 2) {
796 dcr = find_device(jcr, devname, drive);
799 P(dev->mutex); /* Use P to avoid indefinite block */
800 if (!dev->is_open()) {
801 Dmsg0(90, "Device already released\n");
802 bnet_fsend(dir, _("3921 Device %s already released.\n"),
805 } else if (dev->dev_blocked == BST_WAITING_FOR_SYSOP ||
806 dev->dev_blocked == BST_UNMOUNTED_WAITING_FOR_SYSOP) {
807 Dmsg2(90, "%d waiter dev_block=%d. doing unmount\n", dev->num_waiting,
809 bnet_fsend(dir, _("3922 Device %s waiting for mount.\n"),
812 } else if (dev->dev_blocked == BST_DOING_ACQUIRE) {
813 bnet_fsend(dir, _("3923 Device %s is busy in acquire.\n"),
816 } else if (dev->dev_blocked == BST_WRITING_LABEL) {
817 bnet_fsend(dir, _("3914 Device %s is being labeled.\n"),
820 } else if (dev->is_busy()) {
821 send_dir_busy_message(dir, dev);
822 } else { /* device not being used */
823 Dmsg0(90, "Device not in use, unmounting\n");
824 release_volume(jcr->dcr);
825 bnet_fsend(dir, _("3022 Device %s released.\n"),
832 bnet_fsend(dir, _("3999 Device \"%s\" not found or could not be opened.\n"), devname.c_str());
835 /* NB dir->msg gets clobbered in bnet_fsend, so save command */
836 pm_strcpy(jcr->errmsg, dir->msg);
837 bnet_fsend(dir, _("3927 Error scanning release command: %s\n"), jcr->errmsg);
839 bnet_sig(dir, BNET_EOD);
844 static bool bootstrap_cmd(JCR *jcr)
846 return get_bootstrap_file(jcr, jcr->dir_bsock);
850 * Autochanger command from Director
852 static bool changer_cmd(JCR *jcr)
855 BSOCK *dir = jcr->dir_bsock;
858 const char *cmd = NULL;
861 if (sscanf(dir->msg, "autochanger list %127s", devname.c_str()) == 1) {
864 } else if (sscanf(dir->msg, "autochanger slots %127s", devname.c_str()) == 1) {
867 } else if (sscanf(dir->msg, "autochanger drives %127s", devname.c_str()) == 1) {
872 dcr = find_device(jcr, devname, -1);
875 P(dev->mutex); /* Use P to avoid indefinite block */
876 if (!dev->device->changer_res) {
877 bnet_fsend(dir, _("3995 Device %s is not an autochanger.\n"),
879 /* Under certain "safe" conditions, we can steal the lock */
880 } else if (!dev->is_open() || dev->can_steal_lock()) {
881 autochanger_cmd(dcr, dir, cmd);
882 } else if (dev->is_busy() || dev->is_blocked()) {
883 send_dir_busy_message(dir, dev);
884 } else { /* device not being used */
885 autochanger_cmd(dcr, dir, cmd);
891 bnet_fsend(dir, _("3999 Device \"%s\" not found or could not be opened.\n"), devname.c_str());
893 } else { /* error on scanf */
894 pm_strcpy(jcr->errmsg, dir->msg);
895 bnet_fsend(dir, _("3908 Error scanning autocharger drives/list/slots command: %s\n"),
898 bnet_sig(dir, BNET_EOD);
903 * Read and return the Volume label
905 static bool readlabel_cmd(JCR *jcr)
908 BSOCK *dir = jcr->dir_bsock;
914 if (sscanf(dir->msg, "readlabel %127s Slot=%d drive=%d", devname.c_str(),
915 &Slot, &drive) == 3) {
916 dcr = find_device(jcr, devname, drive);
919 P(dev->mutex); /* Use P to avoid indefinite block */
920 if (!dev->is_open()) {
921 read_volume_label(jcr, dev, Slot);
923 /* Under certain "safe" conditions, we can steal the lock */
924 } else if (dev->can_steal_lock()) {
925 read_volume_label(jcr, dev, Slot);
926 } else if (dev->is_busy() || dev->is_blocked()) {
927 send_dir_busy_message(dir, dev);
928 } else { /* device not being used */
929 read_volume_label(jcr, dev, Slot);
935 bnet_fsend(dir, _("3999 Device \"%s\" not found or could not be opened.\n"), devname.c_str());
938 pm_strcpy(jcr->errmsg, dir->msg);
939 bnet_fsend(dir, _("3909 Error scanning readlabel command: %s\n"), jcr->errmsg);
941 bnet_sig(dir, BNET_EOD);
947 * Read the tape label
949 * Enter with the mutex set
951 static void read_volume_label(JCR *jcr, DEVICE *dev, int Slot)
953 BSOCK *dir = jcr->dir_bsock;
958 steal_device_lock(dev, &hold, BST_WRITING_LABEL);
960 if (!try_autoload_device(jcr, Slot, "")) {
961 goto bail_out; /* error */
964 dev->clear_labeled(); /* force read of label */
965 switch (read_dev_volume_label(dcr)) {
967 /* DO NOT add quotes around the Volume name. It is scanned in the DIR */
968 bnet_fsend(dir, _("3001 Volume=%s Slot=%d\n"), dev->VolHdr.VolumeName, Slot);
969 Dmsg1(100, "Volume: %s\n", dev->VolHdr.VolumeName);
972 bnet_fsend(dir, _("3902 Cannot mount Volume on Storage Device %s because:\n%s"),
973 dev->print_name(), jcr->errmsg);
978 give_back_device_lock(dev, &hold);
982 static bool try_autoload_device(JCR *jcr, int slot, const char *VolName)
985 BSOCK *dir = jcr->dir_bsock;
987 bstrncpy(dcr->VolumeName, VolName, sizeof(dcr->VolumeName));
988 dcr->VolCatInfo.Slot = slot;
989 dcr->VolCatInfo.InChanger = slot > 0;
990 if (autoload_device(dcr, 0, dir) < 0) { /* autoload if possible */
996 static void send_dir_busy_message(BSOCK *dir, DEVICE *dev)
998 if (dev->is_blocked()) {
999 switch (dev->dev_blocked) {
1001 bnet_fsend(dir, _("3931 Device %s is BLOCKED. user unmounted.\n"),
1004 case BST_UNMOUNTED_WAITING_FOR_SYSOP:
1005 bnet_fsend(dir, _("3932 Device %s is BLOCKED. user unmounted during wait for media/mount.\n"),
1008 case BST_WAITING_FOR_SYSOP:
1009 bnet_fsend(dir, _("3933 Device %s is BLOCKED waiting for media.\n"),
1012 case BST_DOING_ACQUIRE:
1013 bnet_fsend(dir, _("3934 Device %s is being initialized.\n"),
1016 case BST_WRITING_LABEL:
1017 bnet_fsend(dir, _("3935 Device %s is blocked labeling a Volume.\n"),
1021 bnet_fsend(dir, _("3935 Device %s is blocked for unknown reason.\n"),
1025 } else if (dev->can_read()) {
1026 bnet_fsend(dir, _("3936 Device %s is busy reading.\n"),
1027 dev->print_name());;
1029 bnet_fsend(dir, _("3937 Device %s is busy with %d writer(s).\n"),
1030 dev->print_name(), dev->num_writers);