Allow 'zpool replace' to use short device names

The 'zpool replace' command would fail when given a short name
because unlike on other platforms the short name cannot be
deterministically expanded to a single path.  Multiple path
prefixes must be checked and in addition the partition suffix
for whole disks is determined by the prefix.

To handle this complexity a zfs_strcmp_pathname() function was
added which takes either a short or fully qualified device name.
Short names will be expanded using the prefixes in the default
import search path, or the ZPOOL_IMPORT_PATH environment variable
if it's defined.  All posible expansions are then compared against
the comparison path.  Care is taken to strip redundant slashes to
ensure legitimate matches are not missed.

In the context of this work the existing zfs_resolve_shortname()
function was extended to consider the ZPOOL_IMPORT_PATH when set.
The zfs_append_partition() interface was also simplified to take
only a single buffer.

The vast majority of these changes rework existing Linux specific
code which was originally written to accomidate udev.  However,
there is some minimal cleanup which removes Illumos specific code.
This was done to improve readability but the basic flow and intent
of the upstream code was maintained.

These changes are the logical conclusion of the previos work to
adjust the 'zpool import' search behavior, see commit 44867b6a.

Signed-off-by: Brian Behlendorf <behlendorf1@llnl.gov>
Closes #544
Closes #976
This commit is contained in:
Brian Behlendorf 2012-10-17 16:58:54 -07:00
parent c7dfc08629
commit eac4720465
5 changed files with 183 additions and 93 deletions

View File

