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