blob: e5a2dac5f715fe3a98c8c42a6059a29e3914dba3 [file] [log] [blame]
Linus Torvalds1da177e2005-04-16 15:20:36 -07001/*
2 * linux/fs/nfs/callback_xdr.c
3 *
4 * Copyright (C) 2004 Trond Myklebust
5 *
6 * NFSv4 callback encode/decode procedures
7 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07008#include <linux/kernel.h>
9#include <linux/sunrpc/svc.h>
10#include <linux/nfs4.h>
11#include <linux/nfs_fs.h>
Trond Myklebust4ce79712005-06-22 17:16:21 +000012#include "nfs4_fs.h"
Linus Torvalds1da177e2005-04-16 15:20:36 -070013#include "callback.h"
14
15#define CB_OP_TAGLEN_MAXSZ (512)
16#define CB_OP_HDR_RES_MAXSZ (2 + CB_OP_TAGLEN_MAXSZ)
17#define CB_OP_GETATTR_BITMAP_MAXSZ (4)
18#define CB_OP_GETATTR_RES_MAXSZ (CB_OP_HDR_RES_MAXSZ + \
19 CB_OP_GETATTR_BITMAP_MAXSZ + \
20 2 + 2 + 3 + 3)
21#define CB_OP_RECALL_RES_MAXSZ (CB_OP_HDR_RES_MAXSZ)
22
Benny Halevy4aece6a2009-04-01 09:23:26 -040023#if defined(CONFIG_NFS_V4_1)
24#define CB_OP_SEQUENCE_RES_MAXSZ (CB_OP_HDR_RES_MAXSZ + \
25 4 + 1 + 3)
26#endif /* CONFIG_NFS_V4_1 */
27
Linus Torvalds1da177e2005-04-16 15:20:36 -070028#define NFSDBG_FACILITY NFSDBG_CALLBACK
29
Al Viroe6f684f2006-10-19 23:28:50 -070030typedef __be32 (*callback_process_op_t)(void *, void *);
31typedef __be32 (*callback_decode_arg_t)(struct svc_rqst *, struct xdr_stream *, void *);
32typedef __be32 (*callback_encode_res_t)(struct svc_rqst *, struct xdr_stream *, void *);
Linus Torvalds1da177e2005-04-16 15:20:36 -070033
34
35struct callback_op {
36 callback_process_op_t process_op;
37 callback_decode_arg_t decode_args;
38 callback_encode_res_t encode_res;
39 long res_maxsize;
40};
41
42static struct callback_op callback_ops[];
43
Al Viro7111c662006-10-19 23:28:45 -070044static __be32 nfs4_callback_null(struct svc_rqst *rqstp, void *argp, void *resp)
Linus Torvalds1da177e2005-04-16 15:20:36 -070045{
46 return htonl(NFS4_OK);
47}
48
Al Viro5704fde2006-10-19 23:28:51 -070049static int nfs4_decode_void(struct svc_rqst *rqstp, __be32 *p, void *dummy)
Linus Torvalds1da177e2005-04-16 15:20:36 -070050{
51 return xdr_argsize_check(rqstp, p);
52}
53
Al Viro5704fde2006-10-19 23:28:51 -070054static int nfs4_encode_void(struct svc_rqst *rqstp, __be32 *p, void *dummy)
Linus Torvalds1da177e2005-04-16 15:20:36 -070055{
56 return xdr_ressize_check(rqstp, p);
57}
58
Al Viro5704fde2006-10-19 23:28:51 -070059static __be32 *read_buf(struct xdr_stream *xdr, int nbytes)
Linus Torvalds1da177e2005-04-16 15:20:36 -070060{
Al Viro5704fde2006-10-19 23:28:51 -070061 __be32 *p;
Linus Torvalds1da177e2005-04-16 15:20:36 -070062
63 p = xdr_inline_decode(xdr, nbytes);
64 if (unlikely(p == NULL))
65 printk(KERN_WARNING "NFSv4 callback reply buffer overflowed!\n");
66 return p;
67}
68
Al Viroe6f684f2006-10-19 23:28:50 -070069static __be32 decode_string(struct xdr_stream *xdr, unsigned int *len, const char **str)
Linus Torvalds1da177e2005-04-16 15:20:36 -070070{
Al Viro5704fde2006-10-19 23:28:51 -070071 __be32 *p;
Linus Torvalds1da177e2005-04-16 15:20:36 -070072
73 p = read_buf(xdr, 4);
74 if (unlikely(p == NULL))
75 return htonl(NFS4ERR_RESOURCE);
76 *len = ntohl(*p);
77
78 if (*len != 0) {
79 p = read_buf(xdr, *len);
80 if (unlikely(p == NULL))
81 return htonl(NFS4ERR_RESOURCE);
82 *str = (const char *)p;
83 } else
84 *str = NULL;
85
86 return 0;
87}
88
Al Viroe6f684f2006-10-19 23:28:50 -070089static __be32 decode_fh(struct xdr_stream *xdr, struct nfs_fh *fh)
Linus Torvalds1da177e2005-04-16 15:20:36 -070090{
Al Viro5704fde2006-10-19 23:28:51 -070091 __be32 *p;
Linus Torvalds1da177e2005-04-16 15:20:36 -070092
93 p = read_buf(xdr, 4);
94 if (unlikely(p == NULL))
95 return htonl(NFS4ERR_RESOURCE);
96 fh->size = ntohl(*p);
97 if (fh->size > NFS4_FHSIZE)
98 return htonl(NFS4ERR_BADHANDLE);
99 p = read_buf(xdr, fh->size);
100 if (unlikely(p == NULL))
101 return htonl(NFS4ERR_RESOURCE);
102 memcpy(&fh->data[0], p, fh->size);
103 memset(&fh->data[fh->size], 0, sizeof(fh->data) - fh->size);
104 return 0;
105}
106
Al Viroe6f684f2006-10-19 23:28:50 -0700107static __be32 decode_bitmap(struct xdr_stream *xdr, uint32_t *bitmap)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700108{
Al Viro5704fde2006-10-19 23:28:51 -0700109 __be32 *p;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700110 unsigned int attrlen;
111
112 p = read_buf(xdr, 4);
113 if (unlikely(p == NULL))
114 return htonl(NFS4ERR_RESOURCE);
115 attrlen = ntohl(*p);
116 p = read_buf(xdr, attrlen << 2);
117 if (unlikely(p == NULL))
118 return htonl(NFS4ERR_RESOURCE);
119 if (likely(attrlen > 0))
120 bitmap[0] = ntohl(*p++);
121 if (attrlen > 1)
122 bitmap[1] = ntohl(*p);
123 return 0;
124}
125
Al Viroe6f684f2006-10-19 23:28:50 -0700126static __be32 decode_stateid(struct xdr_stream *xdr, nfs4_stateid *stateid)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700127{
Al Viro5704fde2006-10-19 23:28:51 -0700128 __be32 *p;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700129
130 p = read_buf(xdr, 16);
131 if (unlikely(p == NULL))
132 return htonl(NFS4ERR_RESOURCE);
133 memcpy(stateid->data, p, 16);
134 return 0;
135}
136
Al Viroe6f684f2006-10-19 23:28:50 -0700137static __be32 decode_compound_hdr_arg(struct xdr_stream *xdr, struct cb_compound_hdr_arg *hdr)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700138{
Al Viro5704fde2006-10-19 23:28:51 -0700139 __be32 *p;
Al Viroe6f684f2006-10-19 23:28:50 -0700140 __be32 status;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700141
142 status = decode_string(xdr, &hdr->taglen, &hdr->tag);
143 if (unlikely(status != 0))
144 return status;
145 /* We do not like overly long tags! */
Chuck Lever5cce4282007-10-26 13:33:01 -0400146 if (hdr->taglen > CB_OP_TAGLEN_MAXSZ - 12) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700147 printk("NFSv4 CALLBACK %s: client sent tag of length %u\n",
Harvey Harrison3110ff82008-05-02 13:42:44 -0700148 __func__, hdr->taglen);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700149 return htonl(NFS4ERR_RESOURCE);
150 }
151 p = read_buf(xdr, 12);
152 if (unlikely(p == NULL))
153 return htonl(NFS4ERR_RESOURCE);
Benny Halevyb8f2ef82009-04-01 09:23:19 -0400154 hdr->minorversion = ntohl(*p++);
Benny Halevy48a9e2d2009-04-01 09:23:20 -0400155 /* Check minor version is zero or one. */
156 if (hdr->minorversion <= 1) {
157 p++; /* skip callback_ident */
158 } else {
Benny Halevyb8f2ef82009-04-01 09:23:19 -0400159 printk(KERN_WARNING "%s: NFSv4 server callback with "
160 "illegal minor version %u!\n",
161 __func__, hdr->minorversion);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700162 return htonl(NFS4ERR_MINOR_VERS_MISMATCH);
163 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700164 hdr->nops = ntohl(*p);
Benny Halevyb8f2ef82009-04-01 09:23:19 -0400165 dprintk("%s: minorversion %d nops %d\n", __func__,
166 hdr->minorversion, hdr->nops);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700167 return 0;
168}
169
Al Viroe6f684f2006-10-19 23:28:50 -0700170static __be32 decode_op_hdr(struct xdr_stream *xdr, unsigned int *op)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700171{
Al Viro5704fde2006-10-19 23:28:51 -0700172 __be32 *p;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700173 p = read_buf(xdr, 4);
174 if (unlikely(p == NULL))
175 return htonl(NFS4ERR_RESOURCE);
176 *op = ntohl(*p);
177 return 0;
178}
179
Al Viroe6f684f2006-10-19 23:28:50 -0700180static __be32 decode_getattr_args(struct svc_rqst *rqstp, struct xdr_stream *xdr, struct cb_getattrargs *args)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700181{
Al Viroe6f684f2006-10-19 23:28:50 -0700182 __be32 status;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700183
184 status = decode_fh(xdr, &args->fh);
185 if (unlikely(status != 0))
186 goto out;
Chuck Lever671beed2007-12-10 14:58:22 -0500187 args->addr = svc_addr(rqstp);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700188 status = decode_bitmap(xdr, args->bitmap);
189out:
Harvey Harrison3110ff82008-05-02 13:42:44 -0700190 dprintk("%s: exit with status = %d\n", __func__, ntohl(status));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700191 return status;
192}
193
Al Viroe6f684f2006-10-19 23:28:50 -0700194static __be32 decode_recall_args(struct svc_rqst *rqstp, struct xdr_stream *xdr, struct cb_recallargs *args)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700195{
Al Viro5704fde2006-10-19 23:28:51 -0700196 __be32 *p;
Al Viroe6f684f2006-10-19 23:28:50 -0700197 __be32 status;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700198
Chuck Leverc1d35862007-12-10 14:58:29 -0500199 args->addr = svc_addr(rqstp);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700200 status = decode_stateid(xdr, &args->stateid);
201 if (unlikely(status != 0))
202 goto out;
203 p = read_buf(xdr, 4);
204 if (unlikely(p == NULL)) {
205 status = htonl(NFS4ERR_RESOURCE);
206 goto out;
207 }
208 args->truncate = ntohl(*p);
209 status = decode_fh(xdr, &args->fh);
210out:
Harvey Harrison3110ff82008-05-02 13:42:44 -0700211 dprintk("%s: exit with status = %d\n", __func__, ntohl(status));
Alexey Dobriyan3873bc52006-05-27 03:31:12 +0400212 return status;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700213}
214
Benny Halevy4aece6a2009-04-01 09:23:26 -0400215#if defined(CONFIG_NFS_V4_1)
216
217static unsigned decode_sessionid(struct xdr_stream *xdr,
218 struct nfs4_sessionid *sid)
219{
220 uint32_t *p;
221 int len = NFS4_MAX_SESSIONID_LEN;
222
223 p = read_buf(xdr, len);
224 if (unlikely(p == NULL))
225 return htonl(NFS4ERR_RESOURCE);;
226
227 memcpy(sid->data, p, len);
228 return 0;
229}
230
231static unsigned decode_rc_list(struct xdr_stream *xdr,
232 struct referring_call_list *rc_list)
233{
234 uint32_t *p;
235 int i;
236 unsigned status;
237
238 status = decode_sessionid(xdr, &rc_list->rcl_sessionid);
239 if (status)
240 goto out;
241
242 status = htonl(NFS4ERR_RESOURCE);
243 p = read_buf(xdr, sizeof(uint32_t));
244 if (unlikely(p == NULL))
245 goto out;
246
247 rc_list->rcl_nrefcalls = ntohl(*p++);
248 if (rc_list->rcl_nrefcalls) {
249 p = read_buf(xdr,
250 rc_list->rcl_nrefcalls * 2 * sizeof(uint32_t));
251 if (unlikely(p == NULL))
252 goto out;
253 rc_list->rcl_refcalls = kmalloc(rc_list->rcl_nrefcalls *
254 sizeof(*rc_list->rcl_refcalls),
255 GFP_KERNEL);
256 if (unlikely(rc_list->rcl_refcalls == NULL))
257 goto out;
258 for (i = 0; i < rc_list->rcl_nrefcalls; i++) {
259 rc_list->rcl_refcalls[i].rc_sequenceid = ntohl(*p++);
260 rc_list->rcl_refcalls[i].rc_slotid = ntohl(*p++);
261 }
262 }
263 status = 0;
264
265out:
266 return status;
267}
268
269static unsigned decode_cb_sequence_args(struct svc_rqst *rqstp,
270 struct xdr_stream *xdr,
271 struct cb_sequenceargs *args)
272{
273 uint32_t *p;
274 int i;
275 unsigned status;
276
277 status = decode_sessionid(xdr, &args->csa_sessionid);
278 if (status)
279 goto out;
280
281 status = htonl(NFS4ERR_RESOURCE);
282 p = read_buf(xdr, 5 * sizeof(uint32_t));
283 if (unlikely(p == NULL))
284 goto out;
285
Ricardo Labiaga65fc64e2009-04-01 09:23:30 -0400286 args->csa_addr = svc_addr(rqstp);
Benny Halevy4aece6a2009-04-01 09:23:26 -0400287 args->csa_sequenceid = ntohl(*p++);
288 args->csa_slotid = ntohl(*p++);
289 args->csa_highestslotid = ntohl(*p++);
290 args->csa_cachethis = ntohl(*p++);
291 args->csa_nrclists = ntohl(*p++);
292 args->csa_rclists = NULL;
293 if (args->csa_nrclists) {
294 args->csa_rclists = kmalloc(args->csa_nrclists *
295 sizeof(*args->csa_rclists),
296 GFP_KERNEL);
297 if (unlikely(args->csa_rclists == NULL))
298 goto out;
299
300 for (i = 0; i < args->csa_nrclists; i++) {
301 status = decode_rc_list(xdr, &args->csa_rclists[i]);
302 if (status)
303 goto out_free;
304 }
305 }
306 status = 0;
307
308 dprintk("%s: sessionid %x:%x:%x:%x sequenceid %u slotid %u "
309 "highestslotid %u cachethis %d nrclists %u\n",
310 __func__,
311 ((u32 *)&args->csa_sessionid)[0],
312 ((u32 *)&args->csa_sessionid)[1],
313 ((u32 *)&args->csa_sessionid)[2],
314 ((u32 *)&args->csa_sessionid)[3],
315 args->csa_sequenceid, args->csa_slotid,
316 args->csa_highestslotid, args->csa_cachethis,
317 args->csa_nrclists);
318out:
319 dprintk("%s: exit with status = %d\n", __func__, ntohl(status));
320 return status;
321
322out_free:
323 for (i = 0; i < args->csa_nrclists; i++)
324 kfree(args->csa_rclists[i].rcl_refcalls);
325 kfree(args->csa_rclists);
326 goto out;
327}
328
329#endif /* CONFIG_NFS_V4_1 */
330
Al Viroe6f684f2006-10-19 23:28:50 -0700331static __be32 encode_string(struct xdr_stream *xdr, unsigned int len, const char *str)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700332{
Al Viro5704fde2006-10-19 23:28:51 -0700333 __be32 *p;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700334
335 p = xdr_reserve_space(xdr, 4 + len);
336 if (unlikely(p == NULL))
337 return htonl(NFS4ERR_RESOURCE);
338 xdr_encode_opaque(p, str, len);
339 return 0;
340}
341
342#define CB_SUPPORTED_ATTR0 (FATTR4_WORD0_CHANGE|FATTR4_WORD0_SIZE)
343#define CB_SUPPORTED_ATTR1 (FATTR4_WORD1_TIME_METADATA|FATTR4_WORD1_TIME_MODIFY)
Al Viro5704fde2006-10-19 23:28:51 -0700344static __be32 encode_attr_bitmap(struct xdr_stream *xdr, const uint32_t *bitmap, __be32 **savep)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700345{
Al Viro5704fde2006-10-19 23:28:51 -0700346 __be32 bm[2];
347 __be32 *p;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700348
349 bm[0] = htonl(bitmap[0] & CB_SUPPORTED_ATTR0);
350 bm[1] = htonl(bitmap[1] & CB_SUPPORTED_ATTR1);
351 if (bm[1] != 0) {
352 p = xdr_reserve_space(xdr, 16);
353 if (unlikely(p == NULL))
354 return htonl(NFS4ERR_RESOURCE);
355 *p++ = htonl(2);
356 *p++ = bm[0];
357 *p++ = bm[1];
358 } else if (bm[0] != 0) {
359 p = xdr_reserve_space(xdr, 12);
360 if (unlikely(p == NULL))
361 return htonl(NFS4ERR_RESOURCE);
362 *p++ = htonl(1);
363 *p++ = bm[0];
364 } else {
365 p = xdr_reserve_space(xdr, 8);
366 if (unlikely(p == NULL))
367 return htonl(NFS4ERR_RESOURCE);
368 *p++ = htonl(0);
369 }
370 *savep = p;
371 return 0;
372}
373
Al Viroe6f684f2006-10-19 23:28:50 -0700374static __be32 encode_attr_change(struct xdr_stream *xdr, const uint32_t *bitmap, uint64_t change)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700375{
Al Viro5704fde2006-10-19 23:28:51 -0700376 __be32 *p;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700377
378 if (!(bitmap[0] & FATTR4_WORD0_CHANGE))
379 return 0;
380 p = xdr_reserve_space(xdr, 8);
Harvey Harrison90dc7d22008-02-20 13:03:05 -0800381 if (unlikely(!p))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700382 return htonl(NFS4ERR_RESOURCE);
383 p = xdr_encode_hyper(p, change);
384 return 0;
385}
386
Al Viroe6f684f2006-10-19 23:28:50 -0700387static __be32 encode_attr_size(struct xdr_stream *xdr, const uint32_t *bitmap, uint64_t size)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700388{
Al Viro5704fde2006-10-19 23:28:51 -0700389 __be32 *p;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700390
391 if (!(bitmap[0] & FATTR4_WORD0_SIZE))
392 return 0;
393 p = xdr_reserve_space(xdr, 8);
Harvey Harrison90dc7d22008-02-20 13:03:05 -0800394 if (unlikely(!p))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700395 return htonl(NFS4ERR_RESOURCE);
396 p = xdr_encode_hyper(p, size);
397 return 0;
398}
399
Al Viroe6f684f2006-10-19 23:28:50 -0700400static __be32 encode_attr_time(struct xdr_stream *xdr, const struct timespec *time)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700401{
Al Viro5704fde2006-10-19 23:28:51 -0700402 __be32 *p;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700403
404 p = xdr_reserve_space(xdr, 12);
Harvey Harrison90dc7d22008-02-20 13:03:05 -0800405 if (unlikely(!p))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700406 return htonl(NFS4ERR_RESOURCE);
407 p = xdr_encode_hyper(p, time->tv_sec);
408 *p = htonl(time->tv_nsec);
409 return 0;
410}
411
Al Viroe6f684f2006-10-19 23:28:50 -0700412static __be32 encode_attr_ctime(struct xdr_stream *xdr, const uint32_t *bitmap, const struct timespec *time)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700413{
414 if (!(bitmap[1] & FATTR4_WORD1_TIME_METADATA))
415 return 0;
416 return encode_attr_time(xdr,time);
417}
418
Al Viroe6f684f2006-10-19 23:28:50 -0700419static __be32 encode_attr_mtime(struct xdr_stream *xdr, const uint32_t *bitmap, const struct timespec *time)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700420{
421 if (!(bitmap[1] & FATTR4_WORD1_TIME_MODIFY))
422 return 0;
423 return encode_attr_time(xdr,time);
424}
425
Al Viroe6f684f2006-10-19 23:28:50 -0700426static __be32 encode_compound_hdr_res(struct xdr_stream *xdr, struct cb_compound_hdr_res *hdr)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700427{
Al Viroe6f684f2006-10-19 23:28:50 -0700428 __be32 status;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700429
430 hdr->status = xdr_reserve_space(xdr, 4);
431 if (unlikely(hdr->status == NULL))
432 return htonl(NFS4ERR_RESOURCE);
433 status = encode_string(xdr, hdr->taglen, hdr->tag);
434 if (unlikely(status != 0))
435 return status;
436 hdr->nops = xdr_reserve_space(xdr, 4);
437 if (unlikely(hdr->nops == NULL))
438 return htonl(NFS4ERR_RESOURCE);
439 return 0;
440}
441
Al Viroe6f684f2006-10-19 23:28:50 -0700442static __be32 encode_op_hdr(struct xdr_stream *xdr, uint32_t op, __be32 res)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700443{
Al Viro5704fde2006-10-19 23:28:51 -0700444 __be32 *p;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700445
446 p = xdr_reserve_space(xdr, 8);
447 if (unlikely(p == NULL))
448 return htonl(NFS4ERR_RESOURCE);
449 *p++ = htonl(op);
450 *p = res;
451 return 0;
452}
453
Al Viroe6f684f2006-10-19 23:28:50 -0700454static __be32 encode_getattr_res(struct svc_rqst *rqstp, struct xdr_stream *xdr, const struct cb_getattrres *res)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700455{
Al Viro5704fde2006-10-19 23:28:51 -0700456 __be32 *savep = NULL;
Al Viroe6f684f2006-10-19 23:28:50 -0700457 __be32 status = res->status;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700458
459 if (unlikely(status != 0))
460 goto out;
461 status = encode_attr_bitmap(xdr, res->bitmap, &savep);
462 if (unlikely(status != 0))
463 goto out;
464 status = encode_attr_change(xdr, res->bitmap, res->change_attr);
465 if (unlikely(status != 0))
466 goto out;
467 status = encode_attr_size(xdr, res->bitmap, res->size);
468 if (unlikely(status != 0))
469 goto out;
470 status = encode_attr_ctime(xdr, res->bitmap, &res->ctime);
471 if (unlikely(status != 0))
472 goto out;
473 status = encode_attr_mtime(xdr, res->bitmap, &res->mtime);
474 *savep = htonl((unsigned int)((char *)xdr->p - (char *)(savep+1)));
475out:
Harvey Harrison3110ff82008-05-02 13:42:44 -0700476 dprintk("%s: exit with status = %d\n", __func__, ntohl(status));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700477 return status;
478}
479
Benny Halevy34bc47c92009-04-01 09:23:22 -0400480#if defined(CONFIG_NFS_V4_1)
481
Benny Halevy4aece6a2009-04-01 09:23:26 -0400482static unsigned encode_sessionid(struct xdr_stream *xdr,
483 const struct nfs4_sessionid *sid)
484{
485 uint32_t *p;
486 int len = NFS4_MAX_SESSIONID_LEN;
487
488 p = xdr_reserve_space(xdr, len);
489 if (unlikely(p == NULL))
490 return htonl(NFS4ERR_RESOURCE);
491
492 memcpy(p, sid, len);
493 return 0;
494}
495
496static unsigned encode_cb_sequence_res(struct svc_rqst *rqstp,
497 struct xdr_stream *xdr,
498 const struct cb_sequenceres *res)
499{
500 uint32_t *p;
501 unsigned status = res->csr_status;
502
503 if (unlikely(status != 0))
504 goto out;
505
506 encode_sessionid(xdr, &res->csr_sessionid);
507
508 p = xdr_reserve_space(xdr, 4 * sizeof(uint32_t));
509 if (unlikely(p == NULL))
510 return htonl(NFS4ERR_RESOURCE);
511
512 *p++ = htonl(res->csr_sequenceid);
513 *p++ = htonl(res->csr_slotid);
514 *p++ = htonl(res->csr_highestslotid);
515 *p++ = htonl(res->csr_target_highestslotid);
516out:
517 dprintk("%s: exit with status = %d\n", __func__, ntohl(status));
518 return status;
519}
520
Benny Halevy34bc47c92009-04-01 09:23:22 -0400521static __be32
522preprocess_nfs41_op(int nop, unsigned int op_nr, struct callback_op **op)
523{
Benny Halevy281fe152009-04-01 09:23:27 -0400524 if (op_nr == OP_CB_SEQUENCE) {
525 if (nop != 0)
526 return htonl(NFS4ERR_SEQUENCE_POS);
527 } else {
528 if (nop == 0)
529 return htonl(NFS4ERR_OP_NOT_IN_SESSION);
530 }
531
Benny Halevy34bc47c92009-04-01 09:23:22 -0400532 switch (op_nr) {
533 case OP_CB_GETATTR:
534 case OP_CB_RECALL:
Benny Halevy4aece6a2009-04-01 09:23:26 -0400535 case OP_CB_SEQUENCE:
Benny Halevy34bc47c92009-04-01 09:23:22 -0400536 *op = &callback_ops[op_nr];
537 break;
538
539 case OP_CB_LAYOUTRECALL:
540 case OP_CB_NOTIFY_DEVICEID:
541 case OP_CB_NOTIFY:
542 case OP_CB_PUSH_DELEG:
543 case OP_CB_RECALL_ANY:
544 case OP_CB_RECALLABLE_OBJ_AVAIL:
545 case OP_CB_RECALL_SLOT:
Benny Halevy34bc47c92009-04-01 09:23:22 -0400546 case OP_CB_WANTS_CANCELLED:
547 case OP_CB_NOTIFY_LOCK:
548 return htonl(NFS4ERR_NOTSUPP);
549
550 default:
551 return htonl(NFS4ERR_OP_ILLEGAL);
552 }
553
554 return htonl(NFS_OK);
555}
556
557#else /* CONFIG_NFS_V4_1 */
558
559static __be32
560preprocess_nfs41_op(int nop, unsigned int op_nr, struct callback_op **op)
561{
562 return htonl(NFS4ERR_MINOR_VERS_MISMATCH);
563}
564
565#endif /* CONFIG_NFS_V4_1 */
566
567static __be32
568preprocess_nfs4_op(unsigned int op_nr, struct callback_op **op)
569{
570 switch (op_nr) {
571 case OP_CB_GETATTR:
572 case OP_CB_RECALL:
573 *op = &callback_ops[op_nr];
574 break;
575 default:
576 return htonl(NFS4ERR_OP_ILLEGAL);
577 }
578
579 return htonl(NFS_OK);
580}
581
582static __be32 process_op(uint32_t minorversion, int nop,
583 struct svc_rqst *rqstp,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700584 struct xdr_stream *xdr_in, void *argp,
585 struct xdr_stream *xdr_out, void *resp)
586{
Trond Myklebusta162a6b2006-03-20 13:44:10 -0500587 struct callback_op *op = &callback_ops[0];
588 unsigned int op_nr = OP_CB_ILLEGAL;
Benny Halevy34bc47c92009-04-01 09:23:22 -0400589 __be32 status;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700590 long maxlen;
Al Viroe6f684f2006-10-19 23:28:50 -0700591 __be32 res;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700592
Harvey Harrison3110ff82008-05-02 13:42:44 -0700593 dprintk("%s: start\n", __func__);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700594 status = decode_op_hdr(xdr_in, &op_nr);
Benny Halevy34bc47c92009-04-01 09:23:22 -0400595 if (unlikely(status)) {
596 status = htonl(NFS4ERR_OP_ILLEGAL);
597 goto out;
Trond Myklebusta162a6b2006-03-20 13:44:10 -0500598 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700599
Benny Halevy34bc47c92009-04-01 09:23:22 -0400600 dprintk("%s: minorversion=%d nop=%d op_nr=%u\n",
601 __func__, minorversion, nop, op_nr);
602
603 status = minorversion ? preprocess_nfs41_op(nop, op_nr, &op) :
604 preprocess_nfs4_op(op_nr, &op);
605 if (status == htonl(NFS4ERR_OP_ILLEGAL))
606 op_nr = OP_CB_ILLEGAL;
607out:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700608 maxlen = xdr_out->end - xdr_out->p;
609 if (maxlen > 0 && maxlen < PAGE_SIZE) {
610 if (likely(status == 0 && op->decode_args != NULL))
611 status = op->decode_args(rqstp, xdr_in, argp);
612 if (likely(status == 0 && op->process_op != NULL))
613 status = op->process_op(argp, resp);
614 } else
615 status = htonl(NFS4ERR_RESOURCE);
616
617 res = encode_op_hdr(xdr_out, op_nr, status);
618 if (status == 0)
619 status = res;
620 if (op->encode_res != NULL && status == 0)
621 status = op->encode_res(rqstp, xdr_out, resp);
Harvey Harrison3110ff82008-05-02 13:42:44 -0700622 dprintk("%s: done, status = %d\n", __func__, ntohl(status));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700623 return status;
624}
625
626/*
627 * Decode, process and encode a COMPOUND
628 */
Al Viro7111c662006-10-19 23:28:45 -0700629static __be32 nfs4_callback_compound(struct svc_rqst *rqstp, void *argp, void *resp)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700630{
Trond Myklebust3a6258e2008-05-06 13:32:40 -0400631 struct cb_compound_hdr_arg hdr_arg = { 0 };
632 struct cb_compound_hdr_res hdr_res = { NULL };
Linus Torvalds1da177e2005-04-16 15:20:36 -0700633 struct xdr_stream xdr_in, xdr_out;
Al Viro5704fde2006-10-19 23:28:51 -0700634 __be32 *p;
Al Viroe6f684f2006-10-19 23:28:50 -0700635 __be32 status;
Trond Myklebust3a6258e2008-05-06 13:32:40 -0400636 unsigned int nops = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700637
Harvey Harrison3110ff82008-05-02 13:42:44 -0700638 dprintk("%s: start\n", __func__);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700639
640 xdr_init_decode(&xdr_in, &rqstp->rq_arg, rqstp->rq_arg.head[0].iov_base);
641
Al Viro5704fde2006-10-19 23:28:51 -0700642 p = (__be32*)((char *)rqstp->rq_res.head[0].iov_base + rqstp->rq_res.head[0].iov_len);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700643 xdr_init_encode(&xdr_out, &rqstp->rq_res, p);
644
Trond Myklebust3a6258e2008-05-06 13:32:40 -0400645 status = decode_compound_hdr_arg(&xdr_in, &hdr_arg);
646 if (status == __constant_htonl(NFS4ERR_RESOURCE))
647 return rpc_garbage_args;
648
Linus Torvalds1da177e2005-04-16 15:20:36 -0700649 hdr_res.taglen = hdr_arg.taglen;
650 hdr_res.tag = hdr_arg.tag;
Trond Myklebust3a6258e2008-05-06 13:32:40 -0400651 if (encode_compound_hdr_res(&xdr_out, &hdr_res) != 0)
652 return rpc_system_err;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700653
Trond Myklebust3a6258e2008-05-06 13:32:40 -0400654 while (status == 0 && nops != hdr_arg.nops) {
Benny Halevy34bc47c92009-04-01 09:23:22 -0400655 status = process_op(hdr_arg.minorversion, nops,
656 rqstp, &xdr_in, argp, &xdr_out, resp);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700657 nops++;
658 }
Trond Myklebust3a6258e2008-05-06 13:32:40 -0400659
Linus Torvalds1da177e2005-04-16 15:20:36 -0700660 *hdr_res.status = status;
661 *hdr_res.nops = htonl(nops);
Harvey Harrison3110ff82008-05-02 13:42:44 -0700662 dprintk("%s: done, status = %u\n", __func__, ntohl(status));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700663 return rpc_success;
664}
665
666/*
667 * Define NFS4 callback COMPOUND ops.
668 */
669static struct callback_op callback_ops[] = {
670 [0] = {
671 .res_maxsize = CB_OP_HDR_RES_MAXSZ,
672 },
673 [OP_CB_GETATTR] = {
674 .process_op = (callback_process_op_t)nfs4_callback_getattr,
675 .decode_args = (callback_decode_arg_t)decode_getattr_args,
676 .encode_res = (callback_encode_res_t)encode_getattr_res,
677 .res_maxsize = CB_OP_GETATTR_RES_MAXSZ,
678 },
679 [OP_CB_RECALL] = {
680 .process_op = (callback_process_op_t)nfs4_callback_recall,
681 .decode_args = (callback_decode_arg_t)decode_recall_args,
682 .res_maxsize = CB_OP_RECALL_RES_MAXSZ,
Benny Halevy4aece6a2009-04-01 09:23:26 -0400683 },
684#if defined(CONFIG_NFS_V4_1)
685 [OP_CB_SEQUENCE] = {
686 .process_op = (callback_process_op_t)nfs4_callback_sequence,
687 .decode_args = (callback_decode_arg_t)decode_cb_sequence_args,
688 .encode_res = (callback_encode_res_t)encode_cb_sequence_res,
689 .res_maxsize = CB_OP_SEQUENCE_RES_MAXSZ,
690 },
691#endif /* CONFIG_NFS_V4_1 */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700692};
693
694/*
695 * Define NFS4 callback procedures
696 */
697static struct svc_procedure nfs4_callback_procedures1[] = {
698 [CB_NULL] = {
699 .pc_func = nfs4_callback_null,
700 .pc_decode = (kxdrproc_t)nfs4_decode_void,
701 .pc_encode = (kxdrproc_t)nfs4_encode_void,
702 .pc_xdrressize = 1,
703 },
704 [CB_COMPOUND] = {
705 .pc_func = nfs4_callback_compound,
706 .pc_encode = (kxdrproc_t)nfs4_encode_void,
707 .pc_argsize = 256,
708 .pc_ressize = 256,
709 .pc_xdrressize = NFS4_CALLBACK_BUFSIZE,
710 }
711};
712
713struct svc_version nfs4_callback_version1 = {
714 .vs_vers = 1,
715 .vs_nproc = ARRAY_SIZE(nfs4_callback_procedures1),
716 .vs_proc = nfs4_callback_procedures1,
717 .vs_xdrsize = NFS4_CALLBACK_XDRSIZE,
718 .vs_dispatch = NULL,
719};
720