]> git.proxmox.com Git - mirror_zfs.git/blame - cmd/zvol_id/zvol_id_main.c
cstyle: Resolve C style issues
[mirror_zfs.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
26#include <stdio.h>
27#include <stdlib.h>
28#include <fcntl.h>
29#include <unistd.h>
30#include <linux/ioctl.h>
31#include <sys/stat.h>
32#include <sys/ioctl.h>
33#include <sys/zfs_znode.h>
34#include <sys/fs/zfs.h>
35
d1d7e268
MK
36static int
37ioctl_get_msg(char *var, int fd)
4c0d8e50
FN
38{
39 int error = 0;
40 char msg[ZFS_MAXNAMELEN];
41
42 error = ioctl(fd, BLKZNAME, msg);
43 if (error < 0) {
4c0d8e50
FN
44 return (error);
45 }
46
47 snprintf(var, ZFS_MAXNAMELEN, "%s", msg);
48 return (error);
49}
50
d1d7e268
MK
51int
52main(int argc, char **argv)
4c0d8e50
FN
53{
54 int fd, error = 0;
55 char zvol_name[ZFS_MAXNAMELEN], zvol_name_part[ZFS_MAXNAMELEN];
56 char *dev_name;
57 struct stat64 statbuf;
58 int dev_minor, dev_part;
59
60 if (argc < 2) {
61 printf("Usage: %s /dev/zvol_device_node\n", argv[0]);
62 return (EINVAL);
63 }
64
65 dev_name = argv[1];
66 error = stat64(dev_name, &statbuf);
67 if (error != 0) {
68 printf("Unable to access device file: %s\n", dev_name);
69 return (errno);
70 }
71
72 dev_minor = minor(statbuf.st_rdev);
73 dev_part = dev_minor % ZVOL_MINORS;
74
75 fd = open(dev_name, O_RDONLY);
76 if (fd < 0) {
77 printf("Unable to open device file: %s\n", dev_name);
78 return (errno);
79 }
80
a5729f7b
FN
81 error = ioctl_get_msg(zvol_name, fd);
82 if (error < 0) {
83 printf("ioctl_get_msg failed:%s\n", strerror(errno));
84 return (errno);
85 }
4c0d8e50
FN
86 if (dev_part > 0)
87 snprintf(zvol_name_part, ZFS_MAXNAMELEN, "%s-part%d", zvol_name,
88 dev_part);
89 else
90 snprintf(zvol_name_part, ZFS_MAXNAMELEN, "%s", zvol_name);
91
92 printf("%s\n", zvol_name_part);
93 close(fd);
94 return (error);
95}