aboutsummaryrefslogtreecommitdiffstats
path: root/cmd/zvol_id
diff options
context:
space:
mode:
authorMatt Macy <mmacy@FreeBSD.org>2020-08-24 22:48:19 +0000
committerMatt Macy <mmacy@FreeBSD.org>2020-08-24 22:48:19 +0000
commit3b0ce0e28db46d0403929aba45c682285e1ac217 (patch)
tree91721e6e5518bd0d8113dee535898f2225443411 /cmd/zvol_id
downloadsrc-3b0ce0e28db46d0403929aba45c682285e1ac217.tar.gz
src-3b0ce0e28db46d0403929aba45c682285e1ac217.zip
Vendor import of openzfs master @ 184df27eef0abdc7ab2105b21257f753834b936bvendor/openzfs/2.0-rc0-g184df27
Sponsored by: iX Systems, Inc.
Notes
Notes: svn path=/vendor-sys/openzfs/dist/; revision=364736 svn path=/vendor-sys/openzfs/2.0-rc0-g184df27/; revision=364741; tag=vendor/openzfs/2.0-rc0-g184df27
Diffstat (limited to 'cmd/zvol_id')
-rw-r--r--cmd/zvol_id/.gitignore1
-rw-r--r--cmd/zvol_id/Makefile.am10
-rw-r--r--cmd/zvol_id/zvol_id_main.c110
3 files changed, 121 insertions, 0 deletions
diff --git a/cmd/zvol_id/.gitignore b/cmd/zvol_id/.gitignore
new file mode 100644
index 000000000000..8b757a2d6781
--- /dev/null
+++ b/cmd/zvol_id/.gitignore
@@ -0,0 +1 @@
+zvol_id
diff --git a/cmd/zvol_id/Makefile.am b/cmd/zvol_id/Makefile.am
new file mode 100644
index 000000000000..a584875081eb
--- /dev/null
+++ b/cmd/zvol_id/Makefile.am
@@ -0,0 +1,10 @@
+include $(top_srcdir)/config/Rules.am
+
+# Disable GCC stack protection for zvol_id. This is a kludge and should be
+# removed once https://github.com/zfsonlinux/zfs/issues/569 is resolved.
+AM_CFLAGS += -fno-stack-protector
+
+udev_PROGRAMS = zvol_id
+
+zvol_id_SOURCES = \
+ zvol_id_main.c
diff --git a/cmd/zvol_id/zvol_id_main.c b/cmd/zvol_id/zvol_id_main.c
new file mode 100644
index 000000000000..4a2d74cc203c
--- /dev/null
+++ b/cmd/zvol_id/zvol_id_main.c
@@ -0,0 +1,110 @@
+/*
+ * CDDL HEADER START
+ *
+ * The contents of this file are subject to the terms of the
+ * Common Development and Distribution License (the "License").
+ * You may not use this file except in compliance with the License.
+ *
+ * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
+ * or http://www.opensolaris.org/os/licensing.
+ * See the License for the specific language governing permissions
+ * and limitations under the License.
+ *
+ * When distributing Covered Code, include this CDDL HEADER in each
+ * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
+ * If applicable, add the following below this CDDL HEADER, with the
+ * fields enclosed by brackets "[]" replaced with your own identifying
+ * information: Portions Copyright [yyyy] [name of copyright owner]
+ *
+ * CDDL HEADER END
+ */
+/*
+ * Copyright (c) 2011, Fajar A. Nugraha. All rights reserved.
+ * Use is subject to license terms.
+ */
+
+#include <ctype.h>
+#include <string.h>
+#include <stdio.h>
+#include <stdlib.h>
+#include <fcntl.h>
+#include <unistd.h>
+#include <linux/ioctl.h>
+#include <sys/stat.h>
+#include <sys/ioctl.h>
+#include <sys/zfs_znode.h>
+#include <sys/fs/zfs.h>
+
+static int
+ioctl_get_msg(char *var, int fd)
+{
+ int error = 0;
+ char msg[ZFS_MAX_DATASET_NAME_LEN];
+
+ error = ioctl(fd, BLKZNAME, msg);
+ if (error < 0) {
+ return (error);
+ }
+
+ snprintf(var, ZFS_MAX_DATASET_NAME_LEN, "%s", msg);
+ return (error);
+}
+
+int
+main(int argc, char **argv)
+{
+ int fd, error = 0;
+ char zvol_name[ZFS_MAX_DATASET_NAME_LEN];
+ char *zvol_name_part = NULL;
+ char *dev_name;
+ struct stat64 statbuf;
+ int dev_minor, dev_part;
+ int i;
+ int rc;
+
+ if (argc < 2) {
+ printf("Usage: %s /dev/zvol_device_node\n", argv[0]);
+ return (EINVAL);
+ }
+
+ dev_name = argv[1];
+ error = stat64(dev_name, &statbuf);
+ if (error != 0) {
+ printf("Unable to access device file: %s\n", dev_name);
+ return (errno);
+ }
+
+ dev_minor = minor(statbuf.st_rdev);
+ dev_part = dev_minor % ZVOL_MINORS;
+
+ fd = open(dev_name, O_RDONLY);
+ if (fd < 0) {
+ printf("Unable to open device file: %s\n", dev_name);
+ return (errno);
+ }
+
+ error = ioctl_get_msg(zvol_name, fd);
+ if (error < 0) {
+ printf("ioctl_get_msg failed:%s\n", strerror(errno));
+ return (errno);
+ }
+ if (dev_part > 0)
+ rc = asprintf(&zvol_name_part, "%s-part%d", zvol_name,
+ dev_part);
+ else
+ rc = asprintf(&zvol_name_part, "%s", zvol_name);
+
+ if (rc == -1 || zvol_name_part == NULL)
+ goto error;
+
+ for (i = 0; i < strlen(zvol_name_part); i++) {
+ if (isblank(zvol_name_part[i]))
+ zvol_name_part[i] = '+';
+ }
+
+ printf("%s\n", zvol_name_part);
+ free(zvol_name_part);
+error:
+ close(fd);
+ return (error);
+}