1
0
mirror of git://sourceware.org/git/lvm2.git synced 2025-01-03 05:18:29 +03:00

Add vg_lock_newname() library function.

Various tools need to check for existence of a VG before doing something
(vgsplit, vgrename, vgcreate).  Currently we don't have an interface to
check for existence, but the existence check is part of the vg_read* call(s).
This patch is an attempt to pull out some of that functionality into a
separate function, and hopefully simplify our vg_read interface, and
move those patches along.

vg_lock_newname() is only concerned about checking whether a vg exists in
the system.  Unfortunately, we cannot just scan the system, but we must first
obtain a lock.  Since we are reserving a vgname, we take a WRITE lock on
the vgname.  Once obtained, we scan the system to ensure the name does
not exist.  The return codes and behavior is in the function header.
You might think of this function as similar to an open() call with
O_CREAT and O_EXCL flags (returns failure with -EEXIST if file already
exists).

NOTE: I think including the word "lock" in the function name is important,
as it clearly states the function obtains a lock and makes the code more
readable, especially when it comes to cleanup / unlocking.  The ultimate
function name is somewhat open for debate though so later we may rename.
This commit is contained in:
Dave Wysochanski 2009-06-09 14:29:10 +00:00
parent b4a2de670a
commit 07b0c948ee
2 changed files with 49 additions and 0 deletions

View File

@ -127,6 +127,8 @@ struct pv_segment;
#define FAILED_RESIZEABLE 0x00000020U
#define FAILED_CLUSTERED 0x00000040U
#define FAILED_ALLOCATION 0x00000080U
#define FAILED_EXIST 0x00000100U
#define SUCCESS 0x00000000U
/* Ordered list - see lv_manip.c */
typedef enum {
@ -401,6 +403,7 @@ vg_t *vg_lock_and_read(struct cmd_context *cmd, const char *vg_name,
const char *vgid,
uint32_t lock_flags, uint32_t status_flags,
uint32_t misc_flags);
uint32_t vg_lock_newname(struct cmd_context *cmd, const char *vgname);
/*
* Return a handle to VG metadata.

View File

@ -2908,6 +2908,52 @@ uint32_t vg_might_exist(vg_t *vg_handle)
return 1;
}
/*
* Lock a vgname and/or check for existence.
* Takes a WRITE lock on the vgname before scanning.
* If scanning fails or vgname found, release the lock.
* NOTE: If you find the return codes confusing, you might think of this
* function as similar to an open() call with O_CREAT and O_EXCL flags
* (open returns fail with -EEXIST if file already exists).
*
* Returns:
* FAILED_LOCKING - Cannot lock name
* FAILED_EXIST - VG name already exists - cannot reserve
* SUCCESS - VG name does not exist in system and WRITE lock held
*/
uint32_t vg_lock_newname(struct cmd_context *cmd, const char *vgname)
{
if (!lock_vol(cmd, vgname, LCK_VG_WRITE)) {
return FAILED_LOCKING;
}
/* Find the vgname in the cache */
/* If it's not there we must do full scan to be completely sure */
if (!fmt_from_vgname(vgname, NULL)) {
lvmcache_label_scan(cmd, 0);
if (!fmt_from_vgname(vgname, NULL)) {
if (memlock()) {
/*
* FIXME: Disallow calling this function if
* memlock() is true.
*/
unlock_vg(cmd, vgname);
return FAILED_LOCKING;
}
lvmcache_label_scan(cmd, 2);
if (!fmt_from_vgname(vgname, NULL)) {
/* vgname not found after scanning */
return SUCCESS;
}
}
}
/* Found vgname, cannot reserve */
unlock_vg(cmd, vgname);
return FAILED_EXIST;
}
/*
* Gets/Sets for external LVM library
*/