Commit 9cf514cc authored by Christoph Hellwig's avatar Christoph Hellwig
Browse files

nfsd: implement pNFS operations



Add support for the GETDEVICEINFO, LAYOUTGET, LAYOUTCOMMIT and
LAYOUTRETURN NFSv4.1 operations, as well as backing code to manage
outstanding layouts and devices.

Layout management is very straight forward, with a nfs4_layout_stateid
structure that extends nfs4_stid to manage layout stateids as the
top-level structure.  It is linked into the nfs4_file and nfs4_client
structures like the other stateids, and contains a linked list of
layouts that hang of the stateid.  The actual layout operations are
implemented in layout drivers that are not part of this commit, but
will be added later.

The worst part of this commit is the management of the pNFS device IDs,
which suffers from a specification that is not sanely implementable due
to the fact that the device-IDs are global and not bound to an export,
and have a small enough size so that we can't store the fsid portion of
a file handle, and must never be reused.  As we still do need perform all
export authentication and validation checks on a device ID passed to
GETDEVICEINFO we are caught between a rock and a hard place.  To work
around this issue we add a new hash that maps from a 64-bit integer to a
fsid so that we can look up the export to authenticate against it,
a 32-bit integer as a generation that we can bump when changing the device,
and a currently unused 32-bit integer that could be used in the future
to handle more than a single device per export.  Entries in this hash
table are never deleted as we can't reuse the ids anyway, and would have
a severe lifetime problem anyway as Linux export structures are temporary
structures that can go away under load.

