Skip to content

Commit

Permalink
mac: fix accessing global variables in callback
Browse files Browse the repository at this point in the history
  • Loading branch information
sebsura committed Mar 25, 2024
1 parent 6f18ab1 commit 47a91dd
Show file tree
Hide file tree
Showing 3 changed files with 50 additions and 51 deletions.
73 changes: 42 additions & 31 deletions core/src/stored/mac.cc
Expand Up @@ -65,9 +65,14 @@ static char ReplicateData[] = "replicate data %d\n";
static char end_replicate[] = "end replicate\n";


/* get information from first original SOS label for our job */
static bool found_first_sos_label = false;

/* last callback information of our job */
struct cb_data {
bool found_first_sos_label{false};
uint32_t last_VolSessionId{0};
uint32_t last_VolSessionTime{0};
int32_t last_FileIndex{0};
int32_t last_Stream{0};
};

/**
* Get response from Storage daemon to a command we sent.
Expand Down Expand Up @@ -109,7 +114,9 @@ static bool response(JobControlRecord* jcr,
* Returns: true if OK
* false if error
*/
static bool CloneRecordInternally(DeviceControlRecord* dcr, DeviceRecord* rec)
static bool CloneRecordInternally(DeviceControlRecord* dcr,
DeviceRecord* rec,
cb_data* data)
{
bool retval = false;
bool translated_record = false;
Expand All @@ -127,9 +134,9 @@ static bool CloneRecordInternally(DeviceControlRecord* dcr, DeviceRecord* rec)

if (rec->FileIndex < 0) {
if (rec->FileIndex == SOS_LABEL) {
if (!found_first_sos_label) {
if (!data->found_first_sos_label) {
Dmsg0(200, "Found first SOS_LABEL and adopting job info\n");
found_first_sos_label = true;
data->found_first_sos_label = true;

Session_Label sos_label;

Expand Down Expand Up @@ -177,13 +184,13 @@ static bool CloneRecordInternally(DeviceControlRecord* dcr, DeviceRecord* rec)
* JobFiles, which we then use as the output FileIndex. */
if (rec->FileIndex >= 0) {
// If something changed, increment FileIndex
if (rec->VolSessionId != rec->last_VolSessionId
|| rec->VolSessionTime != rec->last_VolSessionTime
|| rec->FileIndex != rec->last_FileIndex) {
if (rec->VolSessionId != data->last_VolSessionId
|| rec->VolSessionTime != data->last_VolSessionTime
|| rec->FileIndex != data->last_FileIndex) {
jcr->JobFiles++;
rec->last_VolSessionId = rec->VolSessionId;
rec->last_VolSessionTime = rec->VolSessionTime;
rec->last_FileIndex = rec->FileIndex;
data->last_VolSessionId = rec->VolSessionId;
data->last_VolSessionTime = rec->VolSessionTime;
data->last_FileIndex = rec->FileIndex;
}
rec->FileIndex = jcr->JobFiles; /* set sequential output FileIndex */
}
Expand Down Expand Up @@ -256,8 +263,8 @@ static bool CloneRecordInternally(DeviceControlRecord* dcr, DeviceRecord* rec)
/* Restore packet -- the read record function uses this information
* to check if the job changed, so we need to restore it to how it was;
* otherwise the record will get zeroed on job change. */
rec->VolSessionId = rec->last_VolSessionId;
rec->VolSessionTime = rec->last_VolSessionTime;
rec->VolSessionId = data->last_VolSessionId;
rec->VolSessionTime = data->last_VolSessionTime;

if (translated_record) {
FreeRecord(jcr->sd_impl->dcr->after_rec);
Expand All @@ -275,7 +282,9 @@ static bool CloneRecordInternally(DeviceControlRecord* dcr, DeviceRecord* rec)
* Returns: true if OK
* false if error
*/
static bool CloneRecordToRemoteSd(DeviceControlRecord* dcr, DeviceRecord* rec)
static bool CloneRecordToRemoteSd(DeviceControlRecord* dcr,
DeviceRecord* rec,
cb_data* data)
{
POOLMEM* msgsave;
JobControlRecord* jcr = dcr->jcr;
Expand All @@ -287,33 +296,33 @@ static bool CloneRecordToRemoteSd(DeviceControlRecord* dcr, DeviceRecord* rec)
if (rec->FileIndex < 0) { return true; }

// See if this is the first record being processed.
if (rec->last_FileIndex == 0) {
if (data->last_FileIndex == 0) {
/* Initialize the last counters so we can compare
* things in the next run through here. */
rec->last_VolSessionId = rec->VolSessionId;
rec->last_VolSessionTime = rec->VolSessionTime;
rec->last_FileIndex = rec->FileIndex;
rec->last_Stream = rec->Stream;
data->last_VolSessionId = rec->VolSessionId;
data->last_VolSessionTime = rec->VolSessionTime;
data->last_FileIndex = rec->FileIndex;
data->last_Stream = rec->Stream;
jcr->JobFiles = 1;

// Need to send both a new header only.
send_eod = false;
send_header = true;
} else {
// See if we are changing file or stream type.
if (rec->VolSessionId != rec->last_VolSessionId
|| rec->VolSessionTime != rec->last_VolSessionTime
|| rec->FileIndex != rec->last_FileIndex
|| rec->Stream != rec->last_Stream) {
if (rec->VolSessionId != data->last_VolSessionId
|| rec->VolSessionTime != data->last_VolSessionTime
|| rec->FileIndex != data->last_FileIndex
|| rec->Stream != data->last_Stream) {
/* See if we are changing the FileIndex e.g.
* start processing the next file in the backup stream. */
if (rec->FileIndex != rec->last_FileIndex) { jcr->JobFiles++; }
if (rec->FileIndex != data->last_FileIndex) { jcr->JobFiles++; }

// Keep track of the new state.
rec->last_VolSessionId = rec->VolSessionId;
rec->last_VolSessionTime = rec->VolSessionTime;
rec->last_FileIndex = rec->FileIndex;
rec->last_Stream = rec->Stream;
data->last_VolSessionId = rec->VolSessionId;
data->last_VolSessionTime = rec->VolSessionTime;
data->last_FileIndex = rec->FileIndex;
data->last_Stream = rec->Stream;

// Need to send both a EOD and a new header.
send_eod = true;
Expand Down Expand Up @@ -545,9 +554,10 @@ bool DoMacRun(JobControlRecord* jcr)
now = (utime_t)time(NULL);
UpdateJobStatistics(jcr, now);

cb_data data{};
// Read all data and send it to remote SD.
ok = ReadRecords(jcr->sd_impl->read_dcr, CloneRecordToRemoteSd,
MountNextReadVolume);
MountNextReadVolume, &data);

/* Send the last EOD to close the last data transfer and a next EOD to
* signal the remote we are done. */
Expand Down Expand Up @@ -641,9 +651,10 @@ bool DoMacRun(JobControlRecord* jcr)
SetStartVolPosition(jcr->sd_impl->dcr);
jcr->JobFiles = 0;

cb_data data{};
// Read all data and make a local clone of it.
ok = ReadRecords(jcr->sd_impl->read_dcr, CloneRecordInternally,
MountNextReadVolume);
MountNextReadVolume, &data);
}

bail_out:
Expand Down
6 changes: 1 addition & 5 deletions core/src/stored/record.cc
Expand Up @@ -2,7 +2,7 @@
BAREOS® - Backup Archiving REcovery Open Sourced
Copyright (C) 2001-2012 Free Software Foundation Europe e.V.
Copyright (C) 2016-2023 Bareos GmbH & Co. KG
Copyright (C) 2016-2024 Bareos GmbH & Co. KG
This program is Free Software; you can redistribute it and/or
modify it under the terms of version three of the GNU Affero General Public
Expand Down Expand Up @@ -519,10 +519,6 @@ void DumpRecord(const char* tag, const DeviceRecord* rec)
Dmsg2(100, "%-14s %p\n", "bsr", rec->bsr);
Dmsg2(100, "%-14s %p\n", "data", rec->data);
Dmsg2(100, "%-14s %d\n", "match_stat", rec->match_stat);
Dmsg2(100, "%-14s %u\n", "last_VolSessionId", rec->last_VolSessionId);
Dmsg2(100, "%-14s %u\n", "last_VolSessionTime", rec->last_VolSessionTime);
Dmsg2(100, "%-14s %d\n", "last_FileIndex", rec->last_FileIndex);
Dmsg2(100, "%-14s %d\n", "last_Stream", rec->last_Stream);
Dmsg2(100, "%-14s %s\n", "own_mempool", rec->own_mempool ? "true" : "false");
}

Expand Down
22 changes: 7 additions & 15 deletions core/src/stored/record.h
Expand Up @@ -3,7 +3,7 @@
Copyright (C) 2000-2012 Free Software Foundation Europe e.V.
Copyright (C) 2011-2012 Planets Communications B.V.
Copyright (C) 2013-2021 Bareos GmbH & Co. KG
Copyright (C) 2013-2024 Bareos GmbH & Co. KG
This program is Free Software; you can redistribute it and/or
modify it under the terms of version three of the GNU Affero General Public
Expand Down Expand Up @@ -121,12 +121,8 @@ struct DeviceRecord {
char state_bits[REC_STATE_BYTES]{}; /**< State bits */
rec_state state{st_none}; /**< State of WriteRecordToBlock */
BootStrapRecord* bsr{nullptr}; /**< Pointer to bsr that matched */
POOLMEM* data{nullptr}; /**< Record data. This MUST be a memory pool item */
int32_t match_stat{0}; /**< BootStrapRecord match status */
uint32_t last_VolSessionId{0}; /**< Used in sequencing FI for Vbackup */
uint32_t last_VolSessionTime{0};
int32_t last_FileIndex{0};
int32_t last_Stream{0}; /**< Used in SD-SD replication */
POOLMEM* data{nullptr}; /**< Record data. This MUST be a memory pool item */
int32_t match_stat{0}; /**< BootStrapRecord match status */
bool own_mempool{false}; /**< Do we own the POOLMEM pointed to in data ? */
};

Expand All @@ -150,17 +146,13 @@ struct DeviceRecord {
* ser_volume_label() and UnserVolumeLabel() and is slightly different.
*/
struct Volume_Label {
/*
* The first items in this structure are saved
/* The first items in this structure are saved
* in the Device buffer, but are not actually written
* to the tape.
*/
* to the tape. */
int32_t LabelType{}; /**< This is written in header only */
uint32_t LabelSize{}; /**< length of serialized label */
/*
* The items below this line are stored on
* the tape
*/
/* The items below this line are stored on
* the tape */
char Id[32]{}; /**< Bareos Immortal ... */

uint32_t VerNum{}; /**< Label version number */
Expand Down

0 comments on commit 47a91dd

Please sign in to comment.