libdm: Clean up error messages and dm_ioctl use.

This change removes unnecessary malloc() calls for dm_ioctls. It also
simplifies and fixes line endings on some error messages.

Bug: 110035986
Test: N/A
Change-Id: I2f56e5dab7f25cd9b2f80896f80101db56228981
This commit is contained in:
David Anderson 2018-06-19 15:37:25 -07:00
parent b9a2243eb3
commit b6181e3e46
2 changed files with 17 additions and 27 deletions

View file

@ -51,23 +51,18 @@ bool DeviceMapper::CreateDevice(const std::string& name) {
return false;
}
std::unique_ptr<struct dm_ioctl, decltype(&free)> io(
static_cast<struct dm_ioctl*>(malloc(sizeof(struct dm_ioctl))), free);
if (io == nullptr) {
LOG(ERROR) << "Failed to allocate dm_ioctl";
return false;
}
InitIo(io.get(), name);
struct dm_ioctl io;
InitIo(&io, name);
if (ioctl(fd_, DM_DEV_CREATE, io.get())) {
PLOG(ERROR) << "DM_DEV_CREATE failed to create [" << name << "]";
if (ioctl(fd_, DM_DEV_CREATE, &io)) {
PLOG(ERROR) << "DM_DEV_CREATE failed for [" << name << "]";
return false;
}
// Check to make sure the newly created device doesn't already have targets
// added or opened by someone
CHECK(io->target_count == 0) << "Unexpected targets for newly created [" << name << "] device";
CHECK(io->open_count == 0) << "Unexpected opens for newly created [" << name << "] device";
CHECK(io.target_count == 0) << "Unexpected targets for newly created [" << name << "] device";
CHECK(io.open_count == 0) << "Unexpected opens for newly created [" << name << "] device";
// Creates a new device mapper device with the name passed in
return true;
@ -84,22 +79,17 @@ bool DeviceMapper::DeleteDevice(const std::string& name) {
return false;
}
std::unique_ptr<struct dm_ioctl, decltype(&free)> io(
static_cast<struct dm_ioctl*>(malloc(sizeof(struct dm_ioctl))), free);
if (io == nullptr) {
LOG(ERROR) << "Failed to allocate dm_ioctl";
return false;
}
InitIo(io.get(), name);
struct dm_ioctl io;
InitIo(&io, name);
if (ioctl(fd_, DM_DEV_REMOVE, io.get())) {
PLOG(ERROR) << "DM_DEV_REMOVE failed to create [" << name << "]";
if (ioctl(fd_, DM_DEV_REMOVE, &io)) {
PLOG(ERROR) << "DM_DEV_REMOVE failed for [" << name << "]";
return false;
}
// Check to make sure appropriate uevent is generated so ueventd will
// do the right thing and remove the corresponding device node and symlinks.
CHECK(io->flags & DM_UEVENT_GENERATED_FLAG)
CHECK(io.flags & DM_UEVENT_GENERATED_FLAG)
<< "Didn't generate uevent for [" << name << "] removal";
return true;
@ -147,7 +137,7 @@ bool DeviceMapper::GetAvailableTargets(std::vector<DmTarget>* targets) {
io->data_start = sizeof(*io);
if (ioctl(fd_, DM_LIST_VERSIONS, io)) {
PLOG(ERROR) << "Failed to get DM_LIST_VERSIONS from kernel";
PLOG(ERROR) << "DM_LIST_VERSIONS failed";
return false;
}
@ -209,7 +199,7 @@ bool DeviceMapper::GetAvailableDevices(std::vector<DmBlockDevice>* devices) {
io->data_start = sizeof(*io);
if (ioctl(fd_, DM_LIST_DEVICES, io)) {
PLOG(ERROR) << "Failed to get DM_LIST_DEVICES from kernel";
PLOG(ERROR) << "DM_LIST_DEVICES failed";
return false;
}

View file

@ -49,14 +49,14 @@ static int Usage(void) {
static int DmCreateCmdHandler(int argc, char** argv) {
if (argc < 1) {
std::cerr << "DmCreateCmdHandler: atleast 'name' MUST be provided for target device";
std::cerr << "Usage: dmctl create <name> [table-args]" << std::endl;
return -EINVAL;
}
std::string name = argv[0];
DeviceMapper& dm = DeviceMapper::Instance();
if (!dm.CreateDevice(name)) {
std::cerr << "DmCreateCmdHandler: Failed to create " << name << " device";
std::cerr << "Failed to create device-mapper device with name: " << name << std::endl;
return -EIO;
}
@ -71,14 +71,14 @@ static int DmCreateCmdHandler(int argc, char** argv) {
static int DmDeleteCmdHandler(int argc, char** argv) {
if (argc < 1) {
std::cerr << "DmCreateCmdHandler: atleast 'name' MUST be provided for target device";
std::cerr << "Usage: dmctl delete <name>" << std::endl;
return -EINVAL;
}
std::string name = argv[0];
DeviceMapper& dm = DeviceMapper::Instance();
if (!dm.DeleteDevice(name)) {
std::cerr << "DmCreateCmdHandler: Failed to create " << name << " device";
std::cerr << "Failed to delete [" << name << "]" << std::endl;
return -EIO;
}