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