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