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