xref: /titanic_44/usr/src/stand/lib/fs/nfs/nfs_inet.h (revision 55553f719b521a0bb4deab6efc944cd30c1a56aa)
1 /*
2  * CDDL HEADER START
3  *
4  * The contents of this file are subject to the terms of the
5  * Common Development and Distribution License, Version 1.0 only
6  * (the "License").  You may not use this file except in compliance
7  * with the License.
8  *
9  * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
10  * or http://www.opensolaris.org/os/licensing.
11  * See the License for the specific language governing permissions
12  * and limitations under the License.
13  *
14  * When distributing Covered Code, include this CDDL HEADER in each
15  * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
16  * If applicable, add the following below this CDDL HEADER, with the
17  * fields enclosed by brackets "[]" replaced with your own identifying
18  * information: Portions Copyright [yyyy] [name of copyright owner]
19  *
20  * CDDL HEADER END
21  */
22 /*
23  * Copyright 2004 Sun Microsystems, Inc.  All rights reserved.
24  * Use is subject to license terms.
25  *
26  * nfs_inet.h contains definitions specific to inetboot's nfs implementation.
27  */
28 
29 #ifndef _NFS_INET_H
30 #define	_NFS_INET_H
31 
32 #pragma ident	"%Z%%M%	%I%	%E% SMI"
33 
34 #ifdef	__cplusplus
35 extern "C" {
36 #endif
37 
38 #include <netinet/in.h>
39 #include <sys/socket.h>
40 #include <net/if.h>
41 #include <netinet/if_ether.h>
42 #include <netinet/in_systm.h>
43 #include <netinet/ip.h>
44 #include <netinet/udp.h>
45 #include <sys/saio.h>
46 #include <rpcsvc/nfs_prot.h>
47 #include <rpcsvc/nfs4_prot.h>
48 #include "clnt.h"
49 #include <sys/vfs.h>
50 #include <sys/dirent.h>
51 
52 #define	NFSBUF_SIZE	(READ_SIZE+1024)
53 #define	READ_SIZE	(8192)	/* NFS readsize */
54 #define	NFS_READ_DECR	(1024)	/* NFS readsize decrement */
55 #define	NFS3BUF_SIZE	(READ3_SIZE+1024)
56 #define	READ3_SIZE	(32 * 1024)	/* NFS3 readsize */
57 #define	NFS4BUF_SIZE	(READ4_SIZE+1024)
58 #define	READ4_SIZE	(32 * 1024)	/* NFS4 readsize */
59 #define	NFS4_MAX_UTF8STRING	(8 * 1024)
60 #define	NFS4_MAX_BITWORDS	(2)
61 #define	NFS_MAX_FERRS	(3)	/* MAX frame errors before decr read size */
62 #define	NFS_REXMIT_MIN	(3)	/* NFS retry min in secs */
63 #define	NFS_REXMIT_MAX	(15)	/* NFS retry max in secs */
64 
65 extern int nfs_readsize;
66 extern struct nfs_file roothandle;
67 extern CLIENT *root_CLIENT;
68 
69 /*
70  * Boot specific V4 fh with maximum allowed data statically allocated
71  */
72 struct nfs_bfh4 {
73 	uint_t len;
74 	char data[NFS4_FHSIZE];
75 };
76 
77 /*
78  * Boot specific V3 fh with maximum allowed data statically allocated
79  */
80 struct nfs_bfh3 {
81 	uint_t len;
82 	char data[NFS3_FHSIZE];
83 };
84 
85 union _nfs_fh {
86 	nfs_fh fh2;
87 	struct nfs_bfh3 fh3;
88 	struct nfs_bfh4 fh4;
89 };
90 
91 union _nfs_cookie {
92 	nfscookie cookie2;
93 	cookie3 cookie3;
94 	nfs_cookie4 cookie4;
95 };
96 
97 union _nfs_ftype {
98 	ftype type2;
99 	ftype3 type3;
100 	nfs_ftype4 type4;
101 };
102 
103 /*
104  * NFS: This structure represents the current open file.
105  */
106 struct nfs_file {
107 	int version;
108 	ulong_t offset;
109 	union _nfs_ftype ftype;
110 	union _nfs_fh fh;
111 	union _nfs_cookie cookie;
112 };
113 
114 struct nfs_fid {
115 	ushort_t nf_len;
116 	ushort_t nf_pad;
117 	struct nfs_fh fh;
118 };
119 
120 #define	cfile_is_dir(cf)    (((cf)->version == NFS_VERSION) ?	\
121 				((cf)->ftype.type2 == NFDIR) :	\
122 				(((cf)->version == NFS_V3) ?	\
123 				((cf)->ftype.type3 == NF3DIR) : \
124 				(((cf)->version == NFS_V4) ?	\
125 				((cf)->ftype.type4 == NF4DIR) : 0)))
126 
127 #define	cfile_is_lnk(cf)    (((cf)->version == NFS_VERSION) ?	\
128 				((cf)->ftype.type2 == NFLNK) :	\
129 				(((cf)->version == NFS_V3) ?	\
130 				((cf)->ftype.type3 == NF3LNK) : \
131 				(((cf)->version == NFS_V4) ?	\
132 				((cf)->ftype.type4 == NF4LNK) : 0)))
133 
134 /*
135  * Predefine an attribute bitmap that inetboot will most likely be
136  * interested in.
137  */
138 typedef union attr4_bitmap1_u {
139 	struct {
140 		unsigned int
141 #ifdef _BIT_FIELDS_HTOL
142 		b_pad4:			11,
143 		b_fattr4_fileid:	1,
144 		b_fattr4_filehandle:	1,
145 		b_pad3:			10,
146 		b_fattr4_fsid:		1,
147 		b_pad2:			3,
148 		b_fattr4_size:		1,
149 		b_pad1:			2,
150 		b_fattr4_type:		1,
151 		b_supported_attrs:	1;
152 #endif
153 #ifdef _BIT_FIELDS_LTOH
154 		b_supported_attrs:	1,
155 		b_fattr4_type:		1,
156 		b_pad1:			2,
157 		b_fattr4_size:		1,
158 		b_pad2:			3,
159 		b_fattr4_fsid:		1,
160 		b_pad3:			10,
161 		b_fattr4_filehandle:	1,
162 		b_fattr4_fileid:	1,
163 		b_pad4:			11;
164 #endif
165 	} bitmap_s;
166 	uint_t word;
167 } attr4_bitmap1_t;
168 
169 #define	bm_supported_attrs	bitmap_s.b_supported_attrs
170 #define	bm_fattr4_type		bitmap_s.b_fattr4_type
171 #define	bm_fattr4_size		bitmap_s.b_fattr4_size
172 #define	bm_fattr4_fsid		bitmap_s.b_fattr4_fsid
173 #define	bm_fattr4_fileid	bitmap_s.b_fattr4_fileid
174 #define	bm_fattr4_filehandle	bitmap_s.b_fattr4_filehandle
175 
176 typedef	union attr4_bitmap2_u {
177 	struct {
178 		unsigned int
179 #ifdef _BIT_FIELDS_HTOL
180 		b_pad4:			10,
181 		b_fattr4_time_modify:	1,
182 		b_fattr4_time_metadata:	1,
183 		b_pad3:			4,
184 		b_fattr4_time_access:	1,
185 		b_pad2:			13,
186 		b_fattr4_mode:		1,
187 		b_pad1:			1;
188 #endif
189 #ifdef _BIT_FIELDS_LTOH
190 		b_pad1:			1,
191 		b_fattr4_mode:		1,
192 		b_pad2:			13,
193 		b_fattr4_time_access:	1,
194 		b_pad3:			4,
195 		b_fattr4_time_metadata:	1,
196 		b_fattr4_time_modify:	1,
197 		b_pad4:			10;
198 #endif
199 	} bitmap_s;
200 	uint_t word;
201 } attr4_bitmap2_t;
202 
203 #define	bm_fattr4_mode		bitmap_s.b_fattr4_mode
204 #define	bm_fattr4_time_access	bitmap_s.b_fattr4_time_access
205 #define	bm_fattr4_time_metadata	bitmap_s.b_fattr4_time_metadata
206 #define	bm_fattr4_time_modify	bitmap_s.b_fattr4_time_modify
207 
208 typedef struct b_bitmap4 {
209 	uint_t b_bitmap_len;
210 	uint_t b_bitmap_val[NFS4_MAX_BITWORDS];
211 } b_bitmap4_t;
212 
213 /*
214  * Define a usable set of v4 atttributes for inetboot.
215  */
216 typedef struct b_fattr4_s {
217 	b_bitmap4_t	b_supported_attrs;
218 	nfs_ftype4	b_fattr4_type;
219 	uint64_t	b_fattr4_size;
220 	fsid4		b_fattr4_fsid;
221 	struct nfs_bfh4	b_fattr4_filehandle;
222 	uint64_t	b_fattr4_fileid;
223 	mode4		b_fattr4_mode;
224 	nfstime4	b_fattr4_time_access;
225 	nfstime4	b_fattr4_time_metadata;
226 	nfstime4	b_fattr4_time_modify;
227 } b_fattr4_t;
228 
229 /*
230  * common to putfh and putfhroot.
231  */
232 typedef struct putfh4arg_s {
233 	uint_t		pf_opnum;	/* can either be putfh or putrootfh */
234 	struct nfs_bfh4	pf_filehandle;	/* only used by putfh */
235 } putfh4arg_t;
236 
237 /*
238  * Use this struct to construct our OTW compound procedures.  Layout makes for
239  * easy XDR'ing. Include putfh.
240  */
241 typedef union compound_u {
242 	struct {
243 		utf8string	tag;
244 		uint_t		minorversion;	/* 0 */
245 		uint_t		argarray_len;	/* 1 + n for putfh */
246 		bool_t		isputrootfh;	/* flag */
247 		putfh4arg_t	opputfh;	/* putfh args */
248 	} compound_ua_s;
249 	struct {
250 		nfsstat4	status;		/* status of last op */
251 		utf8string	tag;
252 		uint_t		resarray_len;	/* 1 + n for putfh */
253 		uint_t		opputfh;	/* putfh opnum */
254 		nfsstat4	putfh_status;	/* putfh status */
255 	} compound_ur_s;
256 } b_compound_t;
257 
258 /*
259  * Define some macros for easy access into the compound structrue
260  */
261 #define	ca_tag compound_ua_s.tag
262 #define	ca_minorversion compound_ua_s.minorversion
263 #define	ca_argarray_len compound_ua_s.argarray_len
264 #define	ca_isputrootfh compound_ua_s.isputrootfh
265 #define	ca_opputfh compound_ua_s.opputfh
266 
267 #define	cr_status compound_ur_s.status
268 #define	cr_tag compound_ur_s.tag
269 #define	cr_resarray_len compound_ur_s.resarray_len
270 #define	cr_opputfh compound_ur_s.opputfh
271 #define	cr_putfh_status compound_ur_s.putfh_status
272 /*
273  * Define simple compound structs that include op specific data
274  */
275 typedef struct getattrres_cmn {
276 	uint_t		gc_opgetattr;		/* getattr opnum */
277 	nfsstat4	gc_attr_status;		/* getattr result */
278 	b_bitmap4_t	gc_retattr;		/* getattr result */
279 	uint_t		gc_attrlist_len;	/* getattr result */
280 	b_fattr4_t	gc_attrs;		/* getattr result */
281 } getattrres_cmn_t;
282 
283 /*
284  * getattr: putfh/getattr
285  */
286 typedef struct getattr4arg_s {
287 	b_compound_t	ga_arg;		/* compound + putfh */
288 	uint_t		ga_opgetattr;	/* getattr opnum */
289 	b_bitmap4_t	ga_attr_req;	/* getattr arg */
290 } getattr4arg_t;
291 
292 typedef struct getattr4res_s {
293 	b_compound_t		gr_res;	/* compound + putfh */
294 	getattrres_cmn_t	gr_cmn;
295 } getattr4res_t;
296 
297 #define	gr_opgetattr gr_cmn.gc_opgetattr
298 #define	gr_attr_status gr_cmn.gc_attr_status
299 #define	gr_retattr gr_cmn.gc_retattr
300 #define	gr_attrs gr_cmn.gc_attrs
301 
302 /*
303  * lookup: putfh/lookup/getattr
304  */
305 typedef struct lookup4arg_s {
306 	b_compound_t	la_arg;		/* compound + putfh */
307 	uint_t		la_oplookup;	/* lookup opnum */
308 	component4	la_pathname;	/* lookup arg */
309 	uint_t		la_opgetattr;	/* getattr opnum */
310 	b_bitmap4_t	la_attr_req;	/* getattr arg */
311 } lookup4arg_t;
312 
313 typedef struct lookup4res_s {
314 	b_compound_t		lr_res;		/* compound + putfh */
315 	uint_t			lr_oplookup;	/* lookup opnum */
316 	nfsstat4		lr_lookup_status;	/* lookup result */
317 	getattrres_cmn_t	lr_gcmn;	/* getattr result */
318 } lookup4res_t;
319 
320 #define	lr_opgetattr lr_gcmn.gc_opgetattr
321 #define	lr_attr_status lr_gcmn.gc_attr_status
322 #define	lr_retattr lr_gcmn.gc_retattr
323 #define	lr_attrs lr_gcmn.gc_attrs
324 
325 /*
326  * lookupp: putfh/lookupp/getattr
327  *
328  * For results: use the lookup4res_t
329  */
330 typedef struct lookupp4arg_s {
331 	b_compound_t	la_arg;		/* compound + putfh */
332 	uint_t		la_oplookupp;	/* lookupp opnum */
333 	uint_t		la_opgetattr;	/* lookupp arg */
334 	b_bitmap4_t	la_attr_req;	/* lookupp arg */
335 } lookupp4arg_t;
336 
337 /*
338  * read: putfh/read
339  */
340 typedef struct read4arg_s {
341 	b_compound_t	r_arg;		/* compound + putfh */
342 	uint_t		r_opread;	/* read opnum */
343 	stateid4	r_stateid;	/* read arg */
344 	offset4		r_offset;	/* read arg */
345 	count4		r_count;	/* read arg */
346 } read4arg_t;
347 
348 typedef struct read4res_s {
349 	b_compound_t	r_res;		/* compound + putfh */
350 	uint_t		r_opread;	/* read opnum */
351 	nfsstat4	r_status;	/* read result */
352 	bool_t		r_eof;		/* read result */
353 	uint_t		r_data_len;	/* read result */
354 	char		*r_data_val;	/* read result */
355 } read4res_t;
356 
357 typedef struct b_entry4_s {
358 	nfs_cookie4		b_cookie;
359 	utf8string		b_name;
360 	uint64_t		b_fileid;
361 	struct b_entry4_s	*b_nextentry;
362 } b_entry4_t;
363 
364 /*
365  * readdir: putfh/readdir/getattr
366  */
367 typedef struct readdir4arg_s {
368 	b_compound_t	rd_arg;		/* compoud + putfh */
369 	uint_t		rd_opreaddir;	/* readdir opnum */
370 	nfs_cookie4	rd_cookie;	/* readdir arg */
371 	verifier4	rd_cookieverf;	/* readdir arg */
372 	count4		rd_dircount;	/* readdir arg */
373 	count4		rd_maxcount;	/* readdir arg */
374 	b_bitmap4_t	rd_attr_req;	/* readdir arg */
375 } readdir4arg_t;
376 
377 typedef struct readdir4res_s {
378 	b_compound_t	rd_res;		/* compound + putfh */
379 	uint_t		rd_opreaddir;	/* readdir opnum */
380 	nfsstat4	rd_status;	/* readdir result */
381 	verifier4	rd_cookieverf;	/* readdir result */
382 	b_entry4_t	*rd_entries;	/* readdir result */
383 	bool_t		rd_eof;		/* readdir result */
384 } readdir4res_t;
385 
386 /*
387  * readlink: putfh/readlink
388  */
389 typedef struct readlink4arg_s {
390 	b_compound_t	rl_arg;		/* compound + putfh */
391 	uint_t		rl_opreadlink;	/* readlink opnum */
392 } readlink4arg_t;
393 
394 typedef struct readlink4res_s {
395 	b_compound_t	rl_res;		/* compound + putfh */
396 	uint_t		rl_opreadlink;	/* readlink opnum */
397 	nfsstat4	rl_status;	/* readlink result */
398 	utf8string	rl_link;	/* readlink result */
399 } readlink4res_t;
400 
401 /*
402  * Generic NFS functions
403  */
404 extern int	boot_nfs_mountroot(char *);
405 extern int	boot_nfs_unmountroot(void);
406 extern int	lookup(char *pathname, struct nfs_file *, bool_t);
407 extern bool_t	whoami(void);
408 extern bool_t	getfile(char *, char *, struct in_addr *, char *);
409 
410 /*
411  * NFS Version 2 specific functions
412  */
413 extern void	nfs_error(enum nfsstat);
414 extern ssize_t	nfsread(struct nfs_file *, char *, size_t);
415 extern int	nfsgetattr(struct nfs_file *, struct vattr *);
416 extern int	nfsgetdents(struct nfs_file *, struct dirent *, unsigned);
417 extern struct nfs_file *nfslookup(struct nfs_file *, char *, int *);
418 extern int nfsgetsymlink(struct nfs_file *cfile, char **path);
419 
420 /*
421  * NFS Version 3 specific functions
422  */
423 extern void	nfs3_error(enum nfsstat3);
424 extern ssize_t	nfs3read(struct nfs_file *, char *, size_t);
425 extern int	nfs3getattr(struct nfs_file *, struct vattr *);
426 extern int	nfs3getdents(struct nfs_file *, struct dirent *, unsigned);
427 extern struct nfs_file *nfs3lookup(struct nfs_file *, char *, int *);
428 extern int	nfs3getsymlink(struct nfs_file *, char **);
429 
430 /*
431  * NFS Version 4 specific functions
432  */
433 extern void	nfs4_error(enum nfsstat4);
434 extern ssize_t	nfs4read(struct nfs_file *, char *, size_t);
435 extern int	nfs4getattr(struct nfs_file *, struct vattr *);
436 extern int	nfs4_getdents(struct nfs_file *, struct dirent *, unsigned);
437 extern struct nfs_file *nfs4lookup(struct nfs_file *, char *, int *);
438 extern struct nfs_file *nfs4lookupp(struct nfs_file *, int *, uint64_t *);
439 extern int	nfs4getsymlink(struct nfs_file *, char **);
440 extern void	compound_init(b_compound_t *, utf8string *, uint_t, uint_t,
441 				struct nfs_bfh4 *);
442 
443 /*
444  * NFSv4 xdr ops
445  */
446 extern bool_t	xdr_getattr4_args(XDR *, getattr4arg_t *);
447 extern bool_t	xdr_getattr4_res(XDR *, getattr4res_t *);
448 extern bool_t	xdr_lookup4_args(XDR *, lookup4arg_t *);
449 extern bool_t	xdr_lookup4_res(XDR *, lookup4res_t *);
450 extern bool_t	xdr_lookupp4_args(XDR *, lookupp4arg_t *);
451 extern bool_t	xdr_read4_args(XDR *, read4arg_t *);
452 extern bool_t	xdr_read4_res(XDR *, read4res_t *);
453 extern bool_t	xdr_readdir4_args(XDR *, readdir4arg_t *);
454 extern bool_t	xdr_readdir4_res(XDR *, readdir4res_t *);
455 extern bool_t	xdr_readlink4_args(XDR *, readlink4arg_t *);
456 extern bool_t	xdr_readlink4_res(XDR *, readlink4res_t *);
457 
458 #ifdef	__cplusplus
459 }
460 #endif
461 
462 #endif /* _NFS_INET_H */
463