1 /*	$NetBSD: nfsport.h,v 1.1.1.1 2013/09/30 07:19:41 dholland Exp $	*/
2 /*-
3  * Copyright (c) 1989, 1993
4  *	The Regents of the University of California.  All rights reserved.
5  *
6  * This code is derived from software contributed to Berkeley by
7  * Rick Macklem at The University of Guelph.
8  *
9  * Redistribution and use in source and binary forms, with or without
10  * modification, are permitted provided that the following conditions
11  * are met:
12  * 1. Redistributions of source code must retain the above copyright
13  *    notice, this list of conditions and the following disclaimer.
14  * 2. Redistributions in binary form must reproduce the above copyright
15  *    notice, this list of conditions and the following disclaimer in the
16  *    documentation and/or other materials provided with the distribution.
17  * 4. Neither the name of the University nor the names of its contributors
18  *    may be used to endorse or promote products derived from this software
19  *    without specific prior written permission.
20  *
21  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
22  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
23  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
24  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
25  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
26  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
27  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
28  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
29  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
30  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
31  * SUCH DAMAGE.
32  *
33  * FreeBSD: head/sys/fs/nfs/nfsport.h 254337 2013-08-14 21:11:26Z rmacklem
34  * $NetBSD: nfsport.h,v 1.1.1.1 2013/09/30 07:19:41 dholland Exp $
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/namei.h>
61 #include <sys/proc.h>
62 #include <sys/protosw.h>
63 #include <sys/reboot.h>
64 #include <sys/resourcevar.h>
65 #include <sys/signalvar.h>
66 #include <sys/socket.h>
67 #include <sys/socketvar.h>
68 #include <sys/stat.h>
69 #include <sys/syslog.h>
70 #include <sys/sysproto.h>
71 #include <sys/time.h>
72 #include <sys/uio.h>
73 #include <sys/vnode.h>
74 #include <sys/bio.h>
75 #include <sys/buf.h>
76 #include <sys/acl.h>
77 #include <sys/module.h>
78 #include <sys/sysent.h>
79 #include <sys/syscall.h>
80 #include <sys/priv.h>
81 #include <sys/kthread.h>
82 #include <sys/syscallsubr.h>
83 #include <net/if.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 <crypto/des/des.h>
100 #include <sys/md5.h>
101 #include <rpc/rpc.h>
102 #include <rpc/rpcsec_gss.h>
103 
104 /*
105  * For Darwin, these functions should be "static" when built in a kext.
106  * (This is always defined as nil otherwise.)
107  */
108 #define	APPLESTATIC
109 #include <ufs/ufs/dir.h>
110 #include <ufs/ufs/quota.h>
111 #include <ufs/ufs/inode.h>
112 #include <ufs/ufs/extattr.h>
113 #include <ufs/ufs/ufsmount.h>
114 #include <vm/uma.h>
115 #include <vm/vm.h>
116 #include <vm/vm_object.h>
117 #include <vm/vm_extern.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 #define	NFSMTOD	mtod
174 
175 /*
176  * Client side constant for size of a lockowner name.
177  */
178 #define	NFSV4CL_LOCKNAMELEN	12
179 
180 /*
181  * Type for a mutex lock.
182  */
183 #define	NFSMUTEX_T		struct mtx
184 
185 #endif	/* _KERNEL */
186 
187 /*
188  * NFSv4 Operation numbers.
189  */
190 #define	NFSV4OP_ACCESS		3
191 #define	NFSV4OP_CLOSE		4
192 #define	NFSV4OP_COMMIT		5
193 #define	NFSV4OP_CREATE		6
194 #define	NFSV4OP_DELEGPURGE	7
195 #define	NFSV4OP_DELEGRETURN	8
196 #define	NFSV4OP_GETATTR		9
197 #define	NFSV4OP_GETFH		10
198 #define	NFSV4OP_LINK		11
199 #define	NFSV4OP_LOCK		12
200 #define	NFSV4OP_LOCKT		13
201 #define	NFSV4OP_LOCKU		14
202 #define	NFSV4OP_LOOKUP		15
203 #define	NFSV4OP_LOOKUPP		16
204 #define	NFSV4OP_NVERIFY		17
205 #define	NFSV4OP_OPEN		18
206 #define	NFSV4OP_OPENATTR	19
207 #define	NFSV4OP_OPENCONFIRM	20
208 #define	NFSV4OP_OPENDOWNGRADE	21
209 #define	NFSV4OP_PUTFH		22
210 #define	NFSV4OP_PUTPUBFH	23
211 #define	NFSV4OP_PUTROOTFH	24
212 #define	NFSV4OP_READ		25
213 #define	NFSV4OP_READDIR		26
214 #define	NFSV4OP_READLINK	27
215 #define	NFSV4OP_REMOVE		28
216 #define	NFSV4OP_RENAME		29
217 #define	NFSV4OP_RENEW		30
218 #define	NFSV4OP_RESTOREFH	31
219 #define	NFSV4OP_SAVEFH		32
220 #define	NFSV4OP_SECINFO		33
221 #define	NFSV4OP_SETATTR		34
222 #define	NFSV4OP_SETCLIENTID	35
223 #define	NFSV4OP_SETCLIENTIDCFRM	36
224 #define	NFSV4OP_VERIFY		37
225 #define	NFSV4OP_WRITE		38
226 #define	NFSV4OP_RELEASELCKOWN	39
227 
228 /*
229  * Must be one greater than the last Operation#.
230  */
231 #define	NFSV4OP_NOPS		40
232 
233 /*
234  * Additional Ops for NFSv4.1.
235  */
236 #define	NFSV4OP_BACKCHANNELCTL	40
237 #define	NFSV4OP_BINDCONNTOSESS	41
238 #define	NFSV4OP_EXCHANGEID	42
239 #define	NFSV4OP_CREATESESSION	43
240 #define	NFSV4OP_DESTROYSESSION	44
241 #define	NFSV4OP_FREESTATEID	45
242 #define	NFSV4OP_GETDIRDELEG	46
243 #define	NFSV4OP_GETDEVINFO	47
244 #define	NFSV4OP_GETDEVLIST	48
245 #define	NFSV4OP_LAYOUTCOMMIT	49
246 #define	NFSV4OP_LAYOUTGET	50
247 #define	NFSV4OP_LAYOUTRETURN	51
248 #define	NFSV4OP_SECINFONONAME	52
249 #define	NFSV4OP_SEQUENCE	53
250 #define	NFSV4OP_SETSSV		54
251 #define	NFSV4OP_TESTSTATEID	55
252 #define	NFSV4OP_WANTDELEG	56
253 #define	NFSV4OP_DESTROYCLIENTID	57
254 #define	NFSV4OP_RECLAIMCOMPL	58
255 
256 /*
257  * Must be one more than last op#.
258  */
259 #define	NFSV41_NOPS		59
260 
261 /* Quirky case if the illegal op code */
262 #define	NFSV4OP_OPILLEGAL	10044
263 
264 /*
265  * Fake NFSV4OP_xxx used for nfsstat. Start at NFSV4OP_NOPS.
266  */
267 #define	NFSV4OP_SYMLINK		(NFSV4OP_NOPS)
268 #define	NFSV4OP_MKDIR		(NFSV4OP_NOPS + 1)
269 #define	NFSV4OP_RMDIR		(NFSV4OP_NOPS + 2)
270 #define	NFSV4OP_READDIRPLUS	(NFSV4OP_NOPS + 3)
271 #define	NFSV4OP_MKNOD		(NFSV4OP_NOPS + 4)
272 #define	NFSV4OP_FSSTAT		(NFSV4OP_NOPS + 5)
273 #define	NFSV4OP_FSINFO		(NFSV4OP_NOPS + 6)
274 #define	NFSV4OP_PATHCONF	(NFSV4OP_NOPS + 7)
275 #define	NFSV4OP_V3CREATE	(NFSV4OP_NOPS + 8)
276 
277 /*
278  * This is the count of the fake operations listed above.
279  */
280 #define	NFSV4OP_FAKENOPS	9
281 
282 /*
283  * and the Callback OPs
284  */
285 #define	NFSV4OP_CBGETATTR	3
286 #define	NFSV4OP_CBRECALL	4
287 
288 /*
289  * Must be one greater than the last Callback Operation#.
290  */
291 #define	NFSV4OP_CBNOPS		5
292 
293 /*
294  * Additional Callback Ops for NFSv4.1 only. Not yet in nfsstats.
295  */
296 #define	NFSV4OP_CBLAYOUTRECALL	5
297 #define	NFSV4OP_CBNOTIFY	6
298 #define	NFSV4OP_CBPUSHDELEG	7
299 #define	NFSV4OP_CBRECALLANY	8
300 #define	NFSV4OP_CBRECALLOBJAVAIL 9
301 #define	NFSV4OP_CBRECALLSLOT	10
302 #define	NFSV4OP_CBSEQUENCE	11
303 #define	NFSV4OP_CBWANTCANCELLED	12
304 #define	NFSV4OP_CBNOTIFYLOCK	13
305 #define	NFSV4OP_CBNOTIFYDEVID	14
306 
307 /*
308  * The lower numbers -> 21 are used by NFSv2 and v3. These define higher
309  * numbers used by NFSv4.
310  * NFS_V3NPROCS is one greater than the last V3 op and NFS_NPROCS is
311  * one greater than the last number.
312  */
313 #ifndef	NFS_V3NPROCS
314 #define	NFS_V3NPROCS		22
315 
316 #define	NFSPROC_LOOKUPP		22
317 #define	NFSPROC_SETCLIENTID	23
318 #define	NFSPROC_SETCLIENTIDCFRM	24
319 #define	NFSPROC_LOCK		25
320 #define	NFSPROC_LOCKU		26
321 #define	NFSPROC_OPEN		27
322 #define	NFSPROC_CLOSE		28
323 #define	NFSPROC_OPENCONFIRM	29
324 #define	NFSPROC_LOCKT		30
325 #define	NFSPROC_OPENDOWNGRADE	31
326 #define	NFSPROC_RENEW		32
327 #define	NFSPROC_PUTROOTFH	33
328 #define	NFSPROC_RELEASELCKOWN	34
329 #define	NFSPROC_DELEGRETURN	35
330 #define	NFSPROC_RETDELEGREMOVE	36
331 #define	NFSPROC_RETDELEGRENAME1	37
332 #define	NFSPROC_RETDELEGRENAME2	38
333 #define	NFSPROC_GETACL		39
334 #define	NFSPROC_SETACL		40
335 
336 /*
337  * Must be defined as one higher than the last Proc# above.
338  */
339 #define	NFSV4_NPROCS		41
340 
341 /* Additional procedures for NFSv4.1. */
342 #define	NFSPROC_EXCHANGEID	41
343 #define	NFSPROC_CREATESESSION	42
344 #define	NFSPROC_DESTROYSESSION	43
345 #define	NFSPROC_DESTROYCLIENT	44
346 #define	NFSPROC_FREESTATEID	45
347 #define	NFSPROC_LAYOUTGET	46
348 #define	NFSPROC_GETDEVICEINFO	47
349 #define	NFSPROC_LAYOUTCOMMIT	48
350 #define	NFSPROC_LAYOUTRETURN	49
351 #define	NFSPROC_RECLAIMCOMPL	50
352 #define	NFSPROC_WRITEDS		51
353 #define	NFSPROC_READDS		52
354 #define	NFSPROC_COMMITDS	53
355 
356 /*
357  * Must be defined as one higher than the last NFSv4.1 Proc# above.
358  */
359 #define	NFSV41_NPROCS		54
360 
361 #endif	/* NFS_V3NPROCS */
362 
363 /*
364  * Stats structure
365  */
366 struct ext_nfsstats {
367 	int	attrcache_hits;
368 	int	attrcache_misses;
369 	int	lookupcache_hits;
370 	int	lookupcache_misses;
371 	int	direofcache_hits;
372 	int	direofcache_misses;
373 	int	accesscache_hits;
374 	int	accesscache_misses;
375 	int	biocache_reads;
376 	int	read_bios;
377 	int	read_physios;
378 	int	biocache_writes;
379 	int	write_bios;
380 	int	write_physios;
381 	int	biocache_readlinks;
382 	int	readlink_bios;
383 	int	biocache_readdirs;
384 	int	readdir_bios;
385 	int	rpccnt[NFSV4_NPROCS];
386 	int	rpcretries;
387 	int	srvrpccnt[NFSV4OP_NOPS + NFSV4OP_FAKENOPS];
388 	int	srvrpc_errs;
389 	int	srv_errs;
390 	int	rpcrequests;
391 	int	rpctimeouts;
392 	int	rpcunexpected;
393 	int	rpcinvalid;
394 	int	srvcache_inproghits;
395 	int	srvcache_idemdonehits;
396 	int	srvcache_nonidemdonehits;
397 	int	srvcache_misses;
398 	int	srvcache_tcppeak;
399 	int	srvcache_size;
400 	int	srvclients;
401 	int	srvopenowners;
402 	int	srvopens;
403 	int	srvlockowners;
404 	int	srvlocks;
405 	int	srvdelegates;
406 	int	cbrpccnt[NFSV4OP_CBNOPS];
407 	int	clopenowners;
408 	int	clopens;
409 	int	cllockowners;
410 	int	cllocks;
411 	int	cldelegates;
412 	int	cllocalopenowners;
413 	int	cllocalopens;
414 	int	cllocallockowners;
415 	int	cllocallocks;
416 };
417 
418 #ifdef _KERNEL
419 /*
420  * Define the ext_nfsstats as nfsstats for the kernel code.
421  */
422 #define nfsstats	ext_nfsstats
423 
424 /*
425  * Define NFS_NPROCS as NFSV4_NPROCS for the experimental kernel code.
426  */
427 #ifndef	NFS_NPROCS
428 #define	NFS_NPROCS		NFSV4_NPROCS
429 #endif
430 
431 #include <fs/nfs/nfskpiport.h>
432 #include <fs/nfs/nfsdport.h>
433 #include <fs/nfs/rpcv2.h>
434 #include <fs/nfs/nfsproto.h>
435 #include <fs/nfs/nfs.h>
436 #include <fs/nfs/nfsclstate.h>
437 #include <fs/nfs/nfs_var.h>
438 #include <fs/nfs/nfsm_subs.h>
439 #include <fs/nfs/nfsrvcache.h>
440 #include <fs/nfs/nfsrvstate.h>
441 #include <fs/nfs/xdr_subs.h>
442 #include <fs/nfs/nfscl.h>
443 #include <nfsclient/nfsargs.h>
444 #include <fs/nfsclient/nfsmount.h>
445 
446 /*
447  * Just to keep nfs_var.h happy.
448  */
449 struct nfs_vattr {
450 	int	junk;
451 };
452 
453 struct nfsvattr {
454 	struct vattr	na_vattr;
455 	nfsattrbit_t	na_suppattr;
456 	u_int32_t	na_mntonfileno;
457 	u_int64_t	na_filesid[2];
458 };
459 
460 #define	na_type		na_vattr.va_type
461 #define	na_mode		na_vattr.va_mode
462 #define	na_nlink	na_vattr.va_nlink
463 #define	na_uid		na_vattr.va_uid
464 #define	na_gid		na_vattr.va_gid
465 #define	na_fsid		na_vattr.va_fsid
466 #define	na_fileid	na_vattr.va_fileid
467 #define	na_size		na_vattr.va_size
468 #define	na_blocksize	na_vattr.va_blocksize
469 #define	na_atime	na_vattr.va_atime
470 #define	na_mtime	na_vattr.va_mtime
471 #define	na_ctime	na_vattr.va_ctime
472 #define	na_gen		na_vattr.va_gen
473 #define	na_flags	na_vattr.va_flags
474 #define	na_rdev		na_vattr.va_rdev
475 #define	na_bytes	na_vattr.va_bytes
476 #define	na_filerev	na_vattr.va_filerev
477 #define	na_vaflags	na_vattr.va_vaflags
478 
479 #include <fs/nfsclient/nfsnode.h>
480 
481 /*
482  * This is the header structure used for the lists, etc. (It has the
483  * above record in it.
484  */
485 struct nfsrv_stablefirst {
486 	LIST_HEAD(, nfsrv_stable) nsf_head;	/* Head of nfsrv_stable list */
487 	time_t		nsf_eograce;	/* Time grace period ends */
488 	time_t		*nsf_bootvals;	/* Previous boottime values */
489 	struct file	*nsf_fp;	/* File table pointer */
490 	u_char		nsf_flags;	/* NFSNSF_ flags */
491 	struct nfsf_rec	nsf_rec;	/* and above first record */
492 };
493 #define	nsf_lease	nsf_rec.lease
494 #define	nsf_numboots	nsf_rec.numboots
495 
496 /* NFSNSF_xxx flags */
497 #define	NFSNSF_UPDATEDONE	0x01
498 #define	NFSNSF_GRACEOVER	0x02
499 #define	NFSNSF_NEEDLOCK		0x04
500 #define	NFSNSF_EXPIREDCLIENT	0x08
501 #define	NFSNSF_NOOPENS		0x10
502 #define	NFSNSF_OK		0x20
503 
504 /*
505  * Maximum number of boot times allowed in record. Although there is
506  * really no need for a fixed upper bound, this serves as a sanity check
507  * for a corrupted file.
508  */
509 #define	NFSNSF_MAXNUMBOOTS	10000
510 
511 /*
512  * This structure defines the other records in the file. The
513  * nst_client array is actually the size of the client string name.
514  */
515 struct nfst_rec {
516 	u_int16_t	len;
517 	u_char		flag;
518 	u_char		client[1];
519 };
520 /* and the values for flag */
521 #define	NFSNST_NEWSTATE	0x1
522 #define	NFSNST_REVOKE		0x2
523 #define	NFSNST_GOTSTATE		0x4
524 
525 /*
526  * This structure is linked onto nfsrv_stablefirst for the duration of
527  * reclaim.
528  */
529 struct nfsrv_stable {
530 	LIST_ENTRY(nfsrv_stable) nst_list;
531 	struct nfsclient	*nst_clp;
532 	struct nfst_rec		nst_rec;
533 };
534 #define	nst_timestamp	nst_rec.timestamp
535 #define	nst_len		nst_rec.len
536 #define	nst_flag	nst_rec.flag
537 #define	nst_client	nst_rec.client
538 
539 /*
540  * At some point the server will run out of kernel storage for
541  * state structures. For FreeBSD5.2, this results in a panic
542  * kmem_map is full. It happens at well over 1000000 opens plus
543  * locks on a PIII-800 with 256Mbytes, so that is where I've set
544  * the limit. If your server panics due to too many opens/locks,
545  * decrease the size of NFSRV_V4STATELIMIT. If you find the server
546  * returning NFS4ERR_RESOURCE a lot and have lots of memory, try
547  * increasing it.
548  */
549 #define	NFSRV_V4STATELIMIT	500000	/* Max # of Opens + Locks */
550 
551 /*
552  * The type required differs with BSDen (just the second arg).
553  */
554 void nfsrvd_rcv(struct socket *, void *, int);
555 
556 /*
557  * Macros for handling socket addresses. (Hopefully this makes the code
558  * more portable, since I've noticed some 'BSD don't have sockaddrs in
559  * mbufs any more.)
560  */
561 #define	NFSSOCKADDR(a, t)	((t)(a))
562 #define	NFSSOCKADDRALLOC(a) 					\
563     do {							\
564 	MALLOC((a), struct sockaddr *, sizeof (struct sockaddr), \
565 	    M_SONAME, M_WAITOK); 				\
566 	NFSBZERO((a), sizeof (struct sockaddr)); 		\
567     } while (0)
568 #define	NFSSOCKADDRSIZE(a, s)		((a)->sa_len = (s))
569 #define	NFSSOCKADDRFREE(a) 					\
570 	do { 							\
571 		if (a) 						\
572 			FREE((caddr_t)(a), M_SONAME); 		\
573 	} while (0)
574 
575 /*
576  * These should be defined as a process or thread structure, as required
577  * for signal handling, etc.
578  */
579 #define	NFSNEWCRED(c)		(crdup(c))
580 #define	NFSPROCCRED(p)		((p)->td_ucred)
581 #define	NFSFREECRED(c)		(crfree(c))
582 #define	NFSUIOPROC(u, p)	((u)->uio_td = NULL)
583 #define	NFSPROCP(p)		((p)->td_proc)
584 
585 /*
586  * Define these so that cn_hash and its length is ignored.
587  */
588 #define	NFSCNHASHZERO(c)
589 #define	NFSCNHASH(c, v)
590 #define	NCHNAMLEN	9999999
591 
592 /*
593  * These macros are defined to initialize and set the timer routine.
594  */
595 #define	NFS_TIMERINIT \
596 	newnfs_timer(NULL)
597 
598 /*
599  * Handle SMP stuff:
600  */
601 #define	NFSSTATESPINLOCK	extern struct mtx nfs_state_mutex
602 #define	NFSLOCKSTATE()		mtx_lock(&nfs_state_mutex)
603 #define	NFSUNLOCKSTATE()	mtx_unlock(&nfs_state_mutex)
604 #define	NFSSTATEMUTEXPTR	(&nfs_state_mutex)
605 #define	NFSREQSPINLOCK		extern struct mtx nfs_req_mutex
606 #define	NFSLOCKREQ()		mtx_lock(&nfs_req_mutex)
607 #define	NFSUNLOCKREQ()		mtx_unlock(&nfs_req_mutex)
608 #define	NFSSOCKMUTEX		extern struct mtx nfs_slock_mutex
609 #define	NFSSOCKMUTEXPTR		(&nfs_slock_mutex)
610 #define	NFSLOCKSOCK()		mtx_lock(&nfs_slock_mutex)
611 #define	NFSUNLOCKSOCK()		mtx_unlock(&nfs_slock_mutex)
612 #define	NFSNAMEIDMUTEX		extern struct mtx nfs_nameid_mutex
613 #define	NFSLOCKNAMEID()		mtx_lock(&nfs_nameid_mutex)
614 #define	NFSUNLOCKNAMEID()	mtx_unlock(&nfs_nameid_mutex)
615 #define	NFSNAMEIDREQUIRED()	mtx_assert(&nfs_nameid_mutex, MA_OWNED)
616 #define	NFSCLSTATEMUTEX		extern struct mtx nfs_clstate_mutex
617 #define	NFSCLSTATEMUTEXPTR	(&nfs_clstate_mutex)
618 #define	NFSLOCKCLSTATE()	mtx_lock(&nfs_clstate_mutex)
619 #define	NFSUNLOCKCLSTATE()	mtx_unlock(&nfs_clstate_mutex)
620 #define	NFSDLOCKMUTEX		extern struct mtx newnfsd_mtx
621 #define	NFSDLOCKMUTEXPTR	(&newnfsd_mtx)
622 #define	NFSD_LOCK()		mtx_lock(&newnfsd_mtx)
623 #define	NFSD_UNLOCK()		mtx_unlock(&newnfsd_mtx)
624 #define	NFSD_LOCK_ASSERT()	mtx_assert(&newnfsd_mtx, MA_OWNED)
625 #define	NFSD_UNLOCK_ASSERT()	mtx_assert(&newnfsd_mtx, MA_NOTOWNED)
626 #define	NFSV4ROOTLOCKMUTEX	extern struct mtx nfs_v4root_mutex
627 #define	NFSV4ROOTLOCKMUTEXPTR	(&nfs_v4root_mutex)
628 #define	NFSLOCKV4ROOTMUTEX()	mtx_lock(&nfs_v4root_mutex)
629 #define	NFSUNLOCKV4ROOTMUTEX()	mtx_unlock(&nfs_v4root_mutex)
630 #define	NFSLOCKNODE(n)		mtx_lock(&((n)->n_mtx))
631 #define	NFSUNLOCKNODE(n)	mtx_unlock(&((n)->n_mtx))
632 #define	NFSLOCKMNT(m)		mtx_lock(&((m)->nm_mtx))
633 #define	NFSUNLOCKMNT(m)		mtx_unlock(&((m)->nm_mtx))
634 #define	NFSLOCKREQUEST(r)	mtx_lock(&((r)->r_mtx))
635 #define	NFSUNLOCKREQUEST(r)	mtx_unlock(&((r)->r_mtx))
636 #define	NFSPROCLISTLOCK()	sx_slock(&allproc_lock)
637 #define	NFSPROCLISTUNLOCK()	sx_sunlock(&allproc_lock)
638 #define	NFSLOCKSOCKREQ(r)	mtx_lock(&((r)->nr_mtx))
639 #define	NFSUNLOCKSOCKREQ(r)	mtx_unlock(&((r)->nr_mtx))
640 #define	NFSLOCKDS(d)		mtx_lock(&((d)->nfsclds_mtx))
641 #define	NFSUNLOCKDS(d)		mtx_unlock(&((d)->nfsclds_mtx))
642 
643 /*
644  * Use these macros to initialize/free a mutex.
645  */
646 #define	NFSINITSOCKMUTEX(m)	mtx_init((m), "nfssock", NULL, MTX_DEF)
647 #define	NFSFREEMUTEX(m)		mtx_destroy((m))
648 
649 int nfsmsleep(void *, void *, int, const char *, struct timespec *);
650 
651 /*
652  * And weird vm stuff in the nfs server.
653  */
654 #define	PDIRUNLOCK	0x0
655 #define	MAX_COMMIT_COUNT	(1024 * 1024)
656 
657 /*
658  * Define these to handle the type of va_rdev.
659  */
660 #define	NFSMAKEDEV(m, n)	makedev((m), (n))
661 #define	NFSMAJOR(d)		major(d)
662 #define	NFSMINOR(d)		minor(d)
663 
664 /*
665  * Define this to be the macro that returns the minimum size required
666  * for a directory entry.
667  */
668 #define	DIRENT_SIZE(dp)		GENERIC_DIRSIZ(dp)
669 
670 /*
671  * The vnode tag for nfsv4root.
672  */
673 #define	VT_NFSV4ROOT		"nfsv4root"
674 
675 /*
676  * Define whatever it takes to do a vn_rdwr().
677  */
678 #define	NFSD_RDWR(r, v, b, l, o, s, i, c, a, p) \
679 	vn_rdwr((r), (v), (b), (l), (o), (s), (i), (c), NULL, (a), (p))
680 
681 /*
682  * Macros for handling memory for different BSDen.
683  * NFSBCOPY(src, dst, len) - copies len bytes, non-overlapping
684  * NFSOVBCOPY(src, dst, len) - ditto, but data areas might overlap
685  * NFSBCMP(cp1, cp2, len) - compare len bytes, return 0 if same
686  * NFSBZERO(cp, len) - set len bytes to 0x0
687  */
688 #define	NFSBCOPY(s, d, l)	bcopy((s), (d), (l))
689 #define	NFSOVBCOPY(s, d, l)	ovbcopy((s), (d), (l))
690 #define	NFSBCMP(s, d, l)	bcmp((s), (d), (l))
691 #define	NFSBZERO(s, l)		bzero((s), (l))
692 
693 /*
694  * Some queue.h files don't have these dfined in them.
695  */
696 #define	LIST_END(head)		NULL
697 #define	SLIST_END(head)		NULL
698 #define	TAILQ_END(head)		NULL
699 
700 /*
701  * This must be defined to be a global variable that increments once
702  * per second, but never stops or goes backwards, even when a "date"
703  * command changes the TOD clock. It is used for delta times for
704  * leases, etc.
705  */
706 #define	NFSD_MONOSEC		time_uptime
707 
708 /*
709  * Declare the malloc types.
710  */
711 MALLOC_DECLARE(M_NEWNFSRVCACHE);
712 MALLOC_DECLARE(M_NEWNFSDCLIENT);
713 MALLOC_DECLARE(M_NEWNFSDSTATE);
714 MALLOC_DECLARE(M_NEWNFSDLOCK);
715 MALLOC_DECLARE(M_NEWNFSDLOCKFILE);
716 MALLOC_DECLARE(M_NEWNFSSTRING);
717 MALLOC_DECLARE(M_NEWNFSUSERGROUP);
718 MALLOC_DECLARE(M_NEWNFSDREQ);
719 MALLOC_DECLARE(M_NEWNFSFH);
720 MALLOC_DECLARE(M_NEWNFSCLOWNER);
721 MALLOC_DECLARE(M_NEWNFSCLOPEN);
722 MALLOC_DECLARE(M_NEWNFSCLDELEG);
723 MALLOC_DECLARE(M_NEWNFSCLCLIENT);
724 MALLOC_DECLARE(M_NEWNFSCLLOCKOWNER);
725 MALLOC_DECLARE(M_NEWNFSCLLOCK);
726 MALLOC_DECLARE(M_NEWNFSDIROFF);
727 MALLOC_DECLARE(M_NEWNFSV4NODE);
728 MALLOC_DECLARE(M_NEWNFSDIRECTIO);
729 MALLOC_DECLARE(M_NEWNFSMNT);
730 MALLOC_DECLARE(M_NEWNFSDROLLBACK);
731 MALLOC_DECLARE(M_NEWNFSLAYOUT);
732 MALLOC_DECLARE(M_NEWNFSFLAYOUT);
733 MALLOC_DECLARE(M_NEWNFSDEVINFO);
734 MALLOC_DECLARE(M_NEWNFSSOCKREQ);
735 MALLOC_DECLARE(M_NEWNFSCLDS);
736 MALLOC_DECLARE(M_NEWNFSLAYRECALL);
737 #define	M_NFSRVCACHE	M_NEWNFSRVCACHE
738 #define	M_NFSDCLIENT	M_NEWNFSDCLIENT
739 #define	M_NFSDSTATE	M_NEWNFSDSTATE
740 #define	M_NFSDLOCK	M_NEWNFSDLOCK
741 #define	M_NFSDLOCKFILE	M_NEWNFSDLOCKFILE
742 #define	M_NFSSTRING	M_NEWNFSSTRING
743 #define	M_NFSUSERGROUP	M_NEWNFSUSERGROUP
744 #define	M_NFSDREQ	M_NEWNFSDREQ
745 #define	M_NFSFH		M_NEWNFSFH
746 #define	M_NFSCLOWNER	M_NEWNFSCLOWNER
747 #define	M_NFSCLOPEN	M_NEWNFSCLOPEN
748 #define	M_NFSCLDELEG	M_NEWNFSCLDELEG
749 #define	M_NFSCLCLIENT	M_NEWNFSCLCLIENT
750 #define	M_NFSCLLOCKOWNER M_NEWNFSCLLOCKOWNER
751 #define	M_NFSCLLOCK	M_NEWNFSCLLOCK
752 #define	M_NFSDIROFF	M_NEWNFSDIROFF
753 #define	M_NFSV4NODE	M_NEWNFSV4NODE
754 #define	M_NFSDIRECTIO	M_NEWNFSDIRECTIO
755 #define	M_NFSDROLLBACK	M_NEWNFSDROLLBACK
756 #define	M_NFSLAYOUT	M_NEWNFSLAYOUT
757 #define	M_NFSFLAYOUT	M_NEWNFSFLAYOUT
758 #define	M_NFSDEVINFO	M_NEWNFSDEVINFO
759 #define	M_NFSSOCKREQ	M_NEWNFSSOCKREQ
760 #define	M_NFSCLDS	M_NEWNFSCLDS
761 #define	M_NFSLAYRECALL	M_NEWNFSLAYRECALL
762 
763 #define	NFSINT_SIGMASK(set) 						\
764 	(SIGISMEMBER(set, SIGINT) || SIGISMEMBER(set, SIGTERM) ||	\
765 	 SIGISMEMBER(set, SIGHUP) || SIGISMEMBER(set, SIGKILL) ||	\
766 	 SIGISMEMBER(set, SIGQUIT))
767 
768 /*
769  * Convert a quota block count to byte count.
770  */
771 #define	NFSQUOTABLKTOBYTE(q, b)	(q) *= (b)
772 
773 /*
774  * Define this as the largest file size supported. (It should probably
775  * be available via a VFS_xxx Op, but it isn't.
776  */
777 #define	NFSRV_MAXFILESIZE	((u_int64_t)0x800000000000)
778 
779 /*
780  * Set this macro to index() or strchr(), whichever is supported.
781  */
782 #define	STRCHR(s, c)		strchr((s), (c))
783 
784 /*
785  * Set the n_time in the client write rpc, as required.
786  */
787 #define	NFSWRITERPC_SETTIME(w, n, v4)					\
788 	do {								\
789 		if (w) {						\
790 			(n)->n_mtime = (n)->n_vattr.na_vattr.va_mtime; \
791 			if (v4)						\
792 			    (n)->n_change = (n)->n_vattr.na_vattr.va_filerev; \
793 		}							\
794 	} while (0)
795 
796 /*
797  * Fake value, just to make the client work.
798  */
799 #define	NFS_LATTR_NOSHRINK	1
800 
801 /*
802  * Prototypes for functions where the arguments vary for different ports.
803  */
804 int nfscl_loadattrcache(struct vnode **, struct nfsvattr *, void *, void *,
805     int, int);
806 int newnfs_realign(struct mbuf **, int);
807 
808 /*
809  * If the port runs on an SMP box that can enforce Atomic ops with low
810  * overheads, define these as atomic increments/decrements. If not,
811  * don't worry about it, since these are used for stats that can be
812  * "out by one" without disastrous consequences.
813  */
814 #define	NFSINCRGLOBAL(a)	((a)++)
815 
816 /*
817  * Assorted funky stuff to make things work under Darwin8.
818  */
819 /*
820  * These macros checks for a field in vattr being set.
821  */
822 #define	NFSATTRISSET(t, v, a)	((v)->a != (t)VNOVAL)
823 #define	NFSATTRISSETTIME(v, a)	((v)->a.tv_sec != VNOVAL)
824 
825 /*
826  * Manipulate mount flags.
827  */
828 #define	NFSSTA_HASWRITEVERF	0x00040000  /* Has write verifier */
829 #define	NFSSTA_GOTFSINFO	0x00100000  /* Got the fsinfo */
830 #define	NFSSTA_NOLAYOUTCOMMIT	0x04000000  /* Don't do LayoutCommit */
831 #define	NFSSTA_SESSPERSIST	0x08000000  /* Has a persistent session */
832 #define	NFSSTA_TIMEO		0x10000000  /* Experiencing a timeout */
833 #define	NFSSTA_LOCKTIMEO	0x20000000  /* Experiencing a lockd timeout */
834 #define	NFSSTA_HASSETFSID	0x40000000  /* Has set the fsid */
835 #define	NFSSTA_PNFS		0x80000000  /* pNFS is enabled */
836 
837 #define	NFSHASNFSV3(n)		((n)->nm_flag & NFSMNT_NFSV3)
838 #define	NFSHASNFSV4(n)		((n)->nm_flag & NFSMNT_NFSV4)
839 #define	NFSHASNFSV4N(n)		((n)->nm_minorvers > 0)
840 #define	NFSHASNFSV3OR4(n)	((n)->nm_flag & (NFSMNT_NFSV3 | NFSMNT_NFSV4))
841 #define	NFSHASGOTFSINFO(n)	((n)->nm_state & NFSSTA_GOTFSINFO)
842 #define	NFSHASHASSETFSID(n)	((n)->nm_state & NFSSTA_HASSETFSID)
843 #define	NFSHASSTRICT3530(n)	((n)->nm_flag & NFSMNT_STRICT3530)
844 #define	NFSHASWRITEVERF(n)	((n)->nm_state & NFSSTA_HASWRITEVERF)
845 #define	NFSHASINT(n)		((n)->nm_flag & NFSMNT_INT)
846 #define	NFSHASSOFT(n)		((n)->nm_flag & NFSMNT_SOFT)
847 #define	NFSHASINTORSOFT(n)	((n)->nm_flag & (NFSMNT_INT | NFSMNT_SOFT))
848 #define	NFSHASDUMBTIMR(n)	((n)->nm_flag & NFSMNT_DUMBTIMR)
849 #define	NFSHASNOCONN(n)		((n)->nm_flag & NFSMNT_MNTD)
850 #define	NFSHASKERB(n)		((n)->nm_flag & NFSMNT_KERB)
851 #define	NFSHASALLGSSNAME(n)	((n)->nm_flag & NFSMNT_ALLGSSNAME)
852 #define	NFSHASINTEGRITY(n)	((n)->nm_flag & NFSMNT_INTEGRITY)
853 #define	NFSHASPRIVACY(n)	((n)->nm_flag & NFSMNT_PRIVACY)
854 #define	NFSSETWRITEVERF(n)	((n)->nm_state |= NFSSTA_HASWRITEVERF)
855 #define	NFSSETHASSETFSID(n)	((n)->nm_state |= NFSSTA_HASSETFSID)
856 #define	NFSHASPNFSOPT(n)	((n)->nm_flag & NFSMNT_PNFS)
857 #define	NFSHASNOLAYOUTCOMMIT(n)	((n)->nm_state & NFSSTA_NOLAYOUTCOMMIT)
858 #define	NFSHASSESSPERSIST(n)	((n)->nm_state & NFSSTA_SESSPERSIST)
859 #define	NFSHASPNFS(n)		((n)->nm_state & NFSSTA_PNFS)
860 
861 /*
862  * Gets the stats field out of the mount structure.
863  */
864 #define	vfs_statfs(m)	(&((m)->mnt_stat))
865 
866 /*
867  * Set boottime.
868  */
869 #define	NFSSETBOOTTIME(b)	((b) = boottime)
870 
871 /*
872  * The size of directory blocks in the buffer cache.
873  * MUST BE in the range of PAGE_SIZE <= NFS_DIRBLKSIZ <= MAXBSIZE!!
874  */
875 #define	NFS_DIRBLKSIZ	(16 * DIRBLKSIZ) /* Must be a multiple of DIRBLKSIZ */
876 
877 /*
878  * Define these macros to access mnt_flag fields.
879  */
880 #define	NFSMNT_RDONLY(m)	((m)->mnt_flag & MNT_RDONLY)
881 #endif	/* _KERNEL */
882 
883 /*
884  * Define a structure similar to ufs_args for use in exporting the V4 root.
885  */
886 struct nfsex_args {
887 	char	*fspec;
888 	struct export_args	export;
889 };
890 
891 /*
892  * These export flags should be defined, but there are no bits left.
893  * Maybe a separate mnt_exflag field could be added or the mnt_flag
894  * field increased to 64 bits?
895  */
896 #ifndef	MNT_EXSTRICTACCESS
897 #define	MNT_EXSTRICTACCESS	0x0
898 #endif
899 #ifndef MNT_EXV4ONLY
900 #define	MNT_EXV4ONLY		0x0
901 #endif
902 
903 #ifdef _KERNEL
904 /*
905  * Define this to invalidate the attribute cache for the nfs node.
906  */
907 #define	NFSINVALATTRCACHE(n)	((n)->n_attrstamp = 0)
908 
909 /* Used for FreeBSD only */
910 void nfsd_mntinit(void);
911 
912 /*
913  * Define these for vnode lock/unlock ops.
914  *
915  * These are good abstractions to macro out, so that they can be added to
916  * later, for debugging or stats, etc.
917  */
918 #define	NFSVOPLOCK(v, f)	vn_lock((v), (f))
919 #define	NFSVOPUNLOCK(v, f)	VOP_UNLOCK((v), (f))
920 #define	NFSVOPISLOCKED(v)	VOP_ISLOCKED((v))
921 
922 /*
923  * Define ncl_hash().
924  */
925 #define	ncl_hash(f, l)	(fnv_32_buf((f), (l), FNV1_32_INIT))
926 
927 int newnfs_iosize(struct nfsmount *);
928 
929 #ifdef NFS_DEBUG
930 
931 extern int nfs_debug;
932 #define	NFS_DEBUG_ASYNCIO	1 /* asynchronous i/o */
933 #define	NFS_DEBUG_WG		2 /* server write gathering */
934 #define	NFS_DEBUG_RC		4 /* server request caching */
935 
936 #define	NFS_DPF(cat, args)					\
937 	do {							\
938 		if (nfs_debug & NFS_DEBUG_##cat) printf args;	\
939 	} while (0)
940 
941 #else
942 
943 #define	NFS_DPF(cat, args)
944 
945 #endif
946 
947 int newnfs_vncmpf(struct vnode *, void *);
948 
949 #ifndef NFS_MINDIRATTRTIMO
950 #define	NFS_MINDIRATTRTIMO 3		/* VDIR attrib cache timeout in sec */
951 #endif
952 #ifndef NFS_MAXDIRATTRTIMO
953 #define	NFS_MAXDIRATTRTIMO 60
954 #endif
955 
956 /*
957  * Nfs outstanding request list element
958  */
959 struct nfsreq {
960 	TAILQ_ENTRY(nfsreq) r_chain;
961 	u_int32_t	r_flags;	/* flags on request, see below */
962 	struct nfsmount *r_nmp;		/* Client mnt ptr */
963 	struct mtx	r_mtx;		/* Mutex lock for this structure */
964 };
965 
966 #ifndef NFS_MAXBSIZE
967 #define	NFS_MAXBSIZE	MAXBSIZE
968 #endif
969 
970 /*
971  * This macro checks to see if issuing of delegations is allowed for this
972  * vnode.
973  */
974 #ifdef VV_DISABLEDELEG
975 #define	NFSVNO_DELEGOK(v)						\
976 	((v) == NULL || ((v)->v_vflag & VV_DISABLEDELEG) == 0)
977 #else
978 #define	NFSVNO_DELEGOK(v)	(1)
979 #endif
980 
981 #endif	/* _KERNEL */
982 
983 #endif	/* _NFS_NFSPORT_H */
984