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