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