]> git.sur5r.net Git - bacula/bacula/blob - bacula/src/dird/next_vol.c
Misc -- see kes-1.32
[bacula/bacula] / bacula / src / dird / next_vol.c
1 /*
2  *
3  *   Bacula Director -- next_vol -- handles finding the next
4  *    volume for append.  Split out of catreq.c August MMIII
5  *    catalog request from the Storage daemon.
6
7  *     Kern Sibbald, March MMI
8  *
9  *   Version $Id$
10  */
11 /*
12    Copyright (C) 2000, 2001, 2002 Kern Sibbald and John Walker
13
14    This program is free software; you can redistribute it and/or
15    modify it under the terms of the GNU General Public License as
16    published by the Free Software Foundation; either version 2 of
17    the License, or (at your option) any later version.
18
19    This program is distributed in the hope that it will be useful,
20    but WITHOUT ANY WARRANTY; without even the implied warranty of
21    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
22    General Public License for more details.
23
24    You should have received a copy of the GNU General Public
25    License along with this program; if not, write to the Free
26    Software Foundation, Inc., 59 Temple Place - Suite 330, Boston,
27    MA 02111-1307, USA.
28
29  */
30
31 #include "bacula.h"
32 #include "dird.h"
33
34 /*
35  *  Items needed:
36  *   jcr->PoolId
37  *   jcr->store
38  *   jcr->db
39  *   jcr->pool
40  *   MEDIA_DBR mr (zeroed out)
41  *   create -- whether or not to create a new volume
42  */
43 int find_next_volume_for_append(JCR *jcr, MEDIA_DBR *mr, int create)
44 {
45    int ok, retry = 0;
46
47    mr->PoolId = jcr->PoolId;
48    bstrncpy(mr->MediaType, jcr->store->media_type, sizeof(mr->MediaType));
49    Dmsg2(120, "CatReq FindMedia: Id=%d, MediaType=%s\n", mr->PoolId, mr->MediaType);
50    /*
51     * Find the Next Volume for Append
52     */
53    db_lock(jcr->db);
54    for ( ;; ) {
55       strcpy(mr->VolStatus, "Append");  /* want only appendable volumes */
56       ok = db_find_next_volume(jcr, jcr->db, 1, mr);  
57       Dmsg2(100, "catreq after find_next_vol ok=%d FW=%d\n", ok, mr->FirstWritten);
58       if (!ok) {
59          /* Well, try finding recycled volumes */
60          ok = find_recycled_volume(jcr, mr);
61          Dmsg2(100, "find_recycled_volume %d FW=%d\n", ok, mr->FirstWritten);
62          if (!ok) {
63             prune_volumes(jcr);  
64             ok = recycle_oldest_purged_volume(jcr, mr);
65             Dmsg2(200, "find_recycled_volume2 %d FW=%d\n", ok, mr->FirstWritten);
66             if (!ok && create) {
67                /* See if we can create a new Volume */
68                ok = newVolume(jcr, mr);
69             }
70          }
71
72          if (!ok && (jcr->pool->purge_oldest_volume ||
73                      jcr->pool->recycle_oldest_volume)) {
74             Dmsg2(200, "No next volume found. PurgeOldest=%d\n RecyleOldest=%d",
75                 jcr->pool->purge_oldest_volume, jcr->pool->recycle_oldest_volume);
76             /* Find oldest volume to recycle */
77             ok = db_find_next_volume(jcr, jcr->db, -1, mr);
78             Dmsg1(400, "Find oldest=%d\n", ok);
79             if (ok) {
80                UAContext *ua;
81                Dmsg0(400, "Try purge.\n");
82                /* Try to purge oldest volume */
83                ua = new_ua_context(jcr);
84                if (jcr->pool->purge_oldest_volume) {
85                   Jmsg(jcr, M_INFO, 0, _("Purging oldest volume \"%s\"\n"), mr->VolumeName);
86                   ok = purge_jobs_from_volume(ua, mr);
87                } else {
88                   Jmsg(jcr, M_INFO, 0, _("Pruning oldest volume \"%s\"\n"), mr->VolumeName);
89                   ok = prune_volume(ua, mr);
90                }
91                free_ua_context(ua);
92                if (ok) {
93                   ok = recycle_volume(jcr, mr);
94                   Dmsg1(400, "Recycle after purge oldest=%d\n", ok);
95                }
96             }
97          }
98       }
99       /* Check if use duration applies, then if it has expired */
100       Dmsg2(100, "VolJobs=%d FirstWritten=%d\n", mr->VolJobs, mr->FirstWritten);
101       if (ok && mr->VolJobs > 0 && mr->VolUseDuration > 0 && 
102            strcmp(mr->VolStatus, "Append") == 0) {
103          utime_t now = time(NULL);
104          if (mr->VolUseDuration <= (now - mr->FirstWritten)) {
105             ok = FALSE;
106             Dmsg4(100, "Duration=%d now=%d start=%d now-start=%d\n",
107                (int)mr->VolUseDuration, (int)now, (int)mr->FirstWritten, 
108                (int)(now-mr->FirstWritten));
109             Jmsg(jcr, M_INFO, 0, _("Max configured use duration exceeded. "       
110                "Marking Volume \"%s\" as Used.\n"), mr->VolumeName);
111             strcpy(mr->VolStatus, "Used");  /* yes, mark as used */
112             if (!db_update_media_record(jcr, jcr->db, mr)) {
113                Jmsg(jcr, M_ERROR, 0, _("Catalog error updating volume \"%s\". ERR=%s"),
114                     mr->VolumeName, db_strerror(jcr->db));
115             }           
116             if (retry++ < 200) {            /* sanity check */
117                continue;                    /* try again from the top */
118             } else {
119                Jmsg(jcr, M_ERROR, 0, _(
120 "We seem to be looping trying to find the next volume. I give up.\n"));
121             }
122          }
123       }
124       break;
125    } /* end for loop */
126    db_unlock(jcr->db);
127    return ok;
128 }