Skip to content

Commit 05bdb99

Browse files
Christoph Hellwigaxboe
authored andcommitted
block: replace fmode_t with a block-specific type for block open flags
The only overlap between the block open flags mapped into the fmode_t and other uses of fmode_t are FMODE_READ and FMODE_WRITE. Define a new blk_mode_t instead for use in blkdev_get_by_{dev,path}, ->open and ->ioctl and stop abusing fmode_t. Signed-off-by: Christoph Hellwig <hch@lst.de> Acked-by: Jack Wang <jinpu.wang@ionos.com> [rnbd] Reviewed-by: Hannes Reinecke <hare@suse.de> Reviewed-by: Christian Brauner <brauner@kernel.org> Link: https://lore.kernel.org/r/20230608110258.189493-28-hch@lst.de Signed-off-by: Jens Axboe <axboe@kernel.dk>
1 parent 5e4ea83 commit 05bdb99

82 files changed

Lines changed: 334 additions & 315 deletions

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

arch/um/drivers/ubd_kern.c

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -108,9 +108,9 @@ static inline void ubd_set_bit(__u64 bit, unsigned char *data)
108108
static DEFINE_MUTEX(ubd_lock);
109109
static DEFINE_MUTEX(ubd_mutex); /* replaces BKL, might not be needed */
110110

