DragonFlyBSD Kernel Audit
sys/kern/subr_disk.c
← back
   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
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
1219
1220
1221
1222
1223
1224
1225
1226
1227
1228
1229
1230
1231
1232
1233
1234
1235
1236
1237
1238
1239
1240
1241
1242
1243
1244
1245
1246
1247
1248
1249
1250
1251
1252
1253
1254
1255
1256
1257
1258
1259
1260
1261
1262
1263
1264
1265
1266
1267
1268
1269
1270
1271
1272
1273
1274
1275
1276
1277
1278
1279
1280
1281
1282
1283
1284
1285
1286
1287
1288
1289
1290
1291
1292
1293
1294
1295
1296
1297
1298
1299
1300
1301
1302
1303
1304
1305
1306
1307
1308
1309
1310
1311
1312
1313
1314
1315
1316
1317
1318
1319
1320
1321
1322
1323
1324
1325
1326
1327
1328
1329
1330
1331
1332
1333
1334
1335
1336
1337
1338
1339
1340
1341
1342
1343
1344
1345
1346
1347
1348
1349
1350
1351
1352
1353
1354
1355
1356
1357
1358
1359
1360
1361
1362
1363
1364
1365
1366
1367
1368
1369
1370
1371
1372
1373
1374
1375
1376
1377
1378
1379
1380
1381
1382
1383
1384
1385
1386
1387
1388
1389
1390
1391
1392
1393
1394
1395
1396
1397
1398
1399
1400
1401
1402
1403
1404
1405
1406
1407
1408
1409
1410
1411
1412
1413
1414
1415
1416
1417
1418
1419
1420
1421
1422
1423
1424
1425
1426
1427
1428
1429
1430
1431
1432
1433
1434
1435
1436
1437
1438
1439
1440
1441
1442
1443
1444
1445
1446
1447
1448
1449
1450
1451
1452
1453
1454
1455
1456
1457
1458
1459
1460
1461
1462
1463
1464
1465
1466
1467
1468
1469
1470
1471
1472
1473
1474
1475
1476
1477
1478
1479
1480
1481
1482
1483
1484
1485
1486
1487
1488
1489
1490
1491
1492
1493
1494
1495
1496
1497
1498
1499
1500
1501
1502
1503
1504
1505
1506
1507
1508
1509
1510
1511
1512
1513
1514
1515
1516
1517
1518
1519
1520
1521
1522
1523
1524
1525
1526
1527
1528
1529
1530
1531
1532
1533
1534
1535
1536
1537
1538
1539
1540
1541
1542
1543
1544
1545
1546
1547
1548
1549
1550
1551
1552
1553
1554
1555
1556
1557
1558
1559
1560
1561
1562
1563
1564
1565
1566
1567
1568
1569
1570
1571
1572
1573
1574
1575
1576
1577
1578
1579
1580
1581
1582
1583
1584
1585
1586
1587
1588
1589
1590
1591
1592
1593
1594
1595
1596
1597
1598
1599
1600
/*
 * Copyright (c) 2003,2004,2009 The DragonFly Project.  All rights reserved.
 *
 * This code is derived from software contributed to The DragonFly Project
 * by Matthew Dillon <dillon@backplane.com>
 * and Alex Hornung <ahornung@gmail.com>
 *
 * 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.
 * 3. Neither the name of The DragonFly Project nor the names of its
 *    contributors may be used to endorse or promote products derived
 *    from this software without specific, prior written permission.
 *
 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS 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 THE
 * COPYRIGHT HOLDERS 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.
 *
 * ----------------------------------------------------------------------------
 * "THE BEER-WARE LICENSE" (Revision 42):
 * <phk@FreeBSD.ORG> wrote this file.  As long as you retain this notice you
 * can do whatever you want with this stuff. If we meet some day, and you think
 * this stuff is worth it, you can buy me a beer in return.   Poul-Henning Kamp
 * ----------------------------------------------------------------------------
 *
 * Copyright (c) 1982, 1986, 1988, 1993
 *	The Regents of the University of California.  All rights reserved.
 * (c) UNIX System Laboratories, Inc.
 * All or some portions of this file are derived from material licensed
 * to the University of California by American Telephone and Telegraph
 * Co. or Unix System Laboratories, Inc. and are reproduced herein with
 * the permission of UNIX System Laboratories, Inc.
 *
 * 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.
 * 3. Neither the name of the University nor the names of its contributors
 *    may be used to endorse or promote products derived from this software
 *    without specific prior written permission.
 *
 * THIS SOFTWARE IS PROVIDED BY THE REGENTS 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 THE REGENTS 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.
 *
 *	@(#)ufs_disksubr.c	8.5 (Berkeley) 1/21/94
 * $FreeBSD: src/sys/kern/subr_disk.c,v 1.20.2.6 2001/10/05 07:14:57 peter Exp $
 * $FreeBSD: src/sys/ufs/ufs/ufs_disksubr.c,v 1.44.2.3 2001/03/05 05:42:19 obrien Exp $
 */

#include <sys/param.h>
#include <sys/systm.h>
#include <sys/kernel.h>
#include <sys/proc.h>
#include <sys/sysctl.h>
#include <sys/buf.h>
#include <sys/caps.h>
#include <sys/conf.h>
#include <sys/disklabel.h>
#include <sys/disklabel32.h>
#include <sys/disklabel64.h>
#include <sys/diskslice.h>
#include <sys/diskmbr.h>
#include <sys/disk.h>
#include <sys/kerneldump.h>
#include <sys/malloc.h>
#include <machine/md_var.h>
#include <sys/ctype.h>
#include <sys/syslog.h>
#include <sys/device.h>
#include <sys/msgport.h>
#include <sys/devfs.h>
#include <sys/thread.h>
#include <sys/dsched.h>
#include <sys/queue.h>
#include <sys/lock.h>
#include <sys/udev.h>
#include <sys/uuid.h>

#include <sys/buf2.h>
#include <sys/msgport2.h>

static MALLOC_DEFINE(M_DISK, "disk", "disk data");
static int disk_debug_enable = 0;

static void disk_msg_autofree_reply(lwkt_port_t, lwkt_msg_t);
static void disk_msg_core(void *);
static int disk_probe_slice(struct disk *dp, cdev_t dev, int slice, int reprobe);
static void disk_probe(struct disk *dp, int reprobe);
static void _setdiskinfo(struct disk *disk, struct disk_info *info);
static void bioqwritereorder(struct bio_queue_head *bioq);
static void disk_cleanname(char *name);
static int disk_debug(int, char *, ...) __printflike(2, 3);
static cdev_t _disk_create_named(const char *name, int unit, struct disk *dp,
    struct dev_ops *raw_ops, int clone);

