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