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