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