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