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