]> git.sur5r.net Git - bacula/bacula/blob - bacula/src/dird/next_vol.c
This commit was manufactured by cvs2svn to create tag
[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       bstrncpy(mr->VolStatus, "Append", sizeof(mr->VolStatus));  /* 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          /* 
73           * Don't do a purge if called from a command i.e. create == 0.
74           */ 
75          if (!ok && (jcr->pool->purge_oldest_volume ||
76                      jcr->pool->recycle_oldest_volume)) {
77             Dmsg2(200, "No next volume found. PurgeOldest=%d\n RecyleOldest=%d",
78                 jcr->pool->purge_oldest_volume, jcr->pool->recycle_oldest_volume);
79             /* Find oldest volume to recycle */
80             ok = db_find_next_volume(jcr, jcr->db, -1, mr);
81             Dmsg1(400, "Find oldest=%d\n", ok);
82             if (ok) {
83                UAContext *ua;
84                Dmsg0(400, "Try purge.\n");
85                /* Try to purge oldest volume */
86                ua = new_ua_context(jcr);
87                if (jcr->pool->purge_oldest_volume && create) {
88                   Jmsg(jcr, M_INFO, 0, _("Purging oldest volume \"%s\"\n"), mr->VolumeName);
89                   ok = purge_jobs_from_volume(ua, mr);
90                } else if (jcr->pool->recycle_oldest_volume) {
91                   Jmsg(jcr, M_INFO, 0, _("Pruning oldest volume \"%s\"\n"), mr->VolumeName);
92                   ok = prune_volume(ua, mr);
93                }
94                free_ua_context(ua);
95                if (ok) {
96                   ok = recycle_volume(jcr, mr);
97                   Dmsg1(400, "Recycle after purge oldest=%d\n", ok);
98                }
99             }
100          }
101       }
102       Dmsg2(100, "VolJobs=%d FirstWritten=%d\n", mr->VolJobs, mr->FirstWritten);
103       if (ok) {
104          /* If we can use the volume, check if it is expired */
105          if (has_volume_expired(jcr, mr)) {
106             if (retry++ < 200) {            /* sanity check */
107                continue;                    /* try again from the top */
108             } else {
109                Jmsg(jcr, M_ERROR, 0, _(
110 "We seem to be looping trying to find the next volume. I give up.\n"));
111             }
112          }
113       }
114       break;
115    } /* end for loop */
116    db_unlock(jcr->db);
117    return ok;
118 }
119
120 /*
121  * Check if any time limits or use limits have expired
122  *   if so, set the VolStatus appropriately.
123  */
124 bool has_volume_expired(JCR *jcr, MEDIA_DBR *mr)
125 {
126    bool expired = false;
127    /*
128     * Check limits and expirations if "Append" and it has been used
129     * i.e. mr->VolJobs > 0
130     *
131     */
132    if (strcmp(mr->VolStatus, "Append") == 0 && mr->VolJobs > 0) {
133       /* First handle Max Volume Bytes */
134       if ((mr->MaxVolBytes > 0 && mr->VolBytes >= mr->MaxVolBytes)) {
135          Jmsg(jcr, M_INFO, 0, _("Max Volume bytes exceeded. "             
136              "Marking Volume \"%s\" as Full.\n"), mr->VolumeName);
137          bstrncpy(mr->VolStatus, "Full", sizeof(mr->VolStatus));
138          expired = true;
139
140       /* Now see if Volume should only be used once */
141       } else if (mr->VolBytes > 0 && jcr->pool->use_volume_once) {
142          Jmsg(jcr, M_INFO, 0, _("Volume used once. "             
143              "Marking Volume \"%s\" as Used.\n"), mr->VolumeName);
144          bstrncpy(mr->VolStatus, "Used", sizeof(mr->VolStatus));
145          expired = true;
146
147       /* Now see if Max Jobs written to volume */
148       } else if (mr->MaxVolJobs > 0 && mr->MaxVolJobs <= mr->VolJobs) {
149          Jmsg(jcr, M_INFO, 0, _("Max Volume jobs exceeded. "       
150              "Marking Volume \"%s\" as Used.\n"), mr->VolumeName);
151          bstrncpy(mr->VolStatus, "Used", sizeof(mr->VolStatus));
152          expired = true;
153
154       /* Now see if Max Files written to volume */
155       } else if (mr->MaxVolFiles > 0 && mr->MaxVolFiles <= mr->VolFiles) {
156          Jmsg(jcr, M_INFO, 0, _("Max Volume files exceeded. "       
157              "Marking Volume \"%s\" as Used.\n"), mr->VolumeName);
158          bstrncpy(mr->VolStatus, "Used", sizeof(mr->VolStatus));
159          expired = true;
160
161       /* Finally, check Use duration expiration */
162       } else if (mr->VolUseDuration > 0) {
163          utime_t now = time(NULL);
164          /* See if Vol Use has expired */
165          if (mr->VolUseDuration <= (now - mr->FirstWritten)) {
166             Jmsg(jcr, M_INFO, 0, _("Max configured use duration exceeded. "       
167                "Marking Volume \"%s\" as Used.\n"), mr->VolumeName);
168             bstrncpy(mr->VolStatus, "Used", sizeof(mr->VolStatus));
169             expired = true;
170          }
171       }
172    }
173    if (expired) {
174       /* Need to update media */
175       if (!db_update_media_record(jcr, jcr->db, mr)) {
176          Jmsg(jcr, M_ERROR, 0, _("Catalog error updating volume \"%s\". ERR=%s"),
177               mr->VolumeName, db_strerror(jcr->db));
178       }           
179    }  
180    return expired;
181 }
182
183 /*
184  * Try hard to recycle the current volume
185  *
186  *  Returns: on failure - reason = NULL
187  *           on success - reason - pointer to reason
188  */
189 void check_if_volume_valid_or_recyclable(JCR *jcr, MEDIA_DBR *mr, char **reason)
190 {
191    int ok;
192
193    *reason = NULL;
194
195    /*  Check if a duration or limit has expired */
196    if (has_volume_expired(jcr, mr)) {
197       *reason = "volume has expired";
198       /* Keep going because we may be able to recycle volume */
199    }
200
201    /*
202     * Now see if we can use the volume as is
203     */
204    if (strcmp(mr->VolStatus, "Append") == 0 ||
205        strcmp(mr->VolStatus, "Recycle") == 0) {
206       *reason = NULL;
207       return;
208    }
209                                                                                         
210    /*
211     * Check if the Volume is already marked for recycling
212     */
213    if (strcmp(mr->VolStatus, "Purged") == 0) {
214       if (recycle_volume(jcr, mr)) {
215          Jmsg(jcr, M_INFO, 0, "Recycled current volume \"%s\"\n", mr->VolumeName);
216          *reason = NULL;
217          return;
218       } else {
219          /* In principle this shouldn't happen */
220          *reason = "and recycling of current volume failed";
221          return;
222       }
223    }
224                                                                                         
225    /* At this point, the volume is not valid for writing */
226    *reason = "but should be Append, Purged or Recycle";
227                                                                                         
228    /*
229     * What we're trying to do here is see if the current volume is
230     * "recyclable" - ie. if we prune all expired jobs off it, is
231     * it now possible to reuse it for the job that it is currently
232     * needed for?
233     */
234    if ((mr->LastWritten + mr->VolRetention) < (utime_t)time(NULL)
235          && mr->Recycle && jcr->pool->recycle_current_volume
236          && (strcmp(mr->VolStatus, "Full") == 0 ||
237             strcmp(mr->VolStatus, "Used") == 0)) {
238       /*
239        * Attempt prune of current volume to see if we can
240        * recycle it for use.
241        */
242       UAContext *ua;
243                                                                                         
244       ua = new_ua_context(jcr);
245       ok = prune_volume(ua, mr);
246       free_ua_context(ua);
247                                                                                         
248       if (ok) {
249          /* If fully purged, recycle current volume */
250          if (recycle_volume(jcr, mr)) {
251             Jmsg(jcr, M_INFO, 0, "Recycled current volume \"%s\"\n", mr->VolumeName);
252             *reason = NULL;
253          } else {
254             *reason = "but should be Append, Purged or Recycle (recycling of the "
255                "current volume failed)";
256          }
257       } else {
258          *reason = "but should be Append, Purged or Recycle (cannot automatically "
259             "recycle current volume, as it still contains unpruned data)";
260       }
261    }
262 }