xref: /freebsd/sys/fs/nfs/nfsrvstate.h (revision 43a5ec4eb41567cc92586503212743d89686d78f)
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_NFSRVSTATE_H_
32 #define	_NFS_NFSRVSTATE_H_
33 
34 #if defined(_KERNEL) || defined(KERNEL)
35 /*
36  * Definitions for NFS V4 server state handling.
37  */
38 
39 /*
40  * List heads for nfsclient, nfsstate and nfslockfile.
41  * (Some systems seem to like to dynamically size these things, but I
42  *  don't see any point in doing so for these ones.)
43  */
44 LIST_HEAD(nfsclienthashhead, nfsclient);
45 LIST_HEAD(nfsstatehead, nfsstate);
46 LIST_HEAD(nfslockhead, nfslock);
47 LIST_HEAD(nfslockhashhead, nfslockfile);
48 LIST_HEAD(nfssessionhead, nfsdsession);
49 LIST_HEAD(nfssessionhashhead, nfsdsession);
50 TAILQ_HEAD(nfslayouthead, nfslayout);
51 SLIST_HEAD(nfsdsdirhead, nfsdsdir);
52 TAILQ_HEAD(nfsdevicehead, nfsdevice);
53 LIST_HEAD(nfsdontlisthead, nfsdontlist);
54 
55 /*
56  * List head for nfsusrgrp.
57  */
58 TAILQ_HEAD(nfsuserhashhead, nfsusrgrp);
59 
60 #define	NFSCLIENTHASH(id)						\
61 	(&nfsclienthash[(id).lval[1] % nfsrv_clienthashsize])
62 #define	NFSSTATEHASH(clp, id)						\
63 	(&((clp)->lc_stateid[(id).other[2] % nfsrv_statehashsize]))
64 #define	NFSUSERHASH(id)							\
65 	(&nfsuserhash[(id) % nfsrv_lughashsize])
66 #define	NFSUSERNAMEHASH(p, l)						\
67 	(&nfsusernamehash[((l)>=4?(*(p)+*((p)+1)+*((p)+2)+*((p)+3)):*(p)) \
68 		% nfsrv_lughashsize])
69 #define	NFSGROUPHASH(id)						\
70 	(&nfsgrouphash[(id) % nfsrv_lughashsize])
71 #define	NFSGROUPNAMEHASH(p, l)						\
72 	(&nfsgroupnamehash[((l)>=4?(*(p)+*((p)+1)+*((p)+2)+*((p)+3)):*(p)) \
73 		% nfsrv_lughashsize])
74 
75 struct nfssessionhash {
76 	struct mtx			mtx;
77 	struct nfssessionhashhead	list;
78 };
79 #define	NFSSESSIONHASH(f) 						\
80 	(&nfssessionhash[nfsrv_hashsessionid(f) % nfsrv_sessionhashsize])
81 
82 struct nfslayouthash {
83 	struct mtx		mtx;
84 	struct nfslayouthead	list;
85 };
86 #define	NFSLAYOUTHASH(f) 						\
87 	(&nfslayouthash[nfsrv_hashfh(f) % nfsrv_layouthashsize])
88 
89 /*
90  * Client server structure for V4. It is doubly linked into two lists.
91  * The first is a hash table based on the clientid and the second is a
92  * list of all clients maintained in LRU order.
93  * The actual size malloc'd is large enough to accommodate the id string.
94  */
95 struct nfsclient {
96 	LIST_ENTRY(nfsclient) lc_hash;		/* Clientid hash list */
97 	struct nfsstatehead *lc_stateid;	/* Stateid hash */
98 	struct nfsstatehead lc_open;		/* Open owner list */
99 	struct nfsstatehead lc_deleg;		/* Delegations */
100 	struct nfsstatehead lc_olddeleg;	/* and old delegations */
101 	struct nfssessionhead lc_session;	/* List of NFSv4.1 sessions */
102 	time_t		lc_expiry;		/* Expiry time (sec) */
103 	time_t		lc_delegtime;		/* Old deleg expiry (sec) */
104 	nfsquad_t	lc_clientid;		/* 64 bit clientid */
105 	nfsquad_t	lc_confirm;		/* 64 bit confirm value */
106 	u_int32_t	lc_program;		/* RPC Program # */
107 	u_int32_t	lc_callback;		/* Callback id */
108 	u_int32_t	lc_stateindex;		/* Current state index# */
109 	u_int32_t	lc_statemaxindex;	/* Max state index# */
110 	u_int32_t	lc_cbref;		/* Cnt of callbacks */
111 	uid_t		lc_uid;			/* User credential */
112 	gid_t		lc_gid;
113 	u_int16_t	lc_idlen;		/* Client ID and len */
114 	u_int16_t	lc_namelen;		/* plus GSS principal and len */
115 	u_char		*lc_name;
116 	struct nfssockreq lc_req;		/* Callback info */
117 	u_int32_t	lc_flags;		/* LCL_ flag bits */
118 	u_char		lc_verf[NFSX_VERF];	 /* client verifier */
119 	u_char		lc_id[1];		/* Malloc'd correct size */
120 };
121 
122 #define	CLOPS_CONFIRM		0x0001
123 #define	CLOPS_RENEW		0x0002
124 #define	CLOPS_RENEWOP		0x0004
125 
126 /*
127  * Structure for NFSv4.1 Layouts.
128  * Malloc'd to correct size for the lay_xdr.
129  */
130 struct nfslayout {
131 	TAILQ_ENTRY(nfslayout)	lay_list;
132 	nfsv4stateid_t		lay_stateid;
133 	nfsquad_t		lay_clientid;
134 	fhandle_t		lay_fh;
135 	char			lay_deviceid[NFSX_V4DEVICEID];
136 	fsid_t			lay_fsid;
137 	uint32_t		lay_layoutlen;
138 	uint16_t		lay_mirrorcnt;
139 	uint16_t		lay_trycnt;
140 	uint16_t		lay_type;
141 	uint16_t		lay_flags;
142 	uint32_t		lay_xdr[0];
143 };
144 
145 /* Flags for lay_flags. */
146 #define	NFSLAY_READ	0x0001
147 #define	NFSLAY_RW	0x0002
148 #define	NFSLAY_RECALL	0x0004
149 #define	NFSLAY_RETURNED	0x0008
150 #define	NFSLAY_CALLB	0x0010
151 #define	NFSLAY_NOSPC	0x0020
152 
153 /*
154  * Structure for an NFSv4.1 session.
155  * Locking rules for this structure.
156  * To add/delete one of these structures from the lists, you must lock
157  * both: NFSLOCKSTATE() and NFSLOCKSESSION(session hashhead) in that order.
158  * To traverse the lists looking for one of these, you must hold one
159  * of these two locks.
160  * The exception is if the thread holds the exclusive root sleep lock.
161  * In this case, all other nfsd threads are blocked, so locking the
162  * mutexes isn't required.
163  * When manipulating sess_refcnt, NFSLOCKSTATE() must be locked.
164  * When manipulating the fields withinsess_cbsess except nfsess_xprt,
165  * sess_cbsess.nfsess_mtx must be locked.
166  * When manipulating sess_slots and sess_cbsess.nfsess_xprt,
167  * NFSLOCKSESSION(session hashhead) must be locked.
168  */
169 struct nfsdsession {
170 	uint64_t		sess_refcnt;	/* Reference count. */
171 	LIST_ENTRY(nfsdsession)	sess_hash;	/* Hash list of sessions. */
172 	LIST_ENTRY(nfsdsession)	sess_list;	/* List of client sessions. */
173 	struct nfsslot		sess_slots[NFSV4_SLOTS];
174 	struct nfsclient	*sess_clp;	/* Associated clientid. */
175 	uint32_t		sess_crflags;
176 	uint32_t		sess_cbprogram;
177 	uint32_t		sess_maxreq;
178 	uint32_t		sess_maxresp;
179 	uint32_t		sess_maxrespcached;
180 	uint32_t		sess_maxops;
181 	uint32_t		sess_maxslots;
182 	uint32_t		sess_cbmaxreq;
183 	uint32_t		sess_cbmaxresp;
184 	uint32_t		sess_cbmaxrespcached;
185 	uint32_t		sess_cbmaxops;
186 	uint8_t			sess_sessionid[NFSX_V4SESSIONID];
187 	struct nfsclsession	sess_cbsess;	/* Callback session. */
188 };
189 
190 /*
191  * Nfs state structure. I couldn't resist overloading this one, since
192  * it makes cleanup, etc. simpler. These structures are used in four ways:
193  * - open_owner structures chained off of nfsclient
194  * - open file structures chained off an open_owner structure
195  * - lock_owner structures chained off an open file structure
196  * - delegated file structures chained off of nfsclient and nfslockfile
197  * - the ls_list field is used for the chain it is in
198  * - the ls_head structure is used to chain off the sibling structure
199  *   (it is a union between an nfsstate and nfslock structure head)
200  *    If it is a lockowner stateid, nfslock structures hang off it.
201  * For the open file and lockowner cases, it is in the hash table in
202  * nfsclient for stateid.
203  */
204 struct nfsstate {
205 	LIST_ENTRY(nfsstate)	ls_hash;	/* Hash list entry */
206 	LIST_ENTRY(nfsstate)	ls_list;	/* List of opens/delegs */
207 	LIST_ENTRY(nfsstate)	ls_file;	/* Opens/Delegs for a file */
208 	union {
209 		struct nfsstatehead	open; /* Opens list */
210 		struct nfslockhead	lock; /* Locks list */
211 	} ls_head;
212 	nfsv4stateid_t		ls_stateid;	/* The state id */
213 	u_int32_t		ls_seq;		/* seq id */
214 	uid_t			ls_uid;		/* uid of locker */
215 	u_int32_t		ls_flags;	/* Type of lock, etc. */
216 	union {
217 		struct nfsstate	*openowner;	/* Open only */
218 		u_int32_t	opentolockseq;	/* Lock call only */
219 		u_int32_t	noopens;	/* Openowner only */
220 		struct {
221 			u_quad_t	filerev; /* Delegations only */
222 			time_t		expiry;
223 			time_t		limit;
224 			u_int64_t	compref;
225 			time_t		last;
226 		} deleg;
227 	} ls_un;
228 	struct nfslockfile	*ls_lfp;	/* Back pointer */
229 	struct nfsrvcache	*ls_op;		/* Op cache reference */
230 	struct nfsclient	*ls_clp;	/* Back pointer */
231 	u_short			ls_ownerlen;	/* Length of ls_owner */
232 	u_char			ls_owner[1];	/* malloc'd the correct size */
233 };
234 #define	ls_lock			ls_head.lock
235 #define	ls_open			ls_head.open
236 #define	ls_opentolockseq	ls_un.opentolockseq
237 #define	ls_openowner		ls_un.openowner
238 #define	ls_openstp		ls_un.openowner
239 #define	ls_noopens		ls_un.noopens
240 #define	ls_filerev		ls_un.deleg.filerev
241 #define	ls_delegtime		ls_un.deleg.expiry
242 #define	ls_delegtimelimit	ls_un.deleg.limit
243 #define	ls_compref		ls_un.deleg.compref
244 #define	ls_lastrecall		ls_un.deleg.last
245 
246 /*
247  * Nfs lock structure.
248  * This structure is chained off of the nfsstate (the lockowner) and
249  * nfslockfile (the file) structures, for the file and owner it
250  * refers to. It holds flags and a byte range.
251  * It also has back pointers to the associated lock_owner and lockfile.
252  */
253 struct nfslock {
254 	LIST_ENTRY(nfslock)	lo_lckowner;
255 	LIST_ENTRY(nfslock)	lo_lckfile;
256 	struct nfsstate		*lo_stp;
257 	struct nfslockfile	*lo_lfp;
258 	u_int64_t		lo_first;
259 	u_int64_t		lo_end;
260 	u_int32_t		lo_flags;
261 };
262 
263 /*
264  * Structure used to return a conflicting lock. (Must be large
265  * enough for the largest lock owner we can have.)
266  */
267 struct nfslockconflict {
268 	nfsquad_t		cl_clientid;
269 	u_int64_t		cl_first;
270 	u_int64_t		cl_end;
271 	u_int32_t		cl_flags;
272 	u_short			cl_ownerlen;
273 	u_char			cl_owner[NFSV4_OPAQUELIMIT];
274 };
275 
276 /*
277  * This structure is used to keep track of local locks that might need
278  * to be rolled back.
279  */
280 struct nfsrollback {
281 	LIST_ENTRY(nfsrollback)	rlck_list;
282 	uint64_t		rlck_first;
283 	uint64_t		rlck_end;
284 	int			rlck_type;
285 };
286 
287 /*
288  * This structure refers to a file for which lock(s) and/or open(s) exist.
289  * Searched via hash table on file handle or found via the back pointer from an
290  * open or lock owner.
291  */
292 struct nfslockfile {
293 	LIST_HEAD(, nfsstate)	lf_open;	/* Open list */
294 	LIST_HEAD(, nfsstate)	lf_deleg;	/* Delegation list */
295 	LIST_HEAD(, nfslock)	lf_lock;	/* Lock list */
296 	LIST_HEAD(, nfslock)	lf_locallock;	/* Local lock list */
297 	LIST_HEAD(, nfsrollback) lf_rollback;	/* Local lock rollback list */
298 	LIST_ENTRY(nfslockfile)	lf_hash;	/* Hash list entry */
299 	fhandle_t		lf_fh;		/* The file handle */
300 	struct nfsv4lock	lf_locallock_lck; /* serialize local locking */
301 	int			lf_usecount;	/* Ref count for locking */
302 };
303 
304 /*
305  * This structure is malloc'd an chained off hash lists for user/group
306  * names.
307  */
308 struct nfsusrgrp {
309 	TAILQ_ENTRY(nfsusrgrp)	lug_numhash;	/* Hash by id# */
310 	TAILQ_ENTRY(nfsusrgrp)	lug_namehash;	/* and by name */
311 	time_t			lug_expiry;	/* Expiry time in sec */
312 	union {
313 		uid_t		un_uid;		/* id# */
314 		gid_t		un_gid;
315 	} lug_un;
316 	struct ucred		*lug_cred;	/* Cred. with groups list */
317 	int			lug_namelen;	/* Name length */
318 	u_char			lug_name[1];	/* malloc'd correct length */
319 };
320 #define	lug_uid		lug_un.un_uid
321 #define	lug_gid		lug_un.un_gid
322 
323 /*
324  * These structures are used for the stable storage restart stuff.
325  */
326 /*
327  * Record at beginning of file.
328  */
329 struct nfsf_rec {
330 	u_int32_t	lease;			/* Lease duration */
331 	u_int32_t	numboots;		/* Number of boottimes */
332 };
333 
334 void nfsrv_cleanclient(struct nfsclient *, NFSPROC_T *);
335 void nfsrv_freedeleglist(struct nfsstatehead *);
336 
337 /*
338  * This structure is used to create the list of device info entries for
339  * a GetDeviceInfo operation and stores the DS server info.
340  * The nfsdev_addrandhost field has the fully qualified host domain name
341  * followed by the network address in XDR.
342  * It is allocated with nfsrv_dsdirsize nfsdev_dsdir[] entries.
343  */
344 struct nfsdevice {
345 	TAILQ_ENTRY(nfsdevice)	nfsdev_list;
346 	vnode_t			nfsdev_dvp;
347 	struct nfsmount		*nfsdev_nmp;
348 	char			nfsdev_deviceid[NFSX_V4DEVICEID];
349 	uint16_t		nfsdev_hostnamelen;
350 	uint16_t		nfsdev_fileaddrlen;
351 	uint16_t		nfsdev_flexaddrlen;
352 	uint16_t		nfsdev_mdsisset;
353 	char			*nfsdev_fileaddr;
354 	char			*nfsdev_flexaddr;
355 	char			*nfsdev_host;
356 	fsid_t			nfsdev_mdsfsid;
357 	uint32_t		nfsdev_nextdir;
358 	bool			nfsdev_nospc;
359 	vnode_t			nfsdev_dsdir[0];
360 };
361 
362 /*
363  * This structure holds the va_size, va_filerev, va_atime, va_mtime and
364  * va_bytes for the DS file and is stored in the metadata file's extended
365  * attribute pnfsd.dsattr.
366  * opnfsdsattr was missing the va_bytes field and, as such, it was updated.
367  */
368 struct opnfsdsattr {
369 	uint64_t	dsa_filerev;
370 	uint64_t	dsa_size;
371 	struct timespec	dsa_atime;
372 	struct timespec	dsa_mtime;
373 };
374 
375 struct pnfsdsattr {
376 	uint64_t	dsa_filerev;
377 	uint64_t	dsa_size;
378 	struct timespec	dsa_atime;
379 	struct timespec	dsa_mtime;
380 	uint64_t	dsa_bytes;
381 };
382 
383 /*
384  * This structure is a list element for a list the pNFS server uses to
385  * mark that the recovery of a mirror file is in progress.
386  */
387 struct nfsdontlist {
388 	LIST_ENTRY(nfsdontlist)	nfsmr_list;
389 	uint32_t		nfsmr_flags;
390 	fhandle_t		nfsmr_fh;
391 };
392 
393 /* nfsmr_flags bits. */
394 #define	NFSMR_DONTLAYOUT	0x00000001
395 
396 #endif	/* defined(_KERNEL) || defined(KERNEL) */
397 
398 /*
399  * This structure holds the information about the DS file and is stored
400  * in the metadata file's extended attribute called pnfsd.dsfile.
401  */
402 #define	PNFS_FILENAME_LEN	(2 * sizeof(fhandle_t))
403 struct pnfsdsfile {
404 	fhandle_t	dsf_fh;
405 	uint32_t	dsf_dir;
406 	union {
407 		struct sockaddr_in	sin;
408 		struct sockaddr_in6	sin6;
409 	} dsf_nam;
410 	char		dsf_filename[PNFS_FILENAME_LEN + 1];
411 };
412 #define	dsf_sin		dsf_nam.sin
413 #define	dsf_sin6	dsf_nam.sin6
414 
415 #endif	/* _NFS_NFSRVSTATE_H_ */
416