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.
7 * Kern Sibbald, March MMI
12 Copyright (C) 2001-2005 Kern Sibbald
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.
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.
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,
36 * mr.PoolId must be set
40 * MEDIA_DBR mr (zeroed out)
41 * create -- whether or not to create a new volume
43 int find_next_volume_for_append(JCR *jcr, MEDIA_DBR *mr, bool create)
48 STORE *store = jcr->store;
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);
53 * If we are using an Autochanger, restrict Volume
54 * search to the Autochanger on the first pass
56 InChanger = store->autochanger;
58 * Find the Next Volume for Append
62 bstrncpy(mr->VolStatus, "Append", sizeof(mr->VolStatus)); /* want only appendable volumes */
64 * 1. Look for volume with "Append" status.
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);
70 * 2. Try finding a recycled volume
72 ok = find_recycled_volume(jcr, InChanger, mr);
73 Dmsg2(100, "find_recycled_volume %d FW=%d\n", ok, mr->FirstWritten);
76 * 3. Try recycling any purged volume
78 ok = recycle_oldest_purged_volume(jcr, InChanger, mr);
81 * 4. Try pruning Volumes
84 ok = recycle_oldest_purged_volume(jcr, InChanger, mr);
88 continue; /* retry again accepting any volume */
91 Dmsg2(200, "find_recycled_volume2 %d FW=%d\n", ok, mr->FirstWritten);
94 * 5. Try "creating" a new Volume
96 ok = newVolume(jcr, mr);
105 char ed1[50], ed2[50];
107 * 6. Try pulling a volume from the Scratch pool
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);
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);
124 Jmsg(jcr, M_INFO, 0, _("Using Volume \"%s\" from 'Scratch' pool.\n"),
126 /* Set new Pool Id in smr record, then copy it to mr */
127 smr.PoolId = mr->PoolId;
128 memcpy(mr, &smr, sizeof(MEDIA_DBR));
133 * Look at more drastic ways to find an Appendable Volume
135 if (!ok && (jcr->pool->purge_oldest_volume ||
136 jcr->pool->recycle_oldest_volume)) {
137 Dmsg2(200, "No next volume found. PurgeOldest=%d\n RecyleOldest=%d",
138 jcr->pool->purge_oldest_volume, jcr->pool->recycle_oldest_volume);
139 /* Find oldest volume to recycle */
140 ok = db_find_next_volume(jcr, jcr->db, -1, InChanger, mr);
141 Dmsg1(400, "Find oldest=%d\n", ok);
144 Dmsg0(400, "Try purge.\n");
146 * 5. Try to purging oldest volume only if not UA calling us.
148 ua = new_ua_context(jcr);
149 if (jcr->pool->purge_oldest_volume && create) {
150 Jmsg(jcr, M_INFO, 0, _("Purging oldest volume \"%s\"\n"), mr->VolumeName);
151 ok = purge_jobs_from_volume(ua, mr);
153 * 5. or try recycling the oldest volume
155 } else if (jcr->pool->recycle_oldest_volume) {
156 Jmsg(jcr, M_INFO, 0, _("Pruning oldest volume \"%s\"\n"), mr->VolumeName);
157 ok = prune_volume(ua, mr);
161 ok = recycle_volume(jcr, mr);
162 Dmsg1(400, "Recycle after purge oldest=%d\n", ok);
167 Dmsg2(100, "VolJobs=%d FirstWritten=%d\n", mr->VolJobs, mr->FirstWritten);
169 /* If we can use the volume, check if it is expired */
170 if (has_volume_expired(jcr, mr)) {
171 if (retry++ < 200) { /* sanity check */
172 continue; /* try again from the top */
174 Jmsg(jcr, M_ERROR, 0, _(
175 "We seem to be looping trying to find the next volume. I give up.\n"));
186 * Check if any time limits or use limits have expired
187 * if so, set the VolStatus appropriately.
189 bool has_volume_expired(JCR *jcr, MEDIA_DBR *mr)
191 bool expired = false;
193 * Check limits and expirations if "Append" and it has been used
194 * i.e. mr->VolJobs > 0
197 if (strcmp(mr->VolStatus, "Append") == 0 && mr->VolJobs > 0) {
198 /* First handle Max Volume Bytes */
199 if ((mr->MaxVolBytes > 0 && mr->VolBytes >= mr->MaxVolBytes)) {
200 Jmsg(jcr, M_INFO, 0, _("Max Volume bytes exceeded. "
201 "Marking Volume \"%s\" as Full.\n"), mr->VolumeName);
202 bstrncpy(mr->VolStatus, "Full", sizeof(mr->VolStatus));
205 /* Now see if Volume should only be used once */
206 } else if (mr->VolBytes > 0 && jcr->pool->use_volume_once) {
207 Jmsg(jcr, M_INFO, 0, _("Volume used once. "
208 "Marking Volume \"%s\" as Used.\n"), mr->VolumeName);
209 bstrncpy(mr->VolStatus, "Used", sizeof(mr->VolStatus));
212 /* Now see if Max Jobs written to volume */
213 } else if (mr->MaxVolJobs > 0 && mr->MaxVolJobs <= mr->VolJobs) {
214 Jmsg(jcr, M_INFO, 0, _("Max Volume jobs exceeded. "
215 "Marking Volume \"%s\" as Used.\n"), mr->VolumeName);
216 bstrncpy(mr->VolStatus, "Used", sizeof(mr->VolStatus));
219 /* Now see if Max Files written to volume */
220 } else if (mr->MaxVolFiles > 0 && mr->MaxVolFiles <= mr->VolFiles) {
221 Jmsg(jcr, M_INFO, 0, _("Max Volume files exceeded. "
222 "Marking Volume \"%s\" as Used.\n"), mr->VolumeName);
223 bstrncpy(mr->VolStatus, "Used", sizeof(mr->VolStatus));
226 /* Finally, check Use duration expiration */
227 } else if (mr->VolUseDuration > 0) {
228 utime_t now = time(NULL);
229 /* See if Vol Use has expired */
230 if (mr->VolUseDuration <= (now - mr->FirstWritten)) {
231 Jmsg(jcr, M_INFO, 0, _("Max configured use duration exceeded. "
232 "Marking Volume \"%s\" as Used.\n"), mr->VolumeName);
233 bstrncpy(mr->VolStatus, "Used", sizeof(mr->VolStatus));
239 /* Need to update media */
240 if (!db_update_media_record(jcr, jcr->db, mr)) {
241 Jmsg(jcr, M_ERROR, 0, _("Catalog error updating volume \"%s\". ERR=%s"),
242 mr->VolumeName, db_strerror(jcr->db));
249 * Try hard to recycle the current volume
251 * Returns: on failure - reason = NULL
252 * on success - reason - pointer to reason
254 void check_if_volume_valid_or_recyclable(JCR *jcr, MEDIA_DBR *mr, const char **reason)
260 /* Check if a duration or limit has expired */
261 if (has_volume_expired(jcr, mr)) {
262 *reason = _("volume has expired");
263 /* Keep going because we may be able to recycle volume */
267 * Now see if we can use the volume as is
269 if (strcmp(mr->VolStatus, "Append") == 0 ||
270 strcmp(mr->VolStatus, "Recycle") == 0) {
276 * Check if the Volume is already marked for recycling
278 if (strcmp(mr->VolStatus, "Purged") == 0) {
279 if (recycle_volume(jcr, mr)) {
280 Jmsg(jcr, M_INFO, 0, _("Recycled current volume \"%s\"\n"), mr->VolumeName);
284 /* In principle this shouldn't happen */
285 *reason = _("and recycling of current volume failed");
290 /* At this point, the volume is not valid for writing */
291 *reason = _("but should be Append, Purged or Recycle");
294 * What we're trying to do here is see if the current volume is
295 * "recyclable" - ie. if we prune all expired jobs off it, is
296 * it now possible to reuse it for the job that it is currently
299 if ((mr->LastWritten + mr->VolRetention) < (utime_t)time(NULL)
300 && mr->Recycle && jcr->pool->recycle_current_volume
301 && (strcmp(mr->VolStatus, "Full") == 0 ||
302 strcmp(mr->VolStatus, "Used") == 0)) {
304 * Attempt prune of current volume to see if we can
305 * recycle it for use.
309 ua = new_ua_context(jcr);
310 ok = prune_volume(ua, mr);
314 /* If fully purged, recycle current volume */
315 if (recycle_volume(jcr, mr)) {
316 Jmsg(jcr, M_INFO, 0, _("Recycled current volume \"%s\"\n"), mr->VolumeName);
319 *reason = _("but should be Append, Purged or Recycle (recycling of the "
320 "current volume failed)");
323 *reason = _("but should be Append, Purged or Recycle (cannot automatically "
324 "recycle current volume, as it still contains unpruned data)");