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