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