/** * Return a constraint that only the given region will satisfy. */ PedConstraint* ped_constraint_exact (const PedGeometry* geom) { PedAlignment start_align; PedAlignment end_align; PedGeometry start_sector; PedGeometry end_sector; int ok; /* With grain size of 0, it always succeeds. */ ok = ped_alignment_init (&start_align, geom->start, 0); assert (ok); ok = ped_alignment_init (&end_align, geom->end, 0); assert (ok); ok = ped_geometry_init (&start_sector, geom->dev, geom->start, 1); if (!ok) return NULL; ok = ped_geometry_init (&end_sector, geom->dev, geom->end, 1); if (!ok) return NULL; return ped_constraint_new (&start_align, &end_align, &start_sector, &end_sector, 1, geom->dev->length); }
static PedConstraint *reiserfs_get_resize_constraint(const PedFileSystem * fs) { PedDevice *dev; PedSector min_size; PedGeometry full_disk; reiserfs_fs_t *fs_info; PedAlignment start_align; PedGeometry start_sector; PED_ASSERT(fs != NULL); fs_info = fs->type_specific; dev = fs->geom->dev; if (!ped_alignment_init(&start_align, fs->geom->start, 0)) return NULL; if (!ped_geometry_init(&full_disk, dev, 0, dev->length - 1)) return NULL; if (!ped_geometry_init(&start_sector, dev, fs->geom->start, 1)) return NULL; /* Minsize for reiserfs is area occupied by data blocks and metadata blocks minus free space blocks and minus bitmap blocks which describes free space blocks. */ min_size = reiserfs_fs_min_size(fs_info) * (reiserfs_fs_block_size(fs_info) / PED_SECTOR_SIZE_DEFAULT); return ped_constraint_new(&start_align, ped_alignment_any, &start_sector, &full_disk, min_size, dev->length); }
static PedConstraint* _primary_constraint (PedDisk* disk) { PedAlignment start_align; PedAlignment end_align; PedGeometry max_geom; PedSector sector_size; LinuxSpecific* arch_specific; DasdDiskSpecific* disk_specific; PedSector start; PDEBUG; arch_specific = LINUX_SPECIFIC (disk->dev); disk_specific = disk->disk_specific; sector_size = arch_specific->real_sector_size / disk->dev->sector_size; if (!ped_alignment_init (&start_align, 0, disk->dev->hw_geom.sectors * sector_size)) return NULL; if (!ped_alignment_init (&end_align, -1, disk->dev->hw_geom.sectors * sector_size)) return NULL; start = (FIRST_USABLE_TRK * (long long) disk->dev->hw_geom.sectors * (long long) arch_specific->real_sector_size / (long long) disk->dev->sector_size); if (!ped_geometry_init (&max_geom, disk->dev, start, disk->dev->length)) return NULL; return ped_constraint_new(&start_align, &end_align, &max_geom, &max_geom, 1, disk->dev->length); }
/** * Return a constraint that only the given region will satisfy. */ PedConstraint* ped_constraint_exact (const PedGeometry* geom) { PedAlignment start_align; PedAlignment end_align; PedGeometry start_sector; PedGeometry end_sector; ped_alignment_init (&start_align, geom->start, 0); ped_alignment_init (&end_align, geom->end, 0); ped_geometry_init (&start_sector, geom->dev, geom->start, 1); ped_geometry_init (&end_sector, geom->dev, geom->end, 1); return ped_constraint_new (&start_align, &end_align, &start_sector, &end_sector, 1, geom->dev->length); }
static PedConstraint* _get_primary_constraint (PedDisk* disk) { PedGeometry max_geom; if (!ped_geometry_init (&max_geom, disk->dev, 1, disk->dev->length - 1)) return NULL; return ped_constraint_new_from_max (&max_geom); }
static PedConstraint* _get_constraint (const PedDevice* dev) { PedGeometry max; ped_geometry_init (&max, dev, 1, dev->length - 1); return ped_constraint_new_from_max (&max); }
/* The constraint for the volume header partition is different, because it must * contain the first sector of the disk. */ static PedConstraint* _get_extended_constraint (PedDisk* disk) { PedGeometry min_geom; if (!ped_geometry_init (&min_geom, disk->dev, 0, 1)) return NULL; return ped_constraint_new_from_min (&min_geom); }
/** * Return a constraint that requires a region to entirely contain \p min. * * \return \c NULL on failure. */ PedConstraint* ped_constraint_new_from_min (const PedGeometry* min) { PedGeometry full_dev; PED_ASSERT (min != NULL); ped_geometry_init (&full_dev, min->dev, 0, min->dev->length); return ped_constraint_new_from_min_max (min, &full_dev); }
/** * Find the largest region that satisfies a constraint. * * There might be more than one solution. This function makes no * guarantees about which solution it will choose in this case. */ PedGeometry* ped_constraint_solve_max (const PedConstraint* constraint) { PedDevice* dev; PedGeometry full_dev; if (!constraint) return NULL; dev = constraint->start_range->dev; ped_geometry_init (&full_dev, dev, 0, dev->length - 1); return ped_constraint_solve_nearest (constraint, &full_dev); }
/** * Return a constraint that requires a region to be entirely contained inside * \p max, and to entirely contain \p min. * * \return \c NULL on failure. */ PedConstraint* ped_constraint_new_from_min_max ( const PedGeometry* min, const PedGeometry* max) { PedGeometry start_range; PedGeometry end_range; PED_ASSERT (min != NULL); PED_ASSERT (max != NULL); PED_ASSERT (ped_geometry_test_inside (max, min)); ped_geometry_init (&start_range, min->dev, max->start, min->start - max->start + 1); ped_geometry_init (&end_range, min->dev, min->end, max->end - min->end + 1); return ped_constraint_new ( ped_alignment_any, ped_alignment_any, &start_range, &end_range, min->length, max->length); }
static PedConstraint *reiserfs_get_create_constraint(const PedDevice *dev) { PedGeometry full_dev; PedSector min_blks = (SUPER_OFFSET_IN_BYTES / DEFAULT_BLOCK_SIZE) + 2 + DEFAULT_JOURNAL_SIZE + 1 + 100 + 1; if (!ped_geometry_init(&full_dev, dev, 0, dev->length - 1)) return NULL; return ped_constraint_new(ped_alignment_any, ped_alignment_any, &full_dev, &full_dev, min_blks * (DEFAULT_BLOCK_SIZE / 512), dev->length); }
PedConstraint * hfsplus_get_resize_constraint (const PedFileSystem *fs) { PedDevice* dev = fs->geom->dev; PedAlignment start_align; PedGeometry start_sector; PedGeometry full_dev; PedSector min_size; if (!ped_alignment_init (&start_align, fs->geom->start, 0)) return NULL; if (!ped_geometry_init (&start_sector, dev, fs->geom->start, 1)) return NULL; if (!ped_geometry_init (&full_dev, dev, 0, dev->length - 1)) return NULL; min_size = hfsplus_get_min_size (fs); if (!min_size) return NULL; return ped_constraint_new (&start_align, ped_alignment_any, &start_sector, &full_dev, min_size, fs->geom->length); }
PedConstraint * hfs_get_resize_constraint (const PedFileSystem *fs) { PedDevice* dev = fs->geom->dev; PedAlignment start_align; PedGeometry start_sector; PedGeometry full_dev; PedSector min_size; if (!ped_alignment_init (&start_align, fs->geom->start, 0)) return NULL; if (!ped_geometry_init (&start_sector, dev, fs->geom->start, 1)) return NULL; if (!ped_geometry_init (&full_dev, dev, 0, dev->length - 1)) return NULL; /* 2 = last two sectors (alternate MDB and unused sector) */ min_size = hfs_get_empty_end(fs) + 2; if (min_size == 2) return NULL; return ped_constraint_new (&start_align, ped_alignment_any, &start_sector, &full_dev, min_size, fs->geom->length); }
static PedConstraint *reiserfs_get_copy_constraint(const PedFileSystem *fs, const PedDevice *dev) { PedGeometry full_dev; PED_ASSERT(fs != NULL); PED_ASSERT(dev != NULL); if (!ped_geometry_init(&full_dev, dev, 0, dev->length - 1)) return NULL; return ped_constraint_new(ped_alignment_any, ped_alignment_any, &full_dev, &full_dev, reiserfs_fs_bitmap_used(fs->type_specific), dev->length); }
/** * Return a constraint that any region on the given device will satisfy. */ PedConstraint* ped_constraint_any (const PedDevice* dev) { PedGeometry full_dev; if (!ped_geometry_init (&full_dev, dev, 0, dev->length)) return NULL; return ped_constraint_new ( ped_alignment_any, ped_alignment_any, &full_dev, &full_dev, 1, dev->length); }
static PedConstraint* _amiga_get_constraint (const PedDisk *disk) { PedDevice *dev = disk->dev; PedAlignment start_align, end_align; PedGeometry max_geom; PedSector cyl_size = dev->hw_geom.sectors * dev->hw_geom.heads; if (!ped_alignment_init(&start_align, 0, cyl_size)) return NULL; if (!ped_alignment_init(&end_align, -1, cyl_size)) return NULL; if (!ped_geometry_init(&max_geom, dev, MAX_RDB_BLOCK + 1, dev->length - MAX_RDB_BLOCK - 1)) return NULL; return ped_constraint_new (&start_align, &end_align, &max_geom, &max_geom, 1, dev->length); }
static PedConstraint* _primary_constraint (PedDisk* disk) { PedDevice* dev = disk->dev; PedAlignment start_align; PedAlignment end_align; PedGeometry max_geom; PedSector cylinder_size; cylinder_size = dev->hw_geom.sectors * dev->hw_geom.heads; if (!ped_alignment_init (&start_align, 0, cylinder_size)) return NULL; if (!ped_alignment_init (&end_align, -1, cylinder_size)) return NULL; if (!ped_geometry_init (&max_geom, dev, cylinder_size, dev->length - cylinder_size)) return NULL; return ped_constraint_new (&start_align, &end_align, &max_geom, &max_geom, 1, dev->length); }
/* * Given a constraint and a start ("half of the solution"), find the * range of all possible ends, such that all (start, end) are solutions * to constraint (subject to additional alignment requirements). */ static PedGeometry* _constraint_get_end_range (const PedConstraint* constraint, PedSector start) { PedDevice* dev = constraint->end_range->dev; PedSector first_min_max_end; PedSector last_min_max_end; PedGeometry end_min_max_range; if (start + constraint->min_size - 1 > dev->length - 1) return NULL; first_min_max_end = start + constraint->min_size - 1; last_min_max_end = start + constraint->max_size - 1; if (last_min_max_end > dev->length - 1) last_min_max_end = dev->length - 1; ped_geometry_init (&end_min_max_range, dev, first_min_max_end, last_min_max_end - first_min_max_end + 1); return ped_geometry_intersect (&end_min_max_range, constraint->end_range); }
/* * Return the region within which the start must lie * in order to satisfy a constriant. It takes into account * constraint->start_range, constraint->min_size and constraint->max_size. * All sectors in this range that also satisfy alignment requirements have * an end, such that the (start, end) satisfy the constraint. */ static PedGeometry* _constraint_get_canonical_start_range (const PedConstraint* constraint) { PedSector first_end_soln; PedSector last_end_soln; PedSector min_start; PedSector max_start; PedGeometry start_min_max_range; if (constraint->min_size > constraint->max_size) return NULL; first_end_soln = ped_alignment_align_down ( constraint->end_align, constraint->end_range, constraint->end_range->start); last_end_soln = ped_alignment_align_up ( constraint->end_align, constraint->end_range, constraint->end_range->end); if (first_end_soln == -1 || last_end_soln == -1 || first_end_soln > last_end_soln || last_end_soln < constraint->min_size) return NULL; min_start = first_end_soln - constraint->max_size + 1; if (min_start < 0) min_start = 0; max_start = last_end_soln - constraint->min_size + 1; if (max_start < 0) return NULL; ped_geometry_init ( &start_min_max_range, constraint->start_range->dev, min_start, max_start - min_start + 1); return ped_geometry_intersect (&start_min_max_range, constraint->start_range); }
int main (int argc, char **argv) { set_program_name (argv[0]); PedSector start = 0, len = 0; PedGeometry geom, new_geom; PedDevice *dev; PedFileSystem *fs; PedTimer *g_timer = NULL; if (argc != 2 && argc != 4) { fprintf(stderr, "usage: %s <device>\n" " %s <device> <start> <length>\n", argv[0], argv[0]); return 1; } dev = ped_device_get(argv[1]); if (!dev) { fprintf(stderr, "cannot create device %s\n", argv[1]); return 1; } if (!ped_device_open(dev)) { fprintf(stderr, "cannot open device %s\n", argv[1]); return 1; } if (!ped_geometry_init(&geom, dev, 0, dev->length)) { fprintf(stderr, "cannot initialize geometry\n"); return 1; } if (argc > 2) { start = strtoll(argv[2], NULL, 0); len = strtoll(argv[3], NULL, 0); } else { start = 0; len = dev->length; } if (!ped_geometry_init(&new_geom, dev, start, len)) { fprintf(stderr, "cannot initialize new geometry\n"); return 1; } fs = ped_file_system_open(&geom); if (!fs) { fprintf(stderr, "cannot read fs\n"); return 1; } if (!ped_file_system_resize(fs, &new_geom, g_timer)) { fprintf(stderr, "cannot resize filesystem\n"); return 1; } ped_file_system_close(fs); return 0; }