blob: 05af212f0edfb1558e238bb0ce95a5ef7688987f [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>
Tejun Heo5a0e3ad2010-03-24 17:04:11 +090012#include <linux/slab.h>
Trond Myklebust4ce79712005-06-22 17:16:21 +000013#include "nfs4_fs.h"
Linus Torvalds1da177e2005-04-16 15:20:36 -070014#include "callback.h"
15
16#define CB_OP_TAGLEN_MAXSZ (512)
17#define CB_OP_HDR_RES_MAXSZ (2 + CB_OP_TAGLEN_MAXSZ)
18#define CB_OP_GETATTR_BITMAP_MAXSZ (4)
19#define CB_OP_GETATTR_RES_MAXSZ (CB_OP_HDR_RES_MAXSZ + \
20 CB_OP_GETATTR_BITMAP_MAXSZ + \
21 2 + 2 + 3 + 3)
22#define CB_OP_RECALL_RES_MAXSZ (CB_OP_HDR_RES_MAXSZ)
23
Benny Halevy4aece6a2009-04-01 09:23:26 -040024#if defined(CONFIG_NFS_V4_1)
25#define CB_OP_SEQUENCE_RES_MAXSZ (CB_OP_HDR_RES_MAXSZ + \
26 4 + 1 + 3)
Alexandros Batsakis31f09602009-12-05 13:27:02 -050027#define CB_OP_RECALLANY_RES_MAXSZ (CB_OP_HDR_RES_MAXSZ)
Andy Adamsonb9efa1b2010-01-20 16:06:27 -050028#define CB_OP_RECALLSLOT_RES_MAXSZ (CB_OP_HDR_RES_MAXSZ)
Benny Halevy4aece6a2009-04-01 09:23:26 -040029#endif /* CONFIG_NFS_V4_1 */
30
Linus Torvalds1da177e2005-04-16 15:20:36 -070031#define NFSDBG_FACILITY NFSDBG_CALLBACK
32
Andy Adamson31d2b432010-01-14 17:45:04 -050033/* Internal error code */
34#define NFS4ERR_RESOURCE_HDR 11050
35
Al Viroe6f684f2006-10-19 23:28:50 -070036typedef __be32 (*callback_process_op_t)(void *, void *);
37typedef __be32 (*callback_decode_arg_t)(struct svc_rqst *, struct xdr_stream *, void *);
38typedef __be32 (*callback_encode_res_t)(struct svc_rqst *, struct xdr_stream *, void *);
Linus Torvalds1da177e2005-04-16 15:20:36 -070039
40
41struct callback_op {
42 callback_process_op_t process_op;
43 callback_decode_arg_t decode_args;
44 callback_encode_res_t encode_res;
45 long res_maxsize;
46};
47
48static struct callback_op callback_ops[];
49
Al Viro7111c662006-10-19 23:28:45 -070050static __be32 nfs4_callback_null(struct svc_rqst *rqstp, void *argp, void *resp)
Linus Torvalds1da177e2005-04-16 15:20:36 -070051{
52 return htonl(NFS4_OK);
53}
54
Al Viro5704fde2006-10-19 23:28:51 -070055static int nfs4_decode_void(struct svc_rqst *rqstp, __be32 *p, void *dummy)
Linus Torvalds1da177e2005-04-16 15:20:36 -070056{
57 return xdr_argsize_check(rqstp, p);
58}
59
Al Viro5704fde2006-10-19 23:28:51 -070060static int nfs4_encode_void(struct svc_rqst *rqstp, __be32 *p, void *dummy)
Linus Torvalds1da177e2005-04-16 15:20:36 -070061{
62 return xdr_ressize_check(rqstp, p);
63}
64
Al Viro5704fde2006-10-19 23:28:51 -070065static __be32 *read_buf(struct xdr_stream *xdr, int nbytes)
Linus Torvalds1da177e2005-04-16 15:20:36 -070066{
Al Viro5704fde2006-10-19 23:28:51 -070067 __be32 *p;
Linus Torvalds1da177e2005-04-16 15:20:36 -070068
69 p = xdr_inline_decode(xdr, nbytes);
70 if (unlikely(p == NULL))
71 printk(KERN_WARNING "NFSv4 callback reply buffer overflowed!\n");
72 return p;
73}
74
Al Viroe6f684f2006-10-19 23:28:50 -070075static __be32 decode_string(struct xdr_stream *xdr, unsigned int *len, const char **str)
Linus Torvalds1da177e2005-04-16 15:20:36 -070076{
Al Viro5704fde2006-10-19 23:28:51 -070077 __be32 *p;
Linus Torvalds1da177e2005-04-16 15:20:36 -070078
79 p = read_buf(xdr, 4);
80 if (unlikely(p == NULL))
81 return htonl(NFS4ERR_RESOURCE);
82 *len = ntohl(*p);
83
84 if (*len != 0) {
85 p = read_buf(xdr, *len);
86 if (unlikely(p == NULL))
87 return htonl(NFS4ERR_RESOURCE);
88 *str = (const char *)p;
89 } else
90 *str = NULL;
91
92 return 0;
93}
94
Al Viroe6f684f2006-10-19 23:28:50 -070095static __be32 decode_fh(struct xdr_stream *xdr, struct nfs_fh *fh)
Linus Torvalds1da177e2005-04-16 15:20:36 -070096{
Al Viro5704fde2006-10-19 23:28:51 -070097 __be32 *p;
Linus Torvalds1da177e2005-04-16 15:20:36 -070098
99 p = read_buf(xdr, 4);
100 if (unlikely(p == NULL))
101 return htonl(NFS4ERR_RESOURCE);
102 fh->size = ntohl(*p);
103 if (fh->size > NFS4_FHSIZE)
104 return htonl(NFS4ERR_BADHANDLE);
105 p = read_buf(xdr, fh->size);
106 if (unlikely(p == NULL))
107 return htonl(NFS4ERR_RESOURCE);
108 memcpy(&fh->data[0], p, fh->size);
109 memset(&fh->data[fh->size], 0, sizeof(fh->data) - fh->size);
110 return 0;
111}
112
Al Viroe6f684f2006-10-19 23:28:50 -0700113static __be32 decode_bitmap(struct xdr_stream *xdr, uint32_t *bitmap)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700114{
Al Viro5704fde2006-10-19 23:28:51 -0700115 __be32 *p;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700116 unsigned int attrlen;
117
118 p = read_buf(xdr, 4);
119 if (unlikely(p == NULL))
120 return htonl(NFS4ERR_RESOURCE);
121 attrlen = ntohl(*p);
122 p = read_buf(xdr, attrlen << 2);
123 if (unlikely(p == NULL))
124 return htonl(NFS4ERR_RESOURCE);
125 if (likely(attrlen > 0))
126 bitmap[0] = ntohl(*p++);
127 if (attrlen > 1)
128 bitmap[1] = ntohl(*p);
129 return 0;
130}
131
Al Viroe6f684f2006-10-19 23:28:50 -0700132static __be32 decode_stateid(struct xdr_stream *xdr, nfs4_stateid *stateid)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700133{
Al Viro5704fde2006-10-19 23:28:51 -0700134 __be32 *p;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700135
136 p = read_buf(xdr, 16);
137 if (unlikely(p == NULL))
138 return htonl(NFS4ERR_RESOURCE);
139 memcpy(stateid->data, p, 16);
140 return 0;
141}
142
Al Viroe6f684f2006-10-19 23:28:50 -0700143static __be32 decode_compound_hdr_arg(struct xdr_stream *xdr, struct cb_compound_hdr_arg *hdr)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700144{
Al Viro5704fde2006-10-19 23:28:51 -0700145 __be32 *p;
Al Viroe6f684f2006-10-19 23:28:50 -0700146 __be32 status;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700147
148 status = decode_string(xdr, &hdr->taglen, &hdr->tag);
149 if (unlikely(status != 0))
150 return status;
151 /* We do not like overly long tags! */
Chuck Lever5cce4282007-10-26 13:33:01 -0400152 if (hdr->taglen > CB_OP_TAGLEN_MAXSZ - 12) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700153 printk("NFSv4 CALLBACK %s: client sent tag of length %u\n",
Harvey Harrison3110ff82008-05-02 13:42:44 -0700154 __func__, hdr->taglen);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700155 return htonl(NFS4ERR_RESOURCE);
156 }
157 p = read_buf(xdr, 12);
158 if (unlikely(p == NULL))
159 return htonl(NFS4ERR_RESOURCE);
Benny Halevyb8f2ef82009-04-01 09:23:19 -0400160 hdr->minorversion = ntohl(*p++);
Benny Halevy48a9e2d2009-04-01 09:23:20 -0400161 /* Check minor version is zero or one. */
162 if (hdr->minorversion <= 1) {
163 p++; /* skip callback_ident */
164 } else {
Benny Halevyb8f2ef82009-04-01 09:23:19 -0400165 printk(KERN_WARNING "%s: NFSv4 server callback with "
166 "illegal minor version %u!\n",
167 __func__, hdr->minorversion);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700168 return htonl(NFS4ERR_MINOR_VERS_MISMATCH);
169 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700170 hdr->nops = ntohl(*p);
Benny Halevyb8f2ef82009-04-01 09:23:19 -0400171 dprintk("%s: minorversion %d nops %d\n", __func__,
172 hdr->minorversion, hdr->nops);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700173 return 0;
174}
175
Al Viroe6f684f2006-10-19 23:28:50 -0700176static __be32 decode_op_hdr(struct xdr_stream *xdr, unsigned int *op)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700177{
Al Viro5704fde2006-10-19 23:28:51 -0700178 __be32 *p;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700179 p = read_buf(xdr, 4);
180 if (unlikely(p == NULL))
Andy Adamson31d2b432010-01-14 17:45:04 -0500181 return htonl(NFS4ERR_RESOURCE_HDR);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700182 *op = ntohl(*p);
183 return 0;
184}
185
Al Viroe6f684f2006-10-19 23:28:50 -0700186static __be32 decode_getattr_args(struct svc_rqst *rqstp, struct xdr_stream *xdr, struct cb_getattrargs *args)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700187{
Al Viroe6f684f2006-10-19 23:28:50 -0700188 __be32 status;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700189
190 status = decode_fh(xdr, &args->fh);
191 if (unlikely(status != 0))
192 goto out;
Chuck Lever671beed2007-12-10 14:58:22 -0500193 args->addr = svc_addr(rqstp);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700194 status = decode_bitmap(xdr, args->bitmap);
195out:
Harvey Harrison3110ff82008-05-02 13:42:44 -0700196 dprintk("%s: exit with status = %d\n", __func__, ntohl(status));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700197 return status;
198}
199
Al Viroe6f684f2006-10-19 23:28:50 -0700200static __be32 decode_recall_args(struct svc_rqst *rqstp, struct xdr_stream *xdr, struct cb_recallargs *args)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700201{
Al Viro5704fde2006-10-19 23:28:51 -0700202 __be32 *p;
Al Viroe6f684f2006-10-19 23:28:50 -0700203 __be32 status;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700204
Chuck Leverc1d35862007-12-10 14:58:29 -0500205 args->addr = svc_addr(rqstp);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700206 status = decode_stateid(xdr, &args->stateid);
207 if (unlikely(status != 0))
208 goto out;
209 p = read_buf(xdr, 4);
210 if (unlikely(p == NULL)) {
211 status = htonl(NFS4ERR_RESOURCE);
212 goto out;
213 }
214 args->truncate = ntohl(*p);
215 status = decode_fh(xdr, &args->fh);
216out:
Harvey Harrison3110ff82008-05-02 13:42:44 -0700217 dprintk("%s: exit with status = %d\n", __func__, ntohl(status));
Alexey Dobriyan3873bc52006-05-27 03:31:12 +0400218 return status;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700219}
220
Benny Halevy4aece6a2009-04-01 09:23:26 -0400221#if defined(CONFIG_NFS_V4_1)
222
Andy Adamson9733f0d2010-01-22 12:03:08 -0500223static __be32 decode_sessionid(struct xdr_stream *xdr,
Benny Halevy4aece6a2009-04-01 09:23:26 -0400224 struct nfs4_sessionid *sid)
225{
Andy Adamson9733f0d2010-01-22 12:03:08 -0500226 __be32 *p;
Benny Halevy4aece6a2009-04-01 09:23:26 -0400227 int len = NFS4_MAX_SESSIONID_LEN;
228
229 p = read_buf(xdr, len);
230 if (unlikely(p == NULL))
Joe Perchesa419aef2009-08-18 11:18:35 -0700231 return htonl(NFS4ERR_RESOURCE);
Benny Halevy4aece6a2009-04-01 09:23:26 -0400232
233 memcpy(sid->data, p, len);
234 return 0;
235}
236
Andy Adamson9733f0d2010-01-22 12:03:08 -0500237static __be32 decode_rc_list(struct xdr_stream *xdr,
Benny Halevy4aece6a2009-04-01 09:23:26 -0400238 struct referring_call_list *rc_list)
239{
Andy Adamson9733f0d2010-01-22 12:03:08 -0500240 __be32 *p;
Benny Halevy4aece6a2009-04-01 09:23:26 -0400241 int i;
Andy Adamson9733f0d2010-01-22 12:03:08 -0500242 __be32 status;
Benny Halevy4aece6a2009-04-01 09:23:26 -0400243
244 status = decode_sessionid(xdr, &rc_list->rcl_sessionid);
245 if (status)
246 goto out;
247
248 status = htonl(NFS4ERR_RESOURCE);
249 p = read_buf(xdr, sizeof(uint32_t));
250 if (unlikely(p == NULL))
251 goto out;
252
253 rc_list->rcl_nrefcalls = ntohl(*p++);
254 if (rc_list->rcl_nrefcalls) {
255 p = read_buf(xdr,
256 rc_list->rcl_nrefcalls * 2 * sizeof(uint32_t));
257 if (unlikely(p == NULL))
258 goto out;
259 rc_list->rcl_refcalls = kmalloc(rc_list->rcl_nrefcalls *
260 sizeof(*rc_list->rcl_refcalls),
261 GFP_KERNEL);
262 if (unlikely(rc_list->rcl_refcalls == NULL))
263 goto out;
264 for (i = 0; i < rc_list->rcl_nrefcalls; i++) {
265 rc_list->rcl_refcalls[i].rc_sequenceid = ntohl(*p++);
266 rc_list->rcl_refcalls[i].rc_slotid = ntohl(*p++);
267 }
268 }
269 status = 0;
270
271out:
272 return status;
273}
274
Andy Adamson9733f0d2010-01-22 12:03:08 -0500275static __be32 decode_cb_sequence_args(struct svc_rqst *rqstp,
Benny Halevy4aece6a2009-04-01 09:23:26 -0400276 struct xdr_stream *xdr,
277 struct cb_sequenceargs *args)
278{
Andy Adamson9733f0d2010-01-22 12:03:08 -0500279 __be32 *p;
Benny Halevy4aece6a2009-04-01 09:23:26 -0400280 int i;
Andy Adamson9733f0d2010-01-22 12:03:08 -0500281 __be32 status;
Benny Halevy4aece6a2009-04-01 09:23:26 -0400282
283 status = decode_sessionid(xdr, &args->csa_sessionid);
284 if (status)
285 goto out;
286
287 status = htonl(NFS4ERR_RESOURCE);
288 p = read_buf(xdr, 5 * sizeof(uint32_t));
289 if (unlikely(p == NULL))
290 goto out;
291
Ricardo Labiaga65fc64e2009-04-01 09:23:30 -0400292 args->csa_addr = svc_addr(rqstp);
Benny Halevy4aece6a2009-04-01 09:23:26 -0400293 args->csa_sequenceid = ntohl(*p++);
294 args->csa_slotid = ntohl(*p++);
295 args->csa_highestslotid = ntohl(*p++);
296 args->csa_cachethis = ntohl(*p++);
297 args->csa_nrclists = ntohl(*p++);
298 args->csa_rclists = NULL;
299 if (args->csa_nrclists) {
300 args->csa_rclists = kmalloc(args->csa_nrclists *
301 sizeof(*args->csa_rclists),
302 GFP_KERNEL);
303 if (unlikely(args->csa_rclists == NULL))
304 goto out;
305
306 for (i = 0; i < args->csa_nrclists; i++) {
307 status = decode_rc_list(xdr, &args->csa_rclists[i]);
308 if (status)
309 goto out_free;
310 }
311 }
312 status = 0;
313
314 dprintk("%s: sessionid %x:%x:%x:%x sequenceid %u slotid %u "
315 "highestslotid %u cachethis %d nrclists %u\n",
316 __func__,
317 ((u32 *)&args->csa_sessionid)[0],
318 ((u32 *)&args->csa_sessionid)[1],
319 ((u32 *)&args->csa_sessionid)[2],
320 ((u32 *)&args->csa_sessionid)[3],
321 args->csa_sequenceid, args->csa_slotid,
322 args->csa_highestslotid, args->csa_cachethis,
323 args->csa_nrclists);
324out:
325 dprintk("%s: exit with status = %d\n", __func__, ntohl(status));
326 return status;
327
328out_free:
329 for (i = 0; i < args->csa_nrclists; i++)
330 kfree(args->csa_rclists[i].rcl_refcalls);
331 kfree(args->csa_rclists);
332 goto out;
333}
334
Andy Adamson9733f0d2010-01-22 12:03:08 -0500335static __be32 decode_recallany_args(struct svc_rqst *rqstp,
Alexandros Batsakis31f09602009-12-05 13:27:02 -0500336 struct xdr_stream *xdr,
337 struct cb_recallanyargs *args)
338{
Andy Adamson9733f0d2010-01-22 12:03:08 -0500339 __be32 *p;
Alexandros Batsakis31f09602009-12-05 13:27:02 -0500340
341 args->craa_addr = svc_addr(rqstp);
342 p = read_buf(xdr, 4);
343 if (unlikely(p == NULL))
344 return htonl(NFS4ERR_BADXDR);
345 args->craa_objs_to_keep = ntohl(*p++);
346 p = read_buf(xdr, 4);
347 if (unlikely(p == NULL))
348 return htonl(NFS4ERR_BADXDR);
349 args->craa_type_mask = ntohl(*p);
350
351 return 0;
352}
353
Andy Adamson9733f0d2010-01-22 12:03:08 -0500354static __be32 decode_recallslot_args(struct svc_rqst *rqstp,
Andy Adamsonb9efa1b2010-01-20 16:06:27 -0500355 struct xdr_stream *xdr,
356 struct cb_recallslotargs *args)
357{
358 __be32 *p;
359
360 args->crsa_addr = svc_addr(rqstp);
361 p = read_buf(xdr, 4);
362 if (unlikely(p == NULL))
363 return htonl(NFS4ERR_BADXDR);
364 args->crsa_target_max_slots = ntohl(*p++);
365 return 0;
366}
367
Benny Halevy4aece6a2009-04-01 09:23:26 -0400368#endif /* CONFIG_NFS_V4_1 */
369
Al Viroe6f684f2006-10-19 23:28:50 -0700370static __be32 encode_string(struct xdr_stream *xdr, unsigned int len, const char *str)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700371{
Al Viro5704fde2006-10-19 23:28:51 -0700372 __be32 *p;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700373
374 p = xdr_reserve_space(xdr, 4 + len);
375 if (unlikely(p == NULL))
376 return htonl(NFS4ERR_RESOURCE);
377 xdr_encode_opaque(p, str, len);
378 return 0;
379}
380
381#define CB_SUPPORTED_ATTR0 (FATTR4_WORD0_CHANGE|FATTR4_WORD0_SIZE)
382#define CB_SUPPORTED_ATTR1 (FATTR4_WORD1_TIME_METADATA|FATTR4_WORD1_TIME_MODIFY)
Al Viro5704fde2006-10-19 23:28:51 -0700383static __be32 encode_attr_bitmap(struct xdr_stream *xdr, const uint32_t *bitmap, __be32 **savep)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700384{
Al Viro5704fde2006-10-19 23:28:51 -0700385 __be32 bm[2];
386 __be32 *p;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700387
388 bm[0] = htonl(bitmap[0] & CB_SUPPORTED_ATTR0);
389 bm[1] = htonl(bitmap[1] & CB_SUPPORTED_ATTR1);
390 if (bm[1] != 0) {
391 p = xdr_reserve_space(xdr, 16);
392 if (unlikely(p == NULL))
393 return htonl(NFS4ERR_RESOURCE);
394 *p++ = htonl(2);
395 *p++ = bm[0];
396 *p++ = bm[1];
397 } else if (bm[0] != 0) {
398 p = xdr_reserve_space(xdr, 12);
399 if (unlikely(p == NULL))
400 return htonl(NFS4ERR_RESOURCE);
401 *p++ = htonl(1);
402 *p++ = bm[0];
403 } else {
404 p = xdr_reserve_space(xdr, 8);
405 if (unlikely(p == NULL))
406 return htonl(NFS4ERR_RESOURCE);
407 *p++ = htonl(0);
408 }
409 *savep = p;
410 return 0;
411}
412
Al Viroe6f684f2006-10-19 23:28:50 -0700413static __be32 encode_attr_change(struct xdr_stream *xdr, const uint32_t *bitmap, uint64_t change)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700414{
Al Viro5704fde2006-10-19 23:28:51 -0700415 __be32 *p;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700416
417 if (!(bitmap[0] & FATTR4_WORD0_CHANGE))
418 return 0;
419 p = xdr_reserve_space(xdr, 8);
Harvey Harrison90dc7d22008-02-20 13:03:05 -0800420 if (unlikely(!p))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700421 return htonl(NFS4ERR_RESOURCE);
422 p = xdr_encode_hyper(p, change);
423 return 0;
424}
425
Al Viroe6f684f2006-10-19 23:28:50 -0700426static __be32 encode_attr_size(struct xdr_stream *xdr, const uint32_t *bitmap, uint64_t size)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700427{
Al Viro5704fde2006-10-19 23:28:51 -0700428 __be32 *p;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700429
430 if (!(bitmap[0] & FATTR4_WORD0_SIZE))
431 return 0;
432 p = xdr_reserve_space(xdr, 8);
Harvey Harrison90dc7d22008-02-20 13:03:05 -0800433 if (unlikely(!p))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700434 return htonl(NFS4ERR_RESOURCE);
435 p = xdr_encode_hyper(p, size);
436 return 0;
437}
438
Al Viroe6f684f2006-10-19 23:28:50 -0700439static __be32 encode_attr_time(struct xdr_stream *xdr, const struct timespec *time)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700440{
Al Viro5704fde2006-10-19 23:28:51 -0700441 __be32 *p;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700442
443 p = xdr_reserve_space(xdr, 12);
Harvey Harrison90dc7d22008-02-20 13:03:05 -0800444 if (unlikely(!p))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700445 return htonl(NFS4ERR_RESOURCE);
446 p = xdr_encode_hyper(p, time->tv_sec);
447 *p = htonl(time->tv_nsec);
448 return 0;
449}
450
Al Viroe6f684f2006-10-19 23:28:50 -0700451static __be32 encode_attr_ctime(struct xdr_stream *xdr, const uint32_t *bitmap, const struct timespec *time)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700452{
453 if (!(bitmap[1] & FATTR4_WORD1_TIME_METADATA))
454 return 0;
455 return encode_attr_time(xdr,time);
456}
457
Al Viroe6f684f2006-10-19 23:28:50 -0700458static __be32 encode_attr_mtime(struct xdr_stream *xdr, const uint32_t *bitmap, const struct timespec *time)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700459{
460 if (!(bitmap[1] & FATTR4_WORD1_TIME_MODIFY))
461 return 0;
462 return encode_attr_time(xdr,time);
463}
464
Al Viroe6f684f2006-10-19 23:28:50 -0700465static __be32 encode_compound_hdr_res(struct xdr_stream *xdr, struct cb_compound_hdr_res *hdr)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700466{
Al Viroe6f684f2006-10-19 23:28:50 -0700467 __be32 status;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700468
469 hdr->status = xdr_reserve_space(xdr, 4);
470 if (unlikely(hdr->status == NULL))
471 return htonl(NFS4ERR_RESOURCE);
472 status = encode_string(xdr, hdr->taglen, hdr->tag);
473 if (unlikely(status != 0))
474 return status;
475 hdr->nops = xdr_reserve_space(xdr, 4);
476 if (unlikely(hdr->nops == NULL))
477 return htonl(NFS4ERR_RESOURCE);
478 return 0;
479}
480
Al Viroe6f684f2006-10-19 23:28:50 -0700481static __be32 encode_op_hdr(struct xdr_stream *xdr, uint32_t op, __be32 res)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700482{
Al Viro5704fde2006-10-19 23:28:51 -0700483 __be32 *p;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700484
485 p = xdr_reserve_space(xdr, 8);
486 if (unlikely(p == NULL))
Andy Adamson31d2b432010-01-14 17:45:04 -0500487 return htonl(NFS4ERR_RESOURCE_HDR);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700488 *p++ = htonl(op);
489 *p = res;
490 return 0;
491}
492
Al Viroe6f684f2006-10-19 23:28:50 -0700493static __be32 encode_getattr_res(struct svc_rqst *rqstp, struct xdr_stream *xdr, const struct cb_getattrres *res)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700494{
Al Viro5704fde2006-10-19 23:28:51 -0700495 __be32 *savep = NULL;
Al Viroe6f684f2006-10-19 23:28:50 -0700496 __be32 status = res->status;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700497
498 if (unlikely(status != 0))
499 goto out;
500 status = encode_attr_bitmap(xdr, res->bitmap, &savep);
501 if (unlikely(status != 0))
502 goto out;
503 status = encode_attr_change(xdr, res->bitmap, res->change_attr);
504 if (unlikely(status != 0))
505 goto out;
506 status = encode_attr_size(xdr, res->bitmap, res->size);
507 if (unlikely(status != 0))
508 goto out;
509 status = encode_attr_ctime(xdr, res->bitmap, &res->ctime);
510 if (unlikely(status != 0))
511 goto out;
512 status = encode_attr_mtime(xdr, res->bitmap, &res->mtime);
513 *savep = htonl((unsigned int)((char *)xdr->p - (char *)(savep+1)));
514out:
Harvey Harrison3110ff82008-05-02 13:42:44 -0700515 dprintk("%s: exit with status = %d\n", __func__, ntohl(status));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700516 return status;
517}
518
Benny Halevy34bc47c92009-04-01 09:23:22 -0400519#if defined(CONFIG_NFS_V4_1)
520
Andy Adamson9733f0d2010-01-22 12:03:08 -0500521static __be32 encode_sessionid(struct xdr_stream *xdr,
Benny Halevy4aece6a2009-04-01 09:23:26 -0400522 const struct nfs4_sessionid *sid)
523{
Andy Adamson9733f0d2010-01-22 12:03:08 -0500524 __be32 *p;
Benny Halevy4aece6a2009-04-01 09:23:26 -0400525 int len = NFS4_MAX_SESSIONID_LEN;
526
527 p = xdr_reserve_space(xdr, len);
528 if (unlikely(p == NULL))
529 return htonl(NFS4ERR_RESOURCE);
530
531 memcpy(p, sid, len);
532 return 0;
533}
534
Andy Adamson9733f0d2010-01-22 12:03:08 -0500535static __be32 encode_cb_sequence_res(struct svc_rqst *rqstp,
Benny Halevy4aece6a2009-04-01 09:23:26 -0400536 struct xdr_stream *xdr,
537 const struct cb_sequenceres *res)
538{
Andy Adamson9733f0d2010-01-22 12:03:08 -0500539 __be32 *p;
Benny Halevy4aece6a2009-04-01 09:23:26 -0400540 unsigned status = res->csr_status;
541
542 if (unlikely(status != 0))
543 goto out;
544
545 encode_sessionid(xdr, &res->csr_sessionid);
546
547 p = xdr_reserve_space(xdr, 4 * sizeof(uint32_t));
548 if (unlikely(p == NULL))
549 return htonl(NFS4ERR_RESOURCE);
550
551 *p++ = htonl(res->csr_sequenceid);
552 *p++ = htonl(res->csr_slotid);
553 *p++ = htonl(res->csr_highestslotid);
554 *p++ = htonl(res->csr_target_highestslotid);
555out:
556 dprintk("%s: exit with status = %d\n", __func__, ntohl(status));
557 return status;
558}
559
Benny Halevy34bc47c92009-04-01 09:23:22 -0400560static __be32
561preprocess_nfs41_op(int nop, unsigned int op_nr, struct callback_op **op)
562{
Benny Halevy281fe152009-04-01 09:23:27 -0400563 if (op_nr == OP_CB_SEQUENCE) {
564 if (nop != 0)
565 return htonl(NFS4ERR_SEQUENCE_POS);
566 } else {
567 if (nop == 0)
568 return htonl(NFS4ERR_OP_NOT_IN_SESSION);
569 }
570
Benny Halevy34bc47c92009-04-01 09:23:22 -0400571 switch (op_nr) {
572 case OP_CB_GETATTR:
573 case OP_CB_RECALL:
Benny Halevy4aece6a2009-04-01 09:23:26 -0400574 case OP_CB_SEQUENCE:
Alexandros Batsakis31f09602009-12-05 13:27:02 -0500575 case OP_CB_RECALL_ANY:
Andy Adamsonb9efa1b2010-01-20 16:06:27 -0500576 case OP_CB_RECALL_SLOT:
Benny Halevy34bc47c92009-04-01 09:23:22 -0400577 *op = &callback_ops[op_nr];
578 break;
579
580 case OP_CB_LAYOUTRECALL:
581 case OP_CB_NOTIFY_DEVICEID:
582 case OP_CB_NOTIFY:
583 case OP_CB_PUSH_DELEG:
Benny Halevy34bc47c92009-04-01 09:23:22 -0400584 case OP_CB_RECALLABLE_OBJ_AVAIL:
Benny Halevy34bc47c92009-04-01 09:23:22 -0400585 case OP_CB_WANTS_CANCELLED:
586 case OP_CB_NOTIFY_LOCK:
587 return htonl(NFS4ERR_NOTSUPP);
588
589 default:
590 return htonl(NFS4ERR_OP_ILLEGAL);
591 }
592
593 return htonl(NFS_OK);
594}
595
596#else /* CONFIG_NFS_V4_1 */
597
598static __be32
599preprocess_nfs41_op(int nop, unsigned int op_nr, struct callback_op **op)
600{
601 return htonl(NFS4ERR_MINOR_VERS_MISMATCH);
602}
603
604#endif /* CONFIG_NFS_V4_1 */
605
606static __be32
607preprocess_nfs4_op(unsigned int op_nr, struct callback_op **op)
608{
609 switch (op_nr) {
610 case OP_CB_GETATTR:
611 case OP_CB_RECALL:
612 *op = &callback_ops[op_nr];
613 break;
614 default:
615 return htonl(NFS4ERR_OP_ILLEGAL);
616 }
617
618 return htonl(NFS_OK);
619}
620
621static __be32 process_op(uint32_t minorversion, int nop,
622 struct svc_rqst *rqstp,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700623 struct xdr_stream *xdr_in, void *argp,
Andy Adamson49110962010-01-14 17:45:08 -0500624 struct xdr_stream *xdr_out, void *resp, int* drc_status)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700625{
Trond Myklebusta162a6b2006-03-20 13:44:10 -0500626 struct callback_op *op = &callback_ops[0];
Andy Adamson31d2b432010-01-14 17:45:04 -0500627 unsigned int op_nr;
Benny Halevy34bc47c92009-04-01 09:23:22 -0400628 __be32 status;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700629 long maxlen;
Al Viroe6f684f2006-10-19 23:28:50 -0700630 __be32 res;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700631
Harvey Harrison3110ff82008-05-02 13:42:44 -0700632 dprintk("%s: start\n", __func__);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700633 status = decode_op_hdr(xdr_in, &op_nr);
Andy Adamson31d2b432010-01-14 17:45:04 -0500634 if (unlikely(status))
635 return status;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700636
Benny Halevy34bc47c92009-04-01 09:23:22 -0400637 dprintk("%s: minorversion=%d nop=%d op_nr=%u\n",
638 __func__, minorversion, nop, op_nr);
639
640 status = minorversion ? preprocess_nfs41_op(nop, op_nr, &op) :
641 preprocess_nfs4_op(op_nr, &op);
642 if (status == htonl(NFS4ERR_OP_ILLEGAL))
643 op_nr = OP_CB_ILLEGAL;
Andy Adamsonb92b3012010-01-14 17:45:05 -0500644 if (status)
645 goto encode_hdr;
Andy Adamson31d2b432010-01-14 17:45:04 -0500646
Andy Adamson49110962010-01-14 17:45:08 -0500647 if (*drc_status) {
648 status = *drc_status;
649 goto encode_hdr;
650 }
651
Linus Torvalds1da177e2005-04-16 15:20:36 -0700652 maxlen = xdr_out->end - xdr_out->p;
653 if (maxlen > 0 && maxlen < PAGE_SIZE) {
Andy Adamsone95e60d2010-01-14 17:45:06 -0500654 status = op->decode_args(rqstp, xdr_in, argp);
655 if (likely(status == 0))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700656 status = op->process_op(argp, resp);
657 } else
658 status = htonl(NFS4ERR_RESOURCE);
659
Andy Adamson49110962010-01-14 17:45:08 -0500660 /* Only set by OP_CB_SEQUENCE processing */
661 if (status == htonl(NFS4ERR_RETRY_UNCACHED_REP)) {
662 *drc_status = status;
663 status = 0;
664 }
665
Andy Adamsonb92b3012010-01-14 17:45:05 -0500666encode_hdr:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700667 res = encode_op_hdr(xdr_out, op_nr, status);
Andy Adamson31d2b432010-01-14 17:45:04 -0500668 if (unlikely(res))
669 return res;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700670 if (op->encode_res != NULL && status == 0)
671 status = op->encode_res(rqstp, xdr_out, resp);
Harvey Harrison3110ff82008-05-02 13:42:44 -0700672 dprintk("%s: done, status = %d\n", __func__, ntohl(status));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700673 return status;
674}
675
676/*
677 * Decode, process and encode a COMPOUND
678 */
Al Viro7111c662006-10-19 23:28:45 -0700679static __be32 nfs4_callback_compound(struct svc_rqst *rqstp, void *argp, void *resp)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700680{
Trond Myklebust3a6258e2008-05-06 13:32:40 -0400681 struct cb_compound_hdr_arg hdr_arg = { 0 };
682 struct cb_compound_hdr_res hdr_res = { NULL };
Linus Torvalds1da177e2005-04-16 15:20:36 -0700683 struct xdr_stream xdr_in, xdr_out;
Al Viro5704fde2006-10-19 23:28:51 -0700684 __be32 *p;
Andy Adamson49110962010-01-14 17:45:08 -0500685 __be32 status, drc_status = 0;
Trond Myklebust3a6258e2008-05-06 13:32:40 -0400686 unsigned int nops = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700687
Harvey Harrison3110ff82008-05-02 13:42:44 -0700688 dprintk("%s: start\n", __func__);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700689
690 xdr_init_decode(&xdr_in, &rqstp->rq_arg, rqstp->rq_arg.head[0].iov_base);
691
Al Viro5704fde2006-10-19 23:28:51 -0700692 p = (__be32*)((char *)rqstp->rq_res.head[0].iov_base + rqstp->rq_res.head[0].iov_len);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700693 xdr_init_encode(&xdr_out, &rqstp->rq_res, p);
694
Trond Myklebust3a6258e2008-05-06 13:32:40 -0400695 status = decode_compound_hdr_arg(&xdr_in, &hdr_arg);
696 if (status == __constant_htonl(NFS4ERR_RESOURCE))
697 return rpc_garbage_args;
698
Linus Torvalds1da177e2005-04-16 15:20:36 -0700699 hdr_res.taglen = hdr_arg.taglen;
700 hdr_res.tag = hdr_arg.tag;
Trond Myklebust3a6258e2008-05-06 13:32:40 -0400701 if (encode_compound_hdr_res(&xdr_out, &hdr_res) != 0)
702 return rpc_system_err;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700703
Trond Myklebust3a6258e2008-05-06 13:32:40 -0400704 while (status == 0 && nops != hdr_arg.nops) {
Andy Adamson49110962010-01-14 17:45:08 -0500705 status = process_op(hdr_arg.minorversion, nops, rqstp,
706 &xdr_in, argp, &xdr_out, resp, &drc_status);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700707 nops++;
708 }
Trond Myklebust3a6258e2008-05-06 13:32:40 -0400709
Andy Adamson31d2b432010-01-14 17:45:04 -0500710 /* Buffer overflow in decode_ops_hdr or encode_ops_hdr. Return
711 * resource error in cb_compound status without returning op */
712 if (unlikely(status == htonl(NFS4ERR_RESOURCE_HDR))) {
713 status = htonl(NFS4ERR_RESOURCE);
714 nops--;
715 }
716
Linus Torvalds1da177e2005-04-16 15:20:36 -0700717 *hdr_res.status = status;
718 *hdr_res.nops = htonl(nops);
Harvey Harrison3110ff82008-05-02 13:42:44 -0700719 dprintk("%s: done, status = %u\n", __func__, ntohl(status));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700720 return rpc_success;
721}
722
723/*
724 * Define NFS4 callback COMPOUND ops.
725 */
726static struct callback_op callback_ops[] = {
727 [0] = {
728 .res_maxsize = CB_OP_HDR_RES_MAXSZ,
729 },
730 [OP_CB_GETATTR] = {
731 .process_op = (callback_process_op_t)nfs4_callback_getattr,
732 .decode_args = (callback_decode_arg_t)decode_getattr_args,
733 .encode_res = (callback_encode_res_t)encode_getattr_res,
734 .res_maxsize = CB_OP_GETATTR_RES_MAXSZ,
735 },
736 [OP_CB_RECALL] = {
737 .process_op = (callback_process_op_t)nfs4_callback_recall,
738 .decode_args = (callback_decode_arg_t)decode_recall_args,
739 .res_maxsize = CB_OP_RECALL_RES_MAXSZ,
Benny Halevy4aece6a2009-04-01 09:23:26 -0400740 },
741#if defined(CONFIG_NFS_V4_1)
742 [OP_CB_SEQUENCE] = {
743 .process_op = (callback_process_op_t)nfs4_callback_sequence,
744 .decode_args = (callback_decode_arg_t)decode_cb_sequence_args,
745 .encode_res = (callback_encode_res_t)encode_cb_sequence_res,
746 .res_maxsize = CB_OP_SEQUENCE_RES_MAXSZ,
747 },
Alexandros Batsakis31f09602009-12-05 13:27:02 -0500748 [OP_CB_RECALL_ANY] = {
749 .process_op = (callback_process_op_t)nfs4_callback_recallany,
750 .decode_args = (callback_decode_arg_t)decode_recallany_args,
751 .res_maxsize = CB_OP_RECALLANY_RES_MAXSZ,
752 },
Andy Adamsonb9efa1b2010-01-20 16:06:27 -0500753 [OP_CB_RECALL_SLOT] = {
754 .process_op = (callback_process_op_t)nfs4_callback_recallslot,
755 .decode_args = (callback_decode_arg_t)decode_recallslot_args,
756 .res_maxsize = CB_OP_RECALLSLOT_RES_MAXSZ,
757 },
Benny Halevy4aece6a2009-04-01 09:23:26 -0400758#endif /* CONFIG_NFS_V4_1 */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700759};
760
761/*
762 * Define NFS4 callback procedures
763 */
764static struct svc_procedure nfs4_callback_procedures1[] = {
765 [CB_NULL] = {
766 .pc_func = nfs4_callback_null,
767 .pc_decode = (kxdrproc_t)nfs4_decode_void,
768 .pc_encode = (kxdrproc_t)nfs4_encode_void,
769 .pc_xdrressize = 1,
770 },
771 [CB_COMPOUND] = {
772 .pc_func = nfs4_callback_compound,
773 .pc_encode = (kxdrproc_t)nfs4_encode_void,
774 .pc_argsize = 256,
775 .pc_ressize = 256,
776 .pc_xdrressize = NFS4_CALLBACK_BUFSIZE,
777 }
778};
779
780struct svc_version nfs4_callback_version1 = {
781 .vs_vers = 1,
782 .vs_nproc = ARRAY_SIZE(nfs4_callback_procedures1),
783 .vs_proc = nfs4_callback_procedures1,
784 .vs_xdrsize = NFS4_CALLBACK_XDRSIZE,
785 .vs_dispatch = NULL,
Steve Dickson49697ee2009-10-13 16:07:33 -0400786 .vs_hidden = 1,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700787};
788
Alexandros Batsakis07bccc22009-12-05 13:19:01 -0500789struct svc_version nfs4_callback_version4 = {
790 .vs_vers = 4,
791 .vs_nproc = ARRAY_SIZE(nfs4_callback_procedures1),
792 .vs_proc = nfs4_callback_procedures1,
793 .vs_xdrsize = NFS4_CALLBACK_XDRSIZE,
794 .vs_dispatch = NULL,
795};