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