Training courses

Kernel and Embedded Linux

Bootlin training courses

Embedded Linux, kernel,
Yocto Project, Buildroot, real-time,
graphics, boot time, debugging...

Bootlin logo

Elixir Cross Referencer

   1
   2
   3
   4
   5
   6
   7
   8
   9
  10
  11
  12
  13
  14
  15
  16
  17
  18
  19
  20
  21
  22
  23
  24
  25
  26
  27
  28
  29
  30
  31
  32
  33
  34
  35
  36
  37
  38
  39
  40
  41
  42
  43
  44
  45
  46
  47
  48
  49
  50
  51
  52
  53
  54
  55
  56
  57
  58
  59
  60
  61
  62
  63
  64
  65
  66
  67
  68
  69
  70
  71
  72
  73
  74
  75
  76
  77
  78
  79
  80
  81
  82
  83
  84
  85
  86
  87
  88
  89
  90
  91
  92
  93
  94
  95
  96
  97
  98
  99
 100
 101
 102
 103
 104
 105
 106
 107
 108
 109
 110
 111
 112
 113
 114
 115
 116
 117
 118
 119
 120
 121
 122
 123
 124
 125
 126
 127
 128
 129
 130
 131
 132
 133
 134
 135
 136
 137
 138
 139
 140
 141
 142
 143
 144
 145
 146
 147
 148
 149
 150
 151
 152
 153
 154
 155
 156
 157
 158
 159
 160
 161
 162
 163
 164
 165
 166
 167
 168
 169
 170
 171
 172
 173
 174
 175
 176
 177
 178
 179
 180
 181
 182
 183
 184
 185
 186
 187
 188
 189
 190
 191
 192
 193
 194
 195
 196
 197
 198
 199
 200
 201
 202
 203
 204
 205
 206
 207
 208
 209
 210
 211
 212
 213
 214
 215
 216
 217
 218
 219
 220
 221
 222
 223
 224
 225
 226
 227
 228
 229
 230
 231
 232
 233
 234
 235
 236
 237
 238
 239
 240
 241
 242
 243
 244
 245
 246
 247
 248
 249
 250
 251
 252
 253
 254
 255
 256
 257
 258
 259
 260
 261
 262
 263
 264
 265
 266
 267
 268
 269
 270
 271
 272
 273
 274
 275
 276
 277
 278
 279
 280
 281
 282
 283
 284
 285
 286
 287
 288
 289
 290
 291
 292
 293
 294
 295
 296
 297
 298
 299
 300
 301
 302
 303
 304
 305
 306
 307
 308
 309
 310
 311
 312
 313
 314
 315
 316
 317
 318
 319
 320
 321
 322
 323
 324
 325
 326
 327
 328
 329
 330
 331
 332
 333
 334
 335
 336
 337
 338
 339
 340
 341
 342
 343
 344
 345
 346
 347
 348
 349
 350
 351
 352
 353
 354
 355
 356
 357
 358
 359
 360
 361
 362
 363
 364
 365
 366
 367
 368
 369
 370
 371
 372
 373
 374
 375
 376
 377
 378
 379
 380
 381
 382
 383
 384
 385
 386
 387
 388
 389
 390
 391
 392
 393
 394
 395
 396
 397
 398
 399
 400
 401
 402
 403
 404
 405
 406
 407
 408
 409
 410
 411
 412
 413
 414
 415
 416
 417
 418
 419
 420
 421
 422
 423
 424
 425
 426
 427
 428
 429
 430
 431
 432
 433
 434
 435
 436
 437
 438
 439
 440
 441
 442
 443
 444
 445
 446
 447
 448
 449
 450
 451
 452
 453
 454
 455
 456
 457
 458
 459
 460
 461
 462
 463
 464
 465
 466
 467
 468
 469
 470
 471
 472
 473
 474
 475
 476
 477
 478
 479
 480
 481
 482
 483
 484
 485
 486
 487
 488
 489
 490
 491
 492
 493
 494
 495
 496
 497
 498
 499
 500
 501
 502
 503
 504
 505
 506
 507
 508
 509
 510
 511
 512
 513
 514
 515
 516
 517
 518
 519
 520
 521
 522
 523
 524
 525
 526
 527
 528
 529
 530
 531
 532
 533
 534
 535
 536
 537
 538
 539
 540
 541
 542
 543
 544
 545
 546
 547
 548
 549
 550
 551
 552
 553
 554
 555
 556
 557
 558
 559
 560
 561
 562
 563
 564
 565
 566
 567
 568
 569
 570
 571
 572
 573
 574
 575
 576
 577
 578
 579
 580
 581
 582
 583
 584
 585
 586
 587
 588
 589
 590
 591
 592
 593
 594
 595
 596
 597
 598
 599
 600
 601
 602
 603
 604
 605
 606
 607
 608
 609
 610
 611
 612
 613
 614
 615
 616
 617
 618
 619
 620
 621
 622
 623
 624
 625
 626
 627
 628
 629
 630
 631
 632
 633
 634
 635
 636
 637
 638
 639
 640
 641
 642
 643
 644
 645
 646
 647
 648
 649
 650
 651
 652
 653
 654
 655
 656
 657
 658
 659
 660
 661
 662
 663
 664
 665
 666
 667
 668
 669
 670
 671
 672
 673
 674
 675
 676
 677
 678
 679
 680
 681
 682
 683
 684
 685
 686
 687
 688
 689
 690
 691
 692
 693
 694
 695
 696
 697
 698
 699
 700
 701
 702
 703
 704
 705
 706
 707
 708
 709
 710
 711
 712
 713
 714
 715
 716
 717
 718
 719
 720
 721
 722
 723
 724
 725
 726
 727
 728
 729
 730
 731
 732
 733
 734
 735
 736
 737
 738
 739
 740
 741
 742
 743
 744
 745
 746
 747
 748
 749
 750
 751
 752
 753
 754
 755
 756
 757
 758
 759
 760
 761
 762
 763
 764
 765
 766
 767
 768
 769
 770
 771
 772
 773
 774
 775
 776
 777
 778
 779
 780
 781
 782
 783
 784
 785
 786
 787
 788
 789
 790
 791
 792
 793
 794
 795
 796
 797
 798
 799
 800
 801
 802
 803
 804
 805
 806
 807
 808
 809
 810
 811
 812
 813
 814
 815
 816
 817
 818
 819
 820
 821
 822
 823
 824
 825
 826
 827
 828
 829
 830
 831
 832
 833
 834
 835
 836
 837
 838
 839
 840
 841
 842
 843
 844
 845
 846
 847
 848
 849
 850
 851
 852
 853
 854
 855
 856
 857
 858
 859
 860
 861
 862
 863
 864
 865
 866
 867
 868
 869
 870
 871
 872
 873
 874
 875
 876
 877
 878
 879
 880
 881
 882
 883
 884
 885
 886
 887
 888
 889
 890
 891
 892
 893
 894
 895
 896
 897
 898
 899
 900
 901
 902
 903
 904
 905
 906
 907
 908
 909
 910
 911
 912
 913
 914
 915
 916
 917
 918
 919
 920
 921
 922
 923
 924
 925
 926
 927
 928
 929
 930
 931
 932
 933
 934
 935
 936
 937
 938
 939
 940
 941
 942
 943
 944
 945
 946
 947
 948
 949
 950
 951
 952
 953
 954
 955
 956
 957
 958
 959
 960
 961
 962
 963
 964
 965
 966
 967
 968
 969
 970
 971
 972
 973
 974
 975
 976
 977
 978
 979
 980
 981
 982
 983
 984
 985
 986
 987
 988
 989
 990
 991
 992
 993
 994
 995
 996
 997
 998
 999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
