]> git.sur5r.net Git - bacula/bacula/blobdiff - bacula/src/stored/spool.c
Apply patch from Richard Mortimer to ensure that the number
[bacula/bacula] / bacula / src / stored / spool.c
index 8724854b9456a731c4039aeb6d92db00aa870c6f..f64d0c0930a5419b7faa5d27a13468d067544d0a 100644 (file)
@@ -6,22 +6,17 @@
  *  Version $Id$
  */
 /*
-   Copyright (C) 2004-2005 Kern Sibbald
+   Copyright (C) 2004-2006 Kern Sibbald
 
    This program is free software; you can redistribute it and/or
-   modify it under the terms of the GNU General Public License as
-   published by the Free Software Foundation; either version 2 of
-   the License, or (at your option) any later version.
+   modify it under the terms of the GNU General Public License
+   version 2 as amended with additional clauses defined in the
+   file LICENSE in the main source directory.
 
    This program is distributed in the hope that it will be useful,
    but WITHOUT ANY WARRANTY; without even the implied warranty of
-   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
-   General Public License for more details.
-
-   You should have received a copy of the GNU General Public
-   License along with this program; if not, write to the Free
-   Software Foundation, Inc., 59 Temple Place - Suite 330, Boston,
-   MA 02111-1307, USA.
+   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 
+   the file LICENSE for additional details.
 
  */
 
@@ -67,21 +62,31 @@ enum {
    RB_OK
 };
 
