forked from Surachai-kent/util-linux
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcontext.c
More file actions
1555 lines (1366 loc) · 35.6 KB
/
context.c
File metadata and controls
1555 lines (1366 loc) · 35.6 KB
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
#ifdef HAVE_LIBBLKID
# include <blkid.h>
#endif
#include "blkdev.h"
#ifdef __linux__
# include "partx.h"
#endif
#include "loopdev.h"
#include "fdiskP.h"
#include "strutils.h"
/**
* SECTION: context
* @title: Context
* @short_description: stores info about device, labels etc.
*
* The library distinguish between three types of partitioning objects.
*
* on-disk label data
* - disk label specific
* - probed and read by disklabel drivers when assign device to the context
* or when switch to another disk label type
* - only fdisk_write_disklabel() modify on-disk data
*
* in-memory label data
* - generic data and disklabel specific data stored in struct fdisk_label
* - all partitioning operations are based on in-memory data only
*
* struct fdisk_partition
* - provides abstraction to present partitions to users
* - fdisk_partition is possible to gather to fdisk_table container
* - used as unified template for new partitions
* - used (with fdisk_table) in fdisk scripts
* - the struct fdisk_partition is always completely independent object and
* any change to the object has no effect to in-memory (or on-disk) label data
*
* Don't forget to inform kernel about changes by fdisk_reread_partition_table()
* or more smart fdisk_reread_changes().
*/
/**
* fdisk_new_context:
*
* Returns: newly allocated libfdisk handler
*/
struct fdisk_context *fdisk_new_context(void)
{
struct fdisk_context *cxt;
cxt = calloc(1, sizeof(*cxt));
if (!cxt)
return NULL;
DBG(CXT, ul_debugobj(cxt, "alloc"));
cxt->dev_fd = -1;
cxt->refcount = 1;
INIT_LIST_HEAD(&cxt->wipes);
/*
* Allocate label specific structs.
*
* This is necessary (for example) to store label specific
* context setting.
*/
cxt->labels[ cxt->nlabels++ ] = fdisk_new_gpt_label(cxt);
cxt->labels[ cxt->nlabels++ ] = fdisk_new_dos_label(cxt);
cxt->labels[ cxt->nlabels++ ] = fdisk_new_bsd_label(cxt);
cxt->labels[ cxt->nlabels++ ] = fdisk_new_sgi_label(cxt);
cxt->labels[ cxt->nlabels++ ] = fdisk_new_sun_label(cxt);
bindtextdomain(LIBFDISK_TEXTDOMAIN, LOCALEDIR);
return cxt;
}
static int init_nested_from_parent(struct fdisk_context *cxt, int isnew)
{
struct fdisk_context *parent;
assert(cxt);
assert(cxt->parent);
parent = cxt->parent;
INIT_LIST_HEAD(&cxt->wipes);
cxt->alignment_offset = parent->alignment_offset;
cxt->ask_cb = parent->ask_cb;
cxt->ask_data = parent->ask_data;
cxt->dev_fd = parent->dev_fd;
cxt->first_lba = parent->first_lba;
cxt->firstsector_bufsz = parent->firstsector_bufsz;
cxt->firstsector = parent->firstsector;
cxt->geom = parent->geom;
cxt->grain = parent->grain;
cxt->io_size = parent->io_size;
cxt->last_lba = parent->last_lba;
cxt->min_io_size = parent->min_io_size;
cxt->optimal_io_size = parent->optimal_io_size;
cxt->phy_sector_size = parent->phy_sector_size;
cxt->readonly = parent->readonly;
cxt->script = parent->script;
fdisk_ref_script(cxt->script);
cxt->sector_size = parent->sector_size;
cxt->total_sectors = parent->total_sectors;
cxt->user_geom = parent->user_geom;
cxt->user_log_sector = parent->user_log_sector;
cxt->user_pyh_sector = parent->user_pyh_sector;
/* parent <--> nested independent setting, initialize for new nested
* contexts only */
if (isnew) {
cxt->listonly = parent->listonly;
cxt->display_details = parent->display_details;
cxt->display_in_cyl_units = parent->display_in_cyl_units;
cxt->protect_bootbits = parent->protect_bootbits;
}
free(cxt->dev_model);
cxt->dev_model = NULL;
cxt->dev_model_probed = 0;
return strdup_between_structs(cxt, parent, dev_path);
}
/**
* fdisk_new_nested_context:
* @parent: parental context
* @name: optional label name (e.g. "bsd")
*
* Create a new nested fdisk context for nested disk labels (e.g. BSD or PMBR).
* The function also probes for the nested label on the device if device is
* already assigned to parent.
*
* The new context is initialized according to @parent and both context shares
* some settings and file descriptor to the device. The child propagate some
* changes (like fdisk_assign_device()) to parent, but it does not work
* vice-versa. The behavior is undefined if you assign another device to
* parent.
*
* Returns: new context for nested partition table.
*/
struct fdisk_context *fdisk_new_nested_context(struct fdisk_context *parent,
const char *name)
{
struct fdisk_context *cxt;
struct fdisk_label *lb = NULL;
assert(parent);
cxt = calloc(1, sizeof(*cxt));
if (!cxt)
return NULL;
DBG(CXT, ul_debugobj(parent, "alloc nested [%p] [name=%s]", cxt, name));
cxt->refcount = 1;
fdisk_ref_context(parent);
cxt->parent = parent;
if (init_nested_from_parent(cxt, 1) != 0) {
cxt->parent = NULL;
fdisk_unref_context(cxt);
return NULL;
}
if (name) {
if (strcasecmp(name, "bsd") == 0)
lb = cxt->labels[ cxt->nlabels++ ] = fdisk_new_bsd_label(cxt);
else if (strcasecmp(name, "dos") == 0 || strcasecmp(name, "mbr") == 0)
lb = cxt->labels[ cxt->nlabels++ ] = fdisk_new_dos_label(cxt);
}
if (lb && parent->dev_fd >= 0) {
DBG(CXT, ul_debugobj(cxt, "probing for nested %s", lb->name));
cxt->label = lb;
if (lb->op->probe(cxt) == 1)
__fdisk_switch_label(cxt, lb);
else {
DBG(CXT, ul_debugobj(cxt, "not found %s label", lb->name));
if (lb->op->deinit)
lb->op->deinit(lb);
cxt->label = NULL;
}
}
return cxt;
}
/**
* fdisk_ref_context:
* @cxt: context pointer
*
* Increments reference counter.
*/
void fdisk_ref_context(struct fdisk_context *cxt)
{
if (cxt)
cxt->refcount++;
}
/**
* fdisk_get_label:
* @cxt: context instance
* @name: label name (e.g. "gpt")
*
* If no @name specified then returns the current context label.
*
* The label is allocated and maintained within the context #cxt. There is
* nothing like reference counting for labels, you cannot deallocate the
* label.
*
* Returns: label struct or NULL in case of error.
*/
struct fdisk_label *fdisk_get_label(struct fdisk_context *cxt, const char *name)
{
size_t i;
assert(cxt);
if (!name)
return cxt->label;
if (strcasecmp(name, "mbr") == 0)
name = "dos";
for (i = 0; i < cxt->nlabels; i++)
if (cxt->labels[i]
&& strcasecmp(cxt->labels[i]->name, name) == 0)
return cxt->labels[i];
DBG(CXT, ul_debugobj(cxt, "failed to found %s label driver", name));
return NULL;
}
/**
* fdisk_next_label:
* @cxt: context instance
* @lb: returns pointer to the next label
*
* <informalexample>
* <programlisting>
* // print all supported labels
* struct fdisk_context *cxt = fdisk_new_context();
* struct fdisk_label *lb = NULL;
*
* while (fdisk_next_label(cxt, &lb) == 0)
* print("label name: %s\n", fdisk_label_get_name(lb));
* fdisk_unref_context(cxt);
* </programlisting>
* </informalexample>
*
* Returns: <0 in case of error, 0 on success, 1 at the end.
*/
int fdisk_next_label(struct fdisk_context *cxt, struct fdisk_label **lb)
{
size_t i;
struct fdisk_label *res = NULL;
if (!lb || !cxt)
return -EINVAL;
if (!*lb)
res = cxt->labels[0];
else {
for (i = 1; i < cxt->nlabels; i++) {
if (*lb == cxt->labels[i - 1]) {
res = cxt->labels[i];
break;
}
}
}
*lb = res;
return res ? 0 : 1;
}
/**
* fdisk_get_nlabels:
* @cxt: context
*
* Returns: number of supported label types
*/
size_t fdisk_get_nlabels(struct fdisk_context *cxt)
{
return cxt ? cxt->nlabels : 0;
}
int __fdisk_switch_label(struct fdisk_context *cxt, struct fdisk_label *lb)
{
if (!lb || !cxt)
return -EINVAL;
if (lb->disabled) {
DBG(CXT, ul_debugobj(cxt, "*** attempt to switch to disabled label %s -- ignore!", lb->name));
return -EINVAL;
}
cxt->label = lb;
DBG(CXT, ul_debugobj(cxt, "--> switching context to %s!", lb->name));
fdisk_apply_label_device_properties(cxt);
return 0;
}
/**
* fdisk_has_label:
* @cxt: fdisk context
*
* Returns: return 1 if there is label on the device.
*/
int fdisk_has_label(struct fdisk_context *cxt)
{
return cxt && cxt->label;
}
/**
* fdisk_has_protected_bootbits:
* @cxt: fdisk context
*
* Returns: return 1 if boot bits protection enabled.
*/
int fdisk_has_protected_bootbits(struct fdisk_context *cxt)
{
return cxt && cxt->protect_bootbits;
}
/**
* fdisk_enable_bootbits_protection:
* @cxt: fdisk context
* @enable: 1 or 0
*
* The library zeroizes all the first sector when create a new disk label by
* default. This function can be used to control this behavior. For now it's
* supported for MBR and GPT.
*
* Returns: 0 on success, < 0 on error.
*/
int fdisk_enable_bootbits_protection(struct fdisk_context *cxt, int enable)
{
if (!cxt)
return -EINVAL;
cxt->protect_bootbits = enable ? 1 : 0;
return 0;
}
/**
* fdisk_disable_dialogs
* @cxt: fdisk context
* @disable: 1 or 0
*
* The library uses dialog driven partitioning by default.
*
* Returns: 0 on success, < 0 on error.
*
* Since: 2.31
*/
int fdisk_disable_dialogs(struct fdisk_context *cxt, int disable)
{
if (!cxt)
return -EINVAL;
cxt->no_disalogs = disable;
return 0;
}
/**
* fdisk_has_dialogs
* @cxt: fdisk context
*
* See fdisk_disable_dialogs()
*
* Returns: 1 if dialog driven partitioning enabled (default), or 0.
*
* Since: 2.31
*/
int fdisk_has_dialogs(struct fdisk_context *cxt)
{
return cxt->no_disalogs == 0;
}
/**
* fdisk_enable_wipe
* @cxt: fdisk context
* @enable: 1 or 0
*
* The library removes all PT/filesystem/RAID signatures before it writes
* partition table. The probing area where it looks for signatures is from
* the begin of the disk. The device is wiped by libblkid.
*
* See also fdisk_wipe_partition().
*
* Returns: 0 on success, < 0 on error.
*/
int fdisk_enable_wipe(struct fdisk_context *cxt, int enable)
{
if (!cxt)
return -EINVAL;
fdisk_set_wipe_area(cxt, 0, cxt->total_sectors, enable);
return 0;
}
/**
* fdisk_has_wipe
* @cxt: fdisk context
*
* Returns the current wipe setting. See fdisk_enable_wipe().
*
* Returns: 0 on success, < 0 on error.
*/
int fdisk_has_wipe(struct fdisk_context *cxt)
{
if (!cxt)
return 0;
return fdisk_has_wipe_area(cxt, 0, cxt->total_sectors);
}
/**
* fdisk_get_collision
* @cxt: fdisk context
*
* Returns: name of the filesystem or RAID detected on the device or NULL.
*/
const char *fdisk_get_collision(struct fdisk_context *cxt)
{
return cxt->collision;
}
/**
* fdisk_is_ptcollision:
* @cxt: fdisk context
*
* The collision detected by libblkid (usually another partition table). Note
* that libfdisk does not support all partitions tables, so fdisk_has_label()
* may return false, but fdisk_is_ptcollision() may return true.
*
* Since: 2.30
*
* Returns: 0 or 1
*/
int fdisk_is_ptcollision(struct fdisk_context *cxt)
{
return cxt->pt_collision;
}
/**
* fdisk_get_npartitions:
* @cxt: context
*
* The maximal number of the partitions depends on disklabel and does not
* have to describe the real limit of PT.
*
* For example the limit for MBR without extend partition is 4, with extended
* partition it's unlimited (so the function returns the current number of all
* partitions in this case).
*
* And for example for GPT it depends on space allocated on disk for array of
* entry records (usually 128).
*
* It's fine to use fdisk_get_npartitions() in loops, but don't forget that
* partition may be unused (see fdisk_is_partition_used()).
*
* <informalexample>
* <programlisting>
* struct fdisk_partition *pa = NULL;
* size_t i, nmax = fdisk_get_npartitions(cxt);
*
* for (i = 0; i < nmax; i++) {
* if (!fdisk_is_partition_used(cxt, i))
* continue;
* ... do something ...
* }
* </programlisting>
* </informalexample>
*
* Note that the recommended way to list partitions is to use
* fdisk_get_partitions() and struct fdisk_table then ask disk driver for each
* individual partitions.
*
* Returns: maximal number of partitions for the current label.
*/
size_t fdisk_get_npartitions(struct fdisk_context *cxt)
{
return cxt && cxt->label ? cxt->label->nparts_max : 0;
}
/**
* fdisk_is_labeltype:
* @cxt: fdisk context
* @id: FDISK_DISKLABEL_*
*
* See also fdisk_is_label() macro in libfdisk.h.
*
* Returns: return 1 if the current label is @id
*/
int fdisk_is_labeltype(struct fdisk_context *cxt, enum fdisk_labeltype id)
{
assert(cxt);
return cxt->label && (unsigned)fdisk_label_get_type(cxt->label) == id;
}
/**
* fdisk_get_parent:
* @cxt: nested fdisk context
*
* Returns: pointer to parental context, or NULL
*/
struct fdisk_context *fdisk_get_parent(struct fdisk_context *cxt)
{
assert(cxt);
return cxt->parent;
}
static void reset_context(struct fdisk_context *cxt)
{
size_t i;
DBG(CXT, ul_debugobj(cxt, "*** resetting context"));
/* reset drives' private data */
for (i = 0; i < cxt->nlabels; i++)
fdisk_deinit_label(cxt->labels[i]);
if (cxt->parent) {
/* the first sector may be independent on parent */
if (cxt->parent->firstsector != cxt->firstsector) {
DBG(CXT, ul_debugobj(cxt, " firstsector independent on parent (freeing)"));
free(cxt->firstsector);
}
} else {
/* we close device only in primary context */
if (cxt->dev_fd > -1 && cxt->is_priv)
close(cxt->dev_fd);
DBG(CXT, ul_debugobj(cxt, " freeing firstsector"));
free(cxt->firstsector);
}
free(cxt->dev_path);
cxt->dev_path = NULL;
free(cxt->dev_model);
cxt->dev_model = NULL;
cxt->dev_model_probed = 0;
free(cxt->collision);
cxt->collision = NULL;
memset(&cxt->dev_st, 0, sizeof(cxt->dev_st));
cxt->dev_fd = -1;
cxt->is_priv = 0;
cxt->is_excl = 0;
cxt->firstsector = NULL;
cxt->firstsector_bufsz = 0;
fdisk_zeroize_device_properties(cxt);
fdisk_unref_script(cxt->script);
cxt->script = NULL;
cxt->label = NULL;
fdisk_free_wipe_areas(cxt);
}
/* fdisk_assign_device() body */
static int fdisk_assign_fd(struct fdisk_context *cxt, int fd,
const char *fname, int readonly,
int priv, int excl)
{
assert(cxt);
assert(fd >= 0);
errno = 0;
/* redirect request to parent */
if (cxt->parent) {
int rc, org = fdisk_is_listonly(cxt->parent);
/* assign_device() is sensitive to "listonly" mode, so let's
* follow the current context setting for the parent to avoid
* unwanted extra warnings. */
fdisk_enable_listonly(cxt->parent, fdisk_is_listonly(cxt));
rc = fdisk_assign_fd(cxt->parent, fd, fname, readonly, priv, excl);
fdisk_enable_listonly(cxt->parent, org);
if (!rc)
rc = init_nested_from_parent(cxt, 0);
if (!rc)
fdisk_probe_labels(cxt);
return rc;
}
reset_context(cxt);
if (fstat(fd, &cxt->dev_st) != 0)
goto fail;
cxt->readonly = readonly ? 1 : 0;
cxt->dev_fd = fd;
cxt->is_priv = priv ? 1 : 0;
cxt->is_excl = excl ? 1 : 0;
cxt->dev_path = fname ? strdup(fname) : NULL;
if (!cxt->dev_path)
goto fail;
fdisk_discover_topology(cxt);
fdisk_discover_geometry(cxt);
fdisk_apply_user_device_properties(cxt);
if (fdisk_read_firstsector(cxt) < 0)
goto fail;
/* warn about obsolete stuff on the device if we aren't in list-only */
if (!fdisk_is_listonly(cxt) && fdisk_check_collisions(cxt) < 0)
goto fail;
fdisk_probe_labels(cxt);
fdisk_apply_label_device_properties(cxt);
/* Don't report collision if there is already a valid partition table.
* The bootbits are wiped when we create a *new* partition table only. */
if (fdisk_is_ptcollision(cxt) && fdisk_has_label(cxt)) {
DBG(CXT, ul_debugobj(cxt, "ignore old %s", cxt->collision));
cxt->pt_collision = 0;
free(cxt->collision);
cxt->collision = NULL;
}
DBG(CXT, ul_debugobj(cxt, "initialized for %s [%s %s %s]",
fname,
cxt->readonly ? "READ-ONLY" : "READ-WRITE",
cxt->is_excl ? "EXCL" : "",
cxt->is_priv ? "PRIV" : ""));
return 0;
fail:
{
int rc = errno ? -errno : -EINVAL;
cxt->dev_fd = -1;
DBG(CXT, ul_debugobj(cxt, "failed to assign device [rc=%d]", rc));
return rc;
}
}
/**
* fdisk_assign_device:
* @cxt: context
* @fname: path to the device to be handled
* @readonly: how to open the device
*
* Open the device, discovery topology, geometry, detect disklabel, check for
* collisions and switch the current label driver to reflect the probing
* result.
*
* If in standard mode (!= non-listonly mode) then also detects for collisions.
* The result is accessible by fdisk_get_collision() and
* fdisk_is_ptcollision(). The collision (e.g. old obsolete PT) may be removed
* by fdisk_enable_wipe(). Note that new PT and old PT may be on different
* locations.
*
* Note that this function resets all generic setting in context.
*
* If the @cxt is nested context (necessary for example to edit BSD or PMBR)
* then the device is assigned to the parental context and necessary properties
* are copied to the @cxt. The change is propagated in child->parent direction
* only. It's impossible to use a different device for primary and nested
* contexts.
*
* Returns: 0 on success, < 0 on error.
*/
int fdisk_assign_device(struct fdisk_context *cxt,
const char *fname, int readonly)
{
int fd, rc, flags = O_CLOEXEC;
DBG(CXT, ul_debugobj(cxt, "assigning device %s", fname));
assert(cxt);
if (readonly)
flags |= O_RDONLY;
else
flags |= (O_RDWR | O_EXCL);
errno = 0;
fd = open(fname,flags);
if (fd < 0 && errno == EBUSY && (flags & O_EXCL)) {
flags &= ~O_EXCL;
errno = 0;
fd = open(fname, flags);
}
if (fd < 0) {
rc = -errno;
DBG(CXT, ul_debugobj(cxt, "failed to assign device [rc=%d]", rc));
return rc;
}
rc = fdisk_assign_fd(cxt, fd, fname, readonly, 1, flags & O_EXCL);
if (rc)
close(fd);
return rc;
}
/**
* fdisk_assign_device_by_fd:
* @cxt: context
* @fd: device file descriptor
* @fname: path to the device (used for dialogs, debugging, partition names, ...)
* @readonly: how to use the device
*
* Like fdisk_assign_device(), but caller is responsible to open and close the
* device. The library only fsync() the device on fdisk_deassign_device().
*
* The device has to be open O_RDWR on @readonly=0.
*
* Returns: 0 on success, < 0 on error.
*
* Since: 2.35
*/
int fdisk_assign_device_by_fd(struct fdisk_context *cxt, int fd,
const char *fname, int readonly)
{
DBG(CXT, ul_debugobj(cxt, "assign by fd"));
return fdisk_assign_fd(cxt, fd, fname, readonly, 0, 0);
}
/**
* fdisk_deassign_device:
* @cxt: context
* @nosync: disable sync() after close().
*
* Call fsync(), close() and than sync(), but for read-only handler close the
* device only. If the @cxt is nested context then the request is redirected to
* the parent.
*
* Returns: 0 on success, < 0 on error.
*/
int fdisk_deassign_device(struct fdisk_context *cxt, int nosync)
{
assert(cxt);
assert(cxt->dev_fd >= 0);
if (cxt->parent) {
int rc = fdisk_deassign_device(cxt->parent, nosync);
if (!rc)
rc = init_nested_from_parent(cxt, 0);
return rc;
}
DBG(CXT, ul_debugobj(cxt, "de-assigning device %s", cxt->dev_path));
if (cxt->readonly && cxt->is_priv)
close(cxt->dev_fd);
else {
if (fsync(cxt->dev_fd)) {
fdisk_warn(cxt, _("%s: fsync device failed"),
cxt->dev_path);
return -errno;
}
if (cxt->is_priv && close(cxt->dev_fd)) {
fdisk_warn(cxt, _("%s: close device failed"),
cxt->dev_path);
return -errno;
}
if (!nosync) {
fdisk_info(cxt, _("Syncing disks."));
sync();
}
}
free(cxt->dev_path);
cxt->dev_path = NULL;
cxt->dev_fd = -1;
cxt->is_priv = 0;
cxt->is_excl = 0;
return 0;
}
/**
* fdisk_reassign_device:
* @cxt: context
*
* This function is "hard reset" of the context and it does not write anything
* to the device. All in-memory changes associated with the context will be
* lost. It's recommended to use this function after some fatal problem when the
* context (and label specific driver) is in an undefined state.
*
* Returns: 0 on success, < 0 on error.
*/
int fdisk_reassign_device(struct fdisk_context *cxt)
{
char *devname;
int rdonly, rc, fd, priv, excl;
assert(cxt);
assert(cxt->dev_fd >= 0);
DBG(CXT, ul_debugobj(cxt, "re-assigning device %s", cxt->dev_path));
devname = strdup(cxt->dev_path);
if (!devname)
return -ENOMEM;
rdonly = cxt->readonly;
fd = cxt->dev_fd;
priv = cxt->is_priv;
excl = cxt->is_excl;
fdisk_deassign_device(cxt, 1);
if (priv)
/* reopen and assign */
rc = fdisk_assign_device(cxt, devname, rdonly);
else
/* assign only */
rc = fdisk_assign_fd(cxt, fd, devname, rdonly, priv, excl);
free(devname);
return rc;
}
/**
* fdisk_reread_partition_table:
* @cxt: context
*
* Force *kernel* to re-read partition table on block devices.
*
* Returns: 0 on success, < 0 in case of error.
*/
int fdisk_reread_partition_table(struct fdisk_context *cxt)
{
int i = 0;
assert(cxt);
assert(cxt->dev_fd >= 0);
if (!S_ISBLK(cxt->dev_st.st_mode))
return 0;
DBG(CXT, ul_debugobj(cxt, "calling re-read ioctl"));
sync();
#ifdef BLKRRPART
fdisk_info(cxt, _("Calling ioctl() to re-read partition table."));
i = ioctl(cxt->dev_fd, BLKRRPART);
#else
errno = ENOSYS;
i = 1;
#endif
if (i) {
fdisk_warn(cxt, _("Re-reading the partition table failed."));
fdisk_info(cxt, _(
"The kernel still uses the old table. The "
"new table will be used at the next reboot "
"or after you run partprobe(8) or partx(8)."));
return -errno;
}
return 0;
}
#ifdef __linux__
static inline int add_to_partitions_array(
struct fdisk_partition ***ary,
struct fdisk_partition *pa,
size_t *n, size_t nmax)
{
if (!*ary) {
*ary = calloc(nmax, sizeof(struct fdisk_partition *));
if (!*ary)
return -ENOMEM;
}
(*ary)[*n] = pa;
(*n)++;
return 0;
}
#endif
/**
* fdisk_reread_changes:
* @cxt: context
* @org: original layout (on disk)
*
* Like fdisk_reread_partition_table() but don't forces kernel re-read all
* partition table. The BLKPG_* ioctls are used for individual partitions. The
* advantage is that unmodified partitions maybe mounted.
*
* The function behaves like fdisk_reread_partition_table() on systems where
* are no available BLKPG_* ioctls.
*
* Returns: <0 on error, or 0.
*/
#ifdef __linux__
int fdisk_reread_changes(struct fdisk_context *cxt, struct fdisk_table *org)
{
struct fdisk_table *tb = NULL;
struct fdisk_iter itr;
struct fdisk_partition *pa;
struct fdisk_partition **rem = NULL, **add = NULL, **upd = NULL;
int change, rc = 0, err = 0;
size_t nparts, i, nadds = 0, nupds = 0, nrems = 0;
unsigned int ssf;
DBG(CXT, ul_debugobj(cxt, "rereading changes"));
fdisk_reset_iter(&itr, FDISK_ITER_FORWARD);
/* the current layout */
fdisk_get_partitions(cxt, &tb);
/* maximal number of partitions */
nparts = max(fdisk_table_get_nents(tb), fdisk_table_get_nents(org));
while (fdisk_diff_tables(org, tb, &itr, &pa, &change) == 0) {
if (change == FDISK_DIFF_UNCHANGED)
continue;
switch (change) {
case FDISK_DIFF_REMOVED:
rc = add_to_partitions_array(&rem, pa, &nrems, nparts);
break;
case FDISK_DIFF_ADDED:
rc = add_to_partitions_array(&add, pa, &nadds, nparts);
break;
case FDISK_DIFF_RESIZED:
rc = add_to_partitions_array(&upd, pa, &nupds, nparts);
break;
case FDISK_DIFF_MOVED:
rc = add_to_partitions_array(&rem, pa, &nrems, nparts);
if (!rc)
rc = add_to_partitions_array(&add, pa, &nadds, nparts);
break;
}
if (rc != 0)
goto done;
}
/* sector size factor -- used to recount from real to 512-byte sectors */
ssf = cxt->sector_size / 512;
for (i = 0; i < nrems; i++) {
pa = rem[i];
DBG(PART, ul_debugobj(pa, "#%zu calling BLKPG_DEL_PARTITION", pa->partno));
if (partx_del_partition(cxt->dev_fd, pa->partno + 1) != 0) {
fdisk_warn(cxt, _("Failed to remove partition %zu from system"), pa->partno + 1);
err++;
}
}
for (i = 0; i < nupds; i++) {
pa = upd[i];
DBG(PART, ul_debugobj(pa, "#%zu calling BLKPG_RESIZE_PARTITION", pa->partno));
if (partx_resize_partition(cxt->dev_fd, pa->partno + 1,
pa->start * ssf, pa->size * ssf) != 0) {
fdisk_warn(cxt, _("Failed to update system information about partition %zu"), pa->partno + 1);
err++;
}
}
for (i = 0; i < nadds; i++) {
uint64_t sz;
pa = add[i];
sz = pa->size * ssf;
DBG(PART, ul_debugobj(pa, "#%zu calling BLKPG_ADD_PARTITION", pa->partno));
if (fdisk_is_label(cxt, DOS) && fdisk_partition_is_container(pa))
/* Let's follow the Linux kernel and reduce
* DOS extended partition to 1 or 2 sectors.
*/
sz = min(sz, (uint64_t) 2);
if (partx_add_partition(cxt->dev_fd, pa->partno + 1,
pa->start * ssf, sz) != 0) {
fdisk_warn(cxt, _("Failed to add partition %zu to system"), pa->partno + 1);
err++;
}
}
if (err)
fdisk_info(cxt, _(
"The kernel still uses the old partitions. The new "
"table will be used at the next reboot. "));
done:
free(rem);
free(add);
free(upd);
fdisk_unref_table(tb);
return rc;
}
#else
int fdisk_reread_changes(struct fdisk_context *cxt,