/*-
 * SPDX-License-Identifier: BSD-2-Clause-FreeBSD
 *
 *  Copyright (c) 2004, 2007 Lukas Ertl
 *  Copyright (c) 2007, 2009 Ulf Lilleengen
 *  All rights reserved.
 * 
 * Redistribution and use in source and binary forms, with or without
 * modification, are permitted provided that the following conditions
 * are met:
 * 1. Redistributions of source code must retain the above copyright
 *    notice, this list of conditions and the following disclaimer.
 * 2. Redistributions in binary form must reproduce the above copyright
 *    notice, this list of conditions and the following disclaimer in the
 *    documentation and/or other materials provided with the distribution.
 * 
 * THIS SOFTWARE IS PROVIDED BY AUTHOR AND CONTRIBUTORS ``AS IS'' AND
 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
 * ARE DISCLAIMED.  IN NO EVENT SHALL AUTHOR OR CONTRIBUTORS BE LIABLE
 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
 * SUCH DAMAGE.
 *
 */

#include <sys/cdefs.h>
__FBSDID("$FreeBSD$");

#include <sys/param.h>
#include <sys/bio.h>
#include <sys/kernel.h>
#include <sys/kthread.h>
#include <sys/lock.h>
#include <sys/malloc.h>
#include <sys/module.h>
#include <sys/mutex.h>
#include <sys/sbuf.h>
#include <sys/sysctl.h>
#include <sys/systm.h>

#include <geom/geom.h>
#include <geom/vinum/geom_vinum_var.h>
#include <geom/vinum/geom_vinum.h>
#include <geom/vinum/geom_vinum_raid5.h>

SYSCTL_DECL(_kern_geom);
static SYSCTL_NODE(_kern_geom, OID_AUTO, vinum, CTLFLAG_RW, 0,
    "GEOM_VINUM stuff");
u_int g_vinum_debug = 0;
SYSCTL_UINT(_kern_geom_vinum, OID_AUTO, debug, CTLFLAG_RWTUN, &g_vinum_debug, 0,
    "Debug level");

static int	gv_create(struct g_geom *, struct gctl_req *);
static void	gv_attach(struct gv_softc *, struct gctl_req *);
static void	gv_detach(struct gv_softc *, struct gctl_req *);
static void	gv_parityop(struct gv_softc *, struct gctl_req *);


static void
gv_orphan(struct g_consumer *cp)
{
	struct g_geom *gp;
	struct gv_softc *sc;
	struct gv_drive *d;
	
	g_topology_assert();

	KASSERT(cp != NULL, ("gv_orphan: null cp"));
	gp = cp->geom;
	KASSERT(gp != NULL, ("gv_orphan: null gp"));
	sc = gp->softc;
	KASSERT(sc != NULL, ("gv_orphan: null sc"));
	d = cp->private;
	KASSERT(d != NULL, ("gv_orphan: null d"));

	g_trace(G_T_TOPOLOGY, "gv_orphan(%s)", gp->name);

	gv_post_event(sc, GV_EVENT_DRIVE_LOST, d, NULL, 0, 0);
}