-void list_spool_stats(BSOCK *bs)
+void list_spool_stats(void sendit(const char *msg, int len, void *sarg), void *arg)
 {
-   char ed1[30], ed2[30];
+   char *msg, ed1[30], ed2[30];
+   int len;
+
+   msg = (char *)get_pool_memory(PM_MESSAGE);
+
    if (spool_stats.data_jobs || spool_stats.max_data_size) {
-      bnet_fsend(bs, "Data spooling: %u active jobs, %s bytes; %u total jobs, %s max bytes/job.\n",
+      len = Mmsg(msg, _("Data spooling: %u active jobs, %s bytes; %u total jobs, %s max bytes/job.\n"),
          spool_stats.data_jobs, edit_uint64_with_commas(spool_stats.data_size, ed1),
          spool_stats.total_data_jobs,
          edit_uint64_with_commas(spool_stats.max_data_size, ed2));
+
+      sendit(msg, len, arg);
    }
    if (spool_stats.attr_jobs || spool_stats.max_attr_size) {
-      bnet_fsend(bs, "Attr spooling: %u active jobs, %s bytes; %u total jobs, %s max bytes.\n",
+      len = Mmsg(msg, _("Attr spooling: %u active jobs, %s bytes; %u total jobs, %s max bytes.\n"),
          spool_stats.attr_jobs, edit_uint64_with_commas(spool_stats.attr_size, ed1),
          spool_stats.total_attr_jobs,
          edit_uint64_with_commas(spool_stats.max_attr_size, ed2));
+   
+      sendit(msg, len, arg);
    }
+
+   free_pool_memory(msg);
 }
 
 bool begin_data_spool(DCR *dcr)
@@ -119,7 +124,7 @@ bool commit_data_spool(DCR *dcr)
       Dmsg0(100, "Committing spooled data\n");
       stat = despool_data(dcr, true /*commit*/);
       if (!stat) {
-         Pmsg1(000, "Bad return from despool WroteVol=%d\n", dcr->WroteVol);
+         Dmsg1(100, _("Bad return from despool WroteVol=%d\n"), dcr->WroteVol);
          close_data_spool_file(dcr);
          return false;
       }
@@ -136,7 +141,7 @@ static void make_unique_data_spool_filename(DCR *dcr, POOLMEM **name)
    } else {
       dir = working_directory;
    }
-   Mmsg(name, "%s/%s.data.spool.%s.%s", dir, my_name, dcr->jcr->Job, 
+   Mmsg(name, "%s/%s.data.%s.%s.spool", dir, my_name, dcr->jcr->Job, 
         dcr->device->hdr.name);
 }
 
@@ -169,12 +174,12 @@ static bool close_data_spool_file(DCR *dcr)
    P(mutex);
    spool_stats.data_jobs--;
    spool_stats.total_data_jobs++;
-   if (spool_stats.data_size < dcr->spool_size) {
+   if (spool_stats.data_size < dcr->job_spool_size) {
       spool_stats.data_size = 0;
    } else {
-      spool_stats.data_size -= dcr->spool_size;
+      spool_stats.data_size -= dcr->job_spool_size;
    }
-   dcr->spool_size = 0;
+   dcr->job_spool_size = 0;
    V(mutex);
 
    make_unique_data_spool_filename(dcr, &name);
@@ -200,11 +205,23 @@ static bool despool_data(DCR *dcr, bool commit)
    char ec1[50];
 
    Dmsg0(100, "Despooling data\n");
-   Jmsg(jcr, M_INFO, 0, _("%s spooled data to Volume. Despooling %s bytes ...\n"),
-        commit?"Committing":"Writing",
-        edit_uint64_with_commas(jcr->dcr->spool_size, ec1));
+   /* Commit means that the job is done, so we commit, otherwise, we
+    *  are despooling because of user spool size max or some error  
+    *  (e.g. filesystem full).
+    */
+   if (commit) {
+      Jmsg(jcr, M_INFO, 0, _("Committing spooled data to Volume \"%s\". Despooling %s bytes ...\n"),
+         jcr->dcr->VolumeName,
+         edit_uint64_with_commas(jcr->dcr->job_spool_size, ec1));
+   } else {
+      Jmsg(jcr, M_INFO, 0, _("Writing spooled data to Volume. Despooling %s bytes ...\n"),
+         edit_uint64_with_commas(jcr->dcr->job_spool_size, ec1));
+   }
+   dcr->despool_wait = true;
    dcr->spooling = false;
    lock_device(dcr->dev);
+   dcr->despool_wait = false;
+   dcr->despooling = true;
    dcr->dev_locked = true;
 
    /*
@@ -230,6 +247,9 @@ static bool despool_data(DCR *dcr, bool commit)
    Dmsg1(800, "read/write block size = %d\n", block->buf_len);
    lseek(rdcr->spool_fd, 0, SEEK_SET); /* rewind */
 
+   /* Add run time, to get current wait time */
+   time_t despool_start = time(NULL) - jcr->run_time;
+
    for ( ; ok; ) {
       if (job_canceled(jcr)) {
          ok = false;
@@ -245,10 +265,22 @@ static bool despool_data(DCR *dcr, bool commit)
       ok = write_block_to_device(dcr);
       if (!ok) {
          Jmsg2(jcr, M_FATAL, 0, _("Fatal append error on device %s: ERR=%s\n"),
-               dcr->dev->print_name(), strerror_dev(dcr->dev));
+               dcr->dev->print_name(), dcr->dev->bstrerror());
       }
       Dmsg3(800, "Write block ok=%d FI=%d LI=%d\n", ok, block->FirstIndex, block->LastIndex);
    }
+
+   /* Subtracting run_time give us elapsed time - wait_time since we started despooling */
+   time_t despool_elapsed = time(NULL) - despool_start - jcr->run_time;
+
+   if (despool_elapsed <= 0) {
+      despool_elapsed = 1;
+   }
+
+   Jmsg(dcr->jcr, M_INFO, 0, _("Despooling elapsed time = %02d:%02d:%02d, Transfer rate = %s bytes/second\n"),
+         despool_elapsed / 3600, despool_elapsed % 3600 / 60, despool_elapsed % 60,
+         edit_uint64_with_suffix(jcr->dcr->job_spool_size / despool_elapsed, ec1));
+
    dcr->block = block;                /* reset block */
 
    lseek(rdcr->spool_fd, 0, SEEK_SET); /* rewind */
@@ -256,20 +288,20 @@ static bool despool_data(DCR *dcr, bool commit)
       berrno be;
       Jmsg(dcr->jcr, M_ERROR, 0, _("Ftruncate spool file failed: ERR=%s\n"),
          be.strerror());
-      Pmsg1(000, "Bad return from ftruncate. ERR=%s\n", be.strerror());
+      Pmsg1(000, _("Bad return from ftruncate. ERR=%s\n"), be.strerror());
       ok = false;
    }
 
    P(mutex);
-   if (spool_stats.data_size < dcr->spool_size) {
+   if (spool_stats.data_size < dcr->job_spool_size) {
       spool_stats.data_size = 0;
    } else {
-      spool_stats.data_size -= dcr->spool_size;
+      spool_stats.data_size -= dcr->job_spool_size;
    }
    V(mutex);
    P(dcr->dev->spool_mutex);
-   dcr->dev->spool_size -= dcr->spool_size;
-   dcr->spool_size = 0;               /* zap size in input dcr */
+   dcr->dev->spool_size -= dcr->job_spool_size;
+   dcr->job_spool_size = 0;            /* zap size in input dcr */
    V(dcr->dev->spool_mutex);
    free_memory(rdev->dev_name);
    free_pool_memory(rdev->errmsg);
@@ -277,9 +309,10 @@ static bool despool_data(DCR *dcr, bool commit)
    rdcr->jcr = NULL;
    free_dcr(rdcr);
    free(rdev);
-   unlock_device(dcr->dev);
    dcr->dev_locked = false;
    dcr->spooling = true;           /* turn on spooling again */
+   dcr->despooling = false;
+   unlock_device(dcr->dev);
    return ok;
 }
 
@@ -308,21 +341,21 @@ static int read_block_from_spool_file(DCR *dcr)
          Jmsg(dcr->jcr, M_FATAL, 0, _("Spool header read error. ERR=%s\n"),
               be.strerror());
       } else {
-         Pmsg2(000, "Spool read error. Wanted %u bytes, got %u\n", rlen, stat);
-         Jmsg2(dcr->jcr, M_FATAL, 0, _("Spool header read error. Wanted %u bytes, got %u\n"), rlen, stat);
+         Pmsg2(000, _("Spool read error. Wanted %u bytes, got %d\n"), rlen, stat);
+         Jmsg2(dcr->jcr, M_FATAL, 0, _("Spool header read error. Wanted %u bytes, got %d\n"), rlen, stat);
       }
       return RB_ERROR;
    }
    rlen = hdr.len;
    if (rlen > block->buf_len) {
-      Pmsg2(000, "Spool block too big. Max %u bytes, got %u\n", block->buf_len, rlen);
+      Pmsg2(000, _("Spool block too big. Max %u bytes, got %u\n"), block->buf_len, rlen);
       Jmsg2(dcr->jcr, M_FATAL, 0, _("Spool block too big. Max %u bytes, got %u\n"), block->buf_len, rlen);
       return RB_ERROR;
    }
    stat = read(dcr->spool_fd, (char *)block->buf, (size_t)rlen);
    if (stat != (ssize_t)rlen) {
-      Pmsg2(000, "Spool data read error. Wanted %u bytes, got %u\n", rlen, stat);
-      Jmsg2(dcr->jcr, M_FATAL, 0, _("Spool data read error. Wanted %u bytes, got %u\n"), rlen, stat);
+      Pmsg2(000, _("Spool data read error. Wanted %u bytes, got %d\n"), rlen, stat);
+      Jmsg2(dcr->jcr, M_FATAL, 0, _("Spool data read error. Wanted %u bytes, got %d\n"), rlen, stat);
       return RB_ERROR;
    }
    /* Setup write pointers */
