]>
Commit | Line | Data |
---|---|---|
1da177e4 LT |
1 | /* |
2 | * linux/fs/nfs/callback_xdr.c | |
3 | * | |
4 | * Copyright (C) 2004 Trond Myklebust | |
5 | * | |
6 | * NFSv4 callback encode/decode procedures | |
7 | */ | |
1da177e4 LT |
8 | #include <linux/kernel.h> |
9 | #include <linux/sunrpc/svc.h> | |
10 | #include <linux/nfs4.h> | |
11 | #include <linux/nfs_fs.h> | |
5a0e3ad6 | 12 | #include <linux/slab.h> |
c36fca52 | 13 | #include <linux/sunrpc/bc_xprt.h> |
4ce79717 | 14 | #include "nfs4_fs.h" |
1da177e4 | 15 | #include "callback.h" |
c36fca52 | 16 | #include "internal.h" |
1da177e4 LT |
17 | |
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 | ||
4aece6a1 BH |
26 | #if defined(CONFIG_NFS_V4_1) |
27 | #define CB_OP_SEQUENCE_RES_MAXSZ (CB_OP_HDR_RES_MAXSZ + \ | |
28 | 4 + 1 + 3) | |
31f09607 | 29 | #define CB_OP_RECALLANY_RES_MAXSZ (CB_OP_HDR_RES_MAXSZ) |
b9efa1b2 | 30 | #define CB_OP_RECALLSLOT_RES_MAXSZ (CB_OP_HDR_RES_MAXSZ) |
4aece6a1 BH |
31 | #endif /* CONFIG_NFS_V4_1 */ |
32 | ||
1da177e4 LT |
33 | #define NFSDBG_FACILITY NFSDBG_CALLBACK |
34 | ||
31d2b435 AA |
35 | /* Internal error code */ |
36 | #define NFS4ERR_RESOURCE_HDR 11050 | |
37 | ||
c36fca52 AA |
38 | typedef __be32 (*callback_process_op_t)(void *, void *, |
39 | struct cb_process_state *); | |
e6f684f6 AV |
40 | typedef __be32 (*callback_decode_arg_t)(struct svc_rqst *, struct xdr_stream *, void *); |
41 | typedef __be32 (*callback_encode_res_t)(struct svc_rqst *, struct xdr_stream *, void *); | |
1da177e4 LT |
42 | |
43 | ||
44 | struct 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 | ||
51 | static struct callback_op callback_ops[]; | |
52 | ||
7111c66e | 53 | static __be32 nfs4_callback_null(struct svc_rqst *rqstp, void *argp, void *resp) |
1da177e4 LT |
54 | { |
55 | return htonl(NFS4_OK); | |
56 | } | |
57 | ||
5704fdeb | 58 | static int nfs4_decode_void(struct svc_rqst *rqstp, __be32 *p, void *dummy) |
1da177e4 LT |
59 | { |
60 | return xdr_argsize_check(rqstp, p); | |
61 | } | |
62 | ||
5704fdeb | 63 | static int nfs4_encode_void(struct svc_rqst *rqstp, __be32 *p, void *dummy) |
1da177e4 LT |
64 | { |
65 | return xdr_ressize_check(rqstp, p); | |
66 | } | |
67 | ||
5704fdeb | 68 | static __be32 *read_buf(struct xdr_stream *xdr, int nbytes) |
1da177e4 | 69 | { |
5704fdeb | 70 | __be32 *p; |
1da177e4 LT |
71 | |
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 | ||
e6f684f6 | 78 | static __be32 decode_string(struct xdr_stream *xdr, unsigned int *len, const char **str) |
1da177e4 | 79 | { |
5704fdeb | 80 | __be32 *p; |
1da177e4 LT |
81 | |
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 | ||
e6f684f6 | 98 | static __be32 decode_fh(struct xdr_stream *xdr, struct nfs_fh *fh) |
1da177e4 | 99 | { |
5704fdeb | 100 | __be32 *p; |
1da177e4 LT |
101 | |
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 | ||
e6f684f6 | 116 | static __be32 decode_bitmap(struct xdr_stream *xdr, uint32_t *bitmap) |
1da177e4 | 117 | { |
5704fdeb | 118 | __be32 *p; |
1da177e4 LT |
119 | 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 | ||
e6f684f6 | 135 | static __be32 decode_stateid(struct xdr_stream *xdr, nfs4_stateid *stateid) |
1da177e4 | 136 | { |
5704fdeb | 137 | __be32 *p; |
1da177e4 LT |
138 | |
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 | ||
e6f684f6 | 146 | static __be32 decode_compound_hdr_arg(struct xdr_stream *xdr, struct cb_compound_hdr_arg *hdr) |
1da177e4 | 147 | { |
5704fdeb | 148 | __be32 *p; |
e6f684f6 | 149 | __be32 status; |
1da177e4 LT |
150 | |
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! */ | |
5cce428d | 155 | if (hdr->taglen > CB_OP_TAGLEN_MAXSZ - 12) { |
1da177e4 | 156 | printk("NFSv4 CALLBACK %s: client sent tag of length %u\n", |
3110ff80 | 157 | __func__, hdr->taglen); |
1da177e4 LT |
158 | return htonl(NFS4ERR_RESOURCE); |
159 | } | |
160 | p = read_buf(xdr, 12); | |
161 | if (unlikely(p == NULL)) | |
162 | return htonl(NFS4ERR_RESOURCE); | |
b8f2ef84 | 163 | hdr->minorversion = ntohl(*p++); |
48a9e2d2 BH |
164 | /* Check minor version is zero or one. */ |
165 | if (hdr->minorversion <= 1) { | |
c36fca52 | 166 | hdr->cb_ident = ntohl(*p++); /* ignored by v4.1 */ |
48a9e2d2 | 167 | } else { |
b8f2ef84 BH |
168 | printk(KERN_WARNING "%s: NFSv4 server callback with " |
169 | "illegal minor version %u!\n", | |
170 | __func__, hdr->minorversion); | |
1da177e4 LT |
171 | return htonl(NFS4ERR_MINOR_VERS_MISMATCH); |
172 | } | |
1da177e4 | 173 | hdr->nops = ntohl(*p); |
b8f2ef84 BH |
174 | dprintk("%s: minorversion %d nops %d\n", __func__, |
175 | hdr->minorversion, hdr->nops); | |
1da177e4 LT |
176 | return 0; |
177 | } | |
178 | ||
e6f684f6 | 179 | static __be32 decode_op_hdr(struct xdr_stream *xdr, unsigned int *op) |
1da177e4 | 180 | { |
5704fdeb | 181 | __be32 *p; |
1da177e4 LT |
182 | p = read_buf(xdr, 4); |
183 | if (unlikely(p == NULL)) | |
31d2b435 | 184 | return htonl(NFS4ERR_RESOURCE_HDR); |
1da177e4 LT |
185 | *op = ntohl(*p); |
186 | return 0; | |
187 | } | |
188 | ||
e6f684f6 | 189 | static __be32 decode_getattr_args(struct svc_rqst *rqstp, struct xdr_stream *xdr, struct cb_getattrargs *args) |
1da177e4 | 190 | { |
e6f684f6 | 191 | __be32 status; |
1da177e4 LT |
192 | |
193 | status = decode_fh(xdr, &args->fh); | |
194 | if (unlikely(status != 0)) | |
195 | goto out; | |
671beed7 | 196 | args->addr = svc_addr(rqstp); |
1da177e4 LT |
197 | status = decode_bitmap(xdr, args->bitmap); |
198 | out: | |
3110ff80 | 199 | dprintk("%s: exit with status = %d\n", __func__, ntohl(status)); |
1da177e4 LT |
200 | return status; |
201 | } | |
202 | ||
e6f684f6 | 203 | static __be32 decode_recall_args(struct svc_rqst *rqstp, struct xdr_stream *xdr, struct cb_recallargs *args) |
1da177e4 | 204 | { |
5704fdeb | 205 | __be32 *p; |
e6f684f6 | 206 | __be32 status; |
1da177e4 | 207 | |
c1d35866 | 208 | args->addr = svc_addr(rqstp); |
1da177e4 LT |
209 | 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); | |
219 | out: | |
3110ff80 | 220 | dprintk("%s: exit with status = %d\n", __func__, ntohl(status)); |
3873bc50 | 221 | return status; |
1da177e4 LT |
222 | } |
223 | ||
4aece6a1 BH |
224 | #if defined(CONFIG_NFS_V4_1) |
225 | ||
9733f0d9 | 226 | static __be32 decode_sessionid(struct xdr_stream *xdr, |
4aece6a1 BH |
227 | struct nfs4_sessionid *sid) |
228 | { | |
9733f0d9 | 229 | __be32 *p; |
4aece6a1 BH |
230 | int len = NFS4_MAX_SESSIONID_LEN; |
231 | ||
232 | p = read_buf(xdr, len); | |
233 | if (unlikely(p == NULL)) | |
a419aef8 | 234 | return htonl(NFS4ERR_RESOURCE); |
4aece6a1 BH |
235 | |
236 | memcpy(sid->data, p, len); | |
237 | return 0; | |
238 | } | |
239 | ||
9733f0d9 | 240 | static __be32 decode_rc_list(struct xdr_stream *xdr, |
4aece6a1 BH |
241 | struct referring_call_list *rc_list) |
242 | { | |
9733f0d9 | 243 | __be32 *p; |
4aece6a1 | 244 | int i; |
9733f0d9 | 245 | __be32 status; |
4aece6a1 BH |
246 | |
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 | ||
274 | out: | |
275 | return status; | |
276 | } | |
277 | ||
9733f0d9 | 278 | static __be32 decode_cb_sequence_args(struct svc_rqst *rqstp, |
4aece6a1 BH |
279 | struct xdr_stream *xdr, |
280 | struct cb_sequenceargs *args) | |
281 | { | |
9733f0d9 | 282 | __be32 *p; |
4aece6a1 | 283 | int i; |
9733f0d9 | 284 | __be32 status; |
4aece6a1 BH |
285 | |
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 | ||
65fc64e5 | 295 | args->csa_addr = svc_addr(rqstp); |
4aece6a1 BH |
296 | 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); | |
327 | out: | |
328 | dprintk("%s: exit with status = %d\n", __func__, ntohl(status)); | |
329 | return status; | |
330 | ||
331 | out_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 | ||
9733f0d9 | 338 | static __be32 decode_recallany_args(struct svc_rqst *rqstp, |
31f09607 AB |
339 | struct xdr_stream *xdr, |
340 | struct cb_recallanyargs *args) | |
341 | { | |
9733f0d9 | 342 | __be32 *p; |
31f09607 AB |
343 | |
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 | ||
9733f0d9 | 357 | static __be32 decode_recallslot_args(struct svc_rqst *rqstp, |
b9efa1b2 AA |
358 | 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 | ||
4aece6a1 BH |
371 | #endif /* CONFIG_NFS_V4_1 */ |
372 | ||
e6f684f6 | 373 | static __be32 encode_string(struct xdr_stream *xdr, unsigned int len, const char *str) |
1da177e4 | 374 | { |
5704fdeb | 375 | __be32 *p; |
1da177e4 LT |
376 | |
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) | |
5704fdeb | 386 | static __be32 encode_attr_bitmap(struct xdr_stream *xdr, const uint32_t *bitmap, __be32 **savep) |
1da177e4 | 387 | { |
5704fdeb AV |
388 | __be32 bm[2]; |
389 | __be32 *p; | |
1da177e4 LT |
390 | |
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 | ||
e6f684f6 | 416 | static __be32 encode_attr_change(struct xdr_stream *xdr, const uint32_t *bitmap, uint64_t change) |
1da177e4 | 417 | { |
5704fdeb | 418 | __be32 *p; |
1da177e4 LT |
419 | |
420 | if (!(bitmap[0] & FATTR4_WORD0_CHANGE)) | |
421 | return 0; | |
422 | p = xdr_reserve_space(xdr, 8); | |
90dc7d27 | 423 | if (unlikely(!p)) |
1da177e4 LT |
424 | return htonl(NFS4ERR_RESOURCE); |
425 | p = xdr_encode_hyper(p, change); | |
426 | return 0; | |
427 | } | |
428 | ||
e6f684f6 | 429 | static __be32 encode_attr_size(struct xdr_stream *xdr, const uint32_t *bitmap, uint64_t size) |
1da177e4 | 430 | { |
5704fdeb | 431 | __be32 *p; |
1da177e4 LT |
432 | |
433 | if (!(bitmap[0] & FATTR4_WORD0_SIZE)) | |
434 | return 0; | |
435 | p = xdr_reserve_space(xdr, 8); | |
90dc7d27 | 436 | if (unlikely(!p)) |
1da177e4 LT |
437 | return htonl(NFS4ERR_RESOURCE); |
438 | p = xdr_encode_hyper(p, size); | |
439 | return 0; | |
440 | } | |
441 | ||
e6f684f6 | 442 | static __be32 encode_attr_time(struct xdr_stream *xdr, const struct timespec *time) |
1da177e4 | 443 | { |
5704fdeb | 444 | __be32 *p; |
1da177e4 LT |
445 | |
446 | p = xdr_reserve_space(xdr, 12); | |
90dc7d27 | 447 | if (unlikely(!p)) |
1da177e4 LT |
448 | return htonl(NFS4ERR_RESOURCE); |
449 | p = xdr_encode_hyper(p, time->tv_sec); | |
450 | *p = htonl(time->tv_nsec); | |
451 | return 0; | |
452 | } | |
453 | ||
e6f684f6 | 454 | static __be32 encode_attr_ctime(struct xdr_stream *xdr, const uint32_t *bitmap, const struct timespec *time) |
1da177e4 LT |
455 | { |
456 | if (!(bitmap[1] & FATTR4_WORD1_TIME_METADATA)) | |
457 | return 0; | |
458 | return encode_attr_time(xdr,time); | |
459 | } | |
460 | ||
e6f684f6 | 461 | static __be32 encode_attr_mtime(struct xdr_stream *xdr, const uint32_t *bitmap, const struct timespec *time) |
1da177e4 LT |
462 | { |
463 | if (!(bitmap[1] & FATTR4_WORD1_TIME_MODIFY)) | |
464 | return 0; | |
465 | return encode_attr_time(xdr,time); | |
466 | } | |
467 | ||
e6f684f6 | 468 | static __be32 encode_compound_hdr_res(struct xdr_stream *xdr, struct cb_compound_hdr_res *hdr) |
1da177e4 | 469 | { |
e6f684f6 | 470 | __be32 status; |
1da177e4 LT |
471 | |
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 | ||
e6f684f6 | 484 | static __be32 encode_op_hdr(struct xdr_stream *xdr, uint32_t op, __be32 res) |
1da177e4 | 485 | { |
5704fdeb | 486 | __be32 *p; |
1da177e4 LT |
487 | |
488 | p = xdr_reserve_space(xdr, 8); | |
489 | if (unlikely(p == NULL)) | |
31d2b435 | 490 | return htonl(NFS4ERR_RESOURCE_HDR); |
1da177e4 LT |
491 | *p++ = htonl(op); |
492 | *p = res; | |
493 | return 0; | |
494 | } | |
495 | ||
e6f684f6 | 496 | static __be32 encode_getattr_res(struct svc_rqst *rqstp, struct xdr_stream *xdr, const struct cb_getattrres *res) |
1da177e4 | 497 | { |
5704fdeb | 498 | __be32 *savep = NULL; |
e6f684f6 | 499 | __be32 status = res->status; |
1da177e4 LT |
500 | |
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))); | |
517 | out: | |
3110ff80 | 518 | dprintk("%s: exit with status = %d\n", __func__, ntohl(status)); |
1da177e4 LT |
519 | return status; |
520 | } | |
521 | ||
34bc47c9 BH |
522 | #if defined(CONFIG_NFS_V4_1) |
523 | ||
9733f0d9 | 524 | static __be32 encode_sessionid(struct xdr_stream *xdr, |
4aece6a1 BH |
525 | const struct nfs4_sessionid *sid) |
526 | { | |
9733f0d9 | 527 | __be32 *p; |
4aece6a1 BH |
528 | 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 | ||
9733f0d9 | 538 | static __be32 encode_cb_sequence_res(struct svc_rqst *rqstp, |
4aece6a1 BH |
539 | struct xdr_stream *xdr, |
540 | const struct cb_sequenceres *res) | |
541 | { | |
9733f0d9 | 542 | __be32 *p; |
4aece6a1 BH |
543 | 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); | |
558 | out: | |
559 | dprintk("%s: exit with status = %d\n", __func__, ntohl(status)); | |
560 | return status; | |
561 | } | |
562 | ||
34bc47c9 BH |
563 | static __be32 |
564 | preprocess_nfs41_op(int nop, unsigned int op_nr, struct callback_op **op) | |
565 | { | |
281fe15d BH |
566 | 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 | ||
34bc47c9 BH |
574 | switch (op_nr) { |
575 | case OP_CB_GETATTR: | |
576 | case OP_CB_RECALL: | |
4aece6a1 | 577 | case OP_CB_SEQUENCE: |
31f09607 | 578 | case OP_CB_RECALL_ANY: |
b9efa1b2 | 579 | case OP_CB_RECALL_SLOT: |
34bc47c9 BH |
580 | *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: | |
34bc47c9 | 587 | case OP_CB_RECALLABLE_OBJ_AVAIL: |
34bc47c9 BH |
588 | 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 | ||
601 | static __be32 | |
602 | preprocess_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 | ||
609 | static __be32 | |
610 | preprocess_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 | ||
624 | static __be32 process_op(uint32_t minorversion, int nop, | |
625 | struct svc_rqst *rqstp, | |
1da177e4 | 626 | struct xdr_stream *xdr_in, void *argp, |
c36fca52 AA |
627 | struct xdr_stream *xdr_out, void *resp, |
628 | struct cb_process_state *cps) | |
1da177e4 | 629 | { |
a162a6b8 | 630 | struct callback_op *op = &callback_ops[0]; |
31d2b435 | 631 | unsigned int op_nr; |
34bc47c9 | 632 | __be32 status; |
1da177e4 | 633 | long maxlen; |
e6f684f6 | 634 | __be32 res; |
1da177e4 | 635 | |
3110ff80 | 636 | dprintk("%s: start\n", __func__); |
1da177e4 | 637 | status = decode_op_hdr(xdr_in, &op_nr); |
31d2b435 AA |
638 | if (unlikely(status)) |
639 | return status; | |
1da177e4 | 640 | |
34bc47c9 BH |
641 | 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; | |
b92b3019 AA |
648 | if (status) |
649 | goto encode_hdr; | |
31d2b435 | 650 | |
c36fca52 AA |
651 | if (cps->drc_status) { |
652 | status = cps->drc_status; | |
4911096f AA |
653 | goto encode_hdr; |
654 | } | |
655 | ||
1da177e4 LT |
656 | maxlen = xdr_out->end - xdr_out->p; |
657 | if (maxlen > 0 && maxlen < PAGE_SIZE) { | |
e95e60da AA |
658 | status = op->decode_args(rqstp, xdr_in, argp); |
659 | if (likely(status == 0)) | |
c36fca52 | 660 | status = op->process_op(argp, resp, cps); |
1da177e4 LT |
661 | } else |
662 | status = htonl(NFS4ERR_RESOURCE); | |
663 | ||
b92b3019 | 664 | encode_hdr: |
1da177e4 | 665 | res = encode_op_hdr(xdr_out, op_nr, status); |
31d2b435 AA |
666 | if (unlikely(res)) |
667 | return res; | |
1da177e4 LT |
668 | if (op->encode_res != NULL && status == 0) |
669 | status = op->encode_res(rqstp, xdr_out, resp); | |
3110ff80 | 670 | dprintk("%s: done, status = %d\n", __func__, ntohl(status)); |
1da177e4 LT |
671 | return status; |
672 | } | |
673 | ||
674 | /* | |
675 | * Decode, process and encode a COMPOUND | |
676 | */ | |
7111c66e | 677 | static __be32 nfs4_callback_compound(struct svc_rqst *rqstp, void *argp, void *resp) |
1da177e4 | 678 | { |
3a6258e1 TM |
679 | struct cb_compound_hdr_arg hdr_arg = { 0 }; |
680 | struct cb_compound_hdr_res hdr_res = { NULL }; | |
1da177e4 | 681 | struct xdr_stream xdr_in, xdr_out; |
c36fca52 AA |
682 | __be32 *p, status; |
683 | struct cb_process_state cps = { | |
684 | .drc_status = 0, | |
685 | .clp = NULL, | |
686 | }; | |
3a6258e1 | 687 | unsigned int nops = 0; |
1da177e4 | 688 | |
3110ff80 | 689 | dprintk("%s: start\n", __func__); |
1da177e4 LT |
690 | |
691 | xdr_init_decode(&xdr_in, &rqstp->rq_arg, rqstp->rq_arg.head[0].iov_base); | |
692 | ||
5704fdeb | 693 | p = (__be32*)((char *)rqstp->rq_res.head[0].iov_base + rqstp->rq_res.head[0].iov_len); |
1da177e4 LT |
694 | xdr_init_encode(&xdr_out, &rqstp->rq_res, p); |
695 | ||
3a6258e1 TM |
696 | status = decode_compound_hdr_arg(&xdr_in, &hdr_arg); |
697 | if (status == __constant_htonl(NFS4ERR_RESOURCE)) | |
698 | return rpc_garbage_args; | |
699 | ||
c36fca52 AA |
700 | 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 | ||
1da177e4 LT |
707 | hdr_res.taglen = hdr_arg.taglen; |
708 | hdr_res.tag = hdr_arg.tag; | |
3a6258e1 TM |
709 | if (encode_compound_hdr_res(&xdr_out, &hdr_res) != 0) |
710 | return rpc_system_err; | |
1da177e4 | 711 | |
3a6258e1 | 712 | while (status == 0 && nops != hdr_arg.nops) { |
4911096f | 713 | status = process_op(hdr_arg.minorversion, nops, rqstp, |
c36fca52 | 714 | &xdr_in, argp, &xdr_out, resp, &cps); |
1da177e4 LT |
715 | nops++; |
716 | } | |
3a6258e1 | 717 | |
31d2b435 AA |
718 | /* 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 | ||
1da177e4 LT |
725 | *hdr_res.status = status; |
726 | *hdr_res.nops = htonl(nops); | |
c36fca52 | 727 | nfs_put_client(cps.clp); |
3110ff80 | 728 | dprintk("%s: done, status = %u\n", __func__, ntohl(status)); |
1da177e4 LT |
729 | return rpc_success; |
730 | } | |
731 | ||
732 | /* | |
733 | * Define NFS4 callback COMPOUND ops. | |
734 | */ | |
735 | static 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, | |
4aece6a1 BH |
749 | }, |
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 | }, | |
31f09607 AB |
757 | [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 | }, | |
b9efa1b2 AA |
762 | [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 | }, | |
4aece6a1 | 767 | #endif /* CONFIG_NFS_V4_1 */ |
1da177e4 LT |
768 | }; |
769 | ||
770 | /* | |
771 | * Define NFS4 callback procedures | |
772 | */ | |
773 | static 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 | ||
789 | struct 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, | |
49697ee7 | 795 | .vs_hidden = 1, |
1da177e4 LT |
796 | }; |
797 | ||
07bccc2d AB |
798 | struct 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 | }; |