4 * Kern Sibbald, March 2004
9 Copyright (C) 2000-2004 Kern Sibbald and John Walker
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.
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.
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,
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);
36 static int read_block_from_spool_file(DCR *dcr, DEV_BLOCK *block);
38 struct spool_stats_t {
39 uint32_t data_jobs; /* current jobs spooling data */
41 uint32_t total_data_jobs; /* total jobs to have spooled data */
42 uint32_t total_attr_jobs;
43 uint64_t max_data_size; /* max data size */
44 uint64_t max_attr_size;
45 uint64_t data_size; /* current data size (all jobs running) */
49 static pthread_mutex_t mutex = PTHREAD_MUTEX_INITIALIZER;
50 spool_stats_t spool_stats;
53 * Header for data spool record */
55 int32_t FirstIndex; /* FirstIndex for buffer */
56 int32_t LastIndex; /* LastIndex for buffer */
57 uint32_t len; /* length of next buffer */
66 void list_spool_stats(BSOCK *bs)
68 char ed1[30], ed2[30];
69 if (spool_stats.data_jobs || spool_stats.max_data_size) {
70 bnet_fsend(bs, "Data spooling: %d active jobs %s bytes; %d total jobs %s max bytes/job.\n",
71 spool_stats.data_jobs, edit_uint64_with_commas(spool_stats.data_size, ed1),
72 spool_stats.total_data_jobs,
73 edit_uint64_with_commas(spool_stats.max_data_size, ed2));
75 if (spool_stats.attr_jobs || spool_stats.max_attr_size) {
76 bnet_fsend(bs, "Attr spooling: %d active jobs; %d total jobs %s max bytes/job.\n",
77 spool_stats.attr_jobs, spool_stats.total_attr_jobs,
78 edit_uint64_with_commas(spool_stats.max_attr_size, ed1));
82 bool begin_data_spool(JCR *jcr)
85 if (jcr->spool_data) {
86 Dmsg0(100, "Turning on data spooling\n");
87 jcr->dcr->spool_data = true;
88 stat = open_data_spool_file(jcr);
90 jcr->dcr->spooling = true;
91 Jmsg(jcr, M_INFO, 0, _("Spooling data ...\n"));
93 spool_stats.data_jobs++;
100 bool discard_data_spool(JCR *jcr)
102 if (jcr->dcr->spooling) {
103 Dmsg0(100, "Data spooling discarded\n");
104 return close_data_spool_file(jcr);
109 bool commit_data_spool(JCR *jcr)
112 if (jcr->dcr->spooling) {
113 Dmsg0(100, "Committing spooled data\n");
114 stat = despool_data(jcr->dcr);
116 Dmsg1(000, "Bad return from despool WroteVol=%d\n", jcr->dcr->WroteVol);
117 close_data_spool_file(jcr);
120 return close_data_spool_file(jcr);
125 static void make_unique_data_spool_filename(JCR *jcr, POOLMEM **name)
128 if (jcr->dcr->dev->device->spool_directory) {
129 dir = jcr->dcr->dev->device->spool_directory;
131 dir = working_directory;
133 Mmsg(name, "%s/%s.data.spool.%s.%s", dir, my_name, jcr->Job, jcr->device->hdr.name);
137 static bool open_data_spool_file(JCR *jcr)
139 POOLMEM *name = get_pool_memory(PM_MESSAGE);
142 make_unique_data_spool_filename(jcr, &name);
143 if ((spool_fd = open(name, O_CREAT|O_TRUNC|O_RDWR|O_BINARY, 0640)) >= 0) {
144 jcr->dcr->spool_fd = spool_fd;
145 jcr->spool_attributes = true;
147 Jmsg(jcr, M_ERROR, 0, _("Open data spool file %s failed: ERR=%s\n"), name, strerror(errno));
148 free_pool_memory(name);
151 Dmsg1(100, "Created spool file: %s\n", name);
152 free_pool_memory(name);
156 static bool close_data_spool_file(JCR *jcr)
158 POOLMEM *name = get_pool_memory(PM_MESSAGE);
161 spool_stats.data_jobs--;
162 spool_stats.total_data_jobs++;
163 spool_stats.data_size -= jcr->dcr->spool_size;
164 if (spool_stats.data_size < 0) {
165 spool_stats.data_size = 0;
167 jcr->dcr->spool_size = 0;
170 make_unique_data_spool_filename(jcr, &name);
171 close(jcr->dcr->spool_fd);
172 jcr->dcr->spool_fd = -1;
173 jcr->dcr->spooling = false;
175 Dmsg1(100, "Deleted spool file: %s\n", name);
176 free_pool_memory(name);
180 static bool despool_data(DCR *dcr)
189 Dmsg0(100, "Despooling data\n");
190 dcr->spooling = false;
191 lock_device(dcr->dev);
192 dcr->dev_locked = true;
194 /* Setup a dev structure to read */
195 rdev = (DEVICE *)malloc(sizeof(DEVICE));
196 memset(rdev, 0, sizeof(DEVICE));
197 rdev->dev_name = get_memory(strlen("spool")+1);
198 strcpy(rdev->dev_name, "spool");
199 rdev->errmsg = get_pool_memory(PM_EMSG);
201 rdev->max_block_size = dcr->dev->max_block_size;
202 rdev->min_block_size = dcr->dev->min_block_size;
203 rdev->device = dcr->dev->device;
204 rdcr = new_dcr(NULL, rdev);
205 rdcr->spool_fd = dcr->spool_fd;
206 rdcr->jcr = jcr; /* set a valid jcr */
208 Dmsg1(800, "read/write block size = %d\n", block->buf_len);
209 lseek(rdcr->spool_fd, 0, SEEK_SET); /* rewind */
212 if (job_canceled(jcr)) {
216 stat = read_block_from_spool_file(rdcr, block);
217 if (stat == RB_EOT) {
219 } else if (stat == RB_ERROR) {
223 ok = write_block_to_device(dcr, block);
224 Dmsg3(100, "Write block ok=%d FI=%d LI=%d\n", ok, block->FirstIndex, block->LastIndex);
227 lseek(rdcr->spool_fd, 0, SEEK_SET); /* rewind */
228 if (ftruncate(rdcr->spool_fd, 0) != 0) {
229 Jmsg(dcr->jcr, M_FATAL, 0, _("Ftruncate spool file error. ERR=%s\n"),
231 Dmsg1(000, "Bad return from ftruncate. ERR=%s\n", strerror(errno));
236 spool_stats.data_size -= dcr->spool_size;
237 if (spool_stats.data_size < 0) {
238 spool_stats.data_size = 0;
241 P(dcr->dev->spool_mutex);
242 dcr->dev->spool_size -= dcr->spool_size;
243 dcr->spool_size = 0; /* zap size in input dcr */
244 V(dcr->dev->spool_mutex);
245 free_memory(rdev->dev_name);
246 free_pool_memory(rdev->errmsg);
250 unlock_device(dcr->dev);
251 dcr->dev_locked = false;
252 dcr->spooling = true; /* turn on spooling again */
257 * Read a block from the spool file
259 * Returns RB_OK on success
260 * RB_EOT when file done
263 static int read_block_from_spool_file(DCR *dcr, DEV_BLOCK *block)
270 stat = read(dcr->spool_fd, (char *)&hdr, (size_t)rlen);
272 Dmsg0(100, "EOT on spool read.\n");
274 } else if (stat != (ssize_t)rlen) {
276 Jmsg(dcr->jcr, M_FATAL, 0, _("Spool header read error. ERR=%s\n"), strerror(errno));
278 Dmsg2(000, "Spool read error. Wanted %u bytes, got %u\n", rlen, stat);
279 Jmsg2(dcr->jcr, M_FATAL, 0, _("Spool header read error. Wanted %u bytes, got %u\n"), rlen, stat);
284 if (rlen > block->buf_len) {
285 Dmsg2(000, "Spool block too big. Max %u bytes, got %u\n", block->buf_len, rlen);
286 Jmsg2(dcr->jcr, M_FATAL, 0, _("Spool block too big. Max %u bytes, got %u\n"), block->buf_len, rlen);
289 stat = read(dcr->spool_fd, (char *)block->buf, (size_t)rlen);
290 if (stat != (ssize_t)rlen) {
291 Dmsg2(000, "Spool data read error. Wanted %u bytes, got %u\n", rlen, stat);
292 Jmsg2(dcr->jcr, M_FATAL, 0, _("Spool data read error. Wanted %u bytes, got %u\n"), rlen, stat);
295 /* Setup write pointers */
296 block->binbuf = rlen;
297 block->bufp = block->buf + block->binbuf;
298 block->FirstIndex = hdr.FirstIndex;
299 block->LastIndex = hdr.LastIndex;
300 block->VolSessionId = dcr->jcr->VolSessionId;
301 block->VolSessionTime = dcr->jcr->VolSessionTime;
302 Dmsg2(100, "Read block FI=%d LI=%d\n", block->FirstIndex, block->LastIndex);
307 * Write a block to the spool file
309 * Returns: true on success or EOT
310 * false on hard error
312 bool write_block_to_spool_file(DCR *dcr, DEV_BLOCK *block)
315 uint32_t wlen, hlen; /* length to write */
318 bool despool = false;
320 ASSERT(block->binbuf == ((uint32_t) (block->bufp - block->buf)));
321 if (block->binbuf <= WRITE_BLKHDR_LENGTH) { /* Does block have data in it? */
326 wlen = block->binbuf;
327 P(dcr->dev->spool_mutex);
328 dcr->spool_size += hlen + wlen;
329 dcr->dev->spool_size += hlen + wlen;
330 if ((dcr->max_spool_size > 0 && dcr->spool_size >= dcr->max_spool_size) ||
331 (dcr->dev->max_spool_size > 0 && dcr->dev->spool_size >= dcr->dev->max_spool_size)) {
334 V(dcr->dev->spool_mutex);
336 spool_stats.data_size += hlen + wlen;
337 if (spool_stats.data_size > spool_stats.max_data_size) {
338 spool_stats.max_data_size = spool_stats.data_size;
343 char ec1[30], ec2[30], ec3[30], ec4[30];
344 Dmsg4(100, "Despool in write_block_to_spool_file max_size=%s size=%s "
345 "max_job_size=%s job_size=%s\n",
346 edit_uint64_with_commas(dcr->max_spool_size, ec1),
347 edit_uint64_with_commas(dcr->spool_size, ec2),
348 edit_uint64_with_commas(dcr->dev->max_spool_size, ec3),
349 edit_uint64_with_commas(dcr->dev->spool_size, ec4));
351 Jmsg(dcr->jcr, M_INFO, 0, _("User specified spool size reached. Despooling ...\n"));
352 if (!despool_data(dcr)) {
353 Dmsg0(000, "Bad return from despool in write_block.\n");
356 /* Despooling cleard these variables so reset them */
357 P(dcr->dev->spool_mutex);
358 dcr->spool_size += hlen + wlen;
359 dcr->dev->spool_size += hlen + wlen;
360 V(dcr->dev->spool_mutex);
361 Jmsg(dcr->jcr, M_INFO, 0, _("Spooling data again ...\n"));
364 hdr.FirstIndex = block->FirstIndex;
365 hdr.LastIndex = block->LastIndex;
366 hdr.len = block->binbuf;
370 stat = write(dcr->spool_fd, (char*)&hdr, (size_t)hlen);
372 Jmsg(dcr->jcr, M_INFO, 0, _("Error writing header to spool file. ERR=%s\n"), strerror(errno));
374 if (stat != (ssize_t)hlen) {
375 if (!despool_data(dcr)) {
376 Jmsg(dcr->jcr, M_FATAL, 0, _("Fatal despooling error."));
389 stat = write(dcr->spool_fd, block->buf, (size_t)wlen);
391 Jmsg(dcr->jcr, M_INFO, 0, _("Error writing data to spool file. ERR=%s\n"), strerror(errno));
393 if (stat != (ssize_t)wlen) {
394 if (!despool_data(dcr)) {
395 Jmsg(dcr->jcr, M_FATAL, 0, _("Fatal despooling error."));
405 Dmsg2(100, "Wrote block FI=%d LI=%d\n", block->FirstIndex, block->LastIndex);
412 bool are_attributes_spooled(JCR *jcr)
414 return jcr->spool_attributes && jcr->dir_bsock->spool_fd;
418 * Create spool file for attributes.
419 * This is done by "attaching" to the bsock, and when
420 * it is called, the output is written to a file.
421 * The actual spooling is turned on and off in
422 * append.c only during writing of the attributes.
424 bool begin_attribute_spool(JCR *jcr)
426 if (!jcr->no_attributes && jcr->spool_attributes) {
427 return open_spool_file(jcr, jcr->dir_bsock);
432 bool discard_attribute_spool(JCR *jcr)
434 if (are_attributes_spooled(jcr)) {
435 return close_spool_file(jcr, jcr->dir_bsock);
440 bool commit_attribute_spool(JCR *jcr)
442 if (are_attributes_spooled(jcr)) {
443 bnet_despool_to_bsock(jcr->dir_bsock);
444 return close_spool_file(jcr, jcr->dir_bsock);
449 static void make_unique_spool_filename(JCR *jcr, POOLMEM **name, int fd)
451 Mmsg(name, "%s/%s.attr.spool.%s.%d", working_directory, my_name,
455 bool open_spool_file(JCR *jcr, BSOCK *bs)
457 POOLMEM *name = get_pool_memory(PM_MESSAGE);
459 make_unique_spool_filename(jcr, &name, bs->fd);
460 bs->spool_fd = fopen(mp_chr(name), "w+");
462 Jmsg(jcr, M_ERROR, 0, _("fopen attr spool file %s failed: ERR=%s\n"), name, strerror(errno));
463 free_pool_memory(name);
467 spool_stats.attr_jobs++;
469 free_pool_memory(name);
473 bool close_spool_file(JCR *jcr, BSOCK *bs)
475 POOLMEM *name = get_pool_memory(PM_MESSAGE);
478 fseek(bs->spool_fd, 0, SEEK_END);
479 size = ftell(bs->spool_fd);
482 if (spool_stats.attr_size + size > spool_stats.max_attr_size) {
483 spool_stats.max_attr_size = spool_stats.attr_size + size;
486 spool_stats.attr_jobs--;
487 spool_stats.total_attr_jobs++;
489 make_unique_spool_filename(jcr, &name, bs->fd);
490 fclose(bs->spool_fd);
491 unlink(mp_chr(name));
492 free_pool_memory(name);