Skip to content

Commit

Permalink
stored: replace a dlist by std::vector
Browse files Browse the repository at this point in the history
- DeviceControlRecords attached to a Device are now stored in a std::vector
- Device is pure virtual therefore added a derived class needed for Spooling
  • Loading branch information
franku committed May 6, 2020
1 parent 55fbbd4 commit 9332469
Show file tree
Hide file tree
Showing 9 changed files with 44 additions and 35 deletions.
15 changes: 10 additions & 5 deletions core/src/stored/acquire.cc
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,8 @@
#include "stored/block.h"
#include "stored/jcr_private.h"

#include <algorithm>

namespace storagedaemon {

DeviceControlRecord::DeviceControlRecord()
Expand Down Expand Up @@ -821,8 +823,8 @@ static void AttachDcrToDev(DeviceControlRecord* dcr)
jcr->getJobType() != JT_SYSTEM) {
dev->Lock();
Dmsg4(200, "Attach Jid=%d dcr=%p size=%d dev=%s\n", (uint32_t)jcr->JobId,
dcr, dev->attached_dcrs->size(), dev->print_name());
dev->attached_dcrs->append(dcr); /* attach dcr to device */
dcr, dev->attached_dcrs.size(), dev->print_name());
dev->attached_dcrs.push_back(dcr); /* attach dcr to device */
dev->Unlock();
dcr->attached_to_dev = true;
}
Expand All @@ -842,11 +844,14 @@ static void LockedDetachDcrFromDev(DeviceControlRecord* dcr)
dcr->UnreserveDevice();
dev->Lock();
Dmsg4(200, "Detach Jid=%d dcr=%p size=%d to dev=%s\n",
(uint32_t)dcr->jcr->JobId, dcr, dev->attached_dcrs->size(),
(uint32_t)dcr->jcr->JobId, dcr, dev->attached_dcrs.size(),
dev->print_name());
dcr->attached_to_dev = false;
if (dev->attached_dcrs->size()) {
dev->attached_dcrs->remove(dcr); /* detach dcr from device */
if (!dev->attached_dcrs.empty()) {
// detach dcr from device
auto it = std::remove(dev->attached_dcrs.begin(),
dev->attached_dcrs.end(), dcr);
dev->attached_dcrs.erase(it, dev->attached_dcrs.end());
}
// RemoveDcrFromDcrs(dcr); /* remove dcr from jcr list */
dev->Unlock();
Expand Down
6 changes: 2 additions & 4 deletions core/src/stored/block.cc
Original file line number Diff line number Diff line change
Expand Up @@ -498,8 +498,7 @@ static bool TerminateWritingVolume(DeviceControlRecord* dcr)
* Walk through all attached dcrs setting flag to call
* SetNewFileParameters() when that dcr is next used.
*/
DeviceControlRecord* mdcr;
foreach_dlist (mdcr, dev->attached_dcrs) {
for (auto mdcr : dev->attached_dcrs) {
if (mdcr->jcr->JobId == 0) { continue; }
mdcr->NewFile = true; /* set reminder to do set_new_file_params */
}
Expand Down Expand Up @@ -558,8 +557,7 @@ static bool DoNewFileBookkeeping(DeviceControlRecord* dcr)
* Walk through all attached dcrs setting flag to call
* SetNewFileParameters() when that dcr is next used.
*/
DeviceControlRecord* mdcr;
foreach_dlist (mdcr, dev->attached_dcrs) {
for (auto mdcr : dev->attached_dcrs) {
if (mdcr->jcr->JobId == 0) { continue; }
mdcr->NewFile = true; /* set reminder to do set_new_file_params */
}
Expand Down
8 changes: 3 additions & 5 deletions core/src/stored/bscan.cc
Original file line number Diff line number Diff line change
Expand Up @@ -402,10 +402,9 @@ static bool BscanMountNextReadVolume(DeviceControlRecord* dcr)
{
bool status;
Device* dev = dcr->dev;
DeviceControlRecord* mdcr;

Dmsg1(100, "Walk attached jcrs. Volume=%s\n", dev->getVolCatName());
foreach_dlist (mdcr, dev->attached_dcrs) {
for (auto mdcr : dev->attached_dcrs) {
JobControlRecord* mjcr = mdcr->jcr;
Dmsg1(000, "========== JobId=%u ========\n", mjcr->JobId);
if (mjcr->JobId == 0) { continue; }
Expand Down Expand Up @@ -621,7 +620,7 @@ static bool RecordCb(DeviceControlRecord* dcr, DeviceRecord* rec)
/*
* Reset some DeviceControlRecord variables
*/
foreach_dlist (dcr, dev->attached_dcrs) {
for (auto dcr : dev->attached_dcrs) {
dcr->VolFirstIndex = dcr->FileIndex = 0;
dcr->StartBlock = dcr->EndBlock = 0;
dcr->StartFile = dcr->EndFile = 0;
Expand Down Expand Up @@ -787,8 +786,7 @@ static bool RecordCb(DeviceControlRecord* dcr, DeviceRecord* rec)
* Wiffle through all jobs still open and close them.
*/
if (update_db) {
DeviceControlRecord* mdcr;
foreach_dlist (mdcr, dev->attached_dcrs) {
for (auto mdcr : dev->attached_dcrs) {
JobControlRecord* mjcr = mdcr->jcr;
if (!mjcr || mjcr->JobId == 0) { continue; }
jr.JobId = mjcr->JobId;
Expand Down
8 changes: 2 additions & 6 deletions core/src/stored/dev.cc
Original file line number Diff line number Diff line change
Expand Up @@ -144,7 +144,6 @@ static inline Device* init_dev(JobControlRecord* jcr,
{
struct stat statp;
int errstat;
DeviceControlRecord* dcr = NULL;
Device* dev = NULL;
uint32_t max_bs;

Expand Down Expand Up @@ -404,7 +403,7 @@ static inline Device* init_dev(JobControlRecord* jcr,
}

dev->ClearOpened();
dev->attached_dcrs = new dlist(dcr, &dcr->dev_link);
dev->attached_dcrs.clear();
Dmsg2(100, "InitDev: tape=%d dev_name=%s\n", dev->IsTape(), dev->dev_name);
dev->initiated = true;
Dmsg3(100, "dev=%s dev_max_bs=%u max_bs=%u\n", dev->dev_name,
Expand Down Expand Up @@ -1307,10 +1306,7 @@ void Device::term()
pthread_cond_destroy(&wait_next_vol);
pthread_mutex_destroy(&spool_mutex);
// RwlDestroy(&lock);
if (attached_dcrs) {
delete attached_dcrs;
attached_dcrs = NULL;
}
attached_dcrs.clear();
if (device) { device->dev = NULL; }
delete this;
}
Expand Down
18 changes: 17 additions & 1 deletion core/src/stored/dev.h
Original file line number Diff line number Diff line change
Expand Up @@ -65,6 +65,8 @@
#include "stored/record.h"
#include "stored/volume_catalog_info.h"

#include <vector>

class dlist;

namespace storagedaemon {
Expand Down Expand Up @@ -241,7 +243,7 @@ class Device {
Device() = default;
virtual ~Device() = default;
Device* volatile swap_dev{}; /**< Swap vol from this device */
dlist* attached_dcrs{}; /**< Attached DeviceControlRecord list */
std::vector<DeviceControlRecord*> attached_dcrs; /**< Attached DeviceControlRecords */
pthread_mutex_t mutex_ = PTHREAD_MUTEX_INITIALIZER; /**< Access control */
pthread_mutex_t spool_mutex = PTHREAD_MUTEX_INITIALIZER; /**< Mutex for updating spool_size */
pthread_mutex_t acquire_mutex = PTHREAD_MUTEX_INITIALIZER; /**< Mutex for acquire code */
Expand Down Expand Up @@ -550,6 +552,20 @@ class Device {
protected:
void set_mode(int mode);
};

class SpoolDevice :public Device
{
public:
int d_ioctl(int fd, ioctl_req_t request, char* mt_com = NULL) override {return -1;}
int d_open(const char* pathname, int flags, int mode) override {return -1;}
int d_close(int fd) override {return -1;}
ssize_t d_read(int fd, void* buffer, size_t count) override { return 0;}
ssize_t d_write(int fd, const void* buffer, size_t count) override { return 0;}
boffset_t d_lseek(DeviceControlRecord* dcr,
boffset_t offset,
int whence) override { return 0;}
bool d_truncate(DeviceControlRecord* dcr) override {return false;}
};
/* clang-format on */

inline const char* Device::strerror() const { return errmsg; }
Expand Down
3 changes: 1 addition & 2 deletions core/src/stored/device.cc
Original file line number Diff line number Diff line change
Expand Up @@ -163,8 +163,7 @@ bool FixupDeviceBlockWriteError(DeviceControlRecord* dcr, int retries)
* Walk through all attached jcrs indicating the volume has changed
*/
Dmsg1(100, "Notify vol change. Volume=%s\n", dev->getVolCatName());
DeviceControlRecord* mdcr;
foreach_dlist (mdcr, dev->attached_dcrs) {
for (auto mdcr : dev->attached_dcrs) {
JobControlRecord* mjcr = mdcr->jcr;
if (mjcr->JobId == 0) { continue; /* ignore console */ }
mdcr->NewVol = true;
Expand Down
1 change: 0 additions & 1 deletion core/src/stored/device_control_record.h
Original file line number Diff line number Diff line change
Expand Up @@ -68,7 +68,6 @@ class DeviceControlRecord {
bool will_write_{}; /**< Set if DeviceControlRecord will be used for writing */

public:
dlink dev_link; /**< Link to attach to dev */
JobControlRecord* jcr{}; /**< Pointer to JobControlRecord */
pthread_mutex_t mutex_ = PTHREAD_MUTEX_INITIALIZER; /**< Access control */
pthread_mutex_t r_mutex = PTHREAD_MUTEX_INITIALIZER; /**< rLock pre-mutex */
Expand Down
14 changes: 7 additions & 7 deletions core/src/stored/spool.cc
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,8 @@
#include "lib/util.h"
#include "include/jcr.h"

#include "include/make_unique.h"

namespace storagedaemon {

/* Forward referenced subroutines */
Expand Down Expand Up @@ -242,7 +244,6 @@ static const char* spool_name = "*spool*";
*/
static bool DespoolData(DeviceControlRecord* dcr, bool commit)
{
Device* rdev;
DeviceControlRecord* rdcr;
bool ok = true;
DeviceBlock* block;
Expand Down Expand Up @@ -291,17 +292,16 @@ static bool DespoolData(DeviceControlRecord* dcr, bool commit)
* We create a dev structure to read from the spool file
* in rdev and rdcr.
*/
rdev = (Device*)malloc(sizeof(Device));
memset((void*)rdev, 0, sizeof(Device));
auto rdev(std::make_unique<SpoolDevice>());
rdev->dev_name = GetMemory(strlen(spool_name) + 1);
bstrncpy(rdev->dev_name, spool_name, SizeofPoolMemory(rdev->dev_name));
rdev->errmsg = GetPoolMemory(PM_EMSG);
*rdev->errmsg = 0;
rdev->errmsg[0] = 0;
rdev->max_block_size = dcr->dev->max_block_size;
rdev->min_block_size = dcr->dev->min_block_size;
rdev->device = dcr->dev->device;
rdcr = dcr->get_new_spooling_dcr();
SetupNewDcrDevice(jcr, rdcr, rdev, NULL);
SetupNewDcrDevice(jcr, rdcr, rdev.get(), NULL);
rdcr->spool_fd = dcr->spool_fd;
block = dcr->block; /* save block */
dcr->block = rdcr->block; /* make read and write block the same */
Expand Down Expand Up @@ -416,12 +416,12 @@ static bool DespoolData(DeviceControlRecord* dcr, bool commit)
FreePoolMemory(rdev->errmsg);

/*
* Be careful to NULL the jcr and free rdev after FreeDcr()
* null the jcr
* rdev will be freed by its smart pointer
*/
rdcr->jcr = NULL;
rdcr->SetDev(NULL);
FreeDeviceControlRecord(rdcr);
free(rdev);
dcr->spooling = true; /* turn on spooling again */
dcr->despooling = false;

Expand Down
6 changes: 2 additions & 4 deletions core/src/stored/status.cc
Original file line number Diff line number Diff line change
Expand Up @@ -541,10 +541,9 @@ static void SendBlockedStatus(Device* dev, StatusPacket* sp)
sp->send(msg, len);
break;
case BST_WAITING_FOR_SYSOP: {
DeviceControlRecord* dcr;
bool found_jcr = false;
dev->Lock();
foreach_dlist (dcr, dev->attached_dcrs) {
for (auto dcr : dev->attached_dcrs) {
if (dcr->jcr->JobStatus == JS_WaitMount) {
len = Mmsg(
msg,
Expand Down Expand Up @@ -601,7 +600,6 @@ static void SendBlockedStatus(Device* dev, StatusPacket* sp)
static void SendDeviceStatus(Device* dev, StatusPacket* sp)
{
int len;
DeviceControlRecord* dcr = NULL;
bool found = false;
PoolMem msg(PM_MESSAGE);

Expand Down Expand Up @@ -646,7 +644,7 @@ static void SendDeviceStatus(Device* dev, StatusPacket* sp)
len = Mmsg(msg, _("Attached Jobs: "));
sp->send(msg, len);
dev->Lock();
foreach_dlist (dcr, dev->attached_dcrs) {
for (auto dcr : dev->attached_dcrs) {
if (dcr->jcr) {
if (found) {
len = Mmsg(msg, ",%d", (int)dcr->jcr->JobId);
Expand Down

0 comments on commit 9332469

Please sign in to comment.