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
|
#include "c.h"
#include "strutils.h"
#ifdef HAVE_LIBBLKID
# include <blkid.h>
#endif
#include "fdiskP.h"
/**
* SECTION: partition
* @title: Partition
* @short_description: generic label independent partition abstraction
*
* The fdisk_partition provides label independent abstraction. The partitions
* are not directly connected with partition table (label) data. Any change to
* fdisk_partition does not affects in-memory or on-disk label data.
*
* The fdisk_partition is possible to use as a template for
* fdisk_add_partition() or fdisk_set_partition() operations.
*/
static void init_partition(struct fdisk_partition *pa)
{
FDISK_INIT_UNDEF(pa->size);
FDISK_INIT_UNDEF(pa->start);
FDISK_INIT_UNDEF(pa->partno);
FDISK_INIT_UNDEF(pa->parent_partno);
FDISK_INIT_UNDEF(pa->boot);
INIT_LIST_HEAD(&pa->parts);
}
/**
* fdisk_new_partition:
*
* Returns: new instance.
*/
struct fdisk_partition *fdisk_new_partition(void)
{
struct fdisk_partition *pa = calloc(1, sizeof(*pa));
if (!pa)
return NULL;
pa->refcount = 1;
init_partition(pa);
DBG(PART, ul_debugobj(pa, "alloc"));
return pa;
}
/**
* fdisk_reset_partition:
* @pa: partition
*
* Resets partition content.
*/
void fdisk_reset_partition(struct fdisk_partition *pa)
{
int ref;
if (!pa)
return;
DBG(PART, ul_debugobj(pa, "reset"));
ref = pa->refcount;
fdisk_unref_parttype(pa->type);
free(pa->name);
free(pa->uuid);
free(pa->attrs);
free(pa->fstype);
free(pa->fsuuid);
free(pa->fslabel);
free(pa->start_chs);
free(pa->end_chs);
memset(pa, 0, sizeof(*pa));
pa->refcount = ref;
init_partition(pa);
}
static struct fdisk_partition *__copy_partition(struct fdisk_partition *o)
{
struct fdisk_partition *n = fdisk_new_partition();
int rc;
if (!n)
return NULL;
memcpy(n, o, sizeof(*n));
/* do not copy reference to lists, etc.*/
n->refcount = 1;
INIT_LIST_HEAD(&n->parts);
if (n->type)
fdisk_ref_parttype(n->type);
/* note that strdup_between_structs() deallocates destination pointer,
* so make sure it's NULL as we call memcpy() before ... */
n->name = NULL;
rc = strdup_between_structs(n, o, name);
n->uuid = NULL;
if (!rc)
rc = strdup_between_structs(n, o, uuid);
n->attrs = NULL;
if (!rc)
rc = strdup_between_structs(n, o, attrs);
n->fstype = NULL;
if (!rc)
rc = strdup_between_structs(n, o, fstype);
n->fsuuid = NULL;
if (!rc)
rc = strdup_between_structs(n, o, fsuuid);
n->fslabel = NULL;
if (!rc)
rc = strdup_between_structs(n, o, fslabel);
n->start_chs = NULL;
if (!rc)
rc = strdup_between_structs(n, o, start_chs);
n->end_chs = NULL;
if (!rc)
rc = strdup_between_structs(n, o, end_chs);
if (rc) {
fdisk_unref_partition(n);
n = NULL;
}
return n;
}
/**
* fdisk_ref_partition:
* @pa: partition pointer
*
* Increments reference counter.
*/
void fdisk_ref_partition(struct fdisk_partition *pa)
{
if (pa)
pa->refcount++;
}
/**
* fdisk_unref_partition:
* @pa: partition pointer
*
* Decrements reference counter, on zero the @pa is automatically
* deallocated.
*/
void fdisk_unref_partition(struct fdisk_partition *pa)
{
if (!pa)
return;
pa->refcount--;
if (pa->refcount <= 0) {
list_del(&pa->parts);
fdisk_reset_partition(pa);
DBG(PART, ul_debugobj(pa, "free"));
free(pa);
}
}
/**
* fdisk_partition_set_start:
* @pa: partition
* @off: offset in sectors, maximal is UINT64_MAX-1
*
* Note that zero is valid offset too. Use fdisk_partition_unset_start() to
* undefine the offset.
*
* Returns: 0 on success, <0 on error.
*/
int fdisk_partition_set_start(struct fdisk_partition *pa, fdisk_sector_t off)
{
if (!pa)
return -EINVAL;
if (FDISK_IS_UNDEF(off))
return -ERANGE;
pa->start = off;
pa->fs_probed = 0;
return 0;
}
/**
* fdisk_partition_unset_start:
* @pa: partition
*
* Sets the size as undefined. See fdisk_partition_has_start().
*
* Returns: 0 on success, <0 on error.
*/
int fdisk_partition_unset_start(struct fdisk_partition *pa)
{
if (!pa)
return -EINVAL;
FDISK_INIT_UNDEF(pa->start);
pa->fs_probed = 0;
return 0;
}
/**
* fdisk_partition_get_start:
* @pa: partition
*
* The zero is also valid offset. The function may return random undefined
* value when start offset is undefined (for example after
* fdisk_partition_unset_start()). Always use fdisk_partition_has_start() to be
* sure that you work with valid numbers.
*
* Returns: start offset in sectors
*/
fdisk_sector_t fdisk_partition_get_start(struct fdisk_partition *pa)
{
return pa->start;
}
/**
* fdisk_partition_has_start:
* @pa: partition
*
* Returns: 1 or 0
*/
int fdisk_partition_has_start(struct fdisk_partition *pa)
{
return pa && !FDISK_IS_UNDEF(pa->start);
}
/**
* fdisk_partition_cmp_start:
* @a: partition
* @b: partition
*
* Compares partitions according to start offset, See fdisk_table_sort_partitions().
*
* Return: 0 if the same, <0 if @b greater, >0 if @a greater.
*/
int fdisk_partition_cmp_start(struct fdisk_partition *a,
struct fdisk_partition *b)
{
int no_a = FDISK_IS_UNDEF(a->start),
no_b = FDISK_IS_UNDEF(b->start);
if (no_a && no_b)
return 0;
if (no_a)
return -1;
if (no_b)
return 1;
return cmp_numbers(a->start, b->start);
}
/**
* fdisk_partition_start_follow_default
* @pa: partition
* @enable: 0|1
*
* When @pa used as a template for fdisk_add_partition() when force label driver
* to use the first possible space for the new partition.
*
* Returns: 0 on success, <0 on error.
*/
int fdisk_partition_start_follow_default(struct fdisk_partition *pa, int enable)
{
if (!pa)
return -EINVAL;
pa->start_follow_default = enable ? 1 : 0;
return 0;
}
/**
* fdisk_partition_start_is_default:
* @pa: partition
*
* See fdisk_partition_start_follow_default().
*
* Returns: 1 if the partition follows default
*/
int fdisk_partition_start_is_default(struct fdisk_partition *pa)
{
assert(pa);
return pa->start_follow_default;
}
/**
* fdisk_partition_set_size:
* @pa: partition
* @sz: size in sectors, maximal is UIN64_MAX-1
*
* Note that zero is valid size too. Use fdisk_partition_unset_size() to
* undefine the size.
*
* Returns: 0 on success, <0 on error.
*/
int fdisk_partition_set_size(struct fdisk_partition *pa, fdisk_sector_t sz)
{
if (!pa)
return -EINVAL;
if (FDISK_IS_UNDEF(sz))
return -ERANGE;
pa->size = sz;
pa->fs_probed = 0;
return 0;
}
/**
* fdisk_partition_unset_size:
* @pa: partition
*
* Sets the size as undefined. See fdisk_partition_has_size().
*
* Returns: 0 on success, <0 on error.
*/
int fdisk_partition_unset_size(struct fdisk_partition *pa)
{
if (!pa)
return -EINVAL;
FDISK_INIT_UNDEF(pa->size);
pa->fs_probed = 0;
return 0;
}
/**
* fdisk_partition_get_size:
* @pa: partition
*
* The zero is also valid size. The function may return random undefined
* value when size is undefined (for example after fdisk_partition_unset_size()).
* Always use fdisk_partition_has_size() to be sure that you work with valid
* numbers.
*
* Returns: size offset in sectors
*/
fdisk_sector_t fdisk_partition_get_size(struct fdisk_partition *pa)
{
return pa->size;
}
/**
* fdisk_partition_has_size:
* @pa: partition
*
* Returns: 1 or 0
*/
int fdisk_partition_has_size(struct fdisk_partition *pa)
{
return pa && !FDISK_IS_UNDEF(pa->size);
}
/**
* fdisk_partition_size_explicit:
* @pa: partition
* @enable: 0|1
*
* By default libfdisk aligns the size when add the new partition (by
* fdisk_add_partition()). If you want to disable this functionality use
* @enable = 1.
*
* Returns: 0 on success, <0 on error.
*/
int fdisk_partition_size_explicit(struct fdisk_partition *pa, int enable)
{
if (!pa)
return -EINVAL;
pa->size_explicit = enable ? 1 : 0;
return 0;
}
/**
* fdisk_partition_set_partno:
* @pa: partition
* @num: partition number (0 is the first partition, maximal is SIZE_MAX-1)
*
* Note that zero is valid partno too. Use fdisk_partition_unset_partno() to
* undefine the partno.
*
* Returns: 0 on success, <0 on error.
*/
int fdisk_partition_set_partno(struct fdisk_partition *pa, size_t num)
{
if (!pa)
return -EINVAL;
if (FDISK_IS_UNDEF(num))
return -ERANGE;
pa->partno = num;
return 0;
}
/**
* fdisk_partition_unset_partno:
* @pa: partition
*
* Sets the partno as undefined. See fdisk_partition_has_partno().
*
* Returns: 0 on success, <0 on error.
*/
int fdisk_partition_unset_partno(struct fdisk_partition *pa)
{
if (!pa)
return -EINVAL;
FDISK_INIT_UNDEF(pa->partno);
return 0;
}
/**
* fdisk_partition_get_partno:
* @pa: partition
*
* The zero is also valid partition number. The function may return random
* value when partno is undefined (for example after fdisk_partition_unset_partno()).
* Always use fdisk_partition_has_partno() to be sure that you work with valid
* numbers.
*
* Returns: partition number (0 is the first partition)
*/
size_t fdisk_partition_get_partno(struct fdisk_partition *pa)
{
return pa->partno;
}
/**
* fdisk_partition_has_partno:
* @pa: partition
*
* Returns: 1 or 0
*/
int fdisk_partition_has_partno(struct fdisk_partition *pa)
{
return pa && !FDISK_IS_UNDEF(pa->partno);
}
/**
* fdisk_partition_cmp_partno:
* @a: partition
* @b: partition
*
* Compares partitions according to partition number See fdisk_table_sort_partitions().
*
* Return: 0 if the same, <0 if @b greater, >0 if @a greater.
*/
int fdisk_partition_cmp_partno(struct fdisk_partition *a,
struct fdisk_partition *b)
{
return a->partno - b->partno;
}
/**
* fdisk_partition_partno_follow_default
* @pa: partition
* @enable: 0|1
*
* When @pa used as a template for fdisk_add_partition() when force label driver
* to add a new partition to the default (next) position.
*
* Returns: 0 on success, <0 on error.
*/
int fdisk_partition_partno_follow_default(struct fdisk_partition *pa, int enable)
{
if (!pa)
return -EINVAL;
pa->partno_follow_default = enable ? 1 : 0;
return 0;
}
/**
* fdisk_partition_set_type:
* @pa: partition
* @type: partition type
*
* Sets partition type.
*
* Returns: 0 on success, <0 on error.
*/
int fdisk_partition_set_type(struct fdisk_partition *pa,
struct fdisk_parttype *type)
{
if (!pa)
return -EINVAL;
fdisk_ref_parttype(type);
fdisk_unref_parttype(pa->type);
pa->type = type;
return 0;
}
/**
* fdisk_partition_get_type:
* @pa: partition
*
* Returns: pointer to partition type.
*/
struct fdisk_parttype *fdisk_partition_get_type(struct fdisk_partition *pa)
{
return pa ? pa->type : NULL;
}
/**
* fdisk_partition_set_name:
* @pa: partition
* @name: partition name
*
* Returns: 0 on success, <0 on error.
*/
int fdisk_partition_set_name(struct fdisk_partition *pa, const char *name)
{
if (!pa)
return -EINVAL;
return strdup_to_struct_member(pa, name, name);
}
/**
* fdisk_partition_get_name:
* @pa: partition
*
* Returns: partition name
*/
const char *fdisk_partition_get_name(struct fdisk_partition *pa)
{
return pa ? pa->name : NULL;
}
/**
* fdisk_partition_set_uuid:
* @pa: partition
* @uuid: UUID of the partition
*
* Returns: 0 on success, <0 on error.
*/
int fdisk_partition_set_uuid(struct fdisk_partition *pa, const char *uuid)
{
if (!pa)
return -EINVAL;
return strdup_to_struct_member(pa, uuid, uuid);
}
/**
* fdisk_partition_has_end:
* @pa: partition
*
* Returns: 1 if the partition has defined last sector
*/
int fdisk_partition_has_end(struct fdisk_partition *pa)
{
return pa && !FDISK_IS_UNDEF(pa->start) && !FDISK_IS_UNDEF(pa->size);
}
/**
* fdisk_partition_get_end:
* @pa: partition
*
* This function may returns absolute non-sense, always check
* fdisk_partition_has_end().
*
* Note that partition end is defined by fdisk_partition_set_start() and
* fdisk_partition_set_size().
*
* Returns: last partition sector LBA.
*/
fdisk_sector_t fdisk_partition_get_end(struct fdisk_partition *pa)
{
return pa->start + pa->size - (pa->size == 0 ? 0 : 1);
}
/**
* fdisk_partition_end_follow_default
* @pa: partition
* @enable: 0|1
*
* When @pa used as a template for fdisk_add_partition() when force label
* driver to use all the possible space for the new partition.
*
* Returns: 0 on success, <0 on error.
*/
int fdisk_partition_end_follow_default(struct fdisk_partition *pa, int enable)
{
if (!pa)
return -EINVAL;
pa->end_follow_default = enable ? 1 : 0;
return 0;
}
/**
* fdisk_partition_end_is_default:
* @pa: partition
*
* Returns: 1 if the partition follows default
*/
int fdisk_partition_end_is_default(struct fdisk_partition *pa)
{
assert(pa);
return pa->end_follow_default;
}
/**
* fdisk_partition_get_uuid:
* @pa: partition
*
* Returns: partition UUID as string
*/
const char *fdisk_partition_get_uuid(struct fdisk_partition *pa)
{
return pa ? pa->uuid : NULL;
}
/**
* fdisk_partition_get_attrs:
* @pa: partition
*
* Returns: partition attributes in string format
*/
const char *fdisk_partition_get_attrs(struct fdisk_partition *pa)
{
return pa ? pa->attrs : NULL;
}
/**
* fdisk_partition_set_attrs:
* @pa: partition
* @attrs: attributes
*
* Sets @attrs to @pa.
*
* Return: 0 on success, <0 on error.
*/
int fdisk_partition_set_attrs(struct fdisk_partition *pa, const char *attrs)
{
if (!pa)
return -EINVAL;
return strdup_to_struct_member(pa, attrs, attrs);
}
/**
* fdisk_partition_is_nested:
* @pa: partition
*
* Returns: 1 if the partition is nested (e.g. MBR logical partition)
*/
int fdisk_partition_is_nested(struct fdisk_partition *pa)
{
return pa && !FDISK_IS_UNDEF(pa->parent_partno);
}
/**
* fdisk_partition_is_container:
* @pa: partition
*
* Returns: 1 if the partition is container (e.g. MBR extended partition)
*/
int fdisk_partition_is_container(struct fdisk_partition *pa)
{
return pa && pa->container;
}
/**
* fdisk_partition_get_parent:
* @pa: partition
* @parent: parent parno
*
* Returns: returns devno of the parent
*/
int fdisk_partition_get_parent(struct fdisk_partition *pa, size_t *parent)
{
if (pa && parent)
*parent = pa->parent_partno;
else
return -EINVAL;
return 0;
}
/**
* fdisk_partition_is_used:
* @pa: partition
*
* Returns: 1 if the partition points to some area
*/
int fdisk_partition_is_used(struct fdisk_partition *pa)
{
return pa && pa->used;
}
/**
* fdisk_partition_is_bootable:
* @pa: partition
*
* Returns: 1 if the partition has enabled boot flag
*/
int fdisk_partition_is_bootable(struct fdisk_partition *pa)
{
return pa && pa->boot == 1;
}
/**
* fdisk_partition_is_freespace:
* @pa: partition
*
* Returns: 1 if @pa points to freespace
*/
int fdisk_partition_is_freespace(struct fdisk_partition *pa)
{
return pa && pa->freespace;
}
/**
* fdisk_partition_is_wholedisk:
* @pa: partition
*
* Returns: 1 if the partition is special whole-disk (e.g. SUN) partition
*/
int fdisk_partition_is_wholedisk(struct fdisk_partition *pa)
{
return pa && pa->wholedisk;
}
/**
* fdisk_partition_next_partno:
* @pa: partition
* @cxt: context
* @n: returns partition number
*
* If @pa specified and partno-follow-default (see fdisk_partition_partno_follow_default())
* enabled then returns next expected partno or -ERANGE on error.
*
* If @pa is NULL, or @pa does not specify any semantic for the next partno
* then use Ask API to ask user for the next partno. In this case returns 1 if
* no free partition available. If fdisk dialogs are disabled then returns -EINVAL.
*
* Returns: 0 on success, <0 on error, or 1 for non-free partno by Ask API.
*/
int fdisk_partition_next_partno(
struct fdisk_partition *pa,
struct fdisk_context *cxt,
size_t *n)
{
if (!cxt || !n)
return -EINVAL;
if (pa && pa->partno_follow_default) {
size_t i;
DBG(PART, ul_debugobj(pa, "next partno (follow default)"));
for (i = 0; i < cxt->label->nparts_max; i++) {
if (!fdisk_is_partition_used(cxt, i)) {
*n = i;
return 0;
}
}
return -ERANGE;
}
if (pa && fdisk_partition_has_partno(pa)) {
DBG(PART, ul_debugobj(pa, "next partno (specified=%zu)", pa->partno));
if (pa->partno >= cxt->label->nparts_max ||
fdisk_is_partition_used(cxt, pa->partno))
return -ERANGE;
*n = pa->partno;
return 0;
}
if (fdisk_has_dialogs(cxt))
return fdisk_ask_partnum(cxt, n, 1);
return -EINVAL;
}
static int probe_partition_content(struct fdisk_context *cxt, struct fdisk_partition *pa)
{
int rc = 1; /* nothing */
DBG(PART, ul_debugobj(pa, "start probe #%zu partition [cxt %p] >>>", pa->partno, cxt));
/* zeroize the current setting */
strdup_to_struct_member(pa, fstype, NULL);
strdup_to_struct_member(pa, fsuuid, NULL);
strdup_to_struct_member(pa, fslabel, NULL);
if (!fdisk_partition_has_start(pa) ||
!fdisk_partition_has_size(pa))
goto done;
#ifdef HAVE_LIBBLKID
else {
uintmax_t start, size;
blkid_probe pr = blkid_new_probe();
if (!pr)
goto done;
DBG(PART, ul_debugobj(pa, "blkid prober: %p", pr));
blkid_probe_enable_superblocks(pr, 1);
blkid_probe_set_superblocks_flags(pr,
BLKID_SUBLKS_MAGIC |
BLKID_SUBLKS_TYPE |
BLKID_SUBLKS_LABEL |
BLKID_SUBLKS_UUID |
BLKID_SUBLKS_BADCSUM);
start = fdisk_partition_get_start(pa) * fdisk_get_sector_size(cxt);
size = fdisk_partition_get_size(pa) * fdisk_get_sector_size(cxt);
if (blkid_probe_set_device(pr, cxt->dev_fd, start, size) == 0
&& blkid_do_fullprobe(pr) == 0) {
const char *data;
rc = 0;
if (!blkid_probe_lookup_value(pr, "TYPE", &data, NULL))
rc = strdup_to_struct_member(pa, fstype, data);
if (!rc && !blkid_probe_lookup_value(pr, "LABEL", &data, NULL))
rc = strdup_to_struct_member(pa, fslabel, data);
if (!rc && !blkid_probe_lookup_value(pr, "UUID", &data, NULL))
rc = strdup_to_struct_member(pa, fsuuid, data);
}
blkid_free_probe(pr);
pa->fs_probed = 1;
}
#endif /* HAVE_LIBBLKID */
done:
DBG(PART, ul_debugobj(pa, "<<< end probe #%zu partition[cxt %p, rc=%d]", pa->partno, cxt, rc));
return rc;
}
/**
* fdisk_partition_to_string:
* @pa: partition
* @cxt: context
* @id: field (FDISK_FIELD_*)
* @data: returns string with allocated data
*
* Returns info about partition converted to printable string.
*
* For example
* <informalexample>
* <programlisting>
* struct fdisk_partition *pa;
*
* fdisk_get_partition(cxt, 0, &pa);
* fdisk_partition_to_string(pa, FDISK_FIELD_UUID, &data);
* printf("first partition uuid: %s\n", data);
* free(data);
* fdisk_unref_partition(pa);
* </programlisting>
* </informalexample>
*
* returns UUID for the first partition.
*
* Returns: 0 on success, otherwise, a corresponding error.
*/
int fdisk_partition_to_string(struct fdisk_partition *pa,
struct fdisk_context *cxt,
int id,
char **data)
{
char *p = NULL;
int rc = 0;
uint64_t x;
if (!pa || !cxt || !data)
return -EINVAL;
switch (id) {
case FDISK_FIELD_DEVICE:
if (pa->freespace)
p = strdup(_("Free space"));
else if (fdisk_partition_has_partno(pa) && cxt->dev_path) {
if (cxt->label->flags & FDISK_LABEL_FL_INCHARS_PARTNO)
rc = asprintf(&p, "%c", (int) pa->partno + 'a');
else
p = fdisk_partname(cxt->dev_path, pa->partno + 1);
}
break;
case FDISK_FIELD_BOOT:
p = fdisk_partition_is_bootable(pa) ? strdup("*") : NULL;
break;
case FDISK_FIELD_START:
if (fdisk_partition_has_start(pa)) {
x = fdisk_cround(cxt, pa->start);
rc = pa->start_post ?
asprintf(&p, "%"PRIu64"%c", x, pa->start_post) :
asprintf(&p, "%"PRIu64, x);
}
break;
case FDISK_FIELD_END:
if (fdisk_partition_has_end(pa)) {
x = fdisk_cround(cxt, fdisk_partition_get_end(pa));
rc = pa->end_post ?
asprintf(&p, "%"PRIu64"%c", x, pa->end_post) :
asprintf(&p, "%"PRIu64, x);
}
break;
case FDISK_FIELD_SIZE:
if (fdisk_partition_has_size(pa)) {
uint64_t sz = pa->size * cxt->sector_size;
switch (cxt->sizeunit) {
case FDISK_SIZEUNIT_BYTES:
rc = asprintf(&p, "%"PRIu64"", sz);
break;
case FDISK_SIZEUNIT_HUMAN:
if (fdisk_is_details(cxt))
rc = pa->size_post ?
asprintf(&p, "%"PRIu64"%c", sz, pa->size_post) :
asprintf(&p, "%"PRIu64, sz);
else {
p = size_to_human_string(SIZE_SUFFIX_1LETTER, sz);
if (!p)
rc = -ENOMEM;
}
break;
}
}
break;
case FDISK_FIELD_CYLINDERS:
{
uintmax_t sz = fdisk_partition_has_size(pa) ? pa->size : 0;
if (sz)
/* Why we need to cast that to uintmax_t? */
rc = asprintf(&p, "%ju", (uintmax_t)(sz / (cxt->geom.heads * cxt->geom.sectors)) + 1);
break;
}
case FDISK_FIELD_SECTORS:
rc = asprintf(&p, "%ju",
fdisk_partition_has_size(pa) ? (uintmax_t) pa->size : 0);
break;
case FDISK_FIELD_BSIZE:
rc = asprintf(&p, "%"PRIu64, pa->bsize);
break;
case FDISK_FIELD_FSIZE:
rc = asprintf(&p, "%"PRIu64, pa->fsize);
break;
case FDISK_FIELD_CPG:
rc = asprintf(&p, "%"PRIu64, pa->cpg);
break;
case FDISK_FIELD_TYPE:
p = pa->type && pa->type->name ? strdup(_(pa->type->name)) : NULL;
break;
case FDISK_FIELD_TYPEID:
if (pa->type && fdisk_parttype_get_string(pa->type))
rc = asprintf(&p, "%s", fdisk_parttype_get_string(pa->type));
else if (pa->type)
rc = asprintf(&p, "%x", fdisk_parttype_get_code(pa->type));
break;
case FDISK_FIELD_UUID:
p = pa->uuid && *pa->uuid? strdup(pa->uuid) : NULL;
break;
case FDISK_FIELD_NAME:
p = pa->name && *pa->name ? strdup(pa->name) : NULL;
break;
case FDISK_FIELD_ATTR:
p = pa->attrs && *pa->attrs ? strdup(pa->attrs) : NULL;
break;
case FDISK_FIELD_SADDR:
p = pa->start_chs && *pa->start_chs ? strdup(pa->start_chs) : NULL;
break;
case FDISK_FIELD_EADDR:
p = pa->end_chs && *pa->end_chs? strdup(pa->end_chs) : NULL;
break;
case FDISK_FIELD_FSUUID:
if (pa->fs_probed || probe_partition_content(cxt, pa) == 0)
p = pa->fsuuid && *pa->fsuuid ? strdup(pa->fsuuid) : NULL;
break;
case FDISK_FIELD_FSLABEL:
if (pa->fs_probed || probe_partition_content(cxt, pa) == 0)
p = pa->fslabel && *pa->fslabel ? strdup(pa->fslabel) : NULL;
break;
case FDISK_FIELD_FSTYPE:
if (pa->fs_probed || probe_partition_content(cxt, pa) == 0)
p = pa->fstype && *pa->fstype ? strdup(pa->fstype) : NULL;
break;
default:
return -EINVAL;
}
if (rc < 0) {
rc = -ENOMEM;
free(p);
p = NULL;
} else if (rc > 0)
rc = 0;
*data = p;
return rc;
}
/**
* fdisk_get_partition:
* @cxt: context
* @partno: partition number (0 is the first partition)
* @pa: returns data about partition
*
* Reads disklabel and fills in @pa with data about partition @n.
*
* Note that partno may address unused partition and then this function does
* not fill anything to @pa. See fdisk_is_partition_used(). If @pa points to
* NULL then the function allocates a newly allocated fdisk_partition struct,
* use fdisk_unref_partition() to deallocate.
*
* Returns: 0 on success, otherwise, a corresponding error.
*/
int fdisk_get_partition(struct fdisk_context *cxt, size_t partno,
struct fdisk_partition **pa)
{
int rc;
struct fdisk_partition *np = NULL;
if (!cxt || !cxt->label || !pa)
return -EINVAL;
if (!cxt->label->op->get_part)
return -ENOSYS;
if (!fdisk_is_partition_used(cxt, partno))
return -EINVAL;
if (!*pa) {
np = *pa = fdisk_new_partition();
if (!*pa)
return -ENOMEM;
} else
fdisk_reset_partition(*pa);
(*pa)->partno = partno;
rc = cxt->label->op->get_part(cxt, partno, *pa);
if (rc) {
if (np) {
fdisk_unref_partition(np);
*pa = NULL;
} else
fdisk_reset_partition(*pa);
} else
(*pa)->size_explicit = 1;
return rc;
}
static struct fdisk_partition *area_by_offset(
struct fdisk_table *tb,
struct fdisk_partition *cur,
fdisk_sector_t off)
{
struct fdisk_partition *pa = NULL;
struct fdisk_iter itr;
fdisk_reset_iter(&itr, FDISK_ITER_FORWARD);
while (fdisk_table_next_partition(tb, &itr, &pa) == 0) {
if (!fdisk_partition_has_start(pa) || !fdisk_partition_has_size(pa))
continue;
if (fdisk_partition_is_nested(cur) &&
pa->parent_partno != cur->parent_partno)
continue;
if (off >= pa->start && off < pa->start + pa->size)
return pa;
}
return NULL;
}
static int resize_get_first_possible(
struct fdisk_table *tb,
struct fdisk_partition *cur,
fdisk_sector_t *start)
{
struct fdisk_partition *pa = NULL, *first = NULL;
struct fdisk_iter itr;
fdisk_reset_iter(&itr, FDISK_ITER_FORWARD);
*start = 0;
DBG(TAB, ul_debugobj(tb, "checking first possible before start=%ju", (uintmax_t) cur->start));
while (fdisk_table_next_partition(tb, &itr, &pa) == 0) {
if (pa->start > cur->start || pa == cur)
break;
DBG(TAB, ul_debugobj(tb, " checking entry %p [partno=%zu start=%ju, end=%ju, size=%ju%s%s%s]",
pa,
fdisk_partition_get_partno(pa),
(uintmax_t) fdisk_partition_get_start(pa),
(uintmax_t) fdisk_partition_get_end(pa),
(uintmax_t) fdisk_partition_get_size(pa),
fdisk_partition_is_freespace(pa) ? " freespace" : "",
fdisk_partition_is_nested(pa) ? " nested" : "",
fdisk_partition_is_container(pa) ? " container" : ""));
if (!fdisk_partition_is_freespace(pa)) {
DBG(TAB, ul_debugobj(tb, " ignored (no freespace)"));
first = NULL;
continue;
}
if (!fdisk_partition_has_start(pa) || !fdisk_partition_has_size(pa)) {
DBG(TAB, ul_debugobj(tb, " ignored (no start/size)"));
first = NULL;
continue;
}
/* The current is nested, free space has to be nested within the same parent */
if (fdisk_partition_is_nested(cur)
&& pa->parent_partno != cur->parent_partno) {
DBG(TAB, ul_debugobj(tb, " ignore (nested required)"));
first = NULL;
continue;
}
if (pa->start + pa->size <= cur->start) {
first = pa;
DBG(TAB, ul_debugobj(tb, " entry usable"));
}
}
if (first)
*start = first->start;
else
DBG(PART, ul_debugobj(cur, "resize: nothing usable before %ju", (uintmax_t) cur->start));
return first ? 0 : -1;
}
/*
* Verify that area addressed by @start is freespace or the @cur[rent]
* partition and continue to the next table entries until it's freespace, and
* counts size of all this space.
*
* This is core of the partition start offset move operation. We can move the
* start within the current partition of to the another free space. It's
* forbidden to move start of the partition to another already defined
* partition.
*/
static int resize_get_last_possible(
struct fdisk_table *tb,
struct fdisk_partition *cur,
fdisk_sector_t start,
fdisk_sector_t *maxsz)
{
struct fdisk_partition *pa = NULL, *last = NULL;
struct fdisk_iter itr;
fdisk_reset_iter(&itr, FDISK_ITER_FORWARD);
*maxsz = 0;
DBG(TAB, ul_debugobj(tb, "checking last possible for start=%ju", (uintmax_t) start));
while (fdisk_table_next_partition(tb, &itr, &pa) == 0) {
DBG(TAB, ul_debugobj(tb, " checking entry %p [partno=%zu start=%ju, end=%ju, size=%ju%s%s%s]",
pa,
fdisk_partition_get_partno(pa),
(uintmax_t) fdisk_partition_get_start(pa),
(uintmax_t) fdisk_partition_get_end(pa),
(uintmax_t) fdisk_partition_get_size(pa),
fdisk_partition_is_freespace(pa) ? " freespace" : "",
fdisk_partition_is_nested(pa) ? " nested" : "",
fdisk_partition_is_container(pa) ? " container" : ""));
if (!fdisk_partition_has_start(pa) ||
!fdisk_partition_has_size(pa) ||
(fdisk_partition_is_container(pa) && pa != cur)) {
DBG(TAB, ul_debugobj(tb, " ignored (no start/size or container)"));
continue;
}
if (fdisk_partition_is_nested(pa)
&& fdisk_partition_is_container(cur)
&& pa->parent_partno == cur->partno) {
DBG(TAB, ul_debugobj(tb, " ignore (nested child of the current partition)"));
continue;
}
/* The current is nested, free space has to be nested within the same parent */
if (fdisk_partition_is_nested(cur)
&& pa->parent_partno != cur->parent_partno) {
DBG(TAB, ul_debugobj(tb, " ignore (nested required)"));
continue;
}
if (!last) {
if (start >= pa->start && start < pa->start + pa->size) {
if (fdisk_partition_is_freespace(pa) || pa == cur) {
DBG(TAB, ul_debugobj(tb, " accepted as last"));
last = pa;
} else {
DBG(TAB, ul_debugobj(tb, " failed to set last"));
break;
}
*maxsz = pa->size - (start - pa->start);
DBG(TAB, ul_debugobj(tb, " new max=%ju", (uintmax_t) *maxsz));
}
} else if (!fdisk_partition_is_freespace(pa) && pa != cur) {
DBG(TAB, ul_debugobj(tb, " no free space behind current"));
break;
} else {
last = pa;
*maxsz = pa->size - (start - pa->start);
DBG(TAB, ul_debugobj(tb, " new max=%ju (last updated)", (uintmax_t) *maxsz));
}
}
if (last)
DBG(PART, ul_debugobj(cur, "resize: max size=%ju", (uintmax_t) *maxsz));
else
DBG(PART, ul_debugobj(cur, "resize: nothing usable after %ju", (uintmax_t) start));
return last ? 0 : -1;
}
/*
* Uses template @tpl to recount start and size change of the partition @res. The
* @tpl->size and @tpl->start are interpreted as relative to the current setting.
*/
static int recount_resize(
struct fdisk_context *cxt, size_t partno,
struct fdisk_partition *res, struct fdisk_partition *tpl)
{
fdisk_sector_t start, size, xsize;
struct fdisk_partition *cur = NULL;
struct fdisk_table *tb = NULL;
int rc;
DBG(PART, ul_debugobj(tpl, ">>> resize requested"));
FDISK_INIT_UNDEF(start);
FDISK_INIT_UNDEF(size);
rc = fdisk_get_partitions(cxt, &tb);
if (!rc) {
/* For resize we do not follow grain to detect free-space, but
* we support to resize with very small granulation. */
unsigned long org = cxt->grain;
cxt->grain = cxt->sector_size;
rc = fdisk_get_freespaces(cxt, &tb);
cxt->grain = org;
}
if (rc) {
fdisk_unref_table(tb);
return rc;
}
fdisk_table_sort_partitions(tb, fdisk_partition_cmp_start);
DBG(PART, ul_debugobj(tpl, "resize partition partno=%zu in table:", partno));
ON_DBG(PART, fdisk_debug_print_table(tb));
cur = fdisk_table_get_partition_by_partno(tb, partno);
if (!cur) {
fdisk_unref_table(tb);
return -EINVAL;
}
/* 1a) set new start - change relative to the current on-disk setting */
if (tpl->movestart && fdisk_partition_has_start(tpl)) {
start = fdisk_partition_get_start(cur);
if (tpl->movestart == FDISK_MOVE_DOWN) {
if (fdisk_partition_get_start(tpl) > start)
goto erange;
start -= fdisk_partition_get_start(tpl);
} else
start += fdisk_partition_get_start(tpl);
DBG(PART, ul_debugobj(tpl, "resize: moving start %s relative, new start: %ju",
tpl->movestart == FDISK_MOVE_DOWN ? "DOWN" : "UP", (uintmax_t)start));
/* 1b) set new start - try freespace before the curret partition */
} else if (tpl->movestart == FDISK_MOVE_DOWN) {
if (resize_get_first_possible(tb, cur, &start) != 0)
goto erange;
DBG(PART, ul_debugobj(tpl, "resize: moving start DOWN (first possible), new start: %ju",
(uintmax_t)start));
/* 1c) set new start - absolute number */
} else if (fdisk_partition_has_start(tpl)) {
start = fdisk_partition_get_start(tpl);
DBG(PART, ul_debugobj(tpl, "resize: moving start to absolute offset: %ju",
(uintmax_t)start));
}
/* 2) verify that start is within the current partition or any freespace area */
if (!FDISK_IS_UNDEF(start)) {
struct fdisk_partition *area = area_by_offset(tb, cur, start);
if (area == cur)
DBG(PART, ul_debugobj(tpl, "resize: start points to the current partition"));
else if (area && fdisk_partition_is_freespace(area))
DBG(PART, ul_debugobj(tpl, "resize: start points to freespace"));
else if (!area && start >= cxt->first_lba && start < cxt->first_lba + (cxt->grain / cxt->sector_size))
DBG(PART, ul_debugobj(tpl, "resize: start points before first partition"));
else {
DBG(PART, ul_debugobj(tpl, "resize: start verification failed"));
goto erange;
}
} else {
/* no change, start points to the current partition */
DBG(PART, ul_debugobj(tpl, "resize: start unchanged"));
start = fdisk_partition_get_start(cur);
}
/* 3a) set new size -- reduce */
if (tpl->resize == FDISK_RESIZE_REDUCE && fdisk_partition_has_size(tpl)) {
DBG(PART, ul_debugobj(tpl, "resize: reduce"));
size = fdisk_partition_get_size(cur);
if (fdisk_partition_get_size(tpl) > size)
goto erange;
size -= fdisk_partition_get_size(tpl);
/* 3b) set new size -- enlarge */
} else if (tpl->resize == FDISK_RESIZE_ENLARGE && fdisk_partition_has_size(tpl)) {
DBG(PART, ul_debugobj(tpl, "resize: enlarge"));
size = fdisk_partition_get_size(cur);
size += fdisk_partition_get_size(tpl);
/* 3c) set new size -- no size specified, enlarge to all freespace */
} else if (tpl->resize == FDISK_RESIZE_ENLARGE) {
DBG(PART, ul_debugobj(tpl, "resize: enlarge to all possible"));
if (resize_get_last_possible(tb, cur, start, &size))
goto erange;
/* 3d) set new size -- absolute number */
} else if (fdisk_partition_has_size(tpl)) {
DBG(PART, ul_debugobj(tpl, "resize: new absolute size"));
size = fdisk_partition_get_size(tpl);
}
/* 4) verify that size is within the current partition or next free space */
xsize = !FDISK_IS_UNDEF(size) ? size : fdisk_partition_get_size(cur);
if (fdisk_partition_has_size(cur)) {
fdisk_sector_t maxsz;
if (resize_get_last_possible(tb, cur, start, &maxsz))
goto erange;
DBG(PART, ul_debugobj(tpl, "resize: size=%ju, max=%ju",
(uintmax_t) xsize, (uintmax_t) maxsz));
if (xsize > maxsz)
goto erange;
}
if (FDISK_IS_UNDEF(size)) {
DBG(PART, ul_debugobj(tpl, "resize: size unchanged (undefined)"));
}
DBG(PART, ul_debugobj(tpl, "<<< resize: SUCCESS: start %ju->%ju; size %ju->%ju",
(uintmax_t) fdisk_partition_get_start(cur), (uintmax_t) start,
(uintmax_t) fdisk_partition_get_size(cur), (uintmax_t) size));
res->start = start;
res->size = size;
fdisk_unref_table(tb);
return 0;
erange:
DBG(PART, ul_debugobj(tpl, "<<< resize: FAILED"));
fdisk_warnx(cxt, _("Failed to resize partition #%zu."), partno + 1);
fdisk_unref_table(tb);
return -ERANGE;
}
/**
* fdisk_set_partition:
* @cxt: context
* @partno: partition number (0 is the first partition)
* @pa: new partition setting
*
* Modifies disklabel according to setting with in @pa.
*
* Returns: 0 on success, <0 on error.
*/
int fdisk_set_partition(struct fdisk_context *cxt, size_t partno,
struct fdisk_partition *pa)
{
struct fdisk_partition *xpa = pa, *tmp = NULL;
int rc, wipe = 0;
if (!cxt || !cxt->label || !pa)
return -EINVAL;
if (!cxt->label->op->set_part)
return -ENOSYS;
pa->fs_probed = 0;
if (!fdisk_is_partition_used(cxt, partno)) {
pa->partno = partno;
return fdisk_add_partition(cxt, pa, NULL);
}
if (pa->resize || pa->movestart
|| fdisk_partition_has_start(pa) || fdisk_partition_has_size(pa)) {
xpa = __copy_partition(pa);
if (!xpa) {
rc = -ENOMEM;
goto done;
}
xpa->movestart = 0;
xpa->resize = 0;
FDISK_INIT_UNDEF(xpa->size);
FDISK_INIT_UNDEF(xpa->start);
rc = recount_resize(cxt, partno, xpa, pa);
if (rc)
goto done;
}
DBG(CXT, ul_debugobj(cxt, "setting partition %zu %p (start=%ju, end=%ju, size=%ju)",
partno, xpa,
(uintmax_t) fdisk_partition_get_start(xpa),
(uintmax_t) fdisk_partition_get_end(xpa),
(uintmax_t) fdisk_partition_get_size(xpa)));
/* disable wipe for old offset/size setting */
if (fdisk_get_partition(cxt, partno, &tmp) == 0 && tmp) {
wipe = fdisk_set_wipe_area(cxt, fdisk_partition_get_start(tmp),
fdisk_partition_get_size(tmp), FALSE);
fdisk_unref_partition(tmp);
}
/* call label driver */
rc = cxt->label->op->set_part(cxt, partno, xpa);
/* enable wipe for new offset/size */
if (!rc && wipe)
fdisk_wipe_partition(cxt, partno, TRUE);
done:
DBG(CXT, ul_debugobj(cxt, "set_partition() rc=%d", rc));
if (xpa != pa)
fdisk_unref_partition(xpa);
return rc;
}
/**
* fdisk_wipe_partition:
* @cxt: fdisk context
* @partno: partition number
* @enable: 0 or 1
*
* Enable/disable filesystems/RAIDs wiping in area defined by partition start and size.
*
* Returns: <0 in case of error, 0 on success
* Since: 2.29
*/
int fdisk_wipe_partition(struct fdisk_context *cxt, size_t partno, int enable)
{
struct fdisk_partition *pa = NULL;
int rc;
rc = fdisk_get_partition(cxt, partno, &pa);
if (rc)
return rc;
rc = fdisk_set_wipe_area(cxt, fdisk_partition_get_start(pa),
fdisk_partition_get_size(pa), enable);
fdisk_unref_partition(pa);
return rc < 0 ? rc : 0;
}
/**
* fdisk_partition_has_wipe:
* @cxt: fdisk context
* @pa: partition
*
* Since: 2.30
*
* Returns: 1 if the area specified by @pa will be wiped by write command, or 0.
*/
int fdisk_partition_has_wipe(struct fdisk_context *cxt, struct fdisk_partition *pa)
{
return fdisk_has_wipe_area(cxt, fdisk_partition_get_start(pa),
fdisk_partition_get_size(pa));
}
/**
* fdisk_add_partition:
* @cxt: fdisk context
* @pa: template for the partition (or NULL)
* @partno: NULL or returns new partition number
*
* If @pa is not specified or any @pa item is missing the libfdisk will ask by
* fdisk_ask_ API.
*
* The @pa template is important for non-interactive partitioning,
* especially for MBR where is necessary to differentiate between
* primary/logical; this is done by start offset or/and partno.
* The rules for MBR:
*
* A) template specifies start within extended partition: add logical
* B) template specifies start out of extended partition: add primary
* C) template specifies start (or default), partno < 4: add primary
* D) template specifies default start, partno >= 4: add logical
*
* otherwise MBR driver uses Ask-API to get missing information.
*
* Adds a new partition to disklabel.
*
* Returns: 0 on success, <0 on error.
*/
int fdisk_add_partition(struct fdisk_context *cxt,
struct fdisk_partition *pa,
size_t *partno)
{
int rc;
if (!cxt || !cxt->label)
return -EINVAL;
if (!cxt->label->op->add_part)
return -ENOSYS;
if (fdisk_missing_geometry(cxt))
return -EINVAL;
if (pa) {
pa->fs_probed = 0;
DBG(CXT, ul_debugobj(cxt, "adding new partition %p", pa));
if (fdisk_partition_has_start(pa))
DBG(CXT, ul_debugobj(cxt, " start: %ju", (uintmax_t) fdisk_partition_get_start(pa)));
if (fdisk_partition_has_end(pa))
DBG(CXT, ul_debugobj(cxt, " end: %ju", (uintmax_t) fdisk_partition_get_end(pa)));
if (fdisk_partition_has_size(pa))
DBG(CXT, ul_debugobj(cxt, " size: %ju", (uintmax_t) fdisk_partition_get_size(pa)));
DBG(CXT, ul_debugobj(cxt, " defaults: start=%s, end=%s, partno=%s",
pa->start_follow_default ? "yes" : "no",
pa->end_follow_default ? "yes" : "no",
pa->partno_follow_default ? "yes" : "no"));
} else
DBG(CXT, ul_debugobj(cxt, "adding partition"));
rc = cxt->label->op->add_part(cxt, pa, partno);
DBG(CXT, ul_debugobj(cxt, "add partition done (rc=%d)", rc));
return rc;
}
/**
* fdisk_delete_partition:
* @cxt: fdisk context
* @partno: partition number to delete (0 is the first partition)
*
* Deletes a @partno partition from disklabel.
*
* Returns: 0 on success, <0 on error
*/
int fdisk_delete_partition(struct fdisk_context *cxt, size_t partno)
{
if (!cxt || !cxt->label)
return -EINVAL;
if (!cxt->label->op->del_part)
return -ENOSYS;
fdisk_wipe_partition(cxt, partno, 0);
DBG(CXT, ul_debugobj(cxt, "deleting %s partition number %zd",
cxt->label->name, partno));
return cxt->label->op->del_part(cxt, partno);
}
/**
* fdisk_delete_all_partitions:
* @cxt: fdisk context
*
* Delete all used partitions from disklabel.
*
* Returns: 0 on success, otherwise, a corresponding error.
*/
int fdisk_delete_all_partitions(struct fdisk_context *cxt)
{
size_t i;
int rc = 0;
if (!cxt || !cxt->label)
return -EINVAL;
for (i = 0; i < cxt->label->nparts_max; i++) {
if (!fdisk_is_partition_used(cxt, i))
continue;
rc = fdisk_delete_partition(cxt, i);
if (rc)
break;
}
return rc;
}
/**
* fdisk_is_partition_used:
* @cxt: context
* @n: partition number (0 is the first partition)
*
* Check if the partition number @n is used by partition table. This function
* does not check if the device is used (e.g. mounted) by system!
*
* This is faster than fdisk_get_partition() + fdisk_partition_is_used().
*
* Returns: 0 or 1
*/
int fdisk_is_partition_used(struct fdisk_context *cxt, size_t n)
{
if (!cxt || !cxt->label)
return -EINVAL;
if (!cxt->label->op->part_is_used)
return -ENOSYS;
return cxt->label->op->part_is_used(cxt, n);
}
/**
* fdisk_partition_max_size:
* @cxt: context
* @n: partition number (0 is the first partition)
* @maxsz: returns maximum size of partition
*
* Find maximum size the partition can be resized to.
* Takes into account free space between this partition and the next one.
*
* Returns: 0 on success, <0 on error.
*/
int fdisk_partition_get_max_size(struct fdisk_context *cxt, size_t n,
fdisk_sector_t *maxsz)
{
struct fdisk_partition *cur = NULL;
struct fdisk_table *tb = NULL;
int rc;
rc = fdisk_get_partitions(cxt, &tb);
if (rc)
goto out;
rc = fdisk_get_freespaces(cxt, &tb);
if (rc)
goto out;
rc = fdisk_table_sort_partitions(tb, fdisk_partition_cmp_start);
if (rc)
goto out;
cur = fdisk_table_get_partition_by_partno(tb, n);
if (!cur)
goto einval;
if (!fdisk_partition_has_start(cur))
goto einval;
if (resize_get_last_possible(tb, cur,
fdisk_partition_get_start(cur), maxsz))
goto einval;
out:
fdisk_unref_partition(cur);
fdisk_unref_table(tb);
return rc;
einval:
rc = -EINVAL;
goto out;
}
|