Skip to content

Commit 37c3fc9

Browse files
Christoph Hellwigaxboe
authored andcommitted
block: simplify the block device claiming interface
Stop passing the whole device as a separate argument given that it can be trivially deducted and cleanup the !holder debug check. Signed-off-by: Christoph Hellwig <[email protected]> Reviewed-by: Greg Kroah-Hartman <[email protected]> Reviewed-by: Jan Kara <[email protected]> Reviewed-by: Hannes Reinecke <[email protected]> Acked-by: Tejun Heo <[email protected]> Signed-off-by: Jens Axboe <[email protected]>
1 parent a954ea8 commit 37c3fc9

File tree

3 files changed

+25
-44
lines changed

3 files changed

+25
-44
lines changed

drivers/block/loop.c

Lines changed: 5 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1069,7 +1069,6 @@ static int loop_configure(struct loop_device *lo, fmode_t mode,
10691069
struct file *file;
10701070
struct inode *inode;
10711071
struct address_space *mapping;
1072-
struct block_device *claimed_bdev = NULL;
10731072
int error;
10741073
loff_t size;
10751074
bool partscan;
@@ -1088,8 +1087,7 @@ static int loop_configure(struct loop_device *lo, fmode_t mode,
10881087
* here to avoid changing device under exclusive owner.
10891088
*/
10901089
if (!(mode & FMODE_EXCL)) {
1091-
claimed_bdev = bdev_whole(bdev);
1092-
error = bd_prepare_to_claim(bdev, claimed_bdev, loop_configure);
1090+
error = bd_prepare_to_claim(bdev, loop_configure);
10931091
if (error)
10941092
goto out_putf;
10951093
}
@@ -1176,15 +1174,15 @@ static int loop_configure(struct loop_device *lo, fmode_t mode,
11761174
mutex_unlock(&loop_ctl_mutex);
11771175
if (partscan)
11781176
loop_reread_partitions(lo, bdev);
1179-
if (claimed_bdev)
1180-
bd_abort_claiming(bdev, claimed_bdev, loop_configure);
1177+
if (!(mode & FMODE_EXCL))
1178+
bd_abort_claiming(bdev, loop_configure);
11811179
return 0;
11821180

11831181
out_unlock:
11841182
mutex_unlock(&loop_ctl_mutex);
11851183
out_bdev:
1186-
if (claimed_bdev)
1187-
bd_abort_claiming(bdev, claimed_bdev, loop_configure);
1184+
if (!(mode & FMODE_EXCL))
1185+
bd_abort_claiming(bdev, loop_configure);
11881186
out_putf:
11891187
fput(file);
11901188
out:

fs/block_dev.c

Lines changed: 18 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -110,24 +110,20 @@ EXPORT_SYMBOL(invalidate_bdev);
110110
int truncate_bdev_range(struct block_device *bdev, fmode_t mode,
111111
loff_t lstart, loff_t lend)
112112
{
113-
struct block_device *claimed_bdev = NULL;
114-
int err;
115-
116113
/*
117114
* If we don't hold exclusive handle for the device, upgrade to it
118115
* while we discard the buffer cache to avoid discarding buffers
119116
* under live filesystem.
120117
*/
121118
if (!(mode & FMODE_EXCL)) {
122-
claimed_bdev = bdev_whole(bdev);
123-
err = bd_prepare_to_claim(bdev, claimed_bdev,
124-
truncate_bdev_range);
119+
int err = bd_prepare_to_claim(bdev, truncate_bdev_range);
125120
if (err)
126121
return err;
127122
}
123+
128124
truncate_inode_pages_range(bdev->bd_inode->i_mapping, lstart, lend);
129-
if (claimed_bdev)
130-
bd_abort_claiming(bdev, claimed_bdev, truncate_bdev_range);
125+
if (!(mode & FMODE_EXCL))
126+
bd_abort_claiming(bdev, truncate_bdev_range);
131127
return 0;
132128
}
133129
EXPORT_SYMBOL(truncate_bdev_range);
@@ -978,7 +974,6 @@ static bool bd_may_claim(struct block_device *bdev, struct block_device *whole,
978974
/**
979975
* bd_prepare_to_claim - claim a block device
980976
* @bdev: block device of interest
981-
* @whole: the whole device containing @bdev, may equal @bdev
982977
* @holder: holder trying to claim @bdev
983978
*
984979
* Claim @bdev. This function fails if @bdev is already claimed by another
@@ -988,9 +983,12 @@ static bool bd_may_claim(struct block_device *bdev, struct block_device *whole,
988983
* RETURNS:
989984
* 0 if @bdev can be claimed, -EBUSY otherwise.
990985
*/
991-
int bd_prepare_to_claim(struct block_device *bdev, struct block_device *whole,
992-
void *holder)
986+
int bd_prepare_to_claim(struct block_device *bdev, void *holder)
993987
{
988+
struct block_device *whole = bdev_whole(bdev);
989+
990+
if (WARN_ON_ONCE(!holder))
991+
return -EINVAL;
994992
retry:
995993
spin_lock(&bdev_lock);
996994
/* if someone else claimed, fail */
@@ -1030,15 +1028,15 @@ static void bd_clear_claiming(struct block_device *whole, void *holder)
10301028
/**
10311029
* bd_finish_claiming - finish claiming of a block device
10321030
* @bdev: block device of interest
1033-
* @whole: whole block device
10341031
* @holder: holder that has claimed @bdev
10351032
*
10361033
* Finish exclusive open of a block device. Mark the device as exlusively
10371034
* open by the holder and wake up all waiters for exclusive open to finish.
10381035
*/
1039-
static void bd_finish_claiming(struct block_device *bdev,
1040-
struct block_device *whole, void *holder)
1036+
static void bd_finish_claiming(struct block_device *bdev, void *holder)
10411037
{
1038+
struct block_device *whole = bdev_whole(bdev);
1039+
10421040
spin_lock(&bdev_lock);
10431041
BUG_ON(!bd_may_claim(bdev, whole, holder));
10441042
/*
@@ -1063,11 +1061,10 @@ static void bd_finish_claiming(struct block_device *bdev,
10631061
* also used when exclusive open is not actually desired and we just needed
10641062
* to block other exclusive openers for a while.
10651063
*/
1066-
void bd_abort_claiming(struct block_device *bdev, struct block_device *whole,
1067-
void *holder)
1064+
void bd_abort_claiming(struct block_device *bdev, void *holder)
10681065
{
10691066
spin_lock(&bdev_lock);
1070-
bd_clear_claiming(whole, holder);
1067+
bd_clear_claiming(bdev_whole(bdev), holder);
10711068
spin_unlock(&bdev_lock);
10721069
}
10731070
EXPORT_SYMBOL(bd_abort_claiming);
@@ -1487,7 +1484,6 @@ void blkdev_put_no_open(struct block_device *bdev)
14871484
*/
14881485
struct block_device *blkdev_get_by_dev(dev_t dev, fmode_t mode, void *holder)
14891486
{
1490-
struct block_device *claiming;
14911487
bool unblock_events = true;
14921488
struct block_device *bdev;
14931489
struct gendisk *disk;
@@ -1510,15 +1506,9 @@ struct block_device *blkdev_get_by_dev(dev_t dev, fmode_t mode, void *holder)
15101506
disk = bdev->bd_disk;
15111507

15121508
if (mode & FMODE_EXCL) {
1513-
WARN_ON_ONCE(!holder);
1514-
1515-
ret = -ENOMEM;
1516-
claiming = bdget_disk(disk, 0);
1517-
if (!claiming)
1518-
goto put_blkdev;
1519-
ret = bd_prepare_to_claim(bdev, claiming, holder);
1509+
ret = bd_prepare_to_claim(bdev, holder);
15201510
if (ret)
1521-
goto put_claiming;
1511+
goto put_blkdev;
15221512
}
15231513

15241514
disk_block_events(disk);
@@ -1528,7 +1518,7 @@ struct block_device *blkdev_get_by_dev(dev_t dev, fmode_t mode, void *holder)
15281518
if (ret)
15291519
goto abort_claiming;
15301520
if (mode & FMODE_EXCL) {
1531-
bd_finish_claiming(bdev, claiming, holder);
1521+
bd_finish_claiming(bdev, holder);
15321522

15331523
/*
15341524
* Block event polling for write claims if requested. Any write
@@ -1547,18 +1537,13 @@ struct block_device *blkdev_get_by_dev(dev_t dev, fmode_t mode, void *holder)
15471537

15481538
if (unblock_events)
15491539
disk_unblock_events(disk);
1550-
if (mode & FMODE_EXCL)
1551-
bdput(claiming);
15521540
return bdev;
15531541

15541542
abort_claiming:
15551543
if (mode & FMODE_EXCL)
1556-
bd_abort_claiming(bdev, claiming, holder);
1544+
bd_abort_claiming(bdev, holder);
15571545
mutex_unlock(&bdev->bd_mutex);
15581546
disk_unblock_events(disk);
1559-
put_claiming:
1560-
if (mode & FMODE_EXCL)
1561-
bdput(claiming);
15621547
put_blkdev:
15631548
blkdev_put_no_open(bdev);
15641549
if (ret == -ERESTARTSYS)

include/linux/blkdev.h

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1988,10 +1988,8 @@ void blkdev_show(struct seq_file *seqf, off_t offset);
19881988
struct block_device *blkdev_get_by_path(const char *path, fmode_t mode,
19891989
void *holder);
19901990
struct block_device *blkdev_get_by_dev(dev_t dev, fmode_t mode, void *holder);
1991-
int bd_prepare_to_claim(struct block_device *bdev, struct block_device *whole,
1992-
void *holder);
1993-
void bd_abort_claiming(struct block_device *bdev, struct block_device *whole,
1994-
void *holder);
1991+
int bd_prepare_to_claim(struct block_device *bdev, void *holder);
1992+
void bd_abort_claiming(struct block_device *bdev, void *holder);
19951993
void blkdev_put(struct block_device *bdev, fmode_t mode);
19961994

19971995
/* just for blk-cgroup, don't use elsewhere */

0 commit comments

Comments
 (0)