| 1 |
|
| 2 |
#ifdef HAVE_LIBBLKID |
| 3 |
#include <blkid.h> |
| 4 |
#endif |
| 5 |
#include "blkdev.h" |
| 6 |
|
| 7 |
#include "fdiskP.h" |
| 8 |
|
| 9 |
/** |
| 10 |
* SECTION: alignment |
| 11 |
* @title: Alignment |
| 12 |
* @short_description: functions to align partitions and work with disk topology and geometry |
| 13 |
* |
| 14 |
* The libfdisk aligns the end of the partitions to make it possible to align |
| 15 |
* the next partition to the "grain" (see fdisk_get_grain_size()). The grain is |
| 16 |
* usually 1MiB (or more for devices where optimal I/O is greater than 1MiB). |
| 17 |
* |
| 18 |
* It means that the library does not align strictly to physical sector size |
| 19 |
* (or minimal or optimal I/O), but it uses greater granularity. It makes |
| 20 |
* partition tables more portable. If you copy disk layout from 512-sector to |
| 21 |
* 4K-sector device, all partitions are still aligned to physical sectors. |
| 22 |
* |
| 23 |
* This unified concept also makes partition tables more user friendly, all |
| 24 |
* tables look same, LBA of the first partition is 2048 sectors everywhere, etc. |
| 25 |
* |
| 26 |
* It's recommended to not change any alignment or device properties. All is |
| 27 |
* initialized by default by fdisk_assign_device(). |
| 28 |
* |
| 29 |
* Note that terminology used by libfdisk is: |
| 30 |
* - device properties: I/O limits (topology), geometry, sector size, ... |
| 31 |
* - alignment: first, last LBA, grain, ... |
| 32 |
* |
| 33 |
* The alignment setting may be modified by disk label driver. |
| 34 |
*/ |
| 35 |
|
| 36 |
/* |
| 37 |
* Alignment according to logical granularity (usually 1MiB) |
| 38 |
*/ |
| 39 |
static int lba_is_aligned(struct fdisk_context *cxt, uintmax_t lba) |
| 40 |
{ |
| 41 |
unsigned long granularity = max(cxt->phy_sector_size, cxt->min_io_size); |
| 42 |
uintmax_t offset; |
| 43 |
|
| 44 |
if (cxt->grain > granularity) |
| 45 |
granularity = cxt->grain; |
| 46 |
|
| 47 |
offset = (lba * cxt->sector_size) % granularity; |
| 48 |
|
| 49 |
return !((granularity + cxt->alignment_offset - offset) % granularity); |
| 50 |
} |
| 51 |
|
| 52 |
/* |
| 53 |
* Alignment according to physical device topology (usually minimal i/o size) |
| 54 |
*/ |
| 55 |
static int lba_is_phy_aligned(struct fdisk_context *cxt, fdisk_sector_t lba) |
| 56 |
{ |
| 57 |
unsigned long granularity = max(cxt->phy_sector_size, cxt->min_io_size); |
| 58 |
uintmax_t offset = (lba * cxt->sector_size) % granularity; |
| 59 |
|
| 60 |
return !((granularity + cxt->alignment_offset - offset) % granularity); |
| 61 |
} |
| 62 |
|
| 63 |
/** |
| 64 |
* fdisk_align_lba: |
| 65 |
* @cxt: context |
| 66 |
* @lba: address to align |
| 67 |
* @direction: FDISK_ALIGN_{UP,DOWN,NEAREST} |
| 68 |
* |
| 69 |
* This function aligns @lba to the "grain" (see fdisk_get_grain_size()). If the |
| 70 |
* device uses alignment offset then the result is moved according the offset |
| 71 |
* to be on the physical boundary. |
| 72 |
* |
| 73 |
* Returns: alignment LBA. |
| 74 |
*/ |
| 75 |
fdisk_sector_t fdisk_align_lba(struct fdisk_context *cxt, fdisk_sector_t lba, int direction) |
| 76 |
{ |
| 77 |
fdisk_sector_t res; |
| 78 |
|
| 79 |
if (lba_is_aligned(cxt, lba)) |
| 80 |
res = lba; |
| 81 |
else { |
| 82 |
fdisk_sector_t sects_in_phy = cxt->grain / cxt->sector_size; |
| 83 |
|
| 84 |
if (lba < cxt->first_lba) |
| 85 |
res = cxt->first_lba; |
| 86 |
|
| 87 |
else if (direction == FDISK_ALIGN_UP) |
| 88 |
res = ((lba + sects_in_phy) / sects_in_phy) * sects_in_phy; |
| 89 |
|
| 90 |
else if (direction == FDISK_ALIGN_DOWN) |
| 91 |
res = (lba / sects_in_phy) * sects_in_phy; |
| 92 |
|
| 93 |
else /* FDISK_ALIGN_NEAREST */ |
| 94 |
res = ((lba + sects_in_phy / 2) / sects_in_phy) * sects_in_phy; |
| 95 |
|
| 96 |
if (cxt->alignment_offset && !lba_is_aligned(cxt, res) && |
| 97 |
res > cxt->alignment_offset / cxt->sector_size) { |
| 98 |
/* |
| 99 |
* apply alignment_offset |
| 100 |
* |
| 101 |
* On disk with alignment compensation physical blocks starts |
| 102 |
* at LBA < 0 (usually LBA -1). It means we have to move LBA |
| 103 |
* according the offset to be on the physical boundary. |
| 104 |
*/ |
| 105 |
/* fprintf(stderr, "LBA: %llu apply alignment_offset\n", res); */ |
| 106 |
res -= (max(cxt->phy_sector_size, cxt->min_io_size) - |
| 107 |
cxt->alignment_offset) / cxt->sector_size; |
| 108 |
|
| 109 |
if (direction == FDISK_ALIGN_UP && res < lba) |
| 110 |
res += sects_in_phy; |
| 111 |
} |
| 112 |
} |
| 113 |
/* |
| 114 |
if (lba != res) |
| 115 |
DBG(CXT, ul_debugobj(cxt, "LBA %12ju aligned-%s %12ju [grain=%lus]", |
| 116 |
(uintmax_t) lba, |
| 117 |
direction == FDISK_ALIGN_UP ? "up " : |
| 118 |
direction == FDISK_ALIGN_DOWN ? "down" : "near", |
| 119 |
(uintmax_t) res, |
| 120 |
cxt->grain / cxt->sector_size)); |
| 121 |
else |
| 122 |
DBG(CXT, ul_debugobj(cxt, "LBA %12ju already aligned", (uintmax_t)lba)); |
| 123 |
*/ |
| 124 |
return res; |
| 125 |
} |
| 126 |
|
| 127 |
/** |
| 128 |
* fdisk_align_lba_in_range: |
| 129 |
* @cxt: context |
| 130 |
* @lba: LBA |
| 131 |
* @start: range start |
| 132 |
* @stop: range stop |
| 133 |
* |
| 134 |
* Align @lba, the result has to be between @start and @stop |
| 135 |
* |
| 136 |
* Returns: aligned LBA |
| 137 |
*/ |
| 138 |
fdisk_sector_t fdisk_align_lba_in_range(struct fdisk_context *cxt, |
| 139 |
fdisk_sector_t lba, fdisk_sector_t start, fdisk_sector_t stop) |
| 140 |
{ |
| 141 |
fdisk_sector_t res; |
| 142 |
|
| 143 |
/*DBG(CXT, ul_debugobj(cxt, "LBA: align in range <%ju..%ju>", (uintmax_t) start, (uintmax_t) stop));*/ |
| 144 |
|
| 145 |
if (start + (cxt->grain / cxt->sector_size) <= stop) { |
| 146 |
start = fdisk_align_lba(cxt, start, FDISK_ALIGN_UP); |
| 147 |
stop = fdisk_align_lba(cxt, stop, FDISK_ALIGN_DOWN); |
| 148 |
} |
| 149 |
|
| 150 |
if (start + (cxt->grain / cxt->sector_size) > stop) { |
| 151 |
DBG(CXT, ul_debugobj(cxt, "LBA: area smaller than grain, don't align")); |
| 152 |
res = lba; |
| 153 |
goto done; |
| 154 |
} |
| 155 |
|
| 156 |
lba = fdisk_align_lba(cxt, lba, FDISK_ALIGN_NEAREST); |
| 157 |
|
| 158 |
if (lba < start) |
| 159 |
res = start; |
| 160 |
else if (lba > stop) |
| 161 |
res = stop; |
| 162 |
else |
| 163 |
res = lba; |
| 164 |
done: |
| 165 |
DBG(CXT, ul_debugobj(cxt, "%ju in range <%ju..%ju> aligned to %ju", |
| 166 |
(uintmax_t) lba, |
| 167 |
(uintmax_t) start, |
| 168 |
(uintmax_t) stop, |
| 169 |
(uintmax_t) res)); |
| 170 |
return res; |
| 171 |
} |
| 172 |
|
| 173 |
/** |
| 174 |
* fdisk_lba_is_phy_aligned: |
| 175 |
* @cxt: context |
| 176 |
* @lba: LBA to check |
| 177 |
* |
| 178 |
* Check if the @lba is aligned to physical sector boundary. |
| 179 |
* |
| 180 |
* Returns: 1 if aligned. |
| 181 |
*/ |
| 182 |
int fdisk_lba_is_phy_aligned(struct fdisk_context *cxt, fdisk_sector_t lba) |
| 183 |
{ |
| 184 |
return lba_is_phy_aligned(cxt, lba); |
| 185 |
} |
| 186 |
|
| 187 |
static unsigned long get_sector_size(struct fdisk_context *cxt) |
| 188 |
{ |
| 189 |
int sect_sz; |
| 190 |
|
| 191 |
if (!fdisk_is_regfile(cxt) && |
| 192 |
!blkdev_get_sector_size(cxt->dev_fd, §_sz)) |
| 193 |
return (unsigned long) sect_sz; |
| 194 |
|
| 195 |
return DEFAULT_SECTOR_SIZE; |
| 196 |
} |
| 197 |
|
| 198 |
static void recount_geometry(struct fdisk_context *cxt) |
| 199 |
{ |
| 200 |
if (!cxt->geom.heads) |
| 201 |
cxt->geom.heads = 255; |
| 202 |
if (!cxt->geom.sectors) |
| 203 |
cxt->geom.sectors = 63; |
| 204 |
|
| 205 |
cxt->geom.cylinders = cxt->total_sectors / |
| 206 |
(cxt->geom.heads * cxt->geom.sectors); |
| 207 |
} |
| 208 |
|
| 209 |
/** |
| 210 |
* fdisk_override_geometry: |
| 211 |
* @cxt: fdisk context |
| 212 |
* @cylinders: user specified cylinders |
| 213 |
* @heads: user specified heads |
| 214 |
* @sectors: user specified sectors |
| 215 |
* |
| 216 |
* Overrides auto-discovery. The function fdisk_reset_device_properties() |
| 217 |
* restores the original setting. |
| 218 |
* |
| 219 |
* The difference between fdisk_override_geometry() and fdisk_save_user_geometry() |
| 220 |
* is that saved user geometry is persistent setting and it's applied always |
| 221 |
* when device is assigned to the context or device properties are reset. |
| 222 |
* |
| 223 |
* Returns: 0 on success, < 0 on error. |
| 224 |
*/ |
| 225 |
int fdisk_override_geometry(struct fdisk_context *cxt, |
| 226 |
unsigned int cylinders, |
| 227 |
unsigned int heads, |
| 228 |
unsigned int sectors) |
| 229 |
{ |
| 230 |
int rc = 0; |
| 231 |
|
| 232 |
if (!cxt) |
| 233 |
return -EINVAL; |
| 234 |
if (heads) |
| 235 |
cxt->geom.heads = heads; |
| 236 |
if (sectors) |
| 237 |
cxt->geom.sectors = sectors; |
| 238 |
|
| 239 |
if (cylinders) |
| 240 |
cxt->geom.cylinders = cylinders; |
| 241 |
else |
| 242 |
recount_geometry(cxt); |
| 243 |
|
| 244 |
rc = fdisk_reset_alignment(cxt); |
| 245 |
|
| 246 |
DBG(CXT, ul_debugobj(cxt, "override C/H/S: %u/%u/%u [rc=%d]", |
| 247 |
(unsigned) cxt->geom.cylinders, |
| 248 |
(unsigned) cxt->geom.heads, |
| 249 |
(unsigned) cxt->geom.sectors, |
| 250 |
rc)); |
| 251 |
|
| 252 |
return rc; |
| 253 |
} |
| 254 |
|
| 255 |
/** |
| 256 |
* fdisk_save_user_geometry: |
| 257 |
* @cxt: context |
| 258 |
* @cylinders: C |
| 259 |
* @heads: H |
| 260 |
* @sectors: S |
| 261 |
* |
| 262 |
* Save user defined geometry to use it for partitioning. |
| 263 |
* |
| 264 |
* The user properties are applied by fdisk_assign_device() or |
| 265 |
* fdisk_reset_device_properties(). |
| 266 |
|
| 267 |
* Returns: <0 on error, 0 on success. |
| 268 |
*/ |
| 269 |
int fdisk_save_user_geometry(struct fdisk_context *cxt, |
| 270 |
unsigned int cylinders, |
| 271 |
unsigned int heads, |
| 272 |
unsigned int sectors) |
| 273 |
{ |
| 274 |
if (!cxt) |
| 275 |
return -EINVAL; |
| 276 |
|
| 277 |
if (heads) |
| 278 |
cxt->user_geom.heads = heads > 256 ? 0 : heads; |
| 279 |
if (sectors) |
| 280 |
cxt->user_geom.sectors = sectors >= 64 ? 0 : sectors; |
| 281 |
if (cylinders) |
| 282 |
cxt->user_geom.cylinders = cylinders; |
| 283 |
|
| 284 |
DBG(CXT, ul_debugobj(cxt, "user C/H/S: %u/%u/%u", |
| 285 |
(unsigned) cxt->user_geom.cylinders, |
| 286 |
(unsigned) cxt->user_geom.heads, |
| 287 |
(unsigned) cxt->user_geom.sectors)); |
| 288 |
|
| 289 |
return 0; |
| 290 |
} |
| 291 |
|
| 292 |
/** |
| 293 |
* fdisk_save_user_sector_size: |
| 294 |
* @cxt: context |
| 295 |
* @phy: physical sector size |
| 296 |
* @log: logical sector size |
| 297 |
* |
| 298 |
* Save user defined sector sizes to use it for partitioning. |
| 299 |
* |
| 300 |
* The user properties are applied by fdisk_assign_device() or |
| 301 |
* fdisk_reset_device_properties(). |
| 302 |
* |
| 303 |
* Returns: <0 on error, 0 on success. |
| 304 |
*/ |
| 305 |
int fdisk_save_user_sector_size(struct fdisk_context *cxt, |
| 306 |
unsigned int phy, |
| 307 |
unsigned int log) |
| 308 |
{ |
| 309 |
if (!cxt) |
| 310 |
return -EINVAL; |
| 311 |
|
| 312 |
DBG(CXT, ul_debugobj(cxt, "user phy/log sector size: %u/%u", phy, log)); |
| 313 |
|
| 314 |
cxt->user_pyh_sector = phy; |
| 315 |
cxt->user_log_sector = log; |
| 316 |
|
| 317 |
return 0; |
| 318 |
} |
| 319 |
|
| 320 |
/** |
| 321 |
* fdisk_save_user_grain: |
| 322 |
* @cxt: context |
| 323 |
* @grain: size in bytes (>= 512, multiple of 512) |
| 324 |
* |
| 325 |
* Save user define grain size. The size is used to align partitions. |
| 326 |
* |
| 327 |
* The default is 1MiB (or optimal I/O size if greater than 1MiB). It's strongly |
| 328 |
* recommended to use the default. |
| 329 |
* |
| 330 |
* The smallest possible granularity for partitioning is physical sector size |
| 331 |
* (or minimal I/O size; the bigger number win). If the user's @grain size is |
| 332 |
* too small then the smallest possible granularity is used. It means |
| 333 |
* fdisk_save_user_grain(cxt, 512) forces libfdisk to use grain as small as |
| 334 |
* possible. |
| 335 |
* |
| 336 |
* The setting is applied by fdisk_assign_device() or |
| 337 |
* fdisk_reset_device_properties(). |
| 338 |
* |
| 339 |
* Returns: <0 on error, 0 on success. |
| 340 |
*/ |
| 341 |
int fdisk_save_user_grain(struct fdisk_context *cxt, unsigned long grain) |
| 342 |
{ |
| 343 |
if (!cxt || grain % 512) |
| 344 |
return -EINVAL; |
| 345 |
|
| 346 |
DBG(CXT, ul_debugobj(cxt, "user grain size: %lu", grain)); |
| 347 |
cxt->user_grain = grain; |
| 348 |
return 0; |
| 349 |
} |
| 350 |
|
| 351 |
/** |
| 352 |
* fdisk_has_user_device_properties: |
| 353 |
* @cxt: context |
| 354 |
* |
| 355 |
* Returns: 1 if user specified any properties |
| 356 |
*/ |
| 357 |
int fdisk_has_user_device_properties(struct fdisk_context *cxt) |
| 358 |
{ |
| 359 |
return (cxt->user_pyh_sector || cxt->user_log_sector || |
| 360 |
cxt->user_grain || |
| 361 |
fdisk_has_user_device_geometry(cxt)); |
| 362 |
} |
| 363 |
|
| 364 |
int fdisk_has_user_device_geometry(struct fdisk_context *cxt) |
| 365 |
{ |
| 366 |
return (cxt->user_geom.heads || cxt->user_geom.sectors || cxt->user_geom.cylinders); |
| 367 |
} |
| 368 |
|
| 369 |
int fdisk_apply_user_device_properties(struct fdisk_context *cxt) |
| 370 |
{ |
| 371 |
int rc; |
| 372 |
|
| 373 |
if (!cxt) |
| 374 |
return -EINVAL; |
| 375 |
|
| 376 |
DBG(CXT, ul_debugobj(cxt, "applying user device properties")); |
| 377 |
|
| 378 |
if (cxt->user_pyh_sector) |
| 379 |
cxt->phy_sector_size = cxt->user_pyh_sector; |
| 380 |
if (cxt->user_log_sector) { |
| 381 |
uint64_t old_total = cxt->total_sectors; |
| 382 |
uint64_t old_secsz = cxt->sector_size; |
| 383 |
|
| 384 |
cxt->sector_size = cxt->min_io_size = |
| 385 |
cxt->io_size = cxt->user_log_sector; |
| 386 |
|
| 387 |
if (cxt->sector_size != old_secsz) { |
| 388 |
cxt->total_sectors = (old_total * (old_secsz/512)) / (cxt->sector_size >> 9); |
| 389 |
DBG(CXT, ul_debugobj(cxt, "new total sectors: %ju", (uintmax_t)cxt->total_sectors)); |
| 390 |
} |
| 391 |
} |
| 392 |
|
| 393 |
if (cxt->user_geom.heads) |
| 394 |
cxt->geom.heads = cxt->user_geom.heads; |
| 395 |
if (cxt->user_geom.sectors) |
| 396 |
cxt->geom.sectors = cxt->user_geom.sectors; |
| 397 |
|
| 398 |
if (cxt->user_geom.cylinders) |
| 399 |
cxt->geom.cylinders = cxt->user_geom.cylinders; |
| 400 |
else if (cxt->user_geom.heads || cxt->user_geom.sectors) |
| 401 |
recount_geometry(cxt); |
| 402 |
|
| 403 |
rc = fdisk_reset_alignment(cxt); |
| 404 |
if (rc) |
| 405 |
return rc; |
| 406 |
|
| 407 |
if (cxt->user_grain) { |
| 408 |
unsigned long granularity = max(cxt->phy_sector_size, cxt->min_io_size); |
| 409 |
|
| 410 |
cxt->grain = cxt->user_grain < granularity ? granularity : cxt->user_grain; |
| 411 |
DBG(CXT, ul_debugobj(cxt, "new grain: %lu", cxt->grain)); |
| 412 |
} |
| 413 |
|
| 414 |
if (cxt->firstsector_bufsz != cxt->sector_size) |
| 415 |
fdisk_read_firstsector(cxt); |
| 416 |
|
| 417 |
DBG(CXT, ul_debugobj(cxt, "new C/H/S: %u/%u/%u", |
| 418 |
(unsigned) cxt->geom.cylinders, |
| 419 |
(unsigned) cxt->geom.heads, |
| 420 |
(unsigned) cxt->geom.sectors)); |
| 421 |
DBG(CXT, ul_debugobj(cxt, "new log/phy sector size: %u/%u", |
| 422 |
(unsigned) cxt->sector_size, |
| 423 |
(unsigned) cxt->phy_sector_size)); |
| 424 |
|
| 425 |
return 0; |
| 426 |
} |
| 427 |
|
| 428 |
void fdisk_zeroize_device_properties(struct fdisk_context *cxt) |
| 429 |
{ |
| 430 |
assert(cxt); |
| 431 |
|
| 432 |
cxt->io_size = 0; |
| 433 |
cxt->optimal_io_size = 0; |
| 434 |
cxt->min_io_size = 0; |
| 435 |
cxt->phy_sector_size = 0; |
| 436 |
cxt->sector_size = 0; |
| 437 |
cxt->alignment_offset = 0; |
| 438 |
cxt->grain = 0; |
| 439 |
cxt->first_lba = 0; |
| 440 |
cxt->last_lba = 0; |
| 441 |
cxt->total_sectors = 0; |
| 442 |
|
| 443 |
memset(&cxt->geom, 0, sizeof(struct fdisk_geometry)); |
| 444 |
} |
| 445 |
|
| 446 |
/** |
| 447 |
* fdisk_reset_device_properties: |
| 448 |
* @cxt: context |
| 449 |
* |
| 450 |
* Resets and discovery topology (I/O limits), geometry, re-read the first |
| 451 |
* rector on the device if necessary and apply user device setting (geometry |
| 452 |
* and sector size), then initialize alignment according to label driver (see |
| 453 |
* fdisk_reset_alignment()). |
| 454 |
* |
| 455 |
* You don't have to use this function by default, fdisk_assign_device() is |
| 456 |
* smart enough to initialize all necessary setting. |
| 457 |
* |
| 458 |
* Returns: 0 on success, <0 on error. |
| 459 |
*/ |
| 460 |
int fdisk_reset_device_properties(struct fdisk_context *cxt) |
| 461 |
{ |
| 462 |
int rc; |
| 463 |
|
| 464 |
if (!cxt) |
| 465 |
return -EINVAL; |
| 466 |
|
| 467 |
DBG(CXT, ul_debugobj(cxt, "*** resetting device properties")); |
| 468 |
|
| 469 |
fdisk_zeroize_device_properties(cxt); |
| 470 |
fdisk_discover_topology(cxt); |
| 471 |
fdisk_discover_geometry(cxt); |
| 472 |
|
| 473 |
rc = fdisk_read_firstsector(cxt); |
| 474 |
if (rc) |
| 475 |
return rc; |
| 476 |
|
| 477 |
return fdisk_apply_user_device_properties(cxt); |
| 478 |
} |
| 479 |
|
| 480 |
/* |
| 481 |
* Generic (label independent) geometry |
| 482 |
*/ |
| 483 |
int fdisk_discover_geometry(struct fdisk_context *cxt) |
| 484 |
{ |
| 485 |
fdisk_sector_t nsects = 0; |
| 486 |
unsigned int h = 0, s = 0; |
| 487 |
|
| 488 |
assert(cxt); |
| 489 |
assert(cxt->geom.heads == 0); |
| 490 |
|
| 491 |
DBG(CXT, ul_debugobj(cxt, "%s: discovering geometry...", cxt->dev_path)); |
| 492 |
|
| 493 |
if (fdisk_is_regfile(cxt)) |
| 494 |
cxt->total_sectors = cxt->dev_st.st_size / cxt->sector_size; |
| 495 |
else { |
| 496 |
/* get number of 512-byte sectors, and convert it the real sectors */ |
| 497 |
if (!blkdev_get_sectors(cxt->dev_fd, (unsigned long long *) &nsects)) |
| 498 |
cxt->total_sectors = (nsects / (cxt->sector_size >> 9)); |
| 499 |
|
| 500 |
/* what the kernel/bios thinks the geometry is */ |
| 501 |
blkdev_get_geometry(cxt->dev_fd, &h, &s); |
| 502 |
} |
| 503 |
|
| 504 |
DBG(CXT, ul_debugobj(cxt, "total sectors: %ju (ioctl=%ju)", |
| 505 |
(uintmax_t) cxt->total_sectors, |
| 506 |
(uintmax_t) nsects)); |
| 507 |
|
| 508 |
cxt->geom.cylinders = 0; |
| 509 |
cxt->geom.heads = h; |
| 510 |
cxt->geom.sectors = s; |
| 511 |
|
| 512 |
/* obtained heads and sectors */ |
| 513 |
recount_geometry(cxt); |
| 514 |
|
| 515 |
DBG(CXT, ul_debugobj(cxt, "result: C/H/S: %u/%u/%u", |
| 516 |
(unsigned) cxt->geom.cylinders, |
| 517 |
(unsigned) cxt->geom.heads, |
| 518 |
(unsigned) cxt->geom.sectors)); |
| 519 |
return 0; |
| 520 |
} |
| 521 |
|
| 522 |
int fdisk_discover_topology(struct fdisk_context *cxt) |
| 523 |
{ |
| 524 |
#ifdef HAVE_LIBBLKID |
| 525 |
blkid_probe pr; |
| 526 |
#endif |
| 527 |
assert(cxt); |
| 528 |
assert(cxt->sector_size == 0); |
| 529 |
|
| 530 |
DBG(CXT, ul_debugobj(cxt, "%s: discovering topology...", cxt->dev_path)); |
| 531 |
#ifdef HAVE_LIBBLKID |
| 532 |
DBG(CXT, ul_debugobj(cxt, "initialize libblkid prober")); |
| 533 |
|
| 534 |
pr = blkid_new_probe(); |
| 535 |
if (pr && blkid_probe_set_device(pr, cxt->dev_fd, 0, 0) == 0) { |
| 536 |
blkid_topology tp = blkid_probe_get_topology(pr); |
| 537 |
|
| 538 |
if (tp) { |
| 539 |
cxt->min_io_size = blkid_topology_get_minimum_io_size(tp); |
| 540 |
cxt->optimal_io_size = blkid_topology_get_optimal_io_size(tp); |
| 541 |
cxt->phy_sector_size = blkid_topology_get_physical_sector_size(tp); |
| 542 |
cxt->alignment_offset = blkid_topology_get_alignment_offset(tp); |
| 543 |
|
| 544 |
/* I/O size used by fdisk */ |
| 545 |
cxt->io_size = cxt->optimal_io_size; |
| 546 |
if (!cxt->io_size) |
| 547 |
/* optimal I/O is optional, default to minimum IO */ |
| 548 |
cxt->io_size = cxt->min_io_size; |
| 549 |
|
| 550 |
if (cxt->io_size && cxt->phy_sector_size) { |
| 551 |
if (cxt->io_size == 33553920) { |
| 552 |
/* 33553920 (32 MiB - 512) is always a controller error */ |
| 553 |
DBG(CXT, ul_debugobj(cxt, "ignore bad I/O size 33553920")); |
| 554 |
cxt->io_size = cxt->phy_sector_size; |
| 555 |
} else if ((cxt->io_size % cxt->phy_sector_size) != 0) { |
| 556 |
/* ignore optimal I/O if not aligned to phy.sector size */ |
| 557 |
DBG(CXT, ul_debugobj(cxt, "ignore misaligned I/O size")); |
| 558 |
cxt->io_size = cxt->phy_sector_size; |
| 559 |
} |
| 560 |
} |
| 561 |
|
| 562 |
} |
| 563 |
} |
| 564 |
blkid_free_probe(pr); |
| 565 |
#endif |
| 566 |
|
| 567 |
cxt->sector_size = get_sector_size(cxt); |
| 568 |
if (!cxt->phy_sector_size) /* could not discover physical size */ |
| 569 |
cxt->phy_sector_size = cxt->sector_size; |
| 570 |
|
| 571 |
/* no blkid or error, use default values */ |
| 572 |
if (!cxt->min_io_size) |
| 573 |
cxt->min_io_size = cxt->sector_size; |
| 574 |
if (!cxt->io_size) |
| 575 |
cxt->io_size = cxt->sector_size; |
| 576 |
|
| 577 |
DBG(CXT, ul_debugobj(cxt, "result: log/phy sector size: %ld/%ld", |
| 578 |
cxt->sector_size, cxt->phy_sector_size)); |
| 579 |
DBG(CXT, ul_debugobj(cxt, "result: fdisk/optimal/minimal io: %ld/%ld/%ld", |
| 580 |
cxt->io_size, cxt->optimal_io_size, cxt->min_io_size)); |
| 581 |
return 0; |
| 582 |
} |
| 583 |
|
| 584 |
static int has_topology(struct fdisk_context *cxt) |
| 585 |
{ |
| 586 |
/* |
| 587 |
* Assume that the device provides topology info if |
| 588 |
* optimal_io_size is set or alignment_offset is set or |
| 589 |
* minimum_io_size is not power of 2. |
| 590 |
*/ |
| 591 |
if (cxt && |
| 592 |
(cxt->optimal_io_size || |
| 593 |
cxt->alignment_offset || |
| 594 |
!is_power_of_2(cxt->min_io_size))) |
| 595 |
return 1; |
| 596 |
return 0; |
| 597 |
} |
| 598 |
|
| 599 |
/* |
| 600 |
* The LBA of the first partition is based on the device geometry and topology. |
| 601 |
* This offset is generic (and recommended) for all labels. |
| 602 |
* |
| 603 |
* Returns: 0 on error or number of logical sectors. |
| 604 |
*/ |
| 605 |
static fdisk_sector_t topology_get_first_lba(struct fdisk_context *cxt) |
| 606 |
{ |
| 607 |
fdisk_sector_t x = 0, res; |
| 608 |
|
| 609 |
if (!cxt) |
| 610 |
return 0; |
| 611 |
|
| 612 |
if (!cxt->io_size) |
| 613 |
fdisk_discover_topology(cxt); |
| 614 |
|
| 615 |
/* |
| 616 |
* Align the begin of partitions to: |
| 617 |
* |
| 618 |
* a) topology |
| 619 |
* a2) alignment offset |
| 620 |
* a1) or physical sector (minimal_io_size, aka "grain") |
| 621 |
* |
| 622 |
* b) or default to 1MiB (2048 sectors, Windows Vista default) |
| 623 |
* |
| 624 |
* c) or for very small devices use 1 phy.sector |
| 625 |
*/ |
| 626 |
if (has_topology(cxt)) { |
| 627 |
if (cxt->alignment_offset) |
| 628 |
x = cxt->alignment_offset; |
| 629 |
else if (cxt->io_size > 2048 * 512) |
| 630 |
x = cxt->io_size; |
| 631 |
} |
| 632 |
/* default to 1MiB */ |
| 633 |
if (!x) |
| 634 |
x = 2048 * 512; |
| 635 |
|
| 636 |
res = x / cxt->sector_size; |
| 637 |
|
| 638 |
/* don't use huge offset on small devices */ |
| 639 |
if (cxt->total_sectors <= res * 4) |
| 640 |
res = cxt->phy_sector_size / cxt->sector_size; |
| 641 |
|
| 642 |
return res; |
| 643 |
} |
| 644 |
|
| 645 |
static unsigned long topology_get_grain(struct fdisk_context *cxt) |
| 646 |
{ |
| 647 |
unsigned long res; |
| 648 |
|
| 649 |
if (!cxt) |
| 650 |
return 0; |
| 651 |
|
| 652 |
if (!cxt->io_size) |
| 653 |
fdisk_discover_topology(cxt); |
| 654 |
|
| 655 |
res = cxt->io_size; |
| 656 |
|
| 657 |
/* use 1MiB grain always when possible */ |
| 658 |
if (res < 2048 * 512) |
| 659 |
res = 2048 * 512; |
| 660 |
|
| 661 |
/* don't use huge grain on small devices */ |
| 662 |
if (cxt->total_sectors <= (res * 4 / cxt->sector_size)) |
| 663 |
res = cxt->phy_sector_size; |
| 664 |
|
| 665 |
return res; |
| 666 |
} |
| 667 |
|
| 668 |
/* apply label alignment setting to the context -- if not sure use |
| 669 |
* fdisk_reset_alignment() |
| 670 |
*/ |
| 671 |
int fdisk_apply_label_device_properties(struct fdisk_context *cxt) |
| 672 |
{ |
| 673 |
int rc = 0; |
| 674 |
|
| 675 |
if (cxt->label && cxt->label->op->reset_alignment) { |
| 676 |
DBG(CXT, ul_debugobj(cxt, "applying label device properties...")); |
| 677 |
rc = cxt->label->op->reset_alignment(cxt); |
| 678 |
} |
| 679 |
return rc; |
| 680 |
} |
| 681 |
|
| 682 |
/** |
| 683 |
* fdisk_reset_alignment: |
| 684 |
* @cxt: fdisk context |
| 685 |
* |
| 686 |
* Resets alignment setting to the default and label specific values. This |
| 687 |
* function does not change device properties (I/O limits, geometry etc.). |
| 688 |
* |
| 689 |
* Returns: 0 on success, < 0 in case of error. |
| 690 |
*/ |
| 691 |
int fdisk_reset_alignment(struct fdisk_context *cxt) |
| 692 |
{ |
| 693 |
int rc = 0; |
| 694 |
|
| 695 |
if (!cxt) |
| 696 |
return -EINVAL; |
| 697 |
|
| 698 |
DBG(CXT, ul_debugobj(cxt, "resetting alignment...")); |
| 699 |
|
| 700 |
/* default */ |
| 701 |
cxt->grain = topology_get_grain(cxt); |
| 702 |
cxt->first_lba = topology_get_first_lba(cxt); |
| 703 |
cxt->last_lba = cxt->total_sectors - 1; |
| 704 |
|
| 705 |
/* overwrite default by label stuff */ |
| 706 |
rc = fdisk_apply_label_device_properties(cxt); |
| 707 |
|
| 708 |
DBG(CXT, ul_debugobj(cxt, "alignment reset to: " |
| 709 |
"first LBA=%ju, last LBA=%ju, grain=%lu [rc=%d]", |
| 710 |
(uintmax_t) cxt->first_lba, (uintmax_t) cxt->last_lba, |
| 711 |
cxt->grain, rc)); |
| 712 |
return rc; |
| 713 |
} |
| 714 |
|
| 715 |
|
| 716 |
fdisk_sector_t fdisk_scround(struct fdisk_context *cxt, fdisk_sector_t num) |
| 717 |
{ |
| 718 |
fdisk_sector_t un = fdisk_get_units_per_sector(cxt); |
| 719 |
return (num + un - 1) / un; |
| 720 |
} |
| 721 |
|
| 722 |
fdisk_sector_t fdisk_cround(struct fdisk_context *cxt, fdisk_sector_t num) |
| 723 |
{ |
| 724 |
return fdisk_use_cylinders(cxt) ? |
| 725 |
(num / fdisk_get_units_per_sector(cxt)) + 1 : num; |
| 726 |
} |
| 727 |
|