static d_open_t diskopen;
static d_close_t diskclose;
static d_ioctl_t diskioctl;
static d_strategy_t diskstrategy;
static d_psize_t diskpsize;
static d_dump_t diskdump;

static LIST_HEAD(, disk) disklist = LIST_HEAD_INITIALIZER(&disklist);
static struct lwkt_token disklist_token;
static struct lwkt_token ds_token;

static struct dev_ops disk1_ops = {
	{ "disk", 0, D_DISK | D_MPSAFE | D_TRACKCLOSE | D_KVABIO },
	.d_open = diskopen,
	.d_close = diskclose,
	.d_read = physread,
	.d_write = physwrite,
	.d_ioctl = diskioctl,
	.d_strategy = diskstrategy,
	.d_dump = diskdump,
	.d_psize = diskpsize,
};

static struct dev_ops disk2_ops = {
	{ "disk", 0, D_DISK | D_MPSAFE | D_TRACKCLOSE | D_KVABIO |
		     D_NOEMERGPGR },
	.d_open = diskopen,
	.d_close = diskclose,
	.d_read = physread,
	.d_write = physwrite,
	.d_ioctl = diskioctl,
	.d_strategy = diskstrategy,
	.d_dump = diskdump,
	.d_psize = diskpsize,
};

static struct objcache 	*disk_msg_cache;

static struct objcache_malloc_args disk_msg_malloc_args = {
	sizeof(struct disk_msg),
	M_DISK
};

static struct lwkt_port disk_dispose_port;
static struct lwkt_port disk_msg_port;

static int
disk_debug(int level, char *fmt, ...)
{
	__va_list ap;

	__va_start(ap, fmt);
	if (level <= disk_debug_enable)
		kvprintf(fmt, ap);
	__va_end(ap);

	return 0;
}

static int
disk_probe_slice(struct disk *dp, cdev_t dev, int slice, int reprobe)
{
	struct disk_info *info = &dp->d_info;
	struct diskslice *sp = &dp->d_slice->dss_slices[slice];
	disklabel_ops_t ops;
	struct dev_ops *dops;
	struct partinfo part;
	const char *msg;
	char uuid_buf[128];
	cdev_t ndev;
	int sno;
	u_int i;

	disk_debug(2, "disk_probe_slice (begin): %s (%s)\n",
		   dev->si_name, dp->d_cdev->si_name);

	sno = slice ? slice - 1 : 0;
	dops = (dp->d_rawdev->si_ops->head.flags & D_NOEMERGPGR) ?
		&disk2_ops : &disk1_ops;

	ops = &disklabel32_ops;
	msg = ops->op_readdisklabel(dev, sp, &sp->ds_label, info);
	if (msg && !strcmp(msg, "no disk label")) {
		ops = &disklabel64_ops;
		msg = ops->op_readdisklabel(dev, sp, &sp->ds_label, info);
	}

	if (msg == NULL) {
		char packname[DISKLABEL_MAXPACKNAME];

		if (slice != WHOLE_DISK_SLICE)
			ops->op_adjust_label_reserved(dp->d_slice, slice, sp);
		else
			sp->ds_reserved = 0;

		ops->op_getpackname(sp->ds_label, packname, sizeof(packname));
		disk_cleanname(packname);

		destroy_dev_alias(dev, "by-label/*");
		if (packname[0])
			make_dev_alias(dev, "by-label/%s", packname);

		sp->ds_ops = ops;
		for (i = 0; i < ops->op_getnumparts(sp->ds_label); i++) {
			ops->op_loadpartinfo(sp->ds_label, i, &part);

			if (part.fstype) {
				if (reprobe &&
				    (ndev = devfs_find_device_by_name("%s%c",
						dev->si_name, 'a' + i))
				) {
					/*
					 * Device already exists and
					 * is still valid.
					 */
					ndev->si_flags |= SI_REPROBE_TEST;

					/* Destroy old UUID alias */
					destroy_dev_alias(ndev,
							  "part-by-uuid/*");
					destroy_dev_alias(ndev,
							  "part-by-label/*");

					/* Create UUID alias */
					if (!kuuid_is_nil(&part.storage_uuid)) {
						snprintf_uuid(uuid_buf,
						    sizeof(uuid_buf),
						    &part.storage_uuid);
						make_dev_alias(ndev,
						    "part-by-uuid/%s",
						    uuid_buf);
						udev_dict_set_cstr(ndev,
						    "uuid", uuid_buf);
					}
					if (packname[0]) {
						make_dev_alias(ndev,
						    "part-by-label/%s.%c",
						    packname, 'a' + i);
					}
				} else {
					ndev = make_dev_covering(dops,
						dp->d_rawdev->si_ops,
						dkmakeminor(dkunit(dp->d_cdev),
							    slice, i),
						UID_ROOT, GID_OPERATOR, 0640,
						"%s%c", dev->si_name, 'a'+ i);
					ndev->si_parent = dev;
					ndev->si_iosize_max = dev->si_iosize_max;
					ndev->si_disk = dp;
					udev_dict_set_cstr(ndev, "subsystem", "disk");
					/* Inherit parent's disk type */
					if (dp->d_disktype) {
						udev_dict_set_cstr(ndev, "disk-type",
						    __DECONST(char *, dp->d_disktype));
					}

					/* Create serno alias */
					if (dp->d_info.d_serialno) {
						make_dev_alias(ndev,
						    "serno/%s.s%d%c",
						    dp->d_info.d_serialno,
						    sno, 'a' + i);
					}

					/* Create UUID alias */
					if (!kuuid_is_nil(&part.storage_uuid)) {
						snprintf_uuid(uuid_buf,
						    sizeof(uuid_buf),
						    &part.storage_uuid);
						make_dev_alias(ndev,
						    "part-by-uuid/%s",
						    uuid_buf);
						udev_dict_set_cstr(ndev, "uuid", uuid_buf);
					}
					if (packname[0]) {
						make_dev_alias(ndev,
						    "part-by-label/%s.%c",
						    packname, 'a' + i);
					}
					ndev->si_flags |= SI_REPROBE_TEST;
				}
			}
		}
	} else if (info->d_dsflags & DSO_COMPATLABEL) {
		msg = NULL;
		if (sp->ds_size >= 0x100000000ULL)
			ops = &disklabel64_ops;
		else
			ops = &disklabel32_ops;
		sp->ds_label = ops->op_clone_label(info, sp);
	} else {
		if (sp->ds_type == DOSPTYP_386BSD || /* XXX */
		    sp->ds_type == DOSPTYP_NETBSD ||
		    sp->ds_type == DOSPTYP_OPENBSD ||
		    sp->ds_type == DOSPTYP_DFLYBSD) {
			log(LOG_WARNING, "%s: cannot find label (%s)\n",
			    dev->si_name, msg);
		}

		if (sp->ds_label.opaque != NULL && sp->ds_ops != NULL) {
			/* Clear out old label - it's not around anymore */
			disk_debug(2,
			    "disk_probe_slice: clear out old diskabel on %s\n",
			    dev->si_name);

			sp->ds_ops->op_freedisklabel(&sp->ds_label);
			sp->ds_ops = NULL;
		}
	}

	if (msg == NULL) {
		sp->ds_wlabel = FALSE;
	}

	return (msg ? EINVAL : 0);
}

