From b4200acdaf13f15f3c49178b27890a0fc3baada6 Mon Sep 17 00:00:00 2001 From: Vladislav Bolkhovitin Date: Tue, 5 Apr 2011 19:56:01 +0000 Subject: [PATCH] Possibility to specify human readable comments to targets (for instance, to specify which target maps to which port) added git-svn-id: http://svn.code.sf.net/p/scst/svn/trunk@3357 d57e44dd-8a1f-0410-8b47-8ef2f437770f --- scst/include/scst.h | 3 ++ scst/src/scst_lib.c | 1 + scst/src/scst_sysfs.c | 82 +++++++++++++++++++++++++++++++++++++++++++ 3 files changed, 86 insertions(+) diff --git a/scst/include/scst.h b/scst/include/scst.h index 18d55e638..b3daed55f 100644 --- a/scst/include/scst.h +++ b/scst/include/scst.h @@ -1466,6 +1466,9 @@ struct scst_tgt { /* Name of the target */ char *tgt_name; + /* User comment to it to let easier distinguish targets */ + char *tgt_comment; + uint16_t rel_tgt_id; #ifdef CONFIG_SCST_PROC diff --git a/scst/src/scst_lib.c b/scst/src/scst_lib.c index 90259e3e5..748f3a6c8 100644 --- a/scst/src/scst_lib.c +++ b/scst/src/scst_lib.c @@ -2553,6 +2553,7 @@ void scst_free_tgt(struct scst_tgt *tgt) TRACE_ENTRY(); kfree(tgt->tgt_name); + kfree(tgt->tgt_comment); #ifdef CONFIG_SCST_PROC kfree(tgt->default_group_name); #endif diff --git a/scst/src/scst_sysfs.c b/scst/src/scst_sysfs.c index 82425e87e..2c6688b4d 100644 --- a/scst/src/scst_sysfs.c +++ b/scst/src/scst_sysfs.c @@ -2198,6 +2198,80 @@ static struct kobj_attribute scst_rel_tgt_id = __ATTR(rel_tgt_id, S_IRUGO | S_IWUSR, scst_rel_tgt_id_show, scst_rel_tgt_id_store); +static ssize_t scst_tgt_comment_show(struct kobject *kobj, + struct kobj_attribute *attr, char *buf) +{ + struct scst_tgt *tgt; + int res; + + TRACE_ENTRY(); + + tgt = container_of(kobj, struct scst_tgt, tgt_kobj); + + if (tgt->tgt_comment != NULL) + res = sprintf(buf, "%s\n%s", tgt->tgt_comment, + SCST_SYSFS_KEY_MARK "\n"); + else + res = 0; + + TRACE_EXIT_RES(res); + return res; +} + +static ssize_t scst_tgt_comment_store(struct kobject *kobj, + struct kobj_attribute *attr, const char *buf, size_t count) +{ + int res; + struct scst_tgt *tgt; + char *p; + int len; + + TRACE_ENTRY(); + + if ((buf == NULL) || (count == 0)) { + res = 0; + goto out; + } + + tgt = container_of(kobj, struct scst_tgt, tgt_kobj); + + len = strnlen(buf, count); + if (buf[count-1] == '\n') + len--; + + if (len == 0) { + kfree(tgt->tgt_comment); + tgt->tgt_comment = NULL; + goto out_done; + } + + p = kmalloc(len+1, GFP_KERNEL); + if (p == NULL) { + PRINT_ERROR("Unable to alloc tgt_comment string (len %d)", + len+1); + res = -ENOMEM; + goto out; + } + + memcpy(p, buf, len); + p[len] = '\0'; + + kfree(tgt->tgt_comment); + + tgt->tgt_comment = p; + +out_done: + res = count; + +out: + TRACE_EXIT_RES(res); + return res; +} + +static struct kobj_attribute scst_tgt_comment = + __ATTR(comment, S_IRUGO | S_IWUSR, scst_tgt_comment_show, + scst_tgt_comment_store); + /* * Supposed to be called under scst_mutex. In case of error will drop, * then reacquire it. @@ -2269,6 +2343,14 @@ int scst_tgt_sysfs_create(struct scst_tgt *tgt) goto out_err; } + res = sysfs_create_file(&tgt->tgt_kobj, + &scst_tgt_comment.attr); + if (res != 0) { + PRINT_ERROR("Can't add attribute %s for tgt %s", + scst_tgt_comment.attr.name, tgt->tgt_name); + goto out_err; + } + res = sysfs_create_file(&tgt->tgt_kobj, &scst_tgt_addr_method.attr); if (res != 0) {