Skip to content

Commit

Permalink
block: refactor bd_start_claiming
Browse files Browse the repository at this point in the history
Move the locking and assignment of bd_claiming from bd_start_claiming to
bd_prepare_to_claim.

Signed-off-by: Christoph Hellwig <[email protected]>
Acked-by: Tejun Heo <[email protected]>
Signed-off-by: Jens Axboe <[email protected]>
  • Loading branch information
Christoph Hellwig authored and axboe committed Jul 16, 2020
1 parent c5638ab commit 58e46ed
Showing 1 changed file with 13 additions and 20 deletions.
33 changes: 13 additions & 20 deletions fs/block_dev.c
Original file line number Diff line number Diff line change
Expand Up @@ -1015,19 +1015,14 @@ static bool bd_may_claim(struct block_device *bdev, struct block_device *whole,
}

/**
* bd_prepare_to_claim - prepare to claim a block device
* bd_prepare_to_claim - claim a block device
* @bdev: block device of interest
* @whole: the whole device containing @bdev, may equal @bdev
* @holder: holder trying to claim @bdev
*
* Prepare to claim @bdev. This function fails if @bdev is already
* claimed by another holder and waits if another claiming is in
* progress. This function doesn't actually claim. On successful
* return, the caller has ownership of bd_claiming and bd_holder[s].
*
* CONTEXT:
* spin_lock(&bdev_lock). Might release bdev_lock, sleep and regrab
* it multiple times.
* Claim @bdev. This function fails if @bdev is already claimed by another
* holder and waits if another claiming is in progress. return, the caller
* has ownership of bd_claiming and bd_holder[s].
*
* RETURNS:
* 0 if @bdev can be claimed, -EBUSY otherwise.
Expand All @@ -1036,9 +1031,12 @@ static int bd_prepare_to_claim(struct block_device *bdev,
struct block_device *whole, void *holder)
{
retry:
spin_lock(&bdev_lock);
/* if someone else claimed, fail */
if (!bd_may_claim(bdev, whole, holder))
if (!bd_may_claim(bdev, whole, holder)) {
spin_unlock(&bdev_lock);
return -EBUSY;
}

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

/* yay, all mine */
whole->bd_claiming = holder;
spin_unlock(&bdev_lock);
return 0;
}

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

/* prepare to claim, if successful, mark claiming in progress */
spin_lock(&bdev_lock);

err = bd_prepare_to_claim(bdev, whole, holder);
if (err == 0) {
whole->bd_claiming = holder;
spin_unlock(&bdev_lock);
return whole;
} else {
spin_unlock(&bdev_lock);
if (err) {
bdput(whole);
return ERR_PTR(err);
}

return whole;
}
EXPORT_SYMBOL(bd_start_claiming);

Expand Down

0 comments on commit 58e46ed

Please sign in to comment.