GUI: Fix Tomato RAF theme for all builds. Compilation typo.
[tomato.git] / release / src-rt-6.x.4708 / linux / linux-2.6.36 / fs / nfs / callback_proc.c
blob69497eec37f11936abf4569d8820e54e576e4f20
1 /*
2 * linux/fs/nfs/callback_proc.c
4 * Copyright (C) 2004 Trond Myklebust
6 * NFSv4 callback procedures
7 */
8 #include <linux/nfs4.h>
9 #include <linux/nfs_fs.h>
10 #include <linux/slab.h>
11 #include "nfs4_fs.h"
12 #include "callback.h"
13 #include "delegation.h"
14 #include "internal.h"
16 #ifdef NFS_DEBUG
17 #define NFSDBG_FACILITY NFSDBG_CALLBACK
18 #endif
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;
25 struct inode *inode;
27 res->bitmap[0] = res->bitmap[1] = 0;
28 res->status = htonl(NFS4ERR_BADHANDLE);
29 clp = nfs_find_client(args->addr, 4);
30 if (clp == NULL)
31 goto out;
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);
37 if (inode == NULL)
38 goto out_putclient;
39 nfsi = NFS_I(inode);
40 rcu_read_lock();
41 delegation = rcu_dereference(nfsi->delegation);
42 if (delegation == NULL || (delegation->type & FMODE_WRITE) == 0)
43 goto out_iput;
44 res->size = i_size_read(inode);
45 res->change_attr = delegation->change_attr;
46 if (nfsi->npages != 0)
47 res->change_attr++;
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;
55 out_iput:
56 rcu_read_unlock();
57 iput(inode);
58 out_putclient:
59 nfs_put_client(clp);
60 out:
61 dprintk("%s: exit with status = %d\n", __func__, ntohl(res->status));
62 return res->status;
65 __be32 nfs4_callback_recall(struct cb_recallargs *args, void *dummy)
67 struct nfs_client *clp;
68 struct inode *inode;
69 __be32 res;
71 res = htonl(NFS4ERR_BADHANDLE);
72 clp = nfs_find_client(args->addr, 4);
73 if (clp == NULL)
74 goto out;
76 dprintk("NFS: RECALL callback request from %s\n",
77 rpc_peeraddr2str(clp->cl_rpcclient, RPC_DISPLAY_ADDR));
79 do {
80 struct nfs_client *prev = clp;
82 inode = nfs_delegation_find_inode(clp, &args->fh);
83 if (inode != NULL) {
84 /* Set up a helper thread to actually return the delegation */
85 switch (nfs_async_inode_return_delegation(inode, &args->stateid)) {
86 case 0:
87 res = 0;
88 break;
89 case -ENOENT:
90 if (res != 0)
91 res = htonl(NFS4ERR_BAD_STATEID);
92 break;
93 default:
94 res = htonl(NFS4ERR_RESOURCE);
96 iput(inode);
98 clp = nfs_find_client_next(prev);
99 nfs_put_client(prev);
100 } while (clp != NULL);
101 out:
102 dprintk("%s: exit with status = %d\n", __func__, ntohl(res));
103 return res;
106 int nfs4_validate_delegation_stateid(struct nfs_delegation *delegation, const nfs4_stateid *stateid)
108 if (delegation == NULL || memcmp(delegation->stateid.data, stateid->data,
109 sizeof(delegation->stateid.data)) != 0)
110 return 0;
111 return 1;
114 #if defined(CONFIG_NFS_V4_1)
116 int nfs41_validate_delegation_stateid(struct nfs_delegation *delegation, const nfs4_stateid *stateid)
118 if (delegation == NULL)
119 return 0;
121 /* seqid is 4-bytes long */
122 if (((u32 *) &stateid->data)[0] != 0)
123 return 0;
124 if (memcmp(&delegation->stateid.data[4], &stateid->data[4],
125 sizeof(stateid->data)-4))
126 return 0;
128 return 1;
132 * Validate the sequenceID sent by the server.
133 * Return success if the sequenceID is one more than what we last saw on
134 * this slot, accounting for wraparound. Increments the slot's sequence.
136 * We don't yet implement a duplicate request cache, instead we set the
137 * back channel ca_maxresponsesize_cached to zero. This is OK for now
138 * since we only currently implement idempotent callbacks anyway.
140 * We have a single slot backchannel at this time, so we don't bother
141 * checking the used_slots bit array on the table. The lower layer guarantees
142 * a single outstanding callback request at a time.
144 static __be32
145 validate_seqid(struct nfs4_slot_table *tbl, struct cb_sequenceargs * args)
147 struct nfs4_slot *slot;
149 dprintk("%s enter. slotid %d seqid %d\n",
150 __func__, args->csa_slotid, args->csa_sequenceid);
152 if (args->csa_slotid > NFS41_BC_MAX_CALLBACKS)
153 return htonl(NFS4ERR_BADSLOT);
155 slot = tbl->slots + args->csa_slotid;
156 dprintk("%s slot table seqid: %d\n", __func__, slot->seq_nr);
158 /* Normal */
159 if (likely(args->csa_sequenceid == slot->seq_nr + 1)) {
160 slot->seq_nr++;
161 return htonl(NFS4_OK);
164 /* Replay */
165 if (args->csa_sequenceid == slot->seq_nr) {
166 dprintk("%s seqid %d is a replay\n",
167 __func__, args->csa_sequenceid);
168 /* Signal process_op to set this error on next op */
169 if (args->csa_cachethis == 0)
170 return htonl(NFS4ERR_RETRY_UNCACHED_REP);
172 /* The ca_maxresponsesize_cached is 0 with no DRC */
173 else if (args->csa_cachethis == 1)
174 return htonl(NFS4ERR_REP_TOO_BIG_TO_CACHE);
177 /* Wraparound */
178 if (args->csa_sequenceid == 1 && (slot->seq_nr + 1) == 0) {
179 slot->seq_nr = 1;
180 return htonl(NFS4_OK);
183 /* Misordered request */
184 return htonl(NFS4ERR_SEQ_MISORDERED);
188 * Returns a pointer to a held 'struct nfs_client' that matches the server's
189 * address, major version number, and session ID. It is the caller's
190 * responsibility to release the returned reference.
192 * Returns NULL if there are no connections with sessions, or if no session
193 * matches the one of interest.
195 static struct nfs_client *find_client_with_session(
196 const struct sockaddr *addr, u32 nfsversion,
197 struct nfs4_sessionid *sessionid)
199 struct nfs_client *clp;
201 clp = nfs_find_client(addr, 4);
202 if (clp == NULL)
203 return NULL;
205 do {
206 struct nfs_client *prev = clp;
208 if (clp->cl_session != NULL) {
209 if (memcmp(clp->cl_session->sess_id.data,
210 sessionid->data,
211 NFS4_MAX_SESSIONID_LEN) == 0) {
212 /* Returns a held reference to clp */
213 return clp;
216 clp = nfs_find_client_next(prev);
217 nfs_put_client(prev);
218 } while (clp != NULL);
220 return NULL;
224 * For each referring call triple, check the session's slot table for
225 * a match. If the slot is in use and the sequence numbers match, the
226 * client is still waiting for a response to the original request.
228 static bool referring_call_exists(struct nfs_client *clp,
229 uint32_t nrclists,
230 struct referring_call_list *rclists)
232 bool status = 0;
233 int i, j;
234 struct nfs4_session *session;
235 struct nfs4_slot_table *tbl;
236 struct referring_call_list *rclist;
237 struct referring_call *ref;
239 session = clp->cl_session;
240 tbl = &session->fc_slot_table;
242 for (i = 0; i < nrclists; i++) {
243 rclist = &rclists[i];
244 if (memcmp(session->sess_id.data,
245 rclist->rcl_sessionid.data,
246 NFS4_MAX_SESSIONID_LEN) != 0)
247 continue;
249 for (j = 0; j < rclist->rcl_nrefcalls; j++) {
250 ref = &rclist->rcl_refcalls[j];
252 dprintk("%s: sessionid %x:%x:%x:%x sequenceid %u "
253 "slotid %u\n", __func__,
254 ((u32 *)&rclist->rcl_sessionid.data)[0],
255 ((u32 *)&rclist->rcl_sessionid.data)[1],
256 ((u32 *)&rclist->rcl_sessionid.data)[2],
257 ((u32 *)&rclist->rcl_sessionid.data)[3],
258 ref->rc_sequenceid, ref->rc_slotid);
260 spin_lock(&tbl->slot_tbl_lock);
261 status = (test_bit(ref->rc_slotid, tbl->used_slots) &&
262 tbl->slots[ref->rc_slotid].seq_nr ==
263 ref->rc_sequenceid);
264 spin_unlock(&tbl->slot_tbl_lock);
265 if (status)
266 goto out;
270 out:
271 return status;
274 __be32 nfs4_callback_sequence(struct cb_sequenceargs *args,
275 struct cb_sequenceres *res)
277 struct nfs_client *clp;
278 int i;
279 __be32 status;
281 status = htonl(NFS4ERR_BADSESSION);
282 clp = find_client_with_session(args->csa_addr, 4, &args->csa_sessionid);
283 if (clp == NULL)
284 goto out;
286 status = validate_seqid(&clp->cl_session->bc_slot_table, args);
287 if (status)
288 goto out_putclient;
291 * Check for pending referring calls. If a match is found, a
292 * related callback was received before the response to the original
293 * call.
295 if (referring_call_exists(clp, args->csa_nrclists, args->csa_rclists)) {
296 status = htonl(NFS4ERR_DELAY);
297 goto out_putclient;
300 memcpy(&res->csr_sessionid, &args->csa_sessionid,
301 sizeof(res->csr_sessionid));
302 res->csr_sequenceid = args->csa_sequenceid;
303 res->csr_slotid = args->csa_slotid;
304 res->csr_highestslotid = NFS41_BC_MAX_CALLBACKS - 1;
305 res->csr_target_highestslotid = NFS41_BC_MAX_CALLBACKS - 1;
307 out_putclient:
308 nfs_put_client(clp);
309 out:
310 for (i = 0; i < args->csa_nrclists; i++)
311 kfree(args->csa_rclists[i].rcl_refcalls);
312 kfree(args->csa_rclists);
314 if (status == htonl(NFS4ERR_RETRY_UNCACHED_REP))
315 res->csr_status = 0;
316 else
317 res->csr_status = status;
318 dprintk("%s: exit with status = %d res->csr_status %d\n", __func__,
319 ntohl(status), ntohl(res->csr_status));
320 return status;
323 __be32 nfs4_callback_recallany(struct cb_recallanyargs *args, void *dummy)
325 struct nfs_client *clp;
326 __be32 status;
327 fmode_t flags = 0;
329 status = htonl(NFS4ERR_OP_NOT_IN_SESSION);
330 clp = nfs_find_client(args->craa_addr, 4);
331 if (clp == NULL)
332 goto out;
334 dprintk("NFS: RECALL_ANY callback request from %s\n",
335 rpc_peeraddr2str(clp->cl_rpcclient, RPC_DISPLAY_ADDR));
337 if (test_bit(RCA4_TYPE_MASK_RDATA_DLG, (const unsigned long *)
338 &args->craa_type_mask))
339 flags = FMODE_READ;
340 if (test_bit(RCA4_TYPE_MASK_WDATA_DLG, (const unsigned long *)
341 &args->craa_type_mask))
342 flags |= FMODE_WRITE;
344 if (flags)
345 nfs_expire_all_delegation_types(clp, flags);
346 status = htonl(NFS4_OK);
347 out:
348 dprintk("%s: exit with status = %d\n", __func__, ntohl(status));
349 return status;
352 /* Reduce the fore channel's max_slots to the target value */
353 __be32 nfs4_callback_recallslot(struct cb_recallslotargs *args, void *dummy)
355 struct nfs_client *clp;
356 struct nfs4_slot_table *fc_tbl;
357 __be32 status;
359 status = htonl(NFS4ERR_OP_NOT_IN_SESSION);
360 clp = nfs_find_client(args->crsa_addr, 4);
361 if (clp == NULL)
362 goto out;
364 dprintk("NFS: CB_RECALL_SLOT request from %s target max slots %d\n",
365 rpc_peeraddr2str(clp->cl_rpcclient, RPC_DISPLAY_ADDR),
366 args->crsa_target_max_slots);
368 fc_tbl = &clp->cl_session->fc_slot_table;
370 status = htonl(NFS4ERR_BAD_HIGH_SLOT);
371 if (args->crsa_target_max_slots > fc_tbl->max_slots ||
372 args->crsa_target_max_slots < 1)
373 goto out_putclient;
375 status = htonl(NFS4_OK);
376 if (args->crsa_target_max_slots == fc_tbl->max_slots)
377 goto out_putclient;
379 fc_tbl->target_max_slots = args->crsa_target_max_slots;
380 nfs41_handle_recall_slot(clp);
381 out_putclient:
382 nfs_put_client(clp); /* balance nfs_find_client */
383 out:
384 dprintk("%s: exit with status = %d\n", __func__, ntohl(status));
385 return status;
387 #endif /* CONFIG_NFS_V4_1 */