2 Bacula(R) - The Network Backup Solution
4 Copyright (C) 2000-2017 Kern Sibbald
6 The original author of Bacula is Kern Sibbald, with contributions
7 from many 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 This notice must be preserved when any source code is
15 conveyed and/or propagated.
17 Bacula(R) is a registered trademark of Kern Sibbald.
20 * This file handles accepting Director Commands
22 * Most Director commands are handled here, with the
23 * exception of the Job command command and subsequent
24 * subcommands that are handled
27 * N.B. in this file, in general we must use P(dev->mutex) rather
28 * than dev->rLock() so that we can examine the blocked
29 * state rather than blocking ourselves because a Job
30 * thread has the device blocked. In some "safe" cases,
31 * we can do things to a blocked device. CAREFUL!!!!
33 * File daemon commands are handled in fdcmd.c
35 * Written by Kern Sibbald, May MMI
42 /* Exported variables */
44 /* Imported variables */
45 extern BSOCK *filed_chan;
46 extern struct s_last_job last_job;
47 extern bool init_done;
49 /* Static variables */
50 static char derrmsg[] = "3900 Invalid command:";
51 static char OKsetdebug[] = "3000 OK setdebug=%ld trace=%ld options=%s tags=%s\n";
52 static char invalid_cmd[] = "3997 Invalid command for a Director with Monitor directive enabled.\n";
53 static char OK_bootstrap[] = "3000 OK bootstrap\n";
54 static char ERROR_bootstrap[] = "3904 Error bootstrap\n";
55 static char OKclient[] = "3000 OK client command\n";
57 /* Imported functions */
58 extern void terminate_child();
59 extern bool job_cmd(JCR *jcr);
60 extern bool use_cmd(JCR *jcr);
61 extern bool run_cmd(JCR *jcr);
62 extern bool status_cmd(JCR *sjcr);
63 extern bool qstatus_cmd(JCR *jcr);
64 //extern bool query_cmd(JCR *jcr);
66 /* Forward referenced functions */
67 static bool client_cmd(JCR *jcr);
68 static bool storage_cmd(JCR *jcr);
69 static bool label_cmd(JCR *jcr);
70 static bool die_cmd(JCR *jcr);
71 static bool relabel_cmd(JCR *jcr);
72 static bool truncate_cache_cmd(JCR *jcr);
73 static bool upload_cmd(JCR *jcr);
74 static bool readlabel_cmd(JCR *jcr);
75 static bool release_cmd(JCR *jcr);
76 static bool setdebug_cmd(JCR *jcr);
77 static bool cancel_cmd(JCR *cjcr);
78 static bool mount_cmd(JCR *jcr);
79 static bool unmount_cmd(JCR *jcr);
80 static bool enable_cmd(JCR *jcr);
81 static bool disable_cmd(JCR *jcr);
82 //static bool action_on_purge_cmd(JCR *jcr);
83 static bool bootstrap_cmd(JCR *jcr);
84 static bool cloud_list_cmd(JCR *jcr);
85 static bool cloud_prunecache_cmd(JCR *jcr);
86 static bool changer_cmd(JCR *sjcr);
87 static bool do_label(JCR *jcr, int relabel);
88 static DCR *find_device(JCR *jcr, POOL_MEM &dev_name,
89 POOLMEM *media_type, int drive);
90 static DCR *find_any_device(JCR *jcr, POOL_MEM &dev_name,
91 POOLMEM *media_type, int drive);
92 static void read_volume_label(JCR *jcr, DCR *dcr, DEVICE *dev, int Slot);
93 static void label_volume_if_ok(DCR *dcr, char *oldname,
94 char *newname, char *poolname,
95 int Slot, int relabel);
96 static bool try_autoload_device(JCR *jcr, DCR *dcr, int slot, const char *VolName);
97 static void send_dir_busy_message(BSOCK *dir, DEVICE *dev);
99 /* Responses send to Director for storage command */
100 static char BADcmd[] = "2902 Bad %s\n";
101 static char OKstore[] = "2000 OK storage\n";
103 /* Commands received from director that need scanning */
104 static char storaddr[] = "storage address=%s port=%d ssl=%d Job=%127s Authentication=%127s";
108 bool (*func)(JCR *jcr);
109 bool monitoraccess; /* set if monitors can access this cmd */
113 * The following are the recognized commands from the Director.
115 static struct s_cmds cmds[] = {
116 {"JobId=", job_cmd, 0}, /* start Job */
117 {"autochanger", changer_cmd, 0},
118 {"bootstrap", bootstrap_cmd, 0},
119 {"cancel", cancel_cmd, 0},
120 {"client", client_cmd, 0}, /* client address */
121 {".die", die_cmd, 0},
122 {"label", label_cmd, 0}, /* label a tape */
123 {"mount", mount_cmd, 0},
124 {"enable", enable_cmd, 0},
125 {"disable", disable_cmd, 0},
126 {"readlabel", readlabel_cmd, 0},
127 {"release", release_cmd, 0},
128 {"relabel", relabel_cmd, 0}, /* relabel a tape */
129 {"setdebug=", setdebug_cmd, 0}, /* set debug level */
130 {"status", status_cmd, 1},
131 {".status", qstatus_cmd, 1},
132 {"stop", cancel_cmd, 0},
133 {"storage", storage_cmd, 0}, /* get SD addr from Dir */
134 {"truncate", truncate_cache_cmd, 0},
135 {"upload", upload_cmd, 0},
136 {"prunecache", cloud_prunecache_cmd, 0},
137 {"cloudlist", cloud_list_cmd, 0}, /* List volumes/parts in the cloud */
138 {"unmount", unmount_cmd, 0},
139 {"use storage=", use_cmd, 0},
141 // {"query", query_cmd, 0},
142 {NULL, NULL} /* list terminator */
147 * Connection request. We accept connections either from the
148 * Director or a Client (File daemon).
150 * Note, we are running as a separate thread of the Storage daemon.
151 * and it is because a Director has made a connection with
152 * us on the "Message" channel.
154 * Basic tasks done here:
155 * - Create a JCR record
156 * - If it was from the FD, call handle_filed_connection()
157 * - Authenticate the Director
158 * - We wait for a command
159 * - We execute the command
160 * - We continue or exit depending on the return status
162 void *handle_connection_request(void *arg)
164 BSOCK *bs = (BSOCK *)arg;
171 if (bs->recv() <= 0) {
172 Jmsg1(NULL, M_ERROR, 0, _("Connection request from %s failed.\n"), bs->who());
173 bmicrosleep(5, 0); /* make user wait 5 seconds */
178 /* Check for client connection */
179 if (is_client_connection(bs)) {
180 handle_client_connection(bs);
185 * This is a connection from the Director, so setup a JCR
187 Dmsg1(050, "Got a DIR connection at %s\n", bstrftimes(tbuf, sizeof(tbuf),
188 (utime_t)time(NULL)));
189 jcr = new_jcr(sizeof(JCR), stored_free_jcr); /* create Job Control Record */
190 jcr->dir_bsock = bs; /* save Director bsock */
191 jcr->dir_bsock->set_jcr(jcr);
192 jcr->dcrs = New(alist(10, not_owned_by_alist));
193 create_jobmedia_queue(jcr);
194 /* Initialize FD start condition variable */
195 int errstat = pthread_cond_init(&jcr->job_start_wait, NULL);
198 Jmsg1(jcr, M_FATAL, 0, _("Unable to init job cond variable: ERR=%s\n"), be.bstrerror(errstat));
202 Dmsg0(1000, "stored in start_job\n");
205 * Validate then authenticate the Director
207 if (!validate_dir_hello(jcr)) {
210 if (!authenticate_director(jcr)) {
211 Jmsg(jcr, M_FATAL, 0, _("[SF0100] Unable to authenticate Director\n"));
214 Dmsg0(90, "Message channel init completed.\n");
216 dequeue_messages(jcr); /* dequeue any daemon messages */
218 for (quit=false; !quit;) {
220 if ((bnet_stat = bs->recv()) <= 0) {
221 break; /* connection terminated */
223 Dmsg1(199, "<dird: %s", bs->msg);
224 /* Ensure that device initialization is complete */
229 for (i=0; cmds[i].cmd; i++) {
230 if (strncmp(cmds[i].cmd, bs->msg, strlen(cmds[i].cmd)) == 0) {
231 if ((!cmds[i].monitoraccess) && (jcr->director->monitor)) {
232 Dmsg1(100, "Command \"%s\" is invalid.\n", cmds[i].cmd);
233 bs->fsend(invalid_cmd);
234 bs->signal(BNET_EOD);
237 Dmsg1(200, "Do command: %s\n", cmds[i].cmd);
238 if (!cmds[i].func(jcr)) { /* do command */
239 quit = true; /* error, get out */
240 Dmsg1(190, "Command %s requests quit\n", cmds[i].cmd);
242 found = true; /* indicate command found */
246 if (!found) { /* command not found */
248 Mmsg(err_msg, "%s %s\n", derrmsg, bs->msg);
249 bs->fsend(err_msg.c_str());
254 generate_daemon_event(jcr, "JobEnd");
255 generate_plugin_event(jcr, bsdEventJobEnd);
256 flush_jobmedia_queue(jcr);
257 dequeue_messages(jcr); /* send any queued messages */
258 bs->signal(BNET_TERMINATE);
259 free_plugins(jcr); /* release instantiated plugins */
266 * Force SD to die, and hopefully dump itself. Turned on only
267 * in development version.
269 static bool die_cmd(JCR *jcr)
274 BSOCK *dir = jcr->dir_bsock;
275 pthread_mutex_t m=PTHREAD_MUTEX_INITIALIZER;
277 if (strstr(dir->msg, "deadlock")) {
278 Pmsg0(000, "I have been requested to deadlock ...\n");
283 Pmsg1(000, "I have been requested to die ... (%s)\n", dir->msg);
284 a = djcr->JobId; /* ref NULL pointer */
291 * Get address of client from Director
292 * This initiates SD Calls Client.
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, _("[SF0101] 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, _("[SF0102] 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");
329 jcr->file_bsock = cl;
330 jcr->file_bsock->set_jcr(jcr);
331 if (!send_hello_client(jcr, jcr->Job)) {
335 /* Send OK to Director */
336 return dir->fsend(OKclient);
339 jcr->setJobStatus(JS_ErrorTerminated);
340 dir->fsend("3902 Bad %s cmd\n", "client");
345 * Get address of storage daemon from Director
347 static bool storage_cmd(JCR *jcr)
349 int stored_port; /* storage daemon port */
350 int enable_ssl; /* enable ssl to sd */
351 char sd_auth_key[200];
352 BSOCK *dir = jcr->dir_bsock;
353 BSOCK *sd = new_bsock(); /* storage daemon bsock */
354 char Job[MAX_NAME_LENGTH];
356 Dmsg1(050, "StorageCmd: %s", dir->msg);
357 if (sscanf(dir->msg, storaddr, &jcr->stored_addr, &stored_port,
358 &enable_ssl, Job, sd_auth_key) != 5) {
359 pm_strcpy(jcr->errmsg, dir->msg);
360 Jmsg(jcr, M_FATAL, 0, _("[SF0103] Bad storage command: %s"), jcr->errmsg);
361 Pmsg1(010, "Bad storage command: %s", jcr->errmsg);
366 if (jcr->sd_auth_key) {
367 bfree_and_null(jcr->sd_auth_key);
368 jcr->sd_auth_key = bstrdup(sd_auth_key);
370 if (stored_port != 0) {
371 Dmsg2(050, "sd_calls=%d sd_client=%d\n", jcr->sd_calls_client,
373 jcr->sd_calls_client = false; /* We are doing the connecting */
374 Dmsg3(050, "Connect to storage and wait: %s:%d ssl=%d\n", jcr->stored_addr, stored_port,
376 /* Open command communications with Storage daemon */
377 /* Try to connect for 1 hour at 10 second intervals */
378 if (!sd->connect(jcr, 10, (int)me->ClientConnectTimeout, me->heartbeat_interval,
379 _("Storage daemon"), jcr->stored_addr, NULL, stored_port, 1)) {
380 /* destroy() OK because sd is local */
382 Jmsg(jcr, M_FATAL, 0, _("[SF0104] Failed to connect to Storage daemon: %s:%d\n"),
383 jcr->stored_addr, stored_port);
384 Dmsg2(010, "Failed to connect to Storage daemon: %s:%d\n",
385 jcr->stored_addr, stored_port);
389 Dmsg0(050, "Connection OK to SD.\n");
391 jcr->store_bsock = sd;
392 } else { /* The storage daemon called us */
393 jcr->sd_calls_client = true;
394 /* We should already have a storage connection! */
395 if (jcr->file_bsock && jcr->store_bsock == NULL) {
396 jcr->store_bsock = jcr->file_bsock;
398 if (jcr->store_bsock == NULL) {
399 Jmsg0(jcr, M_FATAL, 0, _("[SF0105] In storage_cmd port==0, no prior Storage connection.\n"));
400 Pmsg0(010, "In storage_cmd port==0, no prior Storage connection.\n");
405 if (!send_hello_sd(jcr, Job)) {
409 if (!authenticate_storagedaemon(jcr)) {
413 * We are a client so we read from the socket we just
414 * opened as if we were a FD, so set file_bsock and
415 * clear the store_bsock.
417 jcr->file_bsock = jcr->store_bsock;
418 jcr->store_bsock = NULL;
419 jcr->authenticated = true; /* Dir authentication is sufficient */
420 Dmsg1(050, "=== Storage_cmd authenticated Job=%s with SD.\n", Job);
422 /* Send OK to Director */
423 return dir->fsend(OKstore);
426 Dmsg0(100, "Send storage command failed.\n");
427 dir->fsend(BADcmd, "storage");
433 * Set debug level as requested by the Director
436 static bool setdebug_cmd(JCR *jcr)
438 BSOCK *dir = jcr->dir_bsock;
439 int32_t trace_flag, lvl, hangup, blowup;
440 int64_t level, level_tags = 0;
443 *tags = *options = 0;
445 Dmsg1(10, "setdebug_cmd: %s", dir->msg);
447 if (sscanf(dir->msg, "setdebug=%ld trace=%ld hangup=%ld blowup=%ld options=%55s tags=%511s",
448 &lvl, &trace_flag, &hangup, &blowup, options, tags) != 6)
450 if (sscanf(dir->msg, "setdebug=%ld trace=%ld", &lvl, &trace_flag) != 2 || lvl < 0) {
451 dir->fsend(_("3991 Bad setdebug command: %s\n"), dir->msg);
456 set_trace(trace_flag);
459 set_debug_flags(options);
460 if (!debug_parse_tags(tags, &level_tags)) {
466 debug_level_tags = level_tags;
468 /* TODO: Temp code to activate the new BSR optimisation code */
469 for (char *p = options; *p ; p++) {
471 case 'i': /* Use new match_bsr() code */
472 use_new_match_all = 1;
475 use_new_match_all = 0;
481 return dir->fsend(OKsetdebug, lvl, trace_flag, options, tags);
487 * Be careful, we switch to using the job's JCR! So, using
488 * BSOCKs on that jcr can have two threads in the same code.
490 static bool cancel_cmd(JCR *cjcr)
492 BSOCK *dir = cjcr->dir_bsock;
494 char Job[MAX_NAME_LENGTH];
499 if (sscanf(dir->msg, "cancel Job=%127s", Job) == 1) {
500 status = JS_Canceled;
502 } else if (sscanf(dir->msg, "stop Job=%127s", Job) == 1) {
503 status = JS_Incomplete;
506 dir->fsend(_("3903 Error scanning cancel command.\n"));
509 if (!(jcr=get_jcr_by_full_name(Job))) {
510 dir->fsend(_("3904 Job %s not found.\n"), Job);
512 oldStatus = jcr->JobStatus;
513 jcr->setJobStatus(status);
514 Dmsg2(800, "Cancel JobId=%d %p\n", jcr->JobId, jcr);
515 if (!jcr->authenticated && oldStatus == JS_WaitFD) {
516 pthread_cond_signal(&jcr->job_start_wait); /* wake waiting thread */
518 if (jcr->file_bsock) {
519 jcr->file_bsock->set_terminated();
520 jcr->file_bsock->set_timed_out();
521 Dmsg2(800, "Term bsock jid=%d %p\n", jcr->JobId, jcr);
523 /* Still waiting for FD to connect, release it */
524 pthread_cond_signal(&jcr->job_start_wait); /* wake waiting job */
525 Dmsg2(800, "Signal FD connect jid=%d %p\n", jcr->JobId, jcr);
527 /* If thread waiting on mount, wake him */
528 if (jcr->dcr && jcr->dcr->dev && jcr->dcr->dev->waiting_for_mount()) {
529 pthread_cond_broadcast(&jcr->dcr->dev->wait_next_vol);
530 Dmsg1(100, "JobId=%u broadcast wait_device_release\n", (uint32_t)jcr->JobId);
531 pthread_cond_broadcast(&wait_device_release);
533 if (jcr->read_dcr && jcr->read_dcr->dev && jcr->read_dcr->dev->waiting_for_mount()) {
534 pthread_cond_broadcast(&jcr->read_dcr->dev->wait_next_vol);
535 Dmsg1(100, "JobId=%u broadcast wait_device_release\n", (uint32_t)jcr->JobId);
536 pthread_cond_broadcast(&wait_device_release);
538 jcr->my_thread_send_signal(TIMEOUT_SIGNAL);
539 dir->fsend(_("3000 JobId=%ld Job=\"%s\" marked to be %s.\n"),
540 jcr->JobId, jcr->Job, reason);
545 dir->signal(BNET_EOD);
553 static bool label_cmd(JCR *jcr)
555 return do_label(jcr, 0);
558 static bool relabel_cmd(JCR *jcr)
560 return do_label(jcr, 1);
563 static bool do_label(JCR *jcr, int relabel)
565 POOLMEM *newname, *oldname, *poolname, *mtype;
567 BSOCK *dir = jcr->dir_bsock;
573 newname = get_memory(dir->msglen+1);
574 oldname = get_memory(dir->msglen+1);
575 poolname = get_memory(dir->msglen+1);
576 mtype = get_memory(dir->msglen+1);
578 if (sscanf(dir->msg, "relabel %127s OldName=%127s NewName=%127s PoolName=%127s "
579 "MediaType=%127s Slot=%d drive=%d",
580 dev_name.c_str(), oldname, newname, poolname, mtype,
581 &slot, &drive) == 7) {
586 if (sscanf(dir->msg, "label %127s VolumeName=%127s PoolName=%127s "
587 "MediaType=%127s Slot=%d drive=%d",
588 dev_name.c_str(), newname, poolname, mtype, &slot, &drive) == 6) {
593 unbash_spaces(newname);
594 unbash_spaces(oldname);
595 unbash_spaces(poolname);
596 unbash_spaces(mtype);
597 dcr = find_device(jcr, dev_name, mtype, drive);
602 dev->Lock(); /* Use P to avoid indefinite block */
603 max_jobs = dev->max_concurrent_jobs;
604 dev->max_concurrent_jobs = 1;
605 bstrncpy(dcr->VolumeName, newname, sizeof(dcr->VolumeName));
606 if (dcr->can_i_write_volume()) {
607 if (reserve_volume(dcr, newname) == NULL) {
610 Dmsg1(400, "Reserved Volume=\"%s\" for relabel/truncate.\n", newname);
615 dir->fsend(_("3908 Error reserving Volume=\"%s\": %s"), newname, jcr->errmsg);
616 dev->max_concurrent_jobs = max_jobs;
621 /* some command use recv and don't accept catalog update.
622 * it's not the case here, so we force dir_update_volume_info catalog update */
623 dcr->force_update_volume_info = true;
625 if (!dev->is_open() && !dev->is_busy()) {
626 Dmsg1(400, "Can %slabel. Device is not open\n", relabel?"re":"");
627 label_volume_if_ok(dcr, oldname, newname, poolname, slot, relabel);
629 /* Under certain "safe" conditions, we can steal the lock */
630 } else if (dev->can_obtain_block()) {
631 Dmsg0(400, "Can relabel. can_obtain_block\n");
632 label_volume_if_ok(dcr, oldname, newname, poolname, slot, relabel);
633 } else if (dev->is_busy() || dev->is_blocked()) {
634 send_dir_busy_message(dir, dev);
635 } else { /* device not being used */
636 Dmsg0(400, "Can relabel. device not used\n");
637 label_volume_if_ok(dcr, oldname, newname, poolname, slot, relabel);
639 dev->max_concurrent_jobs = max_jobs;
643 if (chk_dbglvl(DT_VOLUME)) {
644 Dmsg0(0, "Waiting few seconds to force a bug...\n");
646 Dmsg0(0, "Doing free_volume()\n");
650 dir->fsend(_("3999 Device \"%s\" not found or could not be opened.\n"), dev_name.c_str());
653 /* NB dir->msg gets clobbered in bnet_fsend, so save command */
654 pm_strcpy(jcr->errmsg, dir->msg);
655 dir->fsend(_("3903 Error scanning label command: %s\n"), jcr->errmsg);
661 free_memory(oldname);
662 free_memory(newname);
663 free_memory(poolname);
665 dir->signal(BNET_EOD);
670 * Handles truncate cache commands
672 static bool truncate_cache_cmd(JCR *jcr)
674 POOLMEM *volname, *poolname, *mtype;
676 BSOCK *dir = jcr->dir_bsock;
685 volname = get_memory(dir->msglen+1);
686 poolname = get_memory(dir->msglen+1);
687 mtype = get_memory(dir->msglen+1);
688 if (sscanf(dir->msg, "truncate cache Storage=%127s Volume=%127s PoolName=%127s "
689 "MediaType=%127s Slot=%d drive=%d",
690 dev_name.c_str(), volname, poolname, mtype,
691 &slot, &drive) == 6) {
695 unbash_spaces(volname);
696 unbash_spaces(poolname);
697 unbash_spaces(mtype);
698 dcr = find_device(jcr, dev_name, mtype, drive);
703 dev->Lock(); /* Use P to avoid indefinite block */
704 max_jobs = dev->max_concurrent_jobs;
705 dev->max_concurrent_jobs = 1;
706 bstrncpy(dcr->VolumeName, volname, sizeof(dcr->VolumeName));
707 if (dcr->can_i_write_volume()) {
708 if (reserve_volume(dcr, volname) == NULL) {
711 Dmsg1(400, "Reserved volume \"%s\"\n", volname);
716 dir->fsend(_("3908 Error reserving Volume=\"%s\": %s"), volname, jcr->errmsg);
717 dev->max_concurrent_jobs = max_jobs;
721 if ((!dev->is_open() && !dev->is_busy()) || dev->can_obtain_block()) {
722 Dmsg0(400, "Call truncate_cache\n");
723 nbpart = dev->truncate_cache(dcr, volname, &size);
725 dir->fsend(_("3000 OK truncate cache for volume \"%s\" %d part(s) %sB\n"),
726 volname, nbpart, edit_uint64_with_suffix(size, ed1));
728 dir->fsend(_("3900 Truncate cache for volume \"%s\" failed. ERR=%s\n"), volname, dev->errmsg);
730 } else if (dev->is_busy() || dev->is_blocked()) {
731 send_dir_busy_message(dir, dev);
732 } else { /* device not being used */
733 Dmsg0(400, "Call truncate_cache\n");
734 nbpart = dev->truncate_cache(dcr, volname, &size);
736 dir->fsend(_("3000 OK truncate cache for volume \"%s\" %d part(s) %sB\n"), volname,
737 nbpart, edit_uint64_with_suffix(size, ed1));
739 dir->fsend(_("3900 Truncate cache for volume \"%s\" failed. ERR=%s\n"), volname, dev->errmsg);
742 dev->max_concurrent_jobs = max_jobs;
746 if (chk_dbglvl(DT_VOLUME)) {
747 Dmsg0(0, "Waiting few seconds to force a bug...\n");
749 Dmsg0(0, "Doing free_volume()\n");
753 dir->fsend(_("3999 Device \"%s\" not found or could not be opened.\n"), dev_name.c_str());
756 /* NB dir->msg gets clobbered in bnet_fsend, so save command */
757 pm_strcpy(jcr->errmsg, dir->msg);
758 dir->fsend(_("3911 Error scanning truncate command: %s\n"), jcr->errmsg);
764 free_memory(volname);
765 free_memory(poolname);
767 dir->signal(BNET_EOD);
771 static bool cloud_prunecache_cmd(JCR *jcr)
773 /* TODO: Implement a function to prune the cache of a cloud device */
774 jcr->dir_bsock->fsend(_("3900 Not yet implemented\n"));
778 /* List volumes in the cloud */
779 static bool cloud_list_cmd(JCR *jcr)
781 jcr->dir_bsock->fsend(_("3900 Not yet implemented\n"));
786 * Handles upload cache to Cloud command
788 static bool upload_cmd(JCR *jcr)
790 POOLMEM *volname, *poolname, *mtype, *err;
792 BSOCK *dir = jcr->dir_bsock;
798 volname = get_memory(dir->msglen+1);
799 poolname = get_memory(dir->msglen+1);
800 mtype = get_memory(dir->msglen+1);
801 err = get_pool_memory(PM_MESSAGE);
802 *volname = *poolname = *mtype = *err = 0;
804 if (sscanf(dir->msg, "upload Storage=%127s Volume=%127s PoolName=%127s "
805 "MediaType=%127s Slot=%d drive=%d",
806 dev_name.c_str(), volname, poolname, mtype,
807 &slot, &drive) == 6) {
811 unbash_spaces(volname);
812 unbash_spaces(poolname);
813 unbash_spaces(mtype);
814 dcr = find_device(jcr, dev_name, mtype, drive);
819 dev->Lock(); /* Use P to avoid indefinite block */
820 max_jobs = dev->max_concurrent_jobs;
821 dev->max_concurrent_jobs = 1;
822 bstrncpy(dcr->VolumeName, volname, sizeof(dcr->VolumeName));
823 if (dcr->can_i_write_volume()) {
824 if (reserve_volume(dcr, volname) == NULL) {
827 Dmsg1(400, "Reserved volume \"%s\"\n", volname);
832 dir->fsend(_("3908 Error reserving Volume=\"%s\": %s"), volname, jcr->errmsg);
833 dev->max_concurrent_jobs = max_jobs;
837 if ((!dev->is_open() && !dev->is_busy()) || dev->can_obtain_block()) {
838 Dmsg0(400, "Can upload, because device is not open.\n");
839 dev->setVolCatName(volname);
841 if (dev->open_device(dcr, OPEN_READ_WRITE)) {
842 ok = dev->upload_cache(dcr, volname, err);
845 dev->end_of_job(dcr);
847 } else if (dev->is_busy() || dev->is_blocked()) {
848 send_dir_busy_message(dir, dev);
849 } else { /* device not being used */
850 Dmsg0(400, "Can upload, because device not used\n");
851 dev->setVolCatName(volname);
853 if (dev->open_device(dcr, OPEN_READ_WRITE)) {
854 ok = dev->upload_cache(dcr, volname, err);
857 dev->end_of_job(dcr);
860 dev->max_concurrent_jobs = max_jobs;
864 if (chk_dbglvl(DT_VOLUME)) {
865 Dmsg0(0, "Waiting few seconds to force a bug...\n");
867 Dmsg0(0, "Doing free_volume()\n");
871 dir->fsend(_("3999 Device \"%s\" not found or could not be opened.\n"), dev_name.c_str());
874 /* NB dir->msg gets clobbered in bnet_fsend, so save command */
875 pm_strcpy(jcr->errmsg, dir->msg);
876 dir->fsend(_("3912 Error scanning upload command: ERR=%s\n"), jcr->errmsg);
880 dir->fsend(_("3000 OK upload.\n"));
882 dir->fsend(_("3999 Error with the upload: ERR=%s\n"), err);
887 free_pool_memory(err);
888 free_memory(volname);
889 free_memory(poolname);
891 dir->signal(BNET_EOD);
897 * Read the tape label and determine if we can safely
898 * label the tape (not a Bacula volume), then label it.
900 * Enter with the mutex set
902 static void label_volume_if_ok(DCR *dcr, char *oldname,
903 char *newname, char *poolname,
904 int slot, int relabel)
906 BSOCK *dir = dcr->jcr->dir_bsock;
908 DEVICE *dev = dcr->dev;
911 const char *volname = (relabel == 1) ? oldname : newname;
912 uint64_t volCatBytes;
914 if (!obtain_device_block(dev, &hold, BST_WRITING_LABEL)) {
915 send_dir_busy_message(dir, dev);
918 Dmsg1(100, "Stole device %s lock, writing label.\n", dev->print_name());
920 Dmsg0(90, "try_autoload_device - looking for volume_info\n");
921 if (!try_autoload_device(dcr->jcr, dcr, slot, volname)) {
922 goto bail_out; /* error */
927 dev->truncating = true; /* let open_device() know we will truncate it */
929 /* Set old volume name for open if relabeling */
930 dcr->setVolCatName(volname);
932 /* Ensure that the device is open -- autoload_device() closes it */
933 if (dev->is_tape()) {
934 mode = OPEN_READ_WRITE;
936 mode = CREATE_READ_WRITE;
938 if (!dev->open_device(dcr, mode)) {
939 dir->fsend(_("3929 Unable to open device \"%s\": ERR=%s\n"),
940 dev->print_name(), dev->bstrerror());
944 /* See what we have for a Volume */
945 label_status = dev->read_dev_volume_label(dcr);
947 /* Set new volume name */
948 dcr->setVolCatName(newname);
949 switch(label_status) {
951 case VOL_VERSION_ERROR:
952 case VOL_LABEL_ERROR:
956 "3920 Cannot label Volume because it is already labeled: \"%s\"\n"),
957 dev->VolHdr.VolumeName);
961 /* Relabel request. If oldname matches, continue */
962 if (strcmp(oldname, dev->VolHdr.VolumeName) != 0) {
963 dir->fsend(_("3921 Wrong volume mounted.\n"));
966 if (dev->label_type != B_BACULA_LABEL) {
967 dir->fsend(_("3922 Cannot relabel an ANSI/IBM labeled Volume.\n"));
970 /* Fall through wanted! */
973 if (!dev->write_volume_label(dcr, newname, poolname,
974 relabel, true /* write label now */)) {
975 dir->fsend(_("3912 Failed to label Volume %s: ERR=%s\n"),
976 newname, dcr->jcr->errmsg);
979 volCatBytes = dev->VolCatInfo.VolCatBytes;
981 * After writing label, create a new part
983 if (dev->is_cloud()) {
985 if (!dev->open_next_part(dcr)) {
986 dir->fsend(_("3913 Failed to open next part: ERR=%s\n"), dcr->jcr->errmsg);
990 bstrncpy(dcr->VolumeName, newname, sizeof(dcr->VolumeName));
991 /* The following 3000 OK label. string is scanned in ua_label.c */
993 if (dev->dev_type == B_FILE_DEV || dev->dev_type == B_ALIGNED_DEV ||
994 dev->dev_type == B_CLOUD_DEV)
996 type = dev->dev_type;
1000 dir->fsend("3000 OK label. VolBytes=%lld VolABytes=%lld VolType=%d Volume=\"%s\" Device=%s\n",
1001 volCatBytes, dev->VolCatInfo.VolCatAdataBytes,
1002 type, newname, dev->print_name());
1004 case VOL_TYPE_ERROR:
1005 dir->fsend(_("3917 Failed to label Volume: ERR=%s\n"), dcr->jcr->errmsg);
1008 dir->fsend(_("3918 Failed to label Volume (no media): ERR=%s\n"), dcr->jcr->errmsg);
1011 dir->fsend(_("3919 Cannot label Volume. "
1012 "Unknown status %d from read_volume_label()\n"), label_status);
1017 if (dev->is_open() && !dev->has_cap(CAP_ALWAYSOPEN)) {
1021 dev->end_of_job(dcr);
1023 if (!dev->is_open()) {
1024 dev->clear_volhdr();
1026 volume_unused(dcr); /* no longer using volume */
1027 give_back_device_block(dev, &hold);
1033 * Read the tape label
1035 * Enter with the mutex set
1037 static bool read_label(DCR *dcr)
1040 JCR *jcr = dcr->jcr;
1041 BSOCK *dir = jcr->dir_bsock;
1043 DEVICE *dev = dcr->dev;
1045 if (!obtain_device_block(dev, &hold, BST_DOING_ACQUIRE)) {
1046 send_dir_busy_message(dir, dev);
1050 dcr->VolumeName[0] = 0;
1051 dev->clear_labeled(); /* force read of label */
1052 switch (dev->read_dev_volume_label(dcr)) {
1054 dir->fsend(_("3001 Mounted Volume: %s\n"), dev->VolHdr.VolumeName);
1058 dir->fsend(_("3902 Cannot mount Volume on Storage Device \"%s\" because:\n%s"),
1059 dev->print_name(), jcr->errmsg);
1064 give_back_device_block(dev, &hold);
1069 * Searches for device by name, and if found, creates a dcr and
1072 static DCR *find_device(JCR *jcr, POOL_MEM &devname,
1073 POOLMEM *media_type, int drive)
1076 AUTOCHANGER *changer;
1080 unbash_spaces(devname);
1081 foreach_res(device, R_DEVICE) {
1082 /* Find resource, and make sure we were able to open it */
1083 if (strcmp(device->hdr.name, devname.c_str()) == 0 &&
1084 (!media_type || strcmp(device->media_type, media_type) ==0)) {
1086 device->dev = init_dev(jcr, device);
1089 Jmsg(jcr, M_WARNING, 0, _("\n"
1090 "[SW0106] Device \"%s\" requested by DIR could not be opened or does not exist.\n"),
1094 Dmsg1(20, "Found device %s\n", device->hdr.name);
1100 foreach_res(changer, R_AUTOCHANGER) {
1101 /* Find resource, and make sure we were able to open it */
1102 if (strcmp(devname.c_str(), changer->hdr.name) == 0) {
1103 /* Try each device in this AutoChanger */
1104 foreach_alist(device, changer->device) {
1105 Dmsg1(100, "Try changer device %s\n", device->hdr.name);
1107 device->dev = init_dev(jcr, device);
1110 Dmsg1(100, "Device %s could not be opened. Skipped\n", devname.c_str());
1111 Jmsg(jcr, M_WARNING, 0, _("\n"
1112 "[SW0107] Device \"%s\" in changer \"%s\" requested by DIR could not be opened or does not exist.\n"),
1113 device->hdr.name, devname.c_str());
1116 if (!device->dev->autoselect) {
1117 Dmsg1(100, "Device %s not autoselect skipped.\n", devname.c_str());
1118 continue; /* device is not available */
1119 } else if (!device->dev->enabled) {
1120 Dmsg1(100, "Device %s disabled skipped.\n", devname.c_str());
1121 continue; /* device disabled */
1123 if ((drive < 0 || drive == (int)device->dev->drive_index) &&
1124 (!media_type || strcmp(device->media_type, media_type) ==0)) {
1125 Dmsg1(20, "Found changer device %s\n", device->hdr.name);
1129 Dmsg3(100, "Device %s drive wrong: want=%d got=%d skipping\n",
1130 devname.c_str(), drive, (int)device->dev->drive_index);
1132 break; /* we found it but could not open a device */
1138 Dmsg1(100, "Found device %s\n", device->hdr.name);
1139 dcr = new_dcr(jcr, NULL, device->dev);
1140 dcr->device = device;
1146 * Find even disabled devices so that we can enable them
1147 * ***FIXME*** This could probably be merged with find_device with another
1148 * argument, but this is easier for the moment.
1150 static DCR *find_any_device(JCR *jcr, POOL_MEM &devname,
1151 POOLMEM *media_type, int drive)
1154 AUTOCHANGER *changer;
1158 unbash_spaces(devname);
1159 foreach_res(device, R_DEVICE) {
1160 /* Find resource, and make sure we were able to open it */
1161 if (strcmp(device->hdr.name, devname.c_str()) == 0 &&
1162 (!media_type || strcmp(device->media_type, media_type) ==0)) {
1164 device->dev = init_dev(jcr, device);
1167 Jmsg(jcr, M_WARNING, 0, _("\n"
1168 "[SW0108] Device \"%s\" requested by DIR could not be opened or does not exist.\n"),
1172 Dmsg1(20, "Found device %s\n", device->hdr.name);
1178 foreach_res(changer, R_AUTOCHANGER) {
1179 /* Find resource, and make sure we were able to open it */
1180 if (strcmp(devname.c_str(), changer->hdr.name) == 0) {
1181 /* Try each device in this AutoChanger */
1182 foreach_alist(device, changer->device) {
1183 Dmsg1(100, "Try changer device %s\n", device->hdr.name);
1185 device->dev = init_dev(jcr, device);
1188 Dmsg1(100, "Device %s could not be opened. Skipped\n", devname.c_str());
1189 Jmsg(jcr, M_WARNING, 0, _("\n"
1190 "[SW0109] Device \"%s\" in changer \"%s\" requested by DIR could not be opened or does not exist.\n"),
1191 device->hdr.name, devname.c_str());
1194 if ((drive < 0 || drive == (int)device->dev->drive_index) &&
1195 (!media_type || strcmp(device->media_type, media_type) ==0)) {
1196 Dmsg1(20, "Found changer device %s\n", device->hdr.name);
1200 Dmsg3(100, "Device %s drive wrong: want=%d got=%d skipping\n",
1201 devname.c_str(), drive, (int)device->dev->drive_index);
1203 break; /* we found it but could not open a device */
1209 Dmsg1(100, "Found device %s\n", device->hdr.name);
1210 dcr = new_dcr(jcr, NULL, device->dev);
1211 dcr->device = device;
1218 * Mount command from Director
1220 static bool mount_cmd(JCR *jcr)
1223 BSOCK *dir = jcr->dir_bsock;
1226 int32_t drive; /* device index */
1230 Dmsg1(100, "%s\n", dir->msg);
1231 ok = sscanf(dir->msg, "mount %127s drive=%d slot=%d", devname.c_str(),
1232 &drive, &slot) == 3;
1233 Dmsg3(100, "ok=%d device_index=%d slot=%d\n", ok, drive, slot);
1235 dcr = find_device(jcr, devname, NULL, drive);
1238 dev->Lock(); /* Use P to avoid indefinite block */
1239 Dmsg2(100, "mount cmd blocked=%d must_unload=%d\n", dev->blocked(),
1240 dev->must_unload());
1241 switch (dev->blocked()) { /* device blocked? */
1242 case BST_WAITING_FOR_SYSOP:
1243 /* Someone is waiting, wake him */
1244 Dmsg0(100, "Waiting for mount. Attempting to wake thread\n");
1245 dev->set_blocked(BST_MOUNT);
1246 dir->fsend("3001 OK mount requested. %sDevice=%s\n",
1247 slot>0?_("Specified slot ignored. "):"",
1249 Dmsg1(100, "JobId=%u broadcast wait_next_vol\n", (uint32_t)dcr->jcr->JobId);
1250 pthread_cond_broadcast(&dev->wait_next_vol);
1251 Dmsg1(100, "JobId=%u broadcast wait_device_release\n", (uint32_t)dcr->jcr->JobId);
1252 pthread_cond_broadcast(&wait_device_release);
1255 /* In both of these two cases, we (the user) unmounted the Volume */
1256 case BST_UNMOUNTED_WAITING_FOR_SYSOP:
1258 Dmsg2(100, "Unmounted changer=%d slot=%d\n", dev->is_autochanger(), slot);
1259 if (dev->is_autochanger() && slot > 0) {
1260 try_autoload_device(jcr, dcr, slot, "");
1262 /* We freed the device, so reopen it and wake any waiting threads */
1263 if (!dev->open_device(dcr, OPEN_READ_ONLY)) {
1264 dir->fsend(_("3901 Unable to open device \"%s\": ERR=%s\n"),
1265 dev->print_name(), dev->bstrerror());
1266 if (dev->blocked() == BST_UNMOUNTED) {
1267 /* We blocked the device, so unblock it */
1268 Dmsg0(100, "Unmounted. Unblocking device\n");
1269 unblock_device(dev);
1273 dev->read_dev_volume_label(dcr);
1274 if (dev->blocked() == BST_UNMOUNTED) {
1275 /* We blocked the device, so unblock it */
1276 Dmsg0(100, "Unmounted. Unblocking device\n");
1277 read_label(dcr); /* this should not be necessary */
1278 unblock_device(dev);
1280 Dmsg0(100, "Unmounted waiting for mount. Attempting to wake thread\n");
1281 dev->set_blocked(BST_MOUNT);
1283 if (dev->is_labeled()) {
1284 dir->fsend(_("3001 Device \"%s\" is mounted with Volume \"%s\"\n"),
1285 dev->print_name(), dev->VolHdr.VolumeName);
1287 dir->fsend(_("3905 Device \"%s\" open but no Bacula volume is mounted.\n"
1288 "If this is not a blank tape, try unmounting and remounting the Volume.\n"),
1291 pthread_cond_broadcast(&dev->wait_next_vol);
1292 Dmsg1(100, "JobId=%u broadcast wait_device_release\n", (uint32_t)dcr->jcr->JobId);
1293 pthread_cond_broadcast(&wait_device_release);
1296 case BST_DOING_ACQUIRE:
1297 dir->fsend(_("3001 Device \"%s\" is doing acquire.\n"),
1301 case BST_WRITING_LABEL:
1302 dir->fsend(_("3903 Device \"%s\" is being labeled.\n"),
1306 case BST_NOT_BLOCKED:
1307 Dmsg2(100, "Not blocked changer=%d slot=%d\n", dev->is_autochanger(), slot);
1308 if (dev->is_autochanger() && slot > 0) {
1309 try_autoload_device(jcr, dcr, slot, "");
1311 if (dev->is_open()) {
1312 if (dev->is_labeled()) {
1313 dir->fsend(_("3001 Device \"%s\" is mounted with Volume \"%s\"\n"),
1314 dev->print_name(), dev->VolHdr.VolumeName);
1316 dir->fsend(_("3905 Device \"%s\" open but no Bacula volume is mounted.\n"
1317 "If this is not a blank tape, try unmounting and remounting the Volume.\n"),
1320 } else if (dev->is_tape()) {
1321 if (!dev->open_device(dcr, OPEN_READ_ONLY)) {
1322 dir->fsend(_("3901 Unable to open device \"%s\": ERR=%s\n"),
1323 dev->print_name(), dev->bstrerror());
1327 if (dev->is_labeled()) {
1328 dir->fsend(_("3001 Device \"%s\" is already mounted with Volume \"%s\"\n"),
1329 dev->print_name(), dev->VolHdr.VolumeName);
1331 dir->fsend(_("3905 Device \"%s\" open but no Bacula volume is mounted.\n"
1332 "If this is not a blank tape, try unmounting and remounting the Volume.\n"),
1335 if (dev->is_open() && !dev->has_cap(CAP_ALWAYSOPEN)) {
1338 } else if (dev->is_unmountable()) {
1339 if (dev->mount(1)) {
1340 dir->fsend(_("3002 Device \"%s\" is mounted.\n"), dev->print_name());
1342 dir->fsend(_("3907 %s"), dev->bstrerror());
1344 } else { /* must be file */
1345 dir->fsend(_("3906 File device \"%s\" is always mounted.\n"),
1347 pthread_cond_broadcast(&dev->wait_next_vol);
1348 Dmsg1(100, "JobId=%u broadcast wait_device_release\n", (uint32_t)dcr->jcr->JobId);
1349 pthread_cond_broadcast(&wait_device_release);
1354 dir->fsend(_("3930 Device \"%s\" is being released.\n"), dev->print_name());
1358 dir->fsend(_("3905 Unknown wait state %d\n"), dev->blocked());
1364 dir->fsend(_("3999 Device \"%s\" not found or could not be opened.\n"), devname.c_str());
1367 pm_strcpy(jcr->errmsg, dir->msg);
1368 dir->fsend(_("3909 Error scanning mount command: %s\n"), jcr->errmsg);
1370 dir->signal(BNET_EOD);
1374 /* enable command from Director */
1375 static bool enable_cmd(JCR *jcr)
1378 BSOCK *dir = jcr->dir_bsock;
1385 ok = sscanf(dir->msg, "enable %127s drive=%d", devname.c_str(),
1387 Dmsg3(100, "ok=%d device=%s device_index=%d\n", ok, devname.c_str(), drive);
1389 dcr = find_any_device(jcr, devname, NULL, drive);
1392 dev->Lock(); /* Use P to avoid indefinite block */
1394 dir->fsend(_("3003 Device \"%s\" already enabled.\n"), dev->print_name());
1396 dev->enabled = true;
1397 dir->fsend(_("3002 Device \"%s\" enabled.\n"), dev->print_name());
1399 deleted = dev->delete_alerts();
1401 dir->fsend(_("3004 Device \"%s\" deleted %d alert%s.\n"),
1402 dev->print_name(), deleted, deleted>1?"s":"");
1408 /* NB dir->msg gets clobbered in bnet_fsend, so save command */
1409 pm_strcpy(jcr->errmsg, dir->msg);
1410 dir->fsend(_("3907 Error scanning \"enable\" command: %s\n"), jcr->errmsg);
1412 dir->signal(BNET_EOD);
1416 /* enable command from Director */
1417 static bool disable_cmd(JCR *jcr)
1420 BSOCK *dir = jcr->dir_bsock;
1426 ok = sscanf(dir->msg, "disable %127s drive=%d", devname.c_str(),
1428 Dmsg3(100, "ok=%d device=%s device_index=%d\n", ok, devname.c_str(), drive);
1430 dcr = find_device(jcr, devname, NULL, drive);
1434 dev->enabled = false;
1435 dir->fsend(_("3002 Device \"%s\" disabled.\n"), dev->print_name());
1440 /* NB dir->msg gets clobbered in bnet_fsend, so save command */
1441 pm_strcpy(jcr->errmsg, dir->msg);
1442 dir->fsend(_("3907 Error scanning \"disable\" command: %s\n"), jcr->errmsg);
1444 dir->signal(BNET_EOD);
1450 * unmount command from Director
1452 static bool unmount_cmd(JCR *jcr)
1455 BSOCK *dir = jcr->dir_bsock;
1460 if (sscanf(dir->msg, "unmount %127s drive=%d", devname.c_str(), &drive) == 2) {
1461 dcr = find_device(jcr, devname, NULL, drive);
1464 dev->Lock(); /* Use P to avoid indefinite block */
1465 if (!dev->is_open()) {
1466 if (!dev->is_busy()) {
1467 unload_autochanger(dcr, -1);
1469 if (dev->is_unmountable()) {
1470 if (dev->unmount(0)) {
1471 dir->fsend(_("3002 Device \"%s\" unmounted.\n"),
1474 dir->fsend(_("3907 %s"), dev->bstrerror());
1477 Dmsg0(90, "Device already unmounted\n");
1478 dir->fsend(_("3901 Device \"%s\" is already unmounted.\n"),
1481 } else if (dev->blocked() == BST_WAITING_FOR_SYSOP) {
1482 Dmsg2(90, "%d waiter dev_block=%d. doing unmount\n", dev->num_waiting,
1484 if (!unload_autochanger(dcr, -1)) {
1486 * ***FIXME**** what is this ???? -- probably we had
1487 * the wrong volume so we must free it and try again. KES
1492 if (dev->is_unmountable() && !dev->unmount(0)) {
1493 dir->fsend(_("3907 %s"), dev->bstrerror());
1495 dev->set_blocked(BST_UNMOUNTED_WAITING_FOR_SYSOP);
1496 dir->fsend(_("3001 Device \"%s\" unmounted.\n"),
1500 } else if (dev->blocked() == BST_DOING_ACQUIRE) {
1501 dir->fsend(_("3902 Device \"%s\" is busy in acquire.\n"),
1504 } else if (dev->blocked() == BST_WRITING_LABEL) {
1505 dir->fsend(_("3903 Device \"%s\" is being labeled.\n"),
1508 } else if (dev->is_busy()) {
1509 send_dir_busy_message(dir, dev);
1510 } else { /* device not being used */
1511 Dmsg0(90, "Device not in use, unmounting\n");
1512 /* On FreeBSD, I am having ASSERT() failures in block_device()
1513 * and I can only imagine that the thread id that we are
1514 * leaving in no_wait_id is being re-used. So here,
1515 * we simply do it by hand. Gross, but a solution.
1517 /* block_device(dev, BST_UNMOUNTED); replace with 2 lines below */
1518 dev->set_blocked(BST_UNMOUNTED);
1519 clear_thread_id(dev->no_wait_id);
1520 if (!unload_autochanger(dcr, -1)) {
1524 if (dev->is_unmountable() && !dev->unmount(0)) {
1525 dir->fsend(_("3907 %s"), dev->bstrerror());
1527 dir->fsend(_("3002 Device \"%s\" unmounted.\n"),
1534 dir->fsend(_("3999 Device \"%s\" not found or could not be opened.\n"), devname.c_str());
1537 /* NB dir->msg gets clobbered in bnet_fsend, so save command */
1538 pm_strcpy(jcr->errmsg, dir->msg);
1539 dir->fsend(_("3907 Error scanning unmount command: %s\n"), jcr->errmsg);
1541 dir->signal(BNET_EOD);
1547 * The truncate command will recycle a volume. The director can call this
1548 * after purging a volume so that disk space will not be wasted. Only useful
1549 * for File Storage, of course.
1552 * It is currently disabled
1554 static bool action_on_purge_cmd(JCR *jcr)
1556 BSOCK *dir = jcr->dir_bsock;
1558 char devname[MAX_NAME_LENGTH];
1559 char volumename[MAX_NAME_LENGTH];
1562 /* TODO: Need to find a free device and ask for slot to the director */
1563 if (sscanf(dir->msg,
1564 "action_on_purge %127s vol=%127s action=%d",
1565 devname, volumename, &action)!= 5)
1567 dir->fsend(_("3916 Error scanning action_on_purge command\n"));
1570 unbash_spaces(volumename);
1571 unbash_spaces(devname);
1573 /* Check if action is correct */
1574 if (action & AOP_TRUNCTATE) {
1580 dir->signal(BNET_EOD);
1586 * Release command from Director. This rewinds the device and if
1587 * configured does a offline and ensures that Bacula will
1588 * re-read the label of the tape before continuing. This gives
1589 * the operator the chance to change the tape anytime before the
1592 static bool release_cmd(JCR *jcr)
1595 BSOCK *dir = jcr->dir_bsock;
1600 if (sscanf(dir->msg, "release %127s drive=%d", devname.c_str(), &drive) == 2) {
1601 dcr = find_device(jcr, devname, NULL, drive);
1604 dev->Lock(); /* Use P to avoid indefinite block */
1605 if (!dev->is_open()) {
1606 if (!dev->is_busy()) {
1607 unload_autochanger(dcr, -1);
1609 Dmsg0(90, "Device already released\n");
1610 dir->fsend(_("3921 Device \"%s\" already released.\n"),
1613 } else if (dev->blocked() == BST_WAITING_FOR_SYSOP) {
1614 Dmsg2(90, "%d waiter dev_block=%d.\n", dev->num_waiting,
1616 unload_autochanger(dcr, -1);
1617 dir->fsend(_("3922 Device \"%s\" waiting for sysop.\n"),
1620 } else if (dev->blocked() == BST_UNMOUNTED_WAITING_FOR_SYSOP) {
1621 Dmsg2(90, "%d waiter dev_block=%d. doing unmount\n", dev->num_waiting,
1623 dir->fsend(_("3922 Device \"%s\" waiting for mount.\n"),
1626 } else if (dev->blocked() == BST_DOING_ACQUIRE) {
1627 dir->fsend(_("3923 Device \"%s\" is busy in acquire.\n"),
1630 } else if (dev->blocked() == BST_WRITING_LABEL) {
1631 dir->fsend(_("3914 Device \"%s\" is being labeled.\n"),
1634 } else if (dev->is_busy()) {
1635 send_dir_busy_message(dir, dev);
1636 } else { /* device not being used */
1637 Dmsg0(90, "Device not in use, releasing\n");
1638 dcr->release_volume();
1639 dir->fsend(_("3022 Device \"%s\" released.\n"),
1645 dir->fsend(_("3999 Device \"%s\" not found or could not be opened.\n"), devname.c_str());
1648 /* NB dir->msg gets clobbered in bnet_fsend, so save command */
1649 pm_strcpy(jcr->errmsg, dir->msg);
1650 dir->fsend(_("3927 Error scanning release command: %s\n"), jcr->errmsg);
1652 dir->signal(BNET_EOD);
1656 static pthread_mutex_t bsr_mutex = PTHREAD_MUTEX_INITIALIZER;
1657 static uint32_t bsr_uniq = 0;
1659 static bool get_bootstrap_file(JCR *jcr, BSOCK *sock)
1661 POOLMEM *fname = get_pool_memory(PM_FNAME);
1665 if (jcr->RestoreBootstrap) {
1666 unlink(jcr->RestoreBootstrap);
1667 free_pool_memory(jcr->RestoreBootstrap);
1671 Mmsg(fname, "%s/%s.%s.%d.bootstrap", me->working_directory, me->hdr.name,
1672 jcr->Job, bsr_uniq);
1674 Dmsg1(400, "bootstrap=%s\n", fname);
1675 jcr->RestoreBootstrap = fname;
1676 bs = bfopen(fname, "a+b"); /* create file */
1679 Jmsg(jcr, M_FATAL, 0, _("[SF0110] Could not create bootstrap file %s: ERR=%s\n"),
1680 jcr->RestoreBootstrap, be.bstrerror());
1683 Dmsg0(150, "=== Bootstrap file ===\n");
1684 while (sock->recv() >= 0) {
1685 Dmsg1(150, "%s", sock->msg);
1686 fputs(sock->msg, bs);
1689 Dmsg0(150, "=== end bootstrap file ===\n");
1690 jcr->bsr = parse_bsr(jcr, jcr->RestoreBootstrap);
1692 Jmsg(jcr, M_FATAL, 0, _("[SF0111] Error parsing bootstrap file.\n"));
1695 if (chk_dbglvl(150)) {
1696 dump_bsr(NULL, jcr->bsr, true);
1699 /* If we got a bootstrap, we are reading, so create read volume list */
1700 create_restore_volume_list(jcr, true /* store the volumes in the global vol_read list */);
1704 unlink(jcr->RestoreBootstrap);
1705 free_pool_memory(jcr->RestoreBootstrap);
1706 jcr->RestoreBootstrap = NULL;
1708 sock->fsend(ERROR_bootstrap);
1711 return sock->fsend(OK_bootstrap);
1714 static bool bootstrap_cmd(JCR *jcr)
1716 return get_bootstrap_file(jcr, jcr->dir_bsock);
1720 * Autochanger command from Director
1722 static bool changer_cmd(JCR *jcr)
1725 BSOCK *dir = jcr->dir_bsock;
1728 const char *cmd = NULL;
1731 * A safe_cmd may call autochanger script but does not load/unload
1732 * slots so it can be done at the same time that the drive is open.
1734 bool safe_cmd = false;
1736 if (sscanf(dir->msg, "autochanger listall %127s", devname.c_str()) == 1) {
1738 safe_cmd = ok = true;
1739 } else if (sscanf(dir->msg, "autochanger list %127s", devname.c_str()) == 1) {
1741 safe_cmd = ok = true;
1742 } else if (sscanf(dir->msg, "autochanger slots %127s", devname.c_str()) == 1) {
1744 safe_cmd = ok = true;
1745 } else if (sscanf(dir->msg, "autochanger drives %127s", devname.c_str()) == 1) {
1747 safe_cmd = ok = true;
1750 dcr = find_device(jcr, devname, NULL, -1);
1753 dev->Lock(); /* Use P to avoid indefinite block */
1754 if (!dev->device->changer_res) {
1755 dir->fsend(_("3998 Device \"%s\" is not an autochanger.\n"),
1757 /* Under certain "safe" conditions, we can steal the lock */
1758 } else if (safe_cmd || !dev->is_open() || dev->can_obtain_block()) {
1759 autochanger_cmd(dcr, dir, cmd);
1760 } else if (dev->is_busy() || dev->is_blocked()) {
1761 send_dir_busy_message(dir, dev);
1762 } else { /* device not being used */
1763 autochanger_cmd(dcr, dir, cmd);
1768 dir->fsend(_("3999 Device \"%s\" not found or could not be opened.\n"), devname.c_str());
1770 } else { /* error on scanf */
1771 pm_strcpy(jcr->errmsg, dir->msg);
1772 dir->fsend(_("3909 Error scanning autochanger drives/list/slots command: %s\n"),
1775 dir->signal(BNET_EOD);
1780 * Read and return the Volume label
1782 static bool readlabel_cmd(JCR *jcr)
1785 BSOCK *dir = jcr->dir_bsock;
1788 int32_t Slot, drive;
1790 if (sscanf(dir->msg, "readlabel %127s Slot=%d drive=%d", devname.c_str(),
1791 &Slot, &drive) == 3) {
1792 dcr = find_device(jcr, devname, NULL, drive);
1795 dev->Lock(); /* Use P to avoid indefinite block */
1796 if (!dev->is_open()) {
1797 read_volume_label(jcr, dcr, dev, Slot);
1799 /* Under certain "safe" conditions, we can steal the lock */
1800 } else if (dev->can_obtain_block()) {
1801 read_volume_label(jcr, dcr, dev, Slot);
1802 } else if (dev->is_busy() || dev->is_blocked()) {
1803 send_dir_busy_message(dir, dev);
1804 } else { /* device not being used */
1805 read_volume_label(jcr, dcr, dev, Slot);
1810 dir->fsend(_("3999 Device \"%s\" not found or could not be opened.\n"), devname.c_str());
1813 pm_strcpy(jcr->errmsg, dir->msg);
1814 dir->fsend(_("3909 Error scanning readlabel command: %s\n"), jcr->errmsg);
1816 dir->signal(BNET_EOD);
1822 * Read the tape label
1824 * Enter with the mutex set
1826 static void read_volume_label(JCR *jcr, DCR *dcr, DEVICE *dev, int Slot)
1828 BSOCK *dir = jcr->dir_bsock;
1832 if (!obtain_device_block(dev, &hold, BST_WRITING_LABEL)) {
1833 send_dir_busy_message(dir, dev);
1837 if (!try_autoload_device(jcr, dcr, Slot, "")) {
1838 goto bail_out; /* error */
1841 dev->clear_labeled(); /* force read of label */
1842 switch (dev->read_dev_volume_label(dcr)) {
1844 /* DO NOT add quotes around the Volume name. It is scanned in the DIR */
1845 dir->fsend(_("3001 Volume=%s Slot=%d\n"), dev->VolHdr.VolumeName, Slot);
1846 Dmsg1(100, "Volume: %s\n", dev->VolHdr.VolumeName);
1849 dir->fsend(_("3902 Cannot mount Volume on Storage Device \"%s\" because:\n%s"),
1850 dev->print_name(), jcr->errmsg);
1855 give_back_device_block(dev, &hold);
1859 static bool try_autoload_device(JCR *jcr, DCR *dcr, int slot, const char *VolName)
1861 BSOCK *dir = jcr->dir_bsock;
1863 bstrncpy(dcr->VolumeName, VolName, sizeof(dcr->VolumeName));
1864 dcr->VolCatInfo.Slot = slot;
1865 dcr->VolCatInfo.InChanger = slot > 0;
1866 if (autoload_device(dcr, 0, dir) < 0) { /* autoload if possible */
1872 static void send_dir_busy_message(BSOCK *dir, DEVICE *dev)
1874 if (dev->is_blocked()) {
1875 switch (dev->blocked()) {
1877 dir->fsend(_("3931 Device \"%s\" is BLOCKED. user unmounted.\n"),
1880 case BST_UNMOUNTED_WAITING_FOR_SYSOP:
1881 dir->fsend(_("3932 Device \"%s\" is BLOCKED. user unmounted during wait for media/mount.\n"),
1884 case BST_WAITING_FOR_SYSOP:
1885 dir->fsend(_("3933 Device \"%s\" is BLOCKED waiting for media.\n"),
1888 case BST_DOING_ACQUIRE:
1889 dir->fsend(_("3934 Device \"%s\" is being initialized.\n"),
1892 case BST_WRITING_LABEL:
1893 dir->fsend(_("3935 Device \"%s\" is blocked labeling a Volume.\n"),
1897 dir->fsend(_("3935 Device \"%s\" is blocked for unknown reason.\n"),
1901 } else if (dev->can_read()) {
1902 dir->fsend(_("3936 Device \"%s\" is busy reading.\n"),
1903 dev->print_name());;
1905 dir->fsend(_("3937 Device \"%s\" is busy with writers=%d reserved=%d.\n"),
1906 dev->print_name(), dev->num_writers, dev->num_reserved());