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