Debian Codegraph @

libfdisk/src/fdiskP.h

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