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