| 1 |
#ifdef HAVE_LIBBLKID |
| 2 |
# include <blkid.h> |
| 3 |
#endif |
| 4 |
|
| 5 |
#include "cctype.h" |
| 6 |
#include "blkdev.h" |
| 7 |
#ifdef __linux__ |
| 8 |
# include "partx.h" |
| 9 |
#endif |
| 10 |
#include "loopdev.h" |
| 11 |
#include "fdiskP.h" |
| 12 |
|
| 13 |
#include "strutils.h" |
| 14 |
|
| 15 |
/** |
| 16 |
* SECTION: context |
| 17 |
* @title: Context |
| 18 |
* @short_description: stores info about device, labels etc. |
| 19 |
* |
| 20 |
* The library distinguish between three types of partitioning objects. |
| 21 |
* |
| 22 |
* on-disk label data |
| 23 |
* - disk label specific |
| 24 |
* - probed and read by disklabel drivers when assign device to the context |
| 25 |
* or when switch to another disk label type |
| 26 |
* - only fdisk_write_disklabel() modify on-disk data |
| 27 |
* |
| 28 |
* in-memory label data |
| 29 |
* - generic data and disklabel specific data stored in struct fdisk_label |
| 30 |
* - all partitioning operations are based on in-memory data only |
| 31 |
* |
| 32 |
* struct fdisk_partition |
| 33 |
* - provides abstraction to present partitions to users |
| 34 |
* - fdisk_partition is possible to gather to fdisk_table container |
| 35 |
* - used as unified template for new partitions |
| 36 |
* - used (with fdisk_table) in fdisk scripts |
| 37 |
* - the struct fdisk_partition is always completely independent object and |
| 38 |
* any change to the object has no effect to in-memory (or on-disk) label data |
| 39 |
* |
| 40 |
* Don't forget to inform kernel about changes by fdisk_reread_partition_table() |
| 41 |
* or more smart fdisk_reread_changes(). |
| 42 |
*/ |
| 43 |
|
| 44 |
/** |
| 45 |
* fdisk_new_context: |
| 46 |
* |
| 47 |
* Returns: newly allocated libfdisk handler |
| 48 |
*/ |
| 49 |
struct fdisk_context *fdisk_new_context(void) |
| 50 |
{ |
| 51 |
struct fdisk_context *cxt; |
| 52 |
|
| 53 |
cxt = calloc(1, sizeof(*cxt)); |
| 54 |
if (!cxt) |
| 55 |
return NULL; |
| 56 |
|
| 57 |
DBG(CXT, ul_debugobj(cxt, "alloc")); |
| 58 |
cxt->dev_fd = -1; |
| 59 |
cxt->refcount = 1; |
| 60 |
|
| 61 |
INIT_LIST_HEAD(&cxt->wipes); |
| 62 |
|
| 63 |
/* |
| 64 |
* Allocate label specific structs. |
| 65 |
* |
| 66 |
* This is necessary (for example) to store label specific |
| 67 |
* context setting. |
| 68 |
*/ |
| 69 |
cxt->labels[ cxt->nlabels++ ] = fdisk_new_gpt_label(cxt); |
| 70 |
cxt->labels[ cxt->nlabels++ ] = fdisk_new_dos_label(cxt); |
| 71 |
cxt->labels[ cxt->nlabels++ ] = fdisk_new_bsd_label(cxt); |
| 72 |
cxt->labels[ cxt->nlabels++ ] = fdisk_new_sgi_label(cxt); |
| 73 |
cxt->labels[ cxt->nlabels++ ] = fdisk_new_sun_label(cxt); |
| 74 |
|
| 75 |
bindtextdomain(LIBFDISK_TEXTDOMAIN, LOCALEDIR); |
| 76 |
|
| 77 |
return cxt; |
| 78 |
} |
| 79 |
|
| 80 |
static int init_nested_from_parent(struct fdisk_context *cxt, int isnew) |
| 81 |
{ |
| 82 |
struct fdisk_context *parent; |
| 83 |
|
| 84 |
assert(cxt); |
| 85 |
assert(cxt->parent); |
| 86 |
|
| 87 |
parent = cxt->parent; |
| 88 |
|
| 89 |
INIT_LIST_HEAD(&cxt->wipes); |
| 90 |
|
| 91 |
cxt->alignment_offset = parent->alignment_offset; |
| 92 |
cxt->ask_cb = parent->ask_cb; |
| 93 |
cxt->ask_data = parent->ask_data; |
| 94 |
cxt->dev_fd = parent->dev_fd; |
| 95 |
cxt->first_lba = parent->first_lba; |
| 96 |
cxt->firstsector_bufsz = parent->firstsector_bufsz; |
| 97 |
cxt->firstsector = parent->firstsector; |
| 98 |
cxt->geom = parent->geom; |
| 99 |
cxt->grain = parent->grain; |
| 100 |
cxt->io_size = parent->io_size; |
| 101 |
cxt->last_lba = parent->last_lba; |
| 102 |
cxt->min_io_size = parent->min_io_size; |
| 103 |
cxt->optimal_io_size = parent->optimal_io_size; |
| 104 |
cxt->phy_sector_size = parent->phy_sector_size; |
| 105 |
cxt->readonly = parent->readonly; |
| 106 |
cxt->script = parent->script; |
| 107 |
fdisk_ref_script(cxt->script); |
| 108 |
cxt->sector_size = parent->sector_size; |
| 109 |
cxt->total_sectors = parent->total_sectors; |
| 110 |
cxt->user_geom = parent->user_geom; |
| 111 |
cxt->user_log_sector = parent->user_log_sector; |
| 112 |
cxt->user_pyh_sector = parent->user_pyh_sector; |
| 113 |
|
| 114 |
/* parent <--> nested independent setting, initialize for new nested |
| 115 |
* contexts only */ |
| 116 |
if (isnew) { |
| 117 |
cxt->listonly = parent->listonly; |
| 118 |
cxt->display_details = parent->display_details; |
| 119 |
cxt->display_in_cyl_units = parent->display_in_cyl_units; |
| 120 |
cxt->protect_bootbits = parent->protect_bootbits; |
| 121 |
} |
| 122 |
|
| 123 |
free(cxt->dev_model); |
| 124 |
cxt->dev_model = NULL; |
| 125 |
cxt->dev_model_probed = 0; |
| 126 |
|
| 127 |
return strdup_between_structs(cxt, parent, dev_path); |
| 128 |
} |
| 129 |
|
| 130 |
/** |
| 131 |
* fdisk_new_nested_context: |
| 132 |
* @parent: parental context |
| 133 |
* @name: optional label name (e.g. "bsd") |
| 134 |
* |
| 135 |
* Create a new nested fdisk context for nested disk labels (e.g. BSD or PMBR). |
| 136 |
* The function also probes for the nested label on the device if device is |
| 137 |
* already assigned to parent. |
| 138 |
* |
| 139 |
* The new context is initialized according to @parent and both context shares |
| 140 |
* some settings and file descriptor to the device. The child propagate some |
| 141 |
* changes (like fdisk_assign_device()) to parent, but it does not work |
| 142 |
* vice-versa. The behavior is undefined if you assign another device to |
| 143 |
* parent. |
| 144 |
* |
| 145 |
* Returns: new context for nested partition table. |
| 146 |
*/ |
| 147 |
struct fdisk_context *fdisk_new_nested_context(struct fdisk_context *parent, |
| 148 |
const char *name) |
| 149 |
{ |
| 150 |
struct fdisk_context *cxt; |
| 151 |
struct fdisk_label *lb = NULL; |
| 152 |
|
| 153 |
assert(parent); |
| 154 |
|
| 155 |
cxt = calloc(1, sizeof(*cxt)); |
| 156 |
if (!cxt) |
| 157 |
return NULL; |
| 158 |
|
| 159 |
DBG(CXT, ul_debugobj(parent, "alloc nested [%p] [name=%s]", cxt, name)); |
| 160 |
cxt->refcount = 1; |
| 161 |
|
| 162 |
fdisk_ref_context(parent); |
| 163 |
cxt->parent = parent; |
| 164 |
|
| 165 |
if (init_nested_from_parent(cxt, 1) != 0) { |
| 166 |
cxt->parent = NULL; |
| 167 |
fdisk_unref_context(cxt); |
| 168 |
return NULL; |
| 169 |
} |
| 170 |
|
| 171 |
if (name) { |
| 172 |
if (c_strcasecmp(name, "bsd") == 0) |
| 173 |
lb = cxt->labels[ cxt->nlabels++ ] = fdisk_new_bsd_label(cxt); |
| 174 |
else if (c_strcasecmp(name, "dos") == 0 || c_strcasecmp(name, "mbr") == 0) |
| 175 |
lb = cxt->labels[ cxt->nlabels++ ] = fdisk_new_dos_label(cxt); |
| 176 |
} |
| 177 |
|
| 178 |
if (lb && parent->dev_fd >= 0) { |
| 179 |
DBG(CXT, ul_debugobj(cxt, "probing for nested %s", lb->name)); |
| 180 |
|
| 181 |
cxt->label = lb; |
| 182 |
|
| 183 |
if (lb->op->probe(cxt) == 1) |
| 184 |
__fdisk_switch_label(cxt, lb); |
| 185 |
else { |
| 186 |
DBG(CXT, ul_debugobj(cxt, "not found %s label", lb->name)); |
| 187 |
if (lb->op->deinit) |
| 188 |
lb->op->deinit(lb); |
| 189 |
cxt->label = NULL; |
| 190 |
} |
| 191 |
} |
| 192 |
|
| 193 |
return cxt; |
| 194 |
} |
| 195 |
|
| 196 |
|
| 197 |
/** |
| 198 |
* fdisk_ref_context: |
| 199 |
* @cxt: context pointer |
| 200 |
* |
| 201 |
* Increments reference counter. |
| 202 |
*/ |
| 203 |
void fdisk_ref_context(struct fdisk_context *cxt) |
| 204 |
{ |
| 205 |
if (cxt) |
| 206 |
cxt->refcount++; |
| 207 |
} |
| 208 |
|
| 209 |
/** |
| 210 |
* fdisk_get_label: |
| 211 |
* @cxt: context instance |
| 212 |
* @name: label name (e.g. "gpt") |
| 213 |
* |
| 214 |
* If no @name specified then returns the current context label. |
| 215 |
* |
| 216 |
* The label is allocated and maintained within the context #cxt. There is |
| 217 |
* nothing like reference counting for labels, you cannot deallocate the |
| 218 |
* label. |
| 219 |
* |
| 220 |
* Returns: label struct or NULL in case of error. |
| 221 |
*/ |
| 222 |
struct fdisk_label *fdisk_get_label(struct fdisk_context *cxt, const char *name) |
| 223 |
{ |
| 224 |
size_t i; |
| 225 |
|
| 226 |
assert(cxt); |
| 227 |
|
| 228 |
if (!name) |
| 229 |
return cxt->label; |
| 230 |
|
| 231 |
if (c_strcasecmp(name, "mbr") == 0) |
| 232 |
name = "dos"; |
| 233 |
|
| 234 |
for (i = 0; i < cxt->nlabels; i++) |
| 235 |
if (cxt->labels[i] |
| 236 |
&& c_strcasecmp(cxt->labels[i]->name, name) == 0) |
| 237 |
return cxt->labels[i]; |
| 238 |
|
| 239 |
DBG(CXT, ul_debugobj(cxt, "failed to found %s label driver", name)); |
| 240 |
return NULL; |
| 241 |
} |
| 242 |
|
| 243 |
/** |
| 244 |
* fdisk_next_label: |
| 245 |
* @cxt: context instance |
| 246 |
* @lb: returns pointer to the next label |
| 247 |
* |
| 248 |
* <informalexample> |
| 249 |
* <programlisting> |
| 250 |
* // print all supported labels |
| 251 |
* struct fdisk_context *cxt = fdisk_new_context(); |
| 252 |
* struct fdisk_label *lb = NULL; |
| 253 |
* |
| 254 |
* while (fdisk_next_label(cxt, &lb) == 0) |
| 255 |
* print("label name: %s\n", fdisk_label_get_name(lb)); |
| 256 |
* fdisk_unref_context(cxt); |
| 257 |
* </programlisting> |
| 258 |
* </informalexample> |
| 259 |
* |
| 260 |
* Returns: <0 in case of error, 0 on success, 1 at the end. |
| 261 |
*/ |
| 262 |
int fdisk_next_label(struct fdisk_context *cxt, struct fdisk_label **lb) |
| 263 |
{ |
| 264 |
size_t i; |
| 265 |
struct fdisk_label *res = NULL; |
| 266 |
|
| 267 |
if (!lb || !cxt) |
| 268 |
return -EINVAL; |
| 269 |
|
| 270 |
if (!*lb) |
| 271 |
res = cxt->labels[0]; |
| 272 |
else { |
| 273 |
for (i = 1; i < cxt->nlabels; i++) { |
| 274 |
if (*lb == cxt->labels[i - 1]) { |
| 275 |
res = cxt->labels[i]; |
| 276 |
break; |
| 277 |
} |
| 278 |
} |
| 279 |
} |
| 280 |
|
| 281 |
*lb = res; |
| 282 |
return res ? 0 : 1; |
| 283 |
} |
| 284 |
|
| 285 |
/** |
| 286 |
* fdisk_get_nlabels: |
| 287 |
* @cxt: context |
| 288 |
* |
| 289 |
* Returns: number of supported label types |
| 290 |
*/ |
| 291 |
size_t fdisk_get_nlabels(struct fdisk_context *cxt) |
| 292 |
{ |
| 293 |
return cxt ? cxt->nlabels : 0; |
| 294 |
} |
| 295 |
|
| 296 |
int __fdisk_switch_label(struct fdisk_context *cxt, struct fdisk_label *lb) |
| 297 |
{ |
| 298 |
if (!lb || !cxt) |
| 299 |
return -EINVAL; |
| 300 |
if (lb->disabled) { |
| 301 |
DBG(CXT, ul_debugobj(cxt, "*** attempt to switch to disabled label %s -- ignore!", lb->name)); |
| 302 |
return -EINVAL; |
| 303 |
} |
| 304 |
cxt->label = lb; |
| 305 |
DBG(CXT, ul_debugobj(cxt, "--> switching context to %s!", lb->name)); |
| 306 |
|
| 307 |
return fdisk_apply_label_device_properties(cxt); |
| 308 |
} |
| 309 |
|
| 310 |
/** |
| 311 |
* fdisk_has_label: |
| 312 |
* @cxt: fdisk context |
| 313 |
* |
| 314 |
* Returns: return 1 if there is label on the device. |
| 315 |
*/ |
| 316 |
int fdisk_has_label(struct fdisk_context *cxt) |
| 317 |
{ |
| 318 |
return cxt && cxt->label; |
| 319 |
} |
| 320 |
|
| 321 |
/** |
| 322 |
* fdisk_has_protected_bootbits: |
| 323 |
* @cxt: fdisk context |
| 324 |
* |
| 325 |
* Returns: return 1 if boot bits protection enabled. |
| 326 |
*/ |
| 327 |
int fdisk_has_protected_bootbits(struct fdisk_context *cxt) |
| 328 |
{ |
| 329 |
return cxt && cxt->protect_bootbits; |
| 330 |
} |
| 331 |
|
| 332 |
/** |
| 333 |
* fdisk_enable_bootbits_protection: |
| 334 |
* @cxt: fdisk context |
| 335 |
* @enable: 1 or 0 |
| 336 |
* |
| 337 |
* The library zeroizes all the first sector when create a new disk label by |
| 338 |
* default. This function can be used to control this behavior. For now it's |
| 339 |
* supported for MBR and GPT. |
| 340 |
* |
| 341 |
* Returns: 0 on success, < 0 on error. |
| 342 |
*/ |
| 343 |
int fdisk_enable_bootbits_protection(struct fdisk_context *cxt, int enable) |
| 344 |
{ |
| 345 |
if (!cxt) |
| 346 |
return -EINVAL; |
| 347 |
cxt->protect_bootbits = enable ? 1 : 0; |
| 348 |
return 0; |
| 349 |
} |
| 350 |
/** |
| 351 |
* fdisk_disable_dialogs |
| 352 |
* @cxt: fdisk context |
| 353 |
* @disable: 1 or 0 |
| 354 |
* |
| 355 |
* The library uses dialog driven partitioning by default. |
| 356 |
* |
| 357 |
* Returns: 0 on success, < 0 on error. |
| 358 |
* |
| 359 |
* Since: 2.31 |
| 360 |
*/ |
| 361 |
int fdisk_disable_dialogs(struct fdisk_context *cxt, int disable) |
| 362 |
{ |
| 363 |
if (!cxt) |
| 364 |
return -EINVAL; |
| 365 |
|
| 366 |
cxt->no_disalogs = disable; |
| 367 |
return 0; |
| 368 |
} |
| 369 |
|
| 370 |
/** |
| 371 |
* fdisk_has_dialogs |
| 372 |
* @cxt: fdisk context |
| 373 |
* |
| 374 |
* See fdisk_disable_dialogs() |
| 375 |
* |
| 376 |
* Returns: 1 if dialog driven partitioning enabled (default), or 0. |
| 377 |
* |
| 378 |
* Since: 2.31 |
| 379 |
*/ |
| 380 |
int fdisk_has_dialogs(struct fdisk_context *cxt) |
| 381 |
{ |
| 382 |
return cxt->no_disalogs == 0; |
| 383 |
} |
| 384 |
|
| 385 |
/** |
| 386 |
* fdisk_enable_wipe |
| 387 |
* @cxt: fdisk context |
| 388 |
* @enable: 1 or 0 |
| 389 |
* |
| 390 |
* The library removes all PT/filesystem/RAID signatures before it writes |
| 391 |
* partition table. The probing area where it looks for signatures is from |
| 392 |
* the begin of the disk. The device is wiped by libblkid. |
| 393 |
* |
| 394 |
* See also fdisk_wipe_partition(). |
| 395 |
* |
| 396 |
* Returns: 0 on success, < 0 on error. |
| 397 |
*/ |
| 398 |
int fdisk_enable_wipe(struct fdisk_context *cxt, int enable) |
| 399 |
{ |
| 400 |
if (!cxt) |
| 401 |
return -EINVAL; |
| 402 |
|
| 403 |
fdisk_set_wipe_area(cxt, 0, cxt->total_sectors, enable); |
| 404 |
return 0; |
| 405 |
} |
| 406 |
|
| 407 |
/** |
| 408 |
* fdisk_has_wipe |
| 409 |
* @cxt: fdisk context |
| 410 |
* |
| 411 |
* Returns the current wipe setting. See fdisk_enable_wipe(). |
| 412 |
* |
| 413 |
* Returns: 0 on success, < 0 on error. |
| 414 |
*/ |
| 415 |
int fdisk_has_wipe(struct fdisk_context *cxt) |
| 416 |
{ |
| 417 |
if (!cxt) |
| 418 |
return 0; |
| 419 |
|
| 420 |
return fdisk_has_wipe_area(cxt, 0, cxt->total_sectors); |
| 421 |
} |
| 422 |
|
| 423 |
|
| 424 |
/** |
| 425 |
* fdisk_get_collision |
| 426 |
* @cxt: fdisk context |
| 427 |
* |
| 428 |
* Returns: name of the filesystem or RAID detected on the device or NULL. |
| 429 |
*/ |
| 430 |
const char *fdisk_get_collision(struct fdisk_context *cxt) |
| 431 |
{ |
| 432 |
return cxt->collision; |
| 433 |
} |
| 434 |
|
| 435 |
/** |
| 436 |
* fdisk_is_ptcollision: |
| 437 |
* @cxt: fdisk context |
| 438 |
* |
| 439 |
* The collision detected by libblkid (usually another partition table). Note |
| 440 |
* that libfdisk does not support all partitions tables, so fdisk_has_label() |
| 441 |
* may return false, but fdisk_is_ptcollision() may return true. |
| 442 |
* |
| 443 |
* Since: 2.30 |
| 444 |
* |
| 445 |
* Returns: 0 or 1 |
| 446 |
*/ |
| 447 |
int fdisk_is_ptcollision(struct fdisk_context *cxt) |
| 448 |
{ |
| 449 |
return cxt->pt_collision; |
| 450 |
} |
| 451 |
|
| 452 |
/** |
| 453 |
* fdisk_is_collision_area: |
| 454 |
* @cxt: fdisk context |
| 455 |
* |
| 456 |
* If there is a collision with the filesystem or another partition table, |
| 457 |
* verify that the detected magic string is within the specified area. |
| 458 |
* |
| 459 |
* Returns: 0 or 1 |
| 460 |
* |
| 461 |
* Since: v2.42 |
| 462 |
*/ |
| 463 |
int fdisk_is_collision_area(struct fdisk_context *cxt, |
| 464 |
uint64_t start, uint64_t size) |
| 465 |
{ |
| 466 |
if (cxt->collision && |
| 467 |
start <= cxt->collision_offset && cxt->collision_offset <= start + size) |
| 468 |
return 1; |
| 469 |
|
| 470 |
return 0; |
| 471 |
} |
| 472 |
|
| 473 |
/** |
| 474 |
* fdisk_get_npartitions: |
| 475 |
* @cxt: context |
| 476 |
* |
| 477 |
* The maximal number of the partitions depends on disklabel and does not |
| 478 |
* have to describe the real limit of PT. |
| 479 |
* |
| 480 |
* For example the limit for MBR without extend partition is 4, with extended |
| 481 |
* partition it's unlimited (so the function returns the current number of all |
| 482 |
* partitions in this case). |
| 483 |
* |
| 484 |
* And for example for GPT it depends on space allocated on disk for array of |
| 485 |
* entry records (usually 128). |
| 486 |
* |
| 487 |
* It's fine to use fdisk_get_npartitions() in loops, but don't forget that |
| 488 |
* partition may be unused (see fdisk_is_partition_used()). |
| 489 |
* |
| 490 |
* <informalexample> |
| 491 |
* <programlisting> |
| 492 |
* struct fdisk_partition *pa = NULL; |
| 493 |
* size_t i, nmax = fdisk_get_npartitions(cxt); |
| 494 |
* |
| 495 |
* for (i = 0; i < nmax; i++) { |
| 496 |
* if (!fdisk_is_partition_used(cxt, i)) |
| 497 |
* continue; |
| 498 |
* ... do something ... |
| 499 |
* } |
| 500 |
* </programlisting> |
| 501 |
* </informalexample> |
| 502 |
* |
| 503 |
* Note that the recommended way to list partitions is to use |
| 504 |
* fdisk_get_partitions() and struct fdisk_table then ask disk driver for each |
| 505 |
* individual partitions. |
| 506 |
* |
| 507 |
* Returns: maximal number of partitions for the current label. |
| 508 |
*/ |
| 509 |
size_t fdisk_get_npartitions(struct fdisk_context *cxt) |
| 510 |
{ |
| 511 |
return cxt && cxt->label ? cxt->label->nparts_max : 0; |
| 512 |
} |
| 513 |
|
| 514 |
/** |
| 515 |
* fdisk_is_labeltype: |
| 516 |
* @cxt: fdisk context |
| 517 |
* @id: FDISK_DISKLABEL_* |
| 518 |
* |
| 519 |
* See also fdisk_is_label() macro in libfdisk.h. |
| 520 |
* |
| 521 |
* Returns: return 1 if the current label is @id |
| 522 |
*/ |
| 523 |
int fdisk_is_labeltype(struct fdisk_context *cxt, enum fdisk_labeltype id) |
| 524 |
{ |
| 525 |
assert(cxt); |
| 526 |
|
| 527 |
return cxt->label && (unsigned)fdisk_label_get_type(cxt->label) == id; |
| 528 |
} |
| 529 |
|
| 530 |
/** |
| 531 |
* fdisk_get_parent: |
| 532 |
* @cxt: nested fdisk context |
| 533 |
* |
| 534 |
* Returns: pointer to parental context, or NULL |
| 535 |
*/ |
| 536 |
struct fdisk_context *fdisk_get_parent(struct fdisk_context *cxt) |
| 537 |
{ |
| 538 |
assert(cxt); |
| 539 |
return cxt->parent; |
| 540 |
} |
| 541 |
|
| 542 |
static void reset_context(struct fdisk_context *cxt) |
| 543 |
{ |
| 544 |
size_t i; |
| 545 |
|
| 546 |
DBG(CXT, ul_debugobj(cxt, "*** resetting context")); |
| 547 |
|
| 548 |
/* reset drives' private data */ |
| 549 |
for (i = 0; i < cxt->nlabels; i++) |
| 550 |
fdisk_deinit_label(cxt->labels[i]); |
| 551 |
|
| 552 |
if (cxt->parent) { |
| 553 |
/* the first sector may be independent on parent */ |
| 554 |
if (cxt->parent->firstsector != cxt->firstsector) { |
| 555 |
DBG(CXT, ul_debugobj(cxt, " firstsector independent on parent (freeing)")); |
| 556 |
free(cxt->firstsector); |
| 557 |
} |
| 558 |
} else { |
| 559 |
/* we close device only in primary context */ |
| 560 |
if (cxt->dev_fd > -1 && cxt->is_priv) |
| 561 |
close(cxt->dev_fd); |
| 562 |
DBG(CXT, ul_debugobj(cxt, " freeing firstsector")); |
| 563 |
free(cxt->firstsector); |
| 564 |
} |
| 565 |
|
| 566 |
free(cxt->dev_path); |
| 567 |
cxt->dev_path = NULL; |
| 568 |
|
| 569 |
free(cxt->dev_model); |
| 570 |
cxt->dev_model = NULL; |
| 571 |
cxt->dev_model_probed = 0; |
| 572 |
|
| 573 |
free(cxt->collision); |
| 574 |
cxt->collision = NULL; |
| 575 |
|
| 576 |
memset(&cxt->dev_st, 0, sizeof(cxt->dev_st)); |
| 577 |
|
| 578 |
cxt->dev_fd = -1; |
| 579 |
cxt->is_priv = 0; |
| 580 |
cxt->is_excl = 0; |
| 581 |
cxt->firstsector = NULL; |
| 582 |
cxt->firstsector_bufsz = 0; |
| 583 |
|
| 584 |
fdisk_zeroize_device_properties(cxt); |
| 585 |
|
| 586 |
fdisk_unref_script(cxt->script); |
| 587 |
cxt->script = NULL; |
| 588 |
|
| 589 |
cxt->label = NULL; |
| 590 |
|
| 591 |
fdisk_free_wipe_areas(cxt); |
| 592 |
} |
| 593 |
|
| 594 |
/* fdisk_assign_device() body */ |
| 595 |
static int fdisk_assign_fd(struct fdisk_context *cxt, int fd, |
| 596 |
const char *fname, int readonly, |
| 597 |
int priv, int excl) |
| 598 |
{ |
| 599 |
assert(cxt); |
| 600 |
assert(fd >= 0); |
| 601 |
|
| 602 |
errno = 0; |
| 603 |
|
| 604 |
/* redirect request to parent */ |
| 605 |
if (cxt->parent) { |
| 606 |
int rc, org = fdisk_is_listonly(cxt->parent); |
| 607 |
|
| 608 |
/* assign_device() is sensitive to "listonly" mode, so let's |
| 609 |
* follow the current context setting for the parent to avoid |
| 610 |
* unwanted extra warnings. */ |
| 611 |
fdisk_enable_listonly(cxt->parent, fdisk_is_listonly(cxt)); |
| 612 |
|
| 613 |
rc = fdisk_assign_fd(cxt->parent, fd, fname, readonly, priv, excl); |
| 614 |
fdisk_enable_listonly(cxt->parent, org); |
| 615 |
|
| 616 |
if (!rc) |
| 617 |
rc = init_nested_from_parent(cxt, 0); |
| 618 |
if (!rc) |
| 619 |
fdisk_probe_labels(cxt); |
| 620 |
return rc; |
| 621 |
} |
| 622 |
|
| 623 |
reset_context(cxt); |
| 624 |
|
| 625 |
if (fstat(fd, &cxt->dev_st) != 0) |
| 626 |
goto fail; |
| 627 |
|
| 628 |
cxt->readonly = readonly ? 1 : 0; |
| 629 |
cxt->dev_fd = fd; |
| 630 |
cxt->is_priv = priv ? 1 : 0; |
| 631 |
cxt->is_excl = excl ? 1 : 0; |
| 632 |
|
| 633 |
cxt->dev_path = fname ? strdup(fname) : NULL; |
| 634 |
if (!cxt->dev_path) |
| 635 |
goto fail; |
| 636 |
|
| 637 |
fdisk_discover_topology(cxt); |
| 638 |
fdisk_discover_geometry(cxt); |
| 639 |
|
| 640 |
fdisk_apply_user_device_properties(cxt); |
| 641 |
|
| 642 |
if (fdisk_read_firstsector(cxt) < 0) |
| 643 |
goto fail; |
| 644 |
|
| 645 |
/* warn about obsolete stuff on the device if we aren't in list-only */ |
| 646 |
if (!fdisk_is_listonly(cxt) && fdisk_check_collisions(cxt) < 0) |
| 647 |
goto fail; |
| 648 |
|
| 649 |
fdisk_probe_labels(cxt); |
| 650 |
fdisk_apply_label_device_properties(cxt); |
| 651 |
|
| 652 |
/* Don't report collision if there is already a valid partition table. |
| 653 |
* The bootbits are wiped when we create a *new* partition table only. */ |
| 654 |
if (fdisk_is_ptcollision(cxt) && fdisk_has_label(cxt)) { |
| 655 |
DBG(CXT, ul_debugobj(cxt, "ignore old %s", cxt->collision)); |
| 656 |
cxt->pt_collision = 0; |
| 657 |
free(cxt->collision); |
| 658 |
cxt->collision = NULL; |
| 659 |
} |
| 660 |
|
| 661 |
DBG(CXT, ul_debugobj(cxt, "initialized for %s [%s %s %s]", |
| 662 |
fname, |
| 663 |
cxt->readonly ? "READ-ONLY" : "READ-WRITE", |
| 664 |
cxt->is_excl ? "EXCL" : "", |
| 665 |
cxt->is_priv ? "PRIV" : "")); |
| 666 |
return 0; |
| 667 |
fail: |
| 668 |
{ |
| 669 |
int rc = errno ? -errno : -EINVAL; |
| 670 |
cxt->dev_fd = -1; |
| 671 |
DBG(CXT, ul_debugobj(cxt, "failed to assign device [rc=%d]", rc)); |
| 672 |
return rc; |
| 673 |
} |
| 674 |
} |
| 675 |
|
| 676 |
/** |
| 677 |
* fdisk_assign_device: |
| 678 |
* @cxt: context |
| 679 |
* @fname: path to the device to be handled |
| 680 |
* @readonly: how to open the device |
| 681 |
* |
| 682 |
* Open the device, discovery topology, geometry, detect disklabel, check for |
| 683 |
* collisions and switch the current label driver to reflect the probing |
| 684 |
* result. |
| 685 |
* |
| 686 |
* If in standard mode (!= non-listonly mode) then also detects for collisions. |
| 687 |
* The result is accessible by fdisk_get_collision() and |
| 688 |
* fdisk_is_ptcollision(). The collision (e.g. old obsolete PT) may be removed |
| 689 |
* by fdisk_enable_wipe(). Note that new PT and old PT may be on different |
| 690 |
* locations. |
| 691 |
* |
| 692 |
* Note that this function resets all generic setting in context. |
| 693 |
* |
| 694 |
* If the @cxt is nested context (necessary for example to edit BSD or PMBR) |
| 695 |
* then the device is assigned to the parental context and necessary properties |
| 696 |
* are copied to the @cxt. The change is propagated in child->parent direction |
| 697 |
* only. It's impossible to use a different device for primary and nested |
| 698 |
* contexts. |
| 699 |
* |
| 700 |
* Returns: 0 on success, < 0 on error. |
| 701 |
*/ |
| 702 |
int fdisk_assign_device(struct fdisk_context *cxt, |
| 703 |
const char *fname, int readonly) |
| 704 |
{ |
| 705 |
int fd, rc, flags = O_CLOEXEC; |
| 706 |
|
| 707 |
DBG(CXT, ul_debugobj(cxt, "assigning device %s", fname)); |
| 708 |
assert(cxt); |
| 709 |
|
| 710 |
if (readonly) |
| 711 |
flags |= O_RDONLY; |
| 712 |
else |
| 713 |
flags |= (O_RDWR | O_EXCL); |
| 714 |
|
| 715 |
errno = 0; |
| 716 |
fd = open(fname,flags); |
| 717 |
if (fd < 0 && errno == EBUSY && (flags & O_EXCL)) { |
| 718 |
flags &= ~O_EXCL; |
| 719 |
errno = 0; |
| 720 |
fd = open(fname, flags); |
| 721 |
} |
| 722 |
|
| 723 |
if (fd < 0) { |
| 724 |
rc = -errno; |
| 725 |
DBG(CXT, ul_debugobj(cxt, "failed to assign device [rc=%d]", rc)); |
| 726 |
return rc; |
| 727 |
} |
| 728 |
|
| 729 |
rc = fdisk_assign_fd(cxt, fd, fname, readonly, 1, flags & O_EXCL); |
| 730 |
if (rc) |
| 731 |
close(fd); |
| 732 |
return rc; |
| 733 |
} |
| 734 |
|
| 735 |
/** |
| 736 |
* fdisk_assign_device_by_fd: |
| 737 |
* @cxt: context |
| 738 |
* @fd: device file descriptor |
| 739 |
* @fname: path to the device (used for dialogs, debugging, partition names, ...) |
| 740 |
* @readonly: how to use the device |
| 741 |
* |
| 742 |
* Like fdisk_assign_device(), but caller is responsible to open and close the |
| 743 |
* device. The library only fsync() the device on fdisk_deassign_device(). |
| 744 |
* |
| 745 |
* The device has to be open O_RDWR on @readonly=0. |
| 746 |
* |
| 747 |
* Returns: 0 on success, < 0 on error. |
| 748 |
* |
| 749 |
* Since: 2.35 |
| 750 |
*/ |
| 751 |
int fdisk_assign_device_by_fd(struct fdisk_context *cxt, int fd, |
| 752 |
const char *fname, int readonly) |
| 753 |
{ |
| 754 |
DBG(CXT, ul_debugobj(cxt, "assign by fd")); |
| 755 |
return fdisk_assign_fd(cxt, fd, fname, readonly, 0, 0); |
| 756 |
} |
| 757 |
|
| 758 |
/** |
| 759 |
* fdisk_deassign_device: |
| 760 |
* @cxt: context |
| 761 |
* @nosync: disable sync() after close(). |
| 762 |
* |
| 763 |
* Call fsync(), close() and than sync(), but for read-only handler close the |
| 764 |
* device only. If the @cxt is nested context then the request is redirected to |
| 765 |
* the parent. |
| 766 |
* |
| 767 |
* Returns: 0 on success, < 0 on error. |
| 768 |
*/ |
| 769 |
int fdisk_deassign_device(struct fdisk_context *cxt, int nosync) |
| 770 |
{ |
| 771 |
assert(cxt); |
| 772 |
assert(cxt->dev_fd >= 0); |
| 773 |
|
| 774 |
if (cxt->parent) { |
| 775 |
int rc = fdisk_deassign_device(cxt->parent, nosync); |
| 776 |
|
| 777 |
if (!rc) |
| 778 |
rc = init_nested_from_parent(cxt, 0); |
| 779 |
return rc; |
| 780 |
} |
| 781 |
|
| 782 |
DBG(CXT, ul_debugobj(cxt, "de-assigning device %s", cxt->dev_path)); |
| 783 |
|
| 784 |
if (cxt->readonly && cxt->is_priv) |
| 785 |
close(cxt->dev_fd); |
| 786 |
else { |
| 787 |
if (fsync(cxt->dev_fd)) { |
| 788 |
fdisk_warn(cxt, _("%s: fsync device failed"), |
| 789 |
cxt->dev_path); |
| 790 |
return -errno; |
| 791 |
} |
| 792 |
if (cxt->is_priv && close(cxt->dev_fd)) { |
| 793 |
fdisk_warn(cxt, _("%s: close device failed"), |
| 794 |
cxt->dev_path); |
| 795 |
return -errno; |
| 796 |
} |
| 797 |
if (S_ISBLK(cxt->dev_st.st_mode) && !nosync) { |
| 798 |
fdisk_info(cxt, _("Syncing disks.")); |
| 799 |
sync(); |
| 800 |
} |
| 801 |
} |
| 802 |
|
| 803 |
free(cxt->dev_path); |
| 804 |
cxt->dev_path = NULL; |
| 805 |
cxt->dev_fd = -1; |
| 806 |
cxt->is_priv = 0; |
| 807 |
cxt->is_excl = 0; |
| 808 |
|
| 809 |
return 0; |
| 810 |
} |
| 811 |
|
| 812 |
/** |
| 813 |
* fdisk_reassign_device: |
| 814 |
* @cxt: context |
| 815 |
* |
| 816 |
* This function is "hard reset" of the context and it does not write anything |
| 817 |
* to the device. All in-memory changes associated with the context will be |
| 818 |
* lost. It's recommended to use this function after some fatal problem when the |
| 819 |
* context (and label specific driver) is in an undefined state. |
| 820 |
* |
| 821 |
* Returns: 0 on success, < 0 on error. |
| 822 |
*/ |
| 823 |
int fdisk_reassign_device(struct fdisk_context *cxt) |
| 824 |
{ |
| 825 |
char *devname; |
| 826 |
int rdonly, rc, fd, priv, excl; |
| 827 |
|
| 828 |
assert(cxt); |
| 829 |
assert(cxt->dev_fd >= 0); |
| 830 |
|
| 831 |
DBG(CXT, ul_debugobj(cxt, "re-assigning device %s", cxt->dev_path)); |
| 832 |
|
| 833 |
devname = strdup(cxt->dev_path); |
| 834 |
if (!devname) |
| 835 |
return -ENOMEM; |
| 836 |
|
| 837 |
rdonly = cxt->readonly; |
| 838 |
fd = cxt->dev_fd; |
| 839 |
priv = cxt->is_priv; |
| 840 |
excl = cxt->is_excl; |
| 841 |
|
| 842 |
fdisk_deassign_device(cxt, 1); |
| 843 |
|
| 844 |
if (priv) |
| 845 |
/* reopen and assign */ |
| 846 |
rc = fdisk_assign_device(cxt, devname, rdonly); |
| 847 |
else |
| 848 |
/* assign only */ |
| 849 |
rc = fdisk_assign_fd(cxt, fd, devname, rdonly, priv, excl); |
| 850 |
|
| 851 |
free(devname); |
| 852 |
return rc; |
| 853 |
} |
| 854 |
|
| 855 |
/** |
| 856 |
* fdisk_reread_partition_table: |
| 857 |
* @cxt: context |
| 858 |
* |
| 859 |
* Force *kernel* to re-read partition table on block devices. |
| 860 |
* |
| 861 |
* Returns: 0 on success, < 0 in case of error. |
| 862 |
*/ |
| 863 |
int fdisk_reread_partition_table(struct fdisk_context *cxt) |
| 864 |
{ |
| 865 |
int i = 0; |
| 866 |
|
| 867 |
assert(cxt); |
| 868 |
assert(cxt->dev_fd >= 0); |
| 869 |
|
| 870 |
if (!S_ISBLK(cxt->dev_st.st_mode)) |
| 871 |
return 0; |
| 872 |
|
| 873 |
DBG(CXT, ul_debugobj(cxt, "calling re-read ioctl")); |
| 874 |
sync(); |
| 875 |
#ifdef BLKRRPART |
| 876 |
fdisk_info(cxt, _("Calling ioctl() to re-read partition table.")); |
| 877 |
i = ioctl(cxt->dev_fd, BLKRRPART); |
| 878 |
#else |
| 879 |
errno = ENOSYS; |
| 880 |
i = 1; |
| 881 |
#endif |
| 882 |
|
| 883 |
if (i) { |
| 884 |
fdisk_warn(cxt, _("Re-reading the partition table failed.")); |
| 885 |
fdisk_info(cxt, _( |
| 886 |
"The kernel still uses the old table. The " |
| 887 |
"new table will be used at the next reboot " |
| 888 |
"or after you run partprobe(8) or partx(8).")); |
| 889 |
return -errno; |
| 890 |
} |
| 891 |
|
| 892 |
return 0; |
| 893 |
} |
| 894 |
|
| 895 |
#ifdef __linux__ |
| 896 |
static inline int add_to_partitions_array( |
| 897 |
struct fdisk_partition ***ary, |
| 898 |
struct fdisk_partition *pa, |
| 899 |
size_t *n, size_t nmax) |
| 900 |
{ |
| 901 |
if (!*ary) { |
| 902 |
*ary = calloc(nmax, sizeof(struct fdisk_partition *)); |
| 903 |
if (!*ary) |
| 904 |
return -ENOMEM; |
| 905 |
} |
| 906 |
(*ary)[*n] = pa; |
| 907 |
(*n)++; |
| 908 |
return 0; |
| 909 |
} |
| 910 |
#endif |
| 911 |
|
| 912 |
/** |
| 913 |
* fdisk_reread_changes: |
| 914 |
* @cxt: context |
| 915 |
* @org: original layout (on disk) |
| 916 |
* |
| 917 |
* Like fdisk_reread_partition_table() but don't forces kernel re-read all |
| 918 |
* partition table. The BLKPG_* ioctls are used for individual partitions. The |
| 919 |
* advantage is that unmodified partitions maybe mounted. |
| 920 |
* |
| 921 |
* The function behaves like fdisk_reread_partition_table() on systems where |
| 922 |
* are no available BLKPG_* ioctls. |
| 923 |
* |
| 924 |
* Returns: <0 on error, or 0. |
| 925 |
*/ |
| 926 |
#ifdef __linux__ |
| 927 |
int fdisk_reread_changes(struct fdisk_context *cxt, struct fdisk_table *org) |
| 928 |
{ |
| 929 |
struct fdisk_table *tb = NULL; |
| 930 |
struct fdisk_iter itr; |
| 931 |
struct fdisk_partition *pa; |
| 932 |
struct fdisk_partition **rem = NULL, **add = NULL, **upd = NULL; |
| 933 |
int change, rc = 0, err = 0; |
| 934 |
size_t nparts, i, nadds = 0, nupds = 0, nrems = 0; |
| 935 |
unsigned int ssf; |
| 936 |
|
| 937 |
DBG(CXT, ul_debugobj(cxt, "rereading changes")); |
| 938 |
|
| 939 |
fdisk_reset_iter(&itr, FDISK_ITER_FORWARD); |
| 940 |
|
| 941 |
/* the current layout */ |
| 942 |
fdisk_get_partitions(cxt, &tb); |
| 943 |
/* maximal number of partitions */ |
| 944 |
nparts = max(fdisk_table_get_nents(tb), fdisk_table_get_nents(org)); |
| 945 |
|
| 946 |
while (fdisk_diff_tables(org, tb, &itr, &pa, &change) == 0) { |
| 947 |
if (change == FDISK_DIFF_UNCHANGED) |
| 948 |
continue; |
| 949 |
switch (change) { |
| 950 |
case FDISK_DIFF_REMOVED: |
| 951 |
rc = add_to_partitions_array(&rem, pa, &nrems, nparts); |
| 952 |
break; |
| 953 |
case FDISK_DIFF_ADDED: |
| 954 |
rc = add_to_partitions_array(&add, pa, &nadds, nparts); |
| 955 |
break; |
| 956 |
case FDISK_DIFF_RESIZED: |
| 957 |
rc = add_to_partitions_array(&upd, pa, &nupds, nparts); |
| 958 |
break; |
| 959 |
case FDISK_DIFF_MOVED: |
| 960 |
rc = add_to_partitions_array(&rem, pa, &nrems, nparts); |
| 961 |
if (!rc) |
| 962 |
rc = add_to_partitions_array(&add, pa, &nadds, nparts); |
| 963 |
break; |
| 964 |
} |
| 965 |
if (rc != 0) |
| 966 |
goto done; |
| 967 |
} |
| 968 |
|
| 969 |
/* sector size factor -- used to recount from real to 512-byte sectors */ |
| 970 |
ssf = cxt->sector_size / 512; |
| 971 |
|
| 972 |
for (i = 0; i < nrems; i++) { |
| 973 |
pa = rem[i]; |
| 974 |
DBG(PART, ul_debugobj(pa, "#%zu calling BLKPG_DEL_PARTITION", pa->partno)); |
| 975 |
if (partx_del_partition(cxt->dev_fd, pa->partno + 1) != 0) { |
| 976 |
fdisk_warn(cxt, _("Failed to remove partition %zu from system"), pa->partno + 1); |
| 977 |
err++; |
| 978 |
} |
| 979 |
} |
| 980 |
for (i = 0; i < nupds; i++) { |
| 981 |
pa = upd[i]; |
| 982 |
DBG(PART, ul_debugobj(pa, "#%zu calling BLKPG_RESIZE_PARTITION", pa->partno)); |
| 983 |
if (partx_resize_partition(cxt->dev_fd, pa->partno + 1, |
| 984 |
pa->start * ssf, pa->size * ssf) != 0) { |
| 985 |
fdisk_warn(cxt, _("Failed to update system information about partition %zu"), pa->partno + 1); |
| 986 |
err++; |
| 987 |
} |
| 988 |
} |
| 989 |
for (i = 0; i < nadds; i++) { |
| 990 |
uint64_t sz; |
| 991 |
|
| 992 |
pa = add[i]; |
| 993 |
sz = pa->size * ssf; |
| 994 |
|
| 995 |
DBG(PART, ul_debugobj(pa, "#%zu calling BLKPG_ADD_PARTITION", pa->partno)); |
| 996 |
|
| 997 |
if (fdisk_is_label(cxt, DOS) && fdisk_partition_is_container(pa)) |
| 998 |
/* Let's follow the Linux kernel and reduce |
| 999 |
* DOS extended partition to 1 or 2 sectors. |
| 1000 |
*/ |
| 1001 |
sz = min(sz, (uint64_t) 2); |
| 1002 |
|
| 1003 |
if (partx_add_partition(cxt->dev_fd, pa->partno + 1, |
| 1004 |
pa->start * ssf, sz) != 0) { |
| 1005 |
fdisk_warn(cxt, _("Failed to add partition %zu to system"), pa->partno + 1); |
| 1006 |
err++; |
| 1007 |
} |
| 1008 |
} |
| 1009 |
if (err) |
| 1010 |
fdisk_info(cxt, _( |
| 1011 |
"The kernel still uses the old partitions. The new " |
| 1012 |
"table will be used at the next reboot. ")); |
| 1013 |
done: |
| 1014 |
free(rem); |
| 1015 |
free(add); |
| 1016 |
free(upd); |
| 1017 |
fdisk_unref_table(tb); |
| 1018 |
return rc; |
| 1019 |
} |
| 1020 |
#else |
| 1021 |
int fdisk_reread_changes(struct fdisk_context *cxt, |
| 1022 |
struct fdisk_table *org __attribute__((__unused__))) { |
| 1023 |
return fdisk_reread_partition_table(cxt); |
| 1024 |
} |
| 1025 |
#endif |
| 1026 |
|
| 1027 |
/** |
| 1028 |
* fdisk_device_is_used: |
| 1029 |
* @cxt: context |
| 1030 |
* |
| 1031 |
* The function returns always 0 if the device has not been opened by |
| 1032 |
* fdisk_assign_device() or if open read-only. |
| 1033 |
* |
| 1034 |
* Returns: 1 if the device assigned to the context is used by system, or 0. |
| 1035 |
*/ |
| 1036 |
int fdisk_device_is_used(struct fdisk_context *cxt) |
| 1037 |
{ |
| 1038 |
int rc; |
| 1039 |
assert(cxt); |
| 1040 |
assert(cxt->dev_fd >= 0); |
| 1041 |
|
| 1042 |
rc = cxt->readonly ? 0 : |
| 1043 |
cxt->is_excl ? 0 : |
| 1044 |
cxt->is_priv ? 1 : 0; |
| 1045 |
|
| 1046 |
DBG(CXT, ul_debugobj(cxt, "device used: %s [read-only=%d, excl=%d, priv:%d]", |
| 1047 |
rc ? "TRUE" : "FALSE", cxt->readonly, |
| 1048 |
cxt->is_excl, cxt->is_priv)); |
| 1049 |
return rc; |
| 1050 |
} |
| 1051 |
|
| 1052 |
/** |
| 1053 |
* fdisk_is_readonly: |
| 1054 |
* @cxt: context |
| 1055 |
* |
| 1056 |
* Returns: 1 if device open readonly |
| 1057 |
*/ |
| 1058 |
int fdisk_is_readonly(struct fdisk_context *cxt) |
| 1059 |
{ |
| 1060 |
assert(cxt); |
| 1061 |
return cxt->readonly; |
| 1062 |
} |
| 1063 |
|
| 1064 |
/** |
| 1065 |
* fdisk_is_regfile: |
| 1066 |
* @cxt: context |
| 1067 |
* |
| 1068 |
* Since: 2.30 |
| 1069 |
* |
| 1070 |
* Returns: 1 if open file descriptor is regular file rather than a block device. |
| 1071 |
*/ |
| 1072 |
int fdisk_is_regfile(struct fdisk_context *cxt) |
| 1073 |
{ |
| 1074 |
assert(cxt); |
| 1075 |
return S_ISREG(cxt->dev_st.st_mode); |
| 1076 |
} |
| 1077 |
|
| 1078 |
/** |
| 1079 |
* fdisk_unref_context: |
| 1080 |
* @cxt: fdisk context |
| 1081 |
* |
| 1082 |
* Deallocates context struct. |
| 1083 |
*/ |
| 1084 |
void fdisk_unref_context(struct fdisk_context *cxt) |
| 1085 |
{ |
| 1086 |
unsigned i; |
| 1087 |
|
| 1088 |
if (!cxt) |
| 1089 |
return; |
| 1090 |
|
| 1091 |
cxt->refcount--; |
| 1092 |
if (cxt->refcount <= 0) { |
| 1093 |
DBG(CXT, ul_debugobj(cxt, "freeing context %p for %s", cxt, cxt->dev_path)); |
| 1094 |
|
| 1095 |
reset_context(cxt); /* this is sensitive to parent<->child relationship! */ |
| 1096 |
|
| 1097 |
/* deallocate label's private stuff */ |
| 1098 |
for (i = 0; i < cxt->nlabels; i++) { |
| 1099 |
if (!cxt->labels[i]) |
| 1100 |
continue; |
| 1101 |
if (cxt->labels[i]->op->free) |
| 1102 |
cxt->labels[i]->op->free(cxt->labels[i]); |
| 1103 |
else |
| 1104 |
free(cxt->labels[i]); |
| 1105 |
cxt->labels[i] = NULL; |
| 1106 |
} |
| 1107 |
|
| 1108 |
fdisk_unref_context(cxt->parent); |
| 1109 |
cxt->parent = NULL; |
| 1110 |
|
| 1111 |
free(cxt); |
| 1112 |
} |
| 1113 |
} |
| 1114 |
|
| 1115 |
|
| 1116 |
/** |
| 1117 |
* fdisk_enable_details: |
| 1118 |
* @cxt: context |
| 1119 |
* @enable: true/false |
| 1120 |
* |
| 1121 |
* Enables or disables "details" display mode. This function has effect to |
| 1122 |
* fdisk_partition_to_string() function. |
| 1123 |
* |
| 1124 |
* Returns: 0 on success, < 0 on error. |
| 1125 |
*/ |
| 1126 |
int fdisk_enable_details(struct fdisk_context *cxt, int enable) |
| 1127 |
{ |
| 1128 |
assert(cxt); |
| 1129 |
cxt->display_details = enable ? 1 : 0; |
| 1130 |
return 0; |
| 1131 |
} |
| 1132 |
|
| 1133 |
/** |
| 1134 |
* fdisk_is_details: |
| 1135 |
* @cxt: context |
| 1136 |
* |
| 1137 |
* Returns: 1 if details are enabled |
| 1138 |
*/ |
| 1139 |
int fdisk_is_details(struct fdisk_context *cxt) |
| 1140 |
{ |
| 1141 |
assert(cxt); |
| 1142 |
return cxt->display_details == 1; |
| 1143 |
} |
| 1144 |
|
| 1145 |
/** |
| 1146 |
* fdisk_enable_listonly: |
| 1147 |
* @cxt: context |
| 1148 |
* @enable: true/false |
| 1149 |
* |
| 1150 |
* Just list partition only, don't care about another details, mistakes, ... |
| 1151 |
* |
| 1152 |
* Returns: 0 on success, < 0 on error. |
| 1153 |
*/ |
| 1154 |
int fdisk_enable_listonly(struct fdisk_context *cxt, int enable) |
| 1155 |
{ |
| 1156 |
assert(cxt); |
| 1157 |
cxt->listonly = enable ? 1 : 0; |
| 1158 |
return 0; |
| 1159 |
} |
| 1160 |
|
| 1161 |
/** |
| 1162 |
* fdisk_is_listonly: |
| 1163 |
* @cxt: context |
| 1164 |
* |
| 1165 |
* Returns: 1 if list-only mode enabled |
| 1166 |
*/ |
| 1167 |
int fdisk_is_listonly(struct fdisk_context *cxt) |
| 1168 |
{ |
| 1169 |
assert(cxt); |
| 1170 |
return cxt->listonly == 1; |
| 1171 |
} |
| 1172 |
|
| 1173 |
|
| 1174 |
/** |
| 1175 |
* fdisk_set_unit: |
| 1176 |
* @cxt: context |
| 1177 |
* @str: "cylinder" or "sector". |
| 1178 |
* |
| 1179 |
* This is pure shit, unfortunately for example Sun addresses begin of the |
| 1180 |
* partition by cylinders... |
| 1181 |
* |
| 1182 |
* Returns: 0 on success, <0 on error. |
| 1183 |
*/ |
| 1184 |
int fdisk_set_unit(struct fdisk_context *cxt, const char *str) |
| 1185 |
{ |
| 1186 |
assert(cxt); |
| 1187 |
|
| 1188 |
cxt->display_in_cyl_units = 0; |
| 1189 |
|
| 1190 |
if (!str) |
| 1191 |
return 0; |
| 1192 |
|
| 1193 |
if (strcmp(str, "cylinder") == 0 || strcmp(str, "cylinders") == 0) |
| 1194 |
cxt->display_in_cyl_units = 1; |
| 1195 |
|
| 1196 |
else if (strcmp(str, "sector") == 0 || strcmp(str, "sectors") == 0) |
| 1197 |
cxt->display_in_cyl_units = 0; |
| 1198 |
|
| 1199 |
DBG(CXT, ul_debugobj(cxt, "display unit: %s", fdisk_get_unit(cxt, 0))); |
| 1200 |
return 0; |
| 1201 |
} |
| 1202 |
|
| 1203 |
/** |
| 1204 |
* fdisk_get_unit: |
| 1205 |
* @cxt: context |
| 1206 |
* @n: FDISK_PLURAL or FDISK_SINGULAR |
| 1207 |
* |
| 1208 |
* Returns: unit name. |
| 1209 |
*/ |
| 1210 |
const char *fdisk_get_unit(struct fdisk_context *cxt, int n) |
| 1211 |
{ |
| 1212 |
assert(cxt); |
| 1213 |
|
| 1214 |
if (fdisk_use_cylinders(cxt)) |
| 1215 |
return P_("cylinder", "cylinders", n); |
| 1216 |
return P_("sector", "sectors", n); |
| 1217 |
} |
| 1218 |
|
| 1219 |
/** |
| 1220 |
* fdisk_use_cylinders: |
| 1221 |
* @cxt: context |
| 1222 |
* |
| 1223 |
* Returns: 1 if user wants to display in cylinders. |
| 1224 |
*/ |
| 1225 |
int fdisk_use_cylinders(struct fdisk_context *cxt) |
| 1226 |
{ |
| 1227 |
assert(cxt); |
| 1228 |
return cxt->display_in_cyl_units == 1; |
| 1229 |
} |
| 1230 |
|
| 1231 |
/** |
| 1232 |
* fdisk_get_units_per_sector: |
| 1233 |
* @cxt: context |
| 1234 |
* |
| 1235 |
* This is necessary only for brain dead situations when we use "cylinders"; |
| 1236 |
* |
| 1237 |
* Returns: number of "units" per sector, default is 1 if display unit is sector. |
| 1238 |
*/ |
| 1239 |
unsigned int fdisk_get_units_per_sector(struct fdisk_context *cxt) |
| 1240 |
{ |
| 1241 |
assert(cxt); |
| 1242 |
|
| 1243 |
if (fdisk_use_cylinders(cxt)) { |
| 1244 |
assert(cxt->geom.heads); |
| 1245 |
return cxt->geom.heads * cxt->geom.sectors; |
| 1246 |
} |
| 1247 |
return 1; |
| 1248 |
} |
| 1249 |
|
| 1250 |
/** |
| 1251 |
* fdisk_get_optimal_iosize: |
| 1252 |
* @cxt: context |
| 1253 |
* |
| 1254 |
* The optimal I/O is optional and does not have to be provided by device, |
| 1255 |
* anyway libfdisk never returns zero. If the optimal I/O size is not provided |
| 1256 |
* then libfdisk returns minimal I/O size or sector size. |
| 1257 |
* |
| 1258 |
* Returns: optimal I/O size in bytes. |
| 1259 |
*/ |
| 1260 |
unsigned long fdisk_get_optimal_iosize(struct fdisk_context *cxt) |
| 1261 |
{ |
| 1262 |
assert(cxt); |
| 1263 |
return cxt->optimal_io_size ? cxt->optimal_io_size : cxt->io_size; |
| 1264 |
} |
| 1265 |
|
| 1266 |
/** |
| 1267 |
* fdisk_get_minimal_iosize: |
| 1268 |
* @cxt: context |
| 1269 |
* |
| 1270 |
* Returns: minimal I/O size in bytes |
| 1271 |
*/ |
| 1272 |
unsigned long fdisk_get_minimal_iosize(struct fdisk_context *cxt) |
| 1273 |
{ |
| 1274 |
assert(cxt); |
| 1275 |
return cxt->min_io_size; |
| 1276 |
} |
| 1277 |
|
| 1278 |
/** |
| 1279 |
* fdisk_get_physector_size: |
| 1280 |
* @cxt: context |
| 1281 |
* |
| 1282 |
* Returns: physical sector size in bytes |
| 1283 |
*/ |
| 1284 |
unsigned long fdisk_get_physector_size(struct fdisk_context *cxt) |
| 1285 |
{ |
| 1286 |
assert(cxt); |
| 1287 |
return cxt->phy_sector_size; |
| 1288 |
} |
| 1289 |
|
| 1290 |
/** |
| 1291 |
* fdisk_get_sector_size: |
| 1292 |
* @cxt: context |
| 1293 |
* |
| 1294 |
* Returns: logical sector size in bytes |
| 1295 |
*/ |
| 1296 |
unsigned long fdisk_get_sector_size(struct fdisk_context *cxt) |
| 1297 |
{ |
| 1298 |
assert(cxt); |
| 1299 |
return cxt->sector_size; |
| 1300 |
} |
| 1301 |
|
| 1302 |
/** |
| 1303 |
* fdisk_get_alignment_offset |
| 1304 |
* @cxt: context |
| 1305 |
* |
| 1306 |
* The alignment offset is offset between logical and physical sectors. For |
| 1307 |
* backward compatibility the first logical sector on 4K disks does no have to |
| 1308 |
* start on the same place like physical sectors. |
| 1309 |
* |
| 1310 |
* Returns: alignment offset in bytes |
| 1311 |
*/ |
| 1312 |
unsigned long fdisk_get_alignment_offset(struct fdisk_context *cxt) |
| 1313 |
{ |
| 1314 |
assert(cxt); |
| 1315 |
return cxt->alignment_offset; |
| 1316 |
} |
| 1317 |
|
| 1318 |
/** |
| 1319 |
* fdisk_get_grain_size: |
| 1320 |
* @cxt: context |
| 1321 |
* |
| 1322 |
* Returns: grain in bytes used to align partitions (usually 1MiB) |
| 1323 |
*/ |
| 1324 |
unsigned long fdisk_get_grain_size(struct fdisk_context *cxt) |
| 1325 |
{ |
| 1326 |
assert(cxt); |
| 1327 |
return cxt->grain; |
| 1328 |
} |
| 1329 |
|
| 1330 |
/** |
| 1331 |
* fdisk_get_first_lba: |
| 1332 |
* @cxt: context |
| 1333 |
* |
| 1334 |
* Returns: first possible LBA on disk for data partitions. |
| 1335 |
*/ |
| 1336 |
fdisk_sector_t fdisk_get_first_lba(struct fdisk_context *cxt) |
| 1337 |
{ |
| 1338 |
assert(cxt); |
| 1339 |
return cxt->first_lba; |
| 1340 |
} |
| 1341 |
|
| 1342 |
/** |
| 1343 |
* fdisk_set_first_lba: |
| 1344 |
* @cxt: fdisk context |
| 1345 |
* @lba: first possible logical sector for data |
| 1346 |
* |
| 1347 |
* It's strongly recommended to use the default library setting. The first LBA |
| 1348 |
* is always reset by fdisk_assign_device(), fdisk_override_geometry() |
| 1349 |
* and fdisk_reset_alignment(). This is very low level function and library |
| 1350 |
* does not check if your setting makes any sense. |
| 1351 |
* |
| 1352 |
* This function is necessary only when you want to work with very unusual |
| 1353 |
* partition tables like GPT protective MBR or hybrid partition tables on |
| 1354 |
* bootable media where the first partition may start on very crazy offsets. |
| 1355 |
* |
| 1356 |
* Note that this function changes only runtime information. It does not update |
| 1357 |
* any range in on-disk partition table. For example GPT Header contains First |
| 1358 |
* and Last usable LBA fields. These fields are not updated by this function. |
| 1359 |
* Be careful. |
| 1360 |
* |
| 1361 |
* Returns: 0 on success, <0 on error. |
| 1362 |
*/ |
| 1363 |
fdisk_sector_t fdisk_set_first_lba(struct fdisk_context *cxt, fdisk_sector_t lba) |
| 1364 |
{ |
| 1365 |
assert(cxt); |
| 1366 |
DBG(CXT, ul_debugobj(cxt, "setting first LBA from %ju to %ju", |
| 1367 |
(uintmax_t) cxt->first_lba, (uintmax_t) lba)); |
| 1368 |
cxt->first_lba = lba; |
| 1369 |
return 0; |
| 1370 |
} |
| 1371 |
|
| 1372 |
/** |
| 1373 |
* fdisk_get_last_lba: |
| 1374 |
* @cxt: fdisk context |
| 1375 |
* |
| 1376 |
* Note that the device has to be already assigned. |
| 1377 |
* |
| 1378 |
* Returns: last possible LBA on device |
| 1379 |
*/ |
| 1380 |
fdisk_sector_t fdisk_get_last_lba(struct fdisk_context *cxt) |
| 1381 |
{ |
| 1382 |
return cxt->last_lba; |
| 1383 |
} |
| 1384 |
|
| 1385 |
/** |
| 1386 |
* fdisk_set_last_lba: |
| 1387 |
* @cxt: fdisk context |
| 1388 |
* @lba: last possible logical sector |
| 1389 |
* |
| 1390 |
* It's strongly recommended to use the default library setting. The last LBA |
| 1391 |
* is always reset by fdisk_assign_device(), fdisk_override_geometry() and |
| 1392 |
* fdisk_reset_alignment(). |
| 1393 |
* |
| 1394 |
* The default is number of sectors on the device, but maybe modified by the |
| 1395 |
* current disklabel driver (for example GPT uses the end of disk for backup |
| 1396 |
* header, so last_lba is smaller than total number of sectors). |
| 1397 |
* |
| 1398 |
* Returns: 0 on success, <0 on error. |
| 1399 |
*/ |
| 1400 |
fdisk_sector_t fdisk_set_last_lba(struct fdisk_context *cxt, fdisk_sector_t lba) |
| 1401 |
{ |
| 1402 |
assert(cxt); |
| 1403 |
|
| 1404 |
if (lba > cxt->total_sectors - 1 || lba < 1) |
| 1405 |
return -ERANGE; |
| 1406 |
cxt->last_lba = lba; |
| 1407 |
return 0; |
| 1408 |
} |
| 1409 |
|
| 1410 |
/** |
| 1411 |
* fdisk_set_size_unit: |
| 1412 |
* @cxt: fdisk context |
| 1413 |
* @unit: FDISK_SIZEUNIT_* |
| 1414 |
* |
| 1415 |
* Sets unit for SIZE output field (see fdisk_partition_to_string()). |
| 1416 |
* |
| 1417 |
* Returns: 0 on success, <0 on error. |
| 1418 |
*/ |
| 1419 |
int fdisk_set_size_unit(struct fdisk_context *cxt, int unit) |
| 1420 |
{ |
| 1421 |
assert(cxt); |
| 1422 |
cxt->sizeunit = unit; |
| 1423 |
return 0; |
| 1424 |
} |
| 1425 |
|
| 1426 |
/** |
| 1427 |
* fdisk_get_size_unit: |
| 1428 |
* @cxt: fdisk context |
| 1429 |
* |
| 1430 |
* Gets unit for SIZE output field (see fdisk_partition_to_string()). |
| 1431 |
* |
| 1432 |
* Returns: unit |
| 1433 |
*/ |
| 1434 |
int fdisk_get_size_unit(struct fdisk_context *cxt) |
| 1435 |
{ |
| 1436 |
assert(cxt); |
| 1437 |
return cxt->sizeunit; |
| 1438 |
} |
| 1439 |
|
| 1440 |
/** |
| 1441 |
* fdisk_get_nsectors: |
| 1442 |
* @cxt: context |
| 1443 |
* |
| 1444 |
* Returns: size of the device in logical sectors. |
| 1445 |
*/ |
| 1446 |
fdisk_sector_t fdisk_get_nsectors(struct fdisk_context *cxt) |
| 1447 |
{ |
| 1448 |
assert(cxt); |
| 1449 |
return cxt->total_sectors; |
| 1450 |
} |
| 1451 |
|
| 1452 |
/** |
| 1453 |
* fdisk_get_devname: |
| 1454 |
* @cxt: context |
| 1455 |
* |
| 1456 |
* Returns: device name. |
| 1457 |
*/ |
| 1458 |
const char *fdisk_get_devname(struct fdisk_context *cxt) |
| 1459 |
{ |
| 1460 |
assert(cxt); |
| 1461 |
return cxt->dev_path; |
| 1462 |
} |
| 1463 |
|
| 1464 |
/** |
| 1465 |
* fdisk_get_devno: |
| 1466 |
* @cxt: context |
| 1467 |
* |
| 1468 |
* Returns: device number or zero for non-block devices |
| 1469 |
* |
| 1470 |
* Since: 2.33 |
| 1471 |
*/ |
| 1472 |
dev_t fdisk_get_devno(struct fdisk_context *cxt) |
| 1473 |
{ |
| 1474 |
assert(cxt); |
| 1475 |
return S_ISBLK(cxt->dev_st.st_mode) ? cxt->dev_st.st_rdev : 0; |
| 1476 |
} |
| 1477 |
|
| 1478 |
/** |
| 1479 |
* fdisk_get_devmodel: |
| 1480 |
* @cxt: context |
| 1481 |
* |
| 1482 |
* Returns: device model string or NULL. |
| 1483 |
* |
| 1484 |
* Since: 2.33 |
| 1485 |
*/ |
| 1486 |
#ifdef __linux__ |
| 1487 |
const char *fdisk_get_devmodel(struct fdisk_context *cxt) |
| 1488 |
{ |
| 1489 |
assert(cxt); |
| 1490 |
|
| 1491 |
if (cxt->dev_model_probed) |
| 1492 |
return cxt->dev_model; |
| 1493 |
|
| 1494 |
if (fdisk_get_devno(cxt)) { |
| 1495 |
struct path_cxt *pc = ul_new_sysfs_path(fdisk_get_devno(cxt), NULL, NULL); |
| 1496 |
|
| 1497 |
if (pc) { |
| 1498 |
ul_path_read_string(pc, &cxt->dev_model, "device/model"); |
| 1499 |
ul_unref_path(pc); |
| 1500 |
} |
| 1501 |
} |
| 1502 |
cxt->dev_model_probed = 1; |
| 1503 |
return cxt->dev_model; |
| 1504 |
} |
| 1505 |
#else |
| 1506 |
const char *fdisk_get_devmodel(struct fdisk_context *cxt __attribute__((__unused__))) |
| 1507 |
{ |
| 1508 |
return NULL; |
| 1509 |
} |
| 1510 |
#endif |
| 1511 |
|
| 1512 |
/** |
| 1513 |
* fdisk_get_devfd: |
| 1514 |
* @cxt: context |
| 1515 |
* |
| 1516 |
* Returns: device file descriptor. |
| 1517 |
*/ |
| 1518 |
int fdisk_get_devfd(struct fdisk_context *cxt) |
| 1519 |
{ |
| 1520 |
assert(cxt); |
| 1521 |
return cxt->dev_fd; |
| 1522 |
} |
| 1523 |
|
| 1524 |
/** |
| 1525 |
* fdisk_get_geom_heads: |
| 1526 |
* @cxt: context |
| 1527 |
* |
| 1528 |
* Returns: number of geometry heads. |
| 1529 |
*/ |
| 1530 |
unsigned int fdisk_get_geom_heads(struct fdisk_context *cxt) |
| 1531 |
{ |
| 1532 |
assert(cxt); |
| 1533 |
return cxt->geom.heads; |
| 1534 |
} |
| 1535 |
/** |
| 1536 |
* fdisk_get_geom_sectors: |
| 1537 |
* @cxt: context |
| 1538 |
* |
| 1539 |
* Returns: number of geometry sectors. |
| 1540 |
*/ |
| 1541 |
fdisk_sector_t fdisk_get_geom_sectors(struct fdisk_context *cxt) |
| 1542 |
{ |
| 1543 |
assert(cxt); |
| 1544 |
return cxt->geom.sectors; |
| 1545 |
|
| 1546 |
} |
| 1547 |
|
| 1548 |
/** |
| 1549 |
* fdisk_get_geom_cylinders: |
| 1550 |
* @cxt: context |
| 1551 |
* |
| 1552 |
* Returns: number of geometry cylinders |
| 1553 |
*/ |
| 1554 |
fdisk_sector_t fdisk_get_geom_cylinders(struct fdisk_context *cxt) |
| 1555 |
{ |
| 1556 |
assert(cxt); |
| 1557 |
return cxt->geom.cylinders; |
| 1558 |
} |
| 1559 |
|
| 1560 |
int fdisk_missing_geometry(struct fdisk_context *cxt) |
| 1561 |
{ |
| 1562 |
int rc; |
| 1563 |
|
| 1564 |
if (!cxt || !cxt->label) |
| 1565 |
return 0; |
| 1566 |
|
| 1567 |
rc = (fdisk_label_require_geometry(cxt->label) && |
| 1568 |
(!cxt->geom.heads || !cxt->geom.sectors |
| 1569 |
|| !cxt->geom.cylinders)); |
| 1570 |
|
| 1571 |
if (rc && !fdisk_is_listonly(cxt)) |
| 1572 |
fdisk_warnx(cxt, _("Incomplete geometry setting.")); |
| 1573 |
|
| 1574 |
return rc; |
| 1575 |
} |
| 1576 |
|