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