void
gv_start(struct bio *bp)
{
	struct g_geom *gp;
	struct gv_softc *sc;
	
	gp = bp->bio_to->geom;
	sc = gp->softc;

	switch (bp->bio_cmd) {
	case BIO_READ:
	case BIO_WRITE:
	case BIO_DELETE:
		break;
	case BIO_GETATTR:
	default:
		g_io_deliver(bp, EOPNOTSUPP);
		return;
	}
	mtx_lock(&sc->bqueue_mtx);
	bioq_disksort(sc->bqueue_down, bp);
	wakeup(sc);
	mtx_unlock(&sc->bqueue_mtx);
}

void
gv_done(struct bio *bp)
{
	struct g_geom *gp;
	struct gv_softc *sc;
	
	KASSERT(bp != NULL, ("NULL bp"));

	gp = bp->bio_from->geom;
	sc = gp->softc;

	mtx_lock(&sc->bqueue_mtx);
	bioq_disksort(sc->bqueue_up, bp);
	wakeup(sc);
	mtx_unlock(&sc->bqueue_mtx);
}

int
gv_access(struct g_provider *pp, int dr, int dw, int de)
{
	struct g_geom *gp;
	struct gv_softc *sc;
	struct gv_drive *d, *d2;
	int error;
	
	gp = pp->geom;
	sc = gp->softc;
	/*
	 * We want to modify the read count with the write count in case we have
	 * plexes in a RAID-5 organization.
	 */
	dr += dw;

	LIST_FOREACH(d, &sc->drives, drive) {
		if (d->consumer == NULL)
			continue;
		error = g_access(d->consumer, dr, dw, de);
		if (error) {
			LIST_FOREACH(d2, &sc->drives, drive) {
				if (d == d2)
					break;
				g_access(d2->consumer, -dr, -dw, -de);
			}
			G_VINUM_DEBUG(0, "g_access '%s' failed: %d", d->name,
			    error);
			return (error);
		}
	}
	return (0);
}

static void
gv_init(struct g_class *mp)
{
	struct g_geom *gp;
	struct gv_softc *sc;

	g_trace(G_T_TOPOLOGY, "gv_init(%p)", mp);

	gp = g_new_geomf(mp, "VINUM");
	gp->spoiled = gv_orphan;
	gp->orphan = gv_orphan;
	gp->access = gv_access;
	gp->start = gv_start;
	gp->softc = g_malloc(sizeof(struct gv_softc), M_WAITOK | M_ZERO);
	sc = gp->softc;
	sc->geom = gp;
	sc->bqueue_down = g_malloc(sizeof(struct bio_queue_head),
	    M_WAITOK | M_ZERO);
	sc->bqueue_up = g_malloc(sizeof(struct bio_queue_head),
	    M_WAITOK | M_ZERO);
	bioq_init(sc->bqueue_down);
	bioq_init(sc->bqueue_up);
	LIST_INIT(&sc->drives);
	LIST_INIT(&sc->subdisks);
	LIST_INIT(&sc->plexes);
	LIST_INIT(&sc->volumes);
	TAILQ_INIT(&sc->equeue);
	mtx_init(&sc->config_mtx, "gv_config", NULL, MTX_DEF);
	mtx_init(&sc->equeue_mtx, "gv_equeue", NULL, MTX_DEF);
	mtx_init(&sc->bqueue_mtx, "gv_bqueue", NULL, MTX_DEF);
	kproc_create(gv_worker, sc, &sc->worker, 0, 0, "gv_worker");
}

static int
gv_unload(struct gctl_req *req, struct g_class *mp, struct g_geom *gp)
{
	struct gv_softc *sc;

	g_trace(G_T_TOPOLOGY, "gv_unload(%p)", mp);

	g_topology_assert();
	sc = gp->softc;

	if (sc != NULL) {
		gv_worker_exit(sc);
		gp->softc = NULL;
		g_wither_geom(gp, ENXIO);
	}

	return (0);
}

/* Handle userland request of attaching object. */
static void
gv_attach(struct gv_softc *sc, struct gctl_req *req)
{
	struct gv_volume *v;
	struct gv_plex *p;
	struct gv_sd *s;
	off_t *offset;
	int *rename, type_child, type_parent;
	char *child, *parent;

	child = gctl_get_param(req, "child", NULL);
	if (child == NULL) {
		gctl_error(req, "no child given");
		return;
	}
	parent = gctl_get_param(req, "parent", NULL);
	if (parent == NULL) {
		gctl_error(req, "no parent given");
		return;
	}
	offset = gctl_get_paraml(req, "offset", sizeof(*offset));
	if (offset == NULL) {
		gctl_error(req, "no offset given");
		return;
	}
	rename = gctl_get_paraml(req, "rename", sizeof(*rename));
	if (rename == NULL) {
		gctl_error(req, "no rename flag given");
		return;
	}

	type_child = gv_object_type(sc, child);
	type_parent = gv_object_type(sc, parent);

	switch (type_child) {
	case GV_TYPE_PLEX:
		if (type_parent != GV_TYPE_VOL) {
			gctl_error(req, "no such volume to attach to");
			return;
		}
		v = gv_find_vol(sc, parent);
		p = gv_find_plex(sc, child);
		gv_post_event(sc, GV_EVENT_ATTACH_PLEX, p, v, *offset, *rename);
		break;
	case GV_TYPE_SD:
		if (type_parent != GV_TYPE_PLEX) {
			gctl_error(req, "no such plex to attach to");
			return;
		}
		p = gv_find_plex(sc, parent);
		s = gv_find_sd(sc, child);
		gv_post_event(sc, GV_EVENT_ATTACH_SD, s, p, *offset, *rename);
		break;
	default:
		gctl_error(req, "invalid child type");
		break;
	}
}