/*
 * This routine is only called for newly minted drives or to reprobe
 * a drive with no open slices.  disk_probe_slice() is called directly
 * when reprobing partition changes within slices.
 */
static void
disk_probe(struct disk *dp, int reprobe)
{
	struct disk_info *info = &dp->d_info;
	cdev_t dev = dp->d_cdev;
	cdev_t ndev;
	int error, i, sno;
	struct diskslices *osp;
	struct diskslice *sp;
	struct dev_ops *dops;
	char uuid_buf[128];
	char name_buf[64];

	/*
	 * d_media_blksize can be 0 for non-disk storage devices such
	 * as audio CDs.
	 */
	if (info->d_media_blksize == 0)
		return;

	osp = dp->d_slice;
	dp->d_slice = dsmakeslicestruct(BASE_SLICE, info);
	disk_debug(1, "disk_probe (begin): %s\n", dp->d_cdev->si_name);

	error = mbrinit(dev, info, &(dp->d_slice));
	if (error) {
		dsgone(&osp);
		return;
	}

	dops = (dp->d_rawdev->si_ops->head.flags & D_NOEMERGPGR) ?
		&disk2_ops : &disk1_ops;

	for (i = 0; i < dp->d_slice->dss_nslices; i++) {
		/*
		 * Ignore the whole-disk slice, it has already been created.
		 */
		if (i == WHOLE_DISK_SLICE)
			continue;

#if 1
		/*
		 * Ignore the compatibility slice s0 if it's a device mapper
		 * volume.
		 */
		if ((i == COMPATIBILITY_SLICE) &&
		    (info->d_dsflags & DSO_DEVICEMAPPER))
			continue;
#endif

		sp = &dp->d_slice->dss_slices[i];

		/*
		 * Handle s0.  s0 is a compatibility slice if there are no
		 * other slices and it has not otherwise been set up, else
		 * we ignore it.
		 */
		if (i == COMPATIBILITY_SLICE) {
			sno = 0;
			if (sp->ds_type == 0 &&
			    dp->d_slice->dss_nslices == BASE_SLICE) {
				sp->ds_size = info->d_media_blocks;
				sp->ds_reserved = 0;
			}
		} else {
			sno = i - 1;
			sp->ds_reserved = 0;
		}

		/*
		 * Ignore 0-length slices
		 */
		if (sp->ds_size == 0)
			continue;

		ksnprintf(name_buf, sizeof(name_buf),
			  ((info->d_dsflags & DSO_DEVICEMAPPER)
			   ? "%s.s%d" : "%ss%d"),
			  dev->si_name, sno);

		if (reprobe &&
		    (ndev = devfs_find_device_by_name("%s", name_buf))) {
			/*
			 * Device already exists and is still valid
			 */
			ndev->si_flags |= SI_REPROBE_TEST;

			/* Destroy old UUID alias */
			destroy_dev_alias(ndev, "slice-by-uuid/*");

			/* Create UUID alias */
			if (!kuuid_is_nil(&sp->ds_stor_uuid)) {
				snprintf_uuid(uuid_buf, sizeof(uuid_buf),
				    &sp->ds_stor_uuid);
				make_dev_alias(ndev, "slice-by-uuid/%s",
				    uuid_buf);
			}
		} else {
			/*
			 * Else create new device
			 */
			ndev = make_dev_covering(dops, dp->d_rawdev->si_ops,
					dkmakewholeslice(dkunit(dev), i),
					UID_ROOT, GID_OPERATOR, 0640,
					"%s", name_buf);
			ndev->si_parent = dev;
			ndev->si_iosize_max = dev->si_iosize_max;
			udev_dict_set_cstr(ndev, "subsystem", "disk");
			/* Inherit parent's disk type */
			if (dp->d_disktype) {
				udev_dict_set_cstr(ndev, "disk-type",
				    __DECONST(char *, dp->d_disktype));
			}

			/* Create serno alias */
			if (dp->d_info.d_serialno) {
				make_dev_alias(ndev, "serno/%s.s%d",
					       dp->d_info.d_serialno, sno);
			}

			/* Create UUID alias */
			if (!kuuid_is_nil(&sp->ds_stor_uuid)) {
				snprintf_uuid(uuid_buf, sizeof(uuid_buf),
				    &sp->ds_stor_uuid);
				make_dev_alias(ndev, "slice-by-uuid/%s",
				    uuid_buf);
			}

			ndev->si_disk = dp;
			ndev->si_flags |= SI_REPROBE_TEST;
		}
		sp->ds_dev = ndev;

		/*
		 * Probe appropriate slices for a disklabel
		 */
		if (i == COMPATIBILITY_SLICE ||
		    sp->ds_type == DOSPTYP_386BSD ||
		    sp->ds_type == DOSPTYP_NETBSD ||
		    sp->ds_type == DOSPTYP_OPENBSD ||
		    sp->ds_type == DOSPTYP_DFLYBSD)
		{
			if (dp->d_slice->dss_first_bsd_slice == 0)
				dp->d_slice->dss_first_bsd_slice = i;
			disk_probe_slice(dp, ndev, i, reprobe);
		}
	}
	dsgone(&osp);
	disk_debug(1, "disk_probe (end): %s\n", dp->d_cdev->si_name);
}


