xref: /freebsd/sys/fs/nfs/nfsclstate.h (revision 4e8d558c)
1 /*-
2  * SPDX-License-Identifier: BSD-2-Clause
3  *
4  * Copyright (c) 2009 Rick Macklem, University of Guelph
5  * All rights reserved.
6  *
7  * Redistribution and use in source and binary forms, with or without
8  * modification, are permitted provided that the following conditions
9  * are met:
10  * 1. Redistributions of source code must retain the above copyright
11  *    notice, this list of conditions and the following disclaimer.
12  * 2. Redistributions in binary form must reproduce the above copyright
13  *    notice, this list of conditions and the following disclaimer in the
14  *    documentation and/or other materials provided with the distribution.
15  *
16  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
17  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
18  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
19  * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
20  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
21  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
22  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
23  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
24  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
25  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
26  * SUCH DAMAGE.
27  *
28  * $FreeBSD$
29  */
30 
31 #ifndef _NFS_NFSCLSTATE_H_
32 #define	_NFS_NFSCLSTATE_H_
33 
34 /*
35  * Definitions for NFS V4 client state handling.
36  */
37 LIST_HEAD(nfsclopenhead, nfsclopen);
38 LIST_HEAD(nfscllockownerhead, nfscllockowner);
39 SLIST_HEAD(nfscllockownerfhhead, nfscllockownerfh);
40 LIST_HEAD(nfscllockhead, nfscllock);
41 LIST_HEAD(nfsclhead, nfsclclient);
42 LIST_HEAD(nfsclownerhead, nfsclowner);
43 TAILQ_HEAD(nfscldeleghead, nfscldeleg);
44 LIST_HEAD(nfscldeleghash, nfscldeleg);
45 LIST_HEAD(nfsclopenhash, nfsclopen);
46 TAILQ_HEAD(nfscllayouthead, nfscllayout);
47 LIST_HEAD(nfscllayouthash, nfscllayout);
48 LIST_HEAD(nfsclflayouthead, nfsclflayout);
49 LIST_HEAD(nfscldevinfohead, nfscldevinfo);
50 LIST_HEAD(nfsclrecalllayouthead, nfsclrecalllayout);
51 #define	NFSCLDELEGHASHSIZE	256
52 #define	NFSCLDELEGHASH(c, f, l)							\
53 	(&((c)->nfsc_deleghash[ncl_hash((f), (l)) % NFSCLDELEGHASHSIZE]))
54 #define	NFSCLOPENHASHSIZE	256
55 #define	NFSCLOPENHASHFUNC(f, l) (ncl_hash((f), (l)) % NFSCLOPENHASHSIZE)
56 #define	NFSCLOPENHASH(c, f, l)							\
57 	(&((c)->nfsc_openhash[NFSCLOPENHASHFUNC((f), (l))]))
58 #define	NFSCLLAYOUTHASHSIZE	256
59 #define	NFSCLLAYOUTHASH(c, f, l)						\
60 	(&((c)->nfsc_layouthash[ncl_hash((f), (l)) % NFSCLLAYOUTHASHSIZE]))
61 
62 /* Structure for NFSv4.1 session stuff. */
63 struct nfsclsession {
64 	struct mtx	nfsess_mtx;
65 	struct nfsslot	nfsess_cbslots[NFSV4_CBSLOTS];
66 	nfsquad_t	nfsess_clientid;
67 	SVCXPRT		*nfsess_xprt;		/* For backchannel callback */
68 	uint32_t	nfsess_slotseq[64];	/* Max for 64bit nm_slots */
69 	uint64_t	nfsess_slots;
70 	uint64_t	nfsess_badslots;	/* Slots possibly broken */
71 	uint32_t	nfsess_sequenceid;
72 	uint32_t	nfsess_maxcache;	/* Max size for cached reply. */
73 	uint32_t	nfsess_maxreq;		/* Max request size. */
74 	uint32_t	nfsess_maxresp;		/* Max reply size. */
75 	uint16_t	nfsess_foreslots;
76 	uint16_t	nfsess_backslots;
77 	uint8_t		nfsess_sessionid[NFSX_V4SESSIONID];
78 	uint8_t		nfsess_defunct;		/* Non-zero for old sessions */
79 };
80 
81 /*
82  * This structure holds the session, clientid and related information
83  * needed for an NFSv4.1 or NFSv4.2 Meta Data Server (MDS) or Data Server (DS).
84  * It is malloc'd to the correct length.
85  */
86 struct nfsclds {
87 	TAILQ_ENTRY(nfsclds)	nfsclds_list;
88 	struct nfsclsession	nfsclds_sess;
89 	struct mtx		nfsclds_mtx;
90 	struct nfssockreq	*nfsclds_sockp;
91 	time_t			nfsclds_expire;
92 	uint16_t		nfsclds_flags;
93 	uint16_t		nfsclds_servownlen;
94 	uint8_t			nfsclds_verf[NFSX_VERF];
95 	uint8_t			nfsclds_serverown[0];
96 };
97 
98 /*
99  * Flags for nfsclds_flags.
100  */
101 #define	NFSCLDS_HASWRITEVERF	0x0001
102 #define	NFSCLDS_MDS		0x0002
103 #define	NFSCLDS_DS		0x0004
104 #define	NFSCLDS_CLOSED		0x0008
105 #define	NFSCLDS_SAMECONN	0x0010
106 #define	NFSCLDS_MINORV2		0x0020
107 
108 struct nfsclclient {
109 	LIST_ENTRY(nfsclclient) nfsc_list;
110 	struct nfsclownerhead	nfsc_owner;
111 	struct nfscldeleghead	nfsc_deleg;
112 	struct nfscldeleghash	nfsc_deleghash[NFSCLDELEGHASHSIZE];
113 	struct nfsclopenhash	nfsc_openhash[NFSCLOPENHASHSIZE];
114 	struct nfscllayouthead	nfsc_layout;
115 	struct nfscllayouthash	nfsc_layouthash[NFSCLLAYOUTHASHSIZE];
116 	struct nfscldevinfohead	nfsc_devinfo;
117 	struct nfsv4lock	nfsc_lock;
118 	struct proc		*nfsc_renewthread;
119 	struct nfsmount		*nfsc_nmp;
120 	time_t			nfsc_expire;
121 	u_int32_t		nfsc_clientidrev;
122 	u_int32_t		nfsc_rev;
123 	u_int32_t		nfsc_renew;
124 	u_int32_t		nfsc_cbident;
125 	u_int16_t		nfsc_flags;
126 	u_int16_t		nfsc_idlen;
127 	u_int8_t		nfsc_id[1];	/* Malloc'd to correct length */
128 };
129 
130 /*
131  * Bits for nfsc_flags.
132  */
133 #define	NFSCLFLAGS_INITED	0x0001
134 #define	NFSCLFLAGS_HASCLIENTID	0x0002
135 #define	NFSCLFLAGS_RECOVER	0x0004
136 #define	NFSCLFLAGS_UMOUNT	0x0008
137 #define	NFSCLFLAGS_HASTHREAD	0x0010
138 #define	NFSCLFLAGS_AFINET6	0x0020
139 #define	NFSCLFLAGS_EXPIREIT	0x0040
140 #define	NFSCLFLAGS_FIRSTDELEG	0x0080
141 #define	NFSCLFLAGS_GOTDELEG	0x0100
142 #define	NFSCLFLAGS_RECVRINPROG	0x0200
143 
144 struct nfsclowner {
145 	LIST_ENTRY(nfsclowner)	nfsow_list;
146 	struct nfsclopenhead	nfsow_open;
147 	struct nfsclclient	*nfsow_clp;
148 	u_int32_t		nfsow_seqid;
149 	u_int32_t		nfsow_defunct;
150 	struct nfsv4lock	nfsow_rwlock;
151 	u_int8_t		nfsow_owner[NFSV4CL_LOCKNAMELEN];
152 };
153 
154 /*
155  * MALLOC'd to the correct length to accommodate the file handle.
156  */
157 struct nfscldeleg {
158 	TAILQ_ENTRY(nfscldeleg)	nfsdl_list;
159 	LIST_ENTRY(nfscldeleg)	nfsdl_hash;
160 	struct nfsclownerhead	nfsdl_owner;	/* locally issued state */
161 	struct nfscllockownerhead nfsdl_lock;
162 	nfsv4stateid_t		nfsdl_stateid;
163 	struct acl_entry	nfsdl_ace;	/* Delegation ace */
164 	struct nfsclclient	*nfsdl_clp;
165 	struct nfsv4lock	nfsdl_rwlock;	/* for active I/O ops */
166 	struct nfscred		nfsdl_cred;	/* Cred. used for Open */
167 	time_t			nfsdl_timestamp; /* used for stale cleanup */
168 	u_int64_t		nfsdl_sizelimit; /* Limit for file growth */
169 	u_int64_t		nfsdl_size;	/* saved copy of file size */
170 	u_int64_t		nfsdl_change;	/* and change attribute */
171 	struct timespec		nfsdl_modtime;	/* local modify time */
172 	u_int16_t		nfsdl_fhlen;
173 	u_int8_t		nfsdl_flags;
174 	u_int8_t		nfsdl_fh[1];	/* must be last */
175 };
176 
177 /*
178  * nfsdl_flags bits.
179  */
180 #define	NFSCLDL_READ		0x01
181 #define	NFSCLDL_WRITE		0x02
182 #define	NFSCLDL_RECALL		0x04
183 #define	NFSCLDL_NEEDRECLAIM	0x08
184 #define	NFSCLDL_ZAPPED		0x10
185 #define	NFSCLDL_MODTIMESET	0x20
186 #define	NFSCLDL_DELEGRET	0x40
187 
188 /*
189  * MALLOC'd to the correct length to accommodate the file handle.
190  */
191 struct nfsclopen {
192 	LIST_ENTRY(nfsclopen)	nfso_list;
193 	LIST_ENTRY(nfsclopen)	nfso_hash;
194 	struct nfscllockownerhead nfso_lock;
195 	nfsv4stateid_t		nfso_stateid;
196 	struct nfsclowner	*nfso_own;
197 	struct nfscred		nfso_cred;	/* Cred. used for Open */
198 	u_int32_t		nfso_mode;
199 	u_int32_t		nfso_opencnt;
200 	u_int16_t		nfso_fhlen;
201 	u_int8_t		nfso_posixlock;	/* 1 for POSIX type locking */
202 	u_int8_t		nfso_fh[1];	/* must be last */
203 };
204 
205 /*
206  * Return values for nfscl_open(). NFSCLOPEN_OK must == 0.
207  */
208 #define	NFSCLOPEN_OK		0
209 #define	NFSCLOPEN_DOOPEN	1
210 #define	NFSCLOPEN_DOOPENDOWNGRADE 2
211 #define	NFSCLOPEN_SETCRED	3
212 
213 struct nfscllockowner {
214 	LIST_ENTRY(nfscllockowner) nfsl_list;
215 	struct nfscllockhead	nfsl_lock;
216 	struct nfsclopen	*nfsl_open;
217 	NFSPROC_T		*nfsl_inprog;
218 	nfsv4stateid_t		nfsl_stateid;
219 	int			nfsl_lockflags;
220 	u_int32_t		nfsl_seqid;
221 	struct nfsv4lock	nfsl_rwlock;
222 	u_int8_t		nfsl_owner[NFSV4CL_LOCKNAMELEN];
223 	u_int8_t		nfsl_openowner[NFSV4CL_LOCKNAMELEN];
224 };
225 
226 /*
227  * Byte range entry for the above lock owner.
228  */
229 struct nfscllock {
230 	LIST_ENTRY(nfscllock)	nfslo_list;
231 	u_int64_t		nfslo_first;
232 	u_int64_t		nfslo_end;
233 	short			nfslo_type;
234 };
235 
236 /* This structure is used to collect a list of lockowners to free up. */
237 struct nfscllockownerfh {
238 	SLIST_ENTRY(nfscllockownerfh)	nfslfh_list;
239 	struct nfscllockownerhead	nfslfh_lock;
240 	int				nfslfh_len;
241 	uint8_t				nfslfh_fh[NFSX_V4FHMAX];
242 };
243 
244 /*
245  * MALLOC'd to the correct length to accommodate the file handle.
246  */
247 struct nfscllayout {
248 	TAILQ_ENTRY(nfscllayout)	nfsly_list;
249 	LIST_ENTRY(nfscllayout)		nfsly_hash;
250 	nfsv4stateid_t			nfsly_stateid;
251 	struct nfsv4lock		nfsly_lock;
252 	uint64_t			nfsly_filesid[2];
253 	uint64_t			nfsly_lastbyte;
254 	struct nfsclflayouthead		nfsly_flayread;
255 	struct nfsclflayouthead		nfsly_flayrw;
256 	struct nfsclrecalllayouthead	nfsly_recall;
257 	time_t				nfsly_timestamp;
258 	struct nfsclclient		*nfsly_clp;
259 	uint16_t			nfsly_flags;
260 	uint16_t			nfsly_fhlen;
261 	uint8_t				nfsly_fh[1];
262 };
263 
264 /*
265  * Flags for nfsly_flags.
266  */
267 #define	NFSLY_FILES		0x0001
268 #define	NFSLY_BLOCK		0x0002
269 #define	NFSLY_OBJECT		0x0004
270 #define	NFSLY_RECALL		0x0008
271 #define	NFSLY_RECALLFILE	0x0010
272 #define	NFSLY_RECALLFSID	0x0020
273 #define	NFSLY_RECALLALL		0x0040
274 #define	NFSLY_RETONCLOSE	0x0080
275 #define	NFSLY_WRITTEN		0x0100	/* Has been used to write to a DS. */
276 #define	NFSLY_FLEXFILE		0x0200
277 #define	NFSLY_RETURNED		0x0400
278 
279 /*
280  * Flex file layout mirror specific stuff for nfsclflayout.
281  */
282 struct nfsffm {
283 	nfsv4stateid_t		st;
284 	struct nfscldevinfo	*devp;
285 	char			dev[NFSX_V4DEVICEID];
286 	uint32_t		eff;
287 	uid_t			user;
288 	gid_t			group;
289 	struct nfsfh		*fh[NFSDEV_MAXVERS];
290 	uint16_t		fhcnt;
291 };
292 
293 /*
294  * MALLOC'd to the correct length to accommodate the file handle list for File
295  * layout and the list of mirrors for the Flex File Layout.
296  * These hang off of nfsly_flayread and nfsly_flayrw, sorted in increasing
297  * offset order.
298  * The nfsly_flayread list holds the ones with iomode == NFSLAYOUTIOMODE_READ,
299  * whereas the nfsly_flayrw holds the ones with iomode == NFSLAYOUTIOMODE_RW.
300  */
301 struct nfsclflayout {
302 	LIST_ENTRY(nfsclflayout)	nfsfl_list;
303 	uint64_t			nfsfl_off;
304 	uint64_t			nfsfl_end;
305 	uint32_t			nfsfl_iomode;
306 	uint16_t			nfsfl_flags;
307 	union {
308 		struct {
309 			uint64_t	patoff;
310 			uint32_t	util;
311 			uint32_t	stripe1;
312 			uint8_t		dev[NFSX_V4DEVICEID];
313 			uint16_t	fhcnt;
314 			struct nfscldevinfo *devp;
315 		} fl;
316 		struct {
317 			uint64_t	stripeunit;
318 			uint32_t	fflags;
319 			uint32_t	statshint;
320 			uint16_t	mirrorcnt;
321 		} ff;
322 	} nfsfl_un;
323 	union {
324 		struct nfsfh		*fh[0];	/* FH list for DS File layout */
325 		struct nfsffm		ffm[0];	/* Mirror list for Flex File */
326 	} nfsfl_un2;	/* Must be last. Malloc'd to correct array length */
327 };
328 #define	nfsfl_patoff		nfsfl_un.fl.patoff
329 #define	nfsfl_util		nfsfl_un.fl.util
330 #define	nfsfl_stripe1		nfsfl_un.fl.stripe1
331 #define	nfsfl_dev		nfsfl_un.fl.dev
332 #define	nfsfl_fhcnt		nfsfl_un.fl.fhcnt
333 #define	nfsfl_devp		nfsfl_un.fl.devp
334 #define	nfsfl_stripeunit	nfsfl_un.ff.stripeunit
335 #define	nfsfl_fflags		nfsfl_un.ff.fflags
336 #define	nfsfl_statshint		nfsfl_un.ff.statshint
337 #define	nfsfl_mirrorcnt		nfsfl_un.ff.mirrorcnt
338 #define	nfsfl_fh		nfsfl_un2.fh
339 #define	nfsfl_ffm		nfsfl_un2.ffm
340 
341 /*
342  * Flags for nfsfl_flags.
343  */
344 #define	NFSFL_RECALL	0x0001		/* File layout has been recalled */
345 #define	NFSFL_FILE	0x0002		/* File layout */
346 #define	NFSFL_FLEXFILE	0x0004		/* Flex File layout */
347 
348 /*
349  * Structure that is used to store a LAYOUTRECALL.
350  */
351 struct nfsclrecalllayout {
352 	LIST_ENTRY(nfsclrecalllayout)	nfsrecly_list;
353 	uint64_t			nfsrecly_off;
354 	uint64_t			nfsrecly_len;
355 	int				nfsrecly_recalltype;
356 	uint32_t			nfsrecly_iomode;
357 	uint32_t			nfsrecly_stateseqid;
358 	uint32_t			nfsrecly_stat;
359 	uint32_t			nfsrecly_op;
360 	char				nfsrecly_devid[NFSX_V4DEVICEID];
361 };
362 
363 /*
364  * Stores the NFSv4.1 Device Info. Malloc'd to the correct length to
365  * store the list of network connections and list of indices.
366  * nfsdi_data[] is allocated the following way:
367  * - nfsdi_addrcnt * struct nfsclds
368  * - stripe indices, each stored as one byte, since there can be many
369  *   of them. (This implies a limit of 256 on nfsdi_addrcnt, since the
370  *   indices select which address.)
371  * For Flex File, the addrcnt is always one and no stripe indices exist.
372  */
373 struct nfscldevinfo {
374 	LIST_ENTRY(nfscldevinfo)	nfsdi_list;
375 	uint8_t				nfsdi_deviceid[NFSX_V4DEVICEID];
376 	struct nfsclclient		*nfsdi_clp;
377 	uint32_t			nfsdi_refcnt;
378 	uint32_t			nfsdi_layoutrefs;
379 	union {
380 		struct {
381 			uint16_t	stripecnt;
382 		} fl;
383 		struct {
384 			int		versindex;
385 			uint32_t	vers;
386 			uint32_t	minorvers;
387 			uint32_t	rsize;
388 			uint32_t	wsize;
389 		} ff;
390 	} nfsdi_un;
391 	uint16_t			nfsdi_addrcnt;
392 	uint16_t			nfsdi_flags;
393 	struct nfsclds			*nfsdi_data[0];
394 };
395 #define	nfsdi_stripecnt	nfsdi_un.fl.stripecnt
396 #define	nfsdi_versindex	nfsdi_un.ff.versindex
397 #define	nfsdi_vers	nfsdi_un.ff.vers
398 #define	nfsdi_minorvers	nfsdi_un.ff.minorvers
399 #define	nfsdi_rsize	nfsdi_un.ff.rsize
400 #define	nfsdi_wsize	nfsdi_un.ff.wsize
401 
402 /* Flags for nfsdi_flags. */
403 #define	NFSDI_FILELAYOUT	0x0001
404 #define	NFSDI_FLEXFILE		0x0002
405 #define	NFSDI_TIGHTCOUPLED	0X0004
406 
407 /* These inline functions return values from nfsdi_data[]. */
408 /*
409  * Return a pointer to the address at "pos".
410  */
411 static __inline struct nfsclds **
412 nfsfldi_addr(struct nfscldevinfo *ndi, int pos)
413 {
414 
415 	if (pos >= ndi->nfsdi_addrcnt)
416 		return (NULL);
417 	return (&ndi->nfsdi_data[pos]);
418 }
419 
420 /*
421  * Return the Nth ("pos") stripe index.
422  */
423 static __inline int
424 nfsfldi_stripeindex(struct nfscldevinfo *ndi, int pos)
425 {
426 	uint8_t *valp;
427 
428 	if (pos >= ndi->nfsdi_stripecnt)
429 		return (-1);
430 	valp = (uint8_t *)&ndi->nfsdi_data[ndi->nfsdi_addrcnt];
431 	valp += pos;
432 	return ((int)*valp);
433 }
434 
435 /*
436  * Set the Nth ("pos") stripe index to "val".
437  */
438 static __inline void
439 nfsfldi_setstripeindex(struct nfscldevinfo *ndi, int pos, uint8_t val)
440 {
441 	uint8_t *valp;
442 
443 	if (pos >= ndi->nfsdi_stripecnt)
444 		return;
445 	valp = (uint8_t *)&ndi->nfsdi_data[ndi->nfsdi_addrcnt];
446 	valp += pos;
447 	*valp = val;
448 }
449 
450 /*
451  * Macro for incrementing the seqid#.
452  */
453 #define	NFSCL_INCRSEQID(s, n)	do { 					\
454 	    if (((n)->nd_flag & ND_INCRSEQID))				\
455 		(s)++; 							\
456 	} while (0)
457 
458 #endif	/* _NFS_NFSCLSTATE_H_ */
459