]> git.sur5r.net Git - bacula/bacula/blob - bacula/src/stored/fd_cmds.c
Modify insertion of read Volumes in SD to be done before the
[bacula/bacula] / bacula / src / stored / fd_cmds.c
1 /*
2    Bacula® - The Network Backup Solution
3
4    Copyright (C) 2000-2009 Free Software Foundation Europe e.V.
5
6    The main author of Bacula is Kern Sibbald, with contributions from
7    many others, a complete list can be found in the file AUTHORS.
8    This program is Free Software; you can redistribute it and/or
9    modify it under the terms of version two of the GNU General Public
10    License as published by the Free Software Foundation and included
11    in the file LICENSE.
12
13    This program is distributed in the hope that it will be useful, but
14    WITHOUT ANY WARRANTY; without even the implied warranty of
15    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
16    General Public License for more details.
17
18    You should have received a copy of the GNU General Public License
19    along with this program; if not, write to the Free Software
20    Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
21    02110-1301, USA.
22
23    Bacula® is a registered trademark of Kern Sibbald.
24    The licensor of Bacula is the Free Software Foundation Europe
25    (FSFE), Fiduciary Program, Sumatrastrasse 25, 8006 Zürich,
26    Switzerland, email:ftf@fsfeurope.org.
27 */
28 /*
29  * This file handles commands from the File daemon.
30  *
31  *  Kern Sibbald, MM
32  *
33  * We get here because the Director has initiated a Job with
34  *  the Storage daemon, then done the same with the File daemon,
35  *  then when the Storage daemon receives a proper connection from
36  *  the File daemon, control is passed here to handle the
37  *  subsequent File daemon commands.
38  *
39  *   Version $Id$
40  *
41  */
42
43 #include "bacula.h"
44 #include "stored.h"
45
46 /* Imported variables */
47 extern STORES *me;
48
49 /* Static variables */
50 static char ferrmsg[]      = "3900 Invalid command\n";
51
52 /* Imported functions */
53 extern bool do_append_data(JCR *jcr);
54 extern bool do_read_data(JCR *jcr);
55
56 /* Forward referenced FD commands */
57 static bool append_open_session(JCR *jcr);
58 static bool append_close_session(JCR *jcr);
59 static bool append_data_cmd(JCR *jcr);
60 static bool append_end_session(JCR *jcr);
61 static bool read_open_session(JCR *jcr);
62 static bool read_data_cmd(JCR *jcr);
63 static bool read_close_session(JCR *jcr);
64 static bool bootstrap_cmd(JCR *jcr);
65
66 /* Exported function */
67 bool get_bootstrap_file(JCR *jcr, BSOCK *bs);
68
69 struct s_cmds {
70    const char *cmd;
71    bool (*func)(JCR *jcr);
72 };
73
74 /*
75  * The following are the recognized commands from the File daemon
76  */
77 static struct s_cmds fd_cmds[] = {
78    {"append open",  append_open_session},
79    {"append data",  append_data_cmd},
80    {"append end",   append_end_session},
81    {"append close", append_close_session},
82    {"read open",    read_open_session},
83    {"read data",    read_data_cmd},
84    {"read close",   read_close_session},
85    {"bootstrap",    bootstrap_cmd},
86    {NULL,           NULL}                  /* list terminator */
87 };
88
89 /* Commands from the File daemon that require additional scanning */
90 static char read_open[]       = "read open session = %127s %ld %ld %ld %ld %ld %ld\n";
91
92 /* Responses sent to the File daemon */
93 static char NO_open[]         = "3901 Error session already open\n";
94 static char NOT_opened[]      = "3902 Error session not opened\n";
95 static char OK_end[]          = "3000 OK end\n";
96 static char OK_close[]        = "3000 OK close Status = %d\n";
97 static char OK_open[]         = "3000 OK open ticket = %d\n";
98 static char ERROR_append[]    = "3903 Error append data\n";
99 static char OK_bootstrap[]    = "3000 OK bootstrap\n";
100 static char ERROR_bootstrap[] = "3904 Error bootstrap\n";
101
102 /* Information sent to the Director */
103 static char Job_start[] = "3010 Job %s start\n";
104 char Job_end[]   =
105    "3099 Job %s end JobStatus=%d JobFiles=%d JobBytes=%s JobErrors=%u\n";
106
107 /*
108  * Run a File daemon Job -- File daemon already authorized
109  *  Director sends us this command.
110  *
111  * Basic task here is:
112  * - Read a command from the File daemon
113  * - Execute it
114  *
115  */
116 void run_job(JCR *jcr)
117 {
118    BSOCK *dir = jcr->dir_bsock;
119    char ec1[30];
120
121    dir->set_jcr(jcr);
122    Dmsg1(120, "Start run Job=%s\n", jcr->Job);
123    dir->fsend(Job_start, jcr->Job);
124    jcr->start_time = time(NULL);
125    jcr->run_time = jcr->start_time;
126    set_jcr_job_status(jcr, JS_Running);
127    dir_send_job_status(jcr);          /* update director */
128    do_fd_commands(jcr);
129    jcr->end_time = time(NULL);
130    dequeue_messages(jcr);             /* send any queued messages */
131    set_jcr_job_status(jcr, JS_Terminated);
132    generate_daemon_event(jcr, "JobEnd");
133    dir->fsend(Job_end, jcr->Job, jcr->JobStatus, jcr->JobFiles,
134       edit_uint64(jcr->JobBytes, ec1), jcr->JobErrors);
135    dir->signal(BNET_EOD);             /* send EOD to Director daemon */
136    return;
137 }
138
139 /*
140  * Now talk to the FD and do what he says
141  */
142 void do_fd_commands(JCR *jcr)
143 {
144    int i;
145    bool found, quit;
146    BSOCK *fd = jcr->file_bsock;
147
148    fd->set_jcr(jcr);
149    for (quit=false; !quit;) {
150       int stat;
151
152       /* Read command coming from the File daemon */
153       stat = fd->recv();
154       if (is_bnet_stop(fd)) {         /* hardeof or error */
155          break;                       /* connection terminated */
156       }
157       if (stat <= 0) {
158          continue;                    /* ignore signals and zero length msgs */
159       }
160       Dmsg1(110, "<filed: %s", fd->msg);
161       found = false;
162       for (i=0; fd_cmds[i].cmd; i++) {
163          if (strncmp(fd_cmds[i].cmd, fd->msg, strlen(fd_cmds[i].cmd)) == 0) {
164             found = true;               /* indicate command found */
165             jcr->errmsg[0] = 0;
166             if (!fd_cmds[i].func(jcr) || job_canceled(jcr)) {    /* do command */
167                /* Note fd->msg command may be destroyed by comm activity */
168                if (jcr->errmsg[0]) {
169                   Jmsg1(jcr, M_FATAL, 0, _("Command error with FD, hanging up. %s\n"),
170                         jcr->errmsg);
171                } else {
172                   Jmsg0(jcr, M_FATAL, 0, _("Command error with FD, hanging up.\n"));
173                }
174                set_jcr_job_status(jcr, JS_ErrorTerminated);
175                quit = true;
176             }
177             break;
178          }
179       }
180       if (!found) {                   /* command not found */
181          Jmsg1(jcr, M_FATAL, 0, _("FD command not found: %s\n"), fd->msg);
182          Dmsg1(110, "<filed: Command not found: %s\n", fd->msg);
183          fd->fsend(ferrmsg);
184          break;
185       }
186    }
187    fd->signal(BNET_TERMINATE);        /* signal to FD job is done */
188 }
189
190 /*
191  *   Append Data command
192  *     Open Data Channel and receive Data for archiving
193  *     Write the Data to the archive device
194  */
195 static bool append_data_cmd(JCR *jcr)
196 {
197    BSOCK *fd = jcr->file_bsock;
198
199    Dmsg1(120, "Append data: %s", fd->msg);
200    if (jcr->session_opened) {
201       Dmsg1(110, "<bfiled: %s", fd->msg);
202       jcr->set_JobType(JT_BACKUP);
203       if (do_append_data(jcr)) {
204          return true;
205       } else {
206          pm_strcpy(jcr->errmsg, _("Append data error.\n"));
207          bnet_suppress_error_messages(fd, 1); /* ignore errors at this point */
208          fd->fsend(ERROR_append);
209       }
210    } else {
211       pm_strcpy(jcr->errmsg, _("Attempt to append on non-open session.\n"));
212       fd->fsend(NOT_opened);
213    }
214    return false;
215 }
216
217 static bool append_end_session(JCR *jcr)
218 {
219    BSOCK *fd = jcr->file_bsock;
220
221    Dmsg1(120, "store<file: %s", fd->msg);
222    if (!jcr->session_opened) {
223       pm_strcpy(jcr->errmsg, _("Attempt to close non-open session.\n"));
224       fd->fsend(NOT_opened);
225       return false;
226    }
227    return fd->fsend(OK_end);
228 }
229
230
231 /*
232  * Append Open session command
233  *
234  */
235 static bool append_open_session(JCR *jcr)
236 {
237    BSOCK *fd = jcr->file_bsock;
238
239    Dmsg1(120, "Append open session: %s", fd->msg);
240    if (jcr->session_opened) {
241       pm_strcpy(jcr->errmsg, _("Attempt to open already open session.\n"));
242       fd->fsend(NO_open);
243       return false;
244    }
245
246    jcr->session_opened = true;
247
248    /* Send "Ticket" to File Daemon */
249    fd->fsend(OK_open, jcr->VolSessionId);
250    Dmsg1(110, ">filed: %s", fd->msg);
251
252    return true;
253 }
254
255 /*
256  *   Append Close session command
257  *      Close the append session and send back Statistics
258  *         (need to fix statistics)
259  */
260 static bool append_close_session(JCR *jcr)
261 {
262    BSOCK *fd = jcr->file_bsock;
263
264    Dmsg1(120, "<filed: %s", fd->msg);
265    if (!jcr->session_opened) {
266       pm_strcpy(jcr->errmsg, _("Attempt to close non-open session.\n"));
267       fd->fsend(NOT_opened);
268       return false;
269    }
270    /* Send final statistics to File daemon */
271    fd->fsend(OK_close, jcr->JobStatus);
272    Dmsg1(120, ">filed: %s", fd->msg);
273
274    fd->signal(BNET_EOD);              /* send EOD to File daemon */
275
276    jcr->session_opened = false;
277    return true;
278 }
279
280 /*
281  *   Read Data command
282  *     Open Data Channel, read the data from
283  *     the archive device and send to File
284  *     daemon.
285  */
286 static bool read_data_cmd(JCR *jcr)
287 {
288    BSOCK *fd = jcr->file_bsock;
289
290    Dmsg1(120, "Read data: %s", fd->msg);
291    if (jcr->session_opened) {
292       Dmsg1(120, "<bfiled: %s", fd->msg);
293       return do_read_data(jcr);
294    } else {
295       pm_strcpy(jcr->errmsg, _("Attempt to read on non-open session.\n"));
296       fd->fsend(NOT_opened);
297       return false;
298    }
299 }
300
301 /*
302  * Read Open session command
303  *
304  *  We need to scan for the parameters of the job
305  *    to be restored.
306  */
307 static bool read_open_session(JCR *jcr)
308 {
309    BSOCK *fd = jcr->file_bsock;
310
311    Dmsg1(120, "%s\n", fd->msg);
312    if (jcr->session_opened) {
313       pm_strcpy(jcr->errmsg, _("Attempt to open read on non-open session.\n"));
314       fd->fsend(NO_open);
315       return false;
316    }
317
318    if (sscanf(fd->msg, read_open, jcr->read_dcr->VolumeName, &jcr->read_VolSessionId,
319          &jcr->read_VolSessionTime, &jcr->read_StartFile, &jcr->read_EndFile,
320          &jcr->read_StartBlock, &jcr->read_EndBlock) == 7) {
321       if (jcr->session_opened) {
322          pm_strcpy(jcr->errmsg, _("Attempt to open read on non-open session.\n"));
323          fd->fsend(NOT_opened);
324          return false;
325       }
326       Dmsg4(100, "read_open_session got: JobId=%d Vol=%s VolSessId=%ld VolSessT=%ld\n",
327          jcr->JobId, jcr->read_dcr->VolumeName, jcr->read_VolSessionId,
328          jcr->read_VolSessionTime);
329       Dmsg4(100, "  StartF=%ld EndF=%ld StartB=%ld EndB=%ld\n",
330          jcr->read_StartFile, jcr->read_EndFile, jcr->read_StartBlock,
331          jcr->read_EndBlock);
332    }
333
334    jcr->session_opened = true;
335    jcr->set_JobType(JT_RESTORE);
336
337    /* Send "Ticket" to File Daemon */
338    fd->fsend(OK_open, jcr->VolSessionId);
339    Dmsg1(110, ">filed: %s", fd->msg);
340
341    return true;
342 }
343
344 static bool bootstrap_cmd(JCR *jcr)
345 {
346    return get_bootstrap_file(jcr, jcr->file_bsock);
347 }
348
349 static pthread_mutex_t bsr_mutex = PTHREAD_MUTEX_INITIALIZER;
350 static uint32_t bsr_uniq = 0;
351
352 bool get_bootstrap_file(JCR *jcr, BSOCK *sock)
353 {
354    POOLMEM *fname = get_pool_memory(PM_FNAME);
355    FILE *bs;
356    bool ok = false;
357
358    if (jcr->RestoreBootstrap) {
359       unlink(jcr->RestoreBootstrap);
360       free_pool_memory(jcr->RestoreBootstrap);
361    }
362    P(bsr_mutex);
363    bsr_uniq++;
364    Mmsg(fname, "%s/%s.%s.%d.bootstrap", me->working_directory, me->hdr.name,
365       jcr->Job, bsr_uniq);
366    V(bsr_mutex);
367    Dmsg1(400, "bootstrap=%s\n", fname);
368    jcr->RestoreBootstrap = fname;
369    bs = fopen(fname, "a+b");           /* create file */
370    if (!bs) {
371       berrno be;
372       Jmsg(jcr, M_FATAL, 0, _("Could not create bootstrap file %s: ERR=%s\n"),
373          jcr->RestoreBootstrap, be.bstrerror());
374       goto bail_out;
375    }
376    Dmsg0(10, "=== Bootstrap file ===\n");
377    while (sock->recv() >= 0) {
378        Dmsg1(10, "%s", sock->msg);
379        fputs(sock->msg, bs);
380    }
381    fclose(bs);
382    Dmsg0(10, "=== end bootstrap file ===\n");
383    jcr->bsr = parse_bsr(jcr, jcr->RestoreBootstrap);
384    if (!jcr->bsr) {
385       Jmsg(jcr, M_FATAL, 0, _("Error parsing bootstrap file.\n"));
386       goto bail_out;
387    }
388    if (debug_level >= 10) {
389       dump_bsr(jcr->bsr, true);
390    }
391    /* If we got a bootstrap, we are reading, so create read volume list */
392    create_restore_volume_list(jcr);
393    ok = true;
394
395 bail_out:
396    unlink(jcr->RestoreBootstrap);
397    free_pool_memory(jcr->RestoreBootstrap);
398    jcr->RestoreBootstrap = NULL;
399    if (!ok) {
400       sock->fsend(ERROR_bootstrap);
401       return false;
402    }
403    return sock->fsend(OK_bootstrap);
404 }
405
406
407 /*
408  *   Read Close session command
409  *      Close the read session
410  */
411 static bool read_close_session(JCR *jcr)
412 {
413    BSOCK *fd = jcr->file_bsock;
414
415    Dmsg1(120, "Read close session: %s\n", fd->msg);
416    if (!jcr->session_opened) {
417       fd->fsend(NOT_opened);
418       return false;
419    }
420    /* Send final close msg to File daemon */
421    fd->fsend(OK_close, jcr->JobStatus);
422    Dmsg1(160, ">filed: %s\n", fd->msg);
423
424    fd->signal(BNET_EOD);            /* send EOD to File daemon */
425
426    jcr->session_opened = false;
427    return true;
428 }