xref: /freebsd/sys/geom/label/g_label.c (revision b59017c5cad90d0f09a59e68c00457b7faf93e7c)
1 /*-
2  * SPDX-License-Identifier: BSD-2-Clause
3  *
4  * Copyright (c) 2004-2005 Pawel Jakub Dawidek <pjd@FreeBSD.org>
5  * All rights reserved.
6  *
7  * Redistribution and use in source and binary forms, with or without
8  * modification, are permitted provided that the following conditions
9  * are met:
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.
15  *
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
26  * SUCH DAMAGE.
27  */
28 
29 #include <sys/cdefs.h>
30 #include "opt_geom.h"
31 
32 #include <sys/param.h>
33 #include <sys/systm.h>
34 #include <sys/kernel.h>
35 #include <sys/module.h>
36 #include <sys/lock.h>
37 #include <sys/mutex.h>
38 #include <sys/bio.h>
39 #include <sys/ctype.h>
40 #include <sys/malloc.h>
41 #include <sys/libkern.h>
42 #include <sys/sbuf.h>
43 #include <sys/stddef.h>
44 #include <sys/sysctl.h>
45 #include <geom/geom.h>
46 #include <geom/geom_dbg.h>
47 #include <geom/geom_slice.h>
48 #include <geom/label/g_label.h>
49 
50 FEATURE(geom_label, "GEOM labeling support");
51 
52 SYSCTL_DECL(_kern_geom);
53 SYSCTL_NODE(_kern_geom, OID_AUTO, label, CTLFLAG_RW | CTLFLAG_MPSAFE, 0,
54     "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,
57     "Debug level");
58 
59 static int g_label_destroy_geom(struct gctl_req *req, struct g_class *mp,
60     struct g_geom *gp);
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,
63     int flags __unused);
64 static void g_label_generic_taste(struct g_consumer *, char *, size_t);
65 static void g_label_config(struct gctl_req *req, struct g_class *mp,
66     const char *verb);
67 
68 #define	G_LABEL_DIRPREFIX	"label/"
69 
70 struct g_class g_label_class = {
71 	.name = G_LABEL_CLASS_NAME,
72 	.version = G_VERSION,
73 	.ctlreq = g_label_config,
74 	.taste = g_label_taste,
75 	.destroy_geom = g_label_destroy_geom
76 };
77 
78 static struct g_label_desc g_label_generic = {
79         .ld_taste = g_label_generic_taste,
80         .ld_dirprefix = G_LABEL_DIRPREFIX,
81         .ld_enabled = 1
82 };
83 
84 /*
85  * To add a new file system where you want to look for volume labels,
86  * you have to:
87  * 1. Add a file g_label_<file system>.c which implements labels recognition.
88  * 2. Add an 'extern const struct g_label_desc g_label_<file system>;' into
89  *    g_label.h file.
90  * 3. Add an element to the table below '&g_label_<file system>,'.
91  * 4. Add your file to sys/conf/files.
92  * 5. Add your file to sys/modules/geom/geom_label/Makefile.
93  * 6. Add your file system to manual page sbin/geom/class/label/glabel.8.
94  */
95 const struct g_label_desc *g_labels[] = {
96 	&g_label_gpt,
97 	&g_label_gpt_uuid,
98 #ifdef GEOM_LABEL
99 	&g_label_ufs_id,
100 	&g_label_ufs_volume,
101 	&g_label_iso9660,
102 	&g_label_msdosfs,
103 	&g_label_ext2fs,
104 	&g_label_ntfs,
105 	&g_label_disk_ident,
106 	&g_label_flashmap,
107 	&g_label_swaplinux,
108 #endif
109 	&g_label_generic,
110 	NULL
111 };
112 
113 void
114 g_label_rtrim(char *label, size_t size)
115 {
116 	ptrdiff_t i;
117 
118 	for (i = size - 1; i >= 0; i--) {
119 		if (label[i] == '\0')
120 			continue;
121 		else if (label[i] == ' ')
122 			label[i] = '\0';
123 		else
124 			break;
125 	}
126 }
127 
128 static int
129 g_label_destroy_geom(struct gctl_req *req __unused, struct g_class *mp,
130     struct g_geom *gp __unused)
131 {
132 
133 	/*
134 	 * XXX: Unloading a class which is using geom_slice:1.56 is currently
135 	 * XXX: broken, so we deny unloading when we have geoms.
136 	 */
137 	return (EOPNOTSUPP);
138 }
139 
140 static void
141 g_label_orphan(struct g_consumer *cp)
142 {
143 
144 	G_LABEL_DEBUG(1, "Label %s removed.",
145 	    LIST_FIRST(&cp->geom->provider)->name);
146 	g_slice_orphan(cp);
147 }
148 
149 static void
150 g_label_spoiled(struct g_consumer *cp)
151 {
152 
153 	G_LABEL_DEBUG(1, "Label %s removed.",
154 	    LIST_FIRST(&cp->geom->provider)->name);
155 	g_slice_spoiled(cp);
156 }
157 
158 static void
159 g_label_resize(struct g_consumer *cp)
160 {
161 
162 	G_LABEL_DEBUG(1, "Label %s resized.",
163 	    LIST_FIRST(&cp->geom->provider)->name);
164 
165 	g_slice_config(cp->geom, 0, G_SLICE_CONFIG_FORCE, (off_t)0,
166 	    cp->provider->mediasize, cp->provider->sectorsize, "notused");
167 }
168 
169 static int
170 g_label_is_name_ok(const char *label)
171 {
172 	const char *s;
173 
174 	/* Check if the label starts from ../ */
175 	if (strncmp(label, "../", 3) == 0)
176 		return (0);
177 	/* Check if the label contains /../ */
178 	if (strstr(label, "/../") != NULL)
179 		return (0);
180 	/* Check if the label ends at ../ */
181 	if ((s = strstr(label, "/..")) != NULL && s[3] == '\0')
182 		return (0);
183 	return (1);
184 }
185 
186 static void
187 g_label_mangle_name(char *label, size_t size)
188 {
189 	struct sbuf *sb;
190 	const u_char *c;
191 	size_t len, i;
192 
193 	/* Trim trailing whitespace. */
194 	len = strlen(label);
195 	for (i = len; i > 0; i--) {
196 		if (isspace(label[i - 1]))
197 			label[i - 1] = '\0';
198 		else
199 			break;
200 	}
201 	if (*label == '\0')
202 		return;
203 
204 	sb = sbuf_new(NULL, NULL, size, SBUF_FIXEDLEN);
205 	for (c = label; *c != '\0'; c++) {
206 		/* Trim leading whitespace. */
207 		if (isspace(*c) && sbuf_len(sb) == 0)
208 			continue;
209 		if (!isprint(*c) || isspace(*c) || *c =='"' || *c == '%')
210 			sbuf_printf(sb, "%%%02X", *c);
211 		else
212 			sbuf_putc(sb, *c);
213 	}
214 	if (sbuf_finish(sb) != 0)
215 		label[0] = '\0';
216 	else
217 		strlcpy(label, sbuf_data(sb), size);
218 	sbuf_delete(sb);
219 }
220 
221 static struct g_geom *
222 g_label_create(struct gctl_req *req, struct g_class *mp, struct g_provider *pp,
223     const char *label, const char *dirprefix, off_t mediasize)
224 {
225 	struct g_geom *gp;
226 	struct g_provider *pp2;
227 	struct g_consumer *cp;
228 	char name[64];
229 
230 	g_topology_assert();
231 
232 	if (!g_label_is_name_ok(label)) {
233 		G_LABEL_DEBUG(0, "%s contains suspicious label, skipping.",
234 		    pp->name);
235 		G_LABEL_DEBUG(1, "%s suspicious label is: %s", pp->name, label);
236 		if (req != NULL)
237 			gctl_error(req, "Label name %s is invalid.", label);
238 		return (NULL);
239 	}
240 	gp = NULL;
241 	cp = NULL;
242 	if (snprintf(name, sizeof(name), "%s%s", dirprefix, label) >= sizeof(name)) {
243 		if (req != NULL)
244 			gctl_error(req, "Label name %s is too long.", label);
245 		return (NULL);
246 	}
247 	LIST_FOREACH(gp, &mp->geom, geom) {
248 		pp2 = LIST_FIRST(&gp->provider);
249 		if (pp2 == NULL)
250 			continue;
251 		if ((pp2->flags & G_PF_ORPHAN) != 0)
252 			continue;
253 		if (strcmp(pp2->name, name) == 0) {
254 			G_LABEL_DEBUG(1, "Label %s(%s) already exists (%s).",
255 			    label, name, pp->name);
256 			if (req != NULL) {
257 				gctl_error(req, "Provider %s already exists.",
258 				    name);
259 			}
260 			return (NULL);
261 		}
262 	}
263 	gp = g_slice_new(mp, 1, pp, &cp, NULL, 0, NULL);
264 	if (gp == NULL) {
265 		G_LABEL_DEBUG(0, "Cannot create slice %s.", label);
266 		if (req != NULL)
267 			gctl_error(req, "Cannot create slice %s.", label);
268 		return (NULL);
269 	}
270 	gp->orphan = g_label_orphan;
271 	gp->spoiled = g_label_spoiled;
272 	gp->resize = g_label_resize;
273 	g_access(cp, -1, 0, 0);
274 	g_slice_config(gp, 0, G_SLICE_CONFIG_SET, (off_t)0, mediasize,
275 	    pp->sectorsize, "%s", name);
276 	G_LABEL_DEBUG(1, "Label for provider %s is %s.", pp->name, name);
277 	return (gp);
278 }
279 
280 static int
281 g_label_destroy(struct g_geom *gp, boolean_t force)
282 {
283 	struct g_provider *pp;
284 
285 	g_topology_assert();
286 	pp = LIST_FIRST(&gp->provider);
287 	if (pp != NULL && (pp->acr != 0 || pp->acw != 0 || pp->ace != 0)) {
288 		if (force) {
289 			G_LABEL_DEBUG(0, "Provider %s is still open, so it "
290 			    "can't be definitely removed.", pp->name);
291 		} else {
292 			G_LABEL_DEBUG(1,
293 			    "Provider %s is still open (r%dw%de%d).", pp->name,
294 			    pp->acr, pp->acw, pp->ace);
295 			return (EBUSY);
296 		}
297 	} else if (pp != NULL)
298 		G_LABEL_DEBUG(1, "Label %s removed.", pp->name);
299 	g_slice_spoiled(LIST_FIRST(&gp->consumer));
300 	return (0);
301 }
302 
303 static int
304 g_label_read_metadata(struct g_consumer *cp, struct g_label_metadata *md)
305 {
306 	struct g_provider *pp;
307 	u_char *buf;
308 	int error;
309 
310 	pp = cp->provider;
311 	if (pp->sectorsize < sizeof(*md))
312 		return (EINVAL);
313 	buf = g_read_data(cp, pp->mediasize - pp->sectorsize, pp->sectorsize,
314 	    &error);
315 	if (buf == NULL)
316 		return (error);
317 	/* Decode metadata. */
318 	label_metadata_decode(buf, md);
319 	g_free(buf);
320 
321 	return (0);
322 }
323 
324 static void
325 g_label_orphan_taste(struct g_consumer *cp __unused)
326 {
327 
328 	KASSERT(1 == 0, ("%s called?", __func__));
329 }
330 
331 static void
332 g_label_start_taste(struct bio *bp __unused)
333 {
334 
335 	KASSERT(1 == 0, ("%s called?", __func__));
336 }
337 
338 static int
339 g_label_access_taste(struct g_provider *pp __unused, int dr __unused,
340     int dw __unused, int de __unused)
341 {
342 
343 	KASSERT(1 == 0, ("%s called", __func__));
344 	return (EOPNOTSUPP);
345 }
346 
347 static void
348 g_label_generic_taste(struct g_consumer *cp, char *label, size_t size)
349 {
350 	struct g_provider *pp;
351 	struct g_label_metadata md;
352 
353 	g_topology_assert_not();
354 	label[0] = '\0';
355 	pp = cp->provider;
356 
357 	if (g_label_read_metadata(cp, &md) != 0)
358 		return;
359 
360 	if (strcmp(md.md_magic, G_LABEL_MAGIC) != 0)
361 		return;
362 
363 	if (md.md_version > G_LABEL_VERSION) {
364 		printf("geom_label.ko module is too old to handle %s.\n",
365 			pp->name);
366 		return;
367 	}
368 	/*
369 	 * Backward compatibility: there was no md_provsize field in
370 	 * earlier versions of metadata, so only check if we have it.
371 	 */
372 	if (md.md_version >= 2 && md.md_provsize != pp->mediasize)
373 		return;
374 
375 	strlcpy(label, md.md_label, size);
376 }
377 
378 static struct g_geom *
379 g_label_taste(struct g_class *mp, struct g_provider *pp, int flags __unused)
380 {
381 	struct g_consumer *cp;
382 	struct g_geom *gp;
383 	off_t mediasize;
384 	int i;
385 
386 	g_trace(G_T_TOPOLOGY, "%s(%s, %s)", __func__, mp->name, pp->name);
387 	g_topology_assert();
388 
389 	G_LABEL_DEBUG(2, "Tasting %s.", pp->name);
390 
391 	/* Skip providers that are already open for writing. */
392 	if (pp->acw > 0)
393 		return (NULL);
394 
395 	/* Skip broken disks that don't set their sector size */
396 	if (pp->sectorsize == 0)
397 		return (NULL);
398 
399 	if (strcmp(pp->geom->class->name, mp->name) == 0)
400 		return (NULL);
401 
402 	gp = g_new_geomf(mp, "label:taste");
403 	gp->start = g_label_start_taste;
404 	gp->access = g_label_access_taste;
405 	gp->orphan = g_label_orphan_taste;
406 	cp = g_new_consumer(gp);
407 	cp->flags |= G_CF_DIRECT_SEND | G_CF_DIRECT_RECEIVE;
408 	if (g_attach(cp, pp) != 0)
409 		goto end2;
410 	if (g_access(cp, 1, 0, 0) != 0)
411 		goto end;
412 	for (i = 0; g_labels[i] != NULL; i++) {
413 		char label[128];
414 
415 		if (g_labels[i]->ld_enabled == 0)
416 			continue;
417 		g_topology_unlock();
418 		g_labels[i]->ld_taste(cp, label, sizeof(label));
419 		g_label_mangle_name(label, sizeof(label));
420 		g_topology_lock();
421 		if (label[0] == '\0')
422 			continue;
423 		if (g_labels[i] != &g_label_generic) {
424 			mediasize = pp->mediasize;
425 		} else {
426 			mediasize = pp->mediasize - pp->sectorsize;
427 		}
428 		g_label_create(NULL, mp, pp, label,
429 		    g_labels[i]->ld_dirprefix, mediasize);
430 	}
431 	g_access(cp, -1, 0, 0);
432 end:
433 	g_detach(cp);
434 end2:
435 	g_destroy_consumer(cp);
436 	g_destroy_geom(gp);
437 	return (NULL);
438 }
439 
440 static void
441 g_label_ctl_create(struct gctl_req *req, struct g_class *mp)
442 {
443 	struct g_provider *pp;
444 	const char *name;
445 	int *nargs;
446 
447 	g_topology_assert();
448 
449 	nargs = gctl_get_paraml(req, "nargs", sizeof(*nargs));
450 	if (nargs == NULL) {
451 		gctl_error(req, "No '%s' argument", "nargs");
452 		return;
453 	}
454 	if (*nargs != 2) {
455 		gctl_error(req, "Invalid number of arguments.");
456 		return;
457 	}
458 	/*
459 	 * arg1 is the name of provider.
460 	 */
461 	pp = gctl_get_provider(req, "arg1");
462 	if (pp == NULL)
463 		return;
464 	/*
465 	 * arg0 is the label.
466 	 */
467 	name = gctl_get_asciiparam(req, "arg0");
468 	if (name == NULL) {
469 		gctl_error(req, "No 'arg%d' argument", 0);
470 		return;
471 	}
472 	g_label_create(req, mp, pp, name, G_LABEL_DIRPREFIX, pp->mediasize);
473 }
474 
475 static const char *
476 g_label_skip_dir(const char *name)
477 {
478 	u_int i;
479 
480 	if (strncmp(name, _PATH_DEV, strlen(_PATH_DEV)) == 0)
481 		name += strlen(_PATH_DEV);
482 	for (i = 0; g_labels[i] != NULL; i++) {
483 		if (strncmp(name, g_labels[i]->ld_dirprefix,
484 		    strlen(g_labels[i]->ld_dirprefix)) == 0) {
485 			name += strlen(g_labels[i]->ld_dirprefix);
486 			break;
487 		}
488 	}
489 	return (name);
490 }
491 
492 static struct g_geom *
493 g_label_find_geom(struct g_class *mp, const char *name)
494 {
495 	struct g_geom *gp;
496 	struct g_provider *pp;
497 	const char *pname;
498 
499 	name = g_label_skip_dir(name);
500 	LIST_FOREACH(gp, &mp->geom, geom) {
501 		pp = LIST_FIRST(&gp->provider);
502 		pname = g_label_skip_dir(pp->name);
503 		if (strcmp(pname, name) == 0)
504 			return (gp);
505 	}
506 	return (NULL);
507 }
508 
509 static void
510 g_label_ctl_destroy(struct gctl_req *req, struct g_class *mp)
511 {
512 	int *nargs, *force, error, i;
513 	struct g_geom *gp;
514 	const char *name;
515 	char param[16];
516 
517 	g_topology_assert();
518 
519 	nargs = gctl_get_paraml(req, "nargs", sizeof(*nargs));
520 	if (nargs == NULL) {
521 		gctl_error(req, "No '%s' argument", "nargs");
522 		return;
523 	}
524 	if (*nargs <= 0) {
525 		gctl_error(req, "Missing device(s).");
526 		return;
527 	}
528 	force = gctl_get_paraml(req, "force", sizeof(*force));
529 	if (force == NULL) {
530 		gctl_error(req, "No 'force' argument");
531 		return;
532 	}
533 
534 	for (i = 0; i < *nargs; i++) {
535 		snprintf(param, sizeof(param), "arg%d", i);
536 		name = gctl_get_asciiparam(req, param);
537 		if (name == NULL) {
538 			gctl_error(req, "No 'arg%d' argument", i);
539 			return;
540 		}
541 		gp = g_label_find_geom(mp, name);
542 		if (gp == NULL) {
543 			G_LABEL_DEBUG(1, "Label %s is invalid.", name);
544 			gctl_error(req, "Label %s is invalid.", name);
545 			return;
546 		}
547 		error = g_label_destroy(gp, *force);
548 		if (error != 0) {
549 			gctl_error(req, "Cannot destroy label %s (error=%d).",
550 			    LIST_FIRST(&gp->provider)->name, error);
551 			return;
552 		}
553 	}
554 }
555 
556 static void
557 g_label_config(struct gctl_req *req, struct g_class *mp, const char *verb)
558 {
559 	uint32_t *version;
560 
561 	g_topology_assert();
562 
563 	version = gctl_get_paraml(req, "version", sizeof(*version));
564 	if (version == NULL) {
565 		gctl_error(req, "No '%s' argument.", "version");
566 		return;
567 	}
568 	if (*version != G_LABEL_VERSION) {
569 		gctl_error(req, "Userland and kernel parts are out of sync.");
570 		return;
571 	}
572 
573 	if (strcmp(verb, "create") == 0) {
574 		g_label_ctl_create(req, mp);
575 		return;
576 	} else if (strcmp(verb, "destroy") == 0 ||
577 	    strcmp(verb, "stop") == 0) {
578 		g_label_ctl_destroy(req, mp);
579 		return;
580 	}
581 
582 	gctl_error(req, "Unknown verb.");
583 }
584 
585 DECLARE_GEOM_CLASS(g_label_class, g_label);
586 MODULE_VERSION(geom_label, 0);
587