Skip to content

Commit 6202783

Browse files
damien-lemoalkeithbusch
authored andcommitted
nvmet: Improve nvmet_alloc_ctrl() interface and implementation
Introduce struct nvmet_alloc_ctrl_args to define the arguments for the function nvmet_alloc_ctrl() to avoid the need for passing a pointer to a struct nvmet_req as an argument. This new data structure aggregates together the arguments that were passed to nvmet_alloc_ctrl() (subsysnqn, hostnqn and kato), together with the struct nvmet_req fields used by nvmet_alloc_ctrl(), that is, the fields port, p2p_client, and ops as input and the result and error_loc fields as output, as well as a status field. nvmet_alloc_ctrl() is also changed to return a pointer to the allocated and initialized controller structure instead of a status code, as the status is now returned through the status field of struct nvmet_alloc_ctrl_args. The function nvmet_setup_p2p_ns_map() is changed to not take a pointer to a struct nvmet_req as argument, instead, directly specify the p2p_client device pointer needed as argument. The code in nvmet_execute_admin_connect() that initializes a new target controller after allocating it is moved into nvmet_alloc_ctrl(). The code that sets up an admin queue for the controller (and the call to nvmet_install_queue()) remains in nvmet_execute_admin_connect(). Finally, nvmet_alloc_ctrl() is also exported to allow target drivers to use this function directly to allocate and initialize a new controller structure without the need to rely on a fabrics connect command request. Signed-off-by: Damien Le Moal <[email protected]> Reviewed-by: Christoph Hellwig <[email protected]> Tested-by: Rick Wertenbroek <[email protected]> Tested-by: Manivannan Sadhasivam <[email protected]> Signed-off-by: Keith Busch <[email protected]>
1 parent 200adac commit 6202783

File tree

3 files changed

+94
-65
lines changed

3 files changed

+94
-65
lines changed

drivers/nvme/target/core.c

Lines changed: 53 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -1350,15 +1350,15 @@ bool nvmet_host_allowed(struct nvmet_subsys *subsys, const char *hostnqn)
13501350
* Note: ctrl->subsys->lock should be held when calling this function
13511351
*/
13521352
static void nvmet_setup_p2p_ns_map(struct nvmet_ctrl *ctrl,
1353-
struct nvmet_req *req)
1353+
struct device *p2p_client)
13541354
{
13551355
struct nvmet_ns *ns;
13561356
unsigned long idx;
13571357

1358-
if (!req->p2p_client)
1358+
if (!p2p_client)
13591359
return;
13601360

1361-
ctrl->p2p_client = get_device(req->p2p_client);
1361+
ctrl->p2p_client = get_device(p2p_client);
13621362

13631363
xa_for_each(&ctrl->subsys->namespaces, idx, ns)
13641364
nvmet_p2pmem_ns_add_p2p(ctrl, ns);
@@ -1387,45 +1387,44 @@ static void nvmet_fatal_error_handler(struct work_struct *work)
13871387
ctrl->ops->delete_ctrl(ctrl);
13881388
}
13891389

1390-
u16 nvmet_alloc_ctrl(const char *subsysnqn, const char *hostnqn,
1391-
struct nvmet_req *req, u32 kato, struct nvmet_ctrl **ctrlp,
1392-
uuid_t *hostid)
1390+
struct nvmet_ctrl *nvmet_alloc_ctrl(struct nvmet_alloc_ctrl_args *args)
13931391
{
13941392
struct nvmet_subsys *subsys;
13951393
struct nvmet_ctrl *ctrl;
1394+
u32 kato = args->kato;
1395+
u8 dhchap_status;
13961396
int ret;
1397-
u16 status;
13981397

1399-
status = NVME_SC_CONNECT_INVALID_PARAM | NVME_STATUS_DNR;
1400-
subsys = nvmet_find_get_subsys(req->port, subsysnqn);
1398+
args->status = NVME_SC_CONNECT_INVALID_PARAM | NVME_STATUS_DNR;
1399+
subsys = nvmet_find_get_subsys(args->port, args->subsysnqn);
14011400
if (!subsys) {
14021401
pr_warn("connect request for invalid subsystem %s!\n",
1403-
subsysnqn);
1404-
req->cqe->result.u32 = IPO_IATTR_CONNECT_DATA(subsysnqn);
1405-
req->error_loc = offsetof(struct nvme_common_command, dptr);
1406-
goto out;
1402+
args->subsysnqn);
1403+
args->result = IPO_IATTR_CONNECT_DATA(subsysnqn);
1404+
args->error_loc = offsetof(struct nvme_common_command, dptr);
1405+
return NULL;
14071406
}
14081407

