1/*-
2 * SPDX-License-Identifier: BSD-2-Clause-FreeBSD
3 *
4 * Copyright (c) 2009 Fabio Checconi
5 * Copyright (c) 2010 Luigi Rizzo, Universita` di Pisa
6 * All rights reserved.
7 *
8 * Redistribution and use in source and binary forms, with or without
9 * modification, are permitted provided that the following conditions
10 * are met:
11 * 1. Redistributions of source code must retain the above copyright
12 *    notice, this list of conditions and the following disclaimer.
13 * 2. Redistributions in binary form must reproduce the above copyright
14 *    notice, this list of conditions and the following disclaimer in the
15 *    documentation and/or other materials provided with the distribution.
16 *
17 * THIS SOFTWARE IS PROVIDED BY THE AUTHORS AND CONTRIBUTORS ``AS IS'' AND
18 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
19 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
20 * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHORS OR CONTRIBUTORS BE LIABLE
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
30/*
31 * $Id$
32 * $FreeBSD: stable/11/sbin/geom/class/sched/geom_sched.c 330449 2018-03-05 07:26:05Z eadler $
33 *
34 * This file implements the userspace library used by the 'geom'
35 * command to load and manipulate disk schedulers.
36 */
37
38#include <sys/cdefs.h>
39#include <sys/param.h>
40#include <sys/linker.h>
41#include <sys/module.h>
42
43#include <stdio.h>
44#include <stdint.h>
45#include <libgeom.h>
46
47#include "core/geom.h"
48#include "misc/subr.h"
49
50#define	G_SCHED_VERSION	0
51
52uint32_t lib_version = G_LIB_VERSION;
53uint32_t version = G_SCHED_VERSION;
54
55/*
56 * storage for parameters used by this geom class.
57 * Right now only the scheduler name is used.
58 */
59#define	GSCHED_ALGO	"rr"	/* default scheduler */
60
61/*
62 * Adapt to differences in geom library.
63 * in V1 struct g_command misses gc_argname, eld, and G_BOOL is undefined
64 */
65#if G_LIB_VERSION <= 1
66#define G_TYPE_BOOL	G_TYPE_NUMBER
67#endif
68#if G_LIB_VERSION >= 3 && G_LIB_VERSION <= 4
69#define G_ARGNAME	NULL,
70#else
71#define	G_ARGNAME
72#endif
73
74static void
75gcmd_createinsert(struct gctl_req *req, unsigned flags __unused)
76{
77	const char *reqalgo;
78	char name[64];
79
80	if (gctl_has_param(req, "algo"))
81		reqalgo = gctl_get_ascii(req, "algo");
82	else
83		reqalgo = GSCHED_ALGO;
84
85	snprintf(name, sizeof(name), "gsched_%s", reqalgo);
86	/*
87	 * Do not complain about errors here, gctl_issue()
88	 * will fail anyway.
89	 */
90	if (modfind(name) < 0)
91		kldload(name);
92	gctl_issue(req);
93}
94
95struct g_command class_commands[] = {
96	{ "create", G_FLAG_VERBOSE | G_FLAG_LOADKLD, gcmd_createinsert,
97	    {
98		{ 'a', "algo", GSCHED_ALGO, G_TYPE_STRING },
99		G_OPT_SENTINEL
100	    },
101	    G_ARGNAME "[-v] [-a algorithm_name] dev ..."
102	},
103	{ "insert", G_FLAG_VERBOSE | G_FLAG_LOADKLD, gcmd_createinsert,
104	    {
105		{ 'a', "algo", GSCHED_ALGO, G_TYPE_STRING },
106		G_OPT_SENTINEL
107	    },
108	    G_ARGNAME "[-v] [-a algorithm_name] dev ..."
109	},
110	{ "configure", G_FLAG_VERBOSE, NULL,
111	    {
112		{ 'a', "algo", GSCHED_ALGO, G_TYPE_STRING },
113		G_OPT_SENTINEL
114	    },
115	    G_ARGNAME "[-v] [-a algorithm_name] prov ..."
116	},
117	{ "destroy", G_FLAG_VERBOSE, NULL,
118	    {
119		{ 'f', "force", NULL, G_TYPE_BOOL },
120		G_OPT_SENTINEL
121	    },
122	    G_ARGNAME "[-fv] prov ..."
123	},
124	{ "reset", G_FLAG_VERBOSE, NULL, G_NULL_OPTS,
125	    G_ARGNAME "[-v] prov ..."
126	},
127	G_CMD_SENTINEL
128};
129