]> git.sur5r.net Git - bacula/bacula/blob - bacula/src/stored/spool.c
Fix new FileSet exclusion
[bacula/bacula] / bacula / src / stored / spool.c
1 /*
2  *  Spooling code 
3  *
4  *      Kern Sibbald, March 2004
5  *
6  *  Version $Id$
7  */
8 /*
9    Copyright (C) 2000-2004 Kern Sibbald and John Walker
10
11    This program is free software; you can redistribute it and/or
12    modify it under the terms of the GNU General Public License as
13    published by the Free Software Foundation; either version 2 of
14    the License, or (at your option) any later version.
15
16    This program is distributed in the hope that it will be useful,
17    but WITHOUT ANY WARRANTY; without even the implied warranty of
18    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
19    General Public License for more details.
20
21    You should have received a copy of the GNU General Public
22    License along with this program; if not, write to the Free
23    Software Foundation, Inc., 59 Temple Place - Suite 330, Boston,
24    MA 02111-1307, USA.
25
26  */
27
28 #include "bacula.h"
29 #include "stored.h"
30
31 /* Forward referenced subroutines */
32 static void make_unique_data_spool_filename(JCR *jcr, POOLMEM **name);
33 static bool open_data_spool_file(JCR *jcr);
34 static bool close_data_spool_file(JCR *jcr);
35 static bool despool_data(DCR *dcr, bool commit);
36 static int  read_block_from_spool_file(DCR *dcr, DEV_BLOCK *block);
37 static bool open_attr_spool_file(JCR *jcr, BSOCK *bs);
38 static bool close_attr_spool_file(JCR *jcr, BSOCK *bs);
39 static bool write_spool_header(DCR *dcr, DEV_BLOCK *block);
40 static bool write_spool_data(DCR *dcr, DEV_BLOCK *block);
41
42 struct spool_stats_t {
43    uint32_t data_jobs;                /* current jobs spooling data */
44    uint32_t attr_jobs;                
45    uint32_t total_data_jobs;          /* total jobs to have spooled data */
46    uint32_t total_attr_jobs;
47    int64_t max_data_size;             /* max data size */
48    int64_t max_attr_size;
49    int64_t data_size;                 /* current data size (all jobs running) */
50    int64_t attr_size;
51 };
52
53 static pthread_mutex_t mutex = PTHREAD_MUTEX_INITIALIZER;
54 spool_stats_t spool_stats;
55
56 /* 
57  * Header for data spool record */
58 struct spool_hdr {
59    int32_t  FirstIndex;               /* FirstIndex for buffer */
60    int32_t  LastIndex;                /* LastIndex for buffer */
61    uint32_t len;                      /* length of next buffer */
62 };
63
64 enum {
65    RB_EOT = 1,
66    RB_ERROR,
67    RB_OK
68 };
69
70 void list_spool_stats(BSOCK *bs)
71 {
72    char ed1[30], ed2[30];
73    if (spool_stats.data_jobs || spool_stats.max_data_size) {
74       bnet_fsend(bs, "Data spooling: %u active jobs, %s bytes; %u total jobs, %s max bytes/job.\n",
75          spool_stats.data_jobs, edit_uint64_with_commas(spool_stats.data_size, ed1),
76          spool_stats.total_data_jobs, 
77          edit_uint64_with_commas(spool_stats.max_data_size, ed2));
78    }
79    if (spool_stats.attr_jobs || spool_stats.max_attr_size) {
80       bnet_fsend(bs, "Attr spooling: %u active jobs, %s bytes; %u total jobs, %s max bytes.\n",
81          spool_stats.attr_jobs, edit_uint64_with_commas(spool_stats.attr_size, ed1), 
82          spool_stats.total_attr_jobs, 
83          edit_uint64_with_commas(spool_stats.max_attr_size, ed2));
84    }
85 }
86
87 bool begin_data_spool(JCR *jcr)
88 {
89    bool stat = true;
90    if (jcr->spool_data) {
91       Dmsg0(100, "Turning on data spooling\n");
92       jcr->dcr->spool_data = true;
93       stat = open_data_spool_file(jcr);
94       if (stat) {
95          jcr->dcr->spooling = true;
96          Jmsg(jcr, M_INFO, 0, _("Spooling data ...\n"));
97          P(mutex);
98          spool_stats.data_jobs++;
99          V(mutex);
100       }
101    }
102    return stat;
103 }
104
105 bool discard_data_spool(JCR *jcr)
106 {
107    if (jcr->dcr->spooling) {
108       Dmsg0(100, "Data spooling discarded\n");
109       return close_data_spool_file(jcr);
110    }
111    return true;
112 }
113
114 bool commit_data_spool(JCR *jcr)
115 {
116    bool stat;
117
118    if (jcr->dcr->spooling) {
119       Dmsg0(100, "Committing spooled data\n");
120       stat = despool_data(jcr->dcr, true /*commit*/);
121       if (!stat) {
122          Dmsg1(000, "Bad return from despool WroteVol=%d\n", jcr->dcr->WroteVol);
123          close_data_spool_file(jcr);
124          return false;
125       }
126       return close_data_spool_file(jcr);
127    }
128    return true;
129 }
130
131 static void make_unique_data_spool_filename(JCR *jcr, POOLMEM **name)
132 {
133    const char *dir;  
134    if (jcr->dcr->dev->device->spool_directory) {
135       dir = jcr->dcr->dev->device->spool_directory;
136    } else {
137       dir = working_directory;
138    }
139    Mmsg(name, "%s/%s.data.spool.%s.%s", dir, my_name, jcr->Job, jcr->device->hdr.name);
140 }
141
142
143 static bool open_data_spool_file(JCR *jcr)
144 {
145    POOLMEM *name  = get_pool_memory(PM_MESSAGE);
146    int spool_fd;
147
148    make_unique_data_spool_filename(jcr, &name);
149    if ((spool_fd = open(name, O_CREAT|O_TRUNC|O_RDWR|O_BINARY, 0640)) >= 0) {
150       jcr->dcr->spool_fd = spool_fd;
151       jcr->spool_attributes = true;
152    } else {
153       Jmsg(jcr, M_ERROR, 0, _("Open data spool file %s failed: ERR=%s\n"), name, strerror(errno));
154       free_pool_memory(name);
155       return false;
156    }
157    Dmsg1(100, "Created spool file: %s\n", name);
158    free_pool_memory(name);
159    return true;
160 }
161
162 static bool close_data_spool_file(JCR *jcr)
163 {
164    POOLMEM *name  = get_pool_memory(PM_MESSAGE);
165
166    P(mutex);
167    spool_stats.data_jobs--;
168    spool_stats.total_data_jobs++;
169    if (spool_stats.data_size < jcr->dcr->spool_size) {
170       spool_stats.data_size = 0;
171    } else {
172       spool_stats.data_size -= jcr->dcr->spool_size;
173    }
174    jcr->dcr->spool_size = 0;
175    V(mutex);
176
177    make_unique_data_spool_filename(jcr, &name);
178    close(jcr->dcr->spool_fd);
179    jcr->dcr->spool_fd = -1;
180    jcr->dcr->spooling = false;
181    unlink(name);
182    Dmsg1(100, "Deleted spool file: %s\n", name);
183    free_pool_memory(name);
184    return true;
185 }
186
187 static const char *spool_name = "*spool*";
188
189 static bool despool_data(DCR *dcr, bool commit) 
190 {
191    DEVICE *rdev;
192    DCR *rdcr;
193    bool ok = true;
194    DEV_BLOCK *block;
195    JCR *jcr = dcr->jcr;
196    int stat;
197    char ec1[50];
198
199    Dmsg0(100, "Despooling data\n");
200    Jmsg(jcr, M_INFO, 0, _("%s spooled data to Volume. Despooling %s bytes ...\n"),
201         commit?"Committing":"Writting",
202         edit_uint64_with_commas(jcr->dcr->dev->spool_size, ec1));
203    dcr->spooling = false;
204    lock_device(dcr->dev);
205    dcr->dev_locked = true; 
206
207    /* 
208     * This is really quite kludgy and should be fixed some time.
209     * We create a dev structure to read from the spool file 
210     * in rdev and rdcr.
211     */
212    rdev = (DEVICE *)malloc(sizeof(DEVICE));
213    memset(rdev, 0, sizeof(DEVICE));
214    rdev->dev_name = get_memory(strlen(spool_name)+1);
215    strcpy(rdev->dev_name, spool_name);
216    rdev->errmsg = get_pool_memory(PM_EMSG);
217    *rdev->errmsg = 0;
218    rdev->max_block_size = dcr->dev->max_block_size;
219    rdev->min_block_size = dcr->dev->min_block_size;
220    rdev->device = dcr->dev->device;
221    rdcr = new_dcr(NULL, rdev);
222    rdcr->spool_fd = dcr->spool_fd; 
223    rdcr->jcr = jcr;                   /* set a valid jcr */
224    block = rdcr->block;
225
226    Dmsg1(800, "read/write block size = %d\n", block->buf_len);
227    lseek(rdcr->spool_fd, 0, SEEK_SET); /* rewind */
228
229    for ( ; ok; ) {
230       if (job_canceled(jcr)) {
231          ok = false;
232          break;
233       }
234       stat = read_block_from_spool_file(rdcr, block);
235       if (stat == RB_EOT) {
236          break;
237       } else if (stat == RB_ERROR) {
238          ok = false;
239          break;
240       }
241       ok = write_block_to_device(dcr, block);
242       Dmsg3(100, "Write block ok=%d FI=%d LI=%d\n", ok, block->FirstIndex, block->LastIndex);
243    }
244
245    lseek(rdcr->spool_fd, 0, SEEK_SET); /* rewind */
246    if (ftruncate(rdcr->spool_fd, 0) != 0) {
247       Jmsg(dcr->jcr, M_FATAL, 0, _("Ftruncate spool file error. ERR=%s\n"), 
248          strerror(errno));
249       Dmsg1(000, "Bad return from ftruncate. ERR=%s\n", strerror(errno));
250       ok = false;
251    }
252
253    P(mutex);
254    if (spool_stats.data_size < dcr->spool_size) {
255       spool_stats.data_size = 0;
256    } else {
257       spool_stats.data_size -= dcr->spool_size;
258    }
259    V(mutex);
260    P(dcr->dev->spool_mutex);
261    dcr->dev->spool_size -= dcr->spool_size;
262    dcr->spool_size = 0;               /* zap size in input dcr */
263    V(dcr->dev->spool_mutex);
264    free_memory(rdev->dev_name);
265    free_pool_memory(rdev->errmsg);
266    /* Be careful to NULL the jcr and free rdev after free_dcr() */
267    rdcr->jcr = NULL;
268    free_dcr(rdcr);
269    free(rdev);
270    unlock_device(dcr->dev);
271    dcr->dev_locked = false;
272    dcr->spooling = true;           /* turn on spooling again */
273    return ok;
274 }
275
276 /*
277  * Read a block from the spool file
278  * 
279  *  Returns RB_OK on success
280  *          RB_EOT when file done
281  *          RB_ERROR on error
282  */
283 static int read_block_from_spool_file(DCR *dcr, DEV_BLOCK *block)
284 {
285    uint32_t rlen;
286    ssize_t stat;
287    spool_hdr hdr;
288
289    rlen = sizeof(hdr);
290    stat = read(dcr->spool_fd, (char *)&hdr, (size_t)rlen);
291    if (stat == 0) {
292       Dmsg0(100, "EOT on spool read.\n");
293       return RB_EOT;
294    } else if (stat != (ssize_t)rlen) {
295       if (stat == -1) {
296          Jmsg(dcr->jcr, M_FATAL, 0, _("Spool header read error. ERR=%s\n"), strerror(errno));
297       } else {
298          Dmsg2(000, "Spool read error. Wanted %u bytes, got %u\n", rlen, stat);
299          Jmsg2(dcr->jcr, M_FATAL, 0, _("Spool header read error. Wanted %u bytes, got %u\n"), rlen, stat);
300       }
301       return RB_ERROR;
302    }
303    rlen = hdr.len;
304    if (rlen > block->buf_len) {
305       Dmsg2(000, "Spool block too big. Max %u bytes, got %u\n", block->buf_len, rlen);
306       Jmsg2(dcr->jcr, M_FATAL, 0, _("Spool block too big. Max %u bytes, got %u\n"), block->buf_len, rlen);
307       return RB_ERROR;
308    }
309    stat = read(dcr->spool_fd, (char *)block->buf, (size_t)rlen);
310    if (stat != (ssize_t)rlen) {
311       Dmsg2(000, "Spool data read error. Wanted %u bytes, got %u\n", rlen, stat);
312       Jmsg2(dcr->jcr, M_FATAL, 0, _("Spool data read error. Wanted %u bytes, got %u\n"), rlen, stat);
313       return RB_ERROR;
314    }
315    /* Setup write pointers */
316    block->binbuf = rlen;
317    block->bufp = block->buf + block->binbuf;
318    block->FirstIndex = hdr.FirstIndex;
319    block->LastIndex = hdr.LastIndex;
320    block->VolSessionId = dcr->jcr->VolSessionId;
321    block->VolSessionTime = dcr->jcr->VolSessionTime;
322    Dmsg2(100, "Read block FI=%d LI=%d\n", block->FirstIndex, block->LastIndex);
323    return RB_OK;
324 }
325
326 /*
327  * Write a block to the spool file
328  *
329  *  Returns: true on success or EOT
330  *           false on hard error
331  */
332 bool write_block_to_spool_file(DCR *dcr, DEV_BLOCK *block)
333 {
334    uint32_t wlen, hlen;               /* length to write */
335    bool despool = false;
336
337    ASSERT(block->binbuf == ((uint32_t) (block->bufp - block->buf)));
338    if (block->binbuf <= WRITE_BLKHDR_LENGTH) {  /* Does block have data in it? */
339       return true;
340    }
341
342    hlen = sizeof(spool_hdr);
343    wlen = block->binbuf;
344    P(dcr->dev->spool_mutex);
345    dcr->spool_size += hlen + wlen;
346    dcr->dev->spool_size += hlen + wlen;
347    if ((dcr->max_spool_size > 0 && dcr->spool_size >= dcr->max_spool_size) ||
348        (dcr->dev->max_spool_size > 0 && dcr->dev->spool_size >= dcr->dev->max_spool_size)) {
349       despool = true;
350    }
351    V(dcr->dev->spool_mutex);
352    P(mutex);
353    spool_stats.data_size += hlen + wlen;
354    if (spool_stats.data_size > spool_stats.max_data_size) {
355       spool_stats.max_data_size = spool_stats.data_size;
356    }
357    V(mutex);
358    if (despool) {
359 #ifdef xDEBUG 
360       char ec1[30], ec2[30], ec3[30], ec4[30];
361       Dmsg4(100, "Despool in write_block_to_spool_file max_size=%s size=%s "
362             "max_job_size=%s job_size=%s\n", 
363             edit_uint64_with_commas(dcr->max_spool_size, ec1),
364             edit_uint64_with_commas(dcr->spool_size, ec2),
365             edit_uint64_with_commas(dcr->dev->max_spool_size, ec3),
366             edit_uint64_with_commas(dcr->dev->spool_size, ec4));
367 #endif
368       Jmsg(dcr->jcr, M_INFO, 0, _("User specified spool size reached.\n"));
369       if (!despool_data(dcr, false)) {
370          Dmsg0(000, "Bad return from despool in write_block.\n");
371          return false;
372       }
373       /* Despooling cleared these variables so reset them */
374       P(dcr->dev->spool_mutex);
375       dcr->spool_size += hlen + wlen;
376       dcr->dev->spool_size += hlen + wlen;
377       V(dcr->dev->spool_mutex);
378       Jmsg(dcr->jcr, M_INFO, 0, _("Spooling data again ...\n"));
379    }  
380
381
382    if (!write_spool_header(dcr, block)) {
383       return false;
384    }
385    if (!write_spool_data(dcr, block)) {
386      return false;
387    }
388
389    Dmsg2(100, "Wrote block FI=%d LI=%d\n", block->FirstIndex, block->LastIndex);
390    empty_block(block);
391    return true;
392 }
393
394 static bool write_spool_header(DCR *dcr, DEV_BLOCK *block)
395 {
396    spool_hdr hdr;   
397    ssize_t stat;
398
399    hdr.FirstIndex = block->FirstIndex;
400    hdr.LastIndex = block->LastIndex;
401    hdr.len = block->binbuf;
402
403    /* Write header */
404    for (int retry=0; retry<=1; retry++) {
405       stat = write(dcr->spool_fd, (char*)&hdr, sizeof(hdr));
406       if (stat == -1) {
407          Jmsg(dcr->jcr, M_INFO, 0, _("Error writing header to spool file. ERR=%s\n"), strerror(errno));
408       }
409       if (stat != (ssize_t)sizeof(hdr)) {
410          /* If we wrote something, truncate it, then despool */
411          if (stat != -1) {
412             ftruncate(dcr->spool_fd, lseek(dcr->spool_fd, (off_t)0, SEEK_CUR) - stat);
413          }
414          if (!despool_data(dcr, false)) {
415             Jmsg(dcr->jcr, M_FATAL, 0, _("Fatal despooling error."));
416             return false;
417          }
418          continue;                    /* try again */
419       }
420       return true;
421    }
422    Jmsg(dcr->jcr, M_FATAL, 0, _("Retrying after header spooling error failed.\n"));
423    return false;
424 }
425
426 static bool write_spool_data(DCR *dcr, DEV_BLOCK *block)
427 {
428    ssize_t stat;
429
430    /* Write data */
431    for (int retry=0; retry<=1; retry++) {
432       stat = write(dcr->spool_fd, block->buf, (size_t)block->binbuf);
433       if (stat == -1) {
434          Jmsg(dcr->jcr, M_INFO, 0, _("Error writing data to spool file. ERR=%s\n"), strerror(errno));
435       }
436       if (stat != (ssize_t)block->binbuf) {
437          /* 
438           * If we wrote something, truncate it and the header, then despool
439           */
440          if (stat != -1) {
441             ftruncate(dcr->spool_fd, lseek(dcr->spool_fd, (off_t)0, SEEK_CUR)
442                       - stat - sizeof(spool_hdr));
443          }
444          if (!despool_data(dcr, false)) {
445             Jmsg(dcr->jcr, M_FATAL, 0, _("Fatal despooling error."));
446             return false;
447          }
448          if (!write_spool_header(dcr, block)) {
449             return false;
450          }
451          continue;                    /* try again */
452       }
453       return true;
454    }
455    Jmsg(dcr->jcr, M_FATAL, 0, _("Retrying after data spooling error failed.\n"));
456    return false;
457 }
458
459
460
461 bool are_attributes_spooled(JCR *jcr)
462 {
463    return jcr->spool_attributes && jcr->dir_bsock->spool_fd;
464 }
465
466 /* 
467  * Create spool file for attributes.
468  *  This is done by "attaching" to the bsock, and when
469  *  it is called, the output is written to a file.
470  *  The actual spooling is turned on and off in
471  *  append.c only during writing of the attributes.
472  */
473 bool begin_attribute_spool(JCR *jcr)
474 {
475    if (!jcr->no_attributes && jcr->spool_attributes) {
476       return open_attr_spool_file(jcr, jcr->dir_bsock);
477    }
478    return true;
479 }
480
481 bool discard_attribute_spool(JCR *jcr)
482 {
483    if (are_attributes_spooled(jcr)) {
484       return close_attr_spool_file(jcr, jcr->dir_bsock);
485    }
486    return true;
487 }
488
489 static void update_attr_spool_size(ssize_t size)
490 {
491    P(mutex);
492    if (size > 0) {
493      if ((spool_stats.attr_size - size) > 0) {
494         spool_stats.attr_size -= size;
495      } else {
496         spool_stats.attr_size = 0;
497      }
498    }
499    V(mutex);
500 }
501
502 bool commit_attribute_spool(JCR *jcr)
503 {
504    ssize_t size;
505    char ec1[30];
506
507    if (are_attributes_spooled(jcr)) {
508       fseek(jcr->dir_bsock->spool_fd, 0, SEEK_END);
509       size = ftell(jcr->dir_bsock->spool_fd);
510       P(mutex);
511       if (size > 0) {
512         if (spool_stats.attr_size + size > spool_stats.max_attr_size) {
513            spool_stats.max_attr_size = spool_stats.attr_size + size;
514         } 
515       }
516       spool_stats.attr_size += size;
517       V(mutex);
518       Jmsg(jcr, M_INFO, 0, _("Sending spooled attrs to DIR. Despooling %s bytes ...\n"),
519             edit_uint64_with_commas(size, ec1));
520       bnet_despool_to_bsock(jcr->dir_bsock, update_attr_spool_size, size);
521       return close_attr_spool_file(jcr, jcr->dir_bsock);
522    }
523    return true;
524 }
525
526 static void make_unique_spool_filename(JCR *jcr, POOLMEM **name, int fd)
527 {
528    Mmsg(name, "%s/%s.attr.spool.%s.%d", working_directory, my_name,
529       jcr->Job, fd);
530 }
531
532
533 bool open_attr_spool_file(JCR *jcr, BSOCK *bs)
534 {
535    POOLMEM *name  = get_pool_memory(PM_MESSAGE);
536
537    make_unique_spool_filename(jcr, &name, bs->fd);
538    bs->spool_fd = fopen(mp_chr(name), "w+");
539    if (!bs->spool_fd) {
540       Jmsg(jcr, M_ERROR, 0, _("fopen attr spool file %s failed: ERR=%s\n"), name, strerror(errno));
541       free_pool_memory(name);
542       return false;
543    }
544    P(mutex);
545    spool_stats.attr_jobs++;
546    V(mutex);
547    free_pool_memory(name);
548    return true;
549 }
550
551 bool close_attr_spool_file(JCR *jcr, BSOCK *bs)
552 {
553    POOLMEM *name;
554     
555    if (!bs->spool_fd) {
556       return true;
557    }
558    name = get_pool_memory(PM_MESSAGE);
559    P(mutex);
560    spool_stats.attr_jobs--;
561    spool_stats.total_attr_jobs++;
562    V(mutex);
563    make_unique_spool_filename(jcr, &name, bs->fd);
564    fclose(bs->spool_fd);
565    unlink(mp_chr(name));
566    free_pool_memory(name);
567    bs->spool_fd = NULL;
568    bs->spool = false;
569    return true;
570 }