Skip to content

Commit 5ad8b6a

Browse files
committed
Merge tag 'pull-set_blocksize' of git://git.kernel.org/pub/scm/linux/kernel/git/viro/vfs
Pull vfs blocksize updates from Al Viro: "This gets rid of bogus set_blocksize() uses, switches it over to be based on a 'struct file *' and verifies that the caller has the device opened exclusively" * tag 'pull-set_blocksize' of git://git.kernel.org/pub/scm/linux/kernel/git/viro/vfs: make set_blocksize() fail unless block device is opened exclusive set_blocksize(): switch to passing struct file * btrfs_get_bdev_and_sb(): call set_blocksize() only for exclusive opens swsusp: don't bother with setting block size zram: don't bother with reopening - just use O_EXCL for open swapon(2): open swap with O_EXCL swapon(2)/swapoff(2): don't bother with block size pktcdvd: sort set_blocksize() calls out bcache_register(): don't bother with set_blocksize()
2 parents db3d841 + d18a867 commit 5ad8b6a

File tree

16 files changed

+55
-93
lines changed

16 files changed

+55
-93
lines changed

Documentation/filesystems/porting.rst

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1134,3 +1134,10 @@ superblock of the main block device, i.e., the one stored in sb->s_bdev. Block
11341134
device freezing now works for any block device owned by a given superblock, not
11351135
just the main block device. The get_active_super() helper and bd_fsfreeze_sb
11361136
pointer are gone.
1137+
1138+
---
1139+
1140+
**mandatory**
1141+
1142+
set_blocksize() takes opened struct file instead of struct block_device now
1143+
and it *must* be opened exclusive.

block/bdev.c

Lines changed: 10 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -144,8 +144,11 @@ static void set_init_blocksize(struct block_device *bdev)
144144
bdev->bd_inode->i_blkbits = blksize_bits(bsize);
145145
}
146146

