1105068Sphk/*-
2105068Sphk * Copyright (c) 2002 Poul-Henning Kamp
3105068Sphk * Copyright (c) 2002 Networks Associates Technology, Inc.
4105068Sphk * All rights reserved.
5105068Sphk *
6105068Sphk * This software was developed for the FreeBSD Project by Poul-Henning Kamp
7105068Sphk * and NAI Labs, the Security Research Division of Network Associates, Inc.
8105068Sphk * under DARPA/SPAWAR contract N66001-01-C-8035 ("CBOSS"), as part of the
9105068Sphk * DARPA CHATS research program.
10105068Sphk *
11105068Sphk * Redistribution and use in source and binary forms, with or without
12105068Sphk * modification, are permitted provided that the following conditions
13105068Sphk * are met:
14105068Sphk * 1. Redistributions of source code must retain the above copyright
15105068Sphk *    notice, this list of conditions and the following disclaimer.
16105068Sphk * 2. Redistributions in binary form must reproduce the above copyright
17105068Sphk *    notice, this list of conditions and the following disclaimer in the
18105068Sphk *    documentation and/or other materials provided with the distribution.
19105068Sphk * 3. The names of the authors may not be used to endorse or promote
20105068Sphk *    products derived from this software without specific prior written
21105068Sphk *    permission.
22105068Sphk *
23105068Sphk * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
24105068Sphk * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
25105068Sphk * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
26105068Sphk * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
27105068Sphk * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
28105068Sphk * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
29105068Sphk * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
30105068Sphk * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
31105068Sphk * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
32105068Sphk * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
33105068Sphk * SUCH DAMAGE.
34105068Sphk */
35105068Sphk
36116196Sobrien#include <sys/cdefs.h>
37116196Sobrien__FBSDID("$FreeBSD: stable/11/sys/geom/geom_ctl.c 344584 2019-02-26 15:06:44Z markj $");
38116196Sobrien
39105068Sphk#include "opt_geom.h"
40105068Sphk
41105068Sphk#include <sys/param.h>
42105068Sphk#include <sys/systm.h>
43105068Sphk#include <sys/kernel.h>
44105068Sphk#include <sys/sysctl.h>
45105068Sphk#include <sys/bio.h>
46105068Sphk#include <sys/conf.h>
47105068Sphk#include <sys/disk.h>
48105068Sphk#include <sys/malloc.h>
49105068Sphk#include <sys/sysctl.h>
50113892Sphk#include <sys/sbuf.h>
51105068Sphk
52105068Sphk#include <sys/lock.h>
53105068Sphk#include <sys/mutex.h>
54112511Sphk
55112511Sphk#include <vm/vm.h>
56112511Sphk#include <vm/vm_extern.h>
57112511Sphk
58105068Sphk#include <geom/geom.h>
59105068Sphk#include <geom/geom_int.h>
60112709Sphk#define GCTL_TABLE 1
61112511Sphk#include <geom/geom_ctl.h>
62105068Sphk
63113892Sphk#include <machine/stdarg.h>
64113892Sphk
65105068Sphkstatic d_ioctl_t g_ctl_ioctl;
66105068Sphk
67112534Sphkstatic struct cdevsw g_ctl_cdevsw = {
68126080Sphk	.d_version =	D_VERSION,
69126080Sphk	.d_flags =	D_NEEDGIANT,
70112534Sphk	.d_ioctl =	g_ctl_ioctl,
71112534Sphk	.d_name =	"g_ctl",
72105068Sphk};
73105068Sphk
74112534Sphkvoid
75105068Sphkg_ctl_init(void)
76105068Sphk{
77105068Sphk
78216952Skib	make_dev_credf(MAKEDEV_ETERNAL, &g_ctl_cdevsw, 0, NULL,
79112534Sphk	    UID_ROOT, GID_OPERATOR, 0640, PATH_GEOM_CTL);
80112709Sphk	KASSERT(GCTL_PARAM_RD == VM_PROT_READ,
81112709Sphk		("GCTL_PARAM_RD != VM_PROT_READ"));
82112709Sphk	KASSERT(GCTL_PARAM_WR == VM_PROT_WRITE,
83112709Sphk		("GCTL_PARAM_WR != VM_PROT_WRITE"));
84105068Sphk}
85105068Sphk
86112511Sphk/*
87261085Sae * Report an error back to the user in ascii format.  Return nerror
88261085Sae * or EINVAL if nerror isn't specified.
89112511Sphk */
90112709Sphkint
91113892Sphkgctl_error(struct gctl_req *req, const char *fmt, ...)
92112511Sphk{
93113892Sphk	va_list ap;
94112511Sphk
95115624Sphk	if (req == NULL)
96115624Sphk		return (EINVAL);
97115624Sphk
98115624Sphk	/* We only record the first error */
99144789Spjd	if (sbuf_done(req->serror)) {
100144789Spjd		if (!req->nerror)
101144789Spjd			req->nerror = EEXIST;
102261085Sae		return (req->nerror);
103144789Spjd	}
104261085Sae	if (!req->nerror)
105261085Sae		req->nerror = EINVAL;
106115624Sphk
107115624Sphk	va_start(ap, fmt);
108115624Sphk	sbuf_vprintf(req->serror, fmt, ap);
109115949Sphk	va_end(ap);
110115624Sphk	sbuf_finish(req->serror);
111115624Sphk	if (g_debugflags & G_F_CTLDUMP)
112115624Sphk		printf("gctl %p error \"%s\"\n", req, sbuf_data(req->serror));
113261085Sae	return (req->nerror);
114112511Sphk}
115112511Sphk
116112511Sphk/*
117112511Sphk * Allocate space and copyin() something.
118112511Sphk * XXX: this should really be a standard function in the kernel.
119112511Sphk */
120112511Sphkstatic void *
121115624Sphkgeom_alloc_copyin(struct gctl_req *req, void *uaddr, size_t len)
122112511Sphk{
123112511Sphk	void *ptr;
124112511Sphk
125112511Sphk	ptr = g_malloc(len, M_WAITOK);
126261085Sae	req->nerror = copyin(uaddr, ptr, len);
127115624Sphk	if (!req->nerror)
128112511Sphk		return (ptr);
129261089Sae	g_free(ptr);
130112511Sphk	return (NULL);
131112511Sphk}
132112511Sphk
133115624Sphkstatic void
134112709Sphkgctl_copyin(struct gctl_req *req)
135112511Sphk{
136112709Sphk	struct gctl_req_arg *ap;
137112511Sphk	char *p;
138298698Spfg	u_int i;
139112511Sphk
140344584Smarkj	if (req->narg > GEOM_CTL_ARG_MAX) {
141344584Smarkj		gctl_error(req, "too many arguments");
142344584Smarkj		req->arg = NULL;
143344584Smarkj		return;
144344584Smarkj	}
145344584Smarkj
146115624Sphk	ap = geom_alloc_copyin(req, req->arg, req->narg * sizeof(*ap));
147112709Sphk	if (ap == NULL) {
148261086Sae		gctl_error(req, "bad control request");
149115624Sphk		req->arg = NULL;
150115624Sphk		return;
151112709Sphk	}
152112709Sphk
153115624Sphk	/* Nothing have been copyin()'ed yet */
154115624Sphk	for (i = 0; i < req->narg; i++) {
155115624Sphk		ap[i].flag &= ~(GCTL_PARAM_NAMEKERNEL|GCTL_PARAM_VALUEKERNEL);
156115624Sphk		ap[i].flag &= ~GCTL_PARAM_CHANGED;
157115624Sphk		ap[i].kvalue = NULL;
158115624Sphk	}
159115624Sphk
160115624Sphk	for (i = 0; i < req->narg; i++) {
161112709Sphk		if (ap[i].nlen < 1 || ap[i].nlen > SPECNAMELEN) {
162261086Sae			gctl_error(req,
163115624Sphk			    "wrong param name length %d: %d", i, ap[i].nlen);
164112511Sphk			break;
165112709Sphk		}
166115624Sphk		p = geom_alloc_copyin(req, ap[i].name, ap[i].nlen);
167112709Sphk		if (p == NULL)
168112511Sphk			break;
169112709Sphk		if (p[ap[i].nlen - 1] != '\0') {
170261086Sae			gctl_error(req, "unterminated param name");
171112511Sphk			g_free(p);
172112511Sphk			break;
173112511Sphk		}
174112709Sphk		ap[i].name = p;
175115624Sphk		ap[i].flag |= GCTL_PARAM_NAMEKERNEL;
176140367Sphk		if (ap[i].len <= 0) {
177261086Sae			gctl_error(req, "negative param length");
178115624Sphk			break;
179115624Sphk		}
180115624Sphk		p = geom_alloc_copyin(req, ap[i].value, ap[i].len);
181115624Sphk		if (p == NULL)
182115624Sphk			break;
183115624Sphk		if ((ap[i].flag & GCTL_PARAM_ASCII) &&
184115624Sphk		    p[ap[i].len - 1] != '\0') {
185261086Sae			gctl_error(req, "unterminated param value");
186115624Sphk			g_free(p);
187115624Sphk			break;
188115624Sphk		}
189115624Sphk		ap[i].kvalue = p;
190115624Sphk		ap[i].flag |= GCTL_PARAM_VALUEKERNEL;
191112511Sphk	}
192115624Sphk	req->arg = ap;
193115624Sphk	return;
194115624Sphk}
195115624Sphk
196115624Sphkstatic void
197115624Sphkgctl_copyout(struct gctl_req *req)
198115624Sphk{
199115624Sphk	int error, i;
200115624Sphk	struct gctl_req_arg *ap;
201115624Sphk
202115624Sphk	if (req->nerror)
203115624Sphk		return;
204115624Sphk	error = 0;
205115624Sphk	ap = req->arg;
206115624Sphk	for (i = 0; i < req->narg; i++, ap++) {
207115624Sphk		if (!(ap->flag & GCTL_PARAM_CHANGED))
208115624Sphk			continue;
209115624Sphk		error = copyout(ap->kvalue, ap->value, ap->len);
210115624Sphk		if (!error)
211115624Sphk			continue;
212115624Sphk		req->nerror = error;
213115624Sphk		return;
214112511Sphk	}
215115624Sphk	return;
216112511Sphk}
217112511Sphk
218112511Sphkstatic void
219114531Sphkgctl_free(struct gctl_req *req)
220114531Sphk{
221298698Spfg	u_int i;
222114531Sphk
223261091Sae	sbuf_delete(req->serror);
224115624Sphk	if (req->arg == NULL)
225115624Sphk		return;
226114531Sphk	for (i = 0; i < req->narg; i++) {
227115624Sphk		if (req->arg[i].flag & GCTL_PARAM_NAMEKERNEL)
228114531Sphk			g_free(req->arg[i].name);
229115624Sphk		if ((req->arg[i].flag & GCTL_PARAM_VALUEKERNEL) &&
230115624Sphk		    req->arg[i].len > 0)
231115624Sphk			g_free(req->arg[i].kvalue);
232114531Sphk	}
233114531Sphk	g_free(req->arg);
234114531Sphk}
235114531Sphk
236114531Sphkstatic void
237112709Sphkgctl_dump(struct gctl_req *req)
238112511Sphk{
239261086Sae	struct gctl_req_arg *ap;
240112511Sphk	u_int i;
241115624Sphk	int j;
242112511Sphk
243115624Sphk	printf("Dump of gctl request at %p:\n", req);
244115624Sphk	if (req->nerror > 0) {
245115624Sphk		printf("  nerror:\t%d\n", req->nerror);
246115624Sphk		if (sbuf_len(req->serror) > 0)
247115624Sphk			printf("  error:\t\"%s\"\n", sbuf_data(req->serror));
248112511Sphk	}
249261086Sae	if (req->arg == NULL)
250261086Sae		return;
251112511Sphk	for (i = 0; i < req->narg; i++) {
252112511Sphk		ap = &req->arg[i];
253115624Sphk		if (!(ap->flag & GCTL_PARAM_NAMEKERNEL))
254115624Sphk			printf("  param:\t%d@%p", ap->nlen, ap->name);
255115624Sphk		else
256115624Sphk			printf("  param:\t\"%s\"", ap->name);
257112709Sphk		printf(" [%s%s%d] = ",
258112709Sphk		    ap->flag & GCTL_PARAM_RD ? "R" : "",
259112709Sphk		    ap->flag & GCTL_PARAM_WR ? "W" : "",
260112709Sphk		    ap->len);
261115624Sphk		if (!(ap->flag & GCTL_PARAM_VALUEKERNEL)) {
262115624Sphk			printf(" =@ %p", ap->value);
263115624Sphk		} else if (ap->flag & GCTL_PARAM_ASCII) {
264115624Sphk			printf("\"%s\"", (char *)ap->kvalue);
265112511Sphk		} else if (ap->len > 0) {
266117150Sphk			for (j = 0; j < ap->len && j < 512; j++)
267115624Sphk				printf(" %02x", ((u_char *)ap->kvalue)[j]);
268112511Sphk		} else {
269115624Sphk			printf(" = %p", ap->kvalue);
270112511Sphk		}
271112511Sphk		printf("\n");
272112511Sphk	}
273112511Sphk}
274112511Sphk
275157581Smarcelint
276157581Smarcelgctl_set_param(struct gctl_req *req, const char *param, void const *ptr,
277157581Smarcel    int len)
278114670Sphk{
279298698Spfg	u_int i;
280114670Sphk	struct gctl_req_arg *ap;
281114670Sphk
282114670Sphk	for (i = 0; i < req->narg; i++) {
283114670Sphk		ap = &req->arg[i];
284114670Sphk		if (strcmp(param, ap->name))
285114670Sphk			continue;
286157581Smarcel		if (!(ap->flag & GCTL_PARAM_WR))
287157581Smarcel			return (EPERM);
288157581Smarcel		ap->flag |= GCTL_PARAM_CHANGED;
289115624Sphk		if (ap->len < len) {
290157581Smarcel			bcopy(ptr, ap->kvalue, ap->len);
291157581Smarcel			return (ENOSPC);
292114670Sphk		}
293115624Sphk		bcopy(ptr, ap->kvalue, len);
294157581Smarcel		return (0);
295114670Sphk	}
296157581Smarcel	return (EINVAL);
297114670Sphk}
298114670Sphk
299157581Smarcelvoid
300157581Smarcelgctl_set_param_err(struct gctl_req *req, const char *param, void const *ptr,
301157581Smarcel    int len)
302157581Smarcel{
303157581Smarcel
304157581Smarcel	switch (gctl_set_param(req, param, ptr, len)) {
305157581Smarcel	case EPERM:
306157581Smarcel		gctl_error(req, "No write access %s argument", param);
307157581Smarcel		break;
308157581Smarcel	case ENOSPC:
309157581Smarcel		gctl_error(req, "Wrong length %s argument", param);
310157581Smarcel		break;
311157581Smarcel	case EINVAL:
312157581Smarcel		gctl_error(req, "Missing %s argument", param);
313157581Smarcel		break;
314157581Smarcel	}
315157581Smarcel}
316157581Smarcel
317112709Sphkvoid *
318112709Sphkgctl_get_param(struct gctl_req *req, const char *param, int *len)
319112709Sphk{
320298698Spfg	u_int i;
321112709Sphk	void *p;
322112709Sphk	struct gctl_req_arg *ap;
323112709Sphk
324112709Sphk	for (i = 0; i < req->narg; i++) {
325112709Sphk		ap = &req->arg[i];
326112709Sphk		if (strcmp(param, ap->name))
327112709Sphk			continue;
328112709Sphk		if (!(ap->flag & GCTL_PARAM_RD))
329112709Sphk			continue;
330115624Sphk		p = ap->kvalue;
331112709Sphk		if (len != NULL)
332115624Sphk			*len = ap->len;
333112709Sphk		return (p);
334112709Sphk	}
335112709Sphk	return (NULL);
336112709Sphk}
337112709Sphk
338115624Sphkchar const *
339115624Sphkgctl_get_asciiparam(struct gctl_req *req, const char *param)
340115624Sphk{
341298698Spfg	u_int i;
342115624Sphk	char const *p;
343115624Sphk	struct gctl_req_arg *ap;
344115624Sphk
345115624Sphk	for (i = 0; i < req->narg; i++) {
346115624Sphk		ap = &req->arg[i];
347115624Sphk		if (strcmp(param, ap->name))
348115624Sphk			continue;
349115624Sphk		if (!(ap->flag & GCTL_PARAM_RD))
350115624Sphk			continue;
351115624Sphk		p = ap->kvalue;
352115624Sphk		if (ap->len < 1) {
353115624Sphk			gctl_error(req, "No length argument (%s)", param);
354115624Sphk			return (NULL);
355115624Sphk		}
356115624Sphk		if (p[ap->len - 1] != '\0') {
357115624Sphk			gctl_error(req, "Unterminated argument (%s)", param);
358115624Sphk			return (NULL);
359115624Sphk		}
360115624Sphk		return (p);
361115624Sphk	}
362115624Sphk	return (NULL);
363115624Sphk}
364115624Sphk
365113893Sphkvoid *
366113893Sphkgctl_get_paraml(struct gctl_req *req, const char *param, int len)
367113893Sphk{
368113893Sphk	int i;
369113893Sphk	void *p;
370113893Sphk
371113893Sphk	p = gctl_get_param(req, param, &i);
372113893Sphk	if (p == NULL)
373113893Sphk		gctl_error(req, "Missing %s argument", param);
374113893Sphk	else if (i != len) {
375113893Sphk		p = NULL;
376113893Sphk		gctl_error(req, "Wrong length %s argument", param);
377113893Sphk	}
378113893Sphk	return (p);
379113893Sphk}
380113893Sphk
381115624Sphkstruct g_class *
382115624Sphkgctl_get_class(struct gctl_req *req, char const *arg)
383112709Sphk{
384115624Sphk	char const *p;
385112709Sphk	struct g_class *cp;
386112709Sphk
387115624Sphk	p = gctl_get_asciiparam(req, arg);
388112709Sphk	if (p == NULL)
389112709Sphk		return (NULL);
390112709Sphk	LIST_FOREACH(cp, &g_classes, class) {
391115624Sphk		if (!strcmp(p, cp->name))
392112709Sphk			return (cp);
393112709Sphk	}
394112709Sphk	return (NULL);
395112709Sphk}
396112709Sphk
397115624Sphkstruct g_geom *
398115624Sphkgctl_get_geom(struct gctl_req *req, struct g_class *mpr, char const *arg)
399112709Sphk{
400115624Sphk	char const *p;
401112709Sphk	struct g_class *mp;
402112709Sphk	struct g_geom *gp;
403112709Sphk
404115624Sphk	p = gctl_get_asciiparam(req, arg);
405168052Sdelphij	if (p == NULL)
406168052Sdelphij		return (NULL);
407168052Sdelphij	LIST_FOREACH(mp, &g_classes, class) {
408168052Sdelphij		if (mpr != NULL && mpr != mp)
409168052Sdelphij			continue;
410168052Sdelphij		LIST_FOREACH(gp, &mp->geom, geom) {
411168052Sdelphij			if (!strcmp(p, gp->name))
412168052Sdelphij				return (gp);
413112709Sphk		}
414112709Sphk	}
415168052Sdelphij	gctl_error(req, "Geom not found: \"%s\"", p);
416112709Sphk	return (NULL);
417112709Sphk}
418112709Sphk
419115624Sphkstruct g_provider *
420115624Sphkgctl_get_provider(struct gctl_req *req, char const *arg)
421112709Sphk{
422115624Sphk	char const *p;
423112709Sphk	struct g_provider *pp;
424112709Sphk
425115624Sphk	p = gctl_get_asciiparam(req, arg);
426112709Sphk	if (p == NULL)
427112709Sphk		return (NULL);
428115850Sphk	pp = g_provider_by_name(p);
429115850Sphk	if (pp != NULL)
430115850Sphk		return (pp);
431168052Sdelphij	gctl_error(req, "Provider not found: \"%s\"", p);
432112709Sphk	return (NULL);
433112709Sphk}
434112709Sphk
435115624Sphkstatic void
436115624Sphkg_ctl_req(void *arg, int flag __unused)
437112709Sphk{
438112709Sphk	struct g_class *mp;
439115624Sphk	struct gctl_req *req;
440115624Sphk	char const *verb;
441112709Sphk
442112709Sphk	g_topology_assert();
443115624Sphk	req = arg;
444115624Sphk	mp = gctl_get_class(req, "class");
445115726Sphk	if (mp == NULL) {
446115726Sphk		gctl_error(req, "Class not found");
447115624Sphk		return;
448115726Sphk	}
449150304Smarcel	if (mp->ctlreq == NULL) {
450150304Smarcel		gctl_error(req, "Class takes no requests");
451150304Smarcel		return;
452150304Smarcel	}
453115624Sphk	verb = gctl_get_param(req, "verb", NULL);
454150304Smarcel	if (verb == NULL) {
455150304Smarcel		gctl_error(req, "Verb missing");
456150304Smarcel		return;
457150304Smarcel	}
458150304Smarcel	mp->ctlreq(req, mp, verb);
459112709Sphk	g_topology_assert();
460112709Sphk}
461112709Sphk
462112709Sphk
463113876Sphkstatic int
464130585Sphkg_ctl_ioctl_ctl(struct cdev *dev, u_long cmd, caddr_t data, int fflag, struct thread *td)
465112511Sphk{
466112709Sphk	struct gctl_req *req;
467136839Sphk	int nerror;
468112511Sphk
469112511Sphk	req = (void *)data;
470115624Sphk	req->nerror = 0;
471112709Sphk	/* It is an error if we cannot return an error text */
472115624Sphk	if (req->lerror < 2)
473112511Sphk		return (EINVAL);
474112709Sphk	if (!useracc(req->error, req->lerror, VM_PROT_WRITE))
475112709Sphk		return (EINVAL);
476112709Sphk
477261089Sae	req->serror = sbuf_new_auto();
478112709Sphk	/* Check the version */
479261089Sae	if (req->version != GCTL_VERSION) {
480261089Sae		gctl_error(req, "kernel and libgeom version mismatch.");
481261089Sae		req->arg = NULL;
482261089Sae	} else {
483261089Sae		/* Get things on board */
484261089Sae		gctl_copyin(req);
485112709Sphk
486261089Sae		if (g_debugflags & G_F_CTLDUMP)
487261089Sae			gctl_dump(req);
488115624Sphk
489261089Sae		if (!req->nerror) {
490261089Sae			g_waitfor_event(g_ctl_req, req, M_WAITOK, NULL);
491261089Sae			gctl_copyout(req);
492261089Sae		}
493112709Sphk	}
494144789Spjd	if (sbuf_done(req->serror)) {
495261089Sae		copyout(sbuf_data(req->serror), req->error,
496144789Spjd		    imin(req->lerror, sbuf_len(req->serror) + 1));
497144789Spjd	}
498115624Sphk
499136839Sphk	nerror = req->nerror;
500114531Sphk	gctl_free(req);
501136839Sphk	return (nerror);
502112511Sphk}
503112511Sphk
504112511Sphkstatic int
505130585Sphkg_ctl_ioctl(struct cdev *dev, u_long cmd, caddr_t data, int fflag, struct thread *td)
506105068Sphk{
507105068Sphk	int error;
508105068Sphk
509105068Sphk	switch(cmd) {
510112511Sphk	case GEOM_CTL:
511112511Sphk		error = g_ctl_ioctl_ctl(dev, cmd, data, fflag, td);
512112511Sphk		break;
513105068Sphk	default:
514115624Sphk		error = ENOIOCTL;
515105068Sphk		break;
516105068Sphk	}
517105068Sphk	return (error);
518105068Sphk
519105068Sphk}
520