static void
disk_msg_core(void *arg)
{
	struct disk	*dp;
	struct diskslice *sp;
	disk_msg_t msg;
	int run;

	lwkt_gettoken(&disklist_token);
	lwkt_initport_thread(&disk_msg_port, curthread);
	wakeup(curthread);	/* synchronous startup */
	lwkt_reltoken(&disklist_token);

	lwkt_gettoken(&ds_token);
	run = 1;

	while (run) {
		msg = (disk_msg_t)lwkt_waitport(&disk_msg_port, 0);

		switch (msg->hdr.u.ms_result) {
		case DISK_DISK_PROBE:
			dp = (struct disk *)msg->load;
			disk_debug(1,
				    "DISK_DISK_PROBE: %s\n",
					dp->d_cdev->si_name);
			disk_iocom_update(dp);
			disk_probe(dp, 0);
			break;
		case DISK_DISK_DESTROY:
			dp = (struct disk *)msg->load;
			disk_debug(1,
				    "DISK_DISK_DESTROY: %s\n",
					dp->d_cdev->si_name);
			disk_iocom_uninit(dp);

			/*
			 * Interlock against struct disk enumerations.
			 * Wait for enumerations to complete then remove
			 * the dp from the list before tearing it down.
			 * This avoids numerous races.
			 */
			lwkt_gettoken(&disklist_token);
			while (dp->d_refs)
				tsleep(&dp->d_refs, 0, "diskdel", hz / 10);
			LIST_REMOVE(dp, d_list);

			dsched_disk_destroy(dp);
			devfs_destroy_related(dp->d_cdev);
			destroy_dev(dp->d_cdev);
			destroy_only_dev(dp->d_rawdev);

			lwkt_reltoken(&disklist_token);

			if (dp->d_info.d_serialno) {
				kfree(dp->d_info.d_serialno, M_TEMP);
				dp->d_info.d_serialno = NULL;
			}
			break;
		case DISK_UNPROBE:
			dp = (struct disk *)msg->load;
			disk_debug(1,
				    "DISK_DISK_UNPROBE: %s\n",
					dp->d_cdev->si_name);
			devfs_destroy_related(dp->d_cdev);
			break;
		case DISK_SLICE_REPROBE:
			dp = (struct disk *)msg->load;
			sp = (struct diskslice *)msg->load2;
			devfs_clr_related_flag(sp->ds_dev,
						SI_REPROBE_TEST);
			disk_debug(1,
				    "DISK_SLICE_REPROBE: %s\n",
				    sp->ds_dev->si_name);
			disk_probe_slice(dp, sp->ds_dev,
					 dkslice(sp->ds_dev), 1);
			devfs_destroy_related_without_flag(
					sp->ds_dev, SI_REPROBE_TEST);
			break;
		case DISK_DISK_REPROBE:
			dp = (struct disk *)msg->load;
			devfs_clr_related_flag(dp->d_cdev, SI_REPROBE_TEST);
			disk_debug(1,
				    "DISK_DISK_REPROBE: %s\n",
				    dp->d_cdev->si_name);
			disk_probe(dp, 1);
			devfs_destroy_related_without_flag(
					dp->d_cdev, SI_REPROBE_TEST);
			break;
		case DISK_SYNC:
			disk_debug(1, "DISK_SYNC\n");
			break;
		default:
			devfs_debug(DEVFS_DEBUG_WARNING,
				    "disk_msg_core: unknown message "
				    "received at core\n");
			break;
		}
		lwkt_replymsg(&msg->hdr, 0);
	}
	lwkt_reltoken(&ds_token);
	lwkt_exit();
}


/*
 * Acts as a message drain. Any message that is replied to here gets
 * destroyed and the memory freed.
 */
static void
disk_msg_autofree_reply(lwkt_port_t port, lwkt_msg_t msg)
{
	objcache_put(disk_msg_cache, msg);
}


void
disk_msg_send(uint32_t cmd, void *load, void *load2)
{
	disk_msg_t disk_msg;
	lwkt_port_t port = &disk_msg_port;

	disk_msg = objcache_get(disk_msg_cache, M_WAITOK);

	lwkt_initmsg(&disk_msg->hdr, &disk_dispose_port, 0);

	disk_msg->hdr.u.ms_result = cmd;
	disk_msg->load = load;
	disk_msg->load2 = load2;
	KKASSERT(port);
	lwkt_sendmsg(port, &disk_msg->hdr);
}

void
disk_msg_send_sync(uint32_t cmd, void *load, void *load2)
{
	struct lwkt_port rep_port;
	disk_msg_t disk_msg;
	lwkt_port_t port;

	disk_msg = objcache_get(disk_msg_cache, M_WAITOK);
	port = &disk_msg_port;

	/* XXX could probably use curthread's built-in msgport */
	lwkt_initport_thread(&rep_port, curthread);
	lwkt_initmsg(&disk_msg->hdr, &rep_port, 0);

	disk_msg->hdr.u.ms_result = cmd;
	disk_msg->load = load;
	disk_msg->load2 = load2;

	lwkt_domsg(port, &disk_msg->hdr, 0);
	objcache_put(disk_msg_cache, disk_msg);
}

/*
 * Create a raw device for the dev_ops template (which is returned).  Also
 * create a slice and unit managed disk and overload the user visible
 * device space with it.
 *
 * NOTE: The returned raw device is NOT a slice and unit managed device.
 * It is an actual raw device representing the raw disk as specified by
 * the passed dev_ops.  The disk layer not only returns such a raw device,
 * it also uses it internally when passing (modified) commands through.
 */
cdev_t
disk_create(int unit, struct disk *dp, struct dev_ops *raw_ops)
{
	return _disk_create_named(NULL, unit, dp, raw_ops, 0);
}

cdev_t
disk_create_clone(int unit, struct disk *dp,
		  struct dev_ops *raw_ops)
{
	return _disk_create_named(NULL, unit, dp, raw_ops, 1);
}

cdev_t
disk_create_named(const char *name, int unit, struct disk *dp,
		  struct dev_ops *raw_ops)
{
	return _disk_create_named(name, unit, dp, raw_ops, 0);
}

cdev_t
disk_create_named_clone(const char *name, int unit, struct disk *dp,
			struct dev_ops *raw_ops)
{
	return _disk_create_named(name, unit, dp, raw_ops, 1);
}

static cdev_t
_disk_create_named(const char *name, int unit, struct disk *dp,
		   struct dev_ops *raw_ops, int clone)
{
	cdev_t rawdev;
	struct dev_ops *dops;

	disk_debug(1, "disk_create (begin): %s%d\n", name, unit);

	if (name) {
		rawdev = make_only_dev(raw_ops, dkmakewholedisk(unit),
		    UID_ROOT, GID_OPERATOR, 0640, "%s", name);
	} else {
		rawdev = make_only_dev(raw_ops, dkmakewholedisk(unit),
		    UID_ROOT, GID_OPERATOR, 0640,
		    "%s%d", raw_ops->head.name, unit);
	}

	bzero(dp, sizeof(*dp));

	dops = (raw_ops->head.flags & D_NOEMERGPGR) ? &disk2_ops : &disk1_ops;

	dp->d_rawdev = rawdev;
	dp->d_raw_ops = raw_ops;
	dp->d_dev_ops = dops;

	if (name) {
		if (clone) {
			dp->d_cdev = make_only_dev_covering(
					dops, dp->d_rawdev->si_ops,
					dkmakewholedisk(unit),
					UID_ROOT, GID_OPERATOR, 0640,
					"%s", name);
		} else {
			dp->d_cdev = make_dev_covering(
					dops, dp->d_rawdev->si_ops,
					dkmakewholedisk(unit),
					UID_ROOT, GID_OPERATOR, 0640,
					"%s", name);
		}
	} else {
		if (clone) {
			dp->d_cdev = make_only_dev_covering(
					dops, dp->d_rawdev->si_ops,
					dkmakewholedisk(unit),
					UID_ROOT, GID_OPERATOR, 0640,
					"%s%d", raw_ops->head.name, unit);
		} else {
			dp->d_cdev = make_dev_covering(
					dops, dp->d_rawdev->si_ops,
					dkmakewholedisk(unit),
					UID_ROOT, GID_OPERATOR, 0640,
					"%s%d", raw_ops->head.name, unit);
		}
	}

