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
|
/*
* CDDL HEADER START
*
* The contents of this file are subject to the terms of the
* Common Development and Distribution License (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 2008 Sun Microsystems, Inc. All rights reserved.
* Use is subject to license terms.
*/
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <strings.h>
#include <unistd.h>
#include <errno.h>
#include <fcntl.h>
#include <ctype.h>
#include <sys/stat.h>
#include <sys/types.h>
#include <sys/param.h>
#include <sys/systeminfo.h>
#include <sys/efi_partition.h>
#include <sys/byteorder.h>
#include <sys/vtoc.h>
#include <sys/tty.h>
#include <sys/dktp/fdisk.h>
#include <sys/dkio.h>
#include <sys/mnttab.h>
#include "libfdisk.h"
#define DEFAULT_PATH_PREFIX "/dev/rdsk/"
static void fdisk_free_ld_nodes(ext_part_t *epp);
static void fdisk_ext_place_in_sorted_list(ext_part_t *epp,
logical_drive_t *newld);
static void fdisk_ext_remove_from_sorted_list(ext_part_t *epp,
logical_drive_t *delld);
static int fdisk_ext_overlapping_parts(ext_part_t *epp, uint32_t begsec,
uint32_t endsec);
static int fdisk_read_extpart(ext_part_t *epp);
static void fdisk_set_CHS_values(ext_part_t *epp, struct ipart *part);
static int fdisk_init_master_part_table(ext_part_t *epp);
static struct ipart *fdisk_alloc_part_table();
static int fdisk_read_master_part_table(ext_part_t *epp);
static int
fdisk_init_disk_geom(ext_part_t *epp)
{
struct dk_geom disk_geom;
struct dk_minfo disk_info;
int no_virtgeom_ioctl = 0, no_physgeom_ioctl = 0;
/* Get disk's HBA (virtual) geometry */
errno = 0;
if (ioctl(epp->dev_fd, DKIOCG_VIRTGEOM, &disk_geom)) {
if (errno == ENOTTY) {
no_virtgeom_ioctl = 1;
} else if (errno == EINVAL) {
/*
* This means that the ioctl exists, but
* is invalid for this disk, meaning the
* disk doesn't have an HBA geometry
* (like, say, it's larger than 8GB).
*/
epp->disk_geom.virt_cyl = epp->disk_geom.virt_heads =
epp->disk_geom.virt_sec = 0;
} else {
return (FDISK_ENOVGEOM);
}
} else {
/* save virtual geometry values obtained by ioctl */
epp->disk_geom.virt_cyl = disk_geom.dkg_ncyl;
epp->disk_geom.virt_heads = disk_geom.dkg_nhead;
epp->disk_geom.virt_sec = disk_geom.dkg_nsect;
}
errno = 0;
if (ioctl(epp->dev_fd, DKIOCG_PHYGEOM, &disk_geom)) {
if (errno == ENOTTY) {
no_physgeom_ioctl = 1;
} else {
return (FDISK_ENOPGEOM);
}
}
/*
* Call DKIOCGGEOM if the ioctls for physical and virtual
* geometry fail. Get both from this generic call.
*/
if (no_virtgeom_ioctl && no_physgeom_ioctl) {
errno = 0;
if (ioctl(epp->dev_fd, DKIOCGGEOM, &disk_geom)) {
return (FDISK_ENOLGEOM);
}
}
epp->disk_geom.phys_cyl = disk_geom.dkg_ncyl;
epp->disk_geom.phys_heads = disk_geom.dkg_nhead;
epp->disk_geom.phys_sec = disk_geom.dkg_nsect;
epp->disk_geom.alt_cyl = disk_geom.dkg_acyl;
/*
* If DKIOCGMEDIAINFO ioctl succeeds, set the dki_lbsize as the
* size of the sector, else default to 512
*/
if (ioctl(epp->dev_fd, DKIOCGMEDIAINFO, (caddr_t)&disk_info) < 0) {
/* ioctl failed, falling back to default value of 512 bytes */
epp->disk_geom.sectsize = 512;
} else {
epp->disk_geom.sectsize = ((disk_info.dki_lbsize) ?
disk_info.dki_lbsize : 512);
}
/*
* if hba geometry was not set by DKIOC_VIRTGEOM
* or we got an invalid hba geometry
* then set hba geometry based on max values
*/
if (no_virtgeom_ioctl || disk_geom.dkg_ncyl == 0 ||
disk_geom.dkg_nhead == 0 || disk_geom.dkg_nsect == 0 ||
disk_geom.dkg_ncyl > MAX_CYL || disk_geom.dkg_nhead > MAX_HEAD ||
disk_geom.dkg_nsect > MAX_SECT) {
epp->disk_geom.virt_sec = MAX_SECT;
epp->disk_geom.virt_heads = MAX_HEAD + 1;
epp->disk_geom.virt_cyl = (epp->disk_geom.phys_cyl *
epp->disk_geom.phys_heads * epp->disk_geom.phys_sec) /
(epp->disk_geom.virt_sec * epp->disk_geom.virt_heads);
}
return (FDISK_SUCCESS);
}
/*
* Initialise important members of the ext_part_t structure and
* other data structures vital to functionality of libfdisk
*/
int
libfdisk_init(ext_part_t **epp, char *devstr, struct ipart *parttab, int opflag)
{
ext_part_t *temp;
char *canonp;
struct stat sbuf;
int rval = FDISK_SUCCESS;
if ((temp = calloc(1, sizeof (ext_part_t))) == NULL) {
return (ENOMEM);
}
canonp = strstr(devstr, DEFAULT_PATH_PREFIX);
if (canonp == NULL) {
(void) snprintf(temp->device_name, sizeof (temp->device_name),
"%s%s", DEFAULT_PATH_PREFIX, devstr);
} else {
(void) strncpy(temp->device_name, devstr,
sizeof (temp->device_name));
}
/*
* In case of an EFI labeled disk, the device name could be cN[tN]dN.
* There is no pN. So we add "p0" at the end if we do not find it.
*/
if (strrchr(temp->device_name, 'p') == NULL) {
(void) strcat(temp->device_name, "p0");
}
if (stat(temp->device_name, &sbuf) != 0) {
free(temp);
return (EINVAL);
}
temp->ld_head = NULL;
temp->sorted_ld_head = NULL;
if ((temp->dev_fd = open(temp->device_name, O_RDWR, 0666)) < 0) {
free(temp);
return (EINVAL);
}
if ((temp->mtable = parttab) == NULL) {
if ((rval = fdisk_init_master_part_table(temp)) !=
FDISK_SUCCESS) {
return (rval);
}
}
temp->op_flag = opflag;
if ((rval = fdisk_init_disk_geom(temp)) != FDISK_SUCCESS) {
return (rval);
}
*epp = temp;
if (opflag & FDISK_READ_DISK) {
rval = fdisk_read_extpart(*epp);
}
return (rval);
}
int
libfdisk_reset(ext_part_t *epp)
{
int rval = FDISK_SUCCESS;
fdisk_free_ld_nodes(epp);
epp->first_ebr_is_null = 1;
epp->corrupt_logical_drives = 0;
epp->logical_drive_count = 0;
epp->invalid_bb_sig[0] = 0;
if (epp->op_flag & FDISK_READ_DISK) {
rval = fdisk_read_extpart(epp);
}
return (rval);
}
void
libfdisk_fini(ext_part_t **epp)
{
fdisk_free_ld_nodes(*epp);
(void) close((*epp)->dev_fd);
free(*epp);
*epp = NULL;
}
int
fdisk_is_linux_swap(ext_part_t *epp, uint32_t part_start, off_t *lsm_offset)
{
int i;
int rval = -1;
off_t seek_offset;
uint32_t linux_pg_size;
char *buf, *linux_swap_magic;
int sec_sz = fdisk_get_disk_geom(epp, PHYSGEOM, SSIZE);
/*
* Known linux kernel page sizes
* The linux swap magic is found as the last 10 bytes of a disk chunk
* at the beginning of the linux swap partition whose size is that of
* kernel page size.
*/
uint32_t linux_pg_size_arr[] = {4096, };
if ((buf = calloc(1, sec_sz)) == NULL) {
return (ENOMEM);
}
linux_swap_magic = buf + sec_sz - LINUX_SWAP_MAGIC_LENGTH;
for (i = 0; i < sizeof (linux_pg_size_arr)/sizeof (uint32_t); i++) {
linux_pg_size = linux_pg_size_arr[i];
seek_offset = linux_pg_size/sec_sz - 1;
seek_offset += part_start;
seek_offset *= sec_sz;
if ((rval = lseek(epp->dev_fd, seek_offset, SEEK_SET)) < 0) {
break;
}
if ((rval = read(epp->dev_fd, buf, sec_sz)) < sec_sz) {
rval = EIO;
break;
}
if ((strncmp(linux_swap_magic, "SWAP-SPACE",
LINUX_SWAP_MAGIC_LENGTH) == 0) ||
(strncmp(linux_swap_magic, "SWAPSPACE2",
LINUX_SWAP_MAGIC_LENGTH) == 0)) {
/* Found a linux swap */
rval = 0;
*lsm_offset = seek_offset;
break;
}
}
free(buf);
return (rval);
}
int
fdisk_get_solaris_part(ext_part_t *epp, int *pnum, uint32_t *begsec,
uint32_t *numsec)
{
logical_drive_t *temp = fdisk_get_ld_head(epp);
uint32_t part_start;
int pno;
int rval = -1;
off_t lsmo = 0;
for (pno = 5; temp != NULL; temp = temp->next, pno++) {
if (fdisk_is_solaris_part(LE_8(temp->parts[0].systid))) {
part_start = temp->abs_secnum + temp->logdrive_offset;
if (fdisk_is_linux_swap(epp, part_start, &lsmo) == 0) {
continue;
}
*pnum = pno;
*begsec = part_start;
*numsec = temp->numsect;
rval = FDISK_SUCCESS;
}
}
return (rval);
}
int
fdisk_get_part_info(ext_part_t *epp, int pnum, uchar_t *sysid, uint32_t *begsec,
uint32_t *numsec)
{
logical_drive_t *temp = fdisk_get_ld_head(epp);
int pno;
if ((pnum < 5) || (pnum >= MAX_EXT_PARTS + 5)) {
return (EINVAL);
}
for (pno = 5; (pno < pnum) && (temp != NULL); temp = temp->next, pno++)
;
if (temp == NULL) {
return (EINVAL);
}
*sysid = LE_8(temp->parts[0].systid);
*begsec = temp->abs_secnum + temp->logdrive_offset;
*numsec = temp->numsect;
return (FDISK_SUCCESS);
}
/*
* Allocate a node of type logical_drive_t and return the pointer to it
*/
static logical_drive_t *
fdisk_alloc_ld_node()
{
logical_drive_t *temp;
if ((temp = calloc(1, sizeof (logical_drive_t))) == NULL) {
return (NULL);
}
temp->next = NULL;
return (temp);
}
/*
* Free all the logical_drive_t's allocated during the run
*/
static void
fdisk_free_ld_nodes(ext_part_t *epp)
{
logical_drive_t *temp;
for (temp = epp->ld_head; temp != NULL; ) {
temp = epp->ld_head -> next;
free(epp->ld_head);
epp->ld_head = temp;
}
epp->ld_head = NULL;
epp->sorted_ld_head = NULL;
}
/*
* Find the first free sector within the extended partition
*/
int
fdisk_ext_find_first_free_sec(ext_part_t *epp, uint32_t *first_free_sec)
{
logical_drive_t *temp;
uint32_t last_free_sec;
*first_free_sec = epp->ext_beg_sec;
if (epp->ld_head == NULL) {
return (FDISK_SUCCESS);
}
/*
* When the first logical drive is out of order, we need to adjust
* first_free_sec accordingly. In this case, the first extended
* partition sector is not free even though the actual logical drive
* does not occupy space from the beginning of the extended partition.
* The next free sector would be the second sector of the extended
* partition.
*/
if (epp->ld_head->abs_secnum > epp->ext_beg_sec +
MAX_LOGDRIVE_OFFSET) {
(*first_free_sec)++;
}
while (*first_free_sec <= epp->ext_end_sec) {
for (temp = epp->sorted_ld_head; temp != NULL; temp =
temp->sorted_next) {
if (temp->abs_secnum == *first_free_sec) {
*first_free_sec = temp->abs_secnum +
temp->logdrive_offset + temp->numsect;
}
}
last_free_sec = fdisk_ext_find_last_free_sec(epp,
*first_free_sec);
if ((last_free_sec - *first_free_sec) < MAX_LOGDRIVE_OFFSET) {
/*
* Minimum size of a partition assumed to be atleast one
* sector.
*/
*first_free_sec = last_free_sec + 1;
continue;
}
break;
}
if (*first_free_sec > epp->ext_end_sec) {
return (FDISK_EOOBOUND);
}
return (FDISK_SUCCESS);
}
/*
* Find the last free sector within the extended partition given, a beginning
* sector (so that the range - "begsec to last_free_sec" is contiguous)
*/
uint32_t
fdisk_ext_find_last_free_sec(ext_part_t *epp, uint32_t begsec)
{
logical_drive_t *temp;
uint32_t last_free_sec;
last_free_sec = epp->ext_end_sec;
for (temp = epp->sorted_ld_head; temp != NULL;
temp = temp->sorted_next) {
if (temp->abs_secnum > begsec) {
last_free_sec = temp->abs_secnum - 1;
break;
}
}
return (last_free_sec);
}
/*
* Place the given ext_part_t structure in a sorted list, sorted in the
* ascending order of their beginning sectors.
*/
static void
fdisk_ext_place_in_sorted_list(ext_part_t *epp, logical_drive_t *newld)
{
logical_drive_t *pre, *cur;
if (newld->abs_secnum < epp->sorted_ld_head->abs_secnum) {
newld->sorted_next = epp->sorted_ld_head;
epp->sorted_ld_head = newld;
return;
}
pre = cur = epp->sorted_ld_head;
for (; cur != NULL; pre = cur, cur = cur->sorted_next) {
if (newld->abs_secnum < cur->abs_secnum) {
break;
}
}
newld->sorted_next = cur;
pre->sorted_next = newld;
}
static void
fdisk_ext_remove_from_sorted_list(ext_part_t *epp, logical_drive_t *delld)
{
logical_drive_t *pre, *cur;
if (delld == epp->sorted_ld_head) {
epp->sorted_ld_head = delld->sorted_next;
return;
}
pre = cur = epp->sorted_ld_head;
for (; cur != NULL; pre = cur, cur = cur->sorted_next) {
if (cur->abs_secnum == delld->abs_secnum) {
/* Found */
break;
}
}
pre->sorted_next = cur->sorted_next;
}
static int
fdisk_ext_overlapping_parts(ext_part_t *epp, uint32_t begsec, uint32_t endsec)
{
logical_drive_t *temp;
uint32_t firstsec, lastsec, last_free_sec;
for (temp = epp->ld_head; temp != NULL; temp = temp->next) {
firstsec = temp->abs_secnum;
lastsec = firstsec + temp->logdrive_offset + temp->numsect - 1;
if ((begsec >= firstsec) &&
(begsec <= lastsec)) {
return (1);
}
}
/*
* Find the maximum possible end sector value
* given a beginning sector value
*/
last_free_sec = fdisk_ext_find_last_free_sec(epp, begsec);
if (endsec > last_free_sec) {
return (1);
}
return (0);
}
/*
* Check if the logical drive boundaries are sane
*/
int
fdisk_validate_logical_drive(ext_part_t *epp, uint32_t begsec,
uint32_t offset, uint32_t numsec)
{
uint32_t endsec;
endsec = begsec + offset + numsec - 1;
if (begsec < epp->ext_beg_sec ||
begsec > epp->ext_end_sec ||
endsec < epp->ext_beg_sec ||
endsec > epp->ext_end_sec ||
endsec < begsec ||
fdisk_ext_overlapping_parts(epp, begsec, endsec)) {
return (1);
}
return (0);
}
/*
* Procedure to walk through the extended partitions and build a Singly
* Linked List out of the data.
*/
int
fdisk_read_extpart(ext_part_t *epp)
{
struct ipart *fdp, *ext_fdp;
int i = 0, j = 0, ext_part_found = 0, lpart = 5;
off_t secnum, offset;
logical_drive_t *temp, *ep_ptr;
unsigned char *ext_buf;
int sectsize = epp->disk_geom.sectsize;
if ((ext_buf = (uchar_t *)malloc(sectsize)) == NULL) {
return (ENOMEM);
}
fdp = epp->mtable;
for (i = 0; (i < FD_NUMPART) && (!ext_part_found); i++, fdp++) {
if (fdisk_is_dos_extended(LE_8(fdp->systid))) {
ext_part_found = 1;
secnum = LE_32(fdp->relsect);
offset = secnum * sectsize;
epp->ext_beg_sec = secnum;
epp->ext_end_sec = secnum + LE_32(fdp->numsect) - 1;
epp->ext_beg_cyl =
FDISK_SECT_TO_CYL(epp, epp->ext_beg_sec);
epp->ext_end_cyl =
FDISK_SECT_TO_CYL(epp, epp->ext_end_sec);
/*LINTED*/
while (B_TRUE) {
if (lseek(epp->dev_fd, offset, SEEK_SET) < 0) {
return (EIO);
}
if (read(epp->dev_fd, ext_buf, sectsize) <
sectsize) {
return (EIO);
}
/*LINTED*/
ext_fdp = (struct ipart *)
(&ext_buf[FDISK_PART_TABLE_START]);
if ((LE_32(ext_fdp->relsect) == 0) &&
(epp->logical_drive_count == 0)) {
/* No logical drives defined */
epp->first_ebr_is_null = 0;
return (FDISK_ENOLOGDRIVE);
}
temp = fdisk_alloc_ld_node();
temp->abs_secnum = secnum;
temp->logdrive_offset =
LE_32(ext_fdp->relsect);
temp ->numsect = LE_32(ext_fdp->numsect);
if (epp->ld_head == NULL) {
/* adding first logical drive */
if (temp->logdrive_offset >
MAX_LOGDRIVE_OFFSET) {
/* out of order */
temp->abs_secnum +=
temp->logdrive_offset;
temp->logdrive_offset = 0;
}
}
temp->begcyl =
FDISK_SECT_TO_CYL(epp, temp->abs_secnum);
temp->endcyl = FDISK_SECT_TO_CYL(epp,
temp->abs_secnum +
temp->logdrive_offset +
temp->numsect - 1);
/*
* Check for sanity of logical drives
*/
if (fdisk_validate_logical_drive(epp,
temp->abs_secnum, temp->logdrive_offset,
temp->numsect)) {
epp->corrupt_logical_drives = 1;
free(temp);
return (FDISK_EBADLOGDRIVE);
}
temp->parts[0] = *ext_fdp;
ext_fdp++;
temp->parts[1] = *ext_fdp;
if (epp->ld_head == NULL) {
epp->ld_head = temp;
epp->sorted_ld_head = temp;
ep_ptr = temp;
epp->logical_drive_count = 1;
} else {
ep_ptr->next = temp;
ep_ptr = temp;
fdisk_ext_place_in_sorted_list(epp,
temp);
epp->logical_drive_count++;
}
/*LINTED*/
if (LE_16((*(uint16_t *)&ext_buf[510])) !=
MBB_MAGIC) {
epp->invalid_bb_sig[j++] = lpart;
temp->modified = FDISK_MINOR_WRITE;
}
if (LE_32(ext_fdp->relsect) == 0)
break;
else {
secnum = LE_32(fdp->relsect) +
LE_32(ext_fdp->relsect);
offset = secnum * sectsize;
}
lpart++;
}
}
}
return (FDISK_SUCCESS);
}
static int
fdisk_init_master_part_table(ext_part_t *epp)
{
int rval;
if ((epp->mtable = fdisk_alloc_part_table()) == NULL) {
return (ENOMEM);
}
rval = fdisk_read_master_part_table(epp);
if (rval) {
return (rval);
}
return (FDISK_SUCCESS);
}
static struct ipart *
fdisk_alloc_part_table()
{
int size = sizeof (struct ipart);
struct ipart *table;
if ((table = calloc(4, size)) == NULL) {
return (NULL);
}
return (table);
}
/*
* Reads the master fdisk partition table from the device assuming that it has
* a valid table.
* MBR is supposed to be of 512 bytes no matter what the device block size is.
*/
static int
fdisk_read_master_part_table(ext_part_t *epp)
{
uchar_t buf[512];
int sectsize = 512;
int size = sizeof (struct ipart);
int cpcnt = FD_NUMPART * size;
if (lseek(epp->dev_fd, 0, SEEK_SET) < 0) {
return (EIO);
}
if (read(epp->dev_fd, buf, sectsize) < sectsize) {
return (EIO);
}
bcopy(&buf[FDISK_PART_TABLE_START], epp->mtable, cpcnt);
/*LINTED*/
if (LE_16((*(uint16_t *)&buf[510])) != MBB_MAGIC) {
return (FDISK_EBADMAGIC);
}
return (FDISK_SUCCESS);
}
int
fdisk_ext_part_exists(ext_part_t *epp)
{
int i;
struct ipart *part_table = epp->mtable;
if (part_table == NULL) {
/* No extended partition found */
return (0);
}
for (i = 0; i < FD_NUMPART; i++) {
if (fdisk_is_dos_extended(LE_8(part_table[i].systid))) {
break;
}
}
if (i == FD_NUMPART) {
/* No extended partition found */
return (0);
}
return (1);
}
int
fdisk_ext_validate_part_start(ext_part_t *epp, uint32_t begcyl,
uint32_t *begsec)
{
logical_drive_t *temp;
uint32_t first_free_sec;
uint32_t first_free_cyl;
int rval;
rval = fdisk_ext_find_first_free_sec(epp, &first_free_sec);
if (rval != FDISK_SUCCESS) {
return (rval);
}
first_free_cyl = FDISK_SECT_TO_CYL(epp, first_free_sec);
if (begcyl == first_free_cyl) {
*begsec = first_free_sec;
return (FDISK_SUCCESS);
}
/* Check if the cylinder number is beyond the extended partition */
if ((begcyl < epp->ext_beg_cyl) || (begcyl > epp->ext_end_cyl)) {
return (FDISK_EOOBOUND);
}
for (temp = epp->ld_head; temp != NULL; temp = temp->next) {
if ((begcyl >= temp->begcyl) &&
(begcyl <= temp->endcyl)) {
return (FDISK_EOVERLAP);
}
}
*begsec = FDISK_CYL_TO_SECT(epp, begcyl);
return (FDISK_SUCCESS);
}
void
fdisk_change_logical_drive_id(ext_part_t *epp, int pno, uchar_t partid)
{
logical_drive_t *temp;
int i;
i = FD_NUMPART + 1;
for (temp = epp->ld_head; i < pno; temp = temp->next, i++)
;
temp->parts[0].systid = LE_8(partid);
temp->modified = FDISK_MAJOR_WRITE;
}
/*
* A couple of special scenarios :
* 1. Since the first logical drive's EBR is always at the beginning of the
* extended partition, any specification that starts the first logical drive
* out of order will need to address the following issue :
* If the beginning of the drive is not coinciding with the beginning of the
* extended partition and :
* a) The start is within MAX_LOGDRIVE_OFFSET, the offset changes from the
* default of 63 to less than 63.
* logdrive_offset is updated to keep track of the space between
* the beginning of the logical drive and extended partition. abs_secnum
* points to the beginning of the extended partition.
* b) The start is greater than MAX_LOGDRIVE_OFFSET, the offset changes from
* the default of 63 to greater than 63.
* logdrive_offset is set to 0. abs_secnum points to the beginning of the
* logical drive, which is at an offset from the extended partition.
*/
void
fdisk_add_logical_drive(ext_part_t *epp, uint32_t begsec, uint32_t endsec,
uchar_t partid)
{
logical_drive_t *temp, *pre, *cur;
struct ipart *part;
temp = fdisk_alloc_ld_node();
temp->abs_secnum = begsec;
temp->logdrive_offset = MAX_LOGDRIVE_OFFSET;
temp->numsect = endsec - begsec + 1 - MAX_LOGDRIVE_OFFSET;
temp->begcyl = FDISK_SECT_TO_CYL(epp, begsec);
temp->endcyl = FDISK_SECT_TO_CYL(epp, endsec);
temp->modified = FDISK_MAJOR_WRITE;
part = &temp->parts[0];
part->bootid = 0;
part->systid = LE_8(partid);
part->relsect = MAX_LOGDRIVE_OFFSET;
part->numsect = LE_32(temp->numsect);
fdisk_set_CHS_values(epp, part);
if (epp->ld_head == NULL) {
epp->corrupt_logical_drives = 0;
if (begsec != epp->ext_beg_sec) {
part->relsect = LE_32(begsec - epp->ext_beg_sec);
temp->numsect = endsec - begsec + 1;
part->numsect = LE_32(temp->numsect);
if (LE_32(part->relsect) > MAX_LOGDRIVE_OFFSET) {
temp->logdrive_offset = 0;
} else {
temp->abs_secnum = epp->ext_beg_sec;
temp->logdrive_offset = LE_32(part->relsect);
}
}
epp->first_ebr_is_null = 0;
epp->ld_head = temp;
epp->sorted_ld_head = temp;
epp->logical_drive_count = 1;
return;
}
if (temp->abs_secnum == epp->ext_beg_sec) {
part->relsect = LE_32(LE_32(part->relsect) - 1);
temp->logdrive_offset--;
temp->abs_secnum++;
}
for (pre = cur = epp->ld_head; cur != NULL; pre = cur, cur = cur->next)
;
part = &pre->parts[1];
part->bootid = 0;
part->systid = LE_8(EXTDOS);
part->relsect = LE_32(temp->abs_secnum - epp->ext_beg_sec);
part->numsect = LE_32(temp->numsect + temp->logdrive_offset);
fdisk_set_CHS_values(epp, part);
pre->next = temp;
pre->modified = FDISK_MAJOR_WRITE;
epp->logical_drive_count++;
fdisk_ext_place_in_sorted_list(epp, temp);
}
/*
* There are 2 cases that need to be handled.
* 1. Deleting the first extended partition :
* The peculiarity of this case is that the offset of the first extended
* partition is always indicated by the entry in the master boot record.
* (MBR). This never changes, unless the extended partition itself is
* deleted. Hence, the location of the first EBR is fixed.
* It is only the logical drive which is deleted. This first EBR now gives
* information of the next logical drive and the info about the subsequent
* extended partition. Hence the "relsect" of the first EBR is modified to
* point to the next logical drive.
*
* 2. Deleting an intermediate extended partition.
* This is quite normal and follows the semantics of a normal linked list
* delete operation. The node being deleted has the information about the
* logical drive that it houses and the location and the size of the next
* extended partition. This informationis transferred to the node previous
* to the node being deleted.
*
*/
void
fdisk_delete_logical_drive(ext_part_t *epp, int pno)
{
logical_drive_t *pre, *cur;
int i;
i = FD_NUMPART + 1;
pre = cur = epp->ld_head;
for (; i < pno; i++) {
pre = cur;
cur = cur->next;
}
if (cur == epp->ld_head) {
/* Deleting the first logical drive */
if (cur->next == NULL) {
/* Deleting the only logical drive left */
free(cur);
epp->ld_head = NULL;
epp->sorted_ld_head = NULL;
epp->logical_drive_count = 0;
epp->first_ebr_is_null = 1;
} else {
pre = epp->ld_head;
cur = pre->next;
cur->parts[0].relsect =
LE_32(LE_32(cur->parts[0].relsect) +
LE_32(pre->parts[1].relsect));
/* Corner case when partitions are out of order */
if ((pre->abs_secnum != epp->ext_beg_sec) &&
(cur->abs_secnum == epp->ext_beg_sec + 1)) {
cur->logdrive_offset++;
cur->abs_secnum = epp->ext_beg_sec;
} else {
cur->abs_secnum = LE_32(cur->parts[0].relsect) +
epp->ext_beg_sec;
cur->logdrive_offset = 0;
}
fdisk_ext_remove_from_sorted_list(epp, pre);
epp->ld_head = cur;
epp->ld_head->modified = FDISK_MAJOR_WRITE;
epp->logical_drive_count--;
free(pre);
}
} else {
pre->parts[1] = cur->parts[1];
pre->next = cur->next;
fdisk_ext_remove_from_sorted_list(epp, cur);
pre->modified = FDISK_MAJOR_WRITE;
free(cur);
epp->logical_drive_count--;
}
}
static void
fdisk_set_CHS_values(ext_part_t *epp, struct ipart *part)
{
uint32_t lba, cy, hd, sc;
uint32_t sectors = epp->disk_geom.virt_sec;
uint32_t heads = epp->disk_geom.virt_heads;
lba = LE_32(part->relsect) + epp->ext_beg_sec;
if (lba >= heads * sectors * MAX_CYL) {
/*
* the lba address cannot be expressed in CHS value
* so store the maximum CHS field values in the CHS fields.
*/
cy = MAX_CYL + 1;
hd = MAX_HEAD;
sc = MAX_SECT;
} else {
cy = lba / sectors / heads;
hd = lba / sectors % heads;
sc = lba % sectors + 1;
}
part->begcyl = cy & 0xff;
part->beghead = (uchar_t)hd;
part->begsect = (uchar_t)(((cy >> 2) & 0xc0) | sc);
/*
* This code is identical to the code above
* except that it works on ending CHS values
*/
lba += LE_32(part->numsect - 1);
if (lba >= heads * sectors * MAX_CYL) {
cy = MAX_CYL + 1;
hd = MAX_HEAD;
sc = MAX_SECT;
} else {
cy = lba / sectors / heads;
hd = lba / sectors % heads;
sc = lba % sectors + 1;
}
part->endcyl = cy & 0xff;
part->endhead = (uchar_t)hd;
part->endsect = (uchar_t)(((cy >> 2) & 0xc0) | sc);
}
static int
read_modify_write_ebr(ext_part_t *epp, unsigned char *ebr_buf,
struct ipart *ebr_tab, uint32_t sec_offset)
{
off_t seek_offset;
int sectsize = epp->disk_geom.sectsize;
seek_offset = (off_t)sec_offset * sectsize;
if (lseek(epp->dev_fd, seek_offset, SEEK_SET) < 0) {
return (EIO);
}
if (read(epp->dev_fd, ebr_buf, sectsize) < sectsize) {
return (EIO);
}
bzero(&ebr_buf[FDISK_PART_TABLE_START], 4 * sizeof (struct ipart));
if (ebr_tab != NULL) {
bcopy(ebr_tab, &ebr_buf[FDISK_PART_TABLE_START],
2 * sizeof (struct ipart));
}
ebr_buf[510] = 0x55;
ebr_buf[511] = 0xAA;
if (lseek(epp->dev_fd, seek_offset, SEEK_SET) < 0) {
return (EIO);
}
if (write(epp->dev_fd, ebr_buf, sectsize) < sectsize) {
return (EIO);
}
return (0);
}
/*
* XXX - ZFS mounts not detected. Needs to come in as a feature.
* Currently only /etc/mnttab entries are being checked
*/
int
fdisk_mounted_logical_drives(ext_part_t *epp)
{
char *part_str, *canonp;
char compare_pdev_str[PATH_MAX];
char compare_sdev_str[PATH_MAX];
FILE *fp;
struct mnttab mt;
int part;
int look_for_mounted_slices = 0;
uint32_t begsec, numsec;
if ((fp = fopen(MNTTAB, "r")) == NULL) {
return (ENOENT);
}
canonp = epp->device_name + strlen(DEFAULT_PATH_PREFIX);
(void) snprintf(compare_pdev_str, PATH_MAX, "%s%s", "/dev/dsk/",
canonp);
part_str = strrchr(compare_pdev_str, 'p');
*(part_str + 1) = '\0';
(void) strcpy(compare_sdev_str, compare_pdev_str);
part_str = strrchr(compare_sdev_str, 'p');
*part_str = 's';
if (fdisk_get_solaris_part(epp, &part, &begsec, &numsec) ==
FDISK_SUCCESS) {
if (part > FD_NUMPART) {
/*
* Solaris partition is on a logical drive. Look for
* mounted slices.
*/
look_for_mounted_slices = 1;
}
}
while (getmntent(fp, &mt) == 0) {
if (strstr(mt.mnt_special, compare_pdev_str) == NULL) {
if (strstr(mt.mnt_special, compare_sdev_str) == NULL) {
continue;
} else {
if (look_for_mounted_slices) {
return (FDISK_EMOUNTED);
}
}
}
/*
* Get the partition number that is mounted, which would be
* found just beyond the last 'p' in the device string.
* For example, in /dev/dsk/c0t0d0p12, partition number 12
* is just beyond the last 'p'.
*/
part_str = strrchr(mt.mnt_special, 'p');
if (part_str != NULL) {
part_str++;
part = atoi(part_str);
/* Extended partition numbers start from 5 */
if (part >= 5) {
return (FDISK_EMOUNTED);
}
}
}
return (0);
}
int
fdisk_commit_ext_part(ext_part_t *epp)
{
logical_drive_t *temp;
int wflag = 0; /* write flag */
int rval;
int sectsize = epp->disk_geom.sectsize;
unsigned char *ebr_buf;
int ld_count;
uint32_t abs_secnum;
int check_mounts = 0;
off_t lsmo;
char *lsm_buf;
if ((ebr_buf = (unsigned char *)malloc(sectsize)) == NULL) {
return (ENOMEM);
}
if ((lsm_buf = calloc(1, sectsize)) == NULL) {
return (ENOMEM);
}
if (epp->first_ebr_is_null) {
/*
* Indicator that the extended partition as a whole was
* modifies (either created or deleted. Must check for mounts
* and must commit
*/
check_mounts = 1;
}
/*
* Pass1 through the logical drives to make sure that commit of minor
* written block dont get held up due to mounts.
*/
for (temp = epp->ld_head; temp != NULL; temp = temp->next) {
if (temp == epp->ld_head) {
abs_secnum = epp->ext_beg_sec;
} else {
abs_secnum = temp->abs_secnum;
}
if (temp->modified == FDISK_MINOR_WRITE) {
rval = read_modify_write_ebr(epp, ebr_buf,
temp->parts, abs_secnum);
if (rval) {
goto error;
}
temp->modified = 0;
} else if (temp->modified == FDISK_MAJOR_WRITE) {
check_mounts = 1;
}
}
if (!check_mounts) {
goto skip_check_mounts;
}
if ((rval = fdisk_mounted_logical_drives(epp)) != 0) {
/* One/more extended partitions are mounted */
if (ebr_buf) {
free(ebr_buf);
}
if (lsm_buf) {
free(lsm_buf);
}
return (rval);
}
skip_check_mounts:
if (epp->first_ebr_is_null) {
rval = read_modify_write_ebr(epp, ebr_buf, NULL,
epp->ext_beg_sec);
if (rval) {
goto error;
}
wflag = 1;
ld_count = 0;
} else {
if (epp->logical_drive_count == 0) {
/*
* Can hit this case when there is just an extended
* partition with no logical drives, and the user
* committed without making any changes
* We dont have anything to commit. Return success
*/
if (ebr_buf) {
free(ebr_buf);
}
if (lsm_buf) {
free(lsm_buf);
}
return (FDISK_SUCCESS);
}
/*
* Make sure that the first EBR is written with the first
* logical drive's data, which might not be the first in disk
* order.
*/
for (temp = epp->ld_head, ld_count = 0; temp != NULL;
temp = temp->next, ld_count++) {
/*
* Check if the current partition is a solaris old
* partition. In that case, check if it was previously
* a linux swap. If so, overwrite the linux swap magic.
*/
if (temp->parts[0].systid == SUNIXOS) {
uint32_t secnum = temp->abs_secnum +
temp->logdrive_offset;
if (fdisk_is_linux_swap(epp, secnum,
&lsmo) == 0) {
if ((rval = lseek(epp->dev_fd, lsmo,
SEEK_SET)) < 0) {
if (ld_count) {
break;
}
goto error;
}
if (read(epp->dev_fd, lsm_buf,
sectsize) < sectsize) {
rval = EIO;
if (ld_count) {
break;
}
goto error;
}
bzero(lsm_buf + sectsize -
LINUX_SWAP_MAGIC_LENGTH,
LINUX_SWAP_MAGIC_LENGTH);
if ((rval = lseek(epp->dev_fd, lsmo,
SEEK_SET)) < 0) {
if (ld_count) {
break;
}
goto error;
}
if ((rval = write(epp->dev_fd, lsm_buf,
sectsize)) < sectsize) {
rval = EIO;
if (ld_count) {
break;
}
goto error;
}
}
}
if (ld_count == 0) {
abs_secnum = epp->ext_beg_sec;
} else {
abs_secnum = temp->abs_secnum;
}
if (temp->modified) {
rval = read_modify_write_ebr(epp, ebr_buf,
temp->parts, abs_secnum);
if (rval) {
if (ld_count) {
/*
* There was atleast one
* write to the disk before
* this failure. Make sure that
* the kernel is notified.
* Issue the ioctl.
*/
break;
}
goto error;
}
if ((!wflag) && (temp->modified ==
FDISK_MAJOR_WRITE)) {
wflag = 1;
}
}
}
if (wflag == 0) {
/* No changes made */
rval = FDISK_SUCCESS;
goto error;
}
}
/* Issue ioctl to the driver to update extended partition info */
rval = ioctl(epp->dev_fd, DKIOCSETEXTPART);
error:
if (ebr_buf) {
free(ebr_buf);
}
if (lsm_buf) {
free(lsm_buf);
}
return (rval);
}
int
fdisk_init_ext_part(ext_part_t *epp, uint32_t rsect, uint32_t nsect)
{
epp->first_ebr_is_null = 1;
epp->corrupt_logical_drives = 0;
epp->logical_drive_count = 0;
epp->ext_beg_sec = rsect;
epp->ext_end_sec = rsect + nsect - 1;
epp->ext_beg_cyl = FDISK_SECT_TO_CYL(epp, epp->ext_beg_sec);
epp->ext_end_cyl = FDISK_SECT_TO_CYL(epp, epp->ext_end_sec);
epp->invalid_bb_sig[0] = 0;
return (0);
}
int
fdisk_delete_ext_part(ext_part_t *epp)
{
epp->first_ebr_is_null = 1;
/* Clear the logical drive information */
fdisk_free_ld_nodes(epp);
epp->logical_drive_count = 0;
epp->corrupt_logical_drives = 0;
epp->invalid_bb_sig[0] = 0;
return (0);
}
int
fdisk_get_disk_geom(ext_part_t *epp, int type, int what)
{
switch (type) {
case PHYSGEOM:
switch (what) {
case NCYL:
return ((int)epp->disk_geom.phys_cyl);
case NHEADS:
return ((int)epp->disk_geom.phys_heads);
case NSECTPT:
return ((int)epp->disk_geom.phys_sec);
case SSIZE:
return ((int)epp->disk_geom.sectsize);
case ACYL:
return ((int)epp->disk_geom.alt_cyl);
default:
return (EINVAL);
}
case VIRTGEOM:
switch (what) {
case NCYL:
return ((int)epp->disk_geom.virt_cyl);
case NHEADS:
return ((int)epp->disk_geom.virt_heads);
case NSECTPT:
return ((int)epp->disk_geom.virt_sec);
case SSIZE:
return ((int)epp->disk_geom.sectsize);
case ACYL:
return ((int)epp->disk_geom.alt_cyl);
default:
return (EINVAL);
}
default:
return (EINVAL);
}
}
int
fdisk_invalid_bb_sig(ext_part_t *epp, uchar_t **bbsig_arr)
{
*bbsig_arr = &(epp->invalid_bb_sig[0]);
return (epp->invalid_bb_sig[0]);
}
|