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
,
21 struct cb_getattrres
*res
,
22 struct cb_process_state
*cps
)
24 struct nfs_delegation
*delegation
;
25 struct nfs_inode
*nfsi
;
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 */
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
);
43 delegation
= rcu_dereference(nfsi
->delegation
);
44 if (delegation
== NULL
|| (delegation
->type
& FMODE_WRITE
) == 0)
46 res
->size
= i_size_read(inode
);
47 res
->change_attr
= delegation
->change_attr
;
48 if (nfsi
->npages
!= 0)
50 res
->ctime
= inode
->i_ctime
;
51 res
->mtime
= inode
->i_mtime
;
52 res
->bitmap
[0] = (FATTR4_WORD0_CHANGE
|FATTR4_WORD0_SIZE
) &
54 res
->bitmap
[1] = (FATTR4_WORD1_TIME_METADATA
|FATTR4_WORD1_TIME_MODIFY
) &
61 dprintk("%s: exit with status = %d\n", __func__
, ntohl(res
->status
));
65 __be32
nfs4_callback_recall(struct cb_recallargs
*args
, void *dummy
,
66 struct cb_process_state
*cps
)
71 res
= htonl(NFS4ERR_OP_NOT_IN_SESSION
);
72 if (!cps
->clp
) /* Always set for v4.0. Set in cb_sequence for v4.1 */
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
);
82 /* Set up a helper thread to actually return the delegation */
83 switch (nfs_async_inode_return_delegation(inode
, &args
->stateid
)) {
89 res
= htonl(NFS4ERR_BAD_STATEID
);
92 res
= htonl(NFS4ERR_RESOURCE
);
96 dprintk("%s: exit with status = %d\n", __func__
, ntohl(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)
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
)
115 if (stateid
->stateid
.seqid
!= 0)
117 if (memcmp(&delegation
->stateid
.stateid
.other
,
118 &stateid
->stateid
.other
,
119 NFS4_STATEID_OTHER_SIZE
))
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.
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
);
153 if (likely(args
->csa_sequenceid
== slot
->seq_nr
+ 1)) {
155 return htonl(NFS4_OK
);
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
);
172 if (args
->csa_sequenceid
== 1 && (slot
->seq_nr
+ 1) == 0) {
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
,
188 struct referring_call_list
*rclists
)
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)
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
==
226 spin_unlock(&tbl
->slot_tbl_lock
);
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
;
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
))
251 clp
= nfs4_find_client_sessionid(args
->csa_addr
,
252 &args
->csa_sessionid
, 1);
256 /* state manager is resetting the session */
257 if (test_bit(NFS4_SESSION_DRAINING
, &clp
->cl_session
->session_state
)) {
258 status
= NFS4ERR_DELAY
;
262 status
= validate_seqid(&clp
->cl_session
->bc_slot_table
, args
);
267 * Check for pending referring calls. If a match is found, a
268 * related callback was received before the response to the original
271 if (referring_call_exists(clp
, args
->csa_nrclists
, args
->csa_rclists
)) {
272 status
= htonl(NFS4ERR_DELAY
);
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 */
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
;
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
));
301 __be32
nfs4_callback_recallany(struct cb_recallanyargs
*args
, void *dummy
,
302 struct cb_process_state
*cps
)
307 status
= htonl(NFS4ERR_OP_NOT_IN_SESSION
);
308 if (!cps
->clp
) /* set in cb_sequence */
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
))
317 if (test_bit(RCA4_TYPE_MASK_WDATA_DLG
, (const unsigned long *)
318 &args
->craa_type_mask
))
319 flags
|= FMODE_WRITE
;
322 nfs_expire_all_delegation_types(cps
->clp
, flags
);
323 status
= htonl(NFS4_OK
);
325 dprintk("%s: exit with status = %d\n", __func__
, ntohl(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
;
336 status
= htonl(NFS4ERR_OP_NOT_IN_SESSION
);
337 if (!cps
->clp
) /* set in cb_sequence */
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)
351 status
= htonl(NFS4_OK
);
352 if (args
->crsa_target_max_slots
== fc_tbl
->max_slots
)
355 fc_tbl
->target_max_slots
= args
->crsa_target_max_slots
;
356 nfs41_handle_recall_slot(cps
->clp
);
358 dprintk("%s: exit with status = %d\n", __func__
, ntohl(status
));
361 #endif /* CONFIG_NFS_V4_1 */