xref: /freebsd/sys/fs/nfs/nfs.h (revision 2a58b312)
1 /*-
2  * SPDX-License-Identifier: BSD-3-Clause
3  *
4  * Copyright (c) 1989, 1993
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  * $FreeBSD$
35  */
36 
37 #ifndef _NFS_NFS_H_
38 #define	_NFS_NFS_H_
39 /*
40  * Tunable constants for nfs
41  */
42 
43 #define	NFS_MAXIOVEC	34
44 #define	NFS_TICKINTVL	500		/* Desired time for a tick (msec) */
45 #define	NFS_HZ		(hz / nfscl_ticks) /* Ticks/sec */
46 #define	NFS_TIMEO	(1 * NFS_HZ)	/* Default timeout = 1 second */
47 #define	NFS_MINTIMEO	(1 * NFS_HZ)	/* Min timeout to use */
48 #define	NFS_MAXTIMEO	(60 * NFS_HZ)	/* Max timeout to backoff to */
49 #define	NFS_TCPTIMEO	300		/* TCP timeout */
50 #define	NFS_MAXRCVTIMEO	60		/* 1 minute in seconds */
51 #define	NFS_MINIDEMTIMEO (5 * NFS_HZ)	/* Min timeout for non-idempotent ops*/
52 #define	NFS_MAXREXMIT	100		/* Stop counting after this many */
53 #define	NFSV4_CALLBACKTIMEO 800		/* Timeout in msec */
54 #define	NFSV4_CALLBACKRETRY 5		/* Number of retries before failure */
55 #define	NFSV4_SLOTS	64		/* Number of slots, fore channel */
56 #define	NFSV4_CBSLOTS	8		/* Number of slots, back channel */
57 #define	NFSV4_CBRETRYCNT 4		/* # of CBRecall retries upon err */
58 #define	NFSV4_UPCALLTIMEO (15 * NFS_HZ)	/* Timeout in ticks for upcalls */
59 					/* to gssd or nfsuserd */
60 #define	NFSV4_UPCALLRETRY 4		/* Number of retries before failure */
61 #define	NFS_MAXWINDOW	1024		/* Max number of outstanding requests */
62 #define	NFS_RETRANS	10		/* Num of retrans for soft mounts */
63 #define	NFS_RETRANS_TCP	2		/* Num of retrans for TCP soft mounts */
64 #define	NFS_MAXGRPS	16		/* Max. size of groups list */
65 #define	NFS_TRYLATERDEL	15		/* Maximum delay timeout (sec) */
66 #ifndef NFS_REMOVETIMEO
67 #define	NFS_REMOVETIMEO 15  /* # sec to wait for delegret in local syscall */
68 #endif
69 #ifndef NFS_MINATTRTIMO
70 #define	NFS_MINATTRTIMO 5		/* Attribute cache timeout in sec */
71 #endif
72 #ifndef NFS_MAXATTRTIMO
73 #define	NFS_MAXATTRTIMO 60
74 #endif
75 #define	NFS_WSIZE	8192		/* Def. write data size <= 8192 */
76 #define	NFS_RSIZE	8192		/* Def. read data size <= 8192 */
77 #define	NFS_READDIRSIZE	8192		/* Def. readdir size */
78 #define	NFS_DEFRAHEAD	1		/* Def. read ahead # blocks */
79 #define	NFS_MAXRAHEAD	16		/* Max. read ahead # blocks */
80 #define	NFS_MAXASYNCDAEMON 	64	/* Max. number async_daemons runnable */
81 #define	NFS_MAXUIDHASH	64		/* Max. # of hashed uid entries/mp */
82 #ifndef	NFSRV_LEASE
83 #define	NFSRV_LEASE		120	/* Lease time in seconds for V4 */
84 #endif					/* assigned to nfsrv_lease */
85 #ifndef NFSRV_STALELEASE
86 #define	NFSRV_STALELEASE	(5 * nfsrv_lease)
87 #endif
88 #ifndef NFSRV_MOULDYLEASE
89 #define	NFSRV_MOULDYLEASE	604800	/* One week (in sec) */
90 #endif
91 #ifndef NFSCLIENTHASHSIZE
92 #define	NFSCLIENTHASHSIZE	20	/* Size of server client hash table */
93 #endif
94 #ifndef NFSLOCKHASHSIZE
95 #define	NFSLOCKHASHSIZE		20	/* Size of server nfslock hash table */
96 #endif
97 #ifndef NFSSESSIONHASHSIZE
98 #define	NFSSESSIONHASHSIZE	20	/* Size of server session hash table */
99 #endif
100 #define	NFSSTATEHASHSIZE	10	/* Size of server stateid hash table */
101 #define	NFSLAYOUTHIGHWATER	1000000	/* Upper limit for # of layouts */
102 #ifndef	NFSCLDELEGHIGHWATER
103 #define	NFSCLDELEGHIGHWATER	10000	/* limit for client delegations */
104 #endif
105 #ifndef	NFSCLLAYOUTHIGHWATER
106 #define	NFSCLLAYOUTHIGHWATER	10000	/* limit for client pNFS layouts */
107 #endif
108 #ifndef NFSNOOPEN			/* Inactive open owner (sec) */
109 #define	NFSNOOPEN		120
110 #endif
111 #define	NFSRV_LEASEDELTA	15	/* # of seconds to delay beyond lease */
112 #define	NFS_IDMAXSIZE		4	/* max sizeof (in_addr_t) */
113 #ifndef NFSRVCACHE_UDPTIMEOUT
114 #define	NFSRVCACHE_UDPTIMEOUT	30	/* # of sec to hold cached rpcs(udp) */
115 #endif
116 #ifndef NFSRVCACHE_UDPHIGHWATER
117 #define	NFSRVCACHE_UDPHIGHWATER	500	/* Max # of udp cache entries */
118 #endif
119 #ifndef NFSRVCACHE_TCPTIMEOUT
120 #define	NFSRVCACHE_TCPTIMEOUT	(3600*12) /*#of sec to hold cached rpcs(tcp) */
121 #endif
122 #ifndef	NFSRVCACHE_FLOODLEVEL
123 #define	NFSRVCACHE_FLOODLEVEL	16384	/* Very high water mark for cache */
124 #endif
125 #ifndef	NFSRV_CLIENTHIGHWATER
126 #define	NFSRV_CLIENTHIGHWATER	1000
127 #endif
128 #ifndef	NFSRV_MAXDUMPLIST
129 #define	NFSRV_MAXDUMPLIST	10000
130 #endif
131 #ifndef NFS_ACCESSCACHESIZE
132 #define	NFS_ACCESSCACHESIZE	8
133 #endif
134 #define	NFSV4_CBPORT	7745		/* Callback port for testing */
135 
136 /*
137  * This macro defines the high water mark for issuing V4 delegations.
138  * (It is currently set at a conservative 20% of nfsrv_v4statelimit. This
139  *  may want to increase when clients can make more effective use of
140  *  delegations.)
141  */
142 #define	NFSRV_V4DELEGLIMIT(c) (((c) * 5) > nfsrv_v4statelimit)
143 
144 #define	NFS_READDIRBLKSIZ	DIRBLKSIZ	/* Minimal nm_readdirsize */
145 
146 /*
147  * The NFSv4 RFCs do not define an upper limit on the length of Owner and
148  * OwnerGroup strings.  Since FreeBSD handles a group name > 1024bytes in
149  * length, set a generous sanity limit of 10Kbytes.
150  */
151 #define	NFSV4_MAXOWNERGROUPLEN	(10 * 1024)
152 
153 /*
154  * Oddballs
155  */
156 #define	NFS_CMPFH(n, f, s) 						\
157     ((n)->n_fhp->nfh_len == (s) && !NFSBCMP((n)->n_fhp->nfh_fh, (caddr_t)(f), (s)))
158 #define	NFSRV_CMPFH(nf, ns, f, s) 					\
159 	((ns) == (s) && !NFSBCMP((caddr_t)(nf), (caddr_t)(f), (s)))
160 #define	NFS_CMPTIME(t1, t2) 						\
161 	((t1).tv_sec == (t2).tv_sec && (t1).tv_nsec == (t2).tv_nsec)
162 #define	NFS_SETTIME(t) do { 						\
163 	(t).tv_sec = time.tv_sec; (t).tv_nsec = 1000 * time.tv_usec; } while (0)
164 #define	NFS_SRVMAXDATA(n) 						\
165 		(((n)->nd_flag & (ND_NFSV3 | ND_NFSV4)) ? 		\
166 		 nfs_srvmaxio : NFS_V2MAXDATA)
167 #define	NFS64BITSSET	0xffffffffffffffffull
168 #define	NFS64BITSMINUS1	0xfffffffffffffffeull
169 
170 /*
171  * Structures for the nfssvc(2) syscall. Not that anyone but nfsd, mount_nfs
172  * and nfsloaduser should ever try and use it.
173  */
174 struct nfsd_addsock_args {
175 	int	sock;		/* Socket to serve */
176 	caddr_t	name;		/* Client addr for connection based sockets */
177 	int	namelen;	/* Length of name */
178 };
179 
180 /*
181  * nfsd argument for new krpc.
182  * (New version supports pNFS, indicated by NFSSVC_NEWSTRUCT flag.)
183  */
184 struct nfsd_nfsd_args {
185 	const char *principal;	/* GSS-API service principal name */
186 	int	minthreads;	/* minimum service thread count */
187 	int	maxthreads;	/* maximum service thread count */
188 	int	version;	/* Allow multiple variants */
189 	char	*addr;		/* pNFS DS addresses */
190 	int	addrlen;	/* Length of addrs */
191 	char	*dnshost;	/* DNS names for DS addresses */
192 	int	dnshostlen;	/* Length of DNS names */
193 	char	*dspath;	/* DS Mount path on MDS */
194 	int	dspathlen;	/* Length of DS Mount path on MDS */
195 	char	*mdspath;	/* MDS mount for DS path on MDS */
196 	int	mdspathlen;	/* Length of MDS mount for DS path on MDS */
197 	int	mirrorcnt;	/* Number of mirrors to create on DSs */
198 };
199 
200 /*
201  * NFSDEV_MAXMIRRORS - Maximum level of mirroring for a DS.
202  * (Most will only put files on two DSs, but this setting allows up to 4.)
203  * NFSDEV_MAXVERS - maximum number of NFS versions supported by Flex File.
204  */
205 #define	NFSDEV_MAXMIRRORS	4
206 #define	NFSDEV_MAXVERS		4
207 
208 struct nfsd_pnfsd_args {
209 	int	op;		/* Which pNFSd op to perform. */
210 	char	*mdspath;	/* Path of MDS file. */
211 	char	*dspath;	/* Path of recovered DS mounted on dir. */
212 	char	*curdspath;	/* Path of current DS mounted on dir. */
213 };
214 
215 #define	PNFSDOP_DELDSSERVER	1
216 #define	PNFSDOP_COPYMR		2
217 #define	PNFSDOP_FORCEDELDS	3
218 
219 /* Old version. */
220 struct nfsd_nfsd_oargs {
221 	const char *principal;	/* GSS-API service principal name */
222 	int	minthreads;	/* minimum service thread count */
223 	int	maxthreads;	/* maximum service thread count */
224 };
225 
226 /*
227  * Arguments for use by the callback daemon.
228  */
229 struct nfsd_nfscbd_args {
230 	const char *principal;	/* GSS-API service principal name */
231 };
232 
233 struct nfscbd_args {
234 	int	sock;		/* Socket to serve */
235 	caddr_t	name;		/* Client addr for connection based sockets */
236 	int	namelen;	/* Length of name */
237 	u_short	port;		/* Port# for callbacks */
238 };
239 
240 struct nfsd_idargs {
241 	int		nid_flag;	/* Flags (see below) */
242 	uid_t		nid_uid;	/* user/group id */
243 	gid_t		nid_gid;
244 	int		nid_usermax;	/* Upper bound on user name cache */
245 	int		nid_usertimeout;/* User name timeout (minutes) */
246 	u_char		*nid_name;	/* Name */
247 	int		nid_namelen;	/* and its length */
248 	gid_t		*nid_grps;	/* and the list */
249 	int		nid_ngroup;	/* Size of groups list */
250 };
251 
252 struct nfsd_oidargs {
253 	int		nid_flag;	/* Flags (see below) */
254 	uid_t		nid_uid;	/* user/group id */
255 	gid_t		nid_gid;
256 	int		nid_usermax;	/* Upper bound on user name cache */
257 	int		nid_usertimeout;/* User name timeout (minutes) */
258 	u_char		*nid_name;	/* Name */
259 	int		nid_namelen;	/* and its length */
260 };
261 
262 struct nfsuserd_args {
263 	sa_family_t	nuserd_family;	/* Address family to use */
264 	u_short		nuserd_port;	/* Port# */
265 };
266 
267 struct nfsd_clid {
268 	int		nclid_idlen;	/* Length of client id */
269 	u_char		nclid_id[NFSV4_OPAQUELIMIT]; /* and name */
270 };
271 
272 struct nfsd_dumplist {
273 	int		ndl_size;	/* Number of elements */
274 	void		*ndl_list;	/* and the list of elements */
275 };
276 
277 struct nfsd_dumpclients {
278 	u_int32_t	ndcl_flags;		/* LCL_xxx flags */
279 	u_int32_t	ndcl_nopenowners;	/* Number of openowners */
280 	u_int32_t	ndcl_nopens;		/* and opens */
281 	u_int32_t	ndcl_nlockowners;	/* and of lockowners */
282 	u_int32_t	ndcl_nlocks;		/* and of locks */
283 	u_int32_t	ndcl_ndelegs;		/* and of delegations */
284 	u_int32_t	ndcl_nolddelegs;	/* and old delegations */
285 	sa_family_t	ndcl_addrfam;		/* Callback address */
286 	union {
287 		struct in_addr sin_addr;
288 		struct in6_addr sin6_addr;
289 	} ndcl_cbaddr;
290 	struct nfsd_clid ndcl_clid;	/* and client id */
291 };
292 
293 struct nfsd_dumplocklist {
294 	char		*ndllck_fname;	/* File Name */
295 	int		ndllck_size;	/* Number of elements */
296 	void		*ndllck_list;	/* and the list of elements */
297 };
298 
299 struct nfsd_dumplocks {
300 	u_int32_t	ndlck_flags;		/* state flags NFSLCK_xxx */
301 	nfsv4stateid_t	ndlck_stateid;		/* stateid */
302 	u_int64_t	ndlck_first;		/* lock byte range */
303 	u_int64_t	ndlck_end;
304 	struct nfsd_clid ndlck_owner;		/* Owner of open/lock */
305 	sa_family_t	ndlck_addrfam;		/* Callback address */
306 	union {
307 		struct in_addr sin_addr;
308 		struct in6_addr sin6_addr;
309 	} ndlck_cbaddr;
310 	struct nfsd_clid ndlck_clid;	/* and client id */
311 };
312 
313 /*
314  * Structure for referral information.
315  */
316 struct nfsreferral {
317 	u_char		*nfr_srvlist;	/* List of servers */
318 	int		nfr_srvcnt;	/* number of servers */
319 	vnode_t		nfr_vp;	/* vnode for referral */
320 	uint64_t	nfr_dfileno;	/* assigned dir inode# */
321 };
322 
323 /*
324  * Flags for lc_flags and opsflags for nfsrv_getclient().
325  */
326 #define	LCL_NEEDSCONFIRM	0x00000001
327 #define	LCL_DONTCLEAN		0x00000002
328 #define	LCL_WAKEUPWANTED	0x00000004
329 #define	LCL_TCPCALLBACK		0x00000008
330 #define	LCL_CALLBACKSON		0x00000010
331 #define	LCL_INDEXNOTOK		0x00000020
332 #define	LCL_STAMPEDSTABLE	0x00000040
333 #define	LCL_EXPIREIT		0x00000080
334 #define	LCL_CBDOWN		0x00000100
335 #define	LCL_KERBV		0x00000400
336 #define	LCL_NAME		0x00000800
337 #define	LCL_NEEDSCBNULL		0x00001000
338 #define	LCL_GSSINTEGRITY	0x00002000
339 #define	LCL_GSSPRIVACY		0x00004000
340 #define	LCL_ADMINREVOKED	0x00008000
341 #define	LCL_RECLAIMCOMPLETE	0x00010000
342 #define	LCL_NFSV41		0x00020000
343 #define	LCL_DONEBINDCONN	0x00040000
344 #define	LCL_RECLAIMONEFS	0x00080000
345 #define	LCL_NFSV42		0x00100000
346 #define	LCL_TLSCB		0x00200000
347 #define	LCL_MACHCRED		0x00400000
348 
349 #define	LCL_GSS		LCL_KERBV	/* Or of all mechs */
350 
351 /*
352  * Bits for flags in nfslock and nfsstate.
353  * The access, deny, NFSLCK_READ and NFSLCK_WRITE bits must be defined as
354  * below, in the correct order, so the shifts work for tests.
355  */
356 #define	NFSLCK_READACCESS	0x00000001
357 #define	NFSLCK_WRITEACCESS	0x00000002
358 #define	NFSLCK_ACCESSBITS	(NFSLCK_READACCESS | NFSLCK_WRITEACCESS)
359 #define	NFSLCK_SHIFT		2
360 #define	NFSLCK_READDENY		0x00000004
361 #define	NFSLCK_WRITEDENY	0x00000008
362 #define	NFSLCK_DENYBITS		(NFSLCK_READDENY | NFSLCK_WRITEDENY)
363 #define	NFSLCK_SHAREBITS 						\
364     (NFSLCK_READACCESS|NFSLCK_WRITEACCESS|NFSLCK_READDENY|NFSLCK_WRITEDENY)
365 #define	NFSLCK_LOCKSHIFT	4
366 #define	NFSLCK_READ		0x00000010
367 #define	NFSLCK_WRITE		0x00000020
368 #define	NFSLCK_BLOCKING		0x00000040
369 #define	NFSLCK_RECLAIM		0x00000080
370 #define	NFSLCK_OPENTOLOCK	0x00000100
371 #define	NFSLCK_TEST		0x00000200
372 #define	NFSLCK_LOCK		0x00000400
373 #define	NFSLCK_UNLOCK		0x00000800
374 #define	NFSLCK_OPEN		0x00001000
375 #define	NFSLCK_CLOSE		0x00002000
376 #define	NFSLCK_CHECK		0x00004000
377 #define	NFSLCK_RELEASE		0x00008000
378 #define	NFSLCK_NEEDSCONFIRM	0x00010000
379 #define	NFSLCK_CONFIRM		0x00020000
380 #define	NFSLCK_DOWNGRADE	0x00040000
381 #define	NFSLCK_DELEGREAD	0x00080000
382 #define	NFSLCK_DELEGWRITE	0x00100000
383 #define	NFSLCK_DELEGCUR		0x00200000
384 #define	NFSLCK_DELEGPREV	0x00400000
385 #define	NFSLCK_OLDDELEG		0x00800000
386 #define	NFSLCK_DELEGRECALL	0x01000000
387 #define	NFSLCK_SETATTR		0x02000000
388 #define	NFSLCK_DELEGPURGE	0x04000000
389 #define	NFSLCK_DELEGRETURN	0x08000000
390 #define	NFSLCK_WANTWDELEG	0x10000000
391 #define	NFSLCK_WANTRDELEG	0x20000000
392 #define	NFSLCK_WANTNODELEG	0x40000000
393 #define	NFSLCK_WANTBITS							\
394     (NFSLCK_WANTWDELEG | NFSLCK_WANTRDELEG | NFSLCK_WANTNODELEG)
395 
396 /* And bits for nid_flag */
397 #define	NFSID_INITIALIZE	0x0001
398 #define	NFSID_ADDUID		0x0002
399 #define	NFSID_DELUID		0x0004
400 #define	NFSID_ADDUSERNAME	0x0008
401 #define	NFSID_DELUSERNAME	0x0010
402 #define	NFSID_ADDGID		0x0020
403 #define	NFSID_DELGID		0x0040
404 #define	NFSID_ADDGROUPNAME	0x0080
405 #define	NFSID_DELGROUPNAME	0x0100
406 
407 /*
408  * fs.nfs sysctl(3) identifiers
409  */
410 #define	NFS_NFSSTATS	1		/* struct: struct nfsstats */
411 
412 /*
413  * Here is the definition of the attribute bits array and macros that
414  * manipulate it.
415  * THE MACROS MUST BE MANUALLY MODIFIED IF NFSATTRBIT_MAXWORDS CHANGES!!
416  * It is (NFSATTRBIT_MAX + 31) / 32.
417  */
418 #define	NFSATTRBIT_MAXWORDS	3
419 
420 typedef struct {
421 	u_int32_t bits[NFSATTRBIT_MAXWORDS];
422 } nfsattrbit_t;
423 
424 #define	NFSZERO_ATTRBIT(b) do {						\
425 	(b)->bits[0] = 0;						\
426 	(b)->bits[1] = 0;						\
427 	(b)->bits[2] = 0;						\
428 } while (0)
429 
430 #define	NFSSET_ATTRBIT(t, f) do {					\
431 	(t)->bits[0] = (f)->bits[0];			 		\
432 	(t)->bits[1] = (f)->bits[1];					\
433 	(t)->bits[2] = (f)->bits[2];					\
434 } while (0)
435 
436 #define	NFSSETSUPP_ATTRBIT(b, n) do { 					\
437 	(b)->bits[0] = NFSATTRBIT_SUPP0; 				\
438 	(b)->bits[1] = (NFSATTRBIT_SUPP1 | NFSATTRBIT_SUPPSETONLY1);	\
439 	(b)->bits[2] = (NFSATTRBIT_SUPP2 | NFSATTRBIT_SUPPSETONLY2);	\
440 	if (((n)->nd_flag & ND_NFSV41) == 0) {				\
441 		(b)->bits[1] &= ~NFSATTRBIT_NFSV41_1;			\
442 		(b)->bits[2] &= ~NFSATTRBIT_NFSV41_2;			\
443 	}								\
444 	if (((n)->nd_flag & ND_NFSV42) == 0)				\
445 		(b)->bits[2] &= ~NFSATTRBIT_NFSV42_2;			\
446 } while (0)
447 
448 #define	NFSISSET_ATTRBIT(b, p)	((b)->bits[(p) / 32] & (1 << ((p) % 32)))
449 #define	NFSSETBIT_ATTRBIT(b, p)	((b)->bits[(p) / 32] |= (1 << ((p) % 32)))
450 #define	NFSCLRBIT_ATTRBIT(b, p)	((b)->bits[(p) / 32] &= ~(1 << ((p) % 32)))
451 
452 #define	NFSCLRALL_ATTRBIT(b, a)	do { 					\
453 	(b)->bits[0] &= ~((a)->bits[0]);	 			\
454 	(b)->bits[1] &= ~((a)->bits[1]);	 			\
455 	(b)->bits[2] &= ~((a)->bits[2]);				\
456 } while (0)
457 
458 #define	NFSCLRNOT_ATTRBIT(b, a)	do { 					\
459 	(b)->bits[0] &= ((a)->bits[0]);		 			\
460 	(b)->bits[1] &= ((a)->bits[1]);		 			\
461 	(b)->bits[2] &= ((a)->bits[2]);		 			\
462 } while (0)
463 
464 #define	NFSCLRNOTFILLABLE_ATTRBIT(b, n) do { 				\
465 	(b)->bits[0] &= NFSATTRBIT_SUPP0;	 			\
466 	(b)->bits[1] &= NFSATTRBIT_SUPP1;				\
467 	(b)->bits[2] &= NFSATTRBIT_SUPP2;				\
468 	if (((n)->nd_flag & ND_NFSV41) == 0) {				\
469 		(b)->bits[1] &= ~NFSATTRBIT_NFSV41_1;			\
470 		(b)->bits[2] &= ~NFSATTRBIT_NFSV41_2;			\
471 	}								\
472 	if (((n)->nd_flag & ND_NFSV42) == 0)				\
473 		(b)->bits[2] &= ~NFSATTRBIT_NFSV42_2;			\
474 } while (0)
475 
476 #define	NFSCLRNOTSETABLE_ATTRBIT(b, n) do { 				\
477 	(b)->bits[0] &= NFSATTRBIT_SETABLE0;	 			\
478 	(b)->bits[1] &= NFSATTRBIT_SETABLE1;				\
479 	(b)->bits[2] &= NFSATTRBIT_SETABLE2;				\
480 	if (((n)->nd_flag & ND_NFSV41) == 0)				\
481 		(b)->bits[2] &= ~NFSATTRBIT_NFSV41_2;			\
482 	if (((n)->nd_flag & ND_NFSV42) == 0)				\
483 		(b)->bits[2] &= ~NFSATTRBIT_NFSV42_2;			\
484 } while (0)
485 
486 #define	NFSNONZERO_ATTRBIT(b)	((b)->bits[0] || (b)->bits[1] || (b)->bits[2])
487 #define	NFSEQUAL_ATTRBIT(b, p)	((b)->bits[0] == (p)->bits[0] &&	\
488 	(b)->bits[1] == (p)->bits[1] && (b)->bits[2] == (p)->bits[2])
489 
490 #define	NFSGETATTR_ATTRBIT(b) do { 					\
491 	(b)->bits[0] = NFSATTRBIT_GETATTR0;	 			\
492 	(b)->bits[1] = NFSATTRBIT_GETATTR1;				\
493 	(b)->bits[2] = NFSATTRBIT_GETATTR2;				\
494 } while (0)
495 
496 #define	NFSWCCATTR_ATTRBIT(b) do { 					\
497 	(b)->bits[0] = NFSATTRBIT_WCCATTR0;	 			\
498 	(b)->bits[1] = NFSATTRBIT_WCCATTR1;				\
499 	(b)->bits[2] = NFSATTRBIT_WCCATTR2;				\
500 } while (0)
501 
502 #define	NFSWRITEGETATTR_ATTRBIT(b) do { 				\
503 	(b)->bits[0] = NFSATTRBIT_WRITEGETATTR0;			\
504 	(b)->bits[1] = NFSATTRBIT_WRITEGETATTR1;			\
505 	(b)->bits[2] = NFSATTRBIT_WRITEGETATTR2;			\
506 } while (0)
507 
508 #define	NFSCBGETATTR_ATTRBIT(b, c) do { 				\
509 	(c)->bits[0] = ((b)->bits[0] & NFSATTRBIT_CBGETATTR0);		\
510 	(c)->bits[1] = ((b)->bits[1] & NFSATTRBIT_CBGETATTR1);		\
511 	(c)->bits[2] = ((b)->bits[2] & NFSATTRBIT_CBGETATTR2);		\
512 } while (0)
513 
514 #define	NFSPATHCONF_GETATTRBIT(b) do { 					\
515 	(b)->bits[0] = NFSGETATTRBIT_PATHCONF0;		 		\
516 	(b)->bits[1] = NFSGETATTRBIT_PATHCONF1;				\
517 	(b)->bits[2] = NFSGETATTRBIT_PATHCONF2;				\
518 } while (0)
519 
520 #define	NFSSTATFS_GETATTRBIT(b)	do { 					\
521 	(b)->bits[0] = NFSGETATTRBIT_STATFS0;	 			\
522 	(b)->bits[1] = NFSGETATTRBIT_STATFS1;				\
523 	(b)->bits[2] = NFSGETATTRBIT_STATFS2;				\
524 } while (0)
525 
526 #define	NFSROOTFS_GETATTRBIT(b)	do { 					\
527 	(b)->bits[0] = NFSGETATTRBIT_STATFS0 | NFSATTRBIT_GETATTR0 |	\
528 	    NFSATTRBM_LEASETIME;					\
529 	(b)->bits[1] = NFSGETATTRBIT_STATFS1 | NFSATTRBIT_GETATTR1;	\
530 	(b)->bits[2] = NFSGETATTRBIT_STATFS2 | NFSATTRBIT_GETATTR2;	\
531 } while (0)
532 
533 #define	NFSISSETSTATFS_ATTRBIT(b) 					\
534 		(((b)->bits[0] & NFSATTRBIT_STATFS0) || 		\
535 		 ((b)->bits[1] & NFSATTRBIT_STATFS1) ||			\
536 		 ((b)->bits[2] & NFSATTRBIT_STATFS2))
537 
538 #define	NFSCLRSTATFS_ATTRBIT(b)	do { 					\
539 	(b)->bits[0] &= ~NFSATTRBIT_STATFS0;	 			\
540 	(b)->bits[1] &= ~NFSATTRBIT_STATFS1;				\
541 	(b)->bits[2] &= ~NFSATTRBIT_STATFS2;				\
542 } while (0)
543 
544 #define	NFSREADDIRPLUS_ATTRBIT(b) do { 					\
545 	(b)->bits[0] = NFSATTRBIT_READDIRPLUS0;		 		\
546 	(b)->bits[1] = NFSATTRBIT_READDIRPLUS1;				\
547 	(b)->bits[2] = NFSATTRBIT_READDIRPLUS2;				\
548 } while (0)
549 
550 #define	NFSREFERRAL_ATTRBIT(b) do { 					\
551 	(b)->bits[0] = NFSATTRBIT_REFERRAL0;		 		\
552 	(b)->bits[1] = NFSATTRBIT_REFERRAL1;				\
553 	(b)->bits[2] = NFSATTRBIT_REFERRAL2;				\
554 } while (0)
555 
556 /*
557  * Here is the definition of the operation bits array and macros that
558  * manipulate it.
559  * THE MACROS MUST BE MANUALLY MODIFIED IF NFSOPBIT_MAXWORDS CHANGES!!
560  * It is (NFSV42_NOPS + 31) / 32.
561  */
562 #define	NFSOPBIT_MAXWORDS	3
563 
564 typedef struct {
565 	uint32_t bits[NFSOPBIT_MAXWORDS];
566 } nfsopbit_t;
567 
568 #define	NFSZERO_OPBIT(b) do {						\
569 	(b)->bits[0] = 0;						\
570 	(b)->bits[1] = 0;						\
571 	(b)->bits[2] = 0;						\
572 } while (0)
573 
574 #define	NFSSET_OPBIT(t, f) do {						\
575 	(t)->bits[0] = (f)->bits[0];			 		\
576 	(t)->bits[1] = (f)->bits[1];					\
577 	(t)->bits[2] = (f)->bits[2];					\
578 } while (0)
579 
580 #define	NFSISSET_OPBIT(b, p)	((b)->bits[(p) / 32] & (1 << ((p) % 32)))
581 #define	NFSSETBIT_OPBIT(b, p)	((b)->bits[(p) / 32] |= (1 << ((p) % 32)))
582 #define	NFSCLRBIT_OPBIT(b, p)	((b)->bits[(p) / 32] &= ~(1 << ((p) % 32)))
583 
584 /*
585  * Store uid, gid creds that were used when the stateid was acquired.
586  * The RPC layer allows NFS_MAXGRPS + 1 groups to go out on the wire,
587  * so that's how many gets stored here.
588  */
589 struct nfscred {
590 	uid_t 		nfsc_uid;
591 	gid_t		nfsc_groups[NFS_MAXGRPS + 1];
592 	int		nfsc_ngroups;
593 };
594 
595 /*
596  * Constants that define the file handle for the V4 root directory.
597  * (The FSID must never be used by other file systems that are exported.)
598  */
599 #define	NFSV4ROOT_FSID0		((int32_t) -1)
600 #define	NFSV4ROOT_FSID1		((int32_t) -1)
601 #define	NFSV4ROOT_REFERRAL	((int32_t) -2)
602 #define	NFSV4ROOT_INO		2	/* It's traditional */
603 #define	NFSV4ROOT_GEN		1
604 
605 /*
606  * The set of signals the interrupt an I/O in progress for NFSMNT_INT mounts.
607  * What should be in this set is open to debate, but I believe that since
608  * I/O system calls on ufs are never interrupted by signals the set should
609  * be minimal. My reasoning is that many current programs that use signals
610  * such as SIGALRM will not expect file I/O system calls to be interrupted
611  * by them and break.
612  */
613 #if defined(_KERNEL) || defined(KERNEL)
614 
615 struct uio; struct buf; struct vattr; struct nameidata;	/* XXX */
616 
617 /*
618  * Socket errors ignored for connectionless sockets?
619  * For now, ignore them all
620  */
621 #define	NFSIGNORE_SOERROR(s, e) 					\
622 		((e) != EINTR && (e) != ERESTART && (e) != EWOULDBLOCK && \
623 		((s) & PR_CONNREQUIRED) == 0)
624 
625 /*
626  * This structure holds socket information for a connection. Used by the
627  * client and the server for callbacks.
628  */
629 struct nfssockreq {
630 	NFSSOCKADDR_T	nr_nam;
631 	int		nr_sotype;
632 	int		nr_soproto;
633 	int		nr_soflags;
634 	struct ucred	*nr_cred;
635 	int		nr_lock;
636 	NFSMUTEX_T	nr_mtx;
637 	u_int32_t	nr_prog;
638 	u_int32_t	nr_vers;
639 	struct __rpc_client *nr_client;
640 	AUTH		*nr_auth;
641 };
642 
643 /*
644  * And associated nr_lock bits.
645  */
646 #define	NFSR_SNDLOCK		0x01
647 #define	NFSR_WANTSND		0x02
648 #define	NFSR_RCVLOCK		0x04
649 #define	NFSR_WANTRCV		0x08
650 #define	NFSR_RESERVEDPORT	0x10
651 #define	NFSR_LOCALHOST		0x20
652 
653 /*
654  * Queue head for nfsreq's
655  */
656 TAILQ_HEAD(nfsreqhead, nfsreq);
657 
658 /* This is the only nfsreq R_xxx flag still used. */
659 #define	R_DONTRECOVER	0x00000100	/* don't initiate recovery when this
660 					   rpc gets a stale state reply */
661 
662 /*
663  * Network address hash list element
664  */
665 union nethostaddr {
666 	struct in_addr	had_inet;
667 	struct in6_addr had_inet6;
668 };
669 
670 /*
671  * Structure of list of mechanisms.
672  */
673 struct nfsgss_mechlist {
674 	int	len;
675 	const u_char	*str;
676 	int	totlen;
677 };
678 #define	KERBV_MECH	0	/* position in list */
679 
680 /*
681  * This structure is used by the server for describing each request.
682  */
683 struct nfsrv_descript {
684 	struct mbuf		*nd_mrep;	/* Request mbuf list */
685 	struct mbuf		*nd_md;		/* Current dissect mbuf */
686 	struct mbuf		*nd_mreq;	/* Reply mbuf list */
687 	struct mbuf		*nd_mb;		/* Current build mbuf */
688 	NFSSOCKADDR_T		nd_nam;		/* and socket addr */
689 	NFSSOCKADDR_T		nd_nam2;	/* return socket addr */
690 	caddr_t			nd_dpos;	/* Current dissect pos */
691 	caddr_t			nd_bpos;	/* Current build pos */
692 	u_int64_t		nd_flag;	/* nd_flag */
693 	u_int16_t		nd_procnum;	/* RPC # */
694 	u_int32_t		nd_repstat;	/* Reply status */
695 	int			*nd_errp;	/* Pointer to ret status */
696 	u_int32_t		nd_retxid;	/* Reply xid */
697 	struct nfsrvcache	*nd_rp;		/* Assoc. cache entry */
698 	fhandle_t		nd_fh;		/* File handle */
699 	struct ucred		*nd_cred;	/* Credentials */
700 	uid_t			nd_saveduid;	/* Saved uid */
701 	u_int64_t		nd_sockref;	/* Rcv socket ref# */
702 	u_int64_t		nd_compref;	/* Compound RPC ref# */
703 	time_t			nd_tcpconntime;	/* Time TCP connection est. */
704 	nfsquad_t		nd_clientid;	/* Implied clientid */
705 	int			nd_gssnamelen;	/* principal name length */
706 	char			*nd_gssname;	/* principal name */
707 	uint32_t		*nd_slotseq;	/* ptr to slot seq# in req */
708 	uint8_t			nd_sessionid[NFSX_V4SESSIONID];	/* Session id */
709 	uint32_t		nd_slotid;	/* Slotid for this RPC */
710 	SVCXPRT			*nd_xprt;	/* Server RPC handle */
711 	uint32_t		*nd_sequence;	/* Sequence Op. ptr */
712 	nfsv4stateid_t		nd_curstateid;	/* Current StateID */
713 	nfsv4stateid_t		nd_savedcurstateid; /* Saved Current StateID */
714 	uint32_t		nd_maxreq;	/* Max. request (session). */
715 	uint32_t		nd_maxresp;	/* Max. reply (session). */
716 	int			nd_bextpg;	/* Current ext_pgs page */
717 	int			nd_bextpgsiz;	/* Bytes left in page */
718 	int			nd_maxextsiz;	/* Max ext_pgs mbuf size */
719 	nfsopbit_t		nd_allowops;	/* Allowed ops ND_MACHCRED */
720 };
721 
722 #define	nd_princlen	nd_gssnamelen
723 #define	nd_principal	nd_gssname
724 
725 /* Bits for "nd_flag" */
726 #define	ND_DONTSAVEREPLY 	0x00000001
727 #define	ND_SAVEREPLY		0x00000002
728 #define	ND_NFSV2		0x00000004
729 #define	ND_NFSV3		0x00000008
730 #define	ND_NFSV4		0x00000010
731 #define	ND_KERBV		0x00000020
732 #define	ND_GSSINTEGRITY		0x00000040
733 #define	ND_GSSPRIVACY		0x00000080
734 #define	ND_WINDOWVERF		0x00000100
735 #define	ND_GSSINITREPLY		0x00000200
736 #define	ND_STREAMSOCK		0x00000400
737 #define	ND_PUBLOOKUP		0x00000800
738 #define	ND_USEGSSNAME		0x00001000
739 #define	ND_SAMETCPCONN		0x00002000
740 #define	ND_IMPLIEDCLID		0x00004000
741 #define	ND_NOMOREDATA		0x00008000
742 #define	ND_V4WCCATTR		0x00010000
743 #define	ND_NFSCB		0x00020000
744 #define	ND_AUTHNONE		0x00040000
745 #define	ND_EXAUTHSYS		0x00080000
746 #define	ND_EXGSS		0x00100000
747 #define	ND_EXGSSINTEGRITY	0x00200000
748 #define	ND_EXGSSPRIVACY		0x00400000
749 #define	ND_INCRSEQID		0x00800000
750 #define	ND_NFSCL		0x01000000
751 #define	ND_NFSV41		0x02000000
752 #define	ND_HASSEQUENCE		0x04000000
753 #define	ND_CACHETHIS		0x08000000
754 #define	ND_LASTOP		0x10000000
755 #define	ND_LOOPBADSESS		0x20000000
756 #define	ND_DSSERVER		0x40000000
757 #define	ND_CURSTATEID		0x80000000
758 #define	ND_SAVEDCURSTATEID	0x100000000
759 #define	ND_HASSLOTID		0x200000000
760 #define	ND_NFSV42		0x400000000
761 #define	ND_EXTPG		0x800000000
762 #define	ND_TLS			0x1000000000
763 #define	ND_TLSCERT		0x2000000000
764 #define	ND_TLSCERTUSER		0x4000000000
765 #define	ND_EXTLS		0x8000000000
766 #define	ND_EXTLSCERT		0x10000000000
767 #define	ND_EXTLSCERTUSER	0x20000000000
768 #define	ND_ERELOOKUP		0x40000000000
769 #define	ND_MACHCRED		0x80000000000
770 
771 /*
772  * ND_GSS should be the "or" of all GSS type authentications.
773  */
774 #define	ND_GSS		(ND_KERBV)
775 
776 struct nfsv4_opflag {
777 	int	retfh;
778 	int	needscfh;
779 	int	savereply;
780 	int	modifyfs;
781 	int	lktype;
782 	int	needsseq;
783 	int	loopbadsess;
784 };
785 
786 /*
787  * Flags used to indicate what to do w.r.t. seqid checking.
788  */
789 #define	NFSRVSEQID_FIRST	0x01
790 #define	NFSRVSEQID_LAST		0x02
791 #define	NFSRVSEQID_OPEN		0x04
792 
793 /*
794  * assign a doubly linked list to a new head
795  * and prepend one list into another.
796  */
797 #define	LIST_NEWHEAD(nhead, ohead, field) do { 				\
798 	if (((nhead)->lh_first = (ohead)->lh_first) != NULL) 		\
799 		(ohead)->lh_first->field.le_prev = &(nhead)->lh_first; 	\
800 	(ohead)->lh_first = NULL; 					\
801     } while (0)
802 
803 #define	LIST_PREPEND(head, phead, lelm, field) do {			\
804 	if ((head)->lh_first != NULL) {					\
805 		(lelm)->field.le_next = (head)->lh_first;		\
806 		(lelm)->field.le_next->field.le_prev =			\
807 		    &(lelm)->field.le_next;				\
808 	}								\
809 	(head)->lh_first = (phead)->lh_first;				\
810 	(head)->lh_first->field.le_prev = &(head)->lh_first;		\
811     } while (0)
812 
813 /*
814  * File handle structure for client. Malloc'd to the correct length with
815  * malloc type M_NFSFH.
816  */
817 struct nfsfh {
818 	u_int16_t	nfh_len;	/* Length of file handle */
819 	u_int8_t	nfh_fh[1];	/* and the file handle */
820 };
821 
822 /*
823  * File handle structure for server. The NFSRV_MAXFH constant is
824  * set in nfsdport.h. I use a 32bit length, so that alignment is
825  * preserved.
826  */
827 struct nfsrvfh {
828 	u_int32_t	nfsrvfh_len;
829 	u_int8_t	nfsrvfh_data[NFSRV_MAXFH];
830 };
831 
832 /*
833  * This structure is used for sleep locks on the NFSv4 nfsd threads and
834  * NFSv4 client data structures.
835  */
836 struct nfsv4lock {
837 	u_int32_t	nfslock_usecnt;
838 	u_int8_t	nfslock_lock;
839 };
840 #define	NFSV4LOCK_LOCK		0x01
841 #define	NFSV4LOCK_LOCKWANTED	0x02
842 #define	NFSV4LOCK_WANTED	0x04
843 
844 /*
845  * Values for the override argument for nfsvno_accchk().
846  */
847 #define	NFSACCCHK_NOOVERRIDE		0
848 #define	NFSACCCHK_ALLOWROOT		1
849 #define	NFSACCCHK_ALLOWOWNER		2
850 
851 /*
852  * and values for the vpislocked argument for nfsvno_accchk().
853  */
854 #define	NFSACCCHK_VPNOTLOCKED		0
855 #define	NFSACCCHK_VPISLOCKED		1
856 
857 /*
858  * Slot for the NFSv4.1 Sequence Op.
859  */
860 struct nfsslot {
861 	int		nfssl_inprog;
862 	uint32_t	nfssl_seq;
863 	struct mbuf	*nfssl_reply;
864 };
865 
866 /* Enumerated type for nfsuserd state. */
867 typedef enum { NOTRUNNING=0, STARTSTOP=1, RUNNING=2 } nfsuserd_state;
868 
869 #endif	/* _KERNEL */
870 
871 #endif	/* _NFS_NFS_H */
872