]> git.sur5r.net Git - bacula/bacula/blob - bacula/src/stored/spool.c
3f1aefe67fd599cd460bc0b92762b49ea961a0fc
[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 int open_data_spool_file(JCR *jcr);
34 static int close_data_spool_file(JCR *jcr);
35 static bool despool_data(DCR *dcr);
36 static int read_block_from_spool_file(DCR *dcr, DEV_BLOCK *block);
37
38 struct spool_hdr {
39    int32_t  FirstIndex;
40    int32_t  LastIndex;
41    uint32_t len;
42 };
43
44 enum {
45    RB_EOT = 1,
46    RB_ERROR,
47    RB_OK
48 };
49
50 int begin_data_spool(JCR *jcr)
51 {
52    int stat = 1;
53    if (jcr->spool_data) {
54       Dmsg0(100, "Turning on data spooling\n");
55       jcr->dcr->spool_data = true;
56       stat = open_data_spool_file(jcr);
57       if (stat) {
58          jcr->dcr->spooling = true;
59       }
60    }
61    return stat;
62 }
63
64 int discard_data_spool(JCR *jcr)
65 {
66    if (jcr->dcr->spooling) {
67       Dmsg0(100, "Data spooling discarded\n");
68       return close_data_spool_file(jcr);
69    }
70    return 1;
71 }
72
73 int commit_data_spool(JCR *jcr)
74 {
75    bool stat;
76    if (jcr->dcr->spooling) {
77       Dmsg0(100, "Committing spooled data\n");
78       stat = despool_data(jcr->dcr);
79       if (!stat) {
80          Dmsg1(000, "Bad return from despool WroteVol=%d\n", jcr->dcr->WroteVol);
81          close_data_spool_file(jcr);
82          return 0;
83       }
84       return close_data_spool_file(jcr);
85    }
86    return 1;
87 }
88
89 static void make_unique_data_spool_filename(JCR *jcr, POOLMEM **name)
90 {
91    Mmsg(name, "%s/%s.data.spool.%s.%s", working_directory, my_name,
92       jcr->Job, jcr->device->hdr.name);
93 }
94
95
96 static int open_data_spool_file(JCR *jcr)
97 {
98    POOLMEM *name  = get_pool_memory(PM_MESSAGE);
99    int spool_fd;
100
101    make_unique_data_spool_filename(jcr, &name);
102    if ((spool_fd = open(name, O_CREAT|O_TRUNC|O_RDWR|O_BINARY, 0640)) >= 0) {
103       jcr->dcr->spool_fd = spool_fd;
104       jcr->spool_attributes = true;
105    } else {
106       Jmsg(jcr, M_ERROR, 0, "open data spool file %s failed: ERR=%s\n", name, strerror(errno));
107       free_pool_memory(name);
108       return 0;
109     }
110     Dmsg1(100, "Created spool file: %s\n", name);
111     free_pool_memory(name);
112     return 1;
113 }
114
115 static int close_data_spool_file(JCR *jcr)
116 {
117     POOLMEM *name  = get_pool_memory(PM_MESSAGE);
118
119     make_unique_data_spool_filename(jcr, &name);
120     close(jcr->dcr->spool_fd);
121     jcr->dcr->spool_fd = -1;
122     jcr->dcr->spooling = false;
123     unlink(name);
124     Dmsg1(100, "Deleted spool file: %s\n", name);
125     free_pool_memory(name);
126     return 1;
127 }
128
129 static bool despool_data(DCR *dcr)
130 {
131    DEVICE *rdev;
132    DCR *rdcr;
133    dcr->spooling = false;
134    bool ok = true;
135    DEV_BLOCK *block;
136    JCR *jcr = dcr->jcr;
137    int stat;
138
139 // lock_device(dcr->dev);
140    Dmsg0(100, "Despooling data\n");
141    /* Set up a dev structure to read */
142    rdev = (DEVICE *)malloc(sizeof(DEVICE));
143    memset(rdev, 0, sizeof(DEVICE));
144    rdev->dev_name = get_memory(strlen("spool")+1);
145    strcpy(rdev->dev_name, "spool");
146    rdev->errmsg = get_pool_memory(PM_EMSG);
147    *rdev->errmsg = 0;
148    rdcr = new_dcr(NULL, rdev);
149    rdcr->spool_fd = dcr->spool_fd; 
150    rdcr->jcr = jcr;                   /* set a valid jcr */
151    block = rdcr->block;
152    lseek(rdcr->spool_fd, 0, SEEK_SET); /* rewind */
153    for ( ; ok; ) {
154       if (job_canceled(jcr)) {
155          ok = false;
156          break;
157       }
158       stat = read_block_from_spool_file(rdcr, block);
159       if (stat == RB_EOT) {
160          break;
161       } else if (stat == RB_ERROR) {
162          ok = false;
163          break;
164       }
165       ok = write_block_to_device(dcr, block);
166       Dmsg3(100, "Write block ok=%d FI=%d LI=%d\n", ok, block->FirstIndex, block->LastIndex);
167    }
168    lseek(rdcr->spool_fd, 0, SEEK_SET); /* rewind */
169    if (ftruncate(rdcr->spool_fd, 0) != 0) {
170       Dmsg1(000, "Bad return from ftruncate. ERR=%s\n", strerror(errno));
171       ok = false;
172    }
173    free_memory(rdev->dev_name);
174    free_pool_memory(rdev->errmsg);
175    free(rdev);
176    rdcr->jcr = NULL;
177    free_dcr(rdcr);
178 // unlock_device(dcr->dev);
179    return ok;
180 }
181
182 /*
183  * Read a block from the spool file
184  * 
185  *  Returns RB_OK on success
186  *          RB_EOT when file done
187  *          RB_ERROR on error
188  */
189 static int read_block_from_spool_file(DCR *dcr, DEV_BLOCK *block)
190 {
191    uint32_t rlen;
192    ssize_t stat;
193    spool_hdr hdr;
194
195    rlen = sizeof(hdr);
196    stat = read(dcr->spool_fd, (char *)&hdr, (size_t)rlen);
197    if (stat == 0) {
198       Dmsg0(100, "EOT on spool read.\n");
199       return RB_EOT;
200    } else if (stat != (ssize_t)rlen) {
201       if (stat == -1) {
202          Jmsg(dcr->jcr, M_FATAL, 0, "Spool read error. ERR=%s\n", strerror(errno));
203       } else {
204          Dmsg2(000, "Spool read error. Wanted %u bytes, got %u\n", rlen, stat);
205          Jmsg2(dcr->jcr, M_FATAL, 0, "Spool read error. Wanted %u bytes, got %u\n", rlen, stat);
206       }
207       return RB_ERROR;
208    }
209    rlen = hdr.len;
210    if (rlen > block->buf_len) {
211       Dmsg2(000, "Spool block too big. Max %u bytes, got %u\n", block->buf_len, rlen);
212       Jmsg2(dcr->jcr, M_FATAL, 0, "Spool block too big. Max %u bytes, got %u\n", block->buf_len, rlen);
213       return RB_ERROR;
214    }
215    stat = read(dcr->spool_fd, (char *)block->buf, (size_t)rlen);
216    if (stat != (ssize_t)rlen) {
217       Dmsg2(000, "Spool read error. Wanted %u bytes, got %u\n", rlen, stat);
218       Jmsg2(dcr->jcr, M_FATAL, 0, "Spool read error. Wanted %u bytes, got %u\n", rlen, stat);
219       return RB_ERROR;
220    }
221    /* Setup write pointers */
222    block->binbuf = rlen;
223    block->bufp = block->buf + block->binbuf;
224    block->FirstIndex = hdr.FirstIndex;
225    block->LastIndex = hdr.LastIndex;
226    block->VolSessionId = dcr->jcr->VolSessionId;
227    block->VolSessionTime = dcr->jcr->VolSessionTime;
228    Dmsg2(400, "Read block FI=%d LI=%d\n", block->FirstIndex, block->LastIndex);
229    return RB_OK;
230 }
231
232 /*
233  * Write a block to the spool file
234  *
235  *  Returns: true on success or EOT
236  *           false on hard error
237  */
238 bool write_block_to_spool_file(DCR *dcr, DEV_BLOCK *block)
239 {
240    ssize_t stat = 0;
241    uint32_t wlen;                     /* length to write */
242    int retry = 0;
243    spool_hdr hdr;   
244
245    ASSERT(block->binbuf == ((uint32_t) (block->bufp - block->buf)));
246
247    if (block->binbuf <= WRITE_BLKHDR_LENGTH) {  /* Does block have data in it? */
248       Dmsg0(100, "return write_block_to_dev no data to write\n");
249       return true;
250    }
251
252    hdr.FirstIndex = block->FirstIndex;
253    hdr.LastIndex = block->LastIndex;
254    hdr.len = block->binbuf;
255    wlen = sizeof(hdr);
256 write_hdr_again:
257    stat = write(dcr->spool_fd, (char*)&hdr, (size_t)wlen);
258    if (stat != (ssize_t)wlen) {
259       if (!despool_data(dcr)) {
260          return false;
261       }
262       if (retry++ > 1) {
263          return false;
264       }
265       goto write_hdr_again;
266    }
267
268
269    wlen = block->binbuf;
270    Dmsg2(300, "Wrote block FI=%d LI=%d\n", block->FirstIndex, block->LastIndex);
271 write_again:
272    stat = write(dcr->spool_fd, block->buf, (size_t)wlen);
273    if (stat != (ssize_t)wlen) {
274       if (!despool_data(dcr)) {
275          return false;
276       }
277       if (retry++ > 1) {
278          return false;
279       }
280       goto write_again;
281    }
282
283    empty_block(block);
284    return true;
285 }
286
287
288
289 bool are_attributes_spooled(JCR *jcr)
290 {
291    return jcr->spool_attributes && jcr->dir_bsock->spool_fd;
292 }
293
294 int begin_attribute_spool(JCR *jcr)
295 {
296    if (!jcr->no_attributes && jcr->spool_attributes) {
297       return open_spool_file(jcr, jcr->dir_bsock);
298    }
299    return 1;
300 }
301
302 int discard_attribute_spool(JCR *jcr)
303 {
304    if (are_attributes_spooled(jcr)) {
305       return close_spool_file(jcr, jcr->dir_bsock);
306    }
307    return 1;
308 }
309
310 int commit_attribute_spool(JCR *jcr)
311 {
312    if (are_attributes_spooled(jcr)) {
313       bnet_despool_to_bsock(jcr->dir_bsock);
314       return close_spool_file(jcr, jcr->dir_bsock);
315    }
316    return 1;
317 }