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>
12 #include "delegation.h"
16 #define NFSDBG_FACILITY NFSDBG_CALLBACK
19 __be32 nfs4_callback_getattr(struct cb_getattrargs *args, struct cb_getattrres *res)
21 struct nfs_client *clp;
22 struct nfs_delegation *delegation;
23 struct nfs_inode *nfsi;
26 res->bitmap[0] = res->bitmap[1] = 0;
27 res->status = htonl(NFS4ERR_BADHANDLE);
28 clp = nfs_find_client(args->addr, 4);
32 dprintk("NFS: GETATTR callback request from %s\n",
33 rpc_peeraddr2str(clp->cl_rpcclient, RPC_DISPLAY_ADDR));
35 inode = nfs_delegation_find_inode(clp, &args->fh);
39 down_read(&nfsi->rwsem);
40 delegation = nfsi->delegation;
41 if (delegation == NULL || (delegation->type & FMODE_WRITE) == 0)
43 res->size = i_size_read(inode);
44 res->change_attr = delegation->change_attr;
45 if (nfsi->npages != 0)
47 res->ctime = inode->i_ctime;
48 res->mtime = inode->i_mtime;
49 res->bitmap[0] = (FATTR4_WORD0_CHANGE|FATTR4_WORD0_SIZE) &
51 res->bitmap[1] = (FATTR4_WORD1_TIME_METADATA|FATTR4_WORD1_TIME_MODIFY) &
55 up_read(&nfsi->rwsem);
60 dprintk("%s: exit with status = %d\n", __func__, ntohl(res->status));
64 static int (*nfs_validate_delegation_stateid(struct nfs_client *clp))(struct nfs_delegation *, const nfs4_stateid *)
66 #if defined(CONFIG_NFS_V4_1)
67 if (clp->cl_minorversion > 0)
68 return nfs41_validate_delegation_stateid;
70 return nfs4_validate_delegation_stateid;
74 __be32 nfs4_callback_recall(struct cb_recallargs *args, void *dummy)
76 struct nfs_client *clp;
80 res = htonl(NFS4ERR_BADHANDLE);
81 clp = nfs_find_client(args->addr, 4);
85 dprintk("NFS: RECALL callback request from %s\n",
86 rpc_peeraddr2str(clp->cl_rpcclient, RPC_DISPLAY_ADDR));
89 struct nfs_client *prev = clp;
91 inode = nfs_delegation_find_inode(clp, &args->fh);
93 /* Set up a helper thread to actually return the delegation */
94 switch (nfs_async_inode_return_delegation(inode, &args->stateid,
95 nfs_validate_delegation_stateid(clp))) {
101 res = htonl(NFS4ERR_BAD_STATEID);
104 res = htonl(NFS4ERR_RESOURCE);
108 clp = nfs_find_client_next(prev);
109 nfs_put_client(prev);
110 } while (clp != NULL);
112 dprintk("%s: exit with status = %d\n", __func__, ntohl(res));
116 int nfs4_validate_delegation_stateid(struct nfs_delegation *delegation, const nfs4_stateid *stateid)
118 if (delegation == NULL || memcmp(delegation->stateid.data, stateid->data,
119 sizeof(delegation->stateid.data)) != 0)
124 #if defined(CONFIG_NFS_V4_1)
126 int nfs41_validate_delegation_stateid(struct nfs_delegation *delegation, const nfs4_stateid *stateid)
128 if (delegation == NULL)
131 /* seqid is 4-bytes long */
132 if (((u32 *) &stateid->data)[0] != 0)
134 if (memcmp(&delegation->stateid.data[4], &stateid->data[4],
135 sizeof(stateid->data)-4))
142 * Validate the sequenceID sent by the server.
143 * Return success if the sequenceID is one more than what we last saw on
144 * this slot, accounting for wraparound. Increments the slot's sequence.
146 * We don't yet implement a duplicate request cache, instead we set the
147 * back channel ca_maxresponsesize_cached to zero. This is OK for now
148 * since we only currently implement idempotent callbacks anyway.
150 * We have a single slot backchannel at this time, so we don't bother
151 * checking the used_slots bit array on the table. The lower layer guarantees
152 * a single outstanding callback request at a time.
155 validate_seqid(struct nfs4_slot_table *tbl, struct cb_sequenceargs * args)
157 struct nfs4_slot *slot;
159 dprintk("%s enter. slotid %d seqid %d\n",
160 __func__, args->csa_slotid, args->csa_sequenceid);
162 if (args->csa_slotid > NFS41_BC_MAX_CALLBACKS)
163 return htonl(NFS4ERR_BADSLOT);
165 slot = tbl->slots + args->csa_slotid;
166 dprintk("%s slot table seqid: %d\n", __func__, slot->seq_nr);
169 if (likely(args->csa_sequenceid == slot->seq_nr + 1)) {
171 return htonl(NFS4_OK);
175 if (args->csa_sequenceid == slot->seq_nr) {
176 dprintk("%s seqid %d is a replay\n",
177 __func__, args->csa_sequenceid);
178 /* Signal process_op to set this error on next op */
179 if (args->csa_cachethis == 0)
180 return htonl(NFS4ERR_RETRY_UNCACHED_REP);
182 /* The ca_maxresponsesize_cached is 0 with no DRC */
183 else if (args->csa_cachethis == 1)
184 return htonl(NFS4ERR_REP_TOO_BIG_TO_CACHE);
188 if (args->csa_sequenceid == 1 && (slot->seq_nr + 1) == 0) {
190 return htonl(NFS4_OK);
193 /* Misordered request */
194 return htonl(NFS4ERR_SEQ_MISORDERED);
198 * Returns a pointer to a held 'struct nfs_client' that matches the server's
199 * address, major version number, and session ID. It is the caller's
200 * responsibility to release the returned reference.
202 * Returns NULL if there are no connections with sessions, or if no session
203 * matches the one of interest.
205 static struct nfs_client *find_client_with_session(
206 const struct sockaddr *addr, u32 nfsversion,
207 struct nfs4_sessionid *sessionid)
209 struct nfs_client *clp;
211 clp = nfs_find_client(addr, 4);
216 struct nfs_client *prev = clp;
218 if (clp->cl_session != NULL) {
219 if (memcmp(clp->cl_session->sess_id.data,
221 NFS4_MAX_SESSIONID_LEN) == 0) {
222 /* Returns a held reference to clp */
226 clp = nfs_find_client_next(prev);
227 nfs_put_client(prev);
228 } while (clp != NULL);
234 * For each referring call triple, check the session's slot table for
235 * a match. If the slot is in use and the sequence numbers match, the
236 * client is still waiting for a response to the original request.
238 static bool referring_call_exists(struct nfs_client *clp,
240 struct referring_call_list *rclists)
244 struct nfs4_session *session;
245 struct nfs4_slot_table *tbl;
246 struct referring_call_list *rclist;
247 struct referring_call *ref;
250 * XXX When client trunking is implemented, this becomes
251 * a session lookup from within the loop
253 session = clp->cl_session;
254 tbl = &session->fc_slot_table;
256 for (i = 0; i < nrclists; i++) {
257 rclist = &rclists[i];
258 if (memcmp(session->sess_id.data,
259 rclist->rcl_sessionid.data,
260 NFS4_MAX_SESSIONID_LEN) != 0)
263 for (j = 0; j < rclist->rcl_nrefcalls; j++) {
264 ref = &rclist->rcl_refcalls[j];
266 dprintk("%s: sessionid %x:%x:%x:%x sequenceid %u "
267 "slotid %u\n", __func__,
268 ((u32 *)&rclist->rcl_sessionid.data)[0],
269 ((u32 *)&rclist->rcl_sessionid.data)[1],
270 ((u32 *)&rclist->rcl_sessionid.data)[2],
271 ((u32 *)&rclist->rcl_sessionid.data)[3],
272 ref->rc_sequenceid, ref->rc_slotid);
274 spin_lock(&tbl->slot_tbl_lock);
275 status = (test_bit(ref->rc_slotid, tbl->used_slots) &&
276 tbl->slots[ref->rc_slotid].seq_nr ==
278 spin_unlock(&tbl->slot_tbl_lock);
288 __be32 nfs4_callback_sequence(struct cb_sequenceargs *args,
289 struct cb_sequenceres *res)
291 struct nfs_client *clp;
295 status = htonl(NFS4ERR_BADSESSION);
296 clp = find_client_with_session(args->csa_addr, 4, &args->csa_sessionid);
300 status = validate_seqid(&clp->cl_session->bc_slot_table, args);
305 * Check for pending referring calls. If a match is found, a
306 * related callback was received before the response to the original
309 if (referring_call_exists(clp, args->csa_nrclists, args->csa_rclists)) {
310 status = htonl(NFS4ERR_DELAY);
314 memcpy(&res->csr_sessionid, &args->csa_sessionid,
315 sizeof(res->csr_sessionid));
316 res->csr_sequenceid = args->csa_sequenceid;
317 res->csr_slotid = args->csa_slotid;
318 res->csr_highestslotid = NFS41_BC_MAX_CALLBACKS - 1;
319 res->csr_target_highestslotid = NFS41_BC_MAX_CALLBACKS - 1;
324 for (i = 0; i < args->csa_nrclists; i++)
325 kfree(args->csa_rclists[i].rcl_refcalls);
326 kfree(args->csa_rclists);
328 if (status == htonl(NFS4ERR_RETRY_UNCACHED_REP))
331 res->csr_status = status;
332 dprintk("%s: exit with status = %d res->csr_status %d\n", __func__,
333 ntohl(status), ntohl(res->csr_status));
337 __be32 nfs4_callback_recallany(struct cb_recallanyargs *args, void *dummy)
339 struct nfs_client *clp;
343 status = htonl(NFS4ERR_OP_NOT_IN_SESSION);
344 clp = nfs_find_client(args->craa_addr, 4);
348 dprintk("NFS: RECALL_ANY callback request from %s\n",
349 rpc_peeraddr2str(clp->cl_rpcclient, RPC_DISPLAY_ADDR));
351 if (test_bit(RCA4_TYPE_MASK_RDATA_DLG, (const unsigned long *)
352 &args->craa_type_mask))
354 if (test_bit(RCA4_TYPE_MASK_WDATA_DLG, (const unsigned long *)
355 &args->craa_type_mask))
356 flags |= FMODE_WRITE;
359 nfs_expire_all_delegation_types(clp, flags);
360 status = htonl(NFS4_OK);
362 dprintk("%s: exit with status = %d\n", __func__, ntohl(status));
366 /* Reduce the fore channel's max_slots to the target value */
367 __be32 nfs4_callback_recallslot(struct cb_recallslotargs *args, void *dummy)
369 struct nfs_client *clp;
370 struct nfs4_slot_table *fc_tbl;
373 status = htonl(NFS4ERR_OP_NOT_IN_SESSION);
374 clp = nfs_find_client(args->crsa_addr, 4);
378 dprintk("NFS: CB_RECALL_SLOT request from %s target max slots %d\n",
379 rpc_peeraddr2str(clp->cl_rpcclient, RPC_DISPLAY_ADDR),
380 args->crsa_target_max_slots);
382 fc_tbl = &clp->cl_session->fc_slot_table;
384 status = htonl(NFS4ERR_BAD_HIGH_SLOT);
385 if (args->crsa_target_max_slots > fc_tbl->max_slots ||
386 args->crsa_target_max_slots < 1)
389 status = htonl(NFS4_OK);
390 if (args->crsa_target_max_slots == fc_tbl->max_slots)
393 fc_tbl->target_max_slots = args->crsa_target_max_slots;
394 nfs41_handle_recall_slot(clp);
396 nfs_put_client(clp); /* balance nfs_find_client */
398 dprintk("%s: exit with status = %d\n", __func__, ntohl(status));
401 #endif /* CONFIG_NFS_V4_1 */