147-
int set_blocksize(struct block_device *bdev, int size)
147+
int set_blocksize(struct file *file, int size)
148148
{
149+
struct inode *inode = file->f_mapping->host;
150+
struct block_device *bdev = I_BDEV(inode);
151+
149152
/* Size must be a power of two, and between 512 and PAGE_SIZE */
150153
if (size > PAGE_SIZE || size < 512 || !is_power_of_2(size))
151154
return -EINVAL;
@@ -154,10 +157,13 @@ int set_blocksize(struct block_device *bdev, int size)
154157
if (size < bdev_logical_block_size(bdev))
155158
return -EINVAL;
156159

160+
if (!file->private_data)
161+
return -EINVAL;
162+
157163
/* Don't change the size if it is same as current */
158-
if (bdev->bd_inode->i_blkbits != blksize_bits(size)) {
164+
if (inode->i_blkbits != blksize_bits(size)) {
159165
sync_blockdev(bdev);
160-
bdev->bd_inode->i_blkbits = blksize_bits(size);
166+
inode->i_blkbits = blksize_bits(size);
161167
kill_bdev(bdev);
162168
}
163169
return 0;
@@ -167,7 +173,7 @@ EXPORT_SYMBOL(set_blocksize);
167173

168174
int sb_set_blocksize(struct super_block *sb, int size)
169175
{
170-
if (set_blocksize(sb->s_bdev, size))
176+
if (set_blocksize(sb->s_bdev_file, size))
171177
return 0;
172178
/* If we get here, we know size is power of two
173179
* and it's value is between 512 and PAGE_SIZE */

block/ioctl.c

Lines changed: 12 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -503,11 +503,14 @@ static int compat_hdio_getgeo(struct block_device *bdev,
503503
#endif
504504

505505
/* set the logical block size */
506-
static int blkdev_bszset(struct block_device *bdev, blk_mode_t mode,
506+
static int blkdev_bszset(struct file *file, blk_mode_t mode,
507507
int __user *argp)
508508
{
509+
// this one might be file_inode(file)->i_rdev - a rare valid
510+
// use of file_inode() for those.
511+
dev_t dev = I_BDEV(file->f_mapping->host)->bd_dev;
512+
struct file *excl_file;
509513
int ret, n;
510-
struct file *file;
511514

512515
if (!capable(CAP_SYS_ADMIN))
513516
return -EACCES;
@@ -517,13 +520,13 @@ static int blkdev_bszset(struct block_device *bdev, blk_mode_t mode,
517520
return -EFAULT;
518521

519522
if (mode & BLK_OPEN_EXCL)
520-
return set_blocksize(bdev, n);
523+
return set_blocksize(file, n);
521524

522-
file = bdev_file_open_by_dev(bdev->bd_dev, mode, &bdev, NULL);
523-
if (IS_ERR(file))
525+
excl_file = bdev_file_open_by_dev(dev, mode, &dev, NULL);
526+
if (IS_ERR(excl_file))
524527
return -EBUSY;
525-
ret = set_blocksize(bdev, n);
526-
fput(file);
528+
ret = set_blocksize(excl_file, n);
529+
fput(excl_file);
527530
return ret;
528531
}
529532

@@ -652,7 +655,7 @@ long blkdev_ioctl(struct file *file, unsigned cmd, unsigned long arg)
652655
case BLKBSZGET: /* get block device soft block size (cf. BLKSSZGET) */
653656
return put_int(argp, block_size(bdev));
654657
case BLKBSZSET:
655-
return blkdev_bszset(bdev, mode, argp);
658+
return blkdev_bszset(file, mode, argp);
656659
case BLKGETSIZE64:
657660
return put_u64(argp, bdev_nr_bytes(bdev));
658661

@@ -712,7 +715,7 @@ long compat_blkdev_ioctl(struct file *file, unsigned cmd, unsigned long arg)
712715
case BLKBSZGET_32: /* get the logical block size (cf. BLKSSZGET) */
713716
return put_int(argp, bdev_logical_block_size(bdev));
714717
case BLKBSZSET_32:
715-
return blkdev_bszset(bdev, mode, argp);
718+
return blkdev_bszset(file, mode, argp);
716719
case BLKGETSIZE64_32:
717720
return put_u64(argp, bdev_nr_bytes(bdev));
718721

drivers/block/pktcdvd.c

Lines changed: 1 addition & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -2215,6 +2215,7 @@ static int pkt_open_dev(struct pktcdvd_device *pd, bool write)
22152215
}
22162216
dev_info(ddev, "%lukB available on disc\n", lba << 1);
22172217
}
2218+
set_blocksize(bdev_file, CD_FRAMESIZE);
22182219

22192220
return 0;
22202221

@@ -2278,11 +2279,6 @@ static int pkt_open(struct gendisk *disk, blk_mode_t mode)
22782279
ret = pkt_open_dev(pd, mode & BLK_OPEN_WRITE);
22792280
if (ret)
22802281
goto out_dec;
2281-
/*
2282-
* needed here as well, since ext2 (among others) may change
2283-
* the blocksize at mount time
2284-
*/
2285-
set_blocksize(disk->part0, CD_FRAMESIZE);
22862282
}
22872283
mutex_unlock(&ctl_mutex);
22882284
mutex_unlock(&pktcdvd_mutex);
@@ -2526,7 +2522,6 @@ static int pkt_new_dev(struct pktcdvd_device *pd, dev_t dev)
25262522
__module_get(THIS_MODULE);
25272523

25282524
pd->bdev_file = bdev_file;
2529-
set_blocksize(file_bdev(bdev_file), CD_FRAMESIZE);
25302525

25312526
atomic_set(&pd->cdrw.pending_bios, 0);
25322527
pd->cdrw.thread = kthread_run(kcdrwd, pd, "%s", pd->disk->disk_name);

drivers/block/zram/zram_drv.c

Lines changed: 7 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -426,11 +426,10 @@ static void reset_bdev(struct zram *zram)
426426
if (!zram->backing_dev)
427427
return;
428428

429-
fput(zram->bdev_file);
430429
/* hope filp_close flush all of IO */
431430
filp_close(zram->backing_dev, NULL);
432431
zram->backing_dev = NULL;
433-
zram->bdev_file = NULL;
432+
zram->bdev = NULL;
434433
zram->disk->fops = &zram_devops;
435434
kvfree(zram->bitmap);
436435
zram->bitmap = NULL;
@@ -473,10 +472,8 @@ static ssize_t backing_dev_store(struct device *dev,
473472
size_t sz;
474473
struct file *backing_dev = NULL;
475474
struct inode *inode;
476-
struct address_space *mapping;
477475
unsigned int bitmap_sz;
478476
unsigned long nr_pages, *bitmap = NULL;
479-
struct file *bdev_file = NULL;
480477
int err;
481478
struct zram *zram = dev_to_zram(dev);
482479

@@ -497,30 +494,21 @@ static ssize_t backing_dev_store(struct device *dev,
497494
if (sz > 0 && file_name[sz - 1] == '\n')
498495
file_name[sz - 1] = 0x00;
499496

500-
backing_dev = filp_open(file_name, O_RDWR|O_LARGEFILE, 0);
497+
backing_dev = filp_open(file_name, O_RDWR | O_LARGEFILE | O_EXCL, 0);
501498
if (IS_ERR(backing_dev)) {
502499
err = PTR_ERR(backing_dev);
503500
backing_dev = NULL;
504501
goto out;
505502
}
506503

507-
mapping = backing_dev->f_mapping;
508-
inode = mapping->host;
504+
inode = backing_dev->f_mapping->host;
509505

510506
/* Support only block device in this moment */
511507
if (!S_ISBLK(inode->i_mode)) {
512508
err = -ENOTBLK;
513509
goto out;
514510
}
515511

516-
bdev_file = bdev_file_open_by_dev(inode->i_rdev,
517-
BLK_OPEN_READ | BLK_OPEN_WRITE, zram, NULL);
518-
if (IS_ERR(bdev_file)) {
519-
err = PTR_ERR(bdev_file);
520-
bdev_file = NULL;
521-
goto out;
522-
}
523-
524512
nr_pages = i_size_read(inode) >> PAGE_SHIFT;
525513
bitmap_sz = BITS_TO_LONGS(nr_pages) * sizeof(long);
526514
bitmap = kvzalloc(bitmap_sz, GFP_KERNEL);
@@ -531,7 +519,7 @@ static ssize_t backing_dev_store(struct device *dev,
531519

532520
reset_bdev(zram);
533521

534-
zram->bdev_file = bdev_file;
522+
zram->bdev = I_BDEV(inode);
535523
zram->backing_dev = backing_dev;
536524
zram->bitmap = bitmap;
537525
zram->nr_pages = nr_pages;
@@ -544,9 +532,6 @@ static ssize_t backing_dev_store(struct device *dev,
544532
out:
545533
kvfree(bitmap);
546534

547-
if (bdev_file)
548-
fput(bdev_file);
549-
550535
if (backing_dev)
551536
filp_close(backing_dev, NULL);
552537

@@ -587,7 +572,7 @@ static void read_from_bdev_async(struct zram *zram, struct page *page,
587572
{
588573
struct bio *bio;
589574

590-
bio = bio_alloc(file_bdev(zram->bdev_file), 1, parent->bi_opf, GFP_NOIO);
575+
bio = bio_alloc(zram->bdev, 1, parent->bi_opf, GFP_NOIO);
591576
bio->bi_iter.bi_sector = entry * (PAGE_SIZE >> 9);
592577
__bio_add_page(bio, page, PAGE_SIZE, 0);
593578
bio_chain(bio, parent);
@@ -703,7 +688,7 @@ static ssize_t writeback_store(struct device *dev,
703688
continue;
704689
}
705690

706-
bio_init(&bio, file_bdev(zram->bdev_file), &bio_vec, 1,
691+
bio_init(&bio, zram->bdev, &bio_vec, 1,
707692
REQ_OP_WRITE | REQ_SYNC);
708693
bio.bi_iter.bi_sector = blk_idx * (PAGE_SIZE >> 9);
709694
__bio_add_page(&bio, page, PAGE_SIZE, 0);
@@ -785,7 +770,7 @@ static void zram_sync_read(struct work_struct *work)
785770
struct bio_vec bv;
786771
struct bio bio;
787772

788-
bio_init(&bio, file_bdev(zw->zram->bdev_file), &bv, 1, REQ_OP_READ);
773+
bio_init(&bio, zw->zram->bdev, &bv, 1, REQ_OP_READ);
789774
bio.bi_iter.bi_sector = zw->entry * (PAGE_SIZE >> 9);
790775
__bio_add_page(&bio, zw->page, PAGE_SIZE, 0);
791776
zw->error = submit_bio_wait(&bio);

drivers/block/zram/zram_drv.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -132,7 +132,7 @@ struct zram {
132132
spinlock_t wb_limit_lock;
133133
bool wb_limit_enable;
134134
u64 bd_wb_limit;
135-
struct file *bdev_file;
135+
struct block_device *bdev;
136136
unsigned long *bitmap;
137137
unsigned long nr_pages;
138138
#endif

drivers/md/bcache/super.c

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -2555,10 +2555,6 @@ static ssize_t register_bcache(struct kobject *k, struct kobj_attribute *attr,
25552555
if (IS_ERR(bdev_file))
25562556
goto out_free_sb;
25572557

2558-
err = "failed to set blocksize";
2559-
if (set_blocksize(file_bdev(bdev_file), 4096))
2560-
goto out_blkdev_put;
2561-
25622558
err = read_super(sb, file_bdev(bdev_file), &sb_disk);
25632559
if (err)
25642560
goto out_blkdev_put;

fs/btrfs/dev-replace.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -316,7 +316,7 @@ static int btrfs_init_dev_replace_tgtdev(struct btrfs_fs_info *fs_info,
316316
set_bit(BTRFS_DEV_STATE_IN_FS_METADATA, &device->dev_state);
317317
set_bit(BTRFS_DEV_STATE_REPLACE_TGT, &device->dev_state);
318318
device->dev_stats_valid = 1;
319-
set_blocksize(device->bdev, BTRFS_BDEV_BLOCKSIZE);
319+
set_blocksize(bdev_file, BTRFS_BDEV_BLOCKSIZE);
320320
device->fs_devices = fs_devices;
321321

322322
ret = btrfs_get_dev_zone_info(device, false);

fs/btrfs/volumes.c

Lines changed: 8 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -482,10 +482,12 @@ btrfs_get_bdev_and_sb(const char *device_path, blk_mode_t flags, void *holder,
482482

483483
if (flush)
484484
sync_blockdev(bdev);
485-
ret = set_blocksize(bdev, BTRFS_BDEV_BLOCKSIZE);
486-
if (ret) {
487-
fput(*bdev_file);
488-
goto error;
485+
if (holder) {
486+
ret = set_blocksize(*bdev_file, BTRFS_BDEV_BLOCKSIZE);
487+
if (ret) {
488+
fput(*bdev_file);
489+
goto error;
490+
}
489491
}
490492
invalidate_bdev(bdev);
491493
*disk_super = btrfs_read_dev_super(bdev);
@@ -498,6 +500,7 @@ btrfs_get_bdev_and_sb(const char *device_path, blk_mode_t flags, void *holder,
498500
return 0;
499501

500502
error:
503+
*disk_super = NULL;
501504
*bdev_file = NULL;
502505
return ret;
503506
}
@@ -2714,7 +2717,7 @@ int btrfs_init_new_device(struct btrfs_fs_info *fs_info, const char *device_path
27142717
set_bit(BTRFS_DEV_STATE_IN_FS_METADATA, &device->dev_state);
27152718
clear_bit(BTRFS_DEV_STATE_REPLACE_TGT, &device->dev_state);
27162719
device->dev_stats_valid = 1;
2717-
set_blocksize(device->bdev, BTRFS_BDEV_BLOCKSIZE);
2720+
set_blocksize(device->bdev_file, BTRFS_BDEV_BLOCKSIZE);
27182721

27192722
if (seeding_dev) {
27202723
btrfs_clear_sb_rdonly(sb);

fs/ext4/super.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5866,7 +5866,7 @@ static struct file *ext4_get_journal_blkdev(struct super_block *sb,
58665866

58675867
sb_block = EXT4_MIN_BLOCK_SIZE / blocksize;
58685868
offset = EXT4_MIN_BLOCK_SIZE % blocksize;
5869-
set_blocksize(bdev, blocksize);
5869+
set_blocksize(bdev_file, blocksize);
58705870
bh = __bread(bdev, sb_block, blocksize);
58715871
if (!bh) {
58725872
ext4_msg(sb, KERN_ERR, "couldn't read superblock of "

0 commit comments

Comments
 (0)