From: Chuck Lever <chuck.lever@oracle.com>
To: linux-nfs@vger.kernel.org
Subject: [PATCH 06/11] lockd: Add /sys/fs/lockd/nsm_handle/*
Date: Thu, 01 Apr 2010 15:03:15 -0400 [thread overview]
Message-ID: <20100401190315.6395.46544.stgit@localhost.localdomain> (raw)
In-Reply-To: <20100401183724.6395.60353.stgit-bi+AKbBUZKY6gyzm1THtWbp2dZbC/Bob@public.gmane.org>
Populate the new nsm_handle/ directory as nsm_handles come and go.
The entries are named by their priv cookie, which is always guaranteed
to be unique.
Like the nlm_hosts cache, the kref is fixed at one when the handle
is created, and goes to zero only when the handle is ready to be
destroyed.
Signed-off-by: Chuck Lever <chuck.lever@oracle.com>
---
fs/lockd/host.c | 7 ++
fs/lockd/mon.c | 166 +++++++++++++++++++++++++++++++++++++++++--
include/linux/lockd/lockd.h | 1
3 files changed, 166 insertions(+), 8 deletions(-)
diff --git a/fs/lockd/host.c b/fs/lockd/host.c
index b15579b..0a62d8d 100644
--- a/fs/lockd/host.c
+++ b/fs/lockd/host.c
@@ -279,6 +279,7 @@ int nlm_add_kobject(struct nlm_host *host)
{
struct kobject *parent;
char *proto_name;
+ int ret;
parent = nlm_hosts_server_kobj;
if (host->h_server)
@@ -295,9 +296,13 @@ int nlm_add_kobject(struct nlm_host *host)
return -EPROTONOSUPPORT;
}
- return kobject_init_and_add(&host->h_kobj, &nlm_host_ktype,
+ ret = kobject_init_and_add(&host->h_kobj, &nlm_host_ktype,
parent, "%s-%s-%u", host->h_addrbuf,
proto_name, host->h_version);
+ if (likely(ret == 0))
+ ret = sysfs_create_link(&host->h_kobj,
+ &host->h_nsmhandle->sm_kobj, "nsm_handle");
+ return ret;
}
/*
diff --git a/fs/lockd/mon.c b/fs/lockd/mon.c
index 77e4927..dc185e1 100644
--- a/fs/lockd/mon.c
+++ b/fs/lockd/mon.c
@@ -58,6 +58,129 @@ int __read_mostly nsm_use_hostnames;
static struct kobject *nsm_handles_kobj;
+struct nsm_handle_attr {
+ struct attribute attr;
+ ssize_t (*show)(struct nsm_handle *nsm, char *buf);
+ ssize_t (*store)(struct nsm_handle *nsm, const char *buf, size_t count);
+};
+
+static ssize_t nsm_kobj_show(struct kobject *kobj, struct attribute *attr,
+ char *buf)
+{
+ struct nsm_handle_attr *nattr = container_of(attr,
+ struct nsm_handle_attr, attr);
+ struct nsm_handle *nsm = container_of(kobj, struct nsm_handle, sm_kobj);
+ if (nattr->show)
+ return nattr->show(nsm, buf);
+ return 0;
+}
+
+static ssize_t nsm_kobj_store(struct kobject *kobj, struct attribute *attr,
+ const char *buf, size_t count)
+{
+ struct nsm_handle_attr *nattr = container_of(attr,
+ struct nsm_handle_attr, attr);
+ struct nsm_handle *nsm = container_of(kobj, struct nsm_handle, sm_kobj);
+ if (nattr->store)
+ return nattr->store(nsm, buf, count);
+ return 0;
+}
+
+static void nsm_kobj_release(struct kobject *kobj)
+{
+ struct nsm_handle *nsm = container_of(kobj,
+ struct nsm_handle, sm_kobj);
+
+ dprintk("lockd: destroyed nsm_handle for %s (%s)\n",
+ nsm->sm_name, nsm->sm_addrbuf);
+ kfree(nsm);
+}
+
+static struct sysfs_ops nsm_kobj_sysfs_ops = {
+ .show = nsm_kobj_show,
+ .store = nsm_kobj_store,
+};
+
+static ssize_t mon_name_show(struct nsm_handle *nsm, char *buf)
+{
+ char *mon_name = nsm->sm_mon_name;
+
+ /* When this host is unmonitored, sm_mon_name is NULL,
+ * so make up something reasonable. */
+ if (mon_name == NULL)
+ mon_name = nsm_use_hostnames ? nsm->sm_name : nsm->sm_addrbuf;
+
+ return snprintf(buf, PAGE_SIZE, "%s\n", mon_name);
+}
+
+static ssize_t my_name_show(struct nsm_handle *nsm, char *buf)
+{
+ return snprintf(buf, PAGE_SIZE, "%s\n", utsname()->nodename);
+}
+
+static ssize_t address_show(struct nsm_handle *nsm, char *buf)
+{
+ return snprintf(buf, PAGE_SIZE, "%s\n", nsm->sm_addrbuf);
+}
+
+static ssize_t rpcparms_show(struct nsm_handle *nsm, char *buf)
+{
+ return snprintf(buf, PAGE_SIZE, "%u %u %u\n",
+ NLM_PROGRAM, 3, NLMPROC_NSM_NOTIFY);
+}
+
+static ssize_t status_show(struct nsm_handle *nsm, char *buf)
+{
+ return snprintf(buf, PAGE_SIZE, "%smonitored\n",
+ nsm->sm_monitored ? "" : "un" );
+}
+
+static ssize_t sticky_show(struct nsm_handle *nsm, char *buf)
+{
+ return snprintf(buf, PAGE_SIZE, "%s\n",
+ nsm->sm_sticky ? "yes" : "no" );
+}
+
+static ssize_t refcount_show(struct nsm_handle *nsm, char *buf)
+{
+ return snprintf(buf, PAGE_SIZE, "%d\n",
+ atomic_read(&nsm->sm_count));
+}
+
+#ifdef __ATTR_RO
+#undef __ATTR_RO
+#endif
+
+#define __ATTR_RO(_name) { \
+ .attr = { .name = __stringify(_name), .mode = S_IRUSR, }, \
+ .show = _name##_show, \
+}
+
+static struct nsm_handle_attr mon_name_attr = __ATTR_RO(mon_name);
+static struct nsm_handle_attr my_name_attr = __ATTR_RO(my_name);
+static struct nsm_handle_attr address_attr = __ATTR_RO(address);
+static struct nsm_handle_attr rpcparms_attr = __ATTR_RO(rpcparms);
+static struct nsm_handle_attr status_attr = __ATTR_RO(status);
+static struct nsm_handle_attr sticky_attr = __ATTR_RO(sticky);
+static struct nsm_handle_attr refcount_attr = __ATTR_RO(refcount);
+
+static struct attribute *nsm_kobj_attrs[] = {
+ &mon_name_attr.attr,
+ &my_name_attr.attr,
+ &address_attr.attr,
+ &rpcparms_attr.attr,
+ &status_attr.attr,
+ &sticky_attr.attr,
+ &refcount_attr.attr,
+ NULL,
+};
+
+static struct kobj_type nsm_handle_ktype = {
+ .release = nsm_kobj_release,
+ .sysfs_ops = &nsm_kobj_sysfs_ops,
+ .default_attrs = nsm_kobj_attrs,
+};
+
static inline struct sockaddr *nsm_addr(const struct nsm_handle *nsm)
{
return (struct sockaddr *)&nsm->sm_addr;
@@ -189,8 +312,10 @@ void nsm_unmonitor(const struct nlm_host *host)
if (status < 0)
printk(KERN_NOTICE "lockd: cannot unmonitor %s\n",
nsm->sm_name);
- else
+ else {
nsm->sm_monitored = 0;
+ nsm->sm_mon_name = NULL;
+ }
}
}
@@ -261,7 +386,10 @@ static struct nsm_handle *nsm_create_handle(const struct sockaddr *sap,
const char *hostname,
const size_t hostname_len)
{
+ char *p, buf[SM_PRIV_SIZE * 2 + 1];
struct nsm_handle *new;
+ size_t remaining;
+ int i, len, ret;
new = kzalloc(sizeof(*new) + hostname_len + 1, GFP_KERNEL);
if (unlikely(new == NULL))
@@ -280,7 +408,33 @@ static struct nsm_handle *nsm_create_handle(const struct sockaddr *sap,
memcpy(new->sm_name, hostname, hostname_len);
new->sm_name[hostname_len] = '\0';
+ p = buf;
+ remaining = sizeof(buf);
+ for (i = 0; i < SM_PRIV_SIZE; i++) {
+ len = snprintf(p, remaining, "%02x",
+ (u32)(new->sm_priv.data[i]) & 0xff);
+ if (len != 2)
+ goto out;
+ p += len;
+ remaining -= (size_t)len;
+ }
+ if (remaining < 1)
+ goto out;
+ strcat(buf, "\0");
+
+ /* Named after "priv" cookie, which is guaranteed unique */
+ ret = kobject_init_and_add(&new->sm_kobj, &nsm_handle_ktype,
+ nsm_handles_kobj, "%s", buf);
+ if (ret != 0)
+ goto out;
+
return new;
+
+out:
+ kobject_put(&new->sm_kobj);
+ dprintk("lockd: %s failed; could not add kobject\n",
+ __func__);
+ return NULL;
}
/**
@@ -323,7 +477,7 @@ retry:
if (cached != NULL) {
atomic_inc(&cached->sm_count);
spin_unlock(&nsm_lock);
- kfree(new);
+ kobject_put(&new->sm_kobj);
dprintk("lockd: found nsm_handle for %s (%s), "
"cnt %d\n", cached->sm_name,
cached->sm_addrbuf,
@@ -392,11 +546,9 @@ struct nsm_handle *nsm_reboot_lookup(const struct nlm_reboot *info)
void nsm_release(struct nsm_handle *nsm)
{
if (atomic_dec_and_lock(&nsm->sm_count, &nsm_lock)) {
- list_del(&nsm->sm_link);
+ list_del(&nsm->sm_link); /* kobject_del ? */
spin_unlock(&nsm_lock);
- dprintk("lockd: destroyed nsm_handle for %s (%s)\n",
- nsm->sm_name, nsm->sm_addrbuf);
- kfree(nsm);
+ kobject_put(&nsm->sm_kobj);
}
}
@@ -406,7 +558,7 @@ void nsm_release(struct nsm_handle *nsm)
*/
int nsm_init(void)
{
- nsm_handles_kobj = kobject_create_and_add("monitor", nlm_kobj);
+ nsm_handles_kobj = kobject_create_and_add("nsm_handles", nlm_kobj);
if (nsm_handles_kobj == NULL)
return -ENOMEM;
return 0;
diff --git a/include/linux/lockd/lockd.h b/include/linux/lockd/lockd.h
index 8622a28..3d1c594 100644
--- a/include/linux/lockd/lockd.h
+++ b/include/linux/lockd/lockd.h
@@ -78,6 +78,7 @@ struct nlm_host {
#define NSM_ADDRBUF ((8 * 4 + 7) + (1 + 10) + 1)
struct nsm_handle {
+ struct kobject sm_kobj;
struct list_head sm_link;
atomic_t sm_count;
char *sm_mon_name;
next prev parent reply other threads:[~2010-04-01 19:03 UTC|newest]
Thread overview: 15+ messages / expand[flat|nested] mbox.gz Atom feed top
2010-04-01 19:02 [PATCH 00/11] [RFC] possible NFSv2/v3 lock recovery enhancements Chuck Lever
[not found] ` <20100401183724.6395.60353.stgit-bi+AKbBUZKY6gyzm1THtWbp2dZbC/Bob@public.gmane.org>
2010-04-01 19:02 ` [PATCH 01/11] lockd: Add /sys/fs/lockd Chuck Lever
2010-04-01 19:02 ` [PATCH 02/11] lockd: Add /sys/fs/lockd/hosts/ Chuck Lever
2010-04-01 19:02 ` [PATCH 03/11] lockd: Add /sys/fs/lockd/hosts/* Chuck Lever
2010-04-01 19:02 ` [PATCH 04/11] lockd: Add attributes to /sys/fs/lockd/hosts/*/ Chuck Lever
2010-04-01 19:03 ` [PATCH 05/11] lockd: Add /sys/fs/lockd/mon Chuck Lever
2010-04-01 19:03 ` Chuck Lever [this message]
2010-04-01 19:03 ` [PATCH 07/11] lockd: Refactor nlm_host_rebooted() Chuck Lever
2010-04-01 19:03 ` [PATCH 08/11] lockd: Add "reboot" attribute to nsm_handles Chuck Lever
2010-04-01 19:03 ` [PATCH 09/11] lockd: Keep my_name in nsm_handle Chuck Lever
2010-04-01 19:03 ` [PATCH 10/11] lockd: use sm_my_name for nsm_handle lookups Chuck Lever
2010-04-01 19:04 ` [PATCH 11/11] lockd: Allow mount option to specify caller_name Chuck Lever
[not found] ` <20100401190400.6395.52787.stgit-bi+AKbBUZKY6gyzm1THtWbp2dZbC/Bob@public.gmane.org>
2010-04-01 19:45 ` Trond Myklebust
[not found] ` <1270151136.13329.8.camel-bi+AKbBUZKY6gyzm1THtWbp2dZbC/Bob@public.gmane.org>
2010-04-01 20:01 ` Trond Myklebust
2010-04-01 21:08 ` Chuck Lever
Reply instructions:
You may reply publicly to this message via plain-text email
using any one of the following methods:
* Save the following mbox file, import it into your mail client,
and reply-to-all from there: mbox
Avoid top-posting and favor interleaved quoting:
https://en.wikipedia.org/wiki/Posting_style#Interleaved_style
* Reply using the --to, --cc, and --in-reply-to
switches of git-send-email(1):
git send-email \
--in-reply-to=20100401190315.6395.46544.stgit@localhost.localdomain \
--to=chuck.lever@oracle.com \
--cc=linux-nfs@vger.kernel.org \
/path/to/YOUR_REPLY
https://kernel.org/pub/software/scm/git/docs/git-send-email.html
* If your mail client supports setting the In-Reply-To header
via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line
before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox