xref: /freebsd/sys/geom/mountver/g_mountver.c (revision 273c26a3c3bea87a241d6879abd4f991db180bf0)
1 /*-
2  * Copyright (c) 2010 Edward Tomasz Napierala <trasz@FreeBSD.org>
3  * Copyright (c) 2004-2006 Pawel Jakub Dawidek <pjd@FreeBSD.org>
4  * All rights reserved.
5  *
6  * Redistribution and use in source and binary forms, with or without
7  * modification, are permitted provided that the following conditions
8  * are met:
9  * 1. Redistributions of source code must retain the above copyright
10  *    notice, this list of conditions and the following disclaimer.
11  * 2. Redistributions in binary form must reproduce the above copyright
12  *    notice, this list of conditions and the following disclaimer in the
13  *    documentation and/or other materials provided with the distribution.
14  *
15  * THIS SOFTWARE IS PROVIDED BY THE AUTHORS AND CONTRIBUTORS ``AS IS'' AND
16  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
17  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
18  * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHORS OR CONTRIBUTORS BE LIABLE
19  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
20  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
21  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
22  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
23  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
24  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
25  * SUCH DAMAGE.
26  */
27 
28 #include <sys/cdefs.h>
29 __FBSDID("$FreeBSD$");
30 
31 #include <sys/param.h>
32 #include <sys/systm.h>
33 #include <sys/kernel.h>
34 #include <sys/module.h>
35 #include <sys/lock.h>
36 #include <sys/mutex.h>
37 #include <sys/bio.h>
38 #include <sys/disk.h>
39 #include <sys/proc.h>
40 #include <sys/sbuf.h>
41 #include <sys/sysctl.h>
42 #include <sys/malloc.h>
43 #include <sys/eventhandler.h>
44 #include <geom/geom.h>
45 #include <geom/mountver/g_mountver.h>
46 
47 
48 SYSCTL_DECL(_kern_geom);
49 static SYSCTL_NODE(_kern_geom, OID_AUTO, mountver, CTLFLAG_RW,
50     0, "GEOM_MOUNTVER stuff");
51 static u_int g_mountver_debug = 0;
52 static u_int g_mountver_check_ident = 1;
53 SYSCTL_UINT(_kern_geom_mountver, OID_AUTO, debug, CTLFLAG_RW,
54     &g_mountver_debug, 0, "Debug level");
55 SYSCTL_UINT(_kern_geom_mountver, OID_AUTO, check_ident, CTLFLAG_RW,
56     &g_mountver_check_ident, 0, "Check disk ident when reattaching");
57 
58 static eventhandler_tag g_mountver_pre_sync = NULL;
59 
60 static void g_mountver_queue(struct bio *bp);
61 static void g_mountver_orphan(struct g_consumer *cp);
62 static void g_mountver_resize(struct g_consumer *cp);
63 static int g_mountver_destroy(struct g_geom *gp, boolean_t force);
64 static g_taste_t g_mountver_taste;
65 static int g_mountver_destroy_geom(struct gctl_req *req, struct g_class *mp,
66     struct g_geom *gp);
67 static void g_mountver_config(struct gctl_req *req, struct g_class *mp,
68     const char *verb);
69 static void g_mountver_dumpconf(struct sbuf *sb, const char *indent,
70     struct g_geom *gp, struct g_consumer *cp, struct g_provider *pp);
71 static void g_mountver_init(struct g_class *mp);
72 static void g_mountver_fini(struct g_class *mp);
73 
74 struct g_class g_mountver_class = {
75 	.name = G_MOUNTVER_CLASS_NAME,
76 	.version = G_VERSION,
77 	.ctlreq = g_mountver_config,
78 	.taste = g_mountver_taste,
79 	.destroy_geom = g_mountver_destroy_geom,
80 	.init = g_mountver_init,
81 	.fini = g_mountver_fini
82 };
83 
84 static void
85 g_mountver_done(struct bio *bp)
86 {
87 	struct g_geom *gp;
88 	struct bio *pbp;
89 
90 	if (bp->bio_error != ENXIO) {
91 		g_std_done(bp);
92 		return;
93 	}
94 
95 	/*
96 	 * When the device goes away, it's possible that few requests
97 	 * will be completed with ENXIO before g_mountver_orphan()
98 	 * gets called.  To work around that, we have to queue requests
99 	 * that failed with ENXIO, in order to send them later.
100 	 */
101 	gp = bp->bio_from->geom;
102 
103 	pbp = bp->bio_parent;
104 	KASSERT(pbp->bio_to == LIST_FIRST(&gp->provider),
105 	    ("parent request was for someone else"));
106 	g_destroy_bio(bp);
107 	pbp->bio_inbed++;
108 	g_mountver_queue(pbp);
109 }
110 
111 static void
112 g_mountver_send(struct bio *bp)
113 {
114 	struct g_geom *gp;
115 	struct bio *cbp;
116 
117 	gp = bp->bio_to->geom;
118 
119 	cbp = g_clone_bio(bp);
120 	if (cbp == NULL) {
121 		g_io_deliver(bp, ENOMEM);
122 		return;
123 	}
124 
125 	cbp->bio_done = g_mountver_done;
126 	g_io_request(cbp, LIST_FIRST(&gp->consumer));
127 }
128 
129 static void
130 g_mountver_queue(struct bio *bp)
131 {
132 	struct g_mountver_softc *sc;
133 	struct g_geom *gp;
134 
135 	gp = bp->bio_to->geom;
136 	sc = gp->softc;
137 
138 	mtx_lock(&sc->sc_mtx);
139 	TAILQ_INSERT_TAIL(&sc->sc_queue, bp, bio_queue);
140 	mtx_unlock(&sc->sc_mtx);
141 }
142 
143 static void
144 g_mountver_send_queued(struct g_geom *gp)
145 {
146 	struct g_mountver_softc *sc;
147 	struct bio *bp;
148 
149 	sc = gp->softc;
150 
151 	mtx_lock(&sc->sc_mtx);
152 	while ((bp = TAILQ_FIRST(&sc->sc_queue)) != NULL) {
153 		TAILQ_REMOVE(&sc->sc_queue, bp, bio_queue);
154 		G_MOUNTVER_LOGREQ(bp, "Sending queued request.");
155 		g_mountver_send(bp);
156 	}
157 	mtx_unlock(&sc->sc_mtx);
158 }
159 
160 static void
161 g_mountver_discard_queued(struct g_geom *gp)
162 {
163 	struct g_mountver_softc *sc;
164 	struct bio *bp;
165 
166 	sc = gp->softc;
167 
168 	mtx_lock(&sc->sc_mtx);
169 	while ((bp = TAILQ_FIRST(&sc->sc_queue)) != NULL) {
170 		TAILQ_REMOVE(&sc->sc_queue, bp, bio_queue);
171 		G_MOUNTVER_LOGREQ(bp, "Discarding queued request.");
172 		g_io_deliver(bp, ENXIO);
173 	}
174 	mtx_unlock(&sc->sc_mtx);
175 }
176 
177 static void
178 g_mountver_start(struct bio *bp)
179 {
180 	struct g_mountver_softc *sc;
181 	struct g_geom *gp;
182 
183 	gp = bp->bio_to->geom;
184 	sc = gp->softc;
185 	G_MOUNTVER_LOGREQ(bp, "Request received.");
186 
187 	/*
188 	 * It is possible that some bios were returned with ENXIO, even though
189 	 * orphaning didn't happen yet.  In that case, queue all subsequent
190 	 * requests in order to maintain ordering.
191 	 */
192 	if (sc->sc_orphaned || !TAILQ_EMPTY(&sc->sc_queue)) {
193 		G_MOUNTVER_LOGREQ(bp, "Queueing request.");
194 		g_mountver_queue(bp);
195 		if (!sc->sc_orphaned)
196 			g_mountver_send_queued(gp);
197 	} else {
198 		G_MOUNTVER_LOGREQ(bp, "Sending request.");
199 		g_mountver_send(bp);
200 	}
201 }
202 
203 static int
204 g_mountver_access(struct g_provider *pp, int dr, int dw, int de)
205 {
206 	struct g_mountver_softc *sc;
207 	struct g_geom *gp;
208 	struct g_consumer *cp;
209 
210 	g_topology_assert();
211 
212 	gp = pp->geom;
213 	cp = LIST_FIRST(&gp->consumer);
214 	sc = gp->softc;
215 	if (sc == NULL && dr <= 0 && dw <= 0 && de <= 0)
216 		return (0);
217 	KASSERT(sc != NULL, ("Trying to access withered provider \"%s\".", pp->name));
218 
219 	sc->sc_access_r += dr;
220 	sc->sc_access_w += dw;
221 	sc->sc_access_e += de;
222 
223 	if (sc->sc_orphaned)
224 		return (0);
225 
226 	return (g_access(cp, dr, dw, de));
227 }
228 
229 static int
230 g_mountver_create(struct gctl_req *req, struct g_class *mp, struct g_provider *pp)
231 {
232 	struct g_mountver_softc *sc;
233 	struct g_geom *gp;
234 	struct g_provider *newpp;
235 	struct g_consumer *cp;
236 	char name[64];
237 	int error;
238 	int identsize = DISK_IDENT_SIZE;
239 
240 	g_topology_assert();
241 
242 	gp = NULL;
243 	newpp = NULL;
244 	cp = NULL;
245 
246 	snprintf(name, sizeof(name), "%s%s", pp->name, G_MOUNTVER_SUFFIX);
247 	LIST_FOREACH(gp, &mp->geom, geom) {
248 		if (strcmp(gp->name, name) == 0) {
249 			gctl_error(req, "Provider %s already exists.", name);
250 			return (EEXIST);
251 		}
252 	}
253 	gp = g_new_geomf(mp, "%s", name);
254 	sc = g_malloc(sizeof(*sc), M_WAITOK | M_ZERO);
255 	mtx_init(&sc->sc_mtx, "gmountver", NULL, MTX_DEF);
256 	TAILQ_INIT(&sc->sc_queue);
257 	sc->sc_provider_name = strdup(pp->name, M_GEOM);
258 	gp->softc = sc;
259 	gp->start = g_mountver_start;
260 	gp->orphan = g_mountver_orphan;
261 	gp->resize = g_mountver_resize;
262 	gp->access = g_mountver_access;
263 	gp->dumpconf = g_mountver_dumpconf;
264 
265 	newpp = g_new_providerf(gp, "%s", gp->name);
266 	newpp->mediasize = pp->mediasize;
267 	newpp->sectorsize = pp->sectorsize;
268 
269 	cp = g_new_consumer(gp);
270 	error = g_attach(cp, pp);
271 	if (error != 0) {
272 		gctl_error(req, "Cannot attach to provider %s.", pp->name);
273 		goto fail;
274 	}
275 	error = g_access(cp, 1, 0, 0);
276 	if (error != 0) {
277 		gctl_error(req, "Cannot access provider %s.", pp->name);
278 		goto fail;
279 	}
280 	error = g_io_getattr("GEOM::ident", cp, &identsize, sc->sc_ident);
281 	g_access(cp, -1, 0, 0);
282 	if (error != 0) {
283 		if (g_mountver_check_ident) {
284 			gctl_error(req, "Cannot get disk ident from %s; error = %d.", pp->name, error);
285 			goto fail;
286 		}
287 
288 		G_MOUNTVER_DEBUG(0, "Cannot get disk ident from %s; error = %d.", pp->name, error);
289 		sc->sc_ident[0] = '\0';
290 	}
291 
292 	g_error_provider(newpp, 0);
293 	G_MOUNTVER_DEBUG(0, "Device %s created.", gp->name);
294 	return (0);
295 fail:
296 	g_free(sc->sc_provider_name);
297 	if (cp->provider != NULL)
298 		g_detach(cp);
299 	g_destroy_consumer(cp);
300 	g_destroy_provider(newpp);
301 	g_free(gp->softc);
302 	g_destroy_geom(gp);
303 	return (error);
304 }
305 
306 static int
307 g_mountver_destroy(struct g_geom *gp, boolean_t force)
308 {
309 	struct g_mountver_softc *sc;
310 	struct g_provider *pp;
311 
312 	g_topology_assert();
313 	if (gp->softc == NULL)
314 		return (ENXIO);
315 	sc = gp->softc;
316 	pp = LIST_FIRST(&gp->provider);
317 	if (pp != NULL && (pp->acr != 0 || pp->acw != 0 || pp->ace != 0)) {
318 		if (force) {
319 			G_MOUNTVER_DEBUG(0, "Device %s is still open, so it "
320 			    "can't be definitely removed.", pp->name);
321 		} else {
322 			G_MOUNTVER_DEBUG(1, "Device %s is still open (r%dw%de%d).",
323 			    pp->name, pp->acr, pp->acw, pp->ace);
324 			return (EBUSY);
325 		}
326 	} else {
327 		G_MOUNTVER_DEBUG(0, "Device %s removed.", gp->name);
328 	}
329 	if (pp != NULL)
330 		g_wither_provider(pp, ENXIO);
331 	g_mountver_discard_queued(gp);
332 	g_free(sc->sc_provider_name);
333 	g_free(gp->softc);
334 	gp->softc = NULL;
335 	g_wither_geom(gp, ENXIO);
336 
337 	return (0);
338 }
339 
340 static int
341 g_mountver_destroy_geom(struct gctl_req *req, struct g_class *mp, struct g_geom *gp)
342 {
343 
344 	return (g_mountver_destroy(gp, 0));
345 }
346 
347 static void
348 g_mountver_ctl_create(struct gctl_req *req, struct g_class *mp)
349 {
350 	struct g_provider *pp;
351 	const char *name;
352 	char param[16];
353 	int i, *nargs;
354 
355 	g_topology_assert();
356 
357 	nargs = gctl_get_paraml(req, "nargs", sizeof(*nargs));
358 	if (nargs == NULL) {
359 		gctl_error(req, "No '%s' argument", "nargs");
360 		return;
361 	}
362 	if (*nargs <= 0) {
363 		gctl_error(req, "Missing device(s).");
364 		return;
365 	}
366 	for (i = 0; i < *nargs; i++) {
367 		snprintf(param, sizeof(param), "arg%d", i);
368 		name = gctl_get_asciiparam(req, param);
369 		if (name == NULL) {
370 			gctl_error(req, "No 'arg%d' argument", i);
371 			return;
372 		}
373 		if (strncmp(name, "/dev/", strlen("/dev/")) == 0)
374 			name += strlen("/dev/");
375 		pp = g_provider_by_name(name);
376 		if (pp == NULL) {
377 			G_MOUNTVER_DEBUG(1, "Provider %s is invalid.", name);
378 			gctl_error(req, "Provider %s is invalid.", name);
379 			return;
380 		}
381 		if (g_mountver_create(req, mp, pp) != 0)
382 			return;
383 	}
384 }
385 
386 static struct g_geom *
387 g_mountver_find_geom(struct g_class *mp, const char *name)
388 {
389 	struct g_geom *gp;
390 
391 	LIST_FOREACH(gp, &mp->geom, geom) {
392 		if (strcmp(gp->name, name) == 0)
393 			return (gp);
394 	}
395 	return (NULL);
396 }
397 
398 static void
399 g_mountver_ctl_destroy(struct gctl_req *req, struct g_class *mp)
400 {
401 	int *nargs, *force, error, i;
402 	struct g_geom *gp;
403 	const char *name;
404 	char param[16];
405 
406 	g_topology_assert();
407 
408 	nargs = gctl_get_paraml(req, "nargs", sizeof(*nargs));
409 	if (nargs == NULL) {
410 		gctl_error(req, "No '%s' argument", "nargs");
411 		return;
412 	}
413 	if (*nargs <= 0) {
414 		gctl_error(req, "Missing device(s).");
415 		return;
416 	}
417 	force = gctl_get_paraml(req, "force", sizeof(*force));
418 	if (force == NULL) {
419 		gctl_error(req, "No 'force' argument");
420 		return;
421 	}
422 
423 	for (i = 0; i < *nargs; i++) {
424 		snprintf(param, sizeof(param), "arg%d", i);
425 		name = gctl_get_asciiparam(req, param);
426 		if (name == NULL) {
427 			gctl_error(req, "No 'arg%d' argument", i);
428 			return;
429 		}
430 		if (strncmp(name, "/dev/", strlen("/dev/")) == 0)
431 			name += strlen("/dev/");
432 		gp = g_mountver_find_geom(mp, name);
433 		if (gp == NULL) {
434 			G_MOUNTVER_DEBUG(1, "Device %s is invalid.", name);
435 			gctl_error(req, "Device %s is invalid.", name);
436 			return;
437 		}
438 		error = g_mountver_destroy(gp, *force);
439 		if (error != 0) {
440 			gctl_error(req, "Cannot destroy device %s (error=%d).",
441 			    gp->name, error);
442 			return;
443 		}
444 	}
445 }
446 
447 static void
448 g_mountver_orphan(struct g_consumer *cp)
449 {
450 	struct g_mountver_softc *sc;
451 
452 	g_topology_assert();
453 
454 	sc = cp->geom->softc;
455 	sc->sc_orphaned = 1;
456 	if (cp->acr > 0 || cp->acw > 0 || cp->ace > 0)
457 		g_access(cp, -cp->acr, -cp->acw, -cp->ace);
458 	g_detach(cp);
459 	G_MOUNTVER_DEBUG(0, "%s is offline.  Mount verification in progress.", sc->sc_provider_name);
460 }
461 
462 static void
463 g_mountver_resize(struct g_consumer *cp)
464 {
465 	struct g_geom *gp;
466 	struct g_provider *pp;
467 
468 	gp = cp->geom;
469 
470 	LIST_FOREACH(pp, &gp->provider, provider)
471 		g_resize_provider(pp, cp->provider->mediasize);
472 }
473 
474 static int
475 g_mountver_ident_matches(struct g_geom *gp)
476 {
477 	struct g_consumer *cp;
478 	struct g_mountver_softc *sc;
479 	char ident[DISK_IDENT_SIZE];
480 	int error, identsize = DISK_IDENT_SIZE;
481 
482 	sc = gp->softc;
483 	cp = LIST_FIRST(&gp->consumer);
484 
485 	if (g_mountver_check_ident == 0)
486 		return (0);
487 
488 	error = g_access(cp, 1, 0, 0);
489 	if (error != 0) {
490 		G_MOUNTVER_DEBUG(0, "Cannot access %s; "
491 		    "not attaching; error = %d.", gp->name, error);
492 		return (1);
493 	}
494 	error = g_io_getattr("GEOM::ident", cp, &identsize, ident);
495 	g_access(cp, -1, 0, 0);
496 	if (error != 0) {
497 		G_MOUNTVER_DEBUG(0, "Cannot get disk ident for %s; "
498 		    "not attaching; error = %d.", gp->name, error);
499 		return (1);
500 	}
501 	if (strcmp(ident, sc->sc_ident) != 0) {
502 		G_MOUNTVER_DEBUG(1, "Disk ident for %s (\"%s\") is different "
503 		    "from expected \"%s\", not attaching.", gp->name, ident,
504 		    sc->sc_ident);
505 		return (1);
506 	}
507 
508 	return (0);
509 }
510 
511 static struct g_geom *
512 g_mountver_taste(struct g_class *mp, struct g_provider *pp, int flags __unused)
513 {
514 	struct g_mountver_softc *sc;
515 	struct g_consumer *cp;
516 	struct g_geom *gp;
517 	int error;
518 
519 	g_topology_assert();
520 	g_trace(G_T_TOPOLOGY, "%s(%s, %s)", __func__, mp->name, pp->name);
521 	G_MOUNTVER_DEBUG(2, "Tasting %s.", pp->name);
522 
523 	/*
524 	 * Let's check if device already exists.
525 	 */
526 	LIST_FOREACH(gp, &mp->geom, geom) {
527 		sc = gp->softc;
528 		if (sc == NULL)
529 			continue;
530 
531 		/* Already attached? */
532 		if (pp == LIST_FIRST(&gp->provider))
533 			return (NULL);
534 
535 		if (sc->sc_orphaned && strcmp(pp->name, sc->sc_provider_name) == 0)
536 			break;
537 	}
538 	if (gp == NULL)
539 		return (NULL);
540 
541 	cp = LIST_FIRST(&gp->consumer);
542 	g_attach(cp, pp);
543 	error = g_mountver_ident_matches(gp);
544 	if (error != 0) {
545 		g_detach(cp);
546 		return (NULL);
547 	}
548 	if (sc->sc_access_r > 0 || sc->sc_access_w > 0 || sc->sc_access_e > 0) {
549 		error = g_access(cp, sc->sc_access_r, sc->sc_access_w, sc->sc_access_e);
550 		if (error != 0) {
551 			G_MOUNTVER_DEBUG(0, "Cannot access %s; error = %d.", pp->name, error);
552 			g_detach(cp);
553 			return (NULL);
554 		}
555 	}
556 	g_mountver_send_queued(gp);
557 	sc->sc_orphaned = 0;
558 	G_MOUNTVER_DEBUG(0, "%s has completed mount verification.", sc->sc_provider_name);
559 
560 	return (gp);
561 }
562 
563 static void
564 g_mountver_config(struct gctl_req *req, struct g_class *mp, const char *verb)
565 {
566 	uint32_t *version;
567 
568 	g_topology_assert();
569 
570 	version = gctl_get_paraml(req, "version", sizeof(*version));
571 	if (version == NULL) {
572 		gctl_error(req, "No '%s' argument.", "version");
573 		return;
574 	}
575 	if (*version != G_MOUNTVER_VERSION) {
576 		gctl_error(req, "Userland and kernel parts are out of sync.");
577 		return;
578 	}
579 
580 	if (strcmp(verb, "create") == 0) {
581 		g_mountver_ctl_create(req, mp);
582 		return;
583 	} else if (strcmp(verb, "destroy") == 0) {
584 		g_mountver_ctl_destroy(req, mp);
585 		return;
586 	}
587 
588 	gctl_error(req, "Unknown verb.");
589 }
590 
591 static void
592 g_mountver_dumpconf(struct sbuf *sb, const char *indent, struct g_geom *gp,
593     struct g_consumer *cp, struct g_provider *pp)
594 {
595 	struct g_mountver_softc *sc;
596 
597 	if (pp != NULL || cp != NULL)
598 		return;
599 
600 	sc = gp->softc;
601 	sbuf_printf(sb, "%s<State>%s</State>\n", indent,
602 	    sc->sc_orphaned ? "OFFLINE" : "ONLINE");
603 	sbuf_printf(sb, "%s<Provider-Name>%s</Provider-Name>\n", indent, sc->sc_provider_name);
604 	sbuf_printf(sb, "%s<Disk-Ident>%s</Disk-Ident>\n", indent, sc->sc_ident);
605 }
606 
607 static void
608 g_mountver_shutdown_pre_sync(void *arg, int howto)
609 {
610 	struct g_class *mp;
611 	struct g_geom *gp, *gp2;
612 
613 	mp = arg;
614 	g_topology_lock();
615 	LIST_FOREACH_SAFE(gp, &mp->geom, geom, gp2)
616 		g_mountver_destroy(gp, 1);
617 	g_topology_unlock();
618 }
619 
620 static void
621 g_mountver_init(struct g_class *mp)
622 {
623 
624 	g_mountver_pre_sync = EVENTHANDLER_REGISTER(shutdown_pre_sync,
625 	    g_mountver_shutdown_pre_sync, mp, SHUTDOWN_PRI_FIRST);
626 	if (g_mountver_pre_sync == NULL)
627 		G_MOUNTVER_DEBUG(0, "Warning! Cannot register shutdown event.");
628 }
629 
630 static void
631 g_mountver_fini(struct g_class *mp)
632 {
633 
634 	if (g_mountver_pre_sync != NULL)
635 		EVENTHANDLER_DEREGISTER(shutdown_pre_sync, g_mountver_pre_sync);
636 }
637 
638 DECLARE_GEOM_CLASS(g_mountver_class, g_mountver);
639