diff options
-rw-r--r-- | lib/device/dev-io.c | 7 | ||||
-rw-r--r-- | lib/device/device.h | 1 | ||||
-rw-r--r-- | tools/toollib.c | 34 |
3 files changed, 34 insertions, 8 deletions
diff --git a/lib/device/dev-io.c b/lib/device/dev-io.c index 9bf6d2e14..8aa446455 100644 --- a/lib/device/dev-io.c +++ b/lib/device/dev-io.c @@ -656,6 +656,13 @@ int dev_close_immediate(struct device *dev) return _dev_close(dev, 1); } +int dev_close_try(struct device *dev) +{ + if (dev->fd < 0) + return 1; + return _dev_close(dev, 0); +} + void dev_close_all(void) { struct dm_list *doh, *doht; diff --git a/lib/device/device.h b/lib/device/device.h index aaa009fc8..1195daa24 100644 --- a/lib/device/device.h +++ b/lib/device/device.h @@ -122,6 +122,7 @@ int dev_open_readonly_buffered(struct device *dev); int dev_open_readonly_quiet(struct device *dev); int dev_close(struct device *dev); int dev_close_immediate(struct device *dev); +int dev_close_try(struct device *dev); void dev_close_all(void); int dev_test_excl(struct device *dev); diff --git a/tools/toollib.c b/tools/toollib.c index 43702d3d8..ae0159cf7 100644 --- a/tools/toollib.c +++ b/tools/toollib.c @@ -3871,11 +3871,10 @@ static int _pvcreate_check_single(struct cmd_context *cmd, /* * This test will fail if the device belongs to an MD array. + * dev_close_try() is used below for this error case. */ - if (!dev_test_excl(pv->dev)) { - /* FIXME Detect whether device-mapper itself is still using it */ - log_error("Can't open %s exclusively. Mounted filesystem?", - pv_dev_name(pv)); + if (!dev_open_flags(pv->dev, O_EXCL | O_RDWR, 1, 1)) { + log_error("Can't open %s exclusively. Mounted filesystem?", pv_dev_name(pv)); dm_list_move(&pp->arg_fail, &pd->list); return 1; } @@ -3978,10 +3977,8 @@ static int _pv_confirm_single(struct cmd_context *cmd, return 1; /* Repeat the same from check_single. */ - if (!dev_test_excl(pv->dev)) { - /* FIXME Detect whether device-mapper itself is still using it */ - log_error("Can't open %s exclusively. Mounted filesystem?", - pv_dev_name(pv)); + if (!dev_open_flags(pv->dev, O_EXCL | O_RDWR, 1, 1)) { + log_error("Can't open %s exclusively. Mounted filesystem?", pv_dev_name(pv)); goto fail; } @@ -4359,6 +4356,9 @@ int pvcreate_each_device(struct cmd_context *cmd, if (dm_list_empty(&pp->prompts)) goto do_command; + dm_list_iterate_items(pd, &pp->arg_process) + dev_close_try(pd->dev); + /* * Prompts require asking the user, so release the orphans lock, ask * the questions, reacquire the orphans lock, verify that the PVs were @@ -4622,6 +4622,13 @@ do_command: if (!dm_list_empty(&pp->arg_fail)) goto_bad; + dm_list_iterate_items(pd, &pp->arg_create) + dev_close_try(pd->dev); + dm_list_iterate_items(pd, &pp->arg_remove) + dev_close_try(pd->dev); + dm_list_iterate_items(pd, &pp->arg_fail) + dev_close_try(pd->dev); + /* * Returns with VG_ORPHANS write lock held because vgcreate and * vgextend want to use the newly created PVs. @@ -4629,6 +4636,17 @@ do_command: return 1; bad: + dm_list_iterate_items(pd, &pp->arg_devices) + dev_close_try(pd->dev); + dm_list_iterate_items(pd, &pp->arg_process) + dev_close_try(pd->dev); + dm_list_iterate_items(pd, &pp->arg_create) + dev_close_try(pd->dev); + dm_list_iterate_items(pd, &pp->arg_remove) + dev_close_try(pd->dev); + dm_list_iterate_items(pd, &pp->arg_fail) + dev_close_try(pd->dev); + unlock_vg(cmd, VG_ORPHANS); out: return 0; |