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