14091408
down_read(&nvmet_config_sem);
1410-
if (!nvmet_host_allowed(subsys, hostnqn)) {
1409+
if (!nvmet_host_allowed(subsys, args->hostnqn)) {
14111410
pr_info("connect by host %s for subsystem %s not allowed\n",
1412-
hostnqn, subsysnqn);
1413-
req->cqe->result.u32 = IPO_IATTR_CONNECT_DATA(hostnqn);
1411+
args->hostnqn, args->subsysnqn);
1412+
args->result = IPO_IATTR_CONNECT_DATA(hostnqn);
14141413
up_read(&nvmet_config_sem);
1415-
status = NVME_SC_CONNECT_INVALID_HOST | NVME_STATUS_DNR;
1416-
req->error_loc = offsetof(struct nvme_common_command, dptr);
1414+
args->status = NVME_SC_CONNECT_INVALID_HOST | NVME_STATUS_DNR;
1415+
args->error_loc = offsetof(struct nvme_common_command, dptr);
14171416
goto out_put_subsystem;
14181417
}
14191418
up_read(&nvmet_config_sem);
14201419

1421-
status = NVME_SC_INTERNAL;
1420+
args->status = NVME_SC_INTERNAL;
14221421
ctrl = kzalloc(sizeof(*ctrl), GFP_KERNEL);
14231422
if (!ctrl)
14241423
goto out_put_subsystem;
14251424
mutex_init(&ctrl->lock);
14261425

1427-
ctrl->port = req->port;
1428-
ctrl->ops = req->ops;
1426+
ctrl->port = args->port;
1427+
ctrl->ops = args->ops;
14291428

