1 /*- 2 * Copyright (c) 2004 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 <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 which implements looking for volume labels. 71 * 2. Add an 'extern const struct g_label_desc g_label_<your file system>;'. 72 * 3. Add an element to the table below '&g_label_<your_file_system>,'. 73 */ 74 const struct g_label_desc *g_labels[] = { 75 &g_label_ufs, 76 &g_label_iso9660, 77 &g_label_msdosfs, 78 NULL 79 }; 80 81 82 static int 83 g_label_destroy_geom(struct gctl_req *req __unused, struct g_class *mp, 84 struct g_geom *gp __unused) 85 { 86 87 /* 88 * XXX: Unloading a class which is using geom_slice:1.56 is currently 89 * XXX: broken, so we deny unloading when we have geoms. 90 */ 91 return (EOPNOTSUPP); 92 } 93 94 static void 95 g_label_orphan(struct g_consumer *cp __unused) 96 { 97 98 KASSERT(1 == 0, ("%s called?", __func__)); 99 } 100 101 static void 102 g_label_start(struct bio *bp __unused) 103 { 104 105 KASSERT(1 == 0, ("%s called?", __func__)); 106 } 107 108 static int 109 g_label_access(struct g_provider *pp __unused, int dr __unused, int dw __unused, 110 int de __unused) 111 { 112 113 KASSERT(1 == 0, ("%s called", __func__)); 114 return (EOPNOTSUPP); 115 } 116 117 static struct g_geom * 118 g_label_create(struct gctl_req *req, struct g_class *mp, struct g_provider *pp, 119 const char *label, const char *dir, off_t mediasize) 120 { 121 struct g_geom *gp; 122 struct g_provider *pp2; 123 struct g_consumer *cp; 124 char name[64]; 125 126 g_topology_assert(); 127 128 gp = NULL; 129 cp = NULL; 130 snprintf(name, sizeof(name), "%s/%s", dir, label); 131 LIST_FOREACH(gp, &mp->geom, geom) { 132 pp2 = LIST_FIRST(&gp->provider); 133 if (pp2 == NULL) 134 continue; 135 if (strcmp(pp2->name, name) == 0) { 136 G_LABEL_DEBUG(1, "Label %s(%s) already exists (%s).", 137 label, name, pp->name); 138 if (req != NULL) { 139 gctl_error(req, "Provider %s already exists.", 140 name); 141 } 142 return (NULL); 143 } 144 } 145 gp = g_slice_new(mp, 1, pp, &cp, NULL, 0, NULL); 146 if (gp == NULL) { 147 G_LABEL_DEBUG(0, "Cannot create slice %s.", label); 148 if (req != NULL) 149 gctl_error(req, "Cannot create slice %s.", label); 150 return (NULL); 151 } 152 g_access(cp, -1, 0, 0); 153 g_slice_config(gp, 0, G_SLICE_CONFIG_SET, (off_t)0, mediasize, 154 pp->sectorsize, name); 155 G_LABEL_DEBUG(0, "Label for provider %s is %s.", pp->name, name); 156 return (gp); 157 } 158 159 static int 160 g_label_destroy(struct g_geom *gp, boolean_t force) 161 { 162 struct g_provider *pp; 163 164 g_topology_assert(); 165 pp = LIST_FIRST(&gp->provider); 166 if (pp != NULL && (pp->acr != 0 || pp->acw != 0 || pp->ace != 0)) { 167 if (force) { 168 G_LABEL_DEBUG(0, "Provider %s is still open, so it " 169 "can't be definitely removed.", pp->name); 170 } else { 171 G_LABEL_DEBUG(1, 172 "Provider %s is still open (r%dw%de%d).", pp->name, 173 pp->acr, pp->acw, pp->ace); 174 return (EBUSY); 175 } 176 } else { 177 G_LABEL_DEBUG(0, "Label %s removed.", 178 LIST_FIRST(&gp->provider)->name); 179 } 180 g_slice_spoiled(LIST_FIRST(&gp->consumer)); 181 return (0); 182 } 183 184 static int 185 g_label_read_metadata(struct g_consumer *cp, struct g_label_metadata *md) 186 { 187 struct g_provider *pp; 188 u_char *buf; 189 int error; 190 191 g_topology_assert(); 192 193 pp = cp->provider; 194 g_topology_unlock(); 195 buf = g_read_data(cp, pp->mediasize - pp->sectorsize, pp->sectorsize, 196 &error); 197 g_topology_lock(); 198 if (buf == NULL) 199 return (error); 200 /* Decode metadata. */ 201 label_metadata_decode(buf, md); 202 g_free(buf); 203 204 return (0); 205 } 206 207 static struct g_geom * 208 g_label_taste(struct g_class *mp, struct g_provider *pp, int flags __unused) 209 { 210 struct g_label_metadata md; 211 struct g_consumer *cp; 212 struct g_geom *gp; 213 int i; 214 215 g_trace(G_T_TOPOLOGY, "%s(%s, %s)", __func__, mp->name, pp->name); 216 g_topology_assert(); 217 218 G_LABEL_DEBUG(2, "Tasting %s.", pp->name); 219 220 if (strcmp(pp->geom->class->name, mp->name) == 0) 221 return (NULL); 222 223 gp = g_new_geomf(mp, "label:taste"); 224 gp->start = g_label_start; 225 gp->access = g_label_access; 226 gp->orphan = g_label_orphan; 227 cp = g_new_consumer(gp); 228 g_attach(cp, pp); 229 if (g_access(cp, 1, 0, 0) != 0) 230 goto end; 231 do { 232 if (g_label_read_metadata(cp, &md) != 0) 233 break; 234 if (strcmp(md.md_magic, G_LABEL_MAGIC) != 0) 235 break; 236 if (md.md_version > G_LABEL_VERSION) { 237 printf("geom_label.ko module is too old to handle %s.\n", 238 pp->name); 239 break; 240 } 241 g_label_create(NULL, mp, pp, md.md_label, G_LABEL_DIR, 242 pp->mediasize - pp->sectorsize); 243 } while (0); 244 for (i = 0; g_labels[i] != NULL; i++) { 245 char label[64]; 246 247 g_topology_unlock(); 248 g_labels[i]->ld_taste(cp, label, sizeof(label)); 249 g_topology_lock(); 250 if (label[0] == '\0') 251 continue; 252 g_label_create(NULL, mp, pp, label, g_labels[i]->ld_dir, 253 pp->mediasize); 254 } 255 g_access(cp, -1, 0, 0); 256 end: 257 g_detach(cp); 258 g_destroy_consumer(cp); 259 g_destroy_geom(gp); 260 return (NULL); 261 } 262 263 static void 264 g_label_ctl_create(struct gctl_req *req, struct g_class *mp) 265 { 266 struct g_provider *pp; 267 const char *name; 268 int *nargs; 269 270 g_topology_assert(); 271 272 nargs = gctl_get_paraml(req, "nargs", sizeof(*nargs)); 273 if (nargs == NULL) { 274 gctl_error(req, "No '%s' argument", "nargs"); 275 return; 276 } 277 if (*nargs != 2) { 278 gctl_error(req, "Invalid number of argument."); 279 return; 280 } 281 /* 282 * arg1 is the name of provider. 283 */ 284 name = gctl_get_asciiparam(req, "arg1"); 285 if (name == NULL) { 286 gctl_error(req, "No 'arg%d' argument", 1); 287 return; 288 } 289 if (strncmp(name, "/dev/", strlen("/dev/")) == 0) 290 name += strlen("/dev/"); 291 pp = g_provider_by_name(name); 292 if (pp == NULL) { 293 G_LABEL_DEBUG(1, "Provider %s is invalid.", name); 294 gctl_error(req, "Provider %s is invalid.", name); 295 return; 296 } 297 /* 298 * arg0 is the label. 299 */ 300 name = gctl_get_asciiparam(req, "arg0"); 301 if (name == NULL) { 302 gctl_error(req, "No 'arg%d' argument", 0); 303 return; 304 } 305 g_label_create(req, mp, pp, name, G_LABEL_DIR, pp->mediasize); 306 } 307 308 static const char * 309 g_label_skip_dir(const char *name) 310 { 311 char path[64]; 312 u_int i; 313 314 if (strncmp(name, "/dev/", strlen("/dev/")) == 0) 315 name += strlen("/dev/"); 316 if (strncmp(name, G_LABEL_DIR "/", strlen(G_LABEL_DIR "/")) == 0) 317 name += strlen(G_LABEL_DIR "/"); 318 for (i = 0; g_labels[i] != NULL; i++) { 319 snprintf(path, sizeof(path), "%s/", g_labels[i]->ld_dir); 320 if (strncmp(name, path, strlen(path)) == 0) { 321 name += strlen(path); 322 break; 323 } 324 } 325 return (name); 326 } 327 328 static struct g_geom * 329 g_label_find_geom(struct g_class *mp, const char *name) 330 { 331 struct g_geom *gp; 332 struct g_provider *pp; 333 const char *pname; 334 335 name = g_label_skip_dir(name); 336 LIST_FOREACH(gp, &mp->geom, geom) { 337 pp = LIST_FIRST(&gp->provider); 338 pname = g_label_skip_dir(pp->name); 339 if (strcmp(pname, name) == 0) 340 return (gp); 341 } 342 return (NULL); 343 } 344 345 static void 346 g_label_ctl_destroy(struct gctl_req *req, struct g_class *mp) 347 { 348 int *nargs, *force, error, i; 349 struct g_geom *gp; 350 const char *name; 351 char param[16]; 352 353 g_topology_assert(); 354 355 nargs = gctl_get_paraml(req, "nargs", sizeof(*nargs)); 356 if (nargs == NULL) { 357 gctl_error(req, "No '%s' argument", "nargs"); 358 return; 359 } 360 if (*nargs <= 0) { 361 gctl_error(req, "Missing device(s)."); 362 return; 363 } 364 force = gctl_get_paraml(req, "force", sizeof(*force)); 365 if (force == NULL) { 366 gctl_error(req, "No 'force' argument"); 367 return; 368 } 369 370 for (i = 0; i < *nargs; i++) { 371 snprintf(param, sizeof(param), "arg%d", i); 372 name = gctl_get_asciiparam(req, param); 373 if (name == NULL) { 374 gctl_error(req, "No 'arg%d' argument", i); 375 return; 376 } 377 gp = g_label_find_geom(mp, name); 378 if (gp == NULL) { 379 G_LABEL_DEBUG(1, "Label %s is invalid.", name); 380 gctl_error(req, "Label %s is invalid.", name); 381 return; 382 } 383 error = g_label_destroy(gp, *force); 384 if (error != 0) { 385 gctl_error(req, "Cannot destroy label %s (error=%d).", 386 LIST_FIRST(&gp->provider)->name, error); 387 return; 388 } 389 } 390 } 391 392 static void 393 g_label_config(struct gctl_req *req, struct g_class *mp, const char *verb) 394 { 395 uint32_t *version; 396 397 g_topology_assert(); 398 399 version = gctl_get_paraml(req, "version", sizeof(*version)); 400 if (version == NULL) { 401 gctl_error(req, "No '%s' argument.", "version"); 402 return; 403 } 404 if (*version != G_LABEL_VERSION) { 405 gctl_error(req, "Userland and kernel parts are out of sync."); 406 return; 407 } 408 409 if (strcmp(verb, "create") == 0) { 410 g_label_ctl_create(req, mp); 411 return; 412 } else if (strcmp(verb, "destroy") == 0 || 413 strcmp(verb, "stop") == 0) { 414 g_label_ctl_destroy(req, mp); 415 return; 416 } 417 418 gctl_error(req, "Unknown verb."); 419 } 420 421 DECLARE_GEOM_CLASS(g_label_class, g_label); 422