	udev_dict_set_cstr(dp->d_cdev, "subsystem", "disk");
	dp->d_cdev->si_disk = dp;

	if (name)
		dsched_disk_create(dp, name, unit);
	else
		dsched_disk_create(dp, raw_ops->head.name, unit);

	lwkt_gettoken(&disklist_token);
	LIST_INSERT_HEAD(&disklist, dp, d_list);
	lwkt_reltoken(&disklist_token);

	disk_iocom_init(dp);

	disk_debug(1, "disk_create (end): %s%d\n",
		   (name != NULL)?(name):(raw_ops->head.name), unit);

	return (dp->d_rawdev);
}

int
disk_setdisktype(struct disk *disk, const char *type)
{
	int error;

	KKASSERT(disk != NULL);

	disk->d_disktype = type;
	error = udev_dict_set_cstr(disk->d_cdev, "disk-type",
				   __DECONST(char *, type));
	return error;
}

int
disk_getopencount(struct disk *disk)
{
	return disk->d_opencount;
}

static void
_setdiskinfo(struct disk *disk, struct disk_info *info)
{
	char *oldserialno;

	oldserialno = disk->d_info.d_serialno;
	bcopy(info, &disk->d_info, sizeof(disk->d_info));
	info = &disk->d_info;

	disk_debug(1, "_setdiskinfo: %s\n", disk->d_cdev->si_name);

	/*
	 * The serial number is duplicated so the caller can throw
	 * their copy away.
	 */
	if (info->d_serialno && info->d_serialno[0] &&
	    (info->d_serialno[0] != ' ' || strlen(info->d_serialno) > 1)) {
		info->d_serialno = kstrdup(info->d_serialno, M_TEMP);
		disk_cleanname(info->d_serialno);
		if (disk->d_cdev) {
			make_dev_alias(disk->d_cdev, "serno/%s",
				       info->d_serialno);
		}
	} else {
		info->d_serialno = NULL;
	}
	if (oldserialno)
		kfree(oldserialno, M_TEMP);

	dsched_disk_update(disk, info);

	/*
	 * The caller may set d_media_size or d_media_blocks and we
	 * calculate the other.
	 */
	KKASSERT(info->d_media_size == 0 || info->d_media_blocks == 0);
	if (info->d_media_size == 0 && info->d_media_blocks) {
		info->d_media_size = (u_int64_t)info->d_media_blocks *
				     info->d_media_blksize;
	} else if (info->d_media_size && info->d_media_blocks == 0 &&
		   info->d_media_blksize) {
		info->d_media_blocks = info->d_media_size /
				       info->d_media_blksize;
	}

	/*
	 * The si_* fields for rawdev are not set until after the
	 * disk_create() call, so someone using the cooked version
	 * of the raw device (i.e. da0s0) will not get the right
	 * si_iosize_max unless we fix it up here.
	 */
	if (disk->d_cdev && disk->d_rawdev &&
	    disk->d_cdev->si_iosize_max == 0) {
		disk->d_cdev->si_iosize_max = disk->d_rawdev->si_iosize_max;
		disk->d_cdev->si_bsize_phys = disk->d_rawdev->si_bsize_phys;
		disk->d_cdev->si_bsize_best = disk->d_rawdev->si_bsize_best;
	}

	/* Add the serial number to the udev_dictionary */
	if (info->d_serialno)
		udev_dict_set_cstr(disk->d_cdev, "serno", info->d_serialno);
}

/*
 * Disk drivers must call this routine when media parameters are available
 * or have changed.
 */
void
disk_setdiskinfo(struct disk *disk, struct disk_info *info)
{
	_setdiskinfo(disk, info);
	disk_msg_send(DISK_DISK_PROBE, disk, NULL);
	disk_debug(1, "disk_setdiskinfo: sent probe for %s\n",
		   disk->d_cdev->si_name);
}

void
disk_setdiskinfo_sync(struct disk *disk, struct disk_info *info)
{
	_setdiskinfo(disk, info);
	disk_msg_send_sync(DISK_DISK_PROBE, disk, NULL);
	disk_debug(1, "disk_setdiskinfo_sync: sent probe for %s\n",
		   disk->d_cdev->si_name);
}

/*
 * This routine is called when an adapter detaches.  The higher level
 * managed disk device is destroyed while the lower level raw device is
 * released.
 */
void
disk_destroy(struct disk *disk)
{
	disk_msg_send_sync(DISK_DISK_DESTROY, disk, NULL);
	return;
}

int
disk_dumpcheck(cdev_t dev, u_int64_t *size,
	       u_int64_t *blkno, u_int32_t *secsize)
{
	struct partinfo pinfo;
	int error;

	if (size)
		*size = 0;	/* avoid gcc warnings */
	if (secsize)
		*secsize = 512;	/* avoid gcc warnings */
	bzero(&pinfo, sizeof(pinfo));

	error = dev_dioctl(dev, DIOCGPART, (void *)&pinfo, 0,
			   proc0.p_ucred, NULL, NULL);
	if (error)
		return (error);

	if (pinfo.media_blksize == 0)
		return (ENXIO);

	if (blkno) /* XXX: make sure this reserved stuff is right */
		*blkno = pinfo.reserved_blocks +
			pinfo.media_offset / pinfo.media_blksize;
	if (secsize)
		*secsize = pinfo.media_blksize;
	if (size)
		*size = (pinfo.media_blocks - pinfo.reserved_blocks);

	return (0);
}

int
disk_dumpconf(cdev_t dev, u_int onoff)
{
	struct dumperinfo di;
	u_int64_t	size, blkno;
	u_int32_t	secsize;
	int error;

	if (!onoff)
		return set_dumper(NULL);

	error = disk_dumpcheck(dev, &size, &blkno, &secsize);

	if (error)
		return ENXIO;

	bzero(&di, sizeof(struct dumperinfo));
	di.dumper = diskdump;
	di.priv = dev;
	di.blocksize = secsize;
	di.maxiosize = dev->si_iosize_max;
	di.mediaoffset = blkno * DEV_BSIZE;
	di.mediasize = size * DEV_BSIZE;

	return set_dumper(&di);
}

void
disk_unprobe(struct disk *disk)
{
	if (disk == NULL)
		return;

	disk_msg_send_sync(DISK_UNPROBE, disk, NULL);
}

