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
1601
1602
1603
1604
1605
1606
1607
1608
1609
1610
1611
1612
1613
1614
1615
1616
1617
1618
1619
1620
1621
1622
1623
1624
1625
1626
1627
1628
1629
1630
1631
1632
1633
1634
1635
1636
1637
1638
1639
1640
1641
1642
1643
1644
1645
1646
1647
1648
1649
1650
1651
1652
1653
1654
1655
1656
1657
1658
1659
1660
1661
1662
1663
1664
1665
1666
1667
1668
1669
1670
1671
1672
1673
1674
1675
1676
1677
1678
1679
1680
1681
1682
1683
1684
1685
1686
1687
1688
1689
1690
1691
1692
1693
1694
1695
1696
1697
1698
1699
1700
1701
1702
1703
1704
1705
1706
1707
1708
1709
1710
1711
1712
1713
1714
1715
1716
1717
1718
1719
1720
1721
1722
1723
1724
1725
1726
1727
1728
1729
1730
1731
1732
1733
1734
1735
1736
1737
1738
1739
1740
1741
1742
1743
1744
1745
1746
1747
1748
1749
1750
1751
1752
1753
1754
1755
1756
1757
1758
1759
1760
1761
1762
1763
1764
1765
1766
1767
1768
1769
1770
1771
1772
1773
1774
1775
1776
1777
1778
1779
1780
1781
1782
1783
1784
1785
1786
1787
1788
1789
1790
1791
1792
1793
1794
1795
1796
1797
1798
1799
1800
1801
1802
1803
1804
1805
1806
1807
1808
1809
1810
1811
1812
1813
1814
1815
1816
1817
1818
1819
1820
1821
1822
1823
1824
1825
|
/*
* CDDL HEADER START
*
* The contents of this file are subject to the terms of the
* Common Development and Distribution License, Version 1.0 only
* (the "License"). You may not use this file except in compliance
* with the License.
*
* You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
* or http://www.opensolaris.org/os/licensing.
* See the License for the specific language governing permissions
* and limitations under the License.
*
* When distributing Covered Code, include this CDDL HEADER in each
* file and include the License file at usr/src/OPENSOLARIS.LICENSE.
* If applicable, add the following below this CDDL HEADER, with the
* fields enclosed by brackets "[]" replaced with your own identifying
* information: Portions Copyright [yyyy] [name of copyright owner]
*
* CDDL HEADER END
*/
/*
* Copyright 2005 Sun Microsystems, Inc. All rights reserved.
* Use is subject to license terms.
*/
#pragma ident "%Z%%M% %I% %E% SMI"
/*
* This file contains functions to implement automatic configuration
* of scsi disks.
*/
#include "global.h"
#include <fcntl.h>
#include <stdlib.h>
#include <string.h>
#include <strings.h>
#include <stdlib.h>
#include <ctype.h>
#include "misc.h"
#include "param.h"
#include "ctlr_scsi.h"
#include "auto_sense.h"
#include "partition.h"
#include "label.h"
#include "startup.h"
#include "analyze.h"
#include "io.h"
#include "hardware_structs.h"
#include "menu_fdisk.h"
#define DISK_NAME_MAX 256
extern int nctypes;
extern struct ctlr_type ctlr_types[];
/*
* Marker for free hog partition
*/
#define HOG (-1)
/*
* Default partition tables
*
* Disk capacity root swap usr
* ------------- ---- ---- ---
* 0mb to 64mb 0 0 remainder
* 64mb to 180mb 16mb 16mb remainder
* 180mb to 280mb 16mb 32mb remainder
* 280mb to 380mb 24mb 32mb remainder
* 380mb to 600mb 32mb 32mb remainder
* 600mb to 1gb 32mb 64mb remainder
* 1gb to 2gb 64mb 128mb remainder
* 2gb on up 128mb 128mb remainder
*/
struct part_table {
int partitions[NDKMAP];
};
static struct part_table part_table_64mb = {
{ 0, 0, 0, 0, 0, 0, HOG, 0}
};
static struct part_table part_table_180mb = {
{ 16, 16, 0, 0, 0, 0, HOG, 0}
};
static struct part_table part_table_280mb = {
{ 16, 32, 0, 0, 0, 0, HOG, 0}
};
static struct part_table part_table_380mb = {
{ 24, 32, 0, 0, 0, 0, HOG, 0}
};
static struct part_table part_table_600mb = {
{ 32, 32, 0, 0, 0, 0, HOG, 0}
};
static struct part_table part_table_1gb = {
{ 32, 64, 0, 0, 0, 0, HOG, 0}
};
static struct part_table part_table_2gb = {
{ 64, 128, 0, 0, 0, 0, HOG, 0}
};
static struct part_table part_table_infinity = {
{ 128, 128, 0, 0, 0, 0, HOG, 0}
};
static struct default_partitions {
long min_capacity;
long max_capacity;
struct part_table *part_table;
} default_partitions[] = {
{ 0, 64, &part_table_64mb }, /* 0 to 64 mb */
{ 64, 180, &part_table_180mb }, /* 64 to 180 mb */
{ 180, 280, &part_table_280mb }, /* 180 to 280 mb */
{ 280, 380, &part_table_380mb }, /* 280 to 380 mb */
{ 380, 600, &part_table_600mb }, /* 380 to 600 mb */
{ 600, 1024, &part_table_1gb }, /* 600 to 1 gb */
{ 1024, 2048, &part_table_2gb }, /* 1 to 2 gb */
{ 2048, INFINITY, &part_table_infinity }, /* 2 gb on up */
};
#define DEFAULT_PARTITION_TABLE_SIZE \
(sizeof (default_partitions) / sizeof (struct default_partitions))
/*
* msgs for check()
*/
#define FORMAT_MSG "Auto configuration via format.dat"
#define GENERIC_MSG "Auto configuration via generic SCSI-2"
/*
* Disks on symbios(Hardwire raid controller) return a fixed number
* of heads(64)/cylinders(64) and adjust the cylinders depending
* capacity of the configured lun.
* In such a case we get number of physical cylinders < 3 which
* is the minimum required by solaris(2 reserved + 1 data cylinders).
* Hence try to adjust the cylinders by reducing the "nsect/nhead".
*
*/
/*
* assuming a minimum of 32 block cylinders.
*/
#define MINIMUM_NO_HEADS 2
#define MINIMUM_NO_SECTORS 16
#define MINIMUM_NO_CYLINDERS 128
#if defined(_SUNOS_VTOC_8)
/* These are 16-bit fields */
#define MAXIMUM_NO_HEADS 65535
#define MAXIMUM_NO_SECTORS 65535
#define MAXIMUM_NO_CYLINDERS 65535
#endif /* defined(_SUNOS_VTOC_8) */
/*
* minimum number of cylinders required by Solaris.
*/
#define SUN_MIN_CYL 3
/*
* ANSI prototypes for local static functions
*/
static struct disk_type *generic_disk_sense(
int fd,
int can_prompt,
struct dk_label *label,
struct scsi_inquiry *inquiry,
struct scsi_capacity_16 *capacity,
char *disk_name);
static int use_existing_disk_type(
int fd,
int can_prompt,
struct dk_label *label,
struct scsi_inquiry *inquiry,
struct disk_type *disk_type,
struct scsi_capacity_16 *capacity);
int build_default_partition(struct dk_label *label,
int ctrl_type);
static struct disk_type *find_scsi_disk_type(
char *disk_name,
struct dk_label *label);
static struct disk_type *find_scsi_disk_by_name(
char *disk_name);
static struct ctlr_type *find_scsi_ctlr_type(void);
static struct ctlr_info *find_scsi_ctlr_info(
struct dk_cinfo *dkinfo);
static struct disk_type *new_scsi_disk_type(
int fd,
char *disk_name,
struct dk_label *label);
static struct disk_info *find_scsi_disk_info(
struct dk_cinfo *dkinfo);
static char *get_sun_disk_name(
char *disk_name,
struct scsi_inquiry *inquiry);
static char *get_generic_disk_name(
char *disk_name,
struct scsi_inquiry *inquiry);
static int force_blocksize(int fd);
static int raw_format(int fd);
static char *strcopy(
char *dst,
char *src,
int n);
static int adjust_disk_geometry(int capacity, int *cyl,
int *nsect, int *nhead);
#if defined(_SUNOS_VTOC_8)
static int square_box(
int capacity,
int *dim1, int lim1,
int *dim2, int lim2,
int *dim3, int lim3);
#endif /* defined(_SUNOS_VTOC_8) */
/*
* We need to get information necessary to construct a *new* efi
* label type
*/
struct disk_type *
auto_efi_sense(int fd, struct efi_info *label)
{
struct dk_gpt *vtoc;
int i;
struct disk_type *disk, *dp;
struct disk_info *disk_info;
struct ctlr_info *ctlr;
struct dk_cinfo dkinfo;
struct partition_info *part;
/*
* get vendor, product, revision and capacity info.
*/
if (get_disk_info(fd, label) == -1) {
return ((struct disk_type *)NULL);
}
/*
* Now build the default partition table
*/
if (efi_alloc_and_init(fd, EFI_NUMPAR, &vtoc) != 0) {
err_print("efi_alloc_and_init failed. \n");
return ((struct disk_type *)NULL);
}
label->e_parts = vtoc;
for (i = 0; i < min(vtoc->efi_nparts, V_NUMPAR); i++) {
vtoc->efi_parts[i].p_tag = default_vtoc_map[i].p_tag;
vtoc->efi_parts[i].p_flag = default_vtoc_map[i].p_flag;
vtoc->efi_parts[i].p_start = 0;
vtoc->efi_parts[i].p_size = 0;
}
/*
* Make constants first
* and variable partitions later
*/
/* root partition - s0 128 MB */
vtoc->efi_parts[0].p_start = 34;
vtoc->efi_parts[0].p_size = 262144;
/* partition - s1 128 MB */
vtoc->efi_parts[1].p_start = 262178;
vtoc->efi_parts[1].p_size = 262144;
/* partition -s2 is NOT the Backup disk */
vtoc->efi_parts[2].p_tag = V_UNASSIGNED;
/* partition -s6 /usr partition - HOG */
vtoc->efi_parts[6].p_start = 524322;
vtoc->efi_parts[6].p_size = vtoc->efi_last_u_lba - 524322
- (1024 * 16);
/* efi reserved partition - s9 16K */
vtoc->efi_parts[8].p_start = vtoc->efi_last_u_lba - (1024 * 16);
vtoc->efi_parts[8].p_size = (1024 * 16);
vtoc->efi_parts[8].p_tag = V_RESERVED;
/*
* Now stick all of it into the disk_type struct
*/
if (ioctl(fd, DKIOCINFO, &dkinfo) == -1) {
if (option_msg && diag_msg) {
err_print("DKIOCINFO failed\n");
}
return (NULL);
}
ctlr = find_scsi_ctlr_info(&dkinfo);
disk = (struct disk_type *)zalloc(sizeof (struct disk_type));
disk_info = find_scsi_disk_info(&dkinfo);
assert(disk_info->disk_ctlr == ctlr);
dp = ctlr->ctlr_ctype->ctype_dlist;
if (dp == NULL) {
ctlr->ctlr_ctype->ctype_dlist = dp;
} else {
while (dp->dtype_next != NULL) {
dp = dp->dtype_next;
}
dp->dtype_next = disk;
}
disk->dtype_next = NULL;
(void) strlcpy(disk->vendor, label->vendor,
sizeof (disk->vendor));
(void) strlcpy(disk->product, label->product,
sizeof (disk->product));
(void) strlcpy(disk->revision, label->revision,
sizeof (disk->revision));
disk->capacity = label->capacity;
part = (struct partition_info *)
zalloc(sizeof (struct partition_info));
disk->dtype_plist = part;
part->pinfo_name = alloc_string("default");
part->pinfo_next = NULL;
part->etoc = vtoc;
bzero(disk_info->v_volume, LEN_DKL_VVOL);
disk_info->disk_parts = part;
return (disk);
}
/*
* Auto-sense a scsi disk configuration, ie get the information
* necessary to construct a label. We have two different
* ways to auto-sense a scsi disk:
* - format.dat override, via inquiry name
* - generic scsi, via standard mode sense and inquiry
* Depending on how and when we are called, and/or
* change geometry and reformat.
*/
struct disk_type *
auto_sense(
int fd,
int can_prompt,
struct dk_label *label)
{
struct scsi_inquiry inquiry;
struct scsi_capacity_16 capacity;
struct disk_type *disk_type;
char disk_name[DISK_NAME_MAX];
int force_format_dat = 0;
int force_generic = 0;
u_ioparam_t ioparam;
int deflt;
/*
* First, if expert mode, find out if the user
* wants to override any of the standard methods.
*/
if (can_prompt && expert_mode) {
deflt = 1;
ioparam.io_charlist = confirm_list;
if (input(FIO_MSTR, FORMAT_MSG, '?', &ioparam,
&deflt, DATA_INPUT) == 0) {
force_format_dat = 1;
} else if (input(FIO_MSTR, GENERIC_MSG, '?', &ioparam,
&deflt, DATA_INPUT) == 0) {
force_generic = 1;
}
}
/*
* Get the Inquiry data. If this fails, there's
* no hope for this disk, so give up.
*/
if (uscsi_inquiry(fd, (char *)&inquiry, sizeof (inquiry))) {
return ((struct disk_type *)NULL);
}
if (option_msg && diag_msg) {
err_print("Product id: ");
print_buf(inquiry.inq_pid, sizeof (inquiry.inq_pid));
err_print("\n");
}
/*
* Get the Read Capacity
*/
if (uscsi_read_capacity(fd, &capacity)) {
return ((struct disk_type *)NULL);
}
if (option_msg && diag_msg) {
err_print("blocks: %llu (0x%llx)\n",
capacity.sc_capacity, capacity.sc_capacity);
err_print("blksize: %u\n", capacity.sc_lbasize);
}
/*
* Extract the disk name for the format.dat override
*/
(void) get_sun_disk_name(disk_name, &inquiry);
if (option_msg && diag_msg) {
err_print("disk name: `%s`\n", disk_name);
}
/*
* Figure out which method we use for auto sense.
* If a particular method fails, we fall back to
* the next possibility.
*/
if (force_generic) {
return (generic_disk_sense(fd, can_prompt, label,
&inquiry, &capacity, disk_name));
}
/*
* Try for an existing format.dat first
*/
if ((disk_type = find_scsi_disk_by_name(disk_name)) != NULL) {
if (use_existing_disk_type(fd, can_prompt, label,
&inquiry, disk_type, &capacity)) {
return (disk_type);
}
if (force_format_dat) {
return (NULL);
}
}
/*
* Otherwise, try using generic SCSI-2 sense and inquiry.
*/
return (generic_disk_sense(fd, can_prompt, label,
&inquiry, &capacity, disk_name));
}
/*ARGSUSED*/
static struct disk_type *
generic_disk_sense(
int fd,
int can_prompt,
struct dk_label *label,
struct scsi_inquiry *inquiry,
struct scsi_capacity_16 *capacity,
char *disk_name)
{
struct disk_type *disk;
int pcyl;
int ncyl;
int acyl;
int nhead;
int nsect;
int rpm;
long nblocks;
union {
struct mode_format page3;
uchar_t buf3[MAX_MODE_SENSE_SIZE];
} u_page3;
union {
struct mode_geometry page4;
uchar_t buf4[MAX_MODE_SENSE_SIZE];
} u_page4;
struct scsi_capacity_16 new_capacity;
struct mode_format *page3 = &u_page3.page3;
struct mode_geometry *page4 = &u_page4.page4;
struct scsi_ms_header header;
/*
* If the name of this disk appears to be "SUN", use it,
* otherwise construct a name out of the generic
* Inquiry info. If it turns out that we already
* have a SUN disk type of this name that differs
* in geometry, we will revert to the generic name
* anyway.
*/
if (memcmp(disk_name, "SUN", strlen("SUN")) != 0) {
(void) get_generic_disk_name(disk_name, inquiry);
}
/*
* If the device's block size is not 512, we have to
* change block size, reformat, and then sense the
* geometry. To do this, we must be able to prompt
* the user.
*/
if (capacity->sc_lbasize != DEV_BSIZE) {
if (!can_prompt) {
return (NULL);
}
if (force_blocksize(fd)) {
goto err;
}
/*
* Get the capacity again, since this has changed
*/
if (uscsi_read_capacity(fd, &new_capacity)) {
goto err;
}
if (option_msg && diag_msg) {
err_print("blocks: %llu (0x%llx)\n",
new_capacity.sc_capacity,
new_capacity.sc_capacity);
err_print("blksize: %u\n", new_capacity.sc_lbasize);
}
capacity = &new_capacity;
if (capacity->sc_lbasize != DEV_BSIZE) {
goto err;
}
}
/*
* Get current Page 3 - Format Parameters page
*/
if (uscsi_mode_sense(fd, DAD_MODE_FORMAT, MODE_SENSE_PC_CURRENT,
(caddr_t)&u_page3, MAX_MODE_SENSE_SIZE, &header)) {
goto err;
}
/*
* Get current Page 4 - Drive Geometry page
*/
if (uscsi_mode_sense(fd, DAD_MODE_GEOMETRY, MODE_SENSE_PC_CURRENT,
(caddr_t)&u_page4, MAX_MODE_SENSE_SIZE, &header)) {
goto err;
}
/*
* Correct for byte order if necessary
*/
page4->rpm = BE_16(page4->rpm);
page4->step_rate = BE_16(page4->step_rate);
page3->tracks_per_zone = BE_16(page3->tracks_per_zone);
page3->alt_sect_zone = BE_16(page3->alt_sect_zone);
page3->alt_tracks_zone = BE_16(page3->alt_tracks_zone);
page3->alt_tracks_vol = BE_16(page3->alt_tracks_vol);
page3->sect_track = BE_16(page3->sect_track);
page3->data_bytes_sect = BE_16(page3->data_bytes_sect);
page3->interleave = BE_16(page3->interleave);
page3->track_skew = BE_16(page3->track_skew);
page3->cylinder_skew = BE_16(page3->cylinder_skew);
/*
* Construct a new label out of the sense data,
* Inquiry and Capacity.
*/
pcyl = (page4->cyl_ub << 16) + (page4->cyl_mb << 8) + page4->cyl_lb;
nhead = page4->heads;
nsect = page3->sect_track;
rpm = page4->rpm;
/*
* If the number of physical cylinders reported is less
* the SUN_MIN_CYL(3) then try to adjust the geometry so that
* we have atleast SUN_MIN_CYL cylinders.
*/
if (pcyl < SUN_MIN_CYL) {
if (adjust_disk_geometry((int)(capacity->sc_capacity + 1),
&pcyl, &nhead, &nsect)) {
goto err;
}
}
/*
* The sd driver reserves 2 cylinders the backup disk label and
* the deviceid. Set the number of data cylinders to pcyl-acyl.
*/
acyl = DK_ACYL;
ncyl = pcyl - acyl;
if (option_msg && diag_msg) {
err_print("Geometry:\n");
err_print(" pcyl: %d\n", pcyl);
err_print(" ncyl: %d\n", ncyl);
err_print(" heads: %d\n", nhead);
err_print(" nsects: %d\n", nsect);
err_print(" acyl: %d\n", acyl);
#if defined(_SUNOS_VTOC_16)
err_print(" bcyl: %d\n", bcyl);
#endif /* defined(_SUNOS_VTOC_16) */
err_print(" rpm: %d\n", rpm);
}
/*
* Some drives report 0 for page4->rpm, adjust it to AVG_RPM, 3600.
*/
if (rpm < MIN_RPM || rpm > MAX_RPM) {
err_print("Mode sense page(4) reports rpm value as %d,"
" adjusting it to %d\n", rpm, AVG_RPM);
rpm = AVG_RPM;
}
/*
* Get the number of blocks from Read Capacity data. Note that
* the logical block address range from 0 to capacity->sc_capacity.
*/
nblocks = (long)(capacity->sc_capacity + 1);
/*
* Some drives report 0 for nsect (page 3, byte 10 and 11) if they
* have variable number of sectors per track. So adjust nsect.
* Also the value is defined as vendor specific, hence check if
* it is in a tolerable range. The values (32 and 4 below) are
* chosen so that this change below does not generate a different
* geometry for currently supported sun disks.
*/
if ((nsect <= 0) ||
(pcyl * nhead * nsect) < (nblocks - nblocks/32) ||
(pcyl * nhead * nsect) > (nblocks + nblocks/4)) {
err_print("Mode sense page(3) reports nsect value as %d, "
"adjusting it to %ld\n", nsect, nblocks / (pcyl * nhead));
nsect = nblocks / (pcyl * nhead);
}
/*
* Some drives report their physical geometry such that
* it is greater than the actual capacity. Adjust the
* geometry to allow for this, so we don't run off
* the end of the disk.
*/
if ((pcyl * nhead * nsect) > nblocks) {
int p = pcyl;
if (option_msg && diag_msg) {
err_print("Computed capacity (%ld) exceeds actual "
"disk capacity (%ld)\n",
(long)(pcyl * nhead * nsect), nblocks);
}
do {
pcyl--;
} while ((pcyl * nhead * nsect) > nblocks);
if (can_prompt && expert_mode && !option_f) {
/*
* Try to adjust nsect instead of pcyl to see if we
* can optimize. For compatability reasons do this
* only in expert mode (refer to bug 1144812).
*/
int n = nsect;
do {
n--;
} while ((p * nhead * n) > nblocks);
if ((p * nhead * n) > (pcyl * nhead * nsect)) {
u_ioparam_t ioparam;
int deflt = 1;
/*
* Ask the user for a choice here.
*/
ioparam.io_bounds.lower = 1;
ioparam.io_bounds.upper = 2;
err_print("1. Capacity = %d, with pcyl = %d "
"nhead = %d nsect = %d\n",
(pcyl * nhead * nsect),
pcyl, nhead, nsect);
err_print("2. Capacity = %d, with pcyl = %d "
"nhead = %d nsect = %d\n",
(p * nhead * n),
p, nhead, n);
if (input(FIO_INT, "Select one of the above "
"choices ", ':', &ioparam,
&deflt, DATA_INPUT) == 2) {
pcyl = p;
nsect = n;
}
}
}
}
#if defined(_SUNOS_VTOC_8)
/*
* Finally, we need to make sure we don't overflow any of the
* fields in our disk label. To do this we need to `square
* the box' so to speak. We will lose bits here.
*/
if ((pcyl > MAXIMUM_NO_CYLINDERS &&
((nsect > MAXIMUM_NO_SECTORS) ||
(nhead > MAXIMUM_NO_HEADS))) ||
((nsect > MAXIMUM_NO_SECTORS) &&
(nhead > MAXIMUM_NO_HEADS))) {
err_print("This disk is too big to label. "
" You will lose some blocks.\n");
}
if ((pcyl > MAXIMUM_NO_CYLINDERS) ||
(nsect > MAXIMUM_NO_SECTORS) ||
(nhead > MAXIMUM_NO_HEADS)) {
u_ioparam_t ioparam;
int order;
char msg[256];
order = ((ncyl > nhead)<<2) |
((ncyl > nsect)<<1) |
(nhead > nsect);
switch (order) {
case 0x7: /* ncyl > nhead > nsect */
nblocks =
square_box(nblocks,
&pcyl, MAXIMUM_NO_CYLINDERS,
&nhead, MAXIMUM_NO_HEADS,
&nsect, MAXIMUM_NO_SECTORS);
break;
case 0x6: /* ncyl > nsect > nhead */
nblocks =
square_box(nblocks,
&pcyl, MAXIMUM_NO_CYLINDERS,
&nsect, MAXIMUM_NO_SECTORS,
&nhead, MAXIMUM_NO_HEADS);
break;
case 0x4: /* nsect > ncyl > nhead */
nblocks =
square_box(nblocks,
&nsect, MAXIMUM_NO_SECTORS,
&pcyl, MAXIMUM_NO_CYLINDERS,
&nhead, MAXIMUM_NO_HEADS);
break;
case 0x0: /* nsect > nhead > ncyl */
nblocks =
square_box(nblocks,
&nsect, MAXIMUM_NO_SECTORS,
&nhead, MAXIMUM_NO_HEADS,
&pcyl, MAXIMUM_NO_CYLINDERS);
break;
case 0x3: /* nhead > ncyl > nsect */
nblocks =
square_box(nblocks,
&nhead, MAXIMUM_NO_HEADS,
&pcyl, MAXIMUM_NO_CYLINDERS,
&nsect, MAXIMUM_NO_SECTORS);
break;
case 0x1: /* nhead > nsect > ncyl */
nblocks =
square_box(nblocks,
&nhead, MAXIMUM_NO_HEADS,
&nsect, MAXIMUM_NO_SECTORS,
&pcyl, MAXIMUM_NO_CYLINDERS);
break;
default:
/* How did we get here? */
impossible("label overflow adjustment");
/* Do something useful */
nblocks =
square_box(nblocks,
&nhead, MAXIMUM_NO_HEADS,
&nsect, MAXIMUM_NO_SECTORS,
&pcyl, MAXIMUM_NO_CYLINDERS);
break;
}
if (option_msg && diag_msg &&
(capacity->sc_capacity + 1 != nblocks)) {
err_print("After adjusting geometry you lost"
" %llu of %lld blocks.\n",
(capacity->sc_capacity + 1 - nblocks),
capacity->sc_capacity + 1);
}
while (can_prompt && expert_mode && !option_f) {
int deflt = 1;
/*
* Allow user to modify this by hand if desired.
*/
(void) sprintf(msg,
"\nGeometry: %d heads, %d sectors %d "
" cylinders result in %d out of %lld blocks.\n"
"Do you want to modify the device geometry",
nhead, nsect, pcyl,
(int)nblocks, capacity->sc_capacity + 1);
ioparam.io_charlist = confirm_list;
if (input(FIO_MSTR, msg, '?', &ioparam,
&deflt, DATA_INPUT) != 0)
break;
ioparam.io_bounds.lower = MINIMUM_NO_HEADS;
ioparam.io_bounds.upper = MAXIMUM_NO_HEADS;
nhead = input(FIO_INT, "Number of heads", ':',
&ioparam, &nhead, DATA_INPUT);
ioparam.io_bounds.lower = MINIMUM_NO_SECTORS;
ioparam.io_bounds.upper = MAXIMUM_NO_SECTORS;
nsect = input(FIO_INT,
"Number of sectors per track",
':', &ioparam, &nsect, DATA_INPUT);
ioparam.io_bounds.lower = SUN_MIN_CYL;
ioparam.io_bounds.upper = MAXIMUM_NO_CYLINDERS;
pcyl = input(FIO_INT, "Number of cylinders",
':', &ioparam, &pcyl, DATA_INPUT);
nblocks = nhead * nsect * pcyl;
if (nblocks > capacity->sc_capacity + 1) {
err_print("Warning: %ld blocks exceeds "
"disk capacity of %lld blocks\n",
nblocks,
capacity->sc_capacity + 1);
}
}
}
#endif /* defined(_SUNOS_VTOC_8) */
ncyl = pcyl - acyl;
if (option_msg && diag_msg) {
err_print("\nGeometry after adjusting for capacity:\n");
err_print(" pcyl: %d\n", pcyl);
err_print(" ncyl: %d\n", ncyl);
err_print(" heads: %d\n", nhead);
err_print(" nsects: %d\n", nsect);
err_print(" acyl: %d\n", acyl);
err_print(" rpm: %d\n", rpm);
}
(void) memset((char *)label, 0, sizeof (struct dk_label));
label->dkl_magic = DKL_MAGIC;
(void) snprintf(label->dkl_asciilabel, sizeof (label->dkl_asciilabel),
"%s cyl %d alt %d hd %d sec %d",
disk_name, ncyl, acyl, nhead, nsect);
label->dkl_pcyl = pcyl;
label->dkl_ncyl = ncyl;
label->dkl_acyl = acyl;
label->dkl_nhead = nhead;
label->dkl_nsect = nsect;
label->dkl_apc = 0;
label->dkl_intrlv = 1;
label->dkl_rpm = rpm;
#if defined(_FIRMWARE_NEEDS_FDISK)
(void) auto_solaris_part(label);
ncyl = label->dkl_ncyl;
#endif /* defined(_FIRMWARE_NEEDS_FDISK) */
if (!build_default_partition(label, DKC_SCSI_CCS)) {
goto err;
}
(void) checksum(label, CK_MAKESUM);
/*
* Find an existing disk type defined for this disk.
* For this to work, both the name and geometry must
* match. If there is no such type, but there already
* is a disk defined with that name, but with a different
* geometry, construct a new generic disk name out of
* the inquiry information. Whatever name we're
* finally using, if there's no such disk type defined,
* build a new disk definition.
*/
if ((disk = find_scsi_disk_type(disk_name, label)) == NULL) {
if (find_scsi_disk_by_name(disk_name) != NULL) {
char old_name[DISK_NAME_MAX];
(void) strcpy(old_name, disk_name);
(void) get_generic_disk_name(disk_name,
inquiry);
if (option_msg && diag_msg) {
err_print(
"Changing disk type name from '%s' to '%s'\n", old_name, disk_name);
}
(void) snprintf(label->dkl_asciilabel,
sizeof (label->dkl_asciilabel),
"%s cyl %d alt %d hd %d sec %d",
disk_name, ncyl, acyl, nhead, nsect);
(void) checksum(label, CK_MAKESUM);
disk = find_scsi_disk_type(disk_name, label);
}
if (disk == NULL) {
disk = new_scsi_disk_type(fd, disk_name, label);
if (disk == NULL)
goto err;
}
}
return (disk);
err:
if (option_msg && diag_msg) {
err_print(
"Configuration via generic SCSI-2 information failed\n");
}
return (NULL);
}
/*ARGSUSED*/
static int
use_existing_disk_type(
int fd,
int can_prompt,
struct dk_label *label,
struct scsi_inquiry *inquiry,
struct disk_type *disk_type,
struct scsi_capacity_16 *capacity)
{
struct scsi_capacity_16 new_capacity;
int pcyl;
int acyl;
int nhead;
int nsect;
int rpm;
/*
* If the device's block size is not 512, we have to
* change block size, reformat, and then sense the
* geometry. To do this, we must be able to prompt
* the user.
*/
if (capacity->sc_lbasize != DEV_BSIZE) {
if (!can_prompt) {
return (0);
}
if (force_blocksize(fd)) {
goto err;
}
/*
* Get the capacity again, since this has changed
*/
if (uscsi_read_capacity(fd, &new_capacity)) {
goto err;
}
if (option_msg && diag_msg) {
err_print("blocks: %llu (0x%llx)\n",
new_capacity.sc_capacity,
new_capacity.sc_capacity);
err_print("blksize: %u\n", new_capacity.sc_lbasize);
}
capacity = &new_capacity;
if (capacity->sc_lbasize != DEV_BSIZE) {
goto err;
}
}
/*
* Construct a new label out of the format.dat
*/
pcyl = disk_type->dtype_pcyl;
acyl = disk_type->dtype_acyl;
ncyl = disk_type->dtype_ncyl;
nhead = disk_type->dtype_nhead;
nsect = disk_type->dtype_nsect;
rpm = disk_type->dtype_rpm;
if (option_msg && diag_msg) {
err_print("Format.dat geometry:\n");
err_print(" pcyl: %d\n", pcyl);
err_print(" heads: %d\n", nhead);
err_print(" nsects: %d\n", nsect);
err_print(" acyl: %d\n", acyl);
err_print(" rpm: %d\n", rpm);
}
(void) memset((char *)label, 0, sizeof (struct dk_label));
label->dkl_magic = DKL_MAGIC;
(void) snprintf(label->dkl_asciilabel, sizeof (label->dkl_asciilabel),
"%s cyl %d alt %d hd %d sec %d",
disk_type->dtype_asciilabel,
ncyl, acyl, nhead, nsect);
label->dkl_pcyl = pcyl;
label->dkl_ncyl = ncyl;
label->dkl_acyl = acyl;
label->dkl_nhead = nhead;
label->dkl_nsect = nsect;
label->dkl_apc = 0;
label->dkl_intrlv = 1;
label->dkl_rpm = rpm;
if (!build_default_partition(label, DKC_SCSI_CCS)) {
goto err;
}
(void) checksum(label, CK_MAKESUM);
return (1);
err:
if (option_msg && diag_msg) {
err_print(
"Configuration via format.dat geometry failed\n");
}
return (0);
}
int
build_default_partition(
struct dk_label *label,
int ctrl_type)
{
int i;
int ncyls[NDKMAP];
int nblks;
int cyl;
struct dk_vtoc *vtoc;
struct part_table *pt;
struct default_partitions *dpt;
long capacity;
int freecyls;
int blks_per_cyl;
int ncyl;
#ifdef lint
ctrl_type = ctrl_type;
#endif
/*
* Install a default vtoc
*/
vtoc = &label->dkl_vtoc;
vtoc->v_version = V_VERSION;
vtoc->v_nparts = NDKMAP;
vtoc->v_sanity = VTOC_SANE;
for (i = 0; i < NDKMAP; i++) {
vtoc->v_part[i].p_tag = default_vtoc_map[i].p_tag;
vtoc->v_part[i].p_flag = default_vtoc_map[i].p_flag;
}
/*
* Find a partition that matches this disk. Capacity
* is in integral number of megabytes.
*/
capacity = (long)(label->dkl_ncyl * label->dkl_nhead *
label->dkl_nsect) / (long)((1024 * 1024) / DEV_BSIZE);
dpt = default_partitions;
for (i = 0; i < DEFAULT_PARTITION_TABLE_SIZE; i++, dpt++) {
if (capacity >= dpt->min_capacity &&
capacity < dpt->max_capacity) {
break;
}
}
if (i == DEFAULT_PARTITION_TABLE_SIZE) {
if (option_msg && diag_msg) {
err_print("No matching default partition (%ld)\n",
capacity);
}
return (0);
}
pt = dpt->part_table;
/*
* Go through default partition table, finding fixed
* sized entries.
*/
freecyls = label->dkl_ncyl;
blks_per_cyl = label->dkl_nhead * label->dkl_nsect;
for (i = 0; i < NDKMAP; i++) {
if (pt->partitions[i] == HOG || pt->partitions[i] == 0) {
ncyls[i] = 0;
} else {
/*
* Calculate number of cylinders necessary
* for specified size, rounding up to
* the next greatest integral number of
* cylinders. Always give what they
* asked or more, never less.
*/
nblks = pt->partitions[i] * ((1024*1024)/DEV_BSIZE);
nblks += (blks_per_cyl - 1);
ncyls[i] = nblks / blks_per_cyl;
freecyls -= ncyls[i];
}
}
if (freecyls < 0) {
if (option_msg && diag_msg) {
for (i = 0; i < NDKMAP; i++) {
if (ncyls[i] == 0)
continue;
err_print("Partition %d: %d cyls\n",
i, ncyls[i]);
}
err_print("Free cylinders exhausted (%d)\n",
freecyls);
}
return (0);
}
#if defined(i386)
/*
* Set the default boot partition to 1 cylinder
*/
ncyls[8] = 1;
freecyls -= 1;
/*
* If current disk type is not a SCSI disk,
* set the default alternates partition to 2 cylinders
*/
if (ctrl_type != DKC_SCSI_CCS) {
ncyls[9] = 2;
freecyls -= 2;
}
#endif /* defined(i386) */
/*
* Set the free hog partition to whatever space remains.
* It's an error to have more than one HOG partition,
* but we don't verify that here.
*/
for (i = 0; i < NDKMAP; i++) {
if (pt->partitions[i] == HOG) {
assert(ncyls[i] == 0);
ncyls[i] = freecyls;
break;
}
}
/*
* Error checking
*/
ncyl = 0;
for (i = 0; i < NDKMAP; i++) {
ncyl += ncyls[i];
}
assert(ncyl == (label->dkl_ncyl));
/*
* Finally, install the partition in the label.
*/
cyl = 0;
#if defined(_SUNOS_VTOC_16)
for (i = NDKMAP/2; i < NDKMAP; i++) {
if (i == 2 || ncyls[i] == 0)
continue;
label->dkl_vtoc.v_part[i].p_start = cyl * blks_per_cyl;
label->dkl_vtoc.v_part[i].p_size = ncyls[i] * blks_per_cyl;
cyl += ncyls[i];
}
for (i = 0; i < NDKMAP/2; i++) {
#elif defined(_SUNOS_VTOC_8)
for (i = 0; i < NDKMAP; i++) {
#else
#error No VTOC format defined.
#endif /* defined(_SUNOS_VTOC_16) */
if (i == 2 || ncyls[i] == 0)
continue;
#if defined(_SUNOS_VTOC_8)
label->dkl_map[i].dkl_cylno = cyl;
label->dkl_map[i].dkl_nblk = ncyls[i] * blks_per_cyl;
#elif defined(_SUNOS_VTOC_16)
label->dkl_vtoc.v_part[i].p_start = cyl * blks_per_cyl;
label->dkl_vtoc.v_part[i].p_size = ncyls[i] * blks_per_cyl;
#else
#error No VTOC format defined.
#endif /* defined(_SUNOS_VTOC_8) */
cyl += ncyls[i];
}
/*
* Set the whole disk partition
*/
#if defined(_SUNOS_VTOC_8)
label->dkl_map[2].dkl_cylno = 0;
label->dkl_map[2].dkl_nblk =
label->dkl_ncyl * label->dkl_nhead * label->dkl_nsect;
#elif defined(_SUNOS_VTOC_16)
label->dkl_vtoc.v_part[2].p_start = 0;
label->dkl_vtoc.v_part[2].p_size =
(label->dkl_ncyl + label->dkl_acyl) * label->dkl_nhead *
label->dkl_nsect;
#else
#error No VTOC format defined.
#endif /* defined(_SUNOS_VTOC_8) */
if (option_msg && diag_msg) {
float scaled;
err_print("\n");
for (i = 0; i < NDKMAP; i++) {
#if defined(_SUNOS_VTOC_8)
if (label->dkl_map[i].dkl_nblk == 0)
#elif defined(_SUNOS_VTOC_16)
if (label->dkl_vtoc.v_part[i].p_size == 0)
#else
#error No VTOC format defined.
#endif /* defined(_SUNOS_VTOC_8) */
continue;
err_print("Partition %d: ", i);
#if defined(_SUNOS_VTOC_8)
scaled = bn2mb(label->dkl_map[i].dkl_nblk);
#elif defined(_SUNOS_VTOC_16)
scaled = bn2mb(label->dkl_vtoc.v_part[i].p_size);
#else
#error No VTOC format defined.
#endif /* defined(_SUNOS_VTOC_8) */
if (scaled > 1024.0) {
err_print("%6.2fGB ", scaled/1024.0);
} else {
err_print("%6.2fMB ", scaled);
}
err_print(" %6d cylinders\n",
#if defined(_SUNOS_VTOC_8)
label->dkl_map[i].dkl_nblk/blks_per_cyl);
#elif defined(_SUNOS_VTOC_16)
label->dkl_vtoc.v_part[i].p_size/blks_per_cyl);
#else
#error No VTOC format defined.
#endif /* defined(_SUNOS_VTOC_8) */
}
err_print("\n");
}
return (1);
}
/*
* Find an existing scsi disk definition by this name,
* if possible.
*/
static struct disk_type *
find_scsi_disk_type(
char *disk_name,
struct dk_label *label)
{
struct ctlr_type *ctlr;
struct disk_type *dp;
ctlr = find_scsi_ctlr_type();
for (dp = ctlr->ctype_dlist; dp != NULL; dp = dp->dtype_next) {
if (dp->dtype_asciilabel) {
if ((strcmp(dp->dtype_asciilabel, disk_name) == 0) &&
dp->dtype_pcyl == label->dkl_pcyl &&
dp->dtype_ncyl == label->dkl_ncyl &&
dp->dtype_acyl == label->dkl_acyl &&
dp->dtype_nhead == label->dkl_nhead &&
dp->dtype_nsect == label->dkl_nsect) {
return (dp);
}
}
}
return ((struct disk_type *)NULL);
}
/*
* Find an existing scsi disk definition by this name,
* if possible.
*/
static struct disk_type *
find_scsi_disk_by_name(
char *disk_name)
{
struct ctlr_type *ctlr;
struct disk_type *dp;
ctlr = find_scsi_ctlr_type();
for (dp = ctlr->ctype_dlist; dp != NULL; dp = dp->dtype_next) {
if (dp->dtype_asciilabel) {
if ((strcmp(dp->dtype_asciilabel, disk_name) == 0)) {
return (dp);
}
}
}
return ((struct disk_type *)NULL);
}
/*
* Return a pointer to the ctlr_type structure for SCSI
* disks. This list is built into the program, so there's
* no chance of not being able to find it, unless someone
* totally mangles the code.
*/
static struct ctlr_type *
find_scsi_ctlr_type()
{
struct mctlr_list *mlp;
mlp = controlp;
while (mlp != NULL) {
if (mlp->ctlr_type->ctype_ctype == DKC_SCSI_CCS) {
return (mlp->ctlr_type);
}
mlp = mlp->next;
}
impossible("no SCSI controller type");
/*NOTREACHED*/
}
/*
* Return a pointer to the scsi ctlr_info structure. This
* structure is allocated the first time format sees a
* disk on this controller, so it must be present.
*/
static struct ctlr_info *
find_scsi_ctlr_info(
struct dk_cinfo *dkinfo)
{
struct ctlr_info *ctlr;
if (dkinfo->dki_ctype != DKC_SCSI_CCS) {
return (NULL);
}
for (ctlr = ctlr_list; ctlr != NULL; ctlr = ctlr->ctlr_next) {
if (ctlr->ctlr_addr == dkinfo->dki_addr &&
ctlr->ctlr_space == dkinfo->dki_space &&
ctlr->ctlr_ctype->ctype_ctype ==
DKC_SCSI_CCS) {
return (ctlr);
}
}
impossible("no SCSI controller info");
/*NOTREACHED*/
}
static struct disk_type *
new_scsi_disk_type(
int fd,
char *disk_name,
struct dk_label *label)
{
struct disk_type *dp;
struct disk_type *disk;
struct ctlr_info *ctlr;
struct dk_cinfo dkinfo;
struct partition_info *part;
struct partition_info *pt;
struct disk_info *disk_info;
int i;
/*
* Get the disk controller info for this disk
*/
if (ioctl(fd, DKIOCINFO, &dkinfo) == -1) {
if (option_msg && diag_msg) {
err_print("DKIOCINFO failed\n");
}
return (NULL);
}
/*
* Find the ctlr_info for this disk.
*/
ctlr = find_scsi_ctlr_info(&dkinfo);
/*
* Allocate a new disk type for the SCSI controller.
*/
disk = (struct disk_type *)zalloc(sizeof (struct disk_type));
/*
* Find the disk_info instance for this disk.
*/
disk_info = find_scsi_disk_info(&dkinfo);
/*
* The controller and the disk should match.
*/
assert(disk_info->disk_ctlr == ctlr);
/*
* Link the disk into the list of disks
*/
dp = ctlr->ctlr_ctype->ctype_dlist;
if (dp == NULL) {
ctlr->ctlr_ctype->ctype_dlist = dp;
} else {
while (dp->dtype_next != NULL) {
dp = dp->dtype_next;
}
dp->dtype_next = disk;
}
disk->dtype_next = NULL;
/*
* Allocate and initialize the disk name.
*/
disk->dtype_asciilabel = alloc_string(disk_name);
/*
* Initialize disk geometry info
*/
disk->dtype_pcyl = label->dkl_pcyl;
disk->dtype_ncyl = label->dkl_ncyl;
disk->dtype_acyl = label->dkl_acyl;
disk->dtype_nhead = label->dkl_nhead;
disk->dtype_nsect = label->dkl_nsect;
disk->dtype_rpm = label->dkl_rpm;
/*
* Attempt to match the partition map in the label
* with a know partition for this disk type.
*/
for (part = disk->dtype_plist; part; part = part->pinfo_next) {
if (parts_match(label, part)) {
break;
}
}
/*
* If no match was made, we need to create a partition
* map for this disk.
*/
if (part == NULL) {
part = (struct partition_info *)
zalloc(sizeof (struct partition_info));
pt = disk->dtype_plist;
if (pt == NULL) {
disk->dtype_plist = part;
} else {
while (pt->pinfo_next != NULL) {
pt = pt->pinfo_next;
}
pt->pinfo_next = part;
}
part->pinfo_next = NULL;
/*
* Set up the partition name
*/
part->pinfo_name = alloc_string("default");
/*
* Fill in the partition info from the label
*/
for (i = 0; i < NDKMAP; i++) {
#if defined(_SUNOS_VTOC_8)
part->pinfo_map[i] = label->dkl_map[i];
#elif defined(_SUNOS_VTOC_16)
part->pinfo_map[i].dkl_cylno =
label->dkl_vtoc.v_part[i].p_start /
((int)(disk->dtype_nhead *
disk->dtype_nsect - apc));
part->pinfo_map[i].dkl_nblk =
label->dkl_vtoc.v_part[i].p_size;
#else
#error No VTOC format defined.
#endif /* defined(_SUNOS_VTOC_8) */
}
}
/*
* Use the VTOC if valid, or install a default
*/
if (label->dkl_vtoc.v_version == V_VERSION) {
(void) memcpy(disk_info->v_volume, label->dkl_vtoc.v_volume,
LEN_DKL_VVOL);
part->vtoc = label->dkl_vtoc;
} else {
(void) memset(disk_info->v_volume, 0, LEN_DKL_VVOL);
set_vtoc_defaults(part);
}
/*
* Link the disk to the partition map
*/
disk_info->disk_parts = part;
return (disk);
}
static struct disk_info *
find_scsi_disk_info(
struct dk_cinfo *dkinfo)
{
struct disk_info *disk;
struct dk_cinfo *dp;
for (disk = disk_list; disk != NULL; disk = disk->disk_next) {
assert(dkinfo->dki_ctype == DKC_SCSI_CCS);
dp = &disk->disk_dkinfo;
if (dp->dki_ctype == dkinfo->dki_ctype &&
dp->dki_cnum == dkinfo->dki_cnum &&
dp->dki_unit == dkinfo->dki_unit &&
strcmp(dp->dki_dname, dkinfo->dki_dname) == 0) {
return (disk);
}
}
impossible("No SCSI disk info instance\n");
/*NOTREACHED*/
}
static char *
get_sun_disk_name(
char *disk_name,
struct scsi_inquiry *inquiry)
{
/*
* Extract the sun name of the disk
*/
(void) memset(disk_name, 0, DISK_NAME_MAX);
(void) memcpy(disk_name, (char *)&inquiry->inq_pid[9], 7);
return (disk_name);
}
static char *
get_generic_disk_name(
char *disk_name,
struct scsi_inquiry *inquiry)
{
char *p;
(void) memset(disk_name, 0, DISK_NAME_MAX);
p = strcopy(disk_name, inquiry->inq_vid,
sizeof (inquiry->inq_vid));
*p++ = '-';
p = strcopy(p, inquiry->inq_pid, sizeof (inquiry->inq_pid));
*p++ = '-';
p = strcopy(p, inquiry->inq_revision,
sizeof (inquiry->inq_revision));
return (disk_name);
}
static int
force_blocksize(
int fd)
{
union {
struct mode_format page3;
uchar_t buf3[MAX_MODE_SENSE_SIZE];
} u_page3;
struct mode_format *page3 = &u_page3.page3;
struct scsi_ms_header header;
if (check("\
Must reformat device to 512-byte blocksize. Continue") == 0) {
/*
* Get current Page 3 - Format Parameters page
*/
if (uscsi_mode_sense(fd, DAD_MODE_FORMAT,
MODE_SENSE_PC_CURRENT, (caddr_t)&u_page3,
MAX_MODE_SENSE_SIZE, &header)) {
goto err;
}
/*
* Make our changes to the geometry
*/
header.mode_header.length = 0;
header.mode_header.device_specific = 0;
page3->mode_page.ps = 0;
page3->data_bytes_sect = DEV_BSIZE;
/*
* make sure that logical block size is of
* DEV_BSIZE.
*/
header.block_descriptor.blksize_hi = (DEV_BSIZE >> 16);
header.block_descriptor.blksize_mid = (DEV_BSIZE >> 8);
header.block_descriptor.blksize_lo = (char)(DEV_BSIZE);
/*
* Select current Page 3 - Format Parameters page
*/
if (uscsi_mode_select(fd, DAD_MODE_FORMAT,
MODE_SELECT_PF, (caddr_t)&u_page3,
MODESENSE_PAGE_LEN(&u_page3), &header)) {
goto err;
}
/*
* Now reformat the device
*/
if (raw_format(fd)) {
goto err;
}
return (0);
}
err:
if (option_msg && diag_msg) {
err_print(
"Reformat device to 512-byte blocksize failed\n");
}
return (1);
}
static int
raw_format(
int fd)
{
union scsi_cdb cdb;
struct uscsi_cmd ucmd;
struct scsi_defect_hdr defect_hdr;
(void) memset((char *)&ucmd, 0, sizeof (ucmd));
(void) memset((char *)&cdb, 0, sizeof (union scsi_cdb));
(void) memset((char *)&defect_hdr, 0, sizeof (defect_hdr));
cdb.scc_cmd = SCMD_FORMAT;
ucmd.uscsi_cdb = (caddr_t)&cdb;
ucmd.uscsi_cdblen = CDB_GROUP0;
ucmd.uscsi_bufaddr = (caddr_t)&defect_hdr;
ucmd.uscsi_buflen = sizeof (defect_hdr);
cdb.cdb_opaque[1] = FPB_DATA;
/*
* Issue the format ioctl
*/
fmt_print("Formatting...\n");
(void) fflush(stdout);
if (uscsi_cmd(fd, &ucmd,
(option_msg && diag_msg) ? F_NORMAL : F_SILENT)) {
return (1);
}
return (0);
}
/*
* Copy a string of characters from src to dst, for at
* most n bytes. Strip all leading and trailing spaces,
* and stop if there are any non-printable characters.
* Return ptr to the next character to be filled.
*/
static char *
strcopy(
char *dst,
char *src,
int n)
{
int i;
while (*src == ' ' && n > 0) {
src++;
n--;
}
for (i = 0; n-- > 0 && isascii(*src) && isprint(*src); src++) {
if (*src == ' ') {
i++;
} else {
while (i-- > 0)
*dst++ = ' ';
*dst++ = *src;
}
}
*dst = 0;
return (dst);
}
/*
* adjust disk geometry.
* This is used when disk reports a disk geometry page having
* no of physical cylinders is < 3 which is the minimum required
* by Solaris (2 for storing labels and at least one as a data
* cylinder )
*/
int
adjust_disk_geometry(int capacity, int *cyl, int *nhead, int *nsect)
{
int lcyl = *cyl;
int lnhead = *nhead;
int lnsect = *nsect;
assert(lcyl < SUN_MIN_CYL);
/*
* reduce nsect by 2 for each iteration and re-calculate
* the number of cylinders.
*/
while (lnsect > MINIMUM_NO_SECTORS &&
lcyl < MINIMUM_NO_CYLINDERS) {
/*
* make sure that we do not go below MINIMUM_NO_SECTORS.
*/
lnsect = max(MINIMUM_NO_SECTORS, lnsect / 2);
lcyl = (capacity) / (lnhead * lnsect);
}
/*
* If the geometry still does not satisfy
* MINIMUM_NO_CYLINDERS then try to reduce the
* no of heads.
*/
while (lnhead > MINIMUM_NO_HEADS &&
(lcyl < MINIMUM_NO_CYLINDERS)) {
lnhead = max(MINIMUM_NO_HEADS, lnhead / 2);
lcyl = (capacity) / (lnhead * lnsect);
}
/*
* now we should have atleast SUN_MIN_CYL cylinders.
* If we still do not get SUN_MIN_CYL with MINIMUM_NO_HEADS
* and MINIMUM_NO_HEADS then return error.
*/
if (lcyl < SUN_MIN_CYL)
return (1);
else {
*cyl = lcyl;
*nhead = lnhead;
*nsect = lnsect;
return (0);
}
}
#if defined(_SUNOS_VTOC_8)
/*
* Reduce the size of one dimention below a specified
* limit with a minimum loss of volume. Dimenstions are
* assumed to be passed in form the largest value (the one
* that needs to be reduced) to the smallest value. The
* values will be twiddled until they are all less than or
* equal to their limit. Returns the number in the new geometry.
*/
static int
square_box(
int capacity,
int *dim1, int lim1,
int *dim2, int lim2,
int *dim3, int lim3)
{
int i;
/*
* Although the routine should work with any ordering of
* parameters, it's most efficient if they are passed in
* in decreasing magnitude.
*/
assert(*dim1 >= *dim2);
assert(*dim2 >= *dim3);
/*
* This is done in a very arbitrary manner. We could try to
* find better values but I can't come up with a method that
* would run in a reasonable amount of time. That could take
* approximately 65535 * 65535 iterations of a dozen flops each
* or well over 4G flops.
*
* First:
*
* Let's see how far we can go with bitshifts w/o losing
* any blocks.
*/
for (i = 0; (((*dim1)>>i)&1) == 0 && ((*dim1)>>i) > lim1; i++);
if (i) {
*dim1 = ((*dim1)>>i);
*dim3 = ((*dim3)<<i);
}
if (((*dim1) > lim1) || ((*dim2) > lim2) || ((*dim3) > lim3)) {
double d[4];
/*
* Second:
*
* Set the highest value at its limit then calculate errors,
* adjusting the 2nd highest value (we get better resolution
* that way).
*/
d[1] = lim1;
d[3] = *dim3;
d[2] = (double)capacity/(d[1]*d[3]);
/*
* If we overflowed the middle term, set it to its limit and
* chose a new low term.
*/
if (d[2] > lim2) {
d[2] = lim2;
d[3] = (double)capacity/(d[1]*d[2]);
}
/*
* Convert to integers.
*/
*dim1 = (int)d[1];
*dim2 = (int)d[2];
*dim3 = (int)d[3];
}
/*
* Fixup any other possible problems.
* If this happens, we need a new disklabel format.
*/
if (*dim1 > lim1) *dim1 = lim1;
if (*dim2 > lim2) *dim2 = lim2;
if (*dim3 > lim3) *dim3 = lim3;
return (*dim1 * *dim2 * *dim3);
}
#endif /* defined(_SUNOS_VTOC_8) */
|