/* Handle userland request of detaching object. */
static void
gv_detach(struct gv_softc *sc, struct gctl_req *req)
{
	struct gv_plex *p;
	struct gv_sd *s;
	int *flags, type;
	char *object;

	object = gctl_get_param(req, "object", NULL);
	if (object == NULL) {
		gctl_error(req, "no argument given");
		return;
	}

	flags = gctl_get_paraml(req, "flags", sizeof(*flags));
	type = gv_object_type(sc, object);
	switch (type) {
	case GV_TYPE_PLEX:
		p = gv_find_plex(sc, object);
		gv_post_event(sc, GV_EVENT_DETACH_PLEX, p, NULL, *flags, 0);
		break;
	case GV_TYPE_SD:
		s = gv_find_sd(sc, object);
		gv_post_event(sc, GV_EVENT_DETACH_SD, s, NULL, *flags, 0);
		break;
	default:
		gctl_error(req, "invalid object type");
		break;
	}
}

/* Handle userland requests for creating new objects. */
static int
gv_create(struct g_geom *gp, struct gctl_req *req)
{
	struct gv_softc *sc;
	struct gv_drive *d, *d2;
	struct gv_plex *p, *p2;
	struct gv_sd *s, *s2;
	struct gv_volume *v, *v2;
	struct g_provider *pp;
	int error, i, *drives, *flags, *plexes, *subdisks, *volumes;
	char buf[20];

	g_topology_assert();

	sc = gp->softc;

	/* Find out how many of each object have been passed in. */
	volumes = gctl_get_paraml(req, "volumes", sizeof(*volumes));
	plexes = gctl_get_paraml(req, "plexes", sizeof(*plexes));
	subdisks = gctl_get_paraml(req, "subdisks", sizeof(*subdisks));
	drives = gctl_get_paraml(req, "drives", sizeof(*drives));
	if (volumes == NULL || plexes == NULL || subdisks == NULL ||
	    drives == NULL) {
		gctl_error(req, "number of objects not given");
		return (-1);
	}
	flags = gctl_get_paraml(req, "flags", sizeof(*flags));
	if (flags == NULL) {
		gctl_error(req, "flags not given");
		return (-1);
	}

	/* First, handle drive definitions ... */
	for (i = 0; i < *drives; i++) {
		snprintf(buf, sizeof(buf), "drive%d", i);
		d2 = gctl_get_paraml(req, buf, sizeof(*d2));
		if (d2 == NULL) {
			gctl_error(req, "no drive definition given");
			return (-1);
		}
		/*
		 * Make sure that the device specified in the drive config is
		 * an active GEOM provider.
		 */
		pp = g_provider_by_name(d2->device);
		if (pp == NULL) {
			gctl_error(req, "%s: device not found", d2->device);
			goto error;
		}
		if (gv_find_drive(sc, d2->name) != NULL) {
			/* Ignore error. */
			if (*flags & GV_FLAG_F)
				continue;
			gctl_error(req, "drive '%s' already exists", d2->name);
			goto error;
		}
		if (gv_find_drive_device(sc, d2->device) != NULL) {
			gctl_error(req, "device '%s' already configured in "
			    "gvinum", d2->device);
			goto error;
		}


		d = g_malloc(sizeof(*d), M_WAITOK | M_ZERO);
		bcopy(d2, d, sizeof(*d));

		gv_post_event(sc, GV_EVENT_CREATE_DRIVE, d, NULL, 0, 0);
	}

	/* ... then volume definitions ... */
	for (i = 0; i < *volumes; i++) {
		error = 0;
		snprintf(buf, sizeof(buf), "volume%d", i);
		v2 = gctl_get_paraml(req, buf, sizeof(*v2));
		if (v2 == NULL) {
			gctl_error(req, "no volume definition given");
			return (-1);
		}
		if (gv_find_vol(sc, v2->name) != NULL) {
			/* Ignore error. */
			if (*flags & GV_FLAG_F)
				continue;
			gctl_error(req, "volume '%s' already exists", v2->name);
			goto error;
		}

		v = g_malloc(sizeof(*v), M_WAITOK | M_ZERO);
		bcopy(v2, v, sizeof(*v));

		gv_post_event(sc, GV_EVENT_CREATE_VOLUME, v, NULL, 0, 0);
	}

	/* ... then plex definitions ... */
	for (i = 0; i < *plexes; i++) {
		error = 0;
		snprintf(buf, sizeof(buf), "plex%d", i);
		p2 = gctl_get_paraml(req, buf, sizeof(*p2));
		if (p2 == NULL) {
			gctl_error(req, "no plex definition given");
			return (-1);
		}
		if (gv_find_plex(sc, p2->name) != NULL) {
			/* Ignore error. */
			if (*flags & GV_FLAG_F)
				continue;
			gctl_error(req, "plex '%s' already exists", p2->name);
			goto error;
		}

		p = g_malloc(sizeof(*p), M_WAITOK | M_ZERO);
		bcopy(p2, p, sizeof(*p));

		gv_post_event(sc, GV_EVENT_CREATE_PLEX, p, NULL, 0, 0);
	}

	/* ... and, finally, subdisk definitions. */
	for (i = 0; i < *subdisks; i++) {
		error = 0;
		snprintf(buf, sizeof(buf), "sd%d", i);
		s2 = gctl_get_paraml(req, buf, sizeof(*s2));
		if (s2 == NULL) {
			gctl_error(req, "no subdisk definition given");
			return (-1);
		}
		if (gv_find_sd(sc, s2->name) != NULL) {
			/* Ignore error. */
			if (*flags & GV_FLAG_F)
				continue;
			gctl_error(req, "sd '%s' already exists", s2->name);
			goto error;
		}

		s = g_malloc(sizeof(*s), M_WAITOK | M_ZERO);
		bcopy(s2, s, sizeof(*s));

		gv_post_event(sc, GV_EVENT_CREATE_SD, s, NULL, 0, 0);
	}

error:
	gv_post_event(sc, GV_EVENT_SETUP_OBJECTS, sc, NULL, 0, 0);
	gv_post_event(sc, GV_EVENT_SAVE_CONFIG, sc, NULL, 0, 0);

	return (0);
}

