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