2 * Copyright (c) 2004-2005 Pawel Jakub Dawidek <pjd@FreeBSD.org>
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions
8 * 1. Redistributions of source code must retain the above copyright
9 * notice, this list of conditions and the following disclaimer.
10 * 2. Redistributions in binary form must reproduce the above copyright
11 * notice, this list of conditions and the following disclaimer in the
12 * documentation and/or other materials provided with the distribution.
14 * THIS SOFTWARE IS PROVIDED BY THE AUTHORS AND CONTRIBUTORS ``AS IS'' AND
15 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
16 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
17 * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHORS OR CONTRIBUTORS BE LIABLE
18 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
19 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
20 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
21 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
22 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
23 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
27 #include <sys/cdefs.h>
28 __FBSDID("$FreeBSD$");
30 #include <sys/param.h>
31 #include <sys/systm.h>
32 #include <sys/kernel.h>
33 #include <sys/module.h>
35 #include <sys/mutex.h>
37 #include <sys/ctype.h>
38 #include <sys/malloc.h>
39 #include <sys/libkern.h>
41 #include <sys/sysctl.h>
42 #include <geom/geom.h>
43 #include <geom/geom_slice.h>
44 #include <geom/label/g_label.h>
46 FEATURE(geom_label, "GEOM labeling support");
48 SYSCTL_DECL(_kern_geom);
49 SYSCTL_NODE(_kern_geom, OID_AUTO, label, CTLFLAG_RW, 0, "GEOM_LABEL stuff");
50 u_int g_label_debug = 0;
51 TUNABLE_INT("kern.geom.label.debug", &g_label_debug);
52 SYSCTL_UINT(_kern_geom_label, OID_AUTO, debug, CTLFLAG_RW, &g_label_debug, 0,
55 static int g_label_destroy_geom(struct gctl_req *req, struct g_class *mp,
57 static int g_label_destroy(struct g_geom *gp, boolean_t force);
58 static struct g_geom *g_label_taste(struct g_class *mp, struct g_provider *pp,
60 static void g_label_config(struct gctl_req *req, struct g_class *mp,
63 struct g_class g_label_class = {
64 .name = G_LABEL_CLASS_NAME,
66 .ctlreq = g_label_config,
67 .taste = g_label_taste,
68 .destroy_geom = g_label_destroy_geom
72 * To add a new file system where you want to look for volume labels,
74 * 1. Add a file g_label_<file system>.c which implements labels recognition.
75 * 2. Add an 'extern const struct g_label_desc g_label_<file system>;' into
77 * 3. Add an element to the table below '&g_label_<file system>,'.
78 * 4. Add your file to sys/conf/files.
79 * 5. Add your file to sys/modules/geom/geom_label/Makefile.
80 * 6. Add your file system to manual page sbin/geom/class/label/glabel.8.
82 const struct g_label_desc *g_labels[] = {
98 g_label_destroy_geom(struct gctl_req *req __unused, struct g_class *mp,
99 struct g_geom *gp __unused)
103 * XXX: Unloading a class which is using geom_slice:1.56 is currently
104 * XXX: broken, so we deny unloading when we have geoms.
110 g_label_orphan(struct g_consumer *cp)
113 G_LABEL_DEBUG(1, "Label %s removed.",
114 LIST_FIRST(&cp->geom->provider)->name);
119 g_label_spoiled(struct g_consumer *cp)
122 G_LABEL_DEBUG(1, "Label %s removed.",
123 LIST_FIRST(&cp->geom->provider)->name);
128 g_label_is_name_ok(const char *label)
132 /* Check if the label starts from ../ */
133 if (strncmp(label, "../", 3) == 0)
135 /* Check if the label contains /../ */
136 if (strstr(label, "/../") != NULL)
138 /* Check if the label ends at ../ */
139 if ((s = strstr(label, "/..")) != NULL && s[3] == '\0')
145 g_label_mangle_name(char *label, size_t size)
150 sb = sbuf_new(NULL, NULL, size, SBUF_FIXEDLEN);
151 for (c = label; *c != '\0'; c++) {
152 if (!isprint(*c) || isspace(*c) || *c =='"' || *c == '%')
153 sbuf_printf(sb, "%%%02X", *c);
157 if (sbuf_finish(sb) != 0)
160 strlcpy(label, sbuf_data(sb), size);
164 static struct g_geom *
165 g_label_create(struct gctl_req *req, struct g_class *mp, struct g_provider *pp,
166 const char *label, const char *dir, off_t mediasize)
169 struct g_provider *pp2;
170 struct g_consumer *cp;
175 if (!g_label_is_name_ok(label)) {
176 G_LABEL_DEBUG(0, "%s contains suspicious label, skipping.",
178 G_LABEL_DEBUG(1, "%s suspicious label is: %s", pp->name, label);
180 gctl_error(req, "Label name %s is invalid.", label);
185 snprintf(name, sizeof(name), "%s/%s", dir, label);
186 LIST_FOREACH(gp, &mp->geom, geom) {
187 pp2 = LIST_FIRST(&gp->provider);
190 if ((pp2->flags & G_PF_ORPHAN) != 0)
192 if (strcmp(pp2->name, name) == 0) {
193 G_LABEL_DEBUG(1, "Label %s(%s) already exists (%s).",
194 label, name, pp->name);
196 gctl_error(req, "Provider %s already exists.",
202 gp = g_slice_new(mp, 1, pp, &cp, NULL, 0, NULL);
204 G_LABEL_DEBUG(0, "Cannot create slice %s.", label);
206 gctl_error(req, "Cannot create slice %s.", label);
209 gp->orphan = g_label_orphan;
210 gp->spoiled = g_label_spoiled;
211 g_access(cp, -1, 0, 0);
212 g_slice_config(gp, 0, G_SLICE_CONFIG_SET, (off_t)0, mediasize,
213 pp->sectorsize, "%s", name);
214 G_LABEL_DEBUG(1, "Label for provider %s is %s.", pp->name, name);
219 g_label_destroy(struct g_geom *gp, boolean_t force)
221 struct g_provider *pp;
224 pp = LIST_FIRST(&gp->provider);
225 if (pp != NULL && (pp->acr != 0 || pp->acw != 0 || pp->ace != 0)) {
227 G_LABEL_DEBUG(0, "Provider %s is still open, so it "
228 "can't be definitely removed.", pp->name);
231 "Provider %s is still open (r%dw%de%d).", pp->name,
232 pp->acr, pp->acw, pp->ace);
235 } else if (pp != NULL)
236 G_LABEL_DEBUG(1, "Label %s removed.", pp->name);
237 g_slice_spoiled(LIST_FIRST(&gp->consumer));
242 g_label_read_metadata(struct g_consumer *cp, struct g_label_metadata *md)
244 struct g_provider *pp;
252 buf = g_read_data(cp, pp->mediasize - pp->sectorsize, pp->sectorsize,
257 /* Decode metadata. */
258 label_metadata_decode(buf, md);
265 g_label_orphan_taste(struct g_consumer *cp __unused)
268 KASSERT(1 == 0, ("%s called?", __func__));
272 g_label_start_taste(struct bio *bp __unused)
275 KASSERT(1 == 0, ("%s called?", __func__));
279 g_label_access_taste(struct g_provider *pp __unused, int dr __unused,
280 int dw __unused, int de __unused)
283 KASSERT(1 == 0, ("%s called", __func__));
287 static struct g_geom *
288 g_label_taste(struct g_class *mp, struct g_provider *pp, int flags __unused)
290 struct g_label_metadata md;
291 struct g_consumer *cp;
295 g_trace(G_T_TOPOLOGY, "%s(%s, %s)", __func__, mp->name, pp->name);
298 G_LABEL_DEBUG(2, "Tasting %s.", pp->name);
300 /* Skip providers that are already open for writing. */
304 if (strcmp(pp->geom->class->name, mp->name) == 0)
307 gp = g_new_geomf(mp, "label:taste");
308 gp->start = g_label_start_taste;
309 gp->access = g_label_access_taste;
310 gp->orphan = g_label_orphan_taste;
311 cp = g_new_consumer(gp);
313 if (g_access(cp, 1, 0, 0) != 0)
316 if (g_label_read_metadata(cp, &md) != 0)
318 if (strcmp(md.md_magic, G_LABEL_MAGIC) != 0)
320 if (md.md_version > G_LABEL_VERSION) {
321 printf("geom_label.ko module is too old to handle %s.\n",
327 * Backward compatibility:
330 * There was no md_provsize field in earlier versions of
333 if (md.md_version < 2)
334 md.md_provsize = pp->mediasize;
336 if (md.md_provsize != pp->mediasize)
339 g_label_create(NULL, mp, pp, md.md_label, G_LABEL_DIR,
340 pp->mediasize - pp->sectorsize);
342 for (i = 0; g_labels[i] != NULL; i++) {
345 if (g_labels[i]->ld_enabled == 0)
348 g_labels[i]->ld_taste(cp, label, sizeof(label));
349 g_label_mangle_name(label, sizeof(label));
351 if (label[0] == '\0')
353 g_label_create(NULL, mp, pp, label, g_labels[i]->ld_dir,
356 g_access(cp, -1, 0, 0);
359 g_destroy_consumer(cp);
365 g_label_ctl_create(struct gctl_req *req, struct g_class *mp)
367 struct g_provider *pp;
373 nargs = gctl_get_paraml(req, "nargs", sizeof(*nargs));
375 gctl_error(req, "No '%s' argument", "nargs");
379 gctl_error(req, "Invalid number of arguments.");
383 * arg1 is the name of provider.
385 name = gctl_get_asciiparam(req, "arg1");
387 gctl_error(req, "No 'arg%d' argument", 1);
390 if (strncmp(name, "/dev/", strlen("/dev/")) == 0)
391 name += strlen("/dev/");
392 pp = g_provider_by_name(name);
394 G_LABEL_DEBUG(1, "Provider %s is invalid.", name);
395 gctl_error(req, "Provider %s is invalid.", name);
401 name = gctl_get_asciiparam(req, "arg0");
403 gctl_error(req, "No 'arg%d' argument", 0);
406 g_label_create(req, mp, pp, name, G_LABEL_DIR, pp->mediasize);
410 g_label_skip_dir(const char *name)
415 if (strncmp(name, "/dev/", strlen("/dev/")) == 0)
416 name += strlen("/dev/");
417 if (strncmp(name, G_LABEL_DIR "/", strlen(G_LABEL_DIR "/")) == 0)
418 name += strlen(G_LABEL_DIR "/");
419 for (i = 0; g_labels[i] != NULL; i++) {
420 snprintf(path, sizeof(path), "%s/", g_labels[i]->ld_dir);
421 if (strncmp(name, path, strlen(path)) == 0) {
422 name += strlen(path);
429 static struct g_geom *
430 g_label_find_geom(struct g_class *mp, const char *name)
433 struct g_provider *pp;
436 name = g_label_skip_dir(name);
437 LIST_FOREACH(gp, &mp->geom, geom) {
438 pp = LIST_FIRST(&gp->provider);
439 pname = g_label_skip_dir(pp->name);
440 if (strcmp(pname, name) == 0)
447 g_label_ctl_destroy(struct gctl_req *req, struct g_class *mp)
449 int *nargs, *force, error, i;
456 nargs = gctl_get_paraml(req, "nargs", sizeof(*nargs));
458 gctl_error(req, "No '%s' argument", "nargs");
462 gctl_error(req, "Missing device(s).");
465 force = gctl_get_paraml(req, "force", sizeof(*force));
467 gctl_error(req, "No 'force' argument");
471 for (i = 0; i < *nargs; i++) {
472 snprintf(param, sizeof(param), "arg%d", i);
473 name = gctl_get_asciiparam(req, param);
475 gctl_error(req, "No 'arg%d' argument", i);
478 gp = g_label_find_geom(mp, name);
480 G_LABEL_DEBUG(1, "Label %s is invalid.", name);
481 gctl_error(req, "Label %s is invalid.", name);
484 error = g_label_destroy(gp, *force);
486 gctl_error(req, "Cannot destroy label %s (error=%d).",
487 LIST_FIRST(&gp->provider)->name, error);
494 g_label_config(struct gctl_req *req, struct g_class *mp, const char *verb)
500 version = gctl_get_paraml(req, "version", sizeof(*version));
501 if (version == NULL) {
502 gctl_error(req, "No '%s' argument.", "version");
505 if (*version != G_LABEL_VERSION) {
506 gctl_error(req, "Userland and kernel parts are out of sync.");
510 if (strcmp(verb, "create") == 0) {
511 g_label_ctl_create(req, mp);
513 } else if (strcmp(verb, "destroy") == 0 ||
514 strcmp(verb, "stop") == 0) {
515 g_label_ctl_destroy(req, mp);
519 gctl_error(req, "Unknown verb.");
522 DECLARE_GEOM_CLASS(g_label_class, g_label);