blob: c1bb157e94bd8c6c7dad2bba650f9f17d618211c [file] [log] [blame]
Linus Torvalds1da177e2005-04-16 15:20:36 -07001/*
2 * linux/fs/nfs/callback_proc.c
3 *
4 * Copyright (C) 2004 Trond Myklebust
5 *
6 * NFSv4 callback procedures
7 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07008#include <linux/nfs4.h>
9#include <linux/nfs_fs.h>
Tejun Heo5a0e3ad2010-03-24 17:04:11 +090010#include <linux/slab.h>
Trond Myklebust4ce79712005-06-22 17:16:21 +000011#include "nfs4_fs.h"
Linus Torvalds1da177e2005-04-16 15:20:36 -070012#include "callback.h"
13#include "delegation.h"
David Howells24c8dbb2006-08-22 20:06:10 -040014#include "internal.h"
Linus Torvalds1da177e2005-04-16 15:20:36 -070015
Chuck Lever1d98fe62007-12-10 14:57:23 -050016#ifdef NFS_DEBUG
Linus Torvalds1da177e2005-04-16 15:20:36 -070017#define NFSDBG_FACILITY NFSDBG_CALLBACK
Chuck Lever1d98fe62007-12-10 14:57:23 -050018#endif
Andy Adamsonc36fca52011-01-06 02:04:32 +000019
20__be32 nfs4_callback_getattr(struct cb_getattrargs *args,
21 struct cb_getattrres *res,
22 struct cb_process_state *cps)
Linus Torvalds1da177e2005-04-16 15:20:36 -070023{
Linus Torvalds1da177e2005-04-16 15:20:36 -070024 struct nfs_delegation *delegation;
25 struct nfs_inode *nfsi;
26 struct inode *inode;
Chuck Lever1d98fe62007-12-10 14:57:23 -050027
Andy Adamsonc36fca52011-01-06 02:04:32 +000028 res->status = htonl(NFS4ERR_OP_NOT_IN_SESSION);
29 if (!cps->clp) /* Always set for v4.0. Set in cb_sequence for v4.1 */
Linus Torvalds1da177e2005-04-16 15:20:36 -070030 goto out;
Chuck Lever1d98fe62007-12-10 14:57:23 -050031
Andy Adamsonc36fca52011-01-06 02:04:32 +000032 res->bitmap[0] = res->bitmap[1] = 0;
33 res->status = htonl(NFS4ERR_BADHANDLE);
Chuck Lever1d98fe62007-12-10 14:57:23 -050034
Andy Adamsonc36fca52011-01-06 02:04:32 +000035 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);
Linus Torvalds1da177e2005-04-16 15:20:36 -070039 if (inode == NULL)
Andy Adamsonc36fca52011-01-06 02:04:32 +000040 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -070041 nfsi = NFS_I(inode);
Trond Myklebust761fe932010-08-04 09:29:52 -040042 rcu_read_lock();
43 delegation = rcu_dereference(nfsi->delegation);
Linus Torvalds1da177e2005-04-16 15:20:36 -070044 if (delegation == NULL || (delegation->type & FMODE_WRITE) == 0)
45 goto out_iput;
46 res->size = i_size_read(inode);
Trond Myklebustbeb2a5e2006-01-03 09:55:37 +010047 res->change_attr = delegation->change_attr;
48 if (nfsi->npages != 0)
49 res->change_attr++;
Linus Torvalds1da177e2005-04-16 15:20:36 -070050 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;
57out_iput:
Trond Myklebust761fe932010-08-04 09:29:52 -040058 rcu_read_unlock();
Linus Torvalds1da177e2005-04-16 15:20:36 -070059 iput(inode);
Linus Torvalds1da177e2005-04-16 15:20:36 -070060out:
Harvey Harrison3110ff82008-05-02 13:42:44 -070061 dprintk("%s: exit with status = %d\n", __func__, ntohl(res->status));
Linus Torvalds1da177e2005-04-16 15:20:36 -070062 return res->status;
63}
64
Andy Adamsonc36fca52011-01-06 02:04:32 +000065__be32 nfs4_callback_recall(struct cb_recallargs *args, void *dummy,
66 struct cb_process_state *cps)
Linus Torvalds1da177e2005-04-16 15:20:36 -070067{
Linus Torvalds1da177e2005-04-16 15:20:36 -070068 struct inode *inode;
Al Viroe6f684f2006-10-19 23:28:50 -070069 __be32 res;
Linus Torvalds1da177e2005-04-16 15:20:36 -070070
Andy Adamsonc36fca52011-01-06 02:04:32 +000071 res = htonl(NFS4ERR_OP_NOT_IN_SESSION);
72 if (!cps->clp) /* Always set for v4.0. Set in cb_sequence for v4.1 */
Linus Torvalds1da177e2005-04-16 15:20:36 -070073 goto out;
Chuck Lever1d98fe62007-12-10 14:57:23 -050074
75 dprintk("NFS: RECALL callback request from %s\n",
Andy Adamsonc36fca52011-01-06 02:04:32 +000076 rpc_peeraddr2str(cps->clp->cl_rpcclient, RPC_DISPLAY_ADDR));
Chuck Lever1d98fe62007-12-10 14:57:23 -050077
Andy Adamsonc36fca52011-01-06 02:04:32 +000078 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);
Linus Torvalds1da177e2005-04-16 15:20:36 -070095out:
Harvey Harrison3110ff82008-05-02 13:42:44 -070096 dprintk("%s: exit with status = %d\n", __func__, ntohl(res));
Linus Torvalds1da177e2005-04-16 15:20:36 -070097 return res;
98}
Benny Halevyd49433e2009-04-01 09:23:25 -040099
Alexandros Batsakis25976412009-12-05 13:48:55 -0500100int 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
Benny Halevyd49433e2009-04-01 09:23:25 -0400108#if defined(CONFIG_NFS_V4_1)
109
Fred Isamanf2a62562011-01-06 11:36:29 +0000110__be32 nfs4_callback_layoutrecall(struct cb_layoutrecallargs *args,
111 void *dummy, struct cb_process_state *cps)
112{
113 return cpu_to_be32(NFS4ERR_NOTSUPP); /* STUB */
114}
115
Alexandros Batsakis25976412009-12-05 13:48:55 -0500116int nfs41_validate_delegation_stateid(struct nfs_delegation *delegation, const nfs4_stateid *stateid)
117{
118 if (delegation == NULL)
119 return 0;
120
Alexandros Batsakis94499252010-10-20 00:17:56 -0400121 if (stateid->stateid.seqid != 0)
Alexandros Batsakis25976412009-12-05 13:48:55 -0500122 return 0;
Alexandros Batsakis94499252010-10-20 00:17:56 -0400123 if (memcmp(&delegation->stateid.stateid.other,
124 &stateid->stateid.other,
125 NFS4_STATEID_OTHER_SIZE))
Alexandros Batsakis25976412009-12-05 13:48:55 -0500126 return 0;
127
128 return 1;
129}
130
Ricardo Labiaga963891a2009-04-01 09:23:34 -0400131/*
Ricardo Labiaga68f3f902009-04-01 09:23:35 -0400132 * Validate the sequenceID sent by the server.
133 * Return success if the sequenceID is one more than what we last saw on
134 * this slot, accounting for wraparound. Increments the slot's sequence.
135 *
Andy Adamson49110962010-01-14 17:45:08 -0500136 * We don't yet implement a duplicate request cache, instead we set the
137 * back channel ca_maxresponsesize_cached to zero. This is OK for now
Ricardo Labiaga68f3f902009-04-01 09:23:35 -0400138 * since we only currently implement idempotent callbacks anyway.
139 *
140 * We have a single slot backchannel at this time, so we don't bother
141 * checking the used_slots bit array on the table. The lower layer guarantees
142 * a single outstanding callback request at a time.
143 */
Andy Adamson9733f0d2010-01-22 12:03:08 -0500144static __be32
Andy Adamsonb2f28bd2010-01-14 17:45:07 -0500145validate_seqid(struct nfs4_slot_table *tbl, struct cb_sequenceargs * args)
Ricardo Labiaga68f3f902009-04-01 09:23:35 -0400146{
147 struct nfs4_slot *slot;
148
149 dprintk("%s enter. slotid %d seqid %d\n",
Andy Adamsonb2f28bd2010-01-14 17:45:07 -0500150 __func__, args->csa_slotid, args->csa_sequenceid);
Ricardo Labiaga68f3f902009-04-01 09:23:35 -0400151
Andy Adamsonb2f28bd2010-01-14 17:45:07 -0500152 if (args->csa_slotid > NFS41_BC_MAX_CALLBACKS)
Ricardo Labiaga68f3f902009-04-01 09:23:35 -0400153 return htonl(NFS4ERR_BADSLOT);
154
Andy Adamsonb2f28bd2010-01-14 17:45:07 -0500155 slot = tbl->slots + args->csa_slotid;
Ricardo Labiaga68f3f902009-04-01 09:23:35 -0400156 dprintk("%s slot table seqid: %d\n", __func__, slot->seq_nr);
157
158 /* Normal */
Andy Adamsonb2f28bd2010-01-14 17:45:07 -0500159 if (likely(args->csa_sequenceid == slot->seq_nr + 1)) {
Ricardo Labiaga68f3f902009-04-01 09:23:35 -0400160 slot->seq_nr++;
161 return htonl(NFS4_OK);
162 }
163
164 /* Replay */
Andy Adamsonb2f28bd2010-01-14 17:45:07 -0500165 if (args->csa_sequenceid == slot->seq_nr) {
Andy Adamson49110962010-01-14 17:45:08 -0500166 dprintk("%s seqid %d is a replay\n",
Andy Adamsonb2f28bd2010-01-14 17:45:07 -0500167 __func__, args->csa_sequenceid);
Andy Adamson49110962010-01-14 17:45:08 -0500168 /* Signal process_op to set this error on next op */
169 if (args->csa_cachethis == 0)
170 return htonl(NFS4ERR_RETRY_UNCACHED_REP);
171
172 /* The ca_maxresponsesize_cached is 0 with no DRC */
173 else if (args->csa_cachethis == 1)
174 return htonl(NFS4ERR_REP_TOO_BIG_TO_CACHE);
Ricardo Labiaga68f3f902009-04-01 09:23:35 -0400175 }
176
177 /* Wraparound */
Andy Adamsonb2f28bd2010-01-14 17:45:07 -0500178 if (args->csa_sequenceid == 1 && (slot->seq_nr + 1) == 0) {
Ricardo Labiaga68f3f902009-04-01 09:23:35 -0400179 slot->seq_nr = 1;
180 return htonl(NFS4_OK);
181 }
182
183 /* Misordered request */
184 return htonl(NFS4ERR_SEQ_MISORDERED);
185}
186
187/*
Mike Sagera7989c32010-01-19 12:54:40 -0500188 * For each referring call triple, check the session's slot table for
189 * a match. If the slot is in use and the sequence numbers match, the
190 * client is still waiting for a response to the original request.
191 */
192static bool referring_call_exists(struct nfs_client *clp,
193 uint32_t nrclists,
194 struct referring_call_list *rclists)
195{
196 bool status = 0;
197 int i, j;
198 struct nfs4_session *session;
199 struct nfs4_slot_table *tbl;
200 struct referring_call_list *rclist;
201 struct referring_call *ref;
202
203 /*
204 * XXX When client trunking is implemented, this becomes
205 * a session lookup from within the loop
206 */
207 session = clp->cl_session;
208 tbl = &session->fc_slot_table;
209
210 for (i = 0; i < nrclists; i++) {
211 rclist = &rclists[i];
212 if (memcmp(session->sess_id.data,
213 rclist->rcl_sessionid.data,
214 NFS4_MAX_SESSIONID_LEN) != 0)
215 continue;
216
217 for (j = 0; j < rclist->rcl_nrefcalls; j++) {
218 ref = &rclist->rcl_refcalls[j];
219
220 dprintk("%s: sessionid %x:%x:%x:%x sequenceid %u "
221 "slotid %u\n", __func__,
222 ((u32 *)&rclist->rcl_sessionid.data)[0],
223 ((u32 *)&rclist->rcl_sessionid.data)[1],
224 ((u32 *)&rclist->rcl_sessionid.data)[2],
225 ((u32 *)&rclist->rcl_sessionid.data)[3],
226 ref->rc_sequenceid, ref->rc_slotid);
227
228 spin_lock(&tbl->slot_tbl_lock);
229 status = (test_bit(ref->rc_slotid, tbl->used_slots) &&
230 tbl->slots[ref->rc_slotid].seq_nr ==
231 ref->rc_sequenceid);
232 spin_unlock(&tbl->slot_tbl_lock);
233 if (status)
234 goto out;
235 }
236 }
237
238out:
239 return status;
240}
241
Andy Adamson9733f0d2010-01-22 12:03:08 -0500242__be32 nfs4_callback_sequence(struct cb_sequenceargs *args,
Andy Adamsonc36fca52011-01-06 02:04:32 +0000243 struct cb_sequenceres *res,
244 struct cb_process_state *cps)
Benny Halevyd49433e2009-04-01 09:23:25 -0400245{
Ricardo Labiaga68f3f902009-04-01 09:23:35 -0400246 struct nfs_client *clp;
Andy Adamson9733f0d2010-01-22 12:03:08 -0500247 int i;
248 __be32 status;
Benny Halevyd49433e2009-04-01 09:23:25 -0400249
Andy Adamsonc36fca52011-01-06 02:04:32 +0000250 cps->clp = NULL;
251
Ricardo Labiaga68f3f902009-04-01 09:23:35 -0400252 status = htonl(NFS4ERR_BADSESSION);
Andy Adamsonc36fca52011-01-06 02:04:32 +0000253 /* Incoming session must match the callback session */
254 if (memcmp(&args->csa_sessionid, cps->svc_sid, NFS4_MAX_SESSIONID_LEN))
255 goto out;
256
257 clp = nfs4_find_client_sessionid(args->csa_addr,
258 &args->csa_sessionid, 1);
Ricardo Labiaga68f3f902009-04-01 09:23:35 -0400259 if (clp == NULL)
260 goto out;
261
Andy Adamson42acd022011-01-06 02:04:34 +0000262 /* state manager is resetting the session */
263 if (test_bit(NFS4_SESSION_DRAINING, &clp->cl_session->session_state)) {
264 status = NFS4ERR_DELAY;
265 goto out;
266 }
267
Andy Adamsonb2f28bd2010-01-14 17:45:07 -0500268 status = validate_seqid(&clp->cl_session->bc_slot_table, args);
Ricardo Labiaga68f3f902009-04-01 09:23:35 -0400269 if (status)
Andy Adamsonc36fca52011-01-06 02:04:32 +0000270 goto out;
Ricardo Labiaga68f3f902009-04-01 09:23:35 -0400271
Mike Sager72ce2b32010-01-19 12:54:41 -0500272 /*
273 * Check for pending referring calls. If a match is found, a
274 * related callback was received before the response to the original
275 * call.
276 */
277 if (referring_call_exists(clp, args->csa_nrclists, args->csa_rclists)) {
278 status = htonl(NFS4ERR_DELAY);
Andy Adamsonc36fca52011-01-06 02:04:32 +0000279 goto out;
Mike Sager72ce2b32010-01-19 12:54:41 -0500280 }
281
Benny Halevyd49433e2009-04-01 09:23:25 -0400282 memcpy(&res->csr_sessionid, &args->csa_sessionid,
283 sizeof(res->csr_sessionid));
284 res->csr_sequenceid = args->csa_sequenceid;
285 res->csr_slotid = args->csa_slotid;
286 res->csr_highestslotid = NFS41_BC_MAX_CALLBACKS - 1;
287 res->csr_target_highestslotid = NFS41_BC_MAX_CALLBACKS - 1;
Andy Adamson42acd022011-01-06 02:04:34 +0000288 nfs4_cb_take_slot(clp);
Andy Adamsonc36fca52011-01-06 02:04:32 +0000289 cps->clp = clp; /* put in nfs4_callback_compound */
Benny Halevyd49433e2009-04-01 09:23:25 -0400290
Ricardo Labiaga68f3f902009-04-01 09:23:35 -0400291out:
Mike Sager72ce2b32010-01-19 12:54:41 -0500292 for (i = 0; i < args->csa_nrclists; i++)
293 kfree(args->csa_rclists[i].rcl_refcalls);
294 kfree(args->csa_rclists);
295
Andy Adamsonc36fca52011-01-06 02:04:32 +0000296 if (status == htonl(NFS4ERR_RETRY_UNCACHED_REP)) {
297 cps->drc_status = status;
298 status = 0;
299 } else
Andy Adamson49110962010-01-14 17:45:08 -0500300 res->csr_status = status;
Andy Adamsonc36fca52011-01-06 02:04:32 +0000301
Andy Adamson49110962010-01-14 17:45:08 -0500302 dprintk("%s: exit with status = %d res->csr_status %d\n", __func__,
303 ntohl(status), ntohl(res->csr_status));
304 return status;
Benny Halevyd49433e2009-04-01 09:23:25 -0400305}
306
Andy Adamsonc36fca52011-01-06 02:04:32 +0000307__be32 nfs4_callback_recallany(struct cb_recallanyargs *args, void *dummy,
308 struct cb_process_state *cps)
Alexandros Batsakis31f09602009-12-05 13:27:02 -0500309{
Andy Adamson9733f0d2010-01-22 12:03:08 -0500310 __be32 status;
Alexandros Batsakis31f09602009-12-05 13:27:02 -0500311 fmode_t flags = 0;
312
313 status = htonl(NFS4ERR_OP_NOT_IN_SESSION);
Andy Adamsonc36fca52011-01-06 02:04:32 +0000314 if (!cps->clp) /* set in cb_sequence */
Alexandros Batsakis31f09602009-12-05 13:27:02 -0500315 goto out;
316
317 dprintk("NFS: RECALL_ANY callback request from %s\n",
Andy Adamsonc36fca52011-01-06 02:04:32 +0000318 rpc_peeraddr2str(cps->clp->cl_rpcclient, RPC_DISPLAY_ADDR));
Alexandros Batsakis31f09602009-12-05 13:27:02 -0500319
320 if (test_bit(RCA4_TYPE_MASK_RDATA_DLG, (const unsigned long *)
321 &args->craa_type_mask))
322 flags = FMODE_READ;
323 if (test_bit(RCA4_TYPE_MASK_WDATA_DLG, (const unsigned long *)
324 &args->craa_type_mask))
325 flags |= FMODE_WRITE;
326
327 if (flags)
Andy Adamsonc36fca52011-01-06 02:04:32 +0000328 nfs_expire_all_delegation_types(cps->clp, flags);
Alexandros Batsakis31f09602009-12-05 13:27:02 -0500329 status = htonl(NFS4_OK);
330out:
331 dprintk("%s: exit with status = %d\n", __func__, ntohl(status));
332 return status;
333}
Andy Adamsonb9efa1b2010-01-20 16:06:27 -0500334
335/* Reduce the fore channel's max_slots to the target value */
Andy Adamsonc36fca52011-01-06 02:04:32 +0000336__be32 nfs4_callback_recallslot(struct cb_recallslotargs *args, void *dummy,
337 struct cb_process_state *cps)
Andy Adamsonb9efa1b2010-01-20 16:06:27 -0500338{
Andy Adamsonb9efa1b2010-01-20 16:06:27 -0500339 struct nfs4_slot_table *fc_tbl;
Andy Adamson9733f0d2010-01-22 12:03:08 -0500340 __be32 status;
Andy Adamsonb9efa1b2010-01-20 16:06:27 -0500341
342 status = htonl(NFS4ERR_OP_NOT_IN_SESSION);
Andy Adamsonc36fca52011-01-06 02:04:32 +0000343 if (!cps->clp) /* set in cb_sequence */
Andy Adamsonb9efa1b2010-01-20 16:06:27 -0500344 goto out;
345
346 dprintk("NFS: CB_RECALL_SLOT request from %s target max slots %d\n",
Andy Adamsonc36fca52011-01-06 02:04:32 +0000347 rpc_peeraddr2str(cps->clp->cl_rpcclient, RPC_DISPLAY_ADDR),
Andy Adamsonb9efa1b2010-01-20 16:06:27 -0500348 args->crsa_target_max_slots);
349
Andy Adamsonc36fca52011-01-06 02:04:32 +0000350 fc_tbl = &cps->clp->cl_session->fc_slot_table;
Andy Adamsonb9efa1b2010-01-20 16:06:27 -0500351
352 status = htonl(NFS4ERR_BAD_HIGH_SLOT);
Andy Adamsonbae0ac02010-01-21 14:19:16 -0500353 if (args->crsa_target_max_slots > fc_tbl->max_slots ||
Andy Adamsonb9efa1b2010-01-20 16:06:27 -0500354 args->crsa_target_max_slots < 1)
Andy Adamsonc36fca52011-01-06 02:04:32 +0000355 goto out;
Andy Adamsonbae0ac02010-01-21 14:19:16 -0500356
357 status = htonl(NFS4_OK);
358 if (args->crsa_target_max_slots == fc_tbl->max_slots)
Andy Adamsonc36fca52011-01-06 02:04:32 +0000359 goto out;
Andy Adamsonb9efa1b2010-01-20 16:06:27 -0500360
361 fc_tbl->target_max_slots = args->crsa_target_max_slots;
Andy Adamsonc36fca52011-01-06 02:04:32 +0000362 nfs41_handle_recall_slot(cps->clp);
Andy Adamsonb9efa1b2010-01-20 16:06:27 -0500363out:
364 dprintk("%s: exit with status = %d\n", __func__, ntohl(status));
365 return status;
366}
Benny Halevyd49433e2009-04-01 09:23:25 -0400367#endif /* CONFIG_NFS_V4_1 */