@ -366,16 +366,18 @@ is_whole_disk(const char *path)
/* /*
* This may be a shorthand device path or it could be total gibberish. * This may be a shorthand device path or it could be total gibberish.
* Check to see if it's a known device in /dev/, /dev/disk/by-id, * Check to see if it is a known device available in zfs_vdev_paths.
* /dev/disk/by-label, /dev/disk/by-path, /dev/disk/by-uuid, * As part of this check, see if we've been given an entire disk
* /dev/disk/by-vdev, or /dev/disk/zpool/. As part of this check, see * (minus the slice number).
* if we've been given an entire disk (minus the slice number).
*/ */
static int static int
is_shorthand_path(const char *arg, char *path, is_shorthand_path(const char *arg, char *path,
struct stat64 *statbuf, boolean_t *wholedisk) struct stat64 *statbuf, boolean_t *wholedisk)
{ {
if (zfs_resolve_shortname(arg, path, MAXPATHLEN) == 0) { int error;
error = zfs_resolve_shortname(arg, path, MAXPATHLEN);
if (error == 0) {
*wholedisk = is_whole_disk(path); *wholedisk = is_whole_disk(path);
if (*wholedisk || (stat64(path, statbuf) == 0)) if (*wholedisk || (stat64(path, statbuf) == 0))
return (0); return (0);
@ -385,7 +387,7 @@ is_shorthand_path(const char *arg, char *path,
memset(statbuf, 0, sizeof(*statbuf)); memset(statbuf, 0, sizeof(*statbuf));
*wholedisk = B_FALSE; *wholedisk = B_FALSE;
return (ENOENT); return (error);
} }
/* /*
@ -393,9 +395,9 @@ is_shorthand_path(const char *arg, char *path,
* device, fill in the device id to make a complete nvlist. Valid forms for a * device, fill in the device id to make a complete nvlist. Valid forms for a
* leaf vdev are: * leaf vdev are:
* *
* /dev/xxx Complete disk path * /dev/xxx Complete disk path
* /xxx Full path to file * /xxx Full path to file
* xxx Shorthand for /dev/disk/yyy/xxx * xxx Shorthand for <zfs_vdev_paths>/xxx
*/ */
static nvlist_t * static nvlist_t *
make_leaf_vdev(nvlist_t *props, const char *arg, uint64_t is_log) make_leaf_vdev(nvlist_t *props, const char *arg, uint64_t is_log)
@ -959,7 +961,9 @@ make_disks(zpool_handle_t *zhp, nvlist_t *nv)
* deletes and recreates the link during which access attempts * deletes and recreates the link during which access attempts
* will fail with ENOENT. * will fail with ENOENT.
*/ */
zfs_append_partition(path, udevpath, sizeof (udevpath)); strncpy(udevpath, path, MAXPATHLEN);
(void) zfs_append_partition(udevpath, MAXPATHLEN);
if ((strncmp(udevpath, UDISK_ROOT, strlen(UDISK_ROOT)) == 0) && if ((strncmp(udevpath, UDISK_ROOT, strlen(UDISK_ROOT)) == 0) &&
(lstat64(udevpath, &statbuf) == 0) && (lstat64(udevpath, &statbuf) == 0) &&
S_ISLNK(statbuf.st_mode)) S_ISLNK(statbuf.st_mode))
@ -983,9 +987,9 @@ make_disks(zpool_handle_t *zhp, nvlist_t *nv)
} }
/* /*
* Update the path to refer to FIRST_SLICE. The presence of * Update the path to refer to the partition. The presence of
* the 'whole_disk' field indicates to the CLI that we should * the 'whole_disk' field indicates to the CLI that we should
* chop off the slice number when displaying the device in * chop off the partition number when displaying the device in
* future output. * future output.
*/ */
verify(nvlist_add_string(nv, ZPOOL_CONFIG_PATH, udevpath) == 0); verify(nvlist_add_string(nv, ZPOOL_CONFIG_PATH, udevpath) == 0);

View File

@ -54,22 +54,11 @@ extern "C" {
/* /*
* Default device paths * Default device paths
*/ */
#define DISK_ROOT "/dev"
#define UDISK_ROOT "/dev/disk"
#if defined(__sun__) || defined(__sun) #define DEFAULT_IMPORT_PATH_SIZE 8
#define DISK_ROOT "/dev/dsk" extern char *zpool_default_import_path[DEFAULT_IMPORT_PATH_SIZE];
#define RDISK_ROOT "/dev/rdsk"
#define UDISK_ROOT RDISK_ROOT
#define FIRST_SLICE "s0"
#define BACKUP_SLICE "s2"
#endif
#ifdef __linux__
#define DISK_ROOT "/dev"
#define RDISK_ROOT DISK_ROOT
#define UDISK_ROOT "/dev/disk"
#define FIRST_SLICE "1"
#define BACKUP_SLICE ""
#endif
/* /*
* libzfs errors * libzfs errors
@ -658,8 +647,9 @@ extern zfs_handle_t *zfs_path_to_zhandle(libzfs_handle_t *, char *, zfs_type_t);
extern boolean_t zfs_dataset_exists(libzfs_handle_t *, const char *, extern boolean_t zfs_dataset_exists(libzfs_handle_t *, const char *,
zfs_type_t); zfs_type_t);
extern int zfs_spa_version(zfs_handle_t *, int *); extern int zfs_spa_version(zfs_handle_t *, int *);
extern void zfs_append_partition(const char *path, char *buf, size_t buflen); extern int zfs_append_partition(char *path, size_t max_len);
extern int zfs_resolve_shortname(const char *name, char *path, size_t pathlen); extern int zfs_resolve_shortname(const char *name, char *path, size_t pathlen);
extern int zfs_strcmp_pathname(char *name, char *cmp_name, int wholedisk);
/* /*
* Mount support functions. * Mount support functions.

View File

@ -984,9 +984,7 @@ err_blkid1:
} }
#endif /* HAVE_LIBBLKID */ #endif /* HAVE_LIBBLKID */
#define DEFAULT_IMPORT_PATH_SIZE 8 char *
static char *
zpool_default_import_path[DEFAULT_IMPORT_PATH_SIZE] = { zpool_default_import_path[DEFAULT_IMPORT_PATH_SIZE] = {
"/dev/disk/by-vdev", /* Custom rules, use first if they exist */ "/dev/disk/by-vdev", /* Custom rules, use first if they exist */
"/dev/disk/zpool", /* Custom rules, use first if they exist */ "/dev/disk/zpool", /* Custom rules, use first if they exist */

View File

@ -1744,10 +1744,11 @@ vdev_to_nvlist_iter(nvlist_t *nv, nvlist_t *search, boolean_t *avail_spare,
/* /*
* Search for the requested value. Special cases: * Search for the requested value. Special cases:
* *
* - ZPOOL_CONFIG_PATH for whole disk entries. These end in with a * - ZPOOL_CONFIG_PATH for whole disk entries. These end in
* partition suffix "1", "-part1", or "p1". The suffix is hidden * "-part1", or "p1". The suffix is hidden from the user,
* from the user, but included in the string, so this matches around * but included in the string, so this matches around it.
* it. * - ZPOOL_CONFIG_PATH for short names zfs_strcmp_shortname()
* is used to check all possible expanded paths.
* - looking for a top-level vdev name (i.e. ZPOOL_CONFIG_TYPE). * - looking for a top-level vdev name (i.e. ZPOOL_CONFIG_TYPE).
* *
* Otherwise, all other searches are simple string compares. * Otherwise, all other searches are simple string compares.
@ -1757,15 +1758,9 @@ vdev_to_nvlist_iter(nvlist_t *nv, nvlist_t *search, boolean_t *avail_spare,
(void) nvlist_lookup_uint64(nv, ZPOOL_CONFIG_WHOLE_DISK, (void) nvlist_lookup_uint64(nv, ZPOOL_CONFIG_WHOLE_DISK,
&wholedisk); &wholedisk);
if (wholedisk) { if (zfs_strcmp_pathname(srchval, val, wholedisk) == 0)
char buf[MAXPATHLEN]; return (nv);
zfs_append_partition(srchval, buf, sizeof (buf));
if (strcmp(val, buf) == 0)
return (nv);
break;
}
} else if (strcmp(srchkey, ZPOOL_CONFIG_TYPE) == 0 && val) { } else if (strcmp(srchkey, ZPOOL_CONFIG_TYPE) == 0 && val) {
char *type, *idx, *end, *p; char *type, *idx, *end, *p;
uint64_t id, vdev_id; uint64_t id, vdev_id;
@ -1916,7 +1911,6 @@ nvlist_t *
zpool_find_vdev(zpool_handle_t *zhp, const char *path, boolean_t *avail_spare, zpool_find_vdev(zpool_handle_t *zhp, const char *path, boolean_t *avail_spare,
boolean_t *l2cache, boolean_t *log) boolean_t *l2cache, boolean_t *log)
{ {
char buf[MAXPATHLEN];
char *end; char *end;
nvlist_t *nvroot, *search, *ret; nvlist_t *nvroot, *search, *ret;
uint64_t guid; uint64_t guid;
@ -1928,12 +1922,6 @@ zpool_find_vdev(zpool_handle_t *zhp, const char *path, boolean_t *avail_spare,
verify(nvlist_add_uint64(search, ZPOOL_CONFIG_GUID, guid) == 0); verify(nvlist_add_uint64(search, ZPOOL_CONFIG_GUID, guid) == 0);
} else if (zpool_vdev_is_interior(path)) { } else if (zpool_vdev_is_interior(path)) {
verify(nvlist_add_string(search, ZPOOL_CONFIG_TYPE, path) == 0); verify(nvlist_add_string(search, ZPOOL_CONFIG_TYPE, path) == 0);
} else if (path[0] != '/') {
if (zfs_resolve_shortname(path, buf, sizeof (buf)) < 0) {
nvlist_free(search);
return (NULL);
}
verify(nvlist_add_string(search, ZPOOL_CONFIG_PATH, buf) == 0);
} else { } else {
verify(nvlist_add_string(search, ZPOOL_CONFIG_PATH, path) == 0); verify(nvlist_add_string(search, ZPOOL_CONFIG_PATH, path) == 0);
} }
@ -3701,7 +3689,7 @@ read_efi_label(nvlist_t *config, diskaddr_t *sb)
if (nvlist_lookup_string(config, ZPOOL_CONFIG_PATH, &path) != 0) if (nvlist_lookup_string(config, ZPOOL_CONFIG_PATH, &path) != 0)
return (err); return (err);
(void) snprintf(diskname, sizeof (diskname), "%s%s", RDISK_ROOT, (void) snprintf(diskname, sizeof (diskname), "%s%s", DISK_ROOT,
strrchr(path, '/')); strrchr(path, '/'));
if ((fd = open(diskname, O_RDWR|O_DIRECT)) >= 0) { if ((fd = open(diskname, O_RDWR|O_DIRECT)) >= 0) {
struct dk_gpt *vtoc; struct dk_gpt *vtoc;
@ -3839,8 +3827,7 @@ zpool_label_disk(libzfs_handle_t *hdl, zpool_handle_t *zhp, char *name)
start_block = NEW_START_BLOCK; start_block = NEW_START_BLOCK;
} }
(void) snprintf(path, sizeof (path), "%s/%s%s", RDISK_ROOT, name, (void) snprintf(path, sizeof (path), "%s/%s", DISK_ROOT, name);
BACKUP_SLICE);
if ((fd = open(path, O_RDWR|O_DIRECT)) < 0) { if ((fd = open(path, O_RDWR|O_DIRECT)) < 0) {
/* /*
@ -3910,9 +3897,11 @@ zpool_label_disk(libzfs_handle_t *hdl, zpool_handle_t *zhp, char *name)
(void) close(fd); (void) close(fd);
efi_free(vtoc); efi_free(vtoc);
/* Wait for the first expected slice to appear. */ /* Wait for the first expected partition to appear. */
(void) snprintf(path, sizeof (path), "%s/%s%s%s", DISK_ROOT, name,
isdigit(name[strlen(name)-1]) ? "p" : "", FIRST_SLICE); (void) snprintf(path, sizeof (path), "%s/%s", DISK_ROOT, name);
(void) zfs_append_partition(path, MAXPATHLEN);
rval = zpool_label_disk_wait(path, 3000); rval = zpool_label_disk_wait(path, 3000);
if (rval) { if (rval) {
zfs_error_aux(hdl, dgettext(TEXT_DOMAIN, "failed to " zfs_error_aux(hdl, dgettext(TEXT_DOMAIN, "failed to "

View File

@ -800,56 +800,165 @@ zfs_path_to_zhandle(libzfs_handle_t *hdl, char *path, zfs_type_t argtype)
} }
/* /*
* Given a shorthand device name, check if a file by that name exists in a list * Append partition suffix to an otherwise fully qualified device path.
* of directories under /dev. If one is found, store its full path in the * This is used to generate the name the full path as its stored in
* buffer pointed to by the path argument and return 0, else return -1. The * ZPOOL_CONFIG_PATH for whole disk devices. On success the new length
* path buffer must be allocated by the caller. * of 'path' will be returned on error a negative value is returned.
*/ */
int int
zfs_resolve_shortname(const char *name, char *path, size_t pathlen) zfs_append_partition(char *path, size_t max_len)
{ {
int i, err; int len = strlen(path);
char dirs[6][9] = {"by-id", "by-label", "by-path", "by-uuid", "zpool",
"by-vdev"};
/* /dev/ */ if (strncmp(path, UDISK_ROOT, strlen(UDISK_ROOT)) == 0) {
(void) snprintf(path, pathlen, "%s/%s", DISK_ROOT, name); if (len + 6 >= max_len)
err = access(path, F_OK); return (-1);
if (err == 0)
return (err);
/* /dev/mapper/ */ (void) strcat(path, "-part1");
(void) snprintf(path, pathlen, "%s/mapper/%s", DISK_ROOT, name); len += 6;
err = access(path, F_OK); } else {
if (err == 0) if (len + 2 >= max_len)
return (err); return (-1);
/* /dev/disk/<dirs>/ */ if (isdigit(path[len-1])) {
for (i = 0; i < 6 && err < 0; i++) { (void) strcat(path, "p1");
(void) snprintf(path, pathlen, "%s/%s/%s", len += 2;
UDISK_ROOT, dirs[i], name); } else {
err = access(path, F_OK); (void) strcat(path, "1");
len += 1;
}
} }
return (err); return (len);
} }
/* /*
* Append partition suffix to a device path. This should be used to generate * Given a shorthand device name check if a file by that name exists in any
* the name of a whole disk as it is stored in the vdev label. The * of the 'zpool_default_import_path' or ZPOOL_IMPORT_PATH directories. If
* user-visible names of whole disks do not contain the partition information. * one is found, store its fully qualified path in the 'path' buffer passed
* Modifies buf which must be allocated by the caller. * by the caller and return 0, otherwise return an error.
*/ */
void int
zfs_append_partition(const char *path, char *buf, size_t buflen) zfs_resolve_shortname(const char *name, char *path, size_t len)
{ {
if (strncmp(path, UDISK_ROOT, strlen(UDISK_ROOT)) == 0) int i, error = -1;
(void) snprintf(buf, buflen, "%s%s%s", path, "-part", char *dir, *env, *envdup;
FIRST_SLICE);
else env = getenv("ZPOOL_IMPORT_PATH");
(void) snprintf(buf, buflen, "%s%s%s", path, errno = ENOENT;
isdigit(path[strlen(path)-1]) ? "p" : "",
FIRST_SLICE); if (env) {
envdup = strdup(env);
dir = strtok(envdup, ":");
while (dir && error) {
(void) snprintf(path, len, "%s/%s", dir, name);
error = access(path, F_OK);
dir = strtok(NULL, ":");
}
free(envdup);
} else {
for (i = 0; i < DEFAULT_IMPORT_PATH_SIZE && error < 0; i++) {
(void) snprintf(path, len, "%s/%s",
zpool_default_import_path[i], name);
error = access(path, F_OK);
}
}
return (error ? ENOENT : 0);
}
/*
* Given a shorthand device name look for a match against 'cmp_name'. This
* is done by checking all prefix expansions using either the default
* 'zpool_default_import_paths' or the ZPOOL_IMPORT_PATH environment
* variable. Proper partition suffixes will be appended if this is a
* whole disk. When a match is found 0 is returned otherwise ENOENT.
*/
static int
zfs_strcmp_shortname(char *name, char *cmp_name, int wholedisk)
{
int path_len, cmp_len, i = 0, error = ENOENT;
char *dir, *env, *envdup = NULL;
char path_name[MAXPATHLEN];
cmp_len = strlen(cmp_name);
env = getenv("ZPOOL_IMPORT_PATH");
if (env) {
envdup = strdup(env);
dir = strtok(envdup, ":");
} else {
dir = zpool_default_import_path[i];
}
while (dir) {
/* Trim trailing directory slashes from ZPOOL_IMPORT_PATH */
while (dir[strlen(dir)-1] == '/')
dir[strlen(dir)-1] = '\0';
path_len = snprintf(path_name, MAXPATHLEN, "%s/%s", dir, name);
if (wholedisk)
path_len = zfs_append_partition(path_name, MAXPATHLEN);
if ((path_len == cmp_len) && !strcmp(path_name, cmp_name)) {
error = 0;
break;
}
if (env) {
dir = strtok(NULL, ":");
} else if (++i < DEFAULT_IMPORT_PATH_SIZE) {
dir = zpool_default_import_path[i];
} else {
dir = NULL;
}
}
if (env)
free(envdup);
return (error);
}
/*
* Given either a shorthand or fully qualified path name look for a match
* against 'cmp'. The passed name will be expanded as needed for comparison
* purposes and redundant slashes stripped to ensure an accurate match.
*/
int
zfs_strcmp_pathname(char *name, char *cmp, int wholedisk)
{
int path_len, cmp_len;
char path_name[MAXPATHLEN];
char cmp_name[MAXPATHLEN];
char *dir;
/* Strip redundant slashes if one exists due to ZPOOL_IMPORT_PATH */
memset(cmp_name, 0, MAXPATHLEN);
dir = strtok(cmp, "/");
while (dir) {
strcat(cmp_name, "/");
strcat(cmp_name, dir);
dir = strtok(NULL, "/");
}
if (name[0] != '/')
return zfs_strcmp_shortname(name, cmp_name, wholedisk);
strncpy(path_name, name, MAXPATHLEN);
path_len = strlen(path_name);
cmp_len = strlen(cmp_name);
if (wholedisk) {
path_len = zfs_append_partition(path_name, MAXPATHLEN);
if (path_len == -1)
return (ENOMEM);
}
if ((path_len != cmp_len) || strcmp(path_name, cmp_name))
return (ENOENT);
return (0);
} }
/* /*