]>
Commit | Line | Data |
---|---|---|
1da177e4 LT |
1 | /* |
2 | * linux/fs/nfs/callback_proc.c | |
3 | * | |
4 | * Copyright (C) 2004 Trond Myklebust | |
5 | * | |
6 | * NFSv4 callback procedures | |
7 | */ | |
1da177e4 LT |
8 | #include <linux/nfs4.h> |
9 | #include <linux/nfs_fs.h> | |
5a0e3ad6 | 10 | #include <linux/slab.h> |
4ce79717 | 11 | #include "nfs4_fs.h" |
1da177e4 LT |
12 | #include "callback.h" |
13 | #include "delegation.h" | |
24c8dbbb | 14 | #include "internal.h" |
1da177e4 | 15 | |
1d98fe67 | 16 | #ifdef NFS_DEBUG |
1da177e4 | 17 | #define NFSDBG_FACILITY NFSDBG_CALLBACK |
1d98fe67 | 18 | #endif |
c36fca52 AA |
19 | |
20 | __be32 nfs4_callback_getattr(struct cb_getattrargs *args, | |
21 | struct cb_getattrres *res, | |
22 | struct cb_process_state *cps) | |
1da177e4 | 23 | { |
1da177e4 LT |
24 | struct nfs_delegation *delegation; |
25 | struct nfs_inode *nfsi; | |
26 | struct inode *inode; | |
1d98fe67 | 27 | |
c36fca52 AA |
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 */ | |
30 | goto out; | |
31 | ||
1da177e4 LT |
32 | res->bitmap[0] = res->bitmap[1] = 0; |
33 | res->status = htonl(NFS4ERR_BADHANDLE); | |
1d98fe67 CL |
34 | |
35 | dprintk("NFS: GETATTR callback request from %s\n", | |
c36fca52 | 36 | rpc_peeraddr2str(cps->clp->cl_rpcclient, RPC_DISPLAY_ADDR)); |
1d98fe67 | 37 | |
c36fca52 | 38 | inode = nfs_delegation_find_inode(cps->clp, &args->fh); |
1da177e4 | 39 | if (inode == NULL) |
c36fca52 | 40 | goto out; |
1da177e4 | 41 | nfsi = NFS_I(inode); |
761fe93c TM |
42 | rcu_read_lock(); |
43 | delegation = rcu_dereference(nfsi->delegation); | |
1da177e4 LT |
44 | if (delegation == NULL || (delegation->type & FMODE_WRITE) == 0) |
45 | goto out_iput; | |
46 | res->size = i_size_read(inode); | |
beb2a5ec TM |
47 | res->change_attr = delegation->change_attr; |
48 | if (nfsi->npages != 0) | |
49 | res->change_attr++; | |
1da177e4 LT |
50 | 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; | |
57 | out_iput: | |
761fe93c | 58 | rcu_read_unlock(); |
1da177e4 | 59 | iput(inode); |
1da177e4 | 60 | out: |
3110ff80 | 61 | dprintk("%s: exit with status = %d\n", __func__, ntohl(res->status)); |
1da177e4 LT |
62 | return res->status; |
63 | } | |
64 | ||
c36fca52 AA |
65 | __be32 nfs4_callback_recall(struct cb_recallargs *args, void *dummy, |
66 | struct cb_process_state *cps) | |
1da177e4 | 67 | { |
1da177e4 | 68 | struct inode *inode; |
e6f684f6 | 69 | __be32 res; |
1da177e4 | 70 | |
c36fca52 AA |
71 | res = htonl(NFS4ERR_OP_NOT_IN_SESSION); |
72 | if (!cps->clp) /* Always set for v4.0. Set in cb_sequence for v4.1 */ | |
1da177e4 | 73 | goto out; |
1d98fe67 CL |
74 | |
75 | dprintk("NFS: RECALL callback request from %s\n", | |
c36fca52 AA |
76 | rpc_peeraddr2str(cps->clp->cl_rpcclient, RPC_DISPLAY_ADDR)); |
77 | ||
78 | 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); | |
1da177e4 | 95 | out: |
3110ff80 | 96 | dprintk("%s: exit with status = %d\n", __func__, ntohl(res)); |
1da177e4 LT |
97 | return res; |
98 | } | |
d49433e1 | 99 | |
2597641d AB |
100 | int 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 | ||
d49433e1 BH |
108 | #if defined(CONFIG_NFS_V4_1) |
109 | ||
2597641d AB |
110 | int nfs41_validate_delegation_stateid(struct nfs_delegation *delegation, const nfs4_stateid *stateid) |
111 | { | |
112 | if (delegation == NULL) | |
113 | return 0; | |
114 | ||
94499252 | 115 | if (stateid->stateid.seqid != 0) |
2597641d | 116 | return 0; |
94499252 AB |
117 | if (memcmp(&delegation->stateid.stateid.other, |
118 | &stateid->stateid.other, | |
119 | NFS4_STATEID_OTHER_SIZE)) | |
2597641d AB |
120 | return 0; |
121 | ||
122 | return 1; | |
123 | } | |
124 | ||
68f3f901 RL |
125 | /* |
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. | |
129 | * | |
4911096f AA |
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 | |
68f3f901 RL |
132 | * since we only currently implement idempotent callbacks anyway. |
133 | * | |
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. | |
137 | */ | |
9733f0d9 | 138 | static __be32 |
b2f28bd7 | 139 | validate_seqid(struct nfs4_slot_table *tbl, struct cb_sequenceargs * args) |
68f3f901 RL |
140 | { |
141 | struct nfs4_slot *slot; | |
142 | ||
143 | dprintk("%s enter. slotid %d seqid %d\n", | |
b2f28bd7 | 144 | __func__, args->csa_slotid, args->csa_sequenceid); |
68f3f901 | 145 | |
b2f28bd7 | 146 | if (args->csa_slotid > NFS41_BC_MAX_CALLBACKS) |
68f3f901 RL |
147 | return htonl(NFS4ERR_BADSLOT); |
148 | ||
b2f28bd7 | 149 | slot = tbl->slots + args->csa_slotid; |
68f3f901 RL |
150 | dprintk("%s slot table seqid: %d\n", __func__, slot->seq_nr); |
151 | ||
152 | /* Normal */ | |
b2f28bd7 | 153 | if (likely(args->csa_sequenceid == slot->seq_nr + 1)) { |
68f3f901 RL |
154 | slot->seq_nr++; |
155 | return htonl(NFS4_OK); | |
156 | } | |
157 | ||
158 | /* Replay */ | |
b2f28bd7 | 159 | if (args->csa_sequenceid == slot->seq_nr) { |
4911096f | 160 | dprintk("%s seqid %d is a replay\n", |
b2f28bd7 | 161 | __func__, args->csa_sequenceid); |
4911096f AA |
162 | /* Signal process_op to set this error on next op */ |
163 | if (args->csa_cachethis == 0) | |
164 | return htonl(NFS4ERR_RETRY_UNCACHED_REP); | |
165 | ||
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); | |
68f3f901 RL |
169 | } |
170 | ||
171 | /* Wraparound */ | |
b2f28bd7 | 172 | if (args->csa_sequenceid == 1 && (slot->seq_nr + 1) == 0) { |
68f3f901 RL |
173 | slot->seq_nr = 1; |
174 | return htonl(NFS4_OK); | |
175 | } | |
176 | ||
177 | /* Misordered request */ | |
178 | return htonl(NFS4ERR_SEQ_MISORDERED); | |
179 | } | |
180 | ||
a7989c3e MS |
181 | /* |
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. | |
185 | */ | |
186 | static bool referring_call_exists(struct nfs_client *clp, | |
187 | uint32_t nrclists, | |
188 | struct referring_call_list *rclists) | |
189 | { | |
190 | bool status = 0; | |
191 | int i, j; | |
192 | struct nfs4_session *session; | |
193 | struct nfs4_slot_table *tbl; | |
194 | struct referring_call_list *rclist; | |
195 | struct referring_call *ref; | |
196 | ||
197 | /* | |
198 | * XXX When client trunking is implemented, this becomes | |
199 | * a session lookup from within the loop | |
200 | */ | |
201 | session = clp->cl_session; | |
202 | tbl = &session->fc_slot_table; | |
203 | ||
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) | |
209 | continue; | |
210 | ||
211 | for (j = 0; j < rclist->rcl_nrefcalls; j++) { | |
212 | ref = &rclist->rcl_refcalls[j]; | |
213 | ||
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); | |
221 | ||
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 == | |
225 | ref->rc_sequenceid); | |
226 | spin_unlock(&tbl->slot_tbl_lock); | |
227 | if (status) | |
228 | goto out; | |
229 | } | |
230 | } | |
231 | ||
232 | out: | |
233 | return status; | |
234 | } | |
235 | ||
9733f0d9 | 236 | __be32 nfs4_callback_sequence(struct cb_sequenceargs *args, |
c36fca52 AA |
237 | struct cb_sequenceres *res, |
238 | struct cb_process_state *cps) | |
d49433e1 | 239 | { |
68f3f901 | 240 | struct nfs_client *clp; |
9733f0d9 AA |
241 | int i; |
242 | __be32 status; | |
d49433e1 | 243 | |
c36fca52 AA |
244 | cps->clp = NULL; |
245 | ||
68f3f901 | 246 | status = htonl(NFS4ERR_BADSESSION); |
c36fca52 AA |
247 | /* Incoming session must match the callback session */ |
248 | if (memcmp(&args->csa_sessionid, cps->svc_sid, NFS4_MAX_SESSIONID_LEN)) | |
249 | goto out; | |
250 | ||
251 | clp = nfs4_find_client_sessionid(args->csa_addr, | |
252 | &args->csa_sessionid, 1); | |
68f3f901 RL |
253 | if (clp == NULL) |
254 | goto out; | |
255 | ||
b2f28bd7 | 256 | status = validate_seqid(&clp->cl_session->bc_slot_table, args); |
68f3f901 | 257 | if (status) |
c36fca52 | 258 | goto out; |
68f3f901 | 259 | |
72ce2b3c MS |
260 | /* |
261 | * Check for pending referring calls. If a match is found, a | |
262 | * related callback was received before the response to the original | |
263 | * call. | |
264 | */ | |
265 | if (referring_call_exists(clp, args->csa_nrclists, args->csa_rclists)) { | |
266 | status = htonl(NFS4ERR_DELAY); | |
c36fca52 | 267 | goto out; |
72ce2b3c MS |
268 | } |
269 | ||
d49433e1 BH |
270 | memcpy(&res->csr_sessionid, &args->csa_sessionid, |
271 | sizeof(res->csr_sessionid)); | |
272 | res->csr_sequenceid = args->csa_sequenceid; | |
273 | res->csr_slotid = args->csa_slotid; | |
274 | res->csr_highestslotid = NFS41_BC_MAX_CALLBACKS - 1; | |
275 | res->csr_target_highestslotid = NFS41_BC_MAX_CALLBACKS - 1; | |
c36fca52 | 276 | cps->clp = clp; /* put in nfs4_callback_compound */ |
d49433e1 | 277 | |
68f3f901 | 278 | out: |
72ce2b3c MS |
279 | for (i = 0; i < args->csa_nrclists; i++) |
280 | kfree(args->csa_rclists[i].rcl_refcalls); | |
281 | kfree(args->csa_rclists); | |
282 | ||
c36fca52 AA |
283 | if (status == htonl(NFS4ERR_RETRY_UNCACHED_REP)) { |
284 | cps->drc_status = status; | |
285 | status = 0; | |
286 | } else | |
4911096f | 287 | res->csr_status = status; |
c36fca52 | 288 | |
4911096f AA |
289 | dprintk("%s: exit with status = %d res->csr_status %d\n", __func__, |
290 | ntohl(status), ntohl(res->csr_status)); | |
291 | return status; | |
d49433e1 BH |
292 | } |
293 | ||
c36fca52 AA |
294 | __be32 nfs4_callback_recallany(struct cb_recallanyargs *args, void *dummy, |
295 | struct cb_process_state *cps) | |
31f09607 | 296 | { |
9733f0d9 | 297 | __be32 status; |
31f09607 AB |
298 | fmode_t flags = 0; |
299 | ||
300 | status = htonl(NFS4ERR_OP_NOT_IN_SESSION); | |
c36fca52 | 301 | if (!cps->clp) /* set in cb_sequence */ |
31f09607 AB |
302 | goto out; |
303 | ||
304 | dprintk("NFS: RECALL_ANY callback request from %s\n", | |
c36fca52 | 305 | rpc_peeraddr2str(cps->clp->cl_rpcclient, RPC_DISPLAY_ADDR)); |
31f09607 AB |
306 | |
307 | if (test_bit(RCA4_TYPE_MASK_RDATA_DLG, (const unsigned long *) | |
308 | &args->craa_type_mask)) | |
309 | flags = FMODE_READ; | |
310 | if (test_bit(RCA4_TYPE_MASK_WDATA_DLG, (const unsigned long *) | |
311 | &args->craa_type_mask)) | |
312 | flags |= FMODE_WRITE; | |
313 | ||
314 | if (flags) | |
c36fca52 | 315 | nfs_expire_all_delegation_types(cps->clp, flags); |
31f09607 AB |
316 | status = htonl(NFS4_OK); |
317 | out: | |
318 | dprintk("%s: exit with status = %d\n", __func__, ntohl(status)); | |
319 | return status; | |
320 | } | |
b9efa1b2 AA |
321 | |
322 | /* Reduce the fore channel's max_slots to the target value */ | |
c36fca52 AA |
323 | __be32 nfs4_callback_recallslot(struct cb_recallslotargs *args, void *dummy, |
324 | struct cb_process_state *cps) | |
b9efa1b2 | 325 | { |
b9efa1b2 | 326 | struct nfs4_slot_table *fc_tbl; |
9733f0d9 | 327 | __be32 status; |
b9efa1b2 AA |
328 | |
329 | status = htonl(NFS4ERR_OP_NOT_IN_SESSION); | |
c36fca52 | 330 | if (!cps->clp) /* set in cb_sequence */ |
b9efa1b2 AA |
331 | goto out; |
332 | ||
333 | dprintk("NFS: CB_RECALL_SLOT request from %s target max slots %d\n", | |
c36fca52 | 334 | rpc_peeraddr2str(cps->clp->cl_rpcclient, RPC_DISPLAY_ADDR), |
b9efa1b2 AA |
335 | args->crsa_target_max_slots); |
336 | ||
c36fca52 | 337 | fc_tbl = &cps->clp->cl_session->fc_slot_table; |
b9efa1b2 AA |
338 | |
339 | status = htonl(NFS4ERR_BAD_HIGH_SLOT); | |
bae0ac0e | 340 | if (args->crsa_target_max_slots > fc_tbl->max_slots || |
b9efa1b2 | 341 | args->crsa_target_max_slots < 1) |
c36fca52 | 342 | goto out; |
bae0ac0e AA |
343 | |
344 | status = htonl(NFS4_OK); | |
345 | if (args->crsa_target_max_slots == fc_tbl->max_slots) | |
c36fca52 | 346 | goto out; |
b9efa1b2 AA |
347 | |
348 | fc_tbl->target_max_slots = args->crsa_target_max_slots; | |
c36fca52 | 349 | nfs41_handle_recall_slot(cps->clp); |
b9efa1b2 AA |
350 | out: |
351 | dprintk("%s: exit with status = %d\n", __func__, ntohl(status)); | |
352 | return status; | |
353 | } | |
d49433e1 | 354 | #endif /* CONFIG_NFS_V4_1 */ |