]> bbs.cooldavid.org Git - net-next-2.6.git/blame - fs/nfs/callback_proc.c
include cleanup: Update gfp.h and slab.h includes to prepare for breaking implicit...
[net-next-2.6.git] / fs / nfs / callback_proc.c
CommitLineData
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
1da177e4 19
e6f684f6 20__be32 nfs4_callback_getattr(struct cb_getattrargs *args, struct cb_getattrres *res)
1da177e4 21{
adfa6f98 22 struct nfs_client *clp;
1da177e4
LT
23 struct nfs_delegation *delegation;
24 struct nfs_inode *nfsi;
25 struct inode *inode;
1d98fe67 26
1da177e4
LT
27 res->bitmap[0] = res->bitmap[1] = 0;
28 res->status = htonl(NFS4ERR_BADHANDLE);
ff052645 29 clp = nfs_find_client(args->addr, 4);
1da177e4
LT
30 if (clp == NULL)
31 goto out;
1d98fe67
CL
32
33 dprintk("NFS: GETATTR callback request from %s\n",
34 rpc_peeraddr2str(clp->cl_rpcclient, RPC_DISPLAY_ADDR));
35
1da177e4
LT
36 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);
beb2a5ec
TM
45 res->change_attr = delegation->change_attr;
46 if (nfsi->npages != 0)
47 res->change_attr++;
1da177e4
LT
48 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:
24c8dbbb 59 nfs_put_client(clp);
1da177e4 60out:
3110ff80 61 dprintk("%s: exit with status = %d\n", __func__, ntohl(res->status));
1da177e4
LT
62 return res->status;
63}
64
2597641d
AB
65static 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
e6f684f6 75__be32 nfs4_callback_recall(struct cb_recallargs *args, void *dummy)
1da177e4 76{
adfa6f98 77 struct nfs_client *clp;
1da177e4 78 struct inode *inode;
e6f684f6 79 __be32 res;
1da177e4
LT
80
81 res = htonl(NFS4ERR_BADHANDLE);
ff052645 82 clp = nfs_find_client(args->addr, 4);
1da177e4
LT
83 if (clp == NULL)
84 goto out;
1d98fe67
CL
85
86 dprintk("NFS: RECALL callback request from %s\n",
87 rpc_peeraddr2str(clp->cl_rpcclient, RPC_DISPLAY_ADDR));
88
3fbd67ad
TM
89 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 */
2597641d
AB
95 switch (nfs_async_inode_return_delegation(inode, &args->stateid,
96 nfs_validate_delegation_stateid(clp))) {
3fbd67ad
TM
97 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);
1da177e4 112out:
3110ff80 113 dprintk("%s: exit with status = %d\n", __func__, ntohl(res));
1da177e4
LT
114 return res;
115}
d49433e1 116
2597641d
AB
117int 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
d49433e1
BH
125#if defined(CONFIG_NFS_V4_1)
126
2597641d
AB
127int 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
68f3f901
RL
142/*
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.
146 *
4911096f
AA
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
68f3f901
RL
149 * 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 */
9733f0d9 155static __be32
b2f28bd7 156validate_seqid(struct nfs4_slot_table *tbl, struct cb_sequenceargs * args)
68f3f901
RL
157{
158 struct nfs4_slot *slot;
159
160 dprintk("%s enter. slotid %d seqid %d\n",
b2f28bd7 161 __func__, args->csa_slotid, args->csa_sequenceid);
68f3f901 162
b2f28bd7 163 if (args->csa_slotid > NFS41_BC_MAX_CALLBACKS)
68f3f901
RL
164 return htonl(NFS4ERR_BADSLOT);
165
b2f28bd7 166 slot = tbl->slots + args->csa_slotid;
68f3f901
RL
167 dprintk("%s slot table seqid: %d\n", __func__, slot->seq_nr);
168
169 /* Normal */
b2f28bd7 170 if (likely(args->csa_sequenceid == slot->seq_nr + 1)) {
68f3f901
RL
171 slot->seq_nr++;
172 return htonl(NFS4_OK);
173 }
174
175 /* Replay */
b2f28bd7 176 if (args->csa_sequenceid == slot->seq_nr) {
4911096f 177 dprintk("%s seqid %d is a replay\n",
b2f28bd7 178 __func__, args->csa_sequenceid);
4911096f
AA
179 /* 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);
68f3f901
RL
186 }
187
188 /* Wraparound */
b2f28bd7 189 if (args->csa_sequenceid == 1 && (slot->seq_nr + 1) == 0) {
68f3f901
RL
190 slot->seq_nr = 1;
191 return htonl(NFS4_OK);
192 }
193
194 /* Misordered request */
195 return htonl(NFS4ERR_SEQ_MISORDERED);
196}
197
963891ac
RL
198/*
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.
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
a7989c3e
MS
234/*
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
9733f0d9 289__be32 nfs4_callback_sequence(struct cb_sequenceargs *args,
d49433e1
BH
290 struct cb_sequenceres *res)
291{
68f3f901 292 struct nfs_client *clp;
9733f0d9
AA
293 int i;
294 __be32 status;
d49433e1 295
68f3f901
RL
296 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
b2f28bd7 301 status = validate_seqid(&clp->cl_session->bc_slot_table, args);
68f3f901
RL
302 if (status)
303 goto out_putclient;
304
72ce2b3c
MS
305 /*
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
d49433e1
BH
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;
321
68f3f901
RL
322out_putclient:
323 nfs_put_client(clp);
324out:
72ce2b3c
MS
325 for (i = 0; i < args->csa_nrclists; i++)
326 kfree(args->csa_rclists[i].rcl_refcalls);
327 kfree(args->csa_rclists);
328
4911096f
AA
329 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;
d49433e1
BH
336}
337
9733f0d9 338__be32 nfs4_callback_recallany(struct cb_recallanyargs *args, void *dummy)
31f09607
AB
339{
340 struct nfs_client *clp;
9733f0d9 341 __be32 status;
31f09607
AB
342 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}
b9efa1b2
AA
366
367/* Reduce the fore channel's max_slots to the target value */
9733f0d9 368__be32 nfs4_callback_recallslot(struct cb_recallslotargs *args, void *dummy)
b9efa1b2
AA
369{
370 struct nfs_client *clp;
371 struct nfs4_slot_table *fc_tbl;
9733f0d9 372 __be32 status;
b9efa1b2
AA
373
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);
bae0ac0e 386 if (args->crsa_target_max_slots > fc_tbl->max_slots ||
b9efa1b2 387 args->crsa_target_max_slots < 1)
bae0ac0e
AA
388 goto out_putclient;
389
390 status = htonl(NFS4_OK);
391 if (args->crsa_target_max_slots == fc_tbl->max_slots)
392 goto out_putclient;
b9efa1b2
AA
393
394 fc_tbl->target_max_slots = args->crsa_target_max_slots;
395 nfs41_handle_recall_slot(clp);
bae0ac0e 396out_putclient:
b9efa1b2
AA
397 nfs_put_client(clp); /* balance nfs_find_client */
398out:
399 dprintk("%s: exit with status = %d\n", __func__, ntohl(status));
400 return status;
401}
d49433e1 402#endif /* CONFIG_NFS_V4_1 */