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