]> git.ipfire.org Git - thirdparty/util-linux.git/blob - libfdisk/src/fdiskP.h
libfdisk: add comment to fdisk_set_first_lba()
[thirdparty/util-linux.git] / libfdisk / src / fdiskP.h
1 /*
2 * fdiskP.h - private library header file
3 *
4 * Copyright (C) 2012 Karel Zak <kzak@redhat.com>
5 *
6 * This file may be redistributed under the terms of the
7 * GNU Lesser General Public License.
8 */
9
10 #ifndef _LIBFDISK_PRIVATE_H
11 #define _LIBFDISK_PRIVATE_H
12
13 #include <errno.h>
14 #include <stdlib.h>
15 #include <string.h>
16 #include <sys/stat.h>
17 #include <sys/types.h>
18 #include <unistd.h>
19 #include <uuid.h>
20
21 #include "c.h"
22 #include "libfdisk.h"
23
24 #include "list.h"
25 #include "debug.h"
26 #include <stdio.h>
27 #include <stdarg.h>
28
29 /*
30 * Debug
31 */
32 #define LIBFDISK_DEBUG_HELP (1 << 0)
33 #define LIBFDISK_DEBUG_INIT (1 << 1)
34 #define LIBFDISK_DEBUG_CXT (1 << 2)
35 #define LIBFDISK_DEBUG_LABEL (1 << 3)
36 #define LIBFDISK_DEBUG_ASK (1 << 4)
37 #define LIBFDISK_DEBUG_PART (1 << 6)
38 #define LIBFDISK_DEBUG_PARTTYPE (1 << 7)
39 #define LIBFDISK_DEBUG_TAB (1 << 8)
40 #define LIBFDISK_DEBUG_SCRIPT (1 << 9)
41 #define LIBFDISK_DEBUG_WIPE (1 << 10)
42 #define LIBFDISK_DEBUG_ITEM (1 << 11)
43 #define LIBFDISK_DEBUG_ALL 0xFFFF
44
45 UL_DEBUG_DECLARE_MASK(libfdisk);
46 #define DBG(m, x) __UL_DBG(libfdisk, LIBFDISK_DEBUG_, m, x)
47 #define ON_DBG(m, x) __UL_DBG_CALL(libfdisk, LIBFDISK_DEBUG_, m, x)
48 #define DBG_FLUSH __UL_DBG_FLUSH(libfdisk, LIBFDISK_DEBUG_)
49
50 #define UL_DEBUG_CURRENT_MASK UL_DEBUG_MASK(libfdisk)
51 #include "debugobj.h"
52
53 /*
54 * NLS -- the library has to be independent on main program, so define
55 * UL_TEXTDOMAIN_EXPLICIT before you include nls.h.
56 *
57 * Now we use util-linux.po (=PACKAGE), rather than maintain the texts
58 * in the separate libfdisk.po file.
59 */
60 #define LIBFDISK_TEXTDOMAIN PACKAGE
61 #define UL_TEXTDOMAIN_EXPLICIT LIBFDISK_TEXTDOMAIN
62 #include "nls.h"
63
64
65 #ifdef TEST_PROGRAM
66 struct fdisk_test {
67 const char *name;
68 int (*body)(struct fdisk_test *ts, int argc, char *argv[]);
69 const char *usage;
70 };
71
72 /* test.c */
73 extern int fdisk_run_test(struct fdisk_test *tests, int argc, char *argv[]);
74 #endif
75
76 #define FDISK_GPT_NPARTITIONS_DEFAULT 128
77
78 /*
79 * Generic iterator
80 */
81 struct fdisk_iter {
82 struct list_head *p; /* current position */
83 struct list_head *head; /* start position */
84 int direction; /* FDISK_ITER_{FOR,BACK}WARD */
85 };
86
87 #define IS_ITER_FORWARD(_i) ((_i)->direction == FDISK_ITER_FORWARD)
88 #define IS_ITER_BACKWARD(_i) ((_i)->direction == FDISK_ITER_BACKWARD)
89
90 #define FDISK_ITER_INIT(itr, list) \
91 do { \
92 (itr)->p = IS_ITER_FORWARD(itr) ? \
93 (list)->next : (list)->prev; \
94 (itr)->head = (list); \
95 } while(0)
96
97 #define FDISK_ITER_ITERATE(itr, res, restype, member) \
98 do { \
99 res = list_entry((itr)->p, restype, member); \
100 (itr)->p = IS_ITER_FORWARD(itr) ? \
101 (itr)->p->next : (itr)->p->prev; \
102 } while(0)
103
104 /*
105 * Partition types
106 */
107 struct fdisk_parttype {
108 unsigned int code; /* type as number or zero */
109 char *name; /* description */
110 char *typestr; /* type as string or NULL */
111
112 unsigned int flags; /* FDISK_PARTTYPE_* flags */
113 int refcount; /* reference counter for allocated types */
114 };
115
116 enum {
117 FDISK_PARTTYPE_UNKNOWN = (1 << 1),
118 FDISK_PARTTYPE_INVISIBLE = (1 << 2),
119 FDISK_PARTTYPE_ALLOCATED = (1 << 3)
120 };
121
122 #define fdisk_parttype_is_invisible(_x) ((_x) && ((_x)->flags & FDISK_PARTTYPE_INVISIBLE))
123 #define fdisk_parttype_is_allocated(_x) ((_x) && ((_x)->flags & FDISK_PARTTYPE_ALLOCATED))
124
125 struct fdisk_partition {
126 int refcount; /* reference counter */
127
128 size_t partno; /* partition number */
129 size_t parent_partno; /* for logical partitions */
130
131 fdisk_sector_t start; /* first sectors */
132 fdisk_sector_t size; /* size in sectors */
133
134 int movestart; /* FDISK_MOVE_* (scripts only) */
135 int resize; /* FDISK_RESIZE_* (scripts only) */
136
137 char *name; /* partition name */
138 char *uuid; /* partition UUID */
139 char *attrs; /* partition flags/attributes converted to string */
140 struct fdisk_parttype *type; /* partition type */
141
142 char *fstype; /* filesystem type */
143 char *fsuuid; /* filesystem uuid */
144 char *fslabel; /* filesystem label */
145
146 struct list_head parts; /* list of partitions */
147
148 /* extra fields for partition_to_string() */
149 char start_post; /* start postfix (e.g. '+') */
150 char end_post; /* end postfix */
151 char size_post; /* size postfix */
152
153 uint64_t fsize; /* bsd junk */
154 uint64_t bsize;
155 uint64_t cpg;
156
157 char *start_chs; /* start C/H/S in string */
158 char *end_chs; /* end C/H/S in string */
159
160 unsigned int boot; /* MBR: bootable */
161
162 unsigned int container : 1, /* container partition (e.g. extended partition) */
163 end_follow_default : 1, /* use default end */
164 freespace : 1, /* this is free space */
165 partno_follow_default : 1, /* use default partno */
166 size_explicit : 1, /* don't align the size */
167 start_follow_default : 1, /* use default start */
168 fs_probed : 1, /* already probed by blkid */
169 used : 1, /* partition already used */
170 wholedisk : 1; /* special system partition */
171 };
172
173 enum {
174 FDISK_MOVE_NONE = 0,
175 FDISK_MOVE_DOWN = -1,
176 FDISK_MOVE_UP = 1
177 };
178
179 enum {
180 FDISK_RESIZE_NONE = 0,
181 FDISK_RESIZE_REDUCE = -1,
182 FDISK_RESIZE_ENLARGE = 1
183 };
184
185 #define FDISK_INIT_UNDEF(_x) ((_x) = (__typeof__(_x)) -1)
186 #define FDISK_IS_UNDEF(_x) ((_x) == (__typeof__(_x)) -1)
187
188 struct fdisk_table {
189 struct list_head parts; /* partitions */
190 int refcount;
191 size_t nents; /* number of partitions */
192 };
193
194 /*
195 * Legacy CHS based geometry
196 */
197 struct fdisk_geometry {
198 unsigned int heads;
199 fdisk_sector_t sectors;
200 fdisk_sector_t cylinders;
201 };
202
203 /*
204 * Label specific operations
205 */
206 struct fdisk_label_operations {
207 /* probe disk label */
208 int (*probe)(struct fdisk_context *cxt);
209 /* write in-memory changes to disk */
210 int (*write)(struct fdisk_context *cxt);
211 /* verify the partition table */
212 int (*verify)(struct fdisk_context *cxt);
213 /* create new disk label */
214 int (*create)(struct fdisk_context *cxt);
215 /* returns offset and size of the 'n' part of the PT */
216 int (*locate)(struct fdisk_context *cxt, int n, const char **name,
217 uint64_t *offset, size_t *size);
218 /* reorder partitions */
219 int (*reorder)(struct fdisk_context *cxt);
220 /* get details from label */
221 int (*get_item)(struct fdisk_context *cxt, struct fdisk_labelitem *item);
222 /* set disk label ID */
223 int (*set_id)(struct fdisk_context *cxt);
224
225
226 /* new partition */
227 int (*add_part)(struct fdisk_context *cxt, struct fdisk_partition *pa,
228 size_t *partno);
229 /* delete partition */
230 int (*del_part)(struct fdisk_context *cxt, size_t partnum);
231
232 /* fill in partition struct */
233 int (*get_part)(struct fdisk_context *cxt, size_t n,
234 struct fdisk_partition *pa);
235 /* modify partition */
236 int (*set_part)(struct fdisk_context *cxt, size_t n,
237 struct fdisk_partition *pa);
238
239 /* return state of the partition */
240 int (*part_is_used)(struct fdisk_context *cxt, size_t partnum);
241
242 int (*part_toggle_flag)(struct fdisk_context *cxt, size_t i, unsigned long flag);
243
244 /* refresh alignment setting */
245 int (*reset_alignment)(struct fdisk_context *cxt);
246
247 /* free in-memory label stuff */
248 void (*free)(struct fdisk_label *lb);
249
250 /* deinit in-memory label stuff */
251 void (*deinit)(struct fdisk_label *lb);
252 };
253
254 /*
255 * The fields describes how to display libfdisk_partition
256 */
257 struct fdisk_field {
258 int id; /* FDISK_FIELD_* */
259 const char *name; /* field name */
260 double width; /* field width (compatible with libsmartcols whint) */
261 int flags; /* FDISK_FIELDFL_* */
262 };
263
264 /* note that the defaults is to display a column always */
265 enum {
266 FDISK_FIELDFL_DETAIL = (1 << 1), /* only display if fdisk_is_details() */
267 FDISK_FIELDFL_EYECANDY = (1 << 2), /* don't display if fdisk_is_details() */
268 FDISK_FIELDFL_NUMBER = (1 << 3), /* column display numbers */
269 };
270
271 /*
272 * Generic label
273 */
274 struct fdisk_label {
275 const char *name; /* label name */
276 enum fdisk_labeltype id; /* FDISK_DISKLABEL_* */
277 struct fdisk_parttype *parttypes; /* supported partitions types */
278 size_t nparttypes; /* number of items in parttypes[] */
279
280 size_t nparts_max; /* maximal number of partitions */
281 size_t nparts_cur; /* number of currently used partitions */
282
283 int flags; /* FDISK_LABEL_FL_* flags */
284
285 struct fdisk_geometry geom_min; /* minimal geometry */
286 struct fdisk_geometry geom_max; /* maximal geometry */
287
288 unsigned int changed:1, /* label has been modified */
289 disabled:1; /* this driver is disabled at all */
290
291 const struct fdisk_field *fields; /* all possible fields */
292 size_t nfields;
293
294 const struct fdisk_label_operations *op;
295 };
296
297
298 /* label driver flags */
299 enum {
300 FDISK_LABEL_FL_REQUIRE_GEOMETRY = (1 << 2),
301 FDISK_LABEL_FL_INCHARS_PARTNO = (1 << 3)
302 };
303
304 /* label allocators */
305 extern struct fdisk_label *fdisk_new_gpt_label(struct fdisk_context *cxt);
306 extern struct fdisk_label *fdisk_new_dos_label(struct fdisk_context *cxt);
307 extern struct fdisk_label *fdisk_new_bsd_label(struct fdisk_context *cxt);
308 extern struct fdisk_label *fdisk_new_sgi_label(struct fdisk_context *cxt);
309 extern struct fdisk_label *fdisk_new_sun_label(struct fdisk_context *cxt);
310
311
312 struct ask_menuitem {
313 char key;
314 const char *name;
315 const char *desc;
316
317 struct ask_menuitem *next;
318 };
319
320 /* fdisk dialog -- note that nothing from this stuff will be directly exported,
321 * we will have get/set() function for everything.
322 */
323 struct fdisk_ask {
324 int type; /* FDISK_ASKTYPE_* */
325 char *query;
326
327 int refcount;
328
329 union {
330 /* FDISK_ASKTYPE_{NUMBER,OFFSET} */
331 struct ask_number {
332 uint64_t hig; /* high limit */
333 uint64_t low; /* low limit */
334 uint64_t dfl; /* default */
335 uint64_t result;
336 uint64_t base; /* for relative results */
337 uint64_t unit; /* unit for offsets */
338 const char *range; /* by library generated list */
339 unsigned int relative :1,
340 inchars :1,
341 wrap_negative :1;
342 } num;
343 /* FDISK_ASKTYPE_{WARN,WARNX,..} */
344 struct ask_print {
345 const char *mesg;
346 int errnum; /* errno */
347 } print;
348 /* FDISK_ASKTYPE_YESNO */
349 struct ask_yesno {
350 int result; /* TRUE or FALSE */
351 } yesno;
352 /* FDISK_ASKTYPE_STRING */
353 struct ask_string {
354 char *result; /* allocated */
355 } str;
356 /* FDISK_ASKTYPE_MENU */
357 struct ask_menu {
358 int dfl; /* default menu item */
359 int result;
360 struct ask_menuitem *first;
361 } menu;
362 } data;
363 };
364
365 struct fdisk_context {
366 int dev_fd; /* device descriptor */
367 char *dev_path; /* device path */
368 char *dev_model; /* on linux /sys/block/<name>/device/model or NULL */
369 struct stat dev_st; /* stat(2) result */
370
371 int refcount;
372
373 unsigned char *firstsector; /* buffer with master boot record */
374 unsigned long firstsector_bufsz;
375
376
377 /* topology */
378 unsigned long io_size; /* I/O size used by fdisk */
379 unsigned long optimal_io_size; /* optional I/O returned by device */
380 unsigned long min_io_size; /* minimal I/O size */
381 unsigned long phy_sector_size; /* physical size */
382 unsigned long sector_size; /* logical size */
383 unsigned long alignment_offset;
384
385 unsigned int readonly : 1, /* don't write to the device */
386 display_in_cyl_units : 1, /* for obscure labels */
387 display_details : 1, /* expert display mode */
388 protect_bootbits : 1, /* don't zeroize first sector */
389 pt_collision : 1, /* another PT detected by libblkid */
390 no_disalogs : 1, /* disable dialog-driven partititoning */
391 dev_model_probed : 1, /* tried to read from sys */
392 listonly : 1; /* list partition, nothing else */
393
394 char *collision; /* name of already existing FS/PT */
395 struct list_head wipes; /* list of areas to wipe before write */
396
397 int sizeunit; /* SIZE fields, FDISK_SIZEUNIT_* */
398
399 /* alignment */
400 unsigned long grain; /* alignment unit */
401 fdisk_sector_t first_lba; /* recommended begin of the first partition */
402 fdisk_sector_t last_lba; /* recommended end of last partition */
403
404 /* geometry */
405 fdisk_sector_t total_sectors; /* in logical sectors */
406 struct fdisk_geometry geom;
407
408 /* user setting to overwrite device default */
409 struct fdisk_geometry user_geom;
410 unsigned long user_pyh_sector;
411 unsigned long user_log_sector;
412 unsigned long user_grain;
413
414 struct fdisk_label *label; /* current label, pointer to labels[] */
415
416 size_t nlabels; /* number of initialized label drivers */
417 struct fdisk_label *labels[8]; /* all supported labels,
418 * FIXME: use any enum rather than hardcoded number */
419
420 int (*ask_cb)(struct fdisk_context *, struct fdisk_ask *, void *); /* fdisk dialogs callback */
421 void *ask_data; /* ask_cb() data */
422
423 struct fdisk_context *parent; /* for nested PT */
424 struct fdisk_script *script; /* what we want to follow */
425 };
426
427 /* table */
428 enum {
429 FDISK_DIFF_UNCHANGED = 0,
430 FDISK_DIFF_REMOVED,
431 FDISK_DIFF_ADDED,
432 FDISK_DIFF_MOVED,
433 FDISK_DIFF_RESIZED
434 };
435 extern int fdisk_diff_tables(struct fdisk_table *a, struct fdisk_table *b,
436 struct fdisk_iter *itr,
437 struct fdisk_partition **res, int *change);
438 extern void fdisk_debug_print_table(struct fdisk_table *tb);
439
440
441 /* context.c */
442 extern int __fdisk_switch_label(struct fdisk_context *cxt,
443 struct fdisk_label *lb);
444 extern int fdisk_missing_geometry(struct fdisk_context *cxt);
445
446 /* alignment.c */
447 fdisk_sector_t fdisk_scround(struct fdisk_context *cxt, fdisk_sector_t num);
448 fdisk_sector_t fdisk_cround(struct fdisk_context *cxt, fdisk_sector_t num);
449
450 extern int fdisk_discover_geometry(struct fdisk_context *cxt);
451 extern int fdisk_discover_topology(struct fdisk_context *cxt);
452
453 extern int fdisk_has_user_device_geometry(struct fdisk_context *cxt);
454 extern int fdisk_apply_user_device_properties(struct fdisk_context *cxt);
455 extern int fdisk_apply_label_device_properties(struct fdisk_context *cxt);
456 extern void fdisk_zeroize_device_properties(struct fdisk_context *cxt);
457
458 /* utils.c */
459 extern int fdisk_init_firstsector_buffer(struct fdisk_context *cxt,
460 unsigned int protect_off, unsigned int protect_size);
461 extern int fdisk_read_firstsector(struct fdisk_context *cxt);
462
463 /* label.c */
464 extern int fdisk_probe_labels(struct fdisk_context *cxt);
465 extern void fdisk_deinit_label(struct fdisk_label *lb);
466
467 struct fdisk_labelitem {
468 int refcount; /* reference counter */
469 int id; /* <label>_ITEM_* */
470 char type; /* s = string, j = uint64 */
471 const char *name; /* human readable name */
472
473 union {
474 char *str;
475 uint64_t num64;
476 } data;
477 };
478
479 /* Use only internally for non-allocated items, never use
480 * refcouting for such items!
481 */
482 #define FDISK_LABELITEM_INIT { .type = 0, .refcount = 0 }
483
484 /* ask.c */
485 struct fdisk_ask *fdisk_new_ask(void);
486 void fdisk_reset_ask(struct fdisk_ask *ask);
487 int fdisk_ask_set_query(struct fdisk_ask *ask, const char *str);
488 int fdisk_ask_set_type(struct fdisk_ask *ask, int type);
489 int fdisk_do_ask(struct fdisk_context *cxt, struct fdisk_ask *ask);
490 int fdisk_ask_number_set_range(struct fdisk_ask *ask, const char *range);
491 int fdisk_ask_number_set_default(struct fdisk_ask *ask, uint64_t dflt);
492 int fdisk_ask_number_set_low(struct fdisk_ask *ask, uint64_t low);
493 int fdisk_ask_number_set_high(struct fdisk_ask *ask, uint64_t high);
494 int fdisk_ask_number_set_base(struct fdisk_ask *ask, uint64_t base);
495 int fdisk_ask_number_set_unit(struct fdisk_ask *ask, uint64_t unit);
496 int fdisk_ask_number_is_relative(struct fdisk_ask *ask);
497 int fdisk_ask_number_set_wrap_negative(struct fdisk_ask *ask, int wrap_negative);
498 int fdisk_ask_menu_set_default(struct fdisk_ask *ask, int dfl);
499 int fdisk_ask_menu_add_item(struct fdisk_ask *ask, int key,
500 const char *name, const char *desc);
501 int fdisk_ask_print_set_errno(struct fdisk_ask *ask, int errnum);
502 int fdisk_ask_print_set_mesg(struct fdisk_ask *ask, const char *mesg);
503 int fdisk_info_new_partition(
504 struct fdisk_context *cxt,
505 int num, fdisk_sector_t start, fdisk_sector_t stop,
506 struct fdisk_parttype *t);
507
508 /* dos.c */
509 extern struct dos_partition *fdisk_dos_get_partition(
510 struct fdisk_context *cxt,
511 size_t i);
512
513 /* wipe.c */
514 void fdisk_free_wipe_areas(struct fdisk_context *cxt);
515 int fdisk_set_wipe_area(struct fdisk_context *cxt, uint64_t start, uint64_t size, int enable);
516 int fdisk_do_wipe(struct fdisk_context *cxt);
517 int fdisk_has_wipe_area(struct fdisk_context *cxt, uint64_t start, uint64_t size);
518 int fdisk_check_collisions(struct fdisk_context *cxt);
519
520 #endif /* _LIBFDISK_PRIVATE_H */