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