xref: /freebsd/sys/fs/nfs/nfsrvstate.h (revision dd41de95a84d979615a2ef11df6850622bf6184e)
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 			time_t		last;
224 		} deleg;
225 	} ls_un;
226 	struct nfslockfile	*ls_lfp;	/* Back pointer */
227 	struct nfsrvcache	*ls_op;		/* Op cache reference */
228 	struct nfsclient	*ls_clp;	/* Back pointer */
229 	u_short			ls_ownerlen;	/* Length of ls_owner */
230 	u_char			ls_owner[1];	/* malloc'd the correct size */
231 };
232 #define	ls_lock			ls_head.lock
233 #define	ls_open			ls_head.open
234 #define	ls_opentolockseq	ls_un.opentolockseq
235 #define	ls_openowner		ls_un.openowner
236 #define	ls_openstp		ls_un.openowner
237 #define	ls_noopens		ls_un.noopens
238 #define	ls_filerev		ls_un.deleg.filerev
239 #define	ls_delegtime		ls_un.deleg.expiry
240 #define	ls_delegtimelimit	ls_un.deleg.limit
241 #define	ls_compref		ls_un.deleg.compref
242 #define	ls_lastrecall		ls_un.deleg.last
243 
244 /*
245  * Nfs lock structure.
246  * This structure is chained off of the nfsstate (the lockowner) and
247  * nfslockfile (the file) structures, for the file and owner it
248  * refers to. It holds flags and a byte range.
249  * It also has back pointers to the associated lock_owner and lockfile.
250  */
251 struct nfslock {
252 	LIST_ENTRY(nfslock)	lo_lckowner;
253 	LIST_ENTRY(nfslock)	lo_lckfile;
254 	struct nfsstate		*lo_stp;
255 	struct nfslockfile	*lo_lfp;
256 	u_int64_t		lo_first;
257 	u_int64_t		lo_end;
258 	u_int32_t		lo_flags;
259 };
260 
261 /*
262  * Structure used to return a conflicting lock. (Must be large
263  * enough for the largest lock owner we can have.)
264  */
265 struct nfslockconflict {
266 	nfsquad_t		cl_clientid;
267 	u_int64_t		cl_first;
268 	u_int64_t		cl_end;
269 	u_int32_t		cl_flags;
270 	u_short			cl_ownerlen;
271 	u_char			cl_owner[NFSV4_OPAQUELIMIT];
272 };
273 
274 /*
275  * This structure is used to keep track of local locks that might need
276  * to be rolled back.
277  */
278 struct nfsrollback {
279 	LIST_ENTRY(nfsrollback)	rlck_list;
280 	uint64_t		rlck_first;
281 	uint64_t		rlck_end;
282 	int			rlck_type;
283 };
284 
285 /*
286  * This structure refers to a file for which lock(s) and/or open(s) exist.
287  * Searched via hash table on file handle or found via the back pointer from an
288  * open or lock owner.
289  */
290 struct nfslockfile {
291 	LIST_HEAD(, nfsstate)	lf_open;	/* Open list */
292 	LIST_HEAD(, nfsstate)	lf_deleg;	/* Delegation list */
293 	LIST_HEAD(, nfslock)	lf_lock;	/* Lock list */
294 	LIST_HEAD(, nfslock)	lf_locallock;	/* Local lock list */
295 	LIST_HEAD(, nfsrollback) lf_rollback;	/* Local lock rollback list */
296 	LIST_ENTRY(nfslockfile)	lf_hash;	/* Hash list entry */
297 	fhandle_t		lf_fh;		/* The file handle */
298 	struct nfsv4lock	lf_locallock_lck; /* serialize local locking */
299 	int			lf_usecount;	/* Ref count for locking */
300 };
301 
302 /*
303  * This structure is malloc'd an chained off hash lists for user/group
304  * names.
305  */
306 struct nfsusrgrp {
307 	TAILQ_ENTRY(nfsusrgrp)	lug_numhash;	/* Hash by id# */
308 	TAILQ_ENTRY(nfsusrgrp)	lug_namehash;	/* and by name */
309 	time_t			lug_expiry;	/* Expiry time in sec */
310 	union {
311 		uid_t		un_uid;		/* id# */
312 		gid_t		un_gid;
313 	} lug_un;
314 	struct ucred		*lug_cred;	/* Cred. with groups list */
315 	int			lug_namelen;	/* Name length */
316 	u_char			lug_name[1];	/* malloc'd correct length */
317 };
318 #define	lug_uid		lug_un.un_uid
319 #define	lug_gid		lug_un.un_gid
320 
321 /*
322  * These structures are used for the stable storage restart stuff.
323  */
324 /*
325  * Record at beginning of file.
326  */
327 struct nfsf_rec {
328 	u_int32_t	lease;			/* Lease duration */
329 	u_int32_t	numboots;		/* Number of boottimes */
330 };
331 
332 void nfsrv_cleanclient(struct nfsclient *, NFSPROC_T *);
333 void nfsrv_freedeleglist(struct nfsstatehead *);
334 
335 /*
336  * This structure is used to create the list of device info entries for
337  * a GetDeviceInfo operation and stores the DS server info.
338  * The nfsdev_addrandhost field has the fully qualified host domain name
339  * followed by the network address in XDR.
340  * It is allocated with nfsrv_dsdirsize nfsdev_dsdir[] entries.
341  */
342 struct nfsdevice {
343 	TAILQ_ENTRY(nfsdevice)	nfsdev_list;
344 	vnode_t			nfsdev_dvp;
345 	struct nfsmount		*nfsdev_nmp;
346 	char			nfsdev_deviceid[NFSX_V4DEVICEID];
347 	uint16_t		nfsdev_hostnamelen;
348 	uint16_t		nfsdev_fileaddrlen;
349 	uint16_t		nfsdev_flexaddrlen;
350 	uint16_t		nfsdev_mdsisset;
351 	char			*nfsdev_fileaddr;
352 	char			*nfsdev_flexaddr;
353 	char			*nfsdev_host;
354 	fsid_t			nfsdev_mdsfsid;
355 	uint32_t		nfsdev_nextdir;
356 	vnode_t			nfsdev_dsdir[0];
357 };
358 
359 /*
360  * This structure holds the va_size, va_filerev, va_atime, va_mtime and
361  * va_bytes for the DS file and is stored in the metadata file's extended
362  * attribute pnfsd.dsattr.
363  * opnfsdsattr was missing the va_bytes field and, as such, it was updated.
364  */
365 struct opnfsdsattr {
366 	uint64_t	dsa_filerev;
367 	uint64_t	dsa_size;
368 	struct timespec	dsa_atime;
369 	struct timespec	dsa_mtime;
370 };
371 
372 struct pnfsdsattr {
373 	uint64_t	dsa_filerev;
374 	uint64_t	dsa_size;
375 	struct timespec	dsa_atime;
376 	struct timespec	dsa_mtime;
377 	uint64_t	dsa_bytes;
378 };
379 
380 /*
381  * This structure is a list element for a list the pNFS server uses to
382  * mark that the recovery of a mirror file is in progress.
383  */
384 struct nfsdontlist {
385 	LIST_ENTRY(nfsdontlist)	nfsmr_list;
386 	uint32_t		nfsmr_flags;
387 	fhandle_t		nfsmr_fh;
388 };
389 
390 /* nfsmr_flags bits. */
391 #define	NFSMR_DONTLAYOUT	0x00000001
392 
393 #endif	/* defined(_KERNEL) || defined(KERNEL) */
394 
395 /*
396  * This structure holds the information about the DS file and is stored
397  * in the metadata file's extended attribute called pnfsd.dsfile.
398  */
399 #define	PNFS_FILENAME_LEN	(2 * sizeof(fhandle_t))
400 struct pnfsdsfile {
401 	fhandle_t	dsf_fh;
402 	uint32_t	dsf_dir;
403 	union {
404 		struct sockaddr_in	sin;
405 		struct sockaddr_in6	sin6;
406 	} dsf_nam;
407 	char		dsf_filename[PNFS_FILENAME_LEN + 1];
408 };
409 #define	dsf_sin		dsf_nam.sin
410 #define	dsf_sin6	dsf_nam.sin6
411 
412 #endif	/* _NFS_NFSRVSTATE_H_ */
413