blob: a08770a7e857bf10ef5f21f9e10d1465c2c9ce88 [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
Linus Torvalds1da177e2005-04-16 15:20:36 -070019
Al Viroe6f684f2006-10-19 23:28:50 -070020__be32 nfs4_callback_getattr(struct cb_getattrargs *args, struct cb_getattrres *res)
Linus Torvalds1da177e2005-04-16 15:20:36 -070021{
David Howellsadfa6f92006-08-22 20:06:08 -040022 struct nfs_client *clp;
Linus Torvalds1da177e2005-04-16 15:20:36 -070023 struct nfs_delegation *delegation;
24 struct nfs_inode *nfsi;
25 struct inode *inode;
Chuck Lever1d98fe62007-12-10 14:57:23 -050026
Linus Torvalds1da177e2005-04-16 15:20:36 -070027 res->bitmap[0] = res->bitmap[1] = 0;
28 res->status = htonl(NFS4ERR_BADHANDLE);
Chuck Leverff052642007-12-10 14:58:44 -050029 clp = nfs_find_client(args->addr, 4);
Linus Torvalds1da177e2005-04-16 15:20:36 -070030 if (clp == NULL)
31 goto out;
Chuck Lever1d98fe62007-12-10 14:57:23 -050032
33 dprintk("NFS: GETATTR callback request from %s\n",
34 rpc_peeraddr2str(clp->cl_rpcclient, RPC_DISPLAY_ADDR));
35
Linus Torvalds1da177e2005-04-16 15:20:36 -070036 inode = nfs_delegation_find_inode(clp, &args->fh);
37 if (inode == NULL)
38 goto out_putclient;
39 nfsi = NFS_I(inode);
40 down_read(&nfsi->rwsem);
41 delegation = nfsi->delegation;
42 if (delegation == NULL || (delegation->type & FMODE_WRITE) == 0)
43 goto out_iput;
44 res->size = i_size_read(inode);
Trond Myklebustbeb2a5e2006-01-03 09:55:37 +010045 res->change_attr = delegation->change_attr;
46 if (nfsi->npages != 0)
47 res->change_attr++;
Linus Torvalds1da177e2005-04-16 15:20:36 -070048 res->ctime = inode->i_ctime;
49 res->mtime = inode->i_mtime;
50 res->bitmap[0] = (FATTR4_WORD0_CHANGE|FATTR4_WORD0_SIZE) &
51 args->bitmap[0];
52 res->bitmap[1] = (FATTR4_WORD1_TIME_METADATA|FATTR4_WORD1_TIME_MODIFY) &
53 args->bitmap[1];
54 res->status = 0;
55out_iput:
56 up_read(&nfsi->rwsem);
57 iput(inode);
58out_putclient:
David Howells24c8dbb2006-08-22 20:06:10 -040059 nfs_put_client(clp);
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
Alexandros Batsakis25976412009-12-05 13:48:55 -050065static int (*nfs_validate_delegation_stateid(struct nfs_client *clp))(struct nfs_delegation *, const nfs4_stateid *)
66{
67#if defined(CONFIG_NFS_V4_1)
68 if (clp->cl_minorversion > 0)
69 return nfs41_validate_delegation_stateid;
70#endif
71 return nfs4_validate_delegation_stateid;
72}
73
74
Al Viroe6f684f2006-10-19 23:28:50 -070075__be32 nfs4_callback_recall(struct cb_recallargs *args, void *dummy)
Linus Torvalds1da177e2005-04-16 15:20:36 -070076{
David Howellsadfa6f92006-08-22 20:06:08 -040077 struct nfs_client *clp;
Linus Torvalds1da177e2005-04-16 15:20:36 -070078 struct inode *inode;
Al Viroe6f684f2006-10-19 23:28:50 -070079 __be32 res;
Linus Torvalds1da177e2005-04-16 15:20:36 -070080
81 res = htonl(NFS4ERR_BADHANDLE);
Chuck Leverff052642007-12-10 14:58:44 -050082 clp = nfs_find_client(args->addr, 4);
Linus Torvalds1da177e2005-04-16 15:20:36 -070083 if (clp == NULL)
84 goto out;
Chuck Lever1d98fe62007-12-10 14:57:23 -050085
86 dprintk("NFS: RECALL callback request from %s\n",
87 rpc_peeraddr2str(clp->cl_rpcclient, RPC_DISPLAY_ADDR));
88
Trond Myklebust3fbd67a2008-01-26 01:06:40 -050089 do {
90 struct nfs_client *prev = clp;
91
92 inode = nfs_delegation_find_inode(clp, &args->fh);
93 if (inode != NULL) {
94 /* Set up a helper thread to actually return the delegation */
Alexandros Batsakis25976412009-12-05 13:48:55 -050095 switch (nfs_async_inode_return_delegation(inode, &args->stateid,
96 nfs_validate_delegation_stateid(clp))) {
Trond Myklebust3fbd67a2008-01-26 01:06:40 -050097 case 0:
98 res = 0;
99 break;
100 case -ENOENT:
101 if (res != 0)
102 res = htonl(NFS4ERR_BAD_STATEID);
103 break;
104 default:
105 res = htonl(NFS4ERR_RESOURCE);
106 }
107 iput(inode);
108 }
109 clp = nfs_find_client_next(prev);
110 nfs_put_client(prev);
111 } while (clp != NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700112out:
Harvey Harrison3110ff82008-05-02 13:42:44 -0700113 dprintk("%s: exit with status = %d\n", __func__, ntohl(res));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700114 return res;
115}
Benny Halevyd49433e2009-04-01 09:23:25 -0400116
Alexandros Batsakis25976412009-12-05 13:48:55 -0500117int nfs4_validate_delegation_stateid(struct nfs_delegation *delegation, const nfs4_stateid *stateid)
118{
119 if (delegation == NULL || memcmp(delegation->stateid.data, stateid->data,
120 sizeof(delegation->stateid.data)) != 0)
121 return 0;
122 return 1;
123}
124
Benny Halevyd49433e2009-04-01 09:23:25 -0400125#if defined(CONFIG_NFS_V4_1)
126
Alexandros Batsakis25976412009-12-05 13:48:55 -0500127int nfs41_validate_delegation_stateid(struct nfs_delegation *delegation, const nfs4_stateid *stateid)
128{
129 if (delegation == NULL)
130 return 0;
131
132 /* seqid is 4-bytes long */
133 if (((u32 *) &stateid->data)[0] != 0)
134 return 0;
135 if (memcmp(&delegation->stateid.data[4], &stateid->data[4],
136 sizeof(stateid->data)-4))
137 return 0;
138
139 return 1;
140}
141
Ricardo Labiaga963891a2009-04-01 09:23:34 -0400142/*
Ricardo Labiaga68f3f902009-04-01 09:23:35 -0400143 * Validate the sequenceID sent by the server.
144 * Return success if the sequenceID is one more than what we last saw on
145 * this slot, accounting for wraparound. Increments the slot's sequence.
146 *
Andy Adamson49110962010-01-14 17:45:08 -0500147 * We don't yet implement a duplicate request cache, instead we set the
148 * back channel ca_maxresponsesize_cached to zero. This is OK for now
Ricardo Labiaga68f3f902009-04-01 09:23:35 -0400149 * since we only currently implement idempotent callbacks anyway.
150 *
151 * We have a single slot backchannel at this time, so we don't bother
152 * checking the used_slots bit array on the table. The lower layer guarantees
153 * a single outstanding callback request at a time.
154 */
Andy Adamson9733f0d2010-01-22 12:03:08 -0500155static __be32
Andy Adamsonb2f28bd2010-01-14 17:45:07 -0500156validate_seqid(struct nfs4_slot_table *tbl, struct cb_sequenceargs * args)
Ricardo Labiaga68f3f902009-04-01 09:23:35 -0400157{
158 struct nfs4_slot *slot;
159
160 dprintk("%s enter. slotid %d seqid %d\n",
Andy Adamsonb2f28bd2010-01-14 17:45:07 -0500161 __func__, args->csa_slotid, args->csa_sequenceid);
Ricardo Labiaga68f3f902009-04-01 09:23:35 -0400162
Andy Adamsonb2f28bd2010-01-14 17:45:07 -0500163 if (args->csa_slotid > NFS41_BC_MAX_CALLBACKS)
Ricardo Labiaga68f3f902009-04-01 09:23:35 -0400164 return htonl(NFS4ERR_BADSLOT);
165
Andy Adamsonb2f28bd2010-01-14 17:45:07 -0500166 slot = tbl->slots + args->csa_slotid;
Ricardo Labiaga68f3f902009-04-01 09:23:35 -0400167 dprintk("%s slot table seqid: %d\n", __func__, slot->seq_nr);
168
169 /* Normal */
Andy Adamsonb2f28bd2010-01-14 17:45:07 -0500170 if (likely(args->csa_sequenceid == slot->seq_nr + 1)) {
Ricardo Labiaga68f3f902009-04-01 09:23:35 -0400171 slot->seq_nr++;
172 return htonl(NFS4_OK);
173 }
174
175 /* Replay */
Andy Adamsonb2f28bd2010-01-14 17:45:07 -0500176 if (args->csa_sequenceid == slot->seq_nr) {
Andy Adamson49110962010-01-14 17:45:08 -0500177 dprintk("%s seqid %d is a replay\n",
Andy Adamsonb2f28bd2010-01-14 17:45:07 -0500178 __func__, args->csa_sequenceid);
Andy Adamson49110962010-01-14 17:45:08 -0500179 /* Signal process_op to set this error on next op */
180 if (args->csa_cachethis == 0)
181 return htonl(NFS4ERR_RETRY_UNCACHED_REP);
182
183 /* The ca_maxresponsesize_cached is 0 with no DRC */
184 else if (args->csa_cachethis == 1)
185 return htonl(NFS4ERR_REP_TOO_BIG_TO_CACHE);
Ricardo Labiaga68f3f902009-04-01 09:23:35 -0400186 }
187
188 /* Wraparound */
Andy Adamsonb2f28bd2010-01-14 17:45:07 -0500189 if (args->csa_sequenceid == 1 && (slot->seq_nr + 1) == 0) {
Ricardo Labiaga68f3f902009-04-01 09:23:35 -0400190 slot->seq_nr = 1;
191 return htonl(NFS4_OK);
192 }
193
194 /* Misordered request */
195 return htonl(NFS4ERR_SEQ_MISORDERED);
196}
197
198/*
Ricardo Labiaga963891a2009-04-01 09:23:34 -0400199 * Returns a pointer to a held 'struct nfs_client' that matches the server's
200 * address, major version number, and session ID. It is the caller's
201 * responsibility to release the returned reference.
202 *
203 * Returns NULL if there are no connections with sessions, or if no session
204 * matches the one of interest.
205 */
206 static struct nfs_client *find_client_with_session(
207 const struct sockaddr *addr, u32 nfsversion,
208 struct nfs4_sessionid *sessionid)
209{
210 struct nfs_client *clp;
211
212 clp = nfs_find_client(addr, 4);
213 if (clp == NULL)
214 return NULL;
215
216 do {
217 struct nfs_client *prev = clp;
218
219 if (clp->cl_session != NULL) {
220 if (memcmp(clp->cl_session->sess_id.data,
221 sessionid->data,
222 NFS4_MAX_SESSIONID_LEN) == 0) {
223 /* Returns a held reference to clp */
224 return clp;
225 }
226 }
227 clp = nfs_find_client_next(prev);
228 nfs_put_client(prev);
229 } while (clp != NULL);
230
231 return NULL;
232}
233
Mike Sagera7989c32010-01-19 12:54:40 -0500234/*
235 * For each referring call triple, check the session's slot table for
236 * a match. If the slot is in use and the sequence numbers match, the
237 * client is still waiting for a response to the original request.
238 */
239static bool referring_call_exists(struct nfs_client *clp,
240 uint32_t nrclists,
241 struct referring_call_list *rclists)
242{
243 bool status = 0;
244 int i, j;
245 struct nfs4_session *session;
246 struct nfs4_slot_table *tbl;
247 struct referring_call_list *rclist;
248 struct referring_call *ref;
249
250 /*
251 * XXX When client trunking is implemented, this becomes
252 * a session lookup from within the loop
253 */
254 session = clp->cl_session;
255 tbl = &session->fc_slot_table;
256
257 for (i = 0; i < nrclists; i++) {
258 rclist = &rclists[i];
259 if (memcmp(session->sess_id.data,
260 rclist->rcl_sessionid.data,
261 NFS4_MAX_SESSIONID_LEN) != 0)
262 continue;
263
264 for (j = 0; j < rclist->rcl_nrefcalls; j++) {
265 ref = &rclist->rcl_refcalls[j];
266
267 dprintk("%s: sessionid %x:%x:%x:%x sequenceid %u "
268 "slotid %u\n", __func__,
269 ((u32 *)&rclist->rcl_sessionid.data)[0],
270 ((u32 *)&rclist->rcl_sessionid.data)[1],
271 ((u32 *)&rclist->rcl_sessionid.data)[2],
272 ((u32 *)&rclist->rcl_sessionid.data)[3],
273 ref->rc_sequenceid, ref->rc_slotid);
274
275 spin_lock(&tbl->slot_tbl_lock);
276 status = (test_bit(ref->rc_slotid, tbl->used_slots) &&
277 tbl->slots[ref->rc_slotid].seq_nr ==
278 ref->rc_sequenceid);
279 spin_unlock(&tbl->slot_tbl_lock);
280 if (status)
281 goto out;
282 }
283 }
284
285out:
286 return status;
287}
288
Andy Adamson9733f0d2010-01-22 12:03:08 -0500289__be32 nfs4_callback_sequence(struct cb_sequenceargs *args,
Benny Halevyd49433e2009-04-01 09:23:25 -0400290 struct cb_sequenceres *res)
291{
Ricardo Labiaga68f3f902009-04-01 09:23:35 -0400292 struct nfs_client *clp;
Andy Adamson9733f0d2010-01-22 12:03:08 -0500293 int i;
294 __be32 status;
Benny Halevyd49433e2009-04-01 09:23:25 -0400295
Ricardo Labiaga68f3f902009-04-01 09:23:35 -0400296 status = htonl(NFS4ERR_BADSESSION);
297 clp = find_client_with_session(args->csa_addr, 4, &args->csa_sessionid);
298 if (clp == NULL)
299 goto out;
300
Andy Adamsonb2f28bd2010-01-14 17:45:07 -0500301 status = validate_seqid(&clp->cl_session->bc_slot_table, args);
Ricardo Labiaga68f3f902009-04-01 09:23:35 -0400302 if (status)
303 goto out_putclient;
304
Mike Sager72ce2b32010-01-19 12:54:41 -0500305 /*
306 * Check for pending referring calls. If a match is found, a
307 * related callback was received before the response to the original
308 * call.
309 */
310 if (referring_call_exists(clp, args->csa_nrclists, args->csa_rclists)) {
311 status = htonl(NFS4ERR_DELAY);
312 goto out_putclient;
313 }
314
Benny Halevyd49433e2009-04-01 09:23:25 -0400315 memcpy(&res->csr_sessionid, &args->csa_sessionid,
316 sizeof(res->csr_sessionid));
317 res->csr_sequenceid = args->csa_sequenceid;
318 res->csr_slotid = args->csa_slotid;
319 res->csr_highestslotid = NFS41_BC_MAX_CALLBACKS - 1;
320 res->csr_target_highestslotid = NFS41_BC_MAX_CALLBACKS - 1;
321
Ricardo Labiaga68f3f902009-04-01 09:23:35 -0400322out_putclient:
323 nfs_put_client(clp);
324out:
Mike Sager72ce2b32010-01-19 12:54:41 -0500325 for (i = 0; i < args->csa_nrclists; i++)
326 kfree(args->csa_rclists[i].rcl_refcalls);
327 kfree(args->csa_rclists);
328
Andy Adamson49110962010-01-14 17:45:08 -0500329 if (status == htonl(NFS4ERR_RETRY_UNCACHED_REP))
330 res->csr_status = 0;
331 else
332 res->csr_status = status;
333 dprintk("%s: exit with status = %d res->csr_status %d\n", __func__,
334 ntohl(status), ntohl(res->csr_status));
335 return status;
Benny Halevyd49433e2009-04-01 09:23:25 -0400336}
337
Andy Adamson9733f0d2010-01-22 12:03:08 -0500338__be32 nfs4_callback_recallany(struct cb_recallanyargs *args, void *dummy)
Alexandros Batsakis31f09602009-12-05 13:27:02 -0500339{
340 struct nfs_client *clp;
Andy Adamson9733f0d2010-01-22 12:03:08 -0500341 __be32 status;
Alexandros Batsakis31f09602009-12-05 13:27:02 -0500342 fmode_t flags = 0;
343
344 status = htonl(NFS4ERR_OP_NOT_IN_SESSION);
345 clp = nfs_find_client(args->craa_addr, 4);
346 if (clp == NULL)
347 goto out;
348
349 dprintk("NFS: RECALL_ANY callback request from %s\n",
350 rpc_peeraddr2str(clp->cl_rpcclient, RPC_DISPLAY_ADDR));
351
352 if (test_bit(RCA4_TYPE_MASK_RDATA_DLG, (const unsigned long *)
353 &args->craa_type_mask))
354 flags = FMODE_READ;
355 if (test_bit(RCA4_TYPE_MASK_WDATA_DLG, (const unsigned long *)
356 &args->craa_type_mask))
357 flags |= FMODE_WRITE;
358
359 if (flags)
360 nfs_expire_all_delegation_types(clp, flags);
361 status = htonl(NFS4_OK);
362out:
363 dprintk("%s: exit with status = %d\n", __func__, ntohl(status));
364 return status;
365}
Andy Adamsonb9efa1b2010-01-20 16:06:27 -0500366
367/* Reduce the fore channel's max_slots to the target value */
Andy Adamson9733f0d2010-01-22 12:03:08 -0500368__be32 nfs4_callback_recallslot(struct cb_recallslotargs *args, void *dummy)
Andy Adamsonb9efa1b2010-01-20 16:06:27 -0500369{
370 struct nfs_client *clp;
371 struct nfs4_slot_table *fc_tbl;
Andy Adamson9733f0d2010-01-22 12:03:08 -0500372 __be32 status;
Andy Adamsonb9efa1b2010-01-20 16:06:27 -0500373
374 status = htonl(NFS4ERR_OP_NOT_IN_SESSION);
375 clp = nfs_find_client(args->crsa_addr, 4);
376 if (clp == NULL)
377 goto out;
378
379 dprintk("NFS: CB_RECALL_SLOT request from %s target max slots %d\n",
380 rpc_peeraddr2str(clp->cl_rpcclient, RPC_DISPLAY_ADDR),
381 args->crsa_target_max_slots);
382
383 fc_tbl = &clp->cl_session->fc_slot_table;
384
385 status = htonl(NFS4ERR_BAD_HIGH_SLOT);
Andy Adamsonbae0ac02010-01-21 14:19:16 -0500386 if (args->crsa_target_max_slots > fc_tbl->max_slots ||
Andy Adamsonb9efa1b2010-01-20 16:06:27 -0500387 args->crsa_target_max_slots < 1)
Andy Adamsonbae0ac02010-01-21 14:19:16 -0500388 goto out_putclient;
389
390 status = htonl(NFS4_OK);
391 if (args->crsa_target_max_slots == fc_tbl->max_slots)
392 goto out_putclient;
Andy Adamsonb9efa1b2010-01-20 16:06:27 -0500393
394 fc_tbl->target_max_slots = args->crsa_target_max_slots;
395 nfs41_handle_recall_slot(clp);
Andy Adamsonbae0ac02010-01-21 14:19:16 -0500396out_putclient:
Andy Adamsonb9efa1b2010-01-20 16:06:27 -0500397 nfs_put_client(clp); /* balance nfs_find_client */
398out:
399 dprintk("%s: exit with status = %d\n", __func__, ntohl(status));
400 return status;
401}
Benny Halevyd49433e2009-04-01 09:23:25 -0400402#endif /* CONFIG_NFS_V4_1 */