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