g_mirror_ctl.c revision 132908
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: head/sys/geom/mirror/g_mirror_ctl.c 132908 2004-07-31 00:51:33Z pjd $");
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 <sys/bitstring.h>
40#include <vm/uma.h>
41#include <machine/atomic.h>
42#include <geom/geom.h>
43#include <sys/proc.h>
44#include <sys/kthread.h>
45#include <geom/mirror/g_mirror.h>
46
47
48static struct g_mirror_softc *
49g_mirror_find_device(struct g_class *mp, const char *name)
50{
51	struct g_mirror_softc *sc;
52	struct g_geom *gp;
53
54	g_topology_assert();
55	LIST_FOREACH(gp, &mp->geom, geom) {
56		sc = gp->softc;
57		if (sc == NULL)
58			continue;
59		if ((sc->sc_flags & G_MIRROR_DEVICE_FLAG_DESTROY) != 0)
60			continue;
61		if (strcmp(gp->name, name) == 0 ||
62		    strcmp(sc->sc_name, name) == 0) {
63			return (sc);
64		}
65	}
66	return (NULL);
67}
68
69static struct g_mirror_disk *
70g_mirror_find_disk(struct g_mirror_softc *sc, const char *name)
71{
72	struct g_mirror_disk *disk;
73
74	g_topology_assert();
75	LIST_FOREACH(disk, &sc->sc_disks, d_next) {
76		if (disk->d_consumer == NULL)
77			continue;
78		if (disk->d_consumer->provider == NULL)
79			continue;
80		if (strcmp(disk->d_consumer->provider->name, name) == 0)
81			return (disk);
82	}
83	return (NULL);
84}
85
86static void
87g_mirror_ctl_configure(struct gctl_req *req, struct g_class *mp)
88{
89	struct g_mirror_softc *sc;
90	struct g_mirror_disk *disk;
91	const char *name, *balancep;
92	intmax_t *slicep;
93	uint32_t slice;
94	uint8_t balance;
95	int *nargs, *autosync, *noautosync, do_sync = 0;
96
97	g_topology_assert();
98	nargs = gctl_get_paraml(req, "nargs", sizeof(*nargs));
99	if (*nargs != 1) {
100		gctl_error(req, "Invalid number of arguments.");
101		return;
102	}
103	name = gctl_get_asciiparam(req, "arg0");
104	sc = g_mirror_find_device(mp, name);
105	if (sc == NULL) {
106		gctl_error(req, "No such device: %s.", name);
107		return;
108	}
109	if (g_mirror_ndisks(sc, -1) < sc->sc_ndisks) {
110		gctl_error(req, "Not all disks connected.");
111		return;
112	}
113	balancep = gctl_get_asciiparam(req, "balance");
114	if (strcmp(balancep, "none") == 0)
115		balance = sc->sc_balance;
116	else {
117		if (balance_id(balancep) == -1) {
118			gctl_error(req, "Invalid balance algorithm.");
119			return;
120		}
121		balance = balance_id(balancep);
122	}
123	slicep = gctl_get_paraml(req, "slice", sizeof(*slicep));
124	if (slicep == NULL) {
125		gctl_error(req, "No '%s' argument.", "slice");
126		return;
127	}
128	if (*slicep == -1)
129		slice = sc->sc_slice;
130	else
131		slice = *slicep;
132	autosync = gctl_get_paraml(req, "autosync", sizeof(*autosync));
133	if (autosync == NULL) {
134		gctl_error(req, "No '%s' argument.", "autosync");
135		return;
136	}
137	noautosync = gctl_get_paraml(req, "noautosync", sizeof(*noautosync));
138	if (noautosync == NULL) {
139		gctl_error(req, "No '%s' argument.", "noautosync");
140		return;
141	}
142	if (sc->sc_balance == balance && sc->sc_slice == slice && !*autosync &&
143	    !*noautosync) {
144		gctl_error(req, "Nothing has changed.");
145		return;
146	}
147	if (*autosync && *noautosync) {
148		gctl_error(req, "'%s' and '%s' specified.", "autosync",
149		    "noautosync");
150		return;
151	}
152	sc->sc_balance = balance;
153	sc->sc_slice = slice;
154	if ((sc->sc_flags & G_MIRROR_DEVICE_FLAG_NOAUTOSYNC) != 0) {
155		if (*autosync) {
156			sc->sc_flags &= ~G_MIRROR_DEVICE_FLAG_NOAUTOSYNC;
157			do_sync = 1;
158		}
159	} else {
160		if (*noautosync)
161			sc->sc_flags |= G_MIRROR_DEVICE_FLAG_NOAUTOSYNC;
162	}
163	LIST_FOREACH(disk, &sc->sc_disks, d_next) {
164		if (do_sync) {
165			if (disk->d_state == G_MIRROR_DISK_STATE_SYNCHRONIZING)
166				disk->d_flags &= ~G_MIRROR_DISK_FLAG_FORCE_SYNC;
167		}
168		g_mirror_update_metadata(disk);
169		if (do_sync) {
170			if (disk->d_state == G_MIRROR_DISK_STATE_STALE) {
171				g_mirror_event_send(disk,
172				    G_MIRROR_DISK_STATE_DISCONNECTED,
173				    G_MIRROR_EVENT_DONTWAIT);
174			}
175		}
176	}
177}
178
179static void
180g_mirror_ctl_rebuild(struct gctl_req *req, struct g_class *mp)
181{
182	struct g_mirror_softc *sc;
183	struct g_mirror_disk *disk;
184	const char *name;
185	char param[16];
186	int *nargs;
187	u_int i;
188
189	g_topology_assert();
190	nargs = gctl_get_paraml(req, "nargs", sizeof(*nargs));
191	if (nargs == NULL) {
192		gctl_error(req, "No '%s' argument.", "nargs");
193		return;
194	}
195	if (*nargs < 2) {
196		gctl_error(req, "Too few arguments.");
197		return;
198	}
199	name = gctl_get_asciiparam(req, "arg0");
200	if (name == NULL) {
201		gctl_error(req, "No 'arg%u' argument.", 0);
202		return;
203	}
204	sc = g_mirror_find_device(mp, name);
205	if (sc == NULL) {
206		gctl_error(req, "No such device: %s.", name);
207		return;
208	}
209
210	for (i = 1; i < (u_int)*nargs; i++) {
211		snprintf(param, sizeof(param), "arg%u", i);
212		name = gctl_get_asciiparam(req, param);
213		if (name == NULL) {
214			gctl_error(req, "No 'arg%u' argument.", i);
215			return;
216		}
217		disk = g_mirror_find_disk(sc, name);
218		if (disk == NULL) {
219			gctl_error(req, "No such provider: %s.", name);
220			return;
221		}
222		if (g_mirror_ndisks(sc, G_MIRROR_DISK_STATE_ACTIVE) == 1 &&
223		    disk->d_state == G_MIRROR_DISK_STATE_ACTIVE) {
224			/*
225			 * This is the last active disk. There will be nothing
226			 * to rebuild it from, so deny this request.
227			 */
228			gctl_error(req,
229			    "Provider %s is the last active provider in %s.",
230			    name, sc->sc_geom->name);
231			return;
232		}
233		/*
234		 * Do rebuild by resetting syncid and disconnecting disk.
235		 * It'll be retasted, connected to the mirror and
236		 * synchronized.
237		 */
238		disk->d_sync.ds_syncid = 0;
239		if ((sc->sc_flags & G_MIRROR_DEVICE_FLAG_NOAUTOSYNC) != 0)
240			disk->d_flags |= G_MIRROR_DISK_FLAG_FORCE_SYNC;
241		g_mirror_update_metadata(disk);
242		g_mirror_event_send(disk, G_MIRROR_DISK_STATE_DISCONNECTED,
243		    G_MIRROR_EVENT_WAIT);
244	}
245}
246
247static void
248g_mirror_ctl_insert(struct gctl_req *req, struct g_class *mp)
249{
250	struct g_mirror_softc *sc;
251	struct g_mirror_disk *disk;
252	struct g_mirror_metadata md;
253	struct g_provider *pp;
254	struct g_consumer *cp;
255	const char *name;
256	char param[16];
257	u_char *sector;
258	u_int i, n;
259	int error, *nargs, *inactive;
260	struct {
261		struct g_provider	*provider;
262		struct g_consumer	*consumer;
263	} *disks;
264
265	g_topology_assert();
266	nargs = gctl_get_paraml(req, "nargs", sizeof(*nargs));
267	if (nargs == NULL) {
268		gctl_error(req, "No '%s' argument.", "nargs");
269		return;
270	}
271	if (*nargs < 2) {
272		gctl_error(req, "Too few arguments.");
273		return;
274	}
275	inactive = gctl_get_paraml(req, "inactive", sizeof(*inactive));
276	if (inactive == NULL) {
277		gctl_error(req, "No '%s' argument.", "inactive");
278		return;
279	}
280	name = gctl_get_asciiparam(req, "arg0");
281	if (name == NULL) {
282		gctl_error(req, "No 'arg%u' argument.", 0);
283		return;
284	}
285	sc = g_mirror_find_device(mp, name);
286	if (sc == NULL) {
287		gctl_error(req, "No such device: %s.", name);
288		return;
289	}
290	if (g_mirror_ndisks(sc, -1) < sc->sc_ndisks) {
291		gctl_error(req, "Not all disks connected.");
292		return;
293	}
294
295	disks = g_malloc(sizeof(*disks) * (*nargs), M_WAITOK | M_ZERO);
296	for (i = 1, n = 0; i < (u_int)*nargs; i++) {
297		snprintf(param, sizeof(param), "arg%u", i);
298		name = gctl_get_asciiparam(req, param);
299		if (name == NULL) {
300			gctl_error(req, "No 'arg%u' argument.", i);
301			continue;
302		}
303		if (strncmp(name, "/dev/", strlen("/dev/")) == 0)
304			name += strlen("/dev/");
305		if (g_mirror_find_disk(sc, name) != NULL) {
306			gctl_error(req, "Provider %s already inserted.", name);
307			continue;
308		}
309		pp = g_provider_by_name(name);
310		if (pp == NULL) {
311			gctl_error(req, "Unknown provider %s.", name);
312			continue;
313		}
314		if (sc->sc_provider->mediasize > pp->mediasize) {
315			gctl_error(req, "Provider %s too small.", name);
316			continue;
317		}
318		if ((sc->sc_provider->sectorsize % pp->sectorsize) != 0) {
319			gctl_error(req, "Invalid sectorsize of provider %s.",
320			    name);
321			continue;
322		}
323		cp = g_new_consumer(sc->sc_geom);
324		if (g_attach(cp, pp) != 0) {
325			g_destroy_consumer(cp);
326			gctl_error(req, "Cannot attach to provider %s.", name);
327			continue;
328		}
329		if (g_access(cp, 0, 1, 1) != 0) {
330			g_detach(cp);
331			g_destroy_consumer(cp);
332			gctl_error(req, "Cannot access provider %s.", name);
333			continue;
334		}
335		disks[n].provider = pp;
336		disks[n].consumer = cp;
337		n++;
338	}
339	if (n == 0) {
340		g_free(disks);
341		return;
342	}
343	sc->sc_ndisks += n;
344again:
345	for (i = 0; i < n; i++) {
346		if (disks[i].consumer == NULL)
347			continue;
348		g_mirror_fill_metadata(sc, NULL, &md);
349		if (*inactive)
350			md.md_dflags |= G_MIRROR_DISK_FLAG_INACTIVE;
351		pp = disks[i].provider;
352		sector = g_malloc(pp->sectorsize, M_WAITOK);
353		mirror_metadata_encode(&md, sector);
354		error = g_write_data(disks[i].consumer,
355		    pp->mediasize - pp->sectorsize, sector, pp->sectorsize);
356		g_free(sector);
357		if (error != 0) {
358			gctl_error(req, "Cannot store metadata on %s.",
359			    pp->name);
360			g_access(disks[i].consumer, 0, -1, -1);
361			g_detach(disks[i].consumer);
362			g_destroy_consumer(disks[i].consumer);
363			disks[i].consumer = NULL;
364			disks[i].provider = NULL;
365			sc->sc_ndisks--;
366			goto again;
367		}
368	}
369	if (i == 0) {
370		/* All writes failed. */
371		g_free(disks);
372		return;
373	}
374	LIST_FOREACH(disk, &sc->sc_disks, d_next) {
375		g_mirror_update_metadata(disk);
376	}
377	/*
378	 * Release provider and wait for retaste.
379	 */
380	for (i = 0; i < n; i++) {
381		if (disks[i].consumer == NULL)
382			continue;
383		g_access(disks[i].consumer, 0, -1, -1);
384		g_detach(disks[i].consumer);
385		g_destroy_consumer(disks[i].consumer);
386	}
387	g_free(disks);
388}
389
390static void
391g_mirror_ctl_remove(struct gctl_req *req, struct g_class *mp)
392{
393	struct g_mirror_softc *sc;
394	struct g_mirror_disk *disk;
395	const char *name;
396	char param[16];
397	int *nargs;
398	u_int i;
399
400	g_topology_assert();
401	nargs = gctl_get_paraml(req, "nargs", sizeof(*nargs));
402	if (nargs == NULL) {
403		gctl_error(req, "No '%s' argument.", "nargs");
404		return;
405	}
406	if (*nargs < 2) {
407		gctl_error(req, "Too few arguments.");
408		return;
409	}
410	name = gctl_get_asciiparam(req, "arg0");
411	if (name == NULL) {
412		gctl_error(req, "No 'arg%u' argument.", 0);
413		return;
414	}
415	sc = g_mirror_find_device(mp, name);
416	if (sc == NULL) {
417		gctl_error(req, "No such device: %s.", name);
418		return;
419	}
420	if (g_mirror_ndisks(sc, -1) < sc->sc_ndisks) {
421		gctl_error(req, "Not all disks connected.");
422		return;
423	}
424
425	for (i = 1; i < (u_int)*nargs; i++) {
426		snprintf(param, sizeof(param), "arg%u", i);
427		name = gctl_get_asciiparam(req, param);
428		if (name == NULL) {
429			gctl_error(req, "No 'arg%u' argument.", i);
430			return;
431		}
432		disk = g_mirror_find_disk(sc, name);
433		if (disk == NULL) {
434			gctl_error(req, "No such provider: %s.", name);
435			return;
436		}
437		g_mirror_event_send(disk, G_MIRROR_DISK_STATE_DESTROY,
438		    G_MIRROR_EVENT_WAIT);
439	}
440}
441
442static void
443g_mirror_ctl_deactivate(struct gctl_req *req, struct g_class *mp)
444{
445	struct g_mirror_softc *sc;
446	struct g_mirror_disk *disk;
447	const char *name;
448	char param[16];
449	int *nargs;
450	u_int i;
451
452	g_topology_assert();
453	nargs = gctl_get_paraml(req, "nargs", sizeof(*nargs));
454	if (nargs == NULL) {
455		gctl_error(req, "No '%s' argument.", "nargs");
456		return;
457	}
458	if (*nargs < 2) {
459		gctl_error(req, "Too few arguments.");
460		return;
461	}
462	name = gctl_get_asciiparam(req, "arg0");
463	if (name == NULL) {
464		gctl_error(req, "No 'arg%u' argument.", 0);
465		return;
466	}
467	sc = g_mirror_find_device(mp, name);
468	if (sc == NULL) {
469		gctl_error(req, "No such device: %s.", name);
470		return;
471	}
472
473	for (i = 1; i < (u_int)*nargs; i++) {
474		snprintf(param, sizeof(param), "arg%u", i);
475		name = gctl_get_asciiparam(req, param);
476		if (name == NULL) {
477			gctl_error(req, "No 'arg%u' argument.", i);
478			return;
479		}
480		disk = g_mirror_find_disk(sc, name);
481		if (disk == NULL) {
482			gctl_error(req, "No such provider: %s.", name);
483			return;
484		}
485		/*
486		 * Do rebuild by resetting syncid and disconnecting disk.
487		 * It'll be retasted, connected to the mirror and
488		 * synchronized.
489		 */
490		disk->d_flags |= G_MIRROR_DISK_FLAG_INACTIVE;
491		disk->d_flags &= ~G_MIRROR_DISK_FLAG_FORCE_SYNC;
492		g_mirror_update_metadata(disk);
493		sc->sc_bump_syncid = G_MIRROR_BUMP_ON_FIRST_WRITE;
494		g_mirror_event_send(disk, G_MIRROR_DISK_STATE_DISCONNECTED,
495		    G_MIRROR_EVENT_WAIT);
496	}
497}
498
499static void
500g_mirror_ctl_forget(struct gctl_req *req, struct g_class *mp)
501{
502	struct g_mirror_softc *sc;
503	struct g_mirror_disk *disk;
504	const char *name;
505	char param[16];
506	int *nargs;
507	u_int i;
508
509	g_topology_assert();
510	nargs = gctl_get_paraml(req, "nargs", sizeof(*nargs));
511	if (nargs == NULL) {
512		gctl_error(req, "No '%s' argument.", "nargs");
513		return;
514	}
515	if (*nargs < 1) {
516		gctl_error(req, "Missing device(s).");
517		return;
518	}
519
520	for (i = 0; i < (u_int)*nargs; i++) {
521		snprintf(param, sizeof(param), "arg%u", i);
522		name = gctl_get_asciiparam(req, param);
523		if (name == NULL) {
524			gctl_error(req, "No 'arg%u' argument.", i);
525			return;
526		}
527		sc = g_mirror_find_device(mp, name);
528		if (sc == NULL) {
529			gctl_error(req, "No such device: %s.", name);
530			return;
531		}
532		if (g_mirror_ndisks(sc, -1) == sc->sc_ndisks) {
533			G_MIRROR_DEBUG(1,
534			    "All disks connected in %s, skipping.",
535			    sc->sc_name);
536			continue;
537		}
538		sc->sc_ndisks = g_mirror_ndisks(sc, -1);
539		LIST_FOREACH(disk, &sc->sc_disks, d_next) {
540			g_mirror_update_metadata(disk);
541		}
542	}
543}
544
545static void
546g_mirror_ctl_stop(struct gctl_req *req, struct g_class *mp)
547{
548	struct g_mirror_softc *sc;
549	int *force, *nargs, error;
550	const char *name;
551	char param[16];
552	u_int i;
553
554	g_topology_assert();
555
556	nargs = gctl_get_paraml(req, "nargs", sizeof(*nargs));
557	if (nargs == NULL) {
558		gctl_error(req, "No '%s' argument.", "nargs");
559		return;
560	}
561	if (*nargs < 1) {
562		gctl_error(req, "Missing device(s).");
563		return;
564	}
565	force = gctl_get_paraml(req, "force", sizeof(*force));
566	if (force == NULL) {
567		gctl_error(req, "No '%s' argument.", "force");
568		return;
569	}
570
571	for (i = 0; i < (u_int)*nargs; i++) {
572		snprintf(param, sizeof(param), "arg%u", i);
573		name = gctl_get_asciiparam(req, param);
574		if (name == NULL) {
575			gctl_error(req, "No 'arg%u' argument.", i);
576			return;
577		}
578		sc = g_mirror_find_device(mp, name);
579		if (sc == NULL) {
580			gctl_error(req, "No such device: %s.", name);
581			return;
582		}
583		error = g_mirror_destroy(sc, *force);
584		if (error != 0) {
585			gctl_error(req, "Cannot destroy device %s (error=%d).",
586			    sc->sc_geom->name, error);
587			return;
588		}
589	}
590}
591
592void
593g_mirror_config(struct gctl_req *req, struct g_class *mp, const char *verb)
594{
595	uint32_t *version;
596
597	g_topology_assert();
598
599	version = gctl_get_paraml(req, "version", sizeof(*version));
600	if (version == NULL) {
601		gctl_error(req, "No '%s' argument.", "version");
602		return;
603	}
604	if (*version != G_MIRROR_VERSION) {
605		gctl_error(req, "Userland and kernel parts are out of sync.");
606		return;
607	}
608
609	if (strcmp(verb, "configure") == 0)
610		g_mirror_ctl_configure(req, mp);
611	else if (strcmp(verb, "rebuild") == 0)
612		g_mirror_ctl_rebuild(req, mp);
613	else if (strcmp(verb, "insert") == 0)
614		g_mirror_ctl_insert(req, mp);
615	else if (strcmp(verb, "remove") == 0)
616		g_mirror_ctl_remove(req, mp);
617	else if (strcmp(verb, "deactivate") == 0)
618		g_mirror_ctl_deactivate(req, mp);
619	else if (strcmp(verb, "forget") == 0)
620		g_mirror_ctl_forget(req, mp);
621	else if (strcmp(verb, "stop") == 0)
622		g_mirror_ctl_stop(req, mp);
623	else
624		gctl_error(req, "Unknown verb.");
625}
626