Linux kernel mirror (for testing) git.kernel.org/pub/scm/linux/kernel/git/torvalds/linux.git
kernel os linux

nvmet: don't report 0-bytes in serial number

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>

authored by

Martin Wilck and committed by
Jens Axboe
42de82a8 fcbc5459

+14 -2
+14 -2
drivers/nvme/target/admin-cmd.c
··· 168 168 nvmet_req_complete(req, status); 169 169 } 170 170 171 + static void copy_and_pad(char *dst, int dst_len, const char *src, int src_len) 172 + { 173 + int len = min(src_len, dst_len); 174 + 175 + memcpy(dst, src, len); 176 + if (dst_len > len) 177 + memset(dst + len, ' ', dst_len - len); 178 + } 179 + 171 180 static void nvmet_execute_identify_ctrl(struct nvmet_req *req) 172 181 { 173 182 struct nvmet_ctrl *ctrl = req->sq->ctrl; 174 183 struct nvme_id_ctrl *id; 175 184 u16 status = 0; 185 + const char model[] = "Linux"; 176 186 177 187 id = kzalloc(sizeof(*id), GFP_KERNEL); 178 188 if (!id) { ··· 194 184 id->vid = 0; 195 185 id->ssvid = 0; 196 186 197 - memset(id->sn, ' ', sizeof(id->sn)); 198 - snprintf(id->sn, sizeof(id->sn), "%llx", ctrl->subsys->serial); 187 + bin2hex(id->sn, &ctrl->subsys->serial, 188 + min(sizeof(ctrl->subsys->serial), sizeof(id->sn) / 2)); 189 + copy_and_pad(id->mn, sizeof(id->mn), model, sizeof(model) - 1); 190 + copy_and_pad(id->fr, sizeof(id->fr), UTS_RELEASE, strlen(UTS_RELEASE)); 199 191 200 192 memset(id->mn, ' ', sizeof(id->mn)); 201 193 strncpy((char *)id->mn, "Linux", sizeof(id->mn));