]> git.sur5r.net Git - bacula/bacula/blob - bacula/src/stored/append.c
Performance enhancements
[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, 2001, 2002 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 extern int  FiledDataChan;            /* File daemon data channel (port) */
31 extern int BaculaTapeVersion;         /* Version number */
32 extern char BaculaId[];               /* Id string */
33
34 /* Responses sent to the File daemon */
35 static char OK_data[]    = "3000 OK data\n";
36
37 /* 
38  *  Append Data sent from File daemon   
39  *
40  */
41 int do_append_data(JCR *jcr) 
42 {
43    int32_t n;
44    long file_index, stream, last_file_index;
45    BSOCK *ds;
46    BSOCK *fd_sock = jcr->file_bsock;
47    int ok = TRUE;
48    DEVICE *dev = jcr->device->dev;
49    DEV_RECORD rec;
50    DEV_BLOCK  *block;
51    
52    Dmsg0(10, "Start append data.\n");
53
54    /* Tell File daemon to send data */
55    bnet_fsend(fd_sock, OK_data);
56
57    sm_check(__FILE__, __LINE__, False);
58
59 #ifdef NO_ATTRIBUTES_TEST
60 // jcr->spool_attributes = 1;
61    jcr->no_attributes = 1;
62 #endif
63    jcr->spool_attributes = 1;
64   
65    if (!jcr->no_attributes && jcr->spool_attributes) {
66       open_spool_file(jcr, jcr->dir_bsock);
67    }
68
69    ds = fd_sock;
70
71    if (!bnet_set_buffer_size(ds, MAX_NETWORK_BUFFER_SIZE, BNET_SETBUF_WRITE)) {
72       jcr->JobStatus = JS_Cancelled;
73       Jmsg(jcr, M_FATAL, 0, _("Unable to set network buffer size.\n"));
74       return 0;
75    }
76
77    Dmsg1(20, "Begin append device=%s\n", dev_name(dev));
78
79    block = new_block(dev);
80
81    /* 
82     * Acquire output device for writing.  Note, after acquiring a
83     *   device, we MUST release it, which is done at the end of this
84     *   subroutine.
85     */
86    Dmsg0(100, "just before acquire_device\n");
87    if (!acquire_device_for_append(jcr, dev, block)) {
88       jcr->JobStatus = JS_Cancelled;
89       free_block(block);
90       return 0;
91    }
92    sm_check(__FILE__, __LINE__, False);
93
94    Dmsg0(100, "Just after acquire_device_for_append\n");
95    /*
96     * Write Begin Session Record
97     */
98    if (!write_session_label(jcr, block, SOS_LABEL)) {
99       jcr->JobStatus = JS_Cancelled;
100       Jmsg1(jcr, M_FATAL, 0, _("Write session label failed. ERR=%s\n"),
101          strerror_dev(dev));
102       ok = FALSE;
103    }
104
105    sm_check(__FILE__, __LINE__, False);
106
107    memset(&rec, 0, sizeof(rec));
108
109    /* 
110     * Get Data from File daemon, write to device   
111     */
112    jcr->VolFirstFile = 0;
113    time(&jcr->run_time);              /* start counting time for rates */
114    for (last_file_index = 0; ok && !job_cancelled(jcr); ) {
115       char info[100];
116
117       /* Read Stream header from the File daemon.
118        *  The stream header consists of the following:
119        *    file_index (sequential Bacula file index)
120        *    stream     (arbitrary Bacula number to distinguish parts of data)
121        *    info       (Info for Storage daemon -- compressed, encryped, ...)
122        */
123       if ((n=bget_msg(ds)) < 0) { 
124          Jmsg1(jcr, M_FATAL, 0, _("Error reading data header from FD. ERR=%s\n"),
125             bnet_strerror(ds));
126          ok = FALSE;
127          break;
128       }
129       if (n == 0 || job_cancelled(jcr)) {
130          break;                       /* all done */
131       }
132       sm_check(__FILE__, __LINE__, False);
133       ds->msg[ds->msglen] = 0;
134       if (sscanf(ds->msg, "%ld %ld %100s", &file_index, &stream, info) != 3) {
135          Jmsg1(jcr, M_FATAL, 0, _("Malformed data header from FD: %s\n"), ds->msg);
136          ok = FALSE;
137          break;
138       }
139       Dmsg2(190, "<filed: Header FilInx=%d stream=%d\n", file_index, stream);
140
141       if (!(file_index > 0 && (file_index == last_file_index ||
142           file_index == last_file_index + 1))) {
143          Jmsg0(jcr, M_FATAL, 0, _("File index from FD not positive or sequential\n"));
144          ok = FALSE;
145          break;
146       }
147       if (file_index != last_file_index) {
148          jcr->JobFiles = file_index;
149          if (jcr->VolFirstFile == 0) {
150             jcr->VolFirstFile = file_index;
151          }
152          last_file_index = file_index;
153       }
154       
155       /* Read data stream from the File daemon.
156        *  The data stream is just raw bytes
157        */
158       sm_check(__FILE__, __LINE__, False);
159       while ((n=bget_msg(ds)) > 0 && !job_cancelled(jcr)) {
160
161          sm_check(__FILE__, __LINE__, False);
162          rec.VolSessionId = jcr->VolSessionId;
163          rec.VolSessionTime = jcr->VolSessionTime;
164          rec.FileIndex = file_index;
165          rec.Stream = stream;
166          rec.data_len = ds->msglen;
167          rec.data = ds->msg;            /* use message buffer */
168
169          Dmsg4(250, "before writ_rec FI=%d SessId=%d Strm=%s len=%d\n",
170             rec.FileIndex, rec.VolSessionId, stream_to_ascii(rec.Stream), 
171             rec.data_len);
172           
173          while (!write_record_to_block(block, &rec)) {
174             Dmsg2(150, "!write_record_to_block data_len=%d rem=%d\n", rec.data_len,
175                        rec.remainder);
176             if (!write_block_to_device(jcr, dev, block)) {
177                Dmsg2(90, "Got write_block_to_dev error on device %s. %s\n",
178                   dev_name(dev), strerror_dev(dev));
179                Jmsg(jcr, M_FATAL, 0, _("Cannot fixup device error. %s\n"),
180                      strerror_dev(dev));
181                ok = FALSE;
182                break;
183             }
184          }
185          sm_check(__FILE__, __LINE__, False);
186          if (!ok) {
187             Dmsg0(400, "Not OK\n");
188             break;
189          }
190          jcr->JobBytes += rec.data_len;   /* increment bytes this job */
191          Dmsg4(190, "write_record FI=%s SessId=%d Strm=%s len=%d\n",
192             FI_to_ascii(rec.FileIndex), rec.VolSessionId, 
193             stream_to_ascii(rec.Stream), rec.data_len);
194          /* Send attributes and MD5 to Director for Catalog */
195          if (stream == STREAM_UNIX_ATTRIBUTES || stream == STREAM_MD5_SIGNATURE) {
196             if (!jcr->no_attributes) {
197                if (jcr->spool_attributes && jcr->dir_bsock->spool_fd) {
198                   jcr->dir_bsock->spool = 1;
199                }
200                if (!dir_update_file_attributes(jcr, &rec)) {
201                   Jmsg(jcr, M_FATAL, 0, _("Error updating file attributes. ERR=%s\n"),
202                      bnet_strerror(jcr->dir_bsock));
203                   ok = FALSE;
204                   jcr->dir_bsock->spool = 0;
205                   break;
206                }
207                jcr->dir_bsock->spool = 0;
208             }
209          }
210          sm_check(__FILE__, __LINE__, False);
211       }
212       if (n < 0) {
213          Jmsg1(jcr, M_FATAL, 0, _("Network error on data channel. ERR=%s\n"),
214             bnet_strerror(ds));
215          ok = FALSE;
216          break;
217       }
218    }
219    /* 
220     *******FIXME***** we should put the ok status in the End of
221     * session label 
222     *
223     *   We probably need a new flag that says "Do not attempt
224     *   to write because there is no tape".
225     */
226    sm_check(__FILE__, __LINE__, False);
227    Dmsg0(90, "Write_end_session_label()\n");
228    if (!write_session_label(jcr, block, EOS_LABEL)) {
229       Jmsg1(jcr, M_FATAL, 0, _("Error writting end session label. ERR=%s\n"),
230           strerror_dev(dev));
231       ok = FALSE;
232    }
233    /* Write out final block of this session */
234    if (!write_block_to_device(jcr, dev, block)) {
235       Pmsg0(000, "Set ok=FALSE after write_block_to_device.\n");
236       ok = FALSE;
237    }
238
239    /* Release the device */
240    if (!release_device(jcr, dev, block)) {
241       Pmsg0(000, "Error in release_device\n");
242       ok = FALSE;
243    }
244
245    free_block(block);
246
247    if (jcr->spool_attributes && jcr->dir_bsock->spool_fd) {
248       bnet_despool(jcr->dir_bsock);
249       close_spool_file(jcr, jcr->dir_bsock);
250    }
251
252    Dmsg0(90, "return from do_append_data()\n");
253    return ok ? 1 : 0;
254 }