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. In some "safe" cases,
12 * we can do things to a blocked device. CAREFUL!!!!
14 * File daemon commands are handled in fdcmd.c
16 * Kern Sibbald, May MMI
22 Copyright (C) 2000-2004 Kern Sibbald and John Walker
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.
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.
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,
44 /* Exported variables */
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;
54 /* Static variables */
55 static char derrmsg[] = "3900 Invalid command\n";
56 static char OKsetdebug[] = "3000 OK setdebug=%d\n";
59 /* Imported functions */
60 extern void terminate_child();
61 extern int job_cmd(JCR *jcr);
62 extern int status_cmd(JCR *sjcr);
64 /* Forward referenced functions */
65 static int label_cmd(JCR *jcr);
66 static int relabel_cmd(JCR *jcr);
67 static int readlabel_cmd(JCR *jcr);
68 static int release_cmd(JCR *jcr);
69 static int setdebug_cmd(JCR *jcr);
70 static int cancel_cmd(JCR *cjcr);
71 static int mount_cmd(JCR *jcr);
72 static int unmount_cmd(JCR *jcr);
73 static int autochanger_cmd(JCR *sjcr);
74 static int do_label(JCR *jcr, int relabel);
75 static bool find_device(JCR *jcr, char *dname);
76 static void read_volume_label(JCR *jcr, DEVICE *dev, int Slot);
77 static void label_volume_if_ok(JCR *jcr, DEVICE *dev, char *oldname,
78 char *newname, char *poolname,
79 int Slot, int relabel);
83 int (*func)(JCR *jcr);
87 * The following are the recognized commands from the Director.
89 static struct s_cmds cmds[] = {
90 {"JobId=", job_cmd}, /* start Job */
91 {"setdebug=", setdebug_cmd}, /* set debug level */
92 {"cancel", cancel_cmd},
93 {"label", label_cmd}, /* label a tape */
94 {"relabel", relabel_cmd}, /* relabel a tape */
96 {"unmount", unmount_cmd},
97 {"status", status_cmd},
98 {"autochanger", autochanger_cmd},
99 {"release", release_cmd},
100 {"readlabel", readlabel_cmd},
101 {NULL, NULL} /* list terminator */
106 * Connection request. We accept connections either from the
107 * Director or a Client (File daemon).
109 * Note, we are running as a seperate thread of the Storage daemon.
110 * and it is because a Director has made a connection with
111 * us on the "Message" channel.
113 * Basic tasks done here:
114 * - Create a JCR record
115 * - If it was from the FD, call handle_filed_connection()
116 * - Authenticate the Director
117 * - We wait for a command
118 * - We execute the command
119 * - We continue or exit depending on the return status
121 void *connection_request(void *arg)
123 BSOCK *bs = (BSOCK *)arg;
127 char name[MAX_NAME_LENGTH];
129 if (bnet_recv(bs) <= 0) {
130 Emsg0(M_ERROR, 0, _("Connection request failed.\n"));
135 * Do a sanity check on the message received
137 if (bs->msglen < 25 || bs->msglen > (int)sizeof(name)-25) {
138 Emsg1(M_ERROR, 0, _("Invalid Dir connection. Len=%d\n"), bs->msglen);
141 * See if this is a File daemon connection. If so
144 if (sscanf(bs->msg, "Hello Start Job %127s calling\n", name) == 1) {
145 handle_filed_connection(bs, name);
149 jcr = new_jcr(sizeof(JCR), stored_free_jcr); /* create Job Control Record */
150 jcr->dir_bsock = bs; /* save Director bsock */
151 jcr->dir_bsock->jcr = jcr;
153 Dmsg0(1000, "stored in start_job\n");
156 * Authenticate the Director
158 if (!authenticate_director(jcr)) {
159 Jmsg(jcr, M_FATAL, 0, _("Unable to authenticate Director\n"));
163 Dmsg0(90, "Message channel init completed.\n");
165 for (quit=0; !quit;) {
167 if ((bnet_stat = bnet_recv(bs)) <= 0) {
168 break; /* connection terminated */
170 Dmsg1(9, "<dird: %s\n", bs->msg);
172 for (i=0; cmds[i].cmd; i++) {
173 if (strncmp(cmds[i].cmd, bs->msg, strlen(cmds[i].cmd)) == 0) {
174 if (!cmds[i].func(jcr)) { /* do command */
175 quit = TRUE; /* error, get out */
176 Dmsg1(90, "Command %s requsts quit\n", cmds[i].cmd);
178 found = TRUE; /* indicate command found */
182 if (!found) { /* command not found */
183 bnet_fsend(bs, derrmsg);
188 bnet_sig(bs, BNET_TERMINATE);
194 * Set debug level as requested by the Director
197 static int setdebug_cmd(JCR *jcr)
199 BSOCK *dir = jcr->dir_bsock;
200 int level, trace_flag;
202 Dmsg1(10, "setdebug_cmd: %s", dir->msg);
203 if (sscanf(dir->msg, "setdebug=%d", &level, &trace_flag) != 2 || level < 0) {
204 bnet_fsend(dir, "3991 Bad setdebug command: %s\n", dir->msg);
208 set_trace(trace_flag);
209 return bnet_fsend(dir, OKsetdebug, level);
216 static int cancel_cmd(JCR *cjcr)
218 BSOCK *dir = cjcr->dir_bsock;
220 char Job[MAX_NAME_LENGTH];
223 if (sscanf(dir->msg, "cancel Job=%127s", Job) == 1) {
224 if (!(jcr=get_jcr_by_full_name(Job))) {
225 bnet_fsend(dir, _("3992 Job %s not found.\n"), Job);
228 oldStatus = jcr->JobStatus;
229 set_jcr_job_status(jcr, JS_Canceled);
230 if (!jcr->authenticated && oldStatus == JS_WaitFD) {
231 pthread_cond_signal(&jcr->job_start_wait); /* wake waiting thread */
234 if (jcr->file_bsock) {
235 bnet_sig(jcr->file_bsock, BNET_TERMINATE);
237 /* If thread waiting on mount, wake him */
238 if (jcr->device && jcr->device->dev &&
239 (jcr->device->dev->dev_blocked == BST_WAITING_FOR_SYSOP ||
240 jcr->device->dev->dev_blocked == BST_UNMOUNTED ||
241 jcr->device->dev->dev_blocked == BST_UNMOUNTED_WAITING_FOR_SYSOP)) {
242 pthread_cond_signal(&jcr->device->dev->wait_next_vol);
244 bnet_fsend(dir, _("3000 Job %s marked to be canceled.\n"), jcr->Job);
248 bnet_fsend(dir, _("3993 Error scanning cancel command.\n"));
250 bnet_sig(dir, BNET_EOD);
258 static int label_cmd(JCR *jcr)
260 return do_label(jcr, 0);
263 static int relabel_cmd(JCR *jcr)
265 return do_label(jcr, 1);
268 static int do_label(JCR *jcr, int relabel)
270 POOLMEM *dname, *newname, *oldname, *poolname, *mtype;
271 BSOCK *dir = jcr->dir_bsock;
276 dname = get_memory(dir->msglen+1);
277 newname = get_memory(dir->msglen+1);
278 oldname = get_memory(dir->msglen+1);
279 poolname = get_memory(dir->msglen+1);
280 mtype = get_memory(dir->msglen+1);
282 if (sscanf(dir->msg, "relabel %s OldName=%s NewName=%s PoolName=%s MediaType=%s Slot=%d",
283 dname, oldname, newname, poolname, mtype, &slot) == 6) {
288 if (sscanf(dir->msg, "label %s VolumeName=%s PoolName=%s MediaType=%s Slot=%d",
289 dname, newname, poolname, mtype, &slot) == 5) {
294 unbash_spaces(newname);
295 unbash_spaces(oldname);
296 unbash_spaces(poolname);
297 unbash_spaces(mtype);
298 if (find_device(jcr, dname)) {
299 /******FIXME**** compare MediaTypes */
300 dev = jcr->device->dev;
302 P(dev->mutex); /* Use P to avoid indefinite block */
303 if (!(dev->state & ST_OPENED)) {
304 label_volume_if_ok(jcr, dev, oldname, newname, poolname, slot, relabel);
305 force_close_dev(dev);
306 /* Under certain "safe" conditions, we can steal the lock */
307 } else if (dev->dev_blocked &&
308 (dev->dev_blocked == BST_UNMOUNTED ||
309 dev->dev_blocked == BST_WAITING_FOR_SYSOP ||
310 dev->dev_blocked == BST_UNMOUNTED_WAITING_FOR_SYSOP)) {
311 label_volume_if_ok(jcr, dev, oldname, newname, poolname, slot, relabel);
312 } else if (dev_state(dev, ST_READ) || dev->num_writers) {
313 if (dev_state(dev, ST_READ)) {
314 bnet_fsend(dir, _("3911 Device %s is busy with 1 reader.\n"),
317 bnet_fsend(dir, _("3912 Device %s is busy with %d writer(s).\n"),
318 dev_name(dev), dev->num_writers);
320 } else { /* device not being used */
321 label_volume_if_ok(jcr, dev, oldname, newname, poolname, slot, relabel);
325 bnet_fsend(dir, _("3999 Device %s not found\n"), dname);
328 /* NB dir->msg gets clobbered in bnet_fsend, so save command */
329 pm_strcpy(&jcr->errmsg, dir->msg);
330 bnet_fsend(dir, _("3903 Error scanning label command: %s\n"), jcr->errmsg);
333 free_memory(oldname);
334 free_memory(newname);
335 free_memory(poolname);
337 bnet_sig(dir, BNET_EOD);
342 * Read the tape label and determine if we can safely
343 * label the tape (not a Bacula volume), then label it.
345 * Enter with the mutex set
347 static void label_volume_if_ok(JCR *jcr, DEVICE *dev, char *oldname,
348 char *newname, char *poolname,
349 int slot, int relabel)
351 BSOCK *dir = jcr->dir_bsock;
355 steal_device_lock(dev, &hold, BST_WRITING_LABEL);
357 pm_strcpy(&jcr->VolumeName, newname);
358 jcr->VolCatInfo.Slot = slot;
359 autoload_device(jcr, dev, 0, dir); /* autoload if possible */
360 block = new_block(dev);
362 /* Ensure that the device is open -- autoload_device() closes it */
363 for ( ; !(dev->state & ST_OPENED); ) {
364 if (open_dev(dev, jcr->VolumeName, READ_WRITE) < 0) {
365 bnet_fsend(dir, _("3910 Unable to open device %s. ERR=%s\n"),
366 dev_name(dev), strerror_dev(dev));
371 /* See what we have for a Volume */
372 switch (read_dev_volume_label(jcr, dev, block)) {
374 case VOL_VERSION_ERROR:
375 case VOL_LABEL_ERROR:
379 "3911 Cannot label Volume because it is already labeled: \"%s\"\n"),
380 dev->VolHdr.VolName);
383 /* Relabel request. If oldname matches, continue */
384 if (strcmp(oldname, dev->VolHdr.VolName) != 0) {
385 bnet_fsend(dir, _("Wrong volume mounted.\n"));
388 /* Fall through wanted! */
391 if (!write_volume_label_to_dev(jcr, jcr->device, newname, poolname)) {
392 bnet_fsend(dir, _("3912 Failed to label Volume: ERR=%s\n"), strerror_dev(dev));
395 pm_strcpy(&jcr->VolumeName, newname);
396 bnet_fsend(dir, _("3000 OK label. Volume=%s Device=%s\n"),
397 newname, dev_name(dev));
400 bnet_fsend(dir, _("3912 Failed to label Volume: ERR=%s\n"), strerror_dev(dev));
403 bnet_fsend(dir, _("3913 Cannot label Volume. \
404 Unknown status %d from read_volume_label()\n"), jcr->label_status);
409 give_back_device_lock(dev, &hold);
416 * Read the tape label
418 * Enter with the mutex set
420 static int read_label(JCR *jcr, DEVICE *dev)
423 BSOCK *dir = jcr->dir_bsock;
427 steal_device_lock(dev, &hold, BST_DOING_ACQUIRE);
429 jcr->VolumeName[0] = 0;
430 block = new_block(dev);
431 dev->state &= ~ST_LABEL; /* force read of label */
432 switch (read_dev_volume_label(jcr, dev, block)) {
434 bnet_fsend(dir, _("3001 Mounted Volume: %s\n"), dev->VolHdr.VolName);
438 bnet_fsend(dir, _("3902 Cannot mount Volume on Storage Device \"%s\" because:\n%s"),
439 dev_name(dev), jcr->errmsg);
444 give_back_device_lock(dev, &hold);
448 static bool find_device(JCR *jcr, char *dname)
450 DEVRES *device = NULL;
453 unbash_spaces(dname);
455 while ((device=(DEVRES *)GetNextRes(R_DEVICE, (RES *)device))) {
456 /* Find resource, and make sure we were able to open it */
457 if (strcmp(device->hdr.name, dname) == 0 && device->dev) {
458 Dmsg1(20, "Found device %s\n", device->hdr.name);
459 jcr->device = device;
470 * Mount command from Director
472 static int mount_cmd(JCR *jcr)
475 BSOCK *dir = jcr->dir_bsock;
478 dname = get_memory(dir->msglen+1);
479 if (sscanf(dir->msg, "mount %s", dname) == 1) {
480 if (find_device(jcr, dname)) {
482 dev = jcr->device->dev;
483 P(dev->mutex); /* Use P to avoid indefinite block */
484 switch (dev->dev_blocked) { /* device blocked? */
485 case BST_WAITING_FOR_SYSOP:
486 /* Someone is waiting, wake him */
487 Dmsg0(100, "Waiting for mount. Attempting to wake thread\n");
488 dev->dev_blocked = BST_MOUNT;
489 bnet_fsend(dir, "3001 OK mount. Device=%s\n", dev_name(dev));
490 pthread_cond_signal(&dev->wait_next_vol);
493 /* In both of these two cases, we (the user) unmounted the Volume */
494 case BST_UNMOUNTED_WAITING_FOR_SYSOP:
496 /* We freed the device, so reopen it and wake any waiting threads */
497 if (open_dev(dev, NULL, READ_WRITE) < 0) {
498 bnet_fsend(dir, _("3901 open device failed: ERR=%s\n"),
502 block = new_block(dev);
503 read_dev_volume_label(jcr, dev, block);
505 if (dev->dev_blocked == BST_UNMOUNTED) {
506 /* We blocked the device, so unblock it */
507 Dmsg0(100, "Unmounted. Unblocking device\n");
508 read_label(jcr, dev); /* this should not be necessary */
511 Dmsg0(100, "Unmounted waiting for mount. Attempting to wake thread\n");
512 dev->dev_blocked = BST_MOUNT;
514 if (dev_state(dev, ST_LABEL)) {
515 bnet_fsend(dir, _("3001 Device %s is mounted with Volume \"%s\"\n"),
516 dev_name(dev), dev->VolHdr.VolName);
518 bnet_fsend(dir, _("3905 Device %s open but no Bacula volume is mounted.\n"
519 "If this is not a blank tape, try unmounting and remounting the Volume.\n"),
522 pthread_cond_signal(&dev->wait_next_vol);
525 case BST_DOING_ACQUIRE:
526 bnet_fsend(dir, _("3001 Device %s is mounted; doing acquire.\n"),
530 case BST_WRITING_LABEL:
531 bnet_fsend(dir, _("3903 Device %s is being labeled.\n"), dev_name(dev));
534 case BST_NOT_BLOCKED:
535 if (dev_state(dev, ST_OPENED)) {
536 if (dev_state(dev, ST_LABEL)) {
537 bnet_fsend(dir, _("3001 Device %s is mounted with Volume \"%s\"\n"),
538 dev_name(dev), dev->VolHdr.VolName);
540 bnet_fsend(dir, _("3905 Device %s open but no Bacula volume is mounted.\n"
541 "If this is not a blank tape, try unmounting and remounting the Volume.\n"),
545 if (!dev_is_tape(dev)) {
546 bnet_fsend(dir, _("3906 cannot mount non-tape.\n"));
549 if (open_dev(dev, NULL, READ_WRITE) < 0) {
550 bnet_fsend(dir, _("3901 open device failed: ERR=%s\n"),
554 read_label(jcr, dev);
555 if (dev_state(dev, ST_LABEL)) {
556 bnet_fsend(dir, _("3001 Device %s is mounted with Volume \"%s\"\n"),
557 dev_name(dev), dev->VolHdr.VolName);
559 bnet_fsend(dir, _("3905 Device %s open but no Bacula volume is mounted.\n"
560 "If this is not a blank tape, try unmounting and remounting the Volume.\n"),
567 bnet_fsend(dir, _("3905 Bizarre wait state %d\n"), dev->dev_blocked);
572 bnet_fsend(dir, _("3999 Device %s not found\n"), dname);
575 pm_strcpy(&jcr->errmsg, dir->msg);
576 bnet_fsend(dir, _("3909 Error scanning mount command: %s\n"), jcr->errmsg);
579 bnet_sig(dir, BNET_EOD);
584 * unmount command from Director
586 static int unmount_cmd(JCR *jcr)
589 BSOCK *dir = jcr->dir_bsock;
592 dname = get_memory(dir->msglen+1);
593 if (sscanf(dir->msg, "unmount %s", dname) == 1) {
594 if (find_device(jcr, dname)) {
595 dev = jcr->device->dev;
596 P(dev->mutex); /* Use P to avoid indefinite block */
597 if (!(dev->state & ST_OPENED)) {
598 Dmsg0(90, "Device already unmounted\n");
599 bnet_fsend(dir, _("3901 Device \"%s\" is already unmounted.\n"), dev_name(dev));
601 } else if (dev->dev_blocked == BST_WAITING_FOR_SYSOP) {
602 Dmsg2(90, "%d waiter dev_block=%d. doing unmount\n", dev->num_waiting,
604 open_dev(dev, NULL, 0); /* fake open for close */
605 offline_or_rewind_dev(dev);
606 force_close_dev(dev);
607 dev->dev_blocked = BST_UNMOUNTED_WAITING_FOR_SYSOP;
608 bnet_fsend(dir, _("3001 Device \"%s\" unmounted.\n"), dev_name(dev));
610 } else if (dev->dev_blocked == BST_DOING_ACQUIRE) {
611 bnet_fsend(dir, _("3902 Device \"%s\" is busy in acquire.\n"), dev_name(dev));
613 } else if (dev->dev_blocked == BST_WRITING_LABEL) {
614 bnet_fsend(dir, _("3903 Device \"%s\" is being labeled.\n"), dev_name(dev));
616 } else if (dev_state(dev, ST_READ) || dev->num_writers) {
617 if (dev_state(dev, ST_READ)) {
618 Dmsg0(90, "Device in read mode\n");
619 bnet_fsend(dir, _("3904 Device \"%s\" is busy reading.\n"), dev_name(dev));
621 Dmsg1(90, "Device busy with %d writers\n", dev->num_writers);
622 bnet_fsend(dir, _("3905 Device %s is busy with %d writer(s).\n"),
623 dev_name(dev), dev->num_writers);
626 } else { /* device not being used */
627 Dmsg0(90, "Device not in use, unmounting\n");
628 /* On FreeBSD, I am having ASSERT() failures in block_device()
629 * and I can only imagine that the thread id that we are
630 * leaving in no_wait_id is being re-used. So here,
631 * we simply do it by hand. Gross, but a solution.
633 /* block_device(dev, BST_UNMOUNTED); replace with 2 lines below */
634 dev->dev_blocked = BST_UNMOUNTED;
636 open_dev(dev, NULL, 0); /* fake open for close */
637 offline_or_rewind_dev(dev);
638 force_close_dev(dev);
639 bnet_fsend(dir, _("3002 Device %s unmounted.\n"), dev_name(dev));
643 bnet_fsend(dir, _("3999 Device %s not found\n"), dname);
646 /* NB dir->msg gets clobbered in bnet_fsend, so save command */
647 pm_strcpy(&jcr->errmsg, dir->msg);
648 bnet_fsend(dir, _("3907 Error scanning unmount command: %s\n"), jcr->errmsg);
651 bnet_sig(dir, BNET_EOD);
656 * Release command from Director. This rewinds the device and if
657 * configured does a offline and ensures that Bacula will
658 * re-read the label of the tape before continuing. This gives
659 * the operator the chance to change the tape anytime before the
662 static int release_cmd(JCR *jcr)
665 BSOCK *dir = jcr->dir_bsock;
668 dname = get_memory(dir->msglen+1);
669 if (sscanf(dir->msg, "release %s", dname) == 1) {
670 if (find_device(jcr, dname)) {
671 dev = jcr->device->dev;
672 P(dev->mutex); /* Use P to avoid indefinite block */
673 if (!(dev->state & ST_OPENED)) {
674 Dmsg0(90, "Device already released\n");
675 bnet_fsend(dir, _("3911 Device %s already released.\n"), dev_name(dev));
677 } else if (dev->dev_blocked == BST_WAITING_FOR_SYSOP ||
678 dev->dev_blocked == BST_UNMOUNTED_WAITING_FOR_SYSOP) {
679 Dmsg2(90, "%d waiter dev_block=%d. doing unmount\n", dev->num_waiting,
681 bnet_fsend(dir, _("3912 Device %s waiting for mount.\n"), dev_name(dev));
683 } else if (dev->dev_blocked == BST_DOING_ACQUIRE) {
684 bnet_fsend(dir, _("3913 Device %s is busy in acquire.\n"), dev_name(dev));
686 } else if (dev->dev_blocked == BST_WRITING_LABEL) {
687 bnet_fsend(dir, _("3914 Device %s is being labeled.\n"), dev_name(dev));
689 } else if (dev_state(dev, ST_READ) || dev->num_writers) {
690 if (dev_state(dev, ST_READ)) {
691 Dmsg0(90, "Device in read mode\n");
692 bnet_fsend(dir, _("3915 Device %s is busy with 1 reader.\n"), dev_name(dev));
694 Dmsg1(90, "Device busy with %d writers\n", dev->num_writers);
695 bnet_fsend(dir, _("3916 Device %s is busy with %d writer(s).\n"),
696 dev_name(dev), dev->num_writers);
699 } else { /* device not being used */
700 Dmsg0(90, "Device not in use, unmounting\n");
701 release_volume(jcr, dev);
702 bnet_fsend(dir, _("3012 Device %s released.\n"), dev_name(dev));
706 bnet_fsend(dir, _("3999 Device %s not found\n"), dname);
709 /* NB dir->msg gets clobbered in bnet_fsend, so save command */
710 pm_strcpy(&jcr->errmsg, dir->msg);
711 bnet_fsend(dir, _("3917 Error scanning release command: %s\n"), jcr->errmsg);
714 bnet_sig(dir, BNET_EOD);
721 * Autochanger command from Director
723 static int autochanger_cmd(JCR *jcr)
726 BSOCK *dir = jcr->dir_bsock;
729 dname = get_memory(dir->msglen+1);
730 if (sscanf(dir->msg, "autochanger list %s ", dname) == 1) {
731 if (find_device(jcr, dname)) {
732 dev = jcr->device->dev;
733 P(dev->mutex); /* Use P to avoid indefinite block */
734 if (!dev_is_tape(dev)) {
735 bnet_fsend(dir, _("3995 Device %s is not an autochanger.\n"), dev_name(dev));
736 } else if (!(dev->state & ST_OPENED)) {
737 autochanger_list(jcr, dev, dir);
738 /* Under certain "safe" conditions, we can steal the lock */
739 } else if (dev->dev_blocked &&
740 (dev->dev_blocked == BST_UNMOUNTED ||
741 dev->dev_blocked == BST_WAITING_FOR_SYSOP ||
742 dev->dev_blocked == BST_UNMOUNTED_WAITING_FOR_SYSOP)) {
743 autochanger_list(jcr, dev, dir);
744 } else if (dev_state(dev, ST_READ) || dev->num_writers) {
745 if (dev_state(dev, ST_READ)) {
746 bnet_fsend(dir, _("3901 Device %s is busy with 1 reader.\n"), dev_name(dev));
748 bnet_fsend(dir, _("3902 Device %s is busy with %d writer(s).\n"),
749 dev_name(dev), dev->num_writers);
751 } else { /* device not being used */
752 autochanger_list(jcr, dev, dir);
756 bnet_fsend(dir, _("3999 Device %s not found\n"), dname);
758 } else { /* error on scanf */
759 pm_strcpy(&jcr->errmsg, dir->msg);
760 bnet_fsend(dir, _("3908 Error scanning autocharger list command: %s\n"),
764 bnet_sig(dir, BNET_EOD);
769 * Read and return the Volume label
771 static int readlabel_cmd(JCR *jcr)
774 BSOCK *dir = jcr->dir_bsock;
778 dname = get_memory(dir->msglen+1);
779 if (sscanf(dir->msg, "readlabel %s Slot=%d", dname, &Slot) == 2) {
780 if (find_device(jcr, dname)) {
781 dev = jcr->device->dev;
783 P(dev->mutex); /* Use P to avoid indefinite block */
784 if (!dev_state(dev, ST_OPENED)) {
785 read_volume_label(jcr, dev, Slot);
786 force_close_dev(dev);
787 /* Under certain "safe" conditions, we can steal the lock */
788 } else if (dev->dev_blocked &&
789 (dev->dev_blocked == BST_UNMOUNTED ||
790 dev->dev_blocked == BST_WAITING_FOR_SYSOP ||
791 dev->dev_blocked == BST_UNMOUNTED_WAITING_FOR_SYSOP)) {
792 read_volume_label(jcr, dev, Slot);
793 } else if (dev_state(dev, ST_READ) || dev->num_writers) {
794 if (dev_state(dev, ST_READ)) {
795 bnet_fsend(dir, _("3911 Device %s is busy with 1 reader.\n"),
798 bnet_fsend(dir, _("3912 Device %s is busy with %d writer(s).\n"),
799 dev_name(dev), dev->num_writers);
801 } else { /* device not being used */
802 read_volume_label(jcr, dev, Slot);
806 bnet_fsend(dir, _("3999 Device %s not found\n"), dname);
809 pm_strcpy(&jcr->errmsg, dir->msg);
810 bnet_fsend(dir, _("3909 Error scanning readlabel command: %s\n"), jcr->errmsg);
813 bnet_sig(dir, BNET_EOD);
818 * Read the tape label
820 * Enter with the mutex set
822 static void read_volume_label(JCR *jcr, DEVICE *dev, int Slot)
824 BSOCK *dir = jcr->dir_bsock;
828 steal_device_lock(dev, &hold, BST_WRITING_LABEL);
830 jcr->VolumeName[0] = 0;
831 jcr->VolCatInfo.Slot = Slot;
832 autoload_device(jcr, dev, 0, dir); /* autoload if possible */
833 block = new_block(dev);
835 /* Ensure that the device is open -- autoload_device() closes it */
836 for ( ; !dev_state(dev, ST_OPENED); ) {
837 if (open_dev(dev, jcr->VolumeName, READ_WRITE) < 0) {
838 bnet_fsend(dir, _("3910 Unable to open device \"%s\". ERR=%s\n"),
839 dev_name(dev), strerror_dev(dev));
844 dev->state &= ~ST_LABEL; /* force read of label */
845 switch (read_dev_volume_label(jcr, dev, block)) {
847 bnet_fsend(dir, _("3001 Volume=\"%s\" Slot=%d\n"), dev->VolHdr.VolName, Slot);
848 Dmsg1(100, "Volume: %s\n", dev->VolHdr.VolName);
851 bnet_fsend(dir, _("3902 Cannot mount Volume on Storage Device \"%s\" because:\n%s"),
852 dev_name(dev), jcr->errmsg);
858 give_back_device_lock(dev, &hold);