1/*-
  2 * SPDX-License-Identifier: BSD-3-Clause
  3 *
  4 * Copyright (c) 1989, 1993, 1995
  5 *	The Regents of the University of California.  All rights reserved.
  6 *
  7 * This code is derived from software contributed to Berkeley by
  8 * Rick Macklem at The University of Guelph.
  9 *
 10 * Redistribution and use in source and binary forms, with or without
 11 * modification, are permitted provided that the following conditions
 12 * are met:
 13 * 1. Redistributions of source code must retain the above copyright
 14 *    notice, this list of conditions and the following disclaimer.
 15 * 2. Redistributions in binary form must reproduce the above copyright
 16 *    notice, this list of conditions and the following disclaimer in the
 17 *    documentation and/or other materials provided with the distribution.
 18 * 3. Neither the name of the University nor the names of its contributors
 19 *    may be used to endorse or promote products derived from this software
 20 *    without specific prior written permission.
 21 *
 22 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
 23 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
 24 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
 25 * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
 26 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
 27 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
 28 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
 29 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
 30 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
 31 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
 32 * SUCH DAMAGE.
 33 *
 34 *	@(#)nfs.h	8.4 (Berkeley) 5/1/95
 35 */
 36
 37#ifndef _NFSSERVER_NFS_H_
 38#define _NFSSERVER_NFS_H_
 39
 40#ifdef _KERNEL
 41#include "opt_nfs.h"
 42#endif
 43
 44#include <nfs/nfssvc.h>
 45
 46/*
 47 * Tunable constants for nfs
 48 */
 49
 50#define NFS_TICKINTVL	10		/* Desired time for a tick (msec) */
 51#define NFS_HZ		(hz / nfs_ticks) /* Ticks/sec */
 52#define	NFS_TIMEO	(1 * NFS_HZ)	/* Default timeout = 1 second */
 53#define	NFS_MINTIMEO	(1 * NFS_HZ)	/* Min timeout to use */
 54#define	NFS_MAXTIMEO	(60 * NFS_HZ)	/* Max timeout to backoff to */
 55#define	NFS_MINIDEMTIMEO (5 * NFS_HZ)	/* Min timeout for non-idempotent ops*/
 56#define	NFS_MAXUIDHASH	64		/* Max. # of hashed uid entries/mp */
 57#ifndef NFS_GATHERDELAY
 58#define NFS_GATHERDELAY		10	/* Default write gather delay (msec) */
 59#endif
 60#ifdef _KERNEL
 61#define	DIRBLKSIZ	512		/* XXX we used to use ufs's DIRBLKSIZ */
 62#endif
 63
 64/*
 65 * Oddballs
 66 */
 67#define NFS_SRVMAXDATA(n) \
 68		(((n)->nd_flag & ND_NFSV3) ? (((n)->nd_nam2) ? \
 69		 NFS_MAXDGRAMDATA : NFS_MAXDATA) : NFS_V2MAXDATA)
 70
 71/*
 72 * XXX
 73 * The B_INVAFTERWRITE flag should be set to whatever is required by the
 74 * buffer cache code to say "Invalidate the block after it is written back".
 75 */
 76#define	B_INVAFTERWRITE	B_NOCACHE
 77
 78/*
 79 * The IO_METASYNC flag should be implemented for local filesystems.
 80 * (Until then, it is nothin at all.)
 81 */
 82#ifndef IO_METASYNC
 83#define IO_METASYNC	0
 84#endif
 85
 86/* NFS state flags XXX -Wunused */
 87#define	NFSRV_SNDLOCK		0x01000000  /* Send socket lock */
 88#define	NFSRV_WANTSND		0x02000000  /* Want above */
 89
 90/*
 91 * Structures for the nfssvc(2) syscall.  Not that anyone but nfsd and
 92 * mount_nfs should ever try and use it.
 93 */
 94
 95/*
 96 * Add a socket to monitor for NFS requests.
 97 */
 98struct nfsd_addsock_args {
 99	int	sock;		/* Socket to serve */
100	caddr_t	name;		/* Client addr for connection based sockets */
101	int	namelen;	/* Length of name */
102};
103
104/*
105 * Start processing requests.
106 */
107struct nfsd_nfsd_args {
108	const char *principal;	/* GSS-API service principal name */
109	int	minthreads;	/* minimum service thread count */
110	int	maxthreads;	/* maximum service thread count */
111};
112
113/*
114 * XXX to allow amd to include nfs.h without nfsproto.h
115 */
116#ifdef NFS_NPROCS
117#include <nfsserver/nfsrvstats.h>
118#endif
119
120/*
121 * vfs.nfsrv sysctl(3) identifiers
122 */
123#define NFS_NFSRVSTATS	1		/* struct: struct nfsrvstats */
124#define NFS_NFSPRIVPORT	2		/* int: prohibit nfs to resvports */
125
126#ifdef _KERNEL
127
128extern struct mtx nfsd_mtx;
129#define	NFSD_LOCK_ASSERT()	mtx_assert(&nfsd_mtx, MA_OWNED)
130#define	NFSD_UNLOCK_ASSERT()	mtx_assert(&nfsd_mtx, MA_NOTOWNED)
131#define	NFSD_LOCK_DONTCARE()
132#define	NFSD_LOCK()	mtx_lock(&nfsd_mtx)
133#define	NFSD_UNLOCK()	mtx_unlock(&nfsd_mtx)
134
135#ifdef MALLOC_DECLARE
136MALLOC_DECLARE(M_NFSRVDESC);
137MALLOC_DECLARE(M_NFSD);
138#endif
139
140/* Forward declarations */
141struct nfssvc_sock;
142struct nfsrv_descript;
143struct uio;
144struct vattr;
145struct nameidata;
146
147extern struct callout nfsrv_callout;
148extern struct nfsrvstats nfsrvstats;
149
150extern int	nfsrv_ticks;
151extern int	nfsrvw_procrastinate;
152extern int	nfsrvw_procrastinate_v3;
153extern int 	nfsrv_numnfsd;
154
155/* Various values converted to XDR form. */
156extern u_int32_t nfsrv_nfs_false, nfsrv_nfs_true, nfsrv_nfs_xdrneg1,
157	nfsrv_nfs_prog;
158extern u_int32_t nfsrv_rpc_reply, nfsrv_rpc_msgdenied, nfsrv_rpc_mismatch,
159	nfsrv_rpc_vers;
160extern u_int32_t nfsrv_rpc_auth_unix, nfsrv_rpc_msgaccepted, nfsrv_rpc_call,
161	nfsrv_rpc_autherr;
162
163/* Procedure table data */
164extern const int	nfsrvv2_procid[NFS_NPROCS];
165extern const int	nfsrv_nfsv3_procid[NFS_NPROCS];
166extern int32_t (*nfsrv3_procs[NFS_NPROCS])(struct nfsrv_descript *nd,
167		    struct nfssvc_sock *slp, struct mbuf **mreqp);
168
169/*
170 * A list of nfssvc_sock structures is maintained with all the sockets
171 * that require service by the nfsd.
172 */
173#ifndef NFS_WDELAYHASHSIZ
174#define	NFS_WDELAYHASHSIZ 16	/* and with this */
175#endif
176#define	NWDELAYHASH(sock, f) \
177	(&(sock)->ns_wdelayhashtbl[(*((u_int32_t *)(f))) % NFS_WDELAYHASHSIZ])
178
179/*
180 * This structure is used by the server for describing each request.
181 */
182struct nfsrv_descript {
183	struct mbuf		*nd_mrep;	/* Request mbuf list */
184	struct mbuf		*nd_md;		/* Current dissect mbuf */
185	struct mbuf		*nd_mreq;	/* Reply mbuf list */
186	struct sockaddr		*nd_nam;	/* and socket addr */
187	struct sockaddr		*nd_nam2;	/* return socket addr */
188	caddr_t			nd_dpos;	/* Current dissect pos */
189	u_int32_t		nd_procnum;	/* RPC # */
190	int			nd_stable;	/* storage type */
191	int			nd_flag;	/* nd_flag */
192	int			nd_repstat;	/* Reply status */
193	fhandle_t		nd_fh;		/* File handle */
194	struct ucred		*nd_cr;		/* Credentials */
195	int			nd_credflavor;	/* Security flavor */
196};
197
198/* Bits for "nd_flag" */
199#define ND_NFSV3	0x08
200
201/*
202 * Defines for WebNFS
203 */
204
205#define WEBNFS_ESC_CHAR		'%'
206#define WEBNFS_SPECCHAR_START	0x80
207
208#define WEBNFS_NATIVE_CHAR	0x80
209/*
210 * ..
211 * Possibly more here in the future.
212 */
213
214/*
215 * Macro for converting escape characters in WebNFS pathnames.
216 * Should really be in libkern.
217 */
218
219#define HEXTOC(c) \
220	((c) >= 'a' ? ((c) - ('a' - 10)) : \
221	    ((c) >= 'A' ? ((c) - ('A' - 10)) : ((c) - '0')))
222#define HEXSTRTOI(p) \
223	((HEXTOC(p[0]) << 4) + HEXTOC(p[1]))
224
225#ifdef NFS_DEBUG
226
227extern int nfs_debug;
228#define NFS_DEBUG_ASYNCIO	1 /* asynchronous i/o */
229#define NFS_DEBUG_WG		2 /* server write gathering */
230#define NFS_DEBUG_RC		4 /* server request caching */
231
232#define NFS_DPF(cat, args)					\
233	do {							\
234		if (nfs_debug & NFS_DEBUG_##cat) printf args;	\
235	} while (0)
236
237#else
238
239#define NFS_DPF(cat, args)
240
241#endif
242
243/*
244 * The following flags can be passed to nfsrv_fhtovp() function.
245 */
246/* Leave file system busy on success. */
247#define	NFSRV_FLAG_BUSY		0x01
248
249struct mbuf *nfs_rephead(int, struct nfsrv_descript *, int, struct mbuf **,
250	    caddr_t *);
251void	nfsm_srvfattr(struct nfsrv_descript *, struct vattr *,
252	    struct nfs_fattr *);
253void	nfsm_srvwcc(struct nfsrv_descript *, int, struct vattr *, int,
254	    struct vattr *, struct mbuf **, char **);
255void	nfsm_srvpostopattr(struct nfsrv_descript *, int, struct vattr *,
256	    struct mbuf **, char **);
257int	nfs_namei(struct nameidata *, struct nfsrv_descript *, fhandle_t *,
258	    int, struct nfssvc_sock *, struct sockaddr *, struct mbuf **,
259	    caddr_t *, struct vnode **, int, struct vattr *, int *, int);
260void	nfsm_adj(struct mbuf *, int, int);
261int	nfsm_mbuftouio(struct mbuf **, struct uio *, int, caddr_t *);
262void	nfsrv_init(int);
263int	nfsrv_errmap(struct nfsrv_descript *, int);
264void	nfsrvw_sort(gid_t *, int);
265
266int	nfsrv3_access(struct nfsrv_descript *nfsd, struct nfssvc_sock *slp,
267	    struct mbuf **mrq);
268int	nfsrv_commit(struct nfsrv_descript *nfsd, struct nfssvc_sock *slp,
269	    struct mbuf **mrq);
270int	nfsrv_create(struct nfsrv_descript *nfsd, struct nfssvc_sock *slp,
271	    struct mbuf **mrq);
272int	nfsrv_fhtovp(fhandle_t *, int, struct vnode **,
273	    struct nfsrv_descript *, struct nfssvc_sock *, struct sockaddr *,
274	    int *);
275int	nfsrv_setpublicfs(struct mount *, struct netexport *,
276	    struct export_args *);
277int	nfs_ispublicfh(fhandle_t *);
278int	nfsrv_fsinfo(struct nfsrv_descript *nfsd, struct nfssvc_sock *slp,
279	    struct mbuf **mrq);
280int	nfsrv_getattr(struct nfsrv_descript *nfsd, struct nfssvc_sock *slp,
281	    struct mbuf **mrq);
282int	nfsrv_link(struct nfsrv_descript *nfsd, struct nfssvc_sock *slp,
283	    struct mbuf **mrq);
284int	nfsrv_lookup(struct nfsrv_descript *nfsd, struct nfssvc_sock *slp,
285	    struct mbuf **mrq);
286int	nfsrv_mkdir(struct nfsrv_descript *nfsd, struct nfssvc_sock *slp,
287	    struct mbuf **mrq);
288int	nfsrv_mknod(struct nfsrv_descript *nfsd, struct nfssvc_sock *slp,
289	    struct mbuf **mrq);
290int	nfsrv_noop(struct nfsrv_descript *nfsd, struct nfssvc_sock *slp,
291	    struct mbuf **mrq);
292int	nfsrv_null(struct nfsrv_descript *nfsd, struct nfssvc_sock *slp,
293	    struct mbuf **mrq);
294int	nfsrv_pathconf(struct nfsrv_descript *nfsd, struct nfssvc_sock *slp,
295	    struct mbuf **mrq);
296int	nfsrv_read(struct nfsrv_descript *nfsd, struct nfssvc_sock *slp,
297	    struct mbuf **mrq);
298int	nfsrv_readdir(struct nfsrv_descript *nfsd, struct nfssvc_sock *slp,
299	    struct mbuf **mrq);
300int	nfsrv_readdirplus(struct nfsrv_descript *nfsd, struct nfssvc_sock *slp,
301	    struct mbuf **mrq);
302int	nfsrv_readlink(struct nfsrv_descript *nfsd, struct nfssvc_sock *slp,
303	    struct mbuf **mrq);
304int	nfsrv_remove(struct nfsrv_descript *nfsd, struct nfssvc_sock *slp,
305	    struct mbuf **mrq);
306int	nfsrv_rename(struct nfsrv_descript *nfsd, struct nfssvc_sock *slp,
307	    struct mbuf **mrq);
308int	nfsrv_rmdir(struct nfsrv_descript *nfsd, struct nfssvc_sock *slp,
309	    struct mbuf **mrq);
310int	nfsrv_setattr(struct nfsrv_descript *nfsd, struct nfssvc_sock *slp,
311	    struct mbuf **mrq);
312int	nfsrv_statfs(struct nfsrv_descript *nfsd, struct nfssvc_sock *slp,
313	    struct mbuf **mrq);
314int	nfsrv_symlink(struct nfsrv_descript *nfsd, struct nfssvc_sock *slp,
315	    struct mbuf **mrq);
316int	nfsrv_write(struct nfsrv_descript *nfsd, struct nfssvc_sock *slp,
317	    struct mbuf **mrq);
318/*
319 * #ifdef _SYS_SYSPROTO_H_ so that it is only defined when sysproto.h
320 * has been included, so that "struct nfssvc_args" is defined.
321 */
322#ifdef _SYS_SYSPROTO_H_
323int nfssvc_nfsserver(struct thread *, struct nfssvc_args *);
324#endif
325#endif	/* _KERNEL */
326
327#endif