nfsd: clean up an xdr reserved space calculation
[deliverable/linux.git] / fs / nfsd / nfs4xdr.c
CommitLineData
1da177e4 1/*
1da177e4
LT
2 * Server-side XDR for NFSv4
3 *
4 * Copyright (c) 2002 The Regents of the University of Michigan.
5 * All rights reserved.
6 *
7 * Kendrick Smith <kmsmith@umich.edu>
8 * Andy Adamson <andros@umich.edu>
9 *
10 * Redistribution and use in source and binary forms, with or without
11 * modification, are permitted provided that the following conditions
12 * are met:
13 *
14 * 1. Redistributions of source code must retain the above copyright
15 * notice, this list of conditions and the following disclaimer.
16 * 2. Redistributions in binary form must reproduce the above copyright
17 * notice, this list of conditions and the following disclaimer in the
18 * documentation and/or other materials provided with the distribution.
19 * 3. Neither the name of the University nor the names of its
20 * contributors may be used to endorse or promote products derived
21 * from this software without specific prior written permission.
22 *
23 * THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESS OR IMPLIED
24 * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
25 * MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
26 * DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
27 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
28 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
29 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR
30 * BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
31 * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
32 * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
33 * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
34 *
35 * TODO: Neil Brown made the following observation: We currently
36 * initially reserve NFSD_BUFSIZE space on the transmit queue and
37 * never release any of that until the request is complete.
38 * It would be good to calculate a new maximum response size while
39 * decoding the COMPOUND, and call svc_reserve with this number
40 * at the end of nfs4svc_decode_compoundargs.
41 */
42
5a0e3ad6 43#include <linux/slab.h>
1da177e4 44#include <linux/namei.h>
341eb184 45#include <linux/statfs.h>
0733d213 46#include <linux/utsname.h>
17456804 47#include <linux/pagemap.h>
4796f457 48#include <linux/sunrpc/svcauth_gss.h>
9a74af21 49
2ca72e17
BF
50#include "idmap.h"
51#include "acl.h"
9a74af21 52#include "xdr4.h"
0a3adade 53#include "vfs.h"
17456804 54#include "state.h"
1091006c 55#include "cache.h"
3d733711 56#include "netns.h"
2ca72e17 57
18032ca0
DQ
58#ifdef CONFIG_NFSD_V4_SECURITY_LABEL
59#include <linux/security.h>
60#endif
61
62
1da177e4
LT
63#define NFSDDBG_FACILITY NFSDDBG_XDR
64
42ca0993
BF
65/*
66 * As per referral draft, the fsid for a referral MUST be different from the fsid of the containing
67 * directory in order to indicate to the client that a filesystem boundary is present
68 * We use a fixed fsid for a referral
69 */
70#define NFS4_REFERRAL_FSID_MAJOR 0x8000000ULL
71#define NFS4_REFERRAL_FSID_MINOR 0x8000000ULL
72
b37ad28b 73static __be32
a36b1725 74check_filename(char *str, int len)
1da177e4
LT
75{
76 int i;
77
78 if (len == 0)
79 return nfserr_inval;
80 if (isdotent(str, len))
a36b1725 81 return nfserr_badname;
1da177e4
LT
82 for (i = 0; i < len; i++)
83 if (str[i] == '/')
a36b1725 84 return nfserr_badname;
1da177e4
LT
85 return 0;
86}
87
1da177e4 88#define DECODE_HEAD \
2ebbc012 89 __be32 *p; \
b37ad28b 90 __be32 status
1da177e4
LT
91#define DECODE_TAIL \
92 status = 0; \
93out: \
94 return status; \
95xdr_error: \
817cb9d4
CL
96 dprintk("NFSD: xdr error (%s:%d)\n", \
97 __FILE__, __LINE__); \
1da177e4
LT
98 status = nfserr_bad_xdr; \
99 goto out
100
101#define READ32(x) (x) = ntohl(*p++)
102#define READ64(x) do { \
103 (x) = (u64)ntohl(*p++) << 32; \
104 (x) |= ntohl(*p++); \
105} while (0)
106#define READTIME(x) do { \
107 p++; \
108 (x) = ntohl(*p++); \
109 p++; \
110} while (0)
111#define READMEM(x,nbytes) do { \
112 x = (char *)p; \
113 p += XDR_QUADLEN(nbytes); \
114} while (0)
115#define SAVEMEM(x,nbytes) do { \
116 if (!(x = (p==argp->tmp || p == argp->tmpp) ? \
117 savemem(argp, p, nbytes) : \
118 (char *)p)) { \
817cb9d4
CL
119 dprintk("NFSD: xdr error (%s:%d)\n", \
120 __FILE__, __LINE__); \
1da177e4
LT
121 goto xdr_error; \
122 } \
123 p += XDR_QUADLEN(nbytes); \
124} while (0)
125#define COPYMEM(x,nbytes) do { \
126 memcpy((x), p, nbytes); \
127 p += XDR_QUADLEN(nbytes); \
128} while (0)
129
130/* READ_BUF, read_buf(): nbytes must be <= PAGE_SIZE */
131#define READ_BUF(nbytes) do { \
132 if (nbytes <= (u32)((char *)argp->end - (char *)argp->p)) { \
133 p = argp->p; \
134 argp->p += XDR_QUADLEN(nbytes); \
135 } else if (!(p = read_buf(argp, nbytes))) { \
817cb9d4
CL
136 dprintk("NFSD: xdr error (%s:%d)\n", \
137 __FILE__, __LINE__); \
1da177e4
LT
138 goto xdr_error; \
139 } \
140} while (0)
141
590b7431
BF
142static void next_decode_page(struct nfsd4_compoundargs *argp)
143{
590b7431 144 argp->p = page_address(argp->pagelist[0]);
365da4ad 145 argp->pagelist++;
590b7431
BF
146 if (argp->pagelen < PAGE_SIZE) {
147 argp->end = argp->p + (argp->pagelen>>2);
148 argp->pagelen = 0;
149 } else {
150 argp->end = argp->p + (PAGE_SIZE>>2);
151 argp->pagelen -= PAGE_SIZE;
152 }
153}
154
ca2a05aa 155static __be32 *read_buf(struct nfsd4_compoundargs *argp, u32 nbytes)
1da177e4
LT
156{
157 /* We want more bytes than seem to be available.
158 * Maybe we need a new page, maybe we have just run out
159 */
ca2a05aa 160 unsigned int avail = (char *)argp->end - (char *)argp->p;
2ebbc012 161 __be32 *p;
1da177e4
LT
162 if (avail + argp->pagelen < nbytes)
163 return NULL;
164 if (avail + PAGE_SIZE < nbytes) /* need more than a page !! */
165 return NULL;
166 /* ok, we can do it with the current plus the next page */
167 if (nbytes <= sizeof(argp->tmp))
168 p = argp->tmp;
169 else {
f99d49ad 170 kfree(argp->tmpp);
1da177e4
LT
171 p = argp->tmpp = kmalloc(nbytes, GFP_KERNEL);
172 if (!p)
173 return NULL;
174
175 }
ca2a05aa
BF
176 /*
177 * The following memcpy is safe because read_buf is always
178 * called with nbytes > avail, and the two cases above both
179 * guarantee p points to at least nbytes bytes.
180 */
1da177e4 181 memcpy(p, argp->p, avail);
590b7431 182 next_decode_page(argp);
1da177e4
LT
183 memcpy(((char*)p)+avail, argp->p, (nbytes - avail));
184 argp->p += XDR_QUADLEN(nbytes - avail);
185 return p;
186}
187
60adfc50
AA
188static int zero_clientid(clientid_t *clid)
189{
190 return (clid->cl_boot == 0) && (clid->cl_id == 0);
191}
192
2d8498db
CH
193/**
194 * defer_free - mark an allocation as deferred freed
195 * @argp: NFSv4 compound argument structure to be freed with
196 * @release: release callback to free @p, typically kfree()
197 * @p: pointer to be freed
198 *
199 * Marks @p to be freed when processing the compound operation
200 * described in @argp finishes.
201 */
1da177e4
LT
202static int
203defer_free(struct nfsd4_compoundargs *argp,
204 void (*release)(const void *), void *p)
205{
206 struct tmpbuf *tb;
207
208 tb = kmalloc(sizeof(*tb), GFP_KERNEL);
209 if (!tb)
210 return -ENOMEM;
211 tb->buf = p;
212 tb->release = release;
213 tb->next = argp->to_free;
214 argp->to_free = tb;
215 return 0;
216}
217
2d8498db
CH
218/**
219 * savemem - duplicate a chunk of memory for later processing
220 * @argp: NFSv4 compound argument structure to be freed with
221 * @p: pointer to be duplicated
222 * @nbytes: length to be duplicated
223 *
224 * Returns a pointer to a copy of @nbytes bytes of memory at @p
225 * that are preserved until processing of the NFSv4 compound
226 * operation described by @argp finishes.
227 */
2ebbc012 228static char *savemem(struct nfsd4_compoundargs *argp, __be32 *p, int nbytes)
1da177e4 229{
1da177e4 230 if (p == argp->tmp) {
67114fe6 231 p = kmemdup(argp->tmp, nbytes, GFP_KERNEL);
a4db5fe5
BF
232 if (!p)
233 return NULL;
1da177e4 234 } else {
73dff8be 235 BUG_ON(p != argp->tmpp);
1da177e4
LT
236 argp->tmpp = NULL;
237 }
238 if (defer_free(argp, kfree, p)) {
a4db5fe5 239 kfree(p);
1da177e4
LT
240 return NULL;
241 } else
242 return (char *)p;
243}
244
b37ad28b 245static __be32
1da177e4
LT
246nfsd4_decode_bitmap(struct nfsd4_compoundargs *argp, u32 *bmval)
247{
248 u32 bmlen;
249 DECODE_HEAD;
250
251 bmval[0] = 0;
252 bmval[1] = 0;
7e705706 253 bmval[2] = 0;
1da177e4
LT
254
255 READ_BUF(4);
256 READ32(bmlen);
257 if (bmlen > 1000)
258 goto xdr_error;
259
260 READ_BUF(bmlen << 2);
261 if (bmlen > 0)
262 READ32(bmval[0]);
263 if (bmlen > 1)
264 READ32(bmval[1]);
7e705706
AA
265 if (bmlen > 2)
266 READ32(bmval[2]);
1da177e4
LT
267
268 DECODE_TAIL;
269}
270
b37ad28b 271static __be32
3c8e0316 272nfsd4_decode_fattr(struct nfsd4_compoundargs *argp, u32 *bmval,
18032ca0
DQ
273 struct iattr *iattr, struct nfs4_acl **acl,
274 struct xdr_netobj *label)
1da177e4
LT
275{
276 int expected_len, len = 0;
277 u32 dummy32;
278 char *buf;
b8dd7b9a 279 int host_err;
1da177e4
LT
280
281 DECODE_HEAD;
282 iattr->ia_valid = 0;
283 if ((status = nfsd4_decode_bitmap(argp, bmval)))
284 return status;
285
1da177e4
LT
286 READ_BUF(4);
287 READ32(expected_len);
288
289 if (bmval[0] & FATTR4_WORD0_SIZE) {
290 READ_BUF(8);
291 len += 8;
292 READ64(iattr->ia_size);
293 iattr->ia_valid |= ATTR_SIZE;
294 }
295 if (bmval[0] & FATTR4_WORD0_ACL) {
64a817cf 296 u32 nace;
28e05dd8 297 struct nfs4_ace *ace;
1da177e4
LT
298
299 READ_BUF(4); len += 4;
300 READ32(nace);
301
28e05dd8
BF
302 if (nace > NFS4_ACL_MAX)
303 return nfserr_resource;
304
305 *acl = nfs4_acl_new(nace);
1da177e4 306 if (*acl == NULL) {
b8dd7b9a 307 host_err = -ENOMEM;
1da177e4
LT
308 goto out_nfserr;
309 }
28e05dd8 310 defer_free(argp, kfree, *acl);
1da177e4 311
28e05dd8
BF
312 (*acl)->naces = nace;
313 for (ace = (*acl)->aces; ace < (*acl)->aces + nace; ace++) {
1da177e4 314 READ_BUF(16); len += 16;
28e05dd8
BF
315 READ32(ace->type);
316 READ32(ace->flag);
317 READ32(ace->access_mask);
1da177e4
LT
318 READ32(dummy32);
319 READ_BUF(dummy32);
320 len += XDR_QUADLEN(dummy32) << 2;
321 READMEM(buf, dummy32);
28e05dd8 322 ace->whotype = nfs4_acl_get_whotype(buf, dummy32);
3c726023 323 status = nfs_ok;
28e05dd8 324 if (ace->whotype != NFS4_ACL_WHO_NAMED)
ab8e4aee 325 ;
28e05dd8 326 else if (ace->flag & NFS4_ACE_IDENTIFIER_GROUP)
3c726023 327 status = nfsd_map_name_to_gid(argp->rqstp,
ab8e4aee 328 buf, dummy32, &ace->who_gid);
1da177e4 329 else
3c726023 330 status = nfsd_map_name_to_uid(argp->rqstp,
ab8e4aee 331 buf, dummy32, &ace->who_uid);
3c726023
BF
332 if (status)
333 return status;
1da177e4
LT
334 }
335 } else
336 *acl = NULL;
337 if (bmval[1] & FATTR4_WORD1_MODE) {
338 READ_BUF(4);
339 len += 4;
340 READ32(iattr->ia_mode);
341 iattr->ia_mode &= (S_IFMT | S_IALLUGO);
342 iattr->ia_valid |= ATTR_MODE;
343 }
344 if (bmval[1] & FATTR4_WORD1_OWNER) {
345 READ_BUF(4);
346 len += 4;
347 READ32(dummy32);
348 READ_BUF(dummy32);
349 len += (XDR_QUADLEN(dummy32) << 2);
350 READMEM(buf, dummy32);
47c85291
N
351 if ((status = nfsd_map_name_to_uid(argp->rqstp, buf, dummy32, &iattr->ia_uid)))
352 return status;
1da177e4
LT
353 iattr->ia_valid |= ATTR_UID;
354 }
355 if (bmval[1] & FATTR4_WORD1_OWNER_GROUP) {
356 READ_BUF(4);
357 len += 4;
358 READ32(dummy32);
359 READ_BUF(dummy32);
360 len += (XDR_QUADLEN(dummy32) << 2);
361 READMEM(buf, dummy32);
47c85291
N
362 if ((status = nfsd_map_name_to_gid(argp->rqstp, buf, dummy32, &iattr->ia_gid)))
363 return status;
1da177e4
LT
364 iattr->ia_valid |= ATTR_GID;
365 }
366 if (bmval[1] & FATTR4_WORD1_TIME_ACCESS_SET) {
367 READ_BUF(4);
368 len += 4;
369 READ32(dummy32);
370 switch (dummy32) {
371 case NFS4_SET_TO_CLIENT_TIME:
372 /* We require the high 32 bits of 'seconds' to be 0, and we ignore
373 all 32 bits of 'nseconds'. */
374 READ_BUF(12);
375 len += 12;
bf8d9097 376 READ64(iattr->ia_atime.tv_sec);
1da177e4
LT
377 READ32(iattr->ia_atime.tv_nsec);
378 if (iattr->ia_atime.tv_nsec >= (u32)1000000000)
379 return nfserr_inval;
380 iattr->ia_valid |= (ATTR_ATIME | ATTR_ATIME_SET);
381 break;
382 case NFS4_SET_TO_SERVER_TIME:
383 iattr->ia_valid |= ATTR_ATIME;
384 break;
385 default:
386 goto xdr_error;
387 }
388 }
1da177e4
LT
389 if (bmval[1] & FATTR4_WORD1_TIME_MODIFY_SET) {
390 READ_BUF(4);
391 len += 4;
392 READ32(dummy32);
393 switch (dummy32) {
394 case NFS4_SET_TO_CLIENT_TIME:
395 /* We require the high 32 bits of 'seconds' to be 0, and we ignore
396 all 32 bits of 'nseconds'. */
397 READ_BUF(12);
398 len += 12;
bf8d9097 399 READ64(iattr->ia_mtime.tv_sec);
1da177e4
LT
400 READ32(iattr->ia_mtime.tv_nsec);
401 if (iattr->ia_mtime.tv_nsec >= (u32)1000000000)
402 return nfserr_inval;
403 iattr->ia_valid |= (ATTR_MTIME | ATTR_MTIME_SET);
404 break;
405 case NFS4_SET_TO_SERVER_TIME:
406 iattr->ia_valid |= ATTR_MTIME;
407 break;
408 default:
409 goto xdr_error;
410 }
411 }
18032ca0
DQ
412
413 label->len = 0;
414#ifdef CONFIG_NFSD_V4_SECURITY_LABEL
415 if (bmval[2] & FATTR4_WORD2_SECURITY_LABEL) {
416 READ_BUF(4);
417 len += 4;
418 READ32(dummy32); /* lfs: we don't use it */
419 READ_BUF(4);
420 len += 4;
421 READ32(dummy32); /* pi: we don't use it either */
422 READ_BUF(4);
423 len += 4;
424 READ32(dummy32);
425 READ_BUF(dummy32);
426 if (dummy32 > NFSD4_MAX_SEC_LABEL_LEN)
427 return nfserr_badlabel;
428 len += (XDR_QUADLEN(dummy32) << 2);
429 READMEM(buf, dummy32);
430 label->data = kzalloc(dummy32 + 1, GFP_KERNEL);
431 if (!label->data)
432 return nfserr_jukebox;
3378b7f4 433 label->len = dummy32;
18032ca0
DQ
434 defer_free(argp, kfree, label->data);
435 memcpy(label->data, buf, dummy32);
436 }
437#endif
438
3c8e0316
YZ
439 if (bmval[0] & ~NFSD_WRITEABLE_ATTRS_WORD0
440 || bmval[1] & ~NFSD_WRITEABLE_ATTRS_WORD1
441 || bmval[2] & ~NFSD_WRITEABLE_ATTRS_WORD2)
442 READ_BUF(expected_len - len);
443 else if (len != expected_len)
1da177e4
LT
444 goto xdr_error;
445
446 DECODE_TAIL;
447
448out_nfserr:
b8dd7b9a 449 status = nfserrno(host_err);
1da177e4
LT
450 goto out;
451}
452
e31a1b66
BH
453static __be32
454nfsd4_decode_stateid(struct nfsd4_compoundargs *argp, stateid_t *sid)
455{
456 DECODE_HEAD;
457
458 READ_BUF(sizeof(stateid_t));
459 READ32(sid->si_generation);
460 COPYMEM(&sid->si_opaque, sizeof(stateid_opaque_t));
461
462 DECODE_TAIL;
463}
464
b37ad28b 465static __be32
1da177e4
LT
466nfsd4_decode_access(struct nfsd4_compoundargs *argp, struct nfsd4_access *access)
467{
468 DECODE_HEAD;
469
470 READ_BUF(4);
471 READ32(access->ac_req_access);
472
473 DECODE_TAIL;
474}
475
acb2887e
BF
476static __be32 nfsd4_decode_cb_sec(struct nfsd4_compoundargs *argp, struct nfsd4_cb_sec *cbs)
477{
478 DECODE_HEAD;
12fc3e92 479 u32 dummy, uid, gid;
acb2887e
BF
480 char *machine_name;
481 int i;
482 int nr_secflavs;
483
484 /* callback_sec_params4 */
485 READ_BUF(4);
486 READ32(nr_secflavs);
57569a70
BF
487 if (nr_secflavs)
488 cbs->flavor = (u32)(-1);
489 else
490 /* Is this legal? Be generous, take it to mean AUTH_NONE: */
491 cbs->flavor = 0;
acb2887e
BF
492 for (i = 0; i < nr_secflavs; ++i) {
493 READ_BUF(4);
494 READ32(dummy);
495 switch (dummy) {
496 case RPC_AUTH_NULL:
497 /* Nothing to read */
12fc3e92
BF
498 if (cbs->flavor == (u32)(-1))
499 cbs->flavor = RPC_AUTH_NULL;
acb2887e
BF
500 break;
501 case RPC_AUTH_UNIX:
502 READ_BUF(8);
503 /* stamp */
504 READ32(dummy);
505
506 /* machine name */
507 READ32(dummy);
508 READ_BUF(dummy);
509 SAVEMEM(machine_name, dummy);
510
511 /* uid, gid */
512 READ_BUF(8);
12fc3e92
BF
513 READ32(uid);
514 READ32(gid);
acb2887e
BF
515
516 /* more gids */
517 READ_BUF(4);
518 READ32(dummy);
519 READ_BUF(dummy * 4);
12fc3e92 520 if (cbs->flavor == (u32)(-1)) {
03bc6d1c
EB
521 kuid_t kuid = make_kuid(&init_user_ns, uid);
522 kgid_t kgid = make_kgid(&init_user_ns, gid);
523 if (uid_valid(kuid) && gid_valid(kgid)) {
524 cbs->uid = kuid;
525 cbs->gid = kgid;
526 cbs->flavor = RPC_AUTH_UNIX;
527 } else {
528 dprintk("RPC_AUTH_UNIX with invalid"
529 "uid or gid ignoring!\n");
530 }
12fc3e92 531 }
acb2887e
BF
532 break;
533 case RPC_AUTH_GSS:
534 dprintk("RPC_AUTH_GSS callback secflavor "
535 "not supported!\n");
536 READ_BUF(8);
537 /* gcbp_service */
538 READ32(dummy);
539 /* gcbp_handle_from_server */
540 READ32(dummy);
541 READ_BUF(dummy);
542 p += XDR_QUADLEN(dummy);
543 /* gcbp_handle_from_client */
544 READ_BUF(4);
545 READ32(dummy);
546 READ_BUF(dummy);
547 break;
548 default:
549 dprintk("Illegal callback secflavor\n");
550 return nfserr_inval;
551 }
552 }
553 DECODE_TAIL;
554}
555
cb73a9f4
BF
556static __be32 nfsd4_decode_backchannel_ctl(struct nfsd4_compoundargs *argp, struct nfsd4_backchannel_ctl *bc)
557{
558 DECODE_HEAD;
559
560 READ_BUF(4);
561 READ32(bc->bc_cb_program);
562 nfsd4_decode_cb_sec(argp, &bc->bc_cb_sec);
563
564 DECODE_TAIL;
565}
566
1d1bc8f2
BF
567static __be32 nfsd4_decode_bind_conn_to_session(struct nfsd4_compoundargs *argp, struct nfsd4_bind_conn_to_session *bcts)
568{
569 DECODE_HEAD;
1d1bc8f2
BF
570
571 READ_BUF(NFS4_MAX_SESSIONID_LEN + 8);
572 COPYMEM(bcts->sessionid.data, NFS4_MAX_SESSIONID_LEN);
573 READ32(bcts->dir);
6ce2357f
BS
574 /* XXX: skipping ctsa_use_conn_in_rdma_mode. Perhaps Tom Tucker
575 * could help us figure out we should be using it. */
1d1bc8f2
BF
576 DECODE_TAIL;
577}
578
b37ad28b 579static __be32
1da177e4
LT
580nfsd4_decode_close(struct nfsd4_compoundargs *argp, struct nfsd4_close *close)
581{
582 DECODE_HEAD;
583
e31a1b66 584 READ_BUF(4);
1da177e4 585 READ32(close->cl_seqid);
e31a1b66 586 return nfsd4_decode_stateid(argp, &close->cl_stateid);
1da177e4
LT
587
588 DECODE_TAIL;
589}
590
591
b37ad28b 592static __be32
1da177e4
LT
593nfsd4_decode_commit(struct nfsd4_compoundargs *argp, struct nfsd4_commit *commit)
594{
595 DECODE_HEAD;
596
597 READ_BUF(12);
598 READ64(commit->co_offset);
599 READ32(commit->co_count);
600
601 DECODE_TAIL;
602}
603
b37ad28b 604static __be32
1da177e4
LT
605nfsd4_decode_create(struct nfsd4_compoundargs *argp, struct nfsd4_create *create)
606{
607 DECODE_HEAD;
608
609 READ_BUF(4);
610 READ32(create->cr_type);
611 switch (create->cr_type) {
612 case NF4LNK:
613 READ_BUF(4);
614 READ32(create->cr_linklen);
615 READ_BUF(create->cr_linklen);
616 SAVEMEM(create->cr_linkname, create->cr_linklen);
617 break;
618 case NF4BLK:
619 case NF4CHR:
620 READ_BUF(8);
621 READ32(create->cr_specdata1);
622 READ32(create->cr_specdata2);
623 break;
624 case NF4SOCK:
625 case NF4FIFO:
626 case NF4DIR:
627 default:
628 break;
629 }
630
631 READ_BUF(4);
632 READ32(create->cr_namelen);
633 READ_BUF(create->cr_namelen);
634 SAVEMEM(create->cr_name, create->cr_namelen);
a36b1725 635 if ((status = check_filename(create->cr_name, create->cr_namelen)))
1da177e4
LT
636 return status;
637
3c8e0316 638 status = nfsd4_decode_fattr(argp, create->cr_bmval, &create->cr_iattr,
18032ca0 639 &create->cr_acl, &create->cr_label);
c0d6fc8a 640 if (status)
1da177e4
LT
641 goto out;
642
643 DECODE_TAIL;
644}
645
b37ad28b 646static inline __be32
1da177e4
LT
647nfsd4_decode_delegreturn(struct nfsd4_compoundargs *argp, struct nfsd4_delegreturn *dr)
648{
e31a1b66 649 return nfsd4_decode_stateid(argp, &dr->dr_stateid);
1da177e4
LT
650}
651
b37ad28b 652static inline __be32
1da177e4
LT
653nfsd4_decode_getattr(struct nfsd4_compoundargs *argp, struct nfsd4_getattr *getattr)
654{
655 return nfsd4_decode_bitmap(argp, getattr->ga_bmval);
656}
657
b37ad28b 658static __be32
1da177e4
LT
659nfsd4_decode_link(struct nfsd4_compoundargs *argp, struct nfsd4_link *link)
660{
661 DECODE_HEAD;
662
663 READ_BUF(4);
664 READ32(link->li_namelen);
665 READ_BUF(link->li_namelen);
666 SAVEMEM(link->li_name, link->li_namelen);
a36b1725 667 if ((status = check_filename(link->li_name, link->li_namelen)))
1da177e4
LT
668 return status;
669
670 DECODE_TAIL;
671}
672
b37ad28b 673static __be32
1da177e4
LT
674nfsd4_decode_lock(struct nfsd4_compoundargs *argp, struct nfsd4_lock *lock)
675{
676 DECODE_HEAD;
677
1da177e4
LT
678 /*
679 * type, reclaim(boolean), offset, length, new_lock_owner(boolean)
680 */
681 READ_BUF(28);
682 READ32(lock->lk_type);
683 if ((lock->lk_type < NFS4_READ_LT) || (lock->lk_type > NFS4_WRITEW_LT))
684 goto xdr_error;
685 READ32(lock->lk_reclaim);
686 READ64(lock->lk_offset);
687 READ64(lock->lk_length);
688 READ32(lock->lk_is_new);
689
690 if (lock->lk_is_new) {
e31a1b66 691 READ_BUF(4);
1da177e4 692 READ32(lock->lk_new_open_seqid);
e31a1b66
BH
693 status = nfsd4_decode_stateid(argp, &lock->lk_new_open_stateid);
694 if (status)
695 return status;
696 READ_BUF(8 + sizeof(clientid_t));
1da177e4
LT
697 READ32(lock->lk_new_lock_seqid);
698 COPYMEM(&lock->lk_new_clientid, sizeof(clientid_t));
699 READ32(lock->lk_new_owner.len);
700 READ_BUF(lock->lk_new_owner.len);
701 READMEM(lock->lk_new_owner.data, lock->lk_new_owner.len);
702 } else {
e31a1b66
BH
703 status = nfsd4_decode_stateid(argp, &lock->lk_old_lock_stateid);
704 if (status)
705 return status;
706 READ_BUF(4);
1da177e4
LT
707 READ32(lock->lk_old_lock_seqid);
708 }
709
710 DECODE_TAIL;
711}
712
b37ad28b 713static __be32
1da177e4
LT
714nfsd4_decode_lockt(struct nfsd4_compoundargs *argp, struct nfsd4_lockt *lockt)
715{
716 DECODE_HEAD;
717
718 READ_BUF(32);
719 READ32(lockt->lt_type);
720 if((lockt->lt_type < NFS4_READ_LT) || (lockt->lt_type > NFS4_WRITEW_LT))
721 goto xdr_error;
722 READ64(lockt->lt_offset);
723 READ64(lockt->lt_length);
724 COPYMEM(&lockt->lt_clientid, 8);
725 READ32(lockt->lt_owner.len);
726 READ_BUF(lockt->lt_owner.len);
727 READMEM(lockt->lt_owner.data, lockt->lt_owner.len);
728
729 DECODE_TAIL;
730}
731
b37ad28b 732static __be32
1da177e4
LT
733nfsd4_decode_locku(struct nfsd4_compoundargs *argp, struct nfsd4_locku *locku)
734{
735 DECODE_HEAD;
736
e31a1b66 737 READ_BUF(8);
1da177e4
LT
738 READ32(locku->lu_type);
739 if ((locku->lu_type < NFS4_READ_LT) || (locku->lu_type > NFS4_WRITEW_LT))
740 goto xdr_error;
741 READ32(locku->lu_seqid);
e31a1b66
BH
742 status = nfsd4_decode_stateid(argp, &locku->lu_stateid);
743 if (status)
744 return status;
745 READ_BUF(16);
1da177e4
LT
746 READ64(locku->lu_offset);
747 READ64(locku->lu_length);
748
749 DECODE_TAIL;
750}
751
b37ad28b 752static __be32
1da177e4
LT
753nfsd4_decode_lookup(struct nfsd4_compoundargs *argp, struct nfsd4_lookup *lookup)
754{
755 DECODE_HEAD;
756
757 READ_BUF(4);
758 READ32(lookup->lo_len);
759 READ_BUF(lookup->lo_len);
760 SAVEMEM(lookup->lo_name, lookup->lo_len);
a36b1725 761 if ((status = check_filename(lookup->lo_name, lookup->lo_len)))
1da177e4
LT
762 return status;
763
764 DECODE_TAIL;
765}
766
2c8bd7e0 767static __be32 nfsd4_decode_share_access(struct nfsd4_compoundargs *argp, u32 *share_access, u32 *deleg_want, u32 *deleg_when)
04f9e664
BF
768{
769 __be32 *p;
770 u32 w;
771
772 READ_BUF(4);
773 READ32(w);
2c8bd7e0
BH
774 *share_access = w & NFS4_SHARE_ACCESS_MASK;
775 *deleg_want = w & NFS4_SHARE_WANT_MASK;
776 if (deleg_when)
777 *deleg_when = w & NFS4_SHARE_WHEN_MASK;
778
04f9e664
BF
779 switch (w & NFS4_SHARE_ACCESS_MASK) {
780 case NFS4_SHARE_ACCESS_READ:
781 case NFS4_SHARE_ACCESS_WRITE:
782 case NFS4_SHARE_ACCESS_BOTH:
783 break;
784 default:
785 return nfserr_bad_xdr;
786 }
fc0d14fe 787 w &= ~NFS4_SHARE_ACCESS_MASK;
04f9e664
BF
788 if (!w)
789 return nfs_ok;
790 if (!argp->minorversion)
791 return nfserr_bad_xdr;
792 switch (w & NFS4_SHARE_WANT_MASK) {
793 case NFS4_SHARE_WANT_NO_PREFERENCE:
794 case NFS4_SHARE_WANT_READ_DELEG:
795 case NFS4_SHARE_WANT_WRITE_DELEG:
796 case NFS4_SHARE_WANT_ANY_DELEG:
797 case NFS4_SHARE_WANT_NO_DELEG:
798 case NFS4_SHARE_WANT_CANCEL:
799 break;
800 default:
801 return nfserr_bad_xdr;
802 }
92bac8c5 803 w &= ~NFS4_SHARE_WANT_MASK;
04f9e664
BF
804 if (!w)
805 return nfs_ok;
2c8bd7e0
BH
806
807 if (!deleg_when) /* open_downgrade */
808 return nfserr_inval;
04f9e664
BF
809 switch (w) {
810 case NFS4_SHARE_SIGNAL_DELEG_WHEN_RESRC_AVAIL:
811 case NFS4_SHARE_PUSH_DELEG_WHEN_UNCONTENDED:
c668fc6d
BH
812 case (NFS4_SHARE_SIGNAL_DELEG_WHEN_RESRC_AVAIL |
813 NFS4_SHARE_PUSH_DELEG_WHEN_UNCONTENDED):
04f9e664
BF
814 return nfs_ok;
815 }
816xdr_error:
817 return nfserr_bad_xdr;
818}
819
820static __be32 nfsd4_decode_share_deny(struct nfsd4_compoundargs *argp, u32 *x)
821{
822 __be32 *p;
823
824 READ_BUF(4);
825 READ32(*x);
826 /* Note: unlinke access bits, deny bits may be zero. */
01cd4afa 827 if (*x & ~NFS4_SHARE_DENY_BOTH)
04f9e664
BF
828 return nfserr_bad_xdr;
829 return nfs_ok;
830xdr_error:
831 return nfserr_bad_xdr;
832}
833
a084daf5
BF
834static __be32 nfsd4_decode_opaque(struct nfsd4_compoundargs *argp, struct xdr_netobj *o)
835{
836 __be32 *p;
837
838 READ_BUF(4);
839 READ32(o->len);
840
841 if (o->len == 0 || o->len > NFS4_OPAQUE_LIMIT)
842 return nfserr_bad_xdr;
843
844 READ_BUF(o->len);
845 SAVEMEM(o->data, o->len);
846 return nfs_ok;
847xdr_error:
848 return nfserr_bad_xdr;
849}
850
b37ad28b 851static __be32
1da177e4
LT
852nfsd4_decode_open(struct nfsd4_compoundargs *argp, struct nfsd4_open *open)
853{
854 DECODE_HEAD;
2c8bd7e0 855 u32 dummy;
1da177e4
LT
856
857 memset(open->op_bmval, 0, sizeof(open->op_bmval));
858 open->op_iattr.ia_valid = 0;
fe0750e5 859 open->op_openowner = NULL;
1da177e4 860
9d313b17 861 open->op_xdr_error = 0;
1da177e4 862 /* seqid, share_access, share_deny, clientid, ownerlen */
04f9e664 863 READ_BUF(4);
1da177e4 864 READ32(open->op_seqid);
2c8bd7e0
BH
865 /* decode, yet ignore deleg_when until supported */
866 status = nfsd4_decode_share_access(argp, &open->op_share_access,
867 &open->op_deleg_want, &dummy);
04f9e664
BF
868 if (status)
869 goto xdr_error;
870 status = nfsd4_decode_share_deny(argp, &open->op_share_deny);
871 if (status)
872 goto xdr_error;
a084daf5 873 READ_BUF(sizeof(clientid_t));
1da177e4 874 COPYMEM(&open->op_clientid, sizeof(clientid_t));
a084daf5
BF
875 status = nfsd4_decode_opaque(argp, &open->op_owner);
876 if (status)
877 goto xdr_error;
878 READ_BUF(4);
1da177e4
LT
879 READ32(open->op_create);
880 switch (open->op_create) {
881 case NFS4_OPEN_NOCREATE:
882 break;
883 case NFS4_OPEN_CREATE:
884 READ_BUF(4);
885 READ32(open->op_createmode);
886 switch (open->op_createmode) {
887 case NFS4_CREATE_UNCHECKED:
888 case NFS4_CREATE_GUARDED:
c0d6fc8a 889 status = nfsd4_decode_fattr(argp, open->op_bmval,
18032ca0 890 &open->op_iattr, &open->op_acl, &open->op_label);
c0d6fc8a 891 if (status)
1da177e4
LT
892 goto out;
893 break;
894 case NFS4_CREATE_EXCLUSIVE:
ab4684d1
CL
895 READ_BUF(NFS4_VERIFIER_SIZE);
896 COPYMEM(open->op_verf.data, NFS4_VERIFIER_SIZE);
1da177e4 897 break;
79fb54ab
BH
898 case NFS4_CREATE_EXCLUSIVE4_1:
899 if (argp->minorversion < 1)
900 goto xdr_error;
ab4684d1
CL
901 READ_BUF(NFS4_VERIFIER_SIZE);
902 COPYMEM(open->op_verf.data, NFS4_VERIFIER_SIZE);
79fb54ab 903 status = nfsd4_decode_fattr(argp, open->op_bmval,
18032ca0 904 &open->op_iattr, &open->op_acl, &open->op_label);
79fb54ab
BH
905 if (status)
906 goto out;
907 break;
1da177e4
LT
908 default:
909 goto xdr_error;
910 }
911 break;
912 default:
913 goto xdr_error;
914 }
915
916 /* open_claim */
917 READ_BUF(4);
918 READ32(open->op_claim_type);
919 switch (open->op_claim_type) {
920 case NFS4_OPEN_CLAIM_NULL:
921 case NFS4_OPEN_CLAIM_DELEGATE_PREV:
922 READ_BUF(4);
923 READ32(open->op_fname.len);
924 READ_BUF(open->op_fname.len);
925 SAVEMEM(open->op_fname.data, open->op_fname.len);
a36b1725 926 if ((status = check_filename(open->op_fname.data, open->op_fname.len)))
1da177e4
LT
927 return status;
928 break;
929 case NFS4_OPEN_CLAIM_PREVIOUS:
930 READ_BUF(4);
931 READ32(open->op_delegate_type);
932 break;
933 case NFS4_OPEN_CLAIM_DELEGATE_CUR:
e31a1b66
BH
934 status = nfsd4_decode_stateid(argp, &open->op_delegate_stateid);
935 if (status)
936 return status;
937 READ_BUF(4);
1da177e4
LT
938 READ32(open->op_fname.len);
939 READ_BUF(open->op_fname.len);
940 SAVEMEM(open->op_fname.data, open->op_fname.len);
a36b1725 941 if ((status = check_filename(open->op_fname.data, open->op_fname.len)))
1da177e4
LT
942 return status;
943 break;
8b289b2c
BF
944 case NFS4_OPEN_CLAIM_FH:
945 case NFS4_OPEN_CLAIM_DELEG_PREV_FH:
946 if (argp->minorversion < 1)
947 goto xdr_error;
948 /* void */
949 break;
950 case NFS4_OPEN_CLAIM_DELEG_CUR_FH:
951 if (argp->minorversion < 1)
952 goto xdr_error;
953 status = nfsd4_decode_stateid(argp, &open->op_delegate_stateid);
954 if (status)
955 return status;
956 break;
1da177e4
LT
957 default:
958 goto xdr_error;
959 }
960
961 DECODE_TAIL;
962}
963
b37ad28b 964static __be32
1da177e4
LT
965nfsd4_decode_open_confirm(struct nfsd4_compoundargs *argp, struct nfsd4_open_confirm *open_conf)
966{
967 DECODE_HEAD;
e1a90ebd
AS
968
969 if (argp->minorversion >= 1)
970 return nfserr_notsupp;
971
e31a1b66
BH
972 status = nfsd4_decode_stateid(argp, &open_conf->oc_req_stateid);
973 if (status)
974 return status;
975 READ_BUF(4);
1da177e4 976 READ32(open_conf->oc_seqid);
e1a90ebd 977
1da177e4
LT
978 DECODE_TAIL;
979}
980
b37ad28b 981static __be32
1da177e4
LT
982nfsd4_decode_open_downgrade(struct nfsd4_compoundargs *argp, struct nfsd4_open_downgrade *open_down)
983{
984 DECODE_HEAD;
985
e31a1b66
BH
986 status = nfsd4_decode_stateid(argp, &open_down->od_stateid);
987 if (status)
988 return status;
04f9e664 989 READ_BUF(4);
1da177e4 990 READ32(open_down->od_seqid);
2c8bd7e0
BH
991 status = nfsd4_decode_share_access(argp, &open_down->od_share_access,
992 &open_down->od_deleg_want, NULL);
04f9e664
BF
993 if (status)
994 return status;
995 status = nfsd4_decode_share_deny(argp, &open_down->od_share_deny);
996 if (status)
997 return status;
1da177e4
LT
998 DECODE_TAIL;
999}
1000
b37ad28b 1001static __be32
1da177e4
LT
1002nfsd4_decode_putfh(struct nfsd4_compoundargs *argp, struct nfsd4_putfh *putfh)
1003{
1004 DECODE_HEAD;
1005
1006 READ_BUF(4);
1007 READ32(putfh->pf_fhlen);
1008 if (putfh->pf_fhlen > NFS4_FHSIZE)
1009 goto xdr_error;
1010 READ_BUF(putfh->pf_fhlen);
1011 SAVEMEM(putfh->pf_fhval, putfh->pf_fhlen);
1012
1013 DECODE_TAIL;
1014}
1015
e1a90ebd
AS
1016static __be32
1017nfsd4_decode_putpubfh(struct nfsd4_compoundargs *argp, void *p)
1018{
1019 if (argp->minorversion == 0)
1020 return nfs_ok;
1021 return nfserr_notsupp;
1022}
1023
b37ad28b 1024static __be32
1da177e4
LT
1025nfsd4_decode_read(struct nfsd4_compoundargs *argp, struct nfsd4_read *read)
1026{
1027 DECODE_HEAD;
1028
e31a1b66
BH
1029 status = nfsd4_decode_stateid(argp, &read->rd_stateid);
1030 if (status)
1031 return status;
1032 READ_BUF(12);
1da177e4
LT
1033 READ64(read->rd_offset);
1034 READ32(read->rd_length);
1035
1036 DECODE_TAIL;
1037}
1038
b37ad28b 1039static __be32
1da177e4
LT
1040nfsd4_decode_readdir(struct nfsd4_compoundargs *argp, struct nfsd4_readdir *readdir)
1041{
1042 DECODE_HEAD;
1043
1044 READ_BUF(24);
1045 READ64(readdir->rd_cookie);
1046 COPYMEM(readdir->rd_verf.data, sizeof(readdir->rd_verf.data));
1047 READ32(readdir->rd_dircount); /* just in case you needed a useless field... */
1048 READ32(readdir->rd_maxcount);
1049 if ((status = nfsd4_decode_bitmap(argp, readdir->rd_bmval)))
1050 goto out;
1051
1052 DECODE_TAIL;
1053}
1054
b37ad28b 1055static __be32
1da177e4
LT
1056nfsd4_decode_remove(struct nfsd4_compoundargs *argp, struct nfsd4_remove *remove)
1057{
1058 DECODE_HEAD;
1059
1060 READ_BUF(4);
1061 READ32(remove->rm_namelen);
1062 READ_BUF(remove->rm_namelen);
1063 SAVEMEM(remove->rm_name, remove->rm_namelen);
a36b1725 1064 if ((status = check_filename(remove->rm_name, remove->rm_namelen)))
1da177e4
LT
1065 return status;
1066
1067 DECODE_TAIL;
1068}
1069
b37ad28b 1070static __be32
1da177e4
LT
1071nfsd4_decode_rename(struct nfsd4_compoundargs *argp, struct nfsd4_rename *rename)
1072{
1073 DECODE_HEAD;
1074
1075 READ_BUF(4);
1076 READ32(rename->rn_snamelen);
1077 READ_BUF(rename->rn_snamelen + 4);
1078 SAVEMEM(rename->rn_sname, rename->rn_snamelen);
1079 READ32(rename->rn_tnamelen);
1080 READ_BUF(rename->rn_tnamelen);
1081 SAVEMEM(rename->rn_tname, rename->rn_tnamelen);
a36b1725 1082 if ((status = check_filename(rename->rn_sname, rename->rn_snamelen)))
1da177e4 1083 return status;
a36b1725 1084 if ((status = check_filename(rename->rn_tname, rename->rn_tnamelen)))
1da177e4
LT
1085 return status;
1086
1087 DECODE_TAIL;
1088}
1089
b37ad28b 1090static __be32
1da177e4
LT
1091nfsd4_decode_renew(struct nfsd4_compoundargs *argp, clientid_t *clientid)
1092{
1093 DECODE_HEAD;
1094
e1a90ebd
AS
1095 if (argp->minorversion >= 1)
1096 return nfserr_notsupp;
1097
1da177e4
LT
1098 READ_BUF(sizeof(clientid_t));
1099 COPYMEM(clientid, sizeof(clientid_t));
1100
1101 DECODE_TAIL;
1102}
1103
dcb488a3
AA
1104static __be32
1105nfsd4_decode_secinfo(struct nfsd4_compoundargs *argp,
1106 struct nfsd4_secinfo *secinfo)
1107{
1108 DECODE_HEAD;
1109
1110 READ_BUF(4);
1111 READ32(secinfo->si_namelen);
1112 READ_BUF(secinfo->si_namelen);
1113 SAVEMEM(secinfo->si_name, secinfo->si_namelen);
a36b1725 1114 status = check_filename(secinfo->si_name, secinfo->si_namelen);
dcb488a3
AA
1115 if (status)
1116 return status;
1117 DECODE_TAIL;
1118}
1119
04f4ad16
BF
1120static __be32
1121nfsd4_decode_secinfo_no_name(struct nfsd4_compoundargs *argp,
1122 struct nfsd4_secinfo_no_name *sin)
1123{
1124 DECODE_HEAD;
1125
1126 READ_BUF(4);
1127 READ32(sin->sin_style);
1128 DECODE_TAIL;
1129}
1130
b37ad28b 1131static __be32
1da177e4
LT
1132nfsd4_decode_setattr(struct nfsd4_compoundargs *argp, struct nfsd4_setattr *setattr)
1133{
e31a1b66 1134 __be32 status;
1da177e4 1135
e31a1b66
BH
1136 status = nfsd4_decode_stateid(argp, &setattr->sa_stateid);
1137 if (status)
1138 return status;
3c8e0316 1139 return nfsd4_decode_fattr(argp, setattr->sa_bmval, &setattr->sa_iattr,
18032ca0 1140 &setattr->sa_acl, &setattr->sa_label);
1da177e4
LT
1141}
1142
b37ad28b 1143static __be32
1da177e4
LT
1144nfsd4_decode_setclientid(struct nfsd4_compoundargs *argp, struct nfsd4_setclientid *setclientid)
1145{
1146 DECODE_HEAD;
1147
e1a90ebd
AS
1148 if (argp->minorversion >= 1)
1149 return nfserr_notsupp;
1150
ab4684d1
CL
1151 READ_BUF(NFS4_VERIFIER_SIZE);
1152 COPYMEM(setclientid->se_verf.data, NFS4_VERIFIER_SIZE);
1da177e4 1153
a084daf5
BF
1154 status = nfsd4_decode_opaque(argp, &setclientid->se_name);
1155 if (status)
1156 return nfserr_bad_xdr;
1157 READ_BUF(8);
1da177e4
LT
1158 READ32(setclientid->se_callback_prog);
1159 READ32(setclientid->se_callback_netid_len);
1160
1161 READ_BUF(setclientid->se_callback_netid_len + 4);
1162 SAVEMEM(setclientid->se_callback_netid_val, setclientid->se_callback_netid_len);
1163 READ32(setclientid->se_callback_addr_len);
1164
1165 READ_BUF(setclientid->se_callback_addr_len + 4);
1166 SAVEMEM(setclientid->se_callback_addr_val, setclientid->se_callback_addr_len);
1167 READ32(setclientid->se_callback_ident);
1168
1169 DECODE_TAIL;
1170}
1171
b37ad28b 1172static __be32
1da177e4
LT
1173nfsd4_decode_setclientid_confirm(struct nfsd4_compoundargs *argp, struct nfsd4_setclientid_confirm *scd_c)
1174{
1175 DECODE_HEAD;
1176
e1a90ebd
AS
1177 if (argp->minorversion >= 1)
1178 return nfserr_notsupp;
1179
ab4684d1 1180 READ_BUF(8 + NFS4_VERIFIER_SIZE);
1da177e4 1181 COPYMEM(&scd_c->sc_clientid, 8);
ab4684d1 1182 COPYMEM(&scd_c->sc_confirm, NFS4_VERIFIER_SIZE);
1da177e4
LT
1183
1184 DECODE_TAIL;
1185}
1186
1187/* Also used for NVERIFY */
b37ad28b 1188static __be32
1da177e4
LT
1189nfsd4_decode_verify(struct nfsd4_compoundargs *argp, struct nfsd4_verify *verify)
1190{
1da177e4
LT
1191 DECODE_HEAD;
1192
1193 if ((status = nfsd4_decode_bitmap(argp, verify->ve_bmval)))
1194 goto out;
1195
1196 /* For convenience's sake, we compare raw xdr'd attributes in
e5f95703
BF
1197 * nfsd4_proc_verify */
1198
1da177e4
LT
1199 READ_BUF(4);
1200 READ32(verify->ve_attrlen);
1201 READ_BUF(verify->ve_attrlen);
1202 SAVEMEM(verify->ve_attrval, verify->ve_attrlen);
1203
1204 DECODE_TAIL;
1205}
1206
b37ad28b 1207static __be32
1da177e4
LT
1208nfsd4_decode_write(struct nfsd4_compoundargs *argp, struct nfsd4_write *write)
1209{
1210 int avail;
1da177e4
LT
1211 int len;
1212 DECODE_HEAD;
1213
e31a1b66
BH
1214 status = nfsd4_decode_stateid(argp, &write->wr_stateid);
1215 if (status)
1216 return status;
1217 READ_BUF(16);
1da177e4
LT
1218 READ64(write->wr_offset);
1219 READ32(write->wr_stable_how);
1220 if (write->wr_stable_how > 2)
1221 goto xdr_error;
1222 READ32(write->wr_buflen);
1223
1224 /* Sorry .. no magic macros for this.. *
1225 * READ_BUF(write->wr_buflen);
1226 * SAVEMEM(write->wr_buf, write->wr_buflen);
1227 */
1228 avail = (char*)argp->end - (char*)argp->p;
1229 if (avail + argp->pagelen < write->wr_buflen) {
817cb9d4
CL
1230 dprintk("NFSD: xdr error (%s:%d)\n",
1231 __FILE__, __LINE__);
1da177e4
LT
1232 goto xdr_error;
1233 }
70cc7f75
BF
1234 write->wr_head.iov_base = p;
1235 write->wr_head.iov_len = avail;
5a80a54d 1236 WARN_ON(avail != (XDR_QUADLEN(avail) << 2));
70cc7f75 1237 write->wr_pagelist = argp->pagelist;
5a80a54d
BF
1238
1239 len = XDR_QUADLEN(write->wr_buflen) << 2;
1240 if (len >= avail) {
1241 int pages;
1242
1243 len -= avail;
1244
1245 pages = len >> PAGE_SHIFT;
1246 argp->pagelist += pages;
1247 argp->pagelen -= pages * PAGE_SIZE;
1248 len -= pages * PAGE_SIZE;
1249
1250 argp->p = (__be32 *)page_address(argp->pagelist[0]);
365da4ad 1251 argp->pagelist++;
5a80a54d 1252 argp->end = argp->p + XDR_QUADLEN(PAGE_SIZE);
1da177e4 1253 }
5a80a54d 1254 argp->p += XDR_QUADLEN(len);
1da177e4
LT
1255
1256 DECODE_TAIL;
1257}
1258
b37ad28b 1259static __be32
1da177e4
LT
1260nfsd4_decode_release_lockowner(struct nfsd4_compoundargs *argp, struct nfsd4_release_lockowner *rlockowner)
1261{
1262 DECODE_HEAD;
1263
e1a90ebd
AS
1264 if (argp->minorversion >= 1)
1265 return nfserr_notsupp;
1266
1da177e4
LT
1267 READ_BUF(12);
1268 COPYMEM(&rlockowner->rl_clientid, sizeof(clientid_t));
1269 READ32(rlockowner->rl_owner.len);
1270 READ_BUF(rlockowner->rl_owner.len);
1271 READMEM(rlockowner->rl_owner.data, rlockowner->rl_owner.len);
1272
60adfc50
AA
1273 if (argp->minorversion && !zero_clientid(&rlockowner->rl_clientid))
1274 return nfserr_inval;
1da177e4
LT
1275 DECODE_TAIL;
1276}
1277
2db134eb
AA
1278static __be32
1279nfsd4_decode_exchange_id(struct nfsd4_compoundargs *argp,
0733d213 1280 struct nfsd4_exchange_id *exid)
2db134eb 1281{
5afa040b 1282 int dummy, tmp;
0733d213
AA
1283 DECODE_HEAD;
1284
1285 READ_BUF(NFS4_VERIFIER_SIZE);
1286 COPYMEM(exid->verifier.data, NFS4_VERIFIER_SIZE);
1287
a084daf5
BF
1288 status = nfsd4_decode_opaque(argp, &exid->clname);
1289 if (status)
1290 return nfserr_bad_xdr;
0733d213
AA
1291
1292 READ_BUF(4);
1293 READ32(exid->flags);
1294
1295 /* Ignore state_protect4_a */
1296 READ_BUF(4);
1297 READ32(exid->spa_how);
1298 switch (exid->spa_how) {
1299 case SP4_NONE:
1300 break;
1301 case SP4_MACH_CRED:
1302 /* spo_must_enforce */
1303 READ_BUF(4);
1304 READ32(dummy);
1305 READ_BUF(dummy * 4);
1306 p += dummy;
1307
1308 /* spo_must_allow */
1309 READ_BUF(4);
1310 READ32(dummy);
1311 READ_BUF(dummy * 4);
1312 p += dummy;
1313 break;
1314 case SP4_SSV:
1315 /* ssp_ops */
1316 READ_BUF(4);
1317 READ32(dummy);
1318 READ_BUF(dummy * 4);
1319 p += dummy;
1320
1321 READ_BUF(4);
1322 READ32(dummy);
1323 READ_BUF(dummy * 4);
1324 p += dummy;
1325
1326 /* ssp_hash_algs<> */
1327 READ_BUF(4);
5afa040b
MJ
1328 READ32(tmp);
1329 while (tmp--) {
1330 READ_BUF(4);
1331 READ32(dummy);
1332 READ_BUF(dummy);
1333 p += XDR_QUADLEN(dummy);
1334 }
0733d213
AA
1335
1336 /* ssp_encr_algs<> */
1337 READ_BUF(4);
5afa040b
MJ
1338 READ32(tmp);
1339 while (tmp--) {
1340 READ_BUF(4);
1341 READ32(dummy);
1342 READ_BUF(dummy);
1343 p += XDR_QUADLEN(dummy);
1344 }
0733d213
AA
1345
1346 /* ssp_window and ssp_num_gss_handles */
1347 READ_BUF(8);
1348 READ32(dummy);
1349 READ32(dummy);
1350 break;
1351 default:
1352 goto xdr_error;
1353 }
1354
1355 /* Ignore Implementation ID */
1356 READ_BUF(4); /* nfs_impl_id4 array length */
1357 READ32(dummy);
1358
1359 if (dummy > 1)
1360 goto xdr_error;
1361
1362 if (dummy == 1) {
1363 /* nii_domain */
1364 READ_BUF(4);
1365 READ32(dummy);
1366 READ_BUF(dummy);
1367 p += XDR_QUADLEN(dummy);
1368
1369 /* nii_name */
1370 READ_BUF(4);
1371 READ32(dummy);
1372 READ_BUF(dummy);
1373 p += XDR_QUADLEN(dummy);
1374
1375 /* nii_date */
1376 READ_BUF(12);
1377 p += 3;
1378 }
1379 DECODE_TAIL;
2db134eb
AA
1380}
1381
1382static __be32
1383nfsd4_decode_create_session(struct nfsd4_compoundargs *argp,
1384 struct nfsd4_create_session *sess)
1385{
ec6b5d7b 1386 DECODE_HEAD;
ec6b5d7b 1387 u32 dummy;
ec6b5d7b
AA
1388
1389 READ_BUF(16);
1390 COPYMEM(&sess->clientid, 8);
1391 READ32(sess->seqid);
1392 READ32(sess->flags);
1393
1394 /* Fore channel attrs */
1395 READ_BUF(28);
1396 READ32(dummy); /* headerpadsz is always 0 */
1397 READ32(sess->fore_channel.maxreq_sz);
1398 READ32(sess->fore_channel.maxresp_sz);
1399 READ32(sess->fore_channel.maxresp_cached);
1400 READ32(sess->fore_channel.maxops);
1401 READ32(sess->fore_channel.maxreqs);
1402 READ32(sess->fore_channel.nr_rdma_attrs);
1403 if (sess->fore_channel.nr_rdma_attrs == 1) {
1404 READ_BUF(4);
1405 READ32(sess->fore_channel.rdma_attrs);
1406 } else if (sess->fore_channel.nr_rdma_attrs > 1) {
1407 dprintk("Too many fore channel attr bitmaps!\n");
1408 goto xdr_error;
1409 }
1410
1411 /* Back channel attrs */
1412 READ_BUF(28);
1413 READ32(dummy); /* headerpadsz is always 0 */
1414 READ32(sess->back_channel.maxreq_sz);
1415 READ32(sess->back_channel.maxresp_sz);
1416 READ32(sess->back_channel.maxresp_cached);
1417 READ32(sess->back_channel.maxops);
1418 READ32(sess->back_channel.maxreqs);
1419 READ32(sess->back_channel.nr_rdma_attrs);
1420 if (sess->back_channel.nr_rdma_attrs == 1) {
1421 READ_BUF(4);
1422 READ32(sess->back_channel.rdma_attrs);
1423 } else if (sess->back_channel.nr_rdma_attrs > 1) {
1424 dprintk("Too many back channel attr bitmaps!\n");
1425 goto xdr_error;
1426 }
1427
acb2887e 1428 READ_BUF(4);
ec6b5d7b 1429 READ32(sess->callback_prog);
acb2887e 1430 nfsd4_decode_cb_sec(argp, &sess->cb_sec);
ec6b5d7b 1431 DECODE_TAIL;
2db134eb
AA
1432}
1433
1434static __be32
1435nfsd4_decode_destroy_session(struct nfsd4_compoundargs *argp,
1436 struct nfsd4_destroy_session *destroy_session)
1437{
e10e0cfc
BH
1438 DECODE_HEAD;
1439 READ_BUF(NFS4_MAX_SESSIONID_LEN);
1440 COPYMEM(destroy_session->sessionid.data, NFS4_MAX_SESSIONID_LEN);
1441
1442 DECODE_TAIL;
2db134eb
AA
1443}
1444
e1ca12df
BS
1445static __be32
1446nfsd4_decode_free_stateid(struct nfsd4_compoundargs *argp,
1447 struct nfsd4_free_stateid *free_stateid)
1448{
1449 DECODE_HEAD;
1450
1451 READ_BUF(sizeof(stateid_t));
1452 READ32(free_stateid->fr_stateid.si_generation);
1453 COPYMEM(&free_stateid->fr_stateid.si_opaque, sizeof(stateid_opaque_t));
1454
1455 DECODE_TAIL;
1456}
1457
2db134eb
AA
1458static __be32
1459nfsd4_decode_sequence(struct nfsd4_compoundargs *argp,
1460 struct nfsd4_sequence *seq)
1461{
b85d4c01
BH
1462 DECODE_HEAD;
1463
1464 READ_BUF(NFS4_MAX_SESSIONID_LEN + 16);
1465 COPYMEM(seq->sessionid.data, NFS4_MAX_SESSIONID_LEN);
1466 READ32(seq->seqid);
1467 READ32(seq->slotid);
1468 READ32(seq->maxslots);
1469 READ32(seq->cachethis);
1470
1471 DECODE_TAIL;
2db134eb
AA
1472}
1473
17456804
BS
1474static __be32
1475nfsd4_decode_test_stateid(struct nfsd4_compoundargs *argp, struct nfsd4_test_stateid *test_stateid)
1476{
17456804 1477 int i;
03cfb420
BS
1478 __be32 *p, status;
1479 struct nfsd4_test_stateid_id *stateid;
17456804
BS
1480
1481 READ_BUF(4);
1482 test_stateid->ts_num_ids = ntohl(*p++);
1483
03cfb420 1484 INIT_LIST_HEAD(&test_stateid->ts_stateid_list);
17456804
BS
1485
1486 for (i = 0; i < test_stateid->ts_num_ids; i++) {
03cfb420
BS
1487 stateid = kmalloc(sizeof(struct nfsd4_test_stateid_id), GFP_KERNEL);
1488 if (!stateid) {
afcf6792 1489 status = nfserrno(-ENOMEM);
03cfb420
BS
1490 goto out;
1491 }
1492
1493 defer_free(argp, kfree, stateid);
1494 INIT_LIST_HEAD(&stateid->ts_id_list);
1495 list_add_tail(&stateid->ts_id_list, &test_stateid->ts_stateid_list);
1496
1497 status = nfsd4_decode_stateid(argp, &stateid->ts_id_stateid);
17456804 1498 if (status)
03cfb420 1499 goto out;
17456804
BS
1500 }
1501
1502 status = 0;
1503out:
1504 return status;
1505xdr_error:
1506 dprintk("NFSD: xdr error (%s:%d)\n", __FILE__, __LINE__);
1507 status = nfserr_bad_xdr;
1508 goto out;
1509}
1510
345c2842
MJ
1511static __be32 nfsd4_decode_destroy_clientid(struct nfsd4_compoundargs *argp, struct nfsd4_destroy_clientid *dc)
1512{
1513 DECODE_HEAD;
1514
1515 READ_BUF(8);
1516 COPYMEM(&dc->clientid, 8);
1517
1518 DECODE_TAIL;
1519}
1520
4dc6ec00
BF
1521static __be32 nfsd4_decode_reclaim_complete(struct nfsd4_compoundargs *argp, struct nfsd4_reclaim_complete *rc)
1522{
1523 DECODE_HEAD;
1524
1525 READ_BUF(4);
1526 READ32(rc->rca_one_fs);
1527
1528 DECODE_TAIL;
1529}
1530
347e0ad9
BH
1531static __be32
1532nfsd4_decode_noop(struct nfsd4_compoundargs *argp, void *p)
1533{
1534 return nfs_ok;
1535}
1536
3c375c6f
BH
1537static __be32
1538nfsd4_decode_notsupp(struct nfsd4_compoundargs *argp, void *p)
1539{
1e685ec2 1540 return nfserr_notsupp;
3c375c6f
BH
1541}
1542
347e0ad9
BH
1543typedef __be32(*nfsd4_dec)(struct nfsd4_compoundargs *argp, void *);
1544
1545static nfsd4_dec nfsd4_dec_ops[] = {
ad1060c8
BF
1546 [OP_ACCESS] = (nfsd4_dec)nfsd4_decode_access,
1547 [OP_CLOSE] = (nfsd4_dec)nfsd4_decode_close,
1548 [OP_COMMIT] = (nfsd4_dec)nfsd4_decode_commit,
1549 [OP_CREATE] = (nfsd4_dec)nfsd4_decode_create,
1550 [OP_DELEGPURGE] = (nfsd4_dec)nfsd4_decode_notsupp,
1551 [OP_DELEGRETURN] = (nfsd4_dec)nfsd4_decode_delegreturn,
1552 [OP_GETATTR] = (nfsd4_dec)nfsd4_decode_getattr,
1553 [OP_GETFH] = (nfsd4_dec)nfsd4_decode_noop,
1554 [OP_LINK] = (nfsd4_dec)nfsd4_decode_link,
1555 [OP_LOCK] = (nfsd4_dec)nfsd4_decode_lock,
1556 [OP_LOCKT] = (nfsd4_dec)nfsd4_decode_lockt,
1557 [OP_LOCKU] = (nfsd4_dec)nfsd4_decode_locku,
1558 [OP_LOOKUP] = (nfsd4_dec)nfsd4_decode_lookup,
1559 [OP_LOOKUPP] = (nfsd4_dec)nfsd4_decode_noop,
1560 [OP_NVERIFY] = (nfsd4_dec)nfsd4_decode_verify,
1561 [OP_OPEN] = (nfsd4_dec)nfsd4_decode_open,
1562 [OP_OPENATTR] = (nfsd4_dec)nfsd4_decode_notsupp,
1563 [OP_OPEN_CONFIRM] = (nfsd4_dec)nfsd4_decode_open_confirm,
1564 [OP_OPEN_DOWNGRADE] = (nfsd4_dec)nfsd4_decode_open_downgrade,
1565 [OP_PUTFH] = (nfsd4_dec)nfsd4_decode_putfh,
e1a90ebd 1566 [OP_PUTPUBFH] = (nfsd4_dec)nfsd4_decode_putpubfh,
ad1060c8
BF
1567 [OP_PUTROOTFH] = (nfsd4_dec)nfsd4_decode_noop,
1568 [OP_READ] = (nfsd4_dec)nfsd4_decode_read,
1569 [OP_READDIR] = (nfsd4_dec)nfsd4_decode_readdir,
1570 [OP_READLINK] = (nfsd4_dec)nfsd4_decode_noop,
1571 [OP_REMOVE] = (nfsd4_dec)nfsd4_decode_remove,
1572 [OP_RENAME] = (nfsd4_dec)nfsd4_decode_rename,
1573 [OP_RENEW] = (nfsd4_dec)nfsd4_decode_renew,
1574 [OP_RESTOREFH] = (nfsd4_dec)nfsd4_decode_noop,
1575 [OP_SAVEFH] = (nfsd4_dec)nfsd4_decode_noop,
1576 [OP_SECINFO] = (nfsd4_dec)nfsd4_decode_secinfo,
1577 [OP_SETATTR] = (nfsd4_dec)nfsd4_decode_setattr,
1578 [OP_SETCLIENTID] = (nfsd4_dec)nfsd4_decode_setclientid,
1579 [OP_SETCLIENTID_CONFIRM] = (nfsd4_dec)nfsd4_decode_setclientid_confirm,
1580 [OP_VERIFY] = (nfsd4_dec)nfsd4_decode_verify,
1581 [OP_WRITE] = (nfsd4_dec)nfsd4_decode_write,
1582 [OP_RELEASE_LOCKOWNER] = (nfsd4_dec)nfsd4_decode_release_lockowner,
2db134eb
AA
1583
1584 /* new operations for NFSv4.1 */
cb73a9f4 1585 [OP_BACKCHANNEL_CTL] = (nfsd4_dec)nfsd4_decode_backchannel_ctl,
1d1bc8f2 1586 [OP_BIND_CONN_TO_SESSION]= (nfsd4_dec)nfsd4_decode_bind_conn_to_session,
9064caae
RD
1587 [OP_EXCHANGE_ID] = (nfsd4_dec)nfsd4_decode_exchange_id,
1588 [OP_CREATE_SESSION] = (nfsd4_dec)nfsd4_decode_create_session,
1589 [OP_DESTROY_SESSION] = (nfsd4_dec)nfsd4_decode_destroy_session,
e1ca12df 1590 [OP_FREE_STATEID] = (nfsd4_dec)nfsd4_decode_free_stateid,
9064caae
RD
1591 [OP_GET_DIR_DELEGATION] = (nfsd4_dec)nfsd4_decode_notsupp,
1592 [OP_GETDEVICEINFO] = (nfsd4_dec)nfsd4_decode_notsupp,
1593 [OP_GETDEVICELIST] = (nfsd4_dec)nfsd4_decode_notsupp,
1594 [OP_LAYOUTCOMMIT] = (nfsd4_dec)nfsd4_decode_notsupp,
1595 [OP_LAYOUTGET] = (nfsd4_dec)nfsd4_decode_notsupp,
1596 [OP_LAYOUTRETURN] = (nfsd4_dec)nfsd4_decode_notsupp,
04f4ad16 1597 [OP_SECINFO_NO_NAME] = (nfsd4_dec)nfsd4_decode_secinfo_no_name,
9064caae
RD
1598 [OP_SEQUENCE] = (nfsd4_dec)nfsd4_decode_sequence,
1599 [OP_SET_SSV] = (nfsd4_dec)nfsd4_decode_notsupp,
17456804 1600 [OP_TEST_STATEID] = (nfsd4_dec)nfsd4_decode_test_stateid,
9064caae 1601 [OP_WANT_DELEGATION] = (nfsd4_dec)nfsd4_decode_notsupp,
345c2842 1602 [OP_DESTROY_CLIENTID] = (nfsd4_dec)nfsd4_decode_destroy_clientid,
4dc6ec00 1603 [OP_RECLAIM_COMPLETE] = (nfsd4_dec)nfsd4_decode_reclaim_complete,
2db134eb
AA
1604};
1605
e1a90ebd
AS
1606static inline bool
1607nfsd4_opnum_in_range(struct nfsd4_compoundargs *argp, struct nfsd4_op *op)
1608{
8217d146 1609 if (op->opnum < FIRST_NFS4_OP)
e1a90ebd 1610 return false;
8217d146 1611 else if (argp->minorversion == 0 && op->opnum > LAST_NFS40_OP)
e1a90ebd 1612 return false;
8217d146
AS
1613 else if (argp->minorversion == 1 && op->opnum > LAST_NFS41_OP)
1614 return false;
1615 else if (argp->minorversion == 2 && op->opnum > LAST_NFS42_OP)
e1a90ebd
AS
1616 return false;
1617 return true;
1618}
f2feb96b 1619
6ff40dec
BF
1620/*
1621 * Return a rough estimate of the maximum possible reply size. Note the
1622 * estimate includes rpc headers so is meant to be passed to
1623 * svc_reserve, not svc_reserve_auth.
1624 *
1625 * Also note the current compound encoding permits only one operation to
1626 * use pages beyond the first one, so the maximum possible length is the
1627 * maximum over these values, not the sum.
1628 */
1629static int nfsd4_max_reply(u32 opnum)
1630{
1631 switch (opnum) {
1632 case OP_READLINK:
1633 case OP_READDIR:
1634 /*
1635 * Both of these ops take a single page for data and put
1636 * the head and tail in another page:
1637 */
1638 return 2 * PAGE_SIZE;
1639 case OP_READ:
1640 return INT_MAX;
1641 default:
1642 return PAGE_SIZE;
1643 }
1644}
1645
b37ad28b 1646static __be32
1da177e4
LT
1647nfsd4_decode_compound(struct nfsd4_compoundargs *argp)
1648{
1649 DECODE_HEAD;
1650 struct nfsd4_op *op;
1091006c 1651 bool cachethis = false;
6ff40dec 1652 int max_reply = PAGE_SIZE;
1da177e4
LT
1653 int i;
1654
1da177e4
LT
1655 READ_BUF(4);
1656 READ32(argp->taglen);
1657 READ_BUF(argp->taglen + 8);
1658 SAVEMEM(argp->tag, argp->taglen);
1659 READ32(argp->minorversion);
1660 READ32(argp->opcnt);
1661
1662 if (argp->taglen > NFSD4_MAX_TAGLEN)
1663 goto xdr_error;
1664 if (argp->opcnt > 100)
1665 goto xdr_error;
1666
e8c96f8c 1667 if (argp->opcnt > ARRAY_SIZE(argp->iops)) {
1da177e4
LT
1668 argp->ops = kmalloc(argp->opcnt * sizeof(*argp->ops), GFP_KERNEL);
1669 if (!argp->ops) {
1670 argp->ops = argp->iops;
817cb9d4 1671 dprintk("nfsd: couldn't allocate room for COMPOUND\n");
1da177e4
LT
1672 goto xdr_error;
1673 }
1674 }
1675
e1a90ebd 1676 if (argp->minorversion > NFSD_SUPPORTED_MINOR_VERSION)
30cff1ff
BH
1677 argp->opcnt = 0;
1678
1da177e4
LT
1679 for (i = 0; i < argp->opcnt; i++) {
1680 op = &argp->ops[i];
1681 op->replay = NULL;
1682
8a61b18c
BF
1683 READ_BUF(4);
1684 READ32(op->opnum);
1da177e4 1685
e1a90ebd
AS
1686 if (nfsd4_opnum_in_range(argp, op))
1687 op->status = nfsd4_dec_ops[op->opnum](argp, &op->u);
347e0ad9 1688 else {
1da177e4
LT
1689 op->opnum = OP_ILLEGAL;
1690 op->status = nfserr_op_illegal;
1da177e4
LT
1691 }
1692
1693 if (op->status) {
1694 argp->opcnt = i+1;
1695 break;
1696 }
1091006c
BF
1697 /*
1698 * We'll try to cache the result in the DRC if any one
1699 * op in the compound wants to be cached:
1700 */
1701 cachethis |= nfsd4_cache_this_op(op);
6ff40dec
BF
1702
1703 max_reply = max(max_reply, nfsd4_max_reply(op->opnum));
1da177e4 1704 }
1091006c
BF
1705 /* Sessions make the DRC unnecessary: */
1706 if (argp->minorversion)
1707 cachethis = false;
6ff40dec
BF
1708 if (max_reply != INT_MAX)
1709 svc_reserve(argp->rqstp, max_reply);
1091006c 1710 argp->rqstp->rq_cachetype = cachethis ? RC_REPLBUFF : RC_NOCACHE;
1da177e4
LT
1711
1712 DECODE_TAIL;
1713}
1da177e4 1714
1da177e4
LT
1715#define WRITE32(n) *p++ = htonl(n)
1716#define WRITE64(n) do { \
1717 *p++ = htonl((u32)((n) >> 32)); \
1718 *p++ = htonl((u32)(n)); \
1719} while (0)
5108b276 1720#define WRITEMEM(ptr,nbytes) do { if (nbytes > 0) { \
1da177e4
LT
1721 *(p + XDR_QUADLEN(nbytes) -1) = 0; \
1722 memcpy(p, ptr, nbytes); \
1723 p += XDR_QUADLEN(nbytes); \
5108b276 1724}} while (0)
c654b8a9
BF
1725
1726static void write32(__be32 **p, u32 n)
1727{
45eaa1c1 1728 *(*p)++ = htonl(n);
c654b8a9
BF
1729}
1730
1731static void write64(__be32 **p, u64 n)
1732{
45eaa1c1 1733 write32(p, (n >> 32));
c654b8a9
BF
1734 write32(p, (u32)n);
1735}
1736
1737static void write_change(__be32 **p, struct kstat *stat, struct inode *inode)
1738{
1739 if (IS_I_VERSION(inode)) {
1740 write64(p, inode->i_version);
1741 } else {
1742 write32(p, stat->ctime.tv_sec);
1743 write32(p, stat->ctime.tv_nsec);
1744 }
1745}
1746
1747static void write_cinfo(__be32 **p, struct nfsd4_change_info *c)
1748{
1749 write32(p, c->atomic);
1750 if (c->change_supported) {
1751 write64(p, c->before_change);
1752 write64(p, c->after_change);
1753 } else {
1754 write32(p, c->before_ctime_sec);
1755 write32(p, c->before_ctime_nsec);
1756 write32(p, c->after_ctime_sec);
1757 write32(p, c->after_ctime_nsec);
1758 }
1759}
1da177e4
LT
1760
1761#define RESERVE_SPACE(nbytes) do { \
1762 p = resp->p; \
1763 BUG_ON(p + XDR_QUADLEN(nbytes) > resp->end); \
1764} while (0)
1765#define ADJUST_ARGS() resp->p = p
1766
81c3f413 1767/* Encode as an array of strings the string given with components
e7a0444a 1768 * separated @sep, escaped with esc_enter and esc_exit.
81c3f413 1769 */
e7a0444a
WAA
1770static __be32 nfsd4_encode_components_esc(char sep, char *components,
1771 __be32 **pp, int *buflen,
1772 char esc_enter, char esc_exit)
81c3f413 1773{
2ebbc012
AV
1774 __be32 *p = *pp;
1775 __be32 *countp = p;
81c3f413 1776 int strlen, count=0;
e7a0444a 1777 char *str, *end, *next;
81c3f413
BF
1778
1779 dprintk("nfsd4_encode_components(%s)\n", components);
1780 if ((*buflen -= 4) < 0)
1781 return nfserr_resource;
1782 WRITE32(0); /* We will fill this in with @count later */
1783 end = str = components;
1784 while (*end) {
e7a0444a
WAA
1785 bool found_esc = false;
1786
1787 /* try to parse as esc_start, ..., esc_end, sep */
1788 if (*str == esc_enter) {
1789 for (; *end && (*end != esc_exit); end++)
1790 /* find esc_exit or end of string */;
1791 next = end + 1;
1792 if (*end && (!*next || *next == sep)) {
1793 str++;
1794 found_esc = true;
1795 }
1796 }
1797
1798 if (!found_esc)
1799 for (; *end && (*end != sep); end++)
1800 /* find sep or end of string */;
1801
81c3f413
BF
1802 strlen = end - str;
1803 if (strlen) {
1804 if ((*buflen -= ((XDR_QUADLEN(strlen) << 2) + 4)) < 0)
1805 return nfserr_resource;
1806 WRITE32(strlen);
1807 WRITEMEM(str, strlen);
1808 count++;
1809 }
1810 else
1811 end++;
1812 str = end;
1813 }
1814 *pp = p;
1815 p = countp;
1816 WRITE32(count);
1817 return 0;
1818}
1819
e7a0444a
WAA
1820/* Encode as an array of strings the string given with components
1821 * separated @sep.
1822 */
1823static __be32 nfsd4_encode_components(char sep, char *components,
1824 __be32 **pp, int *buflen)
1825{
1826 return nfsd4_encode_components_esc(sep, components, pp, buflen, 0, 0);
1827}
1828
81c3f413
BF
1829/*
1830 * encode a location element of a fs_locations structure
1831 */
b37ad28b 1832static __be32 nfsd4_encode_fs_location4(struct nfsd4_fs_location *location,
2ebbc012 1833 __be32 **pp, int *buflen)
81c3f413 1834{
b37ad28b 1835 __be32 status;
2ebbc012 1836 __be32 *p = *pp;
81c3f413 1837
e7a0444a
WAA
1838 status = nfsd4_encode_components_esc(':', location->hosts, &p, buflen,
1839 '[', ']');
81c3f413
BF
1840 if (status)
1841 return status;
1842 status = nfsd4_encode_components('/', location->path, &p, buflen);
1843 if (status)
1844 return status;
1845 *pp = p;
1846 return 0;
1847}
1848
1849/*
ed748aac 1850 * Encode a path in RFC3530 'pathname4' format
81c3f413 1851 */
ed748aac
TM
1852static __be32 nfsd4_encode_path(const struct path *root,
1853 const struct path *path, __be32 **pp, int *buflen)
81c3f413 1854{
301f0268 1855 struct path cur = *path;
ed748aac
TM
1856 __be32 *p = *pp;
1857 struct dentry **components = NULL;
1858 unsigned int ncomponents = 0;
1859 __be32 err = nfserr_jukebox;
81c3f413 1860
ed748aac 1861 dprintk("nfsd4_encode_components(");
81c3f413 1862
ed748aac
TM
1863 path_get(&cur);
1864 /* First walk the path up to the nfsd root, and store the
1865 * dentries/path components in an array.
1866 */
1867 for (;;) {
1868 if (cur.dentry == root->dentry && cur.mnt == root->mnt)
1869 break;
1870 if (cur.dentry == cur.mnt->mnt_root) {
1871 if (follow_up(&cur))
1872 continue;
1873 goto out_free;
1874 }
1875 if ((ncomponents & 15) == 0) {
1876 struct dentry **new;
1877 new = krealloc(components,
1878 sizeof(*new) * (ncomponents + 16),
1879 GFP_KERNEL);
1880 if (!new)
1881 goto out_free;
1882 components = new;
1883 }
1884 components[ncomponents++] = cur.dentry;
1885 cur.dentry = dget_parent(cur.dentry);
1886 }
81c3f413 1887
ed748aac
TM
1888 *buflen -= 4;
1889 if (*buflen < 0)
1890 goto out_free;
1891 WRITE32(ncomponents);
1892
1893 while (ncomponents) {
1894 struct dentry *dentry = components[ncomponents - 1];
301f0268 1895 unsigned int len;
ed748aac 1896
301f0268
AV
1897 spin_lock(&dentry->d_lock);
1898 len = dentry->d_name.len;
ed748aac 1899 *buflen -= 4 + (XDR_QUADLEN(len) << 2);
301f0268
AV
1900 if (*buflen < 0) {
1901 spin_unlock(&dentry->d_lock);
ed748aac 1902 goto out_free;
301f0268 1903 }
ed748aac
TM
1904 WRITE32(len);
1905 WRITEMEM(dentry->d_name.name, len);
1906 dprintk("/%s", dentry->d_name.name);
301f0268 1907 spin_unlock(&dentry->d_lock);
ed748aac
TM
1908 dput(dentry);
1909 ncomponents--;
81c3f413 1910 }
ed748aac
TM
1911
1912 *pp = p;
1913 err = 0;
1914out_free:
1915 dprintk(")\n");
1916 while (ncomponents)
1917 dput(components[--ncomponents]);
1918 kfree(components);
1919 path_put(&cur);
1920 return err;
1921}
1922
1923static __be32 nfsd4_encode_fsloc_fsroot(struct svc_rqst *rqstp,
1924 const struct path *path, __be32 **pp, int *buflen)
1925{
1926 struct svc_export *exp_ps;
1927 __be32 res;
1928
1929 exp_ps = rqst_find_fsidzero_export(rqstp);
1930 if (IS_ERR(exp_ps))
1931 return nfserrno(PTR_ERR(exp_ps));
1932 res = nfsd4_encode_path(&exp_ps->ex_path, path, pp, buflen);
1933 exp_put(exp_ps);
1934 return res;
81c3f413
BF
1935}
1936
1937/*
1938 * encode a fs_locations structure
1939 */
b37ad28b 1940static __be32 nfsd4_encode_fs_locations(struct svc_rqst *rqstp,
81c3f413 1941 struct svc_export *exp,
2ebbc012 1942 __be32 **pp, int *buflen)
81c3f413 1943{
b37ad28b 1944 __be32 status;
cc45f017 1945 int i;
2ebbc012 1946 __be32 *p = *pp;
81c3f413 1947 struct nfsd4_fs_locations *fslocs = &exp->ex_fslocs;
81c3f413 1948
ed748aac 1949 status = nfsd4_encode_fsloc_fsroot(rqstp, &exp->ex_path, &p, buflen);
81c3f413
BF
1950 if (status)
1951 return status;
1952 if ((*buflen -= 4) < 0)
1953 return nfserr_resource;
1954 WRITE32(fslocs->locations_count);
1955 for (i=0; i<fslocs->locations_count; i++) {
1956 status = nfsd4_encode_fs_location4(&fslocs->locations[i],
1957 &p, buflen);
1958 if (status)
1959 return status;
1960 }
1961 *pp = p;
1962 return 0;
1963}
1da177e4 1964
3d2544b1
BF
1965static u32 nfs4_file_type(umode_t mode)
1966{
1967 switch (mode & S_IFMT) {
1968 case S_IFIFO: return NF4FIFO;
1969 case S_IFCHR: return NF4CHR;
1970 case S_IFDIR: return NF4DIR;
1971 case S_IFBLK: return NF4BLK;
1972 case S_IFLNK: return NF4LNK;
1973 case S_IFREG: return NF4REG;
1974 case S_IFSOCK: return NF4SOCK;
1975 default: return NF4BAD;
1976 };
1977}
1da177e4 1978
b37ad28b 1979static __be32
ab8e4aee 1980nfsd4_encode_name(struct svc_rqst *rqstp, int whotype, kuid_t uid, kgid_t gid,
2ebbc012 1981 __be32 **p, int *buflen)
1da177e4
LT
1982{
1983 int status;
1984
1985 if (*buflen < (XDR_QUADLEN(IDMAP_NAMESZ) << 2) + 4)
1986 return nfserr_resource;
1987 if (whotype != NFS4_ACL_WHO_NAMED)
1988 status = nfs4_acl_write_who(whotype, (u8 *)(*p + 1));
ab8e4aee
EB
1989 else if (gid_valid(gid))
1990 status = nfsd_map_gid_to_name(rqstp, gid, (u8 *)(*p + 1));
1da177e4 1991 else
ab8e4aee 1992 status = nfsd_map_uid_to_name(rqstp, uid, (u8 *)(*p + 1));
1da177e4
LT
1993 if (status < 0)
1994 return nfserrno(status);
1995 *p = xdr_encode_opaque(*p, NULL, status);
1996 *buflen -= (XDR_QUADLEN(status) << 2) + 4;
1997 BUG_ON(*buflen < 0);
1998 return 0;
1999}
2000
b37ad28b 2001static inline __be32
ab8e4aee 2002nfsd4_encode_user(struct svc_rqst *rqstp, kuid_t user, __be32 **p, int *buflen)
1da177e4 2003{
ab8e4aee
EB
2004 return nfsd4_encode_name(rqstp, NFS4_ACL_WHO_NAMED, user, INVALID_GID,
2005 p, buflen);
1da177e4
LT
2006}
2007
b37ad28b 2008static inline __be32
ab8e4aee 2009nfsd4_encode_group(struct svc_rqst *rqstp, kgid_t group, __be32 **p, int *buflen)
1da177e4 2010{
ab8e4aee
EB
2011 return nfsd4_encode_name(rqstp, NFS4_ACL_WHO_NAMED, INVALID_UID, group,
2012 p, buflen);
1da177e4
LT
2013}
2014
b37ad28b 2015static inline __be32
ab8e4aee 2016nfsd4_encode_aclname(struct svc_rqst *rqstp, struct nfs4_ace *ace,
2ebbc012 2017 __be32 **p, int *buflen)
1da177e4 2018{
ab8e4aee
EB
2019 kuid_t uid = INVALID_UID;
2020 kgid_t gid = INVALID_GID;
2021
2022 if (ace->whotype == NFS4_ACL_WHO_NAMED) {
2023 if (ace->flag & NFS4_ACE_IDENTIFIER_GROUP)
2024 gid = ace->who_gid;
2025 else
2026 uid = ace->who_uid;
2027 }
2028 return nfsd4_encode_name(rqstp, ace->whotype, uid, gid, p, buflen);
1da177e4
LT
2029}
2030
42ca0993
BF
2031#define WORD0_ABSENT_FS_ATTRS (FATTR4_WORD0_FS_LOCATIONS | FATTR4_WORD0_FSID | \
2032 FATTR4_WORD0_RDATTR_ERROR)
2033#define WORD1_ABSENT_FS_ATTRS FATTR4_WORD1_MOUNTED_ON_FILEID
2034
18032ca0
DQ
2035#ifdef CONFIG_NFSD_V4_SECURITY_LABEL
2036static inline __be32
2037nfsd4_encode_security_label(struct svc_rqst *rqstp, void *context, int len, __be32 **pp, int *buflen)
2038{
2039 __be32 *p = *pp;
2040
2041 if (*buflen < ((XDR_QUADLEN(len) << 2) + 4 + 4 + 4))
2042 return nfserr_resource;
2043
2044 /*
2045 * For now we use a 0 here to indicate the null translation; in
2046 * the future we may place a call to translation code here.
2047 */
2048 if ((*buflen -= 8) < 0)
2049 return nfserr_resource;
2050
2051 WRITE32(0); /* lfs */
2052 WRITE32(0); /* pi */
2053 p = xdr_encode_opaque(p, context, len);
2054 *buflen -= (XDR_QUADLEN(len) << 2) + 4;
2055
2056 *pp = p;
2057 return 0;
2058}
2059#else
2060static inline __be32
ba4e55bb 2061nfsd4_encode_security_label(struct svc_rqst *rqstp, void *context, int len, __be32 **pp, int *buflen)
18032ca0
DQ
2062{ return 0; }
2063#endif
2064
b37ad28b 2065static __be32 fattr_handle_absent_fs(u32 *bmval0, u32 *bmval1, u32 *rdattr_err)
42ca0993
BF
2066{
2067 /* As per referral draft: */
2068 if (*bmval0 & ~WORD0_ABSENT_FS_ATTRS ||
2069 *bmval1 & ~WORD1_ABSENT_FS_ATTRS) {
2070 if (*bmval0 & FATTR4_WORD0_RDATTR_ERROR ||
2071 *bmval0 & FATTR4_WORD0_FS_LOCATIONS)
2072 *rdattr_err = NFSERR_MOVED;
2073 else
2074 return nfserr_moved;
2075 }
2076 *bmval0 &= WORD0_ABSENT_FS_ATTRS;
2077 *bmval1 &= WORD1_ABSENT_FS_ATTRS;
2078 return 0;
2079}
1da177e4 2080
ae7095a7
BF
2081
2082static int get_parent_attributes(struct svc_export *exp, struct kstat *stat)
2083{
2084 struct path path = exp->ex_path;
2085 int err;
2086
2087 path_get(&path);
2088 while (follow_up(&path)) {
2089 if (path.dentry != path.mnt->mnt_root)
2090 break;
2091 }
3dadecce 2092 err = vfs_getattr(&path, stat);
ae7095a7
BF
2093 path_put(&path);
2094 return err;
2095}
2096
1da177e4
LT
2097/*
2098 * Note: @fhp can be NULL; in this case, we might have to compose the filehandle
2099 * ourselves.
2100 *
84822d0b 2101 * countp is the buffer size in _words_
1da177e4 2102 */
b37ad28b 2103__be32
1da177e4 2104nfsd4_encode_fattr(struct svc_fh *fhp, struct svc_export *exp,
84822d0b 2105 struct dentry *dentry, __be32 **buffer, int count, u32 *bmval,
406a7ea9 2106 struct svc_rqst *rqstp, int ignore_crossmnt)
1da177e4
LT
2107{
2108 u32 bmval0 = bmval[0];
2109 u32 bmval1 = bmval[1];
7e705706 2110 u32 bmval2 = bmval[2];
1da177e4
LT
2111 struct kstat stat;
2112 struct svc_fh tempfh;
2113 struct kstatfs statfs;
84822d0b 2114 int buflen = count << 2;
2ebbc012 2115 __be32 *attrlenp;
1da177e4
LT
2116 u32 dummy;
2117 u64 dummy64;
42ca0993 2118 u32 rdattr_err = 0;
84822d0b 2119 __be32 *p = *buffer;
b37ad28b 2120 __be32 status;
b8dd7b9a 2121 int err;
1da177e4
LT
2122 int aclsupport = 0;
2123 struct nfs4_acl *acl = NULL;
18032ca0
DQ
2124 void *context = NULL;
2125 int contextlen;
2126 bool contextsupport = false;
7e705706
AA
2127 struct nfsd4_compoundres *resp = rqstp->rq_resp;
2128 u32 minorversion = resp->cstate.minorversion;
ebabe9a9
CH
2129 struct path path = {
2130 .mnt = exp->ex_path.mnt,
2131 .dentry = dentry,
2132 };
3d733711 2133 struct nfsd_net *nn = net_generic(SVC_NET(rqstp), nfsd_net_id);
1da177e4
LT
2134
2135 BUG_ON(bmval1 & NFSD_WRITEONLY_ATTRS_WORD1);
7e705706
AA
2136 BUG_ON(bmval0 & ~nfsd_suppattrs0(minorversion));
2137 BUG_ON(bmval1 & ~nfsd_suppattrs1(minorversion));
2138 BUG_ON(bmval2 & ~nfsd_suppattrs2(minorversion));
1da177e4 2139
42ca0993 2140 if (exp->ex_fslocs.migrated) {
7e705706 2141 BUG_ON(bmval[2]);
42ca0993
BF
2142 status = fattr_handle_absent_fs(&bmval0, &bmval1, &rdattr_err);
2143 if (status)
2144 goto out;
2145 }
2146
3dadecce 2147 err = vfs_getattr(&path, &stat);
b8dd7b9a 2148 if (err)
1da177e4 2149 goto out_nfserr;
a16e92ed
BF
2150 if ((bmval0 & (FATTR4_WORD0_FILES_FREE | FATTR4_WORD0_FILES_TOTAL |
2151 FATTR4_WORD0_MAXNAME)) ||
1da177e4
LT
2152 (bmval1 & (FATTR4_WORD1_SPACE_AVAIL | FATTR4_WORD1_SPACE_FREE |
2153 FATTR4_WORD1_SPACE_TOTAL))) {
ebabe9a9 2154 err = vfs_statfs(&path, &statfs);
b8dd7b9a 2155 if (err)
1da177e4
LT
2156 goto out_nfserr;
2157 }
2158 if ((bmval0 & (FATTR4_WORD0_FILEHANDLE | FATTR4_WORD0_FSID)) && !fhp) {
2159 fh_init(&tempfh, NFS4_FHSIZE);
2160 status = fh_compose(&tempfh, exp, dentry, NULL);
2161 if (status)
2162 goto out;
2163 fhp = &tempfh;
2164 }
2165 if (bmval0 & (FATTR4_WORD0_ACL | FATTR4_WORD0_ACLSUPPORT
2166 | FATTR4_WORD0_SUPPORTED_ATTRS)) {
b8dd7b9a
AV
2167 err = nfsd4_get_nfs4_acl(rqstp, dentry, &acl);
2168 aclsupport = (err == 0);
1da177e4 2169 if (bmval0 & FATTR4_WORD0_ACL) {
b8dd7b9a 2170 if (err == -EOPNOTSUPP)
1da177e4 2171 bmval0 &= ~FATTR4_WORD0_ACL;
b8dd7b9a 2172 else if (err == -EINVAL) {
1da177e4
LT
2173 status = nfserr_attrnotsupp;
2174 goto out;
b8dd7b9a 2175 } else if (err != 0)
1da177e4
LT
2176 goto out_nfserr;
2177 }
2178 }
1da177e4 2179
18032ca0
DQ
2180#ifdef CONFIG_NFSD_V4_SECURITY_LABEL
2181 if ((bmval[2] & FATTR4_WORD2_SECURITY_LABEL) ||
2182 bmval[0] & FATTR4_WORD0_SUPPORTED_ATTRS) {
2183 err = security_inode_getsecctx(dentry->d_inode,
2184 &context, &contextlen);
2185 contextsupport = (err == 0);
2186 if (bmval2 & FATTR4_WORD2_SECURITY_LABEL) {
2187 if (err == -EOPNOTSUPP)
2188 bmval2 &= ~FATTR4_WORD2_SECURITY_LABEL;
2189 else if (err)
2190 goto out_nfserr;
2191 }
2192 }
2193#endif /* CONFIG_NFSD_V4_SECURITY_LABEL */
2194
2b44f1ba
BH
2195 if (bmval2) {
2196 if ((buflen -= 16) < 0)
2197 goto out_resource;
7e705706
AA
2198 WRITE32(3);
2199 WRITE32(bmval0);
2200 WRITE32(bmval1);
2201 WRITE32(bmval2);
2b44f1ba
BH
2202 } else if (bmval1) {
2203 if ((buflen -= 12) < 0)
2204 goto out_resource;
7e705706
AA
2205 WRITE32(2);
2206 WRITE32(bmval0);
2207 WRITE32(bmval1);
2208 } else {
2b44f1ba
BH
2209 if ((buflen -= 8) < 0)
2210 goto out_resource;
7e705706
AA
2211 WRITE32(1);
2212 WRITE32(bmval0);
2213 }
1da177e4
LT
2214 attrlenp = p++; /* to be backfilled later */
2215
2216 if (bmval0 & FATTR4_WORD0_SUPPORTED_ATTRS) {
7e705706
AA
2217 u32 word0 = nfsd_suppattrs0(minorversion);
2218 u32 word1 = nfsd_suppattrs1(minorversion);
2219 u32 word2 = nfsd_suppattrs2(minorversion);
2220
42ca0993
BF
2221 if (!aclsupport)
2222 word0 &= ~FATTR4_WORD0_ACL;
18032ca0
DQ
2223 if (!contextsupport)
2224 word2 &= ~FATTR4_WORD2_SECURITY_LABEL;
7e705706 2225 if (!word2) {
2b44f1ba
BH
2226 if ((buflen -= 12) < 0)
2227 goto out_resource;
7e705706
AA
2228 WRITE32(2);
2229 WRITE32(word0);
2230 WRITE32(word1);
2231 } else {
2b44f1ba
BH
2232 if ((buflen -= 16) < 0)
2233 goto out_resource;
7e705706
AA
2234 WRITE32(3);
2235 WRITE32(word0);
2236 WRITE32(word1);
2237 WRITE32(word2);
2238 }
1da177e4
LT
2239 }
2240 if (bmval0 & FATTR4_WORD0_TYPE) {
2241 if ((buflen -= 4) < 0)
2242 goto out_resource;
3d2544b1 2243 dummy = nfs4_file_type(stat.mode);
1da177e4
LT
2244 if (dummy == NF4BAD)
2245 goto out_serverfault;
2246 WRITE32(dummy);
2247 }
2248 if (bmval0 & FATTR4_WORD0_FH_EXPIRE_TYPE) {
2249 if ((buflen -= 4) < 0)
2250 goto out_resource;
49640001 2251 if (exp->ex_flags & NFSEXP_NOSUBTREECHECK)
e34ac862 2252 WRITE32(NFS4_FH_PERSISTENT);
49640001 2253 else
e34ac862 2254 WRITE32(NFS4_FH_PERSISTENT|NFS4_FH_VOL_RENAME);
1da177e4
LT
2255 }
2256 if (bmval0 & FATTR4_WORD0_CHANGE) {
1da177e4
LT
2257 if ((buflen -= 8) < 0)
2258 goto out_resource;
c654b8a9 2259 write_change(&p, &stat, dentry->d_inode);
1da177e4
LT
2260 }
2261 if (bmval0 & FATTR4_WORD0_SIZE) {
2262 if ((buflen -= 8) < 0)
2263 goto out_resource;
2264 WRITE64(stat.size);
2265 }
2266 if (bmval0 & FATTR4_WORD0_LINK_SUPPORT) {
2267 if ((buflen -= 4) < 0)
2268 goto out_resource;
2269 WRITE32(1);
2270 }
2271 if (bmval0 & FATTR4_WORD0_SYMLINK_SUPPORT) {
2272 if ((buflen -= 4) < 0)
2273 goto out_resource;
2274 WRITE32(1);
2275 }
2276 if (bmval0 & FATTR4_WORD0_NAMED_ATTR) {
2277 if ((buflen -= 4) < 0)
2278 goto out_resource;
2279 WRITE32(0);
2280 }
2281 if (bmval0 & FATTR4_WORD0_FSID) {
2282 if ((buflen -= 16) < 0)
2283 goto out_resource;
42ca0993
BF
2284 if (exp->ex_fslocs.migrated) {
2285 WRITE64(NFS4_REFERRAL_FSID_MAJOR);
2286 WRITE64(NFS4_REFERRAL_FSID_MINOR);
af6a4e28
N
2287 } else switch(fsid_source(fhp)) {
2288 case FSIDSOURCE_FSID:
1da177e4
LT
2289 WRITE64((u64)exp->ex_fsid);
2290 WRITE64((u64)0);
af6a4e28
N
2291 break;
2292 case FSIDSOURCE_DEV:
1da177e4
LT
2293 WRITE32(0);
2294 WRITE32(MAJOR(stat.dev));
2295 WRITE32(0);
2296 WRITE32(MINOR(stat.dev));
af6a4e28
N
2297 break;
2298 case FSIDSOURCE_UUID:
2299 WRITEMEM(exp->ex_uuid, 16);
2300 break;
1da177e4
LT
2301 }
2302 }
2303 if (bmval0 & FATTR4_WORD0_UNIQUE_HANDLES) {
2304 if ((buflen -= 4) < 0)
2305 goto out_resource;
2306 WRITE32(0);
2307 }
2308 if (bmval0 & FATTR4_WORD0_LEASE_TIME) {
2309 if ((buflen -= 4) < 0)
2310 goto out_resource;
3d733711 2311 WRITE32(nn->nfsd4_lease);
1da177e4
LT
2312 }
2313 if (bmval0 & FATTR4_WORD0_RDATTR_ERROR) {
2314 if ((buflen -= 4) < 0)
2315 goto out_resource;
42ca0993 2316 WRITE32(rdattr_err);
1da177e4
LT
2317 }
2318 if (bmval0 & FATTR4_WORD0_ACL) {
2319 struct nfs4_ace *ace;
1da177e4
LT
2320
2321 if (acl == NULL) {
2322 if ((buflen -= 4) < 0)
2323 goto out_resource;
2324
2325 WRITE32(0);
2326 goto out_acl;
2327 }
2328 if ((buflen -= 4) < 0)
2329 goto out_resource;
2330 WRITE32(acl->naces);
2331
28e05dd8 2332 for (ace = acl->aces; ace < acl->aces + acl->naces; ace++) {
1da177e4
LT
2333 if ((buflen -= 4*3) < 0)
2334 goto out_resource;
2335 WRITE32(ace->type);
2336 WRITE32(ace->flag);
2337 WRITE32(ace->access_mask & NFS4_ACE_MASK_ALL);
ab8e4aee 2338 status = nfsd4_encode_aclname(rqstp, ace, &p, &buflen);
1da177e4
LT
2339 if (status == nfserr_resource)
2340 goto out_resource;
2341 if (status)
2342 goto out;
2343 }
2344 }
2345out_acl:
2346 if (bmval0 & FATTR4_WORD0_ACLSUPPORT) {
2347 if ((buflen -= 4) < 0)
2348 goto out_resource;
2349 WRITE32(aclsupport ?
2350 ACL4_SUPPORT_ALLOW_ACL|ACL4_SUPPORT_DENY_ACL : 0);
2351 }
2352 if (bmval0 & FATTR4_WORD0_CANSETTIME) {
2353 if ((buflen -= 4) < 0)
2354 goto out_resource;
2355 WRITE32(1);
2356 }
2357 if (bmval0 & FATTR4_WORD0_CASE_INSENSITIVE) {
2358 if ((buflen -= 4) < 0)
2359 goto out_resource;
2930d381 2360 WRITE32(0);
1da177e4
LT
2361 }
2362 if (bmval0 & FATTR4_WORD0_CASE_PRESERVING) {
2363 if ((buflen -= 4) < 0)
2364 goto out_resource;
2365 WRITE32(1);
2366 }
2367 if (bmval0 & FATTR4_WORD0_CHOWN_RESTRICTED) {
2368 if ((buflen -= 4) < 0)
2369 goto out_resource;
2370 WRITE32(1);
2371 }
2372 if (bmval0 & FATTR4_WORD0_FILEHANDLE) {
2373 buflen -= (XDR_QUADLEN(fhp->fh_handle.fh_size) << 2) + 4;
2374 if (buflen < 0)
2375 goto out_resource;
2376 WRITE32(fhp->fh_handle.fh_size);
2377 WRITEMEM(&fhp->fh_handle.fh_base, fhp->fh_handle.fh_size);
2378 }
2379 if (bmval0 & FATTR4_WORD0_FILEID) {
2380 if ((buflen -= 8) < 0)
2381 goto out_resource;
40ee5dc6 2382 WRITE64(stat.ino);
1da177e4
LT
2383 }
2384 if (bmval0 & FATTR4_WORD0_FILES_AVAIL) {
2385 if ((buflen -= 8) < 0)
2386 goto out_resource;
2387 WRITE64((u64) statfs.f_ffree);
2388 }
2389 if (bmval0 & FATTR4_WORD0_FILES_FREE) {
2390 if ((buflen -= 8) < 0)
2391 goto out_resource;
2392 WRITE64((u64) statfs.f_ffree);
2393 }
2394 if (bmval0 & FATTR4_WORD0_FILES_TOTAL) {
2395 if ((buflen -= 8) < 0)
2396 goto out_resource;
2397 WRITE64((u64) statfs.f_files);
2398 }
81c3f413
BF
2399 if (bmval0 & FATTR4_WORD0_FS_LOCATIONS) {
2400 status = nfsd4_encode_fs_locations(rqstp, exp, &p, &buflen);
2401 if (status == nfserr_resource)
2402 goto out_resource;
2403 if (status)
2404 goto out;
2405 }
1da177e4
LT
2406 if (bmval0 & FATTR4_WORD0_HOMOGENEOUS) {
2407 if ((buflen -= 4) < 0)
2408 goto out_resource;
2409 WRITE32(1);
2410 }
2411 if (bmval0 & FATTR4_WORD0_MAXFILESIZE) {
2412 if ((buflen -= 8) < 0)
2413 goto out_resource;
aea240f4 2414 WRITE64(exp->ex_path.mnt->mnt_sb->s_maxbytes);
1da177e4
LT
2415 }
2416 if (bmval0 & FATTR4_WORD0_MAXLINK) {
2417 if ((buflen -= 4) < 0)
2418 goto out_resource;
2419 WRITE32(255);
2420 }
2421 if (bmval0 & FATTR4_WORD0_MAXNAME) {
2422 if ((buflen -= 4) < 0)
2423 goto out_resource;
a16e92ed 2424 WRITE32(statfs.f_namelen);
1da177e4
LT
2425 }
2426 if (bmval0 & FATTR4_WORD0_MAXREAD) {
2427 if ((buflen -= 8) < 0)
2428 goto out_resource;
7adae489 2429 WRITE64((u64) svc_max_payload(rqstp));
1da177e4
LT
2430 }
2431 if (bmval0 & FATTR4_WORD0_MAXWRITE) {
2432 if ((buflen -= 8) < 0)
2433 goto out_resource;
7adae489 2434 WRITE64((u64) svc_max_payload(rqstp));
1da177e4
LT
2435 }
2436 if (bmval1 & FATTR4_WORD1_MODE) {
2437 if ((buflen -= 4) < 0)
2438 goto out_resource;
2439 WRITE32(stat.mode & S_IALLUGO);
2440 }
2441 if (bmval1 & FATTR4_WORD1_NO_TRUNC) {
2442 if ((buflen -= 4) < 0)
2443 goto out_resource;
2444 WRITE32(1);
2445 }
2446 if (bmval1 & FATTR4_WORD1_NUMLINKS) {
2447 if ((buflen -= 4) < 0)
2448 goto out_resource;
2449 WRITE32(stat.nlink);
2450 }
2451 if (bmval1 & FATTR4_WORD1_OWNER) {
2452 status = nfsd4_encode_user(rqstp, stat.uid, &p, &buflen);
2453 if (status == nfserr_resource)
2454 goto out_resource;
2455 if (status)
2456 goto out;
2457 }
2458 if (bmval1 & FATTR4_WORD1_OWNER_GROUP) {
2459 status = nfsd4_encode_group(rqstp, stat.gid, &p, &buflen);
2460 if (status == nfserr_resource)
2461 goto out_resource;
2462 if (status)
2463 goto out;
2464 }
2465 if (bmval1 & FATTR4_WORD1_RAWDEV) {
2466 if ((buflen -= 8) < 0)
2467 goto out_resource;
2468 WRITE32((u32) MAJOR(stat.rdev));
2469 WRITE32((u32) MINOR(stat.rdev));
2470 }
2471 if (bmval1 & FATTR4_WORD1_SPACE_AVAIL) {
2472 if ((buflen -= 8) < 0)
2473 goto out_resource;
2474 dummy64 = (u64)statfs.f_bavail * (u64)statfs.f_bsize;
2475 WRITE64(dummy64);
2476 }
2477 if (bmval1 & FATTR4_WORD1_SPACE_FREE) {
2478 if ((buflen -= 8) < 0)
2479 goto out_resource;
2480 dummy64 = (u64)statfs.f_bfree * (u64)statfs.f_bsize;
2481 WRITE64(dummy64);
2482 }
2483 if (bmval1 & FATTR4_WORD1_SPACE_TOTAL) {
2484 if ((buflen -= 8) < 0)
2485 goto out_resource;
2486 dummy64 = (u64)statfs.f_blocks * (u64)statfs.f_bsize;
2487 WRITE64(dummy64);
2488 }
2489 if (bmval1 & FATTR4_WORD1_SPACE_USED) {
2490 if ((buflen -= 8) < 0)
2491 goto out_resource;
2492 dummy64 = (u64)stat.blocks << 9;
2493 WRITE64(dummy64);
2494 }
2495 if (bmval1 & FATTR4_WORD1_TIME_ACCESS) {
2496 if ((buflen -= 12) < 0)
2497 goto out_resource;
bf8d9097 2498 WRITE64((s64)stat.atime.tv_sec);
1da177e4
LT
2499 WRITE32(stat.atime.tv_nsec);
2500 }
2501 if (bmval1 & FATTR4_WORD1_TIME_DELTA) {
2502 if ((buflen -= 12) < 0)
2503 goto out_resource;
2504 WRITE32(0);
2505 WRITE32(1);
2506 WRITE32(0);
2507 }
2508 if (bmval1 & FATTR4_WORD1_TIME_METADATA) {
2509 if ((buflen -= 12) < 0)
2510 goto out_resource;
bf8d9097 2511 WRITE64((s64)stat.ctime.tv_sec);
1da177e4
LT
2512 WRITE32(stat.ctime.tv_nsec);
2513 }
2514 if (bmval1 & FATTR4_WORD1_TIME_MODIFY) {
2515 if ((buflen -= 12) < 0)
2516 goto out_resource;
bf8d9097 2517 WRITE64((s64)stat.mtime.tv_sec);
1da177e4
LT
2518 WRITE32(stat.mtime.tv_nsec);
2519 }
2520 if (bmval1 & FATTR4_WORD1_MOUNTED_ON_FILEID) {
1da177e4
LT
2521 if ((buflen -= 8) < 0)
2522 goto out_resource;
406a7ea9
FF
2523 /*
2524 * Get parent's attributes if not ignoring crossmount
2525 * and this is the root of a cross-mounted filesystem.
2526 */
2527 if (ignore_crossmnt == 0 &&
ae7095a7
BF
2528 dentry == exp->ex_path.mnt->mnt_root)
2529 get_parent_attributes(exp, &stat);
40ee5dc6 2530 WRITE64(stat.ino);
1da177e4 2531 }
18032ca0
DQ
2532 if (bmval2 & FATTR4_WORD2_SECURITY_LABEL) {
2533 status = nfsd4_encode_security_label(rqstp, context,
2534 contextlen, &p, &buflen);
2535 if (status)
2536 goto out;
2537 }
8c18f205
BH
2538 if (bmval2 & FATTR4_WORD2_SUPPATTR_EXCLCREAT) {
2539 WRITE32(3);
2540 WRITE32(NFSD_SUPPATTR_EXCLCREAT_WORD0);
2541 WRITE32(NFSD_SUPPATTR_EXCLCREAT_WORD1);
2542 WRITE32(NFSD_SUPPATTR_EXCLCREAT_WORD2);
2543 }
7e705706 2544
1da177e4 2545 *attrlenp = htonl((char *)p - (char *)attrlenp - 4);
84822d0b 2546 *buffer = p;
1da177e4
LT
2547 status = nfs_ok;
2548
2549out:
ba4e55bb 2550#ifdef CONFIG_NFSD_V4_SECURITY_LABEL
18032ca0
DQ
2551 if (context)
2552 security_release_secctx(context, contextlen);
ba4e55bb 2553#endif /* CONFIG_NFSD_V4_SECURITY_LABEL */
28e05dd8 2554 kfree(acl);
1da177e4
LT
2555 if (fhp == &tempfh)
2556 fh_put(&tempfh);
2557 return status;
2558out_nfserr:
b8dd7b9a 2559 status = nfserrno(err);
1da177e4
LT
2560 goto out;
2561out_resource:
1da177e4
LT
2562 status = nfserr_resource;
2563 goto out;
2564out_serverfault:
2565 status = nfserr_serverfault;
2566 goto out;
2567}
2568
c0ce6ec8
BF
2569static inline int attributes_need_mount(u32 *bmval)
2570{
2571 if (bmval[0] & ~(FATTR4_WORD0_RDATTR_ERROR | FATTR4_WORD0_LEASE_TIME))
2572 return 1;
2573 if (bmval[1] & ~FATTR4_WORD1_MOUNTED_ON_FILEID)
2574 return 1;
2575 return 0;
2576}
2577
b37ad28b 2578static __be32
1da177e4 2579nfsd4_encode_dirent_fattr(struct nfsd4_readdir *cd,
84822d0b 2580 const char *name, int namlen, __be32 **p, int buflen)
1da177e4
LT
2581{
2582 struct svc_export *exp = cd->rd_fhp->fh_export;
2583 struct dentry *dentry;
b37ad28b 2584 __be32 nfserr;
406a7ea9 2585 int ignore_crossmnt = 0;
1da177e4
LT
2586
2587 dentry = lookup_one_len(name, cd->rd_fhp->fh_dentry, namlen);
2588 if (IS_ERR(dentry))
2589 return nfserrno(PTR_ERR(dentry));
b2c0cea6
BF
2590 if (!dentry->d_inode) {
2591 /*
2592 * nfsd_buffered_readdir drops the i_mutex between
2593 * readdir and calling this callback, leaving a window
2594 * where this directory entry could have gone away.
2595 */
2596 dput(dentry);
2597 return nfserr_noent;
2598 }
1da177e4
LT
2599
2600 exp_get(exp);
406a7ea9
FF
2601 /*
2602 * In the case of a mountpoint, the client may be asking for
2603 * attributes that are only properties of the underlying filesystem
2604 * as opposed to the cross-mounted file system. In such a case,
2605 * we will not follow the cross mount and will fill the attribtutes
2606 * directly from the mountpoint dentry.
2607 */
3227fa41 2608 if (nfsd_mountpoint(dentry, exp)) {
021d3a72
BF
2609 int err;
2610
3227fa41
BF
2611 if (!(exp->ex_flags & NFSEXP_V4ROOT)
2612 && !attributes_need_mount(cd->rd_bmval)) {
2613 ignore_crossmnt = 1;
2614 goto out_encode;
2615 }
dcb488a3
AA
2616 /*
2617 * Why the heck aren't we just using nfsd_lookup??
2618 * Different "."/".." handling? Something else?
2619 * At least, add a comment here to explain....
2620 */
021d3a72
BF
2621 err = nfsd_cross_mnt(cd->rd_rqstp, &dentry, &exp);
2622 if (err) {
2623 nfserr = nfserrno(err);
1da177e4
LT
2624 goto out_put;
2625 }
dcb488a3
AA
2626 nfserr = check_nfsd_access(exp, cd->rd_rqstp);
2627 if (nfserr)
2628 goto out_put;
1da177e4
LT
2629
2630 }
3227fa41 2631out_encode:
1da177e4 2632 nfserr = nfsd4_encode_fattr(NULL, exp, dentry, p, buflen, cd->rd_bmval,
406a7ea9 2633 cd->rd_rqstp, ignore_crossmnt);
1da177e4
LT
2634out_put:
2635 dput(dentry);
2636 exp_put(exp);
2637 return nfserr;
2638}
2639
2ebbc012 2640static __be32 *
b37ad28b 2641nfsd4_encode_rdattr_error(__be32 *p, int buflen, __be32 nfserr)
1da177e4 2642{
2ebbc012 2643 __be32 *attrlenp;
1da177e4
LT
2644
2645 if (buflen < 6)
2646 return NULL;
2647 *p++ = htonl(2);
2648 *p++ = htonl(FATTR4_WORD0_RDATTR_ERROR); /* bmval0 */
2649 *p++ = htonl(0); /* bmval1 */
2650
2651 attrlenp = p++;
2652 *p++ = nfserr; /* no htonl */
2653 *attrlenp = htonl((char *)p - (char *)attrlenp - 4);
2654 return p;
2655}
2656
2657static int
a0ad13ef
N
2658nfsd4_encode_dirent(void *ccdv, const char *name, int namlen,
2659 loff_t offset, u64 ino, unsigned int d_type)
1da177e4 2660{
a0ad13ef 2661 struct readdir_cd *ccd = ccdv;
1da177e4
LT
2662 struct nfsd4_readdir *cd = container_of(ccd, struct nfsd4_readdir, common);
2663 int buflen;
2ebbc012 2664 __be32 *p = cd->buffer;
b2c0cea6 2665 __be32 *cookiep;
b37ad28b 2666 __be32 nfserr = nfserr_toosmall;
1da177e4
LT
2667
2668 /* In nfsv4, "." and ".." never make it onto the wire.. */
2669 if (name && isdotent(name, namlen)) {
2670 cd->common.err = nfs_ok;
2671 return 0;
2672 }
2673
2674 if (cd->offset)
2675 xdr_encode_hyper(cd->offset, (u64) offset);
2676
2677 buflen = cd->buflen - 4 - XDR_QUADLEN(namlen);
2678 if (buflen < 0)
2679 goto fail;
2680
2681 *p++ = xdr_one; /* mark entry present */
b2c0cea6 2682 cookiep = p;
1da177e4
LT
2683 p = xdr_encode_hyper(p, NFS_OFFSET_MAX); /* offset of next entry */
2684 p = xdr_encode_array(p, name, namlen); /* name length & name */
2685
84822d0b 2686 nfserr = nfsd4_encode_dirent_fattr(cd, name, namlen, &p, buflen);
1da177e4
LT
2687 switch (nfserr) {
2688 case nfs_ok:
1da177e4
LT
2689 break;
2690 case nfserr_resource:
2691 nfserr = nfserr_toosmall;
2692 goto fail;
b2c0cea6
BF
2693 case nfserr_noent:
2694 goto skip_entry;
1da177e4
LT
2695 default:
2696 /*
2697 * If the client requested the RDATTR_ERROR attribute,
2698 * we stuff the error code into this attribute
2699 * and continue. If this attribute was not requested,
2700 * then in accordance with the spec, we fail the
2701 * entire READDIR operation(!)
2702 */
2703 if (!(cd->rd_bmval[0] & FATTR4_WORD0_RDATTR_ERROR))
2704 goto fail;
1da177e4 2705 p = nfsd4_encode_rdattr_error(p, buflen, nfserr);
34081efc
FI
2706 if (p == NULL) {
2707 nfserr = nfserr_toosmall;
1da177e4 2708 goto fail;
34081efc 2709 }
1da177e4
LT
2710 }
2711 cd->buflen -= (p - cd->buffer);
2712 cd->buffer = p;
b2c0cea6
BF
2713 cd->offset = cookiep;
2714skip_entry:
1da177e4
LT
2715 cd->common.err = nfs_ok;
2716 return 0;
2717fail:
2718 cd->common.err = nfserr;
2719 return -EINVAL;
2720}
2721
e2f282b9
BH
2722static void
2723nfsd4_encode_stateid(struct nfsd4_compoundres *resp, stateid_t *sid)
2724{
bc749ca4 2725 __be32 *p;
e2f282b9
BH
2726
2727 RESERVE_SPACE(sizeof(stateid_t));
2728 WRITE32(sid->si_generation);
2729 WRITEMEM(&sid->si_opaque, sizeof(stateid_opaque_t));
2730 ADJUST_ARGS();
2731}
2732
695e12f8 2733static __be32
b37ad28b 2734nfsd4_encode_access(struct nfsd4_compoundres *resp, __be32 nfserr, struct nfsd4_access *access)
1da177e4 2735{
bc749ca4 2736 __be32 *p;
1da177e4
LT
2737
2738 if (!nfserr) {
2739 RESERVE_SPACE(8);
2740 WRITE32(access->ac_supported);
2741 WRITE32(access->ac_resp_access);
2742 ADJUST_ARGS();
2743 }
695e12f8 2744 return nfserr;
1da177e4
LT
2745}
2746
1d1bc8f2
BF
2747static __be32 nfsd4_encode_bind_conn_to_session(struct nfsd4_compoundres *resp, __be32 nfserr, struct nfsd4_bind_conn_to_session *bcts)
2748{
2749 __be32 *p;
2750
2751 if (!nfserr) {
2752 RESERVE_SPACE(NFS4_MAX_SESSIONID_LEN + 8);
2753 WRITEMEM(bcts->sessionid.data, NFS4_MAX_SESSIONID_LEN);
2754 WRITE32(bcts->dir);
6e67b5d1 2755 /* Sorry, we do not yet support RDMA over 4.1: */
1d1bc8f2
BF
2756 WRITE32(0);
2757 ADJUST_ARGS();
2758 }
2759 return nfserr;
2760}
2761
695e12f8 2762static __be32
b37ad28b 2763nfsd4_encode_close(struct nfsd4_compoundres *resp, __be32 nfserr, struct nfsd4_close *close)
1da177e4 2764{
e2f282b9
BH
2765 if (!nfserr)
2766 nfsd4_encode_stateid(resp, &close->cl_stateid);
2767
695e12f8 2768 return nfserr;
1da177e4
LT
2769}
2770
2771
695e12f8 2772static __be32
b37ad28b 2773nfsd4_encode_commit(struct nfsd4_compoundres *resp, __be32 nfserr, struct nfsd4_commit *commit)
1da177e4 2774{
bc749ca4 2775 __be32 *p;
1da177e4
LT
2776
2777 if (!nfserr) {
ab4684d1
CL
2778 RESERVE_SPACE(NFS4_VERIFIER_SIZE);
2779 WRITEMEM(commit->co_verf.data, NFS4_VERIFIER_SIZE);
1da177e4
LT
2780 ADJUST_ARGS();
2781 }
695e12f8 2782 return nfserr;
1da177e4
LT
2783}
2784
695e12f8 2785static __be32
b37ad28b 2786nfsd4_encode_create(struct nfsd4_compoundres *resp, __be32 nfserr, struct nfsd4_create *create)
1da177e4 2787{
bc749ca4 2788 __be32 *p;
1da177e4
LT
2789
2790 if (!nfserr) {
2791 RESERVE_SPACE(32);
c654b8a9 2792 write_cinfo(&p, &create->cr_cinfo);
1da177e4
LT
2793 WRITE32(2);
2794 WRITE32(create->cr_bmval[0]);
2795 WRITE32(create->cr_bmval[1]);
2796 ADJUST_ARGS();
2797 }
695e12f8 2798 return nfserr;
1da177e4
LT
2799}
2800
b37ad28b
AV
2801static __be32
2802nfsd4_encode_getattr(struct nfsd4_compoundres *resp, __be32 nfserr, struct nfsd4_getattr *getattr)
1da177e4
LT
2803{
2804 struct svc_fh *fhp = getattr->ga_fhp;
2805 int buflen;
2806
2807 if (nfserr)
2808 return nfserr;
2809
2810 buflen = resp->end - resp->p - (COMPOUND_ERR_SLACK_SPACE >> 2);
2811 nfserr = nfsd4_encode_fattr(fhp, fhp->fh_export, fhp->fh_dentry,
84822d0b 2812 &resp->p, buflen, getattr->ga_bmval,
406a7ea9 2813 resp->rqstp, 0);
1da177e4
LT
2814 return nfserr;
2815}
2816
695e12f8
BH
2817static __be32
2818nfsd4_encode_getfh(struct nfsd4_compoundres *resp, __be32 nfserr, struct svc_fh **fhpp)
1da177e4 2819{
695e12f8 2820 struct svc_fh *fhp = *fhpp;
1da177e4 2821 unsigned int len;
bc749ca4 2822 __be32 *p;
1da177e4
LT
2823
2824 if (!nfserr) {
2825 len = fhp->fh_handle.fh_size;
2826 RESERVE_SPACE(len + 4);
2827 WRITE32(len);
2828 WRITEMEM(&fhp->fh_handle.fh_base, len);
2829 ADJUST_ARGS();
2830 }
695e12f8 2831 return nfserr;
1da177e4
LT
2832}
2833
2834/*
2835* Including all fields other than the name, a LOCK4denied structure requires
2836* 8(clientid) + 4(namelen) + 8(offset) + 8(length) + 4(type) = 32 bytes.
2837*/
2838static void
2839nfsd4_encode_lock_denied(struct nfsd4_compoundres *resp, struct nfsd4_lock_denied *ld)
2840{
7c13f344 2841 struct xdr_netobj *conf = &ld->ld_owner;
bc749ca4 2842 __be32 *p;
1da177e4 2843
7c13f344 2844 RESERVE_SPACE(32 + XDR_LEN(conf->len));
1da177e4
LT
2845 WRITE64(ld->ld_start);
2846 WRITE64(ld->ld_length);
2847 WRITE32(ld->ld_type);
7c13f344 2848 if (conf->len) {
1da177e4 2849 WRITEMEM(&ld->ld_clientid, 8);
7c13f344
BF
2850 WRITE32(conf->len);
2851 WRITEMEM(conf->data, conf->len);
2852 kfree(conf->data);
1da177e4
LT
2853 } else { /* non - nfsv4 lock in conflict, no clientid nor owner */
2854 WRITE64((u64)0); /* clientid */
2855 WRITE32(0); /* length of owner name */
2856 }
2857 ADJUST_ARGS();
2858}
2859
695e12f8 2860static __be32
b37ad28b 2861nfsd4_encode_lock(struct nfsd4_compoundres *resp, __be32 nfserr, struct nfsd4_lock *lock)
1da177e4 2862{
e2f282b9
BH
2863 if (!nfserr)
2864 nfsd4_encode_stateid(resp, &lock->lk_resp_stateid);
2865 else if (nfserr == nfserr_denied)
1da177e4
LT
2866 nfsd4_encode_lock_denied(resp, &lock->lk_denied);
2867
695e12f8 2868 return nfserr;
1da177e4
LT
2869}
2870
695e12f8 2871static __be32
b37ad28b 2872nfsd4_encode_lockt(struct nfsd4_compoundres *resp, __be32 nfserr, struct nfsd4_lockt *lockt)
1da177e4
LT
2873{
2874 if (nfserr == nfserr_denied)
2875 nfsd4_encode_lock_denied(resp, &lockt->lt_denied);
695e12f8 2876 return nfserr;
1da177e4
LT
2877}
2878
695e12f8 2879static __be32
b37ad28b 2880nfsd4_encode_locku(struct nfsd4_compoundres *resp, __be32 nfserr, struct nfsd4_locku *locku)
1da177e4 2881{
e2f282b9
BH
2882 if (!nfserr)
2883 nfsd4_encode_stateid(resp, &locku->lu_stateid);
2884
695e12f8 2885 return nfserr;
1da177e4
LT
2886}
2887
2888
695e12f8 2889static __be32
b37ad28b 2890nfsd4_encode_link(struct nfsd4_compoundres *resp, __be32 nfserr, struct nfsd4_link *link)
1da177e4 2891{
bc749ca4 2892 __be32 *p;
1da177e4
LT
2893
2894 if (!nfserr) {
2895 RESERVE_SPACE(20);
c654b8a9 2896 write_cinfo(&p, &link->li_cinfo);
1da177e4
LT
2897 ADJUST_ARGS();
2898 }
695e12f8 2899 return nfserr;
1da177e4
LT
2900}
2901
2902
695e12f8 2903static __be32
b37ad28b 2904nfsd4_encode_open(struct nfsd4_compoundres *resp, __be32 nfserr, struct nfsd4_open *open)
1da177e4 2905{
bc749ca4 2906 __be32 *p;
1da177e4
LT
2907
2908 if (nfserr)
2909 goto out;
2910
e2f282b9
BH
2911 nfsd4_encode_stateid(resp, &open->op_stateid);
2912 RESERVE_SPACE(40);
c654b8a9 2913 write_cinfo(&p, &open->op_cinfo);
1da177e4
LT
2914 WRITE32(open->op_rflags);
2915 WRITE32(2);
2916 WRITE32(open->op_bmval[0]);
2917 WRITE32(open->op_bmval[1]);
2918 WRITE32(open->op_delegate_type);
2919 ADJUST_ARGS();
2920
2921 switch (open->op_delegate_type) {
2922 case NFS4_OPEN_DELEGATE_NONE:
2923 break;
2924 case NFS4_OPEN_DELEGATE_READ:
e2f282b9
BH
2925 nfsd4_encode_stateid(resp, &open->op_delegate_stateid);
2926 RESERVE_SPACE(20);
7b190fec 2927 WRITE32(open->op_recall);
1da177e4
LT
2928
2929 /*
2930 * TODO: ACE's in delegations
2931 */
2932 WRITE32(NFS4_ACE_ACCESS_ALLOWED_ACE_TYPE);
2933 WRITE32(0);
2934 WRITE32(0);
2935 WRITE32(0); /* XXX: is NULL principal ok? */
2936 ADJUST_ARGS();
2937 break;
2938 case NFS4_OPEN_DELEGATE_WRITE:
e2f282b9
BH
2939 nfsd4_encode_stateid(resp, &open->op_delegate_stateid);
2940 RESERVE_SPACE(32);
1da177e4
LT
2941 WRITE32(0);
2942
2943 /*
2944 * TODO: space_limit's in delegations
2945 */
2946 WRITE32(NFS4_LIMIT_SIZE);
2947 WRITE32(~(u32)0);
2948 WRITE32(~(u32)0);
2949
2950 /*
2951 * TODO: ACE's in delegations
2952 */
2953 WRITE32(NFS4_ACE_ACCESS_ALLOWED_ACE_TYPE);
2954 WRITE32(0);
2955 WRITE32(0);
2956 WRITE32(0); /* XXX: is NULL principal ok? */
2957 ADJUST_ARGS();
2958 break;
d24433cd
BH
2959 case NFS4_OPEN_DELEGATE_NONE_EXT: /* 4.1 */
2960 switch (open->op_why_no_deleg) {
2961 case WND4_CONTENTION:
2962 case WND4_RESOURCE:
2963 RESERVE_SPACE(8);
2964 WRITE32(open->op_why_no_deleg);
2965 WRITE32(0); /* deleg signaling not supported yet */
2966 break;
2967 default:
2968 RESERVE_SPACE(4);
2969 WRITE32(open->op_why_no_deleg);
2970 }
2971 ADJUST_ARGS();
2972 break;
1da177e4
LT
2973 default:
2974 BUG();
2975 }
2976 /* XXX save filehandle here */
2977out:
695e12f8 2978 return nfserr;
1da177e4
LT
2979}
2980
695e12f8 2981static __be32
b37ad28b 2982nfsd4_encode_open_confirm(struct nfsd4_compoundres *resp, __be32 nfserr, struct nfsd4_open_confirm *oc)
1da177e4 2983{
e2f282b9
BH
2984 if (!nfserr)
2985 nfsd4_encode_stateid(resp, &oc->oc_resp_stateid);
1da177e4 2986
695e12f8 2987 return nfserr;
1da177e4
LT
2988}
2989
695e12f8 2990static __be32
b37ad28b 2991nfsd4_encode_open_downgrade(struct nfsd4_compoundres *resp, __be32 nfserr, struct nfsd4_open_downgrade *od)
1da177e4 2992{
e2f282b9
BH
2993 if (!nfserr)
2994 nfsd4_encode_stateid(resp, &od->od_stateid);
1da177e4 2995
695e12f8 2996 return nfserr;
1da177e4
LT
2997}
2998
b37ad28b
AV
2999static __be32
3000nfsd4_encode_read(struct nfsd4_compoundres *resp, __be32 nfserr,
44524359 3001 struct nfsd4_read *read)
1da177e4
LT
3002{
3003 u32 eof;
afc59400
BF
3004 int v;
3005 struct page *page;
1da177e4
LT
3006 unsigned long maxcount;
3007 long len;
bc749ca4 3008 __be32 *p;
1da177e4
LT
3009
3010 if (nfserr)
3011 return nfserr;
3012 if (resp->xbuf->page_len)
3013 return nfserr_resource;
3014
3015 RESERVE_SPACE(8); /* eof flag and byte count */
3016
7adae489 3017 maxcount = svc_max_payload(resp->rqstp);
1da177e4
LT
3018 if (maxcount > read->rd_length)
3019 maxcount = read->rd_length;
3020
3021 len = maxcount;
3022 v = 0;
3023 while (len > 0) {
afc59400
BF
3024 page = *(resp->rqstp->rq_next_page);
3025 if (!page) { /* ran out of pages */
d5f50b0c
BF
3026 maxcount -= len;
3027 break;
3028 }
afc59400 3029 resp->rqstp->rq_vec[v].iov_base = page_address(page);
3cc03b16 3030 resp->rqstp->rq_vec[v].iov_len =
44524359 3031 len < PAGE_SIZE ? len : PAGE_SIZE;
afc59400 3032 resp->rqstp->rq_next_page++;
1da177e4
LT
3033 v++;
3034 len -= PAGE_SIZE;
3035 }
3036 read->rd_vlen = v;
3037
039a87ca 3038 nfserr = nfsd_read_file(read->rd_rqstp, read->rd_fhp, read->rd_filp,
3cc03b16 3039 read->rd_offset, resp->rqstp->rq_vec, read->rd_vlen,
1da177e4
LT
3040 &maxcount);
3041
1da177e4
LT
3042 if (nfserr)
3043 return nfserr;
44524359
N
3044 eof = (read->rd_offset + maxcount >=
3045 read->rd_fhp->fh_dentry->d_inode->i_size);
1da177e4
LT
3046
3047 WRITE32(eof);
3048 WRITE32(maxcount);
3049 ADJUST_ARGS();
6ed6decc
N
3050 resp->xbuf->head[0].iov_len = (char*)p
3051 - (char*)resp->xbuf->head[0].iov_base;
1da177e4
LT
3052 resp->xbuf->page_len = maxcount;
3053
6ed6decc 3054 /* Use rest of head for padding and remaining ops: */
6ed6decc 3055 resp->xbuf->tail[0].iov_base = p;
1da177e4 3056 resp->xbuf->tail[0].iov_len = 0;
1da177e4 3057 if (maxcount&3) {
6ed6decc
N
3058 RESERVE_SPACE(4);
3059 WRITE32(0);
1da177e4
LT
3060 resp->xbuf->tail[0].iov_base += maxcount&3;
3061 resp->xbuf->tail[0].iov_len = 4 - (maxcount&3);
6ed6decc 3062 ADJUST_ARGS();
1da177e4
LT
3063 }
3064 return 0;
3065}
3066
b37ad28b
AV
3067static __be32
3068nfsd4_encode_readlink(struct nfsd4_compoundres *resp, __be32 nfserr, struct nfsd4_readlink *readlink)
1da177e4
LT
3069{
3070 int maxcount;
3071 char *page;
bc749ca4 3072 __be32 *p;
1da177e4
LT
3073
3074 if (nfserr)
3075 return nfserr;
3076 if (resp->xbuf->page_len)
3077 return nfserr_resource;
afc59400 3078 if (!*resp->rqstp->rq_next_page)
d5f50b0c 3079 return nfserr_resource;
1da177e4 3080
afc59400 3081 page = page_address(*(resp->rqstp->rq_next_page++));
1da177e4
LT
3082
3083 maxcount = PAGE_SIZE;
3084 RESERVE_SPACE(4);
3085
3086 /*
3087 * XXX: By default, the ->readlink() VFS op will truncate symlinks
3088 * if they would overflow the buffer. Is this kosher in NFSv4? If
3089 * not, one easy fix is: if ->readlink() precisely fills the buffer,
3090 * assume that truncation occurred, and return NFS4ERR_RESOURCE.
3091 */
3092 nfserr = nfsd_readlink(readlink->rl_rqstp, readlink->rl_fhp, page, &maxcount);
3093 if (nfserr == nfserr_isdir)
3094 return nfserr_inval;
3095 if (nfserr)
3096 return nfserr;
3097
3098 WRITE32(maxcount);
3099 ADJUST_ARGS();
6ed6decc
N
3100 resp->xbuf->head[0].iov_len = (char*)p
3101 - (char*)resp->xbuf->head[0].iov_base;
3102 resp->xbuf->page_len = maxcount;
1da177e4 3103
6ed6decc 3104 /* Use rest of head for padding and remaining ops: */
6ed6decc 3105 resp->xbuf->tail[0].iov_base = p;
1da177e4 3106 resp->xbuf->tail[0].iov_len = 0;
1da177e4 3107 if (maxcount&3) {
6ed6decc
N
3108 RESERVE_SPACE(4);
3109 WRITE32(0);
1da177e4
LT
3110 resp->xbuf->tail[0].iov_base += maxcount&3;
3111 resp->xbuf->tail[0].iov_len = 4 - (maxcount&3);
6ed6decc 3112 ADJUST_ARGS();
1da177e4
LT
3113 }
3114 return 0;
3115}
3116
b37ad28b
AV
3117static __be32
3118nfsd4_encode_readdir(struct nfsd4_compoundres *resp, __be32 nfserr, struct nfsd4_readdir *readdir)
1da177e4
LT
3119{
3120 int maxcount;
3121 loff_t offset;
2ebbc012 3122 __be32 *page, *savep, *tailbase;
bc749ca4 3123 __be32 *p;
1da177e4
LT
3124
3125 if (nfserr)
3126 return nfserr;
3127 if (resp->xbuf->page_len)
3128 return nfserr_resource;
afc59400 3129 if (!*resp->rqstp->rq_next_page)
d5f50b0c 3130 return nfserr_resource;
1da177e4 3131
ab4684d1 3132 RESERVE_SPACE(NFS4_VERIFIER_SIZE);
1da177e4
LT
3133 savep = p;
3134
3135 /* XXX: Following NFSv3, we ignore the READDIR verifier for now. */
3136 WRITE32(0);
3137 WRITE32(0);
3138 ADJUST_ARGS();
3139 resp->xbuf->head[0].iov_len = ((char*)resp->p) - (char*)resp->xbuf->head[0].iov_base;
bb6e8a9f 3140 tailbase = p;
1da177e4
LT
3141
3142 maxcount = PAGE_SIZE;
3143 if (maxcount > readdir->rd_maxcount)
3144 maxcount = readdir->rd_maxcount;
3145
3146 /*
3147 * Convert from bytes to words, account for the two words already
3148 * written, make sure to leave two words at the end for the next
3149 * pointer and eof field.
3150 */
3151 maxcount = (maxcount >> 2) - 4;
3152 if (maxcount < 0) {
3153 nfserr = nfserr_toosmall;
3154 goto err_no_verf;
3155 }
3156
afc59400 3157 page = page_address(*(resp->rqstp->rq_next_page++));
1da177e4
LT
3158 readdir->common.err = 0;
3159 readdir->buflen = maxcount;
3160 readdir->buffer = page;
3161 readdir->offset = NULL;
3162
3163 offset = readdir->rd_cookie;
3164 nfserr = nfsd_readdir(readdir->rd_rqstp, readdir->rd_fhp,
3165 &offset,
3166 &readdir->common, nfsd4_encode_dirent);
3167 if (nfserr == nfs_ok &&
3168 readdir->common.err == nfserr_toosmall &&
3169 readdir->buffer == page)
3170 nfserr = nfserr_toosmall;
1da177e4
LT
3171 if (nfserr)
3172 goto err_no_verf;
3173
3174 if (readdir->offset)
3175 xdr_encode_hyper(readdir->offset, offset);
3176
3177 p = readdir->buffer;
3178 *p++ = 0; /* no more entries */
3179 *p++ = htonl(readdir->common.err == nfserr_eof);
afc59400
BF
3180 resp->xbuf->page_len = ((char*)p) -
3181 (char*)page_address(*(resp->rqstp->rq_next_page-1));
1da177e4 3182
bb6e8a9f 3183 /* Use rest of head for padding and remaining ops: */
bb6e8a9f 3184 resp->xbuf->tail[0].iov_base = tailbase;
1da177e4
LT
3185 resp->xbuf->tail[0].iov_len = 0;
3186 resp->p = resp->xbuf->tail[0].iov_base;
bb6e8a9f 3187 resp->end = resp->p + (PAGE_SIZE - resp->xbuf->head[0].iov_len)/4;
1da177e4
LT
3188
3189 return 0;
3190err_no_verf:
3191 p = savep;
3192 ADJUST_ARGS();
3193 return nfserr;
3194}
3195
695e12f8 3196static __be32
b37ad28b 3197nfsd4_encode_remove(struct nfsd4_compoundres *resp, __be32 nfserr, struct nfsd4_remove *remove)
1da177e4 3198{
bc749ca4 3199 __be32 *p;
1da177e4
LT
3200
3201 if (!nfserr) {
3202 RESERVE_SPACE(20);
c654b8a9 3203 write_cinfo(&p, &remove->rm_cinfo);
1da177e4
LT
3204 ADJUST_ARGS();
3205 }
695e12f8 3206 return nfserr;
1da177e4
LT
3207}
3208
695e12f8 3209static __be32
b37ad28b 3210nfsd4_encode_rename(struct nfsd4_compoundres *resp, __be32 nfserr, struct nfsd4_rename *rename)
1da177e4 3211{
bc749ca4 3212 __be32 *p;
1da177e4
LT
3213
3214 if (!nfserr) {
3215 RESERVE_SPACE(40);
c654b8a9
BF
3216 write_cinfo(&p, &rename->rn_sinfo);
3217 write_cinfo(&p, &rename->rn_tinfo);
1da177e4
LT
3218 ADJUST_ARGS();
3219 }
695e12f8 3220 return nfserr;
1da177e4
LT
3221}
3222
695e12f8 3223static __be32
22b6dee8 3224nfsd4_do_encode_secinfo(struct nfsd4_compoundres *resp,
a77c806f 3225 __be32 nfserr, struct svc_export *exp)
dcb488a3 3226{
676e4ebd 3227 u32 i, nflavs, supported;
4796f457
BF
3228 struct exp_flavor_info *flavs;
3229 struct exp_flavor_info def_flavs[2];
676e4ebd
CL
3230 __be32 *p, *flavorsp;
3231 static bool report = true;
dcb488a3
AA
3232
3233 if (nfserr)
3234 goto out;
4796f457
BF
3235 if (exp->ex_nflavors) {
3236 flavs = exp->ex_flavors;
3237 nflavs = exp->ex_nflavors;
3238 } else { /* Handling of some defaults in absence of real secinfo: */
3239 flavs = def_flavs;
3240 if (exp->ex_client->flavour->flavour == RPC_AUTH_UNIX) {
3241 nflavs = 2;
3242 flavs[0].pseudoflavor = RPC_AUTH_UNIX;
3243 flavs[1].pseudoflavor = RPC_AUTH_NULL;
3244 } else if (exp->ex_client->flavour->flavour == RPC_AUTH_GSS) {
3245 nflavs = 1;
3246 flavs[0].pseudoflavor
3247 = svcauth_gss_flavor(exp->ex_client);
3248 } else {
3249 nflavs = 1;
3250 flavs[0].pseudoflavor
3251 = exp->ex_client->flavour->flavour;
3252 }
3253 }
3254
676e4ebd 3255 supported = 0;
dcb488a3 3256 RESERVE_SPACE(4);
676e4ebd 3257 flavorsp = p++; /* to be backfilled later */
dcb488a3 3258 ADJUST_ARGS();
676e4ebd 3259
4796f457 3260 for (i = 0; i < nflavs; i++) {
676e4ebd 3261 rpc_authflavor_t pf = flavs[i].pseudoflavor;
a77c806f 3262 struct rpcsec_gss_info info;
dcb488a3 3263
676e4ebd
CL
3264 if (rpcauth_get_gssinfo(pf, &info) == 0) {
3265 supported++;
a9f7b4a0 3266 RESERVE_SPACE(4 + 4 + XDR_LEN(info.oid.len) + 4 + 4);
dcb488a3 3267 WRITE32(RPC_AUTH_GSS);
a77c806f
CL
3268 WRITE32(info.oid.len);
3269 WRITEMEM(info.oid.data, info.oid.len);
a77c806f 3270 WRITE32(info.qop);
a77c806f 3271 WRITE32(info.service);
dcb488a3 3272 ADJUST_ARGS();
676e4ebd
CL
3273 } else if (pf < RPC_AUTH_MAXFLAVOR) {
3274 supported++;
dcb488a3 3275 RESERVE_SPACE(4);
676e4ebd 3276 WRITE32(pf);
dcb488a3 3277 ADJUST_ARGS();
676e4ebd
CL
3278 } else {
3279 if (report)
3280 pr_warn("NFS: SECINFO: security flavor %u "
3281 "is not supported\n", pf);
dcb488a3
AA
3282 }
3283 }
a77c806f 3284
676e4ebd
CL
3285 if (nflavs != supported)
3286 report = false;
3287 *flavorsp = htonl(supported);
3288
dcb488a3
AA
3289out:
3290 if (exp)
3291 exp_put(exp);
695e12f8 3292 return nfserr;
dcb488a3
AA
3293}
3294
22b6dee8
MJ
3295static __be32
3296nfsd4_encode_secinfo(struct nfsd4_compoundres *resp, __be32 nfserr,
3297 struct nfsd4_secinfo *secinfo)
3298{
3299 return nfsd4_do_encode_secinfo(resp, nfserr, secinfo->si_exp);
3300}
3301
3302static __be32
3303nfsd4_encode_secinfo_no_name(struct nfsd4_compoundres *resp, __be32 nfserr,
3304 struct nfsd4_secinfo_no_name *secinfo)
3305{
3306 return nfsd4_do_encode_secinfo(resp, nfserr, secinfo->sin_exp);
3307}
3308
1da177e4
LT
3309/*
3310 * The SETATTR encode routine is special -- it always encodes a bitmap,
3311 * regardless of the error status.
3312 */
695e12f8 3313static __be32
b37ad28b 3314nfsd4_encode_setattr(struct nfsd4_compoundres *resp, __be32 nfserr, struct nfsd4_setattr *setattr)
1da177e4 3315{
bc749ca4 3316 __be32 *p;
1da177e4 3317
18032ca0 3318 RESERVE_SPACE(16);
1da177e4 3319 if (nfserr) {
18032ca0
DQ
3320 WRITE32(3);
3321 WRITE32(0);
1da177e4
LT
3322 WRITE32(0);
3323 WRITE32(0);
3324 }
3325 else {
18032ca0 3326 WRITE32(3);
1da177e4
LT
3327 WRITE32(setattr->sa_bmval[0]);
3328 WRITE32(setattr->sa_bmval[1]);
18032ca0 3329 WRITE32(setattr->sa_bmval[2]);
1da177e4
LT
3330 }
3331 ADJUST_ARGS();
695e12f8 3332 return nfserr;
1da177e4
LT
3333}
3334
695e12f8 3335static __be32
b37ad28b 3336nfsd4_encode_setclientid(struct nfsd4_compoundres *resp, __be32 nfserr, struct nfsd4_setclientid *scd)
1da177e4 3337{
bc749ca4 3338 __be32 *p;
1da177e4
LT
3339
3340 if (!nfserr) {
ab4684d1 3341 RESERVE_SPACE(8 + NFS4_VERIFIER_SIZE);
1da177e4 3342 WRITEMEM(&scd->se_clientid, 8);
ab4684d1 3343 WRITEMEM(&scd->se_confirm, NFS4_VERIFIER_SIZE);
1da177e4
LT
3344 ADJUST_ARGS();
3345 }
3346 else if (nfserr == nfserr_clid_inuse) {
3347 RESERVE_SPACE(8);
3348 WRITE32(0);
3349 WRITE32(0);
3350 ADJUST_ARGS();
3351 }
695e12f8 3352 return nfserr;
1da177e4
LT
3353}
3354
695e12f8 3355static __be32
b37ad28b 3356nfsd4_encode_write(struct nfsd4_compoundres *resp, __be32 nfserr, struct nfsd4_write *write)
1da177e4 3357{
bc749ca4 3358 __be32 *p;
1da177e4
LT
3359
3360 if (!nfserr) {
3361 RESERVE_SPACE(16);
3362 WRITE32(write->wr_bytes_written);
3363 WRITE32(write->wr_how_written);
ab4684d1 3364 WRITEMEM(write->wr_verifier.data, NFS4_VERIFIER_SIZE);
1da177e4
LT
3365 ADJUST_ARGS();
3366 }
695e12f8 3367 return nfserr;
1da177e4
LT
3368}
3369
57266a6e
BF
3370static const u32 nfs4_minimal_spo_must_enforce[2] = {
3371 [1] = 1 << (OP_BIND_CONN_TO_SESSION - 32) |
3372 1 << (OP_EXCHANGE_ID - 32) |
3373 1 << (OP_CREATE_SESSION - 32) |
3374 1 << (OP_DESTROY_SESSION - 32) |
3375 1 << (OP_DESTROY_CLIENTID - 32)
3376};
3377
2db134eb 3378static __be32
57b7b43b 3379nfsd4_encode_exchange_id(struct nfsd4_compoundres *resp, __be32 nfserr,
2db134eb
AA
3380 struct nfsd4_exchange_id *exid)
3381{
bc749ca4 3382 __be32 *p;
0733d213
AA
3383 char *major_id;
3384 char *server_scope;
3385 int major_id_sz;
3386 int server_scope_sz;
3387 uint64_t minor_id = 0;
3388
3389 if (nfserr)
3390 return nfserr;
3391
3392 major_id = utsname()->nodename;
3393 major_id_sz = strlen(major_id);
3394 server_scope = utsname()->nodename;
3395 server_scope_sz = strlen(server_scope);
3396
3397 RESERVE_SPACE(
3398 8 /* eir_clientid */ +
3399 4 /* eir_sequenceid */ +
3400 4 /* eir_flags */ +
a8bb84bc 3401 4 /* spr_how */);
0733d213
AA
3402
3403 WRITEMEM(&exid->clientid, 8);
3404 WRITE32(exid->seqid);
3405 WRITE32(exid->flags);
3406
0733d213 3407 WRITE32(exid->spa_how);
a8bb84bc
KM
3408 ADJUST_ARGS();
3409
57266a6e
BF
3410 switch (exid->spa_how) {
3411 case SP4_NONE:
3412 break;
3413 case SP4_MACH_CRED:
a8bb84bc
KM
3414 /* spo_must_enforce, spo_must_allow */
3415 RESERVE_SPACE(16);
3416
57266a6e
BF
3417 /* spo_must_enforce bitmap: */
3418 WRITE32(2);
3419 WRITE32(nfs4_minimal_spo_must_enforce[0]);
3420 WRITE32(nfs4_minimal_spo_must_enforce[1]);
3421 /* empty spo_must_allow bitmap: */
3422 WRITE32(0);
a8bb84bc
KM
3423
3424 ADJUST_ARGS();
57266a6e
BF
3425 break;
3426 default:
3427 WARN_ON_ONCE(1);
3428 }
0733d213 3429
a8bb84bc
KM
3430 RESERVE_SPACE(
3431 8 /* so_minor_id */ +
3432 4 /* so_major_id.len */ +
3433 (XDR_QUADLEN(major_id_sz) * 4) +
3434 4 /* eir_server_scope.len */ +
3435 (XDR_QUADLEN(server_scope_sz) * 4) +
3436 4 /* eir_server_impl_id.count (0) */);
3437
0733d213
AA
3438 /* The server_owner struct */
3439 WRITE64(minor_id); /* Minor id */
3440 /* major id */
3441 WRITE32(major_id_sz);
3442 WRITEMEM(major_id, major_id_sz);
3443
3444 /* Server scope */
3445 WRITE32(server_scope_sz);
3446 WRITEMEM(server_scope, server_scope_sz);
3447
3448 /* Implementation id */
3449 WRITE32(0); /* zero length nfs_impl_id4 array */
3450 ADJUST_ARGS();
3451 return 0;
2db134eb
AA
3452}
3453
3454static __be32
57b7b43b 3455nfsd4_encode_create_session(struct nfsd4_compoundres *resp, __be32 nfserr,
2db134eb
AA
3456 struct nfsd4_create_session *sess)
3457{
bc749ca4 3458 __be32 *p;
ec6b5d7b
AA
3459
3460 if (nfserr)
3461 return nfserr;
3462
3463 RESERVE_SPACE(24);
3464 WRITEMEM(sess->sessionid.data, NFS4_MAX_SESSIONID_LEN);
3465 WRITE32(sess->seqid);
3466 WRITE32(sess->flags);
3467 ADJUST_ARGS();
3468
3469 RESERVE_SPACE(28);
3470 WRITE32(0); /* headerpadsz */
3471 WRITE32(sess->fore_channel.maxreq_sz);
3472 WRITE32(sess->fore_channel.maxresp_sz);
3473 WRITE32(sess->fore_channel.maxresp_cached);
3474 WRITE32(sess->fore_channel.maxops);
3475 WRITE32(sess->fore_channel.maxreqs);
3476 WRITE32(sess->fore_channel.nr_rdma_attrs);
3477 ADJUST_ARGS();
3478
3479 if (sess->fore_channel.nr_rdma_attrs) {
3480 RESERVE_SPACE(4);
3481 WRITE32(sess->fore_channel.rdma_attrs);
3482 ADJUST_ARGS();
3483 }
3484
3485 RESERVE_SPACE(28);
3486 WRITE32(0); /* headerpadsz */
3487 WRITE32(sess->back_channel.maxreq_sz);
3488 WRITE32(sess->back_channel.maxresp_sz);
3489 WRITE32(sess->back_channel.maxresp_cached);
3490 WRITE32(sess->back_channel.maxops);
3491 WRITE32(sess->back_channel.maxreqs);
3492 WRITE32(sess->back_channel.nr_rdma_attrs);
3493 ADJUST_ARGS();
3494
3495 if (sess->back_channel.nr_rdma_attrs) {
3496 RESERVE_SPACE(4);
3497 WRITE32(sess->back_channel.rdma_attrs);
3498 ADJUST_ARGS();
3499 }
3500 return 0;
2db134eb
AA
3501}
3502
3503static __be32
57b7b43b 3504nfsd4_encode_destroy_session(struct nfsd4_compoundres *resp, __be32 nfserr,
2db134eb
AA
3505 struct nfsd4_destroy_session *destroy_session)
3506{
2db134eb
AA
3507 return nfserr;
3508}
3509
e1ca12df 3510static __be32
d1829b38 3511nfsd4_encode_free_stateid(struct nfsd4_compoundres *resp, __be32 nfserr,
e1ca12df
BS
3512 struct nfsd4_free_stateid *free_stateid)
3513{
3514 __be32 *p;
3515
3516 if (nfserr)
3517 return nfserr;
3518
3519 RESERVE_SPACE(4);
d1829b38 3520 *p++ = nfserr;
e1ca12df
BS
3521 ADJUST_ARGS();
3522 return nfserr;
3523}
3524
c47d832b 3525static __be32
57b7b43b 3526nfsd4_encode_sequence(struct nfsd4_compoundres *resp, __be32 nfserr,
2db134eb
AA
3527 struct nfsd4_sequence *seq)
3528{
bc749ca4 3529 __be32 *p;
b85d4c01
BH
3530
3531 if (nfserr)
3532 return nfserr;
3533
3534 RESERVE_SPACE(NFS4_MAX_SESSIONID_LEN + 20);
3535 WRITEMEM(seq->sessionid.data, NFS4_MAX_SESSIONID_LEN);
3536 WRITE32(seq->seqid);
3537 WRITE32(seq->slotid);
b7d7ca35
BF
3538 /* Note slotid's are numbered from zero: */
3539 WRITE32(seq->maxslots - 1); /* sr_highest_slotid */
3540 WRITE32(seq->maxslots - 1); /* sr_target_highest_slotid */
0d7bb719 3541 WRITE32(seq->status_flags);
b85d4c01
BH
3542
3543 ADJUST_ARGS();
557ce264 3544 resp->cstate.datap = p; /* DRC cache data pointer */
b85d4c01 3545 return 0;
2db134eb
AA
3546}
3547
2355c596 3548static __be32
57b7b43b 3549nfsd4_encode_test_stateid(struct nfsd4_compoundres *resp, __be32 nfserr,
17456804
BS
3550 struct nfsd4_test_stateid *test_stateid)
3551{
03cfb420 3552 struct nfsd4_test_stateid_id *stateid, *next;
17456804 3553 __be32 *p;
17456804 3554
03cfb420 3555 RESERVE_SPACE(4 + (4 * test_stateid->ts_num_ids));
17456804 3556 *p++ = htonl(test_stateid->ts_num_ids);
17456804 3557
03cfb420 3558 list_for_each_entry_safe(stateid, next, &test_stateid->ts_stateid_list, ts_id_list) {
02f5fde5 3559 *p++ = stateid->ts_id_status;
17456804 3560 }
17456804 3561
03cfb420 3562 ADJUST_ARGS();
17456804
BS
3563 return nfserr;
3564}
3565
695e12f8
BH
3566static __be32
3567nfsd4_encode_noop(struct nfsd4_compoundres *resp, __be32 nfserr, void *p)
3568{
3569 return nfserr;
3570}
3571
3572typedef __be32(* nfsd4_enc)(struct nfsd4_compoundres *, __be32, void *);
3573
2db134eb
AA
3574/*
3575 * Note: nfsd4_enc_ops vector is shared for v4.0 and v4.1
3576 * since we don't need to filter out obsolete ops as this is
3577 * done in the decoding phase.
3578 */
695e12f8 3579static nfsd4_enc nfsd4_enc_ops[] = {
ad1060c8
BF
3580 [OP_ACCESS] = (nfsd4_enc)nfsd4_encode_access,
3581 [OP_CLOSE] = (nfsd4_enc)nfsd4_encode_close,
3582 [OP_COMMIT] = (nfsd4_enc)nfsd4_encode_commit,
3583 [OP_CREATE] = (nfsd4_enc)nfsd4_encode_create,
3584 [OP_DELEGPURGE] = (nfsd4_enc)nfsd4_encode_noop,
3585 [OP_DELEGRETURN] = (nfsd4_enc)nfsd4_encode_noop,
3586 [OP_GETATTR] = (nfsd4_enc)nfsd4_encode_getattr,
3587 [OP_GETFH] = (nfsd4_enc)nfsd4_encode_getfh,
3588 [OP_LINK] = (nfsd4_enc)nfsd4_encode_link,
3589 [OP_LOCK] = (nfsd4_enc)nfsd4_encode_lock,
3590 [OP_LOCKT] = (nfsd4_enc)nfsd4_encode_lockt,
3591 [OP_LOCKU] = (nfsd4_enc)nfsd4_encode_locku,
3592 [OP_LOOKUP] = (nfsd4_enc)nfsd4_encode_noop,
3593 [OP_LOOKUPP] = (nfsd4_enc)nfsd4_encode_noop,
3594 [OP_NVERIFY] = (nfsd4_enc)nfsd4_encode_noop,
3595 [OP_OPEN] = (nfsd4_enc)nfsd4_encode_open,
84f09f46 3596 [OP_OPENATTR] = (nfsd4_enc)nfsd4_encode_noop,
ad1060c8
BF
3597 [OP_OPEN_CONFIRM] = (nfsd4_enc)nfsd4_encode_open_confirm,
3598 [OP_OPEN_DOWNGRADE] = (nfsd4_enc)nfsd4_encode_open_downgrade,
3599 [OP_PUTFH] = (nfsd4_enc)nfsd4_encode_noop,
3600 [OP_PUTPUBFH] = (nfsd4_enc)nfsd4_encode_noop,
3601 [OP_PUTROOTFH] = (nfsd4_enc)nfsd4_encode_noop,
3602 [OP_READ] = (nfsd4_enc)nfsd4_encode_read,
3603 [OP_READDIR] = (nfsd4_enc)nfsd4_encode_readdir,
3604 [OP_READLINK] = (nfsd4_enc)nfsd4_encode_readlink,
3605 [OP_REMOVE] = (nfsd4_enc)nfsd4_encode_remove,
3606 [OP_RENAME] = (nfsd4_enc)nfsd4_encode_rename,
3607 [OP_RENEW] = (nfsd4_enc)nfsd4_encode_noop,
3608 [OP_RESTOREFH] = (nfsd4_enc)nfsd4_encode_noop,
3609 [OP_SAVEFH] = (nfsd4_enc)nfsd4_encode_noop,
3610 [OP_SECINFO] = (nfsd4_enc)nfsd4_encode_secinfo,
3611 [OP_SETATTR] = (nfsd4_enc)nfsd4_encode_setattr,
3612 [OP_SETCLIENTID] = (nfsd4_enc)nfsd4_encode_setclientid,
3613 [OP_SETCLIENTID_CONFIRM] = (nfsd4_enc)nfsd4_encode_noop,
3614 [OP_VERIFY] = (nfsd4_enc)nfsd4_encode_noop,
3615 [OP_WRITE] = (nfsd4_enc)nfsd4_encode_write,
3616 [OP_RELEASE_LOCKOWNER] = (nfsd4_enc)nfsd4_encode_noop,
2db134eb
AA
3617
3618 /* NFSv4.1 operations */
3619 [OP_BACKCHANNEL_CTL] = (nfsd4_enc)nfsd4_encode_noop,
1d1bc8f2 3620 [OP_BIND_CONN_TO_SESSION] = (nfsd4_enc)nfsd4_encode_bind_conn_to_session,
2db134eb
AA
3621 [OP_EXCHANGE_ID] = (nfsd4_enc)nfsd4_encode_exchange_id,
3622 [OP_CREATE_SESSION] = (nfsd4_enc)nfsd4_encode_create_session,
3623 [OP_DESTROY_SESSION] = (nfsd4_enc)nfsd4_encode_destroy_session,
e1ca12df 3624 [OP_FREE_STATEID] = (nfsd4_enc)nfsd4_encode_free_stateid,
2db134eb
AA
3625 [OP_GET_DIR_DELEGATION] = (nfsd4_enc)nfsd4_encode_noop,
3626 [OP_GETDEVICEINFO] = (nfsd4_enc)nfsd4_encode_noop,
3627 [OP_GETDEVICELIST] = (nfsd4_enc)nfsd4_encode_noop,
3628 [OP_LAYOUTCOMMIT] = (nfsd4_enc)nfsd4_encode_noop,
3629 [OP_LAYOUTGET] = (nfsd4_enc)nfsd4_encode_noop,
3630 [OP_LAYOUTRETURN] = (nfsd4_enc)nfsd4_encode_noop,
22b6dee8 3631 [OP_SECINFO_NO_NAME] = (nfsd4_enc)nfsd4_encode_secinfo_no_name,
2db134eb
AA
3632 [OP_SEQUENCE] = (nfsd4_enc)nfsd4_encode_sequence,
3633 [OP_SET_SSV] = (nfsd4_enc)nfsd4_encode_noop,
17456804 3634 [OP_TEST_STATEID] = (nfsd4_enc)nfsd4_encode_test_stateid,
2db134eb
AA
3635 [OP_WANT_DELEGATION] = (nfsd4_enc)nfsd4_encode_noop,
3636 [OP_DESTROY_CLIENTID] = (nfsd4_enc)nfsd4_encode_noop,
3637 [OP_RECLAIM_COMPLETE] = (nfsd4_enc)nfsd4_encode_noop,
695e12f8
BH
3638};
3639
496c262c
AA
3640/*
3641 * Calculate the total amount of memory that the compound response has taken
58e7b33a 3642 * after encoding the current operation with pad.
496c262c 3643 *
58e7b33a
MJ
3644 * pad: if operation is non-idempotent, pad was calculate by op_rsize_bop()
3645 * which was specified at nfsd4_operation, else pad is zero.
496c262c 3646 *
58e7b33a 3647 * Compare this length to the session se_fmaxresp_sz and se_fmaxresp_cached.
496c262c
AA
3648 *
3649 * Our se_fmaxresp_cached will always be a multiple of PAGE_SIZE, and so
3650 * will be at least a page and will therefore hold the xdr_buf head.
3651 */
57b7b43b 3652__be32 nfsd4_check_resp_size(struct nfsd4_compoundres *resp, u32 pad)
496c262c 3653{
496c262c 3654 struct xdr_buf *xb = &resp->rqstp->rq_res;
496c262c
AA
3655 struct nfsd4_session *session = NULL;
3656 struct nfsd4_slot *slot = resp->cstate.slot;
58e7b33a 3657 u32 length, tlen = 0;
496c262c
AA
3658
3659 if (!nfsd4_has_session(&resp->cstate))
58e7b33a 3660 return 0;
496c262c
AA
3661
3662 session = resp->cstate.session;
58e7b33a
MJ
3663 if (session == NULL)
3664 return 0;
496c262c
AA
3665
3666 if (xb->page_len == 0) {
3667 length = (char *)resp->p - (char *)xb->head[0].iov_base + pad;
3668 } else {
3669 if (xb->tail[0].iov_base && xb->tail[0].iov_len > 0)
3670 tlen = (char *)resp->p - (char *)xb->tail[0].iov_base;
3671
3672 length = xb->head[0].iov_len + xb->page_len + tlen + pad;
3673 }
3674 dprintk("%s length %u, xb->page_len %u tlen %u pad %u\n", __func__,
3675 length, xb->page_len, tlen, pad);
3676
58e7b33a
MJ
3677 if (length > session->se_fchannel.maxresp_sz)
3678 return nfserr_rep_too_big;
3679
73e79482 3680 if ((slot->sl_flags & NFSD4_SLOT_CACHETHIS) &&
58e7b33a 3681 length > session->se_fchannel.maxresp_cached)
496c262c 3682 return nfserr_rep_too_big_to_cache;
58e7b33a
MJ
3683
3684 return 0;
496c262c
AA
3685}
3686
1da177e4
LT
3687void
3688nfsd4_encode_operation(struct nfsd4_compoundres *resp, struct nfsd4_op *op)
3689{
9411b1d4 3690 struct nfs4_stateowner *so = resp->cstate.replay_owner;
2ebbc012 3691 __be32 *statp;
bc749ca4 3692 __be32 *p;
1da177e4
LT
3693
3694 RESERVE_SPACE(8);
3695 WRITE32(op->opnum);
3696 statp = p++; /* to be backfilled at the end */
3697 ADJUST_ARGS();
3698
695e12f8
BH
3699 if (op->opnum == OP_ILLEGAL)
3700 goto status;
3701 BUG_ON(op->opnum < 0 || op->opnum >= ARRAY_SIZE(nfsd4_enc_ops) ||
3702 !nfsd4_enc_ops[op->opnum]);
3703 op->status = nfsd4_enc_ops[op->opnum](resp, op->status, &op->u);
496c262c 3704 /* nfsd4_check_drc_limit guarantees enough room for error status */
58e7b33a
MJ
3705 if (!op->status)
3706 op->status = nfsd4_check_resp_size(resp, 0);
9411b1d4
BF
3707 if (so) {
3708 so->so_replay.rp_status = op->status;
3709 so->so_replay.rp_buflen = (char *)resp->p - (char *)(statp+1);
3710 memcpy(so->so_replay.rp_buf, statp+1, so->so_replay.rp_buflen);
3711 }
695e12f8 3712status:
1da177e4
LT
3713 /*
3714 * Note: We write the status directly, instead of using WRITE32(),
3715 * since it is already in network byte order.
3716 */
3717 *statp = op->status;
3718}
3719
3720/*
3721 * Encode the reply stored in the stateowner reply cache
3722 *
3723 * XDR note: do not encode rp->rp_buflen: the buffer contains the
3724 * previously sent already encoded operation.
3725 *
3726 * called with nfs4_lock_state() held
3727 */
3728void
3729nfsd4_encode_replay(struct nfsd4_compoundres *resp, struct nfsd4_op *op)
3730{
bc749ca4 3731 __be32 *p;
1da177e4
LT
3732 struct nfs4_replay *rp = op->replay;
3733
3734 BUG_ON(!rp);
3735
3736 RESERVE_SPACE(8);
3737 WRITE32(op->opnum);
3738 *p++ = rp->rp_status; /* already xdr'ed */
3739 ADJUST_ARGS();
3740
3741 RESERVE_SPACE(rp->rp_buflen);
3742 WRITEMEM(rp->rp_buf, rp->rp_buflen);
3743 ADJUST_ARGS();
3744}
3745
1da177e4 3746int
2ebbc012 3747nfs4svc_encode_voidres(struct svc_rqst *rqstp, __be32 *p, void *dummy)
1da177e4
LT
3748{
3749 return xdr_ressize_check(rqstp, p);
3750}
3751
3e98abff 3752int nfsd4_release_compoundargs(void *rq, __be32 *p, void *resp)
1da177e4 3753{
3e98abff
BF
3754 struct svc_rqst *rqstp = rq;
3755 struct nfsd4_compoundargs *args = rqstp->rq_argp;
3756
1da177e4
LT
3757 if (args->ops != args->iops) {
3758 kfree(args->ops);
3759 args->ops = args->iops;
3760 }
f99d49ad
JJ
3761 kfree(args->tmpp);
3762 args->tmpp = NULL;
1da177e4
LT
3763 while (args->to_free) {
3764 struct tmpbuf *tb = args->to_free;
3765 args->to_free = tb->next;
3766 tb->release(tb->buf);
3767 kfree(tb);
3768 }
3e98abff 3769 return 1;
1da177e4
LT
3770}
3771
3772int
2ebbc012 3773nfs4svc_decode_compoundargs(struct svc_rqst *rqstp, __be32 *p, struct nfsd4_compoundargs *args)
1da177e4 3774{
1da177e4
LT
3775 args->p = p;
3776 args->end = rqstp->rq_arg.head[0].iov_base + rqstp->rq_arg.head[0].iov_len;
3777 args->pagelist = rqstp->rq_arg.pages;
3778 args->pagelen = rqstp->rq_arg.page_len;
3779 args->tmpp = NULL;
3780 args->to_free = NULL;
3781 args->ops = args->iops;
3782 args->rqstp = rqstp;
3783
3e98abff 3784 return !nfsd4_decode_compound(args);
1da177e4
LT
3785}
3786
3787int
2ebbc012 3788nfs4svc_encode_compoundres(struct svc_rqst *rqstp, __be32 *p, struct nfsd4_compoundres *resp)
1da177e4
LT
3789{
3790 /*
3791 * All that remains is to write the tag and operation count...
3792 */
557ce264 3793 struct nfsd4_compound_state *cs = &resp->cstate;
1da177e4
LT
3794 struct kvec *iov;
3795 p = resp->tagp;
3796 *p++ = htonl(resp->taglen);
3797 memcpy(p, resp->tag, resp->taglen);
3798 p += XDR_QUADLEN(resp->taglen);
3799 *p++ = htonl(resp->opcnt);
3800
3801 if (rqstp->rq_res.page_len)
3802 iov = &rqstp->rq_res.tail[0];
3803 else
3804 iov = &rqstp->rq_res.head[0];
3805 iov->iov_len = ((char*)resp->p) - (char*)iov->iov_base;
3806 BUG_ON(iov->iov_len > PAGE_SIZE);
26c0c75e 3807 if (nfsd4_has_session(cs)) {
f0f51f5c
BF
3808 struct nfsd_net *nn = net_generic(SVC_NET(rqstp), nfsd_net_id);
3809 struct nfs4_client *clp = cs->session->se_client;
26c0c75e
BF
3810 if (cs->status != nfserr_replay_cache) {
3811 nfsd4_store_cache_entry(resp);
73e79482 3812 cs->slot->sl_flags &= ~NFSD4_SLOT_INUSE;
26c0c75e 3813 }
d7682988 3814 /* Renew the clientid on success and on replay */
f0f51f5c 3815 spin_lock(&nn->client_lock);
221a6876 3816 nfsd4_put_session(cs->session);
f0f51f5c
BF
3817 spin_unlock(&nn->client_lock);
3818 put_client_renew(clp);
da3846a2 3819 }
1da177e4
LT
3820 return 1;
3821}
3822
3823/*
3824 * Local variables:
3825 * c-basic-offset: 8
3826 * End:
3827 */
This page took 0.793727 seconds and 5 git commands to generate.