static void
gv_config(struct gctl_req *req, struct g_class *mp, char const *verb)
{
	struct g_geom *gp;
	struct gv_softc *sc;
	struct sbuf *sb;
	char *comment;

	g_topology_assert();

	gp = LIST_FIRST(&mp->geom);
	sc = gp->softc;

	if (!strcmp(verb, "attach")) {
		gv_attach(sc, req);

	} else if (!strcmp(verb, "concat")) {
		gv_concat(gp, req);

	} else if (!strcmp(verb, "detach")) {
		gv_detach(sc, req);

	} else if (!strcmp(verb, "list")) {
		gv_list(gp, req);

	/* Save our configuration back to disk. */
	} else if (!strcmp(verb, "saveconfig")) {
		gv_post_event(sc, GV_EVENT_SAVE_CONFIG, sc, NULL, 0, 0);

	/* Return configuration in string form. */
	} else if (!strcmp(verb, "getconfig")) {
		comment = gctl_get_param(req, "comment", NULL);
		if (comment == NULL) {
			gctl_error(req, "no comment parameter given");
			return;
		}
		sb = sbuf_new(NULL, NULL, GV_CFG_LEN, SBUF_FIXEDLEN);
		gv_format_config(sc, sb, 0, comment);
		sbuf_finish(sb);
		gctl_set_param(req, "config", sbuf_data(sb), sbuf_len(sb) + 1);
		sbuf_delete(sb);

	} else if (!strcmp(verb, "create")) {
		gv_create(gp, req);

	} else if (!strcmp(verb, "mirror")) {
		gv_mirror(gp, req);

	} else if (!strcmp(verb, "move")) {
		gv_move(gp, req);

	} else if (!strcmp(verb, "raid5")) {
		gv_raid5(gp, req);

	} else if (!strcmp(verb, "rebuildparity") ||
	    !strcmp(verb, "checkparity")) {
		gv_parityop(sc, req);

	} else if (!strcmp(verb, "remove")) {
		gv_remove(gp, req);

	} else if (!strcmp(verb, "rename")) {
		gv_rename(gp, req);
	
	} else if (!strcmp(verb, "resetconfig")) {
		gv_post_event(sc, GV_EVENT_RESET_CONFIG, sc, NULL, 0, 0);

	} else if (!strcmp(verb, "start")) {
		gv_start_obj(gp, req);

	} else if (!strcmp(verb, "stripe")) {
		gv_stripe(gp, req);

	} else if (!strcmp(verb, "setstate")) {
		gv_setstate(gp, req);
	} else
		gctl_error(req, "Unknown verb parameter");
}

static void
gv_parityop(struct gv_softc *sc, struct gctl_req *req)
{
	struct gv_plex *p;
	int *flags, *rebuild, type;
	char *plex;

	plex = gctl_get_param(req, "plex", NULL);
	if (plex == NULL) {
		gctl_error(req, "no plex given");
		return;
	}

	flags = gctl_get_paraml(req, "flags", sizeof(*flags));
	if (flags == NULL) {
		gctl_error(req, "no flags given");
		return;
	}

	rebuild = gctl_get_paraml(req, "rebuild", sizeof(*rebuild));
	if (rebuild == NULL) {
		gctl_error(req, "no operation given");
		return;
	}

	type = gv_object_type(sc, plex);
	if (type != GV_TYPE_PLEX) {
		gctl_error(req, "'%s' is not a plex", plex);
		return;
	}
	p = gv_find_plex(sc, plex);

	if (p->state != GV_PLEX_UP) {
		gctl_error(req, "plex %s is not completely accessible",
		    p->name);
		return;
	}

	if (p->org != GV_PLEX_RAID5) {
		gctl_error(req, "plex %s is not a RAID5 plex", p->name);
		return;
	}

	/* Put it in the event queue. */
	/* XXX: The state of the plex might have changed when this event is
	 * picked up ... We should perhaps check this afterwards. */
	if (*rebuild)
		gv_post_event(sc, GV_EVENT_PARITY_REBUILD, p, NULL, 0, 0);
	else
		gv_post_event(sc, GV_EVENT_PARITY_CHECK, p, NULL, 0, 0);
}


