2 Bacula® - The Network Backup Solution
4 Copyright (C) 2000-2009 Free Software Foundation Europe e.V.
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
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.
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
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.
29 * Append code for Storage daemon
30 * Kern Sibbald, May MM
39 /* Responses sent to the File daemon */
40 static char OK_data[] = "3000 OK data\n";
41 static char OK_append[] = "3000 OK append data\n";
43 /* Forward referenced functions */
46 * Append Data sent from File daemon
49 bool do_append_data(JCR *jcr)
52 int32_t file_index, stream, last_file_index;
54 BSOCK *fd_sock = jcr->file_bsock;
57 char buf1[100], buf2[100];
64 Jmsg0(jcr, M_FATAL, 0, _("DCR is NULL!!!\n"));
69 Jmsg0(jcr, M_FATAL, 0, _("DEVICE is NULL!!!\n"));
73 Dmsg1(100, "Start append data. res=%d\n", dev->num_reserved());
75 memset(&rec, 0, sizeof(rec));
79 if (!ds->set_buffer_size(dcr->device->max_network_buffer_size, BNET_SETBUF_WRITE)) {
80 set_jcr_job_status(jcr, JS_ErrorTerminated);
81 Jmsg0(jcr, M_FATAL, 0, _("Unable to set network buffer size.\n"));
85 if (!acquire_device_for_append(dcr)) {
86 set_jcr_job_status(jcr, JS_ErrorTerminated);
90 set_jcr_job_status(jcr, JS_Running);
91 dir_send_job_status(jcr);
93 if (dev->VolCatInfo.VolCatName[0] == 0) {
94 Pmsg0(000, _("NULL Volume name. This shouldn't happen!!!\n"));
96 Dmsg1(50, "Begin append device=%s\n", dev->print_name());
98 begin_data_spool(dcr);
99 begin_attribute_spool(jcr);
101 Dmsg0(100, "Just after acquire_device_for_append\n");
102 if (dev->VolCatInfo.VolCatName[0] == 0) {
103 Pmsg0(000, _("NULL Volume name. This shouldn't happen!!!\n"));
106 * Write Begin Session Record
108 if (!write_session_label(dcr, SOS_LABEL)) {
109 Jmsg1(jcr, M_FATAL, 0, _("Write session label failed. ERR=%s\n"),
111 set_jcr_job_status(jcr, JS_ErrorTerminated);
114 if (dev->VolCatInfo.VolCatName[0] == 0) {
115 Pmsg0(000, _("NULL Volume name. This shouldn't happen!!!\n"));
118 /* Tell File daemon to send data */
119 if (!fd_sock->fsend(OK_data)) {
121 Jmsg1(jcr, M_FATAL, 0, _("Network send error to FD. ERR=%s\n"),
122 be.bstrerror(fd_sock->b_errno));
127 * Get Data from File daemon, write to device. To clarify what is
128 * going on here. We expect:
130 * - Multiple records of data
133 * The Stream header is just used to sychronize things, and
134 * none of the stream header is written to tape.
135 * The Multiple records of data, contain first the Attributes,
136 * then after another stream header, the file data, then
137 * after another stream header, the MD5 data if any.
139 * So we get the (stream header, data, EOD) three time for each
140 * file. 1. for the Attributes, 2. for the file data if any,
141 * and 3. for the MD5 if any.
143 dcr->VolFirstIndex = dcr->VolLastIndex = 0;
144 jcr->run_time = time(NULL); /* start counting time for rates */
145 for (last_file_index = 0; ok && !job_canceled(jcr); ) {
147 /* Read Stream header from the File daemon.
148 * The stream header consists of the following:
149 * file_index (sequential Bacula file index, base 1)
150 * stream (Bacula number to distinguish parts of data)
151 * info (Info for Storage daemon -- compressed, encrypted, ...)
152 * info is not currently used, so is read, but ignored!
154 if ((n=bget_msg(ds)) <= 0) {
155 if (n == BNET_SIGNAL && ds->msglen == BNET_EOD) {
156 break; /* end of data */
158 Jmsg1(jcr, M_FATAL, 0, _("Error reading data header from FD. ERR=%s\n"),
164 if (sscanf(ds->msg, "%ld %ld", &file_index, &stream) != 2) {
165 Jmsg1(jcr, M_FATAL, 0, _("Malformed data header from FD: %s\n"), ds->msg);
170 Dmsg2(890, "<filed: Header FilInx=%d stream=%d\n", file_index, stream);
172 if (!(file_index > 0 && (file_index == last_file_index ||
173 file_index == last_file_index + 1))) {
174 Jmsg0(jcr, M_FATAL, 0, _("File index from FD not positive or sequential\n"));
178 if (file_index != last_file_index) {
179 jcr->JobFiles = file_index;
180 last_file_index = file_index;
183 /* Read data stream from the File daemon.
184 * The data stream is just raw bytes
186 while ((n=bget_msg(ds)) > 0 && !job_canceled(jcr)) {
187 rec.VolSessionId = jcr->VolSessionId;
188 rec.VolSessionTime = jcr->VolSessionTime;
189 rec.FileIndex = file_index;
191 rec.data_len = ds->msglen;
192 rec.data = ds->msg; /* use message buffer */
194 Dmsg4(850, "before writ_rec FI=%d SessId=%d Strm=%s len=%d\n",
195 rec.FileIndex, rec.VolSessionId,
196 stream_to_ascii(buf1, rec.Stream,rec.FileIndex),
199 while (!write_record_to_block(dcr->block, &rec)) {
200 Dmsg2(850, "!write_record_to_block data_len=%d rem=%d\n", rec.data_len,
202 if (!write_block_to_device(dcr)) {
203 Dmsg2(90, "Got write_block_to_dev error on device %s. %s\n",
204 dev->print_name(), dev->bstrerror());
210 Dmsg0(400, "Not OK\n");
213 jcr->JobBytes += rec.data_len; /* increment bytes this job */
214 Dmsg4(850, "write_record FI=%s SessId=%d Strm=%s len=%d\n",
215 FI_to_ascii(buf1, rec.FileIndex), rec.VolSessionId,
216 stream_to_ascii(buf2, rec.Stream, rec.FileIndex), rec.data_len);
218 /* Send attributes and digest to Director for Catalog */
219 if (stream == STREAM_UNIX_ATTRIBUTES || stream == STREAM_UNIX_ATTRIBUTES_EX ||
220 crypto_digest_stream_type(stream) != CRYPTO_DIGEST_NONE) {
221 if (!jcr->no_attributes) {
222 BSOCK *dir = jcr->dir_bsock;
223 if (are_attributes_spooled(jcr)) {
226 Dmsg0(850, "Send attributes to dir.\n");
227 if (!dir_update_file_attributes(dcr, &rec)) {
228 dir->clear_spooling();
229 Jmsg(jcr, M_FATAL, 0, _("Error updating file attributes. ERR=%s\n"),
234 dir->clear_spooling();
237 Dmsg0(650, "Enter bnet_get\n");
239 Dmsg1(650, "End read loop with FD. Stat=%d\n", n);
241 if (ds->is_error()) {
242 if (!job_canceled(jcr)) {
243 Dmsg1(350, "Network read error from FD. ERR=%s\n", ds->bstrerror());
244 Jmsg1(jcr, M_FATAL, 0, _("Network error on data channel. ERR=%s\n"),
252 /* Create Job status for end of session label */
253 set_jcr_job_status(jcr, ok?JS_Terminated:JS_ErrorTerminated);
255 /* Terminate connection with FD */
256 ds->fsend(OK_append);
257 do_fd_commands(jcr); /* finish dialog with FD */
260 * Don't use time_t for job_elapsed as time_t can be 32 or 64 bits,
261 * and the subsequent Jmsg() editing will break
263 int32_t job_elapsed = time(NULL) - jcr->run_time;
265 if (job_elapsed <= 0) {
269 Jmsg(dcr->jcr, M_INFO, 0, _("Job write elapsed time = %02d:%02d:%02d, Transfer rate = %s Bytes/second\n"),
270 job_elapsed / 3600, job_elapsed % 3600 / 60, job_elapsed % 60,
271 edit_uint64_with_suffix(jcr->JobBytes / job_elapsed, ec));
274 Dmsg1(200, "Write EOS label JobStatus=%c\n", jcr->JobStatus);
277 * Check if we can still write. This may not be the case
278 * if we are at the end of the tape or we got a fatal I/O error.
280 if (ok || dev->can_write()) {
281 if (!write_session_label(dcr, EOS_LABEL)) {
282 Jmsg1(jcr, M_FATAL, 0, _("Error writing end session label. ERR=%s\n"),
284 set_jcr_job_status(jcr, JS_ErrorTerminated);
287 if (dev->VolCatInfo.VolCatName[0] == 0) {
288 Pmsg0(000, _("NULL Volume name. This shouldn't happen!!!\n"));
289 Dmsg0(000, _("NULL Volume name. This shouldn't happen!!!\n"));
291 Dmsg0(90, "back from write_end_session_label()\n");
292 /* Flush out final partial block of this session */
293 if (!write_block_to_device(dcr)) {
294 if (!job_canceled(jcr)) {
295 Jmsg2(jcr, M_FATAL, 0, _("Fatal append error on device %s: ERR=%s\n"),
296 dev->print_name(), dev->bstrerror());
297 Dmsg0(100, _("Set ok=FALSE after write_block_to_device.\n"));
301 if (dev->VolCatInfo.VolCatName[0] == 0) {
302 Pmsg0(000, _("NULL Volume name. This shouldn't happen!!!\n"));
303 Dmsg0(000, _("NULL Volume name. This shouldn't happen!!!\n"));
309 discard_data_spool(dcr);
311 /* Note: if commit is OK, the device will remain locked */
312 commit_data_spool(dcr);
316 ok = dvd_close_job(dcr); /* do DVD cleanup if any */
320 * Release the device -- and send final Vol info to DIR
325 if (!ok || job_canceled(jcr)) {
326 discard_attribute_spool(jcr);
328 commit_attribute_spool(jcr);
331 dir_send_job_status(jcr); /* update director */
333 Dmsg1(100, "return from do_append_data() ok=%d\n", ok);