2 * Append code for Storage daemon
8 Copyright (C) 2000-2003 Kern Sibbald and John Walker
10 This program is free software; you can redistribute it and/or
11 modify it under the terms of the GNU General Public License as
12 published by the Free Software Foundation; either version 2 of
13 the License, or (at your option) any later version.
15 This program is distributed in the hope that it will be useful,
16 but WITHOUT ANY WARRANTY; without even the implied warranty of
17 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
18 General Public License for more details.
20 You should have received a copy of the GNU General Public
21 License along with this program; if not, write to the Free
22 Software Foundation, Inc., 59 Temple Place - Suite 330, Boston,
31 /* Responses sent to the File daemon */
32 static char OK_data[] = "3000 OK data\n";
34 /* Forward referenced functions */
35 static bool is_spooled(JCR *jcr);
36 static int begin_attribute_spool(JCR *jcr);
37 static int discard_attribute_spool(JCR *jcr);
38 static int commit_attribute_spool(JCR *jcr);
41 * Append Data sent from File daemon
44 int do_append_data(JCR *jcr)
47 int32_t file_index, stream, last_file_index;
49 BSOCK *fd_sock = jcr->file_bsock;
51 DEVICE *dev = jcr->device->dev;
55 Dmsg0(10, "Start append data.\n");
57 /* Tell File daemon to send data */
58 bnet_fsend(fd_sock, OK_data);
60 begin_attribute_spool(jcr);
64 if (!bnet_set_buffer_size(ds, MAX_NETWORK_BUFFER_SIZE, BNET_SETBUF_WRITE)) {
65 set_jcr_job_status(jcr, JS_ErrorTerminated);
66 Jmsg(jcr, M_FATAL, 0, _("Unable to set network buffer size.\n"));
67 discard_attribute_spool(jcr);
71 Dmsg1(20, "Begin append device=%s\n", dev_name(dev));
73 block = new_block(dev);
74 memset(&rec, 0, sizeof(rec));
77 * Acquire output device for writing. Note, after acquiring a
78 * device, we MUST release it, which is done at the end of this
81 Dmsg0(100, "just before acquire_device\n");
82 if (!(dev=acquire_device_for_append(jcr, dev, block))) {
83 set_jcr_job_status(jcr, JS_ErrorTerminated);
85 discard_attribute_spool(jcr);
88 Dmsg0(100, "Just after acquire_device_for_append\n");
90 * Write Begin Session Record
92 if (!write_session_label(jcr, block, SOS_LABEL)) {
93 Jmsg1(jcr, M_FATAL, 0, _("Write session label failed. ERR=%s\n"),
95 set_jcr_job_status(jcr, JS_ErrorTerminated);
101 * Get Data from File daemon, write to device. To clarify what is
102 * going on here. We expect:
104 * - Multiple records of data
107 * The Stream header is just used to sychronize things, and
108 * none of the stream header is written to tape.
109 * The Multiple records of data, contain first the Attributes,
110 * then after another stream header, the file data, then
111 * after another stream header, the MD5 data if any.
113 * So we get the (stream header, data, EOD) three time for each
114 * file. 1. for the Attributes, 2. for the file data if any,
115 * and 3. for the MD5 if any.
117 jcr->VolFirstIndex = jcr->VolLastIndex = 0;
118 jcr->run_time = time(NULL); /* start counting time for rates */
119 for (last_file_index = 0; ok && !job_canceled(jcr); ) {
121 /* Read Stream header from the File daemon.
122 * The stream header consists of the following:
123 * file_index (sequential Bacula file index, base 1)
124 * stream (Bacula number to distinguish parts of data)
125 * info (Info for Storage daemon -- compressed, encryped, ...)
126 * info is not currently used, so is read, but ignored!
128 if ((n=bget_msg(ds)) <= 0) {
129 if (n == BNET_SIGNAL && ds->msglen == BNET_EOD) {
130 break; /* end of data */
132 Jmsg1(jcr, M_FATAL, 0, _("Error reading data header from FD. ERR=%s\n"),
139 * This hand scanning is a bit more complicated than a simple
140 * sscanf, but it allows us to handle any size integer up to
141 * int64_t without worrying about whether %d, %ld, %lld, or %q
142 * is the correct format for each different architecture.
143 * It is a real pity that sscanf() is not portable.
146 while (B_ISSPACE(*p)) {
149 file_index = (int32_t)str_to_int64(p);
150 while (B_ISDIGIT(*p)) {
153 if (!B_ISSPACE(*p) || !B_ISDIGIT(*(p+1))) {
154 Jmsg1(jcr, M_FATAL, 0, _("Malformed data header from FD: %s\n"), ds->msg);
158 stream = (int32_t)str_to_int64(p);
160 Dmsg2(190, "<filed: Header FilInx=%d stream=%d\n", file_index, stream);
162 if (!(file_index > 0 && (file_index == last_file_index ||
163 file_index == last_file_index + 1))) {
164 Jmsg0(jcr, M_FATAL, 0, _("File index from FD not positive or sequential\n"));
168 if (file_index != last_file_index) {
169 jcr->JobFiles = file_index;
170 last_file_index = file_index;
173 /* Read data stream from the File daemon.
174 * The data stream is just raw bytes
176 while ((n=bget_msg(ds)) > 0 && !job_canceled(jcr)) {
178 rec.VolSessionId = jcr->VolSessionId;
179 rec.VolSessionTime = jcr->VolSessionTime;
180 rec.FileIndex = file_index;
182 rec.data_len = ds->msglen;
183 rec.data = ds->msg; /* use message buffer */
185 Dmsg4(250, "before writ_rec FI=%d SessId=%d Strm=%s len=%d\n",
186 rec.FileIndex, rec.VolSessionId, stream_to_ascii(rec.Stream,rec.FileIndex),
189 while (!write_record_to_block(block, &rec)) {
190 Dmsg2(150, "!write_record_to_block data_len=%d rem=%d\n", rec.data_len,
192 if (!write_block_to_device(jcr, dev, block)) {
193 Dmsg2(90, "Got write_block_to_dev error on device %s. %s\n",
194 dev_name(dev), strerror_dev(dev));
195 Jmsg(jcr, M_FATAL, 0, _("Cannot fixup device error. %s\n"),
202 Dmsg0(400, "Not OK\n");
205 jcr->JobBytes += rec.data_len; /* increment bytes this job */
206 Dmsg4(200, "write_record FI=%s SessId=%d Strm=%s len=%d\n",
207 FI_to_ascii(rec.FileIndex), rec.VolSessionId,
208 stream_to_ascii(rec.Stream, rec.FileIndex), rec.data_len);
210 /* Send attributes and MD5 to Director for Catalog */
211 if (stream == STREAM_UNIX_ATTRIBUTES || stream == STREAM_MD5_SIGNATURE ||
212 stream == STREAM_UNIX_ATTRIBUTES_EX || stream == STREAM_SHA1_SIGNATURE) {
213 if (!jcr->no_attributes) {
214 if (is_spooled(jcr)) {
215 jcr->dir_bsock->spool = 1;
217 Dmsg0(200, "Send attributes.\n");
218 if (!dir_update_file_attributes(jcr, &rec)) {
219 Jmsg(jcr, M_FATAL, 0, _("Error updating file attributes. ERR=%s\n"),
220 bnet_strerror(jcr->dir_bsock));
222 jcr->dir_bsock->spool = 0;
225 jcr->dir_bsock->spool = 0;
229 if (is_bnet_error(ds)) {
230 Jmsg1(jcr, M_FATAL, 0, _("Network error on data channel. ERR=%s\n"),
237 * We probably need a new flag that says "Do not attempt
238 * to write because there is no tape".
240 Dmsg0(90, "Write_end_session_label()\n");
242 /* Create Job status for end of session label */
243 set_jcr_job_status(jcr, ok?JS_Terminated:JS_ErrorTerminated);
245 Dmsg1(200, "Write session label JobStatus=%d\n", jcr->JobStatus);
248 * If !OK, check if we can still write. This may not be the case
249 * if we are at the end of the tape or we got a fatal I/O error.
251 if (ok || dev_can_write(dev)) {
252 if (!write_session_label(jcr, block, EOS_LABEL)) {
253 Jmsg1(jcr, M_FATAL, 0, _("Error writting end session label. ERR=%s\n"),
255 set_jcr_job_status(jcr, JS_ErrorTerminated);
258 /* Flush out final partial block of this session */
259 if (!write_block_to_device(jcr, dev, block)) {
260 Dmsg0(100, _("Set ok=FALSE after write_block_to_device.\n"));
261 set_jcr_job_status(jcr, JS_ErrorTerminated);
266 Dmsg1(200, "release device JobStatus=%d\n", jcr->JobStatus);
267 /* Release the device */
268 if (!release_device(jcr, dev)) {
269 Pmsg0(000, _("Error in release_device\n"));
270 set_jcr_job_status(jcr, JS_ErrorTerminated);
276 commit_attribute_spool(jcr);
278 dir_send_job_status(jcr); /* update director */
280 Dmsg1(100, "return from do_append_data() stat=%d\n", ok);
284 static bool is_spooled(JCR *jcr)
286 return jcr->spool_attributes && jcr->dir_bsock->spool_fd;
289 static int begin_attribute_spool(JCR *jcr)
291 if (!jcr->no_attributes && jcr->spool_attributes) {
292 return open_spool_file(jcr, jcr->dir_bsock);
297 static int discard_attribute_spool(JCR *jcr)
299 if (is_spooled(jcr)) {
300 return close_spool_file(jcr, jcr->dir_bsock);
305 static int commit_attribute_spool(JCR *jcr)
307 if (is_spooled(jcr)) {
308 bnet_despool(jcr->dir_bsock);
309 return close_spool_file(jcr, jcr->dir_bsock);