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