xref: /freebsd/sys/fs/nfs/nfsrvstate.h (revision f6a3b357e9be4c6423c85eff9a847163a0d307c8)
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 	fsid_t			lay_fsid;
136 	uint32_t		lay_layoutlen;
137 	uint16_t		lay_mirrorcnt;
138 	uint16_t		lay_trycnt;
139 	uint16_t		lay_type;
140 	uint16_t		lay_flags;
141 	uint32_t		lay_xdr[0];
142 };
143 
144 /* Flags for lay_flags. */
145 #define	NFSLAY_READ	0x0001
146 #define	NFSLAY_RW	0x0002
147 #define	NFSLAY_RECALL	0x0004
148 #define	NFSLAY_RETURNED	0x0008
149 #define	NFSLAY_CALLB	0x0010
150 
151 /*
152  * Structure for an NFSv4.1 session.
153  * Locking rules for this structure.
154  * To add/delete one of these structures from the lists, you must lock
155  * both: NFSLOCKSTATE() and NFSLOCKSESSION(session hashhead) in that order.
156  * To traverse the lists looking for one of these, you must hold one
157  * of these two locks.
158  * The exception is if the thread holds the exclusive root sleep lock.
159  * In this case, all other nfsd threads are blocked, so locking the
160  * mutexes isn't required.
161  * When manipulating sess_refcnt, NFSLOCKSTATE() must be locked.
162  * When manipulating the fields withinsess_cbsess except nfsess_xprt,
163  * sess_cbsess.nfsess_mtx must be locked.
164  * When manipulating sess_slots and sess_cbsess.nfsess_xprt,
165  * NFSLOCKSESSION(session hashhead) must be locked.
166  */
167 struct nfsdsession {
168 	uint64_t		sess_refcnt;	/* Reference count. */
169 	LIST_ENTRY(nfsdsession)	sess_hash;	/* Hash list of sessions. */
170 	LIST_ENTRY(nfsdsession)	sess_list;	/* List of client sessions. */
171 	struct nfsslot		sess_slots[NFSV4_SLOTS];
172 	struct nfsclient	*sess_clp;	/* Associated clientid. */
173 	uint32_t		sess_crflags;
174 	uint32_t		sess_cbprogram;
175 	uint32_t		sess_maxreq;
176 	uint32_t		sess_maxresp;
177 	uint32_t		sess_maxrespcached;
178 	uint32_t		sess_maxops;
179 	uint32_t		sess_maxslots;
180 	uint32_t		sess_cbmaxreq;
181 	uint32_t		sess_cbmaxresp;
182 	uint32_t		sess_cbmaxrespcached;
183 	uint32_t		sess_cbmaxops;
184 	uint8_t			sess_sessionid[NFSX_V4SESSIONID];
185 	struct nfsclsession	sess_cbsess;	/* Callback session. */
186 };
187 
188 /*
189  * Nfs state structure. I couldn't resist overloading this one, since
190  * it makes cleanup, etc. simpler. These structures are used in four ways:
191  * - open_owner structures chained off of nfsclient
192  * - open file structures chained off an open_owner structure
193  * - lock_owner structures chained off an open file structure
194  * - delegated file structures chained off of nfsclient and nfslockfile
195  * - the ls_list field is used for the chain it is in
196  * - the ls_head structure is used to chain off the sibling structure
197  *   (it is a union between an nfsstate and nfslock structure head)
198  *    If it is a lockowner stateid, nfslock structures hang off it.
199  * For the open file and lockowner cases, it is in the hash table in
200  * nfsclient for stateid.
201  */
202 struct nfsstate {
203 	LIST_ENTRY(nfsstate)	ls_hash;	/* Hash list entry */
204 	LIST_ENTRY(nfsstate)	ls_list;	/* List of opens/delegs */
205 	LIST_ENTRY(nfsstate)	ls_file;	/* Opens/Delegs for a file */
206 	union {
207 		struct nfsstatehead	open; /* Opens list */
208 		struct nfslockhead	lock; /* Locks list */
209 	} ls_head;
210 	nfsv4stateid_t		ls_stateid;	/* The state id */
211 	u_int32_t		ls_seq;		/* seq id */
212 	uid_t			ls_uid;		/* uid of locker */
213 	u_int32_t		ls_flags;	/* Type of lock, etc. */
214 	union {
215 		struct nfsstate	*openowner;	/* Open only */
216 		u_int32_t	opentolockseq;	/* Lock call only */
217 		u_int32_t	noopens;	/* Openowner only */
218 		struct {
219 			u_quad_t	filerev; /* Delegations only */
220 			time_t		expiry;
221 			time_t		limit;
222 			u_int64_t	compref;
223 		} deleg;
224 	} ls_un;
225 	struct nfslockfile	*ls_lfp;	/* Back pointer */
226 	struct nfsrvcache	*ls_op;		/* Op cache reference */
227 	struct nfsclient	*ls_clp;	/* Back pointer */
228 	u_short			ls_ownerlen;	/* Length of ls_owner */
229 	u_char			ls_owner[1];	/* malloc'd the correct size */
230 };
231 #define	ls_lock			ls_head.lock
232 #define	ls_open			ls_head.open
233 #define	ls_opentolockseq	ls_un.opentolockseq
234 #define	ls_openowner		ls_un.openowner
235 #define	ls_openstp		ls_un.openowner
236 #define	ls_noopens		ls_un.noopens
237 #define	ls_filerev		ls_un.deleg.filerev
238 #define	ls_delegtime		ls_un.deleg.expiry
239 #define	ls_delegtimelimit	ls_un.deleg.limit
240 #define	ls_compref		ls_un.deleg.compref
241 
242 /*
243  * Nfs lock structure.
244  * This structure is chained off of the nfsstate (the lockowner) and
245  * nfslockfile (the file) structures, for the file and owner it
246  * refers to. It holds flags and a byte range.
247  * It also has back pointers to the associated lock_owner and lockfile.
248  */
249 struct nfslock {
250 	LIST_ENTRY(nfslock)	lo_lckowner;
251 	LIST_ENTRY(nfslock)	lo_lckfile;
252 	struct nfsstate		*lo_stp;
253 	struct nfslockfile	*lo_lfp;
254 	u_int64_t		lo_first;
255 	u_int64_t		lo_end;
256 	u_int32_t		lo_flags;
257 };
258 
259 /*
260  * Structure used to return a conflicting lock. (Must be large
261  * enough for the largest lock owner we can have.)
262  */
263 struct nfslockconflict {
264 	nfsquad_t		cl_clientid;
265 	u_int64_t		cl_first;
266 	u_int64_t		cl_end;
267 	u_int32_t		cl_flags;
268 	u_short			cl_ownerlen;
269 	u_char			cl_owner[NFSV4_OPAQUELIMIT];
270 };
271 
272 /*
273  * This structure is used to keep track of local locks that might need
274  * to be rolled back.
275  */
276 struct nfsrollback {
277 	LIST_ENTRY(nfsrollback)	rlck_list;
278 	uint64_t		rlck_first;
279 	uint64_t		rlck_end;
280 	int			rlck_type;
281 };
282 
283 /*
284  * This structure refers to a file for which lock(s) and/or open(s) exist.
285  * Searched via hash table on file handle or found via the back pointer from an
286  * open or lock owner.
287  */
288 struct nfslockfile {
289 	LIST_HEAD(, nfsstate)	lf_open;	/* Open list */
290 	LIST_HEAD(, nfsstate)	lf_deleg;	/* Delegation list */
291 	LIST_HEAD(, nfslock)	lf_lock;	/* Lock list */
292 	LIST_HEAD(, nfslock)	lf_locallock;	/* Local lock list */
293 	LIST_HEAD(, nfsrollback) lf_rollback;	/* Local lock rollback list */
294 	LIST_ENTRY(nfslockfile)	lf_hash;	/* Hash list entry */
295 	fhandle_t		lf_fh;		/* The file handle */
296 	struct nfsv4lock	lf_locallock_lck; /* serialize local locking */
297 	int			lf_usecount;	/* Ref count for locking */
298 };
299 
300 /*
301  * This structure is malloc'd an chained off hash lists for user/group
302  * names.
303  */
304 struct nfsusrgrp {
305 	TAILQ_ENTRY(nfsusrgrp)	lug_numhash;	/* Hash by id# */
306 	TAILQ_ENTRY(nfsusrgrp)	lug_namehash;	/* and by name */
307 	time_t			lug_expiry;	/* Expiry time in sec */
308 	union {
309 		uid_t		un_uid;		/* id# */
310 		gid_t		un_gid;
311 	} lug_un;
312 	struct ucred		*lug_cred;	/* Cred. with groups list */
313 	int			lug_namelen;	/* Name length */
314 	u_char			lug_name[1];	/* malloc'd correct length */
315 };
316 #define	lug_uid		lug_un.un_uid
317 #define	lug_gid		lug_un.un_gid
318 
319 /*
320  * These structures are used for the stable storage restart stuff.
321  */
322 /*
323  * Record at beginning of file.
324  */
325 struct nfsf_rec {
326 	u_int32_t	lease;			/* Lease duration */
327 	u_int32_t	numboots;		/* Number of boottimes */
328 };
329 
330 void nfsrv_cleanclient(struct nfsclient *, NFSPROC_T *);
331 void nfsrv_freedeleglist(struct nfsstatehead *);
332 
333 /*
334  * This structure is used to create the list of device info entries for
335  * a GetDeviceInfo operation and stores the DS server info.
336  * The nfsdev_addrandhost field has the fully qualified host domain name
337  * followed by the network address in XDR.
338  * It is allocated with nfsrv_dsdirsize nfsdev_dsdir[] entries.
339  */
340 struct nfsdevice {
341 	TAILQ_ENTRY(nfsdevice)	nfsdev_list;
342 	vnode_t			nfsdev_dvp;
343 	struct nfsmount		*nfsdev_nmp;
344 	char			nfsdev_deviceid[NFSX_V4DEVICEID];
345 	uint16_t		nfsdev_hostnamelen;
346 	uint16_t		nfsdev_fileaddrlen;
347 	uint16_t		nfsdev_flexaddrlen;
348 	uint16_t		nfsdev_mdsisset;
349 	char			*nfsdev_fileaddr;
350 	char			*nfsdev_flexaddr;
351 	char			*nfsdev_host;
352 	fsid_t			nfsdev_mdsfsid;
353 	uint32_t		nfsdev_nextdir;
354 	vnode_t			nfsdev_dsdir[0];
355 };
356 
357 /*
358  * This structure holds the va_size, va_filerev, va_atime and va_mtime for the
359  * DS file and is stored in the metadata file's extended attribute pnfsd.dsattr.
360  */
361 struct pnfsdsattr {
362 	uint64_t	dsa_filerev;
363 	uint64_t	dsa_size;
364 	struct timespec	dsa_atime;
365 	struct timespec	dsa_mtime;
366 };
367 
368 /*
369  * This structure is a list element for a list the pNFS server uses to
370  * mark that the recovery of a mirror file is in progress.
371  */
372 struct nfsdontlist {
373 	LIST_ENTRY(nfsdontlist)	nfsmr_list;
374 	uint32_t		nfsmr_flags;
375 	fhandle_t		nfsmr_fh;
376 };
377 
378 /* nfsmr_flags bits. */
379 #define	NFSMR_DONTLAYOUT	0x00000001
380 
381 #endif	/* defined(_KERNEL) || defined(KERNEL) */
382 
383 /*
384  * This structure holds the information about the DS file and is stored
385  * in the metadata file's extended attribute called pnfsd.dsfile.
386  */
387 #define	PNFS_FILENAME_LEN	(2 * sizeof(fhandle_t))
388 struct pnfsdsfile {
389 	fhandle_t	dsf_fh;
390 	uint32_t	dsf_dir;
391 	union {
392 		struct sockaddr_in	sin;
393 		struct sockaddr_in6	sin6;
394 	} dsf_nam;
395 	char		dsf_filename[PNFS_FILENAME_LEN + 1];
396 };
397 #define	dsf_sin		dsf_nam.sin
398 #define	dsf_sin6	dsf_nam.sin6
399 
400 #endif	/* _NFS_NFSRVSTATE_H_ */
401