]> git.ipfire.org Git - thirdparty/util-linux.git/blob - libfdisk/src/partition.c
60feb74a621323d129c410c14bdaf1b1e13ba398
[thirdparty/util-linux.git] / libfdisk / src / partition.c
1
2 #include "c.h"
3 #include "strutils.h"
4
5 #ifdef HAVE_LIBBLKID
6 # include <blkid.h>
7 #endif
8
9 #include "fdiskP.h"
10 #include "strutils.h"
11
12 /**
13 * SECTION: partition
14 * @title: Partition
15 * @short_description: generic label independent partition abstraction
16 *
17 * The fdisk_partition provides label independent abstraction. The partitions
18 * are not directly connected with partition table (label) data. Any change to
19 * fdisk_partition does not affects in-memory or on-disk label data.
20 *
21 * The fdisk_partition is possible to use as a template for
22 * fdisk_add_partition() or fdisk_set_partition() operations.
23 */
24
25 static void init_partition(struct fdisk_partition *pa)
26 {
27 FDISK_INIT_UNDEF(pa->size);
28 FDISK_INIT_UNDEF(pa->start);
29 FDISK_INIT_UNDEF(pa->partno);
30 FDISK_INIT_UNDEF(pa->parent_partno);
31 FDISK_INIT_UNDEF(pa->boot);
32
33 INIT_LIST_HEAD(&pa->parts);
34 }
35
36 /**
37 * fdisk_new_partition:
38 *
39 * Returns: new instance.
40 */
41 struct fdisk_partition *fdisk_new_partition(void)
42 {
43 struct fdisk_partition *pa = calloc(1, sizeof(*pa));
44
45 pa->refcount = 1;
46 init_partition(pa);
47 DBG(PART, ul_debugobj(pa, "alloc"));
48 return pa;
49 }
50
51 /**
52 * fdisk_reset_partition:
53 * @pa: partition
54 *
55 * Resets partition content.
56 */
57 void fdisk_reset_partition(struct fdisk_partition *pa)
58 {
59 int ref;
60
61 if (!pa)
62 return;
63
64 DBG(PART, ul_debugobj(pa, "reset"));
65 ref = pa->refcount;
66
67 fdisk_unref_parttype(pa->type);
68 free(pa->name);
69 free(pa->uuid);
70 free(pa->attrs);
71 free(pa->fstype);
72 free(pa->fsuuid);
73 free(pa->fslabel);
74 free(pa->start_chs);
75 free(pa->end_chs);
76
77 memset(pa, 0, sizeof(*pa));
78 pa->refcount = ref;
79
80 init_partition(pa);
81 }
82
83 static struct fdisk_partition *__copy_partition(struct fdisk_partition *o)
84 {
85 struct fdisk_partition *n = fdisk_new_partition();
86 int rc;
87
88 if (!n)
89 return NULL;
90
91 memcpy(n, o, sizeof(*n));
92
93 /* do not copy reference to lists, etc.*/
94 n->refcount = 1;
95 INIT_LIST_HEAD(&n->parts);
96
97 if (n->type)
98 fdisk_ref_parttype(n->type);
99
100 rc = strdup_between_structs(n, o, name);
101 if (!rc)
102 rc = strdup_between_structs(n, o, uuid);
103 if (!rc)
104 rc = strdup_between_structs(n, o, attrs);
105 if (!rc)
106 rc = strdup_between_structs(n, o, fstype);
107 if (!rc)
108 rc = strdup_between_structs(n, o, fsuuid);
109 if (!rc)
110 rc = strdup_between_structs(n, o, fslabel);
111 if (!rc)
112 rc = strdup_between_structs(n, o, start_chs);
113 if (!rc)
114 rc = strdup_between_structs(n, o, end_chs);
115
116 if (rc) {
117 fdisk_unref_partition(n);
118 n = NULL;
119 }
120 return n;
121 }
122
123 /**
124 * fdisk_ref_partition:
125 * @pa: partition pointer
126 *
127 * Increments reference counter.
128 */
129 void fdisk_ref_partition(struct fdisk_partition *pa)
130 {
131 if (pa)
132 pa->refcount++;
133 }
134
135 /**
136 * fdisk_unref_partition:
137 * @pa: partition pointer
138 *
139 * Decrements reference counter, on zero the @pa is automatically
140 * deallocated.
141 */
142 void fdisk_unref_partition(struct fdisk_partition *pa)
143 {
144 if (!pa)
145 return;
146
147 pa->refcount--;
148 if (pa->refcount <= 0) {
149 list_del(&pa->parts);
150 fdisk_reset_partition(pa);
151 DBG(PART, ul_debugobj(pa, "free"));
152 free(pa);
153 }
154 }
155
156 /**
157 * fdisk_partition_set_start:
158 * @pa: partition
159 * @off: offset in sectors, maximal is UINT64_MAX-1
160 *
161 * Note that zero is valid offset too. Use fdisk_partition_unset_start() to
162 * undefine the offset.
163 *
164 * Returns: 0 on success, <0 on error.
165 */
166 int fdisk_partition_set_start(struct fdisk_partition *pa, fdisk_sector_t off)
167 {
168 if (!pa)
169 return -EINVAL;
170 if (FDISK_IS_UNDEF(off))
171 return -ERANGE;
172 pa->start = off;
173 pa->fs_probed = 0;
174 return 0;
175 }
176
177 /**
178 * fdisk_partition_unset_start:
179 * @pa: partition
180 *
181 * Sets the size as undefined. See fdisk_partition_has_start().
182 *
183 * Returns: 0 on success, <0 on error.
184 */
185 int fdisk_partition_unset_start(struct fdisk_partition *pa)
186 {
187 if (!pa)
188 return -EINVAL;
189 FDISK_INIT_UNDEF(pa->start);
190 pa->fs_probed = 0;
191 return 0;
192 }
193
194 /**
195 * fdisk_partition_get_start:
196 * @pa: partition
197 *
198 * The zero is also valid offset. The function may return random undefined
199 * value when start offset is undefined (for example after
200 * fdisk_partition_unset_start()). Always use fdisk_partition_has_start() to be
201 * sure that you work with valid numbers.
202 *
203 * Returns: start offset in sectors
204 */
205 fdisk_sector_t fdisk_partition_get_start(struct fdisk_partition *pa)
206 {
207 return pa->start;
208 }
209
210 /**
211 * fdisk_partition_has_start:
212 * @pa: partition
213 *
214 * Returns: 1 or 0
215 */
216 int fdisk_partition_has_start(struct fdisk_partition *pa)
217 {
218 return pa && !FDISK_IS_UNDEF(pa->start);
219 }
220
221
222 /**
223 * fdisk_partition_cmp_start:
224 * @a: partition
225 * @b: partition
226 *
227 * Compares partitions according to start offset, See fdisk_table_sort_partitions().
228 *
229 * Return: 0 if the same, <0 if @b greater, >0 if @a greater.
230 */
231 int fdisk_partition_cmp_start(struct fdisk_partition *a,
232 struct fdisk_partition *b)
233 {
234 int no_a = FDISK_IS_UNDEF(a->start),
235 no_b = FDISK_IS_UNDEF(b->start);
236
237 if (no_a && no_b)
238 return 0;
239 if (no_a)
240 return -1;
241 if (no_b)
242 return 1;
243
244 return cmp_numbers(a->start, b->start);
245 }
246
247 /**
248 * fdisk_partition_start_follow_default
249 * @pa: partition
250 * @enable: 0|1
251 *
252 * When @pa used as a template for fdisk_add_partition() when force label driver
253 * to use the first possible space for the new partition.
254 *
255 * Returns: 0 on success, <0 on error.
256 */
257 int fdisk_partition_start_follow_default(struct fdisk_partition *pa, int enable)
258 {
259 if (!pa)
260 return -EINVAL;
261 pa->start_follow_default = enable ? 1 : 0;
262 return 0;
263 }
264
265 /**
266 * fdisk_partition_start_is_default:
267 * @pa: partition
268 *
269 * See fdisk_partition_start_follow_default().
270 *
271 * Returns: 1 if the partition follows default
272 */
273 int fdisk_partition_start_is_default(struct fdisk_partition *pa)
274 {
275 assert(pa);
276 return pa->start_follow_default;
277 }
278
279 /**
280 * fdisk_partition_set_size:
281 * @pa: partition
282 * @sz: size in sectors, maximal is UIN64_MAX-1
283 *
284 * Note that zero is valid size too. Use fdisk_partition_unset_size() to
285 * undefine the size.
286 *
287 * Returns: 0 on success, <0 on error.
288 */
289 int fdisk_partition_set_size(struct fdisk_partition *pa, fdisk_sector_t sz)
290 {
291 if (!pa)
292 return -EINVAL;
293 if (FDISK_IS_UNDEF(sz))
294 return -ERANGE;
295 pa->size = sz;
296 pa->fs_probed = 0;
297 return 0;
298 }
299
300 /**
301 * fdisk_partition_unset_size:
302 * @pa: partition
303 *
304 * Sets the size as undefined. See fdisk_partition_has_size().
305 *
306 * Returns: 0 on success, <0 on error.
307 */
308 int fdisk_partition_unset_size(struct fdisk_partition *pa)
309 {
310 if (!pa)
311 return -EINVAL;
312 FDISK_INIT_UNDEF(pa->size);
313 pa->fs_probed = 0;
314 return 0;
315 }
316
317 /**
318 * fdisk_partition_get_size:
319 * @pa: partition
320 *
321 * The zero is also valid size. The function may return random undefined
322 * value when size is undefined (for example after fdisk_partition_unset_size()).
323 * Always use fdisk_partition_has_size() to be sure that you work with valid
324 * numbers.
325 *
326 * Returns: size offset in sectors
327 */
328 fdisk_sector_t fdisk_partition_get_size(struct fdisk_partition *pa)
329 {
330 return pa->size;
331 }
332
333 /**
334 * fdisk_partition_has_size:
335 * @pa: partition
336 *
337 * Returns: 1 or 0
338 */
339 int fdisk_partition_has_size(struct fdisk_partition *pa)
340 {
341 return pa && !FDISK_IS_UNDEF(pa->size);
342 }
343
344 /**
345 * fdisk_partition_size_explicit:
346 * @pa: partition
347 * @enable: 0|1
348 *
349 * By default libfdisk aligns the size when add the new partition (by
350 * fdisk_add_partition()). If you want to disable this functionality use
351 * @enable = 1.
352 *
353 * Returns: 0 on success, <0 on error.
354 */
355 int fdisk_partition_size_explicit(struct fdisk_partition *pa, int enable)
356 {
357 if (!pa)
358 return -EINVAL;
359 pa->size_explicit = enable ? 1 : 0;
360 return 0;
361 }
362
363 /**
364 * fdisk_partition_set_partno:
365 * @pa: partition
366 * @num: partition number (0 is the first partition, maximal is SIZE_MAX-1)
367 *
368 * Note that zero is valid partno too. Use fdisk_partition_unset_partno() to
369 * undefine the partno.
370 *
371 * Returns: 0 on success, <0 on error.
372 */
373 int fdisk_partition_set_partno(struct fdisk_partition *pa, size_t num)
374 {
375 if (!pa)
376 return -EINVAL;
377 if (FDISK_IS_UNDEF(num))
378 return -ERANGE;
379 pa->partno = num;
380 return 0;
381 }
382
383 /**
384 * fdisk_partition_unset_partno:
385 * @pa: partition
386 *
387 * Sets the partno as undefined. See fdisk_partition_has_partno().
388 *
389 * Returns: 0 on success, <0 on error.
390 */
391 int fdisk_partition_unset_partno(struct fdisk_partition *pa)
392 {
393 if (!pa)
394 return -EINVAL;
395 FDISK_INIT_UNDEF(pa->partno);
396 return 0;
397 }
398
399 /**
400 * fdisk_partition_get_partno:
401 * @pa: partition
402 *
403 * The zero is also valid partition number. The function may return random
404 * value when partno is undefined (for example after fdisk_partition_unset_partno()).
405 * Always use fdisk_partition_has_partno() to be sure that you work with valid
406 * numbers.
407 *
408 * Returns: partition number (0 is the first partition)
409 */
410 size_t fdisk_partition_get_partno(struct fdisk_partition *pa)
411 {
412 return pa->partno;
413 }
414
415 /**
416 * fdisk_partition_has_partno:
417 * @pa: partition
418 *
419 * Returns: 1 or 0
420 */
421 int fdisk_partition_has_partno(struct fdisk_partition *pa)
422 {
423 return pa && !FDISK_IS_UNDEF(pa->partno);
424 }
425
426
427 /**
428 * fdisk_partition_cmp_partno:
429 * @a: partition
430 * @b: partition
431 *
432 * Compares partitions according to partition number See fdisk_table_sort_partitions().
433 *
434 * Return: 0 if the same, <0 if @b greater, >0 if @a greater.
435 */
436 int fdisk_partition_cmp_partno(struct fdisk_partition *a,
437 struct fdisk_partition *b)
438 {
439 return a->partno - b->partno;
440 }
441
442 /**
443 * fdisk_partition_partno_follow_default
444 * @pa: partition
445 * @enable: 0|1
446 *
447 * When @pa used as a template for fdisk_add_partition() when force label driver
448 * to add a new partition to the default (next) position.
449 *
450 * Returns: 0 on success, <0 on error.
451 */
452 int fdisk_partition_partno_follow_default(struct fdisk_partition *pa, int enable)
453 {
454 if (!pa)
455 return -EINVAL;
456 pa->partno_follow_default = enable ? 1 : 0;
457 return 0;
458 }
459
460 /**
461 * fdisk_partition_set_type:
462 * @pa: partition
463 * @type: partition type
464 *
465 * Sets partition type.
466 *
467 * Returns: 0 on success, <0 on error.
468 */
469 int fdisk_partition_set_type(struct fdisk_partition *pa,
470 struct fdisk_parttype *type)
471 {
472 if (!pa)
473 return -EINVAL;
474
475 fdisk_ref_parttype(type);
476 fdisk_unref_parttype(pa->type);
477 pa->type = type;
478
479 return 0;
480 }
481
482 /**
483 * fdisk_partition_get_type:
484 * @pa: partition
485 *
486 * Returns: pointer to partition type.
487 */
488 struct fdisk_parttype *fdisk_partition_get_type(struct fdisk_partition *pa)
489 {
490 return pa ? pa->type : NULL;
491 }
492
493 int fdisk_partition_set_name(struct fdisk_partition *pa, const char *name)
494 {
495 if (!pa)
496 return -EINVAL;
497 return strdup_to_struct_member(pa, name, name);
498 }
499
500 const char *fdisk_partition_get_name(struct fdisk_partition *pa)
501 {
502 return pa ? pa->name : NULL;
503 }
504
505 int fdisk_partition_set_uuid(struct fdisk_partition *pa, const char *uuid)
506 {
507 if (!pa)
508 return -EINVAL;
509 return strdup_to_struct_member(pa, uuid, uuid);
510 }
511
512 /**
513 * fdisk_partition_has_end:
514 * @pa: partition
515 *
516 * Returns: 1 if the partition has defined last sector
517 */
518 int fdisk_partition_has_end(struct fdisk_partition *pa)
519 {
520 return pa && !FDISK_IS_UNDEF(pa->start) && !FDISK_IS_UNDEF(pa->size);
521 }
522
523 /**
524 * fdisk_partition_get_end:
525 * @pa: partition
526 *
527 * This function may returns absolute non-sense, always check
528 * fdisk_partition_has_end().
529 *
530 * Note that partition end is defined by fdisk_partition_set_start() and
531 * fdisk_partition_set_size().
532 *
533 * Returns: last partition sector LBA.
534 */
535 fdisk_sector_t fdisk_partition_get_end(struct fdisk_partition *pa)
536 {
537 return pa->start + pa->size - (pa->size == 0 ? 0 : 1);
538 }
539
540 /**
541 * fdisk_partition_end_follow_default
542 * @pa: partition
543 * @enable: 0|1
544 *
545 * When @pa used as a template for fdisk_add_partition() when force label
546 * driver to use all the possible space for the new partition.
547 *
548 * Returns: 0 on success, <0 on error.
549 */
550 int fdisk_partition_end_follow_default(struct fdisk_partition *pa, int enable)
551 {
552 if (!pa)
553 return -EINVAL;
554 pa->end_follow_default = enable ? 1 : 0;
555 return 0;
556 }
557
558 /**
559 * fdisk_partition_end_is_default:
560 * @pa: partition
561 *
562 * Returns: 1 if the partition follows default
563 */
564 int fdisk_partition_end_is_default(struct fdisk_partition *pa)
565 {
566 assert(pa);
567 return pa->end_follow_default;
568 }
569
570 /**
571 * fdisk_partition_get_uuid:
572 * @pa: partition
573 *
574 * Returns: partition UUID as string
575 */
576 const char *fdisk_partition_get_uuid(struct fdisk_partition *pa)
577 {
578 return pa ? pa->uuid : NULL;
579 }
580
581 /**
582 * fdisk_partition_get_attrs:
583 * @pa: partition
584 *
585 * Returns: partition attributes in string format
586 */
587 const char *fdisk_partition_get_attrs(struct fdisk_partition *pa)
588 {
589 return pa ? pa->attrs : NULL;
590 }
591
592 /**
593 * fdisk_partition_set_attrs:
594 * @pa: partition
595 * @attrs: attributes
596 *
597 * Sets @attrs to @pa.
598 *
599 * Return: 0 on success, <0 on error.
600 */
601 int fdisk_partition_set_attrs(struct fdisk_partition *pa, const char *attrs)
602 {
603 if (!pa)
604 return -EINVAL;
605 return strdup_to_struct_member(pa, attrs, attrs);
606 }
607
608 /**
609 * fdisk_partition_is_nested:
610 * @pa: partition
611 *
612 * Returns: 1 if the partition is nested (e.g. MBR logical partition)
613 */
614 int fdisk_partition_is_nested(struct fdisk_partition *pa)
615 {
616 return pa && !FDISK_IS_UNDEF(pa->parent_partno);
617 }
618
619 /**
620 * fdisk_partition_is_container:
621 * @pa: partition
622 *
623 * Returns: 1 if the partition is container (e.g. MBR extended partition)
624 */
625 int fdisk_partition_is_container(struct fdisk_partition *pa)
626 {
627 return pa && pa->container;
628 }
629
630 /**
631 * fdisk_partition_get_parent:
632 * @pa: partition
633 * @parent: parent parno
634 *
635 * Returns: returns devno of the parent
636 */
637 int fdisk_partition_get_parent(struct fdisk_partition *pa, size_t *parent)
638 {
639 if (pa && parent)
640 *parent = pa->parent_partno;
641 else
642 return -EINVAL;
643 return 0;
644 }
645
646 /**
647 * fdisk_partition_is_used:
648 * @pa: partition
649 *
650 * Returns: 1 if the partition points to some area
651 */
652 int fdisk_partition_is_used(struct fdisk_partition *pa)
653 {
654 return pa && pa->used;
655 }
656
657 /**
658 * fdisk_partition_is_bootable:
659 * @pa: partition
660 *
661 * Returns: 1 if the partition has enabled boot flag
662 */
663 int fdisk_partition_is_bootable(struct fdisk_partition *pa)
664 {
665 return pa && pa->boot == 1;
666 }
667
668 /**
669 * fdisk_partition_is_freespace:
670 * @pa: partition
671 *
672 * Returns: 1 if @pa points to freespace
673 */
674 int fdisk_partition_is_freespace(struct fdisk_partition *pa)
675 {
676 return pa && pa->freespace;
677 }
678
679 /**
680 * fdisk_partition_is_wholedisk:
681 * @pa: partition
682 *
683 * Returns: 1 if the partition is special whole-disk (e.g. SUN) partition
684 */
685 int fdisk_partition_is_wholedisk(struct fdisk_partition *pa)
686 {
687 return pa && pa->wholedisk;
688 }
689
690 /**
691 * fdisk_partition_next_partno:
692 * @pa: partition
693 * @cxt: context
694 * @n: returns partition number
695 *
696 * If @pa specified and partno-follow-default (see fdisk_partition_partno_follow_default())
697 * enabled then returns next expected partno or -ERANGE on error.
698 *
699 * If @pa is NULL, or @pa does not specify any semantic for the next partno
700 * then use Ask API to ask user for the next partno. In this case returns 1 if
701 * no free partition available. If fdisk dialogs are disabled then returns -EINVAL.
702 *
703 * Returns: 0 on success, <0 on error, or 1 for non-free partno by Ask API.
704 */
705 int fdisk_partition_next_partno(
706 struct fdisk_partition *pa,
707 struct fdisk_context *cxt,
708 size_t *n)
709 {
710 if (!cxt || !n)
711 return -EINVAL;
712
713 if (pa && pa->partno_follow_default) {
714 size_t i;
715
716 DBG(PART, ul_debugobj(pa, "next partno (follow default)"));
717
718 for (i = 0; i < cxt->label->nparts_max; i++) {
719 if (!fdisk_is_partition_used(cxt, i)) {
720 *n = i;
721 return 0;
722 }
723 }
724 return -ERANGE;
725
726 } else if (pa && fdisk_partition_has_partno(pa)) {
727
728 DBG(PART, ul_debugobj(pa, "next partno (specified=%zu)", pa->partno));
729
730 if (pa->partno >= cxt->label->nparts_max ||
731 fdisk_is_partition_used(cxt, pa->partno))
732 return -ERANGE;
733 *n = pa->partno;
734 return 0;
735
736 } else if (fdisk_has_dialogs(cxt))
737 return fdisk_ask_partnum(cxt, n, 1);
738
739 return -EINVAL;
740 }
741
742 static int probe_partition_content(struct fdisk_context *cxt, struct fdisk_partition *pa)
743 {
744 int rc = 1; /* nothing */
745
746 DBG(PART, ul_debugobj(pa, "start probe #%zu partition [cxt %p] >>>", pa->partno, cxt));
747
748 /* zeroize the current setting */
749 strdup_to_struct_member(pa, fstype, NULL);
750 strdup_to_struct_member(pa, fsuuid, NULL);
751 strdup_to_struct_member(pa, fslabel, NULL);
752
753 if (!fdisk_partition_has_start(pa) ||
754 !fdisk_partition_has_size(pa))
755 goto done;
756
757 #ifdef HAVE_LIBBLKID
758 else {
759 uintmax_t start, size;
760
761 blkid_probe pr = blkid_new_probe();
762 if (!pr)
763 goto done;
764
765 DBG(PART, ul_debugobj(pa, "blkid prober: %p", pr));
766
767 start = fdisk_partition_get_start(pa) * fdisk_get_sector_size(cxt);
768 size = fdisk_partition_get_size(pa) * fdisk_get_sector_size(cxt);
769
770 if (blkid_probe_set_device(pr, cxt->dev_fd, start, size) == 0
771 && blkid_do_fullprobe(pr) == 0) {
772
773 const char *data;
774 rc = 0;
775
776 if (!blkid_probe_lookup_value(pr, "TYPE", &data, NULL))
777 rc = strdup_to_struct_member(pa, fstype, data);
778
779 if (!rc && !blkid_probe_lookup_value(pr, "LABEL", &data, NULL))
780 rc = strdup_to_struct_member(pa, fslabel, data);
781
782 if (!rc && !blkid_probe_lookup_value(pr, "UUID", &data, NULL))
783 rc = strdup_to_struct_member(pa, fsuuid, data);
784 }
785
786 blkid_free_probe(pr);
787 pa->fs_probed = 1;
788 }
789 #endif /* HAVE_LIBBLKID */
790
791 done:
792 DBG(PART, ul_debugobj(pa, "<<< end probe #%zu partition[cxt %p, rc=%d]", pa->partno, cxt, rc));
793 return rc;
794 }
795
796 /**
797 * fdisk_partition_to_string:
798 * @pa: partition
799 * @cxt: context
800 * @id: field (FDISK_FIELD_*)
801 * @data: returns string with allocated data
802 *
803 * Returns info about partition converted to printable string.
804 *
805 * For example
806 * <informalexample>
807 * <programlisting>
808 * struct fdisk_partition *pa;
809 *
810 * fdisk_get_partition(cxt, 0, &pa);
811 * fdisk_partition_to_string(pa, FDISK_FIELD_UUID, &data);
812 * printf("first partition uuid: %s\n", data);
813 * free(data);
814 * fdisk_unref_partition(pa);
815 * </programlisting>
816 * </informalexample>
817 *
818 * returns UUID for the first partition.
819 *
820 * Returns: 0 on success, otherwise, a corresponding error.
821 */
822 int fdisk_partition_to_string(struct fdisk_partition *pa,
823 struct fdisk_context *cxt,
824 int id,
825 char **data)
826 {
827 char *p = NULL;
828 int rc = 0;
829 uint64_t x;
830
831 if (!pa || !cxt || !data)
832 return -EINVAL;
833
834 switch (id) {
835 case FDISK_FIELD_DEVICE:
836 if (pa->freespace)
837 p = strdup(_("Free space"));
838 else if (fdisk_partition_has_partno(pa) && cxt->dev_path) {
839 if (cxt->label->flags & FDISK_LABEL_FL_INCHARS_PARTNO)
840 rc = asprintf(&p, "%c", (int) pa->partno + 'a');
841 else
842 p = fdisk_partname(cxt->dev_path, pa->partno + 1);
843 }
844 break;
845 case FDISK_FIELD_BOOT:
846 p = fdisk_partition_is_bootable(pa) ? strdup("*") : NULL;
847 break;
848 case FDISK_FIELD_START:
849 if (fdisk_partition_has_start(pa)) {
850 x = fdisk_cround(cxt, pa->start);
851 rc = pa->start_post ?
852 asprintf(&p, "%"PRIu64"%c", x, pa->start_post) :
853 asprintf(&p, "%"PRIu64, x);
854 }
855 break;
856 case FDISK_FIELD_END:
857 if (fdisk_partition_has_end(pa)) {
858 x = fdisk_cround(cxt, fdisk_partition_get_end(pa));
859 rc = pa->end_post ?
860 asprintf(&p, "%"PRIu64"%c", x, pa->end_post) :
861 asprintf(&p, "%"PRIu64, x);
862 }
863 break;
864 case FDISK_FIELD_SIZE:
865 if (fdisk_partition_has_size(pa)) {
866 uint64_t sz = pa->size * cxt->sector_size;
867
868 switch (cxt->sizeunit) {
869 case FDISK_SIZEUNIT_BYTES:
870 rc = asprintf(&p, "%"PRIu64"", sz);
871 break;
872 case FDISK_SIZEUNIT_HUMAN:
873 if (fdisk_is_details(cxt))
874 rc = pa->size_post ?
875 asprintf(&p, "%"PRIu64"%c", sz, pa->size_post) :
876 asprintf(&p, "%"PRIu64, sz);
877 else {
878 p = size_to_human_string(SIZE_SUFFIX_1LETTER, sz);
879 if (!p)
880 rc = -ENOMEM;
881 }
882 break;
883 }
884 }
885 break;
886 case FDISK_FIELD_CYLINDERS:
887 {
888 uintmax_t sz = fdisk_partition_has_size(pa) ? pa->size : 0;
889 if (sz)
890 /* Why we need to cast that to uintmax_t? */
891 rc = asprintf(&p, "%ju", (uintmax_t)(sz / (cxt->geom.heads * cxt->geom.sectors)) + 1);
892 break;
893 }
894 case FDISK_FIELD_SECTORS:
895 rc = asprintf(&p, "%ju",
896 fdisk_partition_has_size(pa) ? (uintmax_t) pa->size : 0);
897 break;
898 case FDISK_FIELD_BSIZE:
899 rc = asprintf(&p, "%"PRIu64, pa->bsize);
900 break;
901 case FDISK_FIELD_FSIZE:
902 rc = asprintf(&p, "%"PRIu64, pa->fsize);
903 break;
904 case FDISK_FIELD_CPG:
905 rc = asprintf(&p, "%"PRIu64, pa->cpg);
906 break;
907 case FDISK_FIELD_TYPE:
908 p = pa->type && pa->type->name ? strdup(_(pa->type->name)) : NULL;
909 break;
910 case FDISK_FIELD_TYPEID:
911 if (pa->type && fdisk_parttype_get_string(pa->type))
912 rc = asprintf(&p, "%s", fdisk_parttype_get_string(pa->type));
913 else if (pa->type)
914 rc = asprintf(&p, "%x", fdisk_parttype_get_code(pa->type));
915 break;
916 case FDISK_FIELD_UUID:
917 p = pa->uuid && *pa->uuid? strdup(pa->uuid) : NULL;
918 break;
919 case FDISK_FIELD_NAME:
920 p = pa->name && *pa->name ? strdup(pa->name) : NULL;
921 break;
922 case FDISK_FIELD_ATTR:
923 p = pa->attrs && *pa->attrs ? strdup(pa->attrs) : NULL;
924 break;
925 case FDISK_FIELD_SADDR:
926 p = pa->start_chs && *pa->start_chs ? strdup(pa->start_chs) : NULL;
927 break;
928 case FDISK_FIELD_EADDR:
929 p = pa->end_chs && *pa->end_chs? strdup(pa->end_chs) : NULL;
930 break;
931 case FDISK_FIELD_FSUUID:
932 if (pa->fs_probed || probe_partition_content(cxt, pa) == 0)
933 p = pa->fsuuid && *pa->fsuuid ? strdup(pa->fsuuid) : NULL;
934 break;
935 case FDISK_FIELD_FSLABEL:
936 if (pa->fs_probed || probe_partition_content(cxt, pa) == 0)
937 p = pa->fslabel && *pa->fslabel ? strdup(pa->fslabel) : NULL;
938 break;
939 case FDISK_FIELD_FSTYPE:
940 if (pa->fs_probed || probe_partition_content(cxt, pa) == 0)
941 p = pa->fstype && *pa->fstype ? strdup(pa->fstype) : NULL;
942 break;
943 default:
944 return -EINVAL;
945 }
946
947 if (rc < 0) {
948 rc = -ENOMEM;
949 free(p);
950 p = NULL;
951
952 } else if (rc > 0)
953 rc = 0;
954
955 *data = p;
956
957 return rc;
958 }
959
960
961 /**
962 * fdisk_get_partition:
963 * @cxt: context
964 * @partno: partition number (0 is the first partition)
965 * @pa: returns data about partition
966 *
967 * Reads disklabel and fills in @pa with data about partition @n.
968 *
969 * Note that partno may address unused partition and then this function does
970 * not fill anything to @pa. See fdisk_is_partition_used(). If @pa points to
971 * NULL then the function allocates a newly allocated fdisk_partition struct,
972 * use fdisk_unref_partition() to deallocate.
973 *
974 * Returns: 0 on success, otherwise, a corresponding error.
975 */
976 int fdisk_get_partition(struct fdisk_context *cxt, size_t partno,
977 struct fdisk_partition **pa)
978 {
979 int rc;
980 struct fdisk_partition *np = NULL;
981
982 if (!cxt || !cxt->label || !pa)
983 return -EINVAL;
984 if (!cxt->label->op->get_part)
985 return -ENOSYS;
986 if (!fdisk_is_partition_used(cxt, partno))
987 return -EINVAL;
988
989 if (!*pa) {
990 np = *pa = fdisk_new_partition();
991 if (!*pa)
992 return -ENOMEM;
993 } else
994 fdisk_reset_partition(*pa);
995
996 (*pa)->partno = partno;
997 rc = cxt->label->op->get_part(cxt, partno, *pa);
998
999 if (rc) {
1000 if (np) {
1001 fdisk_unref_partition(np);
1002 *pa = NULL;
1003 } else
1004 fdisk_reset_partition(*pa);
1005 } else
1006 (*pa)->size_explicit = 1;
1007 return rc;
1008 }
1009
1010 static struct fdisk_partition *resize_get_by_offset(
1011 struct fdisk_table *tb,
1012 struct fdisk_partition *cur,
1013 fdisk_sector_t off)
1014 {
1015 struct fdisk_partition *pa = NULL;
1016 struct fdisk_iter itr;
1017
1018 fdisk_reset_iter(&itr, FDISK_ITER_FORWARD);
1019
1020 while (fdisk_table_next_partition(tb, &itr, &pa) == 0) {
1021 if (!fdisk_partition_has_start(pa) || !fdisk_partition_has_size(pa))
1022 continue;
1023 if (fdisk_partition_is_nested(cur) &&
1024 pa->parent_partno != cur->parent_partno)
1025 continue;
1026 if (off >= pa->start && off < pa->start + pa->size)
1027 return pa;
1028 }
1029
1030 return NULL;
1031 }
1032
1033 /*
1034 * Verify that area addressed by @start is freespace or the @cur[rent]
1035 * partition and continue to the next table entries until it's freespace, and
1036 * counts size of all this space.
1037 *
1038 * This is core of the partition start offset move operation. We can move the
1039 * start within the current partition of to the another free space. It's
1040 * forbidden to move start of the partition to another already defined
1041 * partition.
1042 */
1043 static int resize_get_last_possible(
1044 struct fdisk_table *tb,
1045 struct fdisk_partition *cur,
1046 fdisk_sector_t start,
1047 fdisk_sector_t *maxsz)
1048 {
1049 struct fdisk_partition *pa = NULL, *last = NULL;
1050 struct fdisk_iter itr;
1051
1052 fdisk_reset_iter(&itr, FDISK_ITER_FORWARD);
1053
1054 *maxsz = 0;
1055 DBG(TAB, ul_debugobj(tb, "checking last possible for start=%ju", (uintmax_t) start));
1056
1057
1058 while (fdisk_table_next_partition(tb, &itr, &pa) == 0) {
1059
1060 DBG(TAB, ul_debugobj(tb, " checking entry %p [partno=%zu start=%ju, end=%ju, size=%ju%s%s%s]",
1061 pa,
1062 fdisk_partition_get_partno(pa),
1063 (uintmax_t) fdisk_partition_get_start(pa),
1064 (uintmax_t) fdisk_partition_get_end(pa),
1065 (uintmax_t) fdisk_partition_get_size(pa),
1066 fdisk_partition_is_freespace(pa) ? " freespace" : "",
1067 fdisk_partition_is_nested(pa) ? " nested" : "",
1068 fdisk_partition_is_container(pa) ? " container" : ""));
1069
1070 if (!fdisk_partition_has_start(pa) ||
1071 !fdisk_partition_has_size(pa) ||
1072 (fdisk_partition_is_container(pa) && pa != cur)) {
1073 DBG(TAB, ul_debugobj(tb, " ignored (no start/size or container)"));
1074 continue;
1075 }
1076
1077 if (fdisk_partition_is_nested(pa)
1078 && fdisk_partition_is_container(cur)
1079 && pa->parent_partno == cur->partno) {
1080 DBG(TAB, ul_debugobj(tb, " ignore (nested child of the current partition)"));
1081 continue;
1082 }
1083
1084 /* The current is nested, free space has to be nested within the same parent */
1085 if (fdisk_partition_is_nested(cur)
1086 && pa->parent_partno != cur->parent_partno) {
1087 DBG(TAB, ul_debugobj(tb, " ignore (nested required)"));
1088 continue;
1089 }
1090
1091 if (!last) {
1092 if (start >= pa->start && start < pa->start + pa->size) {
1093 if (fdisk_partition_is_freespace(pa) || pa == cur) {
1094 DBG(TAB, ul_debugobj(tb, " accepted as last"));
1095 last = pa;
1096 } else {
1097 DBG(TAB, ul_debugobj(tb, " failed to set last"));
1098 break;
1099 }
1100
1101
1102 *maxsz = pa->size - (start - pa->start);
1103 DBG(TAB, ul_debugobj(tb, " new max=%ju", (uintmax_t) *maxsz));
1104 }
1105 } else if (!fdisk_partition_is_freespace(pa) && pa != cur) {
1106 DBG(TAB, ul_debugobj(tb, " no free space behind current"));
1107 break;
1108 } else {
1109 last = pa;
1110 *maxsz = pa->size - (start - pa->start);
1111 DBG(TAB, ul_debugobj(tb, " new max=%ju (last updated)", (uintmax_t) *maxsz));
1112 }
1113 }
1114
1115 if (last)
1116 DBG(PART, ul_debugobj(cur, "resize: max size=%ju", (uintmax_t) *maxsz));
1117 else
1118 DBG(PART, ul_debugobj(cur, "resize: nothing usable after %ju", (uintmax_t) start));
1119
1120 return last ? 0 : -1;
1121 }
1122
1123 /*
1124 * Uses template @tpl to recount start and size change of the partition @res. The
1125 * @tpl->size and @tpl->start are interpreted as relative to the current setting.
1126 */
1127 static int recount_resize(
1128 struct fdisk_context *cxt, size_t partno,
1129 struct fdisk_partition *res, struct fdisk_partition *tpl)
1130 {
1131 fdisk_sector_t start, size, xsize;
1132 struct fdisk_partition *cur = NULL;
1133 struct fdisk_table *tb = NULL;
1134 int rc;
1135
1136 DBG(PART, ul_debugobj(tpl, ">>> resize requested"));
1137
1138 FDISK_INIT_UNDEF(start);
1139 FDISK_INIT_UNDEF(size);
1140
1141 rc = fdisk_get_partitions(cxt, &tb);
1142 if (!rc) {
1143 /* For resize we do not follow grain to detect free-space, but
1144 * we allow to resize with very small granulation. */
1145 unsigned long org = cxt->grain;
1146
1147 cxt->grain = cxt->sector_size;
1148 rc = fdisk_get_freespaces(cxt, &tb);
1149 cxt->grain = org;
1150 }
1151 if (rc) {
1152 fdisk_unref_table(tb);
1153 return rc;
1154 }
1155
1156 fdisk_table_sort_partitions(tb, fdisk_partition_cmp_start);
1157
1158 DBG(PART, ul_debugobj(tpl, "resize partition partno=%zu in table:", partno));
1159 ON_DBG(PART, fdisk_debug_print_table(tb));
1160
1161 cur = fdisk_table_get_partition_by_partno(tb, partno);
1162 if (!cur) {
1163 fdisk_unref_table(tb);
1164 return -EINVAL;
1165 }
1166
1167 /* 1a) set new start - change relative to the current on-disk setting */
1168 if (tpl->movestart && fdisk_partition_has_start(tpl)) {
1169 start = fdisk_partition_get_start(cur);
1170 if (tpl->movestart == FDISK_MOVE_DOWN) {
1171 if (fdisk_partition_get_start(tpl) > start)
1172 goto erange;
1173 start -= fdisk_partition_get_start(tpl);
1174 } else
1175 start += fdisk_partition_get_start(tpl);
1176
1177 DBG(PART, ul_debugobj(tpl, "resize: moving start %s relative, new start: %ju",
1178 tpl->movestart == FDISK_MOVE_DOWN ? "DOWN" : "UP", (uintmax_t)start));
1179
1180 /* 1b) set new start - absolute number */
1181 } else if (fdisk_partition_has_start(tpl)) {
1182 start = fdisk_partition_get_start(tpl);
1183 DBG(PART, ul_debugobj(tpl, "resize: moving start to absolute offset: %ju",
1184 (uintmax_t)start));
1185 }
1186
1187 /* 2) verify that start is within the current partition or any freespace area */
1188 if (!FDISK_IS_UNDEF(start)) {
1189 struct fdisk_partition *area = resize_get_by_offset(tb, cur, start);
1190
1191 if (area == cur)
1192 DBG(PART, ul_debugobj(tpl, "resize: start points to the current partition"));
1193 else if (area && fdisk_partition_is_freespace(area))
1194 DBG(PART, ul_debugobj(tpl, "resize: start points to freespace"));
1195 else if (!area && start >= cxt->first_lba && start < cxt->first_lba + (cxt->grain / cxt->sector_size))
1196 DBG(PART, ul_debugobj(tpl, "resize: start points before first partition"));
1197 else {
1198 DBG(PART, ul_debugobj(tpl, "resize: start verification failed"));
1199 goto erange;
1200 }
1201 } else {
1202 /* no change, start points to the current partition */
1203 DBG(PART, ul_debugobj(tpl, "resize: start unchanged"));
1204 start = fdisk_partition_get_start(cur);
1205 }
1206
1207 /* 3a) set new size -- reduce */
1208 if (tpl->resize == FDISK_RESIZE_REDUCE && fdisk_partition_has_size(tpl)) {
1209 DBG(PART, ul_debugobj(tpl, "resize: reduce"));
1210 size = fdisk_partition_get_size(cur);
1211 if (fdisk_partition_get_size(tpl) > size)
1212 goto erange;
1213 size -= fdisk_partition_get_size(tpl);
1214
1215 /* 3b) set new size -- enlarge */
1216 } else if (tpl->resize == FDISK_RESIZE_ENLARGE && fdisk_partition_has_size(tpl)) {
1217 DBG(PART, ul_debugobj(tpl, "resize: enlarge"));
1218 size = fdisk_partition_get_size(cur);
1219 size += fdisk_partition_get_size(tpl);
1220
1221 /* 3c) set new size -- no size specified, enlarge to all freespace */
1222 } else if (tpl->resize == FDISK_RESIZE_ENLARGE) {
1223 DBG(PART, ul_debugobj(tpl, "resize: enlarge to all possible"));
1224 if (resize_get_last_possible(tb, cur, start, &size))
1225 goto erange;
1226
1227 /* 3d) set new size -- absolute number */
1228 } else if (fdisk_partition_has_size(tpl)) {
1229 DBG(PART, ul_debugobj(tpl, "resize: new absolute size"));
1230 size = fdisk_partition_get_size(tpl);
1231 }
1232
1233 /* 4) verify that size is within the current partition or next free space */
1234 xsize = !FDISK_IS_UNDEF(size) ? size : fdisk_partition_get_size(cur);
1235
1236 if (fdisk_partition_has_size(cur)) {
1237 fdisk_sector_t maxsz;
1238
1239 if (resize_get_last_possible(tb, cur, start, &maxsz))
1240 goto erange;
1241 DBG(PART, ul_debugobj(tpl, "resize: size=%ju, max=%ju",
1242 (uintmax_t) xsize, (uintmax_t) maxsz));
1243 if (xsize > maxsz)
1244 goto erange;
1245 }
1246
1247 if (FDISK_IS_UNDEF(size)) {
1248 DBG(PART, ul_debugobj(tpl, "resize: size unchanged (undefined)"));
1249 }
1250
1251
1252 DBG(PART, ul_debugobj(tpl, "<<< resize: SUCCESS: start %ju->%ju; size %ju->%ju",
1253 (uintmax_t) fdisk_partition_get_start(cur), (uintmax_t) start,
1254 (uintmax_t) fdisk_partition_get_size(cur), (uintmax_t) size));
1255 res->start = start;
1256 res->size = size;
1257 fdisk_unref_table(tb);
1258 return 0;
1259 erange:
1260 DBG(PART, ul_debugobj(tpl, "<<< resize: FAILED"));
1261 fdisk_warnx(cxt, _("Failed to resize partition #%zu."), partno + 1);
1262 fdisk_unref_table(tb);
1263 return -ERANGE;
1264
1265 }
1266
1267 /**
1268 * fdisk_set_partition:
1269 * @cxt: context
1270 * @partno: partition number (0 is the first partition)
1271 * @pa: new partition setting
1272 *
1273 * Modifies disklabel according to setting with in @pa.
1274 *
1275 * Returns: 0 on success, <0 on error.
1276 */
1277 int fdisk_set_partition(struct fdisk_context *cxt, size_t partno,
1278 struct fdisk_partition *pa)
1279 {
1280 struct fdisk_partition *xpa = pa, *tmp = NULL;
1281 int rc, wipe = 0;
1282
1283 if (!cxt || !cxt->label || !pa)
1284 return -EINVAL;
1285 if (!cxt->label->op->set_part)
1286 return -ENOSYS;
1287
1288 pa->fs_probed = 0;
1289
1290 if (!fdisk_is_partition_used(cxt, partno)) {
1291 pa->partno = partno;
1292 return fdisk_add_partition(cxt, pa, NULL);
1293 }
1294
1295 if (pa->resize || fdisk_partition_has_start(pa) || fdisk_partition_has_size(pa)) {
1296 xpa = __copy_partition(pa);
1297 if (!xpa) {
1298 rc = -ENOMEM;
1299 goto done;
1300 }
1301 xpa->movestart = 0;
1302 xpa->resize = 0;
1303 FDISK_INIT_UNDEF(xpa->size);
1304 FDISK_INIT_UNDEF(xpa->start);
1305
1306 rc = recount_resize(cxt, partno, xpa, pa);
1307 if (rc)
1308 goto done;
1309 }
1310
1311 DBG(CXT, ul_debugobj(cxt, "setting partition %zu %p (start=%ju, end=%ju, size=%ju)",
1312 partno, xpa,
1313 (uintmax_t) fdisk_partition_get_start(xpa),
1314 (uintmax_t) fdisk_partition_get_end(xpa),
1315 (uintmax_t) fdisk_partition_get_size(xpa)));
1316
1317 /* disable wipe for old offset/size setting */
1318 if (fdisk_get_partition(cxt, partno, &tmp) == 0 && tmp) {
1319 wipe = fdisk_set_wipe_area(cxt, fdisk_partition_get_start(tmp),
1320 fdisk_partition_get_size(tmp), FALSE);
1321 fdisk_unref_partition(tmp);
1322 }
1323
1324 /* call label driver */
1325 rc = cxt->label->op->set_part(cxt, partno, xpa);
1326
1327 /* enable wipe for new offset/size */
1328 if (!rc && wipe)
1329 fdisk_wipe_partition(cxt, partno, TRUE);
1330 done:
1331 DBG(CXT, ul_debugobj(cxt, "set_partition() rc=%d", rc));
1332 if (xpa != pa)
1333 fdisk_unref_partition(xpa);
1334 return rc;
1335 }
1336
1337 /**
1338 * fdisk_wipe_partition:
1339 * @cxt: fdisk context
1340 * @partno: partition number
1341 * @enable: 0 or 1
1342 *
1343 * Enable/disable filesystems/RAIDs wiping in area defined by partition start and size.
1344 *
1345 * Returns: <0 in case of error, 0 on success
1346 * Since: 2.29
1347 */
1348 int fdisk_wipe_partition(struct fdisk_context *cxt, size_t partno, int enable)
1349 {
1350 struct fdisk_partition *pa = NULL;
1351 int rc;
1352
1353 rc = fdisk_get_partition(cxt, partno, &pa);
1354 if (rc)
1355 return rc;
1356
1357 rc = fdisk_set_wipe_area(cxt, fdisk_partition_get_start(pa),
1358 fdisk_partition_get_size(pa), enable);
1359 fdisk_unref_partition(pa);
1360 return rc < 0 ? rc : 0;
1361 }
1362
1363 /**
1364 * fdisk_partition_has_wipe:
1365 * @cxt: fdisk context
1366 * @pa: partition
1367 *
1368 * Since: 2.30
1369 *
1370 * Returns: 1 if the area specified by @pa will be wiped by write command, or 0.
1371 */
1372 int fdisk_partition_has_wipe(struct fdisk_context *cxt, struct fdisk_partition *pa)
1373 {
1374 return fdisk_has_wipe_area(cxt, fdisk_partition_get_start(pa),
1375 fdisk_partition_get_size(pa));
1376 }
1377
1378
1379 /**
1380 * fdisk_add_partition:
1381 * @cxt: fdisk context
1382 * @pa: template for the partition (or NULL)
1383 * @partno: NULL or returns new partition number
1384 *
1385 * If @pa is not specified or any @pa item is missing the libfdisk will ask by
1386 * fdisk_ask_ API.
1387 *
1388 * The @pa template is is important for non-interactive partitioning,
1389 * especially for MBR where is necessary to differentiate between
1390 * primary/logical; this is done by start offset or/and partno.
1391 * The rules for MBR:
1392 *
1393 * A) template specifies start within extended partition: add logical
1394 * B) template specifies start out of extended partition: add primary
1395 * C) template specifies start (or default), partno < 4: add primary
1396 * D) template specifies default start, partno >= 4: add logical
1397 *
1398 * otherwise MBR driver uses Ask-API to get missing information.
1399 *
1400 * Adds a new partition to disklabel.
1401 *
1402 * Returns: 0 on success, <0 on error.
1403 */
1404 int fdisk_add_partition(struct fdisk_context *cxt,
1405 struct fdisk_partition *pa,
1406 size_t *partno)
1407 {
1408 int rc;
1409
1410 if (!cxt || !cxt->label)
1411 return -EINVAL;
1412 if (!cxt->label->op->add_part)
1413 return -ENOSYS;
1414 if (fdisk_missing_geometry(cxt))
1415 return -EINVAL;
1416
1417 if (pa) {
1418 pa->fs_probed = 0;
1419 DBG(CXT, ul_debugobj(cxt, "adding new partition %p", pa));
1420 if (fdisk_partition_has_start(pa))
1421 DBG(CXT, ul_debugobj(cxt, " start: %ju", (uintmax_t) fdisk_partition_get_start(pa)));
1422 if (fdisk_partition_has_end(pa))
1423 DBG(CXT, ul_debugobj(cxt, " end: %ju", (uintmax_t) fdisk_partition_get_end(pa)));
1424 if (fdisk_partition_has_size(pa))
1425 DBG(CXT, ul_debugobj(cxt, " size: %ju", (uintmax_t) fdisk_partition_get_size(pa)));
1426
1427 DBG(CXT, ul_debugobj(cxt, " defaults: start=%s, end=%s, partno=%s",
1428 pa->start_follow_default ? "yes" : "no",
1429 pa->end_follow_default ? "yes" : "no",
1430 pa->partno_follow_default ? "yes" : "no"));
1431 } else
1432 DBG(CXT, ul_debugobj(cxt, "adding partition"));
1433
1434 rc = cxt->label->op->add_part(cxt, pa, partno);
1435
1436 DBG(CXT, ul_debugobj(cxt, "add partition done (rc=%d)", rc));
1437 return rc;
1438 }
1439
1440 /**
1441 * fdisk_delete_partition:
1442 * @cxt: fdisk context
1443 * @partno: partition number to delete (0 is the first partition)
1444 *
1445 * Deletes a @partno partition from disklabel.
1446 *
1447 * Returns: 0 on success, <0 on error
1448 */
1449 int fdisk_delete_partition(struct fdisk_context *cxt, size_t partno)
1450 {
1451 if (!cxt || !cxt->label)
1452 return -EINVAL;
1453 if (!cxt->label->op->del_part)
1454 return -ENOSYS;
1455
1456 fdisk_wipe_partition(cxt, partno, 0);
1457
1458 DBG(CXT, ul_debugobj(cxt, "deleting %s partition number %zd",
1459 cxt->label->name, partno));
1460 return cxt->label->op->del_part(cxt, partno);
1461 }
1462
1463 /**
1464 * fdisk_delete_all_partitions:
1465 * @cxt: fdisk context
1466 *
1467 * Delete all used partitions from disklabel.
1468 *
1469 * Returns: 0 on success, otherwise, a corresponding error.
1470 */
1471 int fdisk_delete_all_partitions(struct fdisk_context *cxt)
1472 {
1473 size_t i;
1474 int rc = 0;
1475
1476 if (!cxt || !cxt->label)
1477 return -EINVAL;
1478
1479 for (i = 0; i < cxt->label->nparts_max; i++) {
1480
1481 if (!fdisk_is_partition_used(cxt, i))
1482 continue;
1483 rc = fdisk_delete_partition(cxt, i);
1484 if (rc)
1485 break;
1486 }
1487
1488 return rc;
1489 }
1490
1491 /**
1492 * fdisk_is_partition_used:
1493 * @cxt: context
1494 * @n: partition number (0 is the first partition)
1495 *
1496 * Check if the partition number @n is used by partition table. This function
1497 * does not check if the device is used (e.g. mounted) by system!
1498 *
1499 * This is faster than fdisk_get_partition() + fdisk_partition_is_used().
1500 *
1501 * Returns: 0 or 1
1502 */
1503 int fdisk_is_partition_used(struct fdisk_context *cxt, size_t n)
1504 {
1505 if (!cxt || !cxt->label)
1506 return -EINVAL;
1507 if (!cxt->label->op->part_is_used)
1508 return -ENOSYS;
1509
1510 return cxt->label->op->part_is_used(cxt, n);
1511 }
1512