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 */
51191783SrmacklemLIST_HEAD(nfsuserhashhead, nfsusrgrp);
52191783SrmacklemTAILQ_HEAD(nfsuserlruhead, nfsusrgrp);
53191783Srmacklem
54191783Srmacklem#define	NFSCLIENTHASH(id)						\
55191783Srmacklem	(&nfsclienthash[(id).lval[1] % NFSCLIENTHASHSIZE])
56191783Srmacklem#define	NFSSTATEHASH(clp, id)						\
57191783Srmacklem	(&((clp)->lc_stateid[(id).other[2] % NFSSTATEHASHSIZE]))
58191783Srmacklem#define	NFSUSERHASH(id)							\
59191783Srmacklem	(&nfsuserhash[(id) % NFSUSERHASHSIZE])
60191783Srmacklem#define	NFSUSERNAMEHASH(p, l)						\
61191783Srmacklem	(&nfsusernamehash[((l)>=4?(*(p)+*((p)+1)+*((p)+2)+*((p)+3)):*(p)) \
62191783Srmacklem		% NFSUSERHASHSIZE])
63191783Srmacklem#define	NFSGROUPHASH(id)						\
64191783Srmacklem	(&nfsgrouphash[(id) % NFSGROUPHASHSIZE])
65191783Srmacklem#define	NFSGROUPNAMEHASH(p, l)						\
66191783Srmacklem	(&nfsgroupnamehash[((l)>=4?(*(p)+*((p)+1)+*((p)+2)+*((p)+3)):*(p)) \
67191783Srmacklem		% NFSGROUPHASHSIZE])
68191783Srmacklem
69269398Srmacklemstruct nfssessionhash {
70269398Srmacklem	struct mtx			mtx;
71269398Srmacklem	struct nfssessionhashhead	list;
72269398Srmacklem};
73269398Srmacklem#define	NFSSESSIONHASH(f) 						\
74269398Srmacklem	(&nfssessionhash[nfsrv_hashsessionid(f) % NFSSESSIONHASHSIZE])
75269398Srmacklem
76191783Srmacklem/*
77191783Srmacklem * Client server structure for V4. It is doubly linked into two lists.
78191783Srmacklem * The first is a hash table based on the clientid and the second is a
79191783Srmacklem * list of all clients maintained in LRU order.
80191783Srmacklem * The actual size malloc'd is large enough to accomodate the id string.
81191783Srmacklem */
82191783Srmacklemstruct nfsclient {
83191783Srmacklem	LIST_ENTRY(nfsclient) lc_hash;		/* Clientid hash list */
84191783Srmacklem	struct nfsstatehead lc_stateid[NFSSTATEHASHSIZE]; /* stateid hash */
85191783Srmacklem	struct nfsstatehead lc_open;		/* Open owner list */
86191783Srmacklem	struct nfsstatehead lc_deleg;		/* Delegations */
87191783Srmacklem	struct nfsstatehead lc_olddeleg;	/* and old delegations */
88269398Srmacklem	struct nfssessionhead lc_session;	/* List of NFSv4.1 sessions */
89191783Srmacklem	time_t		lc_expiry;		/* Expiry time (sec) */
90191783Srmacklem	time_t		lc_delegtime;		/* Old deleg expiry (sec) */
91191783Srmacklem	nfsquad_t	lc_clientid;		/* 64 bit clientid */
92191783Srmacklem	nfsquad_t	lc_confirm;		/* 64 bit confirm value */
93191783Srmacklem	u_int32_t	lc_program;		/* RPC Program # */
94191783Srmacklem	u_int32_t	lc_callback;		/* Callback id */
95191783Srmacklem	u_int32_t	lc_stateindex;		/* Current state index# */
96191783Srmacklem	u_int32_t	lc_statemaxindex;	/* Max state index# */
97191783Srmacklem	u_int32_t	lc_cbref;		/* Cnt of callbacks */
98191783Srmacklem	uid_t		lc_uid;			/* User credential */
99191783Srmacklem	gid_t		lc_gid;
100191783Srmacklem	u_int16_t	lc_namelen;
101191783Srmacklem	u_char		*lc_name;
102191783Srmacklem	struct nfssockreq lc_req;		/* Callback info */
103191783Srmacklem	u_short		lc_idlen;		/* Length of id string */
104191783Srmacklem	u_int32_t	lc_flags;		/* LCL_ flag bits */
105191783Srmacklem	u_char		lc_verf[NFSX_VERF];	 /* client verifier */
106191783Srmacklem	u_char		lc_id[1];		/* Malloc'd correct size */
107191783Srmacklem};
108191783Srmacklem
109191783Srmacklem#define	CLOPS_CONFIRM		0x0001
110191783Srmacklem#define	CLOPS_RENEW		0x0002
111191783Srmacklem#define	CLOPS_RENEWOP		0x0004
112191783Srmacklem
113191783Srmacklem/*
114269398Srmacklem * Structure for an NFSv4.1 session.
115269398Srmacklem * Locking rules for this structure.
116269398Srmacklem * To add/delete one of these structures from the lists, you must lock
117269398Srmacklem * both: NFSLOCKSESSION(session hashhead) and NFSLOCKSTATE() in that order.
118269398Srmacklem * To traverse the lists looking for one of these, you must hold one
119269398Srmacklem * of these two locks.
120269398Srmacklem * The exception is if the thread holds the exclusive root sleep lock.
121269398Srmacklem * In this case, all other nfsd threads are blocked, so locking the
122269398Srmacklem * mutexes isn't required.
123269398Srmacklem * When manipulating sess_refcnt, NFSLOCKSTATE() must be locked.
124269398Srmacklem * When manipulating the fields withinsess_cbsess except nfsess_xprt,
125269398Srmacklem * sess_cbsess.nfsess_mtx must be locked.
126269398Srmacklem * When manipulating sess_slots and sess_cbsess.nfsess_xprt,
127269398Srmacklem * NFSLOCKSESSION(session hashhead) must be locked.
128269398Srmacklem */
129269398Srmacklemstruct nfsdsession {
130269398Srmacklem	uint64_t		sess_refcnt;	/* Reference count. */
131269398Srmacklem	LIST_ENTRY(nfsdsession)	sess_hash;	/* Hash list of sessions. */
132269398Srmacklem	LIST_ENTRY(nfsdsession)	sess_list;	/* List of client sessions. */
133269398Srmacklem	struct nfsslot		sess_slots[NFSV4_SLOTS];
134269398Srmacklem	struct nfsclient	*sess_clp;	/* Associated clientid. */
135269398Srmacklem	uint32_t		sess_crflags;
136269398Srmacklem	uint32_t		sess_cbprogram;
137269398Srmacklem	uint32_t		sess_maxreq;
138269398Srmacklem	uint32_t		sess_maxresp;
139269398Srmacklem	uint32_t		sess_maxrespcached;
140269398Srmacklem	uint32_t		sess_maxops;
141269398Srmacklem	uint32_t		sess_maxslots;
142269398Srmacklem	uint32_t		sess_cbmaxreq;
143269398Srmacklem	uint32_t		sess_cbmaxresp;
144269398Srmacklem	uint32_t		sess_cbmaxrespcached;
145269398Srmacklem	uint32_t		sess_cbmaxops;
146269398Srmacklem	uint8_t			sess_sessionid[NFSX_V4SESSIONID];
147269398Srmacklem	struct nfsclsession	sess_cbsess;	/* Callback session. */
148269398Srmacklem};
149269398Srmacklem
150269398Srmacklem/*
151191783Srmacklem * Nfs state structure. I couldn't resist overloading this one, since
152191783Srmacklem * it makes cleanup, etc. simpler. These structures are used in four ways:
153191783Srmacklem * - open_owner structures chained off of nfsclient
154191783Srmacklem * - open file structures chained off an open_owner structure
155191783Srmacklem * - lock_owner structures chained off an open file structure
156191783Srmacklem * - delegated file structures chained off of nfsclient and nfslockfile
157191783Srmacklem * - the ls_list field is used for the chain it is in
158191783Srmacklem * - the ls_head structure is used to chain off the sibling structure
159191783Srmacklem *   (it is a union between an nfsstate and nfslock structure head)
160191783Srmacklem *    If it is a lockowner stateid, nfslock structures hang off it.
161191783Srmacklem * For the open file and lockowner cases, it is in the hash table in
162191783Srmacklem * nfsclient for stateid.
163191783Srmacklem */
164191783Srmacklemstruct nfsstate {
165191783Srmacklem	LIST_ENTRY(nfsstate)	ls_hash;	/* Hash list entry */
166191783Srmacklem	LIST_ENTRY(nfsstate)	ls_list;	/* List of opens/delegs */
167191783Srmacklem	LIST_ENTRY(nfsstate)	ls_file;	/* Opens/Delegs for a file */
168191783Srmacklem	union {
169191783Srmacklem		struct nfsstatehead	open; /* Opens list */
170191783Srmacklem		struct nfslockhead	lock; /* Locks list */
171191783Srmacklem	} ls_head;
172191783Srmacklem	nfsv4stateid_t		ls_stateid;	/* The state id */
173191783Srmacklem	u_int32_t		ls_seq;		/* seq id */
174191783Srmacklem	uid_t			ls_uid;		/* uid of locker */
175191783Srmacklem	u_int32_t		ls_flags;	/* Type of lock, etc. */
176191783Srmacklem	union {
177191783Srmacklem		struct nfsstate	*openowner;	/* Open only */
178191783Srmacklem		u_int32_t	opentolockseq;	/* Lock call only */
179191783Srmacklem		u_int32_t	noopens;	/* Openowner only */
180191783Srmacklem		struct {
181191783Srmacklem			u_quad_t	filerev; /* Delegations only */
182191783Srmacklem			time_t		expiry;
183191783Srmacklem			time_t		limit;
184191783Srmacklem			u_int64_t	compref;
185191783Srmacklem		} deleg;
186191783Srmacklem	} ls_un;
187191783Srmacklem	struct nfslockfile	*ls_lfp;	/* Back pointer */
188191783Srmacklem	struct nfsrvcache	*ls_op;		/* Op cache reference */
189191783Srmacklem	struct nfsclient	*ls_clp;	/* Back pointer */
190191783Srmacklem	u_short			ls_ownerlen;	/* Length of ls_owner */
191191783Srmacklem	u_char			ls_owner[1];	/* malloc'd the correct size */
192191783Srmacklem};
193191783Srmacklem#define	ls_lock			ls_head.lock
194191783Srmacklem#define	ls_open			ls_head.open
195191783Srmacklem#define	ls_opentolockseq	ls_un.opentolockseq
196191783Srmacklem#define	ls_openowner		ls_un.openowner
197191783Srmacklem#define	ls_openstp		ls_un.openowner
198191783Srmacklem#define	ls_noopens		ls_un.noopens
199191783Srmacklem#define	ls_filerev		ls_un.deleg.filerev
200191783Srmacklem#define	ls_delegtime		ls_un.deleg.expiry
201191783Srmacklem#define	ls_delegtimelimit	ls_un.deleg.limit
202191783Srmacklem#define	ls_compref		ls_un.deleg.compref
203191783Srmacklem
204191783Srmacklem/*
205191783Srmacklem * Nfs lock structure.
206191783Srmacklem * This structure is chained off of the nfsstate (the lockowner) and
207191783Srmacklem * nfslockfile (the file) structures, for the file and owner it
208191783Srmacklem * refers to. It holds flags and a byte range.
209191783Srmacklem * It also has back pointers to the associated lock_owner and lockfile.
210191783Srmacklem */
211191783Srmacklemstruct nfslock {
212191783Srmacklem	LIST_ENTRY(nfslock)	lo_lckowner;
213191783Srmacklem	LIST_ENTRY(nfslock)	lo_lckfile;
214191783Srmacklem	struct nfsstate		*lo_stp;
215191783Srmacklem	struct nfslockfile	*lo_lfp;
216191783Srmacklem	u_int64_t		lo_first;
217191783Srmacklem	u_int64_t		lo_end;
218191783Srmacklem	u_int32_t		lo_flags;
219191783Srmacklem};
220191783Srmacklem
221191783Srmacklem/*
222191783Srmacklem * Structure used to return a conflicting lock. (Must be large
223191783Srmacklem * enough for the largest lock owner we can have.)
224191783Srmacklem */
225191783Srmacklemstruct nfslockconflict {
226191783Srmacklem	nfsquad_t		cl_clientid;
227191783Srmacklem	u_int64_t		cl_first;
228191783Srmacklem	u_int64_t		cl_end;
229191783Srmacklem	u_int32_t		cl_flags;
230191783Srmacklem	u_short			cl_ownerlen;
231191783Srmacklem	u_char			cl_owner[NFSV4_OPAQUELIMIT];
232191783Srmacklem};
233191783Srmacklem
234191783Srmacklem/*
235205941Srmacklem * This structure is used to keep track of local locks that might need
236205941Srmacklem * to be rolled back.
237205941Srmacklem */
238205941Srmacklemstruct nfsrollback {
239205941Srmacklem	LIST_ENTRY(nfsrollback)	rlck_list;
240205941Srmacklem	uint64_t		rlck_first;
241205941Srmacklem	uint64_t		rlck_end;
242205941Srmacklem	int			rlck_type;
243205941Srmacklem};
244205941Srmacklem
245205941Srmacklem/*
246191783Srmacklem * This structure refers to a file for which lock(s) and/or open(s) exist.
247191783Srmacklem * Searched via hash table on file handle or found via the back pointer from an
248191783Srmacklem * open or lock owner.
249191783Srmacklem */
250191783Srmacklemstruct nfslockfile {
251191783Srmacklem	LIST_HEAD(, nfsstate)	lf_open;	/* Open list */
252191783Srmacklem	LIST_HEAD(, nfsstate)	lf_deleg;	/* Delegation list */
253191783Srmacklem	LIST_HEAD(, nfslock)	lf_lock;	/* Lock list */
254205941Srmacklem	LIST_HEAD(, nfslock)	lf_locallock;	/* Local lock list */
255205941Srmacklem	LIST_HEAD(, nfsrollback) lf_rollback;	/* Local lock rollback list */
256191783Srmacklem	LIST_ENTRY(nfslockfile)	lf_hash;	/* Hash list entry */
257191783Srmacklem	fhandle_t		lf_fh;		/* The file handle */
258205941Srmacklem	struct nfsv4lock	lf_locallock_lck; /* serialize local locking */
259205941Srmacklem	int			lf_usecount;	/* Ref count for locking */
260191783Srmacklem};
261191783Srmacklem
262191783Srmacklem/*
263191783Srmacklem * This structure is malloc'd an chained off hash lists for user/group
264191783Srmacklem * names.
265191783Srmacklem */
266191783Srmacklemstruct nfsusrgrp {
267191783Srmacklem	TAILQ_ENTRY(nfsusrgrp)	lug_lru;	/* LRU list */
268191783Srmacklem	LIST_ENTRY(nfsusrgrp)	lug_numhash;	/* Hash by id# */
269191783Srmacklem	LIST_ENTRY(nfsusrgrp)	lug_namehash;	/* and by name */
270191783Srmacklem	time_t			lug_expiry;	/* Expiry time in sec */
271191783Srmacklem	union {
272191783Srmacklem		uid_t		un_uid;		/* id# */
273191783Srmacklem		gid_t		un_gid;
274191783Srmacklem	} lug_un;
275191783Srmacklem	int			lug_namelen;	/* Name length */
276191783Srmacklem	u_char			lug_name[1];	/* malloc'd correct length */
277191783Srmacklem};
278191783Srmacklem#define	lug_uid		lug_un.un_uid
279191783Srmacklem#define	lug_gid		lug_un.un_gid
280191783Srmacklem
281191783Srmacklem/*
282191783Srmacklem * These structures are used for the stable storage restart stuff.
283191783Srmacklem */
284191783Srmacklem/*
285191783Srmacklem * Record at beginning of file.
286191783Srmacklem */
287191783Srmacklemstruct nfsf_rec {
288191783Srmacklem	u_int32_t	lease;			/* Lease duration */
289191783Srmacklem	u_int32_t	numboots;		/* Number of boottimes */
290191783Srmacklem};
291191783Srmacklem
292191783Srmacklem#if defined(_KERNEL) || defined(KERNEL)
293191783Srmacklemvoid nfsrv_cleanclient(struct nfsclient *, NFSPROC_T *);
294191783Srmacklemvoid nfsrv_freedeleglist(struct nfsstatehead *);
295191783Srmacklem#endif
296191783Srmacklem
297191783Srmacklem#endif	/* _NFS_NFSRVSTATE_H_ */
298