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