NFS add session back channel draining
[linux-2.6/linux-acpi-2.6/ibm-acpi-2.6.git] / fs / nfs / callback_proc.c
blobc1bead2f3e04b5bf3ba5461fb1ca740a24705ef0
1 /*
2 * linux/fs/nfs/callback_proc.c
4 * Copyright (C) 2004 Trond Myklebust
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"
16 #ifdef NFS_DEBUG
17 #define NFSDBG_FACILITY NFSDBG_CALLBACK
18 #endif
20 __be32 nfs4_callback_getattr(struct cb_getattrargs *args,
21 struct cb_getattrres *res,
22 struct cb_process_state *cps)
24 struct nfs_delegation *delegation;
25 struct nfs_inode *nfsi;
26 struct inode *inode;
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;
32 res->bitmap[0] = res->bitmap[1] = 0;
33 res->status = htonl(NFS4ERR_BADHANDLE);
35 dprintk("NFS: GETATTR callback request from %s\n",
36 rpc_peeraddr2str(cps->clp->cl_rpcclient, RPC_DISPLAY_ADDR));
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;
65 __be32 nfs4_callback_recall(struct cb_recallargs *args, void *dummy,
66 struct cb_process_state *cps)
68 struct inode *inode;
69 __be32 res;
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;
75 dprintk("NFS: RECALL callback request from %s\n",
76 rpc_peeraddr2str(cps->clp->cl_rpcclient, RPC_DISPLAY_ADDR));
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);
94 iput(inode);
95 out:
96 dprintk("%s: exit with status = %d\n", __func__, ntohl(res));
97 return res;
100 int nfs4_validate_delegation_stateid(struct nfs_delegation *delegation, const nfs4_stateid *stateid)
102 if (delegation == NULL || memcmp(delegation->stateid.data, stateid->data,
103 sizeof(delegation->stateid.data)) != 0)
104 return 0;
105 return 1;
108 #if defined(CONFIG_NFS_V4_1)
110 int nfs41_validate_delegation_stateid(struct nfs_delegation *delegation, const nfs4_stateid *stateid)
112 if (delegation == NULL)
113 return 0;
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;
122 return 1;
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.
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.
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.
138 static __be32
139 validate_seqid(struct nfs4_slot_table *tbl, struct cb_sequenceargs * args)
141 struct nfs4_slot *slot;
143 dprintk("%s enter. slotid %d seqid %d\n",
144 __func__, args->csa_slotid, args->csa_sequenceid);
146 if (args->csa_slotid > NFS41_BC_MAX_CALLBACKS)
147 return htonl(NFS4ERR_BADSLOT);
149 slot = tbl->slots + args->csa_slotid;
150 dprintk("%s slot table seqid: %d\n", __func__, slot->seq_nr);
152 /* Normal */
153 if (likely(args->csa_sequenceid == slot->seq_nr + 1)) {
154 slot->seq_nr++;
155 return htonl(NFS4_OK);
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);
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);
171 /* Wraparound */
172 if (args->csa_sequenceid == 1 && (slot->seq_nr + 1) == 0) {
173 slot->seq_nr = 1;
174 return htonl(NFS4_OK);
177 /* Misordered request */
178 return htonl(NFS4ERR_SEQ_MISORDERED);
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.
186 static bool referring_call_exists(struct nfs_client *clp,
187 uint32_t nrclists,
188 struct referring_call_list *rclists)
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;
198 * XXX When client trunking is implemented, this becomes
199 * a session lookup from within the loop
201 session = clp->cl_session;
202 tbl = &session->fc_slot_table;
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;
211 for (j = 0; j < rclist->rcl_nrefcalls; j++) {
212 ref = &rclist->rcl_refcalls[j];
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);
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;
232 out:
233 return status;
236 __be32 nfs4_callback_sequence(struct cb_sequenceargs *args,
237 struct cb_sequenceres *res,
238 struct cb_process_state *cps)
240 struct nfs_client *clp;
241 int i;
242 __be32 status;
244 cps->clp = NULL;
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;
251 clp = nfs4_find_client_sessionid(args->csa_addr,
252 &args->csa_sessionid, 1);
253 if (clp == NULL)
254 goto out;
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;
262 status = validate_seqid(&clp->cl_session->bc_slot_table, args);
263 if (status)
264 goto out;
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.
271 if (referring_call_exists(clp, args->csa_nrclists, args->csa_rclists)) {
272 status = htonl(NFS4ERR_DELAY);
273 goto out;
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 */
285 out:
286 for (i = 0; i < args->csa_nrclists; i++)
287 kfree(args->csa_rclists[i].rcl_refcalls);
288 kfree(args->csa_rclists);
290 if (status == htonl(NFS4ERR_RETRY_UNCACHED_REP)) {
291 cps->drc_status = status;
292 status = 0;
293 } else
294 res->csr_status = status;
296 dprintk("%s: exit with status = %d res->csr_status %d\n", __func__,
297 ntohl(status), ntohl(res->csr_status));
298 return status;
301 __be32 nfs4_callback_recallany(struct cb_recallanyargs *args, void *dummy,
302 struct cb_process_state *cps)
304 __be32 status;
305 fmode_t flags = 0;
307 status = htonl(NFS4ERR_OP_NOT_IN_SESSION);
308 if (!cps->clp) /* set in cb_sequence */
309 goto out;
311 dprintk("NFS: RECALL_ANY callback request from %s\n",
312 rpc_peeraddr2str(cps->clp->cl_rpcclient, RPC_DISPLAY_ADDR));
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;
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;
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)
333 struct nfs4_slot_table *fc_tbl;
334 __be32 status;
336 status = htonl(NFS4ERR_OP_NOT_IN_SESSION);
337 if (!cps->clp) /* set in cb_sequence */
338 goto out;
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);
344 fc_tbl = &cps->clp->cl_session->fc_slot_table;
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;
351 status = htonl(NFS4_OK);
352 if (args->crsa_target_max_slots == fc_tbl->max_slots)
353 goto out;
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;
361 #endif /* CONFIG_NFS_V4_1 */