Skip to content

Commit 58e46ed

Browse files
Christoph Hellwigaxboe
authored andcommitted
block: refactor bd_start_claiming
Move the locking and assignment of bd_claiming from bd_start_claiming to bd_prepare_to_claim. Signed-off-by: Christoph Hellwig <hch@lst.de> Acked-by: Tejun Heo <tj@kernel.org> Signed-off-by: Jens Axboe <axboe@kernel.dk>
1 parent c5638ab commit 58e46ed

1 file changed

Lines changed: 13 additions & 20 deletions

File tree

‎fs/block_dev.c‎

Lines changed: 13 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -1015,19 +1015,14 @@ static bool bd_may_claim(struct block_device *bdev, struct block_device *whole,
10151015
}
10161016

10171017
/**
1018-
* bd_prepare_to_claim - prepare to claim a block device
1018+
* bd_prepare_to_claim - claim a block device
10191019
* @bdev: block device of interest
10201020
* @whole: the whole device containing @bdev, may equal @bdev
10211021
* @holder: holder trying to claim @bdev
10221022
*
1023-
* Prepare to claim @bdev. This function fails if @bdev is already
1024-
* claimed by another holder and waits if another claiming is in
1025-
* progress. This function doesn't actually claim. On successful
1026-
* return, the caller has ownership of bd_claiming and bd_holder[s].
1027-
*
1028-
* CONTEXT:
1029-
* spin_lock(&bdev_lock). Might release bdev_lock, sleep and regrab
1030-
* it multiple times.
1023+
* Claim @bdev. This function fails if @bdev is already claimed by another
1024+
* holder and waits if another claiming is in progress. return, the caller
1025+
* has ownership of bd_claiming and bd_holder[s].
10311026
*
10321027
* RETURNS:
10331028
* 0 if @bdev can be claimed, -EBUSY otherwise.
@@ -1036,9 +1031,12 @@ static int bd_prepare_to_claim(struct block_device *bdev,
10361031
struct block_device *whole, void *holder)
10371032
{
10381033
retry:
1034+
spin_lock(&bdev_lock);
10391035
/* if someone else claimed, fail */
1040-
if (!bd_may_claim(bdev, whole, holder))
1036+
if (!bd_may_claim(bdev, whole, holder)) {
1037+
spin_unlock(&bdev_lock);
10411038
return -EBUSY;
1039+
}
10421040

10431041
/* if claiming is already in progress, wait for it to finish */
10441042
if (whole->bd_claiming) {
@@ -1049,11 +1047,12 @@ static int bd_prepare_to_claim(struct block_device *bdev,
10491047
spin_unlock(&bdev_lock);
10501048
schedule();
10511049
finish_wait(wq, &wait);
1052-
spin_lock(&bdev_lock);
10531050
goto retry;
10541051
}
10551052

10561053
/* yay, all mine */
1054+
whole->bd_claiming = holder;
1055+
spin_unlock(&bdev_lock);
10571056
return 0;
10581057
}
10591058

@@ -1134,19 +1133,13 @@ struct block_device *bd_start_claiming(struct block_device *bdev, void *holder)
11341133
if (!whole)
11351134
return ERR_PTR(-ENOMEM);
11361135

1137-
/* prepare to claim, if successful, mark claiming in progress */
1138-
spin_lock(&bdev_lock);
1139-
11401136
err = bd_prepare_to_claim(bdev, whole, holder);
1141-
if (err == 0) {
1142-
whole->bd_claiming = holder;
1143-
spin_unlock(&bdev_lock);
1144-
return whole;
1145-
} else {
1146-
spin_unlock(&bdev_lock);
1137+
if (err) {
11471138
bdput(whole);
11481139
return ERR_PTR(err);
11491140
}
1141+
1142+
return whole;
11501143
}
11511144
EXPORT_SYMBOL(bd_start_claiming);
11521145

0 commit comments

Comments
 (0)