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