static struct g_geom *
gv_taste(struct g_class *mp, struct g_provider *pp, int flags __unused)
{
	struct g_geom *gp;
	struct g_consumer *cp;
	struct gv_softc *sc;
	struct gv_hdr vhdr;
	int error;

 	g_topology_assert();
	g_trace(G_T_TOPOLOGY, "gv_taste(%s, %s)", mp->name, pp->name);

	gp = LIST_FIRST(&mp->geom);
	if (gp == NULL) {
		G_VINUM_DEBUG(0, "error: tasting, but not initialized?");
		return (NULL);
	}
	sc = gp->softc;

	cp = g_new_consumer(gp);
	if (g_attach(cp, pp) != 0) {
		g_destroy_consumer(cp);
		return (NULL);
	}
	if (g_access(cp, 1, 0, 0) != 0) {
		g_detach(cp);
		g_destroy_consumer(cp);
		return (NULL);
	}
	g_topology_unlock();

	error = gv_read_header(cp, &vhdr);

	g_topology_lock();
	g_access(cp, -1, 0, 0);
	g_detach(cp);
	g_destroy_consumer(cp);

	/* Check if what we've been given is a valid vinum drive. */
	if (!error)
		gv_post_event(sc, GV_EVENT_DRIVE_TASTED, pp, NULL, 0, 0);

	return (NULL);
}

