g_mountver.c revision 221451
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 221451 2011-05-04 18:17:21Z ae $");
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	sc = g_malloc(sizeof(*sc), M_WAITOK | M_ZERO);
252	mtx_init(&sc->sc_mtx, "gmountver", NULL, MTX_DEF);
253	TAILQ_INIT(&sc->sc_queue);
254	sc->sc_provider_name = strdup(pp->name, M_GEOM);
255	gp->softc = sc;
256	gp->start = g_mountver_start;
257	gp->orphan = g_mountver_orphan;
258	gp->access = g_mountver_access;
259	gp->dumpconf = g_mountver_dumpconf;
260
261	newpp = g_new_providerf(gp, gp->name);
262	newpp->mediasize = pp->mediasize;
263	newpp->sectorsize = pp->sectorsize;
264
265	cp = g_new_consumer(gp);
266	error = g_attach(cp, pp);
267	if (error != 0) {
268		gctl_error(req, "Cannot attach to provider %s.", pp->name);
269		goto fail;
270	}
271	error = g_access(cp, 1, 0, 0);
272	if (error != 0) {
273		gctl_error(req, "Cannot access provider %s.", pp->name);
274		goto fail;
275	}
276	error = g_io_getattr("GEOM::ident", cp, &identsize, sc->sc_ident);
277	g_access(cp, -1, 0, 0);
278	if (error != 0) {
279		if (g_mountver_check_ident) {
280			gctl_error(req, "Cannot get disk ident from %s; error = %d.", pp->name, error);
281			goto fail;
282		}
283
284		G_MOUNTVER_DEBUG(0, "Cannot get disk ident from %s; error = %d.", pp->name, error);
285		sc->sc_ident[0] = '\0';
286	}
287
288	g_error_provider(newpp, 0);
289	G_MOUNTVER_DEBUG(0, "Device %s created.", gp->name);
290	return (0);
291fail:
292	g_free(sc->sc_provider_name);
293	if (cp->provider != NULL)
294		g_detach(cp);
295	g_destroy_consumer(cp);
296	g_destroy_provider(newpp);
297	g_free(gp->softc);
298	g_destroy_geom(gp);
299	return (error);
300}
301
302static int
303g_mountver_destroy(struct g_geom *gp, boolean_t force)
304{
305	struct g_mountver_softc *sc;
306	struct g_provider *pp;
307
308	g_topology_assert();
309	if (gp->softc == NULL)
310		return (ENXIO);
311	sc = gp->softc;
312	pp = LIST_FIRST(&gp->provider);
313	if (pp != NULL && (pp->acr != 0 || pp->acw != 0 || pp->ace != 0)) {
314		if (force) {
315			G_MOUNTVER_DEBUG(0, "Device %s is still open, so it "
316			    "can't be definitely removed.", pp->name);
317		} else {
318			G_MOUNTVER_DEBUG(1, "Device %s is still open (r%dw%de%d).",
319			    pp->name, pp->acr, pp->acw, pp->ace);
320			return (EBUSY);
321		}
322	} else {
323		G_MOUNTVER_DEBUG(0, "Device %s removed.", gp->name);
324	}
325	if (pp != NULL)
326		g_orphan_provider(pp, ENXIO);
327	g_mountver_discard_queued(gp);
328	g_free(sc->sc_provider_name);
329	g_free(gp->softc);
330	gp->softc = NULL;
331	g_wither_geom(gp, ENXIO);
332
333	return (0);
334}
335
336static int
337g_mountver_destroy_geom(struct gctl_req *req, struct g_class *mp, struct g_geom *gp)
338{
339
340	return (g_mountver_destroy(gp, 0));
341}
342
343static void
344g_mountver_ctl_create(struct gctl_req *req, struct g_class *mp)
345{
346	struct g_provider *pp;
347	const char *name;
348	char param[16];
349	int i, *nargs;
350
351	g_topology_assert();
352
353	nargs = gctl_get_paraml(req, "nargs", sizeof(*nargs));
354	if (nargs == NULL) {
355		gctl_error(req, "No '%s' argument", "nargs");
356		return;
357	}
358	if (*nargs <= 0) {
359		gctl_error(req, "Missing device(s).");
360		return;
361	}
362	for (i = 0; i < *nargs; i++) {
363		snprintf(param, sizeof(param), "arg%d", i);
364		name = gctl_get_asciiparam(req, param);
365		if (name == NULL) {
366			gctl_error(req, "No 'arg%d' argument", i);
367			return;
368		}
369		if (strncmp(name, "/dev/", strlen("/dev/")) == 0)
370			name += strlen("/dev/");
371		pp = g_provider_by_name(name);
372		if (pp == NULL) {
373			G_MOUNTVER_DEBUG(1, "Provider %s is invalid.", name);
374			gctl_error(req, "Provider %s is invalid.", name);
375			return;
376		}
377		if (g_mountver_create(req, mp, pp) != 0)
378			return;
379	}
380}
381
382static struct g_geom *
383g_mountver_find_geom(struct g_class *mp, const char *name)
384{
385	struct g_geom *gp;
386
387	LIST_FOREACH(gp, &mp->geom, geom) {
388		if (strcmp(gp->name, name) == 0)
389			return (gp);
390	}
391	return (NULL);
392}
393
394static void
395g_mountver_ctl_destroy(struct gctl_req *req, struct g_class *mp)
396{
397	int *nargs, *force, error, i;
398	struct g_geom *gp;
399	const char *name;
400	char param[16];
401
402	g_topology_assert();
403
404	nargs = gctl_get_paraml(req, "nargs", sizeof(*nargs));
405	if (nargs == NULL) {
406		gctl_error(req, "No '%s' argument", "nargs");
407		return;
408	}
409	if (*nargs <= 0) {
410		gctl_error(req, "Missing device(s).");
411		return;
412	}
413	force = gctl_get_paraml(req, "force", sizeof(*force));
414	if (force == NULL) {
415		gctl_error(req, "No 'force' argument");
416		return;
417	}
418
419	for (i = 0; i < *nargs; i++) {
420		snprintf(param, sizeof(param), "arg%d", i);
421		name = gctl_get_asciiparam(req, param);
422		if (name == NULL) {
423			gctl_error(req, "No 'arg%d' argument", i);
424			return;
425		}
426		if (strncmp(name, "/dev/", strlen("/dev/")) == 0)
427			name += strlen("/dev/");
428		gp = g_mountver_find_geom(mp, name);
429		if (gp == NULL) {
430			G_MOUNTVER_DEBUG(1, "Device %s is invalid.", name);
431			gctl_error(req, "Device %s is invalid.", name);
432			return;
433		}
434		error = g_mountver_destroy(gp, *force);
435		if (error != 0) {
436			gctl_error(req, "Cannot destroy device %s (error=%d).",
437			    gp->name, error);
438			return;
439		}
440	}
441}
442
443static void
444g_mountver_orphan(struct g_consumer *cp)
445{
446	struct g_mountver_softc *sc;
447
448	g_topology_assert();
449
450	sc = cp->geom->softc;
451	sc->sc_orphaned = 1;
452	if (cp->acr > 0 || cp->acw > 0 || cp->ace > 0)
453		g_access(cp, -cp->acr, -cp->acw, -cp->ace);
454	g_detach(cp);
455	G_MOUNTVER_DEBUG(0, "%s is offline.  Mount verification in progress.", sc->sc_provider_name);
456}
457
458static int
459g_mountver_ident_matches(struct g_geom *gp)
460{
461	struct g_consumer *cp;
462	struct g_mountver_softc *sc;
463	char ident[DISK_IDENT_SIZE];
464	int error, identsize = DISK_IDENT_SIZE;
465
466	sc = gp->softc;
467	cp = LIST_FIRST(&gp->consumer);
468
469	if (g_mountver_check_ident == 0)
470		return (0);
471
472	error = g_access(cp, 1, 0, 0);
473	if (error != 0) {
474		G_MOUNTVER_DEBUG(0, "Cannot access %s; "
475		    "not attaching; error = %d.", gp->name, error);
476		return (1);
477	}
478	error = g_io_getattr("GEOM::ident", cp, &identsize, ident);
479	g_access(cp, -1, 0, 0);
480	if (error != 0) {
481		G_MOUNTVER_DEBUG(0, "Cannot get disk ident for %s; "
482		    "not attaching; error = %d.", gp->name, error);
483		return (1);
484	}
485	if (strcmp(ident, sc->sc_ident) != 0) {
486		G_MOUNTVER_DEBUG(1, "Disk ident for %s (\"%s\") is different "
487		    "from expected \"%s\", not attaching.", gp->name, ident,
488		    sc->sc_ident);
489		return (1);
490	}
491
492	return (0);
493}
494
495static struct g_geom *
496g_mountver_taste(struct g_class *mp, struct g_provider *pp, int flags __unused)
497{
498	struct g_mountver_softc *sc;
499	struct g_consumer *cp;
500	struct g_geom *gp;
501	int error;
502
503	g_topology_assert();
504	g_trace(G_T_TOPOLOGY, "%s(%s, %s)", __func__, mp->name, pp->name);
505	G_MOUNTVER_DEBUG(2, "Tasting %s.", pp->name);
506
507	/*
508	 * Let's check if device already exists.
509	 */
510	LIST_FOREACH(gp, &mp->geom, geom) {
511		sc = gp->softc;
512		if (sc == NULL)
513			continue;
514
515		/* Already attached? */
516		if (pp == LIST_FIRST(&gp->provider))
517			return (NULL);
518
519		if (sc->sc_orphaned && strcmp(pp->name, sc->sc_provider_name) == 0)
520			break;
521	}
522	if (gp == NULL)
523		return (NULL);
524
525	cp = LIST_FIRST(&gp->consumer);
526	g_attach(cp, pp);
527	error = g_mountver_ident_matches(gp);
528	if (error != 0) {
529		g_detach(cp);
530		return (NULL);
531	}
532	if (sc->sc_access_r > 0 || sc->sc_access_w > 0 || sc->sc_access_e > 0) {
533		error = g_access(cp, sc->sc_access_r, sc->sc_access_w, sc->sc_access_e);
534		if (error != 0) {
535			G_MOUNTVER_DEBUG(0, "Cannot access %s; error = %d.", pp->name, error);
536			g_detach(cp);
537			return (NULL);
538		}
539	}
540	g_mountver_send_queued(gp);
541	sc->sc_orphaned = 0;
542	G_MOUNTVER_DEBUG(0, "%s has completed mount verification.", sc->sc_provider_name);
543
544	return (gp);
545}
546
547static void
548g_mountver_config(struct gctl_req *req, struct g_class *mp, const char *verb)
549{
550	uint32_t *version;
551
552	g_topology_assert();
553
554	version = gctl_get_paraml(req, "version", sizeof(*version));
555	if (version == NULL) {
556		gctl_error(req, "No '%s' argument.", "version");
557		return;
558	}
559	if (*version != G_MOUNTVER_VERSION) {
560		gctl_error(req, "Userland and kernel parts are out of sync.");
561		return;
562	}
563
564	if (strcmp(verb, "create") == 0) {
565		g_mountver_ctl_create(req, mp);
566		return;
567	} else if (strcmp(verb, "destroy") == 0) {
568		g_mountver_ctl_destroy(req, mp);
569		return;
570	}
571
572	gctl_error(req, "Unknown verb.");
573}
574
575static void
576g_mountver_dumpconf(struct sbuf *sb, const char *indent, struct g_geom *gp,
577    struct g_consumer *cp, struct g_provider *pp)
578{
579	struct g_mountver_softc *sc;
580
581	if (pp != NULL || cp != NULL)
582		return;
583
584	sc = gp->softc;
585	sbuf_printf(sb, "%s<State>%s</State>\n", indent,
586	    sc->sc_orphaned ? "OFFLINE" : "ONLINE");
587	sbuf_printf(sb, "%s<Provider-Name>%s</Provider-Name>\n", indent, sc->sc_provider_name);
588	sbuf_printf(sb, "%s<Disk-Ident>%s</Disk-Ident>\n", indent, sc->sc_ident);
589}
590
591static void
592g_mountver_shutdown_pre_sync(void *arg, int howto)
593{
594	struct g_class *mp;
595	struct g_geom *gp, *gp2;
596
597	mp = arg;
598	DROP_GIANT();
599	g_topology_lock();
600	LIST_FOREACH_SAFE(gp, &mp->geom, geom, gp2)
601		g_mountver_destroy(gp, 1);
602	g_topology_unlock();
603	PICKUP_GIANT();
604}
605
606static void
607g_mountver_init(struct g_class *mp)
608{
609
610	g_mountver_pre_sync = EVENTHANDLER_REGISTER(shutdown_pre_sync,
611	    g_mountver_shutdown_pre_sync, mp, SHUTDOWN_PRI_FIRST);
612	if (g_mountver_pre_sync == NULL)
613		G_MOUNTVER_DEBUG(0, "Warning! Cannot register shutdown event.");
614}
615
616static void
617g_mountver_fini(struct g_class *mp)
618{
619
620	if (g_mountver_pre_sync != NULL)
621		EVENTHANDLER_DEREGISTER(shutdown_pre_sync, g_mountver_pre_sync);
622}
623
624DECLARE_GEOM_CLASS(g_mountver_class, g_mountver);
625