Skip to content

Commit

Permalink
ublksrv: add one API for checking if the control device is for recove…
Browse files Browse the repository at this point in the history
…ring

We know if the created control device instance is for recovering or
other purpose, so add one API for checking it instead of using device
state.

This way is more reliable, since DEAD device may appear in recovering
code path.

Fix one segfault issue reported by Changhui:

https://lore.kernel.org/linux-block/CAGVVp+XD5MbYOWL4pbLMxXL0yNKO5NJ84--=KVnW6w5-GF7Drw@mail.gmail.com/

Signed-off-by: Ming Lei <[email protected]>
  • Loading branch information
ming1 committed Jun 5, 2024
1 parent c54760f commit 30f52dc
Show file tree
Hide file tree
Showing 5 changed files with 20 additions and 5 deletions.
7 changes: 7 additions & 0 deletions include/ublksrv.h
Original file line number Diff line number Diff line change
Expand Up @@ -539,6 +539,13 @@ extern void ublksrv_ctrl_prep_recovery(struct ublksrv_ctrl_dev *dev,
*/
extern const char *ublksrv_ctrl_get_recovery_jbuf(const struct ublksrv_ctrl_dev *dev);

/**
* Return true if this control device is for recovering
*
* @param dev the ublksrv control device instance
*/
extern bool ublksrv_is_recovering(const struct ublksrv_ctrl_dev *ctrl_dev);

/** @} */ // end of ctrl_dev group


Expand Down
7 changes: 5 additions & 2 deletions include/ublksrv_priv.h
Original file line number Diff line number Diff line change
Expand Up @@ -55,8 +55,11 @@ struct ublksrv_ctrl_dev {
int tgt_argc;
char **tgt_argv;
};
/* used by ->recovery_tgt() */
const char *recovery_jbuf;
/* used by ->recovery_tgt(), tgt_argc == -1 */
struct {
int padding;
const char *recovery_jbuf;
};
};

cpu_set_t *queues_cpuset;
Expand Down
7 changes: 6 additions & 1 deletion lib/ublksrv.c
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,11 @@
#include "ublksrv_priv.h"
#include "ublksrv_aio.h"

bool ublksrv_is_recovering(const struct ublksrv_ctrl_dev *ctrl_dev)
{
return ctrl_dev->tgt_argc == -1;
}

static inline struct ublksrv_io_desc *ublksrv_get_iod(
const struct _ublksrv_queue *q, int tag)
{
Expand Down Expand Up @@ -74,7 +79,7 @@ static int __ublksrv_tgt_init(struct _ublksrv_dev *dev, const char *type_name,
optind = 0; /* so that we can parse our arguments */
tgt->ops = ops;

if (dev->ctrl_dev->dev_info.state != UBLK_S_DEV_QUIESCED)
if (!ublksrv_is_recovering(dev->ctrl_dev))
ret = ops->init_tgt(local_to_tdev(dev), type, argc, argv);
else {
if (ops->recovery_tgt)
Expand Down
1 change: 1 addition & 0 deletions lib/ublksrv_cmd.c
Original file line number Diff line number Diff line change
Expand Up @@ -589,6 +589,7 @@ void ublksrv_ctrl_prep_recovery(struct ublksrv_ctrl_dev *dev,
{
dev->tgt_type = tgt_type;
dev->tgt_ops = tgt_ops;
dev->tgt_argc = -1;
dev->recovery_jbuf = recovery_jbuf;
}

Expand Down
3 changes: 1 addition & 2 deletions ublksrv_tgt.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -343,11 +343,10 @@ static void *ublksrv_io_handler_fn(void *data)
int buf_size;
char *buf;
const char *jbuf;
bool recoverying = dinfo->state == UBLK_S_DEV_QUIESCED;

pthread_mutex_lock(&jbuf_lock);

if (!recoverying) {
if (!ublksrv_is_recovering(cdev)) {
do {
buf = __ublksrv_tgt_realloc_json_buf(dev, &buf_size);
ret = ublksrv_json_write_queue_info(cdev, buf, buf_size,
Expand Down

0 comments on commit 30f52dc

Please sign in to comment.