disk revalidation cleanups and fixlets v2
Subsystems (4)
Hi Jens and Jan,
this series takes the disk size change detection and revalidations from Jan a step further and fully integrate the code path for partitioned vs non-partitioned devices. It also fixes up a few bits where we have unintentionally differing behavior.
Changes since v1:
- rebased on to of for-5.5/zoned
- fixed a commit message
- added two new trivial patches
PATCH 1/7 block: refactor rescan_partitions
Split out a helper that adds one single partition, and another one calling that dealing with the parsed_partitions state. This makes it much more obvious how we clean up all state and start again when using the rescan label.
--- block/partition-generic.c | 172 +++++++++++++++++++++----------------- 1 file changed, 94 insertions(+), 78 deletions(-)
block/partition-generic.c +94 -78
@@ -459,128 +459,144 @@ static int drop_partitions(struct gendisk *disk, struct block_device *bdev)return 0 ; } -int rescan_partitions (struct gendisk *disk,struct block_device *bdev) +static bool blk_add_partition (struct gendisk *disk,struct block_device *bdev, +struct parsed_partitions *state,int p) { -struct parsed_partitions *state =NULL ; +sector_t size = state->parts[p].size; +sector_t from = state->parts[p].from;struct hd_struct *part; -int p, highest, res; -rescan: -if (state && !IS_ERR (state)) { -free_partitions (state); - state =NULL ; + +if (!size) +return true; + +if (from >=get_capacity (disk)) { +printk (KERN_WARNING +"%s: p%d start %llu is beyond EOD, " , + disk->disk_name, p, (unsigned long long ) from); +if (disk_unlock_native_capacity (disk)) +return false; +return true; } - res =drop_partitions (disk, bdev); -if (res) -return res; +if (from + size >get_capacity (disk)) { +printk (KERN_WARNING +"%s: p%d size %llu extends beyond EOD, " , + disk->disk_name, p, (unsigned long long ) size); -if (disk->fops->revalidate_disk) - disk->fops->revalidate_disk (disk); -check_disk_size_change (disk, bdev, true); - bdev->bd_invalidated =0 ; -if (!get_capacity (disk) || !(state =check_partition (disk, bdev))) +if (disk_unlock_native_capacity (disk)) +return false; + +/* +* We can not ignore partitions of broken tables created by for +* example camera firmware, but we limit them to the end of the +* disk to avoid creating invalid block devices. +*/ + size =get_capacity (disk) - from; + } + + part =add_partition (disk, p, from, size, state->parts[p].flags, + &state->parts[p].info); +if (IS_ERR (part)) { +printk (KERN_ERR " %s: p%d could not be added: %ld\n" , + disk->disk_name, p, -PTR_ERR (part)); +return true; + } + +#ifdef CONFIG_BLK_DEV_MD +if (state->parts[p].flags &ADDPART_FLAG_RAID ) +md_autodetect_dev (part_to_dev (part)->devt); +#endif +return true; +} + +static int blk_add_partitions (struct gendisk *disk,struct block_device *bdev) +{ +struct parsed_partitions *state; +int ret = -EAGAIN , p, highest; + + state =check_partition (disk, bdev); +if (!state)return 0 ;if (IS_ERR (state)) {/* -* I/O error reading the partition table. If any -* partition code tried to read beyond EOD, retry -* after unlocking native capacity. +* I/O error reading the partition table. If we tried to read +* beyond EOD, retry after unlocking the native capacity. */ if (PTR_ERR (state) == -ENOSPC ) {printk (KERN_WARNING "%s: partition table beyond EOD, " , disk->disk_name);if (disk_unlock_native_capacity (disk)) - goto rescan; +return -EAGAIN ; }return -EIO ; } -/* Partitions are not supported on zoned block devices */ +/* +* Partitions are not supported on zoned block devices. +*/ if (bdev_is_zoned (bdev)) {pr_warn ("%s: ignoring partition table on zoned block device\n" , disk->disk_name); - goto out; + ret =0 ; + goto out_free_state; }/* -* If any partition code tried to read beyond EOD, try -* unlocking native capacity even if partition table is -* successfully read as we could be missing some partitions. +* If we read beyond EOD, try unlocking native capacity even if the +* partition table was successfully read as we could be missing some +* partitions. */ if (state->access_beyond_eod) {printk (KERN_WARNING "%s: partition table partially beyond EOD, " , disk->disk_name);if (disk_unlock_native_capacity (disk)) - goto rescan; + goto out_free_state; }/* tell userspace that the media / partition table may have changed */ kobject_uevent (&disk_to_dev (disk)->kobj,KOBJ_CHANGE ); -/* Detect the highest partition number and preallocate -* disk->part_tbl. This is an optimization and not strictly -* necessary. +/* +* Detect the highest partition number and preallocate disk->part_tbl. +* This is an optimization and not strictly necessary. */ for (p =1 , highest =0 ; p < state->limit; p++)if (state->parts[p].size) highest = p; -disk_expand_part_tbl (disk, highest); -/* add partitions */ -for (p =1 ; p < state->limit; p++) { -sector_t size, from; +for (p =1 ; p < state->limit; p++) +if (!blk_add_partition (disk, bdev, state, p)) + goto out_free_state; - size = state->parts[p].size; -if (!size) -continue ; + ret =0 ; +out_free_state: +free_partitions (state); +return ret; +} - from = state->parts[p].from; -if (from >=get_capacity (disk)) { -printk (KERN_WARNING -"%s: p%d start %llu is beyond EOD, " , - disk->disk_name, p, (unsigned long long ) from); -if (disk_unlock_native_capacity (disk)) - goto rescan; -continue ; - } +int rescan_partitions (struct gendisk *disk,struct block_device *bdev) +{ +int ret; -if (from + size >get_capacity (disk)) { -printk (KERN_WARNING -"%s: p%d size %llu extends beyond EOD, " , - disk->disk_name, p, (unsigned long long ) size); - -if (disk_unlock_native_capacity (disk)) { -/* free state and restart */ - goto rescan; - }else { -/* -* we can not ignore partitions of broken tables -* created by for example camera firmware, but -* we limit them to the end of the disk to avoid -* creating invalid block devices -*/ - size =get_capacity (disk) - from; - } - } +rescan: + ret =drop_partitions (disk, bdev); +if (ret) +return ret; - part =add_partition (disk, p, from, size, - state->parts[p].flags, - &state->parts[p].info); -if (IS_ERR (part)) { -printk (KERN_ERR " %s: p%d could not be added: %ld\n" , - disk->disk_name, p, -PTR_ERR (part)); -continue ; - } -#ifdef CONFIG_BLK_DEV_MD -if (state->parts[p].flags &ADDPART_FLAG_RAID ) -md_autodetect_dev (part_to_dev (part)->devt); -#endif - } -out: -free_partitions (state); -return 0 ; +if (disk->fops->revalidate_disk) + disk->fops->revalidate_disk (disk); +check_disk_size_change (disk, bdev, true); + bdev->bd_invalidated =0 ; + +if (!get_capacity (disk)) +return 0 ; + + ret =blk_add_partitions (disk, bdev); +if (ret == -EAGAIN ) + goto rescan; +return ret; }int invalidate_partitions (struct gendisk *disk,struct block_device *bdev)
Signature · 1 line
2.20.1
PATCH 2/7 block: merge invalidate_partitions into rescan_partitions
In reply to Christoph HellwigA lot of the logic in invalidate_partitions and rescan_partitions is shared. Merge the two functions to simplify things. There is a small behavior change in that we now send the kevent change notice also if we were not invalidating but no partitions were found, which seems like the right thing to do.
--- block/ioctl.c | 2 +- block/partition-generic.c | 38 ++++++++++++++------------------------ fs/block_dev.c | 5 +---- include/linux/genhd.h | 4 ++-- 4 files changed, 18 insertions(+), 31 deletions(-)
block/ioctl.c +1 -1
@@ -171,7 +171,7 @@ int __blkdev_reread_part(struct block_device *bdev)lockdep_assert_held (&bdev->bd_mutex); -return rescan_partitions (disk, bdev); +return rescan_partitions (disk, bdev, false); }EXPORT_SYMBOL (__blkdev_reread_part);
block/partition-generic.c +14 -24
@@ -576,7 +576,8 @@ static int blk_add_partitions(struct gendisk *disk, struct block_device *bdev)return ret; } -int rescan_partitions (struct gendisk *disk,struct block_device *bdev) +int rescan_partitions (struct gendisk *disk,struct block_device *bdev, +bool invalidate) {int ret;
@@ -585,13 +586,22 @@ int rescan_partitions(struct gendisk *disk, struct block_device *bdev)if (ret)return ret; -if (disk->fops->revalidate_disk) +if (invalidate) +set_capacity (disk,0 ); +else if (disk->fops->revalidate_disk) disk->fops->revalidate_disk (disk); -check_disk_size_change (disk, bdev, true); + +check_disk_size_change (disk, bdev, !invalidate); bdev->bd_invalidated =0 ; -if (!get_capacity (disk)) +if (!get_capacity (disk)) { +/* +* Tell userspace that the media / partition table may have +* changed. +*/ +kobject_uevent (&disk_to_dev (disk)->kobj,KOBJ_CHANGE );return 0 ; + } ret =blk_add_partitions (disk, bdev);if (ret == -EAGAIN )
@@ -599,26 +609,6 @@ int rescan_partitions(struct gendisk *disk, struct block_device *bdev)return ret; } -int invalidate_partitions (struct gendisk *disk,struct block_device *bdev) -{ -int res; - -if (!bdev->bd_invalidated) -return 0 ; - - res =drop_partitions (disk, bdev); -if (res) -return res; - -set_capacity (disk,0 ); -check_disk_size_change (disk, bdev, false); - bdev->bd_invalidated =0 ; -/* tell userspace that the media / partition table may have changed */ -kobject_uevent (&disk_to_dev (disk)->kobj,KOBJ_CHANGE ); - -return 0 ; -} -unsigned char *read_dev_sector (struct block_device *bdev,sector_t n,Sector *p) {struct address_space *mapping = bdev->bd_inode->i_mapping;
fs/block_dev.c +1 -4
@@ -1511,10 +1511,7 @@ static void __blkdev_put(struct block_device *bdev, fmode_t mode, int for_part);static void bdev_disk_changed (struct block_device *bdev,bool invalidate) {if (disk_part_scan_enabled (bdev->bd_disk)) { -if (invalidate) -invalidate_partitions (bdev->bd_disk, bdev); -else -rescan_partitions (bdev->bd_disk, bdev); +rescan_partitions (bdev->bd_disk, bdev, invalidate); }else {check_disk_size_change (bdev->bd_disk, bdev, !invalidate); bdev->bd_invalidated =0 ;
include/linux/genhd.h +2 -2
@@ -622,8 +622,8 @@ extern dev_t blk_lookup_devt(const char *name, int partno);extern char *disk_name (struct gendisk *hd,int partno,char *buf);extern int disk_expand_part_tbl (struct gendisk *disk,int target); -extern int rescan_partitions (struct gendisk *disk,struct block_device *bdev); -extern int invalidate_partitions (struct gendisk *disk,struct block_device *bdev); +int rescan_partitions (struct gendisk *disk,struct block_device *bdev, +bool invalidate); extern struct hd_struct * __must_check add_partition(struct gendisk *disk, int partno, sector_t start, sector_t len, int flags,
Signature · 1 line
2.20.1
Re: [PATCH 2/7] block: merge invalidate_partitions into rescan_partitions
Quoted 6 lines · Christoph Hellwig
A lot of the logic in
invalidate_partitionsandrescan_partitionsis shared. Merge the two functions to simplify things. There is a small behavior change in that we now send the kevent change notice also if we were not invalidating but no partitions were found, which seems like the right thing to do.
Quoted 9 lines
Signed-off-by Christoph Hellwig <[email protected]>Reviewed-by Jan Kara <[email protected]>--- block/ioctl.c | 2 +- block/partition-generic.c | 38 ++++++++++++++------------------------ fs/block_dev.c | 5 +---- include/linux/genhd.h | 4 ++-- 4 files changed, 18 insertions(+), 31 deletions(-)
Cheers,
Hannes
Signature · 4 lines
Dr. Hannes Reinecke Teamlead Storage & Networking [email protected] +49 911 74053 688 SUSE Software Solutions Germany GmbH, Maxfeldstr. 5, 90409 Nürnberg HRB 247165 (AG München), GF: Felix Imendörffer
Re: [PATCH 2/7] block: merge invalidate_partitions into rescan_partitions
In reply to Christoph HellwigHi Christoph,
Quoted 6 lines · Christoph Hellwig
A lot of the logic in
invalidate_partitionsandrescan_partitionsis shared. Merge the two functions to simplify things. There is a small behavior change in that we now send the kevent change notice also if we were not invalidating but no partitions were found, which seems like the right thing to do.
Quoted 9 lines
Signed-off-by Christoph Hellwig <[email protected]>Reviewed-by Jan Kara <[email protected]>--- block/ioctl.c | 2 +- block/partition-generic.c | 38 ++++++++++++++------------------------ fs/block_dev.c | 5 +---- include/linux/genhd.h | 4 ++-- 4 files changed, 18 insertions(+), 31 deletions(-)
Mainline is broken for me because systemd-udevd spins forever using max CPU starting at boot time. I bisected it to this commit.
I'm not an expert in this code, but the following patch fixes it:
block/partition-generic.c +2 -1
@@ -599,7 +599,8 @@ int rescan_partitions(struct gendisk *disk, struct block_device *bdev,* Tell userspace that the media / partition table may have * changed. */ -kobject_uevent (&disk_to_dev (disk)->kobj,KOBJ_CHANGE ); +if (invalidate) +kobject_uevent (&disk_to_dev (disk)->kobj,KOBJ_CHANGE );return 0 ; }
Do you have any better suggestion, or should I just send this as a formal patch?
- Eric
Re: [PATCH 2/7] block: merge invalidate_partitions into rescan_partitions
Quoted 39 lines · Eric Biggers
Hi Christoph,
Quoted 6 lines · Christoph Hellwig
A lot of the logic in
invalidate_partitionsandrescan_partitionsis shared. Merge the two functions to simplify things. There is a small behavior change in that we now send the kevent change notice also if we were not invalidating but no partitions were found, which seems like the right thing to do.Quoted 9 lines
Signed-off-by Christoph Hellwig <[email protected]>Reviewed-by Jan Kara <[email protected]>--- block/ioctl.c | 2 +- block/partition-generic.c | 38 ++++++++++++++------------------------ fs/block_dev.c | 5 +---- include/linux/genhd.h | 4 ++-- 4 files changed, 18 insertions(+), 31 deletions(-)Mainline is broken for me because systemd-udevd spins forever using max CPU starting at boot time. I bisected it to this commit.
I'm not an expert in this code, but the following patch fixes it:
block/partition-generic.c +2
-1@@ -599,7 +599,8 @@ int rescan_partitions(struct gendisk *disk, struct block_device *bdev,* Tell userspace that the media / partition table may have * changed. */ -kobject_uevent (&disk_to_dev (disk)->kobj,KOBJ_CHANGE ); +if (invalidate) +kobject_uevent (&disk_to_dev (disk)->kobj,KOBJ_CHANGE );return 0 ; }
Quoted 2 lines
Do you have any better suggestion, or should I just send this as a formal patch?
Actually this code was moved around more between the bad commit and mainline, so the fix for mainline would be:
fs/block_dev.c +1 -1
@@ -1531,7 +1531,7 @@ int bdev_disk_changed(struct block_device *bdev, bool invalidate) ret =blk_add_partitions (disk, bdev);if (ret == -EAGAIN ) goto rescan; - }else { + }else if (invalidate) {/* * Tell userspace that the media / partition table may have * changed.
Re: [PATCH 2/7] block: merge invalidate_partitions into rescan_partitions
Quoted 2 lines · Eric Biggers
Actually this code was moved around more between the bad commit and mainline, so the fix for mainline would be:
Can you send this JJens with a proper changelog and signoff?
Whіle not sending the event for the non-partition case doesn't make a whole lot sense it clearly breaks userspace after we've done it for a long time.
PATCH 3/7 block: move rescan_partitions to fs/block_dev.c
In reply to Christoph HellwigLarge parts of rescan_partitions aren't about partitions, and moving it to block_dev.c will allow for some further cleanups by merging it into its only caller.
--- block/partition-generic.c | 37 ++----------------------------------- fs/block_dev.c | 38 ++++++++++++++++++++++++++++++++++++-- include/linux/fs.h | 2 -- include/linux/genhd.h | 4 ++-- 4 files changed, 40 insertions(+), 41 deletions(-)
block/partition-generic.c +2 -35
@@ -439,7 +439,7 @@ static bool disk_unlock_native_capacity(struct gendisk *disk) } } -static int drop_partitions (struct gendisk *disk,struct block_device *bdev) +int blk_drop_partitions (struct gendisk *disk,struct block_device *bdev) {struct disk_part_iter piter;struct hd_struct *part;
@@ -509,7 +509,7 @@ static bool blk_add_partition(struct gendisk *disk, struct block_device *bdev,return true; } -static int blk_add_partitions (struct gendisk *disk,struct block_device *bdev) +int blk_add_partitions (struct gendisk *disk,struct block_device *bdev) {struct parsed_partitions *state;int ret = -EAGAIN , p, highest;
@@ -576,39 +576,6 @@ static int blk_add_partitions(struct gendisk *disk, struct block_device *bdev)return ret; } -int rescan_partitions (struct gendisk *disk,struct block_device *bdev, -bool invalidate) -{ -int ret; - -rescan: - ret =drop_partitions (disk, bdev); -if (ret) -return ret; - -if (invalidate) -set_capacity (disk,0 ); -else if (disk->fops->revalidate_disk) - disk->fops->revalidate_disk (disk); - -check_disk_size_change (disk, bdev, !invalidate); - bdev->bd_invalidated =0 ; - -if (!get_capacity (disk)) { -/* -* Tell userspace that the media / partition table may have -* changed. -*/ -kobject_uevent (&disk_to_dev (disk)->kobj,KOBJ_CHANGE ); -return 0 ; - } - - ret =blk_add_partitions (disk, bdev); -if (ret == -EAGAIN ) - goto rescan; -return ret; -} -unsigned char *read_dev_sector (struct block_device *bdev,sector_t n,Sector *p) {struct address_space *mapping = bdev->bd_inode->i_mapping;
fs/block_dev.c +36 -2
@@ -1416,8 +1416,8 @@ static void flush_disk(struct block_device *bdev, bool kill_dirty)* and adjusts it if it differs. When shrinking the bdev size, its all caches * are freed. */ -void check_disk_size_change (struct gendisk *disk,struct block_device *bdev, -bool verbose) +static void check_disk_size_change (struct gendisk *disk, +struct block_device *bdev,bool verbose) {loff_t disk_size, bdev_size;
@@ -1508,6 +1508,40 @@ EXPORT_SYMBOL(bd_set_size);static void __blkdev_put (struct block_device *bdev,fmode_t mode,int for_part); +static int rescan_partitions (struct gendisk *disk,struct block_device *bdev, +bool invalidate) +{ +int ret; + +rescan: + ret =blk_drop_partitions (disk, bdev); +if (ret) +return ret; + +if (invalidate) +set_capacity (disk,0 ); +else if (disk->fops->revalidate_disk) + disk->fops->revalidate_disk (disk); + +check_disk_size_change (disk, bdev, !invalidate); + bdev->bd_invalidated =0 ; + +if (!get_capacity (disk)) { +/* +* Tell userspace that the media / partition table may have +* changed. +*/ +kobject_uevent (&disk_to_dev (disk)->kobj,KOBJ_CHANGE ); +return 0 ; + } + + ret =blk_add_partitions (disk, bdev); +if (ret == -EAGAIN ) + goto rescan; +return ret; +} + +static void bdev_disk_changed (struct block_device *bdev,bool invalidate) {if (disk_part_scan_enabled (bdev->bd_disk)) {
include/linux/fs.h +0 -2
@@ -2703,8 +2703,6 @@ extern void make_bad_inode(struct inode *);extern bool is_bad_inode (struct inode *);#ifdef CONFIG_BLOCK -extern void check_disk_size_change (struct gendisk *disk, -struct block_device *bdev,bool verbose);extern int revalidate_disk (struct gendisk *);extern int check_disk_change (struct block_device *);extern int __invalidate_device (struct block_device *,bool );
include/linux/genhd.h +2 -2
@@ -621,9 +621,9 @@ extern void blk_invalidate_devt(dev_t devt);extern dev_t blk_lookup_devt (const char *name,int partno);extern char *disk_name (struct gendisk *hd,int partno,char *buf); +int blk_add_partitions (struct gendisk *disk,struct block_device *bdev); +int blk_drop_partitions (struct gendisk *disk,struct block_device *bdev);extern int disk_expand_part_tbl (struct gendisk *disk,int target); -int rescan_partitions (struct gendisk *disk,struct block_device *bdev, -bool invalidate); extern struct hd_struct * __must_check add_partition(struct gendisk *disk, int partno, sector_t start, sector_t len, int flags,
Signature · 1 line
2.20.1
Re: [PATCH 3/7] block: move rescan_partitions to fs/block_dev.c
Quoted 4 lines · Christoph Hellwig
Large parts of
rescan_partitionsaren't about partitions, and moving it toblock_dev.cwill allow for some further cleanups by merging it into its only caller.
Quoted 9 lines
Signed-off-by Christoph Hellwig <[email protected]>Reviewed-by Jan Kara <[email protected]>--- block/partition-generic.c | 37 ++----------------------------------- fs/block_dev.c | 38 ++++++++++++++++++++++++++++++++++++-- include/linux/fs.h | 2 -- include/linux/genhd.h | 4 ++-- 4 files changed, 40 insertions(+), 41 deletions(-)
Cheers,
Hannes
Signature · 4 lines
Dr. Hannes Reinecke Teamlead Storage & Networking [email protected] +49 911 74053 688 SUSE Software Solutions Germany GmbH, Maxfeldstr. 5, 90409 Nürnberg HRB 247165 (AG München), GF: Felix Imendörffer
PATCH 4/7 block: fix bdev_disk_changed for non-partitioned devices
In reply to Christoph HellwigWe still have to set the capacity to 0 if invalidating or call revalidate_disk if not even if the disk has no partitions. Fix that by merging rescan_partitions into bdev_disk_changed and just stubbing out blk_add_partitions and blk_drop_partitions for non-partitioned devices.
--- block/ioctl.c | 6 ++---- block/partition-generic.c | 5 +++++ fs/block_dev.c | 27 ++++++++------------------- include/linux/genhd.h | 1 + 4 files changed, 16 insertions(+), 23 deletions(-)
block/ioctl.c +2 -4
@@ -162,16 +162,14 @@ static int blkpg_ioctl(struct block_device *bdev, struct blkpg_ioctl_arg __user*/ int __blkdev_reread_part (struct block_device *bdev) { -struct gendisk *disk = bdev->bd_disk; - -if (!disk_part_scan_enabled (disk) || bdev != bdev->bd_contains) +if (!disk_part_scan_enabled (bdev->bd_disk) || bdev != bdev->bd_contains)return -EINVAL ;if (!capable (CAP_SYS_ADMIN ))return -EACCES ;lockdep_assert_held (&bdev->bd_mutex); -return rescan_partitions (disk, bdev, false); +return bdev_disk_changed (bdev, false); }EXPORT_SYMBOL (__blkdev_reread_part);
block/partition-generic.c +5 -0
@@ -445,6 +445,8 @@ int blk_drop_partitions(struct gendisk *disk, struct block_device *bdev)struct hd_struct *part;int res; +if (!disk_part_scan_enabled (disk)) +return 0 ;if (bdev->bd_part_count || bdev->bd_super)return -EBUSY ; res =invalidate_partition (disk,0 );
@@ -514,6 +516,9 @@ int blk_add_partitions(struct gendisk *disk, struct block_device *bdev)struct parsed_partitions *state;int ret = -EAGAIN , p, highest; +if (!disk_part_scan_enabled (disk)) +return 0 ; + state =check_partition (disk, bdev);if (!state)return 0 ;
fs/block_dev.c +8 -19
@@ -1508,9 +1508,9 @@ EXPORT_SYMBOL(bd_set_size);static void __blkdev_put (struct block_device *bdev,fmode_t mode,int for_part); -static int rescan_partitions (struct gendisk *disk,struct block_device *bdev, -bool invalidate) +int bdev_disk_changed (struct block_device *bdev,bool invalidate) { +struct gendisk *disk = bdev->bd_disk;int ret; rescan:
@@ -1526,30 +1526,19 @@ static int rescan_partitions(struct gendisk *disk, struct block_device *bdev,check_disk_size_change (disk, bdev, !invalidate); bdev->bd_invalidated =0 ; -if (!get_capacity (disk)) { +if (get_capacity (disk)) { + ret =blk_add_partitions (disk, bdev); +if (ret == -EAGAIN ) + goto rescan; + }else {/* * Tell userspace that the media / partition table may have * changed. */ kobject_uevent (&disk_to_dev (disk)->kobj,KOBJ_CHANGE ); -return 0 ; } - - ret =blk_add_partitions (disk, bdev); -if (ret == -EAGAIN ) - goto rescan; -return ret; -} - -static void bdev_disk_changed (struct block_device *bdev,bool invalidate) -{ -if (disk_part_scan_enabled (bdev->bd_disk)) { -rescan_partitions (bdev->bd_disk, bdev, invalidate); - }else { -check_disk_size_change (bdev->bd_disk, bdev, !invalidate); - bdev->bd_invalidated =0 ; - } +return ret; }/*
include/linux/genhd.h +1 -0
@@ -621,6 +621,7 @@ extern void blk_invalidate_devt(dev_t devt);extern dev_t blk_lookup_devt (const char *name,int partno);extern char *disk_name (struct gendisk *hd,int partno,char *buf); +int bdev_disk_changed (struct block_device *bdev,bool invalidate);int blk_add_partitions (struct gendisk *disk,struct block_device *bdev);int blk_drop_partitions (struct gendisk *disk,struct block_device *bdev);extern int disk_expand_part_tbl (struct gendisk *disk,int target);
Signature · 1 line
2.20.1
Re: [PATCH 4/7] block: fix bdev_disk_changed for non-partitioned devices
Quoted 6 lines · Christoph Hellwig
We still have to set the capacity to 0 if invalidating or call
revalidate_diskif not even if the disk has no partitions. Fix that by mergingrescan_partitionsintobdev_disk_changedand just stubbing outblk_add_partitionsandblk_drop_partitionsfor non-partitioned devices.
Quoted 9 lines
Signed-off-by Christoph Hellwig <[email protected]>Reviewed-by Jan Kara <[email protected]>--- block/ioctl.c | 6 ++---- block/partition-generic.c | 5 +++++ fs/block_dev.c | 27 ++++++++------------------- include/linux/genhd.h | 1 + 4 files changed, 16 insertions(+), 23 deletions(-)
Cheers,
Hannes
Signature · 4 lines
Dr. Hannes Reinecke Teamlead Storage & Networking [email protected] +49 911 74053 688 SUSE Software Solutions Germany GmbH, Maxfeldstr. 5, 90409 Nürnberg HRB 247165 (AG München), GF: Felix Imendörffer
PATCH 5/7 block: remove (__)blkdev_reread_part as an exported API
In reply to Christoph HellwigIn general drivers should never mess with partition tables directly. Unfortunately s390 and loop do for somewhat historic reasons, but they can use bdev_disk_changed directly instead when we export it as they satisfy the sanity checks we have in __blkdev_reread_part.
--- block/ioctl.c | 35 +++++---------------------------- drivers/block/loop.c | 13 +++++++----- drivers/s390/block/dasd_genhd.c | 4 +++- fs/block_dev.c | 7 +++++++ include/linux/fs.h | 2 -- 5 files changed, 23 insertions(+), 38 deletions(-)
block/ioctl.c +5 -30
@@ -155,46 +155,21 @@ static int blkpg_ioctl(struct block_device *bdev, struct blkpg_ioctl_arg __user } } -/* -* This is an exported API for the block driver, and will not -* acquire bd_mutex. This API should be used in case that -* caller has held bd_mutex already. -*/ -int __blkdev_reread_part (struct block_device *bdev) +static int blkdev_reread_part (struct block_device *bdev) { +int ret; +if (!disk_part_scan_enabled (bdev->bd_disk) || bdev != bdev->bd_contains)return -EINVAL ;if (!capable (CAP_SYS_ADMIN ))return -EACCES ; -lockdep_assert_held (&bdev->bd_mutex); - -return bdev_disk_changed (bdev, false); -} -EXPORT_SYMBOL (__blkdev_reread_part); - -/* -* This is an exported API for the block driver, and will -* try to acquire bd_mutex. If bd_mutex has been held already -* in current context, please call __blkdev_reread_part(). -* -* Make sure the held locks in current context aren't required -* in open()/close() handler and I/O path for avoiding ABBA deadlock: -* - bd_mutex is held before calling block driver's open/close -* handler -* - reading partition table may submit I/O to the block device -*/ -int blkdev_reread_part (struct block_device *bdev) -{ -int res; -mutex_lock (&bdev->bd_mutex); - res =__blkdev_reread_part (bdev); + ret =bdev_disk_changed (bdev, false);mutex_unlock (&bdev->bd_mutex); -return res; +return ret; } -EXPORT_SYMBOL (blkdev_reread_part);static int blk_ioctl_discard (struct block_device *bdev,fmode_t mode,unsigned long arg,unsigned long flags)
drivers/block/loop.c +8 -5
@@ -640,7 +640,9 @@ static void loop_reread_partitions(struct loop_device *lo, {int rc; - rc =blkdev_reread_part (bdev); +mutex_lock (&bdev->bd_mutex); + rc =bdev_disk_changed (bdev, false); +mutex_unlock (&bdev->bd_mutex);if (rc)pr_warn ("%s: partition scan of loop%d (%s) failed (rc=%d)\n" , __func__, lo->lo_number, lo->lo_file_name, rc);
@@ -1164,10 +1166,11 @@ static int __loop_clr_fd(struct loop_device *lo, bool release)* must be at least one and it can only become zero when the * current holder is released. */ -if (release) - err =__blkdev_reread_part (bdev); -else - err =blkdev_reread_part (bdev); +if (!release) +mutex_lock (&bdev->bd_mutex); + err =bdev_disk_changed (bdev, false); +if (!release) +mutex_unlock (&bdev->bd_mutex);if (err)pr_warn ("%s: partition scan of loop%d failed (rc=%d)\n" , __func__, lo_number, err);
drivers/s390/block/dasd_genhd.c +3 -1
@@ -116,7 +116,9 @@ int dasd_scan_partitions(struct dasd_block *block)return -ENODEV ; } - rc =blkdev_reread_part (bdev); +mutex_lock (&bdev->bd_mutex); + rc =bdev_disk_changed (bdev, false); +mutex_unlock (&bdev->bd_mutex);if (rc)DBF_DEV_EVENT (DBF_ERR , block->base,"scan partitions error, rc %d" , rc);
fs/block_dev.c +7 -0
@@ -1513,6 +1513,8 @@ int bdev_disk_changed(struct block_device *bdev, bool invalidate)struct gendisk *disk = bdev->bd_disk;int ret; +lockdep_assert_held (&bdev->bd_mutex); + rescan: ret =blk_drop_partitions (disk, bdev);if (ret)
@@ -1540,6 +1542,11 @@ int bdev_disk_changed(struct block_device *bdev, bool invalidate)return ret; } +/* +* Only exported for for loop and dasd for historic reasons. Don't use in new +* code! +*/ +EXPORT_SYMBOL_GPL (bdev_disk_changed);/* * bd_mutex locking:
include/linux/fs.h +0 -2
@@ -2632,8 +2632,6 @@ extern void bd_finish_claiming(struct block_device *bdev,extern void bd_abort_claiming (struct block_device *bdev,struct block_device *whole,void *holder);extern void blkdev_put (struct block_device *bdev,fmode_t mode); -extern int __blkdev_reread_part (struct block_device *bdev); -extern int blkdev_reread_part (struct block_device *bdev);#ifdef CONFIG_SYSFS extern int bd_link_disk_holder (struct block_device *bdev,struct gendisk *disk);
Signature · 1 line
2.20.1
Re: [PATCH 5/7] block: remove (__)blkdev_reread_part as an exported API
Quoted 8 lines · Christoph Hellwig
In general drivers should never mess with partition tables directly. Unfortunately s390 and loop do for somewhat historic reasons, but they can use
bdev_disk_changeddirectly instead when we export it as they satisfy the sanity checks we have in__blkdev_reread_part.Signed-off-by Christoph Hellwig <[email protected]>Reviewed-by Stefan Haberland <[email protected]> [dasd]Reviewed-by Jan Kara <[email protected]>
Quoted 8 lines
--- block/ioctl.c | 35 +++++---------------------------- drivers/block/loop.c | 13 +++++++----- drivers/s390/block/dasd_genhd.c | 4 +++- fs/block_dev.c | 7 +++++++ include/linux/fs.h | 2 -- 5 files changed, 23 insertions(+), 38 deletions(-)
Cheers,
Hannes
Signature · 4 lines
Dr. Hannes Reinecke Teamlead Storage & Networking [email protected] +49 911 74053 688 SUSE Software Solutions Germany GmbH, Maxfeldstr. 5, 90409 Nürnberg HRB 247165 (AG München), GF: Felix Imendörffer
PATCH 6/7 block: move clearing bd_invalidated into check_disk_size_change
In reply to Christoph HellwigBoth callers of check_disk_size_change clear bd_invalidate directly after the call, so move the clearing into check_disk_size_change itself.
--- fs/block_dev.c | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-)
fs/block_dev.c +1 -2
@@ -1433,6 +1433,7 @@ static void check_disk_size_change(struct gendisk *disk,if (bdev_size > disk_size)flush_disk (bdev, false); } + bdev->bd_invalidated =0 ; }/**
@@ -1462,7 +1463,6 @@ int revalidate_disk(struct gendisk *disk)mutex_lock (&bdev->bd_mutex);check_disk_size_change (disk, bdev, ret ==0 ); - bdev->bd_invalidated =0 ;mutex_unlock (&bdev->bd_mutex);bdput (bdev); }
@@ -1526,7 +1526,6 @@ int bdev_disk_changed(struct block_device *bdev, bool invalidate) disk->fops->revalidate_disk (disk);check_disk_size_change (disk, bdev, !invalidate); - bdev->bd_invalidated =0 ;if (get_capacity (disk)) { ret =blk_add_partitions (disk, bdev);
Signature · 1 line
2.20.1
Re: [PATCH 6/7] block: move clearing bd_invalidated into check_disk_size_change
Quoted 9 lines · Christoph Hellwig
Both callers of
check_disk_size_changeclearbd_invalidatedirectly after the call, so move the clearing intocheck_disk_size_changeitself.Signed-off-by Christoph Hellwig <[email protected]>--- fs/block_dev.c | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-)
Cheers,
Hannes
Signature · 4 lines
Dr. Hannes Reinecke Teamlead Storage & Networking [email protected] +49 911 74053 688 SUSE Software Solutions Germany GmbH, Maxfeldstr. 5, 90409 Nürnberg HRB 247165 (AG München), GF: Felix Imendörffer
PATCH 7/7 block: move setting bd_invalidated from flush_disk to check_disk_change
In reply to Christoph HellwigThe only other caller of flush_disk instantly clears the flag, so don't bother setting it there.
--- fs/block_dev.c | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-)
fs/block_dev.c +1 -1
@@ -1403,7 +1403,6 @@ static void flush_disk(struct block_device *bdev, bool kill_dirty)"resized disk %s\n" , bdev->bd_disk ? bdev->bd_disk->disk_name : ""); } - bdev->bd_invalidated =1 ; }/**
@@ -1491,6 +1490,7 @@ int check_disk_change(struct block_device *bdev)return 0 ;flush_disk (bdev, true); + bdev->bd_invalidated =1 ;if (bdops->revalidate_disk) bdops->revalidate_disk (bdev->bd_disk);return 1 ;
Signature · 1 line
2.20.1
Re: [PATCH 7/7] block: move setting bd_invalidated from flush_disk to check_disk_change
Quoted 22 lines · Christoph Hellwig
The only other caller of
flush_diskinstantly clears the flag, so don't bother setting it there.Signed-off-by Christoph Hellwig <[email protected]>--- fs/block_dev.c | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-)fs/block_dev.c +0
-1@@ -1403,7 +1403,6 @@ static void flush_disk(struct block_device *bdev, bool kill_dirty)"resized disk %s\n" , bdev->bd_disk ? bdev->bd_disk->disk_name : ""); } - bdev->bd_invalidated =1 ; }/**
fs/block_dev.c int check_disk_change(struct block_device *bdev) · line 1496
Quoted 7 lines
fs/block_dev.c +1
-0@@ -1491,6 +1490,7 @@ int check_disk_change(struct block_device *bdev)return 0 ;flush_disk (bdev, true); + bdev->bd_invalidated =1 ;if (bdops->revalidate_disk) bdops->revalidate_disk (bdev->bd_disk);return 1 ;
Cheers,
Hannes
Signature · 4 lines
Dr. Hannes Reinecke Teamlead Storage & Networking [email protected] +49 911 74053 688 SUSE Software Solutions Germany GmbH, Maxfeldstr. 5, 90409 Nürnberg HRB 247165 (AG München), GF: Felix Imendörffer
Quoted 11 lines · Christoph Hellwig
Hi Jens and Jan,
this series takes the disk size change detection and revalidations from Jan a step further and fully integrate the code path for partitioned vs non-partitioned devices. It also fixes up a few bits where we have unintentionally differing behavior.
Changes since v1:
- rebased on to of for-5.5/zoned
- fixed a commit message
- added two new trivial patches
Applied, thanks.
Signature · 1 line
Jens Axboe