Commit ea9ed9cf authored by Christoph Hellwig's avatar Christoph Hellwig Committed by Nicholas Bellinger

netconsole: use per-attribute show and store methods

Note that the old code actually used the store_attributes method to do
locking, this is moved into the individual methods.
Signed-off-by: default avatarChristoph Hellwig <hch@lst.de>
Signed-off-by: default avatarNicholas Bellinger <nab@linux-iscsi.org>
parent 2eafd729
...@@ -244,15 +244,6 @@ static void free_param_target(struct netconsole_target *nt) ...@@ -244,15 +244,6 @@ static void free_param_target(struct netconsole_target *nt)
* <target>/... * <target>/...
*/ */
struct netconsole_target_attr {
struct configfs_attribute attr;
ssize_t (*show)(struct netconsole_target *nt,
char *buf);
ssize_t (*store)(struct netconsole_target *nt,
const char *buf,
size_t count);
};
static struct netconsole_target *to_target(struct config_item *item) static struct netconsole_target *to_target(struct config_item *item)
{ {
return item ? return item ?
...@@ -264,58 +255,62 @@ static struct netconsole_target *to_target(struct config_item *item) ...@@ -264,58 +255,62 @@ static struct netconsole_target *to_target(struct config_item *item)
* Attribute operations for netconsole_target. * Attribute operations for netconsole_target.
*/ */
static ssize_t show_enabled(struct netconsole_target *nt, char *buf) static ssize_t enabled_show(struct config_item *item, char *buf)
{ {
return snprintf(buf, PAGE_SIZE, "%d\n", nt->enabled); return snprintf(buf, PAGE_SIZE, "%d\n", to_target(item)->enabled);
} }
static ssize_t show_extended(struct netconsole_target *nt, char *buf) static ssize_t extended_show(struct config_item *item, char *buf)
{ {
return snprintf(buf, PAGE_SIZE, "%d\n", nt->extended); return snprintf(buf, PAGE_SIZE, "%d\n", to_target(item)->extended);
} }
static ssize_t show_dev_name(struct netconsole_target *nt, char *buf) static ssize_t dev_name_show(struct config_item *item, char *buf)
{ {
return snprintf(buf, PAGE_SIZE, "%s\n", nt->np.dev_name); return snprintf(buf, PAGE_SIZE, "%s\n", to_target(item)->np.dev_name);
} }
static ssize_t show_local_port(struct netconsole_target *nt, char *buf) static ssize_t local_port_show(struct config_item *item, char *buf)
{ {
return snprintf(buf, PAGE_SIZE, "%d\n", nt->np.local_port); return snprintf(buf, PAGE_SIZE, "%d\n", to_target(item)->np.local_port);
} }
static ssize_t show_remote_port(struct netconsole_target *nt, char *buf) static ssize_t remote_port_show(struct config_item *item, char *buf)
{ {
return snprintf(buf, PAGE_SIZE, "%d\n", nt->np.remote_port); return snprintf(buf, PAGE_SIZE, "%d\n", to_target(item)->np.remote_port);
} }
static ssize_t show_local_ip(struct netconsole_target *nt, char *buf) static ssize_t local_ip_show(struct config_item *item, char *buf)
{ {
struct netconsole_target *nt = to_target(item);
if (nt->np.ipv6) if (nt->np.ipv6)
return snprintf(buf, PAGE_SIZE, "%pI6c\n", &nt->np.local_ip.in6); return snprintf(buf, PAGE_SIZE, "%pI6c\n", &nt->np.local_ip.in6);
else else
return snprintf(buf, PAGE_SIZE, "%pI4\n", &nt->np.local_ip); return snprintf(buf, PAGE_SIZE, "%pI4\n", &nt->np.local_ip);
} }
static ssize_t show_remote_ip(struct netconsole_target *nt, char *buf) static ssize_t remote_ip_show(struct config_item *item, char *buf)
{ {
struct netconsole_target *nt = to_target(item);
if (nt->np.ipv6) if (nt->np.ipv6)
return snprintf(buf, PAGE_SIZE, "%pI6c\n", &nt->np.remote_ip.in6); return snprintf(buf, PAGE_SIZE, "%pI6c\n", &nt->np.remote_ip.in6);
else else
return snprintf(buf, PAGE_SIZE, "%pI4\n", &nt->np.remote_ip); return snprintf(buf, PAGE_SIZE, "%pI4\n", &nt->np.remote_ip);
} }
static ssize_t show_local_mac(struct netconsole_target *nt, char *buf) static ssize_t local_mac_show(struct config_item *item, char *buf)
{ {
struct net_device *dev = nt->np.dev; struct net_device *dev = to_target(item)->np.dev;
static const u8 bcast[ETH_ALEN] = { 0xff, 0xff, 0xff, 0xff, 0xff, 0xff }; static const u8 bcast[ETH_ALEN] = { 0xff, 0xff, 0xff, 0xff, 0xff, 0xff };
return snprintf(buf, PAGE_SIZE, "%pM\n", dev ? dev->dev_addr : bcast); return snprintf(buf, PAGE_SIZE, "%pM\n", dev ? dev->dev_addr : bcast);
} }
static ssize_t show_remote_mac(struct netconsole_target *nt, char *buf) static ssize_t remote_mac_show(struct config_item *item, char *buf)
{ {
return snprintf(buf, PAGE_SIZE, "%pM\n", nt->np.remote_mac); return snprintf(buf, PAGE_SIZE, "%pM\n", to_target(item)->np.remote_mac);
} }
/* /*
...@@ -325,23 +320,26 @@ static ssize_t show_remote_mac(struct netconsole_target *nt, char *buf) ...@@ -325,23 +320,26 @@ static ssize_t show_remote_mac(struct netconsole_target *nt, char *buf)
* would enable him to dynamically add new netpoll targets for new * would enable him to dynamically add new netpoll targets for new
* network interfaces as and when they come up). * network interfaces as and when they come up).
*/ */
static ssize_t store_enabled(struct netconsole_target *nt, static ssize_t enabled_store(struct config_item *item,
const char *buf, const char *buf, size_t count)
size_t count)
{ {
struct netconsole_target *nt = to_target(item);
unsigned long flags; unsigned long flags;
int enabled; int enabled;
int err; int err;
mutex_lock(&dynamic_netconsole_mutex);
err = kstrtoint(buf, 10, &enabled); err = kstrtoint(buf, 10, &enabled);
if (err < 0) if (err < 0)
return err; goto out_unlock;
err = -EINVAL;
if (enabled < 0 || enabled > 1) if (enabled < 0 || enabled > 1)
return -EINVAL; goto out_unlock;
if ((bool)enabled == nt->enabled) { if ((bool)enabled == nt->enabled) {
pr_info("network logging has already %s\n", pr_info("network logging has already %s\n",
nt->enabled ? "started" : "stopped"); nt->enabled ? "started" : "stopped");
return -EINVAL; goto out_unlock;
} }
if (enabled) { /* true */ if (enabled) { /* true */
...@@ -358,7 +356,7 @@ static ssize_t store_enabled(struct netconsole_target *nt, ...@@ -358,7 +356,7 @@ static ssize_t store_enabled(struct netconsole_target *nt,
err = netpoll_setup(&nt->np); err = netpoll_setup(&nt->np);
if (err) if (err)
return err; goto out_unlock;
pr_info("netconsole: network logging started\n"); pr_info("netconsole: network logging started\n");
} else { /* false */ } else { /* false */
...@@ -374,42 +372,56 @@ static ssize_t store_enabled(struct netconsole_target *nt, ...@@ -374,42 +372,56 @@ static ssize_t store_enabled(struct netconsole_target *nt,
nt->enabled = enabled; nt->enabled = enabled;
mutex_unlock(&dynamic_netconsole_mutex);
return strnlen(buf, count); return strnlen(buf, count);
out_unlock:
mutex_unlock(&dynamic_netconsole_mutex);
return err;
} }
static ssize_t store_extended(struct netconsole_target *nt, static ssize_t extended_store(struct config_item *item, const char *buf,
const char *buf,
size_t count) size_t count)
{ {
struct netconsole_target *nt = to_target(item);
int extended; int extended;
int err; int err;
mutex_lock(&dynamic_netconsole_mutex);
if (nt->enabled) { if (nt->enabled) {
pr_err("target (%s) is enabled, disable to update parameters\n", pr_err("target (%s) is enabled, disable to update parameters\n",
config_item_name(&nt->item)); config_item_name(&nt->item));
return -EINVAL; err = -EINVAL;
goto out_unlock;
} }
err = kstrtoint(buf, 10, &extended); err = kstrtoint(buf, 10, &extended);
if (err < 0) if (err < 0)
return err; goto out_unlock;
if (extended < 0 || extended > 1) if (extended < 0 || extended > 1) {
return -EINVAL; err = -EINVAL;
goto out_unlock;
}
nt->extended = extended; nt->extended = extended;
mutex_unlock(&dynamic_netconsole_mutex);
return strnlen(buf, count); return strnlen(buf, count);
out_unlock:
mutex_unlock(&dynamic_netconsole_mutex);
return err;
} }
static ssize_t store_dev_name(struct netconsole_target *nt, static ssize_t dev_name_store(struct config_item *item, const char *buf,
const char *buf,
size_t count) size_t count)
{ {
struct netconsole_target *nt = to_target(item);
size_t len; size_t len;
mutex_lock(&dynamic_netconsole_mutex);
if (nt->enabled) { if (nt->enabled) {
pr_err("target (%s) is enabled, disable to update parameters\n", pr_err("target (%s) is enabled, disable to update parameters\n",
config_item_name(&nt->item)); config_item_name(&nt->item));
mutex_unlock(&dynamic_netconsole_mutex);
return -EINVAL; return -EINVAL;
} }
...@@ -420,53 +432,66 @@ static ssize_t store_dev_name(struct netconsole_target *nt, ...@@ -420,53 +432,66 @@ static ssize_t store_dev_name(struct netconsole_target *nt,
if (nt->np.dev_name[len - 1] == '\n') if (nt->np.dev_name[len - 1] == '\n')
nt->np.dev_name[len - 1] = '\0'; nt->np.dev_name[len - 1] = '\0';
mutex_unlock(&dynamic_netconsole_mutex);
return strnlen(buf, count); return strnlen(buf, count);
} }
static ssize_t store_local_port(struct netconsole_target *nt, static ssize_t local_port_store(struct config_item *item, const char *buf,
const char *buf,
size_t count) size_t count)
{ {
int rv; struct netconsole_target *nt = to_target(item);
int rv = -EINVAL;
mutex_lock(&dynamic_netconsole_mutex);
if (nt->enabled) { if (nt->enabled) {
pr_err("target (%s) is enabled, disable to update parameters\n", pr_err("target (%s) is enabled, disable to update parameters\n",
config_item_name(&nt->item)); config_item_name(&nt->item));
return -EINVAL; goto out_unlock;
} }
rv = kstrtou16(buf, 10, &nt->np.local_port); rv = kstrtou16(buf, 10, &nt->np.local_port);
if (rv < 0) if (rv < 0)
return rv; goto out_unlock;
mutex_unlock(&dynamic_netconsole_mutex);
return strnlen(buf, count); return strnlen(buf, count);
out_unlock:
mutex_unlock(&dynamic_netconsole_mutex);
return rv;
} }
static ssize_t store_remote_port(struct netconsole_target *nt, static ssize_t remote_port_store(struct config_item *item,
const char *buf, const char *buf, size_t count)
size_t count)
{ {
int rv; struct netconsole_target *nt = to_target(item);
int rv = -EINVAL;
mutex_lock(&dynamic_netconsole_mutex);
if (nt->enabled) { if (nt->enabled) {
pr_err("target (%s) is enabled, disable to update parameters\n", pr_err("target (%s) is enabled, disable to update parameters\n",
config_item_name(&nt->item)); config_item_name(&nt->item));
return -EINVAL; goto out_unlock;
} }
rv = kstrtou16(buf, 10, &nt->np.remote_port); rv = kstrtou16(buf, 10, &nt->np.remote_port);
if (rv < 0) if (rv < 0)
return rv; goto out_unlock;
mutex_unlock(&dynamic_netconsole_mutex);
return strnlen(buf, count); return strnlen(buf, count);
out_unlock:
mutex_unlock(&dynamic_netconsole_mutex);
return rv;
} }
static ssize_t store_local_ip(struct netconsole_target *nt, static ssize_t local_ip_store(struct config_item *item, const char *buf,
const char *buf,
size_t count) size_t count)
{ {
struct netconsole_target *nt = to_target(item);
mutex_lock(&dynamic_netconsole_mutex);
if (nt->enabled) { if (nt->enabled) {
pr_err("target (%s) is enabled, disable to update parameters\n", pr_err("target (%s) is enabled, disable to update parameters\n",
config_item_name(&nt->item)); config_item_name(&nt->item));
return -EINVAL; goto out_unlock;
} }
if (strnchr(buf, count, ':')) { if (strnchr(buf, count, ':')) {
...@@ -474,29 +499,35 @@ static ssize_t store_local_ip(struct netconsole_target *nt, ...@@ -474,29 +499,35 @@ static ssize_t store_local_ip(struct netconsole_target *nt,
if (in6_pton(buf, count, nt->np.local_ip.in6.s6_addr, -1, &end) > 0) { if (in6_pton(buf, count, nt->np.local_ip.in6.s6_addr, -1, &end) > 0) {
if (*end && *end != '\n') { if (*end && *end != '\n') {
pr_err("invalid IPv6 address at: <%c>\n", *end); pr_err("invalid IPv6 address at: <%c>\n", *end);
return -EINVAL; goto out_unlock;
} }
nt->np.ipv6 = true; nt->np.ipv6 = true;
} else } else
return -EINVAL; goto out_unlock;
} else { } else {
if (!nt->np.ipv6) { if (!nt->np.ipv6) {
nt->np.local_ip.ip = in_aton(buf); nt->np.local_ip.ip = in_aton(buf);
} else } else
return -EINVAL; goto out_unlock;
} }
mutex_unlock(&dynamic_netconsole_mutex);
return strnlen(buf, count); return strnlen(buf, count);
out_unlock:
mutex_unlock(&dynamic_netconsole_mutex);
return -EINVAL;
} }
static ssize_t store_remote_ip(struct netconsole_target *nt, static ssize_t remote_ip_store(struct config_item *item, const char *buf,
const char *buf,
size_t count) size_t count)
{ {
struct netconsole_target *nt = to_target(item);
mutex_lock(&dynamic_netconsole_mutex);
if (nt->enabled) { if (nt->enabled) {
pr_err("target (%s) is enabled, disable to update parameters\n", pr_err("target (%s) is enabled, disable to update parameters\n",
config_item_name(&nt->item)); config_item_name(&nt->item));
return -EINVAL; goto out_unlock;
} }
if (strnchr(buf, count, ':')) { if (strnchr(buf, count, ':')) {
...@@ -504,74 +535,71 @@ static ssize_t store_remote_ip(struct netconsole_target *nt, ...@@ -504,74 +535,71 @@ static ssize_t store_remote_ip(struct netconsole_target *nt,
if (in6_pton(buf, count, nt->np.remote_ip.in6.s6_addr, -1, &end) > 0) { if (in6_pton(buf, count, nt->np.remote_ip.in6.s6_addr, -1, &end) > 0) {
if (*end && *end != '\n') { if (*end && *end != '\n') {
pr_err("invalid IPv6 address at: <%c>\n", *end); pr_err("invalid IPv6 address at: <%c>\n", *end);
return -EINVAL; goto out_unlock;
} }
nt->np.ipv6 = true; nt->np.ipv6 = true;
} else } else
return -EINVAL; goto out_unlock;
} else { } else {
if (!nt->np.ipv6) { if (!nt->np.ipv6) {
nt->np.remote_ip.ip = in_aton(buf); nt->np.remote_ip.ip = in_aton(buf);
} else } else
return -EINVAL; goto out_unlock;
} }
mutex_unlock(&dynamic_netconsole_mutex);
return strnlen(buf, count); return strnlen(buf, count);
out_unlock:
mutex_unlock(&dynamic_netconsole_mutex);
return -EINVAL;
} }
static ssize_t store_remote_mac(struct netconsole_target *nt, static ssize_t remote_mac_store(struct config_item *item, const char *buf,
const char *buf,
size_t count) size_t count)
{ {
struct netconsole_target *nt = to_target(item);
u8 remote_mac[ETH_ALEN]; u8 remote_mac[ETH_ALEN];
mutex_lock(&dynamic_netconsole_mutex);
if (nt->enabled) { if (nt->enabled) {
pr_err("target (%s) is enabled, disable to update parameters\n", pr_err("target (%s) is enabled, disable to update parameters\n",
config_item_name(&nt->item)); config_item_name(&nt->item));
return -EINVAL; goto out_unlock;
} }
if (!mac_pton(buf, remote_mac)) if (!mac_pton(buf, remote_mac))
return -EINVAL; goto out_unlock;
if (buf[3 * ETH_ALEN - 1] && buf[3 * ETH_ALEN - 1] != '\n') if (buf[3 * ETH_ALEN - 1] && buf[3 * ETH_ALEN - 1] != '\n')
return -EINVAL; goto out_unlock;
memcpy(nt->np.remote_mac, remote_mac, ETH_ALEN); memcpy(nt->np.remote_mac, remote_mac, ETH_ALEN);
mutex_unlock(&dynamic_netconsole_mutex);
return strnlen(buf, count); return strnlen(buf, count);
out_unlock:
mutex_unlock(&dynamic_netconsole_mutex);
return -EINVAL;
} }
/* CONFIGFS_ATTR(, enabled);
* Attribute definitions for netconsole_target. CONFIGFS_ATTR(, extended);
*/ CONFIGFS_ATTR(, dev_name);
CONFIGFS_ATTR(, local_port);
#define NETCONSOLE_TARGET_ATTR_RO(_name) \ CONFIGFS_ATTR(, remote_port);
static struct netconsole_target_attr netconsole_target_##_name = \ CONFIGFS_ATTR(, local_ip);
__CONFIGFS_ATTR(_name, S_IRUGO, show_##_name, NULL) CONFIGFS_ATTR(, remote_ip);
CONFIGFS_ATTR_RO(, local_mac);
#define NETCONSOLE_TARGET_ATTR_RW(_name) \ CONFIGFS_ATTR(, remote_mac);
static struct netconsole_target_attr netconsole_target_##_name = \
__CONFIGFS_ATTR(_name, S_IRUGO | S_IWUSR, show_##_name, store_##_name)
NETCONSOLE_TARGET_ATTR_RW(enabled);
NETCONSOLE_TARGET_ATTR_RW(extended);
NETCONSOLE_TARGET_ATTR_RW(dev_name);
NETCONSOLE_TARGET_ATTR_RW(local_port);
NETCONSOLE_TARGET_ATTR_RW(remote_port);
NETCONSOLE_TARGET_ATTR_RW(local_ip);
NETCONSOLE_TARGET_ATTR_RW(remote_ip);
NETCONSOLE_TARGET_ATTR_RO(local_mac);
NETCONSOLE_TARGET_ATTR_RW(remote_mac);
static struct configfs_attribute *netconsole_target_attrs[] = { static struct configfs_attribute *netconsole_target_attrs[] = {
&netconsole_target_enabled.attr, &attr_enabled,
&netconsole_target_extended.attr, &attr_extended,
&netconsole_target_dev_name.attr, &attr_dev_name,
&netconsole_target_local_port.attr, &attr_local_port,
&netconsole_target_remote_port.attr, &attr_remote_port,
&netconsole_target_local_ip.attr, &attr_local_ip,
&netconsole_target_remote_ip.attr, &attr_remote_ip,
&netconsole_target_local_mac.attr, &attr_local_mac,
&netconsole_target_remote_mac.attr, &attr_remote_mac,
NULL, NULL,
}; };
...@@ -584,43 +612,8 @@ static void netconsole_target_release(struct config_item *item) ...@@ -584,43 +612,8 @@ static void netconsole_target_release(struct config_item *item)
kfree(to_target(item)); kfree(to_target(item));
} }
static ssize_t netconsole_target_attr_show(struct config_item *item,
struct configfs_attribute *attr,
char *buf)
{
ssize_t ret = -EINVAL;
struct netconsole_target *nt = to_target(item);
struct netconsole_target_attr *na =
container_of(attr, struct netconsole_target_attr, attr);
if (na->show)
ret = na->show(nt, buf);
return ret;
}
static ssize_t netconsole_target_attr_store(struct config_item *item,
struct configfs_attribute *attr,
const char *buf,
size_t count)
{
ssize_t ret = -EINVAL;
struct netconsole_target *nt = to_target(item);
struct netconsole_target_attr *na =
container_of(attr, struct netconsole_target_attr, attr);
mutex_lock(&dynamic_netconsole_mutex);
if (na->store)
ret = na->store(nt, buf, count);
mutex_unlock(&dynamic_netconsole_mutex);
return ret;
}
static struct configfs_item_operations netconsole_target_item_ops = { static struct configfs_item_operations netconsole_target_item_ops = {
.release = netconsole_target_release, .release = netconsole_target_release,
.show_attribute = netconsole_target_attr_show,
.store_attribute = netconsole_target_attr_store,
}; };
static struct config_item_type netconsole_target_type = { static struct config_item_type netconsole_target_type = {
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment