]> CyberLeo.Net >> Repos - FreeBSD/FreeBSD.git/blob - sys/geom/geom_ctl.c
malloc() with M_WAITOK doesn't return NULL.
[FreeBSD/FreeBSD.git] / sys / geom / geom_ctl.c
1 /*-
2  * Copyright (c) 2002 Poul-Henning Kamp
3  * Copyright (c) 2002 Networks Associates Technology, Inc.
4  * All rights reserved.
5  *
6  * This software was developed for the FreeBSD Project by Poul-Henning Kamp
7  * and NAI Labs, the Security Research Division of Network Associates, Inc.
8  * under DARPA/SPAWAR contract N66001-01-C-8035 ("CBOSS"), as part of the
9  * DARPA CHATS research program.
10  *
11  * Redistribution and use in source and binary forms, with or without
12  * modification, are permitted provided that the following conditions
13  * are met:
14  * 1. Redistributions of source code must retain the above copyright
15  *    notice, this list of conditions and the following disclaimer.
16  * 2. Redistributions in binary form must reproduce the above copyright
17  *    notice, this list of conditions and the following disclaimer in the
18  *    documentation and/or other materials provided with the distribution.
19  * 3. The names of the authors may not be used to endorse or promote
20  *    products derived from this software without specific prior written
21  *    permission.
22  *
23  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
24  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
25  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
26  * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
27  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
28  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
29  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
30  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
31  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
32  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
33  * SUCH DAMAGE.
34  */
35
36 #include <sys/cdefs.h>
37 __FBSDID("$FreeBSD$");
38
39 #include "opt_geom.h"
40
41 #include <sys/param.h>
42 #include <sys/systm.h>
43 #include <sys/kernel.h>
44 #include <sys/sysctl.h>
45 #include <sys/bio.h>
46 #include <sys/conf.h>
47 #include <sys/disk.h>
48 #include <sys/malloc.h>
49 #include <sys/sysctl.h>
50 #include <sys/sbuf.h>
51
52 #include <sys/lock.h>
53 #include <sys/mutex.h>
54
55 #include <vm/vm.h>
56 #include <vm/vm_extern.h>
57
58 #include <geom/geom.h>
59 #include <geom/geom_int.h>
60 #define GCTL_TABLE 1
61 #include <geom/geom_ctl.h>
62
63 #include <machine/stdarg.h>
64
65 static d_ioctl_t g_ctl_ioctl;
66
67 static struct cdevsw g_ctl_cdevsw = {
68         .d_version =    D_VERSION,
69         .d_flags =      D_NEEDGIANT,
70         .d_ioctl =      g_ctl_ioctl,
71         .d_name =       "g_ctl",
72 };
73
74 void
75 g_ctl_init(void)
76 {
77
78         make_dev_credf(MAKEDEV_ETERNAL, &g_ctl_cdevsw, 0, NULL,
79             UID_ROOT, GID_OPERATOR, 0640, PATH_GEOM_CTL);
80         KASSERT(GCTL_PARAM_RD == VM_PROT_READ,
81                 ("GCTL_PARAM_RD != VM_PROT_READ"));
82         KASSERT(GCTL_PARAM_WR == VM_PROT_WRITE,
83                 ("GCTL_PARAM_WR != VM_PROT_WRITE"));
84 }
85
86 /*
87  * Report an error back to the user in ascii format.  Return whatever copyout
88  * returned, or EINVAL if it succeeded.
89  */
90 int
91 gctl_error(struct gctl_req *req, const char *fmt, ...)
92 {
93         va_list ap;
94
95         if (req == NULL)
96                 return (EINVAL);
97
98         /* We only record the first error */
99         if (sbuf_done(req->serror)) {
100                 if (!req->nerror)
101                         req->nerror = EEXIST;
102         }
103         if (req->nerror)
104                 return (req->nerror);
105
106         va_start(ap, fmt);
107         sbuf_vprintf(req->serror, fmt, ap);
108         va_end(ap);
109         sbuf_finish(req->serror);
110         if (g_debugflags & G_F_CTLDUMP)
111                 printf("gctl %p error \"%s\"\n", req, sbuf_data(req->serror));
112         return (0);
113 }
114
115 /*
116  * Allocate space and copyin() something.
117  * XXX: this should really be a standard function in the kernel.
118  */
119 static void *
120 geom_alloc_copyin(struct gctl_req *req, void *uaddr, size_t len)
121 {
122         void *ptr;
123
124         ptr = g_malloc(len, M_WAITOK);
125         nreq->nerror = copyin(uaddr, ptr, len);
126         if (!req->nerror)
127                 return (ptr);
128         if (ptr != NULL)
129                 g_free(ptr);
130         return (NULL);
131 }
132
133 static void
134 gctl_copyin(struct gctl_req *req)
135 {
136         int error, i;
137         struct gctl_req_arg *ap;
138         char *p;
139
140         ap = geom_alloc_copyin(req, req->arg, req->narg * sizeof(*ap));
141         if (ap == NULL) {
142                 req->nerror = ENOMEM;
143                 req->arg = NULL;
144                 return;
145         }
146
147         /* Nothing have been copyin()'ed yet */
148         for (i = 0; i < req->narg; i++) {
149                 ap[i].flag &= ~(GCTL_PARAM_NAMEKERNEL|GCTL_PARAM_VALUEKERNEL);
150                 ap[i].flag &= ~GCTL_PARAM_CHANGED;
151                 ap[i].kvalue = NULL;
152         }
153
154         error = 0;
155         for (i = 0; i < req->narg; i++) {
156                 if (ap[i].nlen < 1 || ap[i].nlen > SPECNAMELEN) {
157                         error = gctl_error(req,
158                             "wrong param name length %d: %d", i, ap[i].nlen);
159                         break;
160                 }
161                 p = geom_alloc_copyin(req, ap[i].name, ap[i].nlen);
162                 if (p == NULL)
163                         break;
164                 if (p[ap[i].nlen - 1] != '\0') {
165                         error = gctl_error(req, "unterminated param name");
166                         g_free(p);
167                         break;
168                 }
169                 ap[i].name = p;
170                 ap[i].flag |= GCTL_PARAM_NAMEKERNEL;
171                 if (ap[i].len <= 0) {
172                         error = gctl_error(req, "negative param length");
173                         break;
174                 }
175                 p = geom_alloc_copyin(req, ap[i].value, ap[i].len);
176                 if (p == NULL)
177                         break;
178                 if ((ap[i].flag & GCTL_PARAM_ASCII) &&
179                     p[ap[i].len - 1] != '\0') {
180                         error = gctl_error(req, "unterminated param value");
181                         g_free(p);
182                         break;
183                 }
184                 ap[i].kvalue = p;
185                 ap[i].flag |= GCTL_PARAM_VALUEKERNEL;
186         }
187         req->arg = ap;
188         return;
189 }
190
191 static void
192 gctl_copyout(struct gctl_req *req)
193 {
194         int error, i;
195         struct gctl_req_arg *ap;
196
197         if (req->nerror)
198                 return;
199         error = 0;
200         ap = req->arg;
201         for (i = 0; i < req->narg; i++, ap++) {
202                 if (!(ap->flag & GCTL_PARAM_CHANGED))
203                         continue;
204                 error = copyout(ap->kvalue, ap->value, ap->len);
205                 if (!error)
206                         continue;
207                 req->nerror = error;
208                 return;
209         }
210         return;
211 }
212
213 static void
214 gctl_free(struct gctl_req *req)
215 {
216         int i;
217
218         if (req->arg == NULL)
219                 return;
220         for (i = 0; i < req->narg; i++) {
221                 if (req->arg[i].flag & GCTL_PARAM_NAMEKERNEL)
222                         g_free(req->arg[i].name);
223                 if ((req->arg[i].flag & GCTL_PARAM_VALUEKERNEL) &&
224                     req->arg[i].len > 0)
225                         g_free(req->arg[i].kvalue);
226         }
227         g_free(req->arg);
228         sbuf_delete(req->serror);
229 }
230
231 static void
232 gctl_dump(struct gctl_req *req)
233 {
234         u_int i;
235         int j;
236         struct gctl_req_arg *ap;
237
238         printf("Dump of gctl request at %p:\n", req);
239         if (req->nerror > 0) {
240                 printf("  nerror:\t%d\n", req->nerror);
241                 if (sbuf_len(req->serror) > 0)
242                         printf("  error:\t\"%s\"\n", sbuf_data(req->serror));
243         }
244         for (i = 0; i < req->narg; i++) {
245                 ap = &req->arg[i];
246                 if (!(ap->flag & GCTL_PARAM_NAMEKERNEL))
247                         printf("  param:\t%d@%p", ap->nlen, ap->name);
248                 else
249                         printf("  param:\t\"%s\"", ap->name);
250                 printf(" [%s%s%d] = ",
251                     ap->flag & GCTL_PARAM_RD ? "R" : "",
252                     ap->flag & GCTL_PARAM_WR ? "W" : "",
253                     ap->len);
254                 if (!(ap->flag & GCTL_PARAM_VALUEKERNEL)) {
255                         printf(" =@ %p", ap->value);
256                 } else if (ap->flag & GCTL_PARAM_ASCII) {
257                         printf("\"%s\"", (char *)ap->kvalue);
258                 } else if (ap->len > 0) {
259                         for (j = 0; j < ap->len && j < 512; j++)
260                                 printf(" %02x", ((u_char *)ap->kvalue)[j]);
261                 } else {
262                         printf(" = %p", ap->kvalue);
263                 }
264                 printf("\n");
265         }
266 }
267
268 int
269 gctl_set_param(struct gctl_req *req, const char *param, void const *ptr,
270     int len)
271 {
272         int i;
273         struct gctl_req_arg *ap;
274
275         for (i = 0; i < req->narg; i++) {
276                 ap = &req->arg[i];
277                 if (strcmp(param, ap->name))
278                         continue;
279                 if (!(ap->flag & GCTL_PARAM_WR))
280                         return (EPERM);
281                 ap->flag |= GCTL_PARAM_CHANGED;
282                 if (ap->len < len) {
283                         bcopy(ptr, ap->kvalue, ap->len);
284                         return (ENOSPC);
285                 }
286                 bcopy(ptr, ap->kvalue, len);
287                 return (0);
288         }
289         return (EINVAL);
290 }
291
292 void
293 gctl_set_param_err(struct gctl_req *req, const char *param, void const *ptr,
294     int len)
295 {
296
297         switch (gctl_set_param(req, param, ptr, len)) {
298         case EPERM:
299                 gctl_error(req, "No write access %s argument", param);
300                 break;
301         case ENOSPC:
302                 gctl_error(req, "Wrong length %s argument", param);
303                 break;
304         case EINVAL:
305                 gctl_error(req, "Missing %s argument", param);
306                 break;
307         }
308 }
309
310 void *
311 gctl_get_param(struct gctl_req *req, const char *param, int *len)
312 {
313         int i;
314         void *p;
315         struct gctl_req_arg *ap;
316
317         for (i = 0; i < req->narg; i++) {
318                 ap = &req->arg[i];
319                 if (strcmp(param, ap->name))
320                         continue;
321                 if (!(ap->flag & GCTL_PARAM_RD))
322                         continue;
323                 p = ap->kvalue;
324                 if (len != NULL)
325                         *len = ap->len;
326                 return (p);
327         }
328         return (NULL);
329 }
330
331 char const *
332 gctl_get_asciiparam(struct gctl_req *req, const char *param)
333 {
334         int i;
335         char const *p;
336         struct gctl_req_arg *ap;
337
338         for (i = 0; i < req->narg; i++) {
339                 ap = &req->arg[i];
340                 if (strcmp(param, ap->name))
341                         continue;
342                 if (!(ap->flag & GCTL_PARAM_RD))
343                         continue;
344                 p = ap->kvalue;
345                 if (ap->len < 1) {
346                         gctl_error(req, "No length argument (%s)", param);
347                         return (NULL);
348                 }
349                 if (p[ap->len - 1] != '\0') {
350                         gctl_error(req, "Unterminated argument (%s)", param);
351                         return (NULL);
352                 }
353                 return (p);
354         }
355         return (NULL);
356 }
357
358 void *
359 gctl_get_paraml(struct gctl_req *req, const char *param, int len)
360 {
361         int i;
362         void *p;
363
364         p = gctl_get_param(req, param, &i);
365         if (p == NULL)
366                 gctl_error(req, "Missing %s argument", param);
367         else if (i != len) {
368                 p = NULL;
369                 gctl_error(req, "Wrong length %s argument", param);
370         }
371         return (p);
372 }
373
374 struct g_class *
375 gctl_get_class(struct gctl_req *req, char const *arg)
376 {
377         char const *p;
378         struct g_class *cp;
379
380         p = gctl_get_asciiparam(req, arg);
381         if (p == NULL)
382                 return (NULL);
383         LIST_FOREACH(cp, &g_classes, class) {
384                 if (!strcmp(p, cp->name))
385                         return (cp);
386         }
387         return (NULL);
388 }
389
390 struct g_geom *
391 gctl_get_geom(struct gctl_req *req, struct g_class *mpr, char const *arg)
392 {
393         char const *p;
394         struct g_class *mp;
395         struct g_geom *gp;
396
397         p = gctl_get_asciiparam(req, arg);
398         if (p == NULL)
399                 return (NULL);
400         LIST_FOREACH(mp, &g_classes, class) {
401                 if (mpr != NULL && mpr != mp)
402                         continue;
403                 LIST_FOREACH(gp, &mp->geom, geom) {
404                         if (!strcmp(p, gp->name))
405                                 return (gp);
406                 }
407         }
408         gctl_error(req, "Geom not found: \"%s\"", p);
409         return (NULL);
410 }
411
412 struct g_provider *
413 gctl_get_provider(struct gctl_req *req, char const *arg)
414 {
415         char const *p;
416         struct g_provider *pp;
417
418         p = gctl_get_asciiparam(req, arg);
419         if (p == NULL)
420                 return (NULL);
421         pp = g_provider_by_name(p);
422         if (pp != NULL)
423                 return (pp);
424         gctl_error(req, "Provider not found: \"%s\"", p);
425         return (NULL);
426 }
427
428 static void
429 g_ctl_req(void *arg, int flag __unused)
430 {
431         struct g_class *mp;
432         struct gctl_req *req;
433         char const *verb;
434
435         g_topology_assert();
436         req = arg;
437         mp = gctl_get_class(req, "class");
438         if (mp == NULL) {
439                 gctl_error(req, "Class not found");
440                 return;
441         }
442         if (mp->ctlreq == NULL) {
443                 gctl_error(req, "Class takes no requests");
444                 return;
445         }
446         verb = gctl_get_param(req, "verb", NULL);
447         if (verb == NULL) {
448                 gctl_error(req, "Verb missing");
449                 return;
450         }
451         mp->ctlreq(req, mp, verb);
452         g_topology_assert();
453 }
454
455
456 static int
457 g_ctl_ioctl_ctl(struct cdev *dev, u_long cmd, caddr_t data, int fflag, struct thread *td)
458 {
459         struct gctl_req *req;
460         int nerror;
461
462         req = (void *)data;
463         req->nerror = 0;
464         req->serror = sbuf_new_auto();
465         /* It is an error if we cannot return an error text */
466         if (req->lerror < 2)
467                 return (EINVAL);
468         if (!useracc(req->error, req->lerror, VM_PROT_WRITE))
469                 return (EINVAL);
470
471         /* Check the version */
472         if (req->version != GCTL_VERSION)
473                 return (gctl_error(req,
474                     "kernel and libgeom version mismatch."));
475         
476         /* Get things on board */
477         gctl_copyin(req);
478
479         if (g_debugflags & G_F_CTLDUMP)
480                 gctl_dump(req);
481
482         if (!req->nerror) {
483                 g_waitfor_event(g_ctl_req, req, M_WAITOK, NULL);
484                 gctl_copyout(req);
485         }
486         if (sbuf_done(req->serror)) {
487                 req->nerror = copyout(sbuf_data(req->serror), req->error,
488                     imin(req->lerror, sbuf_len(req->serror) + 1));
489         }
490
491         nerror = req->nerror;
492         gctl_free(req);
493         return (nerror);
494 }
495
496 static int
497 g_ctl_ioctl(struct cdev *dev, u_long cmd, caddr_t data, int fflag, struct thread *td)
498 {
499         int error;
500
501         switch(cmd) {
502         case GEOM_CTL:
503                 error = g_ctl_ioctl_ctl(dev, cmd, data, fflag, td);
504                 break;
505         default:
506                 error = ENOIOCTL;
507                 break;
508         }
509         return (error);
510
511 }