2 Bacula® - The Network Backup Solution
4 Copyright (C) 2001-2014 Free Software Foundation Europe e.V.
6 The main author of Bacula is Kern Sibbald, with contributions from many
7 others, a complete list can be found in the file AUTHORS.
9 You may use this file and others of this release according to the
10 license defined in the LICENSE file, which includes the Affero General
11 Public License, v3.0 ("AGPLv3") and some additional permissions and
12 terms pursuant to its AGPLv3 Section 7.
14 Bacula® is a registered trademark of Kern Sibbald.
17 * This file handles accepting Director Commands
19 * Most Director commands are handled here, with the
20 * exception of the Job command command and subsequent
21 * subcommands that are handled
24 * N.B. in this file, in general we must use P(dev->mutex) rather
25 * than dev->rLock() so that we can examine the blocked
26 * state rather than blocking ourselves because a Job
27 * thread has the device blocked. In some "safe" cases,
28 * we can do things to a blocked device. CAREFUL!!!!
30 * File daemon commands are handled in fdcmd.c
32 * Written by Kern Sibbald, May MMI
39 /* Exported variables */
41 /* Imported variables */
42 extern BSOCK *filed_chan;
43 extern struct s_last_job last_job;
44 extern bool init_done;
46 /* Static variables */
47 static char derrmsg[] = "3900 Invalid command:";
48 static char OKsetdebug[] = "3000 OK setdebug=%ld trace=%ld options=%s tags=%s\n";
49 static char invalid_cmd[] = "3997 Invalid command for a Director with Monitor directive enabled.\n";
50 static char OK_bootstrap[] = "3000 OK bootstrap\n";
51 static char ERROR_bootstrap[] = "3904 Error bootstrap\n";
52 static char OKclient[] = "3000 OK client command\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 client_cmd(JCR *jcr);
65 static bool storage_cmd(JCR *jcr);
66 static bool label_cmd(JCR *jcr);
67 static bool die_cmd(JCR *jcr);
68 static bool relabel_cmd(JCR *jcr);
69 static bool readlabel_cmd(JCR *jcr);
70 static bool release_cmd(JCR *jcr);
71 static bool setdebug_cmd(JCR *jcr);
72 static bool cancel_cmd(JCR *cjcr);
73 static bool mount_cmd(JCR *jcr);
74 static bool unmount_cmd(JCR *jcr);
75 //static bool action_on_purge_cmd(JCR *jcr);
76 static bool bootstrap_cmd(JCR *jcr);
77 static bool changer_cmd(JCR *sjcr);
78 static bool do_label(JCR *jcr, int relabel);
79 static DCR *find_device(JCR *jcr, POOL_MEM &dev_name,
80 POOLMEM *media_type, int drive);
81 static void read_volume_label(JCR *jcr, DCR *dcr, DEVICE *dev, int Slot);
82 static void label_volume_if_ok(DCR *dcr, char *oldname,
83 char *newname, char *poolname,
84 int Slot, int relabel);
85 static bool try_autoload_device(JCR *jcr, DCR *dcr, int slot, const char *VolName);
86 static void send_dir_busy_message(BSOCK *dir, DEVICE *dev);
88 /* Responses send to Director for storage command */
89 static char BADcmd[] = "2902 Bad %s\n";
90 static char OKstore[] = "2000 OK storage\n";
92 /* Commands received from director that need scanning */
93 static char storaddr[] = "storage address=%s port=%d ssl=%d Job=%127s Authentication=%127s";
97 bool (*func)(JCR *jcr);
98 bool monitoraccess; /* set if monitors can access this cmd */
102 * The following are the recognized commands from the Director.
104 static struct s_cmds cmds[] = {
105 {"JobId=", job_cmd, 0}, /* start Job */
106 {"autochanger", changer_cmd, 0},
107 {"bootstrap", bootstrap_cmd, 0},
108 {"cancel", cancel_cmd, 0},
109 {"client", client_cmd, 0}, /* client address */
110 {".die", die_cmd, 0},
111 {"label", label_cmd, 0}, /* label a tape */
112 {"mount", mount_cmd, 0},
113 {"readlabel", readlabel_cmd, 0},
114 {"release", release_cmd, 0},
115 {"relabel", relabel_cmd, 0}, /* relabel a tape */
116 {"setdebug=", setdebug_cmd, 0}, /* set debug level */
117 {"status", status_cmd, 1},
118 {".status", qstatus_cmd, 1},
119 {"stop", cancel_cmd, 0},
120 {"storage", storage_cmd, 0}, /* get SD addr from Dir */
121 {"unmount", unmount_cmd, 0},
122 {"use storage=", use_cmd, 0},
124 // {"query", query_cmd, 0},
125 {NULL, NULL} /* list terminator */
130 * Connection request. We accept connections either from the
131 * Director or a Client (File daemon).
133 * Note, we are running as a seperate thread of the Storage daemon.
134 * and it is because a Director has made a connection with
135 * us on the "Message" channel.
137 * Basic tasks done here:
138 * - Create a JCR record
139 * - If it was from the FD, call handle_filed_connection()
140 * - Authenticate the Director
141 * - We wait for a command
142 * - We execute the command
143 * - We continue or exit depending on the return status
145 void *handle_connection_request(void *arg)
147 BSOCK *bs = (BSOCK *)arg;
150 int fd_version, sd_version;
156 if (bs->recv() <= 0) {
157 Jmsg1(NULL, M_ERROR, 0, _("Connection request from %s failed.\n"), bs->who());
158 bmicrosleep(5, 0); /* make user wait 5 seconds */
164 * Do a sanity check on the message received
166 if (bs->msglen < 25 || bs->msglen > (int)sizeof(name)) {
167 Pmsg1(000, "<filed: %s", bs->msg);
168 Jmsg2(NULL, M_ERROR, 0, _("Invalid connection from %s. Len=%d\n"), bs->who(), bs->msglen);
169 bmicrosleep(5, 0); /* make user wait 5 seconds */
174 Dmsg1(100, "Conn: %s", bs->msg);
178 * See if this is a File daemon connection. If so
181 if (sscanf(bs->msg, "Hello Bacula SD: Start Job %127s %d %d", name, &fd_version, &sd_version) == 3 ||
182 sscanf(bs->msg, "Hello FD: Bacula Storage calling Start Job %127s %d", name, &sd_version) == 2 ||
183 sscanf(bs->msg, "Hello Start Job %127s", name) == 1) {
184 Dmsg1(050, "Got a FD connection at %s\n", bstrftimes(tbuf, sizeof(tbuf),
185 (utime_t)time(NULL)));
186 Dmsg1(50, "%s", bs->msg);
187 handle_filed_connection(bs, name, fd_version, sd_version);
192 * This is a connection from the Director, so setup a JCR
194 Dmsg1(050, "Got a DIR connection at %s\n", bstrftimes(tbuf, sizeof(tbuf),
195 (utime_t)time(NULL)));
196 jcr = new_jcr(sizeof(JCR), stored_free_jcr); /* create Job Control Record */
197 jcr->dir_bsock = bs; /* save Director bsock */
198 jcr->dir_bsock->set_jcr(jcr);
199 jcr->dcrs = New(alist(10, not_owned_by_alist));
200 /* Initialize FD start condition variable */
201 int errstat = pthread_cond_init(&jcr->job_start_wait, NULL);
204 Jmsg1(jcr, M_FATAL, 0, _("Unable to init job cond variable: ERR=%s\n"), be.bstrerror(errstat));
208 Dmsg0(1000, "stored in start_job\n");
211 * Authenticate the Director
213 /* We should have: Hello SD: Bacula Director <dirname> calling */
214 if (!authenticate_director(jcr)) {
215 Jmsg(jcr, M_FATAL, 0, _("Unable to authenticate Director\n"));
218 Dmsg0(90, "Message channel init completed.\n");
220 for (quit=false; !quit;) {
222 if ((bnet_stat = bs->recv()) <= 0) {
223 break; /* connection terminated */
225 Dmsg1(199, "<dird: %s\n", bs->msg);
226 /* Ensure that device initialization is complete */
231 for (i=0; cmds[i].cmd; i++) {
232 if (strncmp(cmds[i].cmd, bs->msg, strlen(cmds[i].cmd)) == 0) {
233 if ((!cmds[i].monitoraccess) && (jcr->director->monitor)) {
234 Dmsg1(100, "Command \"%s\" is invalid.\n", cmds[i].cmd);
235 bs->fsend(invalid_cmd);
236 bs->signal(BNET_EOD);
239 Dmsg1(200, "Do command: %s\n", cmds[i].cmd);
240 if (!cmds[i].func(jcr)) { /* do command */
241 quit = true; /* error, get out */
242 Dmsg1(190, "Command %s requests quit\n", cmds[i].cmd);
244 found = true; /* indicate command found */
248 if (!found) { /* command not found */
250 Mmsg(err_msg, "%s %s\n", derrmsg, bs->msg);
251 bs->fsend(err_msg.c_str());
256 generate_daemon_event(jcr, "JobEnd");
257 generate_plugin_event(jcr, bsdEventJobEnd);
258 dequeue_messages(jcr); /* send any queued messages */
259 bs->signal(BNET_TERMINATE);
260 free_plugins(jcr); /* release instantiated plugins */
267 * Force SD to die, and hopefully dump itself. Turned on only
268 * in development version.
270 static bool die_cmd(JCR *jcr)
275 BSOCK *dir = jcr->dir_bsock;
276 pthread_mutex_t m=PTHREAD_MUTEX_INITIALIZER;
278 if (strstr(dir->msg, "deadlock")) {
279 Pmsg0(000, "I have been requested to deadlock ...\n");
284 Pmsg1(000, "I have been requested to die ... (%s)\n", dir->msg);
285 a = djcr->JobId; /* ref NULL pointer */
292 * Get address of client from Director
293 * We attempt to connect to the client (an FD or SD) and
296 static bool client_cmd(JCR *jcr)
298 int client_port; /* client port */
299 int enable_ssl; /* enable ssl */
300 BSOCK *dir = jcr->dir_bsock;
301 BSOCK *cl = new_bsock(); /* client bsock */
303 Dmsg1(100, "ClientCmd: %s", dir->msg);
304 jcr->sd_calls_client = true;
305 if (sscanf(dir->msg, "client address=%s port=%d ssl=%d", &jcr->client_addr, &client_port,
307 pm_strcpy(jcr->errmsg, dir->msg);
308 Jmsg(jcr, M_FATAL, 0, _("Bad client command: %s"), jcr->errmsg);
309 Dmsg1(050, "Bad client command: %s", jcr->errmsg);
313 Dmsg3(110, "Connect to client: %s:%d ssl=%d\n", jcr->client_addr, client_port,
315 /* Open command communications with Client */
316 /* Try to connect for 1 hour at 10 second intervals */
317 if (!cl->connect(jcr, 10, (int)me->ClientConnectTimeout, me->heartbeat_interval,
318 _("Client daemon"), jcr->client_addr, NULL, client_port, 1)) {
319 /* destroy() OK because cl is local */
321 Jmsg(jcr, M_FATAL, 0, _("Failed to connect to Client daemon: %s:%d\n"),
322 jcr->client_addr, client_port);
323 Dmsg2(100, "Failed to connect to Client daemon: %s:%d\n",
324 jcr->client_addr, client_port);
327 Dmsg0(110, "SD connection OK to Client.\n");
330 cl->fsend("Hello FD: Bacula Storage calling Start Job %s 1\n", jcr->Job);
331 jcr->file_bsock = cl;
332 jcr->file_bsock->set_jcr(jcr);
333 /* Send OK to Director */
334 return dir->fsend(OKclient);
337 jcr->setJobStatus(JS_ErrorTerminated);
338 dir->fsend("3902 Bad %s cmd\n", "client");
343 * Get address of storage daemon from Director
345 static bool storage_cmd(JCR *jcr)
347 int stored_port; /* storage daemon port */
348 int enable_ssl; /* enable ssl to sd */
349 char sd_auth_key[200];
350 BSOCK *dir = jcr->dir_bsock;
351 BSOCK *sd = new_bsock(); /* storage daemon bsock */
352 char Job[MAX_NAME_LENGTH];
354 Dmsg1(050, "StorageCmd: %s", dir->msg);
355 if (sscanf(dir->msg, storaddr, &jcr->stored_addr, &stored_port,
356 &enable_ssl, Job, sd_auth_key) != 5) {
357 pm_strcpy(jcr->errmsg, dir->msg);
358 Jmsg(jcr, M_FATAL, 0, _("Bad storage command: %s"), jcr->errmsg);
359 Pmsg1(010, "Bad storage command: %s", jcr->errmsg);
364 if (jcr->sd_auth_key) {
365 bfree_and_null(jcr->sd_auth_key);
366 jcr->sd_auth_key = bstrdup(sd_auth_key);
368 if (stored_port != 0) {
369 Dmsg2(050, "sd_calls=%d sd_client=%d\n", jcr->sd_calls_client,
371 jcr->sd_calls_client = false; /* We are doing the connecting */
372 Dmsg3(050, "Connect to storage and wait: %s:%d ssl=%d\n", jcr->stored_addr, stored_port,
374 /* Open command communications with Storage daemon */
375 /* Try to connect for 1 hour at 10 second intervals */
376 if (!sd->connect(jcr, 10, (int)me->ClientConnectTimeout, me->heartbeat_interval,
377 _("Storage daemon"), jcr->stored_addr, NULL, stored_port, 1)) {
378 /* destroy() OK because sd is local */
380 Jmsg(jcr, M_FATAL, 0, _("Failed to connect to Storage daemon: %s:%d\n"),
381 jcr->stored_addr, stored_port);
382 Dmsg2(010, "Failed to connect to Storage daemon: %s:%d\n",
383 jcr->stored_addr, stored_port);
387 Dmsg0(050, "Connection OK to SD.\n");
389 jcr->store_bsock = sd;
390 } else { /* The storage daemon called us */
391 jcr->sd_calls_client = true;
392 /* We should already have a storage connection! */
393 if (jcr->file_bsock && jcr->store_bsock == NULL) {
394 jcr->store_bsock = jcr->file_bsock;
396 if (jcr->store_bsock == NULL) {
397 Jmsg0(jcr, M_FATAL, 0, _("In storage_cmd port==0, no prior Storage connection.\n"));
398 Pmsg0(010, "In storage_cmd port==0, no prior Storage connection.\n");
403 if (!authenticate_storagedaemon(jcr, Job)) {
407 * We are a client so we read from the socket we just
408 * opened as if we were a FD, so set file_bsock and
409 * clear the store_bsock.
411 jcr->file_bsock = jcr->store_bsock;
412 jcr->store_bsock = NULL;
413 jcr->authenticated = true; /* Dir authentication is sufficient */
414 Dmsg1(050, "=== Storage_cmd authenticated Job=%s with SD.\n", Job);
416 /* Send OK to Director */
417 return dir->fsend(OKstore);
420 Dmsg0(100, "Send storage command failed.\n");
421 dir->fsend(BADcmd, "storage");
427 * Set debug level as requested by the Director
430 static bool setdebug_cmd(JCR *jcr)
432 BSOCK *dir = jcr->dir_bsock;
433 int32_t trace_flag, lvl, hangup; /* hangup is ignored right now */
437 *tags = *options = 0;
439 Dmsg1(10, "setdebug_cmd: %s", dir->msg);
441 if (sscanf(dir->msg, "setdebug=%ld trace=%ld hangup=%ld options=%55s tags=%511s",
442 &lvl, &trace_flag, &hangup, options, tags) != 5)
444 if (sscanf(dir->msg, "setdebug=%ld trace=%ld", &lvl, &trace_flag) != 2 || lvl < 0) {
445 dir->fsend(_("3991 Bad setdebug command: %s\n"), dir->msg);
450 set_trace(trace_flag);
451 set_debug_flags(options);
452 if (!debug_parse_tags(tags, &level)) {
457 return dir->fsend(OKsetdebug, lvl, trace_flag, options, tags);
463 * Be careful, we switch to using the job's JCR! So, using
464 * BSOCKs on that jcr can have two threads in the same code.
466 static bool cancel_cmd(JCR *cjcr)
468 BSOCK *dir = cjcr->dir_bsock;
470 char Job[MAX_NAME_LENGTH];
475 if (sscanf(dir->msg, "cancel Job=%127s", Job) == 1) {
476 status = JS_Canceled;
479 dir->fsend(_("3903 Error scanning cancel command.\n"));
482 if (!(jcr=get_jcr_by_full_name(Job))) {
483 dir->fsend(_("3904 Job %s not found.\n"), Job);
485 oldStatus = jcr->JobStatus;
486 jcr->setJobStatus(status);
487 Dmsg2(800, "Cancel JobId=%d %p\n", jcr->JobId, jcr);
488 if (!jcr->authenticated && oldStatus == JS_WaitFD) {
489 pthread_cond_signal(&jcr->job_start_wait); /* wake waiting thread */
491 if (jcr->file_bsock) {
492 jcr->file_bsock->set_terminated();
493 jcr->file_bsock->set_timed_out();
494 Dmsg2(800, "Term bsock jid=%d %p\n", jcr->JobId, jcr);
496 /* Still waiting for FD to connect, release it */
497 pthread_cond_signal(&jcr->job_start_wait); /* wake waiting job */
498 Dmsg2(800, "Signal FD connect jid=%d %p\n", jcr->JobId, jcr);
500 /* If thread waiting on mount, wake him */
501 if (jcr->dcr && jcr->dcr->dev && jcr->dcr->dev->waiting_for_mount()) {
502 pthread_cond_broadcast(&jcr->dcr->dev->wait_next_vol);
503 Dmsg1(100, "JobId=%u broadcast wait_device_release\n", (uint32_t)jcr->JobId);
504 pthread_cond_broadcast(&wait_device_release);
506 if (jcr->read_dcr && jcr->read_dcr->dev && jcr->read_dcr->dev->waiting_for_mount()) {
507 pthread_cond_broadcast(&jcr->read_dcr->dev->wait_next_vol);
508 Dmsg1(100, "JobId=%u broadcast wait_device_release\n", (uint32_t)jcr->JobId);
509 pthread_cond_broadcast(&wait_device_release);
511 dir->fsend(_("3000 JobId=%ld Job=\"%s\" marked to be %s.\n"),
512 jcr->JobId, jcr->Job, reason);
517 dir->signal(BNET_EOD);
525 static bool label_cmd(JCR *jcr)
527 return do_label(jcr, 0);
530 static bool relabel_cmd(JCR *jcr)
532 return do_label(jcr, 1);
535 static bool do_label(JCR *jcr, int relabel)
537 POOLMEM *newname, *oldname, *poolname, *mtype;
539 BSOCK *dir = jcr->dir_bsock;
545 newname = get_memory(dir->msglen+1);
546 oldname = get_memory(dir->msglen+1);
547 poolname = get_memory(dir->msglen+1);
548 mtype = get_memory(dir->msglen+1);
550 if (sscanf(dir->msg, "relabel %127s OldName=%127s NewName=%127s PoolName=%127s "
551 "MediaType=%127s Slot=%d drive=%d",
552 dev_name.c_str(), oldname, newname, poolname, mtype,
553 &slot, &drive) == 7) {
558 if (sscanf(dir->msg, "label %127s VolumeName=%127s PoolName=%127s "
559 "MediaType=%127s Slot=%d drive=%d",
560 dev_name.c_str(), newname, poolname, mtype, &slot, &drive) == 6) {
565 unbash_spaces(newname);
566 unbash_spaces(oldname);
567 unbash_spaces(poolname);
568 unbash_spaces(mtype);
569 dcr = find_device(jcr, dev_name, mtype, drive);
572 dev->Lock(); /* Use P to avoid indefinite block */
573 if (!dev->is_open() && !dev->is_busy()) {
574 Dmsg1(400, "Can %slabel. Device is not open\n", relabel?"re":"");
575 label_volume_if_ok(dcr, oldname, newname, poolname, slot, relabel);
577 /* Under certain "safe" conditions, we can steal the lock */
578 } else if (dev->can_steal_lock()) {
579 Dmsg0(400, "Can relabel. can_steal_lock\n");
580 label_volume_if_ok(dcr, oldname, newname, poolname, slot, relabel);
581 } else if (dev->is_busy() || dev->is_blocked()) {
582 send_dir_busy_message(dir, dev);
583 } else { /* device not being used */
584 Dmsg0(400, "Can relabel. device not used\n");
585 label_volume_if_ok(dcr, oldname, newname, poolname, slot, relabel);
590 dir->fsend(_("3999 Device \"%s\" not found or could not be opened.\n"), dev_name.c_str());
593 /* NB dir->msg gets clobbered in bnet_fsend, so save command */
594 pm_strcpy(jcr->errmsg, dir->msg);
595 dir->fsend(_("3903 Error scanning label command: %s\n"), jcr->errmsg);
597 free_memory(oldname);
598 free_memory(newname);
599 free_memory(poolname);
601 dir->signal(BNET_EOD);
606 * Read the tape label and determine if we can safely
607 * label the tape (not a Bacula volume), then label it.
609 * Enter with the mutex set
611 static void label_volume_if_ok(DCR *dcr, char *oldname,
612 char *newname, char *poolname,
613 int slot, int relabel)
615 BSOCK *dir = dcr->jcr->dir_bsock;
617 DEVICE *dev = dcr->dev;
620 const char *volname = (relabel == 1) ? oldname : newname;
623 steal_device_lock(dev, &hold, BST_WRITING_LABEL);
624 Dmsg1(100, "Stole device %s lock, writing label.\n", dev->print_name());
626 Dmsg0(90, "try_autoload_device - looking for volume_info\n");
627 if (!try_autoload_device(dcr->jcr, dcr, slot, volname)) {
628 goto bail_out; /* error */
631 /* Ensure that the device is open -- autoload_device() closes it */
632 if (dev->is_tape()) {
633 mode = OPEN_READ_WRITE;
635 mode = CREATE_READ_WRITE;
639 dev->truncating = true; /* let open() know we will truncate it */
641 /* Set old volume name for open if relabeling */
642 dcr->setVolCatName(volname);
643 if (!dev->open(dcr, mode)) {
644 dir->fsend(_("3910 Unable to open device \"%s\": ERR=%s\n"),
645 dev->print_name(), dev->bstrerror());
649 /* See what we have for a Volume */
650 label_status = read_dev_volume_label(dcr);
652 /* Set new volume name */
653 dcr->setVolCatName(newname);
654 switch(label_status) {
656 case VOL_VERSION_ERROR:
657 case VOL_LABEL_ERROR:
661 "3920 Cannot label Volume because it is already labeled: \"%s\"\n"),
662 dev->VolHdr.VolumeName);
666 /* Relabel request. If oldname matches, continue */
667 if (strcmp(oldname, dev->VolHdr.VolumeName) != 0) {
668 dir->fsend(_("3921 Wrong volume mounted.\n"));
671 if (dev->label_type != B_BACULA_LABEL) {
672 dir->fsend(_("3922 Cannot relabel an ANSI/IBM labeled Volume.\n"));
675 /* Fall through wanted! */
678 if (!write_new_volume_label_to_dev(dcr, newname, poolname,
679 relabel, true /* write dvd now */)) {
680 dir->fsend(_("3912 Failed to label Volume: ERR=%s\n"), dev->bstrerror());
683 bstrncpy(dcr->VolumeName, newname, sizeof(dcr->VolumeName));
684 /* The following 3000 OK label. string is scanned in ua_label.c */
685 dir->fsend("3000 OK label. VolBytes=%s DVD=%d Volume=\"%s\" Device=%s\n",
686 edit_uint64(dev->VolCatInfo.VolCatBytes, ed1),
687 dev->is_dvd()?1:0, newname, dev->print_name());
690 dir->fsend(_("3915 Failed to label Volume: ERR=%s\n"), dev->errmsg);
693 dir->fsend(_("3914 Failed to label Volume (no media): ERR=%s\n"), dev->bstrerror());
696 dir->fsend(_("3913 Cannot label Volume. "
697 "Unknown status %d from read_volume_label()\n"), label_status);
702 if (dev->is_open() && !dev->has_cap(CAP_ALWAYSOPEN)) {
705 if (!dev->is_open()) {
708 volume_unused(dcr); /* no longer using volume */
709 give_back_device_lock(dev, &hold);
715 * Read the tape label
717 * Enter with the mutex set
719 static bool read_label(DCR *dcr)
723 BSOCK *dir = jcr->dir_bsock;
725 DEVICE *dev = dcr->dev;
727 steal_device_lock(dev, &hold, BST_DOING_ACQUIRE);
729 dcr->VolumeName[0] = 0;
730 dev->clear_labeled(); /* force read of label */
731 switch (read_dev_volume_label(dcr)) {
733 dir->fsend(_("3001 Mounted Volume: %s\n"), dev->VolHdr.VolumeName);
737 dir->fsend(_("3902 Cannot mount Volume on Storage Device \"%s\" because:\n%s"),
738 dev->print_name(), jcr->errmsg);
743 give_back_device_lock(dev, &hold);
748 * Searches for device by name, and if found, creates a dcr and
751 static DCR *find_device(JCR *jcr, POOL_MEM &devname,
752 POOLMEM *media_type, int drive)
755 AUTOCHANGER *changer;
759 unbash_spaces(devname);
760 foreach_res(device, R_DEVICE) {
761 /* Find resource, and make sure we were able to open it */
762 if (strcmp(device->hdr.name, devname.c_str()) == 0 &&
763 (!media_type || strcmp(device->media_type, media_type) ==0)) {
765 device->dev = init_dev(jcr, device);
768 Jmsg(jcr, M_WARNING, 0, _("\n"
769 " Device \"%s\" requested by DIR could not be opened or does not exist.\n"),
773 Dmsg1(20, "Found device %s\n", device->hdr.name);
779 foreach_res(changer, R_AUTOCHANGER) {
780 /* Find resource, and make sure we were able to open it */
781 if (strcmp(devname.c_str(), changer->hdr.name) == 0) {
782 /* Try each device in this AutoChanger */
783 foreach_alist(device, changer->device) {
784 Dmsg1(100, "Try changer device %s\n", device->hdr.name);
786 device->dev = init_dev(jcr, device);
789 Dmsg1(100, "Device %s could not be opened. Skipped\n", devname.c_str());
790 Jmsg(jcr, M_WARNING, 0, _("\n"
791 " Device \"%s\" in changer \"%s\" requested by DIR could not be opened or does not exist.\n"),
792 device->hdr.name, devname.c_str());
795 if (!device->dev->autoselect) {
796 Dmsg1(100, "Device %s not autoselect skipped.\n", devname.c_str());
797 continue; /* device is not available */
799 if ((drive < 0 || drive == (int)device->dev->drive_index) &&
800 (!media_type || strcmp(device->media_type, media_type) ==0)) {
801 Dmsg1(20, "Found changer device %s\n", device->hdr.name);
805 Dmsg3(100, "Device %s drive wrong: want=%d got=%d skipping\n",
806 devname.c_str(), drive, (int)device->dev->drive_index);
808 break; /* we found it but could not open a device */
814 Dmsg1(100, "Found device %s\n", device->hdr.name);
815 dcr = new_dcr(jcr, NULL, device->dev);
816 dcr->device = device;
823 * Mount command from Director
825 static bool mount_cmd(JCR *jcr)
828 BSOCK *dir = jcr->dir_bsock;
835 ok = sscanf(dir->msg, "mount %127s drive=%d slot=%d", devname.c_str(),
838 ok = sscanf(dir->msg, "mount %127s drive=%d", devname.c_str(), &drive) == 2;
840 Dmsg3(100, "ok=%d drive=%d slot=%d\n", ok, drive, slot);
842 dcr = find_device(jcr, devname, NULL, drive);
845 dev->Lock(); /* Use P to avoid indefinite block */
846 Dmsg2(100, "mount cmd blocked=%d must_unload=%d\n", dev->blocked(),
848 switch (dev->blocked()) { /* device blocked? */
849 case BST_WAITING_FOR_SYSOP:
850 /* Someone is waiting, wake him */
851 Dmsg0(100, "Waiting for mount. Attempting to wake thread\n");
852 dev->set_blocked(BST_MOUNT);
853 dir->fsend("3001 OK mount requested. %sDevice=%s\n",
854 slot>0?_("Specified slot ignored. "):"",
856 Dmsg1(100, "JobId=%u broadcast wait_next_vol\n", (uint32_t)dcr->jcr->JobId);
857 pthread_cond_broadcast(&dev->wait_next_vol);
858 Dmsg1(100, "JobId=%u broadcast wait_device_release\n", (uint32_t)dcr->jcr->JobId);
859 pthread_cond_broadcast(&wait_device_release);
862 /* In both of these two cases, we (the user) unmounted the Volume */
863 case BST_UNMOUNTED_WAITING_FOR_SYSOP:
865 Dmsg2(100, "Unmounted changer=%d slot=%d\n", dev->is_autochanger(), slot);
866 if (dev->is_autochanger() && slot > 0) {
867 try_autoload_device(jcr, dcr, slot, "");
869 /* We freed the device, so reopen it and wake any waiting threads */
870 if (!dev->open(dcr, OPEN_READ_ONLY)) {
871 dir->fsend(_("3901 Unable to open device \"%s\": ERR=%s\n"),
872 dev->print_name(), dev->bstrerror());
873 if (dev->blocked() == BST_UNMOUNTED) {
874 /* We blocked the device, so unblock it */
875 Dmsg0(100, "Unmounted. Unblocking device\n");
880 read_dev_volume_label(dcr);
881 if (dev->blocked() == BST_UNMOUNTED) {
882 /* We blocked the device, so unblock it */
883 Dmsg0(100, "Unmounted. Unblocking device\n");
884 read_label(dcr); /* this should not be necessary */
887 Dmsg0(100, "Unmounted waiting for mount. Attempting to wake thread\n");
888 dev->set_blocked(BST_MOUNT);
890 if (dev->is_labeled()) {
891 dir->fsend(_("3001 Device \"%s\" is mounted with Volume \"%s\"\n"),
892 dev->print_name(), dev->VolHdr.VolumeName);
894 dir->fsend(_("3905 Device \"%s\" open but no Bacula volume is mounted.\n"
895 "If this is not a blank tape, try unmounting and remounting the Volume.\n"),
898 pthread_cond_broadcast(&dev->wait_next_vol);
899 Dmsg1(100, "JobId=%u broadcast wait_device_release\n", (uint32_t)dcr->jcr->JobId);
900 pthread_cond_broadcast(&wait_device_release);
903 case BST_DOING_ACQUIRE:
904 dir->fsend(_("3001 Device \"%s\" is doing acquire.\n"),
908 case BST_WRITING_LABEL:
909 dir->fsend(_("3903 Device \"%s\" is being labeled.\n"),
913 case BST_NOT_BLOCKED:
914 Dmsg2(100, "Not blocked changer=%d slot=%d\n", dev->is_autochanger(), slot);
915 if (dev->is_autochanger() && slot > 0) {
916 try_autoload_device(jcr, dcr, slot, "");
918 if (dev->is_open()) {
919 if (dev->is_labeled()) {
920 dir->fsend(_("3001 Device \"%s\" is mounted with Volume \"%s\"\n"),
921 dev->print_name(), dev->VolHdr.VolumeName);
923 dir->fsend(_("3905 Device \"%s\" open but no Bacula volume is mounted.\n"
924 "If this is not a blank tape, try unmounting and remounting the Volume.\n"),
927 } else if (dev->is_tape()) {
928 if (!dev->open(dcr, OPEN_READ_ONLY)) {
929 dir->fsend(_("3901 Unable to open device \"%s\": ERR=%s\n"),
930 dev->print_name(), dev->bstrerror());
934 if (dev->is_labeled()) {
935 dir->fsend(_("3001 Device \"%s\" is already mounted with Volume \"%s\"\n"),
936 dev->print_name(), dev->VolHdr.VolumeName);
938 dir->fsend(_("3905 Device \"%s\" open but no Bacula volume is mounted.\n"
939 "If this is not a blank tape, try unmounting and remounting the Volume.\n"),
942 if (dev->is_open() && !dev->has_cap(CAP_ALWAYSOPEN)) {
945 } else if (dev->is_unmountable()) {
947 dir->fsend(_("3002 Device \"%s\" is mounted.\n"), dev->print_name());
949 dir->fsend(_("3907 %s"), dev->bstrerror());
951 } else { /* must be file */
952 dir->fsend(_("3906 File device \"%s\" is always mounted.\n"),
954 pthread_cond_broadcast(&dev->wait_next_vol);
955 Dmsg1(100, "JobId=%u broadcast wait_device_release\n", (uint32_t)dcr->jcr->JobId);
956 pthread_cond_broadcast(&wait_device_release);
961 dir->fsend(_("3930 Device \"%s\" is being released.\n"), dev->print_name());
965 dir->fsend(_("3905 Unknown wait state %d\n"), dev->blocked());
971 dir->fsend(_("3999 Device \"%s\" not found or could not be opened.\n"), devname.c_str());
974 pm_strcpy(jcr->errmsg, dir->msg);
975 dir->fsend(_("3909 Error scanning mount command: %s\n"), jcr->errmsg);
977 dir->signal(BNET_EOD);
982 * unmount command from Director
984 static bool unmount_cmd(JCR *jcr)
987 BSOCK *dir = jcr->dir_bsock;
992 if (sscanf(dir->msg, "unmount %127s drive=%d", devname.c_str(), &drive) == 2) {
993 dcr = find_device(jcr, devname, NULL, drive);
996 dev->Lock(); /* Use P to avoid indefinite block */
997 if (!dev->is_open()) {
998 if (!dev->is_busy()) {
999 unload_autochanger(dcr, -1);
1001 if (dev->is_unmountable()) {
1002 if (dev->unmount(0)) {
1003 dir->fsend(_("3002 Device \"%s\" unmounted.\n"),
1006 dir->fsend(_("3907 %s"), dev->bstrerror());
1009 Dmsg0(90, "Device already unmounted\n");
1010 dir->fsend(_("3901 Device \"%s\" is already unmounted.\n"),
1013 } else if (dev->blocked() == BST_WAITING_FOR_SYSOP) {
1014 Dmsg2(90, "%d waiter dev_block=%d. doing unmount\n", dev->num_waiting,
1016 if (!unload_autochanger(dcr, -1)) {
1018 * ***FIXME**** what is this ???? -- probably we had
1019 * the wrong volume so we must free it and try again. KES
1024 if (dev->is_unmountable() && !dev->unmount(0)) {
1025 dir->fsend(_("3907 %s"), dev->bstrerror());
1027 dev->set_blocked(BST_UNMOUNTED_WAITING_FOR_SYSOP);
1028 dir->fsend(_("3001 Device \"%s\" unmounted.\n"),
1032 } else if (dev->blocked() == BST_DOING_ACQUIRE) {
1033 dir->fsend(_("3902 Device \"%s\" is busy in acquire.\n"),
1036 } else if (dev->blocked() == BST_WRITING_LABEL) {
1037 dir->fsend(_("3903 Device \"%s\" is being labeled.\n"),
1040 } else if (dev->is_busy()) {
1041 send_dir_busy_message(dir, dev);
1042 } else { /* device not being used */
1043 Dmsg0(90, "Device not in use, unmounting\n");
1044 /* On FreeBSD, I am having ASSERT() failures in block_device()
1045 * and I can only imagine that the thread id that we are
1046 * leaving in no_wait_id is being re-used. So here,
1047 * we simply do it by hand. Gross, but a solution.
1049 /* block_device(dev, BST_UNMOUNTED); replace with 2 lines below */
1050 dev->set_blocked(BST_UNMOUNTED);
1051 clear_thread_id(dev->no_wait_id);
1052 if (!unload_autochanger(dcr, -1)) {
1056 if (dev->is_unmountable() && !dev->unmount(0)) {
1057 dir->fsend(_("3907 %s"), dev->bstrerror());
1059 dir->fsend(_("3002 Device \"%s\" unmounted.\n"),
1066 dir->fsend(_("3999 Device \"%s\" not found or could not be opened.\n"), devname.c_str());
1069 /* NB dir->msg gets clobbered in bnet_fsend, so save command */
1070 pm_strcpy(jcr->errmsg, dir->msg);
1071 dir->fsend(_("3907 Error scanning unmount command: %s\n"), jcr->errmsg);
1073 dir->signal(BNET_EOD);
1079 * The truncate command will recycle a volume. The director can call this
1080 * after purging a volume so that disk space will not be wasted. Only useful
1081 * for File Storage, of course.
1084 * It is currently disabled
1086 static bool action_on_purge_cmd(JCR *jcr)
1088 BSOCK *dir = jcr->dir_bsock;
1090 char devname[MAX_NAME_LENGTH];
1091 char volumename[MAX_NAME_LENGTH];
1094 /* TODO: Need to find a free device and ask for slot to the director */
1095 if (sscanf(dir->msg,
1096 "action_on_purge %127s vol=%127s action=%d",
1097 devname, volumename, &action)!= 5)
1099 dir->fsend(_("3916 Error scanning action_on_purge command\n"));
1102 unbash_spaces(volumename);
1103 unbash_spaces(devname);
1105 /* Check if action is correct */
1106 if (action & AOP_TRUNCTATE) {
1112 dir->signal(BNET_EOD);
1118 * Release command from Director. This rewinds the device and if
1119 * configured does a offline and ensures that Bacula will
1120 * re-read the label of the tape before continuing. This gives
1121 * the operator the chance to change the tape anytime before the
1124 static bool release_cmd(JCR *jcr)
1127 BSOCK *dir = jcr->dir_bsock;
1132 if (sscanf(dir->msg, "release %127s drive=%d", devname.c_str(), &drive) == 2) {
1133 dcr = find_device(jcr, devname, NULL, drive);
1136 dev->Lock(); /* Use P to avoid indefinite block */
1137 if (!dev->is_open()) {
1138 if (!dev->is_busy()) {
1139 unload_autochanger(dcr, -1);
1141 Dmsg0(90, "Device already released\n");
1142 dir->fsend(_("3921 Device \"%s\" already released.\n"),
1145 } else if (dev->blocked() == BST_WAITING_FOR_SYSOP) {
1146 Dmsg2(90, "%d waiter dev_block=%d.\n", dev->num_waiting,
1148 unload_autochanger(dcr, -1);
1149 dir->fsend(_("3922 Device \"%s\" waiting for sysop.\n"),
1152 } else if (dev->blocked() == BST_UNMOUNTED_WAITING_FOR_SYSOP) {
1153 Dmsg2(90, "%d waiter dev_block=%d. doing unmount\n", dev->num_waiting,
1155 dir->fsend(_("3922 Device \"%s\" waiting for mount.\n"),
1158 } else if (dev->blocked() == BST_DOING_ACQUIRE) {
1159 dir->fsend(_("3923 Device \"%s\" is busy in acquire.\n"),
1162 } else if (dev->blocked() == BST_WRITING_LABEL) {
1163 dir->fsend(_("3914 Device \"%s\" is being labeled.\n"),
1166 } else if (dev->is_busy()) {
1167 send_dir_busy_message(dir, dev);
1168 } else { /* device not being used */
1169 Dmsg0(90, "Device not in use, releasing\n");
1170 dcr->release_volume();
1171 dir->fsend(_("3022 Device \"%s\" released.\n"),
1177 dir->fsend(_("3999 Device \"%s\" not found or could not be opened.\n"), devname.c_str());
1180 /* NB dir->msg gets clobbered in bnet_fsend, so save command */
1181 pm_strcpy(jcr->errmsg, dir->msg);
1182 dir->fsend(_("3927 Error scanning release command: %s\n"), jcr->errmsg);
1184 dir->signal(BNET_EOD);
1188 static pthread_mutex_t bsr_mutex = PTHREAD_MUTEX_INITIALIZER;
1189 static uint32_t bsr_uniq = 0;
1191 static bool get_bootstrap_file(JCR *jcr, BSOCK *sock)
1193 POOLMEM *fname = get_pool_memory(PM_FNAME);
1197 if (jcr->RestoreBootstrap) {
1198 unlink(jcr->RestoreBootstrap);
1199 free_pool_memory(jcr->RestoreBootstrap);
1203 Mmsg(fname, "%s/%s.%s.%d.bootstrap", me->working_directory, me->hdr.name,
1204 jcr->Job, bsr_uniq);
1206 Dmsg1(400, "bootstrap=%s\n", fname);
1207 jcr->RestoreBootstrap = fname;
1208 bs = fopen(fname, "a+b"); /* create file */
1211 Jmsg(jcr, M_FATAL, 0, _("Could not create bootstrap file %s: ERR=%s\n"),
1212 jcr->RestoreBootstrap, be.bstrerror());
1215 Dmsg0(150, "=== Bootstrap file ===\n");
1216 while (sock->recv() >= 0) {
1217 Dmsg1(150, "%s", sock->msg);
1218 fputs(sock->msg, bs);
1221 Dmsg0(150, "=== end bootstrap file ===\n");
1222 jcr->bsr = parse_bsr(jcr, jcr->RestoreBootstrap);
1224 Jmsg(jcr, M_FATAL, 0, _("Error parsing bootstrap file.\n"));
1227 if (chk_dbglvl(150)) {
1228 dump_bsr(jcr->bsr, true);
1230 /* If we got a bootstrap, we are reading, so create read volume list */
1231 create_restore_volume_list(jcr);
1235 unlink(jcr->RestoreBootstrap);
1236 free_pool_memory(jcr->RestoreBootstrap);
1237 jcr->RestoreBootstrap = NULL;
1239 sock->fsend(ERROR_bootstrap);
1242 return sock->fsend(OK_bootstrap);
1245 static bool bootstrap_cmd(JCR *jcr)
1247 return get_bootstrap_file(jcr, jcr->dir_bsock);
1251 * Autochanger command from Director
1253 static bool changer_cmd(JCR *jcr)
1256 BSOCK *dir = jcr->dir_bsock;
1259 const char *cmd = NULL;
1262 * A safe_cmd may call autochanger script but does not load/unload
1263 * slots so it can be done at the same time that the drive is open.
1265 bool safe_cmd = false;
1267 if (sscanf(dir->msg, "autochanger listall %127s", devname.c_str()) == 1) {
1269 safe_cmd = ok = true;
1270 } else if (sscanf(dir->msg, "autochanger list %127s", devname.c_str()) == 1) {
1272 safe_cmd = ok = true;
1273 } else if (sscanf(dir->msg, "autochanger slots %127s", devname.c_str()) == 1) {
1275 safe_cmd = ok = true;
1276 } else if (sscanf(dir->msg, "autochanger drives %127s", devname.c_str()) == 1) {
1278 safe_cmd = ok = true;
1281 dcr = find_device(jcr, devname, NULL, -1);
1284 dev->Lock(); /* Use P to avoid indefinite block */
1285 if (!dev->device->changer_res) {
1286 dir->fsend(_("3998 Device \"%s\" is not an autochanger.\n"),
1288 /* Under certain "safe" conditions, we can steal the lock */
1289 } else if (safe_cmd || !dev->is_open() || dev->can_steal_lock()) {
1290 autochanger_cmd(dcr, dir, cmd);
1291 } else if (dev->is_busy() || dev->is_blocked()) {
1292 send_dir_busy_message(dir, dev);
1293 } else { /* device not being used */
1294 autochanger_cmd(dcr, dir, cmd);
1299 dir->fsend(_("3999 Device \"%s\" not found or could not be opened.\n"), devname.c_str());
1301 } else { /* error on scanf */
1302 pm_strcpy(jcr->errmsg, dir->msg);
1303 dir->fsend(_("3908 Error scanning autochanger drives/list/slots command: %s\n"),
1306 dir->signal(BNET_EOD);
1311 * Read and return the Volume label
1313 static bool readlabel_cmd(JCR *jcr)
1316 BSOCK *dir = jcr->dir_bsock;
1319 int32_t Slot, drive;
1321 if (sscanf(dir->msg, "readlabel %127s Slot=%d drive=%d", devname.c_str(),
1322 &Slot, &drive) == 3) {
1323 dcr = find_device(jcr, devname, NULL, drive);
1326 dev->Lock(); /* Use P to avoid indefinite block */
1327 if (!dev->is_open()) {
1328 read_volume_label(jcr, dcr, dev, Slot);
1330 /* Under certain "safe" conditions, we can steal the lock */
1331 } else if (dev->can_steal_lock()) {
1332 read_volume_label(jcr, dcr, dev, Slot);
1333 } else if (dev->is_busy() || dev->is_blocked()) {
1334 send_dir_busy_message(dir, dev);
1335 } else { /* device not being used */
1336 read_volume_label(jcr, dcr, dev, Slot);
1341 dir->fsend(_("3999 Device \"%s\" not found or could not be opened.\n"), devname.c_str());
1344 pm_strcpy(jcr->errmsg, dir->msg);
1345 dir->fsend(_("3909 Error scanning readlabel command: %s\n"), jcr->errmsg);
1347 dir->signal(BNET_EOD);
1353 * Read the tape label
1355 * Enter with the mutex set
1357 static void read_volume_label(JCR *jcr, DCR *dcr, DEVICE *dev, int Slot)
1359 BSOCK *dir = jcr->dir_bsock;
1363 steal_device_lock(dev, &hold, BST_WRITING_LABEL);
1365 if (!try_autoload_device(jcr, dcr, Slot, "")) {
1366 goto bail_out; /* error */
1369 dev->clear_labeled(); /* force read of label */
1370 switch (read_dev_volume_label(dcr)) {
1372 /* DO NOT add quotes around the Volume name. It is scanned in the DIR */
1373 dir->fsend(_("3001 Volume=%s Slot=%d\n"), dev->VolHdr.VolumeName, Slot);
1374 Dmsg1(100, "Volume: %s\n", dev->VolHdr.VolumeName);
1377 dir->fsend(_("3902 Cannot mount Volume on Storage Device \"%s\" because:\n%s"),
1378 dev->print_name(), jcr->errmsg);
1383 give_back_device_lock(dev, &hold);
1387 static bool try_autoload_device(JCR *jcr, DCR *dcr, int slot, const char *VolName)
1389 BSOCK *dir = jcr->dir_bsock;
1391 bstrncpy(dcr->VolumeName, VolName, sizeof(dcr->VolumeName));
1392 dcr->VolCatInfo.Slot = slot;
1393 dcr->VolCatInfo.InChanger = slot > 0;
1394 if (autoload_device(dcr, 0, dir) < 0) { /* autoload if possible */
1400 static void send_dir_busy_message(BSOCK *dir, DEVICE *dev)
1402 if (dev->is_blocked()) {
1403 switch (dev->blocked()) {
1405 dir->fsend(_("3931 Device \"%s\" is BLOCKED. user unmounted.\n"),
1408 case BST_UNMOUNTED_WAITING_FOR_SYSOP:
1409 dir->fsend(_("3932 Device \"%s\" is BLOCKED. user unmounted during wait for media/mount.\n"),
1412 case BST_WAITING_FOR_SYSOP:
1413 dir->fsend(_("3933 Device \"%s\" is BLOCKED waiting for media.\n"),
1416 case BST_DOING_ACQUIRE:
1417 dir->fsend(_("3934 Device \"%s\" is being initialized.\n"),
1420 case BST_WRITING_LABEL:
1421 dir->fsend(_("3935 Device \"%s\" is blocked labeling a Volume.\n"),
1425 dir->fsend(_("3935 Device \"%s\" is blocked for unknown reason.\n"),
1429 } else if (dev->can_read()) {
1430 dir->fsend(_("3936 Device \"%s\" is busy reading.\n"),
1431 dev->print_name());;
1433 dir->fsend(_("3937 Device \"%s\" is busy with writers=%d reserved=%d.\n"),
1434 dev->print_name(), dev->num_writers, dev->num_reserved());