2 * SPDX-License-Identifier: BSD-2-Clause-FreeBSD
4 * Copyright (c) 2004-2005 Pawel Jakub Dawidek <pjd@FreeBSD.org>
7 * Redistribution and use in source and binary forms, with or without
8 * modification, are permitted provided that the following conditions
10 * 1. Redistributions of source code must retain the above copyright
11 * notice, this list of conditions and the following disclaimer.
12 * 2. Redistributions in binary form must reproduce the above copyright
13 * notice, this list of conditions and the following disclaimer in the
14 * documentation and/or other materials provided with the distribution.
16 * THIS SOFTWARE IS PROVIDED BY THE AUTHORS AND CONTRIBUTORS ``AS IS'' AND
17 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
18 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
19 * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHORS OR CONTRIBUTORS BE LIABLE
20 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
21 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
22 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
23 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
24 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
25 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
29 #include <sys/cdefs.h>
30 __FBSDID("$FreeBSD$");
34 #include <sys/param.h>
35 #include <sys/systm.h>
36 #include <sys/kernel.h>
37 #include <sys/module.h>
39 #include <sys/mutex.h>
41 #include <sys/ctype.h>
42 #include <sys/malloc.h>
43 #include <sys/libkern.h>
45 #include <sys/stddef.h>
46 #include <sys/sysctl.h>
47 #include <geom/geom.h>
48 #include <geom/geom_dbg.h>
49 #include <geom/geom_slice.h>
50 #include <geom/label/g_label.h>
52 FEATURE(geom_label, "GEOM labeling support");
54 SYSCTL_DECL(_kern_geom);
55 SYSCTL_NODE(_kern_geom, OID_AUTO, label, CTLFLAG_RW | CTLFLAG_MPSAFE, 0,
57 u_int g_label_debug = 0;
58 SYSCTL_UINT(_kern_geom_label, OID_AUTO, debug, CTLFLAG_RWTUN, &g_label_debug, 0,
61 static int g_label_destroy_geom(struct gctl_req *req, struct g_class *mp,
63 static int g_label_destroy(struct g_geom *gp, boolean_t force);
64 static struct g_geom *g_label_taste(struct g_class *mp, struct g_provider *pp,
66 static void g_label_generic_taste(struct g_consumer *, char *, size_t);
67 static void g_label_config(struct gctl_req *req, struct g_class *mp,
70 #define G_LABEL_DIRPREFIX "label/"
72 struct g_class g_label_class = {
73 .name = G_LABEL_CLASS_NAME,
75 .ctlreq = g_label_config,
76 .taste = g_label_taste,
77 .destroy_geom = g_label_destroy_geom
80 static struct g_label_desc g_label_generic = {
81 .ld_taste = g_label_generic_taste,
82 .ld_dirprefix = G_LABEL_DIRPREFIX,
87 * To add a new file system where you want to look for volume labels,
89 * 1. Add a file g_label_<file system>.c which implements labels recognition.
90 * 2. Add an 'extern const struct g_label_desc g_label_<file system>;' into
92 * 3. Add an element to the table below '&g_label_<file system>,'.
93 * 4. Add your file to sys/conf/files.
94 * 5. Add your file to sys/modules/geom/geom_label/Makefile.
95 * 6. Add your file system to manual page sbin/geom/class/label/glabel.8.
97 const struct g_label_desc *g_labels[] = {
116 g_label_rtrim(char *label, size_t size)
120 for (i = size - 1; i >= 0; i--) {
121 if (label[i] == '\0')
123 else if (label[i] == ' ')
131 g_label_destroy_geom(struct gctl_req *req __unused, struct g_class *mp,
132 struct g_geom *gp __unused)
136 * XXX: Unloading a class which is using geom_slice:1.56 is currently
137 * XXX: broken, so we deny unloading when we have geoms.
143 g_label_orphan(struct g_consumer *cp)
146 G_LABEL_DEBUG(1, "Label %s removed.",
147 LIST_FIRST(&cp->geom->provider)->name);
152 g_label_spoiled(struct g_consumer *cp)
155 G_LABEL_DEBUG(1, "Label %s removed.",
156 LIST_FIRST(&cp->geom->provider)->name);
161 g_label_resize(struct g_consumer *cp)
164 G_LABEL_DEBUG(1, "Label %s resized.",
165 LIST_FIRST(&cp->geom->provider)->name);
167 g_slice_config(cp->geom, 0, G_SLICE_CONFIG_FORCE, (off_t)0,
168 cp->provider->mediasize, cp->provider->sectorsize, "notused");
172 g_label_is_name_ok(const char *label)
176 /* Check if the label starts from ../ */
177 if (strncmp(label, "../", 3) == 0)
179 /* Check if the label contains /../ */
180 if (strstr(label, "/../") != NULL)
182 /* Check if the label ends at ../ */
183 if ((s = strstr(label, "/..")) != NULL && s[3] == '\0')
189 g_label_mangle_name(char *label, size_t size)
195 /* Trim trailing whitespace. */
197 for (i = len; i > 0; i--) {
198 if (isspace(label[i - 1]))
207 sb = sbuf_new(NULL, NULL, size, SBUF_FIXEDLEN);
208 for (c = label; *c != '\0'; c++) {
209 /* Trim leading whitespace. */
210 if (isspace(*c) && sbuf_len(sb) == 0)
212 if (!isprint(*c) || isspace(*c) || *c =='"' || *c == '%')
213 sbuf_printf(sb, "%%%02X", *c);
217 if (sbuf_finish(sb) != 0)
220 strlcpy(label, sbuf_data(sb), size);
224 static struct g_geom *
225 g_label_create(struct gctl_req *req, struct g_class *mp, struct g_provider *pp,
226 const char *label, const char *dirprefix, off_t mediasize)
229 struct g_provider *pp2;
230 struct g_consumer *cp;
235 if (!g_label_is_name_ok(label)) {
236 G_LABEL_DEBUG(0, "%s contains suspicious label, skipping.",
238 G_LABEL_DEBUG(1, "%s suspicious label is: %s", pp->name, label);
240 gctl_error(req, "Label name %s is invalid.", label);
245 if (snprintf(name, sizeof(name), "%s%s", dirprefix, label) >= sizeof(name)) {
247 gctl_error(req, "Label name %s is too long.", label);
250 LIST_FOREACH(gp, &mp->geom, geom) {
251 pp2 = LIST_FIRST(&gp->provider);
254 if ((pp2->flags & G_PF_ORPHAN) != 0)
256 if (strcmp(pp2->name, name) == 0) {
257 G_LABEL_DEBUG(1, "Label %s(%s) already exists (%s).",
258 label, name, pp->name);
260 gctl_error(req, "Provider %s already exists.",
266 gp = g_slice_new(mp, 1, pp, &cp, NULL, 0, NULL);
268 G_LABEL_DEBUG(0, "Cannot create slice %s.", label);
270 gctl_error(req, "Cannot create slice %s.", label);
273 gp->orphan = g_label_orphan;
274 gp->spoiled = g_label_spoiled;
275 gp->resize = g_label_resize;
276 g_access(cp, -1, 0, 0);
277 g_slice_config(gp, 0, G_SLICE_CONFIG_SET, (off_t)0, mediasize,
278 pp->sectorsize, "%s", name);
279 G_LABEL_DEBUG(1, "Label for provider %s is %s.", pp->name, name);
284 g_label_destroy(struct g_geom *gp, boolean_t force)
286 struct g_provider *pp;
289 pp = LIST_FIRST(&gp->provider);
290 if (pp != NULL && (pp->acr != 0 || pp->acw != 0 || pp->ace != 0)) {
292 G_LABEL_DEBUG(0, "Provider %s is still open, so it "
293 "can't be definitely removed.", pp->name);
296 "Provider %s is still open (r%dw%de%d).", pp->name,
297 pp->acr, pp->acw, pp->ace);
300 } else if (pp != NULL)
301 G_LABEL_DEBUG(1, "Label %s removed.", pp->name);
302 g_slice_spoiled(LIST_FIRST(&gp->consumer));
307 g_label_read_metadata(struct g_consumer *cp, struct g_label_metadata *md)
309 struct g_provider *pp;
314 buf = g_read_data(cp, pp->mediasize - pp->sectorsize, pp->sectorsize,
318 /* Decode metadata. */
319 label_metadata_decode(buf, md);
326 g_label_orphan_taste(struct g_consumer *cp __unused)
329 KASSERT(1 == 0, ("%s called?", __func__));
333 g_label_start_taste(struct bio *bp __unused)
336 KASSERT(1 == 0, ("%s called?", __func__));
340 g_label_access_taste(struct g_provider *pp __unused, int dr __unused,
341 int dw __unused, int de __unused)
344 KASSERT(1 == 0, ("%s called", __func__));
349 g_label_generic_taste(struct g_consumer *cp, char *label, size_t size)
351 struct g_provider *pp;
352 struct g_label_metadata md;
354 g_topology_assert_not();
358 if (g_label_read_metadata(cp, &md) != 0)
361 if (strcmp(md.md_magic, G_LABEL_MAGIC) != 0)
364 if (md.md_version > G_LABEL_VERSION) {
365 printf("geom_label.ko module is too old to handle %s.\n",
370 * Backward compatibility: there was no md_provsize field in
371 * earlier versions of metadata, so only check if we have it.
373 if (md.md_version >= 2 && md.md_provsize != pp->mediasize)
376 strlcpy(label, md.md_label, size);
379 static struct g_geom *
380 g_label_taste(struct g_class *mp, struct g_provider *pp, int flags __unused)
382 struct g_consumer *cp;
387 g_trace(G_T_TOPOLOGY, "%s(%s, %s)", __func__, mp->name, pp->name);
390 G_LABEL_DEBUG(2, "Tasting %s.", pp->name);
392 /* Skip providers that are already open for writing. */
396 if (strcmp(pp->geom->class->name, mp->name) == 0)
399 gp = g_new_geomf(mp, "label:taste");
400 gp->start = g_label_start_taste;
401 gp->access = g_label_access_taste;
402 gp->orphan = g_label_orphan_taste;
403 cp = g_new_consumer(gp);
405 if (g_access(cp, 1, 0, 0) != 0)
407 for (i = 0; g_labels[i] != NULL; i++) {
410 if (g_labels[i]->ld_enabled == 0)
413 g_labels[i]->ld_taste(cp, label, sizeof(label));
414 g_label_mangle_name(label, sizeof(label));
416 if (label[0] == '\0')
418 if (g_labels[i] != &g_label_generic) {
419 mediasize = pp->mediasize;
421 mediasize = pp->mediasize - pp->sectorsize;
423 g_label_create(NULL, mp, pp, label,
424 g_labels[i]->ld_dirprefix, mediasize);
426 g_access(cp, -1, 0, 0);
429 g_destroy_consumer(cp);
435 g_label_ctl_create(struct gctl_req *req, struct g_class *mp)
437 struct g_provider *pp;
443 nargs = gctl_get_paraml(req, "nargs", sizeof(*nargs));
445 gctl_error(req, "No '%s' argument", "nargs");
449 gctl_error(req, "Invalid number of arguments.");
453 * arg1 is the name of provider.
455 pp = gctl_get_provider(req, "arg1");
461 name = gctl_get_asciiparam(req, "arg0");
463 gctl_error(req, "No 'arg%d' argument", 0);
466 g_label_create(req, mp, pp, name, G_LABEL_DIRPREFIX, pp->mediasize);
470 g_label_skip_dir(const char *name)
474 if (strncmp(name, _PATH_DEV, strlen(_PATH_DEV)) == 0)
475 name += strlen(_PATH_DEV);
476 for (i = 0; g_labels[i] != NULL; i++) {
477 if (strncmp(name, g_labels[i]->ld_dirprefix,
478 strlen(g_labels[i]->ld_dirprefix)) == 0) {
479 name += strlen(g_labels[i]->ld_dirprefix);
486 static struct g_geom *
487 g_label_find_geom(struct g_class *mp, const char *name)
490 struct g_provider *pp;
493 name = g_label_skip_dir(name);
494 LIST_FOREACH(gp, &mp->geom, geom) {
495 pp = LIST_FIRST(&gp->provider);
496 pname = g_label_skip_dir(pp->name);
497 if (strcmp(pname, name) == 0)
504 g_label_ctl_destroy(struct gctl_req *req, struct g_class *mp)
506 int *nargs, *force, error, i;
513 nargs = gctl_get_paraml(req, "nargs", sizeof(*nargs));
515 gctl_error(req, "No '%s' argument", "nargs");
519 gctl_error(req, "Missing device(s).");
522 force = gctl_get_paraml(req, "force", sizeof(*force));
524 gctl_error(req, "No 'force' argument");
528 for (i = 0; i < *nargs; i++) {
529 snprintf(param, sizeof(param), "arg%d", i);
530 name = gctl_get_asciiparam(req, param);
532 gctl_error(req, "No 'arg%d' argument", i);
535 gp = g_label_find_geom(mp, name);
537 G_LABEL_DEBUG(1, "Label %s is invalid.", name);
538 gctl_error(req, "Label %s is invalid.", name);
541 error = g_label_destroy(gp, *force);
543 gctl_error(req, "Cannot destroy label %s (error=%d).",
544 LIST_FIRST(&gp->provider)->name, error);
551 g_label_config(struct gctl_req *req, struct g_class *mp, const char *verb)
557 version = gctl_get_paraml(req, "version", sizeof(*version));
558 if (version == NULL) {
559 gctl_error(req, "No '%s' argument.", "version");
562 if (*version != G_LABEL_VERSION) {
563 gctl_error(req, "Userland and kernel parts are out of sync.");
567 if (strcmp(verb, "create") == 0) {
568 g_label_ctl_create(req, mp);
570 } else if (strcmp(verb, "destroy") == 0 ||
571 strcmp(verb, "stop") == 0) {
572 g_label_ctl_destroy(req, mp);
576 gctl_error(req, "Unknown verb.");
579 DECLARE_GEOM_CLASS(g_label_class, g_label);
580 MODULE_VERSION(geom_label, 0);