#include <linux/namei.h>
#include <linux/sunrpc/svc.h>
#include "nfs2xdr_gen.h"
#include "auth.h"
#include "nfsd.h"
#include "cache.h"
#include "xdr.h"
#include "vfs.h"
#include "nfserr.h"
#include "trace.h"
#define NFSDDBG_FACILITY NFSDDBG_PROC
struct fhandle_wrapper {
fhandle xdrgen;
struct svc_fh fh;
};
static_assert(offsetof(struct fhandle_wrapper, xdrgen) == 0);
struct attrstat_wrapper {
struct attrstat xdrgen;
struct kstat stat;
};
static_assert(offsetof(struct attrstat_wrapper, xdrgen) == 0);
struct sattrargs_wrapper {
struct sattrargs xdrgen;
struct svc_fh fh;
struct iattr iattrs;
};
static_assert(offsetof(struct sattrargs_wrapper, xdrgen) == 0);
struct diropargs_wrapper {
struct diropargs xdrgen;
struct svc_fh fh;
};
static_assert(offsetof(struct diropargs_wrapper, xdrgen) == 0);
struct diropres_wrapper {
struct diropres xdrgen;
struct svc_fh fh;
struct kstat stat;
};
static_assert(offsetof(struct diropres_wrapper, xdrgen) == 0);
struct createargs_wrapper {
struct createargs xdrgen;
struct svc_fh fh;
struct iattr iattrs;
};
static_assert(offsetof(struct createargs_wrapper, xdrgen) == 0);
struct readargs_wrapper {
struct readargs xdrgen;
struct svc_fh fh;
};
static_assert(offsetof(struct readargs_wrapper, xdrgen) == 0);
struct writeargs_wrapper {
struct writeargs xdrgen;
struct svc_fh fh;
};
static_assert(offsetof(struct writeargs_wrapper, xdrgen) == 0);
struct renameargs_wrapper {
struct renameargs xdrgen;
struct svc_fh ffh;
struct svc_fh tfh;
};
static_assert(offsetof(struct renameargs_wrapper, xdrgen) == 0);
struct linkargs_wrapper {
struct linkargs xdrgen;
struct svc_fh ffh;
struct svc_fh tfh;
};
static_assert(offsetof(struct linkargs_wrapper, xdrgen) == 0);
struct symlinkargs_wrapper {
struct symlinkargs xdrgen;
struct svc_fh ffh;
struct iattr iattrs;
};
static_assert(offsetof(struct symlinkargs_wrapper, xdrgen) == 0);
struct statfsres_wrapper {
struct statfsres xdrgen;
struct kstatfs stats;
};
static_assert(offsetof(struct statfsres_wrapper, xdrgen) == 0);
static __be32 nfsd_map_status(__be32 status)
{
switch (status) {
case nfs_ok:
break;
case nfserr_nofilehandle:
case nfserr_badhandle:
status = nfserr_stale;
break;
case nfserr_wrongsec:
case nfserr_xdev:
case nfserr_file_open:
status = nfserr_acces;
break;
case nfserr_symlink_not_dir:
status = nfserr_notdir;
break;
case nfserr_symlink:
case nfserr_wrong_type:
status = nfserr_io;
break;
}
return status;
}
static __always_inline void
nfsd_fhandle_to_svc_fh(struct svc_fh *fhp, const fhandle *fhandle)
{
fh_init(fhp, NFS_FHSIZE);
fhp->fh_handle.fh_size = NFS_FHSIZE;
memcpy(&fhp->fh_handle.fh_raw, fhandle, NFS_FHSIZE);
}
static __always_inline void
nfsd_svc_fh_to_fhandle(fhandle *fhandle, const struct svc_fh *fhp)
{
memcpy(fhandle, fhp->fh_handle.fh_raw, NFS_FHSIZE);
}
static __always_inline void
nfsd_timespec64_to_timeval(struct timeval *dst,
const struct timespec64 *src)
{
dst->seconds = src->tv_sec;
dst->useconds = src->tv_nsec / NSEC_PER_USEC;
}
static __always_inline void
nfsd_timeval_to_timespec64(struct timespec64 *dst, const struct timeval *src)
{
dst->tv_sec = src->seconds;
dst->tv_nsec = src->useconds * NSEC_PER_USEC;
}
static u32
nfsd_mode_to_ftype(umode_t mode)
{
switch (mode & S_IFMT) {
case S_IFREG: return NFREG;
case S_IFDIR: return NFDIR;
case S_IFBLK: return NFBLK;
case S_IFCHR: return NFCHR;
case S_IFLNK: return NFLNK;
case S_IFSOCK: return NFNON;
case S_IFIFO: return NFNON;
}
return NFNON;
}
static void
nfsd_stat_to_fattr(struct svc_rqst *rqstp, struct fattr *fattr,
const struct kstat *stat, const struct svc_fh *fhp)
{
struct user_namespace *userns = nfsd_user_namespace(rqstp);
struct timespec64 time;
u32 fsid;
fattr->type = nfsd_mode_to_ftype(stat->mode);
fattr->mode = stat->mode;
fattr->nlink = stat->nlink;
fattr->uid = from_kuid_munged(userns, stat->uid);
fattr->gid = from_kgid_munged(userns, stat->gid);
if (S_ISLNK(stat->mode) && stat->size > NFS_MAXPATHLEN)
fattr->size = NFS_MAXPATHLEN;
else
fattr->size = stat->size;
fattr->blocksize = stat->blksize;
if (S_ISCHR(stat->mode) || S_ISBLK(stat->mode))
fattr->rdev = new_encode_dev(stat->rdev);
else
fattr->rdev = 0xffffffff;
fattr->blocks = stat->blocks;
switch (fsid_source(fhp)) {
case FSIDSOURCE_FSID:
fsid = (u32)fhp->fh_export->ex_fsid;
break;
case FSIDSOURCE_UUID:
fsid = ((u32 *)fhp->fh_export->ex_uuid)[0];
fsid ^= ((u32 *)fhp->fh_export->ex_uuid)[1];
fsid ^= ((u32 *)fhp->fh_export->ex_uuid)[2];
fsid ^= ((u32 *)fhp->fh_export->ex_uuid)[3];
break;
default:
fsid = new_encode_dev(stat->dev);
}
fattr->fsid = fsid;
fattr->fileid = stat->ino;
nfsd_timespec64_to_timeval(&fattr->atime, &stat->atime);
time = stat->mtime;
lease_get_mtime(d_inode(fhp->fh_dentry), &time);
nfsd_timespec64_to_timeval(&fattr->mtime, &time);
nfsd_timespec64_to_timeval(&fattr->ctime, &stat->ctime);
}
static __be32 nfsd_map_io_status(__be32 status)
{
switch (status) {
case nfserr_symlink:
case nfserr_wrong_type:
return nfserr_isdir;
}
return nfsd_map_status(status);
}
#define NFS2_SATTR_SET_TO_SERVER_TIME (1000000)
static bool
nfsd_sattr_to_iattr(struct svc_rqst *rqstp, struct iattr *iap,
const struct sattr *sattr)
{
static const unsigned int dont_set_it = (unsigned int)-1;
memset(iap, 0, sizeof(*iap));
if (sattr->mode != dont_set_it && sattr->mode != 0xffff) {
iap->ia_valid |= ATTR_MODE;
iap->ia_mode = sattr->mode;
}
if (sattr->uid != dont_set_it) {
iap->ia_uid = make_kuid(nfsd_user_namespace(rqstp), sattr->uid);
if (uid_valid(iap->ia_uid))
iap->ia_valid |= ATTR_UID;
}
if (sattr->gid != dont_set_it) {
iap->ia_gid = make_kgid(nfsd_user_namespace(rqstp), sattr->gid);
if (gid_valid(iap->ia_gid))
iap->ia_valid |= ATTR_GID;
}
if (sattr->size != dont_set_it) {
iap->ia_valid |= ATTR_SIZE;
iap->ia_size = sattr->size;
}
if (sattr->atime.seconds != dont_set_it &&
sattr->atime.useconds != dont_set_it) {
if (sattr->atime.useconds > NFS2_SATTR_SET_TO_SERVER_TIME)
return false;
iap->ia_valid |= ATTR_ATIME | ATTR_ATIME_SET;
nfsd_timeval_to_timespec64(&iap->ia_atime, &sattr->atime);
if (sattr->atime.useconds == NFS2_SATTR_SET_TO_SERVER_TIME)
iap->ia_valid &= ~ATTR_ATIME_SET;
}
if (sattr->mtime.seconds != dont_set_it &&
sattr->mtime.useconds != dont_set_it) {
if (sattr->mtime.useconds > NFS2_SATTR_SET_TO_SERVER_TIME)
return false;
iap->ia_valid |= ATTR_MTIME | ATTR_MTIME_SET;
nfsd_timeval_to_timespec64(&iap->ia_mtime, &sattr->mtime);
if (sattr->mtime.useconds == NFS2_SATTR_SET_TO_SERVER_TIME)
iap->ia_valid &= ~(ATTR_ATIME_SET | ATTR_MTIME_SET);
}
return true;
}
static __be32 nfsd_proc_null(struct svc_rqst *rqstp)
{
return rpc_success;
}
static __be32 nfsd_proc_getattr(struct svc_rqst *rqstp)
{
struct fhandle_wrapper *argp = rqstp->rq_argp;
struct attrstat_wrapper *resp = rqstp->rq_resp;
struct kstat *statp = &resp->stat;
struct svc_fh *fhp = &argp->fh;
nfsd_fhandle_to_svc_fh(fhp, &argp->xdrgen);
trace_nfsd_vfs_getattr(rqstp, fhp);
resp->xdrgen.status = fh_verify(rqstp, fhp, 0, NFSD_MAY_NOP |
NFSD_MAY_BYPASS_GSS_ON_ROOT);
if (resp->xdrgen.status != nfs_ok)
goto out;
resp->xdrgen.status = fh_getattr(fhp, statp);
out:
if (resp->xdrgen.status == nfs_ok)
nfsd_stat_to_fattr(rqstp, &resp->xdrgen.u.attributes, statp, fhp);
else
resp->xdrgen.status = nfsd_map_status(resp->xdrgen.status);
fh_put(fhp);
return rpc_success;
}
static __be32 nfsd_proc_setattr(struct svc_rqst *rqstp)
{
struct sattrargs_wrapper *argp = rqstp->rq_argp;
struct attrstat_wrapper *resp = rqstp->rq_resp;
struct kstat *statp = &resp->stat;
struct iattr *iap = &argp->iattrs;
struct nfsd_attrs nattrs = {
.na_iattr = iap,
};
struct svc_fh *fhp = &argp->fh;
nfsd_fhandle_to_svc_fh(fhp, &argp->xdrgen.file);
if (!nfsd_sattr_to_iattr(rqstp, iap, &argp->xdrgen.attributes)) {
resp->xdrgen.status = nfserr_io;
goto out;
}
#define BOTH_TIME_SET (ATTR_ATIME_SET | ATTR_MTIME_SET)
#define MAX_TOUCH_TIME_ERROR (30*60)
if ((iap->ia_valid & BOTH_TIME_SET) == BOTH_TIME_SET &&
iap->ia_mtime.tv_sec == iap->ia_atime.tv_sec) {
int hosterr;
time64_t delta = iap->ia_atime.tv_sec - ktime_get_real_seconds();
resp->xdrgen.status = fh_verify(rqstp, fhp, 0, NFSD_MAY_NOP);
if (resp->xdrgen.status != nfs_ok)
goto out;
hosterr = fh_want_write(fhp);
if (hosterr) {
resp->xdrgen.status = nfserrno(hosterr);
goto out;
}
if (delta < 0)
delta = -delta;
if (delta < MAX_TOUCH_TIME_ERROR &&
setattr_prepare(&nop_mnt_idmap, fhp->fh_dentry, iap) != 0) {
iap->ia_valid &= ~BOTH_TIME_SET;
}
}
resp->xdrgen.status = nfsd_setattr(rqstp, fhp, &nattrs, NULL);
if (resp->xdrgen.status != nfs_ok)
goto out;
resp->xdrgen.status = fh_getattr(fhp, statp);
out:
if (resp->xdrgen.status == nfs_ok)
nfsd_stat_to_fattr(rqstp, &resp->xdrgen.u.attributes, statp, fhp);
else
resp->xdrgen.status = nfsd_map_status(resp->xdrgen.status);
fh_put(fhp);
return rpc_success;
}
static __be32 nfsd_proc_root(struct svc_rqst *rqstp)
{
return rpc_success;
}
static __be32 nfsd_proc_lookup(struct svc_rqst *rqstp)
{
struct diropargs_wrapper *argp = rqstp->rq_argp;
struct diropres_wrapper *resp = rqstp->rq_resp;
struct diropargs *what = &argp->xdrgen;
struct kstat *statp = &resp->stat;
struct svc_fh *dirfhp = &argp->fh;
struct svc_fh *fhp = &resp->fh;
nfsd_fhandle_to_svc_fh(dirfhp, &what->dir);
fh_init(fhp, NFS_FHSIZE);
resp->xdrgen.status = nfsd_lookup(rqstp, dirfhp,
(char *)what->name.data,
what->name.len, fhp);
if (resp->xdrgen.status != nfs_ok)
goto out;
resp->xdrgen.status = fh_getattr(fhp, statp);
out:
if (resp->xdrgen.status == nfs_ok) {
nfsd_svc_fh_to_fhandle(&resp->xdrgen.u.diropok.file, fhp);
nfsd_stat_to_fattr(rqstp, &resp->xdrgen.u.diropok.attributes,
statp, fhp);
} else {
resp->xdrgen.status = nfsd_map_status(resp->xdrgen.status);
}
fh_put(fhp);
fh_put(dirfhp);
return rpc_success;
}
static __be32 nfsd_proc_readlink(struct svc_rqst *rqstp)
{
struct fhandle_wrapper *argp = rqstp->rq_argp;
struct readlinkres *resp = rqstp->rq_resp;
struct svc_fh *fhp = &argp->fh;
struct page *page;
u32 len;
nfsd_fhandle_to_svc_fh(fhp, &argp->xdrgen);
len = NFS_MAXPATHLEN;
page = *(rqstp->rq_next_page++);
resp->status = nfsd_readlink(rqstp, fhp, page_address(page), &len);
if (resp->status == nfs_ok)
resp->u.data.len = len;
else
resp->status = nfsd_map_status(resp->status);
fh_put(fhp);
return rpc_success;
}
static __be32 nfsd_proc_read(struct svc_rqst *rqstp)
{
struct readargs_wrapper *argp = rqstp->rq_argp;
struct readres *resp = rqstp->rq_resp;
unsigned long count = argp->xdrgen.count;
u32 offset = argp->xdrgen.offset;
struct svc_fh *fhp = &argp->fh;
struct kstat stat;
u32 eof;
nfsd_fhandle_to_svc_fh(fhp, &argp->xdrgen.file);
count = min_t(u32, count, NFS_MAXDATA);
count = min_t(u32, count, rqstp->rq_res.buflen);
svc_reserve_auth(rqstp, (19 << 2) + count + 4);
resp->status = nfsd_read(rqstp, fhp, offset, &count, &eof);
if (resp->status == nfs_ok) {
struct readresok *resok = &resp->u.readresok;
resok->data.len = count;
resp->status = fh_getattr(fhp, &stat);
if (resp->status == nfs_ok)
nfsd_stat_to_fattr(rqstp, &resok->attributes, &stat,
fhp);
}
if (resp->status == nfserr_jukebox)
set_bit(RQ_DROPME, &rqstp->rq_flags);
if (resp->status != nfs_ok)
resp->status = nfsd_map_io_status(resp->status);
fh_put(fhp);
return rpc_success;
}
static __be32 nfsd_proc_writecache(struct svc_rqst *rqstp)
{
return rpc_success;
}
static __be32 nfsd_proc_write(struct svc_rqst *rqstp)
{
struct writeargs_wrapper *argp = rqstp->rq_argp;
struct attrstat_wrapper *resp = rqstp->rq_resp;
struct kstat *statp = &resp->stat;
unsigned long count = argp->xdrgen.data.len;
struct svc_fh *fhp = &argp->fh;
nfsd_fhandle_to_svc_fh(fhp, &argp->xdrgen.file);
resp->xdrgen.status = nfsd_write(rqstp, fhp, argp->xdrgen.offset,
&argp->xdrgen.data, &count,
IOCB_DSYNC, NULL);
if (resp->xdrgen.status == nfs_ok) {
resp->xdrgen.status = fh_getattr(fhp, statp);
if (resp->xdrgen.status == nfs_ok)
nfsd_stat_to_fattr(rqstp, &resp->xdrgen.u.attributes,
statp, fhp);
}
if (resp->xdrgen.status == nfserr_jukebox)
set_bit(RQ_DROPME, &rqstp->rq_flags);
if (resp->xdrgen.status != nfs_ok)
resp->xdrgen.status = nfsd_map_io_status(resp->xdrgen.status);
fh_put(fhp);
return rpc_success;
}
static __be32
nfsd_create_file(struct svc_rqst *rqstp, struct svc_fh *fhp,
struct svc_fh *resfhp, struct createargs_wrapper *argp)
{
struct diropargs *where = &argp->xdrgen.where;
struct iattr *attr = &argp->iattrs;
dev_t rdev = 0, wanted = new_decode_dev(attr->ia_size);
struct nfsd_attrs attrs = {
.na_iattr = attr,
};
struct svc_export *exp;
struct dentry *dchild;
struct inode *inode;
int type, mode;
__be32 status;
int host_err;
if (name_is_dot_dotdot(where->name.data, where->name.len))
return nfserr_exist;
status = fh_verify(rqstp, fhp, S_IFDIR, NFSD_MAY_EXEC);
if (status != nfs_ok)
return status;
host_err = fh_want_write(fhp);
if (host_err)
return nfserrno(host_err);
dchild = start_creating(&nop_mnt_idmap, fhp->fh_dentry,
&QSTR_LEN(where->name.data, where->name.len));
if (IS_ERR(dchild)) {
status = nfserrno(PTR_ERR(dchild));
goto out_drop_write;
}
exp = exp_get(fhp->fh_export);
if (d_is_reg(dchild) &&
unlikely(nfsd_mountpoint(dchild, exp))) {
status = nfsd_cross_mnt(rqstp, &dchild, &exp);
if (status != nfs_ok) {
exp_put(exp);
goto out;
}
}
status = fh_compose(resfhp, exp, dchild, fhp);
exp_put(exp);
if (!status && d_really_is_negative(dchild))
status = nfserr_noent;
if (status) {
if (status != nfserr_noent)
goto out;
status = nfserr_acces;
if (!resfhp->fh_dentry) {
printk(KERN_WARNING
"nfsd_proc_create: file handle not verified\n");
goto out;
}
}
inode = d_inode(resfhp->fh_dentry);
if (attr->ia_valid & ATTR_MODE) {
type = attr->ia_mode & S_IFMT;
mode = attr->ia_mode & ~S_IFMT;
if (!type) {
if (inode) {
type = inode->i_mode & S_IFMT;
switch (type) {
case S_IFCHR:
case S_IFBLK:
rdev = inode->i_rdev;
attr->ia_valid |= ATTR_SIZE;
fallthrough;
case S_IFIFO:
status = nfsd_permission(
&rqstp->rq_cred,
resfhp->fh_export,
resfhp->fh_dentry,
NFSD_MAY_WRITE|NFSD_MAY_LOCAL_ACCESS);
if (status && status != nfserr_rofs)
goto out;
}
} else
type = S_IFREG;
}
} else if (inode) {
type = inode->i_mode & S_IFMT;
mode = inode->i_mode & ~S_IFMT;
} else {
type = S_IFREG;
mode = 0;
}
attr->ia_valid |= ATTR_MODE;
attr->ia_mode = mode;
if (type != S_IFREG) {
if (type != S_IFBLK && type != S_IFCHR) {
rdev = 0;
} else if (type == S_IFCHR && !(attr->ia_valid & ATTR_SIZE)) {
type = S_IFIFO;
} else {
if (!rdev)
rdev = wanted;
}
attr->ia_valid &= ~ATTR_SIZE;
status = nfserr_exist;
if (inode && inode_wrong_type(inode, type))
goto out;
}
status = nfs_ok;
if (!inode) {
status = nfsd_create_locked(rqstp, fhp, &attrs, type,
rdev, resfhp);
dput(dchild);
goto out_drop_write;
} else if (type == S_IFREG) {
dprintk("nfsd: existing %s, valid=%x, size=%ld\n",
where->name.data, attr->ia_valid, (long)attr->ia_size);
attr->ia_valid &= ATTR_SIZE;
if (attr->ia_valid)
status = nfsd_setattr(rqstp, resfhp, &attrs, NULL);
}
out:
end_creating(dchild);
out_drop_write:
fh_drop_write(fhp);
return status;
}
static __be32 nfsd_proc_create(struct svc_rqst *rqstp)
{
struct createargs_wrapper *argp = rqstp->rq_argp;
struct diropres_wrapper *resp = rqstp->rq_resp;
struct kstat *statp = &resp->stat;
struct svc_fh *dirfhp = &argp->fh;
struct svc_fh *fhp = &resp->fh;
nfsd_fhandle_to_svc_fh(dirfhp, &argp->xdrgen.where.dir);
fh_init(fhp, NFS_FHSIZE);
if (!nfsd_sattr_to_iattr(rqstp, &argp->iattrs, &argp->xdrgen.attributes)) {
resp->xdrgen.status = nfserr_io;
goto out;
}
resp->xdrgen.status = nfsd_create_file(rqstp, dirfhp, fhp, argp);
if (resp->xdrgen.status != nfs_ok)
goto out;
resp->xdrgen.status = fh_getattr(fhp, statp);
out:
if (resp->xdrgen.status == nfs_ok) {
nfsd_svc_fh_to_fhandle(&resp->xdrgen.u.diropok.file, fhp);
nfsd_stat_to_fattr(rqstp, &resp->xdrgen.u.diropok.attributes,
statp, fhp);
} else {
resp->xdrgen.status = nfsd_map_status(resp->xdrgen.status);
}
fh_put(fhp);
fh_put(dirfhp);
return rpc_success;
}
static __be32 nfsd_proc_remove(struct svc_rqst *rqstp)
{
struct diropargs_wrapper *argp = rqstp->rq_argp;
struct diropargs *object = &argp->xdrgen;
nfsstat *resp = rqstp->rq_resp;
struct svc_fh *fhp = &argp->fh;
nfsd_fhandle_to_svc_fh(fhp, &object->dir);
*resp = nfsd_unlink(rqstp, fhp, -S_IFDIR,
(char *)object->name.data, object->name.len);
*resp = nfsd_map_status(*resp);
fh_put(fhp);
return rpc_success;
}
static __be32 nfsd_proc_rename(struct svc_rqst *rqstp)
{
struct renameargs_wrapper *argp = rqstp->rq_argp;
struct diropargs *from = &argp->xdrgen.from;
struct diropargs *to = &argp->xdrgen.to;
nfsstat *resp = rqstp->rq_resp;
struct svc_fh *ffhp = &argp->ffh;
struct svc_fh *tfhp = &argp->tfh;
nfsd_fhandle_to_svc_fh(ffhp, &from->dir);
nfsd_fhandle_to_svc_fh(tfhp, &to->dir);
*resp = nfsd_rename(rqstp, ffhp, (char *)from->name.data,
from->name.len, tfhp, (char *)to->name.data,
to->name.len);
*resp = nfsd_map_status(*resp);
fh_put(ffhp);
fh_put(tfhp);
return rpc_success;
}
static __be32 nfsd_proc_link(struct svc_rqst *rqstp)
{
struct linkargs_wrapper *argp = rqstp->rq_argp;
struct diropargs *to = &argp->xdrgen.to;
nfsstat *resp = rqstp->rq_resp;
struct svc_fh *ffhp = &argp->ffh;
struct svc_fh *tfhp = &argp->tfh;
nfsd_fhandle_to_svc_fh(ffhp, &argp->xdrgen.from);
nfsd_fhandle_to_svc_fh(tfhp, &to->dir);
*resp = nfsd_link(rqstp, tfhp, (char *)to->name.data, to->name.len,
ffhp);
*resp = nfsd_map_status(*resp);
fh_put(ffhp);
fh_put(tfhp);
return rpc_success;
}
static char *nfsd_symlink_target(const struct xdr_buf *to)
{
char *result;
result = kmalloc(to->len + 1, GFP_KERNEL);
if (!result)
return ERR_PTR(-ESERVERFAULT);
if (read_bytes_from_xdr_buf(to, 0, result, to->len)) {
kfree(result);
return ERR_PTR(-ESERVERFAULT);
}
result[to->len] = '\0';
if (strlen(result) != to->len) {
kfree(result);
return ERR_PTR(-EINVAL);
}
return result;
}
static __be32 nfsd_proc_symlink(struct svc_rqst *rqstp)
{
struct symlinkargs_wrapper *argp = rqstp->rq_argp;
struct diropargs *from = &argp->xdrgen.from;
struct xdr_buf *to = &argp->xdrgen.to;
nfsstat *resp = rqstp->rq_resp;
struct svc_fh *fhp = &argp->ffh;
struct nfsd_attrs nattrs = {
.na_iattr = &argp->iattrs,
};
struct svc_fh newfh;
char *tname;
nfsd_fhandle_to_svc_fh(fhp, &from->dir);
if (!nfsd_sattr_to_iattr(rqstp, &argp->iattrs, &argp->xdrgen.attributes)) {
*resp = nfserr_io;
goto out;
}
tname = nfsd_symlink_target(to);
if (IS_ERR(tname)) {
*resp = nfserrno(PTR_ERR(tname));
goto out;
}
fh_init(&newfh, NFS_FHSIZE);
*resp = nfsd_symlink(rqstp, fhp, (char *)from->name.data, from->name.len,
tname, &nattrs, &newfh);
kfree(tname);
*resp = nfsd_map_status(*resp);
fh_put(&newfh);
out:
fh_put(fhp);
return rpc_success;
}
static __be32 nfsd_proc_mkdir(struct svc_rqst *rqstp)
{
struct createargs_wrapper *argp = rqstp->rq_argp;
struct diropargs *where = &argp->xdrgen.where;
struct diropres_wrapper *resp = rqstp->rq_resp;
struct kstat *statp = &resp->stat;
struct svc_fh *dirfhp = &argp->fh;
struct svc_fh *fhp = &resp->fh;
struct nfsd_attrs nattrs = {
.na_iattr = &argp->iattrs,
};
nfsd_fhandle_to_svc_fh(dirfhp, &where->dir);
fh_init(fhp, NFS_FHSIZE);
if (!nfsd_sattr_to_iattr(rqstp, &argp->iattrs, &argp->xdrgen.attributes)) {
resp->xdrgen.status = nfserr_io;
goto out;
}
argp->iattrs.ia_valid &= ~ATTR_SIZE;
resp->xdrgen.status = nfsd_create(rqstp, dirfhp,
(char *)where->name.data,
where->name.len, &nattrs,
S_IFDIR, 0, fhp);
if (resp->xdrgen.status != nfs_ok)
goto out;
resp->xdrgen.status = fh_getattr(fhp, statp);
out:
if (resp->xdrgen.status == nfs_ok) {
nfsd_svc_fh_to_fhandle(&resp->xdrgen.u.diropok.file, fhp);
nfsd_stat_to_fattr(rqstp, &resp->xdrgen.u.diropok.attributes,
statp, fhp);
} else {
resp->xdrgen.status = nfsd_map_status(resp->xdrgen.status);
}
fh_put(fhp);
fh_put(dirfhp);
return rpc_success;
}
static __be32 nfsd_proc_rmdir(struct svc_rqst *rqstp)
{
struct diropargs_wrapper *argp = rqstp->rq_argp;
struct diropargs *object = &argp->xdrgen;
nfsstat *resp = rqstp->rq_resp;
struct svc_fh *fhp = &argp->fh;
nfsd_fhandle_to_svc_fh(fhp, &object->dir);
*resp = nfsd_unlink(rqstp, fhp, S_IFDIR,
(char *)object->name.data, object->name.len);
*resp = nfsd_map_status(*resp);
fh_put(fhp);
return rpc_success;
}
static __be32 nfsd_proc_readdir(struct svc_rqst *rqstp)
{
struct readdirargs *argp = rqstp->rq_argp;
struct readdirres_wrapper *resp = rqstp->rq_resp;
loff_t offset = be32_to_cpup((__be32 *)argp->cookie);
struct svc_fh *fhp = &resp->fh;
__be32 status;
nfsd_fhandle_to_svc_fh(fhp, &argp->dir);
trace_nfsd_vfs_readdir(rqstp, fhp, argp->count, offset);
resp->count = argp->count;
status = nfsd_readdir_open(rqstp, fhp, &offset, &resp->iter);
resp->xdrgen.status = nfsd_map_status(status);
return rpc_success;
}
static __be32 nfsd_proc_statfs(struct svc_rqst *rqstp)
{
struct fhandle_wrapper *argp = rqstp->rq_argp;
struct statfsres_wrapper *resp = rqstp->rq_resp;
struct kstatfs *stat = &resp->stats;
struct svc_fh *fhp = &argp->fh;
nfsd_fhandle_to_svc_fh(fhp, &argp->xdrgen);
resp->xdrgen.status = nfsd_statfs(rqstp, fhp, stat,
NFSD_MAY_BYPASS_GSS_ON_ROOT);
if (resp->xdrgen.status == nfs_ok) {
struct info *resinfo = &resp->xdrgen.u.info;
resinfo->tsize = NFS_MAXDATA;
resinfo->bsize = stat->f_bsize;
resinfo->blocks = stat->f_blocks;
resinfo->bfree = stat->f_bfree;
resinfo->bavail = stat->f_bavail;
} else {
resp->xdrgen.status = nfsd_map_status(resp->xdrgen.status);
}
fh_put(fhp);
return rpc_success;
}
#define ST 1 /* status */
#define FH 8 /* filehandle */
#define AT 18 /* attributes */
static const struct svc_procedure nfsd_procedures2[18] = {
[NFSPROC_NULL] = {
.pc_func = nfsd_proc_null,
.pc_decode = xdrgen_svc_decode_void,
.pc_encode = xdrgen_svc_encode_void,
.pc_argsize = XDR_void,
.pc_argzero = 0,
.pc_ressize = 0,
.pc_cachetype = RC_NOCACHE,
.pc_xdrressize = XDR_void,
.pc_name = "NULL",
},
[NFSPROC_GETATTR] = {
.pc_func = nfsd_proc_getattr,
.pc_decode = nfs_svc_decode_fhandle,
.pc_encode = nfs_svc_encode_attrstat,
.pc_argsize = sizeof(struct fhandle_wrapper),
.pc_argzero = 0,
.pc_ressize = sizeof(struct attrstat_wrapper),
.pc_cachetype = RC_NOCACHE,
.pc_xdrressize = NFS2_attrstat_sz,
.pc_name = "GETATTR",
},
[NFSPROC_SETATTR] = {
.pc_func = nfsd_proc_setattr,
.pc_decode = nfs_svc_decode_sattrargs,
.pc_encode = nfs_svc_encode_attrstat,
.pc_argsize = sizeof(struct sattrargs_wrapper),
.pc_argzero = 0,
.pc_ressize = sizeof(struct attrstat_wrapper),
.pc_cachetype = RC_REPLBUFF,
.pc_xdrressize = NFS2_attrstat_sz,
.pc_name = "SETATTR",
},
[NFSPROC_ROOT] = {
.pc_func = nfsd_proc_root,
.pc_decode = xdrgen_svc_decode_void,
.pc_encode = xdrgen_svc_encode_void,
.pc_argsize = XDR_void,
.pc_argzero = 0,
.pc_ressize = 0,
.pc_cachetype = RC_NOCACHE,
.pc_xdrressize = XDR_void,
.pc_name = "ROOT",
},
[NFSPROC_LOOKUP] = {
.pc_func = nfsd_proc_lookup,
.pc_decode = nfs_svc_decode_diropargs,
.pc_encode = nfs_svc_encode_diropres,
.pc_argsize = sizeof(struct diropargs_wrapper),
.pc_argzero = 0,
.pc_ressize = sizeof(struct diropres_wrapper),
.pc_cachetype = RC_NOCACHE,
.pc_xdrressize = NFS2_diropres_sz,
.pc_name = "LOOKUP",
},
[NFSPROC_READLINK] = {
.pc_func = nfsd_proc_readlink,
.pc_decode = nfs_svc_decode_fhandle,
.pc_encode = nfs_svc_encode_readlinkres,
.pc_argsize = sizeof(struct fhandle_wrapper),
.pc_argzero = 0,
.pc_ressize = sizeof(struct readlinkres),
.pc_cachetype = RC_NOCACHE,
.pc_xdrressize = NFS2_readlinkres_sz,
.pc_name = "READLINK",
},
[NFSPROC_READ] = {
.pc_func = nfsd_proc_read,
.pc_decode = nfs_svc_decode_readargs,
.pc_encode = nfs_svc_encode_readres,
.pc_argsize = sizeof(struct readargs_wrapper),
.pc_argzero = 0,
.pc_ressize = sizeof(struct readres),
.pc_cachetype = RC_NOCACHE,
.pc_xdrressize = NFS2_readres_sz,
.pc_name = "READ",
},
[NFSPROC_WRITECACHE] = {
.pc_func = nfsd_proc_writecache,
.pc_decode = xdrgen_svc_decode_void,
.pc_encode = xdrgen_svc_encode_void,
.pc_argsize = XDR_void,
.pc_argzero = 0,
.pc_ressize = 0,
.pc_cachetype = RC_NOCACHE,
.pc_xdrressize = XDR_void,
.pc_name = "WRITECACHE",
},
[NFSPROC_WRITE] = {
.pc_func = nfsd_proc_write,
.pc_decode = nfs_svc_decode_writeargs,
.pc_encode = nfs_svc_encode_attrstat,
.pc_argsize = sizeof(struct writeargs_wrapper),
.pc_argzero = 0,
.pc_ressize = sizeof(struct attrstat_wrapper),
.pc_cachetype = RC_REPLBUFF,
.pc_xdrressize = NFS2_attrstat_sz,
.pc_name = "WRITE",
},
[NFSPROC_CREATE] = {
.pc_func = nfsd_proc_create,
.pc_decode = nfs_svc_decode_createargs,
.pc_encode = nfs_svc_encode_diropres,
.pc_argsize = sizeof(struct createargs_wrapper),
.pc_argzero = 0,
.pc_ressize = sizeof(struct diropres_wrapper),
.pc_cachetype = RC_REPLBUFF,
.pc_xdrressize = NFS2_diropres_sz,
.pc_name = "CREATE",
},
[NFSPROC_REMOVE] = {
.pc_func = nfsd_proc_remove,
.pc_decode = nfs_svc_decode_diropargs,
.pc_encode = nfs_svc_encode_nfsstat,
.pc_argsize = sizeof(struct diropargs_wrapper),
.pc_argzero = 0,
.pc_ressize = sizeof(nfsstat),
.pc_cachetype = RC_REPLSTAT,
.pc_xdrressize = NFS2_nfsstat_sz,
.pc_name = "REMOVE",
},
[NFSPROC_RENAME] = {
.pc_func = nfsd_proc_rename,
.pc_decode = nfs_svc_decode_renameargs,
.pc_encode = nfs_svc_encode_nfsstat,
.pc_argsize = sizeof(struct renameargs_wrapper),
.pc_argzero = 0,
.pc_ressize = sizeof(nfsstat),
.pc_cachetype = RC_REPLSTAT,
.pc_xdrressize = NFS2_nfsstat_sz,
.pc_name = "RENAME",
},
[NFSPROC_LINK] = {
.pc_func = nfsd_proc_link,
.pc_decode = nfs_svc_decode_linkargs,
.pc_encode = nfs_svc_encode_nfsstat,
.pc_argsize = sizeof(struct linkargs_wrapper),
.pc_argzero = 0,
.pc_ressize = sizeof(nfsstat),
.pc_cachetype = RC_REPLSTAT,
.pc_xdrressize = NFS2_nfsstat_sz,
.pc_name = "LINK",
},
[NFSPROC_SYMLINK] = {
.pc_func = nfsd_proc_symlink,
.pc_decode = nfs_svc_decode_symlinkargs,
.pc_encode = nfs_svc_encode_nfsstat,
.pc_argsize = sizeof(struct symlinkargs_wrapper),
.pc_argzero = 0,
.pc_ressize = sizeof(nfsstat),
.pc_cachetype = RC_REPLSTAT,
.pc_xdrressize = NFS2_nfsstat_sz,
.pc_name = "SYMLINK",
},
[NFSPROC_MKDIR] = {
.pc_func = nfsd_proc_mkdir,
.pc_decode = nfs_svc_decode_createargs,
.pc_encode = nfs_svc_encode_diropres,
.pc_argsize = sizeof(struct createargs_wrapper),
.pc_argzero = 0,
.pc_ressize = sizeof(struct diropres_wrapper),
.pc_cachetype = RC_REPLBUFF,
.pc_xdrressize = NFS2_diropres_sz,
.pc_name = "MKDIR",
},
[NFSPROC_RMDIR] = {
.pc_func = nfsd_proc_rmdir,
.pc_decode = nfs_svc_decode_diropargs,
.pc_encode = nfs_svc_encode_nfsstat,
.pc_argsize = sizeof(struct diropargs_wrapper),
.pc_argzero = 0,
.pc_ressize = sizeof(nfsstat),
.pc_cachetype = RC_REPLSTAT,
.pc_xdrressize = NFS2_nfsstat_sz,
.pc_name = "RMDIR",
},
[NFSPROC_READDIR] = {
.pc_func = nfsd_proc_readdir,
.pc_decode = nfs_svc_decode_readdirargs,
.pc_encode = nfs_svc_encode_readdirres,
.pc_release = nfssvc_release_readdirres,
.pc_argsize = sizeof(struct readdirargs),
.pc_argzero = 0,
.pc_ressize = sizeof(struct readdirres_wrapper),
.pc_cachetype = RC_NOCACHE,
.pc_name = "READDIR",
},
[NFSPROC_STATFS] = {
.pc_func = nfsd_proc_statfs,
.pc_decode = nfs_svc_decode_fhandle,
.pc_encode = nfs_svc_encode_statfsres,
.pc_argsize = sizeof(struct fhandle_wrapper),
.pc_argzero = 0,
.pc_ressize = sizeof(struct statfsres_wrapper),
.pc_cachetype = RC_NOCACHE,
.pc_xdrressize = NFS2_statfsres_sz,
.pc_name = "STATFS",
},
};
union nfsd_xdrstore {
struct fhandle_wrapper fhandle;
struct sattrargs_wrapper sattrargs;
struct diropargs_wrapper diropargs;
struct readargs_wrapper readargs;
struct writeargs_wrapper writeargs;
struct createargs_wrapper createargs;
struct renameargs_wrapper renameargs;
struct linkargs_wrapper linkargs;
struct symlinkargs_wrapper symlinkargs;
struct readdirargs readdirargs;
struct attrstat_wrapper attrstat;
struct diropres_wrapper diropres;
struct readlinkres readlinkres;
struct readres readres;
struct readdirres_wrapper readdirres;
struct statfsres_wrapper statfsres;
};
const struct svc_version nfsd_version2 = {
.vs_vers = 2,
.vs_nproc = ARRAY_SIZE(nfsd_procedures2),
.vs_proc = nfsd_procedures2,
.vs_dispatch = nfsd_dispatch,
.vs_xdrsize = sizeof(union nfsd_xdrstore),
}