]> git.sur5r.net Git - bacula/bacula/blob - bacula/src/stored/append.c
More DCR changes
[bacula/bacula] / bacula / src / stored / append.c
1 /*
2  * Append code for Storage daemon
3  *  Kern Sibbald, May MM
4  *
5  *  Version $Id$
6  */
7 /*
8    Copyright (C) 2000-2004 Kern Sibbald and John Walker
9
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.
14
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.
19
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,
23    MA 02111-1307, USA.
24
25  */
26
27 #include "bacula.h"
28 #include "stored.h"
29
30
31 /* Responses sent to the File daemon */
32 static char OK_data[]    = "3000 OK data\n";
33
34 /* Forward referenced functions */
35
36 /* 
37  *  Append Data sent from File daemon   
38  *
39  */
40 int do_append_data(JCR *jcr) 
41 {
42    int32_t n;
43    int32_t file_index, stream, last_file_index;
44    BSOCK *ds;
45    BSOCK *fd_sock = jcr->file_bsock;
46    bool ok = true;
47    DEVICE *dev;
48    DEV_RECORD rec;
49    DEV_BLOCK  *block;
50    DCR *dcr;
51    
52    Dmsg0(10, "Start append data.\n");
53
54    ds = fd_sock;
55
56    if (!bnet_set_buffer_size(ds, jcr->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"));
59       return 0;
60    }
61
62    /* 
63     * Acquire output device for writing.  Note, after acquiring a
64     *   device, we MUST release it, which is done at the end of this
65     *   subroutine.
66     */
67    Dmsg0(100, "just before acquire_device\n");
68    if (!(dcr=acquire_device_for_append(jcr))) {
69       set_jcr_job_status(jcr, JS_ErrorTerminated);
70       return 0;
71    }
72    dev = dcr->dev;
73    block = dcr->block;
74    memset(&rec, 0, sizeof(rec));
75
76    Dmsg1(20, "Begin append device=%s\n", dev_name(dev));
77
78    begin_data_spool(jcr);
79    begin_attribute_spool(jcr);
80
81    Dmsg0(100, "Just after acquire_device_for_append\n");
82    /*
83     * Write Begin Session Record
84     */
85    if (!write_session_label(dcr, block, SOS_LABEL)) {
86       Jmsg1(jcr, M_FATAL, 0, _("Write session label failed. ERR=%s\n"),
87          strerror_dev(dev));
88       set_jcr_job_status(jcr, JS_ErrorTerminated);
89       ok = false;
90    }
91
92    /* Tell File daemon to send data */
93    bnet_fsend(fd_sock, OK_data);
94
95    /* 
96     * Get Data from File daemon, write to device.  To clarify what is
97     *   going on here.  We expect:        
98     *     - A stream header
99     *     - Multiple records of data
100     *     - EOD record
101     *
102     *    The Stream header is just used to sychronize things, and
103     *    none of the stream header is written to tape.
104     *    The Multiple records of data, contain first the Attributes,
105     *    then after another stream header, the file data, then
106     *    after another stream header, the MD5 data if any.  
107     *
108     *   So we get the (stream header, data, EOD) three time for each
109     *   file. 1. for the Attributes, 2. for the file data if any, 
110     *   and 3. for the MD5 if any.
111     */
112    dcr->VolFirstIndex = dcr->VolLastIndex = 0;
113    jcr->run_time = time(NULL);              /* start counting time for rates */
114    for (last_file_index = 0; ok && !job_canceled(jcr); ) {
115
116       /* Read Stream header from the File daemon.
117        *  The stream header consists of the following:
118        *    file_index (sequential Bacula file index, base 1)
119        *    stream     (Bacula number to distinguish parts of data)
120        *    info       (Info for Storage daemon -- compressed, encryped, ...)
121        *       info is not currently used, so is read, but ignored!
122        */
123      if ((n=bget_msg(ds)) <= 0) {
124          if (n == BNET_SIGNAL && ds->msglen == BNET_EOD) {
125             break;                    /* end of data */
126          }
127          Jmsg1(jcr, M_FATAL, 0, _("Error reading data header from FD. ERR=%s\n"),
128             bnet_strerror(ds));
129          ok = false;
130          break;
131       }
132         
133       /* 
134        * This hand scanning is a bit more complicated than a simple
135        *   sscanf, but it allows us to handle any size integer up to
136        *   int64_t without worrying about whether %d, %ld, %lld, or %q 
137        *   is the correct format for each different architecture.
138        * It is a real pity that sscanf() is not portable.
139        */
140       char *p = ds->msg;
141       while (B_ISSPACE(*p)) {
142          p++;
143       }
144       file_index = (int32_t)str_to_int64(p);
145       while (B_ISDIGIT(*p)) {
146          p++;
147       }
148       if (!B_ISSPACE(*p) || !B_ISDIGIT(*(p+1))) {
149          Jmsg1(jcr, M_FATAL, 0, _("Malformed data header from FD: %s\n"), ds->msg);
150          ok = false;
151          break;
152       }
153       stream = (int32_t)str_to_int64(p);
154
155       Dmsg2(190, "<filed: Header FilInx=%d stream=%d\n", file_index, stream);
156
157       if (!(file_index > 0 && (file_index == last_file_index ||
158           file_index == last_file_index + 1))) {
159          Jmsg0(jcr, M_FATAL, 0, _("File index from FD not positive or sequential\n"));
160          ok = false;
161          break;
162       }
163       if (file_index != last_file_index) {
164          jcr->JobFiles = file_index;
165          last_file_index = file_index;
166       }
167       
168       /* Read data stream from the File daemon.
169        *  The data stream is just raw bytes
170        */
171       while ((n=bget_msg(ds)) > 0 && !job_canceled(jcr)) {
172          rec.VolSessionId = jcr->VolSessionId;
173          rec.VolSessionTime = jcr->VolSessionTime;
174          rec.FileIndex = file_index;
175          rec.Stream = stream;
176          rec.data_len = ds->msglen;
177          rec.data = ds->msg;            /* use message buffer */
178
179          Dmsg4(250, "before writ_rec FI=%d SessId=%d Strm=%s len=%d\n",
180             rec.FileIndex, rec.VolSessionId, stream_to_ascii(rec.Stream,rec.FileIndex), 
181             rec.data_len);
182           
183          while (!write_record_to_block(block, &rec)) {
184             Dmsg2(150, "!write_record_to_block data_len=%d rem=%d\n", rec.data_len,
185                        rec.remainder);
186             if (!write_block_to_device(jcr->dcr, block)) {
187                Dmsg2(90, "Got write_block_to_dev error on device %s. %s\n",
188                   dev_name(dev), strerror_dev(dev));
189                Jmsg(jcr, M_FATAL, 0, _("Fatal device error: ERR=%s\n"),
190                      strerror_dev(dev));
191                ok = false;
192                break;
193             }
194          }
195          if (!ok) {
196             Dmsg0(400, "Not OK\n");
197             break;
198          }
199          jcr->JobBytes += rec.data_len;   /* increment bytes this job */
200          Dmsg4(200, "write_record FI=%s SessId=%d Strm=%s len=%d\n",
201             FI_to_ascii(rec.FileIndex), rec.VolSessionId, 
202             stream_to_ascii(rec.Stream, rec.FileIndex), rec.data_len);
203
204          /* Send attributes and MD5 to Director for Catalog */
205          if (stream == STREAM_UNIX_ATTRIBUTES    || stream == STREAM_MD5_SIGNATURE ||
206              stream == STREAM_UNIX_ATTRIBUTES_EX || stream == STREAM_SHA1_SIGNATURE) { 
207             if (!jcr->no_attributes) {
208                if (are_attributes_spooled(jcr)) {
209                   jcr->dir_bsock->spool = true;
210                }
211                Dmsg0(200, "Send attributes.\n");
212                if (!dir_update_file_attributes(dcr, &rec)) {
213                   jcr->dir_bsock->spool = false;
214                   Jmsg(jcr, M_FATAL, 0, _("Error updating file attributes. ERR=%s\n"),
215                      bnet_strerror(jcr->dir_bsock));
216                   ok = false;
217                   break;
218                }
219                jcr->dir_bsock->spool = false;
220             }
221          }
222       }
223       if (is_bnet_error(ds)) {
224          Jmsg1(jcr, M_FATAL, 0, _("Network error on data channel. ERR=%s\n"),
225             bnet_strerror(ds));
226          ok = false;
227          break;
228       }
229    }
230
231    /* Create Job status for end of session label */
232    set_jcr_job_status(jcr, ok?JS_Terminated:JS_ErrorTerminated);
233
234    Dmsg1(200, "Write session label JobStatus=%d\n", jcr->JobStatus);
235
236    /*
237     * If !OK, check if we can still write. This may not be the case
238     *  if we are at the end of the tape or we got a fatal I/O error.
239     */
240    if (ok || dev_can_write(dev)) {
241       if (!write_session_label(dcr, block, EOS_LABEL)) {
242          Jmsg1(jcr, M_FATAL, 0, _("Error writting end session label. ERR=%s\n"),
243              strerror_dev(dev));
244          set_jcr_job_status(jcr, JS_ErrorTerminated);
245          ok = false;
246       }
247       Dmsg0(90, "back from write_end_session_label()\n");
248       /* Flush out final partial block of this session */
249       if (!write_block_to_device(dcr, block)) {
250          Dmsg0(100, _("Set ok=FALSE after write_block_to_device.\n"));
251          set_jcr_job_status(jcr, JS_ErrorTerminated);
252          ok = false;
253       }
254    }
255
256    if (!ok) {
257       discard_data_spool(jcr);
258    } else {
259       commit_data_spool(jcr);
260    }
261
262    Dmsg1(200, "calling release device JobStatus=%d\n", jcr->JobStatus);
263    /* Release the device */
264    if (!release_device(jcr)) {
265       Pmsg0(000, _("Error in release_device\n"));
266       set_jcr_job_status(jcr, JS_ErrorTerminated);
267       ok = false;
268    }
269
270    if (!ok) {
271       discard_attribute_spool(jcr);
272    } else {
273       commit_attribute_spool(jcr);
274    }
275
276    dir_send_job_status(jcr);          /* update director */
277
278    Dmsg1(100, "return from do_append_data() stat=%d\n", ok);
279    return ok;
280 }