1 /* $NetBSD: getdiskrawname.c,v 1.6 2019/08/22 20:23:43 brad Exp $ */
2
3 /*-
4 * Copyright (c) 2012 The NetBSD Foundation, Inc.
5 * All rights reserved.
6 *
7 * This code is derived from software contributed to The NetBSD Foundation
8 * by Christos Zoulas.
9 *
10 * Redistribution and use in source and binary forms, with or without
11 * modification, are permitted provided that the following conditions
12 * are met:
13 * 1. Redistributions of source code must retain the above copyright
14 * notice, this list of conditions and the following disclaimer.
15 * 2. Redistributions in binary form must reproduce the above copyright
16 * notice, this list of conditions and the following disclaimer in the
17 * documentation and/or other materials provided with the distribution.
18 *
19 * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
20 * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
21 * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
22 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
23 * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
24 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
25 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
26 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
27 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
28 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
29 * POSSIBILITY OF SUCH DAMAGE.
30 */
31 #include <sys/cdefs.h>
32 __RCSID("$NetBSD: getdiskrawname.c,v 1.6 2019/08/22 20:23:43 brad Exp $");
33
34 #include <sys/stat.h>
35
36 #include <stdio.h>
37 #include <string.h>
38 #include <errno.h>
39 #include <util.h>
40 #include <limits.h>
41 #include <unistd.h>
42
43 static const char *
resolve_link(char * buf,size_t bufsiz,const char * name)44 resolve_link(char *buf, size_t bufsiz, const char *name)
45 {
46 const char *dp;
47 size_t nlen;
48 ssize_t dlen;
49
50 dlen = readlink(name, buf, bufsiz - 1);
51 if (dlen == -1)
52 return name;
53
54 buf[dlen] = '\0';
55
56 if (buf[0] != '/') {
57 dp = strrchr(name, '/');
58 if (dp != NULL) {
59 nlen = dp - name + 1;
60 if (nlen + 1 > bufsiz)
61 return NULL;
62 if (nlen + dlen + 1 > bufsiz)
63 return NULL;
64
65 memmove(buf + nlen, buf, (size_t)dlen + 1);
66 memcpy(buf, name, nlen);
67 }
68 }
69
70 return buf;
71 }
72
73 /*
74 * zvol device names look like:
75 * /dev/zvol/dsk/pool_name/.../volume_name
76 * /dev/zvol/rdsk/pool_name/.../volume_name
77 *
78 * ZFS pools can be divided nearly to infinity
79 *
80 * This allows for 16 pool names, which one would hope would be enough
81 */
82 #define DISKMAXPARTS 20
83 static int
calc_zvol_name(char * buf,size_t bufsiz,const char * name,const char * raw)84 calc_zvol_name(char *buf, size_t bufsiz, const char *name, const char *raw)
85 {
86 char copyname[PATH_MAX];
87 char *names[DISKMAXPARTS];
88 char *last, *p;
89 size_t i = 0;
90
91 strlcpy(copyname, name, sizeof(copyname));
92 for (p = strtok_r(copyname, "/", &last); p;
93 p = strtok_r(NULL, "/", &last)) {
94 if (i >= DISKMAXPARTS) {
95 errno = ENOSPC;
96 return -1;
97 }
98 names[i++] = p;
99 }
100
101 if (i < 4) {
102 errno = EINVAL;
103 return -1;
104 }
105
106 snprintf(buf, bufsiz, "/dev/zvol/%sdsk", raw);
107 for (size_t j = 3; j < i; j++) {
108 strlcat(buf, "/", bufsiz);
109 strlcat(buf, names[j], bufsiz);
110 }
111 return 0;
112 }
113
114 static int
calc_name(char * buf,size_t bufsiz,const char * name,const char * raw)115 calc_name(char *buf, size_t bufsiz, const char *name, const char *raw)
116 {
117 int skip = 1;
118
119 if (strncmp("/dev/zvol/", name, 10) == 0)
120 return calc_zvol_name(buf, bufsiz, name, raw);
121
122 const char *dp = strrchr(name, '/');
123 if (!*raw && ((dp != NULL && dp[1] != 'r')
124 || (dp == NULL && name[0] != 'r'))) {
125 errno = EINVAL;
126 return -1;
127 }
128 if (raw[0] != 'r')
129 skip = 2;
130 if (dp != NULL)
131 snprintf(buf, bufsiz, "%.*s/%s%s", (int)(dp - name),
132 name, raw, dp + skip);
133 else
134 snprintf(buf, bufsiz, "%s%s", raw, name);
135 return 0;
136 }
137
138 const char *
getdiskrawname(char * buf,size_t bufsiz,const char * name)139 getdiskrawname(char *buf, size_t bufsiz, const char *name)
140 {
141 struct stat st;
142 char dest[PATH_MAX];
143
144 if ((name = resolve_link(dest, sizeof(dest), name)) == NULL) {
145 errno = EINVAL;
146 return NULL;
147 }
148
149 if (stat(name, &st) == -1)
150 return NULL;
151
152 if (!S_ISBLK(st.st_mode)) {
153 errno = EFTYPE;
154 return NULL;
155 }
156
157 if (calc_name(buf, bufsiz, name, "r") == -1)
158 return NULL;
159
160 return buf;
161 }
162
163 const char *
getdiskcookedname(char * buf,size_t bufsiz,const char * name)164 getdiskcookedname(char *buf, size_t bufsiz, const char *name)
165 {
166 struct stat st;
167 char dest[PATH_MAX];
168
169 if ((name = resolve_link(dest, sizeof(dest), name)) == NULL) {
170 errno = EINVAL;
171 return NULL;
172 }
173
174 if (stat(name, &st) == -1)
175 return NULL;
176
177 if (!S_ISCHR(st.st_mode)) {
178 errno = EFTYPE;
179 return NULL;
180 }
181
182 if (calc_name(buf, bufsiz, name, "") == -1)
183 return NULL;
184
185 return buf;
186 }
187