Skip to content

Commit 560e20e

Browse files
Christoph Hellwigbrauner
authored andcommitted
block: consolidate __invalidate_device and fsync_bdev
We currently have two interfaces that take a block_devices and the find a mounted file systems to flush or invaldidate data on it. Both are a bit problematic because they only work for the "main" block devices that is used as s_dev for the super_block, and because they don't call into the file system at all. Merge the two into a new bdev_mark_dead helper that does both the syncing and invalidation and which is properly documented. This is in preparation of merging the functionality into the ->mark_dead holder operation so that it will work on additional block devices used by a file systems and give us a single entry point for invalidation of dead devices or media. Note that a single standalone fsync_bdev call for an obscure ioctl remains for now, but that one will also be deal with in a bit. Signed-off-by: Christoph Hellwig <hch@lst.de> Reviewed-by: Josef Bacik <josef@toxicpanda.com> Message-Id: <20230811100828.1897174-14-hch@lst.de> Signed-off-by: Christian Brauner <brauner@kernel.org>
1 parent 127a509 commit 560e20e

7 files changed

Lines changed: 37 additions & 20 deletions

File tree

block/bdev.c

Lines changed: 28 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -221,7 +221,6 @@ int fsync_bdev(struct block_device *bdev)
221221
}
222222
return sync_blockdev(bdev);
223223
}
224-
EXPORT_SYMBOL(fsync_bdev);
225224

226225
/**
227226
* freeze_bdev - lock a filesystem and force it into a consistent state
@@ -960,26 +959,50 @@ int lookup_bdev(const char *pathname, dev_t *dev)
960959
}
961960
EXPORT_SYMBOL(lookup_bdev);
962961

963-
int __invalidate_device(struct block_device *bdev, bool kill_dirty)
962+
/**
963+
* bdev_mark_dead - mark a block device as dead
964+
* @bdev: block device to operate on
965+
* @surprise: indicate a surprise removal
966+
*
967+
* Tell the file system that this devices or media is dead. If @surprise is set
968+
* to %true the device or media is already gone, if not we are preparing for an
969+
* orderly removal.
970+
*
971+
* This syncs out all dirty data and writes back inodes and then invalidates any
972+
* cached data in the inodes on the file system, the inodes themselves and the
973+
* block device mapping.
974+
*/
975+
void bdev_mark_dead(struct block_device *bdev, bool surprise)
964976
{
965977
struct super_block *sb = get_super(bdev);
966978
int res = 0;
967979

968980
if (sb) {
981+
if (!surprise)
982+
sync_filesystem(sb);
969983
/*
970984
* no need to lock the super, get_super holds the
971985
* read mutex so the filesystem cannot go away
972986
* under us (->put_super runs with the write lock
973987
* hold).
974988
*/
975989
shrink_dcache_sb(sb);
976-
res = invalidate_inodes(sb, kill_dirty);
990+
res = invalidate_inodes(sb, true);
977991
drop_super(sb);
992+
} else {
993+
if (!surprise)
994+
sync_blockdev(bdev);
978995
}
979996
invalidate_bdev(bdev);
980-
return res;
981997
}
982-
EXPORT_SYMBOL(__invalidate_device);
998+
#ifdef CONFIG_DASD_MODULE
999+
/*
1000+
* Drivers should not use this directly, but the DASD driver has historically
1001+
* had a shutdown to offline mode that doesn't actually remove the gendisk
1002+
* that otherwise looks a lot like a safe device removal.
1003+
*/
1004+
EXPORT_SYMBOL_GPL(bdev_mark_dead);
1005+
#endif
9831006

