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_slice.h>
49 #include <geom/label/g_label.h>
51 FEATURE(geom_label, "GEOM labeling support");
53 SYSCTL_DECL(_kern_geom);
54 SYSCTL_NODE(_kern_geom, OID_AUTO, label, CTLFLAG_RW, 0, "GEOM_LABEL stuff");
55 u_int g_label_debug = 0;
56 SYSCTL_UINT(_kern_geom_label, OID_AUTO, debug, CTLFLAG_RWTUN, &g_label_debug, 0,
59 static int g_label_destroy_geom(struct gctl_req *req, struct g_class *mp,
61 static int g_label_destroy(struct g_geom *gp, boolean_t force);
62 static struct g_geom *g_label_taste(struct g_class *mp, struct g_provider *pp,
64 static void g_label_config(struct gctl_req *req, struct g_class *mp,
67 struct g_class g_label_class = {
68 .name = G_LABEL_CLASS_NAME,
70 .ctlreq = g_label_config,
71 .taste = g_label_taste,
72 .destroy_geom = g_label_destroy_geom
76 * To add a new file system where you want to look for volume labels,
78 * 1. Add a file g_label_<file system>.c which implements labels recognition.
79 * 2. Add an 'extern const struct g_label_desc g_label_<file system>;' into
81 * 3. Add an element to the table below '&g_label_<file system>,'.
82 * 4. Add your file to sys/conf/files.
83 * 5. Add your file to sys/modules/geom/geom_label/Makefile.
84 * 6. Add your file system to manual page sbin/geom/class/label/glabel.8.
86 const struct g_label_desc *g_labels[] = {
104 g_label_rtrim(char *label, size_t size)
108 for (i = size - 1; i >= 0; i--) {
109 if (label[i] == '\0')
111 else if (label[i] == ' ')
119 g_label_destroy_geom(struct gctl_req *req __unused, struct g_class *mp,
120 struct g_geom *gp __unused)
124 * XXX: Unloading a class which is using geom_slice:1.56 is currently
125 * XXX: broken, so we deny unloading when we have geoms.
131 g_label_orphan(struct g_consumer *cp)
134 G_LABEL_DEBUG(1, "Label %s removed.",
135 LIST_FIRST(&cp->geom->provider)->name);
140 g_label_spoiled(struct g_consumer *cp)
143 G_LABEL_DEBUG(1, "Label %s removed.",
144 LIST_FIRST(&cp->geom->provider)->name);
149 g_label_resize(struct g_consumer *cp)
152 G_LABEL_DEBUG(1, "Label %s resized.",
153 LIST_FIRST(&cp->geom->provider)->name);
155 g_slice_config(cp->geom, 0, G_SLICE_CONFIG_FORCE, (off_t)0,
156 cp->provider->mediasize, cp->provider->sectorsize, "notused");
160 g_label_is_name_ok(const char *label)
164 /* Check if the label starts from ../ */
165 if (strncmp(label, "../", 3) == 0)
167 /* Check if the label contains /../ */
168 if (strstr(label, "/../") != NULL)
170 /* Check if the label ends at ../ */
171 if ((s = strstr(label, "/..")) != NULL && s[3] == '\0')
177 g_label_mangle_name(char *label, size_t size)
182 sb = sbuf_new(NULL, NULL, size, SBUF_FIXEDLEN);
183 for (c = label; *c != '\0'; c++) {
184 if (!isprint(*c) || isspace(*c) || *c =='"' || *c == '%')
185 sbuf_printf(sb, "%%%02X", *c);
189 if (sbuf_finish(sb) != 0)
192 strlcpy(label, sbuf_data(sb), size);
196 static struct g_geom *
197 g_label_create(struct gctl_req *req, struct g_class *mp, struct g_provider *pp,
198 const char *label, const char *dir, off_t mediasize)
201 struct g_provider *pp2;
202 struct g_consumer *cp;
207 if (!g_label_is_name_ok(label)) {
208 G_LABEL_DEBUG(0, "%s contains suspicious label, skipping.",
210 G_LABEL_DEBUG(1, "%s suspicious label is: %s", pp->name, label);
212 gctl_error(req, "Label name %s is invalid.", label);
217 if (snprintf(name, sizeof(name), "%s/%s", dir, label) >= sizeof(name)) {
219 gctl_error(req, "Label name %s is too long.", label);
222 LIST_FOREACH(gp, &mp->geom, geom) {
223 pp2 = LIST_FIRST(&gp->provider);
226 if ((pp2->flags & G_PF_ORPHAN) != 0)
228 if (strcmp(pp2->name, name) == 0) {
229 G_LABEL_DEBUG(1, "Label %s(%s) already exists (%s).",
230 label, name, pp->name);
232 gctl_error(req, "Provider %s already exists.",
238 gp = g_slice_new(mp, 1, pp, &cp, NULL, 0, NULL);
240 G_LABEL_DEBUG(0, "Cannot create slice %s.", label);
242 gctl_error(req, "Cannot create slice %s.", label);
245 gp->orphan = g_label_orphan;
246 gp->spoiled = g_label_spoiled;
247 gp->resize = g_label_resize;
248 g_access(cp, -1, 0, 0);
249 g_slice_config(gp, 0, G_SLICE_CONFIG_SET, (off_t)0, mediasize,
250 pp->sectorsize, "%s", name);
251 G_LABEL_DEBUG(1, "Label for provider %s is %s.", pp->name, name);
256 g_label_destroy(struct g_geom *gp, boolean_t force)
258 struct g_provider *pp;
261 pp = LIST_FIRST(&gp->provider);
262 if (pp != NULL && (pp->acr != 0 || pp->acw != 0 || pp->ace != 0)) {
264 G_LABEL_DEBUG(0, "Provider %s is still open, so it "
265 "can't be definitely removed.", pp->name);
268 "Provider %s is still open (r%dw%de%d).", pp->name,
269 pp->acr, pp->acw, pp->ace);
272 } else if (pp != NULL)
273 G_LABEL_DEBUG(1, "Label %s removed.", pp->name);
274 g_slice_spoiled(LIST_FIRST(&gp->consumer));
279 g_label_read_metadata(struct g_consumer *cp, struct g_label_metadata *md)
281 struct g_provider *pp;
289 buf = g_read_data(cp, pp->mediasize - pp->sectorsize, pp->sectorsize,
294 /* Decode metadata. */
295 label_metadata_decode(buf, md);
302 g_label_orphan_taste(struct g_consumer *cp __unused)
305 KASSERT(1 == 0, ("%s called?", __func__));
309 g_label_start_taste(struct bio *bp __unused)
312 KASSERT(1 == 0, ("%s called?", __func__));
316 g_label_access_taste(struct g_provider *pp __unused, int dr __unused,
317 int dw __unused, int de __unused)
320 KASSERT(1 == 0, ("%s called", __func__));
324 static struct g_geom *
325 g_label_taste(struct g_class *mp, struct g_provider *pp, int flags __unused)
327 struct g_label_metadata md;
328 struct g_consumer *cp;
332 g_trace(G_T_TOPOLOGY, "%s(%s, %s)", __func__, mp->name, pp->name);
335 G_LABEL_DEBUG(2, "Tasting %s.", pp->name);
337 /* Skip providers that are already open for writing. */
341 if (strcmp(pp->geom->class->name, mp->name) == 0)
344 gp = g_new_geomf(mp, "label:taste");
345 gp->start = g_label_start_taste;
346 gp->access = g_label_access_taste;
347 gp->orphan = g_label_orphan_taste;
348 cp = g_new_consumer(gp);
350 if (g_access(cp, 1, 0, 0) != 0)
353 if (g_label_read_metadata(cp, &md) != 0)
355 if (strcmp(md.md_magic, G_LABEL_MAGIC) != 0)
357 if (md.md_version > G_LABEL_VERSION) {
358 printf("geom_label.ko module is too old to handle %s.\n",
364 * Backward compatibility:
367 * There was no md_provsize field in earlier versions of
370 if (md.md_version < 2)
371 md.md_provsize = pp->mediasize;
373 if (md.md_provsize != pp->mediasize)
376 g_label_create(NULL, mp, pp, md.md_label, G_LABEL_DIR,
377 pp->mediasize - pp->sectorsize);
379 for (i = 0; g_labels[i] != NULL; i++) {
382 if (g_labels[i]->ld_enabled == 0)
385 g_labels[i]->ld_taste(cp, label, sizeof(label));
386 g_label_mangle_name(label, sizeof(label));
388 if (label[0] == '\0')
390 g_label_create(NULL, mp, pp, label, g_labels[i]->ld_dir,
393 g_access(cp, -1, 0, 0);
396 g_destroy_consumer(cp);
402 g_label_ctl_create(struct gctl_req *req, struct g_class *mp)
404 struct g_provider *pp;
410 nargs = gctl_get_paraml(req, "nargs", sizeof(*nargs));
412 gctl_error(req, "No '%s' argument", "nargs");
416 gctl_error(req, "Invalid number of arguments.");
420 * arg1 is the name of provider.
422 name = gctl_get_asciiparam(req, "arg1");
424 gctl_error(req, "No 'arg%d' argument", 1);
427 if (strncmp(name, "/dev/", strlen("/dev/")) == 0)
428 name += strlen("/dev/");
429 pp = g_provider_by_name(name);
431 G_LABEL_DEBUG(1, "Provider %s is invalid.", name);
432 gctl_error(req, "Provider %s is invalid.", name);
438 name = gctl_get_asciiparam(req, "arg0");
440 gctl_error(req, "No 'arg%d' argument", 0);
443 g_label_create(req, mp, pp, name, G_LABEL_DIR, pp->mediasize);
447 g_label_skip_dir(const char *name)
452 if (strncmp(name, "/dev/", strlen("/dev/")) == 0)
453 name += strlen("/dev/");
454 if (strncmp(name, G_LABEL_DIR "/", strlen(G_LABEL_DIR "/")) == 0)
455 name += strlen(G_LABEL_DIR "/");
456 for (i = 0; g_labels[i] != NULL; i++) {
457 snprintf(path, sizeof(path), "%s/", g_labels[i]->ld_dir);
458 if (strncmp(name, path, strlen(path)) == 0) {
459 name += strlen(path);
466 static struct g_geom *
467 g_label_find_geom(struct g_class *mp, const char *name)
470 struct g_provider *pp;
473 name = g_label_skip_dir(name);
474 LIST_FOREACH(gp, &mp->geom, geom) {
475 pp = LIST_FIRST(&gp->provider);
476 pname = g_label_skip_dir(pp->name);
477 if (strcmp(pname, name) == 0)
484 g_label_ctl_destroy(struct gctl_req *req, struct g_class *mp)
486 int *nargs, *force, error, i;
493 nargs = gctl_get_paraml(req, "nargs", sizeof(*nargs));
495 gctl_error(req, "No '%s' argument", "nargs");
499 gctl_error(req, "Missing device(s).");
502 force = gctl_get_paraml(req, "force", sizeof(*force));
504 gctl_error(req, "No 'force' argument");
508 for (i = 0; i < *nargs; i++) {
509 snprintf(param, sizeof(param), "arg%d", i);
510 name = gctl_get_asciiparam(req, param);
512 gctl_error(req, "No 'arg%d' argument", i);
515 gp = g_label_find_geom(mp, name);
517 G_LABEL_DEBUG(1, "Label %s is invalid.", name);
518 gctl_error(req, "Label %s is invalid.", name);
521 error = g_label_destroy(gp, *force);
523 gctl_error(req, "Cannot destroy label %s (error=%d).",
524 LIST_FIRST(&gp->provider)->name, error);
531 g_label_config(struct gctl_req *req, struct g_class *mp, const char *verb)
537 version = gctl_get_paraml(req, "version", sizeof(*version));
538 if (version == NULL) {
539 gctl_error(req, "No '%s' argument.", "version");
542 if (*version != G_LABEL_VERSION) {
543 gctl_error(req, "Userland and kernel parts are out of sync.");
547 if (strcmp(verb, "create") == 0) {
548 g_label_ctl_create(req, mp);
550 } else if (strcmp(verb, "destroy") == 0 ||
551 strcmp(verb, "stop") == 0) {
552 g_label_ctl_destroy(req, mp);
556 gctl_error(req, "Unknown verb.");
559 DECLARE_GEOM_CLASS(g_label_class, g_label);
560 MODULE_VERSION(geom_label, 0);