From: Alain Spineux Date: Thu, 24 Nov 2022 12:22:10 +0000 (+0100) Subject: improve code for volume selection for encrypted volumes X-Git-Tag: Beta-15.0.0~340 X-Git-Url: http://git.ipfire.org/cgi-bin/gitweb.cgi?a=commitdiff_plain;h=e824c268231ba67e4c4d5995ac90ac271fdc11f7;p=thirdparty%2Fbacula.git improve code for volume selection for encrypted volumes --- diff --git a/bacula/src/cats/cats.h b/bacula/src/cats/cats.h index 241fa8ddd..84c1650be 100644 --- a/bacula/src/cats/cats.h +++ b/bacula/src/cats/cats.h @@ -71,6 +71,17 @@ typedef enum { void append_filter(POOLMEM **buf, char *cond); +/* Flags for find_next_volume_for_append() & sql_find */ +enum { + fnv_create_vol = true, + fnv_no_create_vol = false, + fnv_prune = true, + fnv_no_prune = false, + fnv_not_encrypted = false, + fnv_encrypted = true, + fnv_encrypted_any = -1 /* don't care if the volume is encrypted or not */ +}; + /* ============================================================== * * What follows are definitions that are used "globally" for all diff --git a/bacula/src/cats/sql_find.c b/bacula/src/cats/sql_find.c index bc5139391..3456d5a0f 100644 --- a/bacula/src/cats/sql_find.c +++ b/bacula/src/cats/sql_find.c @@ -405,6 +405,7 @@ bool BDB::bdb_find_last_jobid(JCR *jcr, const char *Name, JOB_DBR *jr) */ int BDB::bdb_find_next_volume(JCR *jcr, int item, bool InChanger, MEDIA_DBR *mr) { + POOL_MEM volencrypted(PM_FNAME); SQL_ROW row = NULL; int numrows; const char *order; @@ -418,6 +419,10 @@ int BDB::bdb_find_next_volume(JCR *jcr, int item, bool InChanger, MEDIA_DBR *mr) if (item == -1) { /* find oldest volume */ /* Find oldest volume */ + if (mr->VolEncrypted != fnv_encrypted_any) { + /* we have a requirement on VolEncrypted */ + Mmsg(volencrypted, "AND (VolStatus!='Append' OR VolEncrypted=%d) ", mr->VolEncrypted); + } Mmsg(cmd, "SELECT MediaId,VolumeName,VolJobs,VolFiles,VolBlocks," "VolBytes,VolMounts,VolErrors,VolWrites,MaxVolBytes,VolCapacityBytes," "MediaType,VolStatus,PoolId,VolRetention,VolUseDuration,MaxVolJobs," @@ -430,15 +435,14 @@ int BDB::bdb_find_next_volume(JCR *jcr, int item, bool InChanger, MEDIA_DBR *mr) "FROM Media WHERE PoolId=%s AND MediaType='%s' " " AND (VolStatus IN ('Full', 'Append', 'Used') OR (VolStatus IN ('Recycle', 'Purged', 'Used') AND Recycle=1)) " " AND Enabled=1 " - " AND (VolStatus != 'Append' OR VolEncrypted=%d) " + "%s" /* volencrypted */ "ORDER BY LastWritten LIMIT 1", - edit_int64(mr->PoolId, ed1), esc_type, mr->VolEncrypted); + edit_int64(mr->PoolId, ed1), esc_type, volencrypted.c_str()); item = 1; } else { POOL_MEM changer(PM_FNAME); POOL_MEM voltype(PM_FNAME); POOL_MEM exclude(PM_FNAME); - POOL_MEM volencrypted(PM_FNAME); /* Find next available volume */ /* ***FIXME*** * replace switch with @@ -470,7 +474,7 @@ int BDB::bdb_find_next_volume(JCR *jcr, int item, bool InChanger, MEDIA_DBR *mr) order = sql_media_order_most_recently_written[bdb_get_type_index()]; /* take most recently written */ } if (strcmp(mr->VolStatus, "Append") == 0) { - Mmsg(volencrypted, " AND VolEncrypted=%d", mr->VolEncrypted); + Mmsg(volencrypted, "AND VolEncrypted=%d", mr->VolEncrypted); } if (mr->VolType == 0) { Mmsg(voltype, ""); @@ -493,10 +497,10 @@ int BDB::bdb_find_next_volume(JCR *jcr, int item, bool InChanger, MEDIA_DBR *mr) "VolEncrypted " "FROM Media WHERE PoolId=%s AND MediaType='%s' AND Enabled=1 " "AND VolStatus='%s' " - "%s " - "%s " - "%s " - "%s " + "%s " /* volencrypted */ + "%s " /* voltype */ + "%s " /* changer */ + "%s " /* exclude */ "%s LIMIT %d", edit_int64(mr->PoolId, ed1), esc_type, esc_status, volencrypted.c_str(), diff --git a/bacula/src/dird/dird.h b/bacula/src/dird/dird.h index 311b69a8f..c4cc7deb2 100644 --- a/bacula/src/dird/dird.h +++ b/bacula/src/dird/dird.h @@ -60,14 +60,6 @@ struct del_ctx { int tot_ids; /* total to process */ }; -/* Flags for find_next_volume_for_append() */ -enum { - fnv_create_vol = true, - fnv_no_create_vol = false, - fnv_prune = true, - fnv_no_prune = false -}; - typedef struct { char *plugin_name; POOLMEM *content; diff --git a/bacula/src/dird/next_vol.c b/bacula/src/dird/next_vol.c index 91e0cd32e..981527ea8 100644 --- a/bacula/src/dird/next_vol.c +++ b/bacula/src/dird/next_vol.c @@ -121,11 +121,7 @@ int find_next_volume_for_append(JCR *jcr, MEDIA_DBR *mr, int index, /* Make sure we don't send two times the same volume in the same session */ set_volume_to_exclude_list(jcr, index, mr); - if (vol_encrypted == 1) { - mr->VolEncrypted = 1; - } else { - mr->VolEncrypted = 0; - } + mr->VolEncrypted = vol_encrypted; /* 0=no, 1=yes, -1=any */ /* * Find the Next Volume for Append diff --git a/bacula/src/dird/ua_output.c b/bacula/src/dird/ua_output.c index d5d61cf7f..9af03dfee 100644 --- a/bacula/src/dird/ua_output.c +++ b/bacula/src/dird/ua_output.c @@ -1199,7 +1199,7 @@ static bool list_nextvol(UAContext *ua, int ndays) set_storageid_in_mr(store.store, &mr); /* no need to set ScratchPoolId, since we use fnv_no_create_vol */ if (!find_next_volume_for_append(jcr, &mr, 1, fnv_no_create_vol, fnv_prune, - -1 /* no protect */, -1 /* vol_encrypted*/, errmsg)) { + -1 /* no protect */, fnv_encrypted_any, errmsg)) { ua->error_msg(_("Could not find next Volume for Job %s (Pool=%s, Level=%s).%s\n"), job->name(), pr.Name, level_to_str(edl, sizeof(edl), run->level), errmsg.c_str()); } else { diff --git a/bacula/src/dird/ua_status.c b/bacula/src/dird/ua_status.c index e454ad9fa..c83c613cc 100644 --- a/bacula/src/dird/ua_status.c +++ b/bacula/src/dird/ua_status.c @@ -778,7 +778,8 @@ static void prt_runtime(UAContext *ua, sched_pkt *sp, int novolume, OutputWriter } else { Dmsg0(250, "call find_next_volume_for_append\n"); /* no need to set ScratchPoolId, since we use fnv_no_create_vol */ - ok = find_next_volume_for_append(jcr, &mr, 1, fnv_no_create_vol, fnv_no_prune, -1, -1, tmp); + ok = find_next_volume_for_append(jcr, &mr, 1, fnv_no_create_vol, + fnv_no_prune, -1, fnv_encrypted_any, tmp); } } if (!ok) { diff --git a/bacula/src/stored/askdir.c b/bacula/src/stored/askdir.c index 31c8f2ebe..3476fa0f5 100644 --- a/bacula/src/stored/askdir.c +++ b/bacula/src/stored/askdir.c @@ -343,7 +343,7 @@ bool dir_find_next_appendable_volume(DCR *dcr) bash_spaces(dcr->pool_name); dir->fsend(Find_media, jcr->JobId, vol_index, dcr->pool_name, dcr->media_type, dcr->dev->dev_type, can_create, dcr->dev->use_protect(), - dcr->dev->use_volume_encryption()); + dcr->dev->use_volume_encryption()?1:0); unbash_spaces(dcr->media_type); unbash_spaces(dcr->pool_name); Dmsg1(dbglvl, ">dird %s", dir->msg);