xref: /freebsd/sys/fs/nfs/nfsrvstate.h (revision fcb560670601b2a4d87bb31d7531c8dcc37ee71b)
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 LIST_HEAD(nfsuserhashhead, nfsusrgrp);
52 TAILQ_HEAD(nfsuserlruhead, nfsusrgrp);
53 
54 #define	NFSCLIENTHASH(id)						\
55 	(&nfsclienthash[(id).lval[1] % NFSCLIENTHASHSIZE])
56 #define	NFSSTATEHASH(clp, id)						\
57 	(&((clp)->lc_stateid[(id).other[2] % NFSSTATEHASHSIZE]))
58 #define	NFSUSERHASH(id)							\
59 	(&nfsuserhash[(id) % NFSUSERHASHSIZE])
60 #define	NFSUSERNAMEHASH(p, l)						\
61 	(&nfsusernamehash[((l)>=4?(*(p)+*((p)+1)+*((p)+2)+*((p)+3)):*(p)) \
62 		% NFSUSERHASHSIZE])
63 #define	NFSGROUPHASH(id)						\
64 	(&nfsgrouphash[(id) % NFSGROUPHASHSIZE])
65 #define	NFSGROUPNAMEHASH(p, l)						\
66 	(&nfsgroupnamehash[((l)>=4?(*(p)+*((p)+1)+*((p)+2)+*((p)+3)):*(p)) \
67 		% NFSGROUPHASHSIZE])
68 
69 struct nfssessionhash {
70 	struct mtx			mtx;
71 	struct nfssessionhashhead	list;
72 };
73 #define	NFSSESSIONHASH(f) 						\
74 	(&nfssessionhash[nfsrv_hashsessionid(f) % NFSSESSIONHASHSIZE])
75 
76 /*
77  * Client server structure for V4. It is doubly linked into two lists.
78  * The first is a hash table based on the clientid and the second is a
79  * list of all clients maintained in LRU order.
80  * The actual size malloc'd is large enough to accomodate the id string.
81  */
82 struct nfsclient {
83 	LIST_ENTRY(nfsclient) lc_hash;		/* Clientid hash list */
84 	struct nfsstatehead lc_stateid[NFSSTATEHASHSIZE]; /* stateid hash */
85 	struct nfsstatehead lc_open;		/* Open owner list */
86 	struct nfsstatehead lc_deleg;		/* Delegations */
87 	struct nfsstatehead lc_olddeleg;	/* and old delegations */
88 	struct nfssessionhead lc_session;	/* List of NFSv4.1 sessions */
89 	time_t		lc_expiry;		/* Expiry time (sec) */
90 	time_t		lc_delegtime;		/* Old deleg expiry (sec) */
91 	nfsquad_t	lc_clientid;		/* 64 bit clientid */
92 	nfsquad_t	lc_confirm;		/* 64 bit confirm value */
93 	u_int32_t	lc_program;		/* RPC Program # */
94 	u_int32_t	lc_callback;		/* Callback id */
95 	u_int32_t	lc_stateindex;		/* Current state index# */
96 	u_int32_t	lc_statemaxindex;	/* Max state index# */
97 	u_int32_t	lc_cbref;		/* Cnt of callbacks */
98 	uid_t		lc_uid;			/* User credential */
99 	gid_t		lc_gid;
100 	u_int16_t	lc_namelen;
101 	u_char		*lc_name;
102 	struct nfssockreq lc_req;		/* Callback info */
103 	u_short		lc_idlen;		/* Length of id string */
104 	u_int32_t	lc_flags;		/* LCL_ flag bits */
105 	u_char		lc_verf[NFSX_VERF];	 /* client verifier */
106 	u_char		lc_id[1];		/* Malloc'd correct size */
107 };
108 
109 #define	CLOPS_CONFIRM		0x0001
110 #define	CLOPS_RENEW		0x0002
111 #define	CLOPS_RENEWOP		0x0004
112 
113 /*
114  * Structure for an NFSv4.1 session.
115  * Locking rules for this structure.
116  * To add/delete one of these structures from the lists, you must lock
117  * both: NFSLOCKSESSION(session hashhead) and NFSLOCKSTATE() in that order.
118  * To traverse the lists looking for one of these, you must hold one
119  * of these two locks.
120  * The exception is if the thread holds the exclusive root sleep lock.
121  * In this case, all other nfsd threads are blocked, so locking the
122  * mutexes isn't required.
123  * When manipulating sess_refcnt, NFSLOCKSTATE() must be locked.
124  * When manipulating the fields withinsess_cbsess except nfsess_xprt,
125  * sess_cbsess.nfsess_mtx must be locked.
126  * When manipulating sess_slots and sess_cbsess.nfsess_xprt,
127  * NFSLOCKSESSION(session hashhead) must be locked.
128  */
129 struct nfsdsession {
130 	uint64_t		sess_refcnt;	/* Reference count. */
131 	LIST_ENTRY(nfsdsession)	sess_hash;	/* Hash list of sessions. */
132 	LIST_ENTRY(nfsdsession)	sess_list;	/* List of client sessions. */
133 	struct nfsslot		sess_slots[NFSV4_SLOTS];
134 	struct nfsclient	*sess_clp;	/* Associated clientid. */
135 	uint32_t		sess_crflags;
136 	uint32_t		sess_cbprogram;
137 	uint32_t		sess_maxreq;
138 	uint32_t		sess_maxresp;
139 	uint32_t		sess_maxrespcached;
140 	uint32_t		sess_maxops;
141 	uint32_t		sess_maxslots;
142 	uint32_t		sess_cbmaxreq;
143 	uint32_t		sess_cbmaxresp;
144 	uint32_t		sess_cbmaxrespcached;
145 	uint32_t		sess_cbmaxops;
146 	uint8_t			sess_sessionid[NFSX_V4SESSIONID];
147 	struct nfsclsession	sess_cbsess;	/* Callback session. */
148 };
149 
150 /*
151  * Nfs state structure. I couldn't resist overloading this one, since
152  * it makes cleanup, etc. simpler. These structures are used in four ways:
153  * - open_owner structures chained off of nfsclient
154  * - open file structures chained off an open_owner structure
155  * - lock_owner structures chained off an open file structure
156  * - delegated file structures chained off of nfsclient and nfslockfile
157  * - the ls_list field is used for the chain it is in
158  * - the ls_head structure is used to chain off the sibling structure
159  *   (it is a union between an nfsstate and nfslock structure head)
160  *    If it is a lockowner stateid, nfslock structures hang off it.
161  * For the open file and lockowner cases, it is in the hash table in
162  * nfsclient for stateid.
163  */
164 struct nfsstate {
165 	LIST_ENTRY(nfsstate)	ls_hash;	/* Hash list entry */
166 	LIST_ENTRY(nfsstate)	ls_list;	/* List of opens/delegs */
167 	LIST_ENTRY(nfsstate)	ls_file;	/* Opens/Delegs for a file */
168 	union {
169 		struct nfsstatehead	open; /* Opens list */
170 		struct nfslockhead	lock; /* Locks list */
171 	} ls_head;
172 	nfsv4stateid_t		ls_stateid;	/* The state id */
173 	u_int32_t		ls_seq;		/* seq id */
174 	uid_t			ls_uid;		/* uid of locker */
175 	u_int32_t		ls_flags;	/* Type of lock, etc. */
176 	union {
177 		struct nfsstate	*openowner;	/* Open only */
178 		u_int32_t	opentolockseq;	/* Lock call only */
179 		u_int32_t	noopens;	/* Openowner only */
180 		struct {
181 			u_quad_t	filerev; /* Delegations only */
182 			time_t		expiry;
183 			time_t		limit;
184 			u_int64_t	compref;
185 		} deleg;
186 	} ls_un;
187 	struct nfslockfile	*ls_lfp;	/* Back pointer */
188 	struct nfsrvcache	*ls_op;		/* Op cache reference */
189 	struct nfsclient	*ls_clp;	/* Back pointer */
190 	u_short			ls_ownerlen;	/* Length of ls_owner */
191 	u_char			ls_owner[1];	/* malloc'd the correct size */
192 };
193 #define	ls_lock			ls_head.lock
194 #define	ls_open			ls_head.open
195 #define	ls_opentolockseq	ls_un.opentolockseq
196 #define	ls_openowner		ls_un.openowner
197 #define	ls_openstp		ls_un.openowner
198 #define	ls_noopens		ls_un.noopens
199 #define	ls_filerev		ls_un.deleg.filerev
200 #define	ls_delegtime		ls_un.deleg.expiry
201 #define	ls_delegtimelimit	ls_un.deleg.limit
202 #define	ls_compref		ls_un.deleg.compref
203 
204 /*
205  * Nfs lock structure.
206  * This structure is chained off of the nfsstate (the lockowner) and
207  * nfslockfile (the file) structures, for the file and owner it
208  * refers to. It holds flags and a byte range.
209  * It also has back pointers to the associated lock_owner and lockfile.
210  */
211 struct nfslock {
212 	LIST_ENTRY(nfslock)	lo_lckowner;
213 	LIST_ENTRY(nfslock)	lo_lckfile;
214 	struct nfsstate		*lo_stp;
215 	struct nfslockfile	*lo_lfp;
216 	u_int64_t		lo_first;
217 	u_int64_t		lo_end;
218 	u_int32_t		lo_flags;
219 };
220 
221 /*
222  * Structure used to return a conflicting lock. (Must be large
223  * enough for the largest lock owner we can have.)
224  */
225 struct nfslockconflict {
226 	nfsquad_t		cl_clientid;
227 	u_int64_t		cl_first;
228 	u_int64_t		cl_end;
229 	u_int32_t		cl_flags;
230 	u_short			cl_ownerlen;
231 	u_char			cl_owner[NFSV4_OPAQUELIMIT];
232 };
233 
234 /*
235  * This structure is used to keep track of local locks that might need
236  * to be rolled back.
237  */
238 struct nfsrollback {
239 	LIST_ENTRY(nfsrollback)	rlck_list;
240 	uint64_t		rlck_first;
241 	uint64_t		rlck_end;
242 	int			rlck_type;
243 };
244 
245 /*
246  * This structure refers to a file for which lock(s) and/or open(s) exist.
247  * Searched via hash table on file handle or found via the back pointer from an
248  * open or lock owner.
249  */
250 struct nfslockfile {
251 	LIST_HEAD(, nfsstate)	lf_open;	/* Open list */
252 	LIST_HEAD(, nfsstate)	lf_deleg;	/* Delegation list */
253 	LIST_HEAD(, nfslock)	lf_lock;	/* Lock list */
254 	LIST_HEAD(, nfslock)	lf_locallock;	/* Local lock list */
255 	LIST_HEAD(, nfsrollback) lf_rollback;	/* Local lock rollback list */
256 	LIST_ENTRY(nfslockfile)	lf_hash;	/* Hash list entry */
257 	fhandle_t		lf_fh;		/* The file handle */
258 	struct nfsv4lock	lf_locallock_lck; /* serialize local locking */
259 	int			lf_usecount;	/* Ref count for locking */
260 };
261 
262 /*
263  * This structure is malloc'd an chained off hash lists for user/group
264  * names.
265  */
266 struct nfsusrgrp {
267 	TAILQ_ENTRY(nfsusrgrp)	lug_lru;	/* LRU list */
268 	LIST_ENTRY(nfsusrgrp)	lug_numhash;	/* Hash by id# */
269 	LIST_ENTRY(nfsusrgrp)	lug_namehash;	/* and by name */
270 	time_t			lug_expiry;	/* Expiry time in sec */
271 	union {
272 		uid_t		un_uid;		/* id# */
273 		gid_t		un_gid;
274 	} lug_un;
275 	int			lug_namelen;	/* Name length */
276 	u_char			lug_name[1];	/* malloc'd correct length */
277 };
278 #define	lug_uid		lug_un.un_uid
279 #define	lug_gid		lug_un.un_gid
280 
281 /*
282  * These structures are used for the stable storage restart stuff.
283  */
284 /*
285  * Record at beginning of file.
286  */
287 struct nfsf_rec {
288 	u_int32_t	lease;			/* Lease duration */
289 	u_int32_t	numboots;		/* Number of boottimes */
290 };
291 
292 #if defined(_KERNEL) || defined(KERNEL)
293 void nfsrv_cleanclient(struct nfsclient *, NFSPROC_T *);
294 void nfsrv_freedeleglist(struct nfsstatehead *);
295 #endif
296 
297 #endif	/* _NFS_NFSRVSTATE_H_ */
298