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