xref: /freebsd/sys/fs/nfs/nfsrvstate.h (revision 1f4bcc459a76b7aa664f3fd557684cd0ba6da352)
1 /*-
2  * Copyright (c) 2009 Rick Macklem, University of Guelph
3  * All rights reserved.
4  *
5  * Redistribution and use in source and binary forms, with or without
6  * modification, are permitted provided that the following conditions
7  * are met:
8  * 1. Redistributions of source code must retain the above copyright
9  *    notice, this list of conditions and the following disclaimer.
10  * 2. Redistributions in binary form must reproduce the above copyright
11  *    notice, this list of conditions and the following disclaimer in the
12  *    documentation and/or other materials provided with the distribution.
13  *
14  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
15  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
16  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
17  * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
18  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
19  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
20  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
21  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
22  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
23  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
24  * SUCH DAMAGE.
25  *
26  * $FreeBSD$
27  */
28 
29 #ifndef _NFS_NFSRVSTATE_H_
30 #define	_NFS_NFSRVSTATE_H_
31 
32 /*
33  * Definitions for NFS V4 server state handling.
34  */
35 
36 /*
37  * List heads for nfsclient, nfsstate and nfslockfile.
38  * (Some systems seem to like to dynamically size these things, but I
39  *  don't see any point in doing so for these ones.)
40  */
41 LIST_HEAD(nfsclienthashhead, nfsclient);
42 LIST_HEAD(nfsstatehead, nfsstate);
43 LIST_HEAD(nfslockhead, nfslock);
44 LIST_HEAD(nfslockhashhead, nfslockfile);
45 LIST_HEAD(nfssessionhead, nfsdsession);
46 LIST_HEAD(nfssessionhashhead, nfsdsession);
47 
48 /*
49  * List head for nfsusrgrp.
50  */
51 TAILQ_HEAD(nfsuserhashhead, nfsusrgrp);
52 
53 #define	NFSCLIENTHASH(id)						\
54 	(&nfsclienthash[(id).lval[1] % nfsrv_clienthashsize])
55 #define	NFSSTATEHASH(clp, id)						\
56 	(&((clp)->lc_stateid[(id).other[2] % nfsrv_statehashsize]))
57 #define	NFSUSERHASH(id)							\
58 	(&nfsuserhash[(id) % nfsrv_lughashsize])
59 #define	NFSUSERNAMEHASH(p, l)						\
60 	(&nfsusernamehash[((l)>=4?(*(p)+*((p)+1)+*((p)+2)+*((p)+3)):*(p)) \
61 		% nfsrv_lughashsize])
62 #define	NFSGROUPHASH(id)						\
63 	(&nfsgrouphash[(id) % nfsrv_lughashsize])
64 #define	NFSGROUPNAMEHASH(p, l)						\
65 	(&nfsgroupnamehash[((l)>=4?(*(p)+*((p)+1)+*((p)+2)+*((p)+3)):*(p)) \
66 		% nfsrv_lughashsize])
67 
68 struct nfssessionhash {
69 	struct mtx			mtx;
70 	struct nfssessionhashhead	list;
71 };
72 #define	NFSSESSIONHASH(f) 						\
73 	(&nfssessionhash[nfsrv_hashsessionid(f) % nfsrv_sessionhashsize])
74 
75 /*
76  * Client server structure for V4. It is doubly linked into two lists.
77  * The first is a hash table based on the clientid and the second is a
78  * list of all clients maintained in LRU order.
79  * The actual size malloc'd is large enough to accomodate the id string.
80  */
81 struct nfsclient {
82 	LIST_ENTRY(nfsclient) lc_hash;		/* Clientid hash list */
83 	struct nfsstatehead *lc_stateid;	/* Stateid hash */
84 	struct nfsstatehead lc_open;		/* Open owner list */
85 	struct nfsstatehead lc_deleg;		/* Delegations */
86 	struct nfsstatehead lc_olddeleg;	/* and old delegations */
87 	struct nfssessionhead lc_session;	/* List of NFSv4.1 sessions */
88 	time_t		lc_expiry;		/* Expiry time (sec) */
89 	time_t		lc_delegtime;		/* Old deleg expiry (sec) */
90 	nfsquad_t	lc_clientid;		/* 64 bit clientid */
91 	nfsquad_t	lc_confirm;		/* 64 bit confirm value */
92 	u_int32_t	lc_program;		/* RPC Program # */
93 	u_int32_t	lc_callback;		/* Callback id */
94 	u_int32_t	lc_stateindex;		/* Current state index# */
95 	u_int32_t	lc_statemaxindex;	/* Max state index# */
96 	u_int32_t	lc_cbref;		/* Cnt of callbacks */
97 	uid_t		lc_uid;			/* User credential */
98 	gid_t		lc_gid;
99 	u_int16_t	lc_idlen;		/* Client ID and len */
100 	u_int16_t	lc_namelen;		/* plus GSS principal and len */
101 	u_char		*lc_name;
102 	struct nfssockreq lc_req;		/* Callback info */
103 	u_int32_t	lc_flags;		/* LCL_ flag bits */
104 	u_char		lc_verf[NFSX_VERF];	 /* client verifier */
105 	u_char		lc_id[1];		/* Malloc'd correct size */
106 };
107 
108 #define	CLOPS_CONFIRM		0x0001
109 #define	CLOPS_RENEW		0x0002
110 #define	CLOPS_RENEWOP		0x0004
111 
112 /*
113  * Structure for an NFSv4.1 session.
114  * Locking rules for this structure.
115  * To add/delete one of these structures from the lists, you must lock
116  * both: NFSLOCKSESSION(session hashhead) and NFSLOCKSTATE() in that order.
117  * To traverse the lists looking for one of these, you must hold one
118  * of these two locks.
119  * The exception is if the thread holds the exclusive root sleep lock.
120  * In this case, all other nfsd threads are blocked, so locking the
121  * mutexes isn't required.
122  * When manipulating sess_refcnt, NFSLOCKSTATE() must be locked.
123  * When manipulating the fields withinsess_cbsess except nfsess_xprt,
124  * sess_cbsess.nfsess_mtx must be locked.
125  * When manipulating sess_slots and sess_cbsess.nfsess_xprt,
126  * NFSLOCKSESSION(session hashhead) must be locked.
127  */
128 struct nfsdsession {
129 	uint64_t		sess_refcnt;	/* Reference count. */
130 	LIST_ENTRY(nfsdsession)	sess_hash;	/* Hash list of sessions. */
131 	LIST_ENTRY(nfsdsession)	sess_list;	/* List of client sessions. */
132 	struct nfsslot		sess_slots[NFSV4_SLOTS];
133 	struct nfsclient	*sess_clp;	/* Associated clientid. */
134 	uint32_t		sess_crflags;
135 	uint32_t		sess_cbprogram;
136 	uint32_t		sess_maxreq;
137 	uint32_t		sess_maxresp;
138 	uint32_t		sess_maxrespcached;
139 	uint32_t		sess_maxops;
140 	uint32_t		sess_maxslots;
141 	uint32_t		sess_cbmaxreq;
142 	uint32_t		sess_cbmaxresp;
143 	uint32_t		sess_cbmaxrespcached;
144 	uint32_t		sess_cbmaxops;
145 	uint8_t			sess_sessionid[NFSX_V4SESSIONID];
146 	struct nfsclsession	sess_cbsess;	/* Callback session. */
147 };
148 
149 /*
150  * Nfs state structure. I couldn't resist overloading this one, since
151  * it makes cleanup, etc. simpler. These structures are used in four ways:
152  * - open_owner structures chained off of nfsclient
153  * - open file structures chained off an open_owner structure
154  * - lock_owner structures chained off an open file structure
155  * - delegated file structures chained off of nfsclient and nfslockfile
156  * - the ls_list field is used for the chain it is in
157  * - the ls_head structure is used to chain off the sibling structure
158  *   (it is a union between an nfsstate and nfslock structure head)
159  *    If it is a lockowner stateid, nfslock structures hang off it.
160  * For the open file and lockowner cases, it is in the hash table in
161  * nfsclient for stateid.
162  */
163 struct nfsstate {
164 	LIST_ENTRY(nfsstate)	ls_hash;	/* Hash list entry */
165 	LIST_ENTRY(nfsstate)	ls_list;	/* List of opens/delegs */
166 	LIST_ENTRY(nfsstate)	ls_file;	/* Opens/Delegs for a file */
167 	union {
168 		struct nfsstatehead	open; /* Opens list */
169 		struct nfslockhead	lock; /* Locks list */
170 	} ls_head;
171 	nfsv4stateid_t		ls_stateid;	/* The state id */
172 	u_int32_t		ls_seq;		/* seq id */
173 	uid_t			ls_uid;		/* uid of locker */
174 	u_int32_t		ls_flags;	/* Type of lock, etc. */
175 	union {
176 		struct nfsstate	*openowner;	/* Open only */
177 		u_int32_t	opentolockseq;	/* Lock call only */
178 		u_int32_t	noopens;	/* Openowner only */
179 		struct {
180 			u_quad_t	filerev; /* Delegations only */
181 			time_t		expiry;
182 			time_t		limit;
183 			u_int64_t	compref;
184 		} deleg;
185 	} ls_un;
186 	struct nfslockfile	*ls_lfp;	/* Back pointer */
187 	struct nfsrvcache	*ls_op;		/* Op cache reference */
188 	struct nfsclient	*ls_clp;	/* Back pointer */
189 	u_short			ls_ownerlen;	/* Length of ls_owner */
190 	u_char			ls_owner[1];	/* malloc'd the correct size */
191 };
192 #define	ls_lock			ls_head.lock
193 #define	ls_open			ls_head.open
194 #define	ls_opentolockseq	ls_un.opentolockseq
195 #define	ls_openowner		ls_un.openowner
196 #define	ls_openstp		ls_un.openowner
197 #define	ls_noopens		ls_un.noopens
198 #define	ls_filerev		ls_un.deleg.filerev
199 #define	ls_delegtime		ls_un.deleg.expiry
200 #define	ls_delegtimelimit	ls_un.deleg.limit
201 #define	ls_compref		ls_un.deleg.compref
202 
203 /*
204  * Nfs lock structure.
205  * This structure is chained off of the nfsstate (the lockowner) and
206  * nfslockfile (the file) structures, for the file and owner it
207  * refers to. It holds flags and a byte range.
208  * It also has back pointers to the associated lock_owner and lockfile.
209  */
210 struct nfslock {
211 	LIST_ENTRY(nfslock)	lo_lckowner;
212 	LIST_ENTRY(nfslock)	lo_lckfile;
213 	struct nfsstate		*lo_stp;
214 	struct nfslockfile	*lo_lfp;
215 	u_int64_t		lo_first;
216 	u_int64_t		lo_end;
217 	u_int32_t		lo_flags;
218 };
219 
220 /*
221  * Structure used to return a conflicting lock. (Must be large
222  * enough for the largest lock owner we can have.)
223  */
224 struct nfslockconflict {
225 	nfsquad_t		cl_clientid;
226 	u_int64_t		cl_first;
227 	u_int64_t		cl_end;
228 	u_int32_t		cl_flags;
229 	u_short			cl_ownerlen;
230 	u_char			cl_owner[NFSV4_OPAQUELIMIT];
231 };
232 
233 /*
234  * This structure is used to keep track of local locks that might need
235  * to be rolled back.
236  */
237 struct nfsrollback {
238 	LIST_ENTRY(nfsrollback)	rlck_list;
239 	uint64_t		rlck_first;
240 	uint64_t		rlck_end;
241 	int			rlck_type;
242 };
243 
244 /*
245  * This structure refers to a file for which lock(s) and/or open(s) exist.
246  * Searched via hash table on file handle or found via the back pointer from an
247  * open or lock owner.
248  */
249 struct nfslockfile {
250 	LIST_HEAD(, nfsstate)	lf_open;	/* Open list */
251 	LIST_HEAD(, nfsstate)	lf_deleg;	/* Delegation list */
252 	LIST_HEAD(, nfslock)	lf_lock;	/* Lock list */
253 	LIST_HEAD(, nfslock)	lf_locallock;	/* Local lock list */
254 	LIST_HEAD(, nfsrollback) lf_rollback;	/* Local lock rollback list */
255 	LIST_ENTRY(nfslockfile)	lf_hash;	/* Hash list entry */
256 	fhandle_t		lf_fh;		/* The file handle */
257 	struct nfsv4lock	lf_locallock_lck; /* serialize local locking */
258 	int			lf_usecount;	/* Ref count for locking */
259 };
260 
261 /*
262  * This structure is malloc'd an chained off hash lists for user/group
263  * names.
264  */
265 struct nfsusrgrp {
266 	TAILQ_ENTRY(nfsusrgrp)	lug_numhash;	/* Hash by id# */
267 	TAILQ_ENTRY(nfsusrgrp)	lug_namehash;	/* and by name */
268 	time_t			lug_expiry;	/* Expiry time in sec */
269 	union {
270 		uid_t		un_uid;		/* id# */
271 		gid_t		un_gid;
272 	} lug_un;
273 	struct ucred		*lug_cred;	/* Cred. with groups list */
274 	int			lug_namelen;	/* Name length */
275 	u_char			lug_name[1];	/* malloc'd correct length */
276 };
277 #define	lug_uid		lug_un.un_uid
278 #define	lug_gid		lug_un.un_gid
279 
280 /*
281  * These structures are used for the stable storage restart stuff.
282  */
283 /*
284  * Record at beginning of file.
285  */
286 struct nfsf_rec {
287 	u_int32_t	lease;			/* Lease duration */
288 	u_int32_t	numboots;		/* Number of boottimes */
289 };
290 
291 #if defined(_KERNEL) || defined(KERNEL)
292 void nfsrv_cleanclient(struct nfsclient *, NFSPROC_T *);
293 void nfsrv_freedeleglist(struct nfsstatehead *);
294 #endif
295 
296 #endif	/* _NFS_NFSRVSTATE_H_ */
297