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