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