void
gv_worker(void *arg)
{
	struct g_provider *pp;
	struct gv_softc *sc;
	struct gv_event *ev;
	struct gv_volume *v;
	struct gv_plex *p;
	struct gv_sd *s;
	struct gv_drive *d;
	struct bio *bp;
	int newstate, flags, err, rename;
	char *newname;
	off_t offset;

	sc = arg;
	KASSERT(sc != NULL, ("NULL sc"));
	for (;;) {
		/* Look at the events first... */
		ev = gv_get_event(sc);
		if (ev != NULL) {
			gv_remove_event(sc, ev);

			switch (ev->type) {
			case GV_EVENT_DRIVE_TASTED:
				G_VINUM_DEBUG(2, "event 'drive tasted'");
				pp = ev->arg1;
				gv_drive_tasted(sc, pp);
				break;

			case GV_EVENT_DRIVE_LOST:
				G_VINUM_DEBUG(2, "event 'drive lost'");
				d = ev->arg1;
				gv_drive_lost(sc, d);
				break;

			case GV_EVENT_CREATE_DRIVE:
				G_VINUM_DEBUG(2, "event 'create drive'");
				d = ev->arg1;
				gv_create_drive(sc, d);
				break;

			case GV_EVENT_CREATE_VOLUME:
				G_VINUM_DEBUG(2, "event 'create volume'");
				v = ev->arg1;
				gv_create_volume(sc, v);
				break;

			case GV_EVENT_CREATE_PLEX:
				G_VINUM_DEBUG(2, "event 'create plex'");
				p = ev->arg1;
				gv_create_plex(sc, p);
				break;

			case GV_EVENT_CREATE_SD:
				G_VINUM_DEBUG(2, "event 'create sd'");
				s = ev->arg1;
				gv_create_sd(sc, s);
				break;

			case GV_EVENT_RM_DRIVE:
				G_VINUM_DEBUG(2, "event 'remove drive'");
				d = ev->arg1;
				flags = ev->arg3;
				gv_rm_drive(sc, d, flags);
				/*gv_setup_objects(sc);*/
				break;

			case GV_EVENT_RM_VOLUME:
				G_VINUM_DEBUG(2, "event 'remove volume'");
				v = ev->arg1;
				gv_rm_vol(sc, v);
				/*gv_setup_objects(sc);*/
				break;

			case GV_EVENT_RM_PLEX:
				G_VINUM_DEBUG(2, "event 'remove plex'");
				p = ev->arg1;
				gv_rm_plex(sc, p);
				/*gv_setup_objects(sc);*/
				break;

			case GV_EVENT_RM_SD:
				G_VINUM_DEBUG(2, "event 'remove sd'");
				s = ev->arg1;
				gv_rm_sd(sc, s);
				/*gv_setup_objects(sc);*/
				break;

			case GV_EVENT_SAVE_CONFIG:
				G_VINUM_DEBUG(2, "event 'save config'");
				gv_save_config(sc);
				break;

			case GV_EVENT_SET_SD_STATE:
				G_VINUM_DEBUG(2, "event 'setstate sd'");
				s = ev->arg1;
				newstate = ev->arg3;
				flags = ev->arg4;
				err = gv_set_sd_state(s, newstate, flags);
				if (err)
					G_VINUM_DEBUG(0, "error setting subdisk"
					    " state: error code %d", err);
				break;

			case GV_EVENT_SET_DRIVE_STATE:
				G_VINUM_DEBUG(2, "event 'setstate drive'");
				d = ev->arg1;
				newstate = ev->arg3;
				flags = ev->arg4;
				err = gv_set_drive_state(d, newstate, flags);
				if (err)
					G_VINUM_DEBUG(0, "error setting drive "
					    "state: error code %d", err);
				break;

			case GV_EVENT_SET_VOL_STATE:
				G_VINUM_DEBUG(2, "event 'setstate volume'");
				v = ev->arg1;
				newstate = ev->arg3;
				flags = ev->arg4;
				err = gv_set_vol_state(v, newstate, flags);
				if (err)
					G_VINUM_DEBUG(0, "error setting volume "
					    "state: error code %d", err);
				break;

			case GV_EVENT_SET_PLEX_STATE:
				G_VINUM_DEBUG(2, "event 'setstate plex'");
				p = ev->arg1;
				newstate = ev->arg3;
				flags = ev->arg4;
				err = gv_set_plex_state(p, newstate, flags);
				if (err)
					G_VINUM_DEBUG(0, "error setting plex "
					    "state: error code %d", err);
				break;

			case GV_EVENT_SETUP_OBJECTS:
				G_VINUM_DEBUG(2, "event 'setup objects'");
				gv_setup_objects(sc);
				break;

			case GV_EVENT_RESET_CONFIG:
				G_VINUM_DEBUG(2, "event 'resetconfig'");
				err = gv_resetconfig(sc);
				if (err)
					G_VINUM_DEBUG(0, "error resetting "
					    "config: error code %d", err);
				break;

			case GV_EVENT_PARITY_REBUILD:
				/*
				 * Start the rebuild. The gv_plex_done will
				 * handle issuing of the remaining rebuild bio's
				 * until it's finished. 
				 */
				G_VINUM_DEBUG(2, "event 'rebuild'");
				p = ev->arg1;
				if (p->state != GV_PLEX_UP) {
					G_VINUM_DEBUG(0, "plex %s is not "
					    "completely accessible", p->name);
					break;
				}
				if (p->flags & GV_PLEX_SYNCING ||
				    p->flags & GV_PLEX_REBUILDING ||
				    p->flags & GV_PLEX_GROWING) {
					G_VINUM_DEBUG(0, "plex %s is busy with "
					    "syncing or parity build", p->name);
					break;
				}
				p->synced = 0;
				p->flags |= GV_PLEX_REBUILDING;
				g_topology_assert_not();
				g_topology_lock();
				err = gv_access(p->vol_sc->provider, 1, 1, 0);
				if (err) {
					G_VINUM_DEBUG(0, "unable to access "
					    "provider");
					break;
				}
				g_topology_unlock();
				gv_parity_request(p, GV_BIO_CHECK |
				    GV_BIO_PARITY, 0);
				break;

			case GV_EVENT_PARITY_CHECK:
				/* Start parity check. */
				G_VINUM_DEBUG(2, "event 'check'");
				p = ev->arg1;
				if (p->state != GV_PLEX_UP) {
					G_VINUM_DEBUG(0, "plex %s is not "
					    "completely accessible", p->name);
					break;
				}
				if (p->flags & GV_PLEX_SYNCING ||
				    p->flags & GV_PLEX_REBUILDING ||
				    p->flags & GV_PLEX_GROWING) {
					G_VINUM_DEBUG(0, "plex %s is busy with "
					    "syncing or parity build", p->name);
					break;
				}
				p->synced = 0;
				g_topology_assert_not();
				g_topology_lock();
				err = gv_access(p->vol_sc->provider, 1, 1, 0);
				if (err) {
					G_VINUM_DEBUG(0, "unable to access "
					    "provider");
					break;
				}
				g_topology_unlock();
				gv_parity_request(p, GV_BIO_CHECK, 0);
				break;

			case GV_EVENT_START_PLEX:
				G_VINUM_DEBUG(2, "event 'start' plex");
				p = ev->arg1;
				gv_start_plex(p);
				break;

			case GV_EVENT_START_VOLUME:
				G_VINUM_DEBUG(2, "event 'start' volume");
				v = ev->arg1;
				gv_start_vol(v);
				break;

			case GV_EVENT_ATTACH_PLEX:
				G_VINUM_DEBUG(2, "event 'attach' plex");
				p = ev->arg1;
				v = ev->arg2;
				rename = ev->arg4;
				err = gv_attach_plex(p, v, rename);
				if (err)
					G_VINUM_DEBUG(0, "error attaching %s to"
					    " %s: error code %d", p->name,
					    v->name, err);
				break;

			case GV_EVENT_ATTACH_SD:
				G_VINUM_DEBUG(2, "event 'attach' sd");
				s = ev->arg1;
				p = ev->arg2;
				offset = ev->arg3;
				rename = ev->arg4;
				err = gv_attach_sd(s, p, offset, rename);
				if (err)
					G_VINUM_DEBUG(0, "error attaching %s to"
					    " %s: error code %d", s->name,
					    p->name, err);
				break;

			case GV_EVENT_DETACH_PLEX:
				G_VINUM_DEBUG(2, "event 'detach' plex");
				p = ev->arg1;
				flags = ev->arg3;
				err = gv_detach_plex(p, flags);
				if (err)
					G_VINUM_DEBUG(0, "error detaching %s: "
					    "error code %d", p->name, err);
				break;

			case GV_EVENT_DETACH_SD:
				G_VINUM_DEBUG(2, "event 'detach' sd");
				s = ev->arg1;
				flags = ev->arg3;
				err = gv_detach_sd(s, flags);
				if (err)
					G_VINUM_DEBUG(0, "error detaching %s: "
					    "error code %d", s->name, err);
				break;

			case GV_EVENT_RENAME_VOL:
				G_VINUM_DEBUG(2, "event 'rename' volume");
				v = ev->arg1;
				newname = ev->arg2;
				flags = ev->arg3;
				err = gv_rename_vol(sc, v, newname, flags);
				if (err)
					G_VINUM_DEBUG(0, "error renaming %s to "
					    "%s: error code %d", v->name,
					    newname, err);
				g_free(newname);
				/* Destroy and recreate the provider if we can. */
				if (gv_provider_is_open(v->provider)) {
					G_VINUM_DEBUG(0, "unable to rename "
					    "provider to %s: provider in use",
					    v->name);
					break;
				}
				g_topology_lock();
				g_wither_provider(v->provider, ENOENT);
				g_topology_unlock();
				v->provider = NULL;
				gv_post_event(sc, GV_EVENT_SETUP_OBJECTS, sc,
				    NULL, 0, 0);
				break;

			case GV_EVENT_RENAME_PLEX:
				G_VINUM_DEBUG(2, "event 'rename' plex");
				p = ev->arg1;
				newname = ev->arg2;
				flags = ev->arg3;
				err = gv_rename_plex(sc, p, newname, flags);
				if (err)
					G_VINUM_DEBUG(0, "error renaming %s to "
					    "%s: error code %d", p->name,
					    newname, err);
				g_free(newname);
				break;

			case GV_EVENT_RENAME_SD:
				G_VINUM_DEBUG(2, "event 'rename' sd");
				s = ev->arg1;
				newname = ev->arg2;
				flags = ev->arg3;
				err = gv_rename_sd(sc, s, newname, flags);
				if (err)
					G_VINUM_DEBUG(0, "error renaming %s to "
					    "%s: error code %d", s->name,
					    newname, err);
				g_free(newname);
				break;

			case GV_EVENT_RENAME_DRIVE:
				G_VINUM_DEBUG(2, "event 'rename' drive");
				d = ev->arg1;
				newname = ev->arg2;
				flags = ev->arg3;
				err = gv_rename_drive(sc, d, newname, flags);
				if (err)
					G_VINUM_DEBUG(0, "error renaming %s to "
					    "%s: error code %d", d->name,
					    newname, err);
				g_free(newname);
				break;

			case GV_EVENT_MOVE_SD:
				G_VINUM_DEBUG(2, "event 'move' sd");
				s = ev->arg1;
				d = ev->arg2;
				flags = ev->arg3;
				err = gv_move_sd(sc, s, d, flags);
				if (err)
					G_VINUM_DEBUG(0, "error moving %s to "
					    "%s: error code %d", s->name,
					    d->name, err);
				break;

			case GV_EVENT_THREAD_EXIT:
				G_VINUM_DEBUG(2, "event 'thread exit'");
				g_free(ev);
				mtx_lock(&sc->equeue_mtx);
				mtx_lock(&sc->bqueue_mtx);
				gv_cleanup(sc);
				mtx_destroy(&sc->bqueue_mtx);
				mtx_destroy(&sc->equeue_mtx);
				g_free(sc->bqueue_down);
				g_free(sc->bqueue_up);
				g_free(sc);
				kproc_exit(0);
				/* NOTREACHED */

			default:
				G_VINUM_DEBUG(1, "unknown event %d", ev->type);
			}

			g_free(ev);
			continue;
		}

		/* ... then do I/O processing. */
		mtx_lock(&sc->bqueue_mtx);
		/* First do new requests. */
		bp = bioq_takefirst(sc->bqueue_down);
		if (bp != NULL) {
			mtx_unlock(&sc->bqueue_mtx);
			/* A bio that interfered with another bio. */
			if (bp->bio_pflags & GV_BIO_ONHOLD) {
				s = bp->bio_caller1;
				p = s->plex_sc;
				/* Is it still locked out? */
				if (gv_stripe_active(p, bp)) {
					/* Park the bio on the waiting queue. */
					bioq_disksort(p->wqueue, bp);
				} else {
					bp->bio_pflags &= ~GV_BIO_ONHOLD;
					g_io_request(bp, s->drive_sc->consumer);
				}
			/* A special request requireing special handling. */
			} else if (bp->bio_pflags & GV_BIO_INTERNAL) {
				p = bp->bio_caller1;
				gv_plex_start(p, bp);
			} else {
				gv_volume_start(sc, bp);
			}
			mtx_lock(&sc->bqueue_mtx);
		}
		/* Then do completed requests. */
		bp = bioq_takefirst(sc->bqueue_up);
		if (bp == NULL) {
			msleep(sc, &sc->bqueue_mtx, PRIBIO, "-", hz/10);
			mtx_unlock(&sc->bqueue_mtx);
			continue;
		}
		mtx_unlock(&sc->bqueue_mtx);
		gv_bio_done(sc, bp);
	}
}

#define	VINUM_CLASS_NAME "VINUM"

static struct g_class g_vinum_class	= {
	.name = VINUM_CLASS_NAME,
	.version = G_VERSION,
	.init = gv_init,
	.taste = gv_taste,
	.ctlreq = gv_config,
	.destroy_geom = gv_unload,
};

DECLARE_GEOM_CLASS(g_vinum_class, g_vinum);
MODULE_VERSION(geom_vinum, 0);