1
0
mirror of git://sourceware.org/git/lvm2.git synced 2024-12-21 13:34:40 +03:00

Revise internal locking semantics.

This commit is contained in:
Alasdair Kergon 2004-05-05 12:03:07 +00:00
parent 5668c1abad
commit 23289e6d14
13 changed files with 132 additions and 82 deletions

View File

@ -1,5 +1,7 @@
Version 2.00.16 -
=============================
Revise internal locking semantics.
Move find_pv_by_name to library.
Rename move->copy.
Add devices to segments report.
Begin separating out segment code. There's a lot of change here.

View File

@ -232,9 +232,14 @@ static int _file_lock_resource(struct cmd_context *cmd, const char *resource,
if (!lv_resume_if_active(cmd, resource))
return 0;
break;
case LCK_NULL:
log_debug("Locking LV %s (NL)", resource);
if (!lv_deactivate(cmd, resource))
return 0;
break;
case LCK_READ:
log_debug("Locking LV %s (R)", resource);
if (!lv_activate(cmd, resource))
if (!lv_activate_with_filter(cmd, resource))
return 0;
break;
case LCK_WRITE:
@ -244,7 +249,7 @@ static int _file_lock_resource(struct cmd_context *cmd, const char *resource,
break;
case LCK_EXCL:
log_debug("Locking LV %s (EX)", resource);
if (!lv_deactivate(cmd, resource))
if (!lv_activate_with_filter(cmd, resource))
return 0;
break;
default:

View File

@ -252,32 +252,54 @@ int lock_vol(struct cmd_context *cmd, const char *vol, int flags)
}
/* Unlock list of LVs */
int unlock_lvs(struct cmd_context *cmd, struct list *lvs)
int resume_lvs(struct cmd_context *cmd, struct list *lvs)
{
struct list *lvh;
struct logical_volume *lv;
list_iterate(lvh, lvs) {
lv = list_item(lvh, struct lv_list)->lv;
unlock_lv(cmd, lv->lvid.s);
resume_lv(cmd, lv->lvid.s);
}
return 1;
}
/* Lock a list of LVs */
int lock_lvs(struct cmd_context *cmd, struct list *lvs, int flags)
int suspend_lvs(struct cmd_context *cmd, struct list *lvs)
{
struct list *lvh;
struct logical_volume *lv;
list_iterate(lvh, lvs) {
lv = list_item(lvh, struct lv_list)->lv;
if (!lock_vol(cmd, lv->lvid.s, flags)) {
if (!suspend_lv(cmd, lv->lvid.s)) {
log_error("Failed to suspend %s", lv->name);
list_uniterate(lvh, lvs, lvh) {
lv = list_item(lvh, struct lv_list)->lv;
unlock_lv(cmd, lv->lvid.s);
resume_lv(cmd, lv->lvid.s);
}
return 0;
}
}
return 1;
}
/* Lock a list of LVs */
int activate_lvs_excl(struct cmd_context *cmd, struct list *lvs)
{
struct list *lvh;
struct logical_volume *lv;
list_iterate(lvh, lvs) {
lv = list_item(lvh, struct lv_list)->lv;
if (!activate_lv_excl(cmd, lv->lvid.s)) {
log_error("Failed to activate %s", lv->name);
list_uniterate(lvh, lvs, lvh) {
lv = list_item(lvh, struct lv_list)->lv;
activate_lv(cmd, lv->lvid.s);
}
return 0;

View File

@ -71,16 +71,23 @@ int check_lvm1_vg_inactive(struct cmd_context *cmd, const char *vgname);
#define LCK_VG_WRITE (LCK_VG | LCK_WRITE | LCK_HOLD)
#define LCK_VG_UNLOCK (LCK_VG | LCK_UNLOCK)
#define LCK_LV_SUSPEND (LCK_LV | LCK_WRITE)
#define LCK_LV_EXCLUSIVE (LCK_LV | LCK_EXCL | LCK_NONBLOCK)
#define LCK_LV_SUSPEND (LCK_LV | LCK_WRITE | LCK_NONBLOCK)
#define LCK_LV_RESUME (LCK_LV | LCK_UNLOCK | LCK_NONBLOCK)
#define LCK_LV_UNLOCK (LCK_LV | LCK_UNLOCK)
#define LCK_LV_ACTIVATE (LCK_LV | LCK_READ)
#define LCK_LV_DEACTIVATE (LCK_LV | LCK_EXCL)
#define LCK_LV_ACTIVATE (LCK_LV | LCK_READ | LCK_NONBLOCK)
#define LCK_LV_DEACTIVATE (LCK_LV | LCK_NULL | LCK_NONBLOCK)
#define unlock_lv(cmd, vol) lock_vol(cmd, vol, LCK_LV_UNLOCK)
#define unlock_vg(cmd, vol) lock_vol(cmd, vol, LCK_VG_UNLOCK)
/* Process list of LVs */
int lock_lvs(struct cmd_context *cmd, struct list *lvs, int flags);
int unlock_lvs(struct cmd_context *cmd, struct list *lvs);
#define resume_lv(cmd, vol) lock_vol(cmd, vol, LCK_LV_RESUME)
#define suspend_lv(cmd, vol) lock_vol(cmd, vol, LCK_LV_SUSPEND | LCK_HOLD)
#define deactivate_lv(cmd, vol) lock_vol(cmd, vol, LCK_LV_DEACTIVATE)
#define activate_lv(cmd, vol) lock_vol(cmd, vol, LCK_LV_ACTIVATE | LCK_HOLD)
#define activate_lv_excl(cmd, vol) \
lock_vol(cmd, vol, LCK_LV_EXCLUSIVE | LCK_HOLD)
/* Process list of LVs */
int suspend_lvs(struct cmd_context *cmd, struct list *lvs);
int resume_lvs(struct cmd_context *cmd, struct list *lvs);
int activate_lvs_excl(struct cmd_context *cmd, struct list *lvs);

View File

@ -53,14 +53,16 @@ static int _no_lock_resource(struct cmd_context *cmd, const char *resource,
break;
case LCK_LV:
switch (flags & LCK_TYPE_MASK) {
case LCK_NULL:
return lv_deactivate(cmd, resource);
case LCK_UNLOCK:
return lv_resume_if_active(cmd, resource);
case LCK_READ:
return lv_activate(cmd, resource);
return lv_activate_with_filter(cmd, resource);
case LCK_WRITE:
return lv_suspend_if_active(cmd, resource);
case LCK_EXCL:
return lv_deactivate(cmd, resource);
return lv_activate_with_filter(cmd, resource);
default:
break;
}

View File

@ -52,19 +52,19 @@ static int lvchange_permission(struct cmd_context *cmd,
backup(lv->vg);
if (!lock_vol(cmd, lv->lvid.s, LCK_LV_SUSPEND | LCK_HOLD)) {
if (!suspend_lv(cmd, lv->lvid.s)) {
log_error("Failed to lock %s", lv->name);
vg_revert(lv->vg);
return 0;
}
if (!vg_commit(lv->vg)) {
unlock_lv(cmd, lv->lvid.s);
resume_lv(cmd, lv->lvid.s);
return 0;
}
log_very_verbose("Updating permissions for \"%s\" in kernel", lv->name);
if (!unlock_lv(cmd, lv->lvid.s)) {
if (!resume_lv(cmd, lv->lvid.s)) {
log_error("Problem reactivating %s", lv->name);
return 0;
}
@ -84,7 +84,7 @@ static int lvchange_availability(struct cmd_context *cmd,
if (activate) {
/* FIXME Tighter locking if lv_is_origin() */
log_verbose("Activating logical volume \"%s\"", lv->name);
if (!lock_vol(cmd, lv->lvid.s, LCK_LV_ACTIVATE))
if (!activate_lv(cmd, lv->lvid.s))
return 0;
if ((lv->status & LOCKED) && (pv = get_pvmove_pv_from_lv(lv))) {
log_verbose("Spawning background pvmove process for %s",
@ -93,9 +93,11 @@ static int lvchange_availability(struct cmd_context *cmd,
}
} else {
log_verbose("Deactivating logical volume \"%s\"", lv->name);
if (!lock_vol(cmd, lv->lvid.s, LCK_LV_DEACTIVATE))
if (!deactivate_lv(cmd, lv->lvid.s)) {
stack;
return 0;
}
}
return 1;
}
@ -103,8 +105,7 @@ static int lvchange_availability(struct cmd_context *cmd,
static int lvchange_refresh(struct cmd_context *cmd, struct logical_volume *lv)
{
log_verbose("Refreshing logical volume \"%s\" (if active)", lv->name);
if (!lock_vol(cmd, lv->lvid.s, LCK_LV_SUSPEND | LCK_HOLD) ||
!unlock_lv(cmd, lv->lvid.s))
if (!suspend_lv(cmd, lv->lvid.s) || !resume_lv(cmd, lv->lvid.s))
return 0;
return 1;
@ -199,19 +200,19 @@ static int lvchange_readahead(struct cmd_context *cmd,
backup(lv->vg);
if (!lock_vol(cmd, lv->lvid.s, LCK_LV_SUSPEND | LCK_HOLD)) {
if (!suspend_lv(cmd, lv->lvid.s)) {
log_error("Failed to lock %s", lv->name);
vg_revert(lv->vg);
return 0;
}
if (!vg_commit(lv->vg)) {
unlock_lv(cmd, lv->lvid.s);
resume_lv(cmd, lv->lvid.s);
return 0;
}
log_very_verbose("Updating permissions for \"%s\" in kernel", lv->name);
if (!unlock_lv(cmd, lv->lvid.s)) {
if (!resume_lv(cmd, lv->lvid.s)) {
log_error("Problem reactivating %s", lv->name);
return 0;
}
@ -223,6 +224,7 @@ 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)) {
@ -247,17 +249,16 @@ static int lvchange_persistent(struct cmd_context *cmd,
if (lv_info(lv, &info) && info.exists &&
!arg_count(cmd, force_ARG)) {
if (yes_no_prompt("Logical volume %s will be "
"deactivated first. "
"Continue? [y/n]: ",
lv->name) == 'n') {
"deactivated temporarily. "
"Continue? [y/n]: ", lv->name) == 'n') {
log_print("%s device number not changed.",
lv->name);
return 0;
}
active = 1;
}
log_print("Ensuring %s is inactive. "
"(Reactivate using lvchange -ay.)", lv->name);
if (!lock_vol(cmd, lv->lvid.s, LCK_LV_DEACTIVATE)) {
log_print("Ensuring %s is inactive. ", lv->name);
if (!deactivate_lv(cmd, lv->lvid.s)) {
log_error("%s: deactivation failed", lv->name);
return 0;
}
@ -266,6 +267,14 @@ static int lvchange_persistent(struct cmd_context *cmd,
lv->major = arg_int_value(cmd, major_ARG, lv->major);
log_verbose("Setting persistent device number to (%d, %d) "
"for \"%s\"", lv->major, lv->minor, lv->name);
if (active) {
log_verbose("Re-activating logical volume \"%s\"",
lv->name);
if (!activate_lv(cmd, lv->lvid.s)) {
log_error("%s: reactivation failed", lv->name);
return 0;
}
}
}
log_very_verbose("Updating logical volume \"%s\" on disk(s)", lv->name);
@ -276,19 +285,19 @@ static int lvchange_persistent(struct cmd_context *cmd,
backup(lv->vg);
if (!lock_vol(cmd, lv->lvid.s, LCK_LV_SUSPEND | LCK_HOLD)) {
if (!suspend_lv(cmd, lv->lvid.s)) {
log_error("Failed to lock %s", lv->name);
vg_revert(lv->vg);
return 0;
}
if (!vg_commit(lv->vg)) {
unlock_lv(cmd, lv->lvid.s);
resume_lv(cmd, lv->lvid.s);
return 0;
}
log_very_verbose("Updating permissions for \"%s\" in kernel", lv->name);
if (!unlock_lv(cmd, lv->lvid.s)) {
if (!resume_lv(cmd, lv->lvid.s)) {
log_error("Problem reactivating %s", lv->name);
return 0;
}

View File

@ -528,7 +528,7 @@ static int _lvcreate(struct cmd_context *cmd, struct lvcreate_params *lp)
return 0;
}
if (!lock_vol(cmd, lv->lvid.s, LCK_LV_ACTIVATE)) {
if (!activate_lv(cmd, lv->lvid.s)) {
if (lp->snapshot)
/* FIXME Remove the failed lv we just added */
log_error("Aborting. Failed to activate snapshot "
@ -554,14 +554,14 @@ static int _lvcreate(struct cmd_context *cmd, struct lvcreate_params *lp)
/* Reset permission after zeroing */
if (!(lp->permission & LVM_WRITE))
lv->status &= ~LVM_WRITE;
if (!lock_vol(cmd, lv->lvid.s, LCK_LV_DEACTIVATE)) {
log_err("Couldn't unlock snapshot.");
if (!deactivate_lv(cmd, lv->lvid.s)) {
log_err("Couldn't deactivate new snapshot.");
return 0;
}
/* FIXME write/commit/backup sequence issue */
if (!lock_vol(cmd, org->lvid.s, LCK_LV_SUSPEND | LCK_HOLD)) {
log_error("Failed to lock origin %s", org->name);
if (!suspend_lv(cmd, org->lvid.s)) {
log_error("Failed to suspend origin %s", org->name);
return 0;
}
@ -574,7 +574,7 @@ static int _lvcreate(struct cmd_context *cmd, struct lvcreate_params *lp)
if (!vg_write(vg) || !vg_commit(vg))
return 0;
if (!unlock_lv(cmd, org->lvid.s)) {
if (!resume_lv(cmd, org->lvid.s)) {
log_error("Problem reactivating origin %s", org->name);
return 0;
}

View File

@ -62,7 +62,7 @@ static int lvremove_single(struct cmd_context *cmd, struct logical_volume *lv,
if (!archive(vg))
return ECMD_FAILED;
if (!lock_vol(cmd, lv->lvid.s, LCK_LV_DEACTIVATE)) {
if (!deactivate_lv(cmd, lv->lvid.s)) {
log_error("Unable to deactivate logical volume \"%s\"",
lv->name);
return ECMD_FAILED;

View File

@ -160,19 +160,18 @@ int lvrename(struct cmd_context *cmd, int argc, char **argv)
backup(lv->vg);
if (!lock_vol(cmd, lv->lvid.s, LCK_LV_SUSPEND | LCK_HOLD |
LCK_NONBLOCK)) {
if (!suspend_lv(cmd, lv->lvid.s)) {
stack;
goto error;
}
if (!vg_commit(vg)) {
stack;
unlock_lv(cmd, lv->lvid.s);
resume_lv(cmd, lv->lvid.s);
goto error;
}
unlock_lv(cmd, lv->lvid.s);
resume_lv(cmd, lv->lvid.s);
unlock_vg(cmd, vg_name);

View File

@ -380,19 +380,19 @@ int lvresize(struct cmd_context *cmd, int argc, char **argv)
else
lock_lvid = lv->lvid.s;
if (!lock_vol(cmd, lock_lvid, LCK_LV_SUSPEND | LCK_HOLD)) {
log_error("Can't get lock for %s", lv_name);
if (!suspend_lv(cmd, lock_lvid)) {
log_error("Failed to suspend %s", lv_name);
vg_revert(vg);
goto error;
}
if (!vg_commit(vg)) {
stack;
unlock_lv(cmd, lock_lvid);
resume_lv(cmd, lock_lvid);
goto error;
}
if (!unlock_lv(cmd, lock_lvid)) {
if (!resume_lv(cmd, lock_lvid)) {
log_error("Problem reactivating %s", lv_name);
goto error;
}

View File

@ -241,15 +241,15 @@ static int _update_metadata(struct cmd_context *cmd, struct volume_group *vg,
backup(vg);
if (!lock_lvs(cmd, lvs_changed, LCK_LV_SUSPEND | LCK_HOLD)) {
if (!suspend_lvs(cmd, lvs_changed)) {
stack;
return 0;
}
if (!first_time) {
if (!lock_vol(cmd, lv_mirr->lvid.s, LCK_LV_SUSPEND | LCK_HOLD)) {
if (!suspend_lv(cmd, lv_mirr->lvid.s)) {
stack;
unlock_lvs(cmd, lvs_changed);
resume_lvs(cmd, lvs_changed);
vg_revert(vg);
return 0;
}
@ -258,27 +258,27 @@ static int _update_metadata(struct cmd_context *cmd, struct volume_group *vg,
if (!vg_commit(vg)) {
log_error("ABORTING: Volume group metadata update failed.");
if (!first_time)
unlock_lv(cmd, lv_mirr->lvid.s);
unlock_lvs(cmd, lvs_changed);
resume_lv(cmd, lv_mirr->lvid.s);
resume_lvs(cmd, lvs_changed);
return 0;
}
if (first_time) {
if (!lock_vol(cmd, lv_mirr->lvid.s, LCK_LV_ACTIVATE)) {
if (!activate_lv(cmd, lv_mirr->lvid.s)) {
log_error
("ABORTING: Temporary mirror activation failed.");
unlock_lvs(cmd, lvs_changed);
resume_lvs(cmd, lvs_changed);
return 0;
}
} else if (!unlock_lv(cmd, lv_mirr->lvid.s)) {
log_error("Unable to unlock logical volume \"%s\"",
} else if (!resume_lv(cmd, lv_mirr->lvid.s)) {
log_error("Unable to reactivate logical volume \"%s\"",
lv_mirr->name);
unlock_lvs(cmd, lvs_changed);
resume_lvs(cmd, lvs_changed);
return 0;
}
if (!unlock_lvs(cmd, lvs_changed)) {
log_error("Unable to unlock logical volumes");
if (!resume_lvs(cmd, lvs_changed)) {
log_error("Unable to resume logical volumes");
return 0;
}
@ -331,7 +331,7 @@ static int _set_up_pvmove(struct cmd_context *cmd, const char *pv_name,
}
/* Ensure mirror LV is active */
if (!lock_vol(cmd, lv_mirr->lvid.s, LCK_LV_ACTIVATE)) {
if (!activate_lv(cmd, lv_mirr->lvid.s)) {
log_error
("ABORTING: Temporary mirror activation failed.");
unlock_vg(cmd, pv->vg_name);
@ -363,7 +363,7 @@ static int _set_up_pvmove(struct cmd_context *cmd, const char *pv_name,
}
}
if (!lock_lvs(cmd, lvs_changed, LCK_LV_ACTIVATE)) {
if (!activate_lvs_excl(cmd, lvs_changed)) {
stack;
unlock_vg(cmd, pv->vg_name);
return ECMD_FAILED;
@ -400,12 +400,12 @@ static int _finish_pvmove(struct cmd_context *cmd, struct volume_group *vg,
return 0;
}
if (!lock_lvs(cmd, lvs_changed, LCK_LV_SUSPEND | LCK_HOLD)) {
if (!suspend_lvs(cmd, lvs_changed)) {
log_error("Locking LVs to remove temporary mirror failed");
r = 0;
}
if (!lock_vol(cmd, lv_mirr->lvid.s, LCK_LV_SUSPEND | LCK_HOLD)) {
if (!suspend_lv(cmd, lv_mirr->lvid.s)) {
log_error("Suspension of temporary mirror LV failed");
r = 0;
}
@ -414,20 +414,20 @@ static int _finish_pvmove(struct cmd_context *cmd, struct volume_group *vg,
log_error("ABORTING: Failed to write new data locations "
"to disk.");
vg_revert(vg);
unlock_lv(cmd, lv_mirr->lvid.s);
unlock_lvs(cmd, lvs_changed);
resume_lv(cmd, lv_mirr->lvid.s);
resume_lvs(cmd, lvs_changed);
return 0;
}
if (!unlock_lv(cmd, lv_mirr->lvid.s)) {
log_error("Unable to unlock logical volume \"%s\"",
if (!resume_lv(cmd, lv_mirr->lvid.s)) {
log_error("Unable to reactivate logical volume \"%s\"",
lv_mirr->name);
r = 0;
}
unlock_lvs(cmd, lvs_changed);
resume_lvs(cmd, lvs_changed);
if (!lock_vol(cmd, lv_mirr->lvid.s, LCK_LV_DEACTIVATE)) {
if (!deactivate_lv(cmd, lv_mirr->lvid.s)) {
log_error("ABORTING: Unable to deactivate temporary logical "
"volume \"%s\"", lv_mirr->name);
r = 0;

View File

@ -16,7 +16,7 @@
#include "tools.h"
static int _activate_lvs_in_vg(struct cmd_context *cmd,
struct volume_group *vg, int lock)
struct volume_group *vg, int activate)
{
struct lv_list *lvl;
struct logical_volume *lv;
@ -31,10 +31,16 @@ static int _activate_lvs_in_vg(struct cmd_context *cmd,
continue;
/* Can't deactive a pvmove LV */
if ((lock == LCK_LV_DEACTIVATE) && (lv->status & PVMOVE))
if (!activate && (lv->status & PVMOVE))
continue;
if (!lock_vol(cmd, lv->lvid.s, lock | LCK_NONBLOCK))
if (!activate) {
if (!deactivate_lv(cmd, lv->lvid.s))
continue;
} else if (lv_is_origin(lv)) {
if (!activate_lv_excl(cmd, lv->lvid.s))
continue;
} else if (!activate_lv(cmd, lv->lvid.s))
continue;
if ((lv->status & PVMOVE) &&
@ -67,11 +73,11 @@ static int _vgchange_available(struct cmd_context *cmd, struct volume_group *vg)
log_verbose("%d logical volume(s) in volume group \"%s\" "
"already active", active, vg->name);
if (available && _activate_lvs_in_vg(cmd, vg, LCK_LV_ACTIVATE))
if (available && _activate_lvs_in_vg(cmd, vg, 1))
log_verbose("Activated logical volumes in "
"volume group \"%s\"", vg->name);
if (!available && _activate_lvs_in_vg(cmd, vg, LCK_LV_DEACTIVATE))
if (!available && _activate_lvs_in_vg(cmd, vg, 0))
log_verbose("Deactivated logical volumes in "
"volume group \"%s\"", vg->name);

View File

@ -61,8 +61,7 @@ static int _remove_lv(struct cmd_context *cmd, struct logical_volume *lv,
log_verbose("Deactivating (if active) logical volume %s",
lv->name);
if (!lock_vol(cmd, lv->lvid.s,
LCK_LV_DEACTIVATE | LCK_NONBLOCK)) {
if (!deactivate_lv(cmd, lv->lvid.s)) {
log_error("Failed to deactivate LV %s", lv->name);
return 0;
}
@ -70,8 +69,7 @@ static int _remove_lv(struct cmd_context *cmd, struct logical_volume *lv,
log_verbose("Deactivating (if active) logical volume %s "
"(origin of %s)", snap->origin->name, lv->name);
if (!lock_vol(cmd, snap->origin->lvid.s,
LCK_LV_DEACTIVATE | LCK_NONBLOCK)) {
if (!deactivate_lv(cmd, snap->origin->lvid.s)) {
log_error("Failed to deactivate LV %s",
snap->origin->name);
return 0;