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 struct s_last_job last_job;
47 extern bool init_done;
49 /* Static variables */
50 static char derrmsg[] = "3900 Invalid command\n";
51 static char OKsetdebug[] = "3000 OK setdebug=%d\n";
52 static char illegal_cmd[] = "3997 Illegal command for a Director with Monitor directive enabled\n";
54 /* Imported functions */
55 extern void terminate_child();
56 extern bool job_cmd(JCR *jcr);
57 extern bool use_cmd(JCR *jcr);
58 extern bool run_cmd(JCR *jcr);
59 extern bool status_cmd(JCR *sjcr);
60 extern bool qstatus_cmd(JCR *jcr);
61 //extern bool query_cmd(JCR *jcr);
63 /* Forward referenced functions */
64 static bool label_cmd(JCR *jcr);
65 static bool relabel_cmd(JCR *jcr);
66 static bool readlabel_cmd(JCR *jcr);
67 static bool release_cmd(JCR *jcr);
68 static bool setdebug_cmd(JCR *jcr);
69 static bool cancel_cmd(JCR *cjcr);
70 static bool mount_cmd(JCR *jcr);
71 static bool unmount_cmd(JCR *jcr);
72 static bool bootstrap_cmd(JCR *jcr);
73 static bool changer_cmd(JCR *sjcr);
74 static bool do_label(JCR *jcr, int relabel);
75 static DCR *find_device(JCR *jcr, POOL_MEM &dev_name, int drive);
76 static void read_volume_label(JCR *jcr, DEVICE *dev, int Slot);
77 static void label_volume_if_ok(DCR *dcr, char *oldname,
78 char *newname, char *poolname,
79 int Slot, int relabel);
80 static bool try_autoload_device(JCR *jcr, int slot, const char *VolName);
81 static void send_dir_busy_message(BSOCK *dir, DEVICE *dev);
85 bool (*func)(JCR *jcr);
86 int monitoraccess; /* specify if monitors have access to this function */
90 * The following are the recognized commands from the Director.
92 static struct s_cmds cmds[] = {
93 {"JobId=", job_cmd, 0}, /* start Job */
94 {"autochanger", changer_cmd, 0},
95 {"bootstrap", bootstrap_cmd, 0},
96 {"cancel", cancel_cmd, 0},
97 {"label", label_cmd, 0}, /* label a tape */
98 {"mount", mount_cmd, 0},
99 {"readlabel", readlabel_cmd, 0},
100 {"release", release_cmd, 0},
101 {"relabel", relabel_cmd, 0}, /* relabel a tape */
102 {"setdebug=", setdebug_cmd, 0}, /* set debug level */
103 {"status", status_cmd, 1},
104 {".status", qstatus_cmd, 1},
105 {"unmount", unmount_cmd, 0},
106 {"use storage=", use_cmd, 0},
108 // {"query", query_cmd, 0},
109 {NULL, NULL} /* list terminator */
114 * Connection request. We accept connections either from the
115 * Director or a Client (File daemon).
117 * Note, we are running as a seperate thread of the Storage daemon.
118 * and it is because a Director has made a connection with
119 * us on the "Message" channel.
121 * Basic tasks done here:
122 * - Create a JCR record
123 * - If it was from the FD, call handle_filed_connection()
124 * - Authenticate the Director
125 * - We wait for a command
126 * - We execute the command
127 * - We continue or exit depending on the return status
129 void *handle_connection_request(void *arg)
131 BSOCK *bs = (BSOCK *)arg;
136 char name[MAX_NAME_LENGTH];
138 if (bnet_recv(bs) <= 0) {
139 Emsg0(M_ERROR, 0, _("Connection request failed.\n"));
145 * Do a sanity check on the message received
147 if (bs->msglen < 25 || bs->msglen > (int)sizeof(name)-25) {
148 Emsg1(M_ERROR, 0, _("Invalid connection. Len=%d\n"), bs->msglen);
153 * See if this is a File daemon connection. If so
156 Dmsg1(110, "Conn: %s", bs->msg);
157 if (sscanf(bs->msg, "Hello Start Job %127s", name) == 1) {
158 handle_filed_connection(bs, name);
163 * This is a connection from the Director, so setup a JCR
165 Dmsg0(110, "Start Dir Job\n");
166 jcr = new_jcr(sizeof(JCR), stored_free_jcr); /* create Job Control Record */
167 jcr->dir_bsock = bs; /* save Director bsock */
168 jcr->dir_bsock->jcr = jcr;
169 jcr->dcrs = New(alist(10, not_owned_by_alist));
170 /* Initialize FD start condition variable */
171 int errstat = pthread_cond_init(&jcr->job_start_wait, NULL);
173 Jmsg1(jcr, M_FATAL, 0, _("Unable to init job cond variable: ERR=%s\n"), strerror(errstat));
177 Dmsg0(1000, "stored in start_job\n");
180 * Authenticate the Director
182 if (!authenticate_director(jcr)) {
183 Jmsg(jcr, M_FATAL, 0, _("Unable to authenticate Director\n"));
186 Dmsg0(90, "Message channel init completed.\n");
188 for (quit=false; !quit;) {
190 if ((bnet_stat = bnet_recv(bs)) <= 0) {
191 break; /* connection terminated */
193 Dmsg1(199, "<dird: %s\n", bs->msg);
194 /* Ensure that device initialization is complete */
199 for (i=0; cmds[i].cmd; i++) {
200 if (strncmp(cmds[i].cmd, bs->msg, strlen(cmds[i].cmd)) == 0) {
201 if ((!cmds[i].monitoraccess) && (jcr->director->monitor)) {
202 Dmsg1(100, "Command %s illegal.\n", cmds[i].cmd);
203 bnet_fsend(bs, illegal_cmd);
204 bnet_sig(bs, BNET_EOD);
207 Dmsg1(200, "Do command: %s\n", cmds[i].cmd);
208 if (!cmds[i].func(jcr)) { /* do command */
209 quit = true; /* error, get out */
210 Dmsg1(190, "Command %s reqeusts quit\n", cmds[i].cmd);
212 found = true; /* indicate command found */
216 if (!found) { /* command not found */
217 bnet_fsend(bs, derrmsg);
222 generate_daemon_event(jcr, "JobEnd");
223 dequeue_messages(jcr); /* send any queued messages */
224 bnet_sig(bs, BNET_TERMINATE);
230 * Set debug level as requested by the Director
233 static bool setdebug_cmd(JCR *jcr)
235 BSOCK *dir = jcr->dir_bsock;
236 int level, trace_flag;
238 Dmsg1(10, "setdebug_cmd: %s", dir->msg);
239 if (sscanf(dir->msg, "setdebug=%d trace=%d", &level, &trace_flag) != 2 || level < 0) {
240 bnet_fsend(dir, _("3991 Bad setdebug command: %s\n"), dir->msg);
244 set_trace(trace_flag);
245 return bnet_fsend(dir, OKsetdebug, level);
252 static bool cancel_cmd(JCR *cjcr)
254 BSOCK *dir = cjcr->dir_bsock;
256 char Job[MAX_NAME_LENGTH];
259 if (sscanf(dir->msg, "cancel Job=%127s", Job) == 1) {
260 if (!(jcr=get_jcr_by_full_name(Job))) {
261 bnet_fsend(dir, _("3904 Job %s not found.\n"), Job);
264 oldStatus = jcr->JobStatus;
265 set_jcr_job_status(jcr, JS_Canceled);
266 if (!jcr->authenticated && oldStatus == JS_WaitFD) {
267 pthread_cond_signal(&jcr->job_start_wait); /* wake waiting thread */
270 if (jcr->file_bsock) {
271 bnet_sig(jcr->file_bsock, BNET_TERMINATE);
273 /* If thread waiting on mount, wake him */
274 if (jcr->dcr && jcr->dcr->dev && jcr->dcr->dev->waiting_for_mount()) {
275 pthread_cond_broadcast(&jcr->dcr->dev->wait_next_vol);
276 pthread_cond_broadcast(&wait_device_release);
278 if (jcr->read_dcr && jcr->read_dcr->dev && jcr->read_dcr->dev->waiting_for_mount()) {
279 pthread_cond_broadcast(&jcr->read_dcr->dev->wait_next_vol);
280 pthread_cond_broadcast(&wait_device_release);
282 bnet_fsend(dir, _("3000 Job %s marked to be canceled.\n"), jcr->Job);
286 bnet_fsend(dir, _("3903 Error scanning cancel command.\n"));
288 bnet_sig(dir, BNET_EOD);
296 static bool label_cmd(JCR *jcr)
298 return do_label(jcr, 0);
301 static bool relabel_cmd(JCR *jcr)
303 return do_label(jcr, 1);
306 static bool do_label(JCR *jcr, int relabel)
308 POOLMEM *newname, *oldname, *poolname, *mtype;
310 BSOCK *dir = jcr->dir_bsock;
317 newname = get_memory(dir->msglen+1);
318 oldname = get_memory(dir->msglen+1);
319 poolname = get_memory(dir->msglen+1);
320 mtype = get_memory(dir->msglen+1);
322 if (sscanf(dir->msg, "relabel %127s OldName=%127s NewName=%127s PoolName=%127s "
323 "MediaType=%127s Slot=%d drive=%d",
324 dev_name.c_str(), oldname, newname, poolname, mtype,
325 &slot, &drive) == 7) {
330 if (sscanf(dir->msg, "label %127s VolumeName=%127s PoolName=%127s "
331 "MediaType=%127s Slot=%d drive=%d",
332 dev_name.c_str(), newname, poolname, mtype, &slot, &drive) == 6) {
337 unbash_spaces(newname);
338 unbash_spaces(oldname);
339 unbash_spaces(poolname);
340 unbash_spaces(mtype);
341 dcr = find_device(jcr, dev_name, drive);
344 P(dev->mutex); /* Use P to avoid indefinite block */
345 if (!dev->is_open()) {
346 Dmsg1(400, "Can %slabel. Device is not open\n", relabel?"re":"");
347 label_volume_if_ok(dcr, oldname, newname, poolname, slot, relabel);
349 /* Under certain "safe" conditions, we can steal the lock */
350 } else if (dev->can_steal_lock()) {
351 Dmsg0(400, "Can relabel. can_steal_lock\n");
352 label_volume_if_ok(dcr, oldname, newname, poolname, slot, relabel);
353 } else if (dev->is_busy() || dev->is_blocked()) {
354 send_dir_busy_message(dir, dev);
355 } else { /* device not being used */
356 Dmsg0(400, "Can relabel. device not used\n");
357 label_volume_if_ok(dcr, oldname, newname, poolname, slot, relabel);
363 bnet_fsend(dir, _("3999 Device \"%s\" not found or could not be opened.\n"), dev_name.c_str());
366 /* NB dir->msg gets clobbered in bnet_fsend, so save command */
367 pm_strcpy(jcr->errmsg, dir->msg);
368 bnet_fsend(dir, _("3903 Error scanning label command: %s\n"), jcr->errmsg);
370 free_memory(oldname);
371 free_memory(newname);
372 free_memory(poolname);
374 bnet_sig(dir, BNET_EOD);
379 * Read the tape label and determine if we can safely
380 * label the tape (not a Bacula volume), then label it.
382 * Enter with the mutex set
384 static void label_volume_if_ok(DCR *dcr, char *oldname,
385 char *newname, char *poolname,
386 int slot, int relabel)
388 BSOCK *dir = dcr->jcr->dir_bsock;
390 DEVICE *dev = dcr->dev;
393 const char *volname = (relabel == 0) ? newname : oldname;
396 steal_device_lock(dev, &hold, BST_WRITING_LABEL);
397 Dmsg1(100, "Stole device %s lock, writing label.\n", dev->print_name());
399 Dmsg0(90, "try_autoload_device - looking for volume_info\n");
400 if (relabel && dev->is_dvd()) {
401 dcr->VolCatInfo.VolCatParts=0;
404 if (!try_autoload_device(dcr->jcr, slot, volname)) {
405 goto bail_out; /* error */
408 /* Ensure that the device is open -- autoload_device() closes it */
409 if (dev->is_tape()) {
410 mode = OPEN_READ_WRITE;
412 mode = CREATE_READ_WRITE;
415 bstrncpy(dcr->VolCatInfo.VolCatName, volname, sizeof(dcr->VolCatInfo.VolCatName));
418 if (dev->open(dcr, mode) < 0) {
419 bnet_fsend(dir, _("3910 Unable to open device %s: ERR=%s\n"),
420 dev->print_name(), dev->strerror());
424 /* See what we have for a Volume */
425 label_status = read_dev_volume_label(dcr);
427 switch(label_status) {
429 case VOL_VERSION_ERROR:
430 case VOL_LABEL_ERROR:
434 "3920 Cannot label Volume because it is already labeled: \"%s\"\n"),
435 dev->VolHdr.VolumeName);
439 /* Relabel request. If oldname matches, continue */
440 if (strcmp(oldname, dev->VolHdr.VolumeName) != 0) {
441 bnet_fsend(dir, _("3921 Wrong volume mounted.\n"));
444 if (dev->label_type != B_BACULA_LABEL) {
445 bnet_fsend(dir, _("3922 Cannot relabel an ANSI/IBM labeled Volume.\n"));
448 if (relabel && dev->is_dvd()) {
449 /* Change the partition file name */
450 bstrncpy(dcr->VolumeName, newname, sizeof(dcr->VolumeName));
451 if (!dev->truncate(dcr)) {
452 bnet_fsend(dir, _("3912 Failed to truncate previous DVD volume.\n"));
456 free_volume(dev); /* release old volume name */
457 /* Fall through wanted! */
460 if (!write_new_volume_label_to_dev(dcr, newname, poolname, true /* write dvd now */)) {
461 bnet_fsend(dir, _("3912 Failed to label Volume: ERR=%s\n"), dev->bstrerror());
464 bstrncpy(dcr->VolumeName, newname, sizeof(dcr->VolumeName));
465 /* The following 3000 OK label. string is scanned in ua_label.c */
466 bnet_fsend(dir, "3000 OK label. VolBytes=%s DVD=%d Volume=\"%s\" Device=%s\n",
467 edit_uint64(dev->VolCatInfo.VolCatBytes, ed1),
468 dev->is_dvd()?1:0, newname, dev->print_name());
471 bnet_fsend(dir, _("3912 Failed to label Volume: ERR=%s\n"), dev->bstrerror());
474 bnet_fsend(dir, _("3913 Cannot label Volume. "
475 "Unknown status %d from read_volume_label()\n"), label_status);
480 if (!dev->is_open()) {
483 give_back_device_lock(dev, &hold);
489 * Read the tape label
491 * Enter with the mutex set
493 static bool read_label(DCR *dcr)
497 BSOCK *dir = jcr->dir_bsock;
499 DEVICE *dev = dcr->dev;
501 steal_device_lock(dev, &hold, BST_DOING_ACQUIRE);
503 dcr->VolumeName[0] = 0;
504 dev->clear_labeled(); /* force read of label */
505 switch (read_dev_volume_label(dcr)) {
507 bnet_fsend(dir, _("3001 Mounted Volume: %s\n"), dev->VolHdr.VolumeName);
511 bnet_fsend(dir, _("3902 Cannot mount Volume on Storage Device %s because:\n%s"),
512 dev->print_name(), jcr->errmsg);
516 give_back_device_lock(dev, &hold);
521 * Searches for device by name, and if found, creates a dcr and
524 static DCR *find_device(JCR *jcr, POOL_MEM &devname, int drive)
527 AUTOCHANGER *changer;
531 unbash_spaces(devname);
532 foreach_res(device, R_DEVICE) {
533 /* Find resource, and make sure we were able to open it */
534 if (fnmatch(device->hdr.name, devname.c_str(), 0) == 0) {
536 device->dev = init_dev(jcr, device);
539 Jmsg(jcr, M_WARNING, 0, _("\n"
540 " Device \"%s\" requested by DIR could not be opened or does not exist.\n"),
544 Dmsg1(20, "Found device %s\n", device->hdr.name);
550 foreach_res(changer, R_AUTOCHANGER) {
551 /* Find resource, and make sure we were able to open it */
552 if (fnmatch(devname.c_str(), changer->hdr.name, 0) == 0) {
553 /* Try each device in this AutoChanger */
554 foreach_alist(device, changer->device) {
555 Dmsg1(100, "Try changer device %s\n", device->hdr.name);
557 device->dev = init_dev(jcr, device);
560 Dmsg1(100, "Device %s could not be opened. Skipped\n", devname.c_str());
561 Jmsg(jcr, M_WARNING, 0, _("\n"
562 " Device \"%s\" in changer \"%s\" requested by DIR could not be opened or does not exist.\n"),
563 device->hdr.name, devname.c_str());
566 if (!device->dev->autoselect) {
567 Dmsg1(100, "Device %s not autoselect skipped.\n", devname.c_str());
568 continue; /* device is not available */
570 if (drive < 0 || drive == (int)device->dev->drive_index) {
571 Dmsg1(20, "Found changer device %s\n", device->hdr.name);
575 Dmsg3(100, "Device %s drive wrong: want=%d got=%d skipping\n",
576 devname.c_str(), drive, (int)device->dev->drive_index);
578 break; /* we found it but could not open a device */
584 Dmsg1(100, "Found device %s\n", device->hdr.name);
585 dcr = new_dcr(jcr, device->dev);
586 dcr->device = device;
594 * Mount command from Director
596 static bool mount_cmd(JCR *jcr)
599 BSOCK *dir = jcr->dir_bsock;
606 ok = sscanf(dir->msg, "mount %127s drive=%d slot=%d", devname.c_str(),
609 ok = sscanf(dir->msg, "mount %127s drive=%d", devname.c_str(), &drive) == 2;
612 dcr = find_device(jcr, devname, drive);
615 P(dev->mutex); /* Use P to avoid indefinite block */
616 Dmsg1(100, "mount cmd blocked=%d\n", dev->dev_blocked);
617 switch (dev->dev_blocked) { /* device blocked? */
618 case BST_WAITING_FOR_SYSOP:
619 /* Someone is waiting, wake him */
620 Dmsg0(100, "Waiting for mount. Attempting to wake thread\n");
621 dev->dev_blocked = BST_MOUNT;
622 bnet_fsend(dir, "3001 OK mount. Device=%s\n",
624 pthread_cond_broadcast(&dev->wait_next_vol);
625 pthread_cond_broadcast(&wait_device_release);
628 /* In both of these two cases, we (the user) unmounted the Volume */
629 case BST_UNMOUNTED_WAITING_FOR_SYSOP:
631 if (dev->is_autochanger() && slot > 0) {
632 try_autoload_device(jcr, slot, "");
634 /* We freed the device, so reopen it and wake any waiting threads */
635 if (dev->open(dcr, OPEN_READ_ONLY) < 0) {
636 bnet_fsend(dir, _("3901 open device failed: ERR=%s\n"),
638 if (dev->dev_blocked == BST_UNMOUNTED) {
639 /* We blocked the device, so unblock it */
640 Dmsg0(100, "Unmounted. Unblocking device\n");
645 read_dev_volume_label(dcr);
646 if (dev->dev_blocked == BST_UNMOUNTED) {
647 /* We blocked the device, so unblock it */
648 Dmsg0(100, "Unmounted. Unblocking device\n");
649 read_label(dcr); /* this should not be necessary */
652 Dmsg0(100, "Unmounted waiting for mount. Attempting to wake thread\n");
653 dev->dev_blocked = BST_MOUNT;
655 if (dev->is_labeled()) {
656 bnet_fsend(dir, _("3001 Device %s is mounted with Volume \"%s\"\n"),
657 dev->print_name(), dev->VolHdr.VolumeName);
659 bnet_fsend(dir, _("3905 Device %s open but no Bacula volume is mounted.\n"
660 "If this is not a blank tape, try unmounting and remounting the Volume.\n"),
663 pthread_cond_broadcast(&dev->wait_next_vol);
664 pthread_cond_broadcast(&wait_device_release);
667 case BST_DOING_ACQUIRE:
668 bnet_fsend(dir, _("3001 Device %s is doing acquire.\n"),
672 case BST_WRITING_LABEL:
673 bnet_fsend(dir, _("3903 Device %s is being labeled.\n"),
677 case BST_NOT_BLOCKED:
678 if (dev->is_autochanger() && slot > 0) {
679 try_autoload_device(jcr, slot, "");
681 if (dev->is_open()) {
682 if (dev->is_labeled()) {
683 bnet_fsend(dir, _("3001 Device %s is mounted with Volume \"%s\"\n"),
684 dev->print_name(), dev->VolHdr.VolumeName);
686 bnet_fsend(dir, _("3905 Device %s open but no Bacula volume is mounted.\n"
687 "If this is not a blank tape, try unmounting and remounting the Volume.\n"),
690 } else if (dev->is_tape()) {
691 if (dev->open(dcr, OPEN_READ_ONLY) < 0) {
692 bnet_fsend(dir, _("3901 open device failed: ERR=%s\n"),
697 if (dev->is_labeled()) {
698 bnet_fsend(dir, _("3001 Device %s is already mounted with Volume \"%s\"\n"),
699 dev->print_name(), dev->VolHdr.VolumeName);
701 bnet_fsend(dir, _("3905 Device %s open but no Bacula volume is mounted.\n"
702 "If this is not a blank tape, try unmounting and remounting the Volume.\n"),
705 } else if (dev->is_dvd()) {
706 if (mount_dvd(dev, 1)) {
707 bnet_fsend(dir, _("3002 Device %s is mounted.\n"),
710 bnet_fsend(dir, _("3907 %s"), dev->bstrerror());
712 } else { /* must be file */
713 bnet_fsend(dir, _("3906 File device %s is always mounted.\n"),
719 bnet_fsend(dir, _("3905 Bizarre wait state %d\n"), dev->dev_blocked);
726 bnet_fsend(dir, _("3999 Device \"%s\" not found or could not be opened.\n"), devname.c_str());
729 pm_strcpy(jcr->errmsg, dir->msg);
730 bnet_fsend(dir, _("3909 Error scanning mount command: %s\n"), jcr->errmsg);
732 bnet_sig(dir, BNET_EOD);
737 * unmount command from Director
739 static bool unmount_cmd(JCR *jcr)
742 BSOCK *dir = jcr->dir_bsock;
747 if (sscanf(dir->msg, "unmount %127s drive=%d", devname.c_str(), &drive) == 2) {
748 dcr = find_device(jcr, devname, drive);
751 P(dev->mutex); /* Use P to avoid indefinite block */
752 if (!dev->is_open()) {
753 if (!dev->is_busy()) {
754 unload_autochanger(jcr->dcr, -1);
757 if (unmount_dvd(dev, 0)) {
758 bnet_fsend(dir, _("3002 Device %s unmounted.\n"),
761 bnet_fsend(dir, _("3907 %s"), dev->bstrerror());
764 Dmsg0(90, "Device already unmounted\n");
765 bnet_fsend(dir, _("3901 Device %s is already unmounted.\n"),
768 } else if (dev->dev_blocked == BST_WAITING_FOR_SYSOP) {
769 Dmsg2(90, "%d waiter dev_block=%d. doing unmount\n", dev->num_waiting,
771 if (!unload_autochanger(jcr->dcr, -1)) {
774 if (dev->is_dvd() && !unmount_dvd(dev, 0)) {
775 bnet_fsend(dir, _("3907 %s"), dev->bstrerror());
777 dev->dev_blocked = BST_UNMOUNTED_WAITING_FOR_SYSOP;
778 bnet_fsend(dir, _("3001 Device %s unmounted.\n"),
782 } else if (dev->dev_blocked == BST_DOING_ACQUIRE) {
783 bnet_fsend(dir, _("3902 Device %s is busy in acquire.\n"),
786 } else if (dev->dev_blocked == BST_WRITING_LABEL) {
787 bnet_fsend(dir, _("3903 Device %s is being labeled.\n"),
790 } else if (dev->is_busy()) {
791 send_dir_busy_message(dir, dev);
792 } else { /* device not being used */
793 Dmsg0(90, "Device not in use, unmounting\n");
794 /* On FreeBSD, I am having ASSERT() failures in block_device()
795 * and I can only imagine that the thread id that we are
796 * leaving in no_wait_id is being re-used. So here,
797 * we simply do it by hand. Gross, but a solution.
799 /* block_device(dev, BST_UNMOUNTED); replace with 2 lines below */
800 dev->dev_blocked = BST_UNMOUNTED;
802 if (!unload_autochanger(jcr->dcr, -1)) {
805 if (dev->is_dvd() && !unmount_dvd(dev, 0)) {
806 bnet_fsend(dir, _("3907 %s"), dev->bstrerror());
808 bnet_fsend(dir, _("3002 Device %s unmounted.\n"),
816 bnet_fsend(dir, _("3999 Device \"%s\" not found or could not be opened.\n"), devname.c_str());
819 /* NB dir->msg gets clobbered in bnet_fsend, so save command */
820 pm_strcpy(jcr->errmsg, dir->msg);
821 bnet_fsend(dir, _("3907 Error scanning unmount command: %s\n"), jcr->errmsg);
823 bnet_sig(dir, BNET_EOD);
828 * Release command from Director. This rewinds the device and if
829 * configured does a offline and ensures that Bacula will
830 * re-read the label of the tape before continuing. This gives
831 * the operator the chance to change the tape anytime before the
834 static bool release_cmd(JCR *jcr)
837 BSOCK *dir = jcr->dir_bsock;
842 if (sscanf(dir->msg, "release %127s drive=%d", devname.c_str(), &drive) == 2) {
843 dcr = find_device(jcr, devname, drive);
846 P(dev->mutex); /* Use P to avoid indefinite block */
847 if (!dev->is_open()) {
848 Dmsg0(90, "Device already released\n");
849 bnet_fsend(dir, _("3921 Device %s already released.\n"),
852 } else if (dev->dev_blocked == BST_WAITING_FOR_SYSOP ||
853 dev->dev_blocked == BST_UNMOUNTED_WAITING_FOR_SYSOP) {
854 Dmsg2(90, "%d waiter dev_block=%d. doing unmount\n", dev->num_waiting,
856 bnet_fsend(dir, _("3922 Device %s waiting for mount.\n"),
859 } else if (dev->dev_blocked == BST_DOING_ACQUIRE) {
860 bnet_fsend(dir, _("3923 Device %s is busy in acquire.\n"),
863 } else if (dev->dev_blocked == BST_WRITING_LABEL) {
864 bnet_fsend(dir, _("3914 Device %s is being labeled.\n"),
867 } else if (dev->is_busy()) {
868 send_dir_busy_message(dir, dev);
869 } else { /* device not being used */
870 Dmsg0(90, "Device not in use, unmounting\n");
871 release_volume(jcr->dcr);
872 bnet_fsend(dir, _("3022 Device %s released.\n"),
879 bnet_fsend(dir, _("3999 Device \"%s\" not found or could not be opened.\n"), devname.c_str());
882 /* NB dir->msg gets clobbered in bnet_fsend, so save command */
883 pm_strcpy(jcr->errmsg, dir->msg);
884 bnet_fsend(dir, _("3927 Error scanning release command: %s\n"), jcr->errmsg);
886 bnet_sig(dir, BNET_EOD);
891 static bool bootstrap_cmd(JCR *jcr)
893 return get_bootstrap_file(jcr, jcr->dir_bsock);
897 * Autochanger command from Director
899 static bool changer_cmd(JCR *jcr)
902 BSOCK *dir = jcr->dir_bsock;
905 const char *cmd = NULL;
909 if (sscanf(dir->msg, "autochanger list %127s", devname.c_str()) == 1) {
912 } else if (sscanf(dir->msg, "autochanger slots %127s", devname.c_str()) == 1) {
915 } else if (sscanf(dir->msg, "autochanger drives %127s", devname.c_str()) == 1) {
920 dcr = find_device(jcr, devname, -1);
923 P(dev->mutex); /* Use P to avoid indefinite block */
924 if (!dev->device->changer_res) {
925 bnet_fsend(dir, _("3995 Device %s is not an autochanger.\n"),
927 /* Under certain "safe" conditions, we can steal the lock */
928 } else if (dolist || !dev->is_open() || dev->can_steal_lock()) {
929 autochanger_cmd(dcr, dir, cmd);
930 } else if (dev->is_busy() || dev->is_blocked()) {
931 send_dir_busy_message(dir, dev);
932 } else { /* device not being used */
933 autochanger_cmd(dcr, dir, cmd);
939 bnet_fsend(dir, _("3999 Device \"%s\" not found or could not be opened.\n"), devname.c_str());
941 } else { /* error on scanf */
942 pm_strcpy(jcr->errmsg, dir->msg);
943 bnet_fsend(dir, _("3908 Error scanning autocharger drives/list/slots command: %s\n"),
946 bnet_sig(dir, BNET_EOD);
951 * Read and return the Volume label
953 static bool readlabel_cmd(JCR *jcr)
956 BSOCK *dir = jcr->dir_bsock;
962 if (sscanf(dir->msg, "readlabel %127s Slot=%d drive=%d", devname.c_str(),
963 &Slot, &drive) == 3) {
964 dcr = find_device(jcr, devname, drive);
967 P(dev->mutex); /* Use P to avoid indefinite block */
968 if (!dev->is_open()) {
969 read_volume_label(jcr, dev, Slot);
971 /* Under certain "safe" conditions, we can steal the lock */
972 } else if (dev->can_steal_lock()) {
973 read_volume_label(jcr, dev, Slot);
974 } else if (dev->is_busy() || dev->is_blocked()) {
975 send_dir_busy_message(dir, dev);
976 } else { /* device not being used */
977 read_volume_label(jcr, dev, Slot);
983 bnet_fsend(dir, _("3999 Device \"%s\" not found or could not be opened.\n"), devname.c_str());
986 pm_strcpy(jcr->errmsg, dir->msg);
987 bnet_fsend(dir, _("3909 Error scanning readlabel command: %s\n"), jcr->errmsg);
989 bnet_sig(dir, BNET_EOD);
995 * Read the tape label
997 * Enter with the mutex set
999 static void read_volume_label(JCR *jcr, DEVICE *dev, int Slot)
1001 BSOCK *dir = jcr->dir_bsock;
1003 DCR *dcr = jcr->dcr;
1006 steal_device_lock(dev, &hold, BST_WRITING_LABEL);
1008 if (!try_autoload_device(jcr, Slot, "")) {
1009 goto bail_out; /* error */
1012 dev->clear_labeled(); /* force read of label */
1013 switch (read_dev_volume_label(dcr)) {
1015 /* DO NOT add quotes around the Volume name. It is scanned in the DIR */
1016 bnet_fsend(dir, _("3001 Volume=%s Slot=%d\n"), dev->VolHdr.VolumeName, Slot);
1017 Dmsg1(100, "Volume: %s\n", dev->VolHdr.VolumeName);
1020 bnet_fsend(dir, _("3902 Cannot mount Volume on Storage Device %s because:\n%s"),
1021 dev->print_name(), jcr->errmsg);
1026 give_back_device_lock(dev, &hold);
1030 static bool try_autoload_device(JCR *jcr, int slot, const char *VolName)
1032 DCR *dcr = jcr->dcr;
1033 BSOCK *dir = jcr->dir_bsock;
1035 bstrncpy(dcr->VolumeName, VolName, sizeof(dcr->VolumeName));
1036 dcr->VolCatInfo.Slot = slot;
1037 dcr->VolCatInfo.InChanger = slot > 0;
1038 if (autoload_device(dcr, 0, dir) < 0) { /* autoload if possible */
1044 static void send_dir_busy_message(BSOCK *dir, DEVICE *dev)
1046 if (dev->is_blocked()) {
1047 switch (dev->dev_blocked) {
1049 bnet_fsend(dir, _("3931 Device %s is BLOCKED. user unmounted.\n"),
1052 case BST_UNMOUNTED_WAITING_FOR_SYSOP:
1053 bnet_fsend(dir, _("3932 Device %s is BLOCKED. user unmounted during wait for media/mount.\n"),
1056 case BST_WAITING_FOR_SYSOP:
1057 bnet_fsend(dir, _("3933 Device %s is BLOCKED waiting for media.\n"),
1060 case BST_DOING_ACQUIRE:
1061 bnet_fsend(dir, _("3934 Device %s is being initialized.\n"),
1064 case BST_WRITING_LABEL:
1065 bnet_fsend(dir, _("3935 Device %s is blocked labeling a Volume.\n"),
1069 bnet_fsend(dir, _("3935 Device %s is blocked for unknown reason.\n"),
1073 } else if (dev->can_read()) {
1074 bnet_fsend(dir, _("3936 Device %s is busy reading.\n"),
1075 dev->print_name());;
1077 bnet_fsend(dir, _("3937 Device %s is busy with %d writer(s).\n"),
1078 dev->print_name(), dev->num_writers);