xref: /freebsd/sys/fs/nfs/nfsport.h (revision ebacd8013fe5f7fdf9f6a5b286f6680dd2891036)
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_NFSPORT_H_
38 #define	_NFS_NFSPORT_H_
39 
40 /*
41  * In general, I'm not fond of #includes in .h files, but this seems
42  * to be the cleanest way to handle #include files for the ports.
43  */
44 #ifdef _KERNEL
45 #include <sys/unistd.h>
46 #include <sys/param.h>
47 #include <sys/systm.h>
48 #include <sys/conf.h>
49 #include <sys/dirent.h>
50 #include <sys/domain.h>
51 #include <sys/fcntl.h>
52 #include <sys/file.h>
53 #include <sys/filedesc.h>
54 #include <sys/jail.h>
55 #include <sys/kernel.h>
56 #include <sys/lockf.h>
57 #include <sys/malloc.h>
58 #include <sys/mbuf.h>
59 #include <sys/mount.h>
60 #include <sys/mutex.h>
61 #include <sys/namei.h>
62 #include <sys/proc.h>
63 #include <sys/protosw.h>
64 #include <sys/reboot.h>
65 #include <sys/resourcevar.h>
66 #include <sys/signalvar.h>
67 #include <sys/socket.h>
68 #include <sys/socketvar.h>
69 #include <sys/stat.h>
70 #include <sys/syslog.h>
71 #include <sys/sysproto.h>
72 #include <sys/time.h>
73 #include <sys/uio.h>
74 #include <sys/vnode.h>
75 #include <sys/bio.h>
76 #include <sys/buf.h>
77 #include <sys/acl.h>
78 #include <sys/module.h>
79 #include <sys/sysent.h>
80 #include <sys/syscall.h>
81 #include <sys/priv.h>
82 #include <sys/kthread.h>
83 #include <sys/syscallsubr.h>
84 #include <net/if.h>
85 #include <net/if_var.h>
86 #include <net/radix.h>
87 #include <net/route.h>
88 #include <net/if_dl.h>
89 #include <netinet/in.h>
90 #include <netinet/in_pcb.h>
91 #include <netinet/in_systm.h>
92 #include <netinet/in_var.h>
93 #include <netinet/ip.h>
94 #include <netinet/ip_var.h>
95 #include <netinet/tcp.h>
96 #include <netinet/tcp_fsm.h>
97 #include <netinet/tcp_seq.h>
98 #include <netinet/tcp_timer.h>
99 #include <netinet/tcp_var.h>
100 #include <machine/in_cksum.h>
101 #include <sys/md5.h>
102 #include <rpc/rpc.h>
103 #include <rpc/rpcsec_gss.h>
104 
105 #include <ufs/ufs/dir.h>
106 #include <ufs/ufs/quota.h>
107 #include <ufs/ufs/inode.h>
108 #include <ufs/ufs/extattr.h>
109 #include <ufs/ufs/ufsmount.h>
110 #include <vm/uma.h>
111 #include <vm/vm.h>
112 #include <vm/pmap.h>
113 #include <vm/vm_extern.h>
114 #include <vm/vm_object.h>
115 #include <vm/vm_page.h>
116 #include <vm/vm_pageout.h>
117 #include <vm/vm_param.h>
118 #include <nfs/nfssvc.h>
119 #include "opt_nfs.h"
120 #include "opt_ufs.h"
121 
122 /*
123  * These types must be defined before the nfs includes.
124  */
125 #define	NFSSOCKADDR_T	struct sockaddr *
126 #define	NFSPROC_T	struct thread
127 #define	NFSDEV_T	dev_t
128 #define	NFSSVCARGS	nfssvc_args
129 #define	NFSACL_T	struct acl
130 
131 /*
132  * These should be defined as the types used for the corresponding VOP's
133  * argument type.
134  */
135 #define	NFS_ACCESS_ARGS		struct vop_access_args
136 #define	NFS_OPEN_ARGS		struct vop_open_args
137 #define	NFS_GETATTR_ARGS	struct vop_getattr_args
138 #define	NFS_LOOKUP_ARGS		struct vop_lookup_args
139 #define	NFS_READDIR_ARGS	struct vop_readdir_args
140 
141 /*
142  * Allocate mbufs. Must succeed and never set the mbuf ptr to NULL.
143  */
144 #define	NFSMGET(m)	do { 					\
145 		MGET((m), M_WAITOK, MT_DATA); 			\
146 		while ((m) == NULL ) { 				\
147 			(void) nfs_catnap(PZERO, 0, "nfsmget");	\
148 			MGET((m), M_WAITOK, MT_DATA); 		\
149 		} 						\
150 	} while (0)
151 #define	NFSMGETHDR(m)	do { 					\
152 		MGETHDR((m), M_WAITOK, MT_DATA);		\
153 		while ((m) == NULL ) { 				\
154 			(void) nfs_catnap(PZERO, 0, "nfsmget");	\
155 			MGETHDR((m), M_WAITOK, MT_DATA); 	\
156 		} 						\
157 	} while (0)
158 #define	NFSMCLGET(m, w)	do { 					\
159 		MGET((m), M_WAITOK, MT_DATA); 			\
160 		while ((m) == NULL ) { 				\
161 			(void) nfs_catnap(PZERO, 0, "nfsmget");	\
162 			MGET((m), M_WAITOK, MT_DATA); 		\
163 		} 						\
164 		MCLGET((m), (w));				\
165 	} while (0)
166 #define	NFSMCLGETHDR(m, w) do { 				\
167 		MGETHDR((m), M_WAITOK, MT_DATA);		\
168 		while ((m) == NULL ) { 				\
169 			(void) nfs_catnap(PZERO, 0, "nfsmget");	\
170 			MGETHDR((m), M_WAITOK, MT_DATA); 	\
171 		} 						\
172 	} while (0)
173 
174 /*
175  * Client side constant for size of a lockowner name.
176  */
177 #define	NFSV4CL_LOCKNAMELEN	12
178 
179 /*
180  * Type for a mutex lock.
181  */
182 #define	NFSMUTEX_T		struct mtx
183 
184 /* Just define the NFSD_VNETxxx() macros as VNETxxx() macros. */
185 #define	NFSD_VNET_NAME(n)		VNET_NAME(n)
186 #define	NFSD_VNET_DECLARE(t, n)		VNET_DECLARE(t, n)
187 #define	NFSD_VNET_DEFINE(t, n)		VNET_DEFINE(t, n)
188 #define	NFSD_VNET_DEFINE_STATIC(t, n)	VNET_DEFINE_STATIC(t, n)
189 #define	NFSD_VNET(n)			VNET(n)
190 
191 #define	CTLFLAG_NFSD_VNET		CTLFLAG_VNET
192 
193 #define	NFSD_CURVNET_SET(n)		CURVNET_SET(n)
194 #define	NFSD_CURVNET_SET_QUIET(n)	CURVNET_SET_QUIET(n)
195 #define	NFSD_CURVNET_RESTORE()		CURVNET_RESTORE()
196 #define	NFSD_TD_TO_VNET(n)		TD_TO_VNET(n)
197 
198 #endif	/* _KERNEL */
199 
200 /*
201  * NFSv4 Operation numbers.
202  */
203 #define	NFSV4OP_ACCESS		3
204 #define	NFSV4OP_CLOSE		4
205 #define	NFSV4OP_COMMIT		5
206 #define	NFSV4OP_CREATE		6
207 #define	NFSV4OP_DELEGPURGE	7
208 #define	NFSV4OP_DELEGRETURN	8
209 #define	NFSV4OP_GETATTR		9
210 #define	NFSV4OP_GETFH		10
211 #define	NFSV4OP_LINK		11
212 #define	NFSV4OP_LOCK		12
213 #define	NFSV4OP_LOCKT		13
214 #define	NFSV4OP_LOCKU		14
215 #define	NFSV4OP_LOOKUP		15
216 #define	NFSV4OP_LOOKUPP		16
217 #define	NFSV4OP_NVERIFY		17
218 #define	NFSV4OP_OPEN		18
219 #define	NFSV4OP_OPENATTR	19
220 #define	NFSV4OP_OPENCONFIRM	20
221 #define	NFSV4OP_OPENDOWNGRADE	21
222 #define	NFSV4OP_PUTFH		22
223 #define	NFSV4OP_PUTPUBFH	23
224 #define	NFSV4OP_PUTROOTFH	24
225 #define	NFSV4OP_READ		25
226 #define	NFSV4OP_READDIR		26
227 #define	NFSV4OP_READLINK	27
228 #define	NFSV4OP_REMOVE		28
229 #define	NFSV4OP_RENAME		29
230 #define	NFSV4OP_RENEW		30
231 #define	NFSV4OP_RESTOREFH	31
232 #define	NFSV4OP_SAVEFH		32
233 #define	NFSV4OP_SECINFO		33
234 #define	NFSV4OP_SETATTR		34
235 #define	NFSV4OP_SETCLIENTID	35
236 #define	NFSV4OP_SETCLIENTIDCFRM	36
237 #define	NFSV4OP_VERIFY		37
238 #define	NFSV4OP_WRITE		38
239 #define	NFSV4OP_RELEASELCKOWN	39
240 
241 /*
242  * Must be one greater than the last Operation#.
243  */
244 #define	NFSV4OP_NOPS		40
245 
246 /*
247  * Additional Ops for NFSv4.1.
248  */
249 #define	NFSV4OP_BACKCHANNELCTL	40
250 #define	NFSV4OP_BINDCONNTOSESS	41
251 #define	NFSV4OP_EXCHANGEID	42
252 #define	NFSV4OP_CREATESESSION	43
253 #define	NFSV4OP_DESTROYSESSION	44
254 #define	NFSV4OP_FREESTATEID	45
255 #define	NFSV4OP_GETDIRDELEG	46
256 #define	NFSV4OP_GETDEVINFO	47
257 #define	NFSV4OP_GETDEVLIST	48
258 #define	NFSV4OP_LAYOUTCOMMIT	49
259 #define	NFSV4OP_LAYOUTGET	50
260 #define	NFSV4OP_LAYOUTRETURN	51
261 #define	NFSV4OP_SECINFONONAME	52
262 #define	NFSV4OP_SEQUENCE	53
263 #define	NFSV4OP_SETSSV		54
264 #define	NFSV4OP_TESTSTATEID	55
265 #define	NFSV4OP_WANTDELEG	56
266 #define	NFSV4OP_DESTROYCLIENTID	57
267 #define	NFSV4OP_RECLAIMCOMPL	58
268 
269 /*
270  * Must be one more than last op#.
271  */
272 #define	NFSV41_NOPS		59
273 
274 /*
275  * Additional operations for NFSv4.2.
276  */
277 #define	NFSV4OP_ALLOCATE	59
278 #define	NFSV4OP_COPY		60
279 #define	NFSV4OP_COPYNOTIFY	61
280 #define	NFSV4OP_DEALLOCATE	62
281 #define	NFSV4OP_IOADVISE	63
282 #define	NFSV4OP_LAYOUTERROR	64
283 #define	NFSV4OP_LAYOUTSTATS	65
284 #define	NFSV4OP_OFFLOADCANCEL	66
285 #define	NFSV4OP_OFFLOADSTATUS	67
286 #define	NFSV4OP_READPLUS	68
287 #define	NFSV4OP_SEEK		69
288 #define	NFSV4OP_WRITESAME	70
289 #define	NFSV4OP_CLONE		71
290 
291 /* One greater than the last Operation # defined in RFC-7862. */
292 #define	NFSV42_PURENOPS		72
293 
294 /* and the optional Extended attribute operations (RFC-8276). */
295 #define	NFSV4OP_GETXATTR	72
296 #define	NFSV4OP_SETXATTR	73
297 #define	NFSV4OP_LISTXATTRS	74
298 #define	NFSV4OP_REMOVEXATTR	75
299 
300 /*
301  * Must be one more than the last NFSv4.2 op#.
302  */
303 #define	NFSV42_NOPS		76
304 
305 /* Quirky case if the illegal op code */
306 #define	NFSV4OP_OPILLEGAL	10044
307 
308 /*
309  * Fake NFSV4OP_xxx used for nfsstat. Start at NFSV42_NOPS.
310  */
311 #define	NFSV4OP_SYMLINK		(NFSV42_NOPS)
312 #define	NFSV4OP_MKDIR		(NFSV42_NOPS + 1)
313 #define	NFSV4OP_RMDIR		(NFSV42_NOPS + 2)
314 #define	NFSV4OP_READDIRPLUS	(NFSV42_NOPS + 3)
315 #define	NFSV4OP_MKNOD		(NFSV42_NOPS + 4)
316 #define	NFSV4OP_FSSTAT		(NFSV42_NOPS + 5)
317 #define	NFSV4OP_FSINFO		(NFSV42_NOPS + 6)
318 #define	NFSV4OP_PATHCONF	(NFSV42_NOPS + 7)
319 #define	NFSV4OP_V3CREATE	(NFSV42_NOPS + 8)
320 
321 /*
322  * This is the count of the fake operations listed above.
323  */
324 #define	NFSV4OP_FAKENOPS	9
325 
326 /*
327  * and the Callback OPs
328  */
329 #define	NFSV4OP_CBGETATTR	3
330 #define	NFSV4OP_CBRECALL	4
331 
332 /*
333  * Must be one greater than the last Callback Operation# for NFSv4.0.
334  */
335 #define	NFSV4OP_CBNOPS		5
336 
337 /*
338  * Additional Callback Ops for NFSv4.1 only.
339  */
340 #define	NFSV4OP_CBLAYOUTRECALL	5
341 #define	NFSV4OP_CBNOTIFY	6
342 #define	NFSV4OP_CBPUSHDELEG	7
343 #define	NFSV4OP_CBRECALLANY	8
344 #define	NFSV4OP_CBRECALLOBJAVAIL 9
345 #define	NFSV4OP_CBRECALLSLOT	10
346 #define	NFSV4OP_CBSEQUENCE	11
347 #define	NFSV4OP_CBWANTCANCELLED	12
348 #define	NFSV4OP_CBNOTIFYLOCK	13
349 #define	NFSV4OP_CBNOTIFYDEVID	14
350 
351 #define	NFSV41_CBNOPS		15
352 
353 /*
354  * Additional callback operations for NFSv4.2.
355  */
356 #define	NFSV4OP_CBOFFLOAD	15
357 
358 #define	NFSV42_CBNOPS		16
359 
360 /*
361  * The lower numbers -> 21 are used by NFSv2 and v3. These define higher
362  * numbers used by NFSv4.
363  * NFS_V3NPROCS is one greater than the last V3 op and NFS_NPROCS is
364  * one greater than the last number.
365  */
366 #ifndef	NFS_V3NPROCS
367 #define	NFS_V3NPROCS		22
368 
369 #define	NFSPROC_LOOKUPP		22
370 #define	NFSPROC_SETCLIENTID	23
371 #define	NFSPROC_SETCLIENTIDCFRM	24
372 #define	NFSPROC_LOCK		25
373 #define	NFSPROC_LOCKU		26
374 #define	NFSPROC_OPEN		27
375 #define	NFSPROC_CLOSE		28
376 #define	NFSPROC_OPENCONFIRM	29
377 #define	NFSPROC_LOCKT		30
378 #define	NFSPROC_OPENDOWNGRADE	31
379 #define	NFSPROC_RENEW		32
380 #define	NFSPROC_PUTROOTFH	33
381 #define	NFSPROC_RELEASELCKOWN	34
382 #define	NFSPROC_DELEGRETURN	35
383 #define	NFSPROC_RETDELEGREMOVE	36
384 #define	NFSPROC_RETDELEGRENAME1	37
385 #define	NFSPROC_RETDELEGRENAME2	38
386 #define	NFSPROC_GETACL		39
387 #define	NFSPROC_SETACL		40
388 
389 /*
390  * Must be defined as one higher than the last Proc# above.
391  */
392 #define	NFSV4_NPROCS		41
393 
394 /* Additional procedures for NFSv4.1. */
395 #define	NFSPROC_EXCHANGEID	41
396 #define	NFSPROC_CREATESESSION	42
397 #define	NFSPROC_DESTROYSESSION	43
398 #define	NFSPROC_DESTROYCLIENT	44
399 #define	NFSPROC_FREESTATEID	45
400 #define	NFSPROC_LAYOUTGET	46
401 #define	NFSPROC_GETDEVICEINFO	47
402 #define	NFSPROC_LAYOUTCOMMIT	48
403 #define	NFSPROC_LAYOUTRETURN	49
404 #define	NFSPROC_RECLAIMCOMPL	50
405 #define	NFSPROC_WRITEDS		51
406 #define	NFSPROC_READDS		52
407 #define	NFSPROC_COMMITDS	53
408 #define	NFSPROC_OPENLAYGET	54
409 #define	NFSPROC_CREATELAYGET	55
410 
411 /*
412  * Must be defined as one higher than the last NFSv4.1 Proc# above.
413  */
414 #define	NFSV41_NPROCS		56
415 
416 /* Additional procedures for NFSv4.2. */
417 #define	NFSPROC_IOADVISE	56
418 #define	NFSPROC_ALLOCATE	57
419 #define	NFSPROC_COPY		58
420 #define	NFSPROC_SEEK		59
421 #define	NFSPROC_SEEKDS		60
422 
423 /* and the ones for the optional Extended attribute support (RFC-8276). */
424 #define	NFSPROC_GETEXTATTR	61
425 #define	NFSPROC_SETEXTATTR	62
426 #define	NFSPROC_RMEXTATTR	63
427 #define	NFSPROC_LISTEXTATTR	64
428 
429 /* BindConnectionToSession, done by the krpc for a new connection. */
430 #define	NFSPROC_BINDCONNTOSESS	65
431 
432 /* Do a Lookup+Open for "oneopenown". */
433 #define	NFSPROC_LOOKUPOPEN	66
434 
435 /* Do an NFSv4.2 Deallocate. */
436 #define	NFSPROC_DEALLOCATE	67
437 
438 /* Do an NFSv4.2 LayoutError. */
439 #define	NFSPROC_LAYOUTERROR	68
440 
441 /* Do an NFSv4 Verify+Write. */
442 #define	NFSPROC_APPENDWRITE	69
443 
444 /*
445  * Must be defined as one higher than the last NFSv4.2 Proc# above.
446  */
447 #define	NFSV42_NPROCS		70
448 
449 /* Value of NFSV42_NPROCS for old nfsstats structure. (Always 69) */
450 #define	NFSV42_OLDNPROCS	69
451 
452 #endif	/* NFS_V3NPROCS */
453 
454 /*
455  * Newest stats structure.
456  * The vers field will be set to NFSSTATS_V1 by the caller.
457  */
458 #define	NFSSTATS_V1	2
459 struct nfsstatsv1 {
460 	int		vers;	/* Set to version requested by caller. */
461 	uint64_t	attrcache_hits;
462 	uint64_t	attrcache_misses;
463 	uint64_t	lookupcache_hits;
464 	uint64_t	lookupcache_misses;
465 	uint64_t	direofcache_hits;
466 	uint64_t	direofcache_misses;
467 	uint64_t	accesscache_hits;
468 	uint64_t	accesscache_misses;
469 	uint64_t	biocache_reads;
470 	uint64_t	read_bios;
471 	uint64_t	read_physios;
472 	uint64_t	biocache_writes;
473 	uint64_t	write_bios;
474 	uint64_t	write_physios;
475 	uint64_t	biocache_readlinks;
476 	uint64_t	readlink_bios;
477 	uint64_t	biocache_readdirs;
478 	uint64_t	readdir_bios;
479 	uint64_t	rpccnt[NFSV42_NPROCS + 10];
480 	uint64_t	rpcretries;
481 	uint64_t	srvrpccnt[NFSV42_NOPS + NFSV4OP_FAKENOPS + 15];
482 	uint64_t	srvlayouts;
483 	uint64_t	cllayouts;
484 	uint64_t	rpcrequests;
485 	uint64_t	rpctimeouts;
486 	uint64_t	rpcunexpected;
487 	uint64_t	rpcinvalid;
488 	uint64_t	srvcache_inproghits;
489 	uint64_t	reserved_2;
490 	uint64_t	srvcache_nonidemdonehits;
491 	uint64_t	srvcache_misses;
492 	uint64_t	srvcache_tcppeak;
493 	int		srvcache_size;	/* Updated by atomic_xx_int(). */
494 	uint64_t	srvclients;
495 	uint64_t	srvopenowners;
496 	uint64_t	srvopens;
497 	uint64_t	srvlockowners;
498 	uint64_t	srvlocks;
499 	uint64_t	srvdelegates;
500 	uint64_t	cbrpccnt[NFSV42_CBNOPS + 10];
501 	uint64_t	clopenowners;
502 	uint64_t	clopens;
503 	uint64_t	cllockowners;
504 	uint64_t	cllocks;
505 	uint64_t	cldelegates;
506 	uint64_t	cllocalopenowners;
507 	uint64_t	cllocalopens;
508 	uint64_t	cllocallockowners;
509 	uint64_t	cllocallocks;
510 	uint64_t	srvstartcnt;
511 	uint64_t	srvdonecnt;
512 	uint64_t	srvbytes[NFSV42_NOPS + NFSV4OP_FAKENOPS + 15];
513 	uint64_t	srvops[NFSV42_NOPS + NFSV4OP_FAKENOPS + 15];
514 	struct bintime	srvduration[NFSV42_NOPS + NFSV4OP_FAKENOPS + 15];
515 	struct bintime	busyfrom;
516 	struct bintime	busytime;
517 };
518 
519 /*
520  * Newer stats structure.
521  * The vers field will be set to NFSSTATS_OV1 by the caller.
522  */
523 #define	NFSSTATS_OV1	1
524 struct nfsstatsov1 {
525 	int		vers;	/* Set to version requested by caller. */
526 	uint64_t	attrcache_hits;
527 	uint64_t	attrcache_misses;
528 	uint64_t	lookupcache_hits;
529 	uint64_t	lookupcache_misses;
530 	uint64_t	direofcache_hits;
531 	uint64_t	direofcache_misses;
532 	uint64_t	accesscache_hits;
533 	uint64_t	accesscache_misses;
534 	uint64_t	biocache_reads;
535 	uint64_t	read_bios;
536 	uint64_t	read_physios;
537 	uint64_t	biocache_writes;
538 	uint64_t	write_bios;
539 	uint64_t	write_physios;
540 	uint64_t	biocache_readlinks;
541 	uint64_t	readlink_bios;
542 	uint64_t	biocache_readdirs;
543 	uint64_t	readdir_bios;
544 	uint64_t	rpccnt[NFSV42_OLDNPROCS];
545 	uint64_t	rpcretries;
546 	uint64_t	srvrpccnt[NFSV42_PURENOPS + NFSV4OP_FAKENOPS];
547 	uint64_t	reserved_0;
548 	uint64_t	reserved_1;
549 	uint64_t	rpcrequests;
550 	uint64_t	rpctimeouts;
551 	uint64_t	rpcunexpected;
552 	uint64_t	rpcinvalid;
553 	uint64_t	srvcache_inproghits;
554 	uint64_t	reserved_2;
555 	uint64_t	srvcache_nonidemdonehits;
556 	uint64_t	srvcache_misses;
557 	uint64_t	srvcache_tcppeak;
558 	int		srvcache_size;	/* Updated by atomic_xx_int(). */
559 	uint64_t	srvclients;
560 	uint64_t	srvopenowners;
561 	uint64_t	srvopens;
562 	uint64_t	srvlockowners;
563 	uint64_t	srvlocks;
564 	uint64_t	srvdelegates;
565 	uint64_t	cbrpccnt[NFSV42_CBNOPS];
566 	uint64_t	clopenowners;
567 	uint64_t	clopens;
568 	uint64_t	cllockowners;
569 	uint64_t	cllocks;
570 	uint64_t	cldelegates;
571 	uint64_t	cllocalopenowners;
572 	uint64_t	cllocalopens;
573 	uint64_t	cllocallockowners;
574 	uint64_t	cllocallocks;
575 	uint64_t	srvstartcnt;
576 	uint64_t	srvdonecnt;
577 	uint64_t	srvbytes[NFSV42_PURENOPS + NFSV4OP_FAKENOPS];
578 	uint64_t	srvops[NFSV42_PURENOPS + NFSV4OP_FAKENOPS];
579 	struct bintime	srvduration[NFSV42_PURENOPS + NFSV4OP_FAKENOPS];
580 	struct bintime	busyfrom;
581 	struct bintime	busytime;
582 };
583 
584 /*
585  * Old stats structure.
586  */
587 struct ext_nfsstats {
588 	int	attrcache_hits;
589 	int	attrcache_misses;
590 	int	lookupcache_hits;
591 	int	lookupcache_misses;
592 	int	direofcache_hits;
593 	int	direofcache_misses;
594 	int	accesscache_hits;
595 	int	accesscache_misses;
596 	int	biocache_reads;
597 	int	read_bios;
598 	int	read_physios;
599 	int	biocache_writes;
600 	int	write_bios;
601 	int	write_physios;
602 	int	biocache_readlinks;
603 	int	readlink_bios;
604 	int	biocache_readdirs;
605 	int	readdir_bios;
606 	int	rpccnt[NFSV4_NPROCS];
607 	int	rpcretries;
608 	int	srvrpccnt[NFSV4OP_NOPS + NFSV4OP_FAKENOPS];
609 	int	reserved_0;
610 	int	reserved_1;
611 	int	rpcrequests;
612 	int	rpctimeouts;
613 	int	rpcunexpected;
614 	int	rpcinvalid;
615 	int	srvcache_inproghits;
616 	int	reserved_2;
617 	int	srvcache_nonidemdonehits;
618 	int	srvcache_misses;
619 	int	srvcache_tcppeak;
620 	int	srvcache_size;
621 	int	srvclients;
622 	int	srvopenowners;
623 	int	srvopens;
624 	int	srvlockowners;
625 	int	srvlocks;
626 	int	srvdelegates;
627 	int	cbrpccnt[NFSV4OP_CBNOPS];
628 	int	clopenowners;
629 	int	clopens;
630 	int	cllockowners;
631 	int	cllocks;
632 	int	cldelegates;
633 	int	cllocalopenowners;
634 	int	cllocalopens;
635 	int	cllocallockowners;
636 	int	cllocallocks;
637 };
638 
639 #ifdef _KERNEL
640 /*
641  * Define NFS_NPROCS as NFSV4_NPROCS for the experimental kernel code.
642  */
643 #ifndef	NFS_NPROCS
644 #define	NFS_NPROCS		NFSV4_NPROCS
645 #endif
646 
647 #include <fs/nfs/nfskpiport.h>
648 #include <fs/nfs/nfsdport.h>
649 #include <fs/nfs/rpcv2.h>
650 #include <fs/nfs/nfsproto.h>
651 #include <fs/nfs/nfs.h>
652 #include <fs/nfs/nfsclstate.h>
653 #include <fs/nfs/nfs_var.h>
654 #include <fs/nfs/nfsm_subs.h>
655 #include <fs/nfs/nfsrvcache.h>
656 #include <fs/nfs/nfsrvstate.h>
657 #include <fs/nfs/xdr_subs.h>
658 #include <fs/nfs/nfscl.h>
659 #include <nfsclient/nfsargs.h>
660 #include <fs/nfsclient/nfsmount.h>
661 
662 /*
663  * Just to keep nfs_var.h happy.
664  */
665 struct nfs_vattr {
666 	int	junk;
667 };
668 
669 struct nfsvattr {
670 	struct vattr	na_vattr;
671 	nfsattrbit_t	na_suppattr;
672 	u_int64_t	na_mntonfileno;
673 	u_int64_t	na_filesid[2];
674 };
675 
676 #define	na_type		na_vattr.va_type
677 #define	na_mode		na_vattr.va_mode
678 #define	na_nlink	na_vattr.va_nlink
679 #define	na_uid		na_vattr.va_uid
680 #define	na_gid		na_vattr.va_gid
681 #define	na_fsid		na_vattr.va_fsid
682 #define	na_fileid	na_vattr.va_fileid
683 #define	na_size		na_vattr.va_size
684 #define	na_blocksize	na_vattr.va_blocksize
685 #define	na_atime	na_vattr.va_atime
686 #define	na_mtime	na_vattr.va_mtime
687 #define	na_ctime	na_vattr.va_ctime
688 #define	na_btime	na_vattr.va_birthtime
689 #define	na_gen		na_vattr.va_gen
690 #define	na_flags	na_vattr.va_flags
691 #define	na_rdev		na_vattr.va_rdev
692 #define	na_bytes	na_vattr.va_bytes
693 #define	na_filerev	na_vattr.va_filerev
694 #define	na_vaflags	na_vattr.va_vaflags
695 
696 #include <fs/nfsclient/nfsnode.h>
697 
698 /*
699  * This is the header structure used for the lists, etc. (It has the
700  * above record in it.
701  */
702 struct nfsrv_stablefirst {
703 	LIST_HEAD(, nfsrv_stable) nsf_head;	/* Head of nfsrv_stable list */
704 	time_t		nsf_eograce;	/* Time grace period ends */
705 	time_t		*nsf_bootvals;	/* Previous boottime values */
706 	struct file	*nsf_fp;	/* File table pointer */
707 	u_char		nsf_flags;	/* NFSNSF_ flags */
708 	struct nfsf_rec	nsf_rec;	/* and above first record */
709 };
710 #define	nsf_lease	nsf_rec.lease
711 #define	nsf_numboots	nsf_rec.numboots
712 
713 /* NFSNSF_xxx flags */
714 #define	NFSNSF_UPDATEDONE	0x01
715 #define	NFSNSF_GRACEOVER	0x02
716 #define	NFSNSF_NEEDLOCK		0x04
717 #define	NFSNSF_EXPIREDCLIENT	0x08
718 #define	NFSNSF_NOOPENS		0x10
719 #define	NFSNSF_OK		0x20
720 
721 /*
722  * Maximum number of boot times allowed in record. Although there is
723  * really no need for a fixed upper bound, this serves as a sanity check
724  * for a corrupted file.
725  */
726 #define	NFSNSF_MAXNUMBOOTS	10000
727 
728 /*
729  * This structure defines the other records in the file. The
730  * nst_client array is actually the size of the client string name.
731  */
732 struct nfst_rec {
733 	u_int16_t	len;
734 	u_char		flag;
735 	u_char		client[1];
736 };
737 /* and the values for flag */
738 #define	NFSNST_NEWSTATE	0x1
739 #define	NFSNST_REVOKE		0x2
740 #define	NFSNST_GOTSTATE		0x4
741 #define	NFSNST_RECLAIMED	0x8
742 
743 /*
744  * This structure is linked onto nfsrv_stablefirst for the duration of
745  * reclaim.
746  */
747 struct nfsrv_stable {
748 	LIST_ENTRY(nfsrv_stable) nst_list;
749 	struct nfsclient	*nst_clp;
750 	struct nfst_rec		nst_rec;
751 };
752 #define	nst_timestamp	nst_rec.timestamp
753 #define	nst_len		nst_rec.len
754 #define	nst_flag	nst_rec.flag
755 #define	nst_client	nst_rec.client
756 
757 /*
758  * At some point the server will run out of kernel storage for
759  * state structures. For FreeBSD5.2, this results in a panic
760  * kmem_map is full. It happens at well over 1000000 opens plus
761  * locks on a PIII-800 with 256Mbytes, so that is where I've set
762  * the limit. If your server panics due to too many opens/locks,
763  * decrease the size of NFSRV_V4STATELIMIT. If you find the server
764  * returning NFS4ERR_RESOURCE a lot and have lots of memory, try
765  * increasing it.
766  */
767 #define	NFSRV_V4STATELIMIT	500000	/* Max # of Opens + Locks */
768 
769 /*
770  * The type required differs with BSDen (just the second arg).
771  */
772 void nfsrvd_rcv(struct socket *, void *, int);
773 
774 /*
775  * Macros for handling socket addresses. (Hopefully this makes the code
776  * more portable, since I've noticed some 'BSD don't have sockaddrs in
777  * mbufs any more.)
778  */
779 #define	NFSSOCKADDR(a, t)	((t)(a))
780 #define	NFSSOCKADDRSIZE(a, s)		((a)->sa_len = (s))
781 
782 /*
783  * These should be defined as a process or thread structure, as required
784  * for signal handling, etc.
785  */
786 #define	NFSNEWCRED(c)		(crdup(c))
787 #define	NFSPROCCRED(p)		((p)->td_ucred)
788 #define	NFSFREECRED(c)		(crfree(c))
789 #define	NFSUIOPROC(u, p)	((u)->uio_td = NULL)
790 #define	NFSPROCP(p)		((p)->td_proc)
791 
792 /*
793  * Define these so that cn_hash and its length is ignored.
794  */
795 #define	NFSCNHASHZERO(c)
796 #define	NFSCNHASH(c, v)
797 #define	NCHNAMLEN	9999999
798 
799 /*
800  * Handle SMP stuff:
801  */
802 #define	NFSSTATESPINLOCK	extern struct mtx nfs_state_mutex
803 #define	NFSLOCKSTATE()		mtx_lock(&nfs_state_mutex)
804 #define	NFSUNLOCKSTATE()	mtx_unlock(&nfs_state_mutex)
805 #define	NFSSTATEMUTEXPTR	(&nfs_state_mutex)
806 #define	NFSREQSPINLOCK		extern struct mtx nfs_req_mutex
807 #define	NFSLOCKREQ()		mtx_lock(&nfs_req_mutex)
808 #define	NFSUNLOCKREQ()		mtx_unlock(&nfs_req_mutex)
809 #define	NFSSOCKMUTEX		extern struct mtx nfs_slock_mutex
810 #define	NFSSOCKMUTEXPTR		(&nfs_slock_mutex)
811 #define	NFSLOCKSOCK()		mtx_lock(&nfs_slock_mutex)
812 #define	NFSUNLOCKSOCK()		mtx_unlock(&nfs_slock_mutex)
813 #define	NFSNAMEIDMUTEX		extern struct mtx nfs_nameid_mutex
814 #define	NFSNAMEIDMUTEXPTR	(&nfs_nameid_mutex)
815 #define	NFSLOCKNAMEID()		mtx_lock(&nfs_nameid_mutex)
816 #define	NFSUNLOCKNAMEID()	mtx_unlock(&nfs_nameid_mutex)
817 #define	NFSNAMEIDREQUIRED()	mtx_assert(&nfs_nameid_mutex, MA_OWNED)
818 #define	NFSCLSTATEMUTEX		extern struct mtx nfs_clstate_mutex
819 #define	NFSCLSTATEMUTEXPTR	(&nfs_clstate_mutex)
820 #define	NFSLOCKCLSTATE()	mtx_lock(&nfs_clstate_mutex)
821 #define	NFSUNLOCKCLSTATE()	mtx_unlock(&nfs_clstate_mutex)
822 #define	NFSDLOCKMUTEX		extern struct mtx newnfsd_mtx
823 #define	NFSDLOCKMUTEXPTR	(&newnfsd_mtx)
824 #define	NFSD_LOCK()		mtx_lock(&newnfsd_mtx)
825 #define	NFSD_UNLOCK()		mtx_unlock(&newnfsd_mtx)
826 #define	NFSD_LOCK_ASSERT()	mtx_assert(&newnfsd_mtx, MA_OWNED)
827 #define	NFSD_UNLOCK_ASSERT()	mtx_assert(&newnfsd_mtx, MA_NOTOWNED)
828 #define	NFSV4ROOTLOCKMUTEX	extern struct mtx nfs_v4root_mutex
829 #define	NFSV4ROOTLOCKMUTEXPTR	(&nfs_v4root_mutex)
830 #define	NFSLOCKV4ROOTMUTEX()	mtx_lock(&nfs_v4root_mutex)
831 #define	NFSUNLOCKV4ROOTMUTEX()	mtx_unlock(&nfs_v4root_mutex)
832 #define	NFSLOCKNODE(n)		mtx_lock(&((n)->n_mtx))
833 #define	NFSUNLOCKNODE(n)	mtx_unlock(&((n)->n_mtx))
834 #define	NFSASSERTNODE(n)	mtx_assert(&((n)->n_mtx), MA_OWNED)
835 #define	NFSLOCKMNT(m)		mtx_lock(&((m)->nm_mtx))
836 #define	NFSUNLOCKMNT(m)		mtx_unlock(&((m)->nm_mtx))
837 #define	NFSLOCKIOD()		mtx_lock(&ncl_iod_mutex)
838 #define	NFSUNLOCKIOD()		mtx_unlock(&ncl_iod_mutex)
839 #define	NFSASSERTIOD()		mtx_assert(&ncl_iod_mutex, MA_OWNED)
840 #define	NFSLOCKREQUEST(r)	mtx_lock(&((r)->r_mtx))
841 #define	NFSUNLOCKREQUEST(r)	mtx_unlock(&((r)->r_mtx))
842 #define	NFSLOCKSOCKREQ(r)	mtx_lock(&((r)->nr_mtx))
843 #define	NFSUNLOCKSOCKREQ(r)	mtx_unlock(&((r)->nr_mtx))
844 #define	NFSLOCKDS(d)		mtx_lock(&((d)->nfsclds_mtx))
845 #define	NFSUNLOCKDS(d)		mtx_unlock(&((d)->nfsclds_mtx))
846 #define	NFSSESSIONMUTEXPTR(s)	(&((s)->mtx))
847 #define	NFSLOCKSESSION(s)	mtx_lock(&((s)->mtx))
848 #define	NFSUNLOCKSESSION(s)	mtx_unlock(&((s)->mtx))
849 #define	NFSLAYOUTMUTEXPTR(l)	(&((l)->mtx))
850 #define	NFSLOCKLAYOUT(l)	mtx_lock(&((l)->mtx))
851 #define	NFSUNLOCKLAYOUT(l)	mtx_unlock(&((l)->mtx))
852 #define	NFSDDSMUTEXPTR		(&nfsrv_dslock_mtx)
853 #define	NFSDDSLOCK()		mtx_lock(&nfsrv_dslock_mtx)
854 #define	NFSDDSUNLOCK()		mtx_unlock(&nfsrv_dslock_mtx)
855 #define	NFSDDONTLISTMUTEXPTR	(&nfsrv_dontlistlock_mtx)
856 #define	NFSDDONTLISTLOCK()	mtx_lock(&nfsrv_dontlistlock_mtx)
857 #define	NFSDDONTLISTUNLOCK()	mtx_unlock(&nfsrv_dontlistlock_mtx)
858 #define	NFSDRECALLMUTEXPTR	(&nfsrv_recalllock_mtx)
859 #define	NFSDRECALLLOCK()	mtx_lock(&nfsrv_recalllock_mtx)
860 #define	NFSDRECALLUNLOCK()	mtx_unlock(&nfsrv_recalllock_mtx)
861 
862 /*
863  * Use these macros to initialize/free a mutex.
864  */
865 #define	NFSINITSOCKMUTEX(m)	mtx_init((m), "nfssock", NULL, MTX_DEF)
866 #define	NFSFREEMUTEX(m)		mtx_destroy((m))
867 
868 int nfsmsleep(void *, void *, int, const char *, struct timespec *);
869 
870 /*
871  * And weird vm stuff in the nfs server.
872  */
873 #define	PDIRUNLOCK	0x0
874 #define	MAX_COMMIT_COUNT	(1024 * 1024)
875 
876 /*
877  * Define these to handle the type of va_rdev.
878  */
879 #define	NFSMAKEDEV(m, n)	makedev((m), (n))
880 #define	NFSMAJOR(d)		major(d)
881 #define	NFSMINOR(d)		minor(d)
882 
883 /*
884  * The vnode tag for nfsv4root.
885  */
886 #define	VT_NFSV4ROOT		"nfsv4root"
887 
888 /*
889  * Define whatever it takes to do a vn_rdwr().
890  */
891 #define	NFSD_RDWR(r, v, b, l, o, s, i, c, a, p) \
892 	vn_rdwr((r), (v), (b), (l), (o), (s), (i), (c), NULL, (a), (p))
893 
894 /*
895  * Macros for handling memory for different BSDen.
896  * NFSBCOPY(src, dst, len) - copies len bytes, non-overlapping
897  * NFSOVBCOPY(src, dst, len) - ditto, but data areas might overlap
898  * NFSBCMP(cp1, cp2, len) - compare len bytes, return 0 if same
899  * NFSBZERO(cp, len) - set len bytes to 0x0
900  */
901 #define	NFSBCOPY(s, d, l)	bcopy((s), (d), (l))
902 #define	NFSOVBCOPY(s, d, l)	ovbcopy((s), (d), (l))
903 #define	NFSBCMP(s, d, l)	bcmp((s), (d), (l))
904 #define	NFSBZERO(s, l)		bzero((s), (l))
905 
906 /*
907  * Some queue.h files don't have these dfined in them.
908  */
909 #ifndef LIST_END
910 #define	LIST_END(head)		NULL
911 #define	SLIST_END(head)		NULL
912 #define	TAILQ_END(head)		NULL
913 #endif
914 
915 /*
916  * This must be defined to be a global variable that increments once
917  * per second, but never stops or goes backwards, even when a "date"
918  * command changes the TOD clock. It is used for delta times for
919  * leases, etc.
920  */
921 #define	NFSD_MONOSEC		time_uptime
922 
923 /*
924  * Declare the malloc types.
925  */
926 MALLOC_DECLARE(M_NEWNFSRVCACHE);
927 MALLOC_DECLARE(M_NEWNFSDCLIENT);
928 MALLOC_DECLARE(M_NEWNFSDSTATE);
929 MALLOC_DECLARE(M_NEWNFSDLOCK);
930 MALLOC_DECLARE(M_NEWNFSDLOCKFILE);
931 MALLOC_DECLARE(M_NEWNFSSTRING);
932 MALLOC_DECLARE(M_NEWNFSUSERGROUP);
933 MALLOC_DECLARE(M_NEWNFSDREQ);
934 MALLOC_DECLARE(M_NEWNFSFH);
935 MALLOC_DECLARE(M_NEWNFSCLOWNER);
936 MALLOC_DECLARE(M_NEWNFSCLOPEN);
937 MALLOC_DECLARE(M_NEWNFSCLDELEG);
938 MALLOC_DECLARE(M_NEWNFSCLCLIENT);
939 MALLOC_DECLARE(M_NEWNFSCLLOCKOWNER);
940 MALLOC_DECLARE(M_NEWNFSCLLOCK);
941 MALLOC_DECLARE(M_NEWNFSDIROFF);
942 MALLOC_DECLARE(M_NEWNFSV4NODE);
943 MALLOC_DECLARE(M_NEWNFSDIRECTIO);
944 MALLOC_DECLARE(M_NEWNFSMNT);
945 MALLOC_DECLARE(M_NEWNFSDROLLBACK);
946 MALLOC_DECLARE(M_NEWNFSLAYOUT);
947 MALLOC_DECLARE(M_NEWNFSFLAYOUT);
948 MALLOC_DECLARE(M_NEWNFSDEVINFO);
949 MALLOC_DECLARE(M_NEWNFSSOCKREQ);
950 MALLOC_DECLARE(M_NEWNFSCLDS);
951 MALLOC_DECLARE(M_NEWNFSLAYRECALL);
952 MALLOC_DECLARE(M_NEWNFSDSESSION);
953 #define	M_NFSRVCACHE	M_NEWNFSRVCACHE
954 #define	M_NFSDCLIENT	M_NEWNFSDCLIENT
955 #define	M_NFSDSTATE	M_NEWNFSDSTATE
956 #define	M_NFSDLOCK	M_NEWNFSDLOCK
957 #define	M_NFSDLOCKFILE	M_NEWNFSDLOCKFILE
958 #define	M_NFSSTRING	M_NEWNFSSTRING
959 #define	M_NFSUSERGROUP	M_NEWNFSUSERGROUP
960 #define	M_NFSDREQ	M_NEWNFSDREQ
961 #define	M_NFSFH		M_NEWNFSFH
962 #define	M_NFSCLOWNER	M_NEWNFSCLOWNER
963 #define	M_NFSCLOPEN	M_NEWNFSCLOPEN
964 #define	M_NFSCLDELEG	M_NEWNFSCLDELEG
965 #define	M_NFSCLCLIENT	M_NEWNFSCLCLIENT
966 #define	M_NFSCLLOCKOWNER M_NEWNFSCLLOCKOWNER
967 #define	M_NFSCLLOCK	M_NEWNFSCLLOCK
968 #define	M_NFSDIROFF	M_NEWNFSDIROFF
969 #define	M_NFSV4NODE	M_NEWNFSV4NODE
970 #define	M_NFSDIRECTIO	M_NEWNFSDIRECTIO
971 #define	M_NFSDROLLBACK	M_NEWNFSDROLLBACK
972 #define	M_NFSLAYOUT	M_NEWNFSLAYOUT
973 #define	M_NFSFLAYOUT	M_NEWNFSFLAYOUT
974 #define	M_NFSDEVINFO	M_NEWNFSDEVINFO
975 #define	M_NFSSOCKREQ	M_NEWNFSSOCKREQ
976 #define	M_NFSCLDS	M_NEWNFSCLDS
977 #define	M_NFSLAYRECALL	M_NEWNFSLAYRECALL
978 #define	M_NFSDSESSION	M_NEWNFSDSESSION
979 
980 #define	NFSINT_SIGMASK(set) 						\
981 	(SIGISMEMBER(set, SIGINT) || SIGISMEMBER(set, SIGTERM) ||	\
982 	 SIGISMEMBER(set, SIGHUP) || SIGISMEMBER(set, SIGKILL) ||	\
983 	 SIGISMEMBER(set, SIGQUIT))
984 
985 /*
986  * Convert a quota block count to byte count.
987  */
988 #define	NFSQUOTABLKTOBYTE(q, b)	(q) *= (b)
989 
990 /*
991  * Define this as the largest file size supported. (It should probably
992  * be available via a VFS_xxx Op, but it isn't.
993  */
994 #define	NFSRV_MAXFILESIZE	((u_int64_t)0x800000000000)
995 
996 /*
997  * Set this macro to index() or strchr(), whichever is supported.
998  */
999 #define	STRCHR(s, c)		strchr((s), (c))
1000 
1001 /*
1002  * Set the n_time in the client write rpc, as required.
1003  */
1004 #define	NFSWRITERPC_SETTIME(w, n, a, v4)				\
1005 	do {								\
1006 		if (w) {						\
1007 			NFSLOCKNODE(n);					\
1008 			(n)->n_mtime = (a)->na_mtime;			\
1009 			if (v4)						\
1010 				(n)->n_change = (a)->na_filerev;	\
1011 			NFSUNLOCKNODE(n);				\
1012 		}							\
1013 	} while (0)
1014 
1015 /*
1016  * Fake value, just to make the client work.
1017  */
1018 #define	NFS_LATTR_NOSHRINK	1
1019 
1020 /*
1021  * Prototypes for functions where the arguments vary for different ports.
1022  */
1023 int nfscl_loadattrcache(struct vnode **, struct nfsvattr *, void *, int, int);
1024 int newnfs_realign(struct mbuf **, int);
1025 bool ncl_pager_setsize(struct vnode *vp, u_quad_t *nsizep);
1026 void ncl_copy_vattr(struct vattr *dst, struct vattr *src);
1027 
1028 /*
1029  * If the port runs on an SMP box that can enforce Atomic ops with low
1030  * overheads, define these as atomic increments/decrements. If not,
1031  * don't worry about it, since these are used for stats that can be
1032  * "out by one" without disastrous consequences.
1033  */
1034 #define	NFSINCRGLOBAL(a)	((a)++)
1035 #define	NFSDECRGLOBAL(a)	((a)--)
1036 
1037 /*
1038  * Assorted funky stuff to make things work under Darwin8.
1039  */
1040 /*
1041  * These macros checks for a field in vattr being set.
1042  */
1043 #define	NFSATTRISSET(t, v, a)	((v)->a != (t)VNOVAL)
1044 #define	NFSATTRISSETTIME(v, a)	((v)->a.tv_sec != VNOVAL)
1045 
1046 /*
1047  * Manipulate mount flags.
1048  */
1049 #define	NFSSTA_HASWRITEVERF	0x00040000  /* Has write verifier */
1050 #define	NFSSTA_GOTFSINFO	0x00100000  /* Got the fsinfo */
1051 #define	NFSSTA_OPENMODE		0x00200000  /* Must use correct open mode */
1052 #define	NFSSTA_FLEXFILE		0x00800000  /* Use Flex File Layout */
1053 #define	NFSSTA_NOLAYOUTCOMMIT	0x04000000  /* Don't do LayoutCommit */
1054 #define	NFSSTA_SESSPERSIST	0x08000000  /* Has a persistent session */
1055 #define	NFSSTA_TIMEO		0x10000000  /* Experiencing a timeout */
1056 #define	NFSSTA_LOCKTIMEO	0x20000000  /* Experiencing a lockd timeout */
1057 #define	NFSSTA_HASSETFSID	0x40000000  /* Has set the fsid */
1058 #define	NFSSTA_PNFS		0x80000000  /* pNFS is enabled */
1059 
1060 #define	NFSHASNFSV3(n)		((n)->nm_flag & NFSMNT_NFSV3)
1061 #define	NFSHASNFSV4(n)		((n)->nm_flag & NFSMNT_NFSV4)
1062 #define	NFSHASNFSV4N(n)		((n)->nm_minorvers > 0)
1063 #define	NFSHASNFSV3OR4(n)	((n)->nm_flag & (NFSMNT_NFSV3 | NFSMNT_NFSV4))
1064 #define	NFSHASGOTFSINFO(n)	((n)->nm_state & NFSSTA_GOTFSINFO)
1065 #define	NFSHASHASSETFSID(n)	((n)->nm_state & NFSSTA_HASSETFSID)
1066 #define	NFSHASSTRICT3530(n)	((n)->nm_flag & NFSMNT_STRICT3530)
1067 #define	NFSHASWRITEVERF(n)	((n)->nm_state & NFSSTA_HASWRITEVERF)
1068 #define	NFSHASINT(n)		((n)->nm_flag & NFSMNT_INT)
1069 #define	NFSHASSOFT(n)		((n)->nm_flag & NFSMNT_SOFT)
1070 #define	NFSHASINTORSOFT(n)	((n)->nm_flag & (NFSMNT_INT | NFSMNT_SOFT))
1071 #define	NFSHASDUMBTIMR(n)	((n)->nm_flag & NFSMNT_DUMBTIMR)
1072 #define	NFSHASNOCONN(n)		((n)->nm_flag & NFSMNT_MNTD)
1073 #define	NFSHASKERB(n)		((n)->nm_flag & NFSMNT_KERB)
1074 #define	NFSHASALLGSSNAME(n)	((n)->nm_flag & NFSMNT_ALLGSSNAME)
1075 #define	NFSHASINTEGRITY(n)	((n)->nm_flag & NFSMNT_INTEGRITY)
1076 #define	NFSHASPRIVACY(n)	((n)->nm_flag & NFSMNT_PRIVACY)
1077 #define	NFSSETWRITEVERF(n)	((n)->nm_state |= NFSSTA_HASWRITEVERF)
1078 #define	NFSSETHASSETFSID(n)	((n)->nm_state |= NFSSTA_HASSETFSID)
1079 #define	NFSHASPNFSOPT(n)	((n)->nm_flag & NFSMNT_PNFS)
1080 #define	NFSHASNOLAYOUTCOMMIT(n)	((n)->nm_state & NFSSTA_NOLAYOUTCOMMIT)
1081 #define	NFSHASSESSPERSIST(n)	((n)->nm_state & NFSSTA_SESSPERSIST)
1082 #define	NFSHASPNFS(n)		((n)->nm_state & NFSSTA_PNFS)
1083 #define	NFSHASFLEXFILE(n)	((n)->nm_state & NFSSTA_FLEXFILE)
1084 #define	NFSHASOPENMODE(n)	((n)->nm_state & NFSSTA_OPENMODE)
1085 #define	NFSHASONEOPENOWN(n)	(((n)->nm_flag & NFSMNT_ONEOPENOWN) != 0 &&	\
1086 				    (n)->nm_minorvers > 0)
1087 #define	NFSHASTLS(n)		(((n)->nm_newflag & NFSMNT_TLS) != 0)
1088 #define	NFSHASSYSKRB5(n)	(((n)->nm_newflag & NFSMNT_SYSKRB5) != 0)
1089 
1090 /*
1091  * Set boottime.
1092  */
1093 #define	NFSSETBOOTTIME(b)	(getboottime(&b))
1094 
1095 /*
1096  * The size of directory blocks in the buffer cache.
1097  * MUST BE in the range of PAGE_SIZE <= NFS_DIRBLKSIZ <= MAXBSIZE!!
1098  */
1099 #define	NFS_DIRBLKSIZ	(16 * DIRBLKSIZ) /* Must be a multiple of DIRBLKSIZ */
1100 
1101 /*
1102  * Define these macros to access mnt_flag fields.
1103  */
1104 #define	NFSMNT_RDONLY(m)	((m)->mnt_flag & MNT_RDONLY)
1105 #endif	/* _KERNEL */
1106 
1107 /*
1108  * Define a structure similar to ufs_args for use in exporting the V4 root.
1109  */
1110 struct nfsex_args {
1111 	char	*fspec;
1112 	struct export_args	export;
1113 };
1114 
1115 struct nfsex_oldargs {
1116 	char	*fspec;
1117 	struct o2export_args	export;
1118 };
1119 
1120 /*
1121  * These export flags should be defined, but there are no bits left.
1122  * Maybe a separate mnt_exflag field could be added or the mnt_flag
1123  * field increased to 64 bits?
1124  */
1125 #ifndef	MNT_EXSTRICTACCESS
1126 #define	MNT_EXSTRICTACCESS	0x0
1127 #endif
1128 #ifndef MNT_EXV4ONLY
1129 #define	MNT_EXV4ONLY		0x0
1130 #endif
1131 
1132 #ifdef _KERNEL
1133 /*
1134  * Define this to invalidate the attribute cache for the nfs node.
1135  */
1136 #define	NFSINVALATTRCACHE(n)	((n)->n_attrstamp = 0)
1137 
1138 /* Used for FreeBSD only */
1139 void nfsd_mntinit(void);
1140 
1141 /*
1142  * Define these for vnode lock/unlock ops.
1143  *
1144  * These are good abstractions to macro out, so that they can be added to
1145  * later, for debugging or stats, etc.
1146  */
1147 #define	NFSVOPLOCK(v, f)	vn_lock((v), (f))
1148 #define	NFSVOPUNLOCK(v)		VOP_UNLOCK((v))
1149 #define	NFSVOPISLOCKED(v)	VOP_ISLOCKED((v))
1150 
1151 /*
1152  * Define ncl_hash().
1153  */
1154 #define	ncl_hash(f, l)	(fnv_32_buf((f), (l), FNV1_32_INIT))
1155 
1156 int newnfs_iosize(struct nfsmount *);
1157 
1158 int newnfs_vncmpf(struct vnode *, void *);
1159 
1160 #ifndef NFS_MINDIRATTRTIMO
1161 #define	NFS_MINDIRATTRTIMO 3		/* VDIR attrib cache timeout in sec */
1162 #endif
1163 #ifndef NFS_MAXDIRATTRTIMO
1164 #define	NFS_MAXDIRATTRTIMO 60
1165 #endif
1166 
1167 /*
1168  * Nfs outstanding request list element
1169  */
1170 struct nfsreq {
1171 	TAILQ_ENTRY(nfsreq) r_chain;
1172 	u_int32_t	r_flags;	/* flags on request, see below */
1173 	struct nfsmount *r_nmp;		/* Client mnt ptr */
1174 	struct mtx	r_mtx;		/* Mutex lock for this structure */
1175 };
1176 
1177 #ifndef NFS_MAXBSIZE
1178 #define	NFS_MAXBSIZE	(maxbcachebuf)
1179 #endif
1180 
1181 /*
1182  * This macro checks to see if issuing of delegations is allowed for this
1183  * vnode.
1184  */
1185 #ifdef VV_DISABLEDELEG
1186 #define	NFSVNO_DELEGOK(v)						\
1187 	((v) == NULL || ((v)->v_vflag & VV_DISABLEDELEG) == 0)
1188 #else
1189 #define	NFSVNO_DELEGOK(v)	(1)
1190 #endif
1191 
1192 /*
1193  * Name used by getnewvnode() to describe filesystem, "nfs".
1194  * For performance reasons it is useful to have the same string
1195  * used in both places that call getnewvnode().
1196  */
1197 extern const char nfs_vnode_tag[];
1198 
1199 /*
1200  * Check for the errors that indicate a DS should be disabled.
1201  * ENXIO indicates that the krpc cannot do an RPC on the DS.
1202  * EIO is returned by the RPC as an indication of I/O problems on the
1203  * server.
1204  * Are there other fatal errors?
1205  */
1206 #define	nfsds_failerr(e)	((e) == ENXIO || (e) == EIO)
1207 
1208 /*
1209  * Get a pointer to the MDS session, which is always the first element
1210  * in the list.
1211  * This macro can only be safely used when the NFSLOCKMNT() lock is held.
1212  * The inline function can be used when the lock isn't held.
1213  */
1214 #define	NFSMNT_MDSSESSION(m)	(&(TAILQ_FIRST(&((m)->nm_sess))->nfsclds_sess))
1215 
1216 static __inline struct nfsclsession *
1217 nfsmnt_mdssession(struct nfsmount *nmp)
1218 {
1219 	struct nfsclsession *tsep;
1220 
1221 	tsep = NULL;
1222 	mtx_lock(&nmp->nm_mtx);
1223 	if (TAILQ_FIRST(&nmp->nm_sess) != NULL)
1224 		tsep = NFSMNT_MDSSESSION(nmp);
1225 	mtx_unlock(&nmp->nm_mtx);
1226 	return (tsep);
1227 }
1228 
1229 #endif	/* _KERNEL */
1230 
1231 #endif	/* _NFS_NFSPORT_H */
1232