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