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