Skip to content

Commit 035ebb3

Browse files
Ryan Moellerbehlendorf
authored andcommitted
Allow platform dependent path stripping for vdevs
On Linux the full path preceding devices is stripped when formatting vdev names. On FreeBSD we only want to strip "/dev/". Hide the implementation details of path stripping behind zfs_strip_path(). Make zfs_strip_partition_path() static in Linux implementation while here, since it is never used outside of the file it is defined in. Reviewed-by: Brian Behlendorf <[email protected]> Signed-off-by: Ryan Moeller <[email protected]> Closes #9565
1 parent 5a6ac4c commit 035ebb3

File tree

3 files changed

+85
-77
lines changed

3 files changed

+85
-77
lines changed

include/libzutil.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -97,7 +97,7 @@ extern int zfs_append_partition(char *path, size_t max_len);
9797
extern int zfs_resolve_shortname(const char *name, char *path, size_t pathlen);
9898

9999
extern char *zfs_strip_partition(char *);
100-
extern char *zfs_strip_partition_path(char *);
100+
extern char *zfs_strip_path(char *);
101101

102102
extern int zfs_strcmp_pathname(const char *, const char *, int);
103103

lib/libzfs/libzfs_pool.c

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3911,8 +3911,7 @@ zpool_vdev_name(libzfs_handle_t *hdl, zpool_handle_t *zhp, nvlist_t *nv,
39113911
*/
39123912
if ((strcmp(type, VDEV_TYPE_DISK) == 0) &&
39133913
!(name_flags & VDEV_NAME_PATH)) {
3914-
path = strrchr(path, '/');
3915-
path++;
3914+
path = zfs_strip_path(path);
39163915
}
39173916

39183917
/*

lib/libzutil/os/linux/zutil_device_path_os.c

Lines changed: 83 additions & 74 deletions
Original file line numberDiff line numberDiff line change
@@ -71,6 +71,89 @@ zfs_append_partition(char *path, size_t max_len)
7171
return (len);
7272
}
7373

74+
/*
75+
* Remove partition suffix from a vdev path. Partition suffixes may take three
76+
* forms: "-partX", "pX", or "X", where X is a string of digits. The second
77+
* case only occurs when the suffix is preceded by a digit, i.e. "md0p0" The
78+
* third case only occurs when preceded by a string matching the regular
79+
* expression "^([hsv]|xv)d[a-z]+", i.e. a scsi, ide, virtio or xen disk.
80+
*
81+
* caller must free the returned string
82+
*/
83+
char *
84+
zfs_strip_partition(char *path)
85+
{
86+
char *tmp = strdup(path);
87+
char *part = NULL, *d = NULL;
88+
if (!tmp)
89+
return (NULL);
90+
91+
if ((part = strstr(tmp, "-part")) && part != tmp) {
92+
d = part + 5;
93+
} else if ((part = strrchr(tmp, 'p')) &&
94+
part > tmp + 1 && isdigit(*(part-1))) {
95+
d = part + 1;
96+
} else if ((tmp[0] == 'h' || tmp[0] == 's' || tmp[0] == 'v') &&
97+
tmp[1] == 'd') {
98+
for (d = &tmp[2]; isalpha(*d); part = ++d) { }
99+
} else if (strncmp("xvd", tmp, 3) == 0) {
100+
for (d = &tmp[3]; isalpha(*d); part = ++d) { }
101+
}
102+
if (part && d && *d != '\0') {
103+
for (; isdigit(*d); d++) { }
104+
if (*d == '\0')
105+
*part = '\0';
106+
}
107+
108+
return (tmp);
109+
}
110+
111+
/*
112+
* Same as zfs_strip_partition, but allows "/dev/" to be in the pathname
113+
*
114+
* path: /dev/sda1
115+
* returns: /dev/sda
116+
*
117+
* Returned string must be freed.
118+
*/
119+
static char *
120+
zfs_strip_partition_path(char *path)
121+
{
122+
char *newpath = strdup(path);
123+
char *sd_offset;
124+
char *new_sd;
125+
126+
if (!newpath)
127+
return (NULL);
128+
129+
/* Point to "sda1" part of "/dev/sda1" */
130+
sd_offset = strrchr(newpath, '/') + 1;
131+
132+
/* Get our new name "sda" */
133+
new_sd = zfs_strip_partition(sd_offset);
134+
if (!new_sd) {
135+
free(newpath);
136+
return (NULL);
137+
}
138+
139+
/* Paste the "sda" where "sda1" was */
140+
strlcpy(sd_offset, new_sd, strlen(sd_offset) + 1);
141+
142+
/* Free temporary "sda" */
143+
free(new_sd);
144+
145+
return (newpath);
146+
}
147+
148+
/*
149+
* Strip the unwanted portion of a device path.
150+
*/
151+
char *
152+
zfs_strip_path(char *path)
153+
{
154+
return (strrchr(path, '/') + 1);
155+
}
156+
74157
/*
75158
* Allocate and return the underlying device name for a device mapper device.
76159
* If a device mapper device maps to multiple devices, return the first device.
@@ -349,80 +432,6 @@ zfs_get_enclosure_sysfs_path(const char *dev_name)
349432
return (path);
350433
}
351434

352-
/*
353-
* Remove partition suffix from a vdev path. Partition suffixes may take three
354-
* forms: "-partX", "pX", or "X", where X is a string of digits. The second
355-
* case only occurs when the suffix is preceded by a digit, i.e. "md0p0" The
356-
* third case only occurs when preceded by a string matching the regular
357-
* expression "^([hsv]|xv)d[a-z]+", i.e. a scsi, ide, virtio or xen disk.
358-
*
359-
* caller must free the returned string
360-
*/
361-
char *
362-
zfs_strip_partition(char *path)
363-
{
364-
char *tmp = strdup(path);
365-
char *part = NULL, *d = NULL;
366-
if (!tmp)
367-
return (NULL);
368-
369-
if ((part = strstr(tmp, "-part")) && part != tmp) {
370-
d = part + 5;
371-
} else if ((part = strrchr(tmp, 'p')) &&
372-
part > tmp + 1 && isdigit(*(part-1))) {
373-
d = part + 1;
374-
} else if ((tmp[0] == 'h' || tmp[0] == 's' || tmp[0] == 'v') &&
375-
tmp[1] == 'd') {
376-
for (d = &tmp[2]; isalpha(*d); part = ++d) { }
377-
} else if (strncmp("xvd", tmp, 3) == 0) {
378-
for (d = &tmp[3]; isalpha(*d); part = ++d) { }
379-
}
380-
if (part && d && *d != '\0') {
381-
for (; isdigit(*d); d++) { }
382-
if (*d == '\0')
383-
*part = '\0';
384-
}
385-
386-
return (tmp);
387-
}
388-
389-
/*
390-
* Same as zfs_strip_partition, but allows "/dev/" to be in the pathname
391-
*
392-
* path: /dev/sda1
393-
* returns: /dev/sda
394-
*
395-
* Returned string must be freed.
396-
*/
397-
char *
398-
zfs_strip_partition_path(char *path)
399-
{
400-
char *newpath = strdup(path);
401-
char *sd_offset;
402-
char *new_sd;
403-
404-
if (!newpath)
405-
return (NULL);
406-
407-
/* Point to "sda1" part of "/dev/sda1" */
408-
sd_offset = strrchr(newpath, '/') + 1;
409-
410-
/* Get our new name "sda" */
411-
new_sd = zfs_strip_partition(sd_offset);
412-
if (!new_sd) {
413-
free(newpath);
414-
return (NULL);
415-
}
416-
417-
/* Paste the "sda" where "sda1" was */
418-
strlcpy(sd_offset, new_sd, strlen(sd_offset) + 1);
419-
420-
/* Free temporary "sda" */
421-
free(new_sd);
422-
423-
return (newpath);
424-
}
425-
426435
#ifdef HAVE_LIBUDEV
427436

428437
/*

0 commit comments

Comments
 (0)