111-
static int ubd_open(struct gendisk *disk, fmode_t mode);
111+
static int ubd_open(struct gendisk *disk, blk_mode_t mode);
112112
static void ubd_release(struct gendisk *disk);
113-
static int ubd_ioctl(struct block_device *bdev, fmode_t mode,
113+
static int ubd_ioctl(struct block_device *bdev, blk_mode_t mode,
114114
unsigned int cmd, unsigned long arg);
115115
static int ubd_getgeo(struct block_device *bdev, struct hd_geometry *geo);
116116

@@ -1154,7 +1154,7 @@ static int __init ubd_driver_init(void){
11541154

11551155
device_initcall(ubd_driver_init);
11561156

1157-
static int ubd_open(struct gendisk *disk, fmode_t mode)
1157+
static int ubd_open(struct gendisk *disk, blk_mode_t mode)
11581158
{
11591159
struct ubd *ubd_dev = disk->private_data;
11601160
int err = 0;
@@ -1389,7 +1389,7 @@ static int ubd_getgeo(struct block_device *bdev, struct hd_geometry *geo)
13891389
return 0;
13901390
}
13911391

1392-
static int ubd_ioctl(struct block_device *bdev, fmode_t mode,
1392+
static int ubd_ioctl(struct block_device *bdev, blk_mode_t mode,
13931393
unsigned int cmd, unsigned long arg)
13941394
{
13951395
struct ubd *ubd_dev = bdev->bd_disk->private_data;

arch/xtensa/platforms/iss/simdisk.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -120,7 +120,7 @@ static void simdisk_submit_bio(struct bio *bio)
120120
bio_endio(bio);
121121
}
122122

123-
static int simdisk_open(struct gendisk *disk, fmode_t mode)
123+
static int simdisk_open(struct gendisk *disk, blk_mode_t mode)
124124
{
125125
struct simdisk *dev = disk->private_data;
126126

block/bdev.c

Lines changed: 16 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -93,22 +93,22 @@ EXPORT_SYMBOL(invalidate_bdev);
9393
* Drop all buffers & page cache for given bdev range. This function bails
9494
* with error if bdev has other exclusive owner (such as filesystem).
9595
*/
96-
int truncate_bdev_range(struct block_device *bdev, fmode_t mode,
96+
int truncate_bdev_range(struct block_device *bdev, blk_mode_t mode,
9797
loff_t lstart, loff_t lend)
9898
{
9999
/*
100100
* If we don't hold exclusive handle for the device, upgrade to it
101101
* while we discard the buffer cache to avoid discarding buffers
102102
* under live filesystem.
103103
*/
104-
if (!(mode & FMODE_EXCL)) {
104+
if (!(mode & BLK_OPEN_EXCL)) {
105105
int err = bd_prepare_to_claim(bdev, truncate_bdev_range, NULL);
106106
if (err)
107107
goto invalidate;
108108
}
109109

110110
truncate_inode_pages_range(bdev->bd_inode->i_mapping, lstart, lend);
111-
if (!(mode & FMODE_EXCL))
111+
if (!(mode & BLK_OPEN_EXCL))
112112
bd_abort_claiming(bdev, truncate_bdev_range);
113113
return 0;
114114

@@ -647,7 +647,7 @@ static void blkdev_flush_mapping(struct block_device *bdev)
647647
bdev_write_inode(bdev);
648648
}
649649

650-
static int blkdev_get_whole(struct block_device *bdev, fmode_t mode)
650+
static int blkdev_get_whole(struct block_device *bdev, blk_mode_t mode)
651651
{
652652
struct gendisk *disk = bdev->bd_disk;
653653
int ret;
@@ -679,7 +679,7 @@ static void blkdev_put_whole(struct block_device *bdev)
679679
bdev->bd_disk->fops->release(bdev->bd_disk);
680680
}
681681

682-
static int blkdev_get_part(struct block_device *part, fmode_t mode)
682+
static int blkdev_get_part(struct block_device *part, blk_mode_t mode)
683683
{
684684
struct gendisk *disk = part->bd_disk;
685685
int ret;
@@ -743,11 +743,11 @@ void blkdev_put_no_open(struct block_device *bdev)
743743
{
744744
put_device(&bdev->bd_device);
745745
}
746-
746+
747747
/**
748748
* blkdev_get_by_dev - open a block device by device number
749749
* @dev: device number of block device to open
750-
* @mode: FMODE_* mask
750+
* @mode: open mode (BLK_OPEN_*)
751751
* @holder: exclusive holder identifier
752752
* @hops: holder operations
753753
*
@@ -765,7 +765,7 @@ void blkdev_put_no_open(struct block_device *bdev)
765765
* RETURNS:
766766
* Reference to the block_device on success, ERR_PTR(-errno) on failure.
767767
*/
768-
struct block_device *blkdev_get_by_dev(dev_t dev, fmode_t mode, void *holder,
768+
struct block_device *blkdev_get_by_dev(dev_t dev, blk_mode_t mode, void *holder,
769769
const struct blk_holder_ops *hops)
770770
{
771771
bool unblock_events = true;
@@ -775,8 +775,8 @@ struct block_device *blkdev_get_by_dev(dev_t dev, fmode_t mode, void *holder,
775775

776776
ret = devcgroup_check_permission(DEVCG_DEV_BLOCK,
777777
MAJOR(dev), MINOR(dev),
778-
((mode & FMODE_READ) ? DEVCG_ACC_READ : 0) |
779-
((mode & FMODE_WRITE) ? DEVCG_ACC_WRITE : 0));
778+
((mode & BLK_OPEN_READ) ? DEVCG_ACC_READ : 0) |
779+
((mode & BLK_OPEN_WRITE) ? DEVCG_ACC_WRITE : 0));
780780
if (ret)
781781
return ERR_PTR(ret);
782782

@@ -786,12 +786,12 @@ struct block_device *blkdev_get_by_dev(dev_t dev, fmode_t mode, void *holder,
786786
disk = bdev->bd_disk;
787787

788788
if (holder) {
789-
mode |= FMODE_EXCL;
789+
mode |= BLK_OPEN_EXCL;
790790
ret = bd_prepare_to_claim(bdev, holder, hops);
791791
if (ret)
792792
goto put_blkdev;
793793
} else {
794-
if (WARN_ON_ONCE(mode & FMODE_EXCL)) {
794+
if (WARN_ON_ONCE(mode & BLK_OPEN_EXCL)) {
795795
ret = -EIO;
796796
goto put_blkdev;
797797
}
@@ -821,7 +821,7 @@ struct block_device *blkdev_get_by_dev(dev_t dev, fmode_t mode, void *holder,
821821
* writeable reference is too fragile given the way @mode is
822822
* used in blkdev_get/put().
823823
*/
824-
if ((mode & FMODE_WRITE) && !bdev->bd_write_holder &&
824+
if ((mode & BLK_OPEN_WRITE) && !bdev->bd_write_holder &&
825825
(disk->event_flags & DISK_EVENT_FLAG_BLOCK_ON_EXCL_WRITE)) {
826826
bdev->bd_write_holder = true;
827827
unblock_events = false;
@@ -848,7 +848,7 @@ EXPORT_SYMBOL(blkdev_get_by_dev);
848848
/**
849849
* blkdev_get_by_path - open a block device by name
850850
* @path: path to the block device to open
851-
* @mode: FMODE_* mask
851+
* @mode: open mode (BLK_OPEN_*)
852852
* @holder: exclusive holder identifier
853853
*
854854
* Open the block device described by the device file at @path. If @holder is
@@ -861,7 +861,7 @@ EXPORT_SYMBOL(blkdev_get_by_dev);
861861
* RETURNS:
862862
* Reference to the block_device on success, ERR_PTR(-errno) on failure.
863863
*/
864-
struct block_device *blkdev_get_by_path(const char *path, fmode_t mode,
864+
struct block_device *blkdev_get_by_path(const char *path, blk_mode_t mode,
865865
void *holder, const struct blk_holder_ops *hops)
866866
{
867867
struct block_device *bdev;
@@ -873,7 +873,7 @@ struct block_device *blkdev_get_by_path(const char *path, fmode_t mode,
873873
return ERR_PTR(error);
874874

875875
bdev = blkdev_get_by_dev(dev, mode, holder, hops);
876-
if (!IS_ERR(bdev) && (mode & FMODE_WRITE) && bdev_read_only(bdev)) {
876+
if (!IS_ERR(bdev) && (mode & BLK_OPEN_WRITE) && bdev_read_only(bdev)) {
877877
blkdev_put(bdev, holder);
878878
return ERR_PTR(-EACCES);
879879
}

block/blk-zoned.c

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -356,8 +356,8 @@ int blkdev_report_zones_ioctl(struct block_device *bdev, unsigned int cmd,
356356
return 0;
357357
}
358358

359-
static int blkdev_truncate_zone_range(struct block_device *bdev, fmode_t mode,
360-
const struct blk_zone_range *zrange)
359+
static int blkdev_truncate_zone_range(struct block_device *bdev,
360+
blk_mode_t mode, const struct blk_zone_range *zrange)
361361
{
362362
loff_t start, end;
363363

@@ -376,7 +376,7 @@ static int blkdev_truncate_zone_range(struct block_device *bdev, fmode_t mode,
376376
* BLKRESETZONE, BLKOPENZONE, BLKCLOSEZONE and BLKFINISHZONE ioctl processing.
377377
* Called from blkdev_ioctl.
378378
*/
379-
int blkdev_zone_mgmt_ioctl(struct block_device *bdev, fmode_t mode,
379+
int blkdev_zone_mgmt_ioctl(struct block_device *bdev, blk_mode_t mode,
380380
unsigned int cmd, unsigned long arg)
381381
{
382382
void __user *argp = (void __user *)arg;
@@ -390,7 +390,7 @@ int blkdev_zone_mgmt_ioctl(struct block_device *bdev, fmode_t mode,
390390
if (!bdev_is_zoned(bdev))
391391
return -ENOTTY;
392392

393-
if (!(mode & FMODE_WRITE))
393+
if (!(mode & BLK_OPEN_WRITE))
394394
return -EBADF;
395395

396396
if (copy_from_user(&zrange, argp, sizeof(struct blk_zone_range)))

block/blk.h

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -396,7 +396,7 @@ void disk_free_zone_bitmaps(struct gendisk *disk);
396396
void disk_clear_zone_settings(struct gendisk *disk);
397397
int blkdev_report_zones_ioctl(struct block_device *bdev, unsigned int cmd,
398398
unsigned long arg);
399-
int blkdev_zone_mgmt_ioctl(struct block_device *bdev, fmode_t mode,
399+
int blkdev_zone_mgmt_ioctl(struct block_device *bdev, blk_mode_t mode,
400400
unsigned int cmd, unsigned long arg);
401401
#else /* CONFIG_BLK_DEV_ZONED */
402402
static inline void disk_free_zone_bitmaps(struct gendisk *disk) {}
@@ -407,7 +407,7 @@ static inline int blkdev_report_zones_ioctl(struct block_device *bdev,
407407
return -ENOTTY;
408408
}
409409
static inline int blkdev_zone_mgmt_ioctl(struct block_device *bdev,
410-
fmode_t mode, unsigned int cmd, unsigned long arg)
410+
blk_mode_t mode, unsigned int cmd, unsigned long arg)
411411
{
412412
return -ENOTTY;
413413
}
@@ -451,7 +451,7 @@ static inline void bio_release_page(struct bio *bio, struct page *page)
451451

452452
struct request_queue *blk_alloc_queue(int node_id);
453453

454-
int disk_scan_partitions(struct gendisk *disk, fmode_t mode);
454+
int disk_scan_partitions(struct gendisk *disk, blk_mode_t mode);
455455

456456
int disk_alloc_events(struct gendisk *disk);
457457
void disk_add_events(struct gendisk *disk);
@@ -466,8 +466,9 @@ extern struct device_attribute dev_attr_events_poll_msecs;
466466

467467
extern struct attribute_group blk_trace_attr_group;
468468

469-
int truncate_bdev_range(struct block_device *bdev, fmode_t mode, loff_t lstart,
470-
loff_t lend);
469+
blk_mode_t file_to_blk_mode(struct file *file);
470+
int truncate_bdev_range(struct block_device *bdev, blk_mode_t mode,
471+
loff_t lstart, loff_t lend);
471472
long blkdev_ioctl(struct file *file, unsigned cmd, unsigned long arg);
472473
long compat_blkdev_ioctl(struct file *file, unsigned cmd, unsigned long arg);
473474

block/fops.c

Lines changed: 26 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -470,6 +470,30 @@ static int blkdev_fsync(struct file *filp, loff_t start, loff_t end,
470470
return error;
471471
}
472472

473+
blk_mode_t file_to_blk_mode(struct file *file)
474+
{
475+
blk_mode_t mode = 0;
476+
477+
if (file->f_mode & FMODE_READ)
478+
mode |= BLK_OPEN_READ;
479+
if (file->f_mode & FMODE_WRITE)
480+
mode |= BLK_OPEN_WRITE;
481+
if (file->f_mode & FMODE_EXCL)
482+
mode |= BLK_OPEN_EXCL;
483+
if (file->f_flags & O_NDELAY)
484+
mode |= BLK_OPEN_NDELAY;
485+
486+
/*
487+
* If all bits in O_ACCMODE set (aka O_RDWR | O_WRONLY), the floppy
488+
* driver has historically allowed ioctls as if the file was opened for
489+
* writing, but does not allow and actual reads or writes.
490+
*/
491+
if ((file->f_flags & O_ACCMODE) == (O_RDWR | O_WRONLY))
492+
mode |= BLK_OPEN_WRITE_IOCTL;
493+
494+
return mode;
495+
}
496+
473497
static int blkdev_open(struct inode *inode, struct file *filp)
474498
{
475499
struct block_device *bdev;
@@ -483,14 +507,10 @@ static int blkdev_open(struct inode *inode, struct file *filp)
483507
filp->f_flags |= O_LARGEFILE;
484508
filp->f_mode |= FMODE_NOWAIT | FMODE_BUF_RASYNC;
485509

486-
if (filp->f_flags & O_NDELAY)
487-
filp->f_mode |= FMODE_NDELAY;
488510
if (filp->f_flags & O_EXCL)
489511
filp->f_mode |= FMODE_EXCL;
490-
if ((filp->f_flags & O_ACCMODE) == 3)
491-
filp->f_mode |= FMODE_WRITE_IOCTL;
492512

493-
bdev = blkdev_get_by_dev(inode->i_rdev, filp->f_mode,
513+
bdev = blkdev_get_by_dev(inode->i_rdev, file_to_blk_mode(filp),
494514
(filp->f_mode & FMODE_EXCL) ? filp : NULL,
495515
NULL);
496516
if (IS_ERR(bdev))
@@ -648,7 +668,7 @@ static long blkdev_fallocate(struct file *file, int mode, loff_t start,
648668
filemap_invalidate_lock(inode->i_mapping);
649669

650670
/* Invalidate the page cache, including dirty pages. */
651-
error = truncate_bdev_range(bdev, file->f_mode, start, end);
671+
error = truncate_bdev_range(bdev, file_to_blk_mode(file), start, end);
652672
if (error)
653673
goto fail;
654674

block/genhd.c

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -339,7 +339,7 @@ void disk_uevent(struct gendisk *disk, enum kobject_action action)
339339
}
340340
EXPORT_SYMBOL_GPL(disk_uevent);
341341

342-
int disk_scan_partitions(struct gendisk *disk, fmode_t mode)
342+
int disk_scan_partitions(struct gendisk *disk, blk_mode_t mode)
343343
{
344344
struct block_device *bdev;
345345
int ret = 0;
@@ -357,7 +357,7 @@ int disk_scan_partitions(struct gendisk *disk, fmode_t mode)
357357
* synchronize with other exclusive openers and other partition
358358
* scanners.
359359
*/
360-
if (!(mode & FMODE_EXCL)) {
360+
if (!(mode & BLK_OPEN_EXCL)) {
361361
ret = bd_prepare_to_claim(disk->part0, disk_scan_partitions,
362362
NULL);
363363
if (ret)
@@ -377,7 +377,7 @@ int disk_scan_partitions(struct gendisk *disk, fmode_t mode)
377377
* creat partition for underlying disk.
378378
*/
379379
clear_bit(GD_NEED_PART_SCAN, &disk->state);
380-
if (!(mode & FMODE_EXCL))
380+
if (!(mode & BLK_OPEN_EXCL))
381381
bd_abort_claiming(disk->part0, disk_scan_partitions);
382382
return ret;
383383
}
@@ -505,7 +505,7 @@ int __must_check device_add_disk(struct device *parent, struct gendisk *disk,
505505

506506
bdev_add(disk->part0, ddev->devt);
507507
if (get_capacity(disk))
508-
disk_scan_partitions(disk, FMODE_READ);
508+
disk_scan_partitions(disk, BLK_OPEN_READ);
509509

510510
/*
511511
* Announce the disk and partitions after all partitions are

0 commit comments

Comments
 (0)