Commit 58e46ed9 authored by Christoph Hellwig's avatar Christoph Hellwig Committed by Jens Axboe

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: default avatarChristoph Hellwig <hch@lst.de>
Acked-by: default avatarTejun Heo <tj@kernel.org>
Signed-off-by: default avatarJens Axboe <axboe@kernel.dk>
parent c5638ab4
...@@ -1015,19 +1015,14 @@ static bool bd_may_claim(struct block_device *bdev, struct block_device *whole, ...@@ -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 * @bdev: block device of interest
* @whole: the whole device containing @bdev, may equal @bdev * @whole: the whole device containing @bdev, may equal @bdev
* @holder: holder trying to claim @bdev * @holder: holder trying to claim @bdev
* *
* Prepare to claim @bdev. This function fails if @bdev is already * Claim @bdev. This function fails if @bdev is already claimed by another
* claimed by another holder and waits if another claiming is in * holder and waits if another claiming is in progress. return, the caller
* progress. This function doesn't actually claim. On successful * has ownership of bd_claiming and bd_holder[s].
* 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.
* *
* RETURNS: * RETURNS:
* 0 if @bdev can be claimed, -EBUSY otherwise. * 0 if @bdev can be claimed, -EBUSY otherwise.
...@@ -1036,9 +1031,12 @@ static int bd_prepare_to_claim(struct block_device *bdev, ...@@ -1036,9 +1031,12 @@ static int bd_prepare_to_claim(struct block_device *bdev,
struct block_device *whole, void *holder) struct block_device *whole, void *holder)
{ {
retry: retry:
spin_lock(&bdev_lock);
/* if someone else claimed, fail */ /* 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; return -EBUSY;
}
/* if claiming is already in progress, wait for it to finish */ /* if claiming is already in progress, wait for it to finish */
if (whole->bd_claiming) { if (whole->bd_claiming) {
...@@ -1049,11 +1047,12 @@ static int bd_prepare_to_claim(struct block_device *bdev, ...@@ -1049,11 +1047,12 @@ static int bd_prepare_to_claim(struct block_device *bdev,
spin_unlock(&bdev_lock); spin_unlock(&bdev_lock);
schedule(); schedule();
finish_wait(wq, &wait); finish_wait(wq, &wait);
spin_lock(&bdev_lock);
goto retry; goto retry;
} }
/* yay, all mine */ /* yay, all mine */
whole->bd_claiming = holder;
spin_unlock(&bdev_lock);
return 0; return 0;
} }
...@@ -1134,19 +1133,13 @@ struct block_device *bd_start_claiming(struct block_device *bdev, void *holder) ...@@ -1134,19 +1133,13 @@ struct block_device *bd_start_claiming(struct block_device *bdev, void *holder)
if (!whole) if (!whole)
return ERR_PTR(-ENOMEM); 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); err = bd_prepare_to_claim(bdev, whole, holder);
if (err == 0) { if (err) {
whole->bd_claiming = holder;
spin_unlock(&bdev_lock);
return whole;
} else {
spin_unlock(&bdev_lock);
bdput(whole); bdput(whole);
return ERR_PTR(err); return ERR_PTR(err);
} }
return whole;
} }
EXPORT_SYMBOL(bd_start_claiming); EXPORT_SYMBOL(bd_start_claiming);
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment