/*
* Copyright (C) 2001-2004 Sistina Software, Inc. All rights reserved.
* Copyright (C) 2004-2007 Red Hat, Inc. All rights reserved.
*
* This file is part of LVM2.
*
* This copyrighted material is made available to anyone wishing to use,
* modify, copy, or redistribute it subject to the terms and conditions
* of the GNU Lesser General Public License v.2.1.
*
* You should have received a copy of the GNU Lesser General Public License
* along with this program; if not, write to the Free Software Foundation,
* Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
*/
#include "tools.h"
static int lvchange_permission(struct cmd_context *cmd,
struct logical_volume *lv)
{
uint32_t lv_access;
struct lvinfo info;
int r = 0;
/*
* If this mirror has a core log (i.e. !log_lv),
* then simply deactivating/activating will cause
* it to reset the sync status. We only need to
* worry about persistent logs.
*/
if (!log_lv && !(lv->status & MIRROR_NOTSYNCED)) {
if (active && !activate_lv(cmd, lv)) {
log_error("Failed to reactivate %s to resynchronize "
"mirror", lv->name);
return 0;
}
return 1;
}
lv->status &= ~MIRROR_NOTSYNCED;
if (log_lv) {
/* Separate mirror log so we can clear it */
detach_mirror_log(first_seg(lv));
if (!vg_write(lv->vg)) {
log_error("Failed to write intermediate VG metadata.");
if (!attach_mirror_log(first_seg(lv), log_lv))
stack;
if (active && !activate_lv(cmd, lv))
stack;
return 0;
}
if (!vg_commit(lv->vg)) {
log_error("Failed to commit intermediate VG metadata.");
if (!attach_mirror_log(first_seg(lv), log_lv))
stack;
if (active && !activate_lv(cmd, lv))
stack;
return 0;
}
backup(lv->vg);
if (!activate_lv(cmd, log_lv)) {
log_error("Unable to activate %s for mirror log resync",
log_lv->name);
return 0;
}
log_very_verbose("Clearing log device %s", log_lv->name);
if (!set_lv(cmd, log_lv, log_lv->size, 0)) {
log_error("Unable to reset sync status for %s", lv->name);
if (!deactivate_lv(cmd, log_lv))
log_error("Failed to deactivate log LV after "
"wiping failed");
return 0;
}
if (!deactivate_lv(cmd, log_lv)) {
log_error("Unable to deactivate log LV %s after wiping "
"for resync", log_lv->name);
return 0;
}
/* Put mirror log back in place */
if (!attach_mirror_log(first_seg(lv), log_lv))
stack;
}
log_very_verbose("Updating logical volume \"%s\" on disk(s)", lv->name);
if (!vg_write(lv->vg) || !vg_commit(lv->vg)) {
log_error("Failed to update metadata on disk.");
return 0;
}
if (active && !activate_lv(cmd, lv)) {
log_error("Failed to reactivate %s after resync", lv->name);
return 0;
}
return 1;
}
static int lvchange_alloc(struct cmd_context *cmd, struct logical_volume *lv)
{
int want_contiguous = 0;
alloc_policy_t alloc;
if (lv->read_ahead == read_ahead) {
if (read_ahead == DM_READ_AHEAD_AUTO)
log_error("Read ahead is already auto for \"%s\"", lv->name);
else
log_error("Read ahead is already %u for \"%s\"",
read_ahead, lv->name);
return 0;
}
lv->read_ahead = read_ahead;
log_verbose("Setting read ahead to %u for \"%s\"", read_ahead,
lv->name);
log_very_verbose("Updating logical volume \"%s\" on disk(s)", lv->name);
if (!vg_write(lv->vg))
return_0;
if (!suspend_lv(cmd, lv)) {
log_error("Failed to lock %s", lv->name);
vg_revert(lv->vg);
goto out;
}
if (!vg_commit(lv->vg)) {
resume_lv(cmd, lv);
goto_out;
}
log_very_verbose("Updating permissions for \"%s\" in kernel", lv->name);
if (!resume_lv(cmd, lv)) {
log_error("Problem reactivating %s", lv->name);
goto out;
}
r = 1;
out:
backup(lv->vg);
return r;
}
static int lvchange_persistent(struct cmd_context *cmd,
struct logical_volume *lv)
{
struct lvinfo info;
int active = 0;
if (!strcmp(arg_str_value(cmd, persistent_ARG, "n"), "n")) {
if (!(lv->status & FIXED_MINOR)) {
log_error("Minor number is already not persistent "
"for \"%s\"", lv->name);
return 0;
}
lv->status &= ~FIXED_MINOR;
lv->minor = -1;
lv->major = -1;
log_verbose("Disabling persistent device number for \"%s\"",
lv->name);
} else {
if (!arg_count(cmd, minor_ARG) && lv->minor < 0) {
log_error("Minor number must be specified with -My");
return 0;
}
if (!arg_count(cmd, major_ARG) && lv->major < 0) {
log_error("Major number must be specified with -My");
return 0;
}
if (lv_info(cmd, lv, &info, 0, 0) && info.exists)
active = 1;
if (active && !arg_count(cmd, force_ARG) &&
yes_no_prompt("Logical volume %s will be "
"deactivated temporarily. "
"Continue? [y/n]: ", lv->name) == 'n') {
log_print("%s device number not changed.",
lv->name);
return 0;
}
log_very_verbose("Updating logical volume \"%s\" on disk(s)", lv->name);
if (!vg_write(lv->vg) || !vg_commit(lv->vg))
return_0;
backup(lv->vg);
if (active) {
log_verbose("Re-activating logical volume \"%s\"", lv->name);
if (!activate_lv(cmd, lv)) {
log_error("%s: reactivation failed", lv->name);
return 0;
}
}
return 1;
}
static int lvchange_tag(struct cmd_context *cmd, struct logical_volume *lv,
int arg)
{
const char *tag;
if (!(tag = arg_str_value(cmd, arg, NULL))) {
log_error("Failed to get tag");
return 0;
}
if (!(lv->vg->fid->fmt->features & FMT_TAGS)) {
log_error("Logical volume %s/%s does not support tags",
lv->vg->name, lv->name);
return 0;
}
if ((arg == addtag_ARG)) {
if (!str_list_add(cmd->mem, &lv->tags, tag)) {
log_error("Failed to add tag %s to %s/%s",
tag, lv->vg->name, lv->name);
return 0;
}
} else {
if (!str_list_del(&lv->tags, tag)) {
log_error("Failed to remove tag %s from %s/%s",
tag, lv->vg->name, lv->name);
return 0;
}
}
log_very_verbose("Updating logical volume \"%s\" on disk(s)", lv->name);
/* No need to suspend LV for this change */
if (!vg_write(lv->vg) || !vg_commit(lv->vg))
return_0;
backup(lv->vg);
return 1;
}
static int lvchange_single(struct cmd_context *cmd, struct logical_volume *lv,
void *handle __attribute((unused)))
{
int doit = 0, docmds = 0;
int archived = 0;
struct logical_volume *origin;
if (lv->status & PVMOVE) {
log_error("Unable to change pvmove LV %s", lv->name);
if (arg_count(cmd, available_ARG))
log_error("Use 'pvmove --abort' to abandon a pvmove");
return ECMD_FAILED;
}
if (lv->status & MIRROR_LOG) {
log_error("Unable to change mirror log LV %s directly", lv->name);
return ECMD_FAILED;
}
if (lv->status & MIRROR_IMAGE) {
log_error("Unable to change mirror image LV %s directly",
lv->name);
return ECMD_FAILED;
}
/* If LV is sparse, activate origin instead */
if (arg_count(cmd, available_ARG) && lv_is_cow(lv) &&
lv_is_virtual_origin(origin = origin_from_cow(lv)))
lv = origin;
if (!(lv_is_visible(lv)) && !lv_is_virtual_origin(lv)) {
log_error("Unable to change internal LV %s directly",
lv->name);
return ECMD_FAILED;
}
int avail_only = /* i.e. only one of -a or --refresh is given */
!(arg_count(cmd, contiguous_ARG) || arg_count(cmd, permission_ARG) ||
arg_count(cmd, readahead_ARG) || arg_count(cmd, persistent_ARG) ||
arg_count(cmd, addtag_ARG) || arg_count(cmd, deltag_ARG) ||
arg_count(cmd, resync_ARG) || arg_count(cmd, alloc_ARG));
if (arg_count(cmd, ignorelockingfailure_ARG) && !avail_only) {
log_error("Only -a permitted with --ignorelockingfailure");
return EINVALID_CMD_LINE;
}
if (avail_only)
cmd->handles_missing_pvs = 1;
if (!argc) {
log_error("Please give logical volume path(s)");
return EINVALID_CMD_LINE;
}
if ((arg_count(cmd, minor_ARG) || arg_count(cmd, major_ARG)) &&
!arg_count(cmd, persistent_ARG)) {
log_error("--major and --minor require -My");
return EINVALID_CMD_LINE;
}
if (arg_count(cmd, minor_ARG) && argc != 1) {
log_error("Only give one logical volume when specifying minor");
return EINVALID_CMD_LINE;
}
if (arg_count(cmd, contiguous_ARG) && arg_count(cmd, alloc_ARG)) {
log_error("Only one of --alloc and --contiguous permitted");
return EINVALID_CMD_LINE;
}