blob: dbd0d649805ce4fe2f6cdc8de9665a945deb3ebe [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>
Andy Adamsonc36fca52011-01-06 02:04:32 +000013#include <linux/sunrpc/bc_xprt.h>
Trond Myklebust4ce79712005-06-22 17:16:21 +000014#include "nfs4_fs.h"
Linus Torvalds1da177e2005-04-16 15:20:36 -070015#include "callback.h"
Andy Adamsonc36fca52011-01-06 02:04:32 +000016#include "internal.h"
Linus Torvalds1da177e2005-04-16 15:20:36 -070017
18#define CB_OP_TAGLEN_MAXSZ (512)
19#define CB_OP_HDR_RES_MAXSZ (2 + CB_OP_TAGLEN_MAXSZ)
20#define CB_OP_GETATTR_BITMAP_MAXSZ (4)
21#define CB_OP_GETATTR_RES_MAXSZ (CB_OP_HDR_RES_MAXSZ + \
22 CB_OP_GETATTR_BITMAP_MAXSZ + \
23 2 + 2 + 3 + 3)
24#define CB_OP_RECALL_RES_MAXSZ (CB_OP_HDR_RES_MAXSZ)
25
Benny Halevy4aece6a2009-04-01 09:23:26 -040026#if defined(CONFIG_NFS_V4_1)
27#define CB_OP_SEQUENCE_RES_MAXSZ (CB_OP_HDR_RES_MAXSZ + \
28 4 + 1 + 3)
Alexandros Batsakis31f09602009-12-05 13:27:02 -050029#define CB_OP_RECALLANY_RES_MAXSZ (CB_OP_HDR_RES_MAXSZ)
Andy Adamsonb9efa1b2010-01-20 16:06:27 -050030#define CB_OP_RECALLSLOT_RES_MAXSZ (CB_OP_HDR_RES_MAXSZ)
Benny Halevy4aece6a2009-04-01 09:23:26 -040031#endif /* CONFIG_NFS_V4_1 */
32
Linus Torvalds1da177e2005-04-16 15:20:36 -070033#define NFSDBG_FACILITY NFSDBG_CALLBACK
34
Andy Adamson31d2b432010-01-14 17:45:04 -050035/* Internal error code */
36#define NFS4ERR_RESOURCE_HDR 11050
37
Andy Adamsonc36fca52011-01-06 02:04:32 +000038typedef __be32 (*callback_process_op_t)(void *, void *,
39 struct cb_process_state *);
Al Viroe6f684f2006-10-19 23:28:50 -070040typedef __be32 (*callback_decode_arg_t)(struct svc_rqst *, struct xdr_stream *, void *);
41typedef __be32 (*callback_encode_res_t)(struct svc_rqst *, struct xdr_stream *, void *);
Linus Torvalds1da177e2005-04-16 15:20:36 -070042
43
44struct callback_op {
45 callback_process_op_t process_op;
46 callback_decode_arg_t decode_args;
47 callback_encode_res_t encode_res;
48 long res_maxsize;
49};
50
51static struct callback_op callback_ops[];
52
Al Viro7111c662006-10-19 23:28:45 -070053static __be32 nfs4_callback_null(struct svc_rqst *rqstp, void *argp, void *resp)
Linus Torvalds1da177e2005-04-16 15:20:36 -070054{
55 return htonl(NFS4_OK);
56}
57
Al Viro5704fde2006-10-19 23:28:51 -070058static int nfs4_decode_void(struct svc_rqst *rqstp, __be32 *p, void *dummy)
Linus Torvalds1da177e2005-04-16 15:20:36 -070059{
60 return xdr_argsize_check(rqstp, p);
61}
62
Al Viro5704fde2006-10-19 23:28:51 -070063static int nfs4_encode_void(struct svc_rqst *rqstp, __be32 *p, void *dummy)
Linus Torvalds1da177e2005-04-16 15:20:36 -070064{
65 return xdr_ressize_check(rqstp, p);
66}
67
Al Viro5704fde2006-10-19 23:28:51 -070068static __be32 *read_buf(struct xdr_stream *xdr, int nbytes)
Linus Torvalds1da177e2005-04-16 15:20:36 -070069{
Al Viro5704fde2006-10-19 23:28:51 -070070 __be32 *p;
Linus Torvalds1da177e2005-04-16 15:20:36 -070071
72 p = xdr_inline_decode(xdr, nbytes);
73 if (unlikely(p == NULL))
74 printk(KERN_WARNING "NFSv4 callback reply buffer overflowed!\n");
75 return p;
76}
77
Al Viroe6f684f2006-10-19 23:28:50 -070078static __be32 decode_string(struct xdr_stream *xdr, unsigned int *len, const char **str)
Linus Torvalds1da177e2005-04-16 15:20:36 -070079{
Al Viro5704fde2006-10-19 23:28:51 -070080 __be32 *p;
Linus Torvalds1da177e2005-04-16 15:20:36 -070081
82 p = read_buf(xdr, 4);
83 if (unlikely(p == NULL))
84 return htonl(NFS4ERR_RESOURCE);
85 *len = ntohl(*p);
86
87 if (*len != 0) {
88 p = read_buf(xdr, *len);
89 if (unlikely(p == NULL))
90 return htonl(NFS4ERR_RESOURCE);
91 *str = (const char *)p;
92 } else
93 *str = NULL;
94
95 return 0;
96}
97
Al Viroe6f684f2006-10-19 23:28:50 -070098static __be32 decode_fh(struct xdr_stream *xdr, struct nfs_fh *fh)
Linus Torvalds1da177e2005-04-16 15:20:36 -070099{
Al Viro5704fde2006-10-19 23:28:51 -0700100 __be32 *p;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700101
102 p = read_buf(xdr, 4);
103 if (unlikely(p == NULL))
104 return htonl(NFS4ERR_RESOURCE);
105 fh->size = ntohl(*p);
106 if (fh->size > NFS4_FHSIZE)
107 return htonl(NFS4ERR_BADHANDLE);
108 p = read_buf(xdr, fh->size);
109 if (unlikely(p == NULL))
110 return htonl(NFS4ERR_RESOURCE);
111 memcpy(&fh->data[0], p, fh->size);
112 memset(&fh->data[fh->size], 0, sizeof(fh->data) - fh->size);
113 return 0;
114}
115
Al Viroe6f684f2006-10-19 23:28:50 -0700116static __be32 decode_bitmap(struct xdr_stream *xdr, uint32_t *bitmap)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700117{
Al Viro5704fde2006-10-19 23:28:51 -0700118 __be32 *p;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700119 unsigned int attrlen;
120
121 p = read_buf(xdr, 4);
122 if (unlikely(p == NULL))
123 return htonl(NFS4ERR_RESOURCE);
124 attrlen = ntohl(*p);
125 p = read_buf(xdr, attrlen << 2);
126 if (unlikely(p == NULL))
127 return htonl(NFS4ERR_RESOURCE);
128 if (likely(attrlen > 0))
129 bitmap[0] = ntohl(*p++);
130 if (attrlen > 1)
131 bitmap[1] = ntohl(*p);
132 return 0;
133}
134
Al Viroe6f684f2006-10-19 23:28:50 -0700135static __be32 decode_stateid(struct xdr_stream *xdr, nfs4_stateid *stateid)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700136{
Al Viro5704fde2006-10-19 23:28:51 -0700137 __be32 *p;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700138
139 p = read_buf(xdr, 16);
140 if (unlikely(p == NULL))
141 return htonl(NFS4ERR_RESOURCE);
142 memcpy(stateid->data, p, 16);
143 return 0;
144}
145
Al Viroe6f684f2006-10-19 23:28:50 -0700146static __be32 decode_compound_hdr_arg(struct xdr_stream *xdr, struct cb_compound_hdr_arg *hdr)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700147{
Al Viro5704fde2006-10-19 23:28:51 -0700148 __be32 *p;
Al Viroe6f684f2006-10-19 23:28:50 -0700149 __be32 status;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700150
151 status = decode_string(xdr, &hdr->taglen, &hdr->tag);
152 if (unlikely(status != 0))
153 return status;
154 /* We do not like overly long tags! */
Chuck Lever5cce4282007-10-26 13:33:01 -0400155 if (hdr->taglen > CB_OP_TAGLEN_MAXSZ - 12) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700156 printk("NFSv4 CALLBACK %s: client sent tag of length %u\n",
Harvey Harrison3110ff82008-05-02 13:42:44 -0700157 __func__, hdr->taglen);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700158 return htonl(NFS4ERR_RESOURCE);
159 }
160 p = read_buf(xdr, 12);
161 if (unlikely(p == NULL))
162 return htonl(NFS4ERR_RESOURCE);
Benny Halevyb8f2ef82009-04-01 09:23:19 -0400163 hdr->minorversion = ntohl(*p++);
Benny Halevy48a9e2d2009-04-01 09:23:20 -0400164 /* Check minor version is zero or one. */
165 if (hdr->minorversion <= 1) {
Andy Adamsonc36fca52011-01-06 02:04:32 +0000166 hdr->cb_ident = ntohl(*p++); /* ignored by v4.1 */
Benny Halevy48a9e2d2009-04-01 09:23:20 -0400167 } else {
Benny Halevyb8f2ef82009-04-01 09:23:19 -0400168 printk(KERN_WARNING "%s: NFSv4 server callback with "
169 "illegal minor version %u!\n",
170 __func__, hdr->minorversion);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700171 return htonl(NFS4ERR_MINOR_VERS_MISMATCH);
172 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700173 hdr->nops = ntohl(*p);
Benny Halevyb8f2ef82009-04-01 09:23:19 -0400174 dprintk("%s: minorversion %d nops %d\n", __func__,
175 hdr->minorversion, hdr->nops);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700176 return 0;
177}
178
Al Viroe6f684f2006-10-19 23:28:50 -0700179static __be32 decode_op_hdr(struct xdr_stream *xdr, unsigned int *op)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700180{
Al Viro5704fde2006-10-19 23:28:51 -0700181 __be32 *p;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700182 p = read_buf(xdr, 4);
183 if (unlikely(p == NULL))
Andy Adamson31d2b432010-01-14 17:45:04 -0500184 return htonl(NFS4ERR_RESOURCE_HDR);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700185 *op = ntohl(*p);
186 return 0;
187}
188
Al Viroe6f684f2006-10-19 23:28:50 -0700189static __be32 decode_getattr_args(struct svc_rqst *rqstp, struct xdr_stream *xdr, struct cb_getattrargs *args)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700190{
Al Viroe6f684f2006-10-19 23:28:50 -0700191 __be32 status;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700192
193 status = decode_fh(xdr, &args->fh);
194 if (unlikely(status != 0))
195 goto out;
Chuck Lever671beed2007-12-10 14:58:22 -0500196 args->addr = svc_addr(rqstp);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700197 status = decode_bitmap(xdr, args->bitmap);
198out:
Harvey Harrison3110ff82008-05-02 13:42:44 -0700199 dprintk("%s: exit with status = %d\n", __func__, ntohl(status));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700200 return status;
201}
202
Al Viroe6f684f2006-10-19 23:28:50 -0700203static __be32 decode_recall_args(struct svc_rqst *rqstp, struct xdr_stream *xdr, struct cb_recallargs *args)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700204{
Al Viro5704fde2006-10-19 23:28:51 -0700205 __be32 *p;
Al Viroe6f684f2006-10-19 23:28:50 -0700206 __be32 status;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700207
Chuck Leverc1d35862007-12-10 14:58:29 -0500208 args->addr = svc_addr(rqstp);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700209 status = decode_stateid(xdr, &args->stateid);
210 if (unlikely(status != 0))
211 goto out;
212 p = read_buf(xdr, 4);
213 if (unlikely(p == NULL)) {
214 status = htonl(NFS4ERR_RESOURCE);
215 goto out;
216 }
217 args->truncate = ntohl(*p);
218 status = decode_fh(xdr, &args->fh);
219out:
Harvey Harrison3110ff82008-05-02 13:42:44 -0700220 dprintk("%s: exit with status = %d\n", __func__, ntohl(status));
Alexey Dobriyan3873bc52006-05-27 03:31:12 +0400221 return status;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700222}
223
Benny Halevy4aece6a2009-04-01 09:23:26 -0400224#if defined(CONFIG_NFS_V4_1)
225
Andy Adamson9733f0d2010-01-22 12:03:08 -0500226static __be32 decode_sessionid(struct xdr_stream *xdr,
Benny Halevy4aece6a2009-04-01 09:23:26 -0400227 struct nfs4_sessionid *sid)
228{
Andy Adamson9733f0d2010-01-22 12:03:08 -0500229 __be32 *p;
Benny Halevy4aece6a2009-04-01 09:23:26 -0400230 int len = NFS4_MAX_SESSIONID_LEN;
231
232 p = read_buf(xdr, len);
233 if (unlikely(p == NULL))
Joe Perchesa419aef2009-08-18 11:18:35 -0700234 return htonl(NFS4ERR_RESOURCE);
Benny Halevy4aece6a2009-04-01 09:23:26 -0400235
236 memcpy(sid->data, p, len);
237 return 0;
238}
239
Andy Adamson9733f0d2010-01-22 12:03:08 -0500240static __be32 decode_rc_list(struct xdr_stream *xdr,
Benny Halevy4aece6a2009-04-01 09:23:26 -0400241 struct referring_call_list *rc_list)
242{
Andy Adamson9733f0d2010-01-22 12:03:08 -0500243 __be32 *p;
Benny Halevy4aece6a2009-04-01 09:23:26 -0400244 int i;
Andy Adamson9733f0d2010-01-22 12:03:08 -0500245 __be32 status;
Benny Halevy4aece6a2009-04-01 09:23:26 -0400246
247 status = decode_sessionid(xdr, &rc_list->rcl_sessionid);
248 if (status)
249 goto out;
250
251 status = htonl(NFS4ERR_RESOURCE);
252 p = read_buf(xdr, sizeof(uint32_t));
253 if (unlikely(p == NULL))
254 goto out;
255
256 rc_list->rcl_nrefcalls = ntohl(*p++);
257 if (rc_list->rcl_nrefcalls) {
258 p = read_buf(xdr,
259 rc_list->rcl_nrefcalls * 2 * sizeof(uint32_t));
260 if (unlikely(p == NULL))
261 goto out;
262 rc_list->rcl_refcalls = kmalloc(rc_list->rcl_nrefcalls *
263 sizeof(*rc_list->rcl_refcalls),
264 GFP_KERNEL);
265 if (unlikely(rc_list->rcl_refcalls == NULL))
266 goto out;
267 for (i = 0; i < rc_list->rcl_nrefcalls; i++) {
268 rc_list->rcl_refcalls[i].rc_sequenceid = ntohl(*p++);
269 rc_list->rcl_refcalls[i].rc_slotid = ntohl(*p++);
270 }
271 }
272 status = 0;
273
274out:
275 return status;
276}
277
Andy Adamson9733f0d2010-01-22 12:03:08 -0500278static __be32 decode_cb_sequence_args(struct svc_rqst *rqstp,
Benny Halevy4aece6a2009-04-01 09:23:26 -0400279 struct xdr_stream *xdr,
280 struct cb_sequenceargs *args)
281{
Andy Adamson9733f0d2010-01-22 12:03:08 -0500282 __be32 *p;
Benny Halevy4aece6a2009-04-01 09:23:26 -0400283 int i;
Andy Adamson9733f0d2010-01-22 12:03:08 -0500284 __be32 status;
Benny Halevy4aece6a2009-04-01 09:23:26 -0400285
286 status = decode_sessionid(xdr, &args->csa_sessionid);
287 if (status)
288 goto out;
289
290 status = htonl(NFS4ERR_RESOURCE);
291 p = read_buf(xdr, 5 * sizeof(uint32_t));
292 if (unlikely(p == NULL))
293 goto out;
294
Ricardo Labiaga65fc64e2009-04-01 09:23:30 -0400295 args->csa_addr = svc_addr(rqstp);
Benny Halevy4aece6a2009-04-01 09:23:26 -0400296 args->csa_sequenceid = ntohl(*p++);
297 args->csa_slotid = ntohl(*p++);
298 args->csa_highestslotid = ntohl(*p++);
299 args->csa_cachethis = ntohl(*p++);
300 args->csa_nrclists = ntohl(*p++);
301 args->csa_rclists = NULL;
302 if (args->csa_nrclists) {
303 args->csa_rclists = kmalloc(args->csa_nrclists *
304 sizeof(*args->csa_rclists),
305 GFP_KERNEL);
306 if (unlikely(args->csa_rclists == NULL))
307 goto out;
308
309 for (i = 0; i < args->csa_nrclists; i++) {
310 status = decode_rc_list(xdr, &args->csa_rclists[i]);
311 if (status)
312 goto out_free;
313 }
314 }
315 status = 0;
316
317 dprintk("%s: sessionid %x:%x:%x:%x sequenceid %u slotid %u "
318 "highestslotid %u cachethis %d nrclists %u\n",
319 __func__,
320 ((u32 *)&args->csa_sessionid)[0],
321 ((u32 *)&args->csa_sessionid)[1],
322 ((u32 *)&args->csa_sessionid)[2],
323 ((u32 *)&args->csa_sessionid)[3],
324 args->csa_sequenceid, args->csa_slotid,
325 args->csa_highestslotid, args->csa_cachethis,
326 args->csa_nrclists);
327out:
328 dprintk("%s: exit with status = %d\n", __func__, ntohl(status));
329 return status;
330
331out_free:
332 for (i = 0; i < args->csa_nrclists; i++)
333 kfree(args->csa_rclists[i].rcl_refcalls);
334 kfree(args->csa_rclists);
335 goto out;
336}
337
Andy Adamson9733f0d2010-01-22 12:03:08 -0500338static __be32 decode_recallany_args(struct svc_rqst *rqstp,
Alexandros Batsakis31f09602009-12-05 13:27:02 -0500339 struct xdr_stream *xdr,
340 struct cb_recallanyargs *args)
341{
Andy Adamson9733f0d2010-01-22 12:03:08 -0500342 __be32 *p;
Alexandros Batsakis31f09602009-12-05 13:27:02 -0500343
344 args->craa_addr = svc_addr(rqstp);
345 p = read_buf(xdr, 4);
346 if (unlikely(p == NULL))
347 return htonl(NFS4ERR_BADXDR);
348 args->craa_objs_to_keep = ntohl(*p++);
349 p = read_buf(xdr, 4);
350 if (unlikely(p == NULL))
351 return htonl(NFS4ERR_BADXDR);
352 args->craa_type_mask = ntohl(*p);
353
354 return 0;
355}
356
Andy Adamson9733f0d2010-01-22 12:03:08 -0500357static __be32 decode_recallslot_args(struct svc_rqst *rqstp,
Andy Adamsonb9efa1b2010-01-20 16:06:27 -0500358 struct xdr_stream *xdr,
359 struct cb_recallslotargs *args)
360{
361 __be32 *p;
362
363 args->crsa_addr = svc_addr(rqstp);
364 p = read_buf(xdr, 4);
365 if (unlikely(p == NULL))
366 return htonl(NFS4ERR_BADXDR);
367 args->crsa_target_max_slots = ntohl(*p++);
368 return 0;
369}
370
Benny Halevy4aece6a2009-04-01 09:23:26 -0400371#endif /* CONFIG_NFS_V4_1 */
372
Al Viroe6f684f2006-10-19 23:28:50 -0700373static __be32 encode_string(struct xdr_stream *xdr, unsigned int len, const char *str)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700374{
Al Viro5704fde2006-10-19 23:28:51 -0700375 __be32 *p;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700376
377 p = xdr_reserve_space(xdr, 4 + len);
378 if (unlikely(p == NULL))
379 return htonl(NFS4ERR_RESOURCE);
380 xdr_encode_opaque(p, str, len);
381 return 0;
382}
383
384#define CB_SUPPORTED_ATTR0 (FATTR4_WORD0_CHANGE|FATTR4_WORD0_SIZE)
385#define CB_SUPPORTED_ATTR1 (FATTR4_WORD1_TIME_METADATA|FATTR4_WORD1_TIME_MODIFY)
Al Viro5704fde2006-10-19 23:28:51 -0700386static __be32 encode_attr_bitmap(struct xdr_stream *xdr, const uint32_t *bitmap, __be32 **savep)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700387{
Al Viro5704fde2006-10-19 23:28:51 -0700388 __be32 bm[2];
389 __be32 *p;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700390
391 bm[0] = htonl(bitmap[0] & CB_SUPPORTED_ATTR0);
392 bm[1] = htonl(bitmap[1] & CB_SUPPORTED_ATTR1);
393 if (bm[1] != 0) {
394 p = xdr_reserve_space(xdr, 16);
395 if (unlikely(p == NULL))
396 return htonl(NFS4ERR_RESOURCE);
397 *p++ = htonl(2);
398 *p++ = bm[0];
399 *p++ = bm[1];
400 } else if (bm[0] != 0) {
401 p = xdr_reserve_space(xdr, 12);
402 if (unlikely(p == NULL))
403 return htonl(NFS4ERR_RESOURCE);
404 *p++ = htonl(1);
405 *p++ = bm[0];
406 } else {
407 p = xdr_reserve_space(xdr, 8);
408 if (unlikely(p == NULL))
409 return htonl(NFS4ERR_RESOURCE);
410 *p++ = htonl(0);
411 }
412 *savep = p;
413 return 0;
414}
415
Al Viroe6f684f2006-10-19 23:28:50 -0700416static __be32 encode_attr_change(struct xdr_stream *xdr, const uint32_t *bitmap, uint64_t change)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700417{
Al Viro5704fde2006-10-19 23:28:51 -0700418 __be32 *p;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700419
420 if (!(bitmap[0] & FATTR4_WORD0_CHANGE))
421 return 0;
422 p = xdr_reserve_space(xdr, 8);
Harvey Harrison90dc7d22008-02-20 13:03:05 -0800423 if (unlikely(!p))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700424 return htonl(NFS4ERR_RESOURCE);
425 p = xdr_encode_hyper(p, change);
426 return 0;
427}
428
Al Viroe6f684f2006-10-19 23:28:50 -0700429static __be32 encode_attr_size(struct xdr_stream *xdr, const uint32_t *bitmap, uint64_t size)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700430{
Al Viro5704fde2006-10-19 23:28:51 -0700431 __be32 *p;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700432
433 if (!(bitmap[0] & FATTR4_WORD0_SIZE))
434 return 0;
435 p = xdr_reserve_space(xdr, 8);
Harvey Harrison90dc7d22008-02-20 13:03:05 -0800436 if (unlikely(!p))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700437 return htonl(NFS4ERR_RESOURCE);
438 p = xdr_encode_hyper(p, size);
439 return 0;
440}
441
Al Viroe6f684f2006-10-19 23:28:50 -0700442static __be32 encode_attr_time(struct xdr_stream *xdr, const struct timespec *time)
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, 12);
Harvey Harrison90dc7d22008-02-20 13:03:05 -0800447 if (unlikely(!p))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700448 return htonl(NFS4ERR_RESOURCE);
449 p = xdr_encode_hyper(p, time->tv_sec);
450 *p = htonl(time->tv_nsec);
451 return 0;
452}
453
Al Viroe6f684f2006-10-19 23:28:50 -0700454static __be32 encode_attr_ctime(struct xdr_stream *xdr, const uint32_t *bitmap, const struct timespec *time)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700455{
456 if (!(bitmap[1] & FATTR4_WORD1_TIME_METADATA))
457 return 0;
458 return encode_attr_time(xdr,time);
459}
460
Al Viroe6f684f2006-10-19 23:28:50 -0700461static __be32 encode_attr_mtime(struct xdr_stream *xdr, const uint32_t *bitmap, const struct timespec *time)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700462{
463 if (!(bitmap[1] & FATTR4_WORD1_TIME_MODIFY))
464 return 0;
465 return encode_attr_time(xdr,time);
466}
467
Al Viroe6f684f2006-10-19 23:28:50 -0700468static __be32 encode_compound_hdr_res(struct xdr_stream *xdr, struct cb_compound_hdr_res *hdr)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700469{
Al Viroe6f684f2006-10-19 23:28:50 -0700470 __be32 status;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700471
472 hdr->status = xdr_reserve_space(xdr, 4);
473 if (unlikely(hdr->status == NULL))
474 return htonl(NFS4ERR_RESOURCE);
475 status = encode_string(xdr, hdr->taglen, hdr->tag);
476 if (unlikely(status != 0))
477 return status;
478 hdr->nops = xdr_reserve_space(xdr, 4);
479 if (unlikely(hdr->nops == NULL))
480 return htonl(NFS4ERR_RESOURCE);
481 return 0;
482}
483
Al Viroe6f684f2006-10-19 23:28:50 -0700484static __be32 encode_op_hdr(struct xdr_stream *xdr, uint32_t op, __be32 res)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700485{
Al Viro5704fde2006-10-19 23:28:51 -0700486 __be32 *p;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700487
488 p = xdr_reserve_space(xdr, 8);
489 if (unlikely(p == NULL))
Andy Adamson31d2b432010-01-14 17:45:04 -0500490 return htonl(NFS4ERR_RESOURCE_HDR);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700491 *p++ = htonl(op);
492 *p = res;
493 return 0;
494}
495
Al Viroe6f684f2006-10-19 23:28:50 -0700496static __be32 encode_getattr_res(struct svc_rqst *rqstp, struct xdr_stream *xdr, const struct cb_getattrres *res)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700497{
Al Viro5704fde2006-10-19 23:28:51 -0700498 __be32 *savep = NULL;
Al Viroe6f684f2006-10-19 23:28:50 -0700499 __be32 status = res->status;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700500
501 if (unlikely(status != 0))
502 goto out;
503 status = encode_attr_bitmap(xdr, res->bitmap, &savep);
504 if (unlikely(status != 0))
505 goto out;
506 status = encode_attr_change(xdr, res->bitmap, res->change_attr);
507 if (unlikely(status != 0))
508 goto out;
509 status = encode_attr_size(xdr, res->bitmap, res->size);
510 if (unlikely(status != 0))
511 goto out;
512 status = encode_attr_ctime(xdr, res->bitmap, &res->ctime);
513 if (unlikely(status != 0))
514 goto out;
515 status = encode_attr_mtime(xdr, res->bitmap, &res->mtime);
516 *savep = htonl((unsigned int)((char *)xdr->p - (char *)(savep+1)));
517out:
Harvey Harrison3110ff82008-05-02 13:42:44 -0700518 dprintk("%s: exit with status = %d\n", __func__, ntohl(status));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700519 return status;
520}
521
Benny Halevy34bc47c92009-04-01 09:23:22 -0400522#if defined(CONFIG_NFS_V4_1)
523
Andy Adamson9733f0d2010-01-22 12:03:08 -0500524static __be32 encode_sessionid(struct xdr_stream *xdr,
Benny Halevy4aece6a2009-04-01 09:23:26 -0400525 const struct nfs4_sessionid *sid)
526{
Andy Adamson9733f0d2010-01-22 12:03:08 -0500527 __be32 *p;
Benny Halevy4aece6a2009-04-01 09:23:26 -0400528 int len = NFS4_MAX_SESSIONID_LEN;
529
530 p = xdr_reserve_space(xdr, len);
531 if (unlikely(p == NULL))
532 return htonl(NFS4ERR_RESOURCE);
533
534 memcpy(p, sid, len);
535 return 0;
536}
537
Andy Adamson9733f0d2010-01-22 12:03:08 -0500538static __be32 encode_cb_sequence_res(struct svc_rqst *rqstp,
Benny Halevy4aece6a2009-04-01 09:23:26 -0400539 struct xdr_stream *xdr,
540 const struct cb_sequenceres *res)
541{
Andy Adamson9733f0d2010-01-22 12:03:08 -0500542 __be32 *p;
Benny Halevy4aece6a2009-04-01 09:23:26 -0400543 unsigned status = res->csr_status;
544
545 if (unlikely(status != 0))
546 goto out;
547
548 encode_sessionid(xdr, &res->csr_sessionid);
549
550 p = xdr_reserve_space(xdr, 4 * sizeof(uint32_t));
551 if (unlikely(p == NULL))
552 return htonl(NFS4ERR_RESOURCE);
553
554 *p++ = htonl(res->csr_sequenceid);
555 *p++ = htonl(res->csr_slotid);
556 *p++ = htonl(res->csr_highestslotid);
557 *p++ = htonl(res->csr_target_highestslotid);
558out:
559 dprintk("%s: exit with status = %d\n", __func__, ntohl(status));
560 return status;
561}
562
Benny Halevy34bc47c92009-04-01 09:23:22 -0400563static __be32
564preprocess_nfs41_op(int nop, unsigned int op_nr, struct callback_op **op)
565{
Benny Halevy281fe152009-04-01 09:23:27 -0400566 if (op_nr == OP_CB_SEQUENCE) {
567 if (nop != 0)
568 return htonl(NFS4ERR_SEQUENCE_POS);
569 } else {
570 if (nop == 0)
571 return htonl(NFS4ERR_OP_NOT_IN_SESSION);
572 }
573
Benny Halevy34bc47c92009-04-01 09:23:22 -0400574 switch (op_nr) {
575 case OP_CB_GETATTR:
576 case OP_CB_RECALL:
Benny Halevy4aece6a2009-04-01 09:23:26 -0400577 case OP_CB_SEQUENCE:
Alexandros Batsakis31f09602009-12-05 13:27:02 -0500578 case OP_CB_RECALL_ANY:
Andy Adamsonb9efa1b2010-01-20 16:06:27 -0500579 case OP_CB_RECALL_SLOT:
Benny Halevy34bc47c92009-04-01 09:23:22 -0400580 *op = &callback_ops[op_nr];
581 break;
582
583 case OP_CB_LAYOUTRECALL:
584 case OP_CB_NOTIFY_DEVICEID:
585 case OP_CB_NOTIFY:
586 case OP_CB_PUSH_DELEG:
Benny Halevy34bc47c92009-04-01 09:23:22 -0400587 case OP_CB_RECALLABLE_OBJ_AVAIL:
Benny Halevy34bc47c92009-04-01 09:23:22 -0400588 case OP_CB_WANTS_CANCELLED:
589 case OP_CB_NOTIFY_LOCK:
590 return htonl(NFS4ERR_NOTSUPP);
591
592 default:
593 return htonl(NFS4ERR_OP_ILLEGAL);
594 }
595
596 return htonl(NFS_OK);
597}
598
599#else /* CONFIG_NFS_V4_1 */
600
601static __be32
602preprocess_nfs41_op(int nop, unsigned int op_nr, struct callback_op **op)
603{
604 return htonl(NFS4ERR_MINOR_VERS_MISMATCH);
605}
606
607#endif /* CONFIG_NFS_V4_1 */
608
609static __be32
610preprocess_nfs4_op(unsigned int op_nr, struct callback_op **op)
611{
612 switch (op_nr) {
613 case OP_CB_GETATTR:
614 case OP_CB_RECALL:
615 *op = &callback_ops[op_nr];
616 break;
617 default:
618 return htonl(NFS4ERR_OP_ILLEGAL);
619 }
620
621 return htonl(NFS_OK);
622}
623
624static __be32 process_op(uint32_t minorversion, int nop,
625 struct svc_rqst *rqstp,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700626 struct xdr_stream *xdr_in, void *argp,
Andy Adamsonc36fca52011-01-06 02:04:32 +0000627 struct xdr_stream *xdr_out, void *resp,
628 struct cb_process_state *cps)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700629{
Trond Myklebusta162a6b2006-03-20 13:44:10 -0500630 struct callback_op *op = &callback_ops[0];
Andy Adamson31d2b432010-01-14 17:45:04 -0500631 unsigned int op_nr;
Benny Halevy34bc47c92009-04-01 09:23:22 -0400632 __be32 status;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700633 long maxlen;
Al Viroe6f684f2006-10-19 23:28:50 -0700634 __be32 res;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700635
Harvey Harrison3110ff82008-05-02 13:42:44 -0700636 dprintk("%s: start\n", __func__);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700637 status = decode_op_hdr(xdr_in, &op_nr);
Andy Adamson31d2b432010-01-14 17:45:04 -0500638 if (unlikely(status))
639 return status;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700640
Benny Halevy34bc47c92009-04-01 09:23:22 -0400641 dprintk("%s: minorversion=%d nop=%d op_nr=%u\n",
642 __func__, minorversion, nop, op_nr);
643
644 status = minorversion ? preprocess_nfs41_op(nop, op_nr, &op) :
645 preprocess_nfs4_op(op_nr, &op);
646 if (status == htonl(NFS4ERR_OP_ILLEGAL))
647 op_nr = OP_CB_ILLEGAL;
Andy Adamsonb92b3012010-01-14 17:45:05 -0500648 if (status)
649 goto encode_hdr;
Andy Adamson31d2b432010-01-14 17:45:04 -0500650
Andy Adamsonc36fca52011-01-06 02:04:32 +0000651 if (cps->drc_status) {
652 status = cps->drc_status;
Andy Adamson49110962010-01-14 17:45:08 -0500653 goto encode_hdr;
654 }
655
Linus Torvalds1da177e2005-04-16 15:20:36 -0700656 maxlen = xdr_out->end - xdr_out->p;
657 if (maxlen > 0 && maxlen < PAGE_SIZE) {
Andy Adamsone95e60d2010-01-14 17:45:06 -0500658 status = op->decode_args(rqstp, xdr_in, argp);
659 if (likely(status == 0))
Andy Adamsonc36fca52011-01-06 02:04:32 +0000660 status = op->process_op(argp, resp, cps);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700661 } else
662 status = htonl(NFS4ERR_RESOURCE);
663
Andy Adamsonb92b3012010-01-14 17:45:05 -0500664encode_hdr:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700665 res = encode_op_hdr(xdr_out, op_nr, status);
Andy Adamson31d2b432010-01-14 17:45:04 -0500666 if (unlikely(res))
667 return res;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700668 if (op->encode_res != NULL && status == 0)
669 status = op->encode_res(rqstp, xdr_out, resp);
Harvey Harrison3110ff82008-05-02 13:42:44 -0700670 dprintk("%s: done, status = %d\n", __func__, ntohl(status));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700671 return status;
672}
673
674/*
675 * Decode, process and encode a COMPOUND
676 */
Al Viro7111c662006-10-19 23:28:45 -0700677static __be32 nfs4_callback_compound(struct svc_rqst *rqstp, void *argp, void *resp)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700678{
Trond Myklebust3a6258e2008-05-06 13:32:40 -0400679 struct cb_compound_hdr_arg hdr_arg = { 0 };
680 struct cb_compound_hdr_res hdr_res = { NULL };
Linus Torvalds1da177e2005-04-16 15:20:36 -0700681 struct xdr_stream xdr_in, xdr_out;
Andy Adamsonc36fca52011-01-06 02:04:32 +0000682 __be32 *p, status;
683 struct cb_process_state cps = {
684 .drc_status = 0,
685 .clp = NULL,
686 };
Trond Myklebust3a6258e2008-05-06 13:32:40 -0400687 unsigned int nops = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700688
Harvey Harrison3110ff82008-05-02 13:42:44 -0700689 dprintk("%s: start\n", __func__);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700690
691 xdr_init_decode(&xdr_in, &rqstp->rq_arg, rqstp->rq_arg.head[0].iov_base);
692
Al Viro5704fde2006-10-19 23:28:51 -0700693 p = (__be32*)((char *)rqstp->rq_res.head[0].iov_base + rqstp->rq_res.head[0].iov_len);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700694 xdr_init_encode(&xdr_out, &rqstp->rq_res, p);
695
Trond Myklebust3a6258e2008-05-06 13:32:40 -0400696 status = decode_compound_hdr_arg(&xdr_in, &hdr_arg);
697 if (status == __constant_htonl(NFS4ERR_RESOURCE))
698 return rpc_garbage_args;
699
Andy Adamsonc36fca52011-01-06 02:04:32 +0000700 if (hdr_arg.minorversion == 0) {
701 cps.clp = nfs4_find_client_ident(hdr_arg.cb_ident);
702 if (!cps.clp)
703 return rpc_drop_reply;
704 } else
705 cps.svc_sid = bc_xprt_sid(rqstp);
706
Linus Torvalds1da177e2005-04-16 15:20:36 -0700707 hdr_res.taglen = hdr_arg.taglen;
708 hdr_res.tag = hdr_arg.tag;
Trond Myklebust3a6258e2008-05-06 13:32:40 -0400709 if (encode_compound_hdr_res(&xdr_out, &hdr_res) != 0)
710 return rpc_system_err;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700711
Trond Myklebust3a6258e2008-05-06 13:32:40 -0400712 while (status == 0 && nops != hdr_arg.nops) {
Andy Adamson49110962010-01-14 17:45:08 -0500713 status = process_op(hdr_arg.minorversion, nops, rqstp,
Andy Adamsonc36fca52011-01-06 02:04:32 +0000714 &xdr_in, argp, &xdr_out, resp, &cps);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700715 nops++;
716 }
Trond Myklebust3a6258e2008-05-06 13:32:40 -0400717
Andy Adamson31d2b432010-01-14 17:45:04 -0500718 /* Buffer overflow in decode_ops_hdr or encode_ops_hdr. Return
719 * resource error in cb_compound status without returning op */
720 if (unlikely(status == htonl(NFS4ERR_RESOURCE_HDR))) {
721 status = htonl(NFS4ERR_RESOURCE);
722 nops--;
723 }
724
Linus Torvalds1da177e2005-04-16 15:20:36 -0700725 *hdr_res.status = status;
726 *hdr_res.nops = htonl(nops);
Andy Adamsonc36fca52011-01-06 02:04:32 +0000727 nfs_put_client(cps.clp);
Harvey Harrison3110ff82008-05-02 13:42:44 -0700728 dprintk("%s: done, status = %u\n", __func__, ntohl(status));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700729 return rpc_success;
730}
731
732/*
733 * Define NFS4 callback COMPOUND ops.
734 */
735static struct callback_op callback_ops[] = {
736 [0] = {
737 .res_maxsize = CB_OP_HDR_RES_MAXSZ,
738 },
739 [OP_CB_GETATTR] = {
740 .process_op = (callback_process_op_t)nfs4_callback_getattr,
741 .decode_args = (callback_decode_arg_t)decode_getattr_args,
742 .encode_res = (callback_encode_res_t)encode_getattr_res,
743 .res_maxsize = CB_OP_GETATTR_RES_MAXSZ,
744 },
745 [OP_CB_RECALL] = {
746 .process_op = (callback_process_op_t)nfs4_callback_recall,
747 .decode_args = (callback_decode_arg_t)decode_recall_args,
748 .res_maxsize = CB_OP_RECALL_RES_MAXSZ,
Benny Halevy4aece6a2009-04-01 09:23:26 -0400749 },
750#if defined(CONFIG_NFS_V4_1)
751 [OP_CB_SEQUENCE] = {
752 .process_op = (callback_process_op_t)nfs4_callback_sequence,
753 .decode_args = (callback_decode_arg_t)decode_cb_sequence_args,
754 .encode_res = (callback_encode_res_t)encode_cb_sequence_res,
755 .res_maxsize = CB_OP_SEQUENCE_RES_MAXSZ,
756 },
Alexandros Batsakis31f09602009-12-05 13:27:02 -0500757 [OP_CB_RECALL_ANY] = {
758 .process_op = (callback_process_op_t)nfs4_callback_recallany,
759 .decode_args = (callback_decode_arg_t)decode_recallany_args,
760 .res_maxsize = CB_OP_RECALLANY_RES_MAXSZ,
761 },
Andy Adamsonb9efa1b2010-01-20 16:06:27 -0500762 [OP_CB_RECALL_SLOT] = {
763 .process_op = (callback_process_op_t)nfs4_callback_recallslot,
764 .decode_args = (callback_decode_arg_t)decode_recallslot_args,
765 .res_maxsize = CB_OP_RECALLSLOT_RES_MAXSZ,
766 },
Benny Halevy4aece6a2009-04-01 09:23:26 -0400767#endif /* CONFIG_NFS_V4_1 */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700768};
769
770/*
771 * Define NFS4 callback procedures
772 */
773static struct svc_procedure nfs4_callback_procedures1[] = {
774 [CB_NULL] = {
775 .pc_func = nfs4_callback_null,
776 .pc_decode = (kxdrproc_t)nfs4_decode_void,
777 .pc_encode = (kxdrproc_t)nfs4_encode_void,
778 .pc_xdrressize = 1,
779 },
780 [CB_COMPOUND] = {
781 .pc_func = nfs4_callback_compound,
782 .pc_encode = (kxdrproc_t)nfs4_encode_void,
783 .pc_argsize = 256,
784 .pc_ressize = 256,
785 .pc_xdrressize = NFS4_CALLBACK_BUFSIZE,
786 }
787};
788
789struct svc_version nfs4_callback_version1 = {
790 .vs_vers = 1,
791 .vs_nproc = ARRAY_SIZE(nfs4_callback_procedures1),
792 .vs_proc = nfs4_callback_procedures1,
793 .vs_xdrsize = NFS4_CALLBACK_XDRSIZE,
794 .vs_dispatch = NULL,
Steve Dickson49697ee2009-10-13 16:07:33 -0400795 .vs_hidden = 1,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700796};
797
Alexandros Batsakis07bccc22009-12-05 13:19:01 -0500798struct svc_version nfs4_callback_version4 = {
799 .vs_vers = 4,
800 .vs_nproc = ARRAY_SIZE(nfs4_callback_procedures1),
801 .vs_proc = nfs4_callback_procedures1,
802 .vs_xdrsize = NFS4_CALLBACK_XDRSIZE,
803 .vs_dispatch = NULL,
804};