NFS add session back channel draining
[pandora-kernel.git] / fs / nfs / callback_proc.c
1 /*
2  * linux/fs/nfs/callback_proc.c
3  *
4  * Copyright (C) 2004 Trond Myklebust
5  *
6  * NFSv4 callback procedures
7  */
8 #include <linux/nfs4.h>
9 #include <linux/nfs_fs.h>
10 #include <linux/slab.h>
11 #include "nfs4_fs.h"
12 #include "callback.h"
13 #include "delegation.h"
14 #include "internal.h"
15
16 #ifdef NFS_DEBUG
17 #define NFSDBG_FACILITY NFSDBG_CALLBACK
18 #endif
19
20 __be32 nfs4_callback_getattr(struct cb_getattrargs *args,
21                              struct cb_getattrres *res,
22                              struct cb_process_state *cps)
23 {
24         struct nfs_delegation *delegation;
25         struct nfs_inode *nfsi;
26         struct inode *inode;
27
28         res->status = htonl(NFS4ERR_OP_NOT_IN_SESSION);
29         if (!cps->clp) /* Always set for v4.0. Set in cb_sequence for v4.1 */
30                 goto out;
31
32         res->bitmap[0] = res->bitmap[1] = 0;
33         res->status = htonl(NFS4ERR_BADHANDLE);
34
35         dprintk("NFS: GETATTR callback request from %s\n",
36                 rpc_peeraddr2str(cps->clp->cl_rpcclient, RPC_DISPLAY_ADDR));
37
38         inode = nfs_delegation_find_inode(cps->clp, &args->fh);
39         if (inode == NULL)
40                 goto out;
41         nfsi = NFS_I(inode);
42         rcu_read_lock();
43         delegation = rcu_dereference(nfsi->delegation);
44         if (delegation == NULL || (delegation->type & FMODE_WRITE) == 0)
45                 goto out_iput;
46         res->size = i_size_read(inode);
47         res->change_attr = delegation->change_attr;
48         if (nfsi->npages != 0)
49                 res->change_attr++;
50         res->ctime = inode->i_ctime;
51         res->mtime = inode->i_mtime;
52         res->bitmap[0] = (FATTR4_WORD0_CHANGE|FATTR4_WORD0_SIZE) &
53                 args->bitmap[0];
54         res->bitmap[1] = (FATTR4_WORD1_TIME_METADATA|FATTR4_WORD1_TIME_MODIFY) &
55                 args->bitmap[1];
56         res->status = 0;
57 out_iput:
58         rcu_read_unlock();
59         iput(inode);
60 out:
61         dprintk("%s: exit with status = %d\n", __func__, ntohl(res->status));
62         return res->status;
63 }
64
65 __be32 nfs4_callback_recall(struct cb_recallargs *args, void *dummy,
66                             struct cb_process_state *cps)
67 {
68         struct inode *inode;
69         __be32 res;
70         
71         res = htonl(NFS4ERR_OP_NOT_IN_SESSION);
72         if (!cps->clp) /* Always set for v4.0. Set in cb_sequence for v4.1 */
73                 goto out;
74
75         dprintk("NFS: RECALL callback request from %s\n",
76                 rpc_peeraddr2str(cps->clp->cl_rpcclient, RPC_DISPLAY_ADDR));
77
78         res = htonl(NFS4ERR_BADHANDLE);
79         inode = nfs_delegation_find_inode(cps->clp, &args->fh);
80         if (inode == NULL)
81                 goto out;
82         /* Set up a helper thread to actually return the delegation */
83         switch (nfs_async_inode_return_delegation(inode, &args->stateid)) {
84         case 0:
85                 res = 0;
86                 break;
87         case -ENOENT:
88                 if (res != 0)
89                         res = htonl(NFS4ERR_BAD_STATEID);
90                 break;
91         default:
92                 res = htonl(NFS4ERR_RESOURCE);
93         }
94         iput(inode);
95 out:
96         dprintk("%s: exit with status = %d\n", __func__, ntohl(res));
97         return res;
98 }
99
100 int nfs4_validate_delegation_stateid(struct nfs_delegation *delegation, const nfs4_stateid *stateid)
101 {
102         if (delegation == NULL || memcmp(delegation->stateid.data, stateid->data,
103                                          sizeof(delegation->stateid.data)) != 0)
104                 return 0;
105         return 1;
106 }
107
108 #if defined(CONFIG_NFS_V4_1)
109
110 int nfs41_validate_delegation_stateid(struct nfs_delegation *delegation, const nfs4_stateid *stateid)
111 {
112         if (delegation == NULL)
113                 return 0;
114
115         if (stateid->stateid.seqid != 0)
116                 return 0;
117         if (memcmp(&delegation->stateid.stateid.other,
118                    &stateid->stateid.other,
119                    NFS4_STATEID_OTHER_SIZE))
120                 return 0;
121
122         return 1;
123 }
124
125 /*
126  * Validate the sequenceID sent by the server.
127  * Return success if the sequenceID is one more than what we last saw on
128  * this slot, accounting for wraparound.  Increments the slot's sequence.
129  *
130  * We don't yet implement a duplicate request cache, instead we set the
131  * back channel ca_maxresponsesize_cached to zero. This is OK for now
132  * since we only currently implement idempotent callbacks anyway.
133  *
134  * We have a single slot backchannel at this time, so we don't bother
135  * checking the used_slots bit array on the table.  The lower layer guarantees
136  * a single outstanding callback request at a time.
137  */
138 static __be32
139 validate_seqid(struct nfs4_slot_table *tbl, struct cb_sequenceargs * args)
140 {
141         struct nfs4_slot *slot;
142
143         dprintk("%s enter. slotid %d seqid %d\n",
144                 __func__, args->csa_slotid, args->csa_sequenceid);
145
146         if (args->csa_slotid > NFS41_BC_MAX_CALLBACKS)
147                 return htonl(NFS4ERR_BADSLOT);
148
149         slot = tbl->slots + args->csa_slotid;
150         dprintk("%s slot table seqid: %d\n", __func__, slot->seq_nr);
151
152         /* Normal */
153         if (likely(args->csa_sequenceid == slot->seq_nr + 1)) {
154                 slot->seq_nr++;
155                 return htonl(NFS4_OK);
156         }
157
158         /* Replay */
159         if (args->csa_sequenceid == slot->seq_nr) {
160                 dprintk("%s seqid %d is a replay\n",
161                         __func__, args->csa_sequenceid);
162                 /* Signal process_op to set this error on next op */
163                 if (args->csa_cachethis == 0)
164                         return htonl(NFS4ERR_RETRY_UNCACHED_REP);
165
166                 /* The ca_maxresponsesize_cached is 0 with no DRC */
167                 else if (args->csa_cachethis == 1)
168                         return htonl(NFS4ERR_REP_TOO_BIG_TO_CACHE);
169         }
170
171         /* Wraparound */
172         if (args->csa_sequenceid == 1 && (slot->seq_nr + 1) == 0) {
173                 slot->seq_nr = 1;
174                 return htonl(NFS4_OK);
175         }
176
177         /* Misordered request */
178         return htonl(NFS4ERR_SEQ_MISORDERED);
179 }
180
181 /*
182  * For each referring call triple, check the session's slot table for
183  * a match.  If the slot is in use and the sequence numbers match, the
184  * client is still waiting for a response to the original request.
185  */
186 static bool referring_call_exists(struct nfs_client *clp,
187                                   uint32_t nrclists,
188                                   struct referring_call_list *rclists)
189 {
190         bool status = 0;
191         int i, j;
192         struct nfs4_session *session;
193         struct nfs4_slot_table *tbl;
194         struct referring_call_list *rclist;
195         struct referring_call *ref;
196
197         /*
198          * XXX When client trunking is implemented, this becomes
199          * a session lookup from within the loop
200          */
201         session = clp->cl_session;
202         tbl = &session->fc_slot_table;
203
204         for (i = 0; i < nrclists; i++) {
205                 rclist = &rclists[i];
206                 if (memcmp(session->sess_id.data,
207                            rclist->rcl_sessionid.data,
208                            NFS4_MAX_SESSIONID_LEN) != 0)
209                         continue;
210
211                 for (j = 0; j < rclist->rcl_nrefcalls; j++) {
212                         ref = &rclist->rcl_refcalls[j];
213
214                         dprintk("%s: sessionid %x:%x:%x:%x sequenceid %u "
215                                 "slotid %u\n", __func__,
216                                 ((u32 *)&rclist->rcl_sessionid.data)[0],
217                                 ((u32 *)&rclist->rcl_sessionid.data)[1],
218                                 ((u32 *)&rclist->rcl_sessionid.data)[2],
219                                 ((u32 *)&rclist->rcl_sessionid.data)[3],
220                                 ref->rc_sequenceid, ref->rc_slotid);
221
222                         spin_lock(&tbl->slot_tbl_lock);
223                         status = (test_bit(ref->rc_slotid, tbl->used_slots) &&
224                                   tbl->slots[ref->rc_slotid].seq_nr ==
225                                         ref->rc_sequenceid);
226                         spin_unlock(&tbl->slot_tbl_lock);
227                         if (status)
228                                 goto out;
229                 }
230         }
231
232 out:
233         return status;
234 }
235
236 __be32 nfs4_callback_sequence(struct cb_sequenceargs *args,
237                               struct cb_sequenceres *res,
238                               struct cb_process_state *cps)
239 {
240         struct nfs_client *clp;
241         int i;
242         __be32 status;
243
244         cps->clp = NULL;
245
246         status = htonl(NFS4ERR_BADSESSION);
247         /* Incoming session must match the callback session */
248         if (memcmp(&args->csa_sessionid, cps->svc_sid, NFS4_MAX_SESSIONID_LEN))
249                 goto out;
250
251         clp = nfs4_find_client_sessionid(args->csa_addr,
252                                          &args->csa_sessionid, 1);
253         if (clp == NULL)
254                 goto out;
255
256         /* state manager is resetting the session */
257         if (test_bit(NFS4_SESSION_DRAINING, &clp->cl_session->session_state)) {
258                 status = NFS4ERR_DELAY;
259                 goto out;
260         }
261
262         status = validate_seqid(&clp->cl_session->bc_slot_table, args);
263         if (status)
264                 goto out;
265
266         /*
267          * Check for pending referring calls.  If a match is found, a
268          * related callback was received before the response to the original
269          * call.
270          */
271         if (referring_call_exists(clp, args->csa_nrclists, args->csa_rclists)) {
272                 status = htonl(NFS4ERR_DELAY);
273                 goto out;
274         }
275
276         memcpy(&res->csr_sessionid, &args->csa_sessionid,
277                sizeof(res->csr_sessionid));
278         res->csr_sequenceid = args->csa_sequenceid;
279         res->csr_slotid = args->csa_slotid;
280         res->csr_highestslotid = NFS41_BC_MAX_CALLBACKS - 1;
281         res->csr_target_highestslotid = NFS41_BC_MAX_CALLBACKS - 1;
282         nfs4_cb_take_slot(clp);
283         cps->clp = clp; /* put in nfs4_callback_compound */
284
285 out:
286         for (i = 0; i < args->csa_nrclists; i++)
287                 kfree(args->csa_rclists[i].rcl_refcalls);
288         kfree(args->csa_rclists);
289
290         if (status == htonl(NFS4ERR_RETRY_UNCACHED_REP)) {
291                 cps->drc_status = status;
292                 status = 0;
293         } else
294                 res->csr_status = status;
295
296         dprintk("%s: exit with status = %d res->csr_status %d\n", __func__,
297                 ntohl(status), ntohl(res->csr_status));
298         return status;
299 }
300
301 __be32 nfs4_callback_recallany(struct cb_recallanyargs *args, void *dummy,
302                                struct cb_process_state *cps)
303 {
304         __be32 status;
305         fmode_t flags = 0;
306
307         status = htonl(NFS4ERR_OP_NOT_IN_SESSION);
308         if (!cps->clp) /* set in cb_sequence */
309                 goto out;
310
311         dprintk("NFS: RECALL_ANY callback request from %s\n",
312                 rpc_peeraddr2str(cps->clp->cl_rpcclient, RPC_DISPLAY_ADDR));
313
314         if (test_bit(RCA4_TYPE_MASK_RDATA_DLG, (const unsigned long *)
315                      &args->craa_type_mask))
316                 flags = FMODE_READ;
317         if (test_bit(RCA4_TYPE_MASK_WDATA_DLG, (const unsigned long *)
318                      &args->craa_type_mask))
319                 flags |= FMODE_WRITE;
320
321         if (flags)
322                 nfs_expire_all_delegation_types(cps->clp, flags);
323         status = htonl(NFS4_OK);
324 out:
325         dprintk("%s: exit with status = %d\n", __func__, ntohl(status));
326         return status;
327 }
328
329 /* Reduce the fore channel's max_slots to the target value */
330 __be32 nfs4_callback_recallslot(struct cb_recallslotargs *args, void *dummy,
331                                 struct cb_process_state *cps)
332 {
333         struct nfs4_slot_table *fc_tbl;
334         __be32 status;
335
336         status = htonl(NFS4ERR_OP_NOT_IN_SESSION);
337         if (!cps->clp) /* set in cb_sequence */
338                 goto out;
339
340         dprintk("NFS: CB_RECALL_SLOT request from %s target max slots %d\n",
341                 rpc_peeraddr2str(cps->clp->cl_rpcclient, RPC_DISPLAY_ADDR),
342                 args->crsa_target_max_slots);
343
344         fc_tbl = &cps->clp->cl_session->fc_slot_table;
345
346         status = htonl(NFS4ERR_BAD_HIGH_SLOT);
347         if (args->crsa_target_max_slots > fc_tbl->max_slots ||
348             args->crsa_target_max_slots < 1)
349                 goto out;
350
351         status = htonl(NFS4_OK);
352         if (args->crsa_target_max_slots == fc_tbl->max_slots)
353                 goto out;
354
355         fc_tbl->target_max_slots = args->crsa_target_max_slots;
356         nfs41_handle_recall_slot(cps->clp);
357 out:
358         dprintk("%s: exit with status = %d\n", __func__, ntohl(status));
359         return status;
360 }
361 #endif /* CONFIG_NFS_V4_1 */