Parts of the XDR data, structures and marshaling/unmarshaling code, as
well as many concepts are derived from the old pNFS server implementation
from Andy Adamson, Benny Halevy, Dean Hildebrand, Marc Eshel, Fred Isaman,
Mike Sager, Ricardo Labiaga and many others.
Signed-off-by: default avatarChristoph Hellwig <hch@lst.de>
parent 4d227fca
......@@ -82,6 +82,16 @@ config NFSD_V4
If unsure, say N.
config NFSD_PNFS
bool "NFSv4.1 server support for Parallel NFS (pNFS)"
depends on NFSD_V4
help
This option enables support for the parallel NFS features of the
minor version 1 of the NFSv4 protocol (RFC5661) in the kernel's NFS
server.
If unsure, say N.
config NFSD_V4_SECURITY_LABEL
bool "Provide Security Label support for NFSv4 server"
depends on NFSD_V4 && SECURITY
......
......@@ -12,3 +12,4 @@ nfsd-$(CONFIG_NFSD_V3) += nfs3proc.o nfs3xdr.o
nfsd-$(CONFIG_NFSD_V3_ACL) += nfs3acl.o
nfsd-$(CONFIG_NFSD_V4) += nfs4proc.o nfs4xdr.o nfs4state.o nfs4idmap.o \
nfs4acl.o nfs4callback.o nfs4recover.o
nfsd-$(CONFIG_NFSD_PNFS) += nfs4layouts.o
......@@ -20,6 +20,7 @@
#include "nfsd.h"
#include "nfsfh.h"
#include "netns.h"
#include "pnfs.h"
#define NFSDDBG_FACILITY NFSDDBG_EXPORT
......@@ -545,6 +546,7 @@ static int svc_export_parse(struct cache_detail *cd, char *mesg, int mlen)
exp.ex_client = dom;
exp.cd = cd;
exp.ex_devid_map = NULL;
/* expiry */
err = -EINVAL;
......@@ -621,6 +623,8 @@ static int svc_export_parse(struct cache_detail *cd, char *mesg, int mlen)
if (!gid_valid(exp.ex_anon_gid))
goto out4;
err = 0;
nfsd4_setup_layout_type(&exp);
}
expp = svc_export_lookup(&exp);
......@@ -703,6 +707,7 @@ static void svc_export_init(struct cache_head *cnew, struct cache_head *citem)
new->ex_fslocs.locations = NULL;
new->ex_fslocs.locations_count = 0;
new->ex_fslocs.migrated = 0;
new->ex_layout_type = 0;
new->ex_uuid = NULL;
new->cd = item->cd;
}
......@@ -717,6 +722,8 @@ static void export_update(struct cache_head *cnew, struct cache_head *citem)
new->ex_anon_uid = item->ex_anon_uid;
new->ex_anon_gid = item->ex_anon_gid;
new->ex_fsid = item->ex_fsid;
new->ex_devid_map = item->ex_devid_map;
item->ex_devid_map = NULL;
new->ex_uuid = item->ex_uuid;
item->ex_uuid = NULL;
new->ex_fslocs.locations = item->ex_fslocs.locations;
......@@ -725,6 +732,7 @@ static void export_update(struct cache_head *cnew, struct cache_head *citem)
item->ex_fslocs.locations_count = 0;
new->ex_fslocs.migrated = item->ex_fslocs.migrated;
item->ex_fslocs.migrated = 0;
new->ex_layout_type = item->ex_layout_type;
new->ex_nflavors = item->ex_nflavors;
for (i = 0; i < MAX_SECINFO_LIST; i++) {
new->ex_flavors[i] = item->ex_flavors[i];
......
......@@ -56,6 +56,8 @@ struct svc_export {
struct nfsd4_fs_locations ex_fslocs;
uint32_t ex_nflavors;
struct exp_flavor_info ex_flavors[MAX_SECINFO_LIST];
enum pnfs_layouttype ex_layout_type;
struct nfsd4_deviceid_map *ex_devid_map;
struct cache_detail *cd;
};
......
/*
* Copyright (c) 2014 Christoph Hellwig.
*/
#include <linux/jhash.h>
#include <linux/sched.h>
#include "pnfs.h"
#include "netns.h"
#define NFSDDBG_FACILITY NFSDDBG_PNFS
struct nfs4_layout {
struct list_head lo_perstate;
struct nfs4_layout_stateid *lo_state;
struct nfsd4_layout_seg lo_seg;
};
static struct kmem_cache *nfs4_layout_cache;
static struct kmem_cache *nfs4_layout_stateid_cache;
const struct nfsd4_layout_ops *nfsd4_layout_ops[LAYOUT_TYPE_MAX] = {
};
/* pNFS device ID to export fsid mapping */
#define DEVID_HASH_BITS 8
#define DEVID_HASH_SIZE (1 << DEVID_HASH_BITS)
#define DEVID_HASH_MASK (DEVID_HASH_SIZE - 1)
static u64 nfsd_devid_seq = 1;
static struct list_head nfsd_devid_hash[DEVID_HASH_SIZE];
static DEFINE_SPINLOCK(nfsd_devid_lock);
static inline u32 devid_hashfn(u64 idx)
{
return jhash_2words(idx, idx >> 32, 0) & DEVID_HASH_MASK;
}
static void
nfsd4_alloc_devid_map(const struct svc_fh *fhp)
{
const struct knfsd_fh *fh = &fhp->fh_handle;
size_t fsid_len = key_len(fh->fh_fsid_type);
struct nfsd4_deviceid_map *map, *old;
int i;
map = kzalloc(sizeof(*map) + fsid_len, GFP_KERNEL);
if (!map)
return;
map->fsid_type = fh->fh_fsid_type;
memcpy(&map->fsid, fh->fh_fsid, fsid_len);
spin_lock(&nfsd_devid_lock);
if (fhp->fh_export->ex_devid_map)
goto out_unlock;
for (i = 0; i < DEVID_HASH_SIZE; i++) {
list_for_each_entry(old, &nfsd_devid_hash[i], hash) {
if (old->fsid_type != fh->fh_fsid_type)
continue;
if (memcmp(old->fsid, fh->fh_fsid,
key_len(old->fsid_type)))
continue;
fhp->fh_export->ex_devid_map = old;
goto out_unlock;
}
}
map->idx = nfsd_devid_seq++;
list_add_tail_rcu(&map->hash, &nfsd_devid_hash[devid_hashfn(map->idx)]);
fhp->fh_export->ex_devid_map = map;
map = NULL;
out_unlock:
spin_unlock(&nfsd_devid_lock);
kfree(map);
}
struct nfsd4_deviceid_map *
nfsd4_find_devid_map(int idx)
{
struct nfsd4_deviceid_map *map, *ret = NULL;
rcu_read_lock();
list_for_each_entry_rcu(map, &nfsd_devid_hash[devid_hashfn(idx)], hash)
if (map->idx == idx)
ret = map;
rcu_read_unlock();
return ret;
}
int
nfsd4_set_deviceid(struct nfsd4_deviceid *id, const struct svc_fh *fhp,
u32 device_generation)
{
if (!fhp->fh_export->ex_devid_map) {
nfsd4_alloc_devid_map(fhp);
if (!fhp->fh_export->ex_devid_map)
return -ENOMEM;
}
id->fsid_idx = fhp->fh_export->ex_devid_map->idx;
id->generation = device_generation;
id->pad = 0;
return 0;
}
void nfsd4_setup_layout_type(struct svc_export *exp)
{
if (exp->ex_flags & NFSEXP_NOPNFS)
return;
}
static void
nfsd4_free_layout_stateid(struct nfs4_stid *stid)
{
struct nfs4_layout_stateid *ls = layoutstateid(stid);
struct nfs4_client *clp = ls->ls_stid.sc_client;
struct nfs4_file *fp = ls->ls_stid.sc_file;
spin_lock(&clp->cl_lock);
list_del_init(&ls->ls_perclnt);
spin_unlock(&clp->cl_lock);
spin_lock(&fp->fi_lock);
list_del_init(&ls->ls_perfile);
spin_unlock(&fp->fi_lock);
kmem_cache_free(nfs4_layout_stateid_cache, ls);
}
static struct nfs4_layout_stateid *
nfsd4_alloc_layout_stateid(struct nfsd4_compound_state *cstate,
struct nfs4_stid *parent, u32 layout_type)
{
struct nfs4_client *clp = cstate->clp;
struct nfs4_file *fp = parent->sc_file;
struct nfs4_layout_stateid *ls;
struct nfs4_stid *stp;
stp = nfs4_alloc_stid(cstate->clp, nfs4_layout_stateid_cache);
if (!stp)
return NULL;
stp->sc_free = nfsd4_free_layout_stateid;
get_nfs4_file(fp);
stp->sc_file = fp;
ls = layoutstateid(stp);
INIT_LIST_HEAD(&ls->ls_perclnt);
INIT_LIST_HEAD(&ls->ls_perfile);
spin_lock_init(&ls->ls_lock);
INIT_LIST_HEAD(&ls->ls_layouts);
ls->ls_layout_type = layout_type;
spin_lock(&clp->cl_lock);
stp->sc_type = NFS4_LAYOUT_STID;
list_add(&ls->ls_perclnt, &clp->cl_lo_states);
spin_unlock(&clp->cl_lock);
spin_lock(&fp->fi_lock);
list_add(&ls->ls_perfile, &fp->fi_lo_states);
spin_unlock(&fp->fi_lock);
return ls;
}
__be32
nfsd4_preprocess_layout_stateid(struct svc_rqst *rqstp,
struct nfsd4_compound_state *cstate, stateid_t *stateid,
bool create, u32 layout_type, struct nfs4_layout_stateid **lsp)
{
struct nfs4_layout_stateid *ls;
struct nfs4_stid *stid;
unsigned char typemask = NFS4_LAYOUT_STID;
__be32 status;
if (create)
typemask |= (NFS4_OPEN_STID | NFS4_LOCK_STID | NFS4_DELEG_STID);
status = nfsd4_lookup_stateid(cstate, stateid, typemask, &stid,
net_generic(SVC_NET(rqstp), nfsd_net_id));
if (status)
goto out;
if (!fh_match(&cstate->current_fh.fh_handle,
&stid->sc_file->fi_fhandle)) {
status = nfserr_bad_stateid;
goto out_put_stid;
}
if (stid->sc_type != NFS4_LAYOUT_STID) {
ls = nfsd4_alloc_layout_stateid(cstate, stid, layout_type);
nfs4_put_stid(stid);
status = nfserr_jukebox;
if (!ls)
goto out;
} else {
ls = container_of(stid, struct nfs4_layout_stateid, ls_stid);
status = nfserr_bad_stateid;
if (stateid->si_generation > stid->sc_stateid.si_generation)
goto out_put_stid;
if (layout_type != ls->ls_layout_type)
goto out_put_stid;
}
*lsp = ls;
return 0;
out_put_stid:
nfs4_put_stid(stid);
out:
return status;
}
static inline u64
layout_end(struct nfsd4_layout_seg *seg)
{
u64 end = seg->offset + seg->length;
return end >= seg->offset ? end : NFS4_MAX_UINT64;
}
static void
layout_update_len(struct nfsd4_layout_seg *lo, u64 end)
{
if (end == NFS4_MAX_UINT64)
lo->length = NFS4_MAX_UINT64;
else
lo->length = end - lo->offset;
}
static bool
layouts_overlapping(struct nfs4_layout *lo, struct nfsd4_layout_seg *s)
{
if (s->iomode != IOMODE_ANY && s->iomode != lo->lo_seg.iomode)
return false;
if (layout_end(&lo->lo_seg) <= s->offset)
return false;
if (layout_end(s) <= lo->lo_seg.offset)
return false;
return true;
}
static bool
layouts_try_merge(struct nfsd4_layout_seg *lo, struct nfsd4_layout_seg *new)
{
if (lo->iomode != new->iomode)
return false;
if (layout_end(new) < lo->offset)
return false;
if (layout_end(lo) < new->offset)
return false;
lo->offset = min(lo->offset, new->offset);
layout_update_len(lo, max(layout_end(lo), layout_end(new)));
return true;
}
__be32
nfsd4_insert_layout(struct nfsd4_layoutget *lgp, struct nfs4_layout_stateid *ls)
{
struct nfsd4_layout_seg *seg = &lgp->lg_seg;
struct nfs4_layout *lp, *new = NULL;
spin_lock(&ls->ls_lock);
list_for_each_entry(lp, &ls->ls_layouts, lo_perstate) {
if (layouts_try_merge(&lp->lo_seg, seg))
goto done;
}
spin_unlock(&ls->ls_lock);
new = kmem_cache_alloc(nfs4_layout_cache, GFP_KERNEL);
if (!new)
return nfserr_jukebox;
memcpy(&new->lo_seg, seg, sizeof(lp->lo_seg));
new->lo_state = ls;
spin_lock(&ls->ls_lock);
list_for_each_entry(lp, &ls->ls_layouts, lo_perstate) {
if (layouts_try_merge(&lp->lo_seg, seg))
goto done;
}
atomic_inc(&ls->ls_stid.sc_count);
list_add_tail(&new->lo_perstate, &ls->ls_layouts);
new = NULL;
done:
update_stateid(&ls->ls_stid.sc_stateid);
memcpy(&lgp->lg_sid, &ls->ls_stid.sc_stateid, sizeof(stateid_t));
spin_unlock(&ls->ls_lock);
if (new)
kmem_cache_free(nfs4_layout_cache, new);
return nfs_ok;
}
static void
nfsd4_free_layouts(struct list_head *reaplist)
{
while (!list_empty(reaplist)) {
struct nfs4_layout *lp = list_first_entry(reaplist,
struct nfs4_layout, lo_perstate);
list_del(&lp->lo_perstate);
nfs4_put_stid(&lp->lo_state->ls_stid);
kmem_cache_free(nfs4_layout_cache, lp);
}
}
static void
nfsd4_return_file_layout(struct nfs4_layout *lp, struct nfsd4_layout_seg *seg,
struct list_head *reaplist)
{
struct nfsd4_layout_seg *lo = &lp->lo_seg;
u64 end = layout_end(lo);
if (seg->offset <= lo->offset) {
if (layout_end(seg) >= end) {
list_move_tail(&lp->lo_perstate, reaplist);
return;
}
end = seg->offset;
} else {
/* retain the whole layout segment on a split. */
if (layout_end(seg) < end) {
dprintk("%s: split not supported\n", __func__);
return;
}
lo->offset = layout_end(seg);
}
layout_update_len(lo, end);
}
__be32
nfsd4_return_file_layouts(struct svc_rqst *rqstp,
struct nfsd4_compound_state *cstate,
struct nfsd4_layoutreturn *lrp)
{
struct nfs4_layout_stateid *ls;
struct nfs4_layout *lp, *n;
LIST_HEAD(reaplist);
__be32 nfserr;
int found = 0;
nfserr = nfsd4_preprocess_layout_stateid(rqstp, cstate, &lrp->lr_sid,
false, lrp->lr_layout_type,
&ls);
if (nfserr)
return nfserr;
spin_lock(&ls->ls_lock);
list_for_each_entry_safe(lp, n, &ls->ls_layouts, lo_perstate) {
if (layouts_overlapping(lp, &lrp->lr_seg)) {
nfsd4_return_file_layout(lp, &lrp->lr_seg, &reaplist);
found++;
}
}
if (!list_empty(&ls->ls_layouts)) {
if (found) {
update_stateid(&ls->ls_stid.sc_stateid);
memcpy(&lrp->lr_sid, &ls->ls_stid.sc_stateid,
sizeof(stateid_t));
}
lrp->lrs_present = 1;
} else {
nfs4_unhash_stid(&ls->ls_stid);
lrp->lrs_present = 0;
}
spin_unlock(&ls->ls_lock);
nfs4_put_stid(&ls->ls_stid);
nfsd4_free_layouts(&reaplist);
return nfs_ok;
}
__be32
nfsd4_return_client_layouts(struct svc_rqst *rqstp,
struct nfsd4_compound_state *cstate,
struct nfsd4_layoutreturn *lrp)
{
struct nfs4_layout_stateid *ls, *n;
struct nfs4_client *clp = cstate->clp;
struct nfs4_layout *lp, *t;
LIST_HEAD(reaplist);
lrp->lrs_present = 0;
spin_lock(&clp->cl_lock);
list_for_each_entry_safe(ls, n, &clp->cl_lo_states, ls_perclnt) {
if (lrp->lr_return_type == RETURN_FSID &&
!fh_fsid_match(&ls->ls_stid.sc_file->fi_fhandle,
&cstate->current_fh.fh_handle))
continue;
spin_lock(&ls->ls_lock);
list_for_each_entry_safe(lp, t, &ls->ls_layouts, lo_perstate) {
if (lrp->lr_seg.iomode == IOMODE_ANY ||
lrp->lr_seg.iomode == lp->lo_seg.iomode)
list_move_tail(&lp->lo_perstate, &reaplist);
}
spin_unlock(&ls->ls_lock);
}
spin_unlock(&clp->cl_lock);
nfsd4_free_layouts(&reaplist);
return 0;
}
static void
nfsd4_return_all_layouts(struct nfs4_layout_stateid *ls,
struct list_head *reaplist)
{
spin_lock(&ls->ls_lock);
list_splice_init(&ls->ls_layouts, reaplist);
spin_unlock(&ls->ls_lock);
}
void
nfsd4_return_all_client_layouts(struct nfs4_client *clp)
{
struct nfs4_layout_stateid *ls, *n;
LIST_HEAD(reaplist);
spin_lock(&clp->cl_lock);
list_for_each_entry_safe(ls, n, &clp->cl_lo_states, ls_perclnt)
nfsd4_return_all_layouts(ls, &reaplist);
spin_unlock(&clp->cl_lock);
nfsd4_free_layouts(&reaplist);
}
void
nfsd4_return_all_file_layouts(struct nfs4_client *clp, struct nfs4_file *fp)
{
struct nfs4_layout_stateid *ls, *n;
LIST_HEAD(reaplist);
spin_lock(&fp->fi_lock);
list_for_each_entry_safe(ls, n, &fp->fi_lo_states, ls_perfile) {
if (ls->ls_stid.sc_client == clp)
nfsd4_return_all_layouts(ls, &reaplist);
}
spin_unlock(&fp->fi_lock);
nfsd4_free_layouts(&reaplist);
}
int
nfsd4_init_pnfs(void)
{
int i;
for (i = 0; i < DEVID_HASH_SIZE; i++)
INIT_LIST_HEAD(&nfsd_devid_hash[i]);
nfs4_layout_cache = kmem_cache_create("nfs4_layout",
sizeof(struct nfs4_layout), 0, 0, NULL);
if (!nfs4_layout_cache)
return -ENOMEM;
nfs4_layout_stateid_cache = kmem_cache_create("nfs4_layout_stateid",
sizeof(struct nfs4_layout_stateid), 0, 0, NULL);
if (!nfs4_layout_stateid_cache) {
kmem_cache_destroy(nfs4_layout_cache);
return -ENOMEM;
}
return 0;
}
void
nfsd4_exit_pnfs(void)
{
int i;
kmem_cache_destroy(nfs4_layout_cache);
kmem_cache_destroy(nfs4_layout_stateid_cache);
for (i = 0; i < DEVID_HASH_SIZE; i++) {
struct nfsd4_deviceid_map *map, *n;
list_for_each_entry_safe(map, n, &nfsd_devid_hash[i], hash)
kfree(map);
}
}
......@@ -43,6 +43,7 @@
#include "current_stateid.h"
#include "netns.h"
#include "acl.h"
#include "pnfs.h"
#ifdef CONFIG_NFSD_V4_SECURITY_LABEL
#include <linux/security.h>
......@@ -1178,6 +1179,252 @@ nfsd4_verify(struct svc_rqst *rqstp, struct nfsd4_compound_state *cstate,
return status == nfserr_same ? nfs_ok : status;
}
#ifdef CONFIG_NFSD_PNFS
static const struct nfsd4_layout_ops *
nfsd4_layout_verify(struct svc_export *exp, unsigned int layout_type)
{
if (!exp->ex_layout_type) {
dprintk("%s: export does not support pNFS\n", __func__);
return NULL;
}
if (exp->ex_layout_type != layout_type) {
dprintk("%s: layout type %d not supported\n",
__func__, layout_type);
return NULL;
}
return nfsd4_layout_ops[layout_type];
}
static __be32
nfsd4_getdeviceinfo(struct svc_rqst *rqstp,
struct nfsd4_compound_state *cstate,
struct nfsd4_getdeviceinfo *gdp)
{
const struct nfsd4_layout_ops *ops;
struct nfsd4_deviceid_map *map;
struct svc_export *exp;
__be32 nfserr;
dprintk("%s: layout_type %u dev_id [0x%llx:0x%x] maxcnt %u\n",
__func__,
gdp->gd_layout_type,
gdp->gd_devid.fsid_idx, gdp->gd_devid.generation,
gdp->gd_maxcount);