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