blob: 5c4eb18c655a61c2ce4a86bc297720c05d16ca7d [file] [log] [blame]
b.liue9582032025-04-17 19:18:16 +08001/*
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
36#include <linux/file.h>
37#include <linux/slab.h>
38#include <linux/namei.h>
39#include <linux/statfs.h>
40#include <linux/utsname.h>
41#include <linux/pagemap.h>
42#include <linux/sunrpc/svcauth_gss.h>
43
44#include "idmap.h"
45#include "acl.h"
46#include "xdr4.h"
47#include "vfs.h"
48#include "state.h"
49#include "cache.h"
50#include "netns.h"
51#include "pnfs.h"
52#include "filecache.h"
53
54#ifdef CONFIG_NFSD_V4_SECURITY_LABEL
55#include <linux/security.h>
56#endif
57
58
59#define NFSDDBG_FACILITY NFSDDBG_XDR
60
61const u32 nfsd_suppattrs[3][3] = {
62 {NFSD4_SUPPORTED_ATTRS_WORD0,
63 NFSD4_SUPPORTED_ATTRS_WORD1,
64 NFSD4_SUPPORTED_ATTRS_WORD2},
65
66 {NFSD4_1_SUPPORTED_ATTRS_WORD0,
67 NFSD4_1_SUPPORTED_ATTRS_WORD1,
68 NFSD4_1_SUPPORTED_ATTRS_WORD2},
69
70 {NFSD4_1_SUPPORTED_ATTRS_WORD0,
71 NFSD4_1_SUPPORTED_ATTRS_WORD1,
72 NFSD4_2_SUPPORTED_ATTRS_WORD2},
73};
74
75/*
76 * As per referral draft, the fsid for a referral MUST be different from the fsid of the containing
77 * directory in order to indicate to the client that a filesystem boundary is present
78 * We use a fixed fsid for a referral
79 */
80#define NFS4_REFERRAL_FSID_MAJOR 0x8000000ULL
81#define NFS4_REFERRAL_FSID_MINOR 0x8000000ULL
82
83static __be32
84check_filename(char *str, int len)
85{
86 int i;
87
88 if (len == 0)
89 return nfserr_inval;
90 if (isdotent(str, len))
91 return nfserr_badname;
92 for (i = 0; i < len; i++)
93 if (str[i] == '/')
94 return nfserr_badname;
95 return 0;
96}
97
98#define DECODE_HEAD \
99 __be32 *p; \
100 __be32 status
101#define DECODE_TAIL \
102 status = 0; \
103out: \
104 return status; \
105xdr_error: \
106 dprintk("NFSD: xdr error (%s:%d)\n", \
107 __FILE__, __LINE__); \
108 status = nfserr_bad_xdr; \
109 goto out
110
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)) { \
119 dprintk("NFSD: xdr error (%s:%d)\n", \
120 __FILE__, __LINE__); \
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))) { \
136 dprintk("NFSD: xdr error (%s:%d)\n", \
137 __FILE__, __LINE__); \
138 goto xdr_error; \
139 } \
140} while (0)
141
142static void next_decode_page(struct nfsd4_compoundargs *argp)
143{
144 argp->p = page_address(argp->pagelist[0]);
145 argp->pagelist++;
146 if (argp->pagelen < PAGE_SIZE) {
147 argp->end = argp->p + XDR_QUADLEN(argp->pagelen);
148 argp->pagelen = 0;
149 } else {
150 argp->end = argp->p + (PAGE_SIZE>>2);
151 argp->pagelen -= PAGE_SIZE;
152 }
153}
154
155static __be32 *read_buf(struct nfsd4_compoundargs *argp, u32 nbytes)
156{
157 /* We want more bytes than seem to be available.
158 * Maybe we need a new page, maybe we have just run out
159 */
160 unsigned int avail = (char *)argp->end - (char *)argp->p;
161 __be32 *p;
162
163 if (argp->pagelen == 0) {
164 struct kvec *vec = &argp->rqstp->rq_arg.tail[0];
165
166 if (!argp->tail) {
167 argp->tail = true;
168 avail = vec->iov_len;
169 argp->p = vec->iov_base;
170 argp->end = vec->iov_base + avail;
171 }
172
173 if (avail < nbytes)
174 return NULL;
175
176 p = argp->p;
177 argp->p += XDR_QUADLEN(nbytes);
178 return p;
179 }
180
181 if (avail + argp->pagelen < nbytes)
182 return NULL;
183 if (avail + PAGE_SIZE < nbytes) /* need more than a page !! */
184 return NULL;
185 /* ok, we can do it with the current plus the next page */
186 if (nbytes <= sizeof(argp->tmp))
187 p = argp->tmp;
188 else {
189 kfree(argp->tmpp);
190 p = argp->tmpp = kmalloc(nbytes, GFP_KERNEL);
191 if (!p)
192 return NULL;
193
194 }
195 /*
196 * The following memcpy is safe because read_buf is always
197 * called with nbytes > avail, and the two cases above both
198 * guarantee p points to at least nbytes bytes.
199 */
200 memcpy(p, argp->p, avail);
201 next_decode_page(argp);
202 memcpy(((char*)p)+avail, argp->p, (nbytes - avail));
203 argp->p += XDR_QUADLEN(nbytes - avail);
204 return p;
205}
206
207static unsigned int compoundargs_bytes_left(struct nfsd4_compoundargs *argp)
208{
209 unsigned int this = (char *)argp->end - (char *)argp->p;
210
211 return this + argp->pagelen;
212}
213
214static int zero_clientid(clientid_t *clid)
215{
216 return (clid->cl_boot == 0) && (clid->cl_id == 0);
217}
218
219/**
220 * svcxdr_tmpalloc - allocate memory to be freed after compound processing
221 * @argp: NFSv4 compound argument structure
222 * @len: length of buffer to allocate
223 *
224 * Allocates a buffer of size @len to be freed when processing the compound
225 * operation described in @argp finishes.
226 */
227static void *
228svcxdr_tmpalloc(struct nfsd4_compoundargs *argp, u32 len)
229{
230 struct svcxdr_tmpbuf *tb;
231
232 tb = kmalloc(sizeof(*tb) + len, GFP_KERNEL);
233 if (!tb)
234 return NULL;
235 tb->next = argp->to_free;
236 argp->to_free = tb;
237 return tb->buf;
238}
239
240/*
241 * For xdr strings that need to be passed to other kernel api's
242 * as null-terminated strings.
243 *
244 * Note null-terminating in place usually isn't safe since the
245 * buffer might end on a page boundary.
246 */
247static char *
248svcxdr_dupstr(struct nfsd4_compoundargs *argp, void *buf, u32 len)
249{
250 char *p = svcxdr_tmpalloc(argp, len + 1);
251
252 if (!p)
253 return NULL;
254 memcpy(p, buf, len);
255 p[len] = '\0';
256 return p;
257}
258
259/**
260 * savemem - duplicate a chunk of memory for later processing
261 * @argp: NFSv4 compound argument structure to be freed with
262 * @p: pointer to be duplicated
263 * @nbytes: length to be duplicated
264 *
265 * Returns a pointer to a copy of @nbytes bytes of memory at @p
266 * that are preserved until processing of the NFSv4 compound
267 * operation described by @argp finishes.
268 */
269static char *savemem(struct nfsd4_compoundargs *argp, __be32 *p, int nbytes)
270{
271 void *ret;
272
273 ret = svcxdr_tmpalloc(argp, nbytes);
274 if (!ret)
275 return NULL;
276 memcpy(ret, p, nbytes);
277 return ret;
278}
279
280static __be32
281nfsd4_decode_time(struct nfsd4_compoundargs *argp, struct timespec64 *tv)
282{
283 DECODE_HEAD;
284
285 READ_BUF(12);
286 p = xdr_decode_hyper(p, &tv->tv_sec);
287 tv->tv_nsec = be32_to_cpup(p++);
288 if (tv->tv_nsec >= (u32)1000000000)
289 return nfserr_inval;
290
291 DECODE_TAIL;
292}
293
294static __be32
295nfsd4_decode_bitmap(struct nfsd4_compoundargs *argp, u32 *bmval)
296{
297 u32 bmlen;
298 DECODE_HEAD;
299
300 bmval[0] = 0;
301 bmval[1] = 0;
302 bmval[2] = 0;
303
304 READ_BUF(4);
305 bmlen = be32_to_cpup(p++);
306 if (bmlen > 1000)
307 goto xdr_error;
308
309 READ_BUF(bmlen << 2);
310 if (bmlen > 0)
311 bmval[0] = be32_to_cpup(p++);
312 if (bmlen > 1)
313 bmval[1] = be32_to_cpup(p++);
314 if (bmlen > 2)
315 bmval[2] = be32_to_cpup(p++);
316
317 DECODE_TAIL;
318}
319
320static __be32
321nfsd4_decode_fattr(struct nfsd4_compoundargs *argp, u32 *bmval,
322 struct iattr *iattr, struct nfs4_acl **acl,
323 struct xdr_netobj *label, int *umask)
324{
325 int expected_len, len = 0;
326 u32 dummy32;
327 char *buf;
328
329 DECODE_HEAD;
330 iattr->ia_valid = 0;
331 if ((status = nfsd4_decode_bitmap(argp, bmval)))
332 return status;
333
334 if (bmval[0] & ~NFSD_WRITEABLE_ATTRS_WORD0
335 || bmval[1] & ~NFSD_WRITEABLE_ATTRS_WORD1
336 || bmval[2] & ~NFSD_WRITEABLE_ATTRS_WORD2) {
337 if (nfsd_attrs_supported(argp->minorversion, bmval))
338 return nfserr_inval;
339 return nfserr_attrnotsupp;
340 }
341
342 READ_BUF(4);
343 expected_len = be32_to_cpup(p++);
344
345 if (bmval[0] & FATTR4_WORD0_SIZE) {
346 READ_BUF(8);
347 len += 8;
348 p = xdr_decode_hyper(p, &iattr->ia_size);
349 iattr->ia_valid |= ATTR_SIZE;
350 }
351 if (bmval[0] & FATTR4_WORD0_ACL) {
352 u32 nace;
353 struct nfs4_ace *ace;
354
355 READ_BUF(4); len += 4;
356 nace = be32_to_cpup(p++);
357
358 if (nace > compoundargs_bytes_left(argp)/20)
359 /*
360 * Even with 4-byte names there wouldn't be
361 * space for that many aces; something fishy is
362 * going on:
363 */
364 return nfserr_fbig;
365
366 *acl = svcxdr_tmpalloc(argp, nfs4_acl_bytes(nace));
367 if (*acl == NULL)
368 return nfserr_jukebox;
369
370 (*acl)->naces = nace;
371 for (ace = (*acl)->aces; ace < (*acl)->aces + nace; ace++) {
372 READ_BUF(16); len += 16;
373 ace->type = be32_to_cpup(p++);
374 ace->flag = be32_to_cpup(p++);
375 ace->access_mask = be32_to_cpup(p++);
376 dummy32 = be32_to_cpup(p++);
377 READ_BUF(dummy32);
378 len += XDR_QUADLEN(dummy32) << 2;
379 READMEM(buf, dummy32);
380 ace->whotype = nfs4_acl_get_whotype(buf, dummy32);
381 status = nfs_ok;
382 if (ace->whotype != NFS4_ACL_WHO_NAMED)
383 ;
384 else if (ace->flag & NFS4_ACE_IDENTIFIER_GROUP)
385 status = nfsd_map_name_to_gid(argp->rqstp,
386 buf, dummy32, &ace->who_gid);
387 else
388 status = nfsd_map_name_to_uid(argp->rqstp,
389 buf, dummy32, &ace->who_uid);
390 if (status)
391 return status;
392 }
393 } else
394 *acl = NULL;
395 if (bmval[1] & FATTR4_WORD1_MODE) {
396 READ_BUF(4);
397 len += 4;
398 iattr->ia_mode = be32_to_cpup(p++);
399 iattr->ia_mode &= (S_IFMT | S_IALLUGO);
400 iattr->ia_valid |= ATTR_MODE;
401 }
402 if (bmval[1] & FATTR4_WORD1_OWNER) {
403 READ_BUF(4);
404 len += 4;
405 dummy32 = be32_to_cpup(p++);
406 READ_BUF(dummy32);
407 len += (XDR_QUADLEN(dummy32) << 2);
408 READMEM(buf, dummy32);
409 if ((status = nfsd_map_name_to_uid(argp->rqstp, buf, dummy32, &iattr->ia_uid)))
410 return status;
411 iattr->ia_valid |= ATTR_UID;
412 }
413 if (bmval[1] & FATTR4_WORD1_OWNER_GROUP) {
414 READ_BUF(4);
415 len += 4;
416 dummy32 = be32_to_cpup(p++);
417 READ_BUF(dummy32);
418 len += (XDR_QUADLEN(dummy32) << 2);
419 READMEM(buf, dummy32);
420 if ((status = nfsd_map_name_to_gid(argp->rqstp, buf, dummy32, &iattr->ia_gid)))
421 return status;
422 iattr->ia_valid |= ATTR_GID;
423 }
424 if (bmval[1] & FATTR4_WORD1_TIME_ACCESS_SET) {
425 READ_BUF(4);
426 len += 4;
427 dummy32 = be32_to_cpup(p++);
428 switch (dummy32) {
429 case NFS4_SET_TO_CLIENT_TIME:
430 len += 12;
431 status = nfsd4_decode_time(argp, &iattr->ia_atime);
432 if (status)
433 return status;
434 iattr->ia_valid |= (ATTR_ATIME | ATTR_ATIME_SET);
435 break;
436 case NFS4_SET_TO_SERVER_TIME:
437 iattr->ia_valid |= ATTR_ATIME;
438 break;
439 default:
440 goto xdr_error;
441 }
442 }
443 if (bmval[1] & FATTR4_WORD1_TIME_MODIFY_SET) {
444 READ_BUF(4);
445 len += 4;
446 dummy32 = be32_to_cpup(p++);
447 switch (dummy32) {
448 case NFS4_SET_TO_CLIENT_TIME:
449 len += 12;
450 status = nfsd4_decode_time(argp, &iattr->ia_mtime);
451 if (status)
452 return status;
453 iattr->ia_valid |= (ATTR_MTIME | ATTR_MTIME_SET);
454 break;
455 case NFS4_SET_TO_SERVER_TIME:
456 iattr->ia_valid |= ATTR_MTIME;
457 break;
458 default:
459 goto xdr_error;
460 }
461 }
462
463 label->len = 0;
464 if (IS_ENABLED(CONFIG_NFSD_V4_SECURITY_LABEL) &&
465 bmval[2] & FATTR4_WORD2_SECURITY_LABEL) {
466 READ_BUF(4);
467 len += 4;
468 dummy32 = be32_to_cpup(p++); /* lfs: we don't use it */
469 READ_BUF(4);
470 len += 4;
471 dummy32 = be32_to_cpup(p++); /* pi: we don't use it either */
472 READ_BUF(4);
473 len += 4;
474 dummy32 = be32_to_cpup(p++);
475 READ_BUF(dummy32);
476 if (dummy32 > NFS4_MAXLABELLEN)
477 return nfserr_badlabel;
478 len += (XDR_QUADLEN(dummy32) << 2);
479 READMEM(buf, dummy32);
480 label->len = dummy32;
481 label->data = svcxdr_dupstr(argp, buf, dummy32);
482 if (!label->data)
483 return nfserr_jukebox;
484 }
485 if (bmval[2] & FATTR4_WORD2_MODE_UMASK) {
486 if (!umask)
487 goto xdr_error;
488 READ_BUF(8);
489 len += 8;
490 dummy32 = be32_to_cpup(p++);
491 iattr->ia_mode = dummy32 & (S_IFMT | S_IALLUGO);
492 dummy32 = be32_to_cpup(p++);
493 *umask = dummy32 & S_IRWXUGO;
494 iattr->ia_valid |= ATTR_MODE;
495 }
496 if (len != expected_len)
497 goto xdr_error;
498
499 DECODE_TAIL;
500}
501
502static __be32
503nfsd4_decode_stateid(struct nfsd4_compoundargs *argp, stateid_t *sid)
504{
505 DECODE_HEAD;
506
507 READ_BUF(sizeof(stateid_t));
508 sid->si_generation = be32_to_cpup(p++);
509 COPYMEM(&sid->si_opaque, sizeof(stateid_opaque_t));
510
511 DECODE_TAIL;
512}
513
514static __be32
515nfsd4_decode_access(struct nfsd4_compoundargs *argp, struct nfsd4_access *access)
516{
517 DECODE_HEAD;
518
519 READ_BUF(4);
520 access->ac_req_access = be32_to_cpup(p++);
521
522 DECODE_TAIL;
523}
524
525static __be32 nfsd4_decode_cb_sec(struct nfsd4_compoundargs *argp, struct nfsd4_cb_sec *cbs)
526{
527 DECODE_HEAD;
528 struct user_namespace *userns = nfsd_user_namespace(argp->rqstp);
529 u32 dummy, uid, gid;
530 char *machine_name;
531 int i;
532 int nr_secflavs;
533
534 /* callback_sec_params4 */
535 READ_BUF(4);
536 nr_secflavs = be32_to_cpup(p++);
537 if (nr_secflavs)
538 cbs->flavor = (u32)(-1);
539 else
540 /* Is this legal? Be generous, take it to mean AUTH_NONE: */
541 cbs->flavor = 0;
542 for (i = 0; i < nr_secflavs; ++i) {
543 READ_BUF(4);
544 dummy = be32_to_cpup(p++);
545 switch (dummy) {
546 case RPC_AUTH_NULL:
547 /* Nothing to read */
548 if (cbs->flavor == (u32)(-1))
549 cbs->flavor = RPC_AUTH_NULL;
550 break;
551 case RPC_AUTH_UNIX:
552 READ_BUF(8);
553 /* stamp */
554 dummy = be32_to_cpup(p++);
555
556 /* machine name */
557 dummy = be32_to_cpup(p++);
558 READ_BUF(dummy);
559 SAVEMEM(machine_name, dummy);
560
561 /* uid, gid */
562 READ_BUF(8);
563 uid = be32_to_cpup(p++);
564 gid = be32_to_cpup(p++);
565
566 /* more gids */
567 READ_BUF(4);
568 dummy = be32_to_cpup(p++);
569 READ_BUF(dummy * 4);
570 if (cbs->flavor == (u32)(-1)) {
571 kuid_t kuid = make_kuid(userns, uid);
572 kgid_t kgid = make_kgid(userns, gid);
573 if (uid_valid(kuid) && gid_valid(kgid)) {
574 cbs->uid = kuid;
575 cbs->gid = kgid;
576 cbs->flavor = RPC_AUTH_UNIX;
577 } else {
578 dprintk("RPC_AUTH_UNIX with invalid"
579 "uid or gid ignoring!\n");
580 }
581 }
582 break;
583 case RPC_AUTH_GSS:
584 dprintk("RPC_AUTH_GSS callback secflavor "
585 "not supported!\n");
586 READ_BUF(8);
587 /* gcbp_service */
588 dummy = be32_to_cpup(p++);
589 /* gcbp_handle_from_server */
590 dummy = be32_to_cpup(p++);
591 READ_BUF(dummy);
592 p += XDR_QUADLEN(dummy);
593 /* gcbp_handle_from_client */
594 READ_BUF(4);
595 dummy = be32_to_cpup(p++);
596 READ_BUF(dummy);
597 break;
598 default:
599 dprintk("Illegal callback secflavor\n");
600 return nfserr_inval;
601 }
602 }
603 DECODE_TAIL;
604}
605
606static __be32 nfsd4_decode_backchannel_ctl(struct nfsd4_compoundargs *argp, struct nfsd4_backchannel_ctl *bc)
607{
608 DECODE_HEAD;
609
610 READ_BUF(4);
611 bc->bc_cb_program = be32_to_cpup(p++);
612 nfsd4_decode_cb_sec(argp, &bc->bc_cb_sec);
613
614 DECODE_TAIL;
615}
616
617static __be32 nfsd4_decode_bind_conn_to_session(struct nfsd4_compoundargs *argp, struct nfsd4_bind_conn_to_session *bcts)
618{
619 DECODE_HEAD;
620
621 READ_BUF(NFS4_MAX_SESSIONID_LEN + 8);
622 COPYMEM(bcts->sessionid.data, NFS4_MAX_SESSIONID_LEN);
623 bcts->dir = be32_to_cpup(p++);
624 /* XXX: skipping ctsa_use_conn_in_rdma_mode. Perhaps Tom Tucker
625 * could help us figure out we should be using it. */
626 DECODE_TAIL;
627}
628
629static __be32
630nfsd4_decode_close(struct nfsd4_compoundargs *argp, struct nfsd4_close *close)
631{
632 DECODE_HEAD;
633
634 READ_BUF(4);
635 close->cl_seqid = be32_to_cpup(p++);
636 return nfsd4_decode_stateid(argp, &close->cl_stateid);
637
638 DECODE_TAIL;
639}
640
641
642static __be32
643nfsd4_decode_commit(struct nfsd4_compoundargs *argp, struct nfsd4_commit *commit)
644{
645 DECODE_HEAD;
646
647 READ_BUF(12);
648 p = xdr_decode_hyper(p, &commit->co_offset);
649 commit->co_count = be32_to_cpup(p++);
650
651 DECODE_TAIL;
652}
653
654static __be32
655nfsd4_decode_create(struct nfsd4_compoundargs *argp, struct nfsd4_create *create)
656{
657 DECODE_HEAD;
658
659 READ_BUF(4);
660 create->cr_type = be32_to_cpup(p++);
661 switch (create->cr_type) {
662 case NF4LNK:
663 READ_BUF(4);
664 create->cr_datalen = be32_to_cpup(p++);
665 READ_BUF(create->cr_datalen);
666 create->cr_data = svcxdr_dupstr(argp, p, create->cr_datalen);
667 if (!create->cr_data)
668 return nfserr_jukebox;
669 break;
670 case NF4BLK:
671 case NF4CHR:
672 READ_BUF(8);
673 create->cr_specdata1 = be32_to_cpup(p++);
674 create->cr_specdata2 = be32_to_cpup(p++);
675 break;
676 case NF4SOCK:
677 case NF4FIFO:
678 case NF4DIR:
679 default:
680 break;
681 }
682
683 READ_BUF(4);
684 create->cr_namelen = be32_to_cpup(p++);
685 READ_BUF(create->cr_namelen);
686 SAVEMEM(create->cr_name, create->cr_namelen);
687 if ((status = check_filename(create->cr_name, create->cr_namelen)))
688 return status;
689
690 status = nfsd4_decode_fattr(argp, create->cr_bmval, &create->cr_iattr,
691 &create->cr_acl, &create->cr_label,
692 &create->cr_umask);
693 if (status)
694 goto out;
695
696 DECODE_TAIL;
697}
698
699static inline __be32
700nfsd4_decode_delegreturn(struct nfsd4_compoundargs *argp, struct nfsd4_delegreturn *dr)
701{
702 return nfsd4_decode_stateid(argp, &dr->dr_stateid);
703}
704
705static inline __be32
706nfsd4_decode_getattr(struct nfsd4_compoundargs *argp, struct nfsd4_getattr *getattr)
707{
708 return nfsd4_decode_bitmap(argp, getattr->ga_bmval);
709}
710
711static __be32
712nfsd4_decode_link(struct nfsd4_compoundargs *argp, struct nfsd4_link *link)
713{
714 DECODE_HEAD;
715
716 READ_BUF(4);
717 link->li_namelen = be32_to_cpup(p++);
718 READ_BUF(link->li_namelen);
719 SAVEMEM(link->li_name, link->li_namelen);
720 if ((status = check_filename(link->li_name, link->li_namelen)))
721 return status;
722
723 DECODE_TAIL;
724}
725
726static __be32
727nfsd4_decode_lock(struct nfsd4_compoundargs *argp, struct nfsd4_lock *lock)
728{
729 DECODE_HEAD;
730
731 /*
732 * type, reclaim(boolean), offset, length, new_lock_owner(boolean)
733 */
734 READ_BUF(28);
735 lock->lk_type = be32_to_cpup(p++);
736 if ((lock->lk_type < NFS4_READ_LT) || (lock->lk_type > NFS4_WRITEW_LT))
737 goto xdr_error;
738 lock->lk_reclaim = be32_to_cpup(p++);
739 p = xdr_decode_hyper(p, &lock->lk_offset);
740 p = xdr_decode_hyper(p, &lock->lk_length);
741 lock->lk_is_new = be32_to_cpup(p++);
742
743 if (lock->lk_is_new) {
744 READ_BUF(4);
745 lock->lk_new_open_seqid = be32_to_cpup(p++);
746 status = nfsd4_decode_stateid(argp, &lock->lk_new_open_stateid);
747 if (status)
748 return status;
749 READ_BUF(8 + sizeof(clientid_t));
750 lock->lk_new_lock_seqid = be32_to_cpup(p++);
751 COPYMEM(&lock->lk_new_clientid, sizeof(clientid_t));
752 lock->lk_new_owner.len = be32_to_cpup(p++);
753 READ_BUF(lock->lk_new_owner.len);
754 READMEM(lock->lk_new_owner.data, lock->lk_new_owner.len);
755 } else {
756 status = nfsd4_decode_stateid(argp, &lock->lk_old_lock_stateid);
757 if (status)
758 return status;
759 READ_BUF(4);
760 lock->lk_old_lock_seqid = be32_to_cpup(p++);
761 }
762
763 DECODE_TAIL;
764}
765
766static __be32
767nfsd4_decode_lockt(struct nfsd4_compoundargs *argp, struct nfsd4_lockt *lockt)
768{
769 DECODE_HEAD;
770
771 READ_BUF(32);
772 lockt->lt_type = be32_to_cpup(p++);
773 if((lockt->lt_type < NFS4_READ_LT) || (lockt->lt_type > NFS4_WRITEW_LT))
774 goto xdr_error;
775 p = xdr_decode_hyper(p, &lockt->lt_offset);
776 p = xdr_decode_hyper(p, &lockt->lt_length);
777 COPYMEM(&lockt->lt_clientid, 8);
778 lockt->lt_owner.len = be32_to_cpup(p++);
779 READ_BUF(lockt->lt_owner.len);
780 READMEM(lockt->lt_owner.data, lockt->lt_owner.len);
781
782 DECODE_TAIL;
783}
784
785static __be32
786nfsd4_decode_locku(struct nfsd4_compoundargs *argp, struct nfsd4_locku *locku)
787{
788 DECODE_HEAD;
789
790 READ_BUF(8);
791 locku->lu_type = be32_to_cpup(p++);
792 if ((locku->lu_type < NFS4_READ_LT) || (locku->lu_type > NFS4_WRITEW_LT))
793 goto xdr_error;
794 locku->lu_seqid = be32_to_cpup(p++);
795 status = nfsd4_decode_stateid(argp, &locku->lu_stateid);
796 if (status)
797 return status;
798 READ_BUF(16);
799 p = xdr_decode_hyper(p, &locku->lu_offset);
800 p = xdr_decode_hyper(p, &locku->lu_length);
801
802 DECODE_TAIL;
803}
804
805static __be32
806nfsd4_decode_lookup(struct nfsd4_compoundargs *argp, struct nfsd4_lookup *lookup)
807{
808 DECODE_HEAD;
809
810 READ_BUF(4);
811 lookup->lo_len = be32_to_cpup(p++);
812 READ_BUF(lookup->lo_len);
813 SAVEMEM(lookup->lo_name, lookup->lo_len);
814 if ((status = check_filename(lookup->lo_name, lookup->lo_len)))
815 return status;
816
817 DECODE_TAIL;
818}
819
820static __be32 nfsd4_decode_share_access(struct nfsd4_compoundargs *argp, u32 *share_access, u32 *deleg_want, u32 *deleg_when)
821{
822 __be32 *p;
823 u32 w;
824
825 READ_BUF(4);
826 w = be32_to_cpup(p++);
827 *share_access = w & NFS4_SHARE_ACCESS_MASK;
828 *deleg_want = w & NFS4_SHARE_WANT_MASK;
829 if (deleg_when)
830 *deleg_when = w & NFS4_SHARE_WHEN_MASK;
831
832 switch (w & NFS4_SHARE_ACCESS_MASK) {
833 case NFS4_SHARE_ACCESS_READ:
834 case NFS4_SHARE_ACCESS_WRITE:
835 case NFS4_SHARE_ACCESS_BOTH:
836 break;
837 default:
838 return nfserr_bad_xdr;
839 }
840 w &= ~NFS4_SHARE_ACCESS_MASK;
841 if (!w)
842 return nfs_ok;
843 if (!argp->minorversion)
844 return nfserr_bad_xdr;
845 switch (w & NFS4_SHARE_WANT_MASK) {
846 case NFS4_SHARE_WANT_NO_PREFERENCE:
847 case NFS4_SHARE_WANT_READ_DELEG:
848 case NFS4_SHARE_WANT_WRITE_DELEG:
849 case NFS4_SHARE_WANT_ANY_DELEG:
850 case NFS4_SHARE_WANT_NO_DELEG:
851 case NFS4_SHARE_WANT_CANCEL:
852 break;
853 default:
854 return nfserr_bad_xdr;
855 }
856 w &= ~NFS4_SHARE_WANT_MASK;
857 if (!w)
858 return nfs_ok;
859
860 if (!deleg_when) /* open_downgrade */
861 return nfserr_inval;
862 switch (w) {
863 case NFS4_SHARE_SIGNAL_DELEG_WHEN_RESRC_AVAIL:
864 case NFS4_SHARE_PUSH_DELEG_WHEN_UNCONTENDED:
865 case (NFS4_SHARE_SIGNAL_DELEG_WHEN_RESRC_AVAIL |
866 NFS4_SHARE_PUSH_DELEG_WHEN_UNCONTENDED):
867 return nfs_ok;
868 }
869xdr_error:
870 return nfserr_bad_xdr;
871}
872
873static __be32 nfsd4_decode_share_deny(struct nfsd4_compoundargs *argp, u32 *x)
874{
875 __be32 *p;
876
877 READ_BUF(4);
878 *x = be32_to_cpup(p++);
879 /* Note: unlinke access bits, deny bits may be zero. */
880 if (*x & ~NFS4_SHARE_DENY_BOTH)
881 return nfserr_bad_xdr;
882 return nfs_ok;
883xdr_error:
884 return nfserr_bad_xdr;
885}
886
887static __be32 nfsd4_decode_opaque(struct nfsd4_compoundargs *argp, struct xdr_netobj *o)
888{
889 __be32 *p;
890
891 READ_BUF(4);
892 o->len = be32_to_cpup(p++);
893
894 if (o->len == 0 || o->len > NFS4_OPAQUE_LIMIT)
895 return nfserr_bad_xdr;
896
897 READ_BUF(o->len);
898 SAVEMEM(o->data, o->len);
899 return nfs_ok;
900xdr_error:
901 return nfserr_bad_xdr;
902}
903
904static __be32
905nfsd4_decode_open(struct nfsd4_compoundargs *argp, struct nfsd4_open *open)
906{
907 DECODE_HEAD;
908 u32 dummy;
909
910 memset(open->op_bmval, 0, sizeof(open->op_bmval));
911 open->op_iattr.ia_valid = 0;
912 open->op_openowner = NULL;
913
914 open->op_xdr_error = 0;
915 /* seqid, share_access, share_deny, clientid, ownerlen */
916 READ_BUF(4);
917 open->op_seqid = be32_to_cpup(p++);
918 /* decode, yet ignore deleg_when until supported */
919 status = nfsd4_decode_share_access(argp, &open->op_share_access,
920 &open->op_deleg_want, &dummy);
921 if (status)
922 goto xdr_error;
923 status = nfsd4_decode_share_deny(argp, &open->op_share_deny);
924 if (status)
925 goto xdr_error;
926 READ_BUF(sizeof(clientid_t));
927 COPYMEM(&open->op_clientid, sizeof(clientid_t));
928 status = nfsd4_decode_opaque(argp, &open->op_owner);
929 if (status)
930 goto xdr_error;
931 READ_BUF(4);
932 open->op_create = be32_to_cpup(p++);
933 switch (open->op_create) {
934 case NFS4_OPEN_NOCREATE:
935 break;
936 case NFS4_OPEN_CREATE:
937 READ_BUF(4);
938 open->op_createmode = be32_to_cpup(p++);
939 switch (open->op_createmode) {
940 case NFS4_CREATE_UNCHECKED:
941 case NFS4_CREATE_GUARDED:
942 status = nfsd4_decode_fattr(argp, open->op_bmval,
943 &open->op_iattr, &open->op_acl, &open->op_label,
944 &open->op_umask);
945 if (status)
946 goto out;
947 break;
948 case NFS4_CREATE_EXCLUSIVE:
949 READ_BUF(NFS4_VERIFIER_SIZE);
950 COPYMEM(open->op_verf.data, NFS4_VERIFIER_SIZE);
951 break;
952 case NFS4_CREATE_EXCLUSIVE4_1:
953 if (argp->minorversion < 1)
954 goto xdr_error;
955 READ_BUF(NFS4_VERIFIER_SIZE);
956 COPYMEM(open->op_verf.data, NFS4_VERIFIER_SIZE);
957 status = nfsd4_decode_fattr(argp, open->op_bmval,
958 &open->op_iattr, &open->op_acl, &open->op_label,
959 &open->op_umask);
960 if (status)
961 goto out;
962 break;
963 default:
964 goto xdr_error;
965 }
966 break;
967 default:
968 goto xdr_error;
969 }
970
971 /* open_claim */
972 READ_BUF(4);
973 open->op_claim_type = be32_to_cpup(p++);
974 switch (open->op_claim_type) {
975 case NFS4_OPEN_CLAIM_NULL:
976 case NFS4_OPEN_CLAIM_DELEGATE_PREV:
977 READ_BUF(4);
978 open->op_fname.len = be32_to_cpup(p++);
979 READ_BUF(open->op_fname.len);
980 SAVEMEM(open->op_fname.data, open->op_fname.len);
981 if ((status = check_filename(open->op_fname.data, open->op_fname.len)))
982 return status;
983 break;
984 case NFS4_OPEN_CLAIM_PREVIOUS:
985 READ_BUF(4);
986 open->op_delegate_type = be32_to_cpup(p++);
987 break;
988 case NFS4_OPEN_CLAIM_DELEGATE_CUR:
989 status = nfsd4_decode_stateid(argp, &open->op_delegate_stateid);
990 if (status)
991 return status;
992 READ_BUF(4);
993 open->op_fname.len = be32_to_cpup(p++);
994 READ_BUF(open->op_fname.len);
995 SAVEMEM(open->op_fname.data, open->op_fname.len);
996 if ((status = check_filename(open->op_fname.data, open->op_fname.len)))
997 return status;
998 break;
999 case NFS4_OPEN_CLAIM_FH:
1000 case NFS4_OPEN_CLAIM_DELEG_PREV_FH:
1001 if (argp->minorversion < 1)
1002 goto xdr_error;
1003 /* void */
1004 break;
1005 case NFS4_OPEN_CLAIM_DELEG_CUR_FH:
1006 if (argp->minorversion < 1)
1007 goto xdr_error;
1008 status = nfsd4_decode_stateid(argp, &open->op_delegate_stateid);
1009 if (status)
1010 return status;
1011 break;
1012 default:
1013 goto xdr_error;
1014 }
1015
1016 DECODE_TAIL;
1017}
1018
1019static __be32
1020nfsd4_decode_open_confirm(struct nfsd4_compoundargs *argp, struct nfsd4_open_confirm *open_conf)
1021{
1022 DECODE_HEAD;
1023
1024 if (argp->minorversion >= 1)
1025 return nfserr_notsupp;
1026
1027 status = nfsd4_decode_stateid(argp, &open_conf->oc_req_stateid);
1028 if (status)
1029 return status;
1030 READ_BUF(4);
1031 open_conf->oc_seqid = be32_to_cpup(p++);
1032
1033 DECODE_TAIL;
1034}
1035
1036static __be32
1037nfsd4_decode_open_downgrade(struct nfsd4_compoundargs *argp, struct nfsd4_open_downgrade *open_down)
1038{
1039 DECODE_HEAD;
1040
1041 status = nfsd4_decode_stateid(argp, &open_down->od_stateid);
1042 if (status)
1043 return status;
1044 READ_BUF(4);
1045 open_down->od_seqid = be32_to_cpup(p++);
1046 status = nfsd4_decode_share_access(argp, &open_down->od_share_access,
1047 &open_down->od_deleg_want, NULL);
1048 if (status)
1049 return status;
1050 status = nfsd4_decode_share_deny(argp, &open_down->od_share_deny);
1051 if (status)
1052 return status;
1053 DECODE_TAIL;
1054}
1055
1056static __be32
1057nfsd4_decode_putfh(struct nfsd4_compoundargs *argp, struct nfsd4_putfh *putfh)
1058{
1059 DECODE_HEAD;
1060
1061 READ_BUF(4);
1062 putfh->pf_fhlen = be32_to_cpup(p++);
1063 if (putfh->pf_fhlen > NFS4_FHSIZE)
1064 goto xdr_error;
1065 READ_BUF(putfh->pf_fhlen);
1066 SAVEMEM(putfh->pf_fhval, putfh->pf_fhlen);
1067
1068 DECODE_TAIL;
1069}
1070
1071static __be32
1072nfsd4_decode_read(struct nfsd4_compoundargs *argp, struct nfsd4_read *read)
1073{
1074 DECODE_HEAD;
1075
1076 status = nfsd4_decode_stateid(argp, &read->rd_stateid);
1077 if (status)
1078 return status;
1079 READ_BUF(12);
1080 p = xdr_decode_hyper(p, &read->rd_offset);
1081 read->rd_length = be32_to_cpup(p++);
1082
1083 DECODE_TAIL;
1084}
1085
1086static __be32
1087nfsd4_decode_readdir(struct nfsd4_compoundargs *argp, struct nfsd4_readdir *readdir)
1088{
1089 DECODE_HEAD;
1090
1091 READ_BUF(24);
1092 p = xdr_decode_hyper(p, &readdir->rd_cookie);
1093 COPYMEM(readdir->rd_verf.data, sizeof(readdir->rd_verf.data));
1094 readdir->rd_dircount = be32_to_cpup(p++);
1095 readdir->rd_maxcount = be32_to_cpup(p++);
1096 if ((status = nfsd4_decode_bitmap(argp, readdir->rd_bmval)))
1097 goto out;
1098
1099 DECODE_TAIL;
1100}
1101
1102static __be32
1103nfsd4_decode_remove(struct nfsd4_compoundargs *argp, struct nfsd4_remove *remove)
1104{
1105 DECODE_HEAD;
1106
1107 READ_BUF(4);
1108 remove->rm_namelen = be32_to_cpup(p++);
1109 READ_BUF(remove->rm_namelen);
1110 SAVEMEM(remove->rm_name, remove->rm_namelen);
1111 if ((status = check_filename(remove->rm_name, remove->rm_namelen)))
1112 return status;
1113
1114 DECODE_TAIL;
1115}
1116
1117static __be32
1118nfsd4_decode_rename(struct nfsd4_compoundargs *argp, struct nfsd4_rename *rename)
1119{
1120 DECODE_HEAD;
1121
1122 READ_BUF(4);
1123 rename->rn_snamelen = be32_to_cpup(p++);
1124 READ_BUF(rename->rn_snamelen);
1125 SAVEMEM(rename->rn_sname, rename->rn_snamelen);
1126 READ_BUF(4);
1127 rename->rn_tnamelen = be32_to_cpup(p++);
1128 READ_BUF(rename->rn_tnamelen);
1129 SAVEMEM(rename->rn_tname, rename->rn_tnamelen);
1130 if ((status = check_filename(rename->rn_sname, rename->rn_snamelen)))
1131 return status;
1132 if ((status = check_filename(rename->rn_tname, rename->rn_tnamelen)))
1133 return status;
1134
1135 DECODE_TAIL;
1136}
1137
1138static __be32
1139nfsd4_decode_renew(struct nfsd4_compoundargs *argp, clientid_t *clientid)
1140{
1141 DECODE_HEAD;
1142
1143 if (argp->minorversion >= 1)
1144 return nfserr_notsupp;
1145
1146 READ_BUF(sizeof(clientid_t));
1147 COPYMEM(clientid, sizeof(clientid_t));
1148
1149 DECODE_TAIL;
1150}
1151
1152static __be32
1153nfsd4_decode_secinfo(struct nfsd4_compoundargs *argp,
1154 struct nfsd4_secinfo *secinfo)
1155{
1156 DECODE_HEAD;
1157
1158 READ_BUF(4);
1159 secinfo->si_namelen = be32_to_cpup(p++);
1160 READ_BUF(secinfo->si_namelen);
1161 SAVEMEM(secinfo->si_name, secinfo->si_namelen);
1162 status = check_filename(secinfo->si_name, secinfo->si_namelen);
1163 if (status)
1164 return status;
1165 DECODE_TAIL;
1166}
1167
1168static __be32
1169nfsd4_decode_secinfo_no_name(struct nfsd4_compoundargs *argp,
1170 struct nfsd4_secinfo_no_name *sin)
1171{
1172 DECODE_HEAD;
1173
1174 READ_BUF(4);
1175 sin->sin_style = be32_to_cpup(p++);
1176 DECODE_TAIL;
1177}
1178
1179static __be32
1180nfsd4_decode_setattr(struct nfsd4_compoundargs *argp, struct nfsd4_setattr *setattr)
1181{
1182 __be32 status;
1183
1184 status = nfsd4_decode_stateid(argp, &setattr->sa_stateid);
1185 if (status)
1186 return status;
1187 return nfsd4_decode_fattr(argp, setattr->sa_bmval, &setattr->sa_iattr,
1188 &setattr->sa_acl, &setattr->sa_label, NULL);
1189}
1190
1191static __be32
1192nfsd4_decode_setclientid(struct nfsd4_compoundargs *argp, struct nfsd4_setclientid *setclientid)
1193{
1194 DECODE_HEAD;
1195
1196 if (argp->minorversion >= 1)
1197 return nfserr_notsupp;
1198
1199 READ_BUF(NFS4_VERIFIER_SIZE);
1200 COPYMEM(setclientid->se_verf.data, NFS4_VERIFIER_SIZE);
1201
1202 status = nfsd4_decode_opaque(argp, &setclientid->se_name);
1203 if (status)
1204 return nfserr_bad_xdr;
1205 READ_BUF(8);
1206 setclientid->se_callback_prog = be32_to_cpup(p++);
1207 setclientid->se_callback_netid_len = be32_to_cpup(p++);
1208 READ_BUF(setclientid->se_callback_netid_len);
1209 SAVEMEM(setclientid->se_callback_netid_val, setclientid->se_callback_netid_len);
1210 READ_BUF(4);
1211 setclientid->se_callback_addr_len = be32_to_cpup(p++);
1212
1213 READ_BUF(setclientid->se_callback_addr_len);
1214 SAVEMEM(setclientid->se_callback_addr_val, setclientid->se_callback_addr_len);
1215 READ_BUF(4);
1216 setclientid->se_callback_ident = be32_to_cpup(p++);
1217
1218 DECODE_TAIL;
1219}
1220
1221static __be32
1222nfsd4_decode_setclientid_confirm(struct nfsd4_compoundargs *argp, struct nfsd4_setclientid_confirm *scd_c)
1223{
1224 DECODE_HEAD;
1225
1226 if (argp->minorversion >= 1)
1227 return nfserr_notsupp;
1228
1229 READ_BUF(8 + NFS4_VERIFIER_SIZE);
1230 COPYMEM(&scd_c->sc_clientid, 8);
1231 COPYMEM(&scd_c->sc_confirm, NFS4_VERIFIER_SIZE);
1232
1233 DECODE_TAIL;
1234}
1235
1236/* Also used for NVERIFY */
1237static __be32
1238nfsd4_decode_verify(struct nfsd4_compoundargs *argp, struct nfsd4_verify *verify)
1239{
1240 DECODE_HEAD;
1241
1242 if ((status = nfsd4_decode_bitmap(argp, verify->ve_bmval)))
1243 goto out;
1244
1245 /* For convenience's sake, we compare raw xdr'd attributes in
1246 * nfsd4_proc_verify */
1247
1248 READ_BUF(4);
1249 verify->ve_attrlen = be32_to_cpup(p++);
1250 READ_BUF(verify->ve_attrlen);
1251 SAVEMEM(verify->ve_attrval, verify->ve_attrlen);
1252
1253 DECODE_TAIL;
1254}
1255
1256static __be32
1257nfsd4_decode_write(struct nfsd4_compoundargs *argp, struct nfsd4_write *write)
1258{
1259 int avail;
1260 int len;
1261 DECODE_HEAD;
1262
1263 status = nfsd4_decode_stateid(argp, &write->wr_stateid);
1264 if (status)
1265 return status;
1266 READ_BUF(16);
1267 p = xdr_decode_hyper(p, &write->wr_offset);
1268 write->wr_stable_how = be32_to_cpup(p++);
1269 if (write->wr_stable_how > NFS_FILE_SYNC)
1270 goto xdr_error;
1271 write->wr_buflen = be32_to_cpup(p++);
1272
1273 /* Sorry .. no magic macros for this.. *
1274 * READ_BUF(write->wr_buflen);
1275 * SAVEMEM(write->wr_buf, write->wr_buflen);
1276 */
1277 avail = (char*)argp->end - (char*)argp->p;
1278 if (avail + argp->pagelen < write->wr_buflen) {
1279 dprintk("NFSD: xdr error (%s:%d)\n",
1280 __FILE__, __LINE__);
1281 goto xdr_error;
1282 }
1283 write->wr_head.iov_base = p;
1284 write->wr_head.iov_len = avail;
1285 write->wr_pagelist = argp->pagelist;
1286
1287 len = XDR_QUADLEN(write->wr_buflen) << 2;
1288 if (len >= avail) {
1289 int pages;
1290
1291 len -= avail;
1292
1293 pages = len >> PAGE_SHIFT;
1294 argp->pagelist += pages;
1295 argp->pagelen -= pages * PAGE_SIZE;
1296 len -= pages * PAGE_SIZE;
1297
1298 next_decode_page(argp);
1299 }
1300 argp->p += XDR_QUADLEN(len);
1301
1302 DECODE_TAIL;
1303}
1304
1305static __be32
1306nfsd4_decode_release_lockowner(struct nfsd4_compoundargs *argp, struct nfsd4_release_lockowner *rlockowner)
1307{
1308 DECODE_HEAD;
1309
1310 if (argp->minorversion >= 1)
1311 return nfserr_notsupp;
1312
1313 READ_BUF(12);
1314 COPYMEM(&rlockowner->rl_clientid, sizeof(clientid_t));
1315 rlockowner->rl_owner.len = be32_to_cpup(p++);
1316 READ_BUF(rlockowner->rl_owner.len);
1317 READMEM(rlockowner->rl_owner.data, rlockowner->rl_owner.len);
1318
1319 if (argp->minorversion && !zero_clientid(&rlockowner->rl_clientid))
1320 return nfserr_inval;
1321 DECODE_TAIL;
1322}
1323
1324static __be32
1325nfsd4_decode_exchange_id(struct nfsd4_compoundargs *argp,
1326 struct nfsd4_exchange_id *exid)
1327{
1328 int dummy, tmp;
1329 DECODE_HEAD;
1330
1331 READ_BUF(NFS4_VERIFIER_SIZE);
1332 COPYMEM(exid->verifier.data, NFS4_VERIFIER_SIZE);
1333
1334 status = nfsd4_decode_opaque(argp, &exid->clname);
1335 if (status)
1336 return nfserr_bad_xdr;
1337
1338 READ_BUF(4);
1339 exid->flags = be32_to_cpup(p++);
1340
1341 /* Ignore state_protect4_a */
1342 READ_BUF(4);
1343 exid->spa_how = be32_to_cpup(p++);
1344 switch (exid->spa_how) {
1345 case SP4_NONE:
1346 break;
1347 case SP4_MACH_CRED:
1348 /* spo_must_enforce */
1349 status = nfsd4_decode_bitmap(argp,
1350 exid->spo_must_enforce);
1351 if (status)
1352 goto out;
1353 /* spo_must_allow */
1354 status = nfsd4_decode_bitmap(argp, exid->spo_must_allow);
1355 if (status)
1356 goto out;
1357 break;
1358 case SP4_SSV:
1359 /* ssp_ops */
1360 READ_BUF(4);
1361 dummy = be32_to_cpup(p++);
1362 READ_BUF(dummy * 4);
1363 p += dummy;
1364
1365 READ_BUF(4);
1366 dummy = be32_to_cpup(p++);
1367 READ_BUF(dummy * 4);
1368 p += dummy;
1369
1370 /* ssp_hash_algs<> */
1371 READ_BUF(4);
1372 tmp = be32_to_cpup(p++);
1373 while (tmp--) {
1374 READ_BUF(4);
1375 dummy = be32_to_cpup(p++);
1376 READ_BUF(dummy);
1377 p += XDR_QUADLEN(dummy);
1378 }
1379
1380 /* ssp_encr_algs<> */
1381 READ_BUF(4);
1382 tmp = be32_to_cpup(p++);
1383 while (tmp--) {
1384 READ_BUF(4);
1385 dummy = be32_to_cpup(p++);
1386 READ_BUF(dummy);
1387 p += XDR_QUADLEN(dummy);
1388 }
1389
1390 /* ignore ssp_window and ssp_num_gss_handles: */
1391 READ_BUF(8);
1392 break;
1393 default:
1394 goto xdr_error;
1395 }
1396
1397 READ_BUF(4); /* nfs_impl_id4 array length */
1398 dummy = be32_to_cpup(p++);
1399
1400 if (dummy > 1)
1401 goto xdr_error;
1402
1403 if (dummy == 1) {
1404 status = nfsd4_decode_opaque(argp, &exid->nii_domain);
1405 if (status)
1406 goto xdr_error;
1407
1408 /* nii_name */
1409 status = nfsd4_decode_opaque(argp, &exid->nii_name);
1410 if (status)
1411 goto xdr_error;
1412
1413 /* nii_date */
1414 status = nfsd4_decode_time(argp, &exid->nii_time);
1415 if (status)
1416 goto xdr_error;
1417 }
1418 DECODE_TAIL;
1419}
1420
1421static __be32
1422nfsd4_decode_create_session(struct nfsd4_compoundargs *argp,
1423 struct nfsd4_create_session *sess)
1424{
1425 DECODE_HEAD;
1426
1427 READ_BUF(16);
1428 COPYMEM(&sess->clientid, 8);
1429 sess->seqid = be32_to_cpup(p++);
1430 sess->flags = be32_to_cpup(p++);
1431
1432 /* Fore channel attrs */
1433 READ_BUF(28);
1434 p++; /* headerpadsz is always 0 */
1435 sess->fore_channel.maxreq_sz = be32_to_cpup(p++);
1436 sess->fore_channel.maxresp_sz = be32_to_cpup(p++);
1437 sess->fore_channel.maxresp_cached = be32_to_cpup(p++);
1438 sess->fore_channel.maxops = be32_to_cpup(p++);
1439 sess->fore_channel.maxreqs = be32_to_cpup(p++);
1440 sess->fore_channel.nr_rdma_attrs = be32_to_cpup(p++);
1441 if (sess->fore_channel.nr_rdma_attrs == 1) {
1442 READ_BUF(4);
1443 sess->fore_channel.rdma_attrs = be32_to_cpup(p++);
1444 } else if (sess->fore_channel.nr_rdma_attrs > 1) {
1445 dprintk("Too many fore channel attr bitmaps!\n");
1446 goto xdr_error;
1447 }
1448
1449 /* Back channel attrs */
1450 READ_BUF(28);
1451 p++; /* headerpadsz is always 0 */
1452 sess->back_channel.maxreq_sz = be32_to_cpup(p++);
1453 sess->back_channel.maxresp_sz = be32_to_cpup(p++);
1454 sess->back_channel.maxresp_cached = be32_to_cpup(p++);
1455 sess->back_channel.maxops = be32_to_cpup(p++);
1456 sess->back_channel.maxreqs = be32_to_cpup(p++);
1457 sess->back_channel.nr_rdma_attrs = be32_to_cpup(p++);
1458 if (sess->back_channel.nr_rdma_attrs == 1) {
1459 READ_BUF(4);
1460 sess->back_channel.rdma_attrs = be32_to_cpup(p++);
1461 } else if (sess->back_channel.nr_rdma_attrs > 1) {
1462 dprintk("Too many back channel attr bitmaps!\n");
1463 goto xdr_error;
1464 }
1465
1466 READ_BUF(4);
1467 sess->callback_prog = be32_to_cpup(p++);
1468 nfsd4_decode_cb_sec(argp, &sess->cb_sec);
1469 DECODE_TAIL;
1470}
1471
1472static __be32
1473nfsd4_decode_destroy_session(struct nfsd4_compoundargs *argp,
1474 struct nfsd4_destroy_session *destroy_session)
1475{
1476 DECODE_HEAD;
1477 READ_BUF(NFS4_MAX_SESSIONID_LEN);
1478 COPYMEM(destroy_session->sessionid.data, NFS4_MAX_SESSIONID_LEN);
1479
1480 DECODE_TAIL;
1481}
1482
1483static __be32
1484nfsd4_decode_free_stateid(struct nfsd4_compoundargs *argp,
1485 struct nfsd4_free_stateid *free_stateid)
1486{
1487 DECODE_HEAD;
1488
1489 READ_BUF(sizeof(stateid_t));
1490 free_stateid->fr_stateid.si_generation = be32_to_cpup(p++);
1491 COPYMEM(&free_stateid->fr_stateid.si_opaque, sizeof(stateid_opaque_t));
1492
1493 DECODE_TAIL;
1494}
1495
1496static __be32
1497nfsd4_decode_sequence(struct nfsd4_compoundargs *argp,
1498 struct nfsd4_sequence *seq)
1499{
1500 DECODE_HEAD;
1501
1502 READ_BUF(NFS4_MAX_SESSIONID_LEN + 16);
1503 COPYMEM(seq->sessionid.data, NFS4_MAX_SESSIONID_LEN);
1504 seq->seqid = be32_to_cpup(p++);
1505 seq->slotid = be32_to_cpup(p++);
1506 seq->maxslots = be32_to_cpup(p++);
1507 seq->cachethis = be32_to_cpup(p++);
1508
1509 DECODE_TAIL;
1510}
1511
1512static __be32
1513nfsd4_decode_test_stateid(struct nfsd4_compoundargs *argp, struct nfsd4_test_stateid *test_stateid)
1514{
1515 int i;
1516 __be32 *p, status;
1517 struct nfsd4_test_stateid_id *stateid;
1518
1519 READ_BUF(4);
1520 test_stateid->ts_num_ids = ntohl(*p++);
1521
1522 INIT_LIST_HEAD(&test_stateid->ts_stateid_list);
1523
1524 for (i = 0; i < test_stateid->ts_num_ids; i++) {
1525 stateid = svcxdr_tmpalloc(argp, sizeof(*stateid));
1526 if (!stateid) {
1527 status = nfserrno(-ENOMEM);
1528 goto out;
1529 }
1530
1531 INIT_LIST_HEAD(&stateid->ts_id_list);
1532 list_add_tail(&stateid->ts_id_list, &test_stateid->ts_stateid_list);
1533
1534 status = nfsd4_decode_stateid(argp, &stateid->ts_id_stateid);
1535 if (status)
1536 goto out;
1537 }
1538
1539 status = 0;
1540out:
1541 return status;
1542xdr_error:
1543 dprintk("NFSD: xdr error (%s:%d)\n", __FILE__, __LINE__);
1544 status = nfserr_bad_xdr;
1545 goto out;
1546}
1547
1548static __be32 nfsd4_decode_destroy_clientid(struct nfsd4_compoundargs *argp, struct nfsd4_destroy_clientid *dc)
1549{
1550 DECODE_HEAD;
1551
1552 READ_BUF(8);
1553 COPYMEM(&dc->clientid, 8);
1554
1555 DECODE_TAIL;
1556}
1557
1558static __be32 nfsd4_decode_reclaim_complete(struct nfsd4_compoundargs *argp, struct nfsd4_reclaim_complete *rc)
1559{
1560 DECODE_HEAD;
1561
1562 READ_BUF(4);
1563 rc->rca_one_fs = be32_to_cpup(p++);
1564
1565 DECODE_TAIL;
1566}
1567
1568#ifdef CONFIG_NFSD_PNFS
1569static __be32
1570nfsd4_decode_getdeviceinfo(struct nfsd4_compoundargs *argp,
1571 struct nfsd4_getdeviceinfo *gdev)
1572{
1573 DECODE_HEAD;
1574 u32 num, i;
1575
1576 READ_BUF(sizeof(struct nfsd4_deviceid) + 3 * 4);
1577 COPYMEM(&gdev->gd_devid, sizeof(struct nfsd4_deviceid));
1578 gdev->gd_layout_type = be32_to_cpup(p++);
1579 gdev->gd_maxcount = be32_to_cpup(p++);
1580 num = be32_to_cpup(p++);
1581 if (num) {
1582 if (num > 1000)
1583 goto xdr_error;
1584 READ_BUF(4 * num);
1585 gdev->gd_notify_types = be32_to_cpup(p++);
1586 for (i = 1; i < num; i++) {
1587 if (be32_to_cpup(p++)) {
1588 status = nfserr_inval;
1589 goto out;
1590 }
1591 }
1592 }
1593 DECODE_TAIL;
1594}
1595
1596static __be32
1597nfsd4_decode_layoutget(struct nfsd4_compoundargs *argp,
1598 struct nfsd4_layoutget *lgp)
1599{
1600 DECODE_HEAD;
1601
1602 READ_BUF(36);
1603 lgp->lg_signal = be32_to_cpup(p++);
1604 lgp->lg_layout_type = be32_to_cpup(p++);
1605 lgp->lg_seg.iomode = be32_to_cpup(p++);
1606 p = xdr_decode_hyper(p, &lgp->lg_seg.offset);
1607 p = xdr_decode_hyper(p, &lgp->lg_seg.length);
1608 p = xdr_decode_hyper(p, &lgp->lg_minlength);
1609
1610 status = nfsd4_decode_stateid(argp, &lgp->lg_sid);
1611 if (status)
1612 return status;
1613
1614 READ_BUF(4);
1615 lgp->lg_maxcount = be32_to_cpup(p++);
1616
1617 DECODE_TAIL;
1618}
1619
1620static __be32
1621nfsd4_decode_layoutcommit(struct nfsd4_compoundargs *argp,
1622 struct nfsd4_layoutcommit *lcp)
1623{
1624 DECODE_HEAD;
1625 u32 timechange;
1626
1627 READ_BUF(20);
1628 p = xdr_decode_hyper(p, &lcp->lc_seg.offset);
1629 p = xdr_decode_hyper(p, &lcp->lc_seg.length);
1630 lcp->lc_reclaim = be32_to_cpup(p++);
1631
1632 status = nfsd4_decode_stateid(argp, &lcp->lc_sid);
1633 if (status)
1634 return status;
1635
1636 READ_BUF(4);
1637 lcp->lc_newoffset = be32_to_cpup(p++);
1638 if (lcp->lc_newoffset) {
1639 READ_BUF(8);
1640 p = xdr_decode_hyper(p, &lcp->lc_last_wr);
1641 } else
1642 lcp->lc_last_wr = 0;
1643 READ_BUF(4);
1644 timechange = be32_to_cpup(p++);
1645 if (timechange) {
1646 status = nfsd4_decode_time(argp, &lcp->lc_mtime);
1647 if (status)
1648 return status;
1649 } else {
1650 lcp->lc_mtime.tv_nsec = UTIME_NOW;
1651 }
1652 READ_BUF(8);
1653 lcp->lc_layout_type = be32_to_cpup(p++);
1654
1655 /*
1656 * Save the layout update in XDR format and let the layout driver deal
1657 * with it later.
1658 */
1659 lcp->lc_up_len = be32_to_cpup(p++);
1660 if (lcp->lc_up_len > 0) {
1661 READ_BUF(lcp->lc_up_len);
1662 READMEM(lcp->lc_up_layout, lcp->lc_up_len);
1663 }
1664
1665 DECODE_TAIL;
1666}
1667
1668static __be32
1669nfsd4_decode_layoutreturn(struct nfsd4_compoundargs *argp,
1670 struct nfsd4_layoutreturn *lrp)
1671{
1672 DECODE_HEAD;
1673
1674 READ_BUF(16);
1675 lrp->lr_reclaim = be32_to_cpup(p++);
1676 lrp->lr_layout_type = be32_to_cpup(p++);
1677 lrp->lr_seg.iomode = be32_to_cpup(p++);
1678 lrp->lr_return_type = be32_to_cpup(p++);
1679 if (lrp->lr_return_type == RETURN_FILE) {
1680 READ_BUF(16);
1681 p = xdr_decode_hyper(p, &lrp->lr_seg.offset);
1682 p = xdr_decode_hyper(p, &lrp->lr_seg.length);
1683
1684 status = nfsd4_decode_stateid(argp, &lrp->lr_sid);
1685 if (status)
1686 return status;
1687
1688 READ_BUF(4);
1689 lrp->lrf_body_len = be32_to_cpup(p++);
1690 if (lrp->lrf_body_len > 0) {
1691 READ_BUF(lrp->lrf_body_len);
1692 READMEM(lrp->lrf_body, lrp->lrf_body_len);
1693 }
1694 } else {
1695 lrp->lr_seg.offset = 0;
1696 lrp->lr_seg.length = NFS4_MAX_UINT64;
1697 }
1698
1699 DECODE_TAIL;
1700}
1701#endif /* CONFIG_NFSD_PNFS */
1702
1703static __be32
1704nfsd4_decode_fallocate(struct nfsd4_compoundargs *argp,
1705 struct nfsd4_fallocate *fallocate)
1706{
1707 DECODE_HEAD;
1708
1709 status = nfsd4_decode_stateid(argp, &fallocate->falloc_stateid);
1710 if (status)
1711 return status;
1712
1713 READ_BUF(16);
1714 p = xdr_decode_hyper(p, &fallocate->falloc_offset);
1715 xdr_decode_hyper(p, &fallocate->falloc_length);
1716
1717 DECODE_TAIL;
1718}
1719
1720static __be32
1721nfsd4_decode_clone(struct nfsd4_compoundargs *argp, struct nfsd4_clone *clone)
1722{
1723 DECODE_HEAD;
1724
1725 status = nfsd4_decode_stateid(argp, &clone->cl_src_stateid);
1726 if (status)
1727 return status;
1728 status = nfsd4_decode_stateid(argp, &clone->cl_dst_stateid);
1729 if (status)
1730 return status;
1731
1732 READ_BUF(8 + 8 + 8);
1733 p = xdr_decode_hyper(p, &clone->cl_src_pos);
1734 p = xdr_decode_hyper(p, &clone->cl_dst_pos);
1735 p = xdr_decode_hyper(p, &clone->cl_count);
1736 DECODE_TAIL;
1737}
1738
1739static __be32
1740nfsd4_decode_copy(struct nfsd4_compoundargs *argp, struct nfsd4_copy *copy)
1741{
1742 DECODE_HEAD;
1743
1744 status = nfsd4_decode_stateid(argp, &copy->cp_src_stateid);
1745 if (status)
1746 return status;
1747 status = nfsd4_decode_stateid(argp, &copy->cp_dst_stateid);
1748 if (status)
1749 return status;
1750
1751 READ_BUF(8 + 8 + 8 + 4 + 4 + 4);
1752 p = xdr_decode_hyper(p, &copy->cp_src_pos);
1753 p = xdr_decode_hyper(p, &copy->cp_dst_pos);
1754 p = xdr_decode_hyper(p, &copy->cp_count);
1755 p++; /* ca_consecutive: we always do consecutive copies */
1756 copy->cp_synchronous = be32_to_cpup(p++);
1757 /* tmp = be32_to_cpup(p); Source server list not supported */
1758
1759 DECODE_TAIL;
1760}
1761
1762static __be32
1763nfsd4_decode_offload_status(struct nfsd4_compoundargs *argp,
1764 struct nfsd4_offload_status *os)
1765{
1766 return nfsd4_decode_stateid(argp, &os->stateid);
1767}
1768
1769static __be32
1770nfsd4_decode_seek(struct nfsd4_compoundargs *argp, struct nfsd4_seek *seek)
1771{
1772 DECODE_HEAD;
1773
1774 status = nfsd4_decode_stateid(argp, &seek->seek_stateid);
1775 if (status)
1776 return status;
1777
1778 READ_BUF(8 + 4);
1779 p = xdr_decode_hyper(p, &seek->seek_offset);
1780 seek->seek_whence = be32_to_cpup(p);
1781
1782 DECODE_TAIL;
1783}
1784
1785static __be32
1786nfsd4_decode_noop(struct nfsd4_compoundargs *argp, void *p)
1787{
1788 return nfs_ok;
1789}
1790
1791static __be32
1792nfsd4_decode_notsupp(struct nfsd4_compoundargs *argp, void *p)
1793{
1794 return nfserr_notsupp;
1795}
1796
1797typedef __be32(*nfsd4_dec)(struct nfsd4_compoundargs *argp, void *);
1798
1799static const nfsd4_dec nfsd4_dec_ops[] = {
1800 [OP_ACCESS] = (nfsd4_dec)nfsd4_decode_access,
1801 [OP_CLOSE] = (nfsd4_dec)nfsd4_decode_close,
1802 [OP_COMMIT] = (nfsd4_dec)nfsd4_decode_commit,
1803 [OP_CREATE] = (nfsd4_dec)nfsd4_decode_create,
1804 [OP_DELEGPURGE] = (nfsd4_dec)nfsd4_decode_notsupp,
1805 [OP_DELEGRETURN] = (nfsd4_dec)nfsd4_decode_delegreturn,
1806 [OP_GETATTR] = (nfsd4_dec)nfsd4_decode_getattr,
1807 [OP_GETFH] = (nfsd4_dec)nfsd4_decode_noop,
1808 [OP_LINK] = (nfsd4_dec)nfsd4_decode_link,
1809 [OP_LOCK] = (nfsd4_dec)nfsd4_decode_lock,
1810 [OP_LOCKT] = (nfsd4_dec)nfsd4_decode_lockt,
1811 [OP_LOCKU] = (nfsd4_dec)nfsd4_decode_locku,
1812 [OP_LOOKUP] = (nfsd4_dec)nfsd4_decode_lookup,
1813 [OP_LOOKUPP] = (nfsd4_dec)nfsd4_decode_noop,
1814 [OP_NVERIFY] = (nfsd4_dec)nfsd4_decode_verify,
1815 [OP_OPEN] = (nfsd4_dec)nfsd4_decode_open,
1816 [OP_OPENATTR] = (nfsd4_dec)nfsd4_decode_notsupp,
1817 [OP_OPEN_CONFIRM] = (nfsd4_dec)nfsd4_decode_open_confirm,
1818 [OP_OPEN_DOWNGRADE] = (nfsd4_dec)nfsd4_decode_open_downgrade,
1819 [OP_PUTFH] = (nfsd4_dec)nfsd4_decode_putfh,
1820 [OP_PUTPUBFH] = (nfsd4_dec)nfsd4_decode_noop,
1821 [OP_PUTROOTFH] = (nfsd4_dec)nfsd4_decode_noop,
1822 [OP_READ] = (nfsd4_dec)nfsd4_decode_read,
1823 [OP_READDIR] = (nfsd4_dec)nfsd4_decode_readdir,
1824 [OP_READLINK] = (nfsd4_dec)nfsd4_decode_noop,
1825 [OP_REMOVE] = (nfsd4_dec)nfsd4_decode_remove,
1826 [OP_RENAME] = (nfsd4_dec)nfsd4_decode_rename,
1827 [OP_RENEW] = (nfsd4_dec)nfsd4_decode_renew,
1828 [OP_RESTOREFH] = (nfsd4_dec)nfsd4_decode_noop,
1829 [OP_SAVEFH] = (nfsd4_dec)nfsd4_decode_noop,
1830 [OP_SECINFO] = (nfsd4_dec)nfsd4_decode_secinfo,
1831 [OP_SETATTR] = (nfsd4_dec)nfsd4_decode_setattr,
1832 [OP_SETCLIENTID] = (nfsd4_dec)nfsd4_decode_setclientid,
1833 [OP_SETCLIENTID_CONFIRM] = (nfsd4_dec)nfsd4_decode_setclientid_confirm,
1834 [OP_VERIFY] = (nfsd4_dec)nfsd4_decode_verify,
1835 [OP_WRITE] = (nfsd4_dec)nfsd4_decode_write,
1836 [OP_RELEASE_LOCKOWNER] = (nfsd4_dec)nfsd4_decode_release_lockowner,
1837
1838 /* new operations for NFSv4.1 */
1839 [OP_BACKCHANNEL_CTL] = (nfsd4_dec)nfsd4_decode_backchannel_ctl,
1840 [OP_BIND_CONN_TO_SESSION]= (nfsd4_dec)nfsd4_decode_bind_conn_to_session,
1841 [OP_EXCHANGE_ID] = (nfsd4_dec)nfsd4_decode_exchange_id,
1842 [OP_CREATE_SESSION] = (nfsd4_dec)nfsd4_decode_create_session,
1843 [OP_DESTROY_SESSION] = (nfsd4_dec)nfsd4_decode_destroy_session,
1844 [OP_FREE_STATEID] = (nfsd4_dec)nfsd4_decode_free_stateid,
1845 [OP_GET_DIR_DELEGATION] = (nfsd4_dec)nfsd4_decode_notsupp,
1846#ifdef CONFIG_NFSD_PNFS
1847 [OP_GETDEVICEINFO] = (nfsd4_dec)nfsd4_decode_getdeviceinfo,
1848 [OP_GETDEVICELIST] = (nfsd4_dec)nfsd4_decode_notsupp,
1849 [OP_LAYOUTCOMMIT] = (nfsd4_dec)nfsd4_decode_layoutcommit,
1850 [OP_LAYOUTGET] = (nfsd4_dec)nfsd4_decode_layoutget,
1851 [OP_LAYOUTRETURN] = (nfsd4_dec)nfsd4_decode_layoutreturn,
1852#else
1853 [OP_GETDEVICEINFO] = (nfsd4_dec)nfsd4_decode_notsupp,
1854 [OP_GETDEVICELIST] = (nfsd4_dec)nfsd4_decode_notsupp,
1855 [OP_LAYOUTCOMMIT] = (nfsd4_dec)nfsd4_decode_notsupp,
1856 [OP_LAYOUTGET] = (nfsd4_dec)nfsd4_decode_notsupp,
1857 [OP_LAYOUTRETURN] = (nfsd4_dec)nfsd4_decode_notsupp,
1858#endif
1859 [OP_SECINFO_NO_NAME] = (nfsd4_dec)nfsd4_decode_secinfo_no_name,
1860 [OP_SEQUENCE] = (nfsd4_dec)nfsd4_decode_sequence,
1861 [OP_SET_SSV] = (nfsd4_dec)nfsd4_decode_notsupp,
1862 [OP_TEST_STATEID] = (nfsd4_dec)nfsd4_decode_test_stateid,
1863 [OP_WANT_DELEGATION] = (nfsd4_dec)nfsd4_decode_notsupp,
1864 [OP_DESTROY_CLIENTID] = (nfsd4_dec)nfsd4_decode_destroy_clientid,
1865 [OP_RECLAIM_COMPLETE] = (nfsd4_dec)nfsd4_decode_reclaim_complete,
1866
1867 /* new operations for NFSv4.2 */
1868 [OP_ALLOCATE] = (nfsd4_dec)nfsd4_decode_fallocate,
1869 [OP_COPY] = (nfsd4_dec)nfsd4_decode_copy,
1870 [OP_COPY_NOTIFY] = (nfsd4_dec)nfsd4_decode_notsupp,
1871 [OP_DEALLOCATE] = (nfsd4_dec)nfsd4_decode_fallocate,
1872 [OP_IO_ADVISE] = (nfsd4_dec)nfsd4_decode_notsupp,
1873 [OP_LAYOUTERROR] = (nfsd4_dec)nfsd4_decode_notsupp,
1874 [OP_LAYOUTSTATS] = (nfsd4_dec)nfsd4_decode_notsupp,
1875 [OP_OFFLOAD_CANCEL] = (nfsd4_dec)nfsd4_decode_offload_status,
1876 [OP_OFFLOAD_STATUS] = (nfsd4_dec)nfsd4_decode_offload_status,
1877 [OP_READ_PLUS] = (nfsd4_dec)nfsd4_decode_notsupp,
1878 [OP_SEEK] = (nfsd4_dec)nfsd4_decode_seek,
1879 [OP_WRITE_SAME] = (nfsd4_dec)nfsd4_decode_notsupp,
1880 [OP_CLONE] = (nfsd4_dec)nfsd4_decode_clone,
1881};
1882
1883static inline bool
1884nfsd4_opnum_in_range(struct nfsd4_compoundargs *argp, struct nfsd4_op *op)
1885{
1886 if (op->opnum < FIRST_NFS4_OP)
1887 return false;
1888 else if (argp->minorversion == 0 && op->opnum > LAST_NFS40_OP)
1889 return false;
1890 else if (argp->minorversion == 1 && op->opnum > LAST_NFS41_OP)
1891 return false;
1892 else if (argp->minorversion == 2 && op->opnum > LAST_NFS42_OP)
1893 return false;
1894 return true;
1895}
1896
1897static __be32
1898nfsd4_decode_compound(struct nfsd4_compoundargs *argp)
1899{
1900 DECODE_HEAD;
1901 struct nfsd4_op *op;
1902 bool cachethis = false;
1903 int auth_slack= argp->rqstp->rq_auth_slack;
1904 int max_reply = auth_slack + 8; /* opcnt, status */
1905 int readcount = 0;
1906 int readbytes = 0;
1907 int i;
1908
1909 READ_BUF(4);
1910 argp->taglen = be32_to_cpup(p++);
1911 READ_BUF(argp->taglen);
1912 SAVEMEM(argp->tag, argp->taglen);
1913 READ_BUF(8);
1914 argp->minorversion = be32_to_cpup(p++);
1915 argp->opcnt = be32_to_cpup(p++);
1916 max_reply += 4 + (XDR_QUADLEN(argp->taglen) << 2);
1917
1918 if (argp->taglen > NFSD4_MAX_TAGLEN)
1919 goto xdr_error;
1920 /*
1921 * NFS4ERR_RESOURCE is a more helpful error than GARBAGE_ARGS
1922 * here, so we return success at the xdr level so that
1923 * nfsd4_proc can handle this is an NFS-level error.
1924 */
1925 if (argp->opcnt > NFSD_MAX_OPS_PER_COMPOUND)
1926 return 0;
1927
1928 if (argp->opcnt > ARRAY_SIZE(argp->iops)) {
1929 argp->ops = kzalloc(argp->opcnt * sizeof(*argp->ops), GFP_KERNEL);
1930 if (!argp->ops) {
1931 argp->ops = argp->iops;
1932 dprintk("nfsd: couldn't allocate room for COMPOUND\n");
1933 goto xdr_error;
1934 }
1935 }
1936
1937 if (argp->minorversion > NFSD_SUPPORTED_MINOR_VERSION)
1938 argp->opcnt = 0;
1939
1940 for (i = 0; i < argp->opcnt; i++) {
1941 op = &argp->ops[i];
1942 op->replay = NULL;
1943
1944 READ_BUF(4);
1945 op->opnum = be32_to_cpup(p++);
1946
1947 if (nfsd4_opnum_in_range(argp, op))
1948 op->status = nfsd4_dec_ops[op->opnum](argp, &op->u);
1949 else {
1950 op->opnum = OP_ILLEGAL;
1951 op->status = nfserr_op_illegal;
1952 }
1953 op->opdesc = OPDESC(op);
1954 /*
1955 * We'll try to cache the result in the DRC if any one
1956 * op in the compound wants to be cached:
1957 */
1958 cachethis |= nfsd4_cache_this_op(op);
1959
1960 if (op->opnum == OP_READ) {
1961 readcount++;
1962 readbytes += nfsd4_max_reply(argp->rqstp, op);
1963 } else
1964 max_reply += nfsd4_max_reply(argp->rqstp, op);
1965 /*
1966 * OP_LOCK and OP_LOCKT may return a conflicting lock.
1967 * (Special case because it will just skip encoding this
1968 * if it runs out of xdr buffer space, and it is the only
1969 * operation that behaves this way.)
1970 */
1971 if (op->opnum == OP_LOCK || op->opnum == OP_LOCKT)
1972 max_reply += NFS4_OPAQUE_LIMIT;
1973
1974 if (op->status) {
1975 argp->opcnt = i+1;
1976 break;
1977 }
1978 }
1979 /* Sessions make the DRC unnecessary: */
1980 if (argp->minorversion)
1981 cachethis = false;
1982 svc_reserve(argp->rqstp, max_reply + readbytes);
1983 argp->rqstp->rq_cachetype = cachethis ? RC_REPLBUFF : RC_NOCACHE;
1984
1985 if (readcount > 1 || max_reply > PAGE_SIZE - auth_slack)
1986 clear_bit(RQ_SPLICE_OK, &argp->rqstp->rq_flags);
1987
1988 DECODE_TAIL;
1989}
1990
1991static __be32 *encode_change(__be32 *p, struct kstat *stat, struct inode *inode,
1992 struct svc_export *exp)
1993{
1994 if (exp->ex_flags & NFSEXP_V4ROOT) {
1995 *p++ = cpu_to_be32(convert_to_wallclock(exp->cd->flush_time));
1996 *p++ = 0;
1997 } else if (IS_I_VERSION(inode)) {
1998 p = xdr_encode_hyper(p, nfsd4_change_attribute(stat, inode));
1999 } else {
2000 *p++ = cpu_to_be32(stat->ctime.tv_sec);
2001 *p++ = cpu_to_be32(stat->ctime.tv_nsec);
2002 }
2003 return p;
2004}
2005
2006/*
2007 * ctime (in NFSv4, time_metadata) is not writeable, and the client
2008 * doesn't really care what resolution could theoretically be stored by
2009 * the filesystem.
2010 *
2011 * The client cares how close together changes can be while still
2012 * guaranteeing ctime changes. For most filesystems (which have
2013 * timestamps with nanosecond fields) that is limited by the resolution
2014 * of the time returned from current_time() (which I'm assuming to be
2015 * 1/HZ).
2016 */
2017static __be32 *encode_time_delta(__be32 *p, struct inode *inode)
2018{
2019 struct timespec64 ts;
2020 u32 ns;
2021
2022 ns = max_t(u32, NSEC_PER_SEC/HZ, inode->i_sb->s_time_gran);
2023 ts = ns_to_timespec64(ns);
2024
2025 p = xdr_encode_hyper(p, ts.tv_sec);
2026 *p++ = cpu_to_be32(ts.tv_nsec);
2027
2028 return p;
2029}
2030
2031static __be32 *encode_cinfo(__be32 *p, struct nfsd4_change_info *c)
2032{
2033 *p++ = cpu_to_be32(c->atomic);
2034 if (c->change_supported) {
2035 p = xdr_encode_hyper(p, c->before_change);
2036 p = xdr_encode_hyper(p, c->after_change);
2037 } else {
2038 *p++ = cpu_to_be32(c->before_ctime_sec);
2039 *p++ = cpu_to_be32(c->before_ctime_nsec);
2040 *p++ = cpu_to_be32(c->after_ctime_sec);
2041 *p++ = cpu_to_be32(c->after_ctime_nsec);
2042 }
2043 return p;
2044}
2045
2046/* Encode as an array of strings the string given with components
2047 * separated @sep, escaped with esc_enter and esc_exit.
2048 */
2049static __be32 nfsd4_encode_components_esc(struct xdr_stream *xdr, char sep,
2050 char *components, char esc_enter,
2051 char esc_exit)
2052{
2053 __be32 *p;
2054 __be32 pathlen;
2055 int pathlen_offset;
2056 int strlen, count=0;
2057 char *str, *end, *next;
2058
2059 dprintk("nfsd4_encode_components(%s)\n", components);
2060
2061 pathlen_offset = xdr->buf->len;
2062 p = xdr_reserve_space(xdr, 4);
2063 if (!p)
2064 return nfserr_resource;
2065 p++; /* We will fill this in with @count later */
2066
2067 end = str = components;
2068 while (*end) {
2069 bool found_esc = false;
2070
2071 /* try to parse as esc_start, ..., esc_end, sep */
2072 if (*str == esc_enter) {
2073 for (; *end && (*end != esc_exit); end++)
2074 /* find esc_exit or end of string */;
2075 next = end + 1;
2076 if (*end && (!*next || *next == sep)) {
2077 str++;
2078 found_esc = true;
2079 }
2080 }
2081
2082 if (!found_esc)
2083 for (; *end && (*end != sep); end++)
2084 /* find sep or end of string */;
2085
2086 strlen = end - str;
2087 if (strlen) {
2088 p = xdr_reserve_space(xdr, strlen + 4);
2089 if (!p)
2090 return nfserr_resource;
2091 p = xdr_encode_opaque(p, str, strlen);
2092 count++;
2093 }
2094 else
2095 end++;
2096 if (found_esc)
2097 end = next;
2098
2099 str = end;
2100 }
2101 pathlen = htonl(count);
2102 write_bytes_to_xdr_buf(xdr->buf, pathlen_offset, &pathlen, 4);
2103 return 0;
2104}
2105
2106/* Encode as an array of strings the string given with components
2107 * separated @sep.
2108 */
2109static __be32 nfsd4_encode_components(struct xdr_stream *xdr, char sep,
2110 char *components)
2111{
2112 return nfsd4_encode_components_esc(xdr, sep, components, 0, 0);
2113}
2114
2115/*
2116 * encode a location element of a fs_locations structure
2117 */
2118static __be32 nfsd4_encode_fs_location4(struct xdr_stream *xdr,
2119 struct nfsd4_fs_location *location)
2120{
2121 __be32 status;
2122
2123 status = nfsd4_encode_components_esc(xdr, ':', location->hosts,
2124 '[', ']');
2125 if (status)
2126 return status;
2127 status = nfsd4_encode_components(xdr, '/', location->path);
2128 if (status)
2129 return status;
2130 return 0;
2131}
2132
2133/*
2134 * Encode a path in RFC3530 'pathname4' format
2135 */
2136static __be32 nfsd4_encode_path(struct xdr_stream *xdr,
2137 const struct path *root,
2138 const struct path *path)
2139{
2140 struct path cur = *path;
2141 __be32 *p;
2142 struct dentry **components = NULL;
2143 unsigned int ncomponents = 0;
2144 __be32 err = nfserr_jukebox;
2145
2146 dprintk("nfsd4_encode_components(");
2147
2148 path_get(&cur);
2149 /* First walk the path up to the nfsd root, and store the
2150 * dentries/path components in an array.
2151 */
2152 for (;;) {
2153 if (path_equal(&cur, root))
2154 break;
2155 if (cur.dentry == cur.mnt->mnt_root) {
2156 if (follow_up(&cur))
2157 continue;
2158 goto out_free;
2159 }
2160 if ((ncomponents & 15) == 0) {
2161 struct dentry **new;
2162 new = krealloc(components,
2163 sizeof(*new) * (ncomponents + 16),
2164 GFP_KERNEL);
2165 if (!new)
2166 goto out_free;
2167 components = new;
2168 }
2169 components[ncomponents++] = cur.dentry;
2170 cur.dentry = dget_parent(cur.dentry);
2171 }
2172 err = nfserr_resource;
2173 p = xdr_reserve_space(xdr, 4);
2174 if (!p)
2175 goto out_free;
2176 *p++ = cpu_to_be32(ncomponents);
2177
2178 while (ncomponents) {
2179 struct dentry *dentry = components[ncomponents - 1];
2180 unsigned int len;
2181
2182 spin_lock(&dentry->d_lock);
2183 len = dentry->d_name.len;
2184 p = xdr_reserve_space(xdr, len + 4);
2185 if (!p) {
2186 spin_unlock(&dentry->d_lock);
2187 goto out_free;
2188 }
2189 p = xdr_encode_opaque(p, dentry->d_name.name, len);
2190 dprintk("/%pd", dentry);
2191 spin_unlock(&dentry->d_lock);
2192 dput(dentry);
2193 ncomponents--;
2194 }
2195
2196 err = 0;
2197out_free:
2198 dprintk(")\n");
2199 while (ncomponents)
2200 dput(components[--ncomponents]);
2201 kfree(components);
2202 path_put(&cur);
2203 return err;
2204}
2205
2206static __be32 nfsd4_encode_fsloc_fsroot(struct xdr_stream *xdr,
2207 struct svc_rqst *rqstp, const struct path *path)
2208{
2209 struct svc_export *exp_ps;
2210 __be32 res;
2211
2212 exp_ps = rqst_find_fsidzero_export(rqstp);
2213 if (IS_ERR(exp_ps))
2214 return nfserrno(PTR_ERR(exp_ps));
2215 res = nfsd4_encode_path(xdr, &exp_ps->ex_path, path);
2216 exp_put(exp_ps);
2217 return res;
2218}
2219
2220/*
2221 * encode a fs_locations structure
2222 */
2223static __be32 nfsd4_encode_fs_locations(struct xdr_stream *xdr,
2224 struct svc_rqst *rqstp, struct svc_export *exp)
2225{
2226 __be32 status;
2227 int i;
2228 __be32 *p;
2229 struct nfsd4_fs_locations *fslocs = &exp->ex_fslocs;
2230
2231 status = nfsd4_encode_fsloc_fsroot(xdr, rqstp, &exp->ex_path);
2232 if (status)
2233 return status;
2234 p = xdr_reserve_space(xdr, 4);
2235 if (!p)
2236 return nfserr_resource;
2237 *p++ = cpu_to_be32(fslocs->locations_count);
2238 for (i=0; i<fslocs->locations_count; i++) {
2239 status = nfsd4_encode_fs_location4(xdr, &fslocs->locations[i]);
2240 if (status)
2241 return status;
2242 }
2243 return 0;
2244}
2245
2246static u32 nfs4_file_type(umode_t mode)
2247{
2248 switch (mode & S_IFMT) {
2249 case S_IFIFO: return NF4FIFO;
2250 case S_IFCHR: return NF4CHR;
2251 case S_IFDIR: return NF4DIR;
2252 case S_IFBLK: return NF4BLK;
2253 case S_IFLNK: return NF4LNK;
2254 case S_IFREG: return NF4REG;
2255 case S_IFSOCK: return NF4SOCK;
2256 default: return NF4BAD;
2257 };
2258}
2259
2260static inline __be32
2261nfsd4_encode_aclname(struct xdr_stream *xdr, struct svc_rqst *rqstp,
2262 struct nfs4_ace *ace)
2263{
2264 if (ace->whotype != NFS4_ACL_WHO_NAMED)
2265 return nfs4_acl_write_who(xdr, ace->whotype);
2266 else if (ace->flag & NFS4_ACE_IDENTIFIER_GROUP)
2267 return nfsd4_encode_group(xdr, rqstp, ace->who_gid);
2268 else
2269 return nfsd4_encode_user(xdr, rqstp, ace->who_uid);
2270}
2271
2272static inline __be32
2273nfsd4_encode_layout_types(struct xdr_stream *xdr, u32 layout_types)
2274{
2275 __be32 *p;
2276 unsigned long i = hweight_long(layout_types);
2277
2278 p = xdr_reserve_space(xdr, 4 + 4 * i);
2279 if (!p)
2280 return nfserr_resource;
2281
2282 *p++ = cpu_to_be32(i);
2283
2284 for (i = LAYOUT_NFSV4_1_FILES; i < LAYOUT_TYPE_MAX; ++i)
2285 if (layout_types & (1 << i))
2286 *p++ = cpu_to_be32(i);
2287
2288 return 0;
2289}
2290
2291#define WORD0_ABSENT_FS_ATTRS (FATTR4_WORD0_FS_LOCATIONS | FATTR4_WORD0_FSID | \
2292 FATTR4_WORD0_RDATTR_ERROR)
2293#define WORD1_ABSENT_FS_ATTRS FATTR4_WORD1_MOUNTED_ON_FILEID
2294#define WORD2_ABSENT_FS_ATTRS 0
2295
2296#ifdef CONFIG_NFSD_V4_SECURITY_LABEL
2297static inline __be32
2298nfsd4_encode_security_label(struct xdr_stream *xdr, struct svc_rqst *rqstp,
2299 void *context, int len)
2300{
2301 __be32 *p;
2302
2303 p = xdr_reserve_space(xdr, len + 4 + 4 + 4);
2304 if (!p)
2305 return nfserr_resource;
2306
2307 /*
2308 * For now we use a 0 here to indicate the null translation; in
2309 * the future we may place a call to translation code here.
2310 */
2311 *p++ = cpu_to_be32(0); /* lfs */
2312 *p++ = cpu_to_be32(0); /* pi */
2313 p = xdr_encode_opaque(p, context, len);
2314 return 0;
2315}
2316#else
2317static inline __be32
2318nfsd4_encode_security_label(struct xdr_stream *xdr, struct svc_rqst *rqstp,
2319 void *context, int len)
2320{ return 0; }
2321#endif
2322
2323static __be32 fattr_handle_absent_fs(u32 *bmval0, u32 *bmval1, u32 *bmval2, u32 *rdattr_err)
2324{
2325 /* As per referral draft: */
2326 if (*bmval0 & ~WORD0_ABSENT_FS_ATTRS ||
2327 *bmval1 & ~WORD1_ABSENT_FS_ATTRS) {
2328 if (*bmval0 & FATTR4_WORD0_RDATTR_ERROR ||
2329 *bmval0 & FATTR4_WORD0_FS_LOCATIONS)
2330 *rdattr_err = NFSERR_MOVED;
2331 else
2332 return nfserr_moved;
2333 }
2334 *bmval0 &= WORD0_ABSENT_FS_ATTRS;
2335 *bmval1 &= WORD1_ABSENT_FS_ATTRS;
2336 *bmval2 &= WORD2_ABSENT_FS_ATTRS;
2337 return 0;
2338}
2339
2340
2341static int get_parent_attributes(struct svc_export *exp, struct kstat *stat)
2342{
2343 struct path path = exp->ex_path;
2344 int err;
2345
2346 path_get(&path);
2347 while (follow_up(&path)) {
2348 if (path.dentry != path.mnt->mnt_root)
2349 break;
2350 }
2351 err = vfs_getattr(&path, stat, STATX_BASIC_STATS, AT_STATX_SYNC_AS_STAT);
2352 path_put(&path);
2353 return err;
2354}
2355
2356static __be32
2357nfsd4_encode_bitmap(struct xdr_stream *xdr, u32 bmval0, u32 bmval1, u32 bmval2)
2358{
2359 __be32 *p;
2360
2361 if (bmval2) {
2362 p = xdr_reserve_space(xdr, 16);
2363 if (!p)
2364 goto out_resource;
2365 *p++ = cpu_to_be32(3);
2366 *p++ = cpu_to_be32(bmval0);
2367 *p++ = cpu_to_be32(bmval1);
2368 *p++ = cpu_to_be32(bmval2);
2369 } else if (bmval1) {
2370 p = xdr_reserve_space(xdr, 12);
2371 if (!p)
2372 goto out_resource;
2373 *p++ = cpu_to_be32(2);
2374 *p++ = cpu_to_be32(bmval0);
2375 *p++ = cpu_to_be32(bmval1);
2376 } else {
2377 p = xdr_reserve_space(xdr, 8);
2378 if (!p)
2379 goto out_resource;
2380 *p++ = cpu_to_be32(1);
2381 *p++ = cpu_to_be32(bmval0);
2382 }
2383
2384 return 0;
2385out_resource:
2386 return nfserr_resource;
2387}
2388
2389/*
2390 * Note: @fhp can be NULL; in this case, we might have to compose the filehandle
2391 * ourselves.
2392 */
2393static __be32
2394nfsd4_encode_fattr(struct xdr_stream *xdr, struct svc_fh *fhp,
2395 struct svc_export *exp,
2396 struct dentry *dentry, u32 *bmval,
2397 struct svc_rqst *rqstp, int ignore_crossmnt)
2398{
2399 u32 bmval0 = bmval[0];
2400 u32 bmval1 = bmval[1];
2401 u32 bmval2 = bmval[2];
2402 struct kstat stat;
2403 struct svc_fh *tempfh = NULL;
2404 struct kstatfs statfs;
2405 __be32 *p;
2406 int starting_len = xdr->buf->len;
2407 int attrlen_offset;
2408 __be32 attrlen;
2409 u32 dummy;
2410 u64 dummy64;
2411 u32 rdattr_err = 0;
2412 __be32 status;
2413 int err;
2414 struct nfs4_acl *acl = NULL;
2415#ifdef CONFIG_NFSD_V4_SECURITY_LABEL
2416 void *context = NULL;
2417 int contextlen;
2418#endif
2419 bool contextsupport = false;
2420 struct nfsd4_compoundres *resp = rqstp->rq_resp;
2421 u32 minorversion = resp->cstate.minorversion;
2422 struct path path = {
2423 .mnt = exp->ex_path.mnt,
2424 .dentry = dentry,
2425 };
2426 struct nfsd_net *nn = net_generic(SVC_NET(rqstp), nfsd_net_id);
2427
2428 BUG_ON(bmval1 & NFSD_WRITEONLY_ATTRS_WORD1);
2429 BUG_ON(!nfsd_attrs_supported(minorversion, bmval));
2430
2431 if (exp->ex_fslocs.migrated) {
2432 status = fattr_handle_absent_fs(&bmval0, &bmval1, &bmval2, &rdattr_err);
2433 if (status)
2434 goto out;
2435 }
2436
2437 err = vfs_getattr(&path, &stat, STATX_BASIC_STATS, AT_STATX_SYNC_AS_STAT);
2438 if (err)
2439 goto out_nfserr;
2440 if ((bmval0 & (FATTR4_WORD0_FILES_AVAIL | FATTR4_WORD0_FILES_FREE |
2441 FATTR4_WORD0_FILES_TOTAL | FATTR4_WORD0_MAXNAME)) ||
2442 (bmval1 & (FATTR4_WORD1_SPACE_AVAIL | FATTR4_WORD1_SPACE_FREE |
2443 FATTR4_WORD1_SPACE_TOTAL))) {
2444 err = vfs_statfs(&path, &statfs);
2445 if (err)
2446 goto out_nfserr;
2447 }
2448 if ((bmval0 & (FATTR4_WORD0_FILEHANDLE | FATTR4_WORD0_FSID)) && !fhp) {
2449 tempfh = kmalloc(sizeof(struct svc_fh), GFP_KERNEL);
2450 status = nfserr_jukebox;
2451 if (!tempfh)
2452 goto out;
2453 fh_init(tempfh, NFS4_FHSIZE);
2454 status = fh_compose(tempfh, exp, dentry, NULL);
2455 if (status)
2456 goto out;
2457 fhp = tempfh;
2458 }
2459 if (bmval0 & FATTR4_WORD0_ACL) {
2460 err = nfsd4_get_nfs4_acl(rqstp, dentry, &acl);
2461 if (err == -EOPNOTSUPP)
2462 bmval0 &= ~FATTR4_WORD0_ACL;
2463 else if (err == -EINVAL) {
2464 status = nfserr_attrnotsupp;
2465 goto out;
2466 } else if (err != 0)
2467 goto out_nfserr;
2468 }
2469
2470#ifdef CONFIG_NFSD_V4_SECURITY_LABEL
2471 if ((bmval2 & FATTR4_WORD2_SECURITY_LABEL) ||
2472 bmval0 & FATTR4_WORD0_SUPPORTED_ATTRS) {
2473 if (exp->ex_flags & NFSEXP_SECURITY_LABEL)
2474 err = security_inode_getsecctx(d_inode(dentry),
2475 &context, &contextlen);
2476 else
2477 err = -EOPNOTSUPP;
2478 contextsupport = (err == 0);
2479 if (bmval2 & FATTR4_WORD2_SECURITY_LABEL) {
2480 if (err == -EOPNOTSUPP)
2481 bmval2 &= ~FATTR4_WORD2_SECURITY_LABEL;
2482 else if (err)
2483 goto out_nfserr;
2484 }
2485 }
2486#endif /* CONFIG_NFSD_V4_SECURITY_LABEL */
2487
2488 status = nfsd4_encode_bitmap(xdr, bmval0, bmval1, bmval2);
2489 if (status)
2490 goto out;
2491
2492 attrlen_offset = xdr->buf->len;
2493 p = xdr_reserve_space(xdr, 4);
2494 if (!p)
2495 goto out_resource;
2496 p++; /* to be backfilled later */
2497
2498 if (bmval0 & FATTR4_WORD0_SUPPORTED_ATTRS) {
2499 u32 supp[3];
2500
2501 memcpy(supp, nfsd_suppattrs[minorversion], sizeof(supp));
2502
2503 if (!IS_POSIXACL(dentry->d_inode))
2504 supp[0] &= ~FATTR4_WORD0_ACL;
2505 if (!contextsupport)
2506 supp[2] &= ~FATTR4_WORD2_SECURITY_LABEL;
2507 if (!supp[2]) {
2508 p = xdr_reserve_space(xdr, 12);
2509 if (!p)
2510 goto out_resource;
2511 *p++ = cpu_to_be32(2);
2512 *p++ = cpu_to_be32(supp[0]);
2513 *p++ = cpu_to_be32(supp[1]);
2514 } else {
2515 p = xdr_reserve_space(xdr, 16);
2516 if (!p)
2517 goto out_resource;
2518 *p++ = cpu_to_be32(3);
2519 *p++ = cpu_to_be32(supp[0]);
2520 *p++ = cpu_to_be32(supp[1]);
2521 *p++ = cpu_to_be32(supp[2]);
2522 }
2523 }
2524 if (bmval0 & FATTR4_WORD0_TYPE) {
2525 p = xdr_reserve_space(xdr, 4);
2526 if (!p)
2527 goto out_resource;
2528 dummy = nfs4_file_type(stat.mode);
2529 if (dummy == NF4BAD) {
2530 status = nfserr_serverfault;
2531 goto out;
2532 }
2533 *p++ = cpu_to_be32(dummy);
2534 }
2535 if (bmval0 & FATTR4_WORD0_FH_EXPIRE_TYPE) {
2536 p = xdr_reserve_space(xdr, 4);
2537 if (!p)
2538 goto out_resource;
2539 if (exp->ex_flags & NFSEXP_NOSUBTREECHECK)
2540 *p++ = cpu_to_be32(NFS4_FH_PERSISTENT);
2541 else
2542 *p++ = cpu_to_be32(NFS4_FH_PERSISTENT|
2543 NFS4_FH_VOL_RENAME);
2544 }
2545 if (bmval0 & FATTR4_WORD0_CHANGE) {
2546 p = xdr_reserve_space(xdr, 8);
2547 if (!p)
2548 goto out_resource;
2549 p = encode_change(p, &stat, d_inode(dentry), exp);
2550 }
2551 if (bmval0 & FATTR4_WORD0_SIZE) {
2552 p = xdr_reserve_space(xdr, 8);
2553 if (!p)
2554 goto out_resource;
2555 p = xdr_encode_hyper(p, stat.size);
2556 }
2557 if (bmval0 & FATTR4_WORD0_LINK_SUPPORT) {
2558 p = xdr_reserve_space(xdr, 4);
2559 if (!p)
2560 goto out_resource;
2561 *p++ = cpu_to_be32(1);
2562 }
2563 if (bmval0 & FATTR4_WORD0_SYMLINK_SUPPORT) {
2564 p = xdr_reserve_space(xdr, 4);
2565 if (!p)
2566 goto out_resource;
2567 *p++ = cpu_to_be32(1);
2568 }
2569 if (bmval0 & FATTR4_WORD0_NAMED_ATTR) {
2570 p = xdr_reserve_space(xdr, 4);
2571 if (!p)
2572 goto out_resource;
2573 *p++ = cpu_to_be32(0);
2574 }
2575 if (bmval0 & FATTR4_WORD0_FSID) {
2576 p = xdr_reserve_space(xdr, 16);
2577 if (!p)
2578 goto out_resource;
2579 if (exp->ex_fslocs.migrated) {
2580 p = xdr_encode_hyper(p, NFS4_REFERRAL_FSID_MAJOR);
2581 p = xdr_encode_hyper(p, NFS4_REFERRAL_FSID_MINOR);
2582 } else switch(fsid_source(fhp)) {
2583 case FSIDSOURCE_FSID:
2584 p = xdr_encode_hyper(p, (u64)exp->ex_fsid);
2585 p = xdr_encode_hyper(p, (u64)0);
2586 break;
2587 case FSIDSOURCE_DEV:
2588 *p++ = cpu_to_be32(0);
2589 *p++ = cpu_to_be32(MAJOR(stat.dev));
2590 *p++ = cpu_to_be32(0);
2591 *p++ = cpu_to_be32(MINOR(stat.dev));
2592 break;
2593 case FSIDSOURCE_UUID:
2594 p = xdr_encode_opaque_fixed(p, exp->ex_uuid,
2595 EX_UUID_LEN);
2596 break;
2597 }
2598 }
2599 if (bmval0 & FATTR4_WORD0_UNIQUE_HANDLES) {
2600 p = xdr_reserve_space(xdr, 4);
2601 if (!p)
2602 goto out_resource;
2603 *p++ = cpu_to_be32(0);
2604 }
2605 if (bmval0 & FATTR4_WORD0_LEASE_TIME) {
2606 p = xdr_reserve_space(xdr, 4);
2607 if (!p)
2608 goto out_resource;
2609 *p++ = cpu_to_be32(nn->nfsd4_lease);
2610 }
2611 if (bmval0 & FATTR4_WORD0_RDATTR_ERROR) {
2612 p = xdr_reserve_space(xdr, 4);
2613 if (!p)
2614 goto out_resource;
2615 *p++ = cpu_to_be32(rdattr_err);
2616 }
2617 if (bmval0 & FATTR4_WORD0_ACL) {
2618 struct nfs4_ace *ace;
2619
2620 if (acl == NULL) {
2621 p = xdr_reserve_space(xdr, 4);
2622 if (!p)
2623 goto out_resource;
2624
2625 *p++ = cpu_to_be32(0);
2626 goto out_acl;
2627 }
2628 p = xdr_reserve_space(xdr, 4);
2629 if (!p)
2630 goto out_resource;
2631 *p++ = cpu_to_be32(acl->naces);
2632
2633 for (ace = acl->aces; ace < acl->aces + acl->naces; ace++) {
2634 p = xdr_reserve_space(xdr, 4*3);
2635 if (!p)
2636 goto out_resource;
2637 *p++ = cpu_to_be32(ace->type);
2638 *p++ = cpu_to_be32(ace->flag);
2639 *p++ = cpu_to_be32(ace->access_mask &
2640 NFS4_ACE_MASK_ALL);
2641 status = nfsd4_encode_aclname(xdr, rqstp, ace);
2642 if (status)
2643 goto out;
2644 }
2645 }
2646out_acl:
2647 if (bmval0 & FATTR4_WORD0_ACLSUPPORT) {
2648 p = xdr_reserve_space(xdr, 4);
2649 if (!p)
2650 goto out_resource;
2651 *p++ = cpu_to_be32(IS_POSIXACL(dentry->d_inode) ?
2652 ACL4_SUPPORT_ALLOW_ACL|ACL4_SUPPORT_DENY_ACL : 0);
2653 }
2654 if (bmval0 & FATTR4_WORD0_CANSETTIME) {
2655 p = xdr_reserve_space(xdr, 4);
2656 if (!p)
2657 goto out_resource;
2658 *p++ = cpu_to_be32(1);
2659 }
2660 if (bmval0 & FATTR4_WORD0_CASE_INSENSITIVE) {
2661 p = xdr_reserve_space(xdr, 4);
2662 if (!p)
2663 goto out_resource;
2664 *p++ = cpu_to_be32(0);
2665 }
2666 if (bmval0 & FATTR4_WORD0_CASE_PRESERVING) {
2667 p = xdr_reserve_space(xdr, 4);
2668 if (!p)
2669 goto out_resource;
2670 *p++ = cpu_to_be32(1);
2671 }
2672 if (bmval0 & FATTR4_WORD0_CHOWN_RESTRICTED) {
2673 p = xdr_reserve_space(xdr, 4);
2674 if (!p)
2675 goto out_resource;
2676 *p++ = cpu_to_be32(1);
2677 }
2678 if (bmval0 & FATTR4_WORD0_FILEHANDLE) {
2679 p = xdr_reserve_space(xdr, fhp->fh_handle.fh_size + 4);
2680 if (!p)
2681 goto out_resource;
2682 p = xdr_encode_opaque(p, &fhp->fh_handle.fh_base,
2683 fhp->fh_handle.fh_size);
2684 }
2685 if (bmval0 & FATTR4_WORD0_FILEID) {
2686 p = xdr_reserve_space(xdr, 8);
2687 if (!p)
2688 goto out_resource;
2689 p = xdr_encode_hyper(p, stat.ino);
2690 }
2691 if (bmval0 & FATTR4_WORD0_FILES_AVAIL) {
2692 p = xdr_reserve_space(xdr, 8);
2693 if (!p)
2694 goto out_resource;
2695 p = xdr_encode_hyper(p, (u64) statfs.f_ffree);
2696 }
2697 if (bmval0 & FATTR4_WORD0_FILES_FREE) {
2698 p = xdr_reserve_space(xdr, 8);
2699 if (!p)
2700 goto out_resource;
2701 p = xdr_encode_hyper(p, (u64) statfs.f_ffree);
2702 }
2703 if (bmval0 & FATTR4_WORD0_FILES_TOTAL) {
2704 p = xdr_reserve_space(xdr, 8);
2705 if (!p)
2706 goto out_resource;
2707 p = xdr_encode_hyper(p, (u64) statfs.f_files);
2708 }
2709 if (bmval0 & FATTR4_WORD0_FS_LOCATIONS) {
2710 status = nfsd4_encode_fs_locations(xdr, rqstp, exp);
2711 if (status)
2712 goto out;
2713 }
2714 if (bmval0 & FATTR4_WORD0_HOMOGENEOUS) {
2715 p = xdr_reserve_space(xdr, 4);
2716 if (!p)
2717 goto out_resource;
2718 *p++ = cpu_to_be32(1);
2719 }
2720 if (bmval0 & FATTR4_WORD0_MAXFILESIZE) {
2721 p = xdr_reserve_space(xdr, 8);
2722 if (!p)
2723 goto out_resource;
2724 p = xdr_encode_hyper(p, exp->ex_path.mnt->mnt_sb->s_maxbytes);
2725 }
2726 if (bmval0 & FATTR4_WORD0_MAXLINK) {
2727 p = xdr_reserve_space(xdr, 4);
2728 if (!p)
2729 goto out_resource;
2730 *p++ = cpu_to_be32(255);
2731 }
2732 if (bmval0 & FATTR4_WORD0_MAXNAME) {
2733 p = xdr_reserve_space(xdr, 4);
2734 if (!p)
2735 goto out_resource;
2736 *p++ = cpu_to_be32(statfs.f_namelen);
2737 }
2738 if (bmval0 & FATTR4_WORD0_MAXREAD) {
2739 p = xdr_reserve_space(xdr, 8);
2740 if (!p)
2741 goto out_resource;
2742 p = xdr_encode_hyper(p, (u64) svc_max_payload(rqstp));
2743 }
2744 if (bmval0 & FATTR4_WORD0_MAXWRITE) {
2745 p = xdr_reserve_space(xdr, 8);
2746 if (!p)
2747 goto out_resource;
2748 p = xdr_encode_hyper(p, (u64) svc_max_payload(rqstp));
2749 }
2750 if (bmval1 & FATTR4_WORD1_MODE) {
2751 p = xdr_reserve_space(xdr, 4);
2752 if (!p)
2753 goto out_resource;
2754 *p++ = cpu_to_be32(stat.mode & S_IALLUGO);
2755 }
2756 if (bmval1 & FATTR4_WORD1_NO_TRUNC) {
2757 p = xdr_reserve_space(xdr, 4);
2758 if (!p)
2759 goto out_resource;
2760 *p++ = cpu_to_be32(1);
2761 }
2762 if (bmval1 & FATTR4_WORD1_NUMLINKS) {
2763 p = xdr_reserve_space(xdr, 4);
2764 if (!p)
2765 goto out_resource;
2766 *p++ = cpu_to_be32(stat.nlink);
2767 }
2768 if (bmval1 & FATTR4_WORD1_OWNER) {
2769 status = nfsd4_encode_user(xdr, rqstp, stat.uid);
2770 if (status)
2771 goto out;
2772 }
2773 if (bmval1 & FATTR4_WORD1_OWNER_GROUP) {
2774 status = nfsd4_encode_group(xdr, rqstp, stat.gid);
2775 if (status)
2776 goto out;
2777 }
2778 if (bmval1 & FATTR4_WORD1_RAWDEV) {
2779 p = xdr_reserve_space(xdr, 8);
2780 if (!p)
2781 goto out_resource;
2782 *p++ = cpu_to_be32((u32) MAJOR(stat.rdev));
2783 *p++ = cpu_to_be32((u32) MINOR(stat.rdev));
2784 }
2785 if (bmval1 & FATTR4_WORD1_SPACE_AVAIL) {
2786 p = xdr_reserve_space(xdr, 8);
2787 if (!p)
2788 goto out_resource;
2789 dummy64 = (u64)statfs.f_bavail * (u64)statfs.f_bsize;
2790 p = xdr_encode_hyper(p, dummy64);
2791 }
2792 if (bmval1 & FATTR4_WORD1_SPACE_FREE) {
2793 p = xdr_reserve_space(xdr, 8);
2794 if (!p)
2795 goto out_resource;
2796 dummy64 = (u64)statfs.f_bfree * (u64)statfs.f_bsize;
2797 p = xdr_encode_hyper(p, dummy64);
2798 }
2799 if (bmval1 & FATTR4_WORD1_SPACE_TOTAL) {
2800 p = xdr_reserve_space(xdr, 8);
2801 if (!p)
2802 goto out_resource;
2803 dummy64 = (u64)statfs.f_blocks * (u64)statfs.f_bsize;
2804 p = xdr_encode_hyper(p, dummy64);
2805 }
2806 if (bmval1 & FATTR4_WORD1_SPACE_USED) {
2807 p = xdr_reserve_space(xdr, 8);
2808 if (!p)
2809 goto out_resource;
2810 dummy64 = (u64)stat.blocks << 9;
2811 p = xdr_encode_hyper(p, dummy64);
2812 }
2813 if (bmval1 & FATTR4_WORD1_TIME_ACCESS) {
2814 p = xdr_reserve_space(xdr, 12);
2815 if (!p)
2816 goto out_resource;
2817 p = xdr_encode_hyper(p, (s64)stat.atime.tv_sec);
2818 *p++ = cpu_to_be32(stat.atime.tv_nsec);
2819 }
2820 if (bmval1 & FATTR4_WORD1_TIME_DELTA) {
2821 p = xdr_reserve_space(xdr, 12);
2822 if (!p)
2823 goto out_resource;
2824 p = encode_time_delta(p, d_inode(dentry));
2825 }
2826 if (bmval1 & FATTR4_WORD1_TIME_METADATA) {
2827 p = xdr_reserve_space(xdr, 12);
2828 if (!p)
2829 goto out_resource;
2830 p = xdr_encode_hyper(p, (s64)stat.ctime.tv_sec);
2831 *p++ = cpu_to_be32(stat.ctime.tv_nsec);
2832 }
2833 if (bmval1 & FATTR4_WORD1_TIME_MODIFY) {
2834 p = xdr_reserve_space(xdr, 12);
2835 if (!p)
2836 goto out_resource;
2837 p = xdr_encode_hyper(p, (s64)stat.mtime.tv_sec);
2838 *p++ = cpu_to_be32(stat.mtime.tv_nsec);
2839 }
2840 if (bmval1 & FATTR4_WORD1_MOUNTED_ON_FILEID) {
2841 struct kstat parent_stat;
2842 u64 ino = stat.ino;
2843
2844 p = xdr_reserve_space(xdr, 8);
2845 if (!p)
2846 goto out_resource;
2847 /*
2848 * Get parent's attributes if not ignoring crossmount
2849 * and this is the root of a cross-mounted filesystem.
2850 */
2851 if (ignore_crossmnt == 0 &&
2852 dentry == exp->ex_path.mnt->mnt_root) {
2853 err = get_parent_attributes(exp, &parent_stat);
2854 if (err)
2855 goto out_nfserr;
2856 ino = parent_stat.ino;
2857 }
2858 p = xdr_encode_hyper(p, ino);
2859 }
2860#ifdef CONFIG_NFSD_PNFS
2861 if (bmval1 & FATTR4_WORD1_FS_LAYOUT_TYPES) {
2862 status = nfsd4_encode_layout_types(xdr, exp->ex_layout_types);
2863 if (status)
2864 goto out;
2865 }
2866
2867 if (bmval2 & FATTR4_WORD2_LAYOUT_TYPES) {
2868 status = nfsd4_encode_layout_types(xdr, exp->ex_layout_types);
2869 if (status)
2870 goto out;
2871 }
2872
2873 if (bmval2 & FATTR4_WORD2_LAYOUT_BLKSIZE) {
2874 p = xdr_reserve_space(xdr, 4);
2875 if (!p)
2876 goto out_resource;
2877 *p++ = cpu_to_be32(stat.blksize);
2878 }
2879#endif /* CONFIG_NFSD_PNFS */
2880 if (bmval2 & FATTR4_WORD2_SUPPATTR_EXCLCREAT) {
2881 u32 supp[3];
2882
2883 memcpy(supp, nfsd_suppattrs[minorversion], sizeof(supp));
2884 supp[0] &= NFSD_SUPPATTR_EXCLCREAT_WORD0;
2885 supp[1] &= NFSD_SUPPATTR_EXCLCREAT_WORD1;
2886 supp[2] &= NFSD_SUPPATTR_EXCLCREAT_WORD2;
2887
2888 status = nfsd4_encode_bitmap(xdr, supp[0], supp[1], supp[2]);
2889 if (status)
2890 goto out;
2891 }
2892
2893 if (bmval2 & FATTR4_WORD2_CHANGE_ATTR_TYPE) {
2894 p = xdr_reserve_space(xdr, 4);
2895 if (!p)
2896 goto out_resource;
2897 if (IS_I_VERSION(d_inode(dentry)))
2898 *p++ = cpu_to_be32(NFS4_CHANGE_TYPE_IS_MONOTONIC_INCR);
2899 else
2900 *p++ = cpu_to_be32(NFS4_CHANGE_TYPE_IS_TIME_METADATA);
2901 }
2902
2903#ifdef CONFIG_NFSD_V4_SECURITY_LABEL
2904 if (bmval2 & FATTR4_WORD2_SECURITY_LABEL) {
2905 status = nfsd4_encode_security_label(xdr, rqstp, context,
2906 contextlen);
2907 if (status)
2908 goto out;
2909 }
2910#endif
2911
2912 attrlen = htonl(xdr->buf->len - attrlen_offset - 4);
2913 write_bytes_to_xdr_buf(xdr->buf, attrlen_offset, &attrlen, 4);
2914 status = nfs_ok;
2915
2916out:
2917#ifdef CONFIG_NFSD_V4_SECURITY_LABEL
2918 if (context)
2919 security_release_secctx(context, contextlen);
2920#endif /* CONFIG_NFSD_V4_SECURITY_LABEL */
2921 kfree(acl);
2922 if (tempfh) {
2923 fh_put(tempfh);
2924 kfree(tempfh);
2925 }
2926 if (status)
2927 xdr_truncate_encode(xdr, starting_len);
2928 return status;
2929out_nfserr:
2930 status = nfserrno(err);
2931 goto out;
2932out_resource:
2933 status = nfserr_resource;
2934 goto out;
2935}
2936
2937static void svcxdr_init_encode_from_buffer(struct xdr_stream *xdr,
2938 struct xdr_buf *buf, __be32 *p, int bytes)
2939{
2940 xdr->scratch.iov_len = 0;
2941 memset(buf, 0, sizeof(struct xdr_buf));
2942 buf->head[0].iov_base = p;
2943 buf->head[0].iov_len = 0;
2944 buf->len = 0;
2945 xdr->buf = buf;
2946 xdr->iov = buf->head;
2947 xdr->p = p;
2948 xdr->end = (void *)p + bytes;
2949 buf->buflen = bytes;
2950}
2951
2952__be32 nfsd4_encode_fattr_to_buf(__be32 **p, int words,
2953 struct svc_fh *fhp, struct svc_export *exp,
2954 struct dentry *dentry, u32 *bmval,
2955 struct svc_rqst *rqstp, int ignore_crossmnt)
2956{
2957 struct xdr_buf dummy;
2958 struct xdr_stream xdr;
2959 __be32 ret;
2960
2961 svcxdr_init_encode_from_buffer(&xdr, &dummy, *p, words << 2);
2962 ret = nfsd4_encode_fattr(&xdr, fhp, exp, dentry, bmval, rqstp,
2963 ignore_crossmnt);
2964 *p = xdr.p;
2965 return ret;
2966}
2967
2968static inline int attributes_need_mount(u32 *bmval)
2969{
2970 if (bmval[0] & ~(FATTR4_WORD0_RDATTR_ERROR | FATTR4_WORD0_LEASE_TIME))
2971 return 1;
2972 if (bmval[1] & ~FATTR4_WORD1_MOUNTED_ON_FILEID)
2973 return 1;
2974 return 0;
2975}
2976
2977static __be32
2978nfsd4_encode_dirent_fattr(struct xdr_stream *xdr, struct nfsd4_readdir *cd,
2979 const char *name, int namlen)
2980{
2981 struct svc_export *exp = cd->rd_fhp->fh_export;
2982 struct dentry *dentry;
2983 __be32 nfserr;
2984 int ignore_crossmnt = 0;
2985
2986 dentry = lookup_positive_unlocked(name, cd->rd_fhp->fh_dentry, namlen);
2987 if (IS_ERR(dentry))
2988 return nfserrno(PTR_ERR(dentry));
2989
2990 exp_get(exp);
2991 /*
2992 * In the case of a mountpoint, the client may be asking for
2993 * attributes that are only properties of the underlying filesystem
2994 * as opposed to the cross-mounted file system. In such a case,
2995 * we will not follow the cross mount and will fill the attribtutes
2996 * directly from the mountpoint dentry.
2997 */
2998 if (nfsd_mountpoint(dentry, exp)) {
2999 int err;
3000
3001 if (!(exp->ex_flags & NFSEXP_V4ROOT)
3002 && !attributes_need_mount(cd->rd_bmval)) {
3003 ignore_crossmnt = 1;
3004 goto out_encode;
3005 }
3006 /*
3007 * Why the heck aren't we just using nfsd_lookup??
3008 * Different "."/".." handling? Something else?
3009 * At least, add a comment here to explain....
3010 */
3011 err = nfsd_cross_mnt(cd->rd_rqstp, &dentry, &exp);
3012 if (err) {
3013 nfserr = nfserrno(err);
3014 goto out_put;
3015 }
3016 nfserr = check_nfsd_access(exp, cd->rd_rqstp);
3017 if (nfserr)
3018 goto out_put;
3019
3020 }
3021out_encode:
3022 nfserr = nfsd4_encode_fattr(xdr, NULL, exp, dentry, cd->rd_bmval,
3023 cd->rd_rqstp, ignore_crossmnt);
3024out_put:
3025 dput(dentry);
3026 exp_put(exp);
3027 return nfserr;
3028}
3029
3030static __be32 *
3031nfsd4_encode_rdattr_error(struct xdr_stream *xdr, __be32 nfserr)
3032{
3033 __be32 *p;
3034
3035 p = xdr_reserve_space(xdr, 20);
3036 if (!p)
3037 return NULL;
3038 *p++ = htonl(2);
3039 *p++ = htonl(FATTR4_WORD0_RDATTR_ERROR); /* bmval0 */
3040 *p++ = htonl(0); /* bmval1 */
3041
3042 *p++ = htonl(4); /* attribute length */
3043 *p++ = nfserr; /* no htonl */
3044 return p;
3045}
3046
3047static int
3048nfsd4_encode_dirent(void *ccdv, const char *name, int namlen,
3049 loff_t offset, u64 ino, unsigned int d_type)
3050{
3051 struct readdir_cd *ccd = ccdv;
3052 struct nfsd4_readdir *cd = container_of(ccd, struct nfsd4_readdir, common);
3053 struct xdr_stream *xdr = cd->xdr;
3054 int start_offset = xdr->buf->len;
3055 int cookie_offset;
3056 u32 name_and_cookie;
3057 int entry_bytes;
3058 __be32 nfserr = nfserr_toosmall;
3059 __be64 wire_offset;
3060 __be32 *p;
3061
3062 /* In nfsv4, "." and ".." never make it onto the wire.. */
3063 if (name && isdotent(name, namlen)) {
3064 cd->common.err = nfs_ok;
3065 return 0;
3066 }
3067
3068 if (cd->cookie_offset) {
3069 wire_offset = cpu_to_be64(offset);
3070 write_bytes_to_xdr_buf(xdr->buf, cd->cookie_offset,
3071 &wire_offset, 8);
3072 }
3073
3074 p = xdr_reserve_space(xdr, 4);
3075 if (!p)
3076 goto fail;
3077 *p++ = xdr_one; /* mark entry present */
3078 cookie_offset = xdr->buf->len;
3079 p = xdr_reserve_space(xdr, 3*4 + namlen);
3080 if (!p)
3081 goto fail;
3082 p = xdr_encode_hyper(p, NFS_OFFSET_MAX); /* offset of next entry */
3083 p = xdr_encode_array(p, name, namlen); /* name length & name */
3084
3085 nfserr = nfsd4_encode_dirent_fattr(xdr, cd, name, namlen);
3086 switch (nfserr) {
3087 case nfs_ok:
3088 break;
3089 case nfserr_resource:
3090 nfserr = nfserr_toosmall;
3091 goto fail;
3092 case nfserr_noent:
3093 xdr_truncate_encode(xdr, start_offset);
3094 goto skip_entry;
3095 case nfserr_jukebox:
3096 /*
3097 * The pseudoroot should only display dentries that lead to
3098 * exports. If we get EJUKEBOX here, then we can't tell whether
3099 * this entry should be included. Just fail the whole READDIR
3100 * with NFS4ERR_DELAY in that case, and hope that the situation
3101 * will resolve itself by the client's next attempt.
3102 */
3103 if (cd->rd_fhp->fh_export->ex_flags & NFSEXP_V4ROOT)
3104 goto fail;
3105 fallthrough;
3106 default:
3107 /*
3108 * If the client requested the RDATTR_ERROR attribute,
3109 * we stuff the error code into this attribute
3110 * and continue. If this attribute was not requested,
3111 * then in accordance with the spec, we fail the
3112 * entire READDIR operation(!)
3113 */
3114 if (!(cd->rd_bmval[0] & FATTR4_WORD0_RDATTR_ERROR))
3115 goto fail;
3116 p = nfsd4_encode_rdattr_error(xdr, nfserr);
3117 if (p == NULL) {
3118 nfserr = nfserr_toosmall;
3119 goto fail;
3120 }
3121 }
3122 nfserr = nfserr_toosmall;
3123 entry_bytes = xdr->buf->len - start_offset;
3124 if (entry_bytes > cd->rd_maxcount)
3125 goto fail;
3126 cd->rd_maxcount -= entry_bytes;
3127 /*
3128 * RFC 3530 14.2.24 describes rd_dircount as only a "hint", and
3129 * notes that it could be zero. If it is zero, then the server
3130 * should enforce only the rd_maxcount value.
3131 */
3132 if (cd->rd_dircount) {
3133 name_and_cookie = 4 + 4 * XDR_QUADLEN(namlen) + 8;
3134 if (name_and_cookie > cd->rd_dircount && cd->cookie_offset)
3135 goto fail;
3136 cd->rd_dircount -= min(cd->rd_dircount, name_and_cookie);
3137 if (!cd->rd_dircount)
3138 cd->rd_maxcount = 0;
3139 }
3140
3141 cd->cookie_offset = cookie_offset;
3142skip_entry:
3143 cd->common.err = nfs_ok;
3144 return 0;
3145fail:
3146 xdr_truncate_encode(xdr, start_offset);
3147 cd->common.err = nfserr;
3148 return -EINVAL;
3149}
3150
3151static __be32
3152nfsd4_encode_stateid(struct xdr_stream *xdr, stateid_t *sid)
3153{
3154 __be32 *p;
3155
3156 p = xdr_reserve_space(xdr, sizeof(stateid_t));
3157 if (!p)
3158 return nfserr_resource;
3159 *p++ = cpu_to_be32(sid->si_generation);
3160 p = xdr_encode_opaque_fixed(p, &sid->si_opaque,
3161 sizeof(stateid_opaque_t));
3162 return 0;
3163}
3164
3165static __be32
3166nfsd4_encode_access(struct nfsd4_compoundres *resp, __be32 nfserr, struct nfsd4_access *access)
3167{
3168 struct xdr_stream *xdr = &resp->xdr;
3169 __be32 *p;
3170
3171 p = xdr_reserve_space(xdr, 8);
3172 if (!p)
3173 return nfserr_resource;
3174 *p++ = cpu_to_be32(access->ac_supported);
3175 *p++ = cpu_to_be32(access->ac_resp_access);
3176 return 0;
3177}
3178
3179static __be32 nfsd4_encode_bind_conn_to_session(struct nfsd4_compoundres *resp, __be32 nfserr, struct nfsd4_bind_conn_to_session *bcts)
3180{
3181 struct xdr_stream *xdr = &resp->xdr;
3182 __be32 *p;
3183
3184 p = xdr_reserve_space(xdr, NFS4_MAX_SESSIONID_LEN + 8);
3185 if (!p)
3186 return nfserr_resource;
3187 p = xdr_encode_opaque_fixed(p, bcts->sessionid.data,
3188 NFS4_MAX_SESSIONID_LEN);
3189 *p++ = cpu_to_be32(bcts->dir);
3190 /* Upshifting from TCP to RDMA is not supported */
3191 *p++ = cpu_to_be32(0);
3192 return 0;
3193}
3194
3195static __be32
3196nfsd4_encode_close(struct nfsd4_compoundres *resp, __be32 nfserr, struct nfsd4_close *close)
3197{
3198 struct xdr_stream *xdr = &resp->xdr;
3199
3200 return nfsd4_encode_stateid(xdr, &close->cl_stateid);
3201}
3202
3203
3204static __be32
3205nfsd4_encode_commit(struct nfsd4_compoundres *resp, __be32 nfserr, struct nfsd4_commit *commit)
3206{
3207 struct xdr_stream *xdr = &resp->xdr;
3208 __be32 *p;
3209
3210 p = xdr_reserve_space(xdr, NFS4_VERIFIER_SIZE);
3211 if (!p)
3212 return nfserr_resource;
3213 p = xdr_encode_opaque_fixed(p, commit->co_verf.data,
3214 NFS4_VERIFIER_SIZE);
3215 return 0;
3216}
3217
3218static __be32
3219nfsd4_encode_create(struct nfsd4_compoundres *resp, __be32 nfserr, struct nfsd4_create *create)
3220{
3221 struct xdr_stream *xdr = &resp->xdr;
3222 __be32 *p;
3223
3224 p = xdr_reserve_space(xdr, 20);
3225 if (!p)
3226 return nfserr_resource;
3227 encode_cinfo(p, &create->cr_cinfo);
3228 return nfsd4_encode_bitmap(xdr, create->cr_bmval[0],
3229 create->cr_bmval[1], create->cr_bmval[2]);
3230}
3231
3232static __be32
3233nfsd4_encode_getattr(struct nfsd4_compoundres *resp, __be32 nfserr, struct nfsd4_getattr *getattr)
3234{
3235 struct svc_fh *fhp = getattr->ga_fhp;
3236 struct xdr_stream *xdr = &resp->xdr;
3237
3238 return nfsd4_encode_fattr(xdr, fhp, fhp->fh_export, fhp->fh_dentry,
3239 getattr->ga_bmval, resp->rqstp, 0);
3240}
3241
3242static __be32
3243nfsd4_encode_getfh(struct nfsd4_compoundres *resp, __be32 nfserr, struct svc_fh **fhpp)
3244{
3245 struct xdr_stream *xdr = &resp->xdr;
3246 struct svc_fh *fhp = *fhpp;
3247 unsigned int len;
3248 __be32 *p;
3249
3250 len = fhp->fh_handle.fh_size;
3251 p = xdr_reserve_space(xdr, len + 4);
3252 if (!p)
3253 return nfserr_resource;
3254 p = xdr_encode_opaque(p, &fhp->fh_handle.fh_base, len);
3255 return 0;
3256}
3257
3258/*
3259* Including all fields other than the name, a LOCK4denied structure requires
3260* 8(clientid) + 4(namelen) + 8(offset) + 8(length) + 4(type) = 32 bytes.
3261*/
3262static __be32
3263nfsd4_encode_lock_denied(struct xdr_stream *xdr, struct nfsd4_lock_denied *ld)
3264{
3265 struct xdr_netobj *conf = &ld->ld_owner;
3266 __be32 *p;
3267
3268again:
3269 p = xdr_reserve_space(xdr, 32 + XDR_LEN(conf->len));
3270 if (!p) {
3271 /*
3272 * Don't fail to return the result just because we can't
3273 * return the conflicting open:
3274 */
3275 if (conf->len) {
3276 kfree(conf->data);
3277 conf->len = 0;
3278 conf->data = NULL;
3279 goto again;
3280 }
3281 return nfserr_resource;
3282 }
3283 p = xdr_encode_hyper(p, ld->ld_start);
3284 p = xdr_encode_hyper(p, ld->ld_length);
3285 *p++ = cpu_to_be32(ld->ld_type);
3286 if (conf->len) {
3287 p = xdr_encode_opaque_fixed(p, &ld->ld_clientid, 8);
3288 p = xdr_encode_opaque(p, conf->data, conf->len);
3289 kfree(conf->data);
3290 } else { /* non - nfsv4 lock in conflict, no clientid nor owner */
3291 p = xdr_encode_hyper(p, (u64)0); /* clientid */
3292 *p++ = cpu_to_be32(0); /* length of owner name */
3293 }
3294 return nfserr_denied;
3295}
3296
3297static __be32
3298nfsd4_encode_lock(struct nfsd4_compoundres *resp, __be32 nfserr, struct nfsd4_lock *lock)
3299{
3300 struct xdr_stream *xdr = &resp->xdr;
3301
3302 if (!nfserr)
3303 nfserr = nfsd4_encode_stateid(xdr, &lock->lk_resp_stateid);
3304 else if (nfserr == nfserr_denied)
3305 nfserr = nfsd4_encode_lock_denied(xdr, &lock->lk_denied);
3306
3307 return nfserr;
3308}
3309
3310static __be32
3311nfsd4_encode_lockt(struct nfsd4_compoundres *resp, __be32 nfserr, struct nfsd4_lockt *lockt)
3312{
3313 struct xdr_stream *xdr = &resp->xdr;
3314
3315 if (nfserr == nfserr_denied)
3316 nfsd4_encode_lock_denied(xdr, &lockt->lt_denied);
3317 return nfserr;
3318}
3319
3320static __be32
3321nfsd4_encode_locku(struct nfsd4_compoundres *resp, __be32 nfserr, struct nfsd4_locku *locku)
3322{
3323 struct xdr_stream *xdr = &resp->xdr;
3324
3325 return nfsd4_encode_stateid(xdr, &locku->lu_stateid);
3326}
3327
3328
3329static __be32
3330nfsd4_encode_link(struct nfsd4_compoundres *resp, __be32 nfserr, struct nfsd4_link *link)
3331{
3332 struct xdr_stream *xdr = &resp->xdr;
3333 __be32 *p;
3334
3335 p = xdr_reserve_space(xdr, 20);
3336 if (!p)
3337 return nfserr_resource;
3338 p = encode_cinfo(p, &link->li_cinfo);
3339 return 0;
3340}
3341
3342
3343static __be32
3344nfsd4_encode_open(struct nfsd4_compoundres *resp, __be32 nfserr, struct nfsd4_open *open)
3345{
3346 struct xdr_stream *xdr = &resp->xdr;
3347 __be32 *p;
3348
3349 nfserr = nfsd4_encode_stateid(xdr, &open->op_stateid);
3350 if (nfserr)
3351 return nfserr;
3352 p = xdr_reserve_space(xdr, 24);
3353 if (!p)
3354 return nfserr_resource;
3355 p = encode_cinfo(p, &open->op_cinfo);
3356 *p++ = cpu_to_be32(open->op_rflags);
3357
3358 nfserr = nfsd4_encode_bitmap(xdr, open->op_bmval[0], open->op_bmval[1],
3359 open->op_bmval[2]);
3360 if (nfserr)
3361 return nfserr;
3362
3363 p = xdr_reserve_space(xdr, 4);
3364 if (!p)
3365 return nfserr_resource;
3366
3367 *p++ = cpu_to_be32(open->op_delegate_type);
3368 switch (open->op_delegate_type) {
3369 case NFS4_OPEN_DELEGATE_NONE:
3370 break;
3371 case NFS4_OPEN_DELEGATE_READ:
3372 nfserr = nfsd4_encode_stateid(xdr, &open->op_delegate_stateid);
3373 if (nfserr)
3374 return nfserr;
3375 p = xdr_reserve_space(xdr, 20);
3376 if (!p)
3377 return nfserr_resource;
3378 *p++ = cpu_to_be32(open->op_recall);
3379
3380 /*
3381 * TODO: ACE's in delegations
3382 */
3383 *p++ = cpu_to_be32(NFS4_ACE_ACCESS_ALLOWED_ACE_TYPE);
3384 *p++ = cpu_to_be32(0);
3385 *p++ = cpu_to_be32(0);
3386 *p++ = cpu_to_be32(0); /* XXX: is NULL principal ok? */
3387 break;
3388 case NFS4_OPEN_DELEGATE_WRITE:
3389 nfserr = nfsd4_encode_stateid(xdr, &open->op_delegate_stateid);
3390 if (nfserr)
3391 return nfserr;
3392 p = xdr_reserve_space(xdr, 32);
3393 if (!p)
3394 return nfserr_resource;
3395 *p++ = cpu_to_be32(open->op_recall);
3396
3397 /*
3398 * TODO: space_limit's in delegations
3399 */
3400 *p++ = cpu_to_be32(NFS4_LIMIT_SIZE);
3401 *p++ = cpu_to_be32(~(u32)0);
3402 *p++ = cpu_to_be32(~(u32)0);
3403
3404 /*
3405 * TODO: ACE's in delegations
3406 */
3407 *p++ = cpu_to_be32(NFS4_ACE_ACCESS_ALLOWED_ACE_TYPE);
3408 *p++ = cpu_to_be32(0);
3409 *p++ = cpu_to_be32(0);
3410 *p++ = cpu_to_be32(0); /* XXX: is NULL principal ok? */
3411 break;
3412 case NFS4_OPEN_DELEGATE_NONE_EXT: /* 4.1 */
3413 switch (open->op_why_no_deleg) {
3414 case WND4_CONTENTION:
3415 case WND4_RESOURCE:
3416 p = xdr_reserve_space(xdr, 8);
3417 if (!p)
3418 return nfserr_resource;
3419 *p++ = cpu_to_be32(open->op_why_no_deleg);
3420 /* deleg signaling not supported yet: */
3421 *p++ = cpu_to_be32(0);
3422 break;
3423 default:
3424 p = xdr_reserve_space(xdr, 4);
3425 if (!p)
3426 return nfserr_resource;
3427 *p++ = cpu_to_be32(open->op_why_no_deleg);
3428 }
3429 break;
3430 default:
3431 BUG();
3432 }
3433 /* XXX save filehandle here */
3434 return 0;
3435}
3436
3437static __be32
3438nfsd4_encode_open_confirm(struct nfsd4_compoundres *resp, __be32 nfserr, struct nfsd4_open_confirm *oc)
3439{
3440 struct xdr_stream *xdr = &resp->xdr;
3441
3442 return nfsd4_encode_stateid(xdr, &oc->oc_resp_stateid);
3443}
3444
3445static __be32
3446nfsd4_encode_open_downgrade(struct nfsd4_compoundres *resp, __be32 nfserr, struct nfsd4_open_downgrade *od)
3447{
3448 struct xdr_stream *xdr = &resp->xdr;
3449
3450 return nfsd4_encode_stateid(xdr, &od->od_stateid);
3451}
3452
3453static __be32 nfsd4_encode_splice_read(
3454 struct nfsd4_compoundres *resp,
3455 struct nfsd4_read *read,
3456 struct file *file, unsigned long maxcount)
3457{
3458 struct xdr_stream *xdr = &resp->xdr;
3459 struct xdr_buf *buf = xdr->buf;
3460 u32 eof;
3461 long len;
3462 int space_left;
3463 __be32 nfserr;
3464 __be32 *p = xdr->p - 2;
3465
3466 /* Make sure there will be room for padding if needed */
3467 if (xdr->end - xdr->p < 1)
3468 return nfserr_resource;
3469
3470 len = maxcount;
3471 nfserr = nfsd_splice_read(read->rd_rqstp, read->rd_fhp,
3472 file, read->rd_offset, &maxcount, &eof);
3473 read->rd_length = maxcount;
3474 if (nfserr) {
3475 /*
3476 * nfsd_splice_actor may have already messed with the
3477 * page length; reset it so as not to confuse
3478 * xdr_truncate_encode:
3479 */
3480 buf->page_len = 0;
3481 return nfserr;
3482 }
3483
3484 *(p++) = htonl(eof);
3485 *(p++) = htonl(maxcount);
3486
3487 buf->page_len = maxcount;
3488 buf->len += maxcount;
3489 xdr->page_ptr += (buf->page_base + maxcount + PAGE_SIZE - 1)
3490 / PAGE_SIZE;
3491
3492 /* Use rest of head for padding and remaining ops: */
3493 buf->tail[0].iov_base = xdr->p;
3494 buf->tail[0].iov_len = 0;
3495 xdr->iov = buf->tail;
3496 if (maxcount&3) {
3497 int pad = 4 - (maxcount&3);
3498
3499 *(xdr->p++) = 0;
3500
3501 buf->tail[0].iov_base += maxcount&3;
3502 buf->tail[0].iov_len = pad;
3503 buf->len += pad;
3504 }
3505
3506 space_left = min_t(int, (void *)xdr->end - (void *)xdr->p,
3507 buf->buflen - buf->len);
3508 buf->buflen = buf->len + space_left;
3509 xdr->end = (__be32 *)((void *)xdr->end + space_left);
3510
3511 return 0;
3512}
3513
3514static __be32 nfsd4_encode_readv(struct nfsd4_compoundres *resp,
3515 struct nfsd4_read *read,
3516 struct file *file, unsigned long maxcount)
3517{
3518 struct xdr_stream *xdr = &resp->xdr;
3519 u32 eof;
3520 int v;
3521 int starting_len = xdr->buf->len - 8;
3522 long len;
3523 int thislen;
3524 __be32 nfserr;
3525 __be32 tmp;
3526 __be32 *p;
3527 u32 zzz = 0;
3528 int pad;
3529
3530 /*
3531 * svcrdma requires every READ payload to start somewhere
3532 * in xdr->pages.
3533 */
3534 if (xdr->iov == xdr->buf->head) {
3535 xdr->iov = NULL;
3536 xdr->end = xdr->p;
3537 }
3538
3539 len = maxcount;
3540 v = 0;
3541 while (len) {
3542 thislen = min_t(long, len, PAGE_SIZE);
3543 p = xdr_reserve_space(xdr, (thislen+3)&~3);
3544 WARN_ON_ONCE(!p);
3545 resp->rqstp->rq_vec[v].iov_base = p;
3546 resp->rqstp->rq_vec[v].iov_len = thislen;
3547 v++;
3548 len -= thislen;
3549 }
3550 read->rd_vlen = v;
3551
3552 len = maxcount;
3553 nfserr = nfsd_readv(resp->rqstp, read->rd_fhp, file, read->rd_offset,
3554 resp->rqstp->rq_vec, read->rd_vlen, &maxcount,
3555 &eof);
3556 read->rd_length = maxcount;
3557 if (nfserr)
3558 return nfserr;
3559 if (svc_encode_read_payload(resp->rqstp, starting_len + 8, maxcount))
3560 return nfserr_io;
3561 xdr_truncate_encode(xdr, starting_len + 8 + ((maxcount+3)&~3));
3562
3563 tmp = htonl(eof);
3564 write_bytes_to_xdr_buf(xdr->buf, starting_len , &tmp, 4);
3565 tmp = htonl(maxcount);
3566 write_bytes_to_xdr_buf(xdr->buf, starting_len + 4, &tmp, 4);
3567
3568 pad = (maxcount&3) ? 4 - (maxcount&3) : 0;
3569 write_bytes_to_xdr_buf(xdr->buf, starting_len + 8 + maxcount,
3570 &zzz, pad);
3571 return 0;
3572
3573}
3574
3575static __be32
3576nfsd4_encode_read(struct nfsd4_compoundres *resp, __be32 nfserr,
3577 struct nfsd4_read *read)
3578{
3579 unsigned long maxcount;
3580 struct xdr_stream *xdr = &resp->xdr;
3581 struct file *file;
3582 int starting_len = xdr->buf->len;
3583 __be32 *p;
3584
3585 if (nfserr)
3586 return nfserr;
3587 file = read->rd_nf->nf_file;
3588
3589 p = xdr_reserve_space(xdr, 8); /* eof flag and byte count */
3590 if (!p) {
3591 WARN_ON_ONCE(test_bit(RQ_SPLICE_OK, &resp->rqstp->rq_flags));
3592 return nfserr_resource;
3593 }
3594 if (resp->xdr.buf->page_len &&
3595 test_bit(RQ_SPLICE_OK, &resp->rqstp->rq_flags)) {
3596 WARN_ON_ONCE(1);
3597 return nfserr_serverfault;
3598 }
3599 xdr_commit_encode(xdr);
3600
3601 maxcount = svc_max_payload(resp->rqstp);
3602 maxcount = min_t(unsigned long, maxcount,
3603 (xdr->buf->buflen - xdr->buf->len));
3604 maxcount = min_t(unsigned long, maxcount, read->rd_length);
3605
3606 if (file->f_op->splice_read &&
3607 test_bit(RQ_SPLICE_OK, &resp->rqstp->rq_flags))
3608 nfserr = nfsd4_encode_splice_read(resp, read, file, maxcount);
3609 else
3610 nfserr = nfsd4_encode_readv(resp, read, file, maxcount);
3611
3612 if (nfserr)
3613 xdr_truncate_encode(xdr, starting_len);
3614
3615 return nfserr;
3616}
3617
3618static __be32
3619nfsd4_encode_readlink(struct nfsd4_compoundres *resp, __be32 nfserr, struct nfsd4_readlink *readlink)
3620{
3621 int maxcount;
3622 __be32 wire_count;
3623 int zero = 0;
3624 struct xdr_stream *xdr = &resp->xdr;
3625 int length_offset = xdr->buf->len;
3626 __be32 *p;
3627
3628 p = xdr_reserve_space(xdr, 4);
3629 if (!p)
3630 return nfserr_resource;
3631 maxcount = PAGE_SIZE;
3632
3633 p = xdr_reserve_space(xdr, maxcount);
3634 if (!p)
3635 return nfserr_resource;
3636 /*
3637 * XXX: By default, vfs_readlink() will truncate symlinks if they
3638 * would overflow the buffer. Is this kosher in NFSv4? If not, one
3639 * easy fix is: if vfs_readlink() precisely fills the buffer, assume
3640 * that truncation occurred, and return NFS4ERR_RESOURCE.
3641 */
3642 nfserr = nfsd_readlink(readlink->rl_rqstp, readlink->rl_fhp,
3643 (char *)p, &maxcount);
3644 if (nfserr == nfserr_isdir)
3645 nfserr = nfserr_inval;
3646 if (nfserr) {
3647 xdr_truncate_encode(xdr, length_offset);
3648 return nfserr;
3649 }
3650
3651 wire_count = htonl(maxcount);
3652 write_bytes_to_xdr_buf(xdr->buf, length_offset, &wire_count, 4);
3653 xdr_truncate_encode(xdr, length_offset + 4 + ALIGN(maxcount, 4));
3654 if (maxcount & 3)
3655 write_bytes_to_xdr_buf(xdr->buf, length_offset + 4 + maxcount,
3656 &zero, 4 - (maxcount&3));
3657 return 0;
3658}
3659
3660static __be32
3661nfsd4_encode_readdir(struct nfsd4_compoundres *resp, __be32 nfserr, struct nfsd4_readdir *readdir)
3662{
3663 int maxcount;
3664 int bytes_left;
3665 loff_t offset;
3666 __be64 wire_offset;
3667 struct xdr_stream *xdr = &resp->xdr;
3668 int starting_len = xdr->buf->len;
3669 __be32 *p;
3670
3671 p = xdr_reserve_space(xdr, NFS4_VERIFIER_SIZE);
3672 if (!p)
3673 return nfserr_resource;
3674
3675 /* XXX: Following NFSv3, we ignore the READDIR verifier for now. */
3676 *p++ = cpu_to_be32(0);
3677 *p++ = cpu_to_be32(0);
3678 resp->xdr.buf->head[0].iov_len = ((char *)resp->xdr.p)
3679 - (char *)resp->xdr.buf->head[0].iov_base;
3680
3681 /*
3682 * Number of bytes left for directory entries allowing for the
3683 * final 8 bytes of the readdir and a following failed op:
3684 */
3685 bytes_left = xdr->buf->buflen - xdr->buf->len
3686 - COMPOUND_ERR_SLACK_SPACE - 8;
3687 if (bytes_left < 0) {
3688 nfserr = nfserr_resource;
3689 goto err_no_verf;
3690 }
3691 maxcount = svc_max_payload(resp->rqstp);
3692 maxcount = min_t(u32, readdir->rd_maxcount, maxcount);
3693 /*
3694 * Note the rfc defines rd_maxcount as the size of the
3695 * READDIR4resok structure, which includes the verifier above
3696 * and the 8 bytes encoded at the end of this function:
3697 */
3698 if (maxcount < 16) {
3699 nfserr = nfserr_toosmall;
3700 goto err_no_verf;
3701 }
3702 maxcount = min_t(int, maxcount-16, bytes_left);
3703
3704 /* RFC 3530 14.2.24 allows us to ignore dircount when it's 0: */
3705 if (!readdir->rd_dircount)
3706 readdir->rd_dircount = svc_max_payload(resp->rqstp);
3707
3708 readdir->xdr = xdr;
3709 readdir->rd_maxcount = maxcount;
3710 readdir->common.err = 0;
3711 readdir->cookie_offset = 0;
3712
3713 offset = readdir->rd_cookie;
3714 nfserr = nfsd_readdir(readdir->rd_rqstp, readdir->rd_fhp,
3715 &offset,
3716 &readdir->common, nfsd4_encode_dirent);
3717 if (nfserr == nfs_ok &&
3718 readdir->common.err == nfserr_toosmall &&
3719 xdr->buf->len == starting_len + 8) {
3720 /* nothing encoded; which limit did we hit?: */
3721 if (maxcount - 16 < bytes_left)
3722 /* It was the fault of rd_maxcount: */
3723 nfserr = nfserr_toosmall;
3724 else
3725 /* We ran out of buffer space: */
3726 nfserr = nfserr_resource;
3727 }
3728 if (nfserr)
3729 goto err_no_verf;
3730
3731 if (readdir->cookie_offset) {
3732 wire_offset = cpu_to_be64(offset);
3733 write_bytes_to_xdr_buf(xdr->buf, readdir->cookie_offset,
3734 &wire_offset, 8);
3735 }
3736
3737 p = xdr_reserve_space(xdr, 8);
3738 if (!p) {
3739 WARN_ON_ONCE(1);
3740 goto err_no_verf;
3741 }
3742 *p++ = 0; /* no more entries */
3743 *p++ = htonl(readdir->common.err == nfserr_eof);
3744
3745 return 0;
3746err_no_verf:
3747 xdr_truncate_encode(xdr, starting_len);
3748 return nfserr;
3749}
3750
3751static __be32
3752nfsd4_encode_remove(struct nfsd4_compoundres *resp, __be32 nfserr, struct nfsd4_remove *remove)
3753{
3754 struct xdr_stream *xdr = &resp->xdr;
3755 __be32 *p;
3756
3757 p = xdr_reserve_space(xdr, 20);
3758 if (!p)
3759 return nfserr_resource;
3760 p = encode_cinfo(p, &remove->rm_cinfo);
3761 return 0;
3762}
3763
3764static __be32
3765nfsd4_encode_rename(struct nfsd4_compoundres *resp, __be32 nfserr, struct nfsd4_rename *rename)
3766{
3767 struct xdr_stream *xdr = &resp->xdr;
3768 __be32 *p;
3769
3770 p = xdr_reserve_space(xdr, 40);
3771 if (!p)
3772 return nfserr_resource;
3773 p = encode_cinfo(p, &rename->rn_sinfo);
3774 p = encode_cinfo(p, &rename->rn_tinfo);
3775 return 0;
3776}
3777
3778static __be32
3779nfsd4_do_encode_secinfo(struct xdr_stream *xdr, struct svc_export *exp)
3780{
3781 u32 i, nflavs, supported;
3782 struct exp_flavor_info *flavs;
3783 struct exp_flavor_info def_flavs[2];
3784 __be32 *p, *flavorsp;
3785 static bool report = true;
3786
3787 if (exp->ex_nflavors) {
3788 flavs = exp->ex_flavors;
3789 nflavs = exp->ex_nflavors;
3790 } else { /* Handling of some defaults in absence of real secinfo: */
3791 flavs = def_flavs;
3792 if (exp->ex_client->flavour->flavour == RPC_AUTH_UNIX) {
3793 nflavs = 2;
3794 flavs[0].pseudoflavor = RPC_AUTH_UNIX;
3795 flavs[1].pseudoflavor = RPC_AUTH_NULL;
3796 } else if (exp->ex_client->flavour->flavour == RPC_AUTH_GSS) {
3797 nflavs = 1;
3798 flavs[0].pseudoflavor
3799 = svcauth_gss_flavor(exp->ex_client);
3800 } else {
3801 nflavs = 1;
3802 flavs[0].pseudoflavor
3803 = exp->ex_client->flavour->flavour;
3804 }
3805 }
3806
3807 supported = 0;
3808 p = xdr_reserve_space(xdr, 4);
3809 if (!p)
3810 return nfserr_resource;
3811 flavorsp = p++; /* to be backfilled later */
3812
3813 for (i = 0; i < nflavs; i++) {
3814 rpc_authflavor_t pf = flavs[i].pseudoflavor;
3815 struct rpcsec_gss_info info;
3816
3817 if (rpcauth_get_gssinfo(pf, &info) == 0) {
3818 supported++;
3819 p = xdr_reserve_space(xdr, 4 + 4 +
3820 XDR_LEN(info.oid.len) + 4 + 4);
3821 if (!p)
3822 return nfserr_resource;
3823 *p++ = cpu_to_be32(RPC_AUTH_GSS);
3824 p = xdr_encode_opaque(p, info.oid.data, info.oid.len);
3825 *p++ = cpu_to_be32(info.qop);
3826 *p++ = cpu_to_be32(info.service);
3827 } else if (pf < RPC_AUTH_MAXFLAVOR) {
3828 supported++;
3829 p = xdr_reserve_space(xdr, 4);
3830 if (!p)
3831 return nfserr_resource;
3832 *p++ = cpu_to_be32(pf);
3833 } else {
3834 if (report)
3835 pr_warn("NFS: SECINFO: security flavor %u "
3836 "is not supported\n", pf);
3837 }
3838 }
3839
3840 if (nflavs != supported)
3841 report = false;
3842 *flavorsp = htonl(supported);
3843 return 0;
3844}
3845
3846static __be32
3847nfsd4_encode_secinfo(struct nfsd4_compoundres *resp, __be32 nfserr,
3848 struct nfsd4_secinfo *secinfo)
3849{
3850 struct xdr_stream *xdr = &resp->xdr;
3851
3852 return nfsd4_do_encode_secinfo(xdr, secinfo->si_exp);
3853}
3854
3855static __be32
3856nfsd4_encode_secinfo_no_name(struct nfsd4_compoundres *resp, __be32 nfserr,
3857 struct nfsd4_secinfo_no_name *secinfo)
3858{
3859 struct xdr_stream *xdr = &resp->xdr;
3860
3861 return nfsd4_do_encode_secinfo(xdr, secinfo->sin_exp);
3862}
3863
3864/*
3865 * The SETATTR encode routine is special -- it always encodes a bitmap,
3866 * regardless of the error status.
3867 */
3868static __be32
3869nfsd4_encode_setattr(struct nfsd4_compoundres *resp, __be32 nfserr, struct nfsd4_setattr *setattr)
3870{
3871 struct xdr_stream *xdr = &resp->xdr;
3872 __be32 *p;
3873
3874 p = xdr_reserve_space(xdr, 16);
3875 if (!p)
3876 return nfserr_resource;
3877 if (nfserr) {
3878 *p++ = cpu_to_be32(3);
3879 *p++ = cpu_to_be32(0);
3880 *p++ = cpu_to_be32(0);
3881 *p++ = cpu_to_be32(0);
3882 }
3883 else {
3884 *p++ = cpu_to_be32(3);
3885 *p++ = cpu_to_be32(setattr->sa_bmval[0]);
3886 *p++ = cpu_to_be32(setattr->sa_bmval[1]);
3887 *p++ = cpu_to_be32(setattr->sa_bmval[2]);
3888 }
3889 return nfserr;
3890}
3891
3892static __be32
3893nfsd4_encode_setclientid(struct nfsd4_compoundres *resp, __be32 nfserr, struct nfsd4_setclientid *scd)
3894{
3895 struct xdr_stream *xdr = &resp->xdr;
3896 __be32 *p;
3897
3898 if (!nfserr) {
3899 p = xdr_reserve_space(xdr, 8 + NFS4_VERIFIER_SIZE);
3900 if (!p)
3901 return nfserr_resource;
3902 p = xdr_encode_opaque_fixed(p, &scd->se_clientid, 8);
3903 p = xdr_encode_opaque_fixed(p, &scd->se_confirm,
3904 NFS4_VERIFIER_SIZE);
3905 }
3906 else if (nfserr == nfserr_clid_inuse) {
3907 p = xdr_reserve_space(xdr, 8);
3908 if (!p)
3909 return nfserr_resource;
3910 *p++ = cpu_to_be32(0);
3911 *p++ = cpu_to_be32(0);
3912 }
3913 return nfserr;
3914}
3915
3916static __be32
3917nfsd4_encode_write(struct nfsd4_compoundres *resp, __be32 nfserr, struct nfsd4_write *write)
3918{
3919 struct xdr_stream *xdr = &resp->xdr;
3920 __be32 *p;
3921
3922 p = xdr_reserve_space(xdr, 16);
3923 if (!p)
3924 return nfserr_resource;
3925 *p++ = cpu_to_be32(write->wr_bytes_written);
3926 *p++ = cpu_to_be32(write->wr_how_written);
3927 p = xdr_encode_opaque_fixed(p, write->wr_verifier.data,
3928 NFS4_VERIFIER_SIZE);
3929 return 0;
3930}
3931
3932static __be32
3933nfsd4_encode_exchange_id(struct nfsd4_compoundres *resp, __be32 nfserr,
3934 struct nfsd4_exchange_id *exid)
3935{
3936 struct xdr_stream *xdr = &resp->xdr;
3937 __be32 *p;
3938 char *major_id;
3939 char *server_scope;
3940 int major_id_sz;
3941 int server_scope_sz;
3942 uint64_t minor_id = 0;
3943
3944 major_id = utsname()->nodename;
3945 major_id_sz = strlen(major_id);
3946 server_scope = utsname()->nodename;
3947 server_scope_sz = strlen(server_scope);
3948
3949 p = xdr_reserve_space(xdr,
3950 8 /* eir_clientid */ +
3951 4 /* eir_sequenceid */ +
3952 4 /* eir_flags */ +
3953 4 /* spr_how */);
3954 if (!p)
3955 return nfserr_resource;
3956
3957 p = xdr_encode_opaque_fixed(p, &exid->clientid, 8);
3958 *p++ = cpu_to_be32(exid->seqid);
3959 *p++ = cpu_to_be32(exid->flags);
3960
3961 *p++ = cpu_to_be32(exid->spa_how);
3962
3963 switch (exid->spa_how) {
3964 case SP4_NONE:
3965 break;
3966 case SP4_MACH_CRED:
3967 /* spo_must_enforce bitmap: */
3968 nfserr = nfsd4_encode_bitmap(xdr,
3969 exid->spo_must_enforce[0],
3970 exid->spo_must_enforce[1],
3971 exid->spo_must_enforce[2]);
3972 if (nfserr)
3973 return nfserr;
3974 /* spo_must_allow bitmap: */
3975 nfserr = nfsd4_encode_bitmap(xdr,
3976 exid->spo_must_allow[0],
3977 exid->spo_must_allow[1],
3978 exid->spo_must_allow[2]);
3979 if (nfserr)
3980 return nfserr;
3981 break;
3982 default:
3983 WARN_ON_ONCE(1);
3984 }
3985
3986 p = xdr_reserve_space(xdr,
3987 8 /* so_minor_id */ +
3988 4 /* so_major_id.len */ +
3989 (XDR_QUADLEN(major_id_sz) * 4) +
3990 4 /* eir_server_scope.len */ +
3991 (XDR_QUADLEN(server_scope_sz) * 4) +
3992 4 /* eir_server_impl_id.count (0) */);
3993 if (!p)
3994 return nfserr_resource;
3995
3996 /* The server_owner struct */
3997 p = xdr_encode_hyper(p, minor_id); /* Minor id */
3998 /* major id */
3999 p = xdr_encode_opaque(p, major_id, major_id_sz);
4000
4001 /* Server scope */
4002 p = xdr_encode_opaque(p, server_scope, server_scope_sz);
4003
4004 /* Implementation id */
4005 *p++ = cpu_to_be32(0); /* zero length nfs_impl_id4 array */
4006 return 0;
4007}
4008
4009static __be32
4010nfsd4_encode_create_session(struct nfsd4_compoundres *resp, __be32 nfserr,
4011 struct nfsd4_create_session *sess)
4012{
4013 struct xdr_stream *xdr = &resp->xdr;
4014 __be32 *p;
4015
4016 p = xdr_reserve_space(xdr, 24);
4017 if (!p)
4018 return nfserr_resource;
4019 p = xdr_encode_opaque_fixed(p, sess->sessionid.data,
4020 NFS4_MAX_SESSIONID_LEN);
4021 *p++ = cpu_to_be32(sess->seqid);
4022 *p++ = cpu_to_be32(sess->flags);
4023
4024 p = xdr_reserve_space(xdr, 28);
4025 if (!p)
4026 return nfserr_resource;
4027 *p++ = cpu_to_be32(0); /* headerpadsz */
4028 *p++ = cpu_to_be32(sess->fore_channel.maxreq_sz);
4029 *p++ = cpu_to_be32(sess->fore_channel.maxresp_sz);
4030 *p++ = cpu_to_be32(sess->fore_channel.maxresp_cached);
4031 *p++ = cpu_to_be32(sess->fore_channel.maxops);
4032 *p++ = cpu_to_be32(sess->fore_channel.maxreqs);
4033 *p++ = cpu_to_be32(sess->fore_channel.nr_rdma_attrs);
4034
4035 if (sess->fore_channel.nr_rdma_attrs) {
4036 p = xdr_reserve_space(xdr, 4);
4037 if (!p)
4038 return nfserr_resource;
4039 *p++ = cpu_to_be32(sess->fore_channel.rdma_attrs);
4040 }
4041
4042 p = xdr_reserve_space(xdr, 28);
4043 if (!p)
4044 return nfserr_resource;
4045 *p++ = cpu_to_be32(0); /* headerpadsz */
4046 *p++ = cpu_to_be32(sess->back_channel.maxreq_sz);
4047 *p++ = cpu_to_be32(sess->back_channel.maxresp_sz);
4048 *p++ = cpu_to_be32(sess->back_channel.maxresp_cached);
4049 *p++ = cpu_to_be32(sess->back_channel.maxops);
4050 *p++ = cpu_to_be32(sess->back_channel.maxreqs);
4051 *p++ = cpu_to_be32(sess->back_channel.nr_rdma_attrs);
4052
4053 if (sess->back_channel.nr_rdma_attrs) {
4054 p = xdr_reserve_space(xdr, 4);
4055 if (!p)
4056 return nfserr_resource;
4057 *p++ = cpu_to_be32(sess->back_channel.rdma_attrs);
4058 }
4059 return 0;
4060}
4061
4062static __be32
4063nfsd4_encode_sequence(struct nfsd4_compoundres *resp, __be32 nfserr,
4064 struct nfsd4_sequence *seq)
4065{
4066 struct xdr_stream *xdr = &resp->xdr;
4067 __be32 *p;
4068
4069 p = xdr_reserve_space(xdr, NFS4_MAX_SESSIONID_LEN + 20);
4070 if (!p)
4071 return nfserr_resource;
4072 p = xdr_encode_opaque_fixed(p, seq->sessionid.data,
4073 NFS4_MAX_SESSIONID_LEN);
4074 *p++ = cpu_to_be32(seq->seqid);
4075 *p++ = cpu_to_be32(seq->slotid);
4076 /* Note slotid's are numbered from zero: */
4077 *p++ = cpu_to_be32(seq->maxslots - 1); /* sr_highest_slotid */
4078 *p++ = cpu_to_be32(seq->maxslots - 1); /* sr_target_highest_slotid */
4079 *p++ = cpu_to_be32(seq->status_flags);
4080
4081 resp->cstate.data_offset = xdr->buf->len; /* DRC cache data pointer */
4082 return 0;
4083}
4084
4085static __be32
4086nfsd4_encode_test_stateid(struct nfsd4_compoundres *resp, __be32 nfserr,
4087 struct nfsd4_test_stateid *test_stateid)
4088{
4089 struct xdr_stream *xdr = &resp->xdr;
4090 struct nfsd4_test_stateid_id *stateid, *next;
4091 __be32 *p;
4092
4093 p = xdr_reserve_space(xdr, 4 + (4 * test_stateid->ts_num_ids));
4094 if (!p)
4095 return nfserr_resource;
4096 *p++ = htonl(test_stateid->ts_num_ids);
4097
4098 list_for_each_entry_safe(stateid, next, &test_stateid->ts_stateid_list, ts_id_list) {
4099 *p++ = stateid->ts_id_status;
4100 }
4101
4102 return 0;
4103}
4104
4105#ifdef CONFIG_NFSD_PNFS
4106static __be32
4107nfsd4_encode_getdeviceinfo(struct nfsd4_compoundres *resp, __be32 nfserr,
4108 struct nfsd4_getdeviceinfo *gdev)
4109{
4110 struct xdr_stream *xdr = &resp->xdr;
4111 const struct nfsd4_layout_ops *ops;
4112 u32 starting_len = xdr->buf->len, needed_len;
4113 __be32 *p;
4114
4115 p = xdr_reserve_space(xdr, 4);
4116 if (!p)
4117 return nfserr_resource;
4118
4119 *p++ = cpu_to_be32(gdev->gd_layout_type);
4120
4121 ops = nfsd4_layout_ops[gdev->gd_layout_type];
4122 nfserr = ops->encode_getdeviceinfo(xdr, gdev);
4123 if (nfserr) {
4124 /*
4125 * We don't bother to burden the layout drivers with
4126 * enforcing gd_maxcount, just tell the client to
4127 * come back with a bigger buffer if it's not enough.
4128 */
4129 if (xdr->buf->len + 4 > gdev->gd_maxcount)
4130 goto toosmall;
4131 return nfserr;
4132 }
4133
4134 if (gdev->gd_notify_types) {
4135 p = xdr_reserve_space(xdr, 4 + 4);
4136 if (!p)
4137 return nfserr_resource;
4138 *p++ = cpu_to_be32(1); /* bitmap length */
4139 *p++ = cpu_to_be32(gdev->gd_notify_types);
4140 } else {
4141 p = xdr_reserve_space(xdr, 4);
4142 if (!p)
4143 return nfserr_resource;
4144 *p++ = 0;
4145 }
4146
4147 return 0;
4148toosmall:
4149 dprintk("%s: maxcount too small\n", __func__);
4150 needed_len = xdr->buf->len + 4 /* notifications */;
4151 xdr_truncate_encode(xdr, starting_len);
4152 p = xdr_reserve_space(xdr, 4);
4153 if (!p)
4154 return nfserr_resource;
4155 *p++ = cpu_to_be32(needed_len);
4156 return nfserr_toosmall;
4157}
4158
4159static __be32
4160nfsd4_encode_layoutget(struct nfsd4_compoundres *resp, __be32 nfserr,
4161 struct nfsd4_layoutget *lgp)
4162{
4163 struct xdr_stream *xdr = &resp->xdr;
4164 const struct nfsd4_layout_ops *ops;
4165 __be32 *p;
4166
4167 p = xdr_reserve_space(xdr, 36 + sizeof(stateid_opaque_t));
4168 if (!p)
4169 return nfserr_resource;
4170
4171 *p++ = cpu_to_be32(1); /* we always set return-on-close */
4172 *p++ = cpu_to_be32(lgp->lg_sid.si_generation);
4173 p = xdr_encode_opaque_fixed(p, &lgp->lg_sid.si_opaque,
4174 sizeof(stateid_opaque_t));
4175
4176 *p++ = cpu_to_be32(1); /* we always return a single layout */
4177 p = xdr_encode_hyper(p, lgp->lg_seg.offset);
4178 p = xdr_encode_hyper(p, lgp->lg_seg.length);
4179 *p++ = cpu_to_be32(lgp->lg_seg.iomode);
4180 *p++ = cpu_to_be32(lgp->lg_layout_type);
4181
4182 ops = nfsd4_layout_ops[lgp->lg_layout_type];
4183 return ops->encode_layoutget(xdr, lgp);
4184}
4185
4186static __be32
4187nfsd4_encode_layoutcommit(struct nfsd4_compoundres *resp, __be32 nfserr,
4188 struct nfsd4_layoutcommit *lcp)
4189{
4190 struct xdr_stream *xdr = &resp->xdr;
4191 __be32 *p;
4192
4193 p = xdr_reserve_space(xdr, 4);
4194 if (!p)
4195 return nfserr_resource;
4196 *p++ = cpu_to_be32(lcp->lc_size_chg);
4197 if (lcp->lc_size_chg) {
4198 p = xdr_reserve_space(xdr, 8);
4199 if (!p)
4200 return nfserr_resource;
4201 p = xdr_encode_hyper(p, lcp->lc_newsize);
4202 }
4203
4204 return 0;
4205}
4206
4207static __be32
4208nfsd4_encode_layoutreturn(struct nfsd4_compoundres *resp, __be32 nfserr,
4209 struct nfsd4_layoutreturn *lrp)
4210{
4211 struct xdr_stream *xdr = &resp->xdr;
4212 __be32 *p;
4213
4214 p = xdr_reserve_space(xdr, 4);
4215 if (!p)
4216 return nfserr_resource;
4217 *p++ = cpu_to_be32(lrp->lrs_present);
4218 if (lrp->lrs_present)
4219 return nfsd4_encode_stateid(xdr, &lrp->lr_sid);
4220 return 0;
4221}
4222#endif /* CONFIG_NFSD_PNFS */
4223
4224static __be32
4225nfsd42_encode_write_res(struct nfsd4_compoundres *resp,
4226 struct nfsd42_write_res *write, bool sync)
4227{
4228 __be32 *p;
4229 p = xdr_reserve_space(&resp->xdr, 4);
4230 if (!p)
4231 return nfserr_resource;
4232
4233 if (sync)
4234 *p++ = cpu_to_be32(0);
4235 else {
4236 __be32 nfserr;
4237 *p++ = cpu_to_be32(1);
4238 nfserr = nfsd4_encode_stateid(&resp->xdr, &write->cb_stateid);
4239 if (nfserr)
4240 return nfserr;
4241 }
4242 p = xdr_reserve_space(&resp->xdr, 8 + 4 + NFS4_VERIFIER_SIZE);
4243 if (!p)
4244 return nfserr_resource;
4245
4246 p = xdr_encode_hyper(p, write->wr_bytes_written);
4247 *p++ = cpu_to_be32(write->wr_stable_how);
4248 p = xdr_encode_opaque_fixed(p, write->wr_verifier.data,
4249 NFS4_VERIFIER_SIZE);
4250 return nfs_ok;
4251}
4252
4253static __be32
4254nfsd4_encode_copy(struct nfsd4_compoundres *resp, __be32 nfserr,
4255 struct nfsd4_copy *copy)
4256{
4257 __be32 *p;
4258
4259 nfserr = nfsd42_encode_write_res(resp, &copy->cp_res,
4260 copy->cp_synchronous);
4261 if (nfserr)
4262 return nfserr;
4263
4264 p = xdr_reserve_space(&resp->xdr, 4 + 4);
4265 *p++ = xdr_one; /* cr_consecutive */
4266 *p++ = cpu_to_be32(copy->cp_synchronous);
4267 return 0;
4268}
4269
4270static __be32
4271nfsd4_encode_offload_status(struct nfsd4_compoundres *resp, __be32 nfserr,
4272 struct nfsd4_offload_status *os)
4273{
4274 struct xdr_stream *xdr = &resp->xdr;
4275 __be32 *p;
4276
4277 p = xdr_reserve_space(xdr, 8 + 4);
4278 if (!p)
4279 return nfserr_resource;
4280 p = xdr_encode_hyper(p, os->count);
4281 *p++ = cpu_to_be32(0);
4282
4283 return nfserr;
4284}
4285
4286static __be32
4287nfsd4_encode_seek(struct nfsd4_compoundres *resp, __be32 nfserr,
4288 struct nfsd4_seek *seek)
4289{
4290 __be32 *p;
4291
4292 p = xdr_reserve_space(&resp->xdr, 4 + 8);
4293 *p++ = cpu_to_be32(seek->seek_eof);
4294 p = xdr_encode_hyper(p, seek->seek_pos);
4295
4296 return 0;
4297}
4298
4299static __be32
4300nfsd4_encode_noop(struct nfsd4_compoundres *resp, __be32 nfserr, void *p)
4301{
4302 return nfserr;
4303}
4304
4305typedef __be32(* nfsd4_enc)(struct nfsd4_compoundres *, __be32, void *);
4306
4307/*
4308 * Note: nfsd4_enc_ops vector is shared for v4.0 and v4.1
4309 * since we don't need to filter out obsolete ops as this is
4310 * done in the decoding phase.
4311 */
4312static const nfsd4_enc nfsd4_enc_ops[] = {
4313 [OP_ACCESS] = (nfsd4_enc)nfsd4_encode_access,
4314 [OP_CLOSE] = (nfsd4_enc)nfsd4_encode_close,
4315 [OP_COMMIT] = (nfsd4_enc)nfsd4_encode_commit,
4316 [OP_CREATE] = (nfsd4_enc)nfsd4_encode_create,
4317 [OP_DELEGPURGE] = (nfsd4_enc)nfsd4_encode_noop,
4318 [OP_DELEGRETURN] = (nfsd4_enc)nfsd4_encode_noop,
4319 [OP_GETATTR] = (nfsd4_enc)nfsd4_encode_getattr,
4320 [OP_GETFH] = (nfsd4_enc)nfsd4_encode_getfh,
4321 [OP_LINK] = (nfsd4_enc)nfsd4_encode_link,
4322 [OP_LOCK] = (nfsd4_enc)nfsd4_encode_lock,
4323 [OP_LOCKT] = (nfsd4_enc)nfsd4_encode_lockt,
4324 [OP_LOCKU] = (nfsd4_enc)nfsd4_encode_locku,
4325 [OP_LOOKUP] = (nfsd4_enc)nfsd4_encode_noop,
4326 [OP_LOOKUPP] = (nfsd4_enc)nfsd4_encode_noop,
4327 [OP_NVERIFY] = (nfsd4_enc)nfsd4_encode_noop,
4328 [OP_OPEN] = (nfsd4_enc)nfsd4_encode_open,
4329 [OP_OPENATTR] = (nfsd4_enc)nfsd4_encode_noop,
4330 [OP_OPEN_CONFIRM] = (nfsd4_enc)nfsd4_encode_open_confirm,
4331 [OP_OPEN_DOWNGRADE] = (nfsd4_enc)nfsd4_encode_open_downgrade,
4332 [OP_PUTFH] = (nfsd4_enc)nfsd4_encode_noop,
4333 [OP_PUTPUBFH] = (nfsd4_enc)nfsd4_encode_noop,
4334 [OP_PUTROOTFH] = (nfsd4_enc)nfsd4_encode_noop,
4335 [OP_READ] = (nfsd4_enc)nfsd4_encode_read,
4336 [OP_READDIR] = (nfsd4_enc)nfsd4_encode_readdir,
4337 [OP_READLINK] = (nfsd4_enc)nfsd4_encode_readlink,
4338 [OP_REMOVE] = (nfsd4_enc)nfsd4_encode_remove,
4339 [OP_RENAME] = (nfsd4_enc)nfsd4_encode_rename,
4340 [OP_RENEW] = (nfsd4_enc)nfsd4_encode_noop,
4341 [OP_RESTOREFH] = (nfsd4_enc)nfsd4_encode_noop,
4342 [OP_SAVEFH] = (nfsd4_enc)nfsd4_encode_noop,
4343 [OP_SECINFO] = (nfsd4_enc)nfsd4_encode_secinfo,
4344 [OP_SETATTR] = (nfsd4_enc)nfsd4_encode_setattr,
4345 [OP_SETCLIENTID] = (nfsd4_enc)nfsd4_encode_setclientid,
4346 [OP_SETCLIENTID_CONFIRM] = (nfsd4_enc)nfsd4_encode_noop,
4347 [OP_VERIFY] = (nfsd4_enc)nfsd4_encode_noop,
4348 [OP_WRITE] = (nfsd4_enc)nfsd4_encode_write,
4349 [OP_RELEASE_LOCKOWNER] = (nfsd4_enc)nfsd4_encode_noop,
4350
4351 /* NFSv4.1 operations */
4352 [OP_BACKCHANNEL_CTL] = (nfsd4_enc)nfsd4_encode_noop,
4353 [OP_BIND_CONN_TO_SESSION] = (nfsd4_enc)nfsd4_encode_bind_conn_to_session,
4354 [OP_EXCHANGE_ID] = (nfsd4_enc)nfsd4_encode_exchange_id,
4355 [OP_CREATE_SESSION] = (nfsd4_enc)nfsd4_encode_create_session,
4356 [OP_DESTROY_SESSION] = (nfsd4_enc)nfsd4_encode_noop,
4357 [OP_FREE_STATEID] = (nfsd4_enc)nfsd4_encode_noop,
4358 [OP_GET_DIR_DELEGATION] = (nfsd4_enc)nfsd4_encode_noop,
4359#ifdef CONFIG_NFSD_PNFS
4360 [OP_GETDEVICEINFO] = (nfsd4_enc)nfsd4_encode_getdeviceinfo,
4361 [OP_GETDEVICELIST] = (nfsd4_enc)nfsd4_encode_noop,
4362 [OP_LAYOUTCOMMIT] = (nfsd4_enc)nfsd4_encode_layoutcommit,
4363 [OP_LAYOUTGET] = (nfsd4_enc)nfsd4_encode_layoutget,
4364 [OP_LAYOUTRETURN] = (nfsd4_enc)nfsd4_encode_layoutreturn,
4365#else
4366 [OP_GETDEVICEINFO] = (nfsd4_enc)nfsd4_encode_noop,
4367 [OP_GETDEVICELIST] = (nfsd4_enc)nfsd4_encode_noop,
4368 [OP_LAYOUTCOMMIT] = (nfsd4_enc)nfsd4_encode_noop,
4369 [OP_LAYOUTGET] = (nfsd4_enc)nfsd4_encode_noop,
4370 [OP_LAYOUTRETURN] = (nfsd4_enc)nfsd4_encode_noop,
4371#endif
4372 [OP_SECINFO_NO_NAME] = (nfsd4_enc)nfsd4_encode_secinfo_no_name,
4373 [OP_SEQUENCE] = (nfsd4_enc)nfsd4_encode_sequence,
4374 [OP_SET_SSV] = (nfsd4_enc)nfsd4_encode_noop,
4375 [OP_TEST_STATEID] = (nfsd4_enc)nfsd4_encode_test_stateid,
4376 [OP_WANT_DELEGATION] = (nfsd4_enc)nfsd4_encode_noop,
4377 [OP_DESTROY_CLIENTID] = (nfsd4_enc)nfsd4_encode_noop,
4378 [OP_RECLAIM_COMPLETE] = (nfsd4_enc)nfsd4_encode_noop,
4379
4380 /* NFSv4.2 operations */
4381 [OP_ALLOCATE] = (nfsd4_enc)nfsd4_encode_noop,
4382 [OP_COPY] = (nfsd4_enc)nfsd4_encode_copy,
4383 [OP_COPY_NOTIFY] = (nfsd4_enc)nfsd4_encode_noop,
4384 [OP_DEALLOCATE] = (nfsd4_enc)nfsd4_encode_noop,
4385 [OP_IO_ADVISE] = (nfsd4_enc)nfsd4_encode_noop,
4386 [OP_LAYOUTERROR] = (nfsd4_enc)nfsd4_encode_noop,
4387 [OP_LAYOUTSTATS] = (nfsd4_enc)nfsd4_encode_noop,
4388 [OP_OFFLOAD_CANCEL] = (nfsd4_enc)nfsd4_encode_noop,
4389 [OP_OFFLOAD_STATUS] = (nfsd4_enc)nfsd4_encode_offload_status,
4390 [OP_READ_PLUS] = (nfsd4_enc)nfsd4_encode_noop,
4391 [OP_SEEK] = (nfsd4_enc)nfsd4_encode_seek,
4392 [OP_WRITE_SAME] = (nfsd4_enc)nfsd4_encode_noop,
4393 [OP_CLONE] = (nfsd4_enc)nfsd4_encode_noop,
4394};
4395
4396/*
4397 * Calculate whether we still have space to encode repsize bytes.
4398 * There are two considerations:
4399 * - For NFS versions >=4.1, the size of the reply must stay within
4400 * session limits
4401 * - For all NFS versions, we must stay within limited preallocated
4402 * buffer space.
4403 *
4404 * This is called before the operation is processed, so can only provide
4405 * an upper estimate. For some nonidempotent operations (such as
4406 * getattr), it's not necessarily a problem if that estimate is wrong,
4407 * as we can fail it after processing without significant side effects.
4408 */
4409__be32 nfsd4_check_resp_size(struct nfsd4_compoundres *resp, u32 respsize)
4410{
4411 struct xdr_buf *buf = &resp->rqstp->rq_res;
4412 struct nfsd4_slot *slot = resp->cstate.slot;
4413
4414 if (buf->len + respsize <= buf->buflen)
4415 return nfs_ok;
4416 if (!nfsd4_has_session(&resp->cstate))
4417 return nfserr_resource;
4418 if (slot->sl_flags & NFSD4_SLOT_CACHETHIS) {
4419 WARN_ON_ONCE(1);
4420 return nfserr_rep_too_big_to_cache;
4421 }
4422 return nfserr_rep_too_big;
4423}
4424
4425void
4426nfsd4_encode_operation(struct nfsd4_compoundres *resp, struct nfsd4_op *op)
4427{
4428 struct xdr_stream *xdr = &resp->xdr;
4429 struct nfs4_stateowner *so = resp->cstate.replay_owner;
4430 struct svc_rqst *rqstp = resp->rqstp;
4431 const struct nfsd4_operation *opdesc = op->opdesc;
4432 int post_err_offset;
4433 nfsd4_enc encoder;
4434 __be32 *p;
4435
4436 p = xdr_reserve_space(xdr, 8);
4437 if (!p) {
4438 WARN_ON_ONCE(1);
4439 return;
4440 }
4441 *p++ = cpu_to_be32(op->opnum);
4442 post_err_offset = xdr->buf->len;
4443
4444 if (op->opnum == OP_ILLEGAL)
4445 goto status;
4446 if (op->status && opdesc &&
4447 !(opdesc->op_flags & OP_NONTRIVIAL_ERROR_ENCODE))
4448 goto status;
4449 BUG_ON(op->opnum < 0 || op->opnum >= ARRAY_SIZE(nfsd4_enc_ops) ||
4450 !nfsd4_enc_ops[op->opnum]);
4451 encoder = nfsd4_enc_ops[op->opnum];
4452 op->status = encoder(resp, op->status, &op->u);
4453 if (opdesc && opdesc->op_release)
4454 opdesc->op_release(&op->u);
4455 xdr_commit_encode(xdr);
4456
4457 /* nfsd4_check_resp_size guarantees enough room for error status */
4458 if (!op->status) {
4459 int space_needed = 0;
4460 if (!nfsd4_last_compound_op(rqstp))
4461 space_needed = COMPOUND_ERR_SLACK_SPACE;
4462 op->status = nfsd4_check_resp_size(resp, space_needed);
4463 }
4464 if (op->status == nfserr_resource && nfsd4_has_session(&resp->cstate)) {
4465 struct nfsd4_slot *slot = resp->cstate.slot;
4466
4467 if (slot->sl_flags & NFSD4_SLOT_CACHETHIS)
4468 op->status = nfserr_rep_too_big_to_cache;
4469 else
4470 op->status = nfserr_rep_too_big;
4471 }
4472 if (op->status == nfserr_resource ||
4473 op->status == nfserr_rep_too_big ||
4474 op->status == nfserr_rep_too_big_to_cache) {
4475 /*
4476 * The operation may have already been encoded or
4477 * partially encoded. No op returns anything additional
4478 * in the case of one of these three errors, so we can
4479 * just truncate back to after the status. But it's a
4480 * bug if we had to do this on a non-idempotent op:
4481 */
4482 warn_on_nonidempotent_op(op);
4483 xdr_truncate_encode(xdr, post_err_offset);
4484 }
4485 if (so) {
4486 int len = xdr->buf->len - post_err_offset;
4487
4488 so->so_replay.rp_status = op->status;
4489 so->so_replay.rp_buflen = len;
4490 read_bytes_from_xdr_buf(xdr->buf, post_err_offset,
4491 so->so_replay.rp_buf, len);
4492 }
4493status:
4494 /* Note that op->status is already in network byte order: */
4495 write_bytes_to_xdr_buf(xdr->buf, post_err_offset - 4, &op->status, 4);
4496}
4497
4498/*
4499 * Encode the reply stored in the stateowner reply cache
4500 *
4501 * XDR note: do not encode rp->rp_buflen: the buffer contains the
4502 * previously sent already encoded operation.
4503 */
4504void
4505nfsd4_encode_replay(struct xdr_stream *xdr, struct nfsd4_op *op)
4506{
4507 __be32 *p;
4508 struct nfs4_replay *rp = op->replay;
4509
4510 BUG_ON(!rp);
4511
4512 p = xdr_reserve_space(xdr, 8 + rp->rp_buflen);
4513 if (!p) {
4514 WARN_ON_ONCE(1);
4515 return;
4516 }
4517 *p++ = cpu_to_be32(op->opnum);
4518 *p++ = rp->rp_status; /* already xdr'ed */
4519
4520 p = xdr_encode_opaque_fixed(p, rp->rp_buf, rp->rp_buflen);
4521}
4522
4523int
4524nfs4svc_encode_voidres(struct svc_rqst *rqstp, __be32 *p)
4525{
4526 return xdr_ressize_check(rqstp, p);
4527}
4528
4529void nfsd4_release_compoundargs(struct svc_rqst *rqstp)
4530{
4531 struct nfsd4_compoundargs *args = rqstp->rq_argp;
4532
4533 if (args->ops != args->iops) {
4534 kfree(args->ops);
4535 args->ops = args->iops;
4536 }
4537 kfree(args->tmpp);
4538 args->tmpp = NULL;
4539 while (args->to_free) {
4540 struct svcxdr_tmpbuf *tb = args->to_free;
4541 args->to_free = tb->next;
4542 kfree(tb);
4543 }
4544}
4545
4546int
4547nfs4svc_decode_compoundargs(struct svc_rqst *rqstp, __be32 *p)
4548{
4549 struct nfsd4_compoundargs *args = rqstp->rq_argp;
4550
4551 if (rqstp->rq_arg.head[0].iov_len % 4) {
4552 /* client is nuts */
4553 dprintk("%s: compound not properly padded! (peeraddr=%pISc xid=0x%x)",
4554 __func__, svc_addr(rqstp), be32_to_cpu(rqstp->rq_xid));
4555 return 0;
4556 }
4557 args->p = p;
4558 args->end = rqstp->rq_arg.head[0].iov_base + rqstp->rq_arg.head[0].iov_len;
4559 args->pagelist = rqstp->rq_arg.pages;
4560 args->pagelen = rqstp->rq_arg.page_len;
4561 args->tail = false;
4562 args->tmpp = NULL;
4563 args->to_free = NULL;
4564 args->ops = args->iops;
4565 args->rqstp = rqstp;
4566
4567 return !nfsd4_decode_compound(args);
4568}
4569
4570int
4571nfs4svc_encode_compoundres(struct svc_rqst *rqstp, __be32 *p)
4572{
4573 /*
4574 * All that remains is to write the tag and operation count...
4575 */
4576 struct nfsd4_compoundres *resp = rqstp->rq_resp;
4577 struct xdr_buf *buf = resp->xdr.buf;
4578
4579 WARN_ON_ONCE(buf->len != buf->head[0].iov_len + buf->page_len +
4580 buf->tail[0].iov_len);
4581
4582 rqstp->rq_next_page = resp->xdr.page_ptr + 1;
4583
4584 p = resp->tagp;
4585 *p++ = htonl(resp->taglen);
4586 memcpy(p, resp->tag, resp->taglen);
4587 p += XDR_QUADLEN(resp->taglen);
4588 *p++ = htonl(resp->opcnt);
4589
4590 nfsd4_sequence_done(resp);
4591 return 1;
4592}
4593
4594/*
4595 * Local variables:
4596 * c-basic-offset: 8
4597 * End:
4598 */