vserver 1.9.5.x5
[linux-2.6.git] / include / linux / nfsd / state.h
1 /*
2  *  linux/include/nfsd/state.h
3  *
4  *  Copyright (c) 2001 The Regents of the University of Michigan.
5  *  All rights reserved.
6  *
7  *  Kendrick Smith <kmsmith@umich.edu>
8  *  Andy Adamson <andros@umich.edu>
9  *  
10  *  Redistribution and use in source and binary forms, with or without
11  *  modification, are permitted provided that the following conditions
12  *  are met:
13  *  
14  *  1. Redistributions of source code must retain the above copyright
15  *     notice, this list of conditions and the following disclaimer.
16  *  2. Redistributions in binary form must reproduce the above copyright
17  *     notice, this list of conditions and the following disclaimer in the
18  *     documentation and/or other materials provided with the distribution.
19  *  3. Neither the name of the University nor the names of its
20  *     contributors may be used to endorse or promote products derived
21  *     from this software without specific prior written permission.
22  *
23  *  THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESS OR IMPLIED
24  *  WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
25  *  MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
26  *  DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
27  *  FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
28  *  CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
29  *  SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR
30  *  BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
31  *  LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
32  *  NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
33  *  SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
34  *
35  */
36
37 #ifndef _NFSD4_STATE_H
38 #define _NFSD4_STATE_H
39
40 #include <linux/list.h>
41 #include <linux/kref.h>
42 #include <linux/sunrpc/clnt.h>
43
44 #define NFS4_OPAQUE_LIMIT 1024
45 typedef struct {
46         u32             cl_boot;
47         u32             cl_id;
48 } clientid_t;
49
50 typedef struct {
51         u32             so_boot;
52         u32             so_stateownerid;
53         u32             so_fileid;
54 } stateid_opaque_t;
55
56 typedef struct {
57         u32                     si_generation;
58         stateid_opaque_t        si_opaque;
59 } stateid_t;
60 #define si_boot           si_opaque.so_boot
61 #define si_stateownerid   si_opaque.so_stateownerid
62 #define si_fileid         si_opaque.so_fileid
63
64 extern stateid_t zerostateid;
65 extern stateid_t onestateid;
66
67 #define ZERO_STATEID(stateid)       (!memcmp((stateid), &zerostateid, sizeof(stateid_t)))
68 #define ONE_STATEID(stateid)        (!memcmp((stateid), &onestateid, sizeof(stateid_t)))
69
70 /* Delegation recall states */
71 #define NFS4_NO_RECALL                  0x000
72 #define NFS4_RECALL_IN_PROGRESS         0x001
73 #define NFS4_RECALL_COMPLETE            0x002
74 #define NFS4_REAP_DELEG                 0x004
75
76 struct nfs4_cb_recall {
77         u32                     cbr_ident;
78         int                     cbr_trunc;
79         stateid_t               cbr_stateid;
80         u32                     cbr_fhlen;
81         u32                     cbr_fhval[NFS4_FHSIZE];
82         struct nfs4_delegation  *cbr_dp;
83 };
84
85 struct nfs4_delegation {
86         struct list_head        dl_del_perfile; /* nfs4_file->fi_del_perfile */
87         struct list_head        dl_del_perclnt; /* nfs4_client->cl_del_perclnt*/
88         struct list_head        dl_recall_lru;  /* delegation recalled */
89         atomic_t                dl_recall_cnt;  /* resend cb_recall only once */
90         atomic_t                dl_count;       /* ref count */
91         atomic_t                dl_state;       /* recall state */
92         struct nfs4_client      *dl_client;
93         struct nfs4_file        *dl_file;
94         struct file_lock        *dl_flock;
95         struct nfs4_stateid     *dl_stp;
96         u32                     dl_type;
97         time_t                  dl_time;
98         struct nfs4_cb_recall   dl_recall;
99 };
100
101 #define dl_stateid      dl_recall.cbr_stateid
102 #define dl_fhlen        dl_recall.cbr_fhlen
103 #define dl_fhval        dl_recall.cbr_fhval
104
105 /* client delegation callback info */
106 struct nfs4_callback {
107         /* SETCLIENTID info */
108         u32                     cb_parsed;  /* addr parsed */
109         u32                     cb_addr;
110         unsigned short          cb_port;
111         u32                     cb_prog;
112         u32                     cb_ident;
113         /* RPC client info */
114         atomic_t                cb_set;     /* successful CB_NULL call */
115         struct rpc_program      cb_program;
116         struct rpc_stat         cb_stat;
117         struct rpc_clnt *       cb_client;
118 };
119
120 /*
121  * struct nfs4_client - one per client.  Clientids live here.
122  *      o Each nfs4_client is hashed by clientid.
123  *
124  *      o Each nfs4_clients is also hashed by name 
125  *        (the opaque quantity initially sent by the client to identify itself).
126  *        
127  *      o cl_perclient list is used to ensure no dangling stateowner references
128  *        when we expire the nfs4_client
129  */
130 struct nfs4_client {
131         struct list_head        cl_idhash;      /* hash by cl_clientid.id */
132         struct list_head        cl_strhash;     /* hash by cl_name */
133         struct list_head        cl_perclient;   /* list: stateowners */
134         struct list_head        cl_del_perclnt; /* list: delegations */
135         struct list_head        cl_lru;         /* tail queue */
136         struct xdr_netobj       cl_name;        /* id generated by client */
137         nfs4_verifier           cl_verifier;    /* generated by client */
138         time_t                  cl_time;        /* time of last lease renewal */
139         u32                     cl_addr;        /* client ipaddress */
140         struct svc_cred         cl_cred;        /* setclientid principal */
141         clientid_t              cl_clientid;    /* generated by server */
142         nfs4_verifier           cl_confirm;     /* generated by server */
143         struct nfs4_callback    cl_callback;    /* callback info */
144         time_t                  cl_first_state; /* first state aquisition*/
145         atomic_t                cl_count;       /* ref count */
146 };
147
148 /* struct nfs4_client_reset
149  * one per old client. Populates reset_str_hashtbl. Filled from conf_id_hashtbl
150  * upon lease reset, or from upcall to state_daemon (to read in state
151  * from non-volitile storage) upon reboot.
152  */
153 struct nfs4_client_reclaim {
154         struct list_head        cr_strhash;     /* hash by cr_name */
155         struct xdr_netobj       cr_name;        /* id generated by client */
156         time_t                  cr_first_state; /* first state aquisition */
157         u32                     cr_expired;     /* boolean: lease expired? */
158 };
159
160 static inline void
161 update_stateid(stateid_t *stateid)
162 {
163         stateid->si_generation++;
164 }
165
166 /* A reasonable value for REPLAY_ISIZE was estimated as follows:  
167  * The OPEN response, typically the largest, requires 
168  *   4(status) + 8(stateid) + 20(changeinfo) + 4(rflags) +  8(verifier) + 
169  *   4(deleg. type) + 8(deleg. stateid) + 4(deleg. recall flag) + 
170  *   20(deleg. space limit) + ~32(deleg. ace) = 112 bytes 
171  */
172
173 #define NFSD4_REPLAY_ISIZE       112 
174
175 /*
176  * Replay buffer, where the result of the last seqid-mutating operation 
177  * is cached. 
178  */
179 struct nfs4_replay {
180         u32                     rp_status;
181         unsigned int            rp_buflen;
182         char                    *rp_buf;
183         unsigned                intrp_allocated;
184         int                     rp_openfh_len;
185         char                    rp_openfh[NFS4_FHSIZE];
186         char                    rp_ibuf[NFSD4_REPLAY_ISIZE];
187 };
188
189 /*
190 * nfs4_stateowner can either be an open_owner, or a lock_owner
191 *
192 *    so_idhash:  stateid_hashtbl[] for open owner, lockstateid_hashtbl[]
193 *         for lock_owner
194 *    so_strhash: ownerstr_hashtbl[] for open_owner, lock_ownerstr_hashtbl[]
195 *         for lock_owner
196 *    so_perclient: nfs4_client->cl_perclient entry - used when nfs4_client
197 *         struct is reaped.
198 *    so_perfilestate: heads the list of nfs4_stateid (either open or lock) 
199 *         and is used to ensure no dangling nfs4_stateid references when we 
200 *         release a stateowner.
201 *    so_perlockowner: (open) nfs4_stateid->st_perlockowner entry - used when
202 *         close is called to reap associated byte-range locks
203 *    so_close_lru: (open) stateowner is placed on this list instead of being
204 *         reaped (when so_perfilestate is empty) to hold the last close replay.
205 *         reaped by laundramat thread after lease period.
206 */
207 struct nfs4_stateowner {
208         struct kref             so_ref;
209         struct list_head        so_idhash;   /* hash by so_id */
210         struct list_head        so_strhash;   /* hash by op_name */
211         struct list_head        so_perclient; /* nfs4_client->cl_perclient */
212         struct list_head        so_perfilestate; /* list: nfs4_stateid */
213         struct list_head        so_perlockowner; /* nfs4_stateid->st_perlockowner */
214         struct list_head        so_close_lru; /* tail queue */
215         time_t                  so_time; /* time of placement on so_close_lru */
216         int                     so_is_open_owner; /* 1=openowner,0=lockowner */
217         u32                     so_id;
218         struct nfs4_client *    so_client;
219         u32                     so_seqid;    
220         struct xdr_netobj       so_owner;     /* open owner name */
221         int                     so_confirmed; /* successful OPEN_CONFIRM? */
222         struct nfs4_replay      so_replay;
223 };
224
225 /*
226 *  nfs4_file: a file opened by some number of (open) nfs4_stateowners.
227 *    o fi_perfile list is used to search for conflicting 
228 *      share_acces, share_deny on the file.
229 */
230 struct nfs4_file {
231         struct list_head        fi_hash;    /* hash by "struct inode *" */
232         struct list_head        fi_perfile; /* list: nfs4_stateid */
233         struct list_head        fi_del_perfile; /* list: nfs4_delegation */
234         struct inode            *fi_inode;
235         u32                     fi_id;      /* used with stateowner->so_id 
236                                              * for stateid_hashtbl hash */
237 };
238
239 /*
240 * nfs4_stateid can either be an open stateid or (eventually) a lock stateid
241 *
242 * (open)nfs4_stateid: one per (open)nfs4_stateowner, nfs4_file
243 *
244 *       st_hash: stateid_hashtbl[] entry or lockstateid_hashtbl entry
245 *       st_perfile: file_hashtbl[] entry.
246 *       st_perfile_state: nfs4_stateowner->so_perfilestate
247 *       st_perlockowner: (open stateid) list of lock nfs4_stateowners
248 *       st_access_bmap: used only for open stateid
249 *       st_deny_bmap: used only for open stateid
250 */
251
252 struct nfs4_stateid {
253         struct list_head              st_hash; 
254         struct list_head              st_perfile;
255         struct list_head              st_perfilestate; 
256         struct list_head              st_perlockowner;
257         struct nfs4_stateowner      * st_stateowner;
258         struct nfs4_file            * st_file;
259         stateid_t                     st_stateid;
260         struct file                 * st_vfs_file;
261         int                           st_vfs_set;
262         unsigned long                 st_access_bmap;
263         unsigned long                 st_deny_bmap;
264 };
265
266 /* flags for preprocess_seqid_op() */
267 #define CHECK_FH                0x00000001
268 #define CONFIRM                 0x00000002
269 #define OPEN_STATE              0x00000004
270 #define LOCK_STATE              0x00000008
271 #define RD_STATE                0x00000010
272 #define WR_STATE                0x00000020
273 #define CLOSE_STATE             0x00000040
274 #define DELEG_RET               0x00000080
275
276 #define seqid_mutating_err(err)                       \
277         (((err) != nfserr_stale_clientid) &&    \
278         ((err) != nfserr_bad_seqid) &&          \
279         ((err) != nfserr_stale_stateid) &&      \
280         ((err) != nfserr_bad_stateid))
281
282 extern time_t nfs4_laundromat(void);
283 extern int nfsd4_renew(clientid_t *clid);
284 extern int nfs4_preprocess_stateid_op(struct svc_fh *current_fh, 
285                 stateid_t *stateid, int flags);
286 extern int nfs4_share_conflict(struct svc_fh *current_fh, 
287                 unsigned int deny_type);
288 extern void nfs4_lock_state(void);
289 extern void nfs4_unlock_state(void);
290 extern int nfs4_in_grace(void);
291 extern int nfs4_check_open_reclaim(clientid_t *clid);
292 extern void put_nfs4_client(struct nfs4_client *clp);
293 extern void nfs4_free_stateowner(struct kref *kref);
294 extern void nfsd4_probe_callback(struct nfs4_client *clp);
295 extern int nfsd4_cb_recall(struct nfs4_delegation *dp);
296
297 static inline void
298 nfs4_put_stateowner(struct nfs4_stateowner *so)
299 {
300         kref_put(&so->so_ref, nfs4_free_stateowner);
301 }
302
303 static inline void
304 nfs4_get_stateowner(struct nfs4_stateowner *so)
305 {
306         kref_get(&so->so_ref);
307 }
308
309 #endif   /* NFSD4_STATE_H */