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