// SPDX-License-Identifier: GPL-2.0
/*
* XDR support for nfsd/protocol version 3.
*
* Copyright (C) 1995, 1996, 1997 Olaf Kirch <okir@monad.swb.de>
*
* 2003-08-09 Jamie Lokier: Use htonl() for nanoseconds, not htons()!
*/
#include <linux/namei.h>
#include <linux/sunrpc/svc_xprt.h>
#include "xdr3.h"
#include "auth.h"
#include "netns.h"
#include "vfs.h"
/*
* Force construction of an empty post-op attr
*/
static const struct svc_fh nfs3svc_null_fh = {
.fh_no_wcc = true,
};
/*
* time_delta. {1, 0} means the server is accurate only
* to the nearest second.
*/
static const struct timespec64 nfs3svc_time_delta = {
.tv_sec = 1,
.tv_nsec = 0,
};
/*
* Mapping of S_IF* types to NFS file types
*/
static const u32 nfs3_ftypes[] = {
NF3NON, NF3FIFO, NF3CHR, NF3BAD,
NF3DIR, NF3BAD, NF3BLK, NF3BAD,
NF3REG, NF3BAD, NF3LNK, NF3BAD,
NF3SOCK, NF3BAD, NF3LNK, NF3BAD,
};
/*
* Basic NFSv3 data types (RFC 1813 Sections 2.5 and 2.6)
*/
static __be32 *
encode_nfstime3(__be32 *p, const struct timespec64 *time)
{
*p++ = cpu_to_be32((u32)time->tv_sec);
*p++ = cpu_to_be32(time->tv_nsec);
return p;
}
static bool
svcxdr_decode_nfstime3(struct xdr_stream *xdr, struct timespec64 *timep)
{
__be32 *p;
p = xdr_inline_decode(xdr, XDR_UNIT * 2);
if (!p)
return false;
timep->tv_sec = be32_to_cpup(p++);
timep->tv_nsec = be32_to_cpup(p);
return true;
}
/**
* svcxdr_decode_nfs_fh3 - Decode an NFSv3 file handle
* @xdr: XDR stream positioned at an undecoded NFSv3 FH
* @fhp: OUT: filled-in server file handle
*
* Return values:
* %false: The encoded file handle was not valid
* %true: @fhp has been initialized
*/
bool
svcxdr_decode_nfs_fh3(struct xdr_stream *xdr, struct svc_fh *fhp)
{
__be32 *p;
u32 size;
if (xdr_stream_decode_u32(xdr, &size) < 0)
return false;
if (size == 0 || size > NFS3_FHSIZE)
return false;
p = xdr_inline_decode(xdr, size);
if (!p)
return false;
fh_init(fhp, NFS3_FHSIZE);
fhp->fh_handle.fh_size = size;
memcpy(&fhp->fh_handle.fh_raw, p, size);
return true;
}
/**
* svcxdr_encode_nfsstat3 - Encode an NFSv3 status code
* @xdr: XDR stream
* @status: status value to encode
*
* Return values:
* %false: Send buffer space was exhausted
* %true: Success
*/
bool
svcxdr_encode_nfsstat3(struct xdr_stream *xdr, __be32 status