[PATCH] nbd: new nbd sysfs entries

From: Paul Clements
Date: Wed Aug 03 2011 - 19:32:53 EST


Description: Add 3 new sysfs entries to nbd.

# cat /sys/block/nbd6/nbd/peer
127.0.0.1
# cat /sys/block/nbd6/nbd/flags
0
# cat /sys/block/nbd6/nbd/xmit_timeout
6

This patch depends on patch nbd-sysfs-framework.diff
Description: Add 3 new sysfs entries to nbd.

# cat /sys/block/nbd6/nbd/peer
127.0.0.1
# cat /sys/block/nbd6/nbd/flags
0
# cat /sys/block/nbd6/nbd/xmit_timeout
6

This patch depends on patch nbd-sysfs-framework.diff

From: Paul Clements <paul.clements@xxxxxxxxxxxx>
Signed-off-by: Paul Clements <paul.clements@xxxxxxxxxxxx>
---

nbd.c | 79 ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++--
1 file changed, 77 insertions(+), 2 deletions(-)

diff --git a/drivers/block/nbd.c b/drivers/block/nbd.c
index 23f3619..44cd41d 100644
--- a/drivers/block/nbd.c
+++ b/drivers/block/nbd.c
@@ -136,7 +136,7 @@ static void sock_shutdown(struct nbd_device *lo, int lock)
mutex_unlock(&lo->tx_lock);
}

-static void nbd_xmit_timeout(unsigned long arg)
+static void xmit_timeout(unsigned long arg)
{
struct task_struct *task = (struct task_struct *)arg;

@@ -183,7 +183,7 @@ static int sock_xmit(struct nbd_device *lo, int send, void *buf, int size,

if (lo->xmit_timeout) {
init_timer(&ti);
- ti.function = nbd_xmit_timeout;
+ ti.function = xmit_timeout;
ti.data = (unsigned long)current;
ti.expires = jiffies + lo->xmit_timeout;
add_timer(&ti);
@@ -719,16 +719,91 @@ struct nbd_sysfs_entry {
ssize_t (*store)(struct nbd_device *, const char *, size_t);
};

+static ssize_t
+peer_show(struct nbd_device *lo, char *page)
+{
+ struct socket *sock = lo->sock;
+ struct sockaddr_storage addr;
+ struct sockaddr *sa = (struct sockaddr *)&addr;
+ struct sockaddr_in *sin = (struct sockaddr_in *)&addr;
+ struct sockaddr_in6 *sin6 = (struct sockaddr_in6 *)&addr;
+ int err, len;
+
+ if (!sock)
+ goto err;
+
+ err = kernel_getpeername(sock, sa, &len);
+ if (err < 0)
+ goto err;
+
+ if (sa->sa_family == AF_INET6)
+ return sprintf(page, "%pI6\n", &sin6->sin6_addr);
+ else if (sa->sa_family == AF_INET)
+ return sprintf(page, "%pI4\n", &sin->sin_addr);
+err:
+ return 0;
+}
+
static ssize_t pid_show(struct nbd_device *lo, char *page)
{
return sprintf(page, "%ld\n", (long)lo->pid);
}

+static ssize_t
+flags_show(struct nbd_device *lo, char *page)
+{
+ return sprintf(page, "%x\n", lo->flags);
+}
+
+static ssize_t
+flags_store(struct nbd_device *lo, const char *buf, size_t len)
+{
+ long n;
+
+ if (strict_strtol(buf, 16, &n))
+ return -EINVAL;
+
+ lo->flags = n;
+
+ return len;
+}
+
+static ssize_t
+xmit_timeout_show(struct nbd_device *lo, char *page)
+{
+ return sprintf(page, "%d\n", lo->xmit_timeout / HZ);
+}
+
+static ssize_t
+xmit_timeout_store(struct nbd_device *lo, const char *buf, size_t len)
+{
+ long n;
+
+ if (strict_strtol(buf, 10, &n))
+ return -EINVAL;
+
+ lo->xmit_timeout = n * HZ;
+
+ return len;
+}
+
+static struct nbd_sysfs_entry nbd_peer =
+__ATTR(peer, S_IRUGO, peer_show, NULL);
+
static struct nbd_sysfs_entry nbd_pid =
__ATTR(pid, S_IRUGO, pid_show, NULL);

+static struct nbd_sysfs_entry nbd_flags =
+__ATTR(flags, S_IRUGO|S_IWUSR, flags_show, flags_store);
+
+static struct nbd_sysfs_entry nbd_xmit_timeout =
+__ATTR(xmit_timeout, S_IRUGO|S_IWUSR, xmit_timeout_show, xmit_timeout_store);
+
static struct attribute *nbd_default_attrs[] = {
+ &nbd_peer.attr,
&nbd_pid.attr,
+ &nbd_flags.attr,
+ &nbd_xmit_timeout.attr,
NULL,
};