]> git.sur5r.net Git - bacula/bacula/blob - bacula/src/filed/backup.c
61fe9c79e00b05a4e228b4542533e9ee5abdfd33
[bacula/bacula] / bacula / src / filed / backup.c
1 /*
2  *  Bacula File Daemon  backup.c  send file attributes and data
3  *   to the Storage daemon.
4  *
5  *    Kern Sibbald, March MM
6  *
7  *   Version $Id$
8  *
9  */
10 /*
11    Copyright (C) 2000-2003 Kern Sibbald and John Walker
12
13    This program is free software; you can redistribute it and/or
14    modify it under the terms of the GNU General Public License as
15    published by the Free Software Foundation; either version 2 of
16    the License, or (at your option) any later version.
17
18    This program is distributed in the hope that it will be useful,
19    but WITHOUT ANY WARRANTY; without even the implied warranty of
20    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
21    General Public License for more details.
22
23    You should have received a copy of the GNU General Public
24    License along with this program; if not, write to the Free
25    Software Foundation, Inc., 59 Temple Place - Suite 330, Boston,
26    MA 02111-1307, USA.
27
28  */
29
30 #include "bacula.h"
31 #include "filed.h"
32
33 static int save_file(FF_PKT *ff_pkt, void *pkt);
34
35 static void *heartbeat_thread(void *arg)
36 {
37    int32_t n;
38    JCR *jcr = (JCR *)arg;
39    BSOCK *sd, *dir;
40
41    jcr->heartbeat_id = pthread_self();
42
43    /* Get our own local copy */
44    sd = dup_bsock(jcr->store_bsock);
45    dir = dup_bsock(jcr->dir_bsock);
46
47    jcr->duped_sd = sd;
48
49    /* Hang reading the socket to the SD, and every time we get
50     *   a heartbeat, we simply send it on to the Director to
51     *   keep him alive.
52     */
53    for ( ;; ) {
54       n = bnet_recv(sd);
55       if (is_bnet_stop(sd)) {
56          break;
57       }
58       if (n == BNET_SIGNAL && sd->msglen == BNET_HEARTBEAT) {
59          bnet_sig(dir, BNET_HEARTBEAT);
60       }
61    }
62    bnet_close(sd);
63    bnet_close(dir);
64    return NULL;
65 }
66
67 /* Startup the heartbeat thread -- see above */
68 static void start_heartbeat_monitor(JCR *jcr)
69 {
70    pthread_t hbtid;
71    jcr->duped_sd = NULL;
72    pthread_create(&hbtid, NULL, heartbeat_thread, (void *)jcr);
73 }
74
75 /* Terminate the heartbeat thread */
76 static void stop_heartbeat_monitor(JCR *jcr) 
77 {
78    pthread_t hbtid = jcr->heartbeat_id;
79
80    while (jcr->duped_sd == NULL) {
81       bmicrosleep(0, 500);            /* avoid race */
82    }
83    jcr->duped_sd->timed_out = 1;      /* set timed_out to terminate read */
84
85    pthread_kill(hbtid, TIMEOUT_SIGNAL);  /* make heartbeat thread go away */
86    pthread_join(hbtid, NULL);         /* wait for him to clean up */
87 }
88
89 /* 
90  * Find all the requested files and send them
91  * to the Storage daemon. 
92  *
93  * Note, we normally carry on a one-way
94  * conversation from this point on with the SD, simply blasting
95  * data to him.  To properly know what is going on, we
96  * also run a "heartbeat" monitor which reads the socket and
97  * reacts accordingly (at the moment it has nothing to do
98  * except echo the heartbeat to the Director).
99  * 
100  */
101 int blast_data_to_storage_daemon(JCR *jcr, char *addr) 
102 {
103    BSOCK *sd;
104    int stat = 1;
105
106    sd = jcr->store_bsock;
107
108    set_jcr_job_status(jcr, JS_Running);
109
110    Dmsg1(110, "bfiled: opened data connection %d to stored\n", sd->fd);
111
112    if (!bnet_set_buffer_size(sd, MAX_NETWORK_BUFFER_SIZE, BNET_SETBUF_WRITE)) {
113       set_jcr_job_status(jcr, JS_ErrorTerminated);
114       return 0;
115    }
116
117    jcr->buf_size = sd->msglen;             
118    /* Adjust for compression so that output buffer is
119     * 12 bytes + 0.1% larger than input buffer plus 18 bytes.
120     * This gives a bit extra plus room for the sparse addr if any.
121     * Note, we adjust the read size to be smaller so that the
122     * same output buffer can be used without growing it.
123     */
124    jcr->compress_buf_size = jcr->buf_size + ((jcr->buf_size+999) / 1000) + 30;
125    jcr->compress_buf = get_memory(jcr->compress_buf_size);
126
127    Dmsg1(100, "set_find_options ff=%p\n", jcr->ff);
128    set_find_options((FF_PKT *)jcr->ff, jcr->incremental, jcr->mtime);
129    Dmsg0(110, "start find files\n");
130
131    start_heartbeat_monitor(jcr);
132
133    /* Subroutine save_file() is called for each file */
134    if (!find_files(jcr, (FF_PKT *)jcr->ff, save_file, (void *)jcr)) {
135       stat = 0;                       /* error */
136       set_jcr_job_status(jcr, JS_ErrorTerminated);
137    }
138
139    stop_heartbeat_monitor(jcr);
140
141    bnet_sig(sd, BNET_EOD);            /* end data connection */
142
143    if (jcr->big_buf) {
144       free(jcr->big_buf);
145       jcr->big_buf = NULL;
146    }
147    if (jcr->compress_buf) {
148       free_pool_memory(jcr->compress_buf);
149       jcr->compress_buf = NULL;
150    }
151    return stat;
152 }          
153
154 /* 
155  * Called here by find() for each file included.
156  *
157  *  *****FIXME*****   add FSMs File System Modules
158  *
159  *  Send the file and its data to the Storage daemon.
160  */
161 static int save_file(FF_PKT *ff_pkt, void *vjcr)
162 {
163    char attribs[MAXSTRING];
164    char attribsEx[MAXSTRING];
165    int stat, stream; 
166    struct MD5Context md5c;
167    struct SHA1Context sha1c;
168    int gotMD5 = 0;
169    int gotSHA1 = 0;
170    unsigned char signature[30];       /* large enough for either signature */
171    BSOCK *sd;
172    JCR *jcr = (JCR *)vjcr;
173    POOLMEM *msgsave;
174
175    if (job_canceled(jcr)) {
176       return 0;
177    }
178
179    sd = jcr->store_bsock;
180    jcr->num_files_examined++;         /* bump total file count */
181
182    switch (ff_pkt->type) {
183    case FT_LNKSAVED:                  /* Hard linked, file already saved */
184       Dmsg2(130, "FT_LNKSAVED hard link: %s => %s\n", ff_pkt->fname, ff_pkt->link);
185       break;
186    case FT_REGE:
187       Dmsg1(130, "FT_REGE saving: %s\n", ff_pkt->fname);
188       break;
189    case FT_REG:
190       Dmsg1(130, "FT_REG saving: %s\n", ff_pkt->fname);
191       break;
192    case FT_LNK:
193       Dmsg2(130, "FT_LNK saving: %s -> %s\n", ff_pkt->fname, ff_pkt->link);
194       break;
195    case FT_DIR:
196       Dmsg1(130, "FT_DIR saving: %s\n", ff_pkt->link);
197       break;
198    case FT_SPEC:
199       Dmsg1(130, "FT_SPEC saving: %s\n", ff_pkt->fname);
200       break;
201    case FT_RAW:
202       Dmsg1(130, "FT_RAW saving: %s\n", ff_pkt->fname);
203       break;
204    case FT_FIFO:
205       Dmsg1(130, "FT_FIFO saving: %s\n", ff_pkt->fname);
206       break;
207    case FT_NOACCESS:
208       Jmsg(jcr, M_NOTSAVED, -1, _("     Could not access %s: ERR=%s\n"), ff_pkt->fname, 
209          strerror(ff_pkt->ff_errno));
210       return 1;
211    case FT_NOFOLLOW:
212       Jmsg(jcr, M_NOTSAVED, -1, _("     Could not follow link %s: ERR=%s\n"), ff_pkt->fname, 
213          strerror(ff_pkt->ff_errno));
214       return 1;
215    case FT_NOSTAT:
216       Jmsg(jcr, M_NOTSAVED, -1, _("     Could not stat %s: ERR=%s\n"), ff_pkt->fname, 
217          strerror(ff_pkt->ff_errno));
218       return 1;
219    case FT_DIRNOCHG:
220    case FT_NOCHG:
221       Jmsg(jcr, M_SKIPPED, -1,  _("     Unchanged file skipped: %s\n"), ff_pkt->fname);
222       return 1;
223    case FT_ISARCH:
224       Jmsg(jcr, M_NOTSAVED, -1, _("     Archive file not saved: %s\n"), ff_pkt->fname);
225       return 1;
226    case FT_NORECURSE:
227       Jmsg(jcr, M_SKIPPED, -1,  _("     Recursion turned off. Directory skipped: %s\n"), 
228          ff_pkt->fname);
229       return 1;
230    case FT_NOFSCHG:
231       Jmsg(jcr, M_SKIPPED, -1,  _("     File system change prohibited. Directory skipped. %s\n"), 
232          ff_pkt->fname);
233       return 1;
234    case FT_NOOPEN:
235       Jmsg(jcr, M_NOTSAVED, -1, _("     Could not open directory %s: ERR=%s\n"), ff_pkt->fname, 
236          strerror(ff_pkt->ff_errno));
237       return 1;
238    default:
239       Jmsg(jcr, M_NOTSAVED, 0,  _("     Unknown file type %d; not saved: %s\n"), ff_pkt->type, ff_pkt->fname);
240       return 1;
241    }
242
243    binit(&ff_pkt->bfd, jcr->use_win_backup_api);
244
245    /* Open any file with data that we intend to save */
246    if (ff_pkt->type != FT_LNKSAVED && (S_ISREG(ff_pkt->statp.st_mode) && 
247          ff_pkt->statp.st_size > 0) || 
248          ff_pkt->type == FT_RAW || ff_pkt->type == FT_FIFO) {
249       btimer_id tid;    
250       if (ff_pkt->type == FT_FIFO) {
251          tid = start_thread_timer(pthread_self(), 60);
252       } else {
253          tid = NULL;
254       }
255       if (bopen(&ff_pkt->bfd, ff_pkt->fname, O_RDONLY | O_BINARY, 0) < 0) {
256          ff_pkt->ff_errno = errno;
257          Jmsg(jcr, M_NOTSAVED, -1, _("     Cannot open %s: ERR=%s.\n"), ff_pkt->fname, 
258               berror(&ff_pkt->bfd));
259          stop_thread_timer(tid);
260          return 1;
261       }
262       stop_thread_timer(tid);
263    }
264
265    Dmsg1(130, "bfiled: sending %s to stored\n", ff_pkt->fname);
266    encode_stat(attribs, &ff_pkt->statp, ff_pkt->LinkFI);
267    stream = encode_attribsEx(jcr, attribsEx, ff_pkt);
268    Dmsg3(200, "File %s\nattribs=%s\nattribsEx=%s\n", ff_pkt->fname, attribs, attribsEx);
269      
270    P(jcr->mutex);
271    jcr->JobFiles++;                    /* increment number of files sent */
272    ff_pkt->FileIndex = jcr->JobFiles;  /* return FileIndex */
273    pm_strcpy(&jcr->last_fname, ff_pkt->fname);
274    V(jcr->mutex);
275     
276    /*
277     * Send Attributes header to Storage daemon
278     *    <file-index> <stream> <info>
279     */
280    if (!bnet_fsend(sd, "%ld %d 0", jcr->JobFiles, stream)) {
281       if (is_bopen(&ff_pkt->bfd)) {
282          bclose(&ff_pkt->bfd);
283       }
284       set_jcr_job_status(jcr, JS_ErrorTerminated);
285       return 0;
286    }
287    Dmsg1(100, ">stored: attrhdr %s\n", sd->msg);
288
289    /*
290     * Send file attributes to Storage daemon
291     *   File_index
292     *   File type
293     *   Filename (full path)
294     *   Encoded attributes
295     *   Link name (if type==FT_LNK or FT_LNKSAVED)
296     *   Encoded extended-attributes (for Win32)
297     *
298     * For a directory, link is the same as fname, but with trailing
299     * slash. For a linked file, link is the link.
300     */
301    if (ff_pkt->type == FT_LNK || ff_pkt->type == FT_LNKSAVED) {
302       Dmsg2(100, "Link %s to %s\n", ff_pkt->fname, ff_pkt->link);
303       stat = bnet_fsend(sd, "%ld %d %s%c%s%c%s%c%s%c", jcr->JobFiles, 
304                ff_pkt->type, ff_pkt->fname, 0, attribs, 0, ff_pkt->link, 0,
305                attribsEx, 0);
306    } else if (ff_pkt->type == FT_DIR) {
307       /* Here link is the canonical filename (i.e. with trailing slash) */
308       stat = bnet_fsend(sd, "%ld %d %s%c%s%c%c%s%c", jcr->JobFiles, 
309                ff_pkt->type, ff_pkt->link, 0, attribs, 0, 0, attribsEx, 0);
310    } else {
311       stat = bnet_fsend(sd, "%ld %d %s%c%s%c%c%s%c", jcr->JobFiles, 
312                ff_pkt->type, ff_pkt->fname, 0, attribs, 0, 0, attribsEx, 0);
313    }
314
315    Dmsg2(100, ">stored: attr len=%d: %s\n", sd->msglen, sd->msg);
316    if (!stat) {
317       if (is_bopen(&ff_pkt->bfd)) {
318          bclose(&ff_pkt->bfd);
319       }
320       set_jcr_job_status(jcr, JS_ErrorTerminated);
321       return 0;
322    }
323    bnet_sig(sd, BNET_EOD);            /* indicate end of attributes data */
324
325    /* 
326     * If the file has data, read it and send to the Storage daemon
327     *
328     */
329    if (is_bopen(&ff_pkt->bfd)) {
330       uint64_t fileAddr = 0;          /* file address */
331       char *rbuf, *wbuf;
332       int rsize = jcr->buf_size;      /* read buffer size */
333
334       msgsave = sd->msg;
335       rbuf = sd->msg;                 /* read buffer */             
336       wbuf = sd->msg;                 /* write buffer */
337
338       Dmsg1(100, "Saving data, type=%d\n", ff_pkt->type);
339
340       if (ff_pkt->flags & FO_SPARSE) {
341          stream = STREAM_SPARSE_DATA;
342       } else {
343          stream = STREAM_FILE_DATA;
344       }
345
346 #ifdef HAVE_LIBZ
347       uLong compress_len, max_compress_len = 0;
348       const Bytef *cbuf = NULL;
349
350       if (ff_pkt->flags & FO_GZIP) {
351          if (stream == STREAM_FILE_DATA) {
352             stream = STREAM_GZIP_DATA;
353          } else {
354             stream = STREAM_SPARSE_GZIP_DATA;
355          }
356
357          if (ff_pkt->flags & FO_SPARSE) {
358             cbuf = (Bytef *)jcr->compress_buf + SPARSE_FADDR_SIZE;
359             max_compress_len = jcr->compress_buf_size - SPARSE_FADDR_SIZE;
360          } else {
361             cbuf = (Bytef *)jcr->compress_buf;
362             max_compress_len = jcr->compress_buf_size; /* set max length */
363          }
364          wbuf = jcr->compress_buf;    /* compressed output here */
365       }
366 #endif
367
368       /*
369        * Send Data header to Storage daemon
370        *    <file-index> <stream> <info>
371        */
372       if (!bnet_fsend(sd, "%ld %d 0", jcr->JobFiles, stream)) {
373          bclose(&ff_pkt->bfd);
374          set_jcr_job_status(jcr, JS_ErrorTerminated);
375          return 0;
376       }
377       Dmsg1(100, ">stored: datahdr %s\n", sd->msg);
378
379       if (ff_pkt->flags & FO_MD5) {
380          MD5Init(&md5c);
381       } else if (ff_pkt->flags & FO_SHA1) {
382          SHA1Init(&sha1c);
383       }
384
385       /*
386        * Make space at beginning of buffer for fileAddr because this
387        *   same buffer will be used for writing if compression if off. 
388        */
389       if (ff_pkt->flags & FO_SPARSE) {
390          rbuf += SPARSE_FADDR_SIZE;
391          rsize -= SPARSE_FADDR_SIZE;
392       }
393
394       /* 
395        * Read the file data
396        */
397       while ((sd->msglen=bread(&ff_pkt->bfd, rbuf, rsize)) > 0) {
398          int sparseBlock = 0;
399
400          /* Check for sparse blocks */
401          if (ff_pkt->flags & FO_SPARSE) {
402             ser_declare;
403             if (sd->msglen == rsize && 
404                 (fileAddr+sd->msglen < (uint64_t)ff_pkt->statp.st_size)) {
405                sparseBlock = is_buf_zero(rbuf, rsize);
406             }
407                
408             ser_begin(wbuf, SPARSE_FADDR_SIZE);
409             ser_uint64(fileAddr);     /* store fileAddr in begin of buffer */
410          } 
411
412          jcr->ReadBytes += sd->msglen;      /* count bytes read */
413          fileAddr += sd->msglen;
414
415          /* Update MD5 if requested */
416          if (ff_pkt->flags & FO_MD5) {
417             MD5Update(&md5c, (unsigned char *)rbuf, sd->msglen);
418             gotMD5 = 1;
419          } else if (ff_pkt->flags & FO_SHA1) {
420             SHA1Update(&sha1c, (unsigned char *)rbuf, sd->msglen);
421             gotSHA1 = 1;
422          }
423
424 #ifdef HAVE_LIBZ
425          /* Do compression if turned on */
426          if (!sparseBlock && ff_pkt->flags & FO_GZIP) {
427             int zstat;
428             compress_len = max_compress_len;
429             Dmsg4(400, "cbuf=0x%x len=%u rbuf=0x%x len=%u\n", cbuf, compress_len,
430                rbuf, sd->msglen);
431             /* NOTE! This call modifies compress_len !!! */
432             if ((zstat=compress2((Bytef *)cbuf, &compress_len, 
433                   (const Bytef *)rbuf, (uLong)sd->msglen,
434                   ff_pkt->GZIP_level)) != Z_OK) {
435                Jmsg(jcr, M_FATAL, 0, _("Compression error: %d\n"), zstat);
436                sd->msg = msgsave;
437                sd->msglen = 0;
438                bclose(&ff_pkt->bfd);
439                set_jcr_job_status(jcr, JS_ErrorTerminated);
440                return 0;
441             }
442             Dmsg2(400, "compressed len=%d uncompressed len=%d\n", 
443                compress_len, sd->msglen);
444
445             sd->msglen = compress_len;   /* set compressed length */
446          }
447 #endif
448
449          /*       #ifndef FD_NO_SEND_TEST */
450          /* Send the buffer to the Storage daemon */
451          if (!sparseBlock) {
452             if (ff_pkt->flags & FO_SPARSE) {
453                sd->msglen += SPARSE_FADDR_SIZE; /* include fileAddr in size */
454             }
455             sd->msg = wbuf;           /* set correct write buffer */
456             if (!bnet_send(sd)) {
457                sd->msg = msgsave;     /* restore read buffer */
458                sd->msglen = 0;
459                bclose(&ff_pkt->bfd);
460                set_jcr_job_status(jcr, JS_ErrorTerminated);
461                return 0;
462             }
463          }
464          Dmsg1(130, "Send data to SD len=%d\n", sd->msglen);
465          /*       #endif */
466          jcr->JobBytes += sd->msglen;   /* count bytes saved possibly compressed */
467          sd->msg = msgsave;             /* restore read buffer */
468
469       } /* end while read file data */
470
471
472       if (sd->msglen < 0) {
473          Jmsg(jcr, M_ERROR, 0, _("Read error on file %s. ERR=%s\n"),
474             ff_pkt->fname, berror(&ff_pkt->bfd));
475       }
476
477       bclose(&ff_pkt->bfd);              /* close file */
478       if (!bnet_sig(sd, BNET_EOD)) {     /* indicate end of file data */
479          set_jcr_job_status(jcr, JS_ErrorTerminated);
480          return 0;
481       }
482    }
483
484
485    /* Terminate any MD5 signature and send it to Storage daemon and the Director */
486    if (gotMD5 && ff_pkt->flags & FO_MD5) {
487       MD5Final(signature, &md5c);
488       bnet_fsend(sd, "%ld %d 0", jcr->JobFiles, STREAM_MD5_SIGNATURE);
489       Dmsg1(100, "bfiled>stored:header %s\n", sd->msg);
490       memcpy(sd->msg, signature, 16);
491       sd->msglen = 16;
492       bnet_send(sd);
493       bnet_sig(sd, BNET_EOD);         /* end of MD5 */
494       gotMD5 = 0;
495    } else if (gotSHA1 && ff_pkt->flags & FO_SHA1) {
496    /* Terminate any SHA1 signature and send it to Storage daemon and the Director */
497       SHA1Final(&sha1c, signature);
498       bnet_fsend(sd, "%ld %d 0", jcr->JobFiles, STREAM_SHA1_SIGNATURE);
499       Dmsg1(100, "bfiled>stored:header %s\n", sd->msg);
500       memcpy(sd->msg, signature, 20);
501       sd->msglen = 20;
502       bnet_send(sd);
503       bnet_sig(sd, BNET_EOD);         /* end of SHA1 */
504       gotMD5 = 0;
505    }
506    return 1;
507 }