#include <linux/filelock.h>
#include "vfs.h"
#include "nfserr.h"
#include "xdr.h"
#include "nfs2xdr_gen.h"
#include "auth.h"
#include "trace.h"
#define NFBAD (7)
#define NFSOCK (6)
static const u32 nfs_ftypes[] = {
NFNON, NFCHR, NFCHR, NFBAD,
NFDIR, NFBAD, NFBLK, NFBAD,
NFREG, NFBAD, NFLNK, NFBAD,
NFSOCK, NFBAD, NFLNK, NFBAD,
};
bool
svcxdr_encode_stat(struct xdr_stream *xdr, __be32 status)
{
__be32 *p;
p = xdr_reserve_space(xdr, sizeof(status));
if (!p)
return false;
*p = status;
return true;
}
bool
svcxdr_decode_fhandle(struct xdr_stream *xdr, struct svc_fh *fhp)
{
__be32 *p;
p = xdr_inline_decode(xdr, NFS_FHSIZE);
if (!p)
return false;
fh_init(fhp, NFS_FHSIZE);
memcpy(&fhp->fh_handle.fh_raw, p, NFS_FHSIZE);
fhp->fh_handle.fh_size = NFS_FHSIZE;
return true;
}
static __be32 *
encode_timeval(__be32 *p, const struct timespec64 *time)
{
*p++ = cpu_to_be32((u32)time->tv_sec);
if (time->tv_nsec)
*p++ = cpu_to_be32(time->tv_nsec / NSEC_PER_USEC);
else
*p++ = xdr_zero;
return p;
}
bool
svcxdr_encode_fattr(struct svc_rqst *rqstp, struct xdr_stream *xdr,
const struct svc_fh *fhp, const struct kstat *stat)
{
struct user_namespace *userns = nfsd_user_namespace(rqstp);
struct dentry *dentry = fhp->fh_dentry;
int type = stat->mode & S_IFMT;
struct timespec64 time;
__be32 *p;
u32 fsid;
p = xdr_reserve_space(xdr, XDR_UNIT * 17);
if (!p)
return false;
*p++ = cpu_to_be32(nfs_ftypes[type >> 12]);
*p++ = cpu_to_be32((u32)stat->mode);
*p++ = cpu_to_be32((u32)stat->nlink);
*p++ = cpu_to_be32((u32)from_kuid_munged(userns, stat->uid));
*p++ = cpu_to_be32((u32)from_kgid_munged(userns, stat->gid));
if (S_ISLNK(type) && stat->size > NFS_MAXPATHLEN)
*p++ = cpu_to_be32(NFS_MAXPATHLEN);
else
*p++ = cpu_to_be32((u32) stat->size);
*p++ = cpu_to_be32((u32) stat->blksize);
if (S_ISCHR(type) || S_ISBLK(type))
*p++ = cpu_to_be32(new_encode_dev(stat->rdev));
else
*p++ = cpu_to_be32(0xffffffff);
*p++ = cpu_to_be32((u32)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);
break;
}
*p++ = cpu_to_be32(fsid);
*p++ = cpu_to_be32((u32)stat->ino);
p = encode_timeval(p, &stat->atime);
time = stat->mtime;
lease_get_mtime(d_inode(dentry), &time);
p = encode_timeval(p, &time);
encode_timeval(p, &stat->ctime);
return true;
}
bool
nfssvc_decode_fhandleargs(struct svc_rqst *rqstp, struct xdr_stream *xdr)
{
struct nfsd_fhandle *args = rqstp->rq_argp;
return svcxdr_decode_fhandle(xdr, &args->fh);
}
bool
nfssvc_encode_attrstatres(struct svc_rqst *rqstp, struct xdr_stream *xdr)
{
struct nfsd_attrstat *resp = rqstp->rq_resp;
if (!svcxdr_encode_stat(xdr, resp->status))
return false;
switch (resp->status) {
case nfs_ok:
if (!svcxdr_encode_fattr(rqstp, xdr, &resp->fh, &resp->stat))
return false;
break;
}
return true;
}
void nfssvc_encode_nfscookie(struct xdr_stream *xdr, unsigned int pos,
u32 cookie)
{
__be32 wire_cookie = cpu_to_be32(cookie);
if (!pos)
return;
write_bytes_to_xdr_buf(xdr->buf, pos, &wire_cookie, XDR_UNIT);
}
bool nfs2_readdirok_encode_begin(struct xdrgen_aggregate_cursor *c)
{
struct svc_rqst *rqstp = c->ctx;
struct readdirres_wrapper *resp = rqstp->rq_resp;
struct xdr_stream *xdr = c->xdr;
int bytes_left;
bytes_left = xdr->buf->buflen - xdr->buf->len - XDR_UNIT * 2;
if (bytes_left < 0)
bytes_left = 0;
resp->space_left = min_t(u32, bytes_left,
clamp(resp->count, (u32)(XDR_UNIT * 2),
(u32)PAGE_SIZE) - XDR_UNIT * 2);
resp->cookie_offset = 0;
return true;
}
bool nfs2_readdirok_encode(struct xdrgen_aggregate_cursor *c,
struct entry *out)
{
struct svc_rqst *rqstp = c->ctx;
struct readdirres_wrapper *resp = rqstp->rq_resp;
struct xdr_stream *xdr = c->xdr;
struct buffered_dirent *de;
int namlen;
u32 need;
if (c->index) {
xdr_commit_encode(xdr);
resp->cookie_offset = xdr->buf->len - XDR_UNIT;
}
de = nfsd_readdir_next(&resp->iter);
if (!de)
return false;
nfssvc_encode_nfscookie(xdr, resp->cookie_offset, (u32)de->offset);
namlen = min_t(int, de->namlen, NFS_MAXNAMLEN);
need = XDR_UNIT * (4 + XDR_QUADLEN(namlen));
if (need > resp->space_left)
return false;
resp->space_left -= need;
out->fileid = (u32)de->ino;
out->name.len = namlen;
out->name.data = (unsigned char *)de->name;
memset(out->cookie, 0, sizeof(out->cookie));
trace_nfsd_dirent(resp->iter.fhp, de->ino, de->name, namlen);
return true;
}
bool nfs2_readdirok_encode_end(struct xdrgen_aggregate_cursor *c, bool ok)
{
struct svc_rqst *rqstp = c->ctx;
struct readdirres_wrapper *resp = rqstp->rq_resp;
struct xdr_stream *xdr = c->xdr;
if (ok)
nfssvc_encode_nfscookie(xdr, resp->cookie_offset,
(u32)resp->iter.offset);
resp->xdrgen.u.readdirok.eof = resp->iter.eof;
rqstp->rq_next_page = xdr->page_ptr + 1;
if (xdr->p == xdr->end)
rqstp->rq_next_page++;
return true;
}
bool nfs2_readdirok_decode_begin(struct xdrgen_aggregate_cursor *c)
{
return false;
}
bool nfs2_readdirok_decode(struct xdrgen_aggregate_cursor *c,
const struct entry *in)
{
return false;
}
bool nfs2_readdirok_decode_end(struct xdrgen_aggregate_cursor *c, bool ok)
{
return false;
}
void nfssvc_release_attrstat(struct svc_rqst *rqstp)
{
struct nfsd_attrstat *resp = rqstp->rq_resp;
fh_put(&resp->fh);
}
void nfssvc_release_readdirres(struct svc_rqst *rqstp)
{
struct readdirres_wrapper *resp = rqstp->rq_resp;
nfsd_readdir_close(&resp->iter);
fh_put(&resp->fh);
}