2 * Append code for Storage daemon
8 Copyright (C) 2000-2005 Kern Sibbald
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 */
37 * Append Data sent from File daemon
40 bool do_append_data(JCR *jcr)
43 int32_t file_index, stream, last_file_index;
45 BSOCK *fd_sock = jcr->file_bsock;
49 DEVICE *dev = dcr->dev;
52 Dmsg0(10, "Start append data.\n");
56 if (!bnet_set_buffer_size(ds, dcr->device->max_network_buffer_size, BNET_SETBUF_WRITE)) {
57 set_jcr_job_status(jcr, JS_ErrorTerminated);
58 Jmsg(jcr, M_FATAL, 0, _("Unable to set network buffer size.\n"));
62 if (!acquire_device_for_append(jcr, dev)) {
63 set_jcr_job_status(jcr, JS_ErrorTerminated);
67 memset(&rec, 0, sizeof(rec));
69 set_jcr_job_status(jcr, JS_Running);
70 dir_send_job_status(jcr);
72 if (dev->VolCatInfo.VolCatName[0] == 0) {
73 Dmsg0(000, "NULL Volume name. This shouldn't happen!!!\n");
75 Dmsg1(20, "Begin append device=%s\n", dev_name(dev));
77 begin_data_spool(jcr);
78 begin_attribute_spool(jcr);
80 Dmsg0(100, "Just after acquire_device_for_append\n");
81 if (dev->VolCatInfo.VolCatName[0] == 0) {
82 Dmsg0(000, "NULL Volume name. This shouldn't happen!!!\n");
85 * Write Begin Session Record
87 if (!write_session_label(dcr, SOS_LABEL)) {
88 Jmsg1(jcr, M_FATAL, 0, _("Write session label failed. ERR=%s\n"),
90 set_jcr_job_status(jcr, JS_ErrorTerminated);
93 if (dev->VolCatInfo.VolCatName[0] == 0) {
94 Dmsg0(000, "NULL Volume name. This shouldn't happen!!!\n");
97 /* Tell File daemon to send data */
98 if (!bnet_fsend(fd_sock, OK_data)) {
100 Jmsg1(jcr, M_FATAL, 0, _("Network send error to FD. ERR=%s\n"),
101 be.strerror(fd_sock->b_errno));
106 * Get Data from File daemon, write to device. To clarify what is
107 * going on here. We expect:
109 * - Multiple records of data
112 * The Stream header is just used to sychronize things, and
113 * none of the stream header is written to tape.
114 * The Multiple records of data, contain first the Attributes,
115 * then after another stream header, the file data, then
116 * after another stream header, the MD5 data if any.
118 * So we get the (stream header, data, EOD) three time for each
119 * file. 1. for the Attributes, 2. for the file data if any,
120 * and 3. for the MD5 if any.
122 dcr->VolFirstIndex = dcr->VolLastIndex = 0;
123 jcr->run_time = time(NULL); /* start counting time for rates */
124 for (last_file_index = 0; ok && !job_canceled(jcr); ) {
126 /* Read Stream header from the File daemon.
127 * The stream header consists of the following:
128 * file_index (sequential Bacula file index, base 1)
129 * stream (Bacula number to distinguish parts of data)
130 * info (Info for Storage daemon -- compressed, encryped, ...)
131 * info is not currently used, so is read, but ignored!
133 if ((n=bget_msg(ds)) <= 0) {
134 if (n == BNET_SIGNAL && ds->msglen == BNET_EOD) {
135 break; /* end of data */
137 Jmsg1(jcr, M_FATAL, 0, _("Error reading data header from FD. ERR=%s\n"),
144 * This hand scanning is a bit more complicated than a simple
145 * sscanf, but it allows us to handle any size integer up to
146 * int64_t without worrying about whether %d, %ld, %lld, or %q
147 * is the correct format for each different architecture.
148 * It is a real pity that sscanf() is not portable.
151 while (B_ISSPACE(*p)) {
154 file_index = (int32_t)str_to_int64(p);
155 while (B_ISDIGIT(*p)) {
158 if (!B_ISSPACE(*p) || !B_ISDIGIT(*(p+1))) {
159 Jmsg1(jcr, M_FATAL, 0, _("Malformed data header from FD: %s\n"), ds->msg);
163 stream = (int32_t)str_to_int64(p);
165 Dmsg2(890, "<filed: Header FilInx=%d stream=%d\n", file_index, stream);
167 if (!(file_index > 0 && (file_index == last_file_index ||
168 file_index == last_file_index + 1))) {
169 Jmsg0(jcr, M_FATAL, 0, _("File index from FD not positive or sequential\n"));
173 if (file_index != last_file_index) {
174 jcr->JobFiles = file_index;
175 last_file_index = file_index;
178 /* Read data stream from the File daemon.
179 * The data stream is just raw bytes
181 while ((n=bget_msg(ds)) > 0 && !job_canceled(jcr)) {
182 rec.VolSessionId = jcr->VolSessionId;
183 rec.VolSessionTime = jcr->VolSessionTime;
184 rec.FileIndex = file_index;
186 rec.data_len = ds->msglen;
187 rec.data = ds->msg; /* use message buffer */
189 Dmsg4(850, "before writ_rec FI=%d SessId=%d Strm=%s len=%d\n",
190 rec.FileIndex, rec.VolSessionId, stream_to_ascii(rec.Stream,rec.FileIndex),
193 while (!write_record_to_block(dcr->block, &rec)) {
194 Dmsg2(850, "!write_record_to_block data_len=%d rem=%d\n", rec.data_len,
196 if (!write_block_to_device(dcr)) {
197 Dmsg2(90, "Got write_block_to_dev error on device %s. %s\n",
198 dev_name(dev), strerror_dev(dev));
199 Jmsg2(jcr, M_FATAL, 0, _("Fatal append error on device %s: ERR=%s\n"),
200 dev_name(dev), strerror_dev(dev));
206 Dmsg0(400, "Not OK\n");
209 jcr->JobBytes += rec.data_len; /* increment bytes this job */
210 Dmsg4(850, "write_record FI=%s SessId=%d Strm=%s len=%d\n",
211 FI_to_ascii(rec.FileIndex), rec.VolSessionId,
212 stream_to_ascii(rec.Stream, rec.FileIndex), rec.data_len);
214 /* Send attributes and MD5 to Director for Catalog */
215 if (stream == STREAM_UNIX_ATTRIBUTES || stream == STREAM_MD5_SIGNATURE ||
216 stream == STREAM_UNIX_ATTRIBUTES_EX || stream == STREAM_SHA1_SIGNATURE) {
217 if (!jcr->no_attributes) {
218 if (are_attributes_spooled(jcr)) {
219 jcr->dir_bsock->spool = true;
221 Dmsg0(850, "Send attributes to dir.\n");
222 if (!dir_update_file_attributes(dcr, &rec)) {
223 jcr->dir_bsock->spool = false;
224 Jmsg(jcr, M_FATAL, 0, _("Error updating file attributes. ERR=%s\n"),
225 bnet_strerror(jcr->dir_bsock));
229 jcr->dir_bsock->spool = false;
232 Dmsg0(350, "Enter bnet_get\n");
234 Dmsg1(350, "End read loop with FD. Stat=%d\n", n);
235 if (is_bnet_error(ds)) {
236 Dmsg1(350, "Network read error from FD. ERR=%s\n", bnet_strerror(ds));
237 Jmsg1(jcr, M_FATAL, 0, _("Network error on data channel. ERR=%s\n"),
244 /* Create Job status for end of session label */
245 set_jcr_job_status(jcr, ok?JS_Terminated:JS_ErrorTerminated);
247 Dmsg1(200, "Write session label JobStatus=%d\n", jcr->JobStatus);
248 if (dev->VolCatInfo.VolCatName[0] == 0) {
249 Dmsg0(000, "NULL Volume name. This shouldn't happen!!!\n");
253 * If !OK, check if we can still write. This may not be the case
254 * if we are at the end of the tape or we got a fatal I/O error.
256 if (ok || dev_can_write(dev)) {
257 if (!write_session_label(dcr, EOS_LABEL)) {
258 Jmsg1(jcr, M_FATAL, 0, _("Error writting end session label. ERR=%s\n"),
260 set_jcr_job_status(jcr, JS_ErrorTerminated);
263 if (dev->VolCatInfo.VolCatName[0] == 0) {
264 Dmsg0(000, "NULL Volume name. This shouldn't happen!!!\n");
266 Dmsg0(90, "back from write_end_session_label()\n");
267 /* Flush out final partial block of this session */
268 if (!write_block_to_device(dcr)) {
269 Dmsg0(100, _("Set ok=FALSE after write_block_to_device.\n"));
270 set_jcr_job_status(jcr, JS_ErrorTerminated);
274 if (dev->VolCatInfo.VolCatName[0] == 0) {
275 Dmsg0(000, "NULL Volume name. This shouldn't happen!!!\n");
279 discard_data_spool(jcr);
281 commit_data_spool(jcr);
284 /* If the device is nor a dvd and WritePartAfterJob
285 * is set to yes, open the next part, so, in case of a device
286 * that requires mount, it will be written to the device.
288 if (dev->is_dvd() && jcr->write_part_after_job && (dev->part_size > 0)) {
289 Dmsg0(100, "Writing last part because write_part_after_job is set.\n");
290 if (dev->part < dev->num_parts) {
291 Jmsg3(jcr, M_FATAL, 0, _("Error while writing, current part number is less than the total number of parts (%d/%d, device=%s)\n"),
292 dev->part, dev->num_parts, dev_name(dev));
293 dev->dev_errno = EIO;
297 if (ok && (open_next_part(dev) < 0)) {
298 Jmsg2(jcr, M_FATAL, 0, _("Unable to open device next part %s. ERR=%s\n"),
299 dev_name(dev), strerror_dev(dev));
300 dev->dev_errno = EIO;
304 dev->VolCatInfo.VolCatParts = dev->num_parts;
307 Dmsg1(200, "calling release device JobStatus=%d\n", jcr->JobStatus);
308 /* Release the device */
309 if (!release_device(jcr)) {
310 Pmsg0(000, _("Error in release_device\n"));
311 set_jcr_job_status(jcr, JS_ErrorTerminated);
316 discard_attribute_spool(jcr);
318 commit_attribute_spool(jcr);
321 dir_send_job_status(jcr); /* update director */
323 Dmsg1(100, "return from do_append_data() stat=%d\n", ok);