1191783Srmacklem/*-
2191783Srmacklem * Copyright (c) 2009 Rick Macklem, University of Guelph
3191783Srmacklem * All rights reserved.
4191783Srmacklem *
5191783Srmacklem * Redistribution and use in source and binary forms, with or without
6191783Srmacklem * modification, are permitted provided that the following conditions
7191783Srmacklem * are met:
8191783Srmacklem * 1. Redistributions of source code must retain the above copyright
9191783Srmacklem *    notice, this list of conditions and the following disclaimer.
10191783Srmacklem * 2. Redistributions in binary form must reproduce the above copyright
11191783Srmacklem *    notice, this list of conditions and the following disclaimer in the
12191783Srmacklem *    documentation and/or other materials provided with the distribution.
13191783Srmacklem *
14191783Srmacklem * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
15191783Srmacklem * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
16191783Srmacklem * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
17191783Srmacklem * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
18191783Srmacklem * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
19191783Srmacklem * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
20191783Srmacklem * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
21191783Srmacklem * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
22191783Srmacklem * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
23191783Srmacklem * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
24191783Srmacklem * SUCH DAMAGE.
25191783Srmacklem *
26191783Srmacklem * $FreeBSD$
27191783Srmacklem */
28191783Srmacklem
29191783Srmacklem#ifndef _NFS_NFSRVSTATE_H_
30191783Srmacklem#define	_NFS_NFSRVSTATE_H_
31191783Srmacklem
32191783Srmacklem/*
33191783Srmacklem * Definitions for NFS V4 server state handling.
34191783Srmacklem */
35191783Srmacklem
36191783Srmacklem/*
37191783Srmacklem * List heads for nfsclient, nfsstate and nfslockfile.
38191783Srmacklem * (Some systems seem to like to dynamically size these things, but I
39191783Srmacklem *  don't see any point in doing so for these ones.)
40191783Srmacklem */
41191783SrmacklemLIST_HEAD(nfsclienthashhead, nfsclient);
42191783SrmacklemLIST_HEAD(nfsstatehead, nfsstate);
43191783SrmacklemLIST_HEAD(nfslockhead, nfslock);
44191783SrmacklemLIST_HEAD(nfslockhashhead, nfslockfile);
45269398SrmacklemLIST_HEAD(nfssessionhead, nfsdsession);
46269398SrmacklemLIST_HEAD(nfssessionhashhead, nfsdsession);
47191783Srmacklem
48191783Srmacklem/*
49191783Srmacklem * List head for nfsusrgrp.
50191783Srmacklem */
51292223SrmacklemTAILQ_HEAD(nfsuserhashhead, nfsusrgrp);
52191783Srmacklem
53191783Srmacklem#define	NFSCLIENTHASH(id)						\
54284216Srmacklem	(&nfsclienthash[(id).lval[1] % nfsrv_clienthashsize])
55191783Srmacklem#define	NFSSTATEHASH(clp, id)						\
56284216Srmacklem	(&((clp)->lc_stateid[(id).other[2] % nfsrv_statehashsize]))
57191783Srmacklem#define	NFSUSERHASH(id)							\
58292223Srmacklem	(&nfsuserhash[(id) % nfsrv_lughashsize])
59191783Srmacklem#define	NFSUSERNAMEHASH(p, l)						\
60191783Srmacklem	(&nfsusernamehash[((l)>=4?(*(p)+*((p)+1)+*((p)+2)+*((p)+3)):*(p)) \
61292223Srmacklem		% nfsrv_lughashsize])
62191783Srmacklem#define	NFSGROUPHASH(id)						\
63292223Srmacklem	(&nfsgrouphash[(id) % nfsrv_lughashsize])
64191783Srmacklem#define	NFSGROUPNAMEHASH(p, l)						\
65191783Srmacklem	(&nfsgroupnamehash[((l)>=4?(*(p)+*((p)+1)+*((p)+2)+*((p)+3)):*(p)) \
66292223Srmacklem		% nfsrv_lughashsize])
67191783Srmacklem
68269398Srmacklemstruct nfssessionhash {
69269398Srmacklem	struct mtx			mtx;
70269398Srmacklem	struct nfssessionhashhead	list;
71269398Srmacklem};
72269398Srmacklem#define	NFSSESSIONHASH(f) 						\
73284216Srmacklem	(&nfssessionhash[nfsrv_hashsessionid(f) % nfsrv_sessionhashsize])
74269398Srmacklem
75191783Srmacklem/*
76191783Srmacklem * Client server structure for V4. It is doubly linked into two lists.
77191783Srmacklem * The first is a hash table based on the clientid and the second is a
78191783Srmacklem * list of all clients maintained in LRU order.
79191783Srmacklem * The actual size malloc'd is large enough to accomodate the id string.
80191783Srmacklem */
81191783Srmacklemstruct nfsclient {
82191783Srmacklem	LIST_ENTRY(nfsclient) lc_hash;		/* Clientid hash list */
83284216Srmacklem	struct nfsstatehead *lc_stateid;	/* Stateid hash */
84191783Srmacklem	struct nfsstatehead lc_open;		/* Open owner list */
85191783Srmacklem	struct nfsstatehead lc_deleg;		/* Delegations */
86191783Srmacklem	struct nfsstatehead lc_olddeleg;	/* and old delegations */
87269398Srmacklem	struct nfssessionhead lc_session;	/* List of NFSv4.1 sessions */
88191783Srmacklem	time_t		lc_expiry;		/* Expiry time (sec) */
89191783Srmacklem	time_t		lc_delegtime;		/* Old deleg expiry (sec) */
90191783Srmacklem	nfsquad_t	lc_clientid;		/* 64 bit clientid */
91191783Srmacklem	nfsquad_t	lc_confirm;		/* 64 bit confirm value */
92191783Srmacklem	u_int32_t	lc_program;		/* RPC Program # */
93191783Srmacklem	u_int32_t	lc_callback;		/* Callback id */
94191783Srmacklem	u_int32_t	lc_stateindex;		/* Current state index# */
95191783Srmacklem	u_int32_t	lc_statemaxindex;	/* Max state index# */
96191783Srmacklem	u_int32_t	lc_cbref;		/* Cnt of callbacks */
97191783Srmacklem	uid_t		lc_uid;			/* User credential */
98191783Srmacklem	gid_t		lc_gid;
99284216Srmacklem	u_int16_t	lc_idlen;		/* Client ID and len */
100284216Srmacklem	u_int16_t	lc_namelen;		/* plus GSS principal and len */
101191783Srmacklem	u_char		*lc_name;
102191783Srmacklem	struct nfssockreq lc_req;		/* Callback info */
103191783Srmacklem	u_int32_t	lc_flags;		/* LCL_ flag bits */
104191783Srmacklem	u_char		lc_verf[NFSX_VERF];	 /* client verifier */
105191783Srmacklem	u_char		lc_id[1];		/* Malloc'd correct size */
106191783Srmacklem};
107191783Srmacklem
108191783Srmacklem#define	CLOPS_CONFIRM		0x0001
109191783Srmacklem#define	CLOPS_RENEW		0x0002
110191783Srmacklem#define	CLOPS_RENEWOP		0x0004
111191783Srmacklem
112191783Srmacklem/*
113269398Srmacklem * Structure for an NFSv4.1 session.
114269398Srmacklem * Locking rules for this structure.
115269398Srmacklem * To add/delete one of these structures from the lists, you must lock
116299222Srmacklem * both: NFSLOCKSTATE() and NFSLOCKSESSION(session hashhead) in that order.
117269398Srmacklem * To traverse the lists looking for one of these, you must hold one
118269398Srmacklem * of these two locks.
119269398Srmacklem * The exception is if the thread holds the exclusive root sleep lock.
120269398Srmacklem * In this case, all other nfsd threads are blocked, so locking the
121269398Srmacklem * mutexes isn't required.
122269398Srmacklem * When manipulating sess_refcnt, NFSLOCKSTATE() must be locked.
123269398Srmacklem * When manipulating the fields withinsess_cbsess except nfsess_xprt,
124269398Srmacklem * sess_cbsess.nfsess_mtx must be locked.
125269398Srmacklem * When manipulating sess_slots and sess_cbsess.nfsess_xprt,
126269398Srmacklem * NFSLOCKSESSION(session hashhead) must be locked.
127269398Srmacklem */
128269398Srmacklemstruct nfsdsession {
129269398Srmacklem	uint64_t		sess_refcnt;	/* Reference count. */
130269398Srmacklem	LIST_ENTRY(nfsdsession)	sess_hash;	/* Hash list of sessions. */
131269398Srmacklem	LIST_ENTRY(nfsdsession)	sess_list;	/* List of client sessions. */
132269398Srmacklem	struct nfsslot		sess_slots[NFSV4_SLOTS];
133269398Srmacklem	struct nfsclient	*sess_clp;	/* Associated clientid. */
134269398Srmacklem	uint32_t		sess_crflags;
135269398Srmacklem	uint32_t		sess_cbprogram;
136269398Srmacklem	uint32_t		sess_maxreq;
137269398Srmacklem	uint32_t		sess_maxresp;
138269398Srmacklem	uint32_t		sess_maxrespcached;
139269398Srmacklem	uint32_t		sess_maxops;
140269398Srmacklem	uint32_t		sess_maxslots;
141269398Srmacklem	uint32_t		sess_cbmaxreq;
142269398Srmacklem	uint32_t		sess_cbmaxresp;
143269398Srmacklem	uint32_t		sess_cbmaxrespcached;
144269398Srmacklem	uint32_t		sess_cbmaxops;
145269398Srmacklem	uint8_t			sess_sessionid[NFSX_V4SESSIONID];
146269398Srmacklem	struct nfsclsession	sess_cbsess;	/* Callback session. */
147269398Srmacklem};
148269398Srmacklem
149269398Srmacklem/*
150191783Srmacklem * Nfs state structure. I couldn't resist overloading this one, since
151191783Srmacklem * it makes cleanup, etc. simpler. These structures are used in four ways:
152191783Srmacklem * - open_owner structures chained off of nfsclient
153191783Srmacklem * - open file structures chained off an open_owner structure
154191783Srmacklem * - lock_owner structures chained off an open file structure
155191783Srmacklem * - delegated file structures chained off of nfsclient and nfslockfile
156191783Srmacklem * - the ls_list field is used for the chain it is in
157191783Srmacklem * - the ls_head structure is used to chain off the sibling structure
158191783Srmacklem *   (it is a union between an nfsstate and nfslock structure head)
159191783Srmacklem *    If it is a lockowner stateid, nfslock structures hang off it.
160191783Srmacklem * For the open file and lockowner cases, it is in the hash table in
161191783Srmacklem * nfsclient for stateid.
162191783Srmacklem */
163191783Srmacklemstruct nfsstate {
164191783Srmacklem	LIST_ENTRY(nfsstate)	ls_hash;	/* Hash list entry */
165191783Srmacklem	LIST_ENTRY(nfsstate)	ls_list;	/* List of opens/delegs */
166191783Srmacklem	LIST_ENTRY(nfsstate)	ls_file;	/* Opens/Delegs for a file */
167191783Srmacklem	union {
168191783Srmacklem		struct nfsstatehead	open; /* Opens list */
169191783Srmacklem		struct nfslockhead	lock; /* Locks list */
170191783Srmacklem	} ls_head;
171191783Srmacklem	nfsv4stateid_t		ls_stateid;	/* The state id */
172191783Srmacklem	u_int32_t		ls_seq;		/* seq id */
173191783Srmacklem	uid_t			ls_uid;		/* uid of locker */
174191783Srmacklem	u_int32_t		ls_flags;	/* Type of lock, etc. */
175191783Srmacklem	union {
176191783Srmacklem		struct nfsstate	*openowner;	/* Open only */
177191783Srmacklem		u_int32_t	opentolockseq;	/* Lock call only */
178191783Srmacklem		u_int32_t	noopens;	/* Openowner only */
179191783Srmacklem		struct {
180191783Srmacklem			u_quad_t	filerev; /* Delegations only */
181191783Srmacklem			time_t		expiry;
182191783Srmacklem			time_t		limit;
183191783Srmacklem			u_int64_t	compref;
184191783Srmacklem		} deleg;
185191783Srmacklem	} ls_un;
186191783Srmacklem	struct nfslockfile	*ls_lfp;	/* Back pointer */
187191783Srmacklem	struct nfsrvcache	*ls_op;		/* Op cache reference */
188191783Srmacklem	struct nfsclient	*ls_clp;	/* Back pointer */
189191783Srmacklem	u_short			ls_ownerlen;	/* Length of ls_owner */
190191783Srmacklem	u_char			ls_owner[1];	/* malloc'd the correct size */
191191783Srmacklem};
192191783Srmacklem#define	ls_lock			ls_head.lock
193191783Srmacklem#define	ls_open			ls_head.open
194191783Srmacklem#define	ls_opentolockseq	ls_un.opentolockseq
195191783Srmacklem#define	ls_openowner		ls_un.openowner
196191783Srmacklem#define	ls_openstp		ls_un.openowner
197191783Srmacklem#define	ls_noopens		ls_un.noopens
198191783Srmacklem#define	ls_filerev		ls_un.deleg.filerev
199191783Srmacklem#define	ls_delegtime		ls_un.deleg.expiry
200191783Srmacklem#define	ls_delegtimelimit	ls_un.deleg.limit
201191783Srmacklem#define	ls_compref		ls_un.deleg.compref
202191783Srmacklem
203191783Srmacklem/*
204191783Srmacklem * Nfs lock structure.
205191783Srmacklem * This structure is chained off of the nfsstate (the lockowner) and
206191783Srmacklem * nfslockfile (the file) structures, for the file and owner it
207191783Srmacklem * refers to. It holds flags and a byte range.
208191783Srmacklem * It also has back pointers to the associated lock_owner and lockfile.
209191783Srmacklem */
210191783Srmacklemstruct nfslock {
211191783Srmacklem	LIST_ENTRY(nfslock)	lo_lckowner;
212191783Srmacklem	LIST_ENTRY(nfslock)	lo_lckfile;
213191783Srmacklem	struct nfsstate		*lo_stp;
214191783Srmacklem	struct nfslockfile	*lo_lfp;
215191783Srmacklem	u_int64_t		lo_first;
216191783Srmacklem	u_int64_t		lo_end;
217191783Srmacklem	u_int32_t		lo_flags;
218191783Srmacklem};
219191783Srmacklem
220191783Srmacklem/*
221191783Srmacklem * Structure used to return a conflicting lock. (Must be large
222191783Srmacklem * enough for the largest lock owner we can have.)
223191783Srmacklem */
224191783Srmacklemstruct nfslockconflict {
225191783Srmacklem	nfsquad_t		cl_clientid;
226191783Srmacklem	u_int64_t		cl_first;
227191783Srmacklem	u_int64_t		cl_end;
228191783Srmacklem	u_int32_t		cl_flags;
229191783Srmacklem	u_short			cl_ownerlen;
230191783Srmacklem	u_char			cl_owner[NFSV4_OPAQUELIMIT];
231191783Srmacklem};
232191783Srmacklem
233191783Srmacklem/*
234205941Srmacklem * This structure is used to keep track of local locks that might need
235205941Srmacklem * to be rolled back.
236205941Srmacklem */
237205941Srmacklemstruct nfsrollback {
238205941Srmacklem	LIST_ENTRY(nfsrollback)	rlck_list;
239205941Srmacklem	uint64_t		rlck_first;
240205941Srmacklem	uint64_t		rlck_end;
241205941Srmacklem	int			rlck_type;
242205941Srmacklem};
243205941Srmacklem
244205941Srmacklem/*
245191783Srmacklem * This structure refers to a file for which lock(s) and/or open(s) exist.
246191783Srmacklem * Searched via hash table on file handle or found via the back pointer from an
247191783Srmacklem * open or lock owner.
248191783Srmacklem */
249191783Srmacklemstruct nfslockfile {
250191783Srmacklem	LIST_HEAD(, nfsstate)	lf_open;	/* Open list */
251191783Srmacklem	LIST_HEAD(, nfsstate)	lf_deleg;	/* Delegation list */
252191783Srmacklem	LIST_HEAD(, nfslock)	lf_lock;	/* Lock list */
253205941Srmacklem	LIST_HEAD(, nfslock)	lf_locallock;	/* Local lock list */
254205941Srmacklem	LIST_HEAD(, nfsrollback) lf_rollback;	/* Local lock rollback list */
255191783Srmacklem	LIST_ENTRY(nfslockfile)	lf_hash;	/* Hash list entry */
256191783Srmacklem	fhandle_t		lf_fh;		/* The file handle */
257205941Srmacklem	struct nfsv4lock	lf_locallock_lck; /* serialize local locking */
258205941Srmacklem	int			lf_usecount;	/* Ref count for locking */
259191783Srmacklem};
260191783Srmacklem
261191783Srmacklem/*
262191783Srmacklem * This structure is malloc'd an chained off hash lists for user/group
263191783Srmacklem * names.
264191783Srmacklem */
265191783Srmacklemstruct nfsusrgrp {
266292223Srmacklem	TAILQ_ENTRY(nfsusrgrp)	lug_numhash;	/* Hash by id# */
267292223Srmacklem	TAILQ_ENTRY(nfsusrgrp)	lug_namehash;	/* and by name */
268191783Srmacklem	time_t			lug_expiry;	/* Expiry time in sec */
269191783Srmacklem	union {
270191783Srmacklem		uid_t		un_uid;		/* id# */
271191783Srmacklem		gid_t		un_gid;
272191783Srmacklem	} lug_un;
273292223Srmacklem	struct ucred		*lug_cred;	/* Cred. with groups list */
274191783Srmacklem	int			lug_namelen;	/* Name length */
275191783Srmacklem	u_char			lug_name[1];	/* malloc'd correct length */
276191783Srmacklem};
277191783Srmacklem#define	lug_uid		lug_un.un_uid
278191783Srmacklem#define	lug_gid		lug_un.un_gid
279191783Srmacklem
280191783Srmacklem/*
281191783Srmacklem * These structures are used for the stable storage restart stuff.
282191783Srmacklem */
283191783Srmacklem/*
284191783Srmacklem * Record at beginning of file.
285191783Srmacklem */
286191783Srmacklemstruct nfsf_rec {
287191783Srmacklem	u_int32_t	lease;			/* Lease duration */
288191783Srmacklem	u_int32_t	numboots;		/* Number of boottimes */
289191783Srmacklem};
290191783Srmacklem
291191783Srmacklem#if defined(_KERNEL) || defined(KERNEL)
292191783Srmacklemvoid nfsrv_cleanclient(struct nfsclient *, NFSPROC_T *);
293191783Srmacklemvoid nfsrv_freedeleglist(struct nfsstatehead *);
294191783Srmacklem#endif
295191783Srmacklem
296191783Srmacklem#endif	/* _NFS_NFSRVSTATE_H_ */
297