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