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