9841007
void sync_bdevs(bool wait)
9851008
{

block/disk-events.c

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -281,7 +281,7 @@ bool disk_check_media_change(struct gendisk *disk)
281281
if (!(events & DISK_EVENT_MEDIA_CHANGE))
282282
return false;
283283

284-
__invalidate_device(disk->part0, true);
284+
bdev_mark_dead(disk->part0, true);
285285
set_bit(GD_NEED_PART_SCAN, &disk->state);
286286
return true;
287287
}
@@ -300,7 +300,7 @@ void disk_force_media_change(struct gendisk *disk)
300300
{
301301
disk_event_uevent(disk, DISK_EVENT_MEDIA_CHANGE);
302302
inc_diskseq(disk);
303-
__invalidate_device(disk->part0, true);
303+
bdev_mark_dead(disk->part0, true);
304304
set_bit(GD_NEED_PART_SCAN, &disk->state);
305305
}
306306
EXPORT_SYMBOL_GPL(disk_force_media_change);

block/genhd.c

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -647,8 +647,7 @@ void del_gendisk(struct gendisk *disk)
647647
mutex_lock(&disk->open_mutex);
648648
xa_for_each(&disk->part_tbl, idx, part) {
649649
remove_inode_hash(part->bd_inode);
650-
fsync_bdev(part);
651-
__invalidate_device(part, true);
650+
bdev_mark_dead(part, false);
652651
}
653652
mutex_unlock(&disk->open_mutex);
654653

block/partitions/core.c

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -281,10 +281,7 @@ static void delete_partition(struct block_device *part)
281281
* looked up any more even when openers still hold references.
282282
*/
283283
remove_inode_hash(part->bd_inode);
284-
285-
fsync_bdev(part);
286-
__invalidate_device(part, true);
287-
284+
bdev_mark_dead(part, false);
288285
drop_partition(part);
289286
}
290287

drivers/s390/block/dasd.c

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -3626,10 +3626,8 @@ int dasd_generic_set_offline(struct ccw_device *cdev)
36263626
* so sync bdev first and then wait for our queues to become
36273627
* empty
36283628
*/
3629-
if (device->block) {
3630-
fsync_bdev(device->block->bdev);
3631-
__invalidate_device(device->block->bdev, true);
3632-
}
3629+
if (device->block)
3630+
bdev_mark_dead(device->block->bdev, false);
36333631
dasd_schedule_device_bh(device);
36343632
rc = wait_event_interruptible(shutdown_waitq,
36353633
_wait_for_empty_queues(device));

fs/super.c

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1359,7 +1359,7 @@ int get_tree_bdev(struct fs_context *fc,
13591359
/*
13601360
* We drop s_umount here because we need to open the bdev and
13611361
* bdev->open_mutex ranks above s_umount (blkdev_put() ->
1362-
* __invalidate_device()). It is safe because we have active sb
1362+
* bdev_mark_dead()). It is safe because we have active sb
13631363
* reference and SB_BORN is not set yet.
13641364
*/
13651365
up_write(&s->s_umount);
@@ -1411,7 +1411,7 @@ struct dentry *mount_bdev(struct file_system_type *fs_type,
14111411
/*
14121412
* We drop s_umount here because we need to open the bdev and
14131413
* bdev->open_mutex ranks above s_umount (blkdev_put() ->
1414-
* __invalidate_device()). It is safe because we have active sb
1414+
* bdev_mark_dead()). It is safe because we have active sb
14151415
* reference and SB_BORN is not set yet.
14161416
*/
14171417
up_write(&s->s_umount);

include/linux/blkdev.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -751,6 +751,7 @@ static inline int bdev_read_only(struct block_device *bdev)
751751

752752
bool set_capacity_and_notify(struct gendisk *disk, sector_t size);
753753
void disk_force_media_change(struct gendisk *disk);
754+
void bdev_mark_dead(struct block_device *bdev, bool surprise);
754755

755756
void add_disk_randomness(struct gendisk *disk) __latent_entropy;
756757
void rand_initialize_disk(struct gendisk *disk);
@@ -809,7 +810,6 @@ int __register_blkdev(unsigned int major, const char *name,
809810
void unregister_blkdev(unsigned int major, const char *name);
810811

811812
bool disk_check_media_change(struct gendisk *disk);
812-
int __invalidate_device(struct block_device *bdev, bool kill_dirty);
813813
void set_capacity(struct gendisk *disk, sector_t size);
814814

815815
#ifdef CONFIG_BLOCK_HOLDER_DEPRECATED

0 commit comments

Comments
 (0)