nfsport.h revision 192152
1209513Simp/*-
2209513Simp * Copyright (c) 1989, 1993
3209552Simp *	The Regents of the University of California.  All rights reserved.
4209513Simp *
5209513Simp * This code is derived from software contributed to Berkeley by
6209513Simp * Rick Macklem at The University of Guelph.
7209513Simp *
8209513Simp * Redistribution and use in source and binary forms, with or without
9209513Simp * modification, are permitted provided that the following conditions
10209513Simp * are met:
11209513Simp * 1. Redistributions of source code must retain the above copyright
12209513Simp *    notice, this list of conditions and the following disclaimer.
13209513Simp * 2. Redistributions in binary form must reproduce the above copyright
14209513Simp *    notice, this list of conditions and the following disclaimer in the
15209513Simp *    documentation and/or other materials provided with the distribution.
16209513Simp * 4. Neither the name of the University nor the names of its contributors
17209513Simp *    may be used to endorse or promote products derived from this software
18209513Simp *    without specific prior written permission.
19209513Simp *
20209513Simp * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
21209513Simp * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
22209513Simp * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
23209513Simp * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
24209513Simp * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
25209513Simp * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
26209513Simp * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
27209513Simp * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
28209513Simp * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
29209513Simp * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
30209513Simp * SUCH DAMAGE.
31209513Simp *
32209513Simp * $FreeBSD: head/sys/fs/nfs/nfsport.h 192152 2009-05-15 19:33:59Z rmacklem $
33209513Simp */
34209513Simp
35209513Simp#ifndef _NFS_NFSPORT_H_
36209513Simp#define	_NFSPORT_NFS_H_
37209513Simp
38209513Simp/*
39209513Simp * In general, I'm not fond of #includes in .h files, but this seems
40209513Simp * to be the cleanest way to handle #include files for the ports.
41218801Sjpaetzel */
42209513Simp#ifdef _KERNEL
43218801Sjpaetzel#include <sys/unistd.h>
44209513Simp#include <sys/param.h>
45209513Simp#include <sys/systm.h>
46218801Sjpaetzel#include <sys/conf.h>
47218801Sjpaetzel#include <sys/dirent.h>
48218801Sjpaetzel#include <sys/domain.h>
49218801Sjpaetzel#include <sys/fcntl.h>
50218801Sjpaetzel#include <sys/file.h>
51218801Sjpaetzel#include <sys/filedesc.h>
52218801Sjpaetzel#include <sys/kernel.h>
53218801Sjpaetzel#include <sys/lockf.h>
54218801Sjpaetzel#include <sys/malloc.h>
55218801Sjpaetzel#include <sys/mbuf.h>
56218801Sjpaetzel#include <sys/mount.h>
57218801Sjpaetzel#include <sys/namei.h>
58218801Sjpaetzel#include <sys/proc.h>
59218801Sjpaetzel#include <sys/protosw.h>
60218801Sjpaetzel#include <sys/reboot.h>
61218801Sjpaetzel#include <sys/resourcevar.h>
62218801Sjpaetzel#include <sys/signalvar.h>
63218801Sjpaetzel#include <sys/socket.h>
64209513Simp#include <sys/socketvar.h>
65209513Simp#include <sys/stat.h>
66218801Sjpaetzel#include <sys/syslog.h>
67220059Sjpaetzel#include <sys/sysproto.h>
68218801Sjpaetzel#include <sys/time.h>
69218801Sjpaetzel#include <sys/uio.h>
70218801Sjpaetzel#include <sys/vnode.h>
71218801Sjpaetzel#include <sys/bio.h>
72218801Sjpaetzel#include <sys/buf.h>
73218801Sjpaetzel#include <sys/acl.h>
74218801Sjpaetzel#include <sys/module.h>
75218801Sjpaetzel#include <sys/sysent.h>
76218801Sjpaetzel#include <sys/syscall.h>
77209513Simp#include <sys/priv.h>
78218801Sjpaetzel#include <sys/kthread.h>
79209513Simp#include <sys/syscallsubr.h>
80218801Sjpaetzel#include <fs/fifofs/fifo.h>
81209513Simp#include <net/if.h>
82209513Simp#include <net/radix.h>
83209513Simp#include <net/route.h>
84209513Simp#include <net/if_dl.h>
85209513Simp#include <netinet/in.h>
86209513Simp#include <netinet/in_pcb.h>
87211730Simp#include <netinet/in_systm.h>
88211730Simp#include <netinet/in_var.h>
89211730Simp#include <netinet/ip.h>
90211730Simp#include <netinet/ip_var.h>
91211730Simp#include <netinet/tcp.h>
92211730Simp#include <netinet/tcp_fsm.h>
93209513Simp#include <netinet/tcp_seq.h>
94211730Simp#include <netinet/tcp_timer.h>
95211730Simp#include <netinet/tcp_var.h>
96211730Simp#include <netinet/vinet.h>
97211730Simp#include <machine/in_cksum.h>
98211730Simp#include <crypto/des/des.h>
99211730Simp#include <sys/md5.h>
100211730Simp#include <rpc/rpc.h>
101209513Simp#include <rpc/rpcclnt.h>
102211730Simp#include <rpc/rpcsec_gss.h>
103211730Simp
104211730Simp/*
105211730Simp * For Darwin, these functions should be "static" when built in a kext.
106211730Simp * (This is always defined as nil otherwise.)
107211730Simp */
108211730Simp#define	APPLESTATIC
109209513Simp#include <ufs/ufs/dir.h>
110211730Simp#include <ufs/ufs/quota.h>
111211730Simp#include <ufs/ufs/inode.h>
112211730Simp#include <ufs/ufs/extattr.h>
113211730Simp#include <ufs/ufs/ufsmount.h>
114211730Simp#include <vm/uma.h>
115211730Simp#include <vm/vm.h>
116211730Simp#include <vm/vm_object.h>
117211730Simp#include <vm/vm_extern.h>
118209513Simp#include <nfs/nfssvc.h>
119211730Simp#include "opt_nfs.h"
120211730Simp#include "opt_ufs.h"
121211730Simp
122211730Simp/*
123211730Simp * These types must be defined before the nfs includes.
124211730Simp */
125211730Simp#define	NFSSOCKADDR_T	struct sockaddr *
126209513Simp#define	NFSPROC_T	struct thread
127211730Simp#define	NFSDEV_T	dev_t
128211730Simp#define	NFSSVCARGS	nfssvc_args
129211730Simp#ifdef NFS4_ACL_EXTATTR_NAME
130211730Simp#define	NFSACL_T	struct acl
131211730Simp#else
132211730Simp#define	NFSACL_T	void
133211730Simp#endif
134209513Simp
135211730Simp/*
136211730Simp * These should be defined as the types used for the corresponding VOP's
137211730Simp * argument type.
138211730Simp */
139209513Simp#define	NFS_ACCESS_ARGS		struct vop_access_args
140211730Simp#define	NFS_OPEN_ARGS		struct vop_open_args
141218801Sjpaetzel#define	NFS_GETATTR_ARGS	struct vop_getattr_args
142218801Sjpaetzel#define	NFS_LOOKUP_ARGS		struct vop_lookup_args
143218801Sjpaetzel#define	NFS_READDIR_ARGS	struct vop_readdir_args
144211730Simp
145209513Simp/*
146218801Sjpaetzel * Allocate mbufs. Must succeed and never set the mbuf ptr to NULL.
147218801Sjpaetzel */
148218801Sjpaetzel#define	NFSMGET(m)	do { 					\
149209513Simp		MGET((m), M_TRYWAIT, MT_DATA); 			\
150209513Simp		while ((m) == NULL ) { 				\
151209513Simp			(void) nfs_catnap(PZERO, "nfsmget");	\
152209513Simp			MGET((m), M_TRYWAIT, MT_DATA); 		\
153209513Simp		} 						\
154209513Simp	} while (0)
155209513Simp#define	NFSMGETHDR(m)	do { 					\
156209513Simp		MGETHDR((m), M_TRYWAIT, MT_DATA);		\
157209513Simp		while ((m) == NULL ) { 				\
158209513Simp			(void) nfs_catnap(PZERO, "nfsmget");	\
159218801Sjpaetzel			MGETHDR((m), M_TRYWAIT, MT_DATA); 	\
160209513Simp		} 						\
161209513Simp	} while (0)
162209513Simp#define	NFSMCLGET(m, w)	do { 					\
163209513Simp		MGET((m), M_TRYWAIT, MT_DATA); 			\
164209513Simp		while ((m) == NULL ) { 				\
165209513Simp			(void) nfs_catnap(PZERO, "nfsmget");	\
166209513Simp			MGET((m), M_TRYWAIT, MT_DATA); 		\
167209513Simp		} 						\
168209513Simp		MCLGET((m), (w));				\
169209513Simp	} while (0)
170209513Simp#define	NFSMCLGETHDR(m, w) do { 				\
171236330Sjpaetzel		MGETHDR((m), M_TRYWAIT, MT_DATA);		\
172209513Simp		while ((m) == NULL ) { 				\
173209513Simp			(void) nfs_catnap(PZERO, "nfsmget");	\
174209513Simp			MGETHDR((m), M_TRYWAIT, MT_DATA); 	\
175209513Simp		} 						\
176209513Simp	} while (0)
177209513Simp#define	NFSMTOD	mtod
178209513Simp
179209513Simp/*
180209513Simp * Client side constant for size of a lockowner name.
181209513Simp */
182209513Simp#define	NFSV4CL_LOCKNAMELEN	12
183209513Simp
184209513Simp/*
185209513Simp * Type for a mutex lock.
186209513Simp */
187209513Simp#define	NFSMUTEX_T		struct mtx
188209513Simp
189209513Simp#endif	/* _KERNEL */
190209513Simp
191209513Simp/*
192209513Simp * NFSv4 Operation numbers.
193209513Simp */
194209513Simp#define	NFSV4OP_ACCESS		3
195209513Simp#define	NFSV4OP_CLOSE		4
196220059Sjpaetzel#define	NFSV4OP_COMMIT		5
197209513Simp#define	NFSV4OP_CREATE		6
198209513Simp#define	NFSV4OP_DELEGPURGE	7
199209513Simp#define	NFSV4OP_DELEGRETURN	8
200209513Simp#define	NFSV4OP_GETATTR		9
201209513Simp#define	NFSV4OP_GETFH		10
202209513Simp#define	NFSV4OP_LINK		11
203209513Simp#define	NFSV4OP_LOCK		12
204209513Simp#define	NFSV4OP_LOCKT		13
205209513Simp#define	NFSV4OP_LOCKU		14
206209513Simp#define	NFSV4OP_LOOKUP		15
207209513Simp#define	NFSV4OP_LOOKUPP		16
208209513Simp#define	NFSV4OP_NVERIFY		17
209209513Simp#define	NFSV4OP_OPEN		18
210209513Simp#define	NFSV4OP_OPENATTR	19
211209513Simp#define	NFSV4OP_OPENCONFIRM	20
212209513Simp#define	NFSV4OP_OPENDOWNGRADE	21
213209513Simp#define	NFSV4OP_PUTFH		22
214209513Simp#define	NFSV4OP_PUTPUBFH	23
215209513Simp#define	NFSV4OP_PUTROOTFH	24
216209513Simp#define	NFSV4OP_READ		25
217209513Simp#define	NFSV4OP_READDIR		26
218220059Sjpaetzel#define	NFSV4OP_READLINK	27
219209513Simp#define	NFSV4OP_REMOVE		28
220209513Simp#define	NFSV4OP_RENAME		29
221220059Sjpaetzel#define	NFSV4OP_RENEW		30
222209513Simp#define	NFSV4OP_RESTOREFH	31
223209513Simp#define	NFSV4OP_SAVEFH		32
224209513Simp#define	NFSV4OP_SECINFO		33
225209513Simp#define	NFSV4OP_SETATTR		34
226209513Simp#define	NFSV4OP_SETCLIENTID	35
227209513Simp#define	NFSV4OP_SETCLIENTIDCFRM	36
228209513Simp#define	NFSV4OP_VERIFY		37
229209513Simp#define	NFSV4OP_WRITE		38
230209513Simp#define	NFSV4OP_RELEASELCKOWN	39
231209513Simp
232209513Simp/*
233209513Simp * Must be one greater than the last Operation#.
234209513Simp */
235209513Simp#define	NFSV4OP_NOPS		40
236209513Simp
237209513Simp/*
238209513Simp * Fake NFSV4OP_xxx used for nfsstat. Start at NFSV4OP_NOPS.
239211730Simp */
240211730Simp#define	NFSV4OP_SYMLINK		(NFSV4OP_NOPS)
241211730Simp#define	NFSV4OP_MKDIR		(NFSV4OP_NOPS + 1)
242211730Simp#define	NFSV4OP_RMDIR		(NFSV4OP_NOPS + 2)
243211730Simp#define	NFSV4OP_READDIRPLUS	(NFSV4OP_NOPS + 3)
244211730Simp#define	NFSV4OP_MKNOD		(NFSV4OP_NOPS + 4)
245211730Simp#define	NFSV4OP_FSSTAT		(NFSV4OP_NOPS + 5)
246211730Simp#define	NFSV4OP_FSINFO		(NFSV4OP_NOPS + 6)
247211730Simp#define	NFSV4OP_PATHCONF	(NFSV4OP_NOPS + 7)
248211730Simp#define	NFSV4OP_V3CREATE	(NFSV4OP_NOPS + 8)
249211730Simp
250211730Simp/*
251211730Simp * This is the count of the fake operations listed above.
252211730Simp */
253211730Simp#define	NFSV4OP_FAKENOPS	9
254211730Simp
255211730Simp/*
256211730Simp * and the Callback OPs
257211730Simp */
258211730Simp#define	NFSV4OP_CBGETATTR	3
259211730Simp#define	NFSV4OP_CBRECALL	4
260211730Simp
261211730Simp/*
262211730Simp * Must be one greater than the last Callback Operation#.
263211730Simp */
264211730Simp#define	NFSV4OP_CBNOPS		5
265211730Simp
266211730Simp/*
267211730Simp * The lower numbers -> 21 are used by NFSv2 and v3. These define higher
268211730Simp * numbers used by NFSv4.
269211730Simp * NFS_V3NPROCS is one greater than the last V3 op and NFS_NPROCS is
270211730Simp * one greater than the last number.
271211730Simp */
272211730Simp#define	NFS_V3NPROCS		22
273211730Simp
274211730Simp#define	NFSPROC_LOOKUPP		22
275211730Simp#define	NFSPROC_SETCLIENTID	23
276211730Simp#define	NFSPROC_SETCLIENTIDCFRM	24
277211730Simp#define	NFSPROC_LOCK		25
278211730Simp#define	NFSPROC_LOCKU		26
279211730Simp#define	NFSPROC_OPEN		27
280211730Simp#define	NFSPROC_CLOSE		28
281211730Simp#define	NFSPROC_OPENCONFIRM	29
282211730Simp#define	NFSPROC_LOCKT		30
283211730Simp#define	NFSPROC_OPENDOWNGRADE	31
284211730Simp#define	NFSPROC_RENEW		32
285211730Simp#define	NFSPROC_PUTROOTFH	33
286211730Simp#define	NFSPROC_RELEASELCKOWN	34
287211730Simp#define	NFSPROC_DELEGRETURN	35
288211730Simp#define	NFSPROC_RETDELEGREMOVE	36
289211730Simp#define	NFSPROC_RETDELEGRENAME1	37
290211730Simp#define	NFSPROC_RETDELEGRENAME2	38
291211730Simp#define	NFSPROC_GETACL		39
292211730Simp#define	NFSPROC_SETACL		40
293211730Simp
294211730Simp/*
295211730Simp * Must be defined as one higher than the last Proc# above.
296211730Simp */
297211730Simp#define	NFSV4_NPROCS		41
298211730Simp
299211730Simp/*
300211730Simp * Stats structure
301211730Simp */
302211730Simpstruct ext_nfsstats {
303211730Simp	int	attrcache_hits;
304211730Simp	int	attrcache_misses;
305211730Simp	int	lookupcache_hits;
306211730Simp	int	lookupcache_misses;
307211730Simp	int	direofcache_hits;
308211730Simp	int	direofcache_misses;
309211730Simp	int	accesscache_hits;
310211730Simp	int	accesscache_misses;
311211730Simp	int	biocache_reads;
312211730Simp	int	read_bios;
313211730Simp	int	read_physios;
314211730Simp	int	biocache_writes;
315211730Simp	int	write_bios;
316211730Simp	int	write_physios;
317211730Simp	int	biocache_readlinks;
318211730Simp	int	readlink_bios;
319211730Simp	int	biocache_readdirs;
320211730Simp	int	readdir_bios;
321211730Simp	int	rpccnt[NFSV4_NPROCS];
322211730Simp	int	rpcretries;
323211730Simp	int	srvrpccnt[NFSV4OP_NOPS + NFSV4OP_FAKENOPS];
324211730Simp	int	srvrpc_errs;
325211730Simp	int	srv_errs;
326211730Simp	int	rpcrequests;
327211730Simp	int	rpctimeouts;
328211730Simp	int	rpcunexpected;
329211730Simp	int	rpcinvalid;
330211730Simp	int	srvcache_inproghits;
331211730Simp	int	srvcache_idemdonehits;
332211730Simp	int	srvcache_nonidemdonehits;
333211730Simp	int	srvcache_misses;
334211730Simp	int	srvcache_tcppeak;
335211730Simp	int	srvcache_size;
336211730Simp	int	srvclients;
337211730Simp	int	srvopenowners;
338211730Simp	int	srvopens;
339211730Simp	int	srvlockowners;
340211730Simp	int	srvlocks;
341211730Simp	int	srvdelegates;
342211730Simp	int	cbrpccnt[NFSV4OP_CBNOPS];
343211730Simp	int	clopenowners;
344211730Simp	int	clopens;
345211730Simp	int	cllockowners;
346211730Simp	int	cllocks;
347211730Simp	int	cldelegates;
348211730Simp	int	cllocalopenowners;
349211730Simp	int	cllocalopens;
350211730Simp	int	cllocallockowners;
351211730Simp	int	cllocallocks;
352211730Simp};
353211730Simp
354211730Simp#ifdef _KERNEL
355211730Simp/*
356211730Simp * Define the ext_nfsstats as nfsstats for the kernel code.
357211730Simp */
358211730Simp#define nfsstats	ext_nfsstats
359211730Simp
360211730Simp/*
361211730Simp * Define NFS_NPROCS as NFSV4_NPROCS for the experimental kernel code.
362211730Simp */
363211730Simp#define	NFS_NPROCS		NFSV4_NPROCS
364211730Simp
365211730Simp#include <fs/nfs/nfskpiport.h>
366211730Simp#include <fs/nfs/nfsdport.h>
367211730Simp#include <fs/nfs/rpcv2.h>
368211730Simp#include <fs/nfs/nfsproto.h>
369211730Simp#include <fs/nfs/nfs.h>
370211730Simp#include <fs/nfs/nfs_var.h>
371211730Simp#include <fs/nfs/nfsm_subs.h>
372211730Simp#include <fs/nfs/nfsrvcache.h>
373211730Simp#include <fs/nfs/nfsrvstate.h>
374211730Simp#include <fs/nfs/xdr_subs.h>
375211730Simp#include <fs/nfs/nfscl.h>
376211730Simp#include <fs/nfs/nfsclstate.h>
377211730Simp#include <fs/nfsclient/nfsargs.h>
378211730Simp#include <fs/nfsclient/nfsmount.h>
379211730Simp
380211730Simp/*
381211730Simp * Just to keep nfs_var.h happy.
382211730Simp */
383211730Simpstruct nfs_vattr {
384211730Simp	int	junk;
385211730Simp};
386211730Simp
387211730Simpstruct nfsvattr {
388211730Simp	struct vattr	na_vattr;
389211730Simp	nfsattrbit_t	na_suppattr;
390211730Simp	u_int32_t	na_mntonfileno;
391211730Simp	u_int64_t	na_filesid[2];
392211730Simp};
393211730Simp
394211730Simp#define	na_type		na_vattr.va_type
395211730Simp#define	na_mode		na_vattr.va_mode
396211730Simp#define	na_nlink	na_vattr.va_nlink
397211730Simp#define	na_uid		na_vattr.va_uid
398211730Simp#define	na_gid		na_vattr.va_gid
399211730Simp#define	na_fsid		na_vattr.va_fsid
400211730Simp#define	na_fileid	na_vattr.va_fileid
401211730Simp#define	na_size		na_vattr.va_size
402211730Simp#define	na_blocksize	na_vattr.va_blocksize
403211730Simp#define	na_atime	na_vattr.va_atime
404211730Simp#define	na_mtime	na_vattr.va_mtime
405211730Simp#define	na_ctime	na_vattr.va_ctime
406211730Simp#define	na_gen		na_vattr.va_gen
407211730Simp#define	na_flags	na_vattr.va_flags
408211730Simp#define	na_rdev		na_vattr.va_rdev
409211730Simp#define	na_bytes	na_vattr.va_bytes
410211730Simp#define	na_filerev	na_vattr.va_filerev
411211730Simp#define	na_vaflags	na_vattr.va_vaflags
412211730Simp
413211730Simp#include <fs/nfsclient/nfsnode.h>
414211730Simp
415211730Simp/*
416211730Simp * This is the header structure used for the lists, etc. (It has the
417211730Simp * above record in it.
418211730Simp */
419211730Simpstruct nfsrv_stablefirst {
420211730Simp	LIST_HEAD(, nfsrv_stable) nsf_head;	/* Head of nfsrv_stable list */
421209513Simp	time_t		nsf_eograce;	/* Time grace period ends */
422209513Simp	time_t		*nsf_bootvals;	/* Previous boottime values */
423209513Simp	struct file	*nsf_fp;	/* File table pointer */
424209513Simp	u_char		nsf_flags;	/* NFSNSF_ flags */
425209513Simp	struct nfsf_rec	nsf_rec;	/* and above first record */
426211730Simp};
427209513Simp#define	nsf_lease	nsf_rec.lease
428209513Simp#define	nsf_numboots	nsf_rec.numboots
429209513Simp
430209513Simp/* NFSNSF_xxx flags */
431209513Simp#define	NFSNSF_UPDATEDONE	0x01
432209513Simp#define	NFSNSF_GRACEOVER	0x02
433209513Simp#define	NFSNSF_NEEDLOCK		0x04
434209513Simp#define	NFSNSF_EXPIREDCLIENT	0x08
435209513Simp#define	NFSNSF_NOOPENS		0x10
436209513Simp#define	NFSNSF_OK		0x20
437209513Simp
438209513Simp/*
439209513Simp * Maximum number of boot times allowed in record. Although there is
440209513Simp * really no need for a fixed upper bound, this serves as a sanity check
441209513Simp * for a corrupted file.
442209513Simp */
443220059Sjpaetzel#define	NFSNSF_MAXNUMBOOTS	10000
444220059Sjpaetzel
445209513Simp/*
446209513Simp * This structure defines the other records in the file. The
447209513Simp * nst_client array is actually the size of the client string name.
448209513Simp */
449209513Simpstruct nfst_rec {
450220059Sjpaetzel	u_int16_t	len;
451220059Sjpaetzel	u_char		flag;
452209513Simp	u_char		client[1];
453209513Simp};
454209513Simp/* and the values for flag */
455209513Simp#define	NFSNST_NEWSTATE	0x1
456209513Simp#define	NFSNST_REVOKE		0x2
457209513Simp#define	NFSNST_GOTSTATE		0x4
458209513Simp
459209513Simp/*
460209513Simp * This structure is linked onto nfsrv_stablefirst for the duration of
461209513Simp * reclaim.
462209513Simp */
463209513Simpstruct nfsrv_stable {
464209513Simp	LIST_ENTRY(nfsrv_stable) nst_list;
465209513Simp	struct nfsclient	*nst_clp;
466209513Simp	struct nfst_rec		nst_rec;
467209513Simp};
468220059Sjpaetzel#define	nst_timestamp	nst_rec.timestamp
469220059Sjpaetzel#define	nst_len		nst_rec.len
470209513Simp#define	nst_flag	nst_rec.flag
471209513Simp#define	nst_client	nst_rec.client
472209513Simp
473209513Simp/*
474209513Simp * At some point the server will run out of kernel storage for
475209513Simp * state structures. For FreeBSD5.2, this results in a panic
476209513Simp * kmem_map is full. It happens at well over 1000000 opens plus
477209513Simp * locks on a PIII-800 with 256Mbytes, so that is where I've set
478209513Simp * the limit. If your server panics due to too many opens/locks,
479209513Simp * decrease the size of NFSRV_V4STATELIMIT. If you find the server
480209513Simp * returning NFS4ERR_RESOURCE a lot and have lots of memory, try
481209513Simp * increasing it.
482218801Sjpaetzel */
483218801Sjpaetzel#define	NFSRV_V4STATELIMIT	500000	/* Max # of Opens + Locks */
484209513Simp
485218801Sjpaetzel/*
486218801Sjpaetzel * The type required differs with BSDen (just the second arg).
487218801Sjpaetzel */
488209513Simpvoid nfsrvd_rcv(struct socket *, void *, int);
489209513Simp
490209513Simp/*
491220059Sjpaetzel * Macros for handling socket addresses. (Hopefully this makes the code
492220059Sjpaetzel * more portable, since I've noticed some 'BSD don't have sockaddrs in
493209513Simp * mbufs any more.)
494209513Simp */
495209513Simp#define	NFSSOCKADDR(a, t)	((t)(a))
496209513Simp#define	NFSSOCKADDRALLOC(a) 					\
497209513Simp    do {							\
498220059Sjpaetzel	MALLOC((a), struct sockaddr *, sizeof (struct sockaddr), \
499220059Sjpaetzel	    M_SONAME, M_WAITOK); 				\
500209513Simp	NFSBZERO((a), sizeof (struct sockaddr)); 		\
501209513Simp    } while (0)
502209513Simp#define	NFSSOCKADDRSIZE(a, s)		((a)->sa_len = (s))
503209513Simp#define	NFSSOCKADDRFREE(a) 					\
504209513Simp	do { 							\
505220059Sjpaetzel		if (a) 						\
506220059Sjpaetzel			FREE((caddr_t)(a), M_SONAME); 		\
507209513Simp	} while (0)
508209513Simp
509209513Simp/*
510209513Simp * These should be defined as a process or thread structure, as required
511209513Simp * for signal handling, etc.
512209513Simp */
513225657Sjpaetzel#define	NFSNEWCRED(c)		(crdup(c))
514220059Sjpaetzel#define	NFSPROCCRED(p)		((p)->td_ucred)
515209513Simp#define	NFSFREECRED(c)		(crfree(c))
516209513Simp#define	NFSUIOPROC(u, p)	((u)->uio_td = NULL)
517209513Simp#define	NFSPROCP(p)		((p)->td_proc)
518209513Simp
519209513Simp/*
520220059Sjpaetzel * Define these so that cn_hash and its length is ignored.
521220059Sjpaetzel */
522209513Simp#define	NFSCNHASHZERO(c)
523209513Simp#define	NFSCNHASH(c, v)
524209513Simp#define	NCHNAMLEN	9999999
525209513Simp
526209513Simp/*
527209513Simp * Define these to use the time of day clock.
528209513Simp */
529209513Simp#define	NFSGETTIME(t)		(getmicrotime(t))
530209513Simp#define	NFSGETNANOTIME(t)	(getnanotime(t))
531209513Simp
532209513Simp/*
533209513Simp * These macros are defined to initialize and set the timer routine.
534209513Simp */
535209513Simp#define	NFS_TIMERINIT \
536209513Simp	newnfs_timer(NULL)
537209513Simp
538209513Simp/*
539209513Simp * Handle SMP stuff:
540209513Simp */
541209513Simp#define	NFSSTATESPINLOCK	extern struct mtx nfs_state_mutex
542#define	NFSLOCKSTATE()		mtx_lock(&nfs_state_mutex)
543#define	NFSUNLOCKSTATE()	mtx_unlock(&nfs_state_mutex)
544#define	NFSREQSPINLOCK		extern struct mtx nfs_req_mutex
545#define	NFSLOCKREQ()		mtx_lock(&nfs_req_mutex)
546#define	NFSUNLOCKREQ()		mtx_unlock(&nfs_req_mutex)
547#define	NFSCACHEMUTEX		extern struct mtx nfs_cache_mutex
548#define	NFSCACHEMUTEXPTR	(&nfs_cache_mutex)
549#define	NFSLOCKCACHE()		mtx_lock(&nfs_cache_mutex)
550#define	NFSUNLOCKCACHE()	mtx_unlock(&nfs_cache_mutex)
551#define	NFSCACHELOCKREQUIRED()	mtx_assert(&nfs_cache_mutex, MA_OWNED)
552#define	NFSSOCKMUTEX		extern struct mtx nfs_slock_mutex
553#define	NFSSOCKMUTEXPTR		(&nfs_slock_mutex)
554#define	NFSLOCKSOCK()		mtx_lock(&nfs_slock_mutex)
555#define	NFSUNLOCKSOCK()		mtx_unlock(&nfs_slock_mutex)
556#define	NFSNAMEIDMUTEX		extern struct mtx nfs_nameid_mutex
557#define	NFSLOCKNAMEID()		mtx_lock(&nfs_nameid_mutex)
558#define	NFSUNLOCKNAMEID()	mtx_unlock(&nfs_nameid_mutex)
559#define	NFSNAMEIDREQUIRED()	mtx_assert(&nfs_nameid_mutex, MA_OWNED)
560#define	NFSCLSTATEMUTEX		extern struct mtx nfs_clstate_mutex
561#define	NFSCLSTATEMUTEXPTR	(&nfs_clstate_mutex)
562#define	NFSLOCKCLSTATE()	mtx_lock(&nfs_clstate_mutex)
563#define	NFSUNLOCKCLSTATE()	mtx_unlock(&nfs_clstate_mutex)
564#define	NFSDLOCKMUTEX		extern struct mtx newnfsd_mtx
565#define	NFSDLOCKMUTEXPTR	(&newnfsd_mtx)
566#define	NFSD_LOCK()		mtx_lock(&newnfsd_mtx)
567#define	NFSD_UNLOCK()		mtx_unlock(&newnfsd_mtx)
568#define	NFSD_LOCK_ASSERT()	mtx_assert(&newnfsd_mtx, MA_OWNED)
569#define	NFSD_UNLOCK_ASSERT()	mtx_assert(&newnfsd_mtx, MA_NOTOWNED)
570#define	NFSV4ROOTLOCKMUTEX	extern struct mtx nfs_v4root_mutex
571#define	NFSV4ROOTLOCKMUTEXPTR	(&nfs_v4root_mutex)
572#define	NFSLOCKV4ROOTMUTEX()	mtx_lock(&nfs_v4root_mutex)
573#define	NFSUNLOCKV4ROOTMUTEX()	mtx_unlock(&nfs_v4root_mutex)
574#define	NFSLOCKNODE(n)		mtx_lock(&((n)->n_mtx))
575#define	NFSUNLOCKNODE(n)	mtx_unlock(&((n)->n_mtx))
576#define	NFSLOCKMNT(m)		mtx_lock(&((m)->nm_mtx))
577#define	NFSUNLOCKMNT(m)		mtx_unlock(&((m)->nm_mtx))
578#define	NFSLOCKREQUEST(r)	mtx_lock(&((r)->r_mtx))
579#define	NFSUNLOCKREQUEST(r)	mtx_unlock(&((r)->r_mtx))
580#define	NFSPROCLISTLOCK()	sx_slock(&allproc_lock)
581#define	NFSPROCLISTUNLOCK()	sx_sunlock(&allproc_lock)
582#define	NFSLOCKSOCKREQ(r)	mtx_lock(&((r)->nr_mtx))
583#define	NFSUNLOCKSOCKREQ(r)	mtx_unlock(&((r)->nr_mtx))
584
585/*
586 * Use these macros to initialize/free a mutex.
587 */
588#define	NFSINITSOCKMUTEX(m)	mtx_init((m), "nfssock", NULL, MTX_DEF)
589#define	NFSFREEMUTEX(m)		mtx_destroy((m))
590
591int nfsmsleep(void *, void *, int, const char *, struct timespec *);
592
593/*
594 * And weird vm stuff in the nfs server.
595 */
596#define	PDIRUNLOCK	0x0
597#define	MAX_COMMIT_COUNT	(1024 * 1024)
598
599/*
600 * These macros are called at the start and end of operations that
601 * might modify the underlying file system.
602 */
603#define	NFS_STARTWRITE(v, m)	vn_start_write((v), (m), V_WAIT)
604#define	NFS_ENDWRITE(m)		vn_finished_write(m)
605
606/*
607 * Define these to handle the type of va_rdev.
608 */
609#define	NFSMAKEDEV(m, n)	makedev((m), (n))
610#define	NFSMAJOR(d)		major(d)
611#define	NFSMINOR(d)		minor(d)
612
613/*
614 * Define this to be the macro that returns the minimum size required
615 * for a directory entry.
616 */
617#define	DIRENT_SIZE(dp)		GENERIC_DIRSIZ(dp)
618
619/*
620 * The vnode tag for nfsv4root.
621 */
622#define	VT_NFSV4ROOT		"nfsv4root"
623
624/*
625 * Define whatever it takes to do a vn_rdwr().
626 */
627#define	NFSD_RDWR(r, v, b, l, o, s, i, c, a, p) \
628	vn_rdwr((r), (v), (b), (l), (o), (s), (i), (c), NULL, (int *)(a), (p))
629
630/*
631 * Macros for handling memory for different BSDen.
632 * NFSBCOPY(src, dst, len) - copies len bytes, non-overlapping
633 * NFSOVBCOPY(src, dst, len) - ditto, but data areas might overlap
634 * NFSBCMP(cp1, cp2, len) - compare len bytes, return 0 if same
635 * NFSBZERO(cp, len) - set len bytes to 0x0
636 */
637#define	NFSBCOPY(s, d, l)	bcopy((s), (d), (l))
638#define	NFSOVBCOPY(s, d, l)	ovbcopy((s), (d), (l))
639#define	NFSBCMP(s, d, l)	bcmp((s), (d), (l))
640#define	NFSBZERO(s, l)		bzero((s), (l))
641
642/*
643 * Some queue.h files don't have these dfined in them.
644 */
645#define	LIST_END(head)		NULL
646#define	SLIST_END(head)		NULL
647#define	TAILQ_END(head)		NULL
648
649/*
650 * This must be defined to be a global variable the increments once
651 * per second, but never stops or goes backwards, even when a "date"
652 * command changes the tod clock. It is used for delta times for
653 * leases, etc.
654 */
655#define	NFSD_MONOSEC		time_uptime
656
657/*
658 * Declare the malloc types.
659 */
660MALLOC_DECLARE(M_NEWNFSRVCACHE);
661MALLOC_DECLARE(M_NEWNFSDCLIENT);
662MALLOC_DECLARE(M_NEWNFSDSTATE);
663MALLOC_DECLARE(M_NEWNFSDLOCK);
664MALLOC_DECLARE(M_NEWNFSDLOCKFILE);
665MALLOC_DECLARE(M_NEWNFSSTRING);
666MALLOC_DECLARE(M_NEWNFSUSERGROUP);
667MALLOC_DECLARE(M_NEWNFSDREQ);
668MALLOC_DECLARE(M_NEWNFSFH);
669MALLOC_DECLARE(M_NEWNFSCLOWNER);
670MALLOC_DECLARE(M_NEWNFSCLOPEN);
671MALLOC_DECLARE(M_NEWNFSCLDELEG);
672MALLOC_DECLARE(M_NEWNFSCLCLIENT);
673MALLOC_DECLARE(M_NEWNFSCLLOCKOWNER);
674MALLOC_DECLARE(M_NEWNFSCLLOCK);
675MALLOC_DECLARE(M_NEWNFSDIROFF);
676MALLOC_DECLARE(M_NEWNFSV4NODE);
677MALLOC_DECLARE(M_NEWNFSDIRECTIO);
678MALLOC_DECLARE(M_NEWNFSMNT);
679#define	M_NFSRVCACHE	M_NEWNFSRVCACHE
680#define	M_NFSDCLIENT	M_NEWNFSDCLIENT
681#define	M_NFSDSTATE	M_NEWNFSDSTATE
682#define	M_NFSDLOCK	M_NEWNFSDLOCK
683#define	M_NFSDLOCKFILE	M_NEWNFSDLOCKFILE
684#define	M_NFSSTRING	M_NEWNFSSTRING
685#define	M_NFSUSERGROUP	M_NEWNFSUSERGROUP
686#define	M_NFSDREQ	M_NEWNFSDREQ
687#define	M_NFSFH		M_NEWNFSFH
688#define	M_NFSCLOWNER	M_NEWNFSCLOWNER
689#define	M_NFSCLOPEN	M_NEWNFSCLOPEN
690#define	M_NFSCLDELEG	M_NEWNFSCLDELEG
691#define	M_NFSCLCLIENT	M_NEWNFSCLCLIENT
692#define	M_NFSCLLOCKOWNER M_NEWNFSCLLOCKOWNER
693#define	M_NFSCLLOCK	M_NEWNFSCLLOCK
694#define	M_NFSDIROFF	M_NEWNFSDIROFF
695#define	M_NFSV4NODE	M_NEWNFSV4NODE
696#define	M_NFSDIRECTIO	M_NEWNFSDIRECTIO
697
698#define	NFSINT_SIGMASK(set) 						\
699	(SIGISMEMBER(set, SIGINT) || SIGISMEMBER(set, SIGTERM) ||	\
700	 SIGISMEMBER(set, SIGHUP) || SIGISMEMBER(set, SIGKILL) ||	\
701	 SIGISMEMBER(set, SIGQUIT))
702
703/*
704 * Convert a quota block count to byte count.
705 */
706#define	NFSQUOTABLKTOBYTE(q, b)	(q) *= (b)
707
708/*
709 * Define this as the largest file size supported. (It should probably
710 * be available via a VFS_xxx Op, but it isn't.
711 */
712#define	NFSRV_MAXFILESIZE	((u_int64_t)0x800000000000)
713
714/*
715 * Set this macro to index() or strchr(), whichever is supported.
716 */
717#define	STRCHR(s, c)	index((s), (c))
718
719/*
720 * Set the n_time in the client write rpc, as required.
721 */
722#define	NFSWRITERPC_SETTIME(w, n, v4)					\
723	do {								\
724		if (w) {						\
725			(n)->n_mtime = (n)->n_vattr.na_vattr.va_mtime; \
726			if (v4)						\
727			    (n)->n_change = (n)->n_vattr.na_vattr.va_filerev; \
728		}							\
729	} while (0)
730
731/*
732 * Fake value, just to make the client work.
733 */
734#define	NFS_LATTR_NOSHRINK	1
735
736/*
737 * Prototypes for functions where the arguments vary for different ports.
738 */
739int nfscl_loadattrcache(struct vnode **, struct nfsvattr *, void *, void *,
740    int, int);
741void newnfs_realign(struct mbuf **);
742
743/*
744 * If the port runs on an SMP box that can enforce Atomic ops with low
745 * overheads, define these as atomic increments/decrements. If not,
746 * don't worry about it, since these are used for stats that can be
747 * "out by one" without disastrous consequences.
748 */
749#define	NFSINCRGLOBAL(a)	((a)++)
750
751/*
752 * Assorted funky stuff to make things work under Darwin8.
753 */
754/*
755 * These macros checks for a field in vattr being set.
756 */
757#define	NFSATTRISSET(t, v, a)	((v)->a != (t)VNOVAL)
758#define	NFSATTRISSETTIME(v, a)	((v)->a.tv_sec != VNOVAL)
759
760/*
761 * Manipulate mount flags.
762 */
763#define	NFSSTA_HASWRITEVERF	0x00040000  /* Has write verifier */
764#define	NFSSTA_GOTFSINFO	0x00100000  /* Got the fsinfo */
765#define	NFSSTA_TIMEO		0x10000000  /* Experiencing a timeout */
766#define	NFSSTA_LOCKTIMEO	0x20000000  /* Experiencing a lockd timeout */
767#define	NFSSTA_HASSETFSID	0x40000000  /* Has set the fsid */
768
769#define	NFSHASNFSV3(n)		((n)->nm_flag & NFSMNT_NFSV3)
770#define	NFSHASNFSV4(n)		((n)->nm_flag & NFSMNT_NFSV4)
771#define	NFSHASNFSV3OR4(n)	((n)->nm_flag & (NFSMNT_NFSV3 | NFSMNT_NFSV4))
772#define	NFSHASGOTFSINFO(n)	((n)->nm_state & NFSSTA_GOTFSINFO)
773#define	NFSHASHASSETFSID(n)	((n)->nm_state & NFSSTA_HASSETFSID)
774#define	NFSHASSTRICT3530(n)	((n)->nm_flag & NFSMNT_STRICT3530)
775#define	NFSHASWRITEVERF(n)	((n)->nm_state & NFSSTA_HASWRITEVERF)
776#define	NFSHASINT(n)		((n)->nm_flag & NFSMNT_INT)
777#define	NFSHASSOFT(n)		((n)->nm_flag & NFSMNT_SOFT)
778#define	NFSHASINTORSOFT(n)	((n)->nm_flag & (NFSMNT_INT | NFSMNT_SOFT))
779#define	NFSHASDUMBTIMR(n)	((n)->nm_flag & NFSMNT_DUMBTIMR)
780#define	NFSHASNOCONN(n)		((n)->nm_flag & NFSMNT_MNTD)
781#define	NFSHASKERB(n)		((n)->nm_flag & NFSMNT_KERB)
782#define	NFSHASALLGSSNAME(n)	((n)->nm_flag & NFSMNT_ALLGSSNAME)
783#define	NFSHASINTEGRITY(n)	((n)->nm_flag & NFSMNT_INTEGRITY)
784#define	NFSHASPRIVACY(n)	((n)->nm_flag & NFSMNT_PRIVACY)
785#define	NFSSETWRITEVERF(n)	((n)->nm_state |= NFSSTA_HASWRITEVERF)
786#define	NFSSETHASSETFSID(n)	((n)->nm_state |= NFSSTA_HASSETFSID)
787#ifdef NFS4_ACL_EXTATTR_NAME
788#define	NFSHASNFS4ACL(m)	((m)->mnt_flag & MNT_NFS4ACLS)
789#else
790#define	NFSHASNFS4ACL(m)	0
791#endif
792
793/*
794 * Gets the stats field out of the mount structure.
795 */
796#define	vfs_statfs(m)	(&((m)->mnt_stat))
797
798/*
799 * Set boottime.
800 */
801#define	NFSSETBOOTTIME(b)	((b) = boottime)
802
803/*
804 * The size of directory blocks in the buffer cache.
805 * MUST BE in the range of PAGE_SIZE <= NFS_DIRBLKSIZ <= MAXBSIZE!!
806 */
807#define	NFS_DIRBLKSIZ	(16 * DIRBLKSIZ) /* Must be a multiple of DIRBLKSIZ */
808
809/*
810 * Define these macros to access mnt_flag fields.
811 */
812#define	NFSMNT_RDONLY(m)	((m)->mnt_flag & MNT_RDONLY)
813#endif	/* _KERNEL */
814
815/*
816 * Define a structure similar to ufs_args for use in exporting the V4 root.
817 */
818struct nfsex_args {
819	char	*fspec;
820	struct export_args	export;
821};
822
823/*
824 * These export flags should be defined, but there are no bits left.
825 * Maybe a separate mnt_exflag field could be added or the mnt_flag
826 * field increased to 64 bits?
827 */
828#ifndef	MNT_EXSTRICTACCESS
829#define	MNT_EXSTRICTACCESS	0x0
830#endif
831#ifndef MNT_EXV4ONLY
832#define	MNT_EXV4ONLY		0x0
833#endif
834
835#ifdef _KERNEL
836/*
837 * Define this to invalidate the attribute cache for the nfs node.
838 */
839#define	NFSINVALATTRCACHE(n)	((n)->n_attrstamp = 0)
840
841/* Used for FreeBSD only */
842void nfsd_mntinit(void);
843
844/*
845 * Define these for vnode lock/unlock ops.
846 */
847#define	NFSVOPLOCK(v, f, p)	vn_lock((v), (f))
848#define	NFSVOPUNLOCK(v, f, p)	VOP_UNLOCK((v), (f))
849#define	NFSVOPISLOCKED(v, p)	VOP_ISLOCKED((v))
850
851/*
852 * Define ncl_hash().
853 */
854#define	ncl_hash(f, l)	(fnv_32_buf((f), (l), FNV1_32_INIT))
855
856int newnfs_iosize(struct nfsmount *);
857
858#ifdef NFS_DEBUG
859
860extern int nfs_debug;
861#define	NFS_DEBUG_ASYNCIO	1 /* asynchronous i/o */
862#define	NFS_DEBUG_WG		2 /* server write gathering */
863#define	NFS_DEBUG_RC		4 /* server request caching */
864
865#define	NFS_DPF(cat, args)					\
866	do {							\
867		if (nfs_debug & NFS_DEBUG_##cat) printf args;	\
868	} while (0)
869
870#else
871
872#define	NFS_DPF(cat, args)
873
874#endif
875
876int newnfs_vncmpf(struct vnode *, void *);
877
878#ifndef NFS_MINDIRATTRTIMO
879#define	NFS_MINDIRATTRTIMO 3		/* VDIR attrib cache timeout in sec */
880#endif
881#ifndef NFS_MAXDIRATTRTIMO
882#define	NFS_MAXDIRATTRTIMO 60
883#endif
884
885/*
886 * Nfs outstanding request list element
887 */
888struct nfsreq {
889	TAILQ_ENTRY(nfsreq) r_chain;
890	u_int32_t	r_flags;	/* flags on request, see below */
891	struct nfsmount *r_nmp;		/* Client mnt ptr */
892	struct mtx	r_mtx;		/* Mutex lock for this structure */
893};
894
895#ifndef NFS_MAXBSIZE
896#define	NFS_MAXBSIZE	MAXBSIZE
897#endif
898
899/*
900 * This macro checks to see if issuing of delegations is allowed for this
901 * vnode.
902 */
903#ifdef VV_DISABLEDELEG
904#define	NFSVNO_DELEGOK(v)						\
905	((v) == NULL || ((v)->v_vflag & VV_DISABLEDELEG) == 0)
906#else
907#define	NFSVNO_DELEGOK(v)	(1)
908#endif
909
910#endif	/* _KERNEL */
911
912#endif	/* _NFSPORT_NFS_H */
913