2 * linux/fs/nfs/callback_proc.c
4 * Copyright (C) 2004 Trond Myklebust
6 * NFSv4 callback procedures
8 #include <linux/nfs4.h>
9 #include <linux/nfs_fs.h>
10 #include <linux/slab.h>
13 #include "delegation.h"
17 #define NFSDBG_FACILITY NFSDBG_CALLBACK
20 __be32 nfs4_callback_getattr(struct cb_getattrargs *args, struct cb_getattrres *res)
22 struct nfs_client *clp;
23 struct nfs_delegation *delegation;
24 struct nfs_inode *nfsi;
27 res->bitmap[0] = res->bitmap[1] = 0;
28 res->status = htonl(NFS4ERR_BADHANDLE);
29 clp = nfs_find_client(args->addr, 4);
33 dprintk("NFS: GETATTR callback request from %s\n",
34 rpc_peeraddr2str(clp->cl_rpcclient, RPC_DISPLAY_ADDR));
36 inode = nfs_delegation_find_inode(clp, &args->fh);
40 down_read(&nfsi->rwsem);
41 delegation = nfsi->delegation;
42 if (delegation == NULL || (delegation->type & FMODE_WRITE) == 0)
44 res->size = i_size_read(inode);
45 res->change_attr = delegation->change_attr;
46 if (nfsi->npages != 0)
48 res->ctime = inode->i_ctime;
49 res->mtime = inode->i_mtime;
50 res->bitmap[0] = (FATTR4_WORD0_CHANGE|FATTR4_WORD0_SIZE) &
52 res->bitmap[1] = (FATTR4_WORD1_TIME_METADATA|FATTR4_WORD1_TIME_MODIFY) &
56 up_read(&nfsi->rwsem);
61 dprintk("%s: exit with status = %d\n", __func__, ntohl(res->status));
65 static int (*nfs_validate_delegation_stateid(struct nfs_client *clp))(struct nfs_delegation *, const nfs4_stateid *)
67 #if defined(CONFIG_NFS_V4_1)
68 if (clp->cl_minorversion > 0)
69 return nfs41_validate_delegation_stateid;
71 return nfs4_validate_delegation_stateid;
75 __be32 nfs4_callback_recall(struct cb_recallargs *args, void *dummy)
77 struct nfs_client *clp;
81 res = htonl(NFS4ERR_BADHANDLE);
82 clp = nfs_find_client(args->addr, 4);
86 dprintk("NFS: RECALL callback request from %s\n",
87 rpc_peeraddr2str(clp->cl_rpcclient, RPC_DISPLAY_ADDR));
90 struct nfs_client *prev = clp;
92 inode = nfs_delegation_find_inode(clp, &args->fh);
94 /* Set up a helper thread to actually return the delegation */
95 switch (nfs_async_inode_return_delegation(inode, &args->stateid,
96 nfs_validate_delegation_stateid(clp))) {
102 res = htonl(NFS4ERR_BAD_STATEID);
105 res = htonl(NFS4ERR_RESOURCE);
109 clp = nfs_find_client_next(prev);
110 nfs_put_client(prev);
111 } while (clp != NULL);
113 dprintk("%s: exit with status = %d\n", __func__, ntohl(res));
117 int nfs4_validate_delegation_stateid(struct nfs_delegation *delegation, const nfs4_stateid *stateid)
119 if (delegation == NULL || memcmp(delegation->stateid.data, stateid->data,
120 sizeof(delegation->stateid.data)) != 0)
125 #if defined(CONFIG_NFS_V4_1)
127 int nfs41_validate_delegation_stateid(struct nfs_delegation *delegation, const nfs4_stateid *stateid)
129 if (delegation == NULL)
132 /* seqid is 4-bytes long */
133 if (((u32 *) &stateid->data)[0] != 0)
135 if (memcmp(&delegation->stateid.data[4], &stateid->data[4],
136 sizeof(stateid->data)-4))
143 * 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.
147 * 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
149 * since we only currently implement idempotent callbacks anyway.
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.
156 validate_seqid(struct nfs4_slot_table *tbl, struct cb_sequenceargs * args)
158 struct nfs4_slot *slot;
160 dprintk("%s enter. slotid %d seqid %d\n",
161 __func__, args->csa_slotid, args->csa_sequenceid);
163 if (args->csa_slotid > NFS41_BC_MAX_CALLBACKS)
164 return htonl(NFS4ERR_BADSLOT);
166 slot = tbl->slots + args->csa_slotid;
167 dprintk("%s slot table seqid: %d\n", __func__, slot->seq_nr);
170 if (likely(args->csa_sequenceid == slot->seq_nr + 1)) {
172 return htonl(NFS4_OK);
176 if (args->csa_sequenceid == slot->seq_nr) {
177 dprintk("%s seqid %d is a replay\n",
178 __func__, args->csa_sequenceid);
179 /* Signal process_op to set this error on next op */
180 if (args->csa_cachethis == 0)
181 return htonl(NFS4ERR_RETRY_UNCACHED_REP);
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);
189 if (args->csa_sequenceid == 1 && (slot->seq_nr + 1) == 0) {
191 return htonl(NFS4_OK);
194 /* Misordered request */
195 return htonl(NFS4ERR_SEQ_MISORDERED);
199 * 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.
203 * Returns NULL if there are no connections with sessions, or if no session
204 * matches the one of interest.
206 static struct nfs_client *find_client_with_session(
207 const struct sockaddr *addr, u32 nfsversion,
208 struct nfs4_sessionid *sessionid)
210 struct nfs_client *clp;
212 clp = nfs_find_client(addr, 4);
217 struct nfs_client *prev = clp;
219 if (clp->cl_session != NULL) {
220 if (memcmp(clp->cl_session->sess_id.data,
222 NFS4_MAX_SESSIONID_LEN) == 0) {
223 /* Returns a held reference to clp */
227 clp = nfs_find_client_next(prev);
228 nfs_put_client(prev);
229 } while (clp != NULL);
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.
239 static bool referring_call_exists(struct nfs_client *clp,
241 struct referring_call_list *rclists)
245 struct nfs4_session *session;
246 struct nfs4_slot_table *tbl;
247 struct referring_call_list *rclist;
248 struct referring_call *ref;
251 * XXX When client trunking is implemented, this becomes
252 * a session lookup from within the loop
254 session = clp->cl_session;
255 tbl = &session->fc_slot_table;
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)
264 for (j = 0; j < rclist->rcl_nrefcalls; j++) {
265 ref = &rclist->rcl_refcalls[j];
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);
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 ==
279 spin_unlock(&tbl->slot_tbl_lock);
289 __be32 nfs4_callback_sequence(struct cb_sequenceargs *args,
290 struct cb_sequenceres *res)
292 struct nfs_client *clp;
296 status = htonl(NFS4ERR_BADSESSION);
297 clp = find_client_with_session(args->csa_addr, 4, &args->csa_sessionid);
301 status = validate_seqid(&clp->cl_session->bc_slot_table, args);
306 * Check for pending referring calls. If a match is found, a
307 * related callback was received before the response to the original
310 if (referring_call_exists(clp, args->csa_nrclists, args->csa_rclists)) {
311 status = htonl(NFS4ERR_DELAY);
315 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;
325 for (i = 0; i < args->csa_nrclists; i++)
326 kfree(args->csa_rclists[i].rcl_refcalls);
327 kfree(args->csa_rclists);
329 if (status == htonl(NFS4ERR_RETRY_UNCACHED_REP))
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));
338 __be32 nfs4_callback_recallany(struct cb_recallanyargs *args, void *dummy)
340 struct nfs_client *clp;
344 status = htonl(NFS4ERR_OP_NOT_IN_SESSION);
345 clp = nfs_find_client(args->craa_addr, 4);
349 dprintk("NFS: RECALL_ANY callback request from %s\n",
350 rpc_peeraddr2str(clp->cl_rpcclient, RPC_DISPLAY_ADDR));
352 if (test_bit(RCA4_TYPE_MASK_RDATA_DLG, (const unsigned long *)
353 &args->craa_type_mask))
355 if (test_bit(RCA4_TYPE_MASK_WDATA_DLG, (const unsigned long *)
356 &args->craa_type_mask))
357 flags |= FMODE_WRITE;
360 nfs_expire_all_delegation_types(clp, flags);
361 status = htonl(NFS4_OK);
363 dprintk("%s: exit with status = %d\n", __func__, ntohl(status));
367 /* Reduce the fore channel's max_slots to the target value */
368 __be32 nfs4_callback_recallslot(struct cb_recallslotargs *args, void *dummy)
370 struct nfs_client *clp;
371 struct nfs4_slot_table *fc_tbl;
374 status = htonl(NFS4ERR_OP_NOT_IN_SESSION);
375 clp = nfs_find_client(args->crsa_addr, 4);
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);
383 fc_tbl = &clp->cl_session->fc_slot_table;
385 status = htonl(NFS4ERR_BAD_HIGH_SLOT);
386 if (args->crsa_target_max_slots > fc_tbl->max_slots ||
387 args->crsa_target_max_slots < 1)
390 status = htonl(NFS4_OK);
391 if (args->crsa_target_max_slots == fc_tbl->max_slots)
394 fc_tbl->target_max_slots = args->crsa_target_max_slots;
395 nfs41_handle_recall_slot(clp);
397 nfs_put_client(clp); /* balance nfs_find_client */
399 dprintk("%s: exit with status = %d\n", __func__, ntohl(status));
402 #endif /* CONFIG_NFS_V4_1 */