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