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