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