Skip to content

Commit

Permalink
block: store a gendisk in struct parsed_partitions
Browse files Browse the repository at this point in the history
Partition scanning only happens on the whole device, so pass a
struct gendisk instead of the whole device block_device to the scanners.
This allows to simplify printing the device name in various places as the
disk name is available in disk->name.

Signed-off-by: Christoph Hellwig <[email protected]>
Reviewed-by: Stefan Haberland <[email protected]>
Link: https://lore.kernel.org/r/[email protected]
Signed-off-by: Jens Axboe <[email protected]>
  • Loading branch information
Christoph Hellwig authored and axboe committed Aug 12, 2021
1 parent 50b4aec commit a08aa9b
Show file tree
Hide file tree
Showing 14 changed files with 52 additions and 73 deletions.
4 changes: 2 additions & 2 deletions block/partitions/acorn.c
Original file line number Diff line number Diff line change
Expand Up @@ -275,7 +275,7 @@ int adfspart_check_ADFS(struct parsed_partitions *state)
/*
* Work out start of non-adfs partition.
*/
nr_sects = (state->bdev->bd_inode->i_size >> 9) - start_sect;
nr_sects = get_capacity(state->disk) - start_sect;

if (start_sect) {
switch (id) {
Expand Down Expand Up @@ -540,7 +540,7 @@ int adfspart_check_EESOX(struct parsed_partitions *state)
if (i != 0) {
sector_t size;

size = get_capacity(state->bdev->bd_disk);
size = get_capacity(state->disk);
put_partition(state, slot++, start, size - start);
strlcat(state->pp_buf, "\n", PAGE_SIZE);
}
Expand Down
20 changes: 2 additions & 18 deletions block/partitions/aix.c
Original file line number Diff line number Diff line change
Expand Up @@ -66,38 +66,22 @@ struct pvd {

#define LVM_MAXLVS 256

/**
* last_lba(): return number of last logical block of device
* @bdev: block device
*
* Description: Returns last LBA value on success, 0 on error.
* This is stored (by sd and ide-geometry) in
* the part[0] entry for this disk, and is the number of
* physical sectors available on the disk.
*/
static u64 last_lba(struct block_device *bdev)
{
if (!bdev || !bdev->bd_inode)
return 0;
return (bdev->bd_inode->i_size >> 9) - 1ULL;
}

/**
* read_lba(): Read bytes from disk, starting at given LBA
* @state
* @lba
* @buffer
* @count
*
* Description: Reads @count bytes from @state->bdev into @buffer.
* Description: Reads @count bytes from @state->disk into @buffer.
* Returns number of bytes read on success, 0 on error.
*/
static size_t read_lba(struct parsed_partitions *state, u64 lba, u8 *buffer,
size_t count)
{
size_t totalreadcount = 0;

if (!buffer || lba + count / 512 > last_lba(state->bdev))
if (!buffer || lba + count / 512 > get_capacity(state->disk) - 1ULL)
return 0;

while (count) {
Expand Down
7 changes: 3 additions & 4 deletions block/partitions/amiga.c
Original file line number Diff line number Diff line change
Expand Up @@ -34,15 +34,14 @@ int amiga_partition(struct parsed_partitions *state)
int start_sect, nr_sects, blk, part, res = 0;
int blksize = 1; /* Multiplier for disk block size */
int slot = 1;
char b[BDEVNAME_SIZE];

for (blk = 0; ; blk++, put_dev_sector(sect)) {
if (blk == RDB_ALLOCATION_LIMIT)
goto rdb_done;
data = read_part_sector(state, blk, &sect);
if (!data) {
pr_err("Dev %s: unable to read RDB block %d\n",
bdevname(state->bdev, b), blk);
state->disk->disk_name, blk);
res = -1;
goto rdb_done;
}
Expand All @@ -64,7 +63,7 @@ int amiga_partition(struct parsed_partitions *state)
}

pr_err("Dev %s: RDB in block %d has bad checksum\n",
bdevname(state->bdev, b), blk);
state->disk->disk_name, blk);
}

/* blksize is blocks per 512 byte standard block */
Expand All @@ -84,7 +83,7 @@ int amiga_partition(struct parsed_partitions *state)
data = read_part_sector(state, blk, &sect);
if (!data) {
pr_err("Dev %s: unable to read partition block %d\n",
bdevname(state->bdev, b), blk);
state->disk->disk_name, blk);
res = -1;
goto rdb_done;
}
Expand Down
4 changes: 2 additions & 2 deletions block/partitions/atari.c
Original file line number Diff line number Diff line change
Expand Up @@ -47,15 +47,15 @@ int atari_partition(struct parsed_partitions *state)
* ATARI partition scheme supports 512 lba only. If this is not
* the case, bail early to avoid miscalculating hd_size.
*/
if (bdev_logical_block_size(state->bdev) != 512)
if (queue_logical_block_size(state->disk->queue) != 512)
return 0;

rs = read_part_sector(state, 0, &sect);
if (!rs)
return -1;

/* Verify this is an Atari rootsector: */
hd_size = state->bdev->bd_inode->i_size >> 9;
hd_size = get_capacity(state->disk);
if (!VALID_PARTITION(&rs->part[0], hd_size) &&
!VALID_PARTITION(&rs->part[1], hd_size) &&
!VALID_PARTITION(&rs->part[2], hd_size) &&
Expand Down
2 changes: 1 addition & 1 deletion block/partitions/check.h
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@
* description.
*/
struct parsed_partitions {
struct block_device *bdev;
struct gendisk *disk;
char name[BDEVNAME_SIZE];
struct {
sector_t from;
Expand Down
6 changes: 2 additions & 4 deletions block/partitions/cmdline.c
Original file line number Diff line number Diff line change
Expand Up @@ -380,7 +380,6 @@ static void cmdline_parts_verifier(int slot, struct parsed_partitions *state)
int cmdline_partition(struct parsed_partitions *state)
{
sector_t disk_size;
char bdev[BDEVNAME_SIZE];
struct cmdline_parts *parts;

if (cmdline) {
Expand All @@ -397,12 +396,11 @@ int cmdline_partition(struct parsed_partitions *state)
if (!bdev_parts)
return 0;

bdevname(state->bdev, bdev);
parts = cmdline_parts_find(bdev_parts, bdev);
parts = cmdline_parts_find(bdev_parts, state->disk->disk_name);
if (!parts)
return 0;

disk_size = get_capacity(state->bdev->bd_disk) << 9;
disk_size = get_capacity(state->disk) << 9;

cmdline_parts_set(parts, disk_size, state);
cmdline_parts_verifier(1, state);
Expand Down
6 changes: 3 additions & 3 deletions block/partitions/core.c
Original file line number Diff line number Diff line change
Expand Up @@ -135,7 +135,7 @@ static struct parsed_partitions *check_partition(struct gendisk *hd)
}
state->pp_buf[0] = '\0';

state->bdev = hd->part0;
state->disk = hd;
snprintf(state->name, BDEVNAME_SIZE, "%s", hd->disk_name);
snprintf(state->pp_buf, PAGE_SIZE, " %s:", state->name);
if (isdigit(state->name[strlen(state->name)-1]))
Expand Down Expand Up @@ -717,10 +717,10 @@ EXPORT_SYMBOL_GPL(bdev_disk_changed);

void *read_part_sector(struct parsed_partitions *state, sector_t n, Sector *p)
{
struct address_space *mapping = state->bdev->bd_inode->i_mapping;
struct address_space *mapping = state->disk->part0->bd_inode->i_mapping;
struct page *page;

if (n >= get_capacity(state->bdev->bd_disk)) {
if (n >= get_capacity(state->disk)) {
state->access_beyond_eod = true;
return NULL;
}
Expand Down
36 changes: 17 additions & 19 deletions block/partitions/efi.c
Original file line number Diff line number Diff line change
Expand Up @@ -124,19 +124,17 @@ efi_crc32(const void *buf, unsigned long len)

/**
* last_lba(): return number of last logical block of device
* @bdev: block device
* @disk: block device
*
* Description: Returns last LBA value on success, 0 on error.
* This is stored (by sd and ide-geometry) in
* the part[0] entry for this disk, and is the number of
* physical sectors available on the disk.
*/
static u64 last_lba(struct block_device *bdev)
static u64 last_lba(struct gendisk *disk)
{
if (!bdev || !bdev->bd_inode)
return 0;
return div_u64(bdev->bd_inode->i_size,
bdev_logical_block_size(bdev)) - 1ULL;
return div_u64(disk->part0->bd_inode->i_size,
queue_logical_block_size(disk->queue)) - 1ULL;
}

static inline int pmbr_part_valid(gpt_mbr_record *part)
Expand Down Expand Up @@ -231,17 +229,17 @@ static int is_pmbr_valid(legacy_mbr *mbr, sector_t total_sectors)
* @buffer: destination buffer
* @count: bytes to read
*
* Description: Reads @count bytes from @state->bdev into @buffer.
* Description: Reads @count bytes from @state->disk into @buffer.
* Returns number of bytes read on success, 0 on error.
*/
static size_t read_lba(struct parsed_partitions *state,
u64 lba, u8 *buffer, size_t count)
{
size_t totalreadcount = 0;
struct block_device *bdev = state->bdev;
sector_t n = lba * (bdev_logical_block_size(bdev) / 512);
sector_t n = lba *
(queue_logical_block_size(state->disk->queue) / 512);

if (!buffer || lba > last_lba(bdev))
if (!buffer || lba > last_lba(state->disk))
return 0;

while (count) {
Expand Down Expand Up @@ -302,14 +300,14 @@ static gpt_entry *alloc_read_gpt_entries(struct parsed_partitions *state,
* @lba: the Logical Block Address of the partition table
*
* Description: returns GPT header on success, NULL on error. Allocates
* and fills a GPT header starting at @ from @state->bdev.
* and fills a GPT header starting at @ from @state->disk.
* Note: remember to free gpt when finished with it.
*/
static gpt_header *alloc_read_gpt_header(struct parsed_partitions *state,
u64 lba)
{
gpt_header *gpt;
unsigned ssz = bdev_logical_block_size(state->bdev);
unsigned ssz = queue_logical_block_size(state->disk->queue);

gpt = kmalloc(ssz, GFP_KERNEL);
if (!gpt)
Expand Down Expand Up @@ -356,10 +354,10 @@ static int is_gpt_valid(struct parsed_partitions *state, u64 lba,

/* Check the GUID Partition Table header size is too big */
if (le32_to_cpu((*gpt)->header_size) >
bdev_logical_block_size(state->bdev)) {
queue_logical_block_size(state->disk->queue)) {
pr_debug("GUID Partition Table Header size is too large: %u > %u\n",
le32_to_cpu((*gpt)->header_size),
bdev_logical_block_size(state->bdev));
queue_logical_block_size(state->disk->queue));
goto fail;
}

Expand Down Expand Up @@ -395,7 +393,7 @@ static int is_gpt_valid(struct parsed_partitions *state, u64 lba,
/* Check the first_usable_lba and last_usable_lba are
* within the disk.
*/
lastlba = last_lba(state->bdev);
lastlba = last_lba(state->disk);
if (le64_to_cpu((*gpt)->first_usable_lba) > lastlba) {
pr_debug("GPT: first_usable_lba incorrect: %lld > %lld\n",
(unsigned long long)le64_to_cpu((*gpt)->first_usable_lba),
Expand Down Expand Up @@ -587,13 +585,13 @@ static int find_valid_gpt(struct parsed_partitions *state, gpt_header **gpt,
gpt_header *pgpt = NULL, *agpt = NULL;
gpt_entry *pptes = NULL, *aptes = NULL;
legacy_mbr *legacymbr;
sector_t total_sectors = i_size_read(state->bdev->bd_inode) >> 9;
sector_t total_sectors = get_capacity(state->disk);
u64 lastlba;

if (!ptes)
return 0;

lastlba = last_lba(state->bdev);
lastlba = last_lba(state->disk);
if (!force_gpt) {
/* This will be added to the EFI Spec. per Intel after v1.02. */
legacymbr = kzalloc(sizeof(*legacymbr), GFP_KERNEL);
Expand Down Expand Up @@ -705,7 +703,7 @@ int efi_partition(struct parsed_partitions *state)
gpt_header *gpt = NULL;
gpt_entry *ptes = NULL;
u32 i;
unsigned ssz = bdev_logical_block_size(state->bdev) / 512;
unsigned ssz = queue_logical_block_size(state->disk->queue) / 512;

if (!find_valid_gpt(state, &gpt, &ptes) || !gpt || !ptes) {
kfree(gpt);
Expand All @@ -722,7 +720,7 @@ int efi_partition(struct parsed_partitions *state)
u64 size = le64_to_cpu(ptes[i].ending_lba) -
le64_to_cpu(ptes[i].starting_lba) + 1ULL;

if (!is_pte_valid(&ptes[i], last_lba(state->bdev)))
if (!is_pte_valid(&ptes[i], last_lba(state->disk)))
continue;

put_partition(state, i+1, start * ssz, size * ssz);
Expand Down
4 changes: 2 additions & 2 deletions block/partitions/ibm.c
Original file line number Diff line number Diff line change
Expand Up @@ -290,8 +290,8 @@ static int find_cms1_partitions(struct parsed_partitions *state,
int ibm_partition(struct parsed_partitions *state)
{
int (*fn)(struct gendisk *disk, dasd_information2_t *info);
struct block_device *bdev = state->bdev;
struct gendisk *disk = bdev->bd_disk;
struct gendisk *disk = state->disk;
struct block_device *bdev = disk->part0;
int blocksize, res;
loff_t i_size, offset, size;
dasd_information2_t *info;
Expand Down
18 changes: 9 additions & 9 deletions block/partitions/ldm.c
Original file line number Diff line number Diff line change
Expand Up @@ -304,7 +304,7 @@ static bool ldm_validate_privheads(struct parsed_partitions *state,
}
}

num_sects = state->bdev->bd_inode->i_size >> 9;
num_sects = get_capacity(state->disk);

if ((ph[0]->config_start > num_sects) ||
((ph[0]->config_start + ph[0]->config_size) > num_sects)) {
Expand Down Expand Up @@ -339,11 +339,11 @@ static bool ldm_validate_privheads(struct parsed_partitions *state,
/**
* ldm_validate_tocblocks - Validate the table of contents and its backups
* @state: Partition check state including device holding the LDM Database
* @base: Offset, into @state->bdev, of the database
* @base: Offset, into @state->disk, of the database
* @ldb: Cache of the database structures
*
* Find and compare the four tables of contents of the LDM Database stored on
* @state->bdev and return the parsed information into @toc1.
* @state->disk and return the parsed information into @toc1.
*
* The offsets and sizes of the configs are range-checked against a privhead.
*
Expand Down Expand Up @@ -486,8 +486,8 @@ static bool ldm_validate_vmdb(struct parsed_partitions *state,
* only likely to happen if the underlying device is strange. If that IS
* the case we should return zero to let someone else try.
*
* Return: 'true' @state->bdev is a dynamic disk
* 'false' @state->bdev is not a dynamic disk, or an error occurred
* Return: 'true' @state->disk is a dynamic disk
* 'false' @state->disk is not a dynamic disk, or an error occurred
*/
static bool ldm_validate_partition_table(struct parsed_partitions *state)
{
Expand Down Expand Up @@ -1340,7 +1340,7 @@ static bool ldm_frag_commit (struct list_head *frags, struct ldmdb *ldb)
/**
* ldm_get_vblks - Read the on-disk database of VBLKs into memory
* @state: Partition check state including device holding the LDM Database
* @base: Offset, into @state->bdev, of the database
* @base: Offset, into @state->disk, of the database
* @ldb: Cache of the database structures
*
* To use the information from the VBLKs, they need to be read from the disk,
Expand Down Expand Up @@ -1432,10 +1432,10 @@ static void ldm_free_vblks (struct list_head *lh)
* example, if the device is hda, we would have: hda1: LDM database, hda2, hda3,
* and so on: the actual data containing partitions.
*
* Return: 1 Success, @state->bdev is a dynamic disk and we handled it
* 0 Success, @state->bdev is not a dynamic disk
* Return: 1 Success, @state->disk is a dynamic disk and we handled it
* 0 Success, @state->disk is not a dynamic disk
* -1 An error occurred before enough information had been read
* Or @state->bdev is a dynamic disk, but it may be corrupted
* Or @state->disk is a dynamic disk, but it may be corrupted
*/
int ldm_partition(struct parsed_partitions *state)
{
Expand Down
2 changes: 1 addition & 1 deletion block/partitions/mac.c
Original file line number Diff line number Diff line change
Expand Up @@ -133,7 +133,7 @@ int mac_partition(struct parsed_partitions *state)
}
#ifdef CONFIG_PPC_PMAC
if (found_root_goodness)
note_bootable_part(state->bdev->bd_dev, found_root,
note_bootable_part(state->disk->part0->bd_dev, found_root,
found_root_goodness);
#endif

Expand Down
6 changes: 4 additions & 2 deletions block/partitions/msdos.c
Original file line number Diff line number Diff line change
Expand Up @@ -135,11 +135,12 @@ static void parse_extended(struct parsed_partitions *state,
Sector sect;
unsigned char *data;
sector_t this_sector, this_size;
sector_t sector_size = bdev_logical_block_size(state->bdev) / 512;
sector_t sector_size;
int loopct = 0; /* number of links followed
without finding a data partition */
int i;

sector_size = queue_logical_block_size(state->disk->queue) / 512;
this_sector = first_sector;
this_size = first_size;

Expand Down Expand Up @@ -579,14 +580,15 @@ static struct {

int msdos_partition(struct parsed_partitions *state)
{
sector_t sector_size = bdev_logical_block_size(state->bdev) / 512;
sector_t sector_size;
Sector sect;
unsigned char *data;
struct msdos_partition *p;
struct fat_boot_sector *fb;
int slot;
u32 disksig;

sector_size = queue_logical_block_size(state->disk->queue) / 512;
data = read_part_sector(state, 0, &sect);
if (!data)
return -1;
Expand Down
Loading

0 comments on commit a08aa9b

Please sign in to comment.