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-2005 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 autochanger_cmd(JCR *sjcr);
75 static bool do_label(JCR *jcr, int relabel);
76 static DEVICE *find_device(JCR *jcr, POOL_MEM &dev_name, int drive);
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 static bool try_autoload_device(JCR *jcr, int slot, const char *VolName);
82 static void send_dir_busy_message(BSOCK *dir, DEVICE *dev);
86 bool (*func)(JCR *jcr);
87 int monitoraccess; /* specify if monitors have access to this function */
91 * The following are the recognized commands from the Director.
93 static struct s_cmds cmds[] = {
94 {"JobId=", job_cmd, 0}, /* start Job */
95 {"autochanger", autochanger_cmd, 0},
96 {"bootstrap", bootstrap_cmd, 0},
97 {"cancel", cancel_cmd, 0},
98 {"label", label_cmd, 0}, /* label a tape */
99 {"mount", mount_cmd, 0},
100 {"readlabel", readlabel_cmd, 0},
101 {"release", release_cmd, 0},
102 {"relabel", relabel_cmd, 0}, /* relabel a tape */
103 {"setdebug=", setdebug_cmd, 0}, /* set debug level */
104 {"status", status_cmd, 1},
105 {".status", qstatus_cmd, 1},
106 {"unmount", unmount_cmd, 0},
107 {"use storage=", use_cmd, 0},
109 // {"query", query_cmd, 0},
110 {NULL, NULL} /* list terminator */
115 * Connection request. We accept connections either from the
116 * Director or a Client (File daemon).
118 * Note, we are running as a seperate thread of the Storage daemon.
119 * and it is because a Director has made a connection with
120 * us on the "Message" channel.
122 * Basic tasks done here:
123 * - Create a JCR record
124 * - If it was from the FD, call handle_filed_connection()
125 * - Authenticate the Director
126 * - We wait for a command
127 * - We execute the command
128 * - We continue or exit depending on the return status
130 void *handle_connection_request(void *arg)
132 BSOCK *bs = (BSOCK *)arg;
137 char name[MAX_NAME_LENGTH];
139 if (bnet_recv(bs) <= 0) {
140 Emsg0(M_ERROR, 0, _("Connection request failed.\n"));
146 * Do a sanity check on the message received
148 if (bs->msglen < 25 || bs->msglen > (int)sizeof(name)-25) {
149 Emsg1(M_ERROR, 0, _("Invalid connection. Len=%d\n"), bs->msglen);
154 * See if this is a File daemon connection. If so
157 Dmsg1(110, "Conn: %s", bs->msg);
158 if (sscanf(bs->msg, "Hello Start Job %127s", name) == 1) {
159 handle_filed_connection(bs, name);
164 * This is a connection from the Director, so setup a JCR
166 Dmsg0(110, "Start Dir Job\n");
167 jcr = new_jcr(sizeof(JCR), stored_free_jcr); /* create Job Control Record */
168 jcr->dir_bsock = bs; /* save Director bsock */
169 jcr->dir_bsock->jcr = jcr;
170 jcr->dcrs = New(alist(10, not_owned_by_alist));
171 /* Initialize FD start condition variable */
172 int errstat = pthread_cond_init(&jcr->job_start_wait, NULL);
174 Jmsg1(jcr, M_FATAL, 0, _("Unable to init job cond variable: ERR=%s\n"), strerror(errstat));
178 Dmsg0(1000, "stored in start_job\n");
181 * Authenticate the Director
183 if (!authenticate_director(jcr)) {
184 Jmsg(jcr, M_FATAL, 0, _("Unable to authenticate Director\n"));
187 Dmsg0(90, "Message channel init completed.\n");
189 for (quit=false; !quit;) {
191 if ((bnet_stat = bnet_recv(bs)) <= 0) {
192 break; /* connection terminated */
194 Dmsg1(199, "<dird: %s\n", bs->msg);
195 /* Ensure that device initialization is complete */
200 for (i=0; cmds[i].cmd; i++) {
201 if (strncmp(cmds[i].cmd, bs->msg, strlen(cmds[i].cmd)) == 0) {
202 if ((!cmds[i].monitoraccess) && (jcr->director->monitor)) {
203 Dmsg1(100, "Command %s illegal.\n", cmds[i].cmd);
204 bnet_fsend(bs, illegal_cmd);
205 bnet_sig(bs, BNET_EOD);
208 Dmsg1(200, "Do command: %s\n", cmds[i].cmd);
209 if (!cmds[i].func(jcr)) { /* do command */
210 quit = true; /* error, get out */
211 Dmsg1(190, "Command %s requsts quit\n", cmds[i].cmd);
213 found = true; /* indicate command found */
217 if (!found) { /* command not found */
218 bnet_fsend(bs, derrmsg);
223 generate_daemon_event(jcr, "JobEnd");
224 dequeue_messages(jcr); /* send any queued messages */
225 bnet_sig(bs, BNET_TERMINATE);
231 * Set debug level as requested by the Director
234 static bool setdebug_cmd(JCR *jcr)
236 BSOCK *dir = jcr->dir_bsock;
237 int level, trace_flag;
239 Dmsg1(10, "setdebug_cmd: %s", dir->msg);
240 if (sscanf(dir->msg, "setdebug=%d trace=%d", &level, &trace_flag) != 2 || level < 0) {
241 bnet_fsend(dir, _("3991 Bad setdebug command: %s\n"), dir->msg);
245 set_trace(trace_flag);
246 return bnet_fsend(dir, OKsetdebug, level);
253 static bool cancel_cmd(JCR *cjcr)
255 BSOCK *dir = cjcr->dir_bsock;
257 char Job[MAX_NAME_LENGTH];
260 if (sscanf(dir->msg, "cancel Job=%127s", Job) == 1) {
261 if (!(jcr=get_jcr_by_full_name(Job))) {
262 bnet_fsend(dir, _("3902 Job %s not found.\n"), Job);
265 oldStatus = jcr->JobStatus;
266 set_jcr_job_status(jcr, JS_Canceled);
267 if (!jcr->authenticated && oldStatus == JS_WaitFD) {
268 pthread_cond_signal(&jcr->job_start_wait); /* wake waiting thread */
271 if (jcr->file_bsock) {
272 bnet_sig(jcr->file_bsock, BNET_TERMINATE);
274 /* If thread waiting on mount, wake him */
275 if (jcr->dcr && jcr->dcr->dev && jcr->dcr->dev->waiting_for_mount()) {
276 pthread_cond_signal(&jcr->dcr->dev->wait_next_vol);
277 pthread_cond_broadcast(&wait_device_release);
279 bnet_fsend(dir, _("3000 Job %s marked to be canceled.\n"), jcr->Job);
283 bnet_fsend(dir, _("3903 Error scanning cancel command.\n"));
285 bnet_sig(dir, BNET_EOD);
293 static bool label_cmd(JCR *jcr)
295 return do_label(jcr, 0);
298 static bool relabel_cmd(JCR *jcr)
300 return do_label(jcr, 1);
303 static bool do_label(JCR *jcr, int relabel)
305 POOLMEM *newname, *oldname, *poolname, *mtype;
307 BSOCK *dir = jcr->dir_bsock;
313 newname = get_memory(dir->msglen+1);
314 oldname = get_memory(dir->msglen+1);
315 poolname = get_memory(dir->msglen+1);
316 mtype = get_memory(dir->msglen+1);
318 if (sscanf(dir->msg, "relabel %127s OldName=%127s NewName=%127s PoolName=%127s "
319 "MediaType=%127s Slot=%d drive=%d",
320 dev_name.c_str(), oldname, newname, poolname, mtype,
321 &slot, &drive) == 7) {
326 if (sscanf(dir->msg, "label %127s VolumeName=%127s PoolName=%127s "
327 "MediaType=%127s Slot=%d drive=%d",
328 dev_name.c_str(), newname, poolname, mtype, &slot, &drive) == 6) {
333 unbash_spaces(newname);
334 unbash_spaces(oldname);
335 unbash_spaces(poolname);
336 unbash_spaces(mtype);
337 dev = find_device(jcr, dev_name, drive);
339 P(dev->mutex); /* Use P to avoid indefinite block */
340 if (!dev->is_open()) {
341 Dmsg0(400, "Can relabel. Device is not open\n");
342 label_volume_if_ok(jcr, dev, oldname, newname, poolname, slot, relabel);
343 force_close_device(dev);
344 /* Under certain "safe" conditions, we can steal the lock */
345 } else if (dev->can_steal_lock()) {
346 Dmsg0(400, "Can relabel. can_steal_lock\n");
347 label_volume_if_ok(jcr, dev, oldname, newname, poolname, slot, relabel);
348 } else if (dev->is_busy() || dev->is_blocked()) {
349 send_dir_busy_message(dir, dev);
350 } else { /* device not being used */
351 Dmsg0(400, "Can relabel. device not used\n");
352 label_volume_if_ok(jcr, dev, oldname, newname, poolname, slot, relabel);
356 bnet_fsend(dir, _("3999 Device \"%s\" not found\n"), dev_name.c_str());
359 /* NB dir->msg gets clobbered in bnet_fsend, so save command */
360 pm_strcpy(jcr->errmsg, dir->msg);
361 bnet_fsend(dir, _("3903 Error scanning label command: %s\n"), jcr->errmsg);
363 free_memory(oldname);
364 free_memory(newname);
365 free_memory(poolname);
367 bnet_sig(dir, BNET_EOD);
372 * Read the tape label and determine if we can safely
373 * label the tape (not a Bacula volume), then label it.
375 * Enter with the mutex set
377 static void label_volume_if_ok(JCR *jcr, DEVICE *dev, char *oldname,
378 char *newname, char *poolname,
379 int slot, int relabel)
381 BSOCK *dir = jcr->dir_bsock;
387 steal_device_lock(dev, &hold, BST_WRITING_LABEL);
388 Dmsg1(100, "Stole device %s lock, writing label.\n", dev->print_name());
390 /* Note, try_autoload_device() opens the device */
391 if (!try_autoload_device(jcr, slot, newname)) {
392 goto bail_out; /* error */
395 /* See what we have for a Volume */
396 label_status = read_dev_volume_label(dcr);
398 switch(label_status) {
400 case VOL_VERSION_ERROR:
401 case VOL_LABEL_ERROR:
405 "3920 Cannot label Volume because it is already labeled: \"%s\"\n"),
406 dev->VolHdr.VolumeName);
410 /* Relabel request. If oldname matches, continue */
411 if (strcmp(oldname, dev->VolHdr.VolumeName) != 0) {
412 bnet_fsend(dir, _("3921 Wrong volume mounted.\n"));
415 if (dev->label_type != B_BACULA_LABEL) {
416 bnet_fsend(dir, _("3922 Cannot relabel an ANSI/IBM labeled Volume.\n"));
419 free_volume(dev); /* release old volume name */
420 /* Fall through wanted! */
423 if (!write_new_volume_label_to_dev(dcr, newname, poolname)) {
424 bnet_fsend(dir, _("3912 Failed to label Volume: ERR=%s\n"), strerror_dev(dev));
427 bstrncpy(dcr->VolumeName, newname, sizeof(dcr->VolumeName));
428 /* The following 3000 OK label. string is scanned in ua_label.c */
429 bnet_fsend(dir, "3000 OK label. Volume=%s Device=%s\n",
430 newname, dev->print_name());
433 bnet_fsend(dir, _("3912 Failed to label Volume: ERR=%s\n"), strerror_dev(dev));
436 bnet_fsend(dir, _("3913 Cannot label Volume. "
437 "Unknown status %d from read_volume_label()\n"), label_status);
442 if (!dev->is_open()) {
445 give_back_device_lock(dev, &hold);
451 * Read the tape label
453 * Enter with the mutex set
455 static bool read_label(DCR *dcr)
459 BSOCK *dir = jcr->dir_bsock;
461 DEVICE *dev = dcr->dev;
463 steal_device_lock(dev, &hold, BST_DOING_ACQUIRE);
465 dcr->VolumeName[0] = 0;
466 dev->clear_labeled(); /* force read of label */
467 switch (read_dev_volume_label(dcr)) {
469 bnet_fsend(dir, _("3001 Mounted Volume: %s\n"), dev->VolHdr.VolumeName);
473 bnet_fsend(dir, _("3902 Cannot mount Volume on Storage Device %s because:\n%s"),
474 dev->print_name(), jcr->errmsg);
478 give_back_device_lock(dev, &hold);
482 static DEVICE *find_device(JCR *jcr, POOL_MEM &devname, int drive)
485 AUTOCHANGER *changer;
488 unbash_spaces(devname);
489 foreach_res(device, R_DEVICE) {
490 /* Find resource, and make sure we were able to open it */
491 if (fnmatch(device->hdr.name, devname.c_str(), 0) == 0) {
493 device->dev = init_dev(jcr, device);
496 Jmsg(jcr, M_WARNING, 0, _("\n"
497 " Device \"%s\" requested by DIR could not be opened or does not exist.\n"),
501 Dmsg1(20, "Found device %s\n", device->hdr.name);
506 foreach_res(changer, R_AUTOCHANGER) {
507 /* Find resource, and make sure we were able to open it */
508 if (fnmatch(devname.c_str(), changer->hdr.name, 0) == 0) {
509 /* Try each device in this AutoChanger */
510 foreach_alist(device, changer->device) {
511 Dmsg1(100, "Try changer device %s\n", device->hdr.name);
513 device->dev = init_dev(jcr, device);
516 Dmsg1(100, "Device %s could not be opened. Skipped\n", devname.c_str());
517 Jmsg(jcr, M_WARNING, 0, _("\n"
518 " Device \"%s\" in changer \"%s\" requested by DIR could not be opened or does not exist.\n"),
519 device->hdr.name, devname.c_str());
522 if (!device->dev->autoselect) {
523 continue; /* device is not available */
525 if (drive < 0 || drive == (int)device->dev->drive_index) {
526 Dmsg1(20, "Found changer device %s\n", device->hdr.name);
531 break; /* we found it but could not open a device */
536 jcr->dcr = new_dcr(jcr, device->dev);
537 jcr->dcr->device = device;
538 return jcr->dcr->dev;
545 * Mount command from Director
547 static bool mount_cmd(JCR *jcr)
550 BSOCK *dir = jcr->dir_bsock;
555 if (sscanf(dir->msg, "mount %127s drive=%d", devname.c_str(), &drive) == 2) {
556 dev = find_device(jcr, devname, drive);
559 P(dev->mutex); /* Use P to avoid indefinite block */
560 switch (dev->dev_blocked) { /* device blocked? */
561 case BST_WAITING_FOR_SYSOP:
562 /* Someone is waiting, wake him */
563 Dmsg0(100, "Waiting for mount. Attempting to wake thread\n");
564 dev->dev_blocked = BST_MOUNT;
565 bnet_fsend(dir, "3001 OK mount. Device=%s\n",
567 pthread_cond_broadcast(&dev->wait_next_vol);
568 pthread_cond_broadcast(&wait_device_release);
571 /* In both of these two cases, we (the user) unmounted the Volume */
572 case BST_UNMOUNTED_WAITING_FOR_SYSOP:
574 /* We freed the device, so reopen it and wake any waiting threads */
575 if (dev->open(dcr, OPEN_READ_WRITE) < 0) {
576 bnet_fsend(dir, _("3901 open device failed: ERR=%s\n"),
580 read_dev_volume_label(dcr);
581 if (dev->dev_blocked == BST_UNMOUNTED) {
582 /* We blocked the device, so unblock it */
583 Dmsg0(100, "Unmounted. Unblocking device\n");
584 read_label(dcr); /* this should not be necessary */
587 Dmsg0(100, "Unmounted waiting for mount. Attempting to wake thread\n");
588 dev->dev_blocked = BST_MOUNT;
590 if (dev->is_labeled()) {
591 bnet_fsend(dir, _("3001 Device %s is mounted with Volume \"%s\"\n"),
592 dev->print_name(), dev->VolHdr.VolumeName);
594 bnet_fsend(dir, _("3905 Device %s open but no Bacula volume is mounted.\n"
595 "If this is not a blank tape, try unmounting and remounting the Volume.\n"),
598 pthread_cond_broadcast(&dev->wait_next_vol);
599 pthread_cond_broadcast(&wait_device_release);
602 case BST_DOING_ACQUIRE:
603 bnet_fsend(dir, _("3001 Device %s is doing acquire.\n"),
607 case BST_WRITING_LABEL:
608 bnet_fsend(dir, _("3903 Device %s is being labeled.\n"),
612 case BST_NOT_BLOCKED:
613 if (dev->is_open()) {
614 if (dev->is_labeled()) {
615 bnet_fsend(dir, _("3001 Device %s is mounted with Volume \"%s\"\n"),
616 dev->print_name(), dev->VolHdr.VolumeName);
618 bnet_fsend(dir, _("3905 Device %s open but no Bacula volume is mounted.\n"
619 "If this is not a blank tape, try unmounting and remounting the Volume.\n"),
622 } else if (dev->is_tape()) {
623 if (dev->open(dcr, OPEN_READ_WRITE) < 0) {
624 bnet_fsend(dir, _("3901 open device failed: ERR=%s\n"),
629 if (dev->is_labeled()) {
630 bnet_fsend(dir, _("3001 Device %s is already mounted with Volume \"%s\"\n"),
631 dev->print_name(), dev->VolHdr.VolumeName);
633 bnet_fsend(dir, _("3905 Device %s open but no Bacula volume is mounted.\n"
634 "If this is not a blank tape, try unmounting and remounting the Volume.\n"),
637 } else if (dev->is_dvd()) {
638 if (mount_dev(dev, 1)) {
639 bnet_fsend(dir, _("3002 Device %s is mounted.\n"),
642 bnet_fsend(dir, _("3907 %s"), strerror_dev(dev));
644 } else { /* must be file */
645 bnet_fsend(dir, _("3906 File device %s is always mounted.\n"),
651 bnet_fsend(dir, _("3905 Bizarre wait state %d\n"), dev->dev_blocked);
656 bnet_fsend(dir, _("3999 Device \"%s\" not found\n"), devname.c_str());
659 pm_strcpy(jcr->errmsg, dir->msg);
660 bnet_fsend(dir, _("3909 Error scanning mount command: %s\n"), jcr->errmsg);
662 bnet_sig(dir, BNET_EOD);
667 * unmount command from Director
669 static bool unmount_cmd(JCR *jcr)
672 BSOCK *dir = jcr->dir_bsock;
676 if (sscanf(dir->msg, "unmount %127s drive=%d", devname.c_str(), &drive) == 2) {
677 dev = find_device(jcr, devname, drive);
679 P(dev->mutex); /* Use P to avoid indefinite block */
680 if (!dev->is_open()) {
681 Dmsg0(90, "Device already unmounted\n");
682 bnet_fsend(dir, _("3901 Device %s is already unmounted.\n"),
685 } else if (dev->dev_blocked == BST_WAITING_FOR_SYSOP) {
686 Dmsg2(90, "%d waiter dev_block=%d. doing unmount\n", dev->num_waiting,
688 offline_or_rewind_dev(dev);
689 force_close_device(dev);
690 dev->dev_blocked = BST_UNMOUNTED_WAITING_FOR_SYSOP;
691 bnet_fsend(dir, _("3001 Device %s unmounted.\n"),
694 } else if (dev->dev_blocked == BST_DOING_ACQUIRE) {
695 bnet_fsend(dir, _("3902 Device %s is busy in acquire.\n"),
698 } else if (dev->dev_blocked == BST_WRITING_LABEL) {
699 bnet_fsend(dir, _("3903 Device %s is being labeled.\n"),
702 } else if (dev->is_busy()) {
703 send_dir_busy_message(dir, dev);
704 } else { /* device not being used */
705 Dmsg0(90, "Device not in use, unmounting\n");
706 /* On FreeBSD, I am having ASSERT() failures in block_device()
707 * and I can only imagine that the thread id that we are
708 * leaving in no_wait_id is being re-used. So here,
709 * we simply do it by hand. Gross, but a solution.
711 /* block_device(dev, BST_UNMOUNTED); replace with 2 lines below */
712 dev->dev_blocked = BST_UNMOUNTED;
714 offline_or_rewind_dev(dev);
715 force_close_device(dev);
716 bnet_fsend(dir, _("3002 Device %s unmounted.\n"),
721 bnet_fsend(dir, _("3999 Device \"%s\" not found\n"), devname.c_str());
724 /* NB dir->msg gets clobbered in bnet_fsend, so save command */
725 pm_strcpy(jcr->errmsg, dir->msg);
726 bnet_fsend(dir, _("3907 Error scanning unmount command: %s\n"), jcr->errmsg);
728 bnet_sig(dir, BNET_EOD);
733 * Release command from Director. This rewinds the device and if
734 * configured does a offline and ensures that Bacula will
735 * re-read the label of the tape before continuing. This gives
736 * the operator the chance to change the tape anytime before the
739 static bool release_cmd(JCR *jcr)
742 BSOCK *dir = jcr->dir_bsock;
746 if (sscanf(dir->msg, "release %127s drive=%d", devname.c_str(), &drive) == 2) {
747 dev = find_device(jcr, devname, drive);
749 P(dev->mutex); /* Use P to avoid indefinite block */
750 if (!dev->is_open()) {
751 Dmsg0(90, "Device already released\n");
752 bnet_fsend(dir, _("3911 Device %s already released.\n"),
755 } else if (dev->dev_blocked == BST_WAITING_FOR_SYSOP ||
756 dev->dev_blocked == BST_UNMOUNTED_WAITING_FOR_SYSOP) {
757 Dmsg2(90, "%d waiter dev_block=%d. doing unmount\n", dev->num_waiting,
759 bnet_fsend(dir, _("3912 Device %s waiting for mount.\n"),
762 } else if (dev->dev_blocked == BST_DOING_ACQUIRE) {
763 bnet_fsend(dir, _("3913 Device %s is busy in acquire.\n"),
766 } else if (dev->dev_blocked == BST_WRITING_LABEL) {
767 bnet_fsend(dir, _("3914 Device %s is being labeled.\n"),
770 } else if (dev->is_busy()) {
771 send_dir_busy_message(dir, dev);
772 } else { /* device not being used */
773 Dmsg0(90, "Device not in use, unmounting\n");
774 release_volume(jcr->dcr);
775 bnet_fsend(dir, _("3012 Device %s released.\n"),
780 bnet_fsend(dir, _("3999 Device \"%s\" not found\n"), devname.c_str());
783 /* NB dir->msg gets clobbered in bnet_fsend, so save command */
784 pm_strcpy(jcr->errmsg, dir->msg);
785 bnet_fsend(dir, _("3917 Error scanning release command: %s\n"), jcr->errmsg);
787 bnet_sig(dir, BNET_EOD);
794 * Autochanger command from Director
796 static bool autochanger_cmd(JCR *jcr)
799 BSOCK *dir = jcr->dir_bsock;
802 const char *cmd = NULL;
805 if (sscanf(dir->msg, "autochanger list %127s ", devname.c_str()) == 1) {
808 } else if (sscanf(dir->msg, "autochanger slots %127s ", devname.c_str()) == 1) {
813 dev = find_device(jcr, devname, -1);
816 P(dev->mutex); /* Use P to avoid indefinite block */
817 if (!dev->is_tape()) {
818 bnet_fsend(dir, _("3995 Device %s is not an autochanger.\n"),
820 /* Under certain "safe" conditions, we can steal the lock */
821 } else if (!dev->is_open() || dev->can_steal_lock()) {
822 autochanger_cmd(dcr, dir, cmd);
823 } else if (dev->is_busy() || dev->is_blocked()) {
824 send_dir_busy_message(dir, dev);
825 } else { /* device not being used */
826 autochanger_cmd(dcr, dir, cmd);
830 bnet_fsend(dir, _("3999 Device \"%s\" not found\n"), devname.c_str());
832 } else { /* error on scanf */
833 pm_strcpy(jcr->errmsg, dir->msg);
834 bnet_fsend(dir, _("3908 Error scanning autocharger list/slots command: %s\n"),
837 bnet_sig(dir, BNET_EOD);
842 * Read and return the Volume label
844 static bool readlabel_cmd(JCR *jcr)
847 BSOCK *dir = jcr->dir_bsock;
852 if (sscanf(dir->msg, "readlabel %127s Slot=%d drive=%d", devname.c_str(),
853 &Slot, &drive) == 3) {
854 dev = find_device(jcr, devname, drive);
856 P(dev->mutex); /* Use P to avoid indefinite block */
857 if (!dev->is_open()) {
858 read_volume_label(jcr, dev, Slot);
859 force_close_device(dev);
860 /* Under certain "safe" conditions, we can steal the lock */
861 } else if (dev->can_steal_lock()) {
862 read_volume_label(jcr, dev, Slot);
863 } else if (dev->is_busy() || dev->is_blocked()) {
864 send_dir_busy_message(dir, dev);
865 } else { /* device not being used */
866 read_volume_label(jcr, dev, Slot);
870 bnet_fsend(dir, _("3999 Device \"%s\" not found\n"), devname.c_str());
873 pm_strcpy(jcr->errmsg, dir->msg);
874 bnet_fsend(dir, _("3909 Error scanning readlabel command: %s\n"), jcr->errmsg);
876 bnet_sig(dir, BNET_EOD);
881 * Read the tape label
883 * Enter with the mutex set
885 static void read_volume_label(JCR *jcr, DEVICE *dev, int Slot)
887 BSOCK *dir = jcr->dir_bsock;
892 steal_device_lock(dev, &hold, BST_WRITING_LABEL);
894 if (!try_autoload_device(jcr, Slot, "")) {
895 goto bail_out; /* error */
898 dev->clear_labeled(); /* force read of label */
899 switch (read_dev_volume_label(dcr)) {
901 /* DO NOT add quotes around the Volume name. It is scanned in the DIR */
902 bnet_fsend(dir, _("3001 Volume=%s Slot=%d\n"), dev->VolHdr.VolumeName, Slot);
903 Dmsg1(100, "Volume: %s\n", dev->VolHdr.VolumeName);
906 bnet_fsend(dir, _("3902 Cannot mount Volume on Storage Device %s because:\n%s"),
907 dev->print_name(), jcr->errmsg);
912 give_back_device_lock(dev, &hold);
916 static bool try_autoload_device(JCR *jcr, int slot, const char *VolName)
919 BSOCK *dir = jcr->dir_bsock;
920 DEVICE *dev = dcr->dev;
922 bstrncpy(dcr->VolumeName, VolName, sizeof(dcr->VolumeName));
923 dcr->VolCatInfo.Slot = slot;
924 dcr->VolCatInfo.InChanger = slot > 0;
925 if (autoload_device(dcr, 0, dir) < 0) { /* autoload if possible */
929 /* Ensure that the device is open -- autoload_device() closes it */
930 if (dev->open(dcr, OPEN_READ_WRITE) < 0) {
931 bnet_fsend(dir, _("3910 Unable to open device %s: ERR=%s\n"),
932 dev->print_name(), dev->strerror());
938 static void send_dir_busy_message(BSOCK *dir, DEVICE *dev)
940 if (dev->can_read()) {
941 bnet_fsend(dir, _("3911 Device %s is busy reading.\n"),
944 bnet_fsend(dir, _("3912 Device %s is busy with %d writer(s).\n"),
945 dev->print_name(), dev->num_writers);