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