Skip to content

Commit f43d8e4

Browse files
mwilckgregkh
authored andcommitted
nvmet: don't report 0-bytes in serial number
commit 42de82a upstream. The NVME standard mandates that the SN, MN, and FR fields of the Identify Controller Data Structure be "ASCII strings". That means that they may not contain 0-bytes, not even string terminators. Signed-off-by: Martin Wilck <mwilck@suse.com> Reviewed-by: Hannes Reinecke <hare@suse.de> [hch: fixed for the move of the serial field, updated description] Signed-off-by: Christoph Hellwig <hch@lst.de> Signed-off-by: Jens Axboe <axboe@kernel.dk> Signed-off-by: Greg Kroah-Hartman <gregkh@linuxfoundation.org>
1 parent 1e38f8e commit f43d8e4

1 file changed

Lines changed: 14 additions & 2 deletions

File tree

drivers/nvme/target/admin-cmd.c

Lines changed: 14 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -166,11 +166,21 @@ static void nvmet_execute_get_log_page(struct nvmet_req *req)
166166
nvmet_req_complete(req, status);
167167
}
168168

169+
static void copy_and_pad(char *dst, int dst_len, const char *src, int src_len)
170+
{
171+
int len = min(src_len, dst_len);
172+
173+
memcpy(dst, src, len);
174+
if (dst_len > len)
175+
memset(dst + len, ' ', dst_len - len);
176+
}
177+
169178
static void nvmet_execute_identify_ctrl(struct nvmet_req *req)
170179
{
171180
struct nvmet_ctrl *ctrl = req->sq->ctrl;
172181
struct nvme_id_ctrl *id;
173182
u16 status = 0;
183+
const char model[] = "Linux";
174184

175185
id = kzalloc(sizeof(*id), GFP_KERNEL);
176186
if (!id) {
@@ -182,8 +192,10 @@ static void nvmet_execute_identify_ctrl(struct nvmet_req *req)
182192
id->vid = 0;
183193
id->ssvid = 0;
184194

185-
memset(id->sn, ' ', sizeof(id->sn));
186-
snprintf(id->sn, sizeof(id->sn), "%llx", ctrl->subsys->serial);
195+
bin2hex(id->sn, &ctrl->subsys->serial,
196+
min(sizeof(ctrl->subsys->serial), sizeof(id->sn) / 2));
197+
copy_and_pad(id->mn, sizeof(id->mn), model, sizeof(model) - 1);
198+
copy_and_pad(id->fr, sizeof(id->fr), UTS_RELEASE, strlen(UTS_RELEASE));
187199

188200
memset(id->mn, ' ', sizeof(id->mn));
189201
strncpy((char *)id->mn, "Linux", sizeof(id->mn));

0 commit comments

Comments
 (0)