void
disk_invalidate (struct disk *disk)
{
	dsgone(&disk->d_slice);
}

/*
 * Enumerate disks, pass a marker and an initial NULL dp to initialize,
 * then loop with the previously returned dp.
 *
 * The returned dp will be referenced, preventing its destruction.  When
 * you pass the returned dp back into the loop the ref is dropped.
 *
 * WARNING: If terminating your loop early you must call
 *	    disk_enumerate_stop().
 */
struct disk *
disk_enumerate(struct disk *marker, struct disk *dp)
{
	lwkt_gettoken(&disklist_token);
	if (dp) {
		--dp->d_refs;
		dp = LIST_NEXT(marker, d_list);
		LIST_REMOVE(marker, d_list);
	} else {
		bzero(marker, sizeof(*marker));
		marker->d_flags = DISKFLAG_MARKER;
		dp = LIST_FIRST(&disklist);
	}
	while (dp) {
		if ((dp->d_flags & DISKFLAG_MARKER) == 0)
			break;
		dp = LIST_NEXT(dp, d_list);
	}
	if (dp) {
		++dp->d_refs;
		LIST_INSERT_AFTER(dp, marker, d_list);
	}
	lwkt_reltoken(&disklist_token);
	return (dp);
}

/*
 * Terminate an enumeration early.  Do not call this function if the
 * enumeration ended normally.  dp can be NULL, indicating that you
 * wish to retain the ref count on dp.
 *
 * This function removes the marker.
 */
void
disk_enumerate_stop(struct disk *marker, struct disk *dp)
{
	lwkt_gettoken(&disklist_token);
	LIST_REMOVE(marker, d_list);
	if (dp)
		--dp->d_refs;
	lwkt_reltoken(&disklist_token);
}

static
int
sysctl_disks(SYSCTL_HANDLER_ARGS)
{
	struct disk marker;
	struct disk *dp;
	int error, first;

	first = 1;
	error = 0;
	dp = NULL;

	while ((dp = disk_enumerate(&marker, dp))) {
		if (!first) {
			error = SYSCTL_OUT(req, " ", 1);
			if (error) {
				disk_enumerate_stop(&marker, dp);
				break;
			}
		} else {
			first = 0;
		}
		error = SYSCTL_OUT(req, dp->d_rawdev->si_name,
				   strlen(dp->d_rawdev->si_name));
		if (error) {
			disk_enumerate_stop(&marker, dp);
			break;
		}
	}
	if (error == 0)
		error = SYSCTL_OUT(req, "", 1);
	return error;
}

SYSCTL_PROC(_kern, OID_AUTO, disks, CTLTYPE_STRING | CTLFLAG_RD, NULL, 0,
    sysctl_disks, "A", "names of available disks");

/*
 * Open a disk device or partition.
 */
static
int
diskopen(struct dev_open_args *ap)
{
	cdev_t dev = ap->a_head.a_dev;
	struct disk *dp;
	int error;

	/*
	 * dp can't be NULL here XXX.
	 *
	 * d_slice will be NULL if setdiskinfo() has not been called yet.
	 * setdiskinfo() is typically called whether the disk is present
	 * or not (e.g. CD), but the base disk device is created first
	 * and there may be a race.
	 */
	dp = dev->si_disk;
	if (dp == NULL || dp->d_slice == NULL)
		return (ENXIO);

	/*
	 * Disallow access to disk volumes if RESTRICTEDROOT
	 */
	if (caps_priv_check_self(SYSCAP_RESTRICTEDROOT))
		return (EPERM);

	error = 0;

	/*
	 * Deal with open races
	 */
	lwkt_gettoken(&ds_token);
	while (dp->d_flags & DISKFLAG_LOCK) {
		dp->d_flags |= DISKFLAG_WANTED;
		error = tsleep(dp, PCATCH, "diskopen", hz);
		if (error) {
			lwkt_reltoken(&ds_token);
			return (error);
		}
	}
	dp->d_flags |= DISKFLAG_LOCK;

	/*
	 * Open the underlying raw device.
	 */
	if (!dsisopen(dp->d_slice)) {
#if 0
		if (!pdev->si_iosize_max)
			pdev->si_iosize_max = dev->si_iosize_max;
#endif
		error = dev_dopen(dp->d_rawdev, ap->a_oflags,
				  ap->a_devtype, ap->a_cred, NULL, NULL);
	}

	if (error)
		goto out;
	error = dsopen(dev, ap->a_devtype, dp->d_info.d_dsflags,
		       &dp->d_slice, &dp->d_info);
	if (!dsisopen(dp->d_slice)) {
		dev_dclose(dp->d_rawdev, ap->a_oflags, ap->a_devtype, NULL);
	}
out:
	dp->d_flags &= ~DISKFLAG_LOCK;
	if (dp->d_flags & DISKFLAG_WANTED) {
		dp->d_flags &= ~DISKFLAG_WANTED;
		wakeup(dp);
	}
	lwkt_reltoken(&ds_token);

	KKASSERT(dp->d_opencount >= 0);
	/* If the open was successful, bump open count */
	if (error == 0)
		atomic_add_int(&dp->d_opencount, 1);

	return(error);
}

/*
 * Close a disk device or partition
 */
static
int
diskclose(struct dev_close_args *ap)
{
	cdev_t dev = ap->a_head.a_dev;
	struct disk *dp;
	int error;
	int lcount;

	error = 0;
	dp = dev->si_disk;

	/*
	 * The cdev_t represents the disk/slice/part.  The shared
	 * dp structure governs all cdevs associated with the disk.
	 *
	 * As a safety only close the underlying raw device on the last
	 * close the disk device if our tracking of the slices/partitions
	 * also indicates nothing is open.
	 */
	KKASSERT(dp->d_opencount >= 1);
	lcount = atomic_fetchadd_int(&dp->d_opencount, -1);

	lwkt_gettoken(&ds_token);
	dsclose(dev, ap->a_devtype, dp->d_slice);
	if (lcount <= 1 && !dsisopen(dp->d_slice)) {
		error = dev_dclose(dp->d_rawdev, ap->a_fflag, ap->a_devtype, NULL);
	}
	lwkt_reltoken(&ds_token);

	return (error);
}

/*
 * First execute the ioctl on the disk device, and if it isn't supported
 * try running it on the backing device.
 */
