2002-10-13 22:33:33 +02:00
|
|
|
/*-
|
2017-11-27 16:17:37 +01:00
|
|
|
* SPDX-License-Identifier: BSD-3-Clause
|
|
|
|
*
|
2002-10-13 22:33:33 +02:00
|
|
|
* Copyright (c) 2002 Poul-Henning Kamp
|
|
|
|
* Copyright (c) 2002 Networks Associates Technology, Inc.
|
|
|
|
* All rights reserved.
|
GEOM: Introduce partial confxml API
Traditionally the GEOM's primary channel of information from kernel to
user-space was confxml, fetched by libgeom through kern.geom.confxml
sysctl. It is convenient and informative, representing full state of
GEOM in a single XML document. But problems start to arise on systems
with hundreds of disks, where the full confxml size reaches many
megabytes, taking significant time to first write it and then parse.
This patch introduces alternative solution, allowing to fetch much
smaller XML document, subset of the full confxml, limited to 64KB and
representing only one specified geom and optionally its parents. It
uses existing GEOM control interface, extended with new "getxml" verb.
In case of any error, such as the buffer overflow, it just transparently
falls back to traditional full confxml. This patch uses the new API in
user-space GEOM tools where it is possible.
Reviewed by: imp
MFC after: 2 month
Sponsored by: iXsystems, Inc.
Differential Revision: https://reviews.freebsd.org/D34529
2022-03-12 17:49:37 +01:00
|
|
|
* Copyright (c) 2022 Alexander Motin <mav@FreeBSD.org>
|
2002-10-13 22:33:33 +02:00
|
|
|
*
|
|
|
|
* This software was developed for the FreeBSD Project by Poul-Henning Kamp
|
|
|
|
* and NAI Labs, the Security Research Division of Network Associates, Inc.
|
|
|
|
* under DARPA/SPAWAR contract N66001-01-C-8035 ("CBOSS"), as part of the
|
|
|
|
* DARPA CHATS research program.
|
|
|
|
*
|
|
|
|
* Redistribution and use in source and binary forms, with or without
|
|
|
|
* modification, are permitted provided that the following conditions
|
|
|
|
* are met:
|
|
|
|
* 1. Redistributions of source code must retain the above copyright
|
|
|
|
* notice, this list of conditions and the following disclaimer.
|
|
|
|
* 2. Redistributions in binary form must reproduce the above copyright
|
|
|
|
* notice, this list of conditions and the following disclaimer in the
|
|
|
|
* documentation and/or other materials provided with the distribution.
|
|
|
|
* 3. The names of the authors may not be used to endorse or promote
|
|
|
|
* products derived from this software without specific prior written
|
|
|
|
* permission.
|
|
|
|
*
|
|
|
|
* THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
|
|
|
|
* ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
|
|
|
|
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
|
|
|
|
* ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
|
|
|
|
* FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
|
|
|
|
* DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
|
|
|
|
* OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
|
|
|
|
* HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
|
|
|
|
* LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
|
|
|
|
* OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
|
|
|
|
* SUCH DAMAGE.
|
|
|
|
*/
|
|
|
|
|
2003-06-11 08:49:16 +02:00
|
|
|
#include <sys/cdefs.h>
|
|
|
|
__FBSDID("$FreeBSD$");
|
|
|
|
|
2002-10-13 22:33:33 +02:00
|
|
|
#include <sys/param.h>
|
|
|
|
#include <sys/systm.h>
|
|
|
|
#include <sys/conf.h>
|
|
|
|
#include <sys/malloc.h>
|
2003-04-23 09:50:01 +02:00
|
|
|
#include <sys/sbuf.h>
|
2002-10-13 22:33:33 +02:00
|
|
|
|
2003-03-23 11:16:14 +01:00
|
|
|
#include <vm/vm.h>
|
|
|
|
#include <vm/vm_extern.h>
|
|
|
|
|
2002-10-13 22:33:33 +02:00
|
|
|
#include <geom/geom.h>
|
|
|
|
#include <geom/geom_int.h>
|
2003-03-27 15:35:00 +01:00
|
|
|
#define GCTL_TABLE 1
|
2003-03-23 11:16:14 +01:00
|
|
|
#include <geom/geom_ctl.h>
|
|
|
|
|
2003-04-23 09:50:01 +02:00
|
|
|
#include <machine/stdarg.h>
|
|
|
|
|
2002-10-13 22:33:33 +02:00
|
|
|
static d_ioctl_t g_ctl_ioctl;
|
|
|
|
|
2003-03-24 14:37:15 +01:00
|
|
|
static struct cdevsw g_ctl_cdevsw = {
|
2004-02-21 22:10:55 +01:00
|
|
|
.d_version = D_VERSION,
|
2019-11-24 00:44:00 +01:00
|
|
|
.d_flags = 0,
|
2003-03-24 14:37:15 +01:00
|
|
|
.d_ioctl = g_ctl_ioctl,
|
|
|
|
.d_name = "g_ctl",
|
2002-10-13 22:33:33 +02:00
|
|
|
};
|
|
|
|
|
2022-03-07 16:57:56 +01:00
|
|
|
CTASSERT(GCTL_PARAM_RD == VM_PROT_READ);
|
|
|
|
CTASSERT(GCTL_PARAM_WR == VM_PROT_WRITE);
|
|
|
|
|
2003-03-24 14:37:15 +01:00
|
|
|
void
|
2002-10-13 22:33:33 +02:00
|
|
|
g_ctl_init(void)
|
|
|
|
{
|
|
|
|
|
2011-01-04 11:59:38 +01:00
|
|
|
make_dev_credf(MAKEDEV_ETERNAL, &g_ctl_cdevsw, 0, NULL,
|
2003-03-24 14:37:15 +01:00
|
|
|
UID_ROOT, GID_OPERATOR, 0640, PATH_GEOM_CTL);
|
2002-10-13 22:33:33 +02:00
|
|
|
}
|
|
|
|
|
2003-03-23 11:16:14 +01:00
|
|
|
/*
|
2014-01-23 20:31:17 +01:00
|
|
|
* Report an error back to the user in ascii format. Return nerror
|
|
|
|
* or EINVAL if nerror isn't specified.
|
2003-03-23 11:16:14 +01:00
|
|
|
*/
|
2003-03-27 15:35:00 +01:00
|
|
|
int
|
2003-04-23 09:50:01 +02:00
|
|
|
gctl_error(struct gctl_req *req, const char *fmt, ...)
|
2003-03-23 11:16:14 +01:00
|
|
|
{
|
2003-04-23 09:50:01 +02:00
|
|
|
va_list ap;
|
2003-06-01 15:47:51 +02:00
|
|
|
|
|
|
|
if (req == NULL)
|
|
|
|
return (EINVAL);
|
|
|
|
|
|
|
|
/* We only record the first error */
|
2005-04-08 11:28:08 +02:00
|
|
|
if (sbuf_done(req->serror)) {
|
|
|
|
if (!req->nerror)
|
|
|
|
req->nerror = EEXIST;
|
2022-02-20 06:30:37 +01:00
|
|
|
#ifdef DIAGNOSTIC
|
|
|
|
printf("gctl_error: buffer closed, message discarded.\n");
|
|
|
|
#endif
|
2003-06-01 15:47:51 +02:00
|
|
|
return (req->nerror);
|
2014-01-23 20:31:17 +01:00
|
|
|
}
|
|
|
|
if (!req->nerror)
|
|
|
|
req->nerror = EINVAL;
|
2003-06-01 15:47:51 +02:00
|
|
|
|
2022-02-20 06:30:37 +01:00
|
|
|
/* If this is the last of several messages, indent it on a new line */
|
|
|
|
if (sbuf_len(req->serror) > 0)
|
|
|
|
sbuf_cat(req->serror, "\n\t");
|
2003-06-01 15:47:51 +02:00
|
|
|
va_start(ap, fmt);
|
|
|
|
sbuf_vprintf(req->serror, fmt, ap);
|
2003-06-07 12:16:53 +02:00
|
|
|
va_end(ap);
|
2022-02-20 06:30:37 +01:00
|
|
|
gctl_post_messages(req);
|
|
|
|
return (req->nerror);
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* The gctl_error() function will only report a single message.
|
|
|
|
* Commands that handle multiple devices may want to report a
|
|
|
|
* message for each of the devices. The gctl_msg() function
|
|
|
|
* can be called multiple times to post messages. When done
|
|
|
|
* the application must either call gctl_post_messages() or
|
|
|
|
* call gctl_error() to cause the messages to be reported to
|
|
|
|
* the calling process.
|
|
|
|
*/
|
|
|
|
void
|
|
|
|
gctl_msg(struct gctl_req *req, const char *fmt, ...)
|
|
|
|
{
|
|
|
|
va_list ap;
|
|
|
|
|
|
|
|
if (req == NULL)
|
|
|
|
return;
|
|
|
|
if (sbuf_done(req->serror)) {
|
|
|
|
#ifdef DIAGNOSTIC
|
|
|
|
printf("gctl_msg: buffer closed, message discarded.\n");
|
|
|
|
#endif
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
/* Put second and later messages indented on a new line */
|
|
|
|
if (sbuf_len(req->serror) > 0)
|
|
|
|
sbuf_cat(req->serror, "\n\t");
|
|
|
|
va_start(ap, fmt);
|
|
|
|
sbuf_vprintf(req->serror, fmt, ap);
|
|
|
|
va_end(ap);
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Post the messages to the user.
|
|
|
|
*/
|
|
|
|
void
|
|
|
|
gctl_post_messages(struct gctl_req *req)
|
|
|
|
{
|
|
|
|
|
|
|
|
if (sbuf_done(req->serror)) {
|
|
|
|
#ifdef DIAGNOSTIC
|
|
|
|
printf("gctl_post_messages: message buffer already closed.\n");
|
|
|
|
#endif
|
|
|
|
return;
|
|
|
|
}
|
2003-06-01 15:47:51 +02:00
|
|
|
sbuf_finish(req->serror);
|
|
|
|
if (g_debugflags & G_F_CTLDUMP)
|
2022-02-20 06:30:37 +01:00
|
|
|
printf("gctl %p message(s) \"%s\"\n", req,
|
|
|
|
sbuf_data(req->serror));
|
2003-03-23 11:16:14 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Allocate space and copyin() something.
|
|
|
|
* XXX: this should really be a standard function in the kernel.
|
|
|
|
*/
|
|
|
|
static void *
|
2003-06-01 15:47:51 +02:00
|
|
|
geom_alloc_copyin(struct gctl_req *req, void *uaddr, size_t len)
|
2003-03-23 11:16:14 +01:00
|
|
|
{
|
|
|
|
void *ptr;
|
|
|
|
|
|
|
|
ptr = g_malloc(len, M_WAITOK);
|
2014-01-23 20:31:17 +01:00
|
|
|
req->nerror = copyin(uaddr, ptr, len);
|
2003-06-01 15:47:51 +02:00
|
|
|
if (!req->nerror)
|
2003-03-23 11:16:14 +01:00
|
|
|
return (ptr);
|
2014-01-23 21:25:38 +01:00
|
|
|
g_free(ptr);
|
2003-03-23 11:16:14 +01:00
|
|
|
return (NULL);
|
|
|
|
}
|
|
|
|
|
2003-06-01 15:47:51 +02:00
|
|
|
static void
|
2003-03-27 15:35:00 +01:00
|
|
|
gctl_copyin(struct gctl_req *req)
|
2003-03-23 11:16:14 +01:00
|
|
|
{
|
2003-03-27 15:35:00 +01:00
|
|
|
struct gctl_req_arg *ap;
|
2003-03-23 11:16:14 +01:00
|
|
|
char *p;
|
2016-04-27 17:10:40 +02:00
|
|
|
u_int i;
|
2003-03-23 11:16:14 +01:00
|
|
|
|
2019-02-20 18:07:08 +01:00
|
|
|
if (req->narg > GEOM_CTL_ARG_MAX) {
|
2019-02-19 22:22:22 +01:00
|
|
|
gctl_error(req, "too many arguments");
|
|
|
|
req->arg = NULL;
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2003-06-01 15:47:51 +02:00
|
|
|
ap = geom_alloc_copyin(req, req->arg, req->narg * sizeof(*ap));
|
2003-03-27 15:35:00 +01:00
|
|
|
if (ap == NULL) {
|
2014-01-23 20:55:02 +01:00
|
|
|
gctl_error(req, "bad control request");
|
2003-06-01 15:47:51 +02:00
|
|
|
req->arg = NULL;
|
|
|
|
return;
|
2003-03-27 15:35:00 +01:00
|
|
|
}
|
|
|
|
|
2003-06-01 15:47:51 +02:00
|
|
|
/* Nothing have been copyin()'ed yet */
|
|
|
|
for (i = 0; i < req->narg; i++) {
|
|
|
|
ap[i].flag &= ~(GCTL_PARAM_NAMEKERNEL|GCTL_PARAM_VALUEKERNEL);
|
|
|
|
ap[i].flag &= ~GCTL_PARAM_CHANGED;
|
|
|
|
ap[i].kvalue = NULL;
|
|
|
|
}
|
|
|
|
|
|
|
|
for (i = 0; i < req->narg; i++) {
|
2003-03-27 15:35:00 +01:00
|
|
|
if (ap[i].nlen < 1 || ap[i].nlen > SPECNAMELEN) {
|
2014-01-23 20:55:02 +01:00
|
|
|
gctl_error(req,
|
2003-06-01 15:47:51 +02:00
|
|
|
"wrong param name length %d: %d", i, ap[i].nlen);
|
2003-03-23 11:16:14 +01:00
|
|
|
break;
|
2003-03-27 15:35:00 +01:00
|
|
|
}
|
2003-06-01 15:47:51 +02:00
|
|
|
p = geom_alloc_copyin(req, ap[i].name, ap[i].nlen);
|
2003-03-27 15:35:00 +01:00
|
|
|
if (p == NULL)
|
2003-03-23 11:16:14 +01:00
|
|
|
break;
|
2003-03-27 15:35:00 +01:00
|
|
|
if (p[ap[i].nlen - 1] != '\0') {
|
2014-01-23 20:55:02 +01:00
|
|
|
gctl_error(req, "unterminated param name");
|
2003-03-23 11:16:14 +01:00
|
|
|
g_free(p);
|
|
|
|
break;
|
|
|
|
}
|
2003-03-27 15:35:00 +01:00
|
|
|
ap[i].name = p;
|
2003-06-01 15:47:51 +02:00
|
|
|
ap[i].flag |= GCTL_PARAM_NAMEKERNEL;
|
2005-01-17 08:14:24 +01:00
|
|
|
if (ap[i].len <= 0) {
|
2014-01-23 20:55:02 +01:00
|
|
|
gctl_error(req, "negative param length");
|
2003-06-01 15:47:51 +02:00
|
|
|
break;
|
|
|
|
}
|
2022-03-07 16:57:56 +01:00
|
|
|
if (ap[i].flag & GCTL_PARAM_RD) {
|
|
|
|
p = geom_alloc_copyin(req, ap[i].value, ap[i].len);
|
|
|
|
if (p == NULL)
|
|
|
|
break;
|
|
|
|
if ((ap[i].flag & GCTL_PARAM_ASCII) &&
|
|
|
|
p[ap[i].len - 1] != '\0') {
|
|
|
|
gctl_error(req, "unterminated param value");
|
|
|
|
g_free(p);
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
p = g_malloc(ap[i].len, M_WAITOK | M_ZERO);
|
2003-06-01 15:47:51 +02:00
|
|
|
}
|
|
|
|
ap[i].kvalue = p;
|
|
|
|
ap[i].flag |= GCTL_PARAM_VALUEKERNEL;
|
2003-03-23 11:16:14 +01:00
|
|
|
}
|
2003-06-01 15:47:51 +02:00
|
|
|
req->arg = ap;
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
static void
|
|
|
|
gctl_copyout(struct gctl_req *req)
|
|
|
|
{
|
|
|
|
int error, i;
|
|
|
|
struct gctl_req_arg *ap;
|
|
|
|
|
|
|
|
if (req->nerror)
|
|
|
|
return;
|
|
|
|
error = 0;
|
|
|
|
ap = req->arg;
|
|
|
|
for (i = 0; i < req->narg; i++, ap++) {
|
|
|
|
if (!(ap->flag & GCTL_PARAM_CHANGED))
|
|
|
|
continue;
|
|
|
|
error = copyout(ap->kvalue, ap->value, ap->len);
|
|
|
|
if (!error)
|
|
|
|
continue;
|
|
|
|
req->nerror = error;
|
|
|
|
return;
|
2003-03-23 11:16:14 +01:00
|
|
|
}
|
2003-06-01 15:47:51 +02:00
|
|
|
return;
|
2003-03-23 11:16:14 +01:00
|
|
|
}
|
|
|
|
|
2003-05-02 14:49:41 +02:00
|
|
|
static void
|
|
|
|
gctl_free(struct gctl_req *req)
|
|
|
|
{
|
2016-04-27 17:10:40 +02:00
|
|
|
u_int i;
|
2003-05-02 14:49:41 +02:00
|
|
|
|
2014-01-23 22:30:31 +01:00
|
|
|
sbuf_delete(req->serror);
|
2003-06-01 15:47:51 +02:00
|
|
|
if (req->arg == NULL)
|
|
|
|
return;
|
2003-05-02 14:49:41 +02:00
|
|
|
for (i = 0; i < req->narg; i++) {
|
2003-06-01 15:47:51 +02:00
|
|
|
if (req->arg[i].flag & GCTL_PARAM_NAMEKERNEL)
|
2003-05-02 14:49:41 +02:00
|
|
|
g_free(req->arg[i].name);
|
2003-06-01 15:47:51 +02:00
|
|
|
if ((req->arg[i].flag & GCTL_PARAM_VALUEKERNEL) &&
|
|
|
|
req->arg[i].len > 0)
|
|
|
|
g_free(req->arg[i].kvalue);
|
2003-05-02 14:49:41 +02:00
|
|
|
}
|
|
|
|
g_free(req->arg);
|
|
|
|
}
|
|
|
|
|
2003-03-23 11:16:14 +01:00
|
|
|
static void
|
2022-03-07 20:40:28 +01:00
|
|
|
gctl_dump(struct gctl_req *req, const char *what)
|
2003-03-23 11:16:14 +01:00
|
|
|
{
|
2014-01-23 20:55:02 +01:00
|
|
|
struct gctl_req_arg *ap;
|
2003-03-23 11:16:14 +01:00
|
|
|
u_int i;
|
2003-06-01 15:47:51 +02:00
|
|
|
int j;
|
2003-03-23 11:16:14 +01:00
|
|
|
|
2022-03-07 20:40:28 +01:00
|
|
|
printf("Dump of gctl %s at %p:\n", what, req);
|
2003-06-01 15:47:51 +02:00
|
|
|
if (req->nerror > 0) {
|
|
|
|
printf(" nerror:\t%d\n", req->nerror);
|
|
|
|
if (sbuf_len(req->serror) > 0)
|
|
|
|
printf(" error:\t\"%s\"\n", sbuf_data(req->serror));
|
2003-03-23 11:16:14 +01:00
|
|
|
}
|
2014-01-23 20:55:02 +01:00
|
|
|
if (req->arg == NULL)
|
|
|
|
return;
|
2003-03-23 11:16:14 +01:00
|
|
|
for (i = 0; i < req->narg; i++) {
|
|
|
|
ap = &req->arg[i];
|
2003-06-01 15:47:51 +02:00
|
|
|
if (!(ap->flag & GCTL_PARAM_NAMEKERNEL))
|
|
|
|
printf(" param:\t%d@%p", ap->nlen, ap->name);
|
|
|
|
else
|
|
|
|
printf(" param:\t\"%s\"", ap->name);
|
2003-03-27 15:35:00 +01:00
|
|
|
printf(" [%s%s%d] = ",
|
|
|
|
ap->flag & GCTL_PARAM_RD ? "R" : "",
|
|
|
|
ap->flag & GCTL_PARAM_WR ? "W" : "",
|
|
|
|
ap->len);
|
2003-06-01 15:47:51 +02:00
|
|
|
if (!(ap->flag & GCTL_PARAM_VALUEKERNEL)) {
|
|
|
|
printf(" =@ %p", ap->value);
|
|
|
|
} else if (ap->flag & GCTL_PARAM_ASCII) {
|
|
|
|
printf("\"%s\"", (char *)ap->kvalue);
|
2003-03-23 11:16:14 +01:00
|
|
|
} else if (ap->len > 0) {
|
2003-07-02 10:07:07 +02:00
|
|
|
for (j = 0; j < ap->len && j < 512; j++)
|
2003-06-01 15:47:51 +02:00
|
|
|
printf(" %02x", ((u_char *)ap->kvalue)[j]);
|
2003-03-23 11:16:14 +01:00
|
|
|
} else {
|
2003-06-01 15:47:51 +02:00
|
|
|
printf(" = %p", ap->kvalue);
|
2003-03-23 11:16:14 +01:00
|
|
|
}
|
|
|
|
printf("\n");
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2006-04-07 18:19:48 +02:00
|
|
|
int
|
|
|
|
gctl_set_param(struct gctl_req *req, const char *param, void const *ptr,
|
|
|
|
int len)
|
2003-05-04 21:24:34 +02:00
|
|
|
{
|
2016-04-27 17:10:40 +02:00
|
|
|
u_int i;
|
2003-05-04 21:24:34 +02:00
|
|
|
struct gctl_req_arg *ap;
|
|
|
|
|
|
|
|
for (i = 0; i < req->narg; i++) {
|
|
|
|
ap = &req->arg[i];
|
|
|
|
if (strcmp(param, ap->name))
|
|
|
|
continue;
|
2006-04-07 18:19:48 +02:00
|
|
|
if (!(ap->flag & GCTL_PARAM_WR))
|
|
|
|
return (EPERM);
|
|
|
|
ap->flag |= GCTL_PARAM_CHANGED;
|
2003-06-01 15:47:51 +02:00
|
|
|
if (ap->len < len) {
|
2006-04-07 18:19:48 +02:00
|
|
|
bcopy(ptr, ap->kvalue, ap->len);
|
|
|
|
return (ENOSPC);
|
2003-05-04 21:24:34 +02:00
|
|
|
}
|
2003-06-01 15:47:51 +02:00
|
|
|
bcopy(ptr, ap->kvalue, len);
|
2006-04-07 18:19:48 +02:00
|
|
|
return (0);
|
|
|
|
}
|
|
|
|
return (EINVAL);
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
gctl_set_param_err(struct gctl_req *req, const char *param, void const *ptr,
|
|
|
|
int len)
|
|
|
|
{
|
|
|
|
|
|
|
|
switch (gctl_set_param(req, param, ptr, len)) {
|
|
|
|
case EPERM:
|
|
|
|
gctl_error(req, "No write access %s argument", param);
|
|
|
|
break;
|
|
|
|
case ENOSPC:
|
|
|
|
gctl_error(req, "Wrong length %s argument", param);
|
|
|
|
break;
|
|
|
|
case EINVAL:
|
|
|
|
gctl_error(req, "Missing %s argument", param);
|
|
|
|
break;
|
2003-05-04 21:24:34 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2003-03-27 15:35:00 +01:00
|
|
|
void *
|
GEOM: Introduce partial confxml API
Traditionally the GEOM's primary channel of information from kernel to
user-space was confxml, fetched by libgeom through kern.geom.confxml
sysctl. It is convenient and informative, representing full state of
GEOM in a single XML document. But problems start to arise on systems
with hundreds of disks, where the full confxml size reaches many
megabytes, taking significant time to first write it and then parse.
This patch introduces alternative solution, allowing to fetch much
smaller XML document, subset of the full confxml, limited to 64KB and
representing only one specified geom and optionally its parents. It
uses existing GEOM control interface, extended with new "getxml" verb.
In case of any error, such as the buffer overflow, it just transparently
falls back to traditional full confxml. This patch uses the new API in
user-space GEOM tools where it is possible.
Reviewed by: imp
MFC after: 2 month
Sponsored by: iXsystems, Inc.
Differential Revision: https://reviews.freebsd.org/D34529
2022-03-12 17:49:37 +01:00
|
|
|
gctl_get_param_flags(struct gctl_req *req, const char *param, int flags, int *len)
|
2003-03-27 15:35:00 +01:00
|
|
|
{
|
2016-04-27 17:10:40 +02:00
|
|
|
u_int i;
|
2003-03-27 15:35:00 +01:00
|
|
|
void *p;
|
|
|
|
struct gctl_req_arg *ap;
|
|
|
|
|
|
|
|
for (i = 0; i < req->narg; i++) {
|
|
|
|
ap = &req->arg[i];
|
|
|
|
if (strcmp(param, ap->name))
|
|
|
|
continue;
|
GEOM: Introduce partial confxml API
Traditionally the GEOM's primary channel of information from kernel to
user-space was confxml, fetched by libgeom through kern.geom.confxml
sysctl. It is convenient and informative, representing full state of
GEOM in a single XML document. But problems start to arise on systems
with hundreds of disks, where the full confxml size reaches many
megabytes, taking significant time to first write it and then parse.
This patch introduces alternative solution, allowing to fetch much
smaller XML document, subset of the full confxml, limited to 64KB and
representing only one specified geom and optionally its parents. It
uses existing GEOM control interface, extended with new "getxml" verb.
In case of any error, such as the buffer overflow, it just transparently
falls back to traditional full confxml. This patch uses the new API in
user-space GEOM tools where it is possible.
Reviewed by: imp
MFC after: 2 month
Sponsored by: iXsystems, Inc.
Differential Revision: https://reviews.freebsd.org/D34529
2022-03-12 17:49:37 +01:00
|
|
|
if ((ap->flag & flags) != flags)
|
2003-03-27 15:35:00 +01:00
|
|
|
continue;
|
2003-06-01 15:47:51 +02:00
|
|
|
p = ap->kvalue;
|
2003-03-27 15:35:00 +01:00
|
|
|
if (len != NULL)
|
2003-06-01 15:47:51 +02:00
|
|
|
*len = ap->len;
|
|
|
|
return (p);
|
|
|
|
}
|
|
|
|
return (NULL);
|
|
|
|
}
|
|
|
|
|
GEOM: Introduce partial confxml API
Traditionally the GEOM's primary channel of information from kernel to
user-space was confxml, fetched by libgeom through kern.geom.confxml
sysctl. It is convenient and informative, representing full state of
GEOM in a single XML document. But problems start to arise on systems
with hundreds of disks, where the full confxml size reaches many
megabytes, taking significant time to first write it and then parse.
This patch introduces alternative solution, allowing to fetch much
smaller XML document, subset of the full confxml, limited to 64KB and
representing only one specified geom and optionally its parents. It
uses existing GEOM control interface, extended with new "getxml" verb.
In case of any error, such as the buffer overflow, it just transparently
falls back to traditional full confxml. This patch uses the new API in
user-space GEOM tools where it is possible.
Reviewed by: imp
MFC after: 2 month
Sponsored by: iXsystems, Inc.
Differential Revision: https://reviews.freebsd.org/D34529
2022-03-12 17:49:37 +01:00
|
|
|
void *
|
|
|
|
gctl_get_param(struct gctl_req *req, const char *param, int *len)
|
|
|
|
{
|
|
|
|
|
|
|
|
return (gctl_get_param_flags(req, param, GCTL_PARAM_RD, len));
|
|
|
|
}
|
|
|
|
|
2003-06-01 15:47:51 +02:00
|
|
|
char const *
|
|
|
|
gctl_get_asciiparam(struct gctl_req *req, const char *param)
|
|
|
|
{
|
|
|
|
char const *p;
|
GEOM: Introduce partial confxml API
Traditionally the GEOM's primary channel of information from kernel to
user-space was confxml, fetched by libgeom through kern.geom.confxml
sysctl. It is convenient and informative, representing full state of
GEOM in a single XML document. But problems start to arise on systems
with hundreds of disks, where the full confxml size reaches many
megabytes, taking significant time to first write it and then parse.
This patch introduces alternative solution, allowing to fetch much
smaller XML document, subset of the full confxml, limited to 64KB and
representing only one specified geom and optionally its parents. It
uses existing GEOM control interface, extended with new "getxml" verb.
In case of any error, such as the buffer overflow, it just transparently
falls back to traditional full confxml. This patch uses the new API in
user-space GEOM tools where it is possible.
Reviewed by: imp
MFC after: 2 month
Sponsored by: iXsystems, Inc.
Differential Revision: https://reviews.freebsd.org/D34529
2022-03-12 17:49:37 +01:00
|
|
|
int len;
|
2003-06-01 15:47:51 +02:00
|
|
|
|
GEOM: Introduce partial confxml API
Traditionally the GEOM's primary channel of information from kernel to
user-space was confxml, fetched by libgeom through kern.geom.confxml
sysctl. It is convenient and informative, representing full state of
GEOM in a single XML document. But problems start to arise on systems
with hundreds of disks, where the full confxml size reaches many
megabytes, taking significant time to first write it and then parse.
This patch introduces alternative solution, allowing to fetch much
smaller XML document, subset of the full confxml, limited to 64KB and
representing only one specified geom and optionally its parents. It
uses existing GEOM control interface, extended with new "getxml" verb.
In case of any error, such as the buffer overflow, it just transparently
falls back to traditional full confxml. This patch uses the new API in
user-space GEOM tools where it is possible.
Reviewed by: imp
MFC after: 2 month
Sponsored by: iXsystems, Inc.
Differential Revision: https://reviews.freebsd.org/D34529
2022-03-12 17:49:37 +01:00
|
|
|
p = gctl_get_param_flags(req, param, GCTL_PARAM_RD, &len);
|
|
|
|
if (p == NULL)
|
|
|
|
return (NULL);
|
|
|
|
if (len < 1) {
|
|
|
|
gctl_error(req, "Argument without length (%s)", param);
|
|
|
|
return (NULL);
|
2003-03-27 15:35:00 +01:00
|
|
|
}
|
GEOM: Introduce partial confxml API
Traditionally the GEOM's primary channel of information from kernel to
user-space was confxml, fetched by libgeom through kern.geom.confxml
sysctl. It is convenient and informative, representing full state of
GEOM in a single XML document. But problems start to arise on systems
with hundreds of disks, where the full confxml size reaches many
megabytes, taking significant time to first write it and then parse.
This patch introduces alternative solution, allowing to fetch much
smaller XML document, subset of the full confxml, limited to 64KB and
representing only one specified geom and optionally its parents. It
uses existing GEOM control interface, extended with new "getxml" verb.
In case of any error, such as the buffer overflow, it just transparently
falls back to traditional full confxml. This patch uses the new API in
user-space GEOM tools where it is possible.
Reviewed by: imp
MFC after: 2 month
Sponsored by: iXsystems, Inc.
Differential Revision: https://reviews.freebsd.org/D34529
2022-03-12 17:49:37 +01:00
|
|
|
if (p[len - 1] != '\0') {
|
|
|
|
gctl_error(req, "Unterminated argument (%s)", param);
|
|
|
|
return (NULL);
|
|
|
|
}
|
|
|
|
return (p);
|
2003-03-27 15:35:00 +01:00
|
|
|
}
|
|
|
|
|
2003-04-23 10:03:47 +02:00
|
|
|
void *
|
2019-10-16 23:49:39 +02:00
|
|
|
gctl_get_paraml_opt(struct gctl_req *req, const char *param, int len)
|
2003-04-23 10:03:47 +02:00
|
|
|
{
|
|
|
|
int i;
|
|
|
|
void *p;
|
|
|
|
|
|
|
|
p = gctl_get_param(req, param, &i);
|
2019-10-16 23:49:39 +02:00
|
|
|
if (i != len) {
|
2003-04-23 10:03:47 +02:00
|
|
|
p = NULL;
|
|
|
|
gctl_error(req, "Wrong length %s argument", param);
|
|
|
|
}
|
|
|
|
return (p);
|
|
|
|
}
|
|
|
|
|
2019-10-16 23:49:39 +02:00
|
|
|
void *
|
|
|
|
gctl_get_paraml(struct gctl_req *req, const char *param, int len)
|
|
|
|
{
|
|
|
|
void *p;
|
|
|
|
|
|
|
|
p = gctl_get_paraml_opt(req, param, len);
|
|
|
|
if (p == NULL)
|
|
|
|
gctl_error(req, "Missing %s argument", param);
|
|
|
|
return (p);
|
|
|
|
}
|
|
|
|
|
2003-06-01 15:47:51 +02:00
|
|
|
struct g_class *
|
|
|
|
gctl_get_class(struct gctl_req *req, char const *arg)
|
2003-03-27 15:35:00 +01:00
|
|
|
{
|
2003-06-01 15:47:51 +02:00
|
|
|
char const *p;
|
2003-03-27 15:35:00 +01:00
|
|
|
struct g_class *cp;
|
|
|
|
|
2003-06-01 15:47:51 +02:00
|
|
|
p = gctl_get_asciiparam(req, arg);
|
2020-07-22 04:14:27 +02:00
|
|
|
if (p == NULL) {
|
|
|
|
gctl_error(req, "Missing %s argument", arg);
|
2003-03-27 15:35:00 +01:00
|
|
|
return (NULL);
|
2020-07-22 04:14:27 +02:00
|
|
|
}
|
2003-03-27 15:35:00 +01:00
|
|
|
LIST_FOREACH(cp, &g_classes, class) {
|
2003-06-01 15:47:51 +02:00
|
|
|
if (!strcmp(p, cp->name))
|
2003-03-27 15:35:00 +01:00
|
|
|
return (cp);
|
|
|
|
}
|
2020-07-22 04:14:27 +02:00
|
|
|
gctl_error(req, "Class not found: \"%s\"", p);
|
2003-03-27 15:35:00 +01:00
|
|
|
return (NULL);
|
|
|
|
}
|
|
|
|
|
2003-06-01 15:47:51 +02:00
|
|
|
struct g_geom *
|
2020-07-27 00:30:55 +02:00
|
|
|
gctl_get_geom(struct gctl_req *req, struct g_class *mp, char const *arg)
|
2003-03-27 15:35:00 +01:00
|
|
|
{
|
2003-06-01 15:47:51 +02:00
|
|
|
char const *p;
|
2003-03-27 15:35:00 +01:00
|
|
|
struct g_geom *gp;
|
|
|
|
|
2020-07-27 00:30:55 +02:00
|
|
|
MPASS(mp != NULL);
|
2003-06-01 15:47:51 +02:00
|
|
|
p = gctl_get_asciiparam(req, arg);
|
2020-07-22 04:14:27 +02:00
|
|
|
if (p == NULL) {
|
|
|
|
gctl_error(req, "Missing %s argument", arg);
|
2007-03-30 18:32:08 +02:00
|
|
|
return (NULL);
|
2020-07-22 04:14:27 +02:00
|
|
|
}
|
2020-07-27 00:30:55 +02:00
|
|
|
LIST_FOREACH(gp, &mp->geom, geom)
|
|
|
|
if (!strcmp(p, gp->name))
|
|
|
|
return (gp);
|
2007-03-30 18:32:08 +02:00
|
|
|
gctl_error(req, "Geom not found: \"%s\"", p);
|
2003-03-27 15:35:00 +01:00
|
|
|
return (NULL);
|
|
|
|
}
|
|
|
|
|
2003-06-01 15:47:51 +02:00
|
|
|
struct g_provider *
|
|
|
|
gctl_get_provider(struct gctl_req *req, char const *arg)
|
2003-03-27 15:35:00 +01:00
|
|
|
{
|
2003-06-01 15:47:51 +02:00
|
|
|
char const *p;
|
2003-03-27 15:35:00 +01:00
|
|
|
struct g_provider *pp;
|
|
|
|
|
2003-06-01 15:47:51 +02:00
|
|
|
p = gctl_get_asciiparam(req, arg);
|
2020-07-22 04:14:27 +02:00
|
|
|
if (p == NULL) {
|
|
|
|
gctl_error(req, "Missing '%s' argument", arg);
|
2003-03-27 15:35:00 +01:00
|
|
|
return (NULL);
|
2020-07-22 04:14:27 +02:00
|
|
|
}
|
2003-06-04 20:17:52 +02:00
|
|
|
pp = g_provider_by_name(p);
|
|
|
|
if (pp != NULL)
|
|
|
|
return (pp);
|
2007-03-30 18:32:08 +02:00
|
|
|
gctl_error(req, "Provider not found: \"%s\"", p);
|
2003-03-27 15:35:00 +01:00
|
|
|
return (NULL);
|
|
|
|
}
|
|
|
|
|
GEOM: Introduce partial confxml API
Traditionally the GEOM's primary channel of information from kernel to
user-space was confxml, fetched by libgeom through kern.geom.confxml
sysctl. It is convenient and informative, representing full state of
GEOM in a single XML document. But problems start to arise on systems
with hundreds of disks, where the full confxml size reaches many
megabytes, taking significant time to first write it and then parse.
This patch introduces alternative solution, allowing to fetch much
smaller XML document, subset of the full confxml, limited to 64KB and
representing only one specified geom and optionally its parents. It
uses existing GEOM control interface, extended with new "getxml" verb.
In case of any error, such as the buffer overflow, it just transparently
falls back to traditional full confxml. This patch uses the new API in
user-space GEOM tools where it is possible.
Reviewed by: imp
MFC after: 2 month
Sponsored by: iXsystems, Inc.
Differential Revision: https://reviews.freebsd.org/D34529
2022-03-12 17:49:37 +01:00
|
|
|
static void
|
|
|
|
g_ctl_getxml(struct gctl_req *req, struct g_class *mp)
|
|
|
|
{
|
|
|
|
const char *name;
|
|
|
|
char *buf;
|
|
|
|
struct sbuf *sb;
|
|
|
|
int len, i = 0, n = 0, *parents;
|
|
|
|
struct g_geom *gp, **gps;
|
|
|
|
struct g_consumer *cp;
|
|
|
|
|
|
|
|
parents = gctl_get_paraml(req, "parents", sizeof(*parents));
|
|
|
|
if (parents == NULL)
|
|
|
|
return;
|
|
|
|
name = gctl_get_asciiparam(req, "arg0");
|
|
|
|
n = 0;
|
|
|
|
LIST_FOREACH(gp, &mp->geom, geom) {
|
|
|
|
if (name && strcmp(gp->name, name) != 0)
|
|
|
|
continue;
|
|
|
|
n++;
|
|
|
|
if (*parents) {
|
|
|
|
LIST_FOREACH(cp, &gp->consumer, consumer)
|
|
|
|
n++;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
gps = g_malloc((n + 1) * sizeof(*gps), M_WAITOK);
|
|
|
|
i = 0;
|
|
|
|
LIST_FOREACH(gp, &mp->geom, geom) {
|
|
|
|
if (name && strcmp(gp->name, name) != 0)
|
|
|
|
continue;
|
|
|
|
gps[i++] = gp;
|
|
|
|
if (*parents) {
|
|
|
|
LIST_FOREACH(cp, &gp->consumer, consumer) {
|
|
|
|
if (cp->provider != NULL)
|
|
|
|
gps[i++] = cp->provider->geom;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
KASSERT(i == n, ("different number of geoms found (%d != %d)",
|
|
|
|
i, n));
|
|
|
|
gps[i] = 0;
|
|
|
|
|
|
|
|
buf = gctl_get_param_flags(req, "output", GCTL_PARAM_WR, &len);
|
|
|
|
if (buf == NULL) {
|
|
|
|
gctl_error(req, "output parameter missing");
|
|
|
|
g_free(gps);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
sb = sbuf_new(NULL, buf, len, SBUF_FIXEDLEN | SBUF_INCLUDENUL);
|
|
|
|
g_conf_specific(sb, gps);
|
|
|
|
gctl_set_param(req, "output", buf, 0);
|
|
|
|
if (sbuf_error(sb))
|
|
|
|
gctl_error(req, "output buffer overflow");
|
|
|
|
sbuf_delete(sb);
|
|
|
|
g_free(gps);
|
|
|
|
}
|
|
|
|
|
2003-06-01 15:47:51 +02:00
|
|
|
static void
|
|
|
|
g_ctl_req(void *arg, int flag __unused)
|
2003-03-27 15:35:00 +01:00
|
|
|
{
|
|
|
|
struct g_class *mp;
|
2003-06-01 15:47:51 +02:00
|
|
|
struct gctl_req *req;
|
|
|
|
char const *verb;
|
2003-03-27 15:35:00 +01:00
|
|
|
|
|
|
|
g_topology_assert();
|
2003-06-01 15:47:51 +02:00
|
|
|
req = arg;
|
|
|
|
mp = gctl_get_class(req, "class");
|
2020-07-22 04:14:27 +02:00
|
|
|
if (mp == NULL)
|
2003-06-01 15:47:51 +02:00
|
|
|
return;
|
2005-09-19 01:54:40 +02:00
|
|
|
verb = gctl_get_param(req, "verb", NULL);
|
|
|
|
if (verb == NULL) {
|
|
|
|
gctl_error(req, "Verb missing");
|
|
|
|
return;
|
|
|
|
}
|
GEOM: Introduce partial confxml API
Traditionally the GEOM's primary channel of information from kernel to
user-space was confxml, fetched by libgeom through kern.geom.confxml
sysctl. It is convenient and informative, representing full state of
GEOM in a single XML document. But problems start to arise on systems
with hundreds of disks, where the full confxml size reaches many
megabytes, taking significant time to first write it and then parse.
This patch introduces alternative solution, allowing to fetch much
smaller XML document, subset of the full confxml, limited to 64KB and
representing only one specified geom and optionally its parents. It
uses existing GEOM control interface, extended with new "getxml" verb.
In case of any error, such as the buffer overflow, it just transparently
falls back to traditional full confxml. This patch uses the new API in
user-space GEOM tools where it is possible.
Reviewed by: imp
MFC after: 2 month
Sponsored by: iXsystems, Inc.
Differential Revision: https://reviews.freebsd.org/D34529
2022-03-12 17:49:37 +01:00
|
|
|
if (strcmp(verb, "getxml") == 0) {
|
|
|
|
g_ctl_getxml(req, mp);
|
|
|
|
} else if (mp->ctlreq == NULL) {
|
|
|
|
gctl_error(req, "Class takes no requests");
|
|
|
|
} else {
|
|
|
|
mp->ctlreq(req, mp, verb);
|
|
|
|
}
|
2003-03-27 15:35:00 +01:00
|
|
|
g_topology_assert();
|
|
|
|
}
|
|
|
|
|
2003-03-23 11:16:14 +01:00
|
|
|
static int
|
2004-06-16 11:47:26 +02:00
|
|
|
g_ctl_ioctl_ctl(struct cdev *dev, u_long cmd, caddr_t data, int fflag, struct thread *td)
|
2003-03-23 11:16:14 +01:00
|
|
|
{
|
2003-03-27 15:35:00 +01:00
|
|
|
struct gctl_req *req;
|
2004-10-23 22:52:15 +02:00
|
|
|
int nerror;
|
2003-03-23 11:16:14 +01:00
|
|
|
|
|
|
|
req = (void *)data;
|
2003-06-01 15:47:51 +02:00
|
|
|
req->nerror = 0;
|
2003-03-27 15:35:00 +01:00
|
|
|
/* It is an error if we cannot return an error text */
|
2003-06-01 15:47:51 +02:00
|
|
|
if (req->lerror < 2)
|
2003-03-23 11:16:14 +01:00
|
|
|
return (EINVAL);
|
2003-03-27 15:35:00 +01:00
|
|
|
if (!useracc(req->error, req->lerror, VM_PROT_WRITE))
|
|
|
|
return (EINVAL);
|
|
|
|
|
2014-01-23 21:25:38 +01:00
|
|
|
req->serror = sbuf_new_auto();
|
2003-03-27 15:35:00 +01:00
|
|
|
/* Check the version */
|
2014-01-23 21:25:38 +01:00
|
|
|
if (req->version != GCTL_VERSION) {
|
|
|
|
gctl_error(req, "kernel and libgeom version mismatch.");
|
|
|
|
req->arg = NULL;
|
|
|
|
} else {
|
|
|
|
/* Get things on board */
|
|
|
|
gctl_copyin(req);
|
2003-03-27 15:35:00 +01:00
|
|
|
|
2014-01-23 21:25:38 +01:00
|
|
|
if (g_debugflags & G_F_CTLDUMP)
|
2022-03-07 20:40:28 +01:00
|
|
|
gctl_dump(req, "request");
|
2003-06-01 15:47:51 +02:00
|
|
|
|
2014-01-23 21:25:38 +01:00
|
|
|
if (!req->nerror) {
|
|
|
|
g_waitfor_event(g_ctl_req, req, M_WAITOK, NULL);
|
2022-03-07 20:40:28 +01:00
|
|
|
|
|
|
|
if (g_debugflags & G_F_CTLDUMP)
|
|
|
|
gctl_dump(req, "result");
|
|
|
|
|
2014-01-23 21:25:38 +01:00
|
|
|
gctl_copyout(req);
|
|
|
|
}
|
2003-03-27 15:35:00 +01:00
|
|
|
}
|
2005-04-08 11:28:08 +02:00
|
|
|
if (sbuf_done(req->serror)) {
|
2014-01-23 21:25:38 +01:00
|
|
|
copyout(sbuf_data(req->serror), req->error,
|
2005-04-08 11:28:08 +02:00
|
|
|
imin(req->lerror, sbuf_len(req->serror) + 1));
|
|
|
|
}
|
2003-06-01 15:47:51 +02:00
|
|
|
|
2004-10-23 22:52:15 +02:00
|
|
|
nerror = req->nerror;
|
2003-05-02 14:49:41 +02:00
|
|
|
gctl_free(req);
|
2004-10-23 22:52:15 +02:00
|
|
|
return (nerror);
|
2003-03-23 11:16:14 +01:00
|
|
|
}
|
|
|
|
|
2002-10-13 22:33:33 +02:00
|
|
|
static int
|
2004-06-16 11:47:26 +02:00
|
|
|
g_ctl_ioctl(struct cdev *dev, u_long cmd, caddr_t data, int fflag, struct thread *td)
|
2002-10-13 22:33:33 +02:00
|
|
|
{
|
|
|
|
int error;
|
|
|
|
|
|
|
|
switch(cmd) {
|
2003-03-23 11:16:14 +01:00
|
|
|
case GEOM_CTL:
|
|
|
|
error = g_ctl_ioctl_ctl(dev, cmd, data, fflag, td);
|
|
|
|
break;
|
2002-10-13 22:33:33 +02:00
|
|
|
default:
|
2003-06-01 15:47:51 +02:00
|
|
|
error = ENOIOCTL;
|
2002-10-13 22:33:33 +02:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
return (error);
|
|
|
|
|
|
|
|
}
|