]> git.sur5r.net Git - bacula/bacula/blob - bacula/src/stored/spool.c
Massive SD calling sequence reorganization
[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);
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);
40 static bool write_spool_data(DCR *dcr);
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          Pmsg1(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       berrno be;
154       Jmsg(jcr, M_FATAL, 0, _("Open data spool file %s failed: ERR=%s\n"), name,
155            be.strerror());
156       free_pool_memory(name);
157       return false;
158    }
159    Dmsg1(100, "Created spool file: %s\n", name);
160    free_pool_memory(name);
161    return true;
162 }
163
164 static bool close_data_spool_file(JCR *jcr)
165 {
166    POOLMEM *name  = get_pool_memory(PM_MESSAGE);
167
168    P(mutex);
169    spool_stats.data_jobs--;
170    spool_stats.total_data_jobs++;
171    if (spool_stats.data_size < jcr->dcr->spool_size) {
172       spool_stats.data_size = 0;
173    } else {
174       spool_stats.data_size -= jcr->dcr->spool_size;
175    }
176    jcr->dcr->spool_size = 0;
177    V(mutex);
178
179    make_unique_data_spool_filename(jcr, &name);
180    close(jcr->dcr->spool_fd);
181    jcr->dcr->spool_fd = -1;
182    jcr->dcr->spooling = false;
183    unlink(name);
184    Dmsg1(100, "Deleted spool file: %s\n", name);
185    free_pool_memory(name);
186    return true;
187 }
188
189 static const char *spool_name = "*spool*";
190
191 static bool despool_data(DCR *dcr, bool commit) 
192 {
193    DEVICE *rdev;
194    DCR *rdcr;
195    bool ok = true;
196    DEV_BLOCK *block;
197    JCR *jcr = dcr->jcr;
198    int stat;
199    char ec1[50];
200
201    Dmsg0(100, "Despooling data\n");
202    Jmsg(jcr, M_INFO, 0, _("%s spooled data to Volume. Despooling %s bytes ...\n"),
203         commit?"Committing":"Writting",
204         edit_uint64_with_commas(jcr->dcr->dev->spool_size, ec1));
205    dcr->spooling = false;
206    lock_device(dcr->dev);
207    dcr->dev_locked = true; 
208
209    /* 
210     * This is really quite kludgy and should be fixed some time.
211     * We create a dev structure to read from the spool file 
212     * in rdev and rdcr.
213     */
214    rdev = (DEVICE *)malloc(sizeof(DEVICE));
215    memset(rdev, 0, sizeof(DEVICE));
216    rdev->dev_name = get_memory(strlen(spool_name)+1);
217    bstrncpy(rdev->dev_name, spool_name, sizeof(rdev->dev_name));
218    rdev->errmsg = get_pool_memory(PM_EMSG);
219    *rdev->errmsg = 0;
220    rdev->max_block_size = dcr->dev->max_block_size;
221    rdev->min_block_size = dcr->dev->min_block_size;
222    rdev->device = dcr->dev->device;
223    rdcr = new_dcr(NULL, rdev);
224    rdcr->spool_fd = dcr->spool_fd; 
225    rdcr->jcr = jcr;                   /* set a valid jcr */
226    block = dcr->block;                /* save block */
227    dcr->block = rdcr->block;          /* make read and write block the same */
228
229    Dmsg1(800, "read/write block size = %d\n", block->buf_len);
230    lseek(rdcr->spool_fd, 0, SEEK_SET); /* rewind */
231
232    for ( ; ok; ) {
233       if (job_canceled(jcr)) {
234          ok = false;
235          break;
236       }
237       stat = read_block_from_spool_file(rdcr);
238       if (stat == RB_EOT) {
239          break;
240       } else if (stat == RB_ERROR) {
241          ok = false;
242          break;
243       }
244       ok = write_block_to_device(dcr);
245       Dmsg3(100, "Write block ok=%d FI=%d LI=%d\n", ok, block->FirstIndex, block->LastIndex);
246    }
247    dcr->block = block;                /* reset block */
248
249    lseek(rdcr->spool_fd, 0, SEEK_SET); /* rewind */
250    if (ftruncate(rdcr->spool_fd, 0) != 0) {
251       berrno be;
252       Jmsg(dcr->jcr, M_FATAL, 0, _("Ftruncate spool file failed: ERR=%s\n"), 
253          be.strerror());
254       Pmsg1(000, "Bad return from ftruncate. ERR=%s\n", be.strerror());
255       ok = false;
256    }
257
258    P(mutex);
259    if (spool_stats.data_size < dcr->spool_size) {
260       spool_stats.data_size = 0;
261    } else {
262       spool_stats.data_size -= dcr->spool_size;
263    }
264    V(mutex);
265    P(dcr->dev->spool_mutex);
266    dcr->dev->spool_size -= dcr->spool_size;
267    dcr->spool_size = 0;               /* zap size in input dcr */
268    V(dcr->dev->spool_mutex);
269    free_memory(rdev->dev_name);
270    free_pool_memory(rdev->errmsg);
271    /* Be careful to NULL the jcr and free rdev after free_dcr() */
272    rdcr->jcr = NULL;
273    free_dcr(rdcr);
274    free(rdev);
275    unlock_device(dcr->dev);
276    dcr->dev_locked = false;
277    dcr->spooling = true;           /* turn on spooling again */
278    return ok;
279 }
280
281 /*
282  * Read a block from the spool file
283  * 
284  *  Returns RB_OK on success
285  *          RB_EOT when file done
286  *          RB_ERROR on error
287  */
288 static int read_block_from_spool_file(DCR *dcr)
289 {
290    uint32_t rlen;
291    ssize_t stat;
292    spool_hdr hdr;
293    DEV_BLOCK *block = dcr->block;
294
295    rlen = sizeof(hdr);
296    stat = read(dcr->spool_fd, (char *)&hdr, (size_t)rlen);
297    if (stat == 0) {
298       Dmsg0(100, "EOT on spool read.\n");
299       return RB_EOT;
300    } else if (stat != (ssize_t)rlen) {
301       if (stat == -1) {
302          berrno be;
303          Jmsg(dcr->jcr, M_FATAL, 0, _("Spool header read error. ERR=%s\n"), 
304               be.strerror());
305       } else {
306          Pmsg2(000, "Spool read error. Wanted %u bytes, got %u\n", rlen, stat);
307          Jmsg2(dcr->jcr, M_FATAL, 0, _("Spool header read error. Wanted %u bytes, got %u\n"), rlen, stat);
308       }
309       return RB_ERROR;
310    }
311    rlen = hdr.len;
312    if (rlen > block->buf_len) {
313       Pmsg2(000, "Spool block too big. Max %u bytes, got %u\n", block->buf_len, rlen);
314       Jmsg2(dcr->jcr, M_FATAL, 0, _("Spool block too big. Max %u bytes, got %u\n"), block->buf_len, rlen);
315       return RB_ERROR;
316    }
317    stat = read(dcr->spool_fd, (char *)block->buf, (size_t)rlen);
318    if (stat != (ssize_t)rlen) {
319       Pmsg2(000, "Spool data read error. Wanted %u bytes, got %u\n", rlen, stat);
320       Jmsg2(dcr->jcr, M_FATAL, 0, _("Spool data read error. Wanted %u bytes, got %u\n"), rlen, stat);
321       return RB_ERROR;
322    }
323    /* Setup write pointers */
324    block->binbuf = rlen;
325    block->bufp = block->buf + block->binbuf;
326    block->FirstIndex = hdr.FirstIndex;
327    block->LastIndex = hdr.LastIndex;
328    block->VolSessionId = dcr->jcr->VolSessionId;
329    block->VolSessionTime = dcr->jcr->VolSessionTime;
330    Dmsg2(100, "Read block FI=%d LI=%d\n", block->FirstIndex, block->LastIndex);
331    return RB_OK;
332 }
333
334 /*
335  * Write a block to the spool file
336  *
337  *  Returns: true on success or EOT
338  *           false on hard error
339  */
340 bool write_block_to_spool_file(DCR *dcr)
341 {
342    uint32_t wlen, hlen;               /* length to write */
343    bool despool = false;
344    DEV_BLOCK *block = dcr->block;
345
346    ASSERT(block->binbuf == ((uint32_t) (block->bufp - block->buf)));
347    if (block->binbuf <= WRITE_BLKHDR_LENGTH) {  /* Does block have data in it? */
348       return true;
349    }
350
351    hlen = sizeof(spool_hdr);
352    wlen = block->binbuf;
353    P(dcr->dev->spool_mutex);
354    dcr->spool_size += hlen + wlen;
355    dcr->dev->spool_size += hlen + wlen;
356    if ((dcr->max_spool_size > 0 && dcr->spool_size >= dcr->max_spool_size) ||
357        (dcr->dev->max_spool_size > 0 && dcr->dev->spool_size >= dcr->dev->max_spool_size)) {
358       despool = true;
359    }
360    V(dcr->dev->spool_mutex);
361    P(mutex);
362    spool_stats.data_size += hlen + wlen;
363    if (spool_stats.data_size > spool_stats.max_data_size) {
364       spool_stats.max_data_size = spool_stats.data_size;
365    }
366    V(mutex);
367    if (despool) {
368 #ifdef xDEBUG 
369       char ec1[30], ec2[30], ec3[30], ec4[30];
370       Dmsg4(100, "Despool in write_block_to_spool_file max_size=%s size=%s "
371             "max_job_size=%s job_size=%s\n", 
372             edit_uint64_with_commas(dcr->max_spool_size, ec1),
373             edit_uint64_with_commas(dcr->spool_size, ec2),
374             edit_uint64_with_commas(dcr->dev->max_spool_size, ec3),
375             edit_uint64_with_commas(dcr->dev->spool_size, ec4));
376 #endif
377       Jmsg(dcr->jcr, M_INFO, 0, _("User specified spool size reached.\n"));
378       if (!despool_data(dcr, false)) {
379          Pmsg0(000, "Bad return from despool in write_block.\n");
380          return false;
381       }
382       /* Despooling cleared these variables so reset them */
383       P(dcr->dev->spool_mutex);
384       dcr->spool_size += hlen + wlen;
385       dcr->dev->spool_size += hlen + wlen;
386       V(dcr->dev->spool_mutex);
387       Jmsg(dcr->jcr, M_INFO, 0, _("Spooling data again ...\n"));
388    }  
389
390
391    if (!write_spool_header(dcr)) {
392       return false;
393    }
394    if (!write_spool_data(dcr)) {
395      return false;
396    }
397
398    Dmsg2(100, "Wrote block FI=%d LI=%d\n", block->FirstIndex, block->LastIndex);
399    empty_block(block);
400    return true;
401 }
402
403 static bool write_spool_header(DCR *dcr)
404 {
405    spool_hdr hdr;   
406    ssize_t stat;
407    DEV_BLOCK *block = dcr->block;
408
409    hdr.FirstIndex = block->FirstIndex;
410    hdr.LastIndex = block->LastIndex;
411    hdr.len = block->binbuf;
412
413    /* Write header */
414    for (int retry=0; retry<=1; retry++) {
415       stat = write(dcr->spool_fd, (char*)&hdr, sizeof(hdr));
416       if (stat == -1) {
417          berrno be;
418          Jmsg(dcr->jcr, M_FATAL, 0, _("Error writing header to spool file. ERR=%s\n"), 
419               be.strerror());
420       }
421       if (stat != (ssize_t)sizeof(hdr)) {
422          /* If we wrote something, truncate it, then despool */
423          if (stat != -1) {
424             if (ftruncate(dcr->spool_fd, lseek(dcr->spool_fd, (off_t)0, SEEK_CUR) - stat) != 0) {
425                berrno be;
426                Jmsg(dcr->jcr, M_FATAL, 0, _("Ftruncate spool file failed: ERR=%s\n"), 
427                   be.strerror());
428                return false;
429             }
430          }
431          if (!despool_data(dcr, false)) {
432             Jmsg(dcr->jcr, M_FATAL, 0, _("Fatal despooling error."));
433             return false;
434          }
435          continue;                    /* try again */
436       }
437       return true;
438    }
439    Jmsg(dcr->jcr, M_FATAL, 0, _("Retrying after header spooling error failed.\n"));
440    return false;
441 }
442
443 static bool write_spool_data(DCR *dcr)
444 {
445    ssize_t stat;
446    DEV_BLOCK *block = dcr->block;
447
448    /* Write data */
449    for (int retry=0; retry<=1; retry++) {
450       stat = write(dcr->spool_fd, block->buf, (size_t)block->binbuf);
451       if (stat == -1) {
452          berrno be;
453          Jmsg(dcr->jcr, M_FATAL, 0, _("Error writing data to spool file. ERR=%s\n"),
454               be.strerror());
455       }
456       if (stat != (ssize_t)block->binbuf) {
457          /* 
458           * If we wrote something, truncate it and the header, then despool
459           */
460          if (stat != -1) {
461             if (ftruncate(dcr->spool_fd, lseek(dcr->spool_fd, (off_t)0, SEEK_CUR)
462                       - stat - sizeof(spool_hdr)) != 0) {
463                berrno be;
464                Jmsg(dcr->jcr, M_FATAL, 0, _("Ftruncate spool file failed: ERR=%s\n"), 
465                   be.strerror());
466                return false;
467             }
468          }
469          if (!despool_data(dcr, false)) {
470             Jmsg(dcr->jcr, M_FATAL, 0, _("Fatal despooling error."));
471             return false;
472          }
473          if (!write_spool_header(dcr)) {
474             return false;
475          }
476          continue;                    /* try again */
477       }
478       return true;
479    }
480    Jmsg(dcr->jcr, M_FATAL, 0, _("Retrying after data spooling error failed.\n"));
481    return false;
482 }
483
484
485
486 bool are_attributes_spooled(JCR *jcr)
487 {
488    return jcr->spool_attributes && jcr->dir_bsock->spool_fd;
489 }
490
491 /* 
492  * Create spool file for attributes.
493  *  This is done by "attaching" to the bsock, and when
494  *  it is called, the output is written to a file.
495  *  The actual spooling is turned on and off in
496  *  append.c only during writing of the attributes.
497  */
498 bool begin_attribute_spool(JCR *jcr)
499 {
500    if (!jcr->no_attributes && jcr->spool_attributes) {
501       return open_attr_spool_file(jcr, jcr->dir_bsock);
502    }
503    return true;
504 }
505
506 bool discard_attribute_spool(JCR *jcr)
507 {
508    if (are_attributes_spooled(jcr)) {
509       return close_attr_spool_file(jcr, jcr->dir_bsock);
510    }
511    return true;
512 }
513
514 static void update_attr_spool_size(ssize_t size)
515 {
516    P(mutex);
517    if (size > 0) {
518      if ((spool_stats.attr_size - size) > 0) {
519         spool_stats.attr_size -= size;
520      } else {
521         spool_stats.attr_size = 0;
522      }
523    }
524    V(mutex);
525 }
526
527 bool commit_attribute_spool(JCR *jcr)
528 {
529    ssize_t size;
530    char ec1[30];
531
532    if (are_attributes_spooled(jcr)) {
533       if (fseek(jcr->dir_bsock->spool_fd, 0, SEEK_END) != 0) {
534          berrno be;
535          Jmsg(jcr, M_FATAL, 0, _("Fseek on attributes file failed: ERR=%s\n"),
536               be.strerror());
537       }
538       size = ftell(jcr->dir_bsock->spool_fd);
539       P(mutex);
540       if (size > 0) {
541         if (spool_stats.attr_size + size > spool_stats.max_attr_size) {
542            spool_stats.max_attr_size = spool_stats.attr_size + size;
543         } 
544       }
545       spool_stats.attr_size += size;
546       V(mutex);
547       Jmsg(jcr, M_INFO, 0, _("Sending spooled attrs to the Director. Despooling %s bytes ...\n"),
548             edit_uint64_with_commas(size, ec1));
549       bnet_despool_to_bsock(jcr->dir_bsock, update_attr_spool_size, size);
550       return close_attr_spool_file(jcr, jcr->dir_bsock);
551    }
552    return true;
553 }
554
555 static void make_unique_spool_filename(JCR *jcr, POOLMEM **name, int fd)
556 {
557    Mmsg(name, "%s/%s.attr.spool.%s.%d", working_directory, my_name,
558       jcr->Job, fd);
559 }
560
561
562 bool open_attr_spool_file(JCR *jcr, BSOCK *bs)
563 {
564    POOLMEM *name  = get_pool_memory(PM_MESSAGE);
565
566    make_unique_spool_filename(jcr, &name, bs->fd);
567    bs->spool_fd = fopen(mp_chr(name), "w+");
568    if (!bs->spool_fd) {
569       berrno be;
570       Jmsg(jcr, M_FATAL, 0, _("fopen attr spool file %s failed: ERR=%s\n"), name,
571            be.strerror());
572       free_pool_memory(name);
573       return false;
574    }
575    P(mutex);
576    spool_stats.attr_jobs++;
577    V(mutex);
578    free_pool_memory(name);
579    return true;
580 }
581
582 bool close_attr_spool_file(JCR *jcr, BSOCK *bs)
583 {
584    POOLMEM *name;
585     
586    if (!bs->spool_fd) {
587       return true;
588    }
589    name = get_pool_memory(PM_MESSAGE);
590    P(mutex);
591    spool_stats.attr_jobs--;
592    spool_stats.total_attr_jobs++;
593    V(mutex);
594    make_unique_spool_filename(jcr, &name, bs->fd);
595    fclose(bs->spool_fd);
596    unlink(mp_chr(name));
597    free_pool_memory(name);
598    bs->spool_fd = NULL;
599    bs->spool = false;
600    return true;
601 }