Skip to content

Commit

Permalink
scst_vdisk: Introduce the function vdev_set_t10_dev_id()
Browse files Browse the repository at this point in the history
This patch reduces code duplication.

This patch does not change any functionality.
  • Loading branch information
lnocturno committed Mar 31, 2023
1 parent db581fc commit 7440bb3
Showing 1 changed file with 34 additions and 41 deletions.
75 changes: 34 additions & 41 deletions scst/src/dev_handlers/scst_vdisk.c
Original file line number Diff line number Diff line change
Expand Up @@ -6852,6 +6852,35 @@ static void vdev_check_node(struct scst_vdisk_dev **pvirt_dev, int orig_nodeid)
return;
}

static int vdev_set_t10_dev_id(struct scst_vdisk_dev *virt_dev,
const char *buf, size_t count)
{
size_t t10_dev_id_size, i;

t10_dev_id_size = sizeof(virt_dev->t10_dev_id);

if (count > t10_dev_id_size ||
(count == t10_dev_id_size && buf[count - 1] != '\n')) {
PRINT_ERROR("T10 device id is too long (max %zu characters)",
t10_dev_id_size - 1);
return -EINVAL;
}

memset(virt_dev->t10_dev_id, 0, t10_dev_id_size);
memcpy(virt_dev->t10_dev_id, buf, count);

for (i = 0; i < t10_dev_id_size; i++) {
if (virt_dev->t10_dev_id[i] == '\n') {
virt_dev->t10_dev_id[i] = '\0';
break;
}
}

virt_dev->t10_dev_id_set = 1;

return 0;
}

/*
* Parse the add_device parameters. @allowed_params restricts which
* parameters can be specified at device creation time.
Expand Down Expand Up @@ -6994,29 +7023,12 @@ static int vdev_parse_add_dev_params(struct scst_vdisk_dev *virt_dev,
}

if (!strcasecmp("t10_dev_id", p)) {
/* Handle in a fashion similar to vdev_sysfs_t10_dev_id_store */
int i, length = strlen(pp);

if (length >= sizeof(virt_dev->t10_dev_id)) {
PRINT_ERROR("T10 device id is too long (max %zd "
"characters)", sizeof(virt_dev->t10_dev_id)-1);
res = -EINVAL;
res = vdev_set_t10_dev_id(virt_dev, pp, strlen(pp));
if (unlikely(res)) {
PRINT_ERROR("Failed to set %s t10_dev_id", pp);
goto out;
}

memset(virt_dev->t10_dev_id, 0, sizeof(virt_dev->t10_dev_id));
memcpy(virt_dev->t10_dev_id, pp, length);

i = 0;
while (i < sizeof(virt_dev->t10_dev_id)) {
if (virt_dev->t10_dev_id[i] == '\n') {
virt_dev->t10_dev_id[i] = '\0';
break;
}
i++;
}

virt_dev->t10_dev_id_set = 1;
continue;
}

Expand Down Expand Up @@ -8890,28 +8902,9 @@ static ssize_t vdev_sysfs_t10_dev_id_store(struct kobject *kobj,

write_lock(&vdisk_serial_rwlock);

if ((count > sizeof(virt_dev->t10_dev_id)) ||
((count == sizeof(virt_dev->t10_dev_id)) &&
(buf[count-1] != '\n'))) {
PRINT_ERROR("T10 device id is too long (max %zd "
"characters)", sizeof(virt_dev->t10_dev_id)-1);
res = -EINVAL;
res = vdev_set_t10_dev_id(virt_dev, buf, count);
if (unlikely(res))
goto out_unlock;
}

memset(virt_dev->t10_dev_id, 0, sizeof(virt_dev->t10_dev_id));
memcpy(virt_dev->t10_dev_id, buf, count);

i = 0;
while (i < sizeof(virt_dev->t10_dev_id)) {
if (virt_dev->t10_dev_id[i] == '\n') {
virt_dev->t10_dev_id[i] = '\0';
break;
}
i++;
}

virt_dev->t10_dev_id_set = 1;

schedule_work(&virt_dev->vdev_inq_changed_work);

Expand Down

0 comments on commit 7440bb3

Please sign in to comment.