static
int
diskioctl(struct dev_ioctl_args *ap)
{
	cdev_t dev = ap->a_head.a_dev;
	struct disk *dp;
	int error;
	u_int u;

	dp = dev->si_disk;
	if (dp == NULL)
		return (ENXIO);

	devfs_debug(DEVFS_DEBUG_DEBUG,
		    "diskioctl: cmd is: %lx (name: %s)\n",
		    ap->a_cmd, dev->si_name);
	devfs_debug(DEVFS_DEBUG_DEBUG,
		    "diskioctl: &dp->d_slice is: %p, %p\n",
		    &dp->d_slice, dp->d_slice);

	if (ap->a_cmd == DIOCGKERNELDUMP) {
		u = *(u_int *)ap->a_data;
		return disk_dumpconf(dev, u);
	}

	if (ap->a_cmd == DIOCRECLUSTER && dev == dp->d_cdev) {
		error = disk_iocom_ioctl(dp, ap->a_cmd, ap->a_data);
		return error;
	}

	if (&dp->d_slice == NULL || dp->d_slice == NULL ||
	    ((dp->d_info.d_dsflags & DSO_DEVICEMAPPER) &&
	     dkslice(dev) == WHOLE_DISK_SLICE)) {
		error = ENOIOCTL;
	} else {
		lwkt_gettoken(&ds_token);
		error = dsioctl(dev, ap->a_cmd, ap->a_data, ap->a_fflag,
				&dp->d_slice, &dp->d_info);
		lwkt_reltoken(&ds_token);
	}

	if (error == ENOIOCTL) {
		error = dev_dioctl(dp->d_rawdev, ap->a_cmd, ap->a_data,
				   ap->a_fflag, ap->a_cred, NULL, NULL);
	}
	return (error);
}

/*
 * Execute strategy routine
 *
 * WARNING! We are using the KVABIO API and must not access memory
 *         through bp->b_data without first calling bkvasync(bp).
 */
static
int
diskstrategy(struct dev_strategy_args *ap)
{
	cdev_t dev = ap->a_head.a_dev;
	struct bio *bio = ap->a_bio;
	struct bio *nbio;
	struct disk *dp;

	dp = dev->si_disk;

	if (dp == NULL) {
		bio->bio_buf->b_error = ENXIO;
		bio->bio_buf->b_flags |= B_ERROR;
		biodone(bio);
		return(0);
	}
	KKASSERT(dev->si_disk == dp);

	/*
	 * The dscheck() function will also transform the slice relative
	 * block number i.e. bio->bio_offset into a block number that can be
	 * passed directly to the underlying raw device.  If dscheck()
	 * returns NULL it will have handled the bio for us (e.g. EOF
	 * or error due to being beyond the device size).
	 */
	if ((nbio = dscheck(dev, bio, dp->d_slice)) != NULL) {
		dev_dstrategy(dp->d_rawdev, nbio);
	} else {
		biodone(bio);
	}
	return(0);
}

/*
 * Return the partition size in ?blocks?
 */
static
int
diskpsize(struct dev_psize_args *ap)
{
	cdev_t dev = ap->a_head.a_dev;
	struct disk *dp;

	dp = dev->si_disk;
	if (dp == NULL)
		return(ENODEV);

	ap->a_result = dssize(dev, &dp->d_slice);

	if ((ap->a_result == -1) &&
	   (dp->d_info.d_dsflags & DSO_RAWPSIZE)) {
		ap->a_head.a_dev = dp->d_rawdev;
		return dev_doperate(&ap->a_head);
	}
	return(0);
}

static int
diskdump(struct dev_dump_args *ap)
{
	cdev_t dev = ap->a_head.a_dev;
	struct disk *dp = dev->si_disk;
	u_int64_t size, offset;
	int error;

	error = disk_dumpcheck(dev, &size, &ap->a_blkno, &ap->a_secsize);
	/* XXX: this should probably go in disk_dumpcheck somehow */
	if (ap->a_length != 0) {
		size *= DEV_BSIZE;
		offset = ap->a_blkno * DEV_BSIZE;
		if ((ap->a_offset < offset) ||
		    (ap->a_offset + ap->a_length - offset > size)) {
			kprintf("Attempt to write outside dump "
				"device boundaries.\n");
			error = ENOSPC;
		}
	}

	if (error == 0) {
		ap->a_head.a_dev = dp->d_rawdev;
		error = dev_doperate(&ap->a_head);
	}

	return(error);
}


SYSCTL_INT(_debug_sizeof, OID_AUTO, diskslices, CTLFLAG_RD,
	   0, sizeof(struct diskslices), "sizeof(struct diskslices)");

SYSCTL_INT(_debug_sizeof, OID_AUTO, disk, CTLFLAG_RD,
	   0, sizeof(struct disk), "sizeof(struct disk)");

/*
 * Reorder interval for burst write allowance and minor write
 * allowance.
 *
 * We always want to trickle some writes in to make use of the
 * disk's zone cache.  Bursting occurs on a longer interval and only
 * runningbufspace is well over the hirunningspace limit.
 */
int bioq_reorder_burst_interval = 60;	/* should be multiple of minor */
SYSCTL_INT(_kern, OID_AUTO, bioq_reorder_burst_interval,
	   CTLFLAG_RW, &bioq_reorder_burst_interval, 0, "");
int bioq_reorder_minor_interval = 5;
SYSCTL_INT(_kern, OID_AUTO, bioq_reorder_minor_interval,
	   CTLFLAG_RW, &bioq_reorder_minor_interval, 0, "");

int bioq_reorder_burst_bytes = 3000000;
SYSCTL_INT(_kern, OID_AUTO, bioq_reorder_burst_bytes,
	   CTLFLAG_RW, &bioq_reorder_burst_bytes, 0, "");
int bioq_reorder_minor_bytes = 262144;
SYSCTL_INT(_kern, OID_AUTO, bioq_reorder_minor_bytes,
	   CTLFLAG_RW, &bioq_reorder_minor_bytes, 0, "");


/*
 * Order I/Os.  Generally speaking this code is designed to make better
 * use of drive zone caches.  A drive zone cache can typically track linear
 * reads or writes for around 16 zones simultaniously.
 *
 * Read prioritization issues:  It is possible for hundreds of megabytes worth
 * of writes to be queued asynchronously.  This creates a huge bottleneck
 * for reads which reduce read bandwidth to a trickle.
 *
 * To solve this problem we generally reorder reads before writes.
 *
 * However, a large number of random reads can also starve writes and
 * make poor use of the drive zone cache so we allow writes to trickle
 * in every N reads.
 */
