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