Deleted Added
full compact
geom_ctl.c (112510) geom_ctl.c (112709)
1/*-
2 * Copyright (c) 2003 Poul-Henning Kamp
3 * All rights reserved.
4 *
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions
7 * are met:
8 * 1. Redistributions of source code must retain the above copyright

--- 12 unchanged lines hidden (view full) ---

21 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
22 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
23 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
24 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
25 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
26 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
27 * SUCH DAMAGE.
28 *
1/*-
2 * Copyright (c) 2003 Poul-Henning Kamp
3 * All rights reserved.
4 *
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions
7 * are met:
8 * 1. Redistributions of source code must retain the above copyright

--- 12 unchanged lines hidden (view full) ---

21 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
22 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
23 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
24 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
25 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
26 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
27 * SUCH DAMAGE.
28 *
29 * $FreeBSD: head/lib/libgeom/geom_ctl.c 112510 2003-03-23 10:15:02Z phk $
29 * $FreeBSD: head/lib/libgeom/geom_ctl.c 112709 2003-03-27 14:35:00Z phk $
30 */
31
32#include <stdio.h>
33#include <fcntl.h>
34#include <errno.h>
35#include <stdint.h>
36#include <sys/types.h>
37#include <stdarg.h>
30 */
31
32#include <stdio.h>
33#include <fcntl.h>
34#include <errno.h>
35#include <stdint.h>
36#include <sys/types.h>
37#include <stdarg.h>
38#include <unistd.h>
38#include <string.h>
39#include <stdlib.h>
40#include <paths.h>
41
42#include <sys/queue.h>
43
39#include <string.h>
40#include <stdlib.h>
41#include <paths.h>
42
43#include <sys/queue.h>
44
44#define GEOM_CTL_TABLE 1
45#define GCTL_TABLE 1
45#include <libgeom.h>
46
47#include <geom/geom_ext.h>
48
49void
46#include <libgeom.h>
47
48#include <geom/geom_ext.h>
49
50void
50geom_ctl_dump(struct geom_ctl_req *req, FILE *f)
51gctl_dump(struct gctl_req *req, FILE *f)
51{
52 u_int i;
53 int j;
52{
53 u_int i;
54 int j;
54 struct geom_ctl_req_arg *ap;
55 struct gctl_req_arg *ap;
55
56 if (req == NULL) {
56
57 if (req == NULL) {
57 fprintf(f, "Dump of geom_ctl request at NULL\n");
58 fprintf(f, "Dump of gctl request at NULL\n");
58 return;
59 }
59 return;
60 }
60 fprintf(f, "Dump of geom_ctl %s request at %p:\n", req->reqt->name, req);
61 fprintf(f, "Dump of gctl %s request at %p:\n", req->reqt->name, req);
61 if (req->error != NULL)
62 fprintf(f, " error:\t\"%s\"\n", req->error);
63 else
64 fprintf(f, " error:\tNULL\n");
65 for (i = 0; i < req->narg; i++) {
66 ap = &req->arg[i];
67 if (ap->name != NULL)
68 fprintf(f, " param:\t\"%s\"", ap->name);
69 else
70 fprintf(f, " meta:\t@%jd", (intmax_t)ap->offset);
62 if (req->error != NULL)
63 fprintf(f, " error:\t\"%s\"\n", req->error);
64 else
65 fprintf(f, " error:\tNULL\n");
66 for (i = 0; i < req->narg; i++) {
67 ap = &req->arg[i];
68 if (ap->name != NULL)
69 fprintf(f, " param:\t\"%s\"", ap->name);
70 else
71 fprintf(f, " meta:\t@%jd", (intmax_t)ap->offset);
72 fprintf(f, " [%s%s",
73 ap->flag & GCTL_PARAM_RD ? "R" : "",
74 ap->flag & GCTL_PARAM_WR ? "W" : "");
71 fflush(f);
75 fflush(f);
72 if (ap->len < 0)
73 fprintf(f, " = [%d] \"%s\"", -ap->len, (char *)ap->value);
76 if (ap->flag & GCTL_PARAM_ASCII)
77 fprintf(f, "%d] = \"%s\"", ap->len, (char *)ap->value);
74 else if (ap->len > 0) {
78 else if (ap->len > 0) {
75 fprintf(f, " = [%d]", ap->len);
79 fprintf(f, "%d] = ", ap->len);
76 fflush(f);
77 for (j = 0; j < ap->len; j++) {
78 fprintf(f, " %02x", ((u_char *)ap->value)[j]);
79 }
80 } else {
80 fflush(f);
81 for (j = 0; j < ap->len; j++) {
82 fprintf(f, " %02x", ((u_char *)ap->value)[j]);
83 }
84 } else {
81 fprintf(f, " = [0] %p", ap->value);
85 fprintf(f, "0] = %p", ap->value);
82 }
83 fprintf(f, "\n");
84 }
85}
86
86 }
87 fprintf(f, "\n");
88 }
89}
90
91/*
92 * Set an error message, if one does not already exist.
93 */
87static void
94static void
88geom_ctl_set_error(struct geom_ctl_req *req, const char *error, ...)
95gctl_set_error(struct gctl_req *req, const char *error, ...)
89{
90 va_list ap;
91
92 if (req->error != NULL)
93 return;
94 va_start(ap, error);
95 vasprintf(&req->error, error, ap);
96{
97 va_list ap;
98
99 if (req->error != NULL)
100 return;
101 va_start(ap, error);
102 vasprintf(&req->error, error, ap);
103 va_end(ap);
96}
97
104}
105
106/*
107 * Check that a malloc operation succeeded, and set a consistent error
108 * message if not.
109 */
98static void
110static void
99geom_ctl_check_alloc(struct geom_ctl_req *req, void *ptr)
111gctl_check_alloc(struct gctl_req *req, void *ptr)
100{
101 if (ptr != NULL)
102 return;
112{
113 if (ptr != NULL)
114 return;
103 geom_ctl_set_error(req, "Could not allocate memory");
115 gctl_set_error(req, "Could not allocate memory");
116 if (req->error == NULL)
117 req->error = "Could not allocate memory";
104}
105
118}
119
106struct geom_ctl_req *
107geom_ctl_get_handle(enum geom_ctl_request req)
120/*
121 * Allocate a new request handle of the specified type.
122 * XXX: Why bother checking the type ?
123 */
124struct gctl_req *
125gctl_get_handle(enum gctl_request req)
108{
126{
109 struct geom_ctl_req_table *gtp;
110 struct geom_ctl_req *rp;
127 struct gctl_req_table *gtp;
128 struct gctl_req *rp;
111
112 rp = calloc(1, sizeof *rp);
113 if (rp == NULL)
114 return (NULL);
115 for (gtp = gcrt; gtp->request != req; gtp++)
129
130 rp = calloc(1, sizeof *rp);
131 if (rp == NULL)
132 return (NULL);
133 for (gtp = gcrt; gtp->request != req; gtp++)
116 if (gtp->request == GEOM_INVALID_REQUEST)
134 if (gtp->request == GCTL_INVALID_REQUEST)
117 break;
118
119 rp->request = req;
120 rp->reqt = gtp;
135 break;
136
137 rp->request = req;
138 rp->reqt = gtp;
121 if (rp->reqt->request == GEOM_INVALID_REQUEST)
122 geom_ctl_set_error(rp, "Invalid request");
139 if (rp->reqt->request == GCTL_INVALID_REQUEST)
140 gctl_set_error(rp, "Invalid request");
123 return (rp);
124}
125
141 return (rp);
142}
143
126void
127geom_ctl_set_param(struct geom_ctl_req *req, const char *name, int len, void* value)
144/*
145 * Allocate space for another argument.
146 */
147static struct gctl_req_arg *
148gctl_new_arg(struct gctl_req *req)
128{
149{
129 struct geom_ctl_req_arg *ap;
150 struct gctl_req_arg *ap;
130
151
131 if (req == NULL || req->error != NULL)
132 return;
133 if (req->reqt->params == 0)
134 geom_ctl_set_error(req, "Request takes no parameters");
135 req->narg++;
136 req->arg = realloc(req->arg, sizeof *ap * req->narg);
152 req->narg++;
153 req->arg = realloc(req->arg, sizeof *ap * req->narg);
137 geom_ctl_check_alloc(req, req->arg);
138 if (req->arg != NULL) {
139 ap = req->arg + (req->narg - 1);
140 memset(ap, 0, sizeof *ap);
141 ap->name = strdup(name);
142 geom_ctl_check_alloc(req, ap->name);
143 ap->nlen = strlen(ap->name);
144 ap->len = len;
145 if (len > 0) {
146 ap->value = value;
147 } else if (len < 0) {
148 ap->len = -strlen(value);
149 ap->value = strdup(value);
150 } else {
151 ap->value = value;
152 }
153 if (len != 0)
154 geom_ctl_check_alloc(req, ap->value);
155 } else {
154 gctl_check_alloc(req, req->arg);
155 if (req->arg == NULL) {
156 req->narg = 0;
156 req->narg = 0;
157 return (NULL);
157 }
158 }
159 ap = req->arg + (req->narg - 1);
160 memset(ap, 0, sizeof *ap);
161 return (ap);
158}
159
160void
162}
163
164void
161geom_ctl_set_meta(struct geom_ctl_req *req, off_t offset, u_int len, void* value)
165gctl_ro_param(struct gctl_req *req, const char *name, int len, const void* value)
162{
166{
163 struct geom_ctl_req_arg *ap;
164 u_int i;
167 struct gctl_req_arg *ap;
165
166 if (req == NULL || req->error != NULL)
167 return;
168
169 if (req == NULL || req->error != NULL)
170 return;
168 if (req->reqt->meta == 0)
169 geom_ctl_set_error(req, "Request takes no meta data");
170 for (i = 0; i < req->narg; i++) {
171 ap = &req->arg[i];
172 if (ap->name != NULL)
173 continue;
174 if (ap->offset >= offset + len)
175 continue;
176 if (ap->offset + ap->len <= offset)
177 continue;
178 geom_ctl_set_error(req, "Overlapping meta data");
171 ap = gctl_new_arg(req);
172 if (ap == NULL)
179 return;
173 return;
180 }
181 req->narg++;
182 req->arg = realloc(req->arg, sizeof *ap * req->narg);
183 geom_ctl_check_alloc(req, req->arg);
184 if (req->arg != NULL) {
185 ap = req->arg + (req->narg - 1);
186 memset(ap, 0, sizeof *ap);
187 ap->value = value;
188 ap->offset = offset;
174 ap->name = strdup(name);
175 gctl_check_alloc(req, ap->name);
176 ap->nlen = strlen(ap->name) + 1;
177 ap->value = __DECONST(void *, value);
178 ap->flag = GCTL_PARAM_RD;
179 if (len >= 0)
189 ap->len = len;
180 ap->len = len;
190 } else {
191 req->narg = 0;
181 else if (len < 0) {
182 ap->flag |= GCTL_PARAM_ASCII;
183 ap->len = strlen(value) + 1;
192 }
193}
194
184 }
185}
186
187void
188gctl_rw_param(struct gctl_req *req, const char *name, int len, void* value)
189{
190 struct gctl_req_arg *ap;
191
192 if (req == NULL || req->error != NULL)
193 return;
194 ap = gctl_new_arg(req);
195 if (ap == NULL)
196 return;
197 ap->name = strdup(name);
198 gctl_check_alloc(req, ap->name);
199 ap->nlen = strlen(ap->name) + 1;
200 ap->value = value;
201 ap->flag = GCTL_PARAM_RW;
202 if (len >= 0)
203 ap->len = len;
204 else if (len < 0)
205 ap->len = strlen(value) + 1;
206}
207
208void
209gctl_ro_meta(struct gctl_req *req, off_t offset, u_int len, const void* value)
210{
211 struct gctl_req_arg *ap;
212
213 if (req == NULL || req->error != NULL)
214 return;
215 ap = gctl_new_arg(req);
216 if (ap == NULL)
217 return;
218 ap->value = __DECONST(void *, value);
219 ap->flag = GCTL_PARAM_RD;
220 ap->offset = offset;
221 ap->len = len;
222}
223
224void
225gctl_rw_meta(struct gctl_req *req, off_t offset, u_int len, void* value)
226{
227 struct gctl_req_arg *ap;
228
229 if (req == NULL || req->error != NULL)
230 return;
231 ap = gctl_new_arg(req);
232 if (ap == NULL)
233 return;
234 ap->value = value;
235 ap->flag = GCTL_PARAM_RW;
236 ap->offset = offset;
237 ap->len = len;
238}
239
195const char *
240const char *
196geom_ctl_issue(struct geom_ctl_req *req)
241gctl_issue(struct gctl_req *req)
197{
198 int fd, error;
199
200 if (req == NULL)
201 return ("NULL request pointer");
202 if (req->error != NULL)
203 return (req->error);
204
242{
243 int fd, error;
244
245 if (req == NULL)
246 return ("NULL request pointer");
247 if (req->error != NULL)
248 return (req->error);
249
205 req->version = GEOM_CTL_VERSION;
250 req->version = GCTL_VERSION;
206 req->lerror = BUFSIZ; /* XXX: arbitrary number */
207 req->error = malloc(req->lerror);
251 req->lerror = BUFSIZ; /* XXX: arbitrary number */
252 req->error = malloc(req->lerror);
253 if (req->error == NULL) {
254 gctl_check_alloc(req, req->error);
255 return (req->error);
256 }
208 memset(req->error, 0, req->lerror);
209 req->lerror--;
210 fd = open(_PATH_DEV PATH_GEOM_CTL, O_RDONLY);
211 if (fd < 0)
212 return(strerror(errno));
213 error = ioctl(fd, GEOM_CTL, req);
257 memset(req->error, 0, req->lerror);
258 req->lerror--;
259 fd = open(_PATH_DEV PATH_GEOM_CTL, O_RDONLY);
260 if (fd < 0)
261 return(strerror(errno));
262 error = ioctl(fd, GEOM_CTL, req);
214 if (error && errno == EINVAL && req->error[0] != '\0')
263 close(fd);
264 if (req->error[0] != '\0')
215 return (req->error);
216 if (error != 0)
217 return(strerror(errno));
218 return (NULL);
219}
220
221void
265 return (req->error);
266 if (error != 0)
267 return(strerror(errno));
268 return (NULL);
269}
270
271void
222geom_ctl_free(struct geom_ctl_req *req)
272gctl_free(struct gctl_req *req)
223{
224 u_int i;
225
273{
274 u_int i;
275
276 if (req == NULL)
277 return;
226 for (i = 0; i < req->narg; i++) {
227 if (req->arg[i].name != NULL)
228 free(req->arg[i].name);
278 for (i = 0; i < req->narg; i++) {
279 if (req->arg[i].name != NULL)
280 free(req->arg[i].name);
229 if (req->arg[i].len < 0)
230 free(req->arg[i].value);
231 }
281 }
282 free(req->arg);
232 if (req->error != NULL)
233 free(req->error);
283 if (req->error != NULL)
284 free(req->error);
234 free(req->arg);
235 free(req);
236}
285 free(req);
286}
237