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