]> git.proxmox.com Git - mirror_zfs-debian.git/blame - cmd/zvol_id/zvol_id_main.c
New upstream version 0.7.9
[mirror_zfs-debian.git] / cmd / zvol_id / zvol_id_main.c
CommitLineData
4c0d8e50
FN
1/*
2 * CDDL HEADER START
3 *
4 * The contents of this file are subject to the terms of the
5 * Common Development and Distribution License (the "License").
6 * You may not use this file except in compliance with the License.
7 *
8 * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
9 * or http://www.opensolaris.org/os/licensing.
10 * See the License for the specific language governing permissions
11 * and limitations under the License.
12 *
13 * When distributing Covered Code, include this CDDL HEADER in each
14 * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
15 * If applicable, add the following below this CDDL HEADER, with the
16 * fields enclosed by brackets "[]" replaced with your own identifying
17 * information: Portions Copyright [yyyy] [name of copyright owner]
18 *
19 * CDDL HEADER END
20 */
21/*
22 * Copyright (c) 2011, Fajar A. Nugraha. All rights reserved.
23 * Use is subject to license terms.
24 */
25
ea04106b
AX
26#include <ctype.h>
27#include <string.h>
4c0d8e50
FN
28#include <stdio.h>
29#include <stdlib.h>
30#include <fcntl.h>
31#include <unistd.h>
32#include <linux/ioctl.h>
33#include <sys/stat.h>
34#include <sys/ioctl.h>
35#include <sys/zfs_znode.h>
36#include <sys/fs/zfs.h>
37
a08ee875
LG
38static int
39ioctl_get_msg(char *var, int fd)
4c0d8e50
FN
40{
41 int error = 0;
cae5b340 42 char msg[ZFS_MAX_DATASET_NAME_LEN];
4c0d8e50
FN
43
44 error = ioctl(fd, BLKZNAME, msg);
45 if (error < 0) {
4c0d8e50
FN
46 return (error);
47 }
48
cae5b340 49 snprintf(var, ZFS_MAX_DATASET_NAME_LEN, "%s", msg);
4c0d8e50
FN
50 return (error);
51}
52
a08ee875
LG
53int
54main(int argc, char **argv)
4c0d8e50
FN
55{
56 int fd, error = 0;
cae5b340 57 char zvol_name[ZFS_MAX_DATASET_NAME_LEN];
42f7b73b 58 char *zvol_name_part = NULL;
4c0d8e50
FN
59 char *dev_name;
60 struct stat64 statbuf;
61 int dev_minor, dev_part;
ea04106b 62 int i;
42f7b73b 63 int rc;
4c0d8e50
FN
64
65 if (argc < 2) {
66 printf("Usage: %s /dev/zvol_device_node\n", argv[0]);
67 return (EINVAL);
68 }
69
70 dev_name = argv[1];
71 error = stat64(dev_name, &statbuf);
72 if (error != 0) {
73 printf("Unable to access device file: %s\n", dev_name);
74 return (errno);
75 }
76
77 dev_minor = minor(statbuf.st_rdev);
78 dev_part = dev_minor % ZVOL_MINORS;
79
80 fd = open(dev_name, O_RDONLY);
81 if (fd < 0) {
82 printf("Unable to open device file: %s\n", dev_name);
83 return (errno);
84 }
85
a5729f7b
FN
86 error = ioctl_get_msg(zvol_name, fd);
87 if (error < 0) {
88 printf("ioctl_get_msg failed:%s\n", strerror(errno));
89 return (errno);
90 }
4c0d8e50 91 if (dev_part > 0)
42f7b73b
AX
92 rc = asprintf(&zvol_name_part, "%s-part%d", zvol_name,
93 dev_part);
4c0d8e50 94 else
42f7b73b
AX
95 rc = asprintf(&zvol_name_part, "%s", zvol_name);
96
97 if (rc == -1 || zvol_name_part == NULL)
98 goto error;
4c0d8e50 99
ea04106b
AX
100 for (i = 0; i < strlen(zvol_name_part); i++) {
101 if (isblank(zvol_name_part[i]))
102 zvol_name_part[i] = '+';
103 }
104
4c0d8e50 105 printf("%s\n", zvol_name_part);
42f7b73b
AX
106 free(zvol_name_part);
107error:
4c0d8e50
FN
108 close(fd);
109 return (error);
110}