1 /*	$OpenBSD: nfs.h,v 1.24 2005/06/08 04:17:14 marius Exp $	*/
2 /*	$NetBSD: nfs.h,v 1.10.4.1 1996/05/27 11:23:56 fvdl Exp $	*/
3 
4 /*
5  * Copyright (c) 1989, 1993, 1995
6  *	The Regents of the University of California.  All rights reserved.
7  *
8  * This code is derived from software contributed to Berkeley by
9  * Rick Macklem at The University of Guelph.
10  *
11  * Redistribution and use in source and binary forms, with or without
12  * modification, are permitted provided that the following conditions
13  * are met:
14  * 1. Redistributions of source code must retain the above copyright
15  *    notice, this list of conditions and the following disclaimer.
16  * 2. Redistributions in binary form must reproduce the above copyright
17  *    notice, this list of conditions and the following disclaimer in the
18  *    documentation and/or other materials provided with the distribution.
19  * 3. Neither the name of the University nor the names of its contributors
20  *    may be used to endorse or promote products derived from this software
21  *    without specific prior written permission.
22  *
23  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
24  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
25  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
26  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
27  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
28  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
29  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
30  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
31  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
32  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
33  * SUCH DAMAGE.
34  *
35  *	@(#)nfs.h	8.4 (Berkeley) 5/1/95
36  */
37 
38 #ifndef _NFS_NFS_H_
39 #define _NFS_NFS_H_
40 
41 /*
42  * Tunable constants for nfs
43  */
44 
45 #define	NFS_MAXIOVEC	34
46 #define NFS_TICKINTVL	5		/* Desired time for a tick (msec) */
47 #define NFS_HZ		(hz / nfs_ticks) /* Ticks/sec */
48 #define	NFS_TIMEO	(1 * NFS_HZ)	/* Default timeout = 1 second */
49 #define	NFS_MINTIMEO	(1 * NFS_HZ)	/* Min timeout to use */
50 #define	NFS_MAXTIMEO	(60 * NFS_HZ)	/* Max timeout to backoff to */
51 #define	NFS_MINIDEMTIMEO (5 * NFS_HZ)	/* Min timeout for non-idempotent ops*/
52 #define	NFS_MAXREXMIT	100		/* Stop counting after this many */
53 #define	NFS_MAXWINDOW	1024		/* Max number of outstanding requests */
54 #define	NFS_RETRANS	10		/* Num of retrans for soft mounts */
55 #define	NFS_MAXGRPS	16		/* Max. size of groups list */
56 #ifndef NFS_MINATTRTIMO
57 #define	NFS_MINATTRTIMO 5		/* Attribute cache timeout in sec */
58 #endif
59 #ifndef NFS_MAXATTRTIMO
60 #define	NFS_MAXATTRTIMO 60
61 #endif
62 #define	NFS_WSIZE	8192		/* Def. write data size <= 8192 */
63 #define	NFS_RSIZE	8192		/* Def. read data size <= 8192 */
64 #define NFS_READDIRSIZE	8192		/* Def. readdir size */
65 #define	NFS_DEFRAHEAD	1		/* Def. read ahead # blocks */
66 #define	NFS_MAXRAHEAD	4		/* Max. read ahead # blocks */
67 #define	NFS_MAXUIDHASH	64		/* Max. # of hashed uid entries/mp */
68 #define	NFS_MAXASYNCDAEMON 	20	/* Max. number async_daemons runable */
69 #define NFS_MAXGATHERDELAY	100	/* Max. write gather delay (msec) */
70 #ifndef NFS_GATHERDELAY
71 #define NFS_GATHERDELAY		10	/* Default write gather delay (msec) */
72 #endif
73 /*
74  * Ideally, NFS_DIRBLKSIZ should be bigger, but I've seen servers with
75  * broken NFS/ethernet drivers that won't work with anything bigger (Linux..)
76  */
77 #define	NFS_DIRBLKSIZ	1024		/* Must be a multiple of DIRBLKSIZ */
78 #define NFS_READDIRBLKSIZ	512	/* Size of read dir blocks. XXX */
79 
80 /*
81  * Oddballs
82  */
83 #define	NMOD(a)		((a) % nfs_asyncdaemons)
84 #define NFS_CMPFH(n, f, s) \
85 	((n)->n_fhsize == (s) && !bcmp((caddr_t)(n)->n_fhp, (caddr_t)(f), (s)))
86 #define NFS_ISV3(v)	(VFSTONFS((v)->v_mount)->nm_flag & NFSMNT_NFSV3)
87 #define NFS_SRVMAXDATA(n) \
88 		(((n)->nd_flag & ND_NFSV3) ? (((n)->nd_nam2) ? \
89 		 NFS_MAXDGRAMDATA : NFS_MAXDATA) : NFS_V2MAXDATA)
90 
91 /*
92  * sys/malloc.h needs M_NFSDIROFF, M_NFSRVDESC and M_NFSBIGFH added.
93  * The VA_EXCLUSIVE flag should be added for va_vaflags and set for an
94  * exclusive create.
95  */
96 #ifndef M_NFSRVDESC
97 #define M_NFSRVDESC	M_TEMP
98 #endif
99 #ifndef M_NFSDIROFF
100 #define M_NFSDIROFF	M_TEMP
101 #endif
102 #ifndef M_NFSBIGFH
103 #define M_NFSBIGFH	M_TEMP
104 #endif
105 #ifndef VA_EXCLUSIVE
106 #define VA_EXCLUSIVE	0
107 #endif
108 
109 /*
110  * The B_INVAFTERWRITE flag should be set to whatever is required by the
111  * buffer cache code to say "Invalidate the block after it is written back".
112  */
113 #define	B_INVAFTERWRITE	B_INVAL
114 
115 /*
116  * The IO_METASYNC flag should be implemented for local file systems.
117  * (Until then, it is nothin at all.)
118  */
119 #ifndef IO_METASYNC
120 #define IO_METASYNC	0
121 #endif
122 
123 /*
124  * Set the attribute timeout based on how recently the file has been modified.
125  */
126 #if 0 /* replaced by nfs_attrtimeo() in nfs_subs.c */
127 #define	NFS_ATTRTIMEO(np) \
128 	((((np)->n_flag & NMODIFIED) || \
129 	 (time.tv_sec - (np)->n_mtime) / 10 < NFS_MINATTRTIMO) ? NFS_MINATTRTIMO : \
130 	 ((time.tv_sec - (np)->n_mtime) / 10 > NFS_MAXATTRTIMO ? NFS_MAXATTRTIMO : \
131 	  (time.tv_sec - (np)->n_mtime) / 10))
132 #endif
133 
134 /*
135  * Structures for the nfssvc(2) syscall. Not that anyone but nfsd and mount_nfs
136  * should ever try and use it.
137  */
138 struct nfsd_args {
139 	int	sock;		/* Socket to serve */
140 	caddr_t	name;		/* Client addr for connection based sockets */
141 	int	namelen;	/* Length of name */
142 };
143 
144 struct nfsd_srvargs {
145 	struct nfsd	*nsd_nfsd;	/* Pointer to in kernel nfsd struct */
146 	uid_t		nsd_uid;	/* Effective uid mapped to cred */
147 	u_int32_t	nsd_haddr;	/* IP address of client */
148 	struct ucred	nsd_cr;		/* Cred. uid maps to */
149 	int		nsd_authlen;	/* Length of auth string (ret) */
150 	u_char		*nsd_authstr;	/* Auth string (ret) */
151 	int		nsd_verflen;	/* and the verifier */
152 	u_char		*nsd_verfstr;
153 	struct timeval	nsd_timestamp;	/* timestamp from verifier */
154 	u_int32_t	nsd_ttl;	/* credential ttl (sec) */
155 	NFSKERBKEY_T	nsd_key;	/* Session key */
156 };
157 
158 struct nfsd_cargs {
159 	char		*ncd_dirp;	/* Mount dir path */
160 	uid_t		ncd_authuid;	/* Effective uid */
161 	int		ncd_authtype;	/* Type of authenticator */
162 	u_int		ncd_authlen;	/* Length of authenticator string */
163 	u_char		*ncd_authstr;	/* Authenticator string */
164 	u_int		ncd_verflen;	/* and the verifier */
165 	u_char		*ncd_verfstr;
166 	NFSKERBKEY_T	ncd_key;	/* Session key */
167 };
168 
169 /*
170  * Stats structure
171  */
172 struct nfsstats {
173 	uint64_t	attrcache_hits;
174 	uint64_t	attrcache_misses;
175 	uint64_t	lookupcache_hits;
176 	uint64_t	lookupcache_misses;
177 	uint64_t	direofcache_hits;
178 	uint64_t	direofcache_misses;
179 	uint64_t	biocache_reads;
180 	uint64_t	read_bios;
181 	uint64_t	read_physios;
182 	uint64_t	biocache_writes;
183 	uint64_t	write_bios;
184 	uint64_t	write_physios;
185 	uint64_t	biocache_readlinks;
186 	uint64_t	readlink_bios;
187 	uint64_t	biocache_readdirs;
188 	uint64_t	readdir_bios;
189 	uint64_t	rpccnt[NFS_NPROCS];
190 	uint64_t	rpcretries;
191 	uint64_t	srvrpccnt[NFS_NPROCS];
192 	uint64_t	srvrpc_errs;
193 	uint64_t	srv_errs;
194 	uint64_t	rpcrequests;
195 	uint64_t	rpctimeouts;
196 	uint64_t	rpcunexpected;
197 	uint64_t	rpcinvalid;
198 	uint64_t	srvcache_inproghits;
199 	uint64_t	srvcache_idemdonehits;
200 	uint64_t	srvcache_nonidemdonehits;
201 	uint64_t	srvcache_misses;
202 	uint64_t	srvnqnfs_leases;
203 	uint64_t	srvnqnfs_maxleases;
204 	uint64_t	srvnqnfs_getleases;
205 	uint64_t	srvvop_writes;
206 };
207 
208 /*
209  * Flags for nfssvc() system call.
210  */
211 #define	NFSSVC_BIOD	0x002
212 #define	NFSSVC_NFSD	0x004
213 #define	NFSSVC_ADDSOCK	0x008
214 #define	NFSSVC_AUTHIN	0x010
215 #define	NFSSVC_GOTAUTH	0x040
216 #define	NFSSVC_AUTHINFAIL 0x080
217 #define	NFSSVC_MNTD	0x100
218 
219 /*
220  * fs.nfs sysctl(3) identifiers
221  */
222 #define	NFS_NFSSTATS	1	/* struct: struct nfsstats */
223 #define	NFS_NIOTHREADS	2	/* number of i/o threads */
224 #define	NFS_MAXID	3
225 
226 #define FS_NFS_NAMES { \
227 			{ 0, 0 }, \
228 			{ "nfsstats", CTLTYPE_STRUCT }, \
229 			{ "iothreads", CTLTYPE_INT } \
230 }
231 
232 /*
233  * The set of signals the interrupt an I/O in progress for NFSMNT_INT mounts.
234  * What should be in this set is open to debate, but I believe that since
235  * I/O system calls on ufs are never interrupted by signals the set should
236  * be minimal. My reasoning is that many current programs that use signals
237  * such as SIGALRM will not expect file I/O system calls to be interrupted
238  * by them and break.
239  */
240 #ifdef _KERNEL
241 extern int nfs_niothreads;
242 
243 struct uio; struct buf; struct vattr; struct nameidata;	/* XXX */
244 
245 #define	NFSINT_SIGMASK	(sigmask(SIGINT)|sigmask(SIGTERM)|sigmask(SIGKILL)| \
246 			 sigmask(SIGHUP)|sigmask(SIGQUIT))
247 
248 /*
249  * Socket errors ignored for connectionless sockets??
250  * For now, ignore them all
251  */
252 #define	NFSIGNORE_SOERROR(s, e) \
253 		((e) != EINTR && (e) != ERESTART && (e) != EWOULDBLOCK && \
254 		((s) & PR_CONNREQUIRED) == 0)
255 
256 /*
257  * Nfs outstanding request list element
258  */
259 struct nfsreq {
260 	TAILQ_ENTRY(nfsreq) r_chain;
261 	struct mbuf	*r_mreq;
262 	struct mbuf	*r_mrep;
263 	struct mbuf	*r_md;
264 	caddr_t		r_dpos;
265 	struct nfsmount *r_nmp;
266 	struct vnode	*r_vp;
267 	u_int32_t	r_xid;
268 	int		r_flags;	/* flags on request, see below */
269 	int		r_retry;	/* max retransmission count */
270 	int		r_rexmit;	/* current retrans count */
271 	int		r_timer;	/* tick counter on reply */
272 	int		r_procnum;	/* NFS procedure number */
273 	int		r_rtt;		/* RTT for rpc */
274 	struct proc	*r_procp;	/* Proc that did I/O system call */
275 };
276 
277 /*
278  * Queue head for nfsreq's
279  */
280 extern TAILQ_HEAD(nfsreqhead, nfsreq) nfs_reqq;
281 
282 /* Flag values for r_flags */
283 #define R_TIMING	0x01		/* timing request (in mntp) */
284 #define R_SENT		0x02		/* request has been sent */
285 #define	R_SOFTTERM	0x04		/* soft mnt, too many retries */
286 #define	R_INTR		0x08		/* intr mnt, signal pending */
287 #define	R_SOCKERR	0x10		/* Fatal error on socket */
288 #define	R_TPRINTFMSG	0x20		/* Did a tprintf msg. */
289 #define	R_MUSTRESEND	0x40		/* Must resend request */
290 #define	R_GETONEREP	0x80		/* Probe for one reply only */
291 
292 /*
293  * A list of nfssvc_sock structures is maintained with all the sockets
294  * that require service by the nfsd.
295  * The nfsuid structs hang off of the nfssvc_sock structs in both lru
296  * and uid hash lists.
297  */
298 #ifndef NFS_UIDHASHSIZ
299 #define	NFS_UIDHASHSIZ	29	/* Tune the size of nfssvc_sock with this */
300 #endif
301 #define	NUIDHASH(sock, uid) \
302 	(&(sock)->ns_uidhashtbl[(uid) % NFS_UIDHASHSIZ])
303 #ifndef NFS_WDELAYHASHSIZ
304 #define	NFS_WDELAYHASHSIZ 16	/* and with this */
305 #endif
306 #define	NWDELAYHASH(sock, f) \
307 	(&(sock)->ns_wdelayhashtbl[(*((u_int32_t *)(f))) % NFS_WDELAYHASHSIZ])
308 #ifndef NFS_MUIDHASHSIZ
309 #define NFS_MUIDHASHSIZ	67	/* Tune the size of nfsmount with this */
310 #endif
311 #define	NMUIDHASH(nmp, uid) \
312 	(&(nmp)->nm_uidhashtbl[(uid) % NFS_MUIDHASHSIZ])
313 #define	NFSNOHASH(fhsum) \
314 	(&nfsnodehashtbl[(fhsum) & nfsnodehash])
315 
316 /*
317  * Network address hash list element
318  */
319 union nethostaddr {
320 	u_int32_t had_inetaddr;
321 	struct mbuf *had_nam;
322 };
323 
324 struct nfsuid {
325 	TAILQ_ENTRY(nfsuid) nu_lru;	/* LRU chain */
326 	LIST_ENTRY(nfsuid) nu_hash;	/* Hash list */
327 	int		nu_flag;	/* Flags */
328 	union nethostaddr nu_haddr;	/* Host addr. for dgram sockets */
329 	struct ucred	nu_cr;		/* Cred uid mapped to */
330 	int		nu_expire;	/* Expiry time (sec) */
331 	struct timeval	nu_timestamp;	/* Kerb. timestamp */
332 	u_int32_t	nu_nickname;	/* Nickname on server */
333 	NFSKERBKEY_T	nu_key;		/* and session key */
334 };
335 
336 #define	nu_inetaddr	nu_haddr.had_inetaddr
337 #define	nu_nam		nu_haddr.had_nam
338 /* Bits for nu_flag */
339 #define	NU_INETADDR	0x1
340 #define NU_NAM		0x2
341 #define NU_NETFAM(u)	(((u)->nu_flag & NU_INETADDR) ? AF_INET : AF_ISO)
342 
343 struct nfssvc_sock {
344 	TAILQ_ENTRY(nfssvc_sock) ns_chain;	/* List of all nfssvc_sock's */
345 	TAILQ_HEAD(, nfsuid) ns_uidlruhead;
346 	struct file	*ns_fp;
347 	struct socket	*ns_so;
348 	struct mbuf	*ns_nam;
349 	struct mbuf	*ns_raw;
350 	struct mbuf	*ns_rawend;
351 	struct mbuf	*ns_rec;
352 	struct mbuf	*ns_recend;
353 	struct mbuf	*ns_frag;
354 	int		ns_flag;
355 	int		ns_solock;
356 	int		ns_cc;
357 	int		ns_reclen;
358 	int		ns_numuids;
359 	u_int32_t	ns_sref;
360 	LIST_HEAD(, nfsrv_descript) ns_tq;	/* Write gather lists */
361 	LIST_HEAD(, nfsuid) ns_uidhashtbl[NFS_UIDHASHSIZ];
362 	LIST_HEAD(nfsrvw_delayhash, nfsrv_descript) ns_wdelayhashtbl[NFS_WDELAYHASHSIZ];
363 };
364 
365 /* Bits for "ns_flag" */
366 #define	SLP_VALID	0x01
367 #define	SLP_DOREC	0x02
368 #define	SLP_NEEDQ	0x04
369 #define	SLP_DISCONN	0x08
370 #define	SLP_GETSTREAM	0x10
371 #define	SLP_LASTFRAG	0x20
372 #define SLP_ALLFLAGS	0xff
373 
374 extern TAILQ_HEAD(nfssvc_sockhead, nfssvc_sock) nfssvc_sockhead;
375 extern int nfssvc_sockhead_flag;
376 #define	SLP_INIT	0x01
377 #define	SLP_WANTINIT	0x02
378 
379 /*
380  * One of these structures is allocated for each nfsd.
381  */
382 struct nfsd {
383 	TAILQ_ENTRY(nfsd) nfsd_chain;	/* List of all nfsd's */
384 	int		nfsd_flag;	/* NFSD_ flags */
385 	struct nfssvc_sock *nfsd_slp;	/* Current socket */
386 	int		nfsd_authlen;	/* Authenticator len */
387 	u_char		nfsd_authstr[RPCAUTH_MAXSIZ]; /* Authenticator data */
388 	int		nfsd_verflen;	/* and the Verifier */
389 	u_char		nfsd_verfstr[RPCVERF_MAXSIZ];
390 	struct proc	*nfsd_procp;	/* Proc ptr */
391 	struct nfsrv_descript *nfsd_nd;	/* Associated nfsrv_descript */
392 };
393 
394 /* Bits for "nfsd_flag" */
395 #define	NFSD_WAITING	0x01
396 #define	NFSD_REQINPROG	0x02
397 #define	NFSD_NEEDAUTH	0x04
398 #define	NFSD_AUTHFAIL	0x08
399 
400 /*
401  * This structure is used by the server for describing each request.
402  * Some fields are used only when write request gathering is performed.
403  */
404 struct nfsrv_descript {
405 	u_quad_t		nd_time;	/* Write deadline (usec) */
406 	off_t			nd_off;		/* Start byte offset */
407 	off_t			nd_eoff;	/* and end byte offset */
408 	LIST_ENTRY(nfsrv_descript) nd_hash;	/* Hash list */
409 	LIST_ENTRY(nfsrv_descript) nd_tq;		/* and timer list */
410 	LIST_HEAD(,nfsrv_descript) nd_coalesce;	/* coalesced writes */
411 	struct mbuf		*nd_mrep;	/* Request mbuf list */
412 	struct mbuf		*nd_md;		/* Current dissect mbuf */
413 	struct mbuf		*nd_mreq;	/* Reply mbuf list */
414 	struct mbuf		*nd_nam;	/* and socket addr */
415 	struct mbuf		*nd_nam2;	/* return socket addr */
416 	caddr_t			nd_dpos;	/* Current dissect pos */
417 	unsigned int		nd_procnum;	/* RPC # */
418 	int			nd_stable;	/* storage type */
419 	int			nd_flag;	/* nd_flag */
420 	int			nd_len;		/* Length of this write */
421 	int			nd_repstat;	/* Reply status */
422 	u_int32_t		nd_retxid;	/* Reply xid */
423 	struct timeval		nd_starttime;	/* Time RPC initiated */
424 	fhandle_t		nd_fh;		/* File handle */
425 	struct ucred		nd_cr;		/* Credentials */
426 };
427 
428 /* Bits for "nd_flag" */
429 #define	ND_READ		LEASE_READ
430 #define ND_WRITE	LEASE_WRITE
431 #define ND_CHECK	0x04
432 #define ND_LEASE	(ND_READ | ND_WRITE | ND_CHECK)
433 #define ND_NFSV3	0x08
434 #define ND_KERBNICK	0x20
435 #define ND_KERBFULL	0x40
436 #define ND_KERBAUTH	(ND_KERBNICK | ND_KERBFULL)
437 
438 extern struct pool nfsreqpl;
439 extern TAILQ_HEAD(nfsdhead, nfsd) nfsd_head;
440 extern int nfsd_head_flag;
441 #define	NFSD_CHECKSLP	0x01
442 
443 /*
444  * These macros compare nfsrv_descript structures.
445  */
446 #define NFSW_CONTIG(o, n) \
447 		((o)->nd_eoff >= (n)->nd_off && \
448 		 !bcmp((caddr_t)&(o)->nd_fh, (caddr_t)&(n)->nd_fh, NFSX_V3FH))
449 
450 #define NFSW_SAMECRED(o, n) \
451 	(((o)->nd_flag & ND_KERBAUTH) == ((n)->nd_flag & ND_KERBAUTH) && \
452  	 !bcmp((caddr_t)&(o)->nd_cr, (caddr_t)&(n)->nd_cr, \
453 		sizeof (struct ucred)))
454 
455 #endif	/* _KERNEL */
456 #endif /* _NFS_NFS_H */
457