Skip to content

Commit 957df9a

Browse files
Christoph Hellwigaxboe
authored andcommitted
nbd: Remove __force casts
Make it again possible for sparse to verify that blk_status_t and Unix error codes are used in the proper context by making nbd_send_cmd() return a blk_status_t instead of an integer. No functionality has been changed. Signed-off-by: Christoph Hellwig <hch@lst.de> [ bvanassche: added description and made two small formatting changes ] Signed-off-by: Bart Van Assche <bvanassche@acm.org> Reviewed-by: Chaitanya Kulkarni <kch@nvidia.com> Link: https://lore.kernel.org/r/20240604221531.327131-1-bvanassche@acm.org Signed-off-by: Jens Axboe <axboe@kernel.dk>
1 parent e038ee6 commit 957df9a

File tree

1 file changed

+22
-29
lines changed

1 file changed

+22
-29
lines changed

drivers/block/nbd.c

Lines changed: 22 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -589,10 +589,11 @@ static inline int was_interrupted(int result)
589589
}
590590

591591
/*
592-
* Returns BLK_STS_RESOURCE if the caller should retry after a delay. Returns
593-
* -EAGAIN if the caller should requeue @cmd. Returns -EIO if sending failed.
592+
* Returns BLK_STS_RESOURCE if the caller should retry after a delay.
593+
* Returns BLK_STS_IOERR if sending failed.
594594
*/
595-
static int nbd_send_cmd(struct nbd_device *nbd, struct nbd_cmd *cmd, int index)
595+
static blk_status_t nbd_send_cmd(struct nbd_device *nbd, struct nbd_cmd *cmd,
596+
int index)
596597
{
597598
struct request *req = blk_mq_rq_from_pdu(cmd);
598599
struct nbd_config *config = nbd->config;
@@ -614,13 +615,13 @@ static int nbd_send_cmd(struct nbd_device *nbd, struct nbd_cmd *cmd, int index)
614615

615616
type = req_to_nbd_cmd_type(req);
616617
if (type == U32_MAX)
617-
return -EIO;
618+
return BLK_STS_IOERR;
618619

619620
if (rq_data_dir(req) == WRITE &&
620621
(config->flags & NBD_FLAG_READ_ONLY)) {
621622
dev_err_ratelimited(disk_to_dev(nbd->disk),
622623
"Write on read-only\n");
623-
return -EIO;
624+
return BLK_STS_IOERR;
624625
}
625626

626627
if (req->cmd_flags & REQ_FUA)
@@ -674,11 +675,11 @@ static int nbd_send_cmd(struct nbd_device *nbd, struct nbd_cmd *cmd, int index)
674675
nsock->sent = sent;
675676
}
676677
set_bit(NBD_CMD_REQUEUED, &cmd->flags);
677-
return (__force int)BLK_STS_RESOURCE;
678+
return BLK_STS_RESOURCE;
678679
}
679680
dev_err_ratelimited(disk_to_dev(nbd->disk),
680681
"Send control failed (result %d)\n", result);
681-
return -EAGAIN;
682+
goto requeue;
682683
}
683684
send_pages:
684685
if (type != NBD_CMD_WRITE)
@@ -715,12 +716,12 @@ static int nbd_send_cmd(struct nbd_device *nbd, struct nbd_cmd *cmd, int index)
715716
nsock->pending = req;
716717
nsock->sent = sent;
717718
set_bit(NBD_CMD_REQUEUED, &cmd->flags);
718-
return (__force int)BLK_STS_RESOURCE;
719+
return BLK_STS_RESOURCE;
719720
}
720721
dev_err(disk_to_dev(nbd->disk),
721722
"Send data failed (result %d)\n",
722723
result);
723-
return -EAGAIN;
724+
goto requeue;
724725
}
725726
/*
726727
* The completion might already have come in,
@@ -737,7 +738,16 @@ static int nbd_send_cmd(struct nbd_device *nbd, struct nbd_cmd *cmd, int index)
737738
trace_nbd_payload_sent(req, handle);
738739
nsock->pending = NULL;
739740
nsock->sent = 0;
740-
return 0;
741+
__set_bit(NBD_CMD_INFLIGHT, &cmd->flags);
742+
return BLK_STS_OK;
743+
744+
requeue:
745+
/* retry on a different socket */
746+
dev_err_ratelimited(disk_to_dev(nbd->disk),
747+
"Request send failed, requeueing\n");
748+
nbd_mark_nsock_dead(nbd, nsock, 1);
749+
nbd_requeue_cmd(cmd);
750+
return BLK_STS_OK;
741751
}
742752

743753
static int nbd_read_reply(struct nbd_device *nbd, struct socket *sock,
@@ -1018,7 +1028,7 @@ static blk_status_t nbd_handle_cmd(struct nbd_cmd *cmd, int index)
10181028
struct nbd_device *nbd = cmd->nbd;
10191029
struct nbd_config *config;
10201030
struct nbd_sock *nsock;
1021-
int ret;
1031+
blk_status_t ret;
10221032

10231033
lockdep_assert_held(&cmd->lock);
10241034

@@ -1072,28 +1082,11 @@ static blk_status_t nbd_handle_cmd(struct nbd_cmd *cmd, int index)
10721082
ret = BLK_STS_OK;
10731083
goto out;
10741084
}
1075-
/*
1076-
* Some failures are related to the link going down, so anything that
1077-
* returns EAGAIN can be retried on a different socket.
1078-
*/
10791085
ret = nbd_send_cmd(nbd, cmd, index);
1080-
/*
1081-
* Access to this flag is protected by cmd->lock, thus it's safe to set
1082-
* the flag after nbd_send_cmd() succeed to send request to server.
1083-
*/
1084-
if (!ret)
1085-
__set_bit(NBD_CMD_INFLIGHT, &cmd->flags);
1086-
else if (ret == -EAGAIN) {
1087-
dev_err_ratelimited(disk_to_dev(nbd->disk),
1088-
"Request send failed, requeueing\n");
1089-
nbd_mark_nsock_dead(nbd, nsock, 1);
1090-
nbd_requeue_cmd(cmd);
1091-
ret = BLK_STS_OK;
1092-
}
10931086
out:
10941087
mutex_unlock(&nsock->tx_lock);
10951088
nbd_config_put(nbd);
1096-
return ret < 0 ? BLK_STS_IOERR : (__force blk_status_t)ret;
1089+
return ret;
10971090
}
10981091

10991092
static blk_status_t nbd_queue_rq(struct blk_mq_hw_ctx *hctx,

0 commit comments

Comments
 (0)