14301429
#ifdef CONFIG_NVME_TARGET_PASSTHRU
14311430
/* By default, set loop targets to clear IDS by default */
@@ -1439,8 +1438,8 @@ u16 nvmet_alloc_ctrl(const char *subsysnqn, const char *hostnqn,
14391438
INIT_WORK(&ctrl->fatal_err_work, nvmet_fatal_error_handler);
14401439
INIT_DELAYED_WORK(&ctrl->ka_work, nvmet_keep_alive_timer);
14411440

1442-
memcpy(ctrl->subsysnqn, subsysnqn, NVMF_NQN_SIZE);
1443-
memcpy(ctrl->hostnqn, hostnqn, NVMF_NQN_SIZE);
1441+
memcpy(ctrl->subsysnqn, args->subsysnqn, NVMF_NQN_SIZE);
1442+
memcpy(ctrl->hostnqn, args->hostnqn, NVMF_NQN_SIZE);
14441443

14451444
kref_init(&ctrl->ref);
14461445
ctrl->subsys = subsys;
@@ -1463,12 +1462,12 @@ u16 nvmet_alloc_ctrl(const char *subsysnqn, const char *hostnqn,
14631462
subsys->cntlid_min, subsys->cntlid_max,
14641463
GFP_KERNEL);
14651464
if (ret < 0) {
1466-
status = NVME_SC_CONNECT_CTRL_BUSY | NVME_STATUS_DNR;
1465+
args->status = NVME_SC_CONNECT_CTRL_BUSY | NVME_STATUS_DNR;
14671466
goto out_free_sqs;
14681467
}
14691468
ctrl->cntlid = ret;
14701469

1471-
uuid_copy(&ctrl->hostid, hostid);
1470+
uuid_copy(&ctrl->hostid, args->hostid);
14721471

14731472
/*
14741473
* Discovery controllers may use some arbitrary high value
@@ -1490,12 +1489,35 @@ u16 nvmet_alloc_ctrl(const char *subsysnqn, const char *hostnqn,
14901489
if (ret)
14911490
goto init_pr_fail;
14921491
list_add_tail(&ctrl->subsys_entry, &subsys->ctrls);
1493-
nvmet_setup_p2p_ns_map(ctrl, req);
1492+
nvmet_setup_p2p_ns_map(ctrl, args->p2p_client);
14941493
nvmet_debugfs_ctrl_setup(ctrl);
14951494
mutex_unlock(&subsys->lock);
14961495

1497-
*ctrlp = ctrl;
1498-
return 0;
1496+
if (args->hostid)
1497+
uuid_copy(&ctrl->hostid, args->hostid);
1498+
1499+
dhchap_status = nvmet_setup_auth(ctrl);
1500+
if (dhchap_status) {
1501+
pr_err("Failed to setup authentication, dhchap status %u\n",
1502+
dhchap_status);
1503+
nvmet_ctrl_put(ctrl);
1504+
if (dhchap_status == NVME_AUTH_DHCHAP_FAILURE_FAILED)
1505+
args->status =
1506+
NVME_SC_CONNECT_INVALID_HOST | NVME_STATUS_DNR;
1507+
else
1508+
args->status = NVME_SC_INTERNAL;
1509+
return NULL;
1510+
}
1511+
1512+
args->status = NVME_SC_SUCCESS;
1513+
1514+
pr_info("Created %s controller %d for subsystem %s for NQN %s%s%s.\n",
1515+
nvmet_is_disc_subsys(ctrl->subsys) ? "discovery" : "nvm",
1516+
ctrl->cntlid, ctrl->subsys->subsysnqn, ctrl->hostnqn,
1517+
ctrl->pi_support ? " T10-PI is enabled" : "",
1518+
nvmet_has_auth(ctrl) ? " with DH-HMAC-CHAP" : "");
1519+
1520+
return ctrl;
14991521

15001522
init_pr_fail:
15011523
mutex_unlock(&subsys->lock);
@@ -1509,9 +1531,9 @@ u16 nvmet_alloc_ctrl(const char *subsysnqn, const char *hostnqn,
15091531
kfree(ctrl);
15101532
out_put_subsystem:
15111533
nvmet_subsys_put(subsys);
1512-
out:
1513-
return status;
1534+
return NULL;
15141535
}
1536+
EXPORT_SYMBOL_GPL(nvmet_alloc_ctrl);
15151537

15161538
static void nvmet_ctrl_free(struct kref *ref)
15171539
{
@@ -1547,6 +1569,7 @@ void nvmet_ctrl_put(struct nvmet_ctrl *ctrl)
15471569
{
15481570
kref_put(&ctrl->ref, nvmet_ctrl_free);
15491571
}
1572+
EXPORT_SYMBOL_GPL(nvmet_ctrl_put);
15501573

15511574
void nvmet_ctrl_fatal_error(struct nvmet_ctrl *ctrl)
15521575
{

drivers/nvme/target/fabrics-cmd.c

Lines changed: 26 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -213,73 +213,67 @@ static void nvmet_execute_admin_connect(struct nvmet_req *req)
213213
struct nvmf_connect_command *c = &req->cmd->connect;
214214
struct nvmf_connect_data *d;
215215
struct nvmet_ctrl *ctrl = NULL;
216-
u16 status;
217-
u8 dhchap_status;
216+
struct nvmet_alloc_ctrl_args args = {
217+
.port = req->port,
218+
.ops = req->ops,
219+
.p2p_client = req->p2p_client,
220+
.kato = le32_to_cpu(c->kato),
221+
};
218222

219223
if (!nvmet_check_transfer_len(req, sizeof(struct nvmf_connect_data)))
220224
return;
221225

222226
d = kmalloc(sizeof(*d), GFP_KERNEL);
223227
if (!d) {
224-
status = NVME_SC_INTERNAL;
228+
args.status = NVME_SC_INTERNAL;
225229
goto complete;
226230
}
227231

228-
status = nvmet_copy_from_sgl(req, 0, d, sizeof(*d));
229-
if (status)
232+
args.status = nvmet_copy_from_sgl(req, 0, d, sizeof(*d));
233+
if (args.status)
230234
goto out;
231235

232236
if (c->recfmt != 0) {
233237
pr_warn("invalid connect version (%d).\n",
234238
le16_to_cpu(c->recfmt));
235-
req->error_loc = offsetof(struct nvmf_connect_command, recfmt);
236-
status = NVME_SC_CONNECT_FORMAT | NVME_STATUS_DNR;
239+
args.error_loc = offsetof(struct nvmf_connect_command, recfmt);
240+
args.status = NVME_SC_CONNECT_FORMAT | NVME_STATUS_DNR;
237241
goto out;
238242
}
239243

240244
if (unlikely(d->cntlid != cpu_to_le16(0xffff))) {
241245
pr_warn("connect attempt for invalid controller ID %#x\n",
242246
d->cntlid);
243-
status = NVME_SC_CONNECT_INVALID_PARAM | NVME_STATUS_DNR;
244-
req->cqe->result.u32 = IPO_IATTR_CONNECT_DATA(cntlid);
247+
args.status = NVME_SC_CONNECT_INVALID_PARAM | NVME_STATUS_DNR;
248+
args.result = IPO_IATTR_CONNECT_DATA(cntlid);
245249
goto out;
246250
}
247251

248252
d->subsysnqn[NVMF_NQN_FIELD_LEN - 1] = '\0';
249253
d->hostnqn[NVMF_NQN_FIELD_LEN - 1] = '\0';
250-
status = nvmet_alloc_ctrl(d->subsysnqn, d->hostnqn, req,
251-
le32_to_cpu(c->kato), &ctrl, &d->hostid);
252-
if (status)
253-
goto out;
254254

255-
dhchap_status = nvmet_setup_auth(ctrl);
256-
if (dhchap_status) {
257-
pr_err("Failed to setup authentication, dhchap status %u\n",
258-
dhchap_status);
259-
nvmet_ctrl_put(ctrl);
260-
if (dhchap_status == NVME_AUTH_DHCHAP_FAILURE_FAILED)
261-
status = (NVME_SC_CONNECT_INVALID_HOST | NVME_STATUS_DNR);
262-
else
263-
status = NVME_SC_INTERNAL;
255+
args.subsysnqn = d->subsysnqn;
256+
args.hostnqn = d->hostnqn;
257+
args.hostid = &d->hostid;
258+
args.kato = c->kato;
259+
260+
ctrl = nvmet_alloc_ctrl(&args);
261+
if (!ctrl)
264262
goto out;
265-
}
266263

267-
status = nvmet_install_queue(ctrl, req);
268-
if (status) {
264+
args.status = nvmet_install_queue(ctrl, req);
265+
if (args.status) {
269266
nvmet_ctrl_put(ctrl);
270267
goto out;
271268
}
272269

273-
pr_info("creating %s controller %d for subsystem %s for NQN %s%s%s.\n",
274-
nvmet_is_disc_subsys(ctrl->subsys) ? "discovery" : "nvm",
275-
ctrl->cntlid, ctrl->subsys->subsysnqn, ctrl->hostnqn,
276-
ctrl->pi_support ? " T10-PI is enabled" : "",
277-
nvmet_has_auth(ctrl) ? " with DH-HMAC-CHAP" : "");
278-
req->cqe->result.u32 = cpu_to_le32(nvmet_connect_result(ctrl));
270+
args.result = cpu_to_le32(nvmet_connect_result(ctrl));
279271
out:
280272
kfree(d);
281273
complete:
282-
nvmet_req_complete(req, status);
274+
req->error_loc = args.error_loc;
275+
req->cqe->result.u32 = args.result;
276+
nvmet_req_complete(req, args.status);
283277
}
284278

285279
static void nvmet_execute_io_connect(struct nvmet_req *req)

drivers/nvme/target/nvmet.h

Lines changed: 15 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -549,9 +549,21 @@ int nvmet_sq_init(struct nvmet_sq *sq);
549549
void nvmet_ctrl_fatal_error(struct nvmet_ctrl *ctrl);
550550

551551
void nvmet_update_cc(struct nvmet_ctrl *ctrl, u32 new);
552-
u16 nvmet_alloc_ctrl(const char *subsysnqn, const char *hostnqn,
553-
struct nvmet_req *req, u32 kato, struct nvmet_ctrl **ctrlp,
554-
uuid_t *hostid);
552+
553+
struct nvmet_alloc_ctrl_args {
554+
struct nvmet_port *port;
555+
char *subsysnqn;
556+
char *hostnqn;
557+
uuid_t *hostid;
558+
const struct nvmet_fabrics_ops *ops;
559+
struct device *p2p_client;
560+
u32 kato;
561+
u32 result;
562+
u16 error_loc;
563+
u16 status;
564+
};
565+
566+
struct nvmet_ctrl *nvmet_alloc_ctrl(struct nvmet_alloc_ctrl_args *args);
555567
struct nvmet_ctrl *nvmet_ctrl_find_get(const char *subsysnqn,
556568
const char *hostnqn, u16 cntlid,
557569
struct nvmet_req *req);

0 commit comments

Comments
 (0)