xref: /netbsd-src/sys/nfs/nfs.h (revision fdecd6a253f999ae92b139670d9e15cc9df4497c)
1 /*	$NetBSD: nfs.h,v 1.15 1997/06/24 23:28:15 fvdl Exp $	*/
2 /*
3  * Copyright (c) 1989, 1993, 1995
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  * 3. All advertising materials mentioning features or use of this software
18  *    must display the following acknowledgement:
19  *	This product includes software developed by the University of
20  *	California, Berkeley and its contributors.
21  * 4. Neither the name of the University nor the names of its contributors
22  *    may be used to endorse or promote products derived from this software
23  *    without specific prior written permission.
24  *
25  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
26  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
27  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
28  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
29  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
30  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
31  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
32  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
33  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
34  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
35  * SUCH DAMAGE.
36  *
37  *	@(#)nfs.h	8.4 (Berkeley) 5/1/95
38  */
39 
40 #ifndef _NFS_NFS_H_
41 #define _NFS_NFS_H_
42 
43 /*
44  * Tunable constants for nfs
45  */
46 
47 #define	NFS_MAXIOVEC	34
48 #define NFS_TICKINTVL	5		/* Desired time for a tick (msec) */
49 #define NFS_HZ		(hz / nfs_ticks) /* Ticks/sec */
50 #define	NFS_TIMEO	(3 * NFS_HZ)	/* Default timeout = 3 seconds */
51 #define	NFS_MINTIMEO	(1 * NFS_HZ)	/* Min timeout to use */
52 #define	NFS_MAXTIMEO	(60 * NFS_HZ)	/* Max timeout to backoff to */
53 #define	NFS_MINIDEMTIMEO (5 * NFS_HZ)	/* Min timeout for non-idempotent ops*/
54 #define	NFS_MAXREXMIT	100		/* Stop counting after this many */
55 #define	NFS_MAXWINDOW	1024		/* Max number of outstanding requests */
56 #define	NFS_RETRANS	10		/* Num of retrans for soft mounts */
57 #define	NFS_MAXGRPS	16		/* Max. size of groups list */
58 #ifndef NFS_MINATTRTIMO
59 #define	NFS_MINATTRTIMO 5		/* Attribute cache timeout in sec */
60 #endif
61 #ifndef NFS_MAXATTRTIMO
62 #define	NFS_MAXATTRTIMO 60
63 #endif
64 #define	NFS_WSIZE	8192		/* Def. write data size <= 8192 */
65 #define	NFS_RSIZE	8192		/* Def. read data size <= 8192 */
66 #define NFS_READDIRSIZE	8192		/* Def. readdir size */
67 #define	NFS_DEFRAHEAD	1		/* Def. read ahead # blocks */
68 #define	NFS_MAXRAHEAD	4		/* Max. read ahead # blocks */
69 #define	NFS_MAXUIDHASH	64		/* Max. # of hashed uid entries/mp */
70 #define	NFS_MAXASYNCDAEMON 	20	/* Max. number async_daemons runable */
71 #define NFS_MAXGATHERDELAY	100	/* Max. write gather delay (msec) */
72 #ifndef NFS_GATHERDELAY
73 #define NFS_GATHERDELAY		10	/* Default write gather delay (msec) */
74 #endif
75 /*
76  * Ideally, NFS_DIRBLKSIZ should be bigger, but I've seen servers with
77  * broken NFS/ethernet drivers that won't work with anything bigger (Linux..)
78  */
79 #define	NFS_DIRBLKSIZ	1024		/* Must be a multiple of DIRBLKSIZ */
80 #define NFS_READDIRBLKSIZ	512	/* Size of read dir blocks. XXX */
81 
82 /*
83  * Oddballs
84  */
85 #define	NMOD(a)		((a) % nfs_asyncdaemons)
86 #define NFS_CMPFH(n, f, s) \
87 	((n)->n_fhsize == (s) && !bcmp((caddr_t)(n)->n_fhp, (caddr_t)(f), (s)))
88 #define NFS_ISV3(v)	(VFSTONFS((v)->v_mount)->nm_flag & NFSMNT_NFSV3)
89 #define NFS_SRVMAXDATA(n) \
90 		(((n)->nd_flag & ND_NFSV3) ? (((n)->nd_nam2) ? \
91 		 NFS_MAXDGRAMDATA : NFS_MAXDATA) : NFS_V2MAXDATA)
92 
93 /*
94  * The VA_EXCLUSIVE flag should be added for va_vaflags and set for an
95  * exclusive create.
96  */
97 #ifndef VA_EXCLUSIVE
98 #define VA_EXCLUSIVE	0
99 #endif
100 
101 /*
102  * The B_INVAFTERWRITE flag should be set to whatever is required by the
103  * buffer cache code to say "Invalidate the block after it is written back".
104  */
105 #define	B_INVAFTERWRITE	B_INVAL
106 
107 /*
108  * The IO_METASYNC flag should be implemented for local file systems.
109  * (Until then, it is nothin at all.)
110  */
111 #ifndef IO_METASYNC
112 #define IO_METASYNC	0
113 #endif
114 
115 /*
116  * Set the attribute timeout based on how recently the file has been modified.
117  */
118 #define	NFS_ATTRTIMEO(np) \
119 	((((np)->n_flag & NMODIFIED) || \
120 	 (time.tv_sec - (np)->n_mtime) / 10 < NFS_MINATTRTIMO) ? NFS_MINATTRTIMO : \
121 	 ((time.tv_sec - (np)->n_mtime) / 10 > NFS_MAXATTRTIMO ? NFS_MAXATTRTIMO : \
122 	  (time.tv_sec - (np)->n_mtime) / 10))
123 
124 /*
125  * Expected allocation sizes for major data structures. If the actual size
126  * of the structure exceeds these sizes, then malloc() will be allocating
127  * almost twice the memory required. This is used in nfs_init() to warn
128  * the sysadmin that the size of a structure should be reduced.
129  * (These sizes are always a power of 2. If the kernel malloc() changes
130  *  to one that does not allocate space in powers of 2 size, then this all
131  *  becomes bunk!)
132  */
133 #define NFS_NODEALLOC	256
134 #define NFS_MNTALLOC	512
135 #define NFS_SVCALLOC	256
136 #define NFS_UIDALLOC	128
137 
138 /*
139  * Structures for the nfssvc(2) syscall. Not that anyone but nfsd and mount_nfs
140  * should ever try and use it.
141  */
142 struct nfsd_args {
143 	int	sock;		/* Socket to serve */
144 	caddr_t	name;		/* Client addr for connection based sockets */
145 	int	namelen;	/* Length of name */
146 };
147 
148 struct nfsd_srvargs {
149 	struct nfsd	*nsd_nfsd;	/* Pointer to in kernel nfsd struct */
150 	uid_t		nsd_uid;	/* Effective uid mapped to cred */
151 	u_int32_t	nsd_haddr;	/* Ip address of client */
152 	struct ucred	nsd_cr;		/* Cred. uid maps to */
153 	int		nsd_authlen;	/* Length of auth string (ret) */
154 	u_char		*nsd_authstr;	/* Auth string (ret) */
155 	int		nsd_verflen;	/* and the verfier */
156 	u_char		*nsd_verfstr;
157 	struct timeval	nsd_timestamp;	/* timestamp from verifier */
158 	u_int32_t	nsd_ttl;	/* credential ttl (sec) */
159 	NFSKERBKEY_T	nsd_key;	/* Session key */
160 };
161 
162 struct nfsd_cargs {
163 	char		*ncd_dirp;	/* Mount dir path */
164 	uid_t		ncd_authuid;	/* Effective uid */
165 	int		ncd_authtype;	/* Type of authenticator */
166 	int		ncd_authlen;	/* Length of authenticator string */
167 	u_char		*ncd_authstr;	/* Authenticator string */
168 	int		ncd_verflen;	/* and the verifier */
169 	u_char		*ncd_verfstr;
170 	NFSKERBKEY_T	ncd_key;	/* Session key */
171 };
172 
173 /*
174  * Stats structure
175  */
176 struct nfsstats {
177 	int	attrcache_hits;
178 	int	attrcache_misses;
179 	int	lookupcache_hits;
180 	int	lookupcache_misses;
181 	int	direofcache_hits;
182 	int	direofcache_misses;
183 	int	biocache_reads;
184 	int	read_bios;
185 	int	read_physios;
186 	int	biocache_writes;
187 	int	write_bios;
188 	int	write_physios;
189 	int	biocache_readlinks;
190 	int	readlink_bios;
191 	int	biocache_readdirs;
192 	int	readdir_bios;
193 	int	rpccnt[NFS_NPROCS];
194 	int	rpcretries;
195 	int	srvrpccnt[NFS_NPROCS];
196 	int	srvrpc_errs;
197 	int	srv_errs;
198 	int	rpcrequests;
199 	int	rpctimeouts;
200 	int	rpcunexpected;
201 	int	rpcinvalid;
202 	int	srvcache_inproghits;
203 	int	srvcache_idemdonehits;
204 	int	srvcache_nonidemdonehits;
205 	int	srvcache_misses;
206 	int	srvnqnfs_leases;
207 	int	srvnqnfs_maxleases;
208 	int	srvnqnfs_getleases;
209 	int	srvvop_writes;
210 };
211 
212 /*
213  * Flags for nfssvc() system call.
214  */
215 #define	NFSSVC_BIOD	0x002
216 #define	NFSSVC_NFSD	0x004
217 #define	NFSSVC_ADDSOCK	0x008
218 #define	NFSSVC_AUTHIN	0x010
219 #define	NFSSVC_GOTAUTH	0x040
220 #define	NFSSVC_AUTHINFAIL 0x080
221 #define	NFSSVC_MNTD	0x100
222 
223 /*
224  * fs.nfs sysctl(3) identifiers
225  */
226 #define NFS_NFSSTATS	1		/* struct: struct nfsstats */
227 
228 #define FS_NFS_NAMES { \
229 		       { 0, 0 }, \
230 		       { "nfsstats", CTLTYPE_STRUCT }, \
231 }
232 
233 /*
234  * The set of signals the interrupt an I/O in progress for NFSMNT_INT mounts.
235  * What should be in this set is open to debate, but I believe that since
236  * I/O system calls on ufs are never interrupted by signals the set should
237  * be minimal. My reasoning is that many current programs that use signals
238  * such as SIGALRM will not expect file I/O system calls to be interrupted
239  * by them and break.
240  */
241 #ifdef _KERNEL
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 	u_int32_t	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 TAILQ_HEAD(, 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	63	/* 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 TAILQ_HEAD(, nfssvc_sock) nfssvc_sockhead;
375 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 	u_int32_t		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 	u_int32_t		nd_duration;	/* Lease duration */
424 	struct timeval		nd_starttime;	/* Time RPC initiated */
425 	fhandle_t		nd_fh;		/* File handle */
426 	struct ucred		nd_cr;		/* Credentials */
427 };
428 
429 /* Bits for "nd_flag" */
430 #define	ND_READ		LEASE_READ
431 #define ND_WRITE	LEASE_WRITE
432 #define ND_CHECK	0x04
433 #define ND_LEASE	(ND_READ | ND_WRITE | ND_CHECK)
434 #define ND_NFSV3	0x08
435 #define ND_NQNFS	0x10
436 #define ND_KERBNICK	0x20
437 #define ND_KERBFULL	0x40
438 #define ND_KERBAUTH	(ND_KERBNICK | ND_KERBFULL)
439 
440 TAILQ_HEAD(, nfsd) nfsd_head;
441 int nfsd_head_flag;
442 #define	NFSD_CHECKSLP	0x01
443 
444 /*
445  * These macros compare nfsrv_descript structures.
446  */
447 #define NFSW_CONTIG(o, n) \
448 		((o)->nd_eoff >= (n)->nd_off && \
449 		 !bcmp((caddr_t)&(o)->nd_fh, (caddr_t)&(n)->nd_fh, NFSX_V3FH))
450 
451 #define NFSW_SAMECRED(o, n) \
452 	(((o)->nd_flag & ND_KERBAUTH) == ((n)->nd_flag & ND_KERBAUTH) && \
453  	 !bcmp((caddr_t)&(o)->nd_cr, (caddr_t)&(n)->nd_cr, \
454 		sizeof (struct ucred)))
455 
456 /*
457  * Defines for WebNFS
458  */
459 
460 #define WEBNFS_ESC_CHAR		'%'
461 #define WEBNFS_SPECCHAR_START	0x80
462 
463 #define WEBNFS_NATIVE_CHAR	0x80
464 /*
465  * ..
466  * Possibly more here in the future.
467  */
468 
469 /*
470  * Macro for converting escape characters in WebNFS pathnames.
471  * Should really be in libkern.
472  */
473 
474 #define HEXTOC(c) \
475 	((c) >= 'a' ? ((c) - ('a' - 10)) : \
476 	    ((c) >= 'A' ? ((c) - ('A' - 10)) : ((c) - '0')))
477 #define HEXSTRTOI(p) \
478 	((HEXTOC(p[0]) << 4) + HEXTOC(p[1]))
479 #endif	/* _KERNEL */
480 
481 #endif /* _NFS_NFS_H */
482