NFS: Add sysfs support for per-container identifier

In order to identify containers to the NFS client, we add a per-net
sysfs attribute that udev can fill with the appropriate identifier.
The identifier could be a unique hostname, but in most cases it
will probably be a persisted uuid.

Signed-off-by: Trond Myklebust <trond.myklebust@hammerspace.com>
This commit is contained in:
Trond Myklebust 2019-01-29 21:40:10 -05:00
parent 10b7a70cbb
commit bf11fbdb20
4 changed files with 135 additions and 0 deletions

View File

@ -49,6 +49,7 @@
#include "pnfs.h"
#include "nfs.h"
#include "netns.h"
#include "sysfs.h"
#define NFSDBG_FACILITY NFSDBG_CLIENT
@ -1072,12 +1073,15 @@ void nfs_clients_init(struct net *net)
#endif
spin_lock_init(&nn->nfs_client_lock);
nn->boot_time = ktime_get_real();
nfs_netns_sysfs_setup(nn, net);
}
void nfs_clients_exit(struct net *net)
{
struct nfs_net *nn = net_generic(net, nfs_net_id);
nfs_netns_sysfs_destroy(nn);
nfs_cleanup_cb_ident_idr(net);
WARN_ON_ONCE(!list_empty(&nn->nfs_client_list));
WARN_ON_ONCE(!list_empty(&nn->nfs_volume_list));

View File

@ -15,6 +15,8 @@ struct bl_dev_msg {
uint32_t major, minor;
};
struct nfs_netns_client;
struct nfs_net {
struct cache_detail *nfs_dns_resolve;
struct rpc_pipe *bl_device_pipe;
@ -29,6 +31,7 @@ struct nfs_net {
unsigned short nfs_callback_tcpport6;
int cb_users[NFS4_MAX_MINOR_VERSION + 1];
#endif
struct nfs_netns_client *nfs_client;
spinlock_t nfs_client_lock;
ktime_t boot_time;
#ifdef CONFIG_PROC_FS

View File

@ -9,7 +9,12 @@
#include <linux/fs.h>
#include <linux/slab.h>
#include <linux/netdevice.h>
#include <linux/string.h>
#include <linux/nfs_fs.h>
#include <linux/rcupdate.h>
#include "nfs4_fs.h"
#include "netns.h"
#include "sysfs.h"
struct kobject *nfs_client_kobj;
@ -67,3 +72,116 @@ void nfs_sysfs_exit(void)
kobject_put(nfs_client_kobj);
kset_unregister(nfs_client_kset);
}
static ssize_t nfs_netns_identifier_show(struct kobject *kobj,
struct kobj_attribute *attr, char *buf)
{
struct nfs_netns_client *c = container_of(kobj,
struct nfs_netns_client,
kobject);
return scnprintf(buf, PAGE_SIZE, "%s\n", c->identifier);
}
/* Strip trailing '\n' */
static size_t nfs_string_strip(const char *c, size_t len)
{
while (len > 0 && c[len-1] == '\n')
--len;
return len;
}
static ssize_t nfs_netns_identifier_store(struct kobject *kobj,
struct kobj_attribute *attr,
const char *buf, size_t count)
{
struct nfs_netns_client *c = container_of(kobj,
struct nfs_netns_client,
kobject);
const char *old;
char *p;
size_t len;
len = nfs_string_strip(buf, min_t(size_t, count, CONTAINER_ID_MAXLEN));
if (!len)
return 0;
p = kmemdup_nul(buf, len, GFP_KERNEL);
if (!p)
return -ENOMEM;
old = xchg(&c->identifier, p);
if (old) {
synchronize_rcu();
kfree(old);
}
return count;
}
static void nfs_netns_client_release(struct kobject *kobj)
{
struct nfs_netns_client *c = container_of(kobj,
struct nfs_netns_client,
kobject);
if (c->identifier)
kfree(c->identifier);
kfree(c);
}
static const void *nfs_netns_client_namespace(struct kobject *kobj)
{
return container_of(kobj, struct nfs_netns_client, kobject)->net;
}
static struct kobj_attribute nfs_netns_client_id = __ATTR(identifier,
0644, nfs_netns_identifier_show, nfs_netns_identifier_store);
static struct attribute *nfs_netns_client_attrs[] = {
&nfs_netns_client_id.attr,
NULL,
};
static struct kobj_type nfs_netns_client_type = {
.release = nfs_netns_client_release,
.default_attrs = nfs_netns_client_attrs,
.sysfs_ops = &kobj_sysfs_ops,
.namespace = nfs_netns_client_namespace,
};
static struct nfs_netns_client *nfs_netns_client_alloc(struct kobject *parent,
struct net *net)
{
struct nfs_netns_client *p;
p = kzalloc(sizeof(*p), GFP_KERNEL);
if (p) {
p->net = net;
p->kobject.kset = nfs_client_kset;
if (kobject_init_and_add(&p->kobject, &nfs_netns_client_type,
parent, "nfs_client") == 0)
return p;
kobject_put(&p->kobject);
}
return NULL;
}
void nfs_netns_sysfs_setup(struct nfs_net *netns, struct net *net)
{
struct nfs_netns_client *clp;
clp = nfs_netns_client_alloc(nfs_client_kobj, net);
if (clp) {
netns->nfs_client = clp;
kobject_uevent(&clp->kobject, KOBJ_ADD);
}
}
void nfs_netns_sysfs_destroy(struct nfs_net *netns)
{
struct nfs_netns_client *clp = netns->nfs_client;
if (clp) {
kobject_uevent(&clp->kobject, KOBJ_REMOVE);
kobject_del(&clp->kobject);
kobject_put(&clp->kobject);
netns->nfs_client = NULL;
}
}

View File

@ -6,10 +6,20 @@
#ifndef __NFS_SYSFS_H
#define __NFS_SYSFS_H
#define CONTAINER_ID_MAXLEN (64)
struct nfs_netns_client {
struct kobject kobject;
struct net *net;
const char *identifier;
};
extern struct kobject *nfs_client_kobj;
extern int nfs_sysfs_init(void);
extern void nfs_sysfs_exit(void);
void nfs_netns_sysfs_setup(struct nfs_net *netns, struct net *net);
void nfs_netns_sysfs_destroy(struct nfs_net *netns);
#endif