void
bioqdisksort(struct bio_queue_head *bioq, struct bio *bio)
{
#if 0
	/*
	 * The BIO wants to be ordered.  Adding to the tail also
	 * causes transition to be set to NULL, forcing the ordering
	 * of all prior I/O's.
	 */
	if (bio->bio_buf->b_flags & B_ORDERED) {
		bioq_insert_tail(bioq, bio);
		return;
	}
#endif

	switch(bio->bio_buf->b_cmd) {
	case BUF_CMD_READ:
		if (bioq->transition) {
			/*
			 * Insert before the first write.  Bleedover writes
			 * based on reorder intervals to prevent starvation.
			 */
			TAILQ_INSERT_BEFORE(bioq->transition, bio, bio_act);
			++bioq->reorder;
			if (bioq->reorder % bioq_reorder_minor_interval == 0) {
				bioqwritereorder(bioq);
				if (bioq->reorder >=
				    bioq_reorder_burst_interval) {
					bioq->reorder = 0;
				}
			}
		} else {
			/*
			 * No writes queued (or ordering was forced),
			 * insert at tail.
			 */
			TAILQ_INSERT_TAIL(&bioq->queue, bio, bio_act);
		}
		break;
	case BUF_CMD_WRITE:
		/*
		 * Writes are always appended.  If no writes were previously
		 * queued or an ordered tail insertion occured the transition
		 * field will be NULL.
		 */
		TAILQ_INSERT_TAIL(&bioq->queue, bio, bio_act);
		if (bioq->transition == NULL)
			bioq->transition = bio;
		break;
	default:
		/*
		 * All other request types are forced to be ordered.
		 */
		bioq_insert_tail(bioq, bio);
		break;
	}
}

/*
 * Move the read-write transition point to prevent reads from
 * completely starving our writes.  This brings a number of writes into
 * the fold every N reads.
 *
 * We bring a few linear writes into the fold on a minor interval
 * and we bring a non-linear burst of writes into the fold on a major
 * interval.  Bursting only occurs if runningbufspace is really high
 * (typically from syncs, fsyncs, or HAMMER flushes).
 */
static
void
bioqwritereorder(struct bio_queue_head *bioq)
{
	struct bio *bio;
	off_t next_offset;
	size_t left;
	size_t n;
	int check_off;

	if (bioq->reorder < bioq_reorder_burst_interval ||
	    !buf_runningbufspace_severe()) {
		left = (size_t)bioq_reorder_minor_bytes;
		check_off = 1;
	} else {
		left = (size_t)bioq_reorder_burst_bytes;
		check_off = 0;
	}

	next_offset = bioq->transition->bio_offset;
	while ((bio = bioq->transition) != NULL &&
	       (check_off == 0 || next_offset == bio->bio_offset)
	) {
		n = bio->bio_buf->b_bcount;
		next_offset = bio->bio_offset + n;
		bioq->transition = TAILQ_NEXT(bio, bio_act);
		if (left < n)
			break;
		left -= n;
	}
}

/*
 * Bounds checking against the media size, used for the raw partition.
 * secsize, mediasize and b_blkno must all be the same units.
 * Possibly this has to be DEV_BSIZE (512).
 */
int
bounds_check_with_mediasize(struct bio *bio, int secsize, uint64_t mediasize)
{
	struct buf *bp = bio->bio_buf;
	int64_t sz;

	sz = howmany(bp->b_bcount, secsize);

	if (bio->bio_offset/DEV_BSIZE + sz > mediasize) {
		sz = mediasize - bio->bio_offset/DEV_BSIZE;
		if (sz == 0) {
			/* If exactly at end of disk, return EOF. */
			bp->b_resid = bp->b_bcount;
			return 0;
		}
		if (sz < 0) {
			/* If past end of disk, return EINVAL. */
			bp->b_error = EINVAL;
			return 0;
		}
		/* Otherwise, truncate request. */
		bp->b_bcount = sz * secsize;
	}

	return 1;
}

/*
 * Disk error is the preface to plaintive error messages
 * about failing disk transfers.  It prints messages of the form

hp0g: hard error reading fsbn 12345 of 12344-12347 (hp0 bn %d cn %d tn %d sn %d)

 * if the offset of the error in the transfer and a disk label
 * are both available.  blkdone should be -1 if the position of the error
 * is unknown; the disklabel pointer may be null from drivers that have not
 * been converted to use them.  The message is printed with kprintf
 * if pri is LOG_PRINTF, otherwise it uses log at the specified priority.
 * The message should be completed (with at least a newline) with kprintf
 * or log(-1, ...), respectively.  There is no trailing space.
 */
void
diskerr(struct bio *bio, cdev_t dev, const char *what, int pri, int donecnt)
{
	struct buf *bp = bio->bio_buf;
	const char *term;

	switch(bp->b_cmd) {
	case BUF_CMD_READ:
		term = "read";
		break;
	case BUF_CMD_WRITE:
		term = "write";
		break;
	default:
		term = "access";
		break;
	}
	kprintf("%s: %s %sing ", dev->si_name, what, term);
	kprintf("offset %012llx for %d",
		(long long)bio->bio_offset,
		bp->b_bcount);

	if (donecnt)
		kprintf(" (%d bytes completed)", donecnt);
}

/*
 * Locate a disk device
 */
cdev_t
disk_locate(const char *devname)
{
	return devfs_find_device_by_name("%s", devname);
}

void
disk_config(void *arg)
{
	disk_msg_send_sync(DISK_SYNC, NULL, NULL);
}

static void
disk_init(void)
{
	struct thread* td_core;

	disk_msg_cache = objcache_create("disk-msg-cache", 0, 0,
					 NULL, NULL, NULL,
					 objcache_malloc_alloc,
					 objcache_malloc_free,
					 &disk_msg_malloc_args);

	lwkt_token_init(&disklist_token, "disks");
	lwkt_token_init(&ds_token, "ds");

	/*
	 * Initialize the reply-only port which acts as a message drain
	 */
	lwkt_initport_replyonly(&disk_dispose_port, disk_msg_autofree_reply);

	lwkt_gettoken(&disklist_token);
	lwkt_create(disk_msg_core, /*args*/NULL, &td_core, NULL,
		    0, -1, "disk_msg_core");
	tsleep(td_core, 0, "diskcore", 0);
	lwkt_reltoken(&disklist_token);
}

static void
disk_uninit(void)
{
	objcache_destroy(disk_msg_cache);
}

/*
 * Clean out illegal characters in a name, such as a serial number,
 * a disklabel packname.
 */
static void
disk_cleanname(char *name)
{
	char c;

	while ((c = *name) != 0) {
		if (c >= 'a' && c <= 'z')
			;
		else if (c >= 'A' && c <= 'Z')
			;
		else if (c >= '0' && c <= '9')
			;
		else if (c == '-' || c == '@' || c == '+' || c == '.')
			;
		else
			c = '_';
		*name++ = c;
	}
}

TUNABLE_INT("kern.disk_debug", &disk_debug_enable);
SYSCTL_INT(_kern, OID_AUTO, disk_debug, CTLFLAG_RW, &disk_debug_enable,
	   0, "Enable subr_disk debugging");

SYSINIT(disk_register, SI_SUB_PRE_DRIVERS, SI_ORDER_FIRST, disk_init, NULL);
SYSUNINIT(disk_register, SI_SUB_PRE_DRIVERS, SI_ORDER_ANY, disk_uninit, NULL);