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