@@ -356,9 +389,9 @@ bool write_block_to_spool_file(DCR *dcr)
    hlen = sizeof(spool_hdr);
    wlen = block->binbuf;
    P(dcr->dev->spool_mutex);
-   dcr->spool_size += hlen + wlen;
+   dcr->job_spool_size += hlen + wlen;
    dcr->dev->spool_size += hlen + wlen;
-   if ((dcr->max_spool_size > 0 && dcr->spool_size >= dcr->max_spool_size) ||
+   if ((dcr->max_job_spool_size > 0 && dcr->job_spool_size >= dcr->max_job_spool_size) ||
        (dcr->dev->max_spool_size > 0 && dcr->dev->spool_size >= dcr->dev->max_spool_size)) {
       despool = true;
    }
@@ -374,19 +407,19 @@ bool write_block_to_spool_file(DCR *dcr)
       char ec1[30], ec2[30], ec3[30], ec4[30];
       Dmsg4(100, "Despool in write_block_to_spool_file max_size=%s size=%s "
             "max_job_size=%s job_size=%s\n",
-            edit_uint64_with_commas(dcr->max_spool_size, ec1),
-            edit_uint64_with_commas(dcr->spool_size, ec2),
+            edit_uint64_with_commas(dcr->max_job_spool_size, ec1),
+            edit_uint64_with_commas(dcr->job_spool_size, ec2),
             edit_uint64_with_commas(dcr->dev->max_spool_size, ec3),
             edit_uint64_with_commas(dcr->dev->spool_size, ec4));
 #endif
       Jmsg(dcr->jcr, M_INFO, 0, _("User specified spool size reached.\n"));
       if (!despool_data(dcr, false)) {
-         Pmsg0(000, "Bad return from despool in write_block.\n");
+         Pmsg0(000, _("Bad return from despool in write_block.\n"));
          return false;
       }
       /* Despooling cleared these variables so reset them */
       P(dcr->dev->spool_mutex);
-      dcr->spool_size += hlen + wlen;
+      dcr->job_spool_size += hlen + wlen;
       dcr->dev->spool_size += hlen + wlen;
       V(dcr->dev->spool_mutex);
       Jmsg(dcr->jcr, M_INFO, 0, _("Spooling data again ...\n"));
@@ -568,7 +601,7 @@ bail_out:
 
 static void make_unique_spool_filename(JCR *jcr, POOLMEM **name, int fd)
 {
-   Mmsg(name, "%s/%s.attr.spool.%s.%d", working_directory, my_name,
+   Mmsg(name, "%s/%s.attr.%s.%d.spool", working_directory, my_name,
       jcr->Job, fd);
 }
 
@@ -578,7 +611,7 @@ bool open_attr_spool_file(JCR *jcr, BSOCK *bs)
    POOLMEM *name  = get_pool_memory(PM_MESSAGE);
 
    make_unique_spool_filename(jcr, &name, bs->fd);
-   bs->spool_fd = fopen(name, "w+");
+   bs->spool_fd = fopen(name, "w+b");
    if (!bs->spool_fd) {
       berrno be;
       Jmsg(jcr, M_FATAL, 0, _("fopen attr spool file %s failed: ERR=%s\n"), name,