nfsd: use C99 struct initializers
[kernel.git] / fs / nfsd / nfs4xdr.c
1 /*
2  *  Server-side XDR for NFSv4
3  *
4  *  Copyright (c) 2002 The Regents of the University of Michigan.
5  *  All rights reserved.
6  *
7  *  Kendrick Smith <kmsmith@umich.edu>
8  *  Andy Adamson   <andros@umich.edu>
9  *
10  *  Redistribution and use in source and binary forms, with or without
11  *  modification, are permitted provided that the following conditions
12  *  are met:
13  *
14  *  1. Redistributions of source code must retain the above copyright
15  *     notice, this list of conditions and the following disclaimer.
16  *  2. Redistributions in binary form must reproduce the above copyright
17  *     notice, this list of conditions and the following disclaimer in the
18  *     documentation and/or other materials provided with the distribution.
19  *  3. Neither the name of the University nor the names of its
20  *     contributors may be used to endorse or promote products derived
21  *     from this software without specific prior written permission.
22  *
23  *  THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESS OR IMPLIED
24  *  WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
25  *  MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
26  *  DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
27  *  FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
28  *  CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
29  *  SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR
30  *  BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
31  *  LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
32  *  NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
33  *  SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
34  *
35  * TODO: Neil Brown made the following observation:  We currently
36  * initially reserve NFSD_BUFSIZE space on the transmit queue and
37  * never release any of that until the request is complete.
38  * It would be good to calculate a new maximum response size while
39  * decoding the COMPOUND, and call svc_reserve with this number
40  * at the end of nfs4svc_decode_compoundargs.
41  */
42
43 #include <linux/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
1494 static void write32(__be32 **p, u32 n)
1495 {
1496         *(*p)++ = n;
1497 }
1498
1499 static void write64(__be32 **p, u64 n)
1500 {
1501         write32(p, (u32)(n >> 32));
1502         write32(p, (u32)n);
1503 }
1504
1505 static void write_change(__be32 **p, struct kstat *stat, struct inode *inode)
1506 {
1507         if (IS_I_VERSION(inode)) {
1508                 write64(p, inode->i_version);
1509         } else {
1510                 write32(p, stat->ctime.tv_sec);
1511                 write32(p, stat->ctime.tv_nsec);
1512         }
1513 }
1514
1515 static void write_cinfo(__be32 **p, struct nfsd4_change_info *c)
1516 {
1517         write32(p, c->atomic);
1518         if (c->change_supported) {
1519                 write64(p, c->before_change);
1520                 write64(p, c->after_change);
1521         } else {
1522                 write32(p, c->before_ctime_sec);
1523                 write32(p, c->before_ctime_nsec);
1524                 write32(p, c->after_ctime_sec);
1525                 write32(p, c->after_ctime_nsec);
1526         }
1527 }
1528
1529 #define RESERVE_SPACE(nbytes)   do {                            \
1530         p = resp->p;                                            \
1531         BUG_ON(p + XDR_QUADLEN(nbytes) > resp->end);            \
1532 } while (0)
1533 #define ADJUST_ARGS()           resp->p = p
1534
1535 /*
1536  * Header routine to setup seqid operation replay cache
1537  */
1538 #define ENCODE_SEQID_OP_HEAD                                    \
1539         __be32 *save;                                           \
1540                                                                 \
1541         save = resp->p;
1542
1543 /*
1544  * Routine for encoding the result of a "seqid-mutating" NFSv4 operation.  This
1545  * is where sequence id's are incremented, and the replay cache is filled.
1546  * Note that we increment sequence id's here, at the last moment, so we're sure
1547  * we know whether the error to be returned is a sequence id mutating error.
1548  */
1549
1550 #define ENCODE_SEQID_OP_TAIL(stateowner) do {                   \
1551         if (seqid_mutating_err(nfserr) && stateowner) {         \
1552                 stateowner->so_seqid++;                         \
1553                 stateowner->so_replay.rp_status = nfserr;       \
1554                 stateowner->so_replay.rp_buflen =               \
1555                           (((char *)(resp)->p - (char *)save)); \
1556                 memcpy(stateowner->so_replay.rp_buf, save,      \
1557                         stateowner->so_replay.rp_buflen);       \
1558         } } while (0);
1559
1560 /* Encode as an array of strings the string given with components
1561  * seperated @sep.
1562  */
1563 static __be32 nfsd4_encode_components(char sep, char *components,
1564                                    __be32 **pp, int *buflen)
1565 {
1566         __be32 *p = *pp;
1567         __be32 *countp = p;
1568         int strlen, count=0;
1569         char *str, *end;
1570
1571         dprintk("nfsd4_encode_components(%s)\n", components);
1572         if ((*buflen -= 4) < 0)
1573                 return nfserr_resource;
1574         WRITE32(0); /* We will fill this in with @count later */
1575         end = str = components;
1576         while (*end) {
1577                 for (; *end && (*end != sep); end++)
1578                         ; /* Point to end of component */
1579                 strlen = end - str;
1580                 if (strlen) {
1581                         if ((*buflen -= ((XDR_QUADLEN(strlen) << 2) + 4)) < 0)
1582                                 return nfserr_resource;
1583                         WRITE32(strlen);
1584                         WRITEMEM(str, strlen);
1585                         count++;
1586                 }
1587                 else
1588                         end++;
1589                 str = end;
1590         }
1591         *pp = p;
1592         p = countp;
1593         WRITE32(count);
1594         return 0;
1595 }
1596
1597 /*
1598  * encode a location element of a fs_locations structure
1599  */
1600 static __be32 nfsd4_encode_fs_location4(struct nfsd4_fs_location *location,
1601                                     __be32 **pp, int *buflen)
1602 {
1603         __be32 status;
1604         __be32 *p = *pp;
1605
1606         status = nfsd4_encode_components(':', location->hosts, &p, buflen);
1607         if (status)
1608                 return status;
1609         status = nfsd4_encode_components('/', location->path, &p, buflen);
1610         if (status)
1611                 return status;
1612         *pp = p;
1613         return 0;
1614 }
1615
1616 /*
1617  * Return the path to an export point in the pseudo filesystem namespace
1618  * Returned string is safe to use as long as the caller holds a reference
1619  * to @exp.
1620  */
1621 static char *nfsd4_path(struct svc_rqst *rqstp, struct svc_export *exp, __be32 *stat)
1622 {
1623         struct svc_fh tmp_fh;
1624         char *path, *rootpath;
1625
1626         fh_init(&tmp_fh, NFS4_FHSIZE);
1627         *stat = exp_pseudoroot(rqstp, &tmp_fh);
1628         if (*stat)
1629                 return NULL;
1630         rootpath = tmp_fh.fh_export->ex_pathname;
1631
1632         path = exp->ex_pathname;
1633
1634         if (strncmp(path, rootpath, strlen(rootpath))) {
1635                 dprintk("nfsd: fs_locations failed;"
1636                         "%s is not contained in %s\n", path, rootpath);
1637                 *stat = nfserr_notsupp;
1638                 return NULL;
1639         }
1640
1641         return path + strlen(rootpath);
1642 }
1643
1644 /*
1645  *  encode a fs_locations structure
1646  */
1647 static __be32 nfsd4_encode_fs_locations(struct svc_rqst *rqstp,
1648                                      struct svc_export *exp,
1649                                      __be32 **pp, int *buflen)
1650 {
1651         __be32 status;
1652         int i;
1653         __be32 *p = *pp;
1654         struct nfsd4_fs_locations *fslocs = &exp->ex_fslocs;
1655         char *root = nfsd4_path(rqstp, exp, &status);
1656
1657         if (status)
1658                 return status;
1659         status = nfsd4_encode_components('/', root, &p, buflen);
1660         if (status)
1661                 return status;
1662         if ((*buflen -= 4) < 0)
1663                 return nfserr_resource;
1664         WRITE32(fslocs->locations_count);
1665         for (i=0; i<fslocs->locations_count; i++) {
1666                 status = nfsd4_encode_fs_location4(&fslocs->locations[i],
1667                                                    &p, buflen);
1668                 if (status)
1669                         return status;
1670         }
1671         *pp = p;
1672         return 0;
1673 }
1674
1675 static u32 nfs4_ftypes[16] = {
1676         NF4BAD,  NF4FIFO, NF4CHR, NF4BAD,
1677         NF4DIR,  NF4BAD,  NF4BLK, NF4BAD,
1678         NF4REG,  NF4BAD,  NF4LNK, NF4BAD,
1679         NF4SOCK, NF4BAD,  NF4LNK, NF4BAD,
1680 };
1681
1682 static __be32
1683 nfsd4_encode_name(struct svc_rqst *rqstp, int whotype, uid_t id, int group,
1684                         __be32 **p, int *buflen)
1685 {
1686         int status;
1687
1688         if (*buflen < (XDR_QUADLEN(IDMAP_NAMESZ) << 2) + 4)
1689                 return nfserr_resource;
1690         if (whotype != NFS4_ACL_WHO_NAMED)
1691                 status = nfs4_acl_write_who(whotype, (u8 *)(*p + 1));
1692         else if (group)
1693                 status = nfsd_map_gid_to_name(rqstp, id, (u8 *)(*p + 1));
1694         else
1695                 status = nfsd_map_uid_to_name(rqstp, id, (u8 *)(*p + 1));
1696         if (status < 0)
1697                 return nfserrno(status);
1698         *p = xdr_encode_opaque(*p, NULL, status);
1699         *buflen -= (XDR_QUADLEN(status) << 2) + 4;
1700         BUG_ON(*buflen < 0);
1701         return 0;
1702 }
1703
1704 static inline __be32
1705 nfsd4_encode_user(struct svc_rqst *rqstp, uid_t uid, __be32 **p, int *buflen)
1706 {
1707         return nfsd4_encode_name(rqstp, NFS4_ACL_WHO_NAMED, uid, 0, p, buflen);
1708 }
1709
1710 static inline __be32
1711 nfsd4_encode_group(struct svc_rqst *rqstp, uid_t gid, __be32 **p, int *buflen)
1712 {
1713         return nfsd4_encode_name(rqstp, NFS4_ACL_WHO_NAMED, gid, 1, p, buflen);
1714 }
1715
1716 static inline __be32
1717 nfsd4_encode_aclname(struct svc_rqst *rqstp, int whotype, uid_t id, int group,
1718                 __be32 **p, int *buflen)
1719 {
1720         return nfsd4_encode_name(rqstp, whotype, id, group, p, buflen);
1721 }
1722
1723 #define WORD0_ABSENT_FS_ATTRS (FATTR4_WORD0_FS_LOCATIONS | FATTR4_WORD0_FSID | \
1724                               FATTR4_WORD0_RDATTR_ERROR)
1725 #define WORD1_ABSENT_FS_ATTRS FATTR4_WORD1_MOUNTED_ON_FILEID
1726
1727 static __be32 fattr_handle_absent_fs(u32 *bmval0, u32 *bmval1, u32 *rdattr_err)
1728 {
1729         /* As per referral draft:  */
1730         if (*bmval0 & ~WORD0_ABSENT_FS_ATTRS ||
1731             *bmval1 & ~WORD1_ABSENT_FS_ATTRS) {
1732                 if (*bmval0 & FATTR4_WORD0_RDATTR_ERROR ||
1733                     *bmval0 & FATTR4_WORD0_FS_LOCATIONS)
1734                         *rdattr_err = NFSERR_MOVED;
1735                 else
1736                         return nfserr_moved;
1737         }
1738         *bmval0 &= WORD0_ABSENT_FS_ATTRS;
1739         *bmval1 &= WORD1_ABSENT_FS_ATTRS;
1740         return 0;
1741 }
1742
1743 /*
1744  * Note: @fhp can be NULL; in this case, we might have to compose the filehandle
1745  * ourselves.
1746  *
1747  * @countp is the buffer size in _words_; upon successful return this becomes
1748  * replaced with the number of words written.
1749  */
1750 __be32
1751 nfsd4_encode_fattr(struct svc_fh *fhp, struct svc_export *exp,
1752                 struct dentry *dentry, __be32 *buffer, int *countp, u32 *bmval,
1753                 struct svc_rqst *rqstp, int ignore_crossmnt)
1754 {
1755         u32 bmval0 = bmval[0];
1756         u32 bmval1 = bmval[1];
1757         u32 bmval2 = bmval[2];
1758         struct kstat stat;
1759         struct svc_fh tempfh;
1760         struct kstatfs statfs;
1761         int buflen = *countp << 2;
1762         __be32 *attrlenp;
1763         u32 dummy;
1764         u64 dummy64;
1765         u32 rdattr_err = 0;
1766         __be32 *p = buffer;
1767         __be32 status;
1768         int err;
1769         int aclsupport = 0;
1770         struct nfs4_acl *acl = NULL;
1771         struct nfsd4_compoundres *resp = rqstp->rq_resp;
1772         u32 minorversion = resp->cstate.minorversion;
1773
1774         BUG_ON(bmval1 & NFSD_WRITEONLY_ATTRS_WORD1);
1775         BUG_ON(bmval0 & ~nfsd_suppattrs0(minorversion));
1776         BUG_ON(bmval1 & ~nfsd_suppattrs1(minorversion));
1777         BUG_ON(bmval2 & ~nfsd_suppattrs2(minorversion));
1778
1779         if (exp->ex_fslocs.migrated) {
1780                 BUG_ON(bmval[2]);
1781                 status = fattr_handle_absent_fs(&bmval0, &bmval1, &rdattr_err);
1782                 if (status)
1783                         goto out;
1784         }
1785
1786         err = vfs_getattr(exp->ex_path.mnt, dentry, &stat);
1787         if (err)
1788                 goto out_nfserr;
1789         if ((bmval0 & (FATTR4_WORD0_FILES_FREE | FATTR4_WORD0_FILES_TOTAL |
1790                         FATTR4_WORD0_MAXNAME)) ||
1791             (bmval1 & (FATTR4_WORD1_SPACE_AVAIL | FATTR4_WORD1_SPACE_FREE |
1792                        FATTR4_WORD1_SPACE_TOTAL))) {
1793                 err = vfs_statfs(dentry, &statfs);
1794                 if (err)
1795                         goto out_nfserr;
1796         }
1797         if ((bmval0 & (FATTR4_WORD0_FILEHANDLE | FATTR4_WORD0_FSID)) && !fhp) {
1798                 fh_init(&tempfh, NFS4_FHSIZE);
1799                 status = fh_compose(&tempfh, exp, dentry, NULL);
1800                 if (status)
1801                         goto out;
1802                 fhp = &tempfh;
1803         }
1804         if (bmval0 & (FATTR4_WORD0_ACL | FATTR4_WORD0_ACLSUPPORT
1805                         | FATTR4_WORD0_SUPPORTED_ATTRS)) {
1806                 err = nfsd4_get_nfs4_acl(rqstp, dentry, &acl);
1807                 aclsupport = (err == 0);
1808                 if (bmval0 & FATTR4_WORD0_ACL) {
1809                         if (err == -EOPNOTSUPP)
1810                                 bmval0 &= ~FATTR4_WORD0_ACL;
1811                         else if (err == -EINVAL) {
1812                                 status = nfserr_attrnotsupp;
1813                                 goto out;
1814                         } else if (err != 0)
1815                                 goto out_nfserr;
1816                 }
1817         }
1818         if (bmval0 & FATTR4_WORD0_FS_LOCATIONS) {
1819                 if (exp->ex_fslocs.locations == NULL) {
1820                         bmval0 &= ~FATTR4_WORD0_FS_LOCATIONS;
1821                 }
1822         }
1823         if ((buflen -= 16) < 0)
1824                 goto out_resource;
1825
1826         if (unlikely(bmval2)) {
1827                 WRITE32(3);
1828                 WRITE32(bmval0);
1829                 WRITE32(bmval1);
1830                 WRITE32(bmval2);
1831         } else if (likely(bmval1)) {
1832                 WRITE32(2);
1833                 WRITE32(bmval0);
1834                 WRITE32(bmval1);
1835         } else {
1836                 WRITE32(1);
1837                 WRITE32(bmval0);
1838         }
1839         attrlenp = p++;                /* to be backfilled later */
1840
1841         if (bmval0 & FATTR4_WORD0_SUPPORTED_ATTRS) {
1842                 u32 word0 = nfsd_suppattrs0(minorversion);
1843                 u32 word1 = nfsd_suppattrs1(minorversion);
1844                 u32 word2 = nfsd_suppattrs2(minorversion);
1845
1846                 if ((buflen -= 12) < 0)
1847                         goto out_resource;
1848                 if (!aclsupport)
1849                         word0 &= ~FATTR4_WORD0_ACL;
1850                 if (!exp->ex_fslocs.locations)
1851                         word0 &= ~FATTR4_WORD0_FS_LOCATIONS;
1852                 if (!word2) {
1853                         WRITE32(2);
1854                         WRITE32(word0);
1855                         WRITE32(word1);
1856                 } else {
1857                         WRITE32(3);
1858                         WRITE32(word0);
1859                         WRITE32(word1);
1860                         WRITE32(word2);
1861                 }
1862         }
1863         if (bmval0 & FATTR4_WORD0_TYPE) {
1864                 if ((buflen -= 4) < 0)
1865                         goto out_resource;
1866                 dummy = nfs4_ftypes[(stat.mode & S_IFMT) >> 12];
1867                 if (dummy == NF4BAD)
1868                         goto out_serverfault;
1869                 WRITE32(dummy);
1870         }
1871         if (bmval0 & FATTR4_WORD0_FH_EXPIRE_TYPE) {
1872                 if ((buflen -= 4) < 0)
1873                         goto out_resource;
1874                 if (exp->ex_flags & NFSEXP_NOSUBTREECHECK)
1875                         WRITE32(NFS4_FH_PERSISTENT);
1876                 else
1877                         WRITE32(NFS4_FH_PERSISTENT|NFS4_FH_VOL_RENAME);
1878         }
1879         if (bmval0 & FATTR4_WORD0_CHANGE) {
1880                 if ((buflen -= 8) < 0)
1881                         goto out_resource;
1882                 write_change(&p, &stat, dentry->d_inode);
1883         }
1884         if (bmval0 & FATTR4_WORD0_SIZE) {
1885                 if ((buflen -= 8) < 0)
1886                         goto out_resource;
1887                 WRITE64(stat.size);
1888         }
1889         if (bmval0 & FATTR4_WORD0_LINK_SUPPORT) {
1890                 if ((buflen -= 4) < 0)
1891                         goto out_resource;
1892                 WRITE32(1);
1893         }
1894         if (bmval0 & FATTR4_WORD0_SYMLINK_SUPPORT) {
1895                 if ((buflen -= 4) < 0)
1896                         goto out_resource;
1897                 WRITE32(1);
1898         }
1899         if (bmval0 & FATTR4_WORD0_NAMED_ATTR) {
1900                 if ((buflen -= 4) < 0)
1901                         goto out_resource;
1902                 WRITE32(0);
1903         }
1904         if (bmval0 & FATTR4_WORD0_FSID) {
1905                 if ((buflen -= 16) < 0)
1906                         goto out_resource;
1907                 if (exp->ex_fslocs.migrated) {
1908                         WRITE64(NFS4_REFERRAL_FSID_MAJOR);
1909                         WRITE64(NFS4_REFERRAL_FSID_MINOR);
1910                 } else switch(fsid_source(fhp)) {
1911                 case FSIDSOURCE_FSID:
1912                         WRITE64((u64)exp->ex_fsid);
1913                         WRITE64((u64)0);
1914                         break;
1915                 case FSIDSOURCE_DEV:
1916                         WRITE32(0);
1917                         WRITE32(MAJOR(stat.dev));
1918                         WRITE32(0);
1919                         WRITE32(MINOR(stat.dev));
1920                         break;
1921                 case FSIDSOURCE_UUID:
1922                         WRITEMEM(exp->ex_uuid, 16);
1923                         break;
1924                 }
1925         }
1926         if (bmval0 & FATTR4_WORD0_UNIQUE_HANDLES) {
1927                 if ((buflen -= 4) < 0)
1928                         goto out_resource;
1929                 WRITE32(0);
1930         }
1931         if (bmval0 & FATTR4_WORD0_LEASE_TIME) {
1932                 if ((buflen -= 4) < 0)
1933                         goto out_resource;
1934                 WRITE32(NFSD_LEASE_TIME);
1935         }
1936         if (bmval0 & FATTR4_WORD0_RDATTR_ERROR) {
1937                 if ((buflen -= 4) < 0)
1938                         goto out_resource;
1939                 WRITE32(rdattr_err);
1940         }
1941         if (bmval0 & FATTR4_WORD0_ACL) {
1942                 struct nfs4_ace *ace;
1943
1944                 if (acl == NULL) {
1945                         if ((buflen -= 4) < 0)
1946                                 goto out_resource;
1947
1948                         WRITE32(0);
1949                         goto out_acl;
1950                 }
1951                 if ((buflen -= 4) < 0)
1952                         goto out_resource;
1953                 WRITE32(acl->naces);
1954
1955                 for (ace = acl->aces; ace < acl->aces + acl->naces; ace++) {
1956                         if ((buflen -= 4*3) < 0)
1957                                 goto out_resource;
1958                         WRITE32(ace->type);
1959                         WRITE32(ace->flag);
1960                         WRITE32(ace->access_mask & NFS4_ACE_MASK_ALL);
1961                         status = nfsd4_encode_aclname(rqstp, ace->whotype,
1962                                 ace->who, ace->flag & NFS4_ACE_IDENTIFIER_GROUP,
1963                                 &p, &buflen);
1964                         if (status == nfserr_resource)
1965                                 goto out_resource;
1966                         if (status)
1967                                 goto out;
1968                 }
1969         }
1970 out_acl:
1971         if (bmval0 & FATTR4_WORD0_ACLSUPPORT) {
1972                 if ((buflen -= 4) < 0)
1973                         goto out_resource;
1974                 WRITE32(aclsupport ?
1975                         ACL4_SUPPORT_ALLOW_ACL|ACL4_SUPPORT_DENY_ACL : 0);
1976         }
1977         if (bmval0 & FATTR4_WORD0_CANSETTIME) {
1978                 if ((buflen -= 4) < 0)
1979                         goto out_resource;
1980                 WRITE32(1);
1981         }
1982         if (bmval0 & FATTR4_WORD0_CASE_INSENSITIVE) {
1983                 if ((buflen -= 4) < 0)
1984                         goto out_resource;
1985                 WRITE32(1);
1986         }
1987         if (bmval0 & FATTR4_WORD0_CASE_PRESERVING) {
1988                 if ((buflen -= 4) < 0)
1989                         goto out_resource;
1990                 WRITE32(1);
1991         }
1992         if (bmval0 & FATTR4_WORD0_CHOWN_RESTRICTED) {
1993                 if ((buflen -= 4) < 0)
1994                         goto out_resource;
1995                 WRITE32(1);
1996         }
1997         if (bmval0 & FATTR4_WORD0_FILEHANDLE) {
1998                 buflen -= (XDR_QUADLEN(fhp->fh_handle.fh_size) << 2) + 4;
1999                 if (buflen < 0)
2000                         goto out_resource;
2001                 WRITE32(fhp->fh_handle.fh_size);
2002                 WRITEMEM(&fhp->fh_handle.fh_base, fhp->fh_handle.fh_size);
2003         }
2004         if (bmval0 & FATTR4_WORD0_FILEID) {
2005                 if ((buflen -= 8) < 0)
2006                         goto out_resource;
2007                 WRITE64(stat.ino);
2008         }
2009         if (bmval0 & FATTR4_WORD0_FILES_AVAIL) {
2010                 if ((buflen -= 8) < 0)
2011                         goto out_resource;
2012                 WRITE64((u64) statfs.f_ffree);
2013         }
2014         if (bmval0 & FATTR4_WORD0_FILES_FREE) {
2015                 if ((buflen -= 8) < 0)
2016                         goto out_resource;
2017                 WRITE64((u64) statfs.f_ffree);
2018         }
2019         if (bmval0 & FATTR4_WORD0_FILES_TOTAL) {
2020                 if ((buflen -= 8) < 0)
2021                         goto out_resource;
2022                 WRITE64((u64) statfs.f_files);
2023         }
2024         if (bmval0 & FATTR4_WORD0_FS_LOCATIONS) {
2025                 status = nfsd4_encode_fs_locations(rqstp, exp, &p, &buflen);
2026                 if (status == nfserr_resource)
2027                         goto out_resource;
2028                 if (status)
2029                         goto out;
2030         }
2031         if (bmval0 & FATTR4_WORD0_HOMOGENEOUS) {
2032                 if ((buflen -= 4) < 0)
2033                         goto out_resource;
2034                 WRITE32(1);
2035         }
2036         if (bmval0 & FATTR4_WORD0_MAXFILESIZE) {
2037                 if ((buflen -= 8) < 0)
2038                         goto out_resource;
2039                 WRITE64(~(u64)0);
2040         }
2041         if (bmval0 & FATTR4_WORD0_MAXLINK) {
2042                 if ((buflen -= 4) < 0)
2043                         goto out_resource;
2044                 WRITE32(255);
2045         }
2046         if (bmval0 & FATTR4_WORD0_MAXNAME) {
2047                 if ((buflen -= 4) < 0)
2048                         goto out_resource;
2049                 WRITE32(statfs.f_namelen);
2050         }
2051         if (bmval0 & FATTR4_WORD0_MAXREAD) {
2052                 if ((buflen -= 8) < 0)
2053                         goto out_resource;
2054                 WRITE64((u64) svc_max_payload(rqstp));
2055         }
2056         if (bmval0 & FATTR4_WORD0_MAXWRITE) {
2057                 if ((buflen -= 8) < 0)
2058                         goto out_resource;
2059                 WRITE64((u64) svc_max_payload(rqstp));
2060         }
2061         if (bmval1 & FATTR4_WORD1_MODE) {
2062                 if ((buflen -= 4) < 0)
2063                         goto out_resource;
2064                 WRITE32(stat.mode & S_IALLUGO);
2065         }
2066         if (bmval1 & FATTR4_WORD1_NO_TRUNC) {
2067                 if ((buflen -= 4) < 0)
2068                         goto out_resource;
2069                 WRITE32(1);
2070         }
2071         if (bmval1 & FATTR4_WORD1_NUMLINKS) {
2072                 if ((buflen -= 4) < 0)
2073                         goto out_resource;
2074                 WRITE32(stat.nlink);
2075         }
2076         if (bmval1 & FATTR4_WORD1_OWNER) {
2077                 status = nfsd4_encode_user(rqstp, stat.uid, &p, &buflen);
2078                 if (status == nfserr_resource)
2079                         goto out_resource;
2080                 if (status)
2081                         goto out;
2082         }
2083         if (bmval1 & FATTR4_WORD1_OWNER_GROUP) {
2084                 status = nfsd4_encode_group(rqstp, stat.gid, &p, &buflen);
2085                 if (status == nfserr_resource)
2086                         goto out_resource;
2087                 if (status)
2088                         goto out;
2089         }
2090         if (bmval1 & FATTR4_WORD1_RAWDEV) {
2091                 if ((buflen -= 8) < 0)
2092                         goto out_resource;
2093                 WRITE32((u32) MAJOR(stat.rdev));
2094                 WRITE32((u32) MINOR(stat.rdev));
2095         }
2096         if (bmval1 & FATTR4_WORD1_SPACE_AVAIL) {
2097                 if ((buflen -= 8) < 0)
2098                         goto out_resource;
2099                 dummy64 = (u64)statfs.f_bavail * (u64)statfs.f_bsize;
2100                 WRITE64(dummy64);
2101         }
2102         if (bmval1 & FATTR4_WORD1_SPACE_FREE) {
2103                 if ((buflen -= 8) < 0)
2104                         goto out_resource;
2105                 dummy64 = (u64)statfs.f_bfree * (u64)statfs.f_bsize;
2106                 WRITE64(dummy64);
2107         }
2108         if (bmval1 & FATTR4_WORD1_SPACE_TOTAL) {
2109                 if ((buflen -= 8) < 0)
2110                         goto out_resource;
2111                 dummy64 = (u64)statfs.f_blocks * (u64)statfs.f_bsize;
2112                 WRITE64(dummy64);
2113         }
2114         if (bmval1 & FATTR4_WORD1_SPACE_USED) {
2115                 if ((buflen -= 8) < 0)
2116                         goto out_resource;
2117                 dummy64 = (u64)stat.blocks << 9;
2118                 WRITE64(dummy64);
2119         }
2120         if (bmval1 & FATTR4_WORD1_TIME_ACCESS) {
2121                 if ((buflen -= 12) < 0)
2122                         goto out_resource;
2123                 WRITE32(0);
2124                 WRITE32(stat.atime.tv_sec);
2125                 WRITE32(stat.atime.tv_nsec);
2126         }
2127         if (bmval1 & FATTR4_WORD1_TIME_DELTA) {
2128                 if ((buflen -= 12) < 0)
2129                         goto out_resource;
2130                 WRITE32(0);
2131                 WRITE32(1);
2132                 WRITE32(0);
2133         }
2134         if (bmval1 & FATTR4_WORD1_TIME_METADATA) {
2135                 if ((buflen -= 12) < 0)
2136                         goto out_resource;
2137                 WRITE32(0);
2138                 WRITE32(stat.ctime.tv_sec);
2139                 WRITE32(stat.ctime.tv_nsec);
2140         }
2141         if (bmval1 & FATTR4_WORD1_TIME_MODIFY) {
2142                 if ((buflen -= 12) < 0)
2143                         goto out_resource;
2144                 WRITE32(0);
2145                 WRITE32(stat.mtime.tv_sec);
2146                 WRITE32(stat.mtime.tv_nsec);
2147         }
2148         if (bmval1 & FATTR4_WORD1_MOUNTED_ON_FILEID) {
2149                 if ((buflen -= 8) < 0)
2150                         goto out_resource;
2151                 /*
2152                  * Get parent's attributes if not ignoring crossmount
2153                  * and this is the root of a cross-mounted filesystem.
2154                  */
2155                 if (ignore_crossmnt == 0 &&
2156                     exp->ex_path.mnt->mnt_root->d_inode == dentry->d_inode) {
2157                         err = vfs_getattr(exp->ex_path.mnt->mnt_parent,
2158                                 exp->ex_path.mnt->mnt_mountpoint, &stat);
2159                         if (err)
2160                                 goto out_nfserr;
2161                 }
2162                 WRITE64(stat.ino);
2163         }
2164         if (bmval2 & FATTR4_WORD2_SUPPATTR_EXCLCREAT) {
2165                 WRITE32(3);
2166                 WRITE32(NFSD_SUPPATTR_EXCLCREAT_WORD0);
2167                 WRITE32(NFSD_SUPPATTR_EXCLCREAT_WORD1);
2168                 WRITE32(NFSD_SUPPATTR_EXCLCREAT_WORD2);
2169         }
2170
2171         *attrlenp = htonl((char *)p - (char *)attrlenp - 4);
2172         *countp = p - buffer;
2173         status = nfs_ok;
2174
2175 out:
2176         kfree(acl);
2177         if (fhp == &tempfh)
2178                 fh_put(&tempfh);
2179         return status;
2180 out_nfserr:
2181         status = nfserrno(err);
2182         goto out;
2183 out_resource:
2184         *countp = 0;
2185         status = nfserr_resource;
2186         goto out;
2187 out_serverfault:
2188         status = nfserr_serverfault;
2189         goto out;
2190 }
2191
2192 static inline int attributes_need_mount(u32 *bmval)
2193 {
2194         if (bmval[0] & ~(FATTR4_WORD0_RDATTR_ERROR | FATTR4_WORD0_LEASE_TIME))
2195                 return 1;
2196         if (bmval[1] & ~FATTR4_WORD1_MOUNTED_ON_FILEID)
2197                 return 1;
2198         return 0;
2199 }
2200
2201 static __be32
2202 nfsd4_encode_dirent_fattr(struct nfsd4_readdir *cd,
2203                 const char *name, int namlen, __be32 *p, int *buflen)
2204 {
2205         struct svc_export *exp = cd->rd_fhp->fh_export;
2206         struct dentry *dentry;
2207         __be32 nfserr;
2208         int ignore_crossmnt = 0;
2209
2210         dentry = lookup_one_len(name, cd->rd_fhp->fh_dentry, namlen);
2211         if (IS_ERR(dentry))
2212                 return nfserrno(PTR_ERR(dentry));
2213
2214         exp_get(exp);
2215         /*
2216          * In the case of a mountpoint, the client may be asking for
2217          * attributes that are only properties of the underlying filesystem
2218          * as opposed to the cross-mounted file system. In such a case,
2219          * we will not follow the cross mount and will fill the attribtutes
2220          * directly from the mountpoint dentry.
2221          */
2222         if (d_mountpoint(dentry) && !attributes_need_mount(cd->rd_bmval))
2223                 ignore_crossmnt = 1;
2224         else if (d_mountpoint(dentry)) {
2225                 int err;
2226
2227                 /*
2228                  * Why the heck aren't we just using nfsd_lookup??
2229                  * Different "."/".." handling?  Something else?
2230                  * At least, add a comment here to explain....
2231                  */
2232                 err = nfsd_cross_mnt(cd->rd_rqstp, &dentry, &exp);
2233                 if (err) {
2234                         nfserr = nfserrno(err);
2235                         goto out_put;
2236                 }
2237                 nfserr = check_nfsd_access(exp, cd->rd_rqstp);
2238                 if (nfserr)
2239                         goto out_put;
2240
2241         }
2242         nfserr = nfsd4_encode_fattr(NULL, exp, dentry, p, buflen, cd->rd_bmval,
2243                                         cd->rd_rqstp, ignore_crossmnt);
2244 out_put:
2245         dput(dentry);
2246         exp_put(exp);
2247         return nfserr;
2248 }
2249
2250 static __be32 *
2251 nfsd4_encode_rdattr_error(__be32 *p, int buflen, __be32 nfserr)
2252 {
2253         __be32 *attrlenp;
2254
2255         if (buflen < 6)
2256                 return NULL;
2257         *p++ = htonl(2);
2258         *p++ = htonl(FATTR4_WORD0_RDATTR_ERROR); /* bmval0 */
2259         *p++ = htonl(0);                         /* bmval1 */
2260
2261         attrlenp = p++;
2262         *p++ = nfserr;       /* no htonl */
2263         *attrlenp = htonl((char *)p - (char *)attrlenp - 4);
2264         return p;
2265 }
2266
2267 static int
2268 nfsd4_encode_dirent(void *ccdv, const char *name, int namlen,
2269                     loff_t offset, u64 ino, unsigned int d_type)
2270 {
2271         struct readdir_cd *ccd = ccdv;
2272         struct nfsd4_readdir *cd = container_of(ccd, struct nfsd4_readdir, common);
2273         int buflen;
2274         __be32 *p = cd->buffer;
2275         __be32 nfserr = nfserr_toosmall;
2276
2277         /* In nfsv4, "." and ".." never make it onto the wire.. */
2278         if (name && isdotent(name, namlen)) {
2279                 cd->common.err = nfs_ok;
2280                 return 0;
2281         }
2282
2283         if (cd->offset)
2284                 xdr_encode_hyper(cd->offset, (u64) offset);
2285
2286         buflen = cd->buflen - 4 - XDR_QUADLEN(namlen);
2287         if (buflen < 0)
2288                 goto fail;
2289
2290         *p++ = xdr_one;                             /* mark entry present */
2291         cd->offset = p;                             /* remember pointer */
2292         p = xdr_encode_hyper(p, NFS_OFFSET_MAX);    /* offset of next entry */
2293         p = xdr_encode_array(p, name, namlen);      /* name length & name */
2294
2295         nfserr = nfsd4_encode_dirent_fattr(cd, name, namlen, p, &buflen);
2296         switch (nfserr) {
2297         case nfs_ok:
2298                 p += buflen;
2299                 break;
2300         case nfserr_resource:
2301                 nfserr = nfserr_toosmall;
2302                 goto fail;
2303         case nfserr_dropit:
2304                 goto fail;
2305         default:
2306                 /*
2307                  * If the client requested the RDATTR_ERROR attribute,
2308                  * we stuff the error code into this attribute
2309                  * and continue.  If this attribute was not requested,
2310                  * then in accordance with the spec, we fail the
2311                  * entire READDIR operation(!)
2312                  */
2313                 if (!(cd->rd_bmval[0] & FATTR4_WORD0_RDATTR_ERROR))
2314                         goto fail;
2315                 p = nfsd4_encode_rdattr_error(p, buflen, nfserr);
2316                 if (p == NULL) {
2317                         nfserr = nfserr_toosmall;
2318                         goto fail;
2319                 }
2320         }
2321         cd->buflen -= (p - cd->buffer);
2322         cd->buffer = p;
2323         cd->common.err = nfs_ok;
2324         return 0;
2325 fail:
2326         cd->common.err = nfserr;
2327         return -EINVAL;
2328 }
2329
2330 static void
2331 nfsd4_encode_stateid(struct nfsd4_compoundres *resp, stateid_t *sid)
2332 {
2333         __be32 *p;
2334
2335         RESERVE_SPACE(sizeof(stateid_t));
2336         WRITE32(sid->si_generation);
2337         WRITEMEM(&sid->si_opaque, sizeof(stateid_opaque_t));
2338         ADJUST_ARGS();
2339 }
2340
2341 static __be32
2342 nfsd4_encode_access(struct nfsd4_compoundres *resp, __be32 nfserr, struct nfsd4_access *access)
2343 {
2344         __be32 *p;
2345
2346         if (!nfserr) {
2347                 RESERVE_SPACE(8);
2348                 WRITE32(access->ac_supported);
2349                 WRITE32(access->ac_resp_access);
2350                 ADJUST_ARGS();
2351         }
2352         return nfserr;
2353 }
2354
2355 static __be32
2356 nfsd4_encode_close(struct nfsd4_compoundres *resp, __be32 nfserr, struct nfsd4_close *close)
2357 {
2358         ENCODE_SEQID_OP_HEAD;
2359
2360         if (!nfserr)
2361                 nfsd4_encode_stateid(resp, &close->cl_stateid);
2362
2363         ENCODE_SEQID_OP_TAIL(close->cl_stateowner);
2364         return nfserr;
2365 }
2366
2367
2368 static __be32
2369 nfsd4_encode_commit(struct nfsd4_compoundres *resp, __be32 nfserr, struct nfsd4_commit *commit)
2370 {
2371         __be32 *p;
2372
2373         if (!nfserr) {
2374                 RESERVE_SPACE(8);
2375                 WRITEMEM(commit->co_verf.data, 8);
2376                 ADJUST_ARGS();
2377         }
2378         return nfserr;
2379 }
2380
2381 static __be32
2382 nfsd4_encode_create(struct nfsd4_compoundres *resp, __be32 nfserr, struct nfsd4_create *create)
2383 {
2384         __be32 *p;
2385
2386         if (!nfserr) {
2387                 RESERVE_SPACE(32);
2388                 write_cinfo(&p, &create->cr_cinfo);
2389                 WRITE32(2);
2390                 WRITE32(create->cr_bmval[0]);
2391                 WRITE32(create->cr_bmval[1]);
2392                 ADJUST_ARGS();
2393         }
2394         return nfserr;
2395 }
2396
2397 static __be32
2398 nfsd4_encode_getattr(struct nfsd4_compoundres *resp, __be32 nfserr, struct nfsd4_getattr *getattr)
2399 {
2400         struct svc_fh *fhp = getattr->ga_fhp;
2401         int buflen;
2402
2403         if (nfserr)
2404                 return nfserr;
2405
2406         buflen = resp->end - resp->p - (COMPOUND_ERR_SLACK_SPACE >> 2);
2407         nfserr = nfsd4_encode_fattr(fhp, fhp->fh_export, fhp->fh_dentry,
2408                                     resp->p, &buflen, getattr->ga_bmval,
2409                                     resp->rqstp, 0);
2410         if (!nfserr)
2411                 resp->p += buflen;
2412         return nfserr;
2413 }
2414
2415 static __be32
2416 nfsd4_encode_getfh(struct nfsd4_compoundres *resp, __be32 nfserr, struct svc_fh **fhpp)
2417 {
2418         struct svc_fh *fhp = *fhpp;
2419         unsigned int len;
2420         __be32 *p;
2421
2422         if (!nfserr) {
2423                 len = fhp->fh_handle.fh_size;
2424                 RESERVE_SPACE(len + 4);
2425                 WRITE32(len);
2426                 WRITEMEM(&fhp->fh_handle.fh_base, len);
2427                 ADJUST_ARGS();
2428         }
2429         return nfserr;
2430 }
2431
2432 /*
2433 * Including all fields other than the name, a LOCK4denied structure requires
2434 *   8(clientid) + 4(namelen) + 8(offset) + 8(length) + 4(type) = 32 bytes.
2435 */
2436 static void
2437 nfsd4_encode_lock_denied(struct nfsd4_compoundres *resp, struct nfsd4_lock_denied *ld)
2438 {
2439         __be32 *p;
2440
2441         RESERVE_SPACE(32 + XDR_LEN(ld->ld_sop ? ld->ld_sop->so_owner.len : 0));
2442         WRITE64(ld->ld_start);
2443         WRITE64(ld->ld_length);
2444         WRITE32(ld->ld_type);
2445         if (ld->ld_sop) {
2446                 WRITEMEM(&ld->ld_clientid, 8);
2447                 WRITE32(ld->ld_sop->so_owner.len);
2448                 WRITEMEM(ld->ld_sop->so_owner.data, ld->ld_sop->so_owner.len);
2449                 kref_put(&ld->ld_sop->so_ref, nfs4_free_stateowner);
2450         }  else {  /* non - nfsv4 lock in conflict, no clientid nor owner */
2451                 WRITE64((u64)0); /* clientid */
2452                 WRITE32(0); /* length of owner name */
2453         }
2454         ADJUST_ARGS();
2455 }
2456
2457 static __be32
2458 nfsd4_encode_lock(struct nfsd4_compoundres *resp, __be32 nfserr, struct nfsd4_lock *lock)
2459 {
2460         ENCODE_SEQID_OP_HEAD;
2461
2462         if (!nfserr)
2463                 nfsd4_encode_stateid(resp, &lock->lk_resp_stateid);
2464         else if (nfserr == nfserr_denied)
2465                 nfsd4_encode_lock_denied(resp, &lock->lk_denied);
2466
2467         ENCODE_SEQID_OP_TAIL(lock->lk_replay_owner);
2468         return nfserr;
2469 }
2470
2471 static __be32
2472 nfsd4_encode_lockt(struct nfsd4_compoundres *resp, __be32 nfserr, struct nfsd4_lockt *lockt)
2473 {
2474         if (nfserr == nfserr_denied)
2475                 nfsd4_encode_lock_denied(resp, &lockt->lt_denied);
2476         return nfserr;
2477 }
2478
2479 static __be32
2480 nfsd4_encode_locku(struct nfsd4_compoundres *resp, __be32 nfserr, struct nfsd4_locku *locku)
2481 {
2482         ENCODE_SEQID_OP_HEAD;
2483
2484         if (!nfserr)
2485                 nfsd4_encode_stateid(resp, &locku->lu_stateid);
2486
2487         ENCODE_SEQID_OP_TAIL(locku->lu_stateowner);
2488         return nfserr;
2489 }
2490
2491
2492 static __be32
2493 nfsd4_encode_link(struct nfsd4_compoundres *resp, __be32 nfserr, struct nfsd4_link *link)
2494 {
2495         __be32 *p;
2496
2497         if (!nfserr) {
2498                 RESERVE_SPACE(20);
2499                 write_cinfo(&p, &link->li_cinfo);
2500                 ADJUST_ARGS();
2501         }
2502         return nfserr;
2503 }
2504
2505
2506 static __be32
2507 nfsd4_encode_open(struct nfsd4_compoundres *resp, __be32 nfserr, struct nfsd4_open *open)
2508 {
2509         __be32 *p;
2510         ENCODE_SEQID_OP_HEAD;
2511
2512         if (nfserr)
2513                 goto out;
2514
2515         nfsd4_encode_stateid(resp, &open->op_stateid);
2516         RESERVE_SPACE(40);
2517         write_cinfo(&p, &open->op_cinfo);
2518         WRITE32(open->op_rflags);
2519         WRITE32(2);
2520         WRITE32(open->op_bmval[0]);
2521         WRITE32(open->op_bmval[1]);
2522         WRITE32(open->op_delegate_type);
2523         ADJUST_ARGS();
2524
2525         switch (open->op_delegate_type) {
2526         case NFS4_OPEN_DELEGATE_NONE:
2527                 break;
2528         case NFS4_OPEN_DELEGATE_READ:
2529                 nfsd4_encode_stateid(resp, &open->op_delegate_stateid);
2530                 RESERVE_SPACE(20);
2531                 WRITE32(open->op_recall);
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         case NFS4_OPEN_DELEGATE_WRITE:
2543                 nfsd4_encode_stateid(resp, &open->op_delegate_stateid);
2544                 RESERVE_SPACE(32);
2545                 WRITE32(0);
2546
2547                 /*
2548                  * TODO: space_limit's in delegations
2549                  */
2550                 WRITE32(NFS4_LIMIT_SIZE);
2551                 WRITE32(~(u32)0);
2552                 WRITE32(~(u32)0);
2553
2554                 /*
2555                  * TODO: ACE's in delegations
2556                  */
2557                 WRITE32(NFS4_ACE_ACCESS_ALLOWED_ACE_TYPE);
2558                 WRITE32(0);
2559                 WRITE32(0);
2560                 WRITE32(0);   /* XXX: is NULL principal ok? */
2561                 ADJUST_ARGS();
2562                 break;
2563         default:
2564                 BUG();
2565         }
2566         /* XXX save filehandle here */
2567 out:
2568         ENCODE_SEQID_OP_TAIL(open->op_stateowner);
2569         return nfserr;
2570 }
2571
2572 static __be32
2573 nfsd4_encode_open_confirm(struct nfsd4_compoundres *resp, __be32 nfserr, struct nfsd4_open_confirm *oc)
2574 {
2575         ENCODE_SEQID_OP_HEAD;
2576
2577         if (!nfserr)
2578                 nfsd4_encode_stateid(resp, &oc->oc_resp_stateid);
2579
2580         ENCODE_SEQID_OP_TAIL(oc->oc_stateowner);
2581         return nfserr;
2582 }
2583
2584 static __be32
2585 nfsd4_encode_open_downgrade(struct nfsd4_compoundres *resp, __be32 nfserr, struct nfsd4_open_downgrade *od)
2586 {
2587         ENCODE_SEQID_OP_HEAD;
2588
2589         if (!nfserr)
2590                 nfsd4_encode_stateid(resp, &od->od_stateid);
2591
2592         ENCODE_SEQID_OP_TAIL(od->od_stateowner);
2593         return nfserr;
2594 }
2595
2596 static __be32
2597 nfsd4_encode_read(struct nfsd4_compoundres *resp, __be32 nfserr,
2598                   struct nfsd4_read *read)
2599 {
2600         u32 eof;
2601         int v, pn;
2602         unsigned long maxcount; 
2603         long len;
2604         __be32 *p;
2605
2606         if (nfserr)
2607                 return nfserr;
2608         if (resp->xbuf->page_len)
2609                 return nfserr_resource;
2610
2611         RESERVE_SPACE(8); /* eof flag and byte count */
2612
2613         maxcount = svc_max_payload(resp->rqstp);
2614         if (maxcount > read->rd_length)
2615                 maxcount = read->rd_length;
2616
2617         len = maxcount;
2618         v = 0;
2619         while (len > 0) {
2620                 pn = resp->rqstp->rq_resused++;
2621                 resp->rqstp->rq_vec[v].iov_base =
2622                         page_address(resp->rqstp->rq_respages[pn]);
2623                 resp->rqstp->rq_vec[v].iov_len =
2624                         len < PAGE_SIZE ? len : PAGE_SIZE;
2625                 v++;
2626                 len -= PAGE_SIZE;
2627         }
2628         read->rd_vlen = v;
2629
2630         nfserr = nfsd_read(read->rd_rqstp, read->rd_fhp, read->rd_filp,
2631                         read->rd_offset, resp->rqstp->rq_vec, read->rd_vlen,
2632                         &maxcount);
2633
2634         if (nfserr == nfserr_symlink)
2635                 nfserr = nfserr_inval;
2636         if (nfserr)
2637                 return nfserr;
2638         eof = (read->rd_offset + maxcount >=
2639                read->rd_fhp->fh_dentry->d_inode->i_size);
2640
2641         WRITE32(eof);
2642         WRITE32(maxcount);
2643         ADJUST_ARGS();
2644         resp->xbuf->head[0].iov_len = (char*)p
2645                                         - (char*)resp->xbuf->head[0].iov_base;
2646         resp->xbuf->page_len = maxcount;
2647
2648         /* Use rest of head for padding and remaining ops: */
2649         resp->xbuf->tail[0].iov_base = p;
2650         resp->xbuf->tail[0].iov_len = 0;
2651         if (maxcount&3) {
2652                 RESERVE_SPACE(4);
2653                 WRITE32(0);
2654                 resp->xbuf->tail[0].iov_base += maxcount&3;
2655                 resp->xbuf->tail[0].iov_len = 4 - (maxcount&3);
2656                 ADJUST_ARGS();
2657         }
2658         return 0;
2659 }
2660
2661 static __be32
2662 nfsd4_encode_readlink(struct nfsd4_compoundres *resp, __be32 nfserr, struct nfsd4_readlink *readlink)
2663 {
2664         int maxcount;
2665         char *page;
2666         __be32 *p;
2667
2668         if (nfserr)
2669                 return nfserr;
2670         if (resp->xbuf->page_len)
2671                 return nfserr_resource;
2672
2673         page = page_address(resp->rqstp->rq_respages[resp->rqstp->rq_resused++]);
2674
2675         maxcount = PAGE_SIZE;
2676         RESERVE_SPACE(4);
2677
2678         /*
2679          * XXX: By default, the ->readlink() VFS op will truncate symlinks
2680          * if they would overflow the buffer.  Is this kosher in NFSv4?  If
2681          * not, one easy fix is: if ->readlink() precisely fills the buffer,
2682          * assume that truncation occurred, and return NFS4ERR_RESOURCE.
2683          */
2684         nfserr = nfsd_readlink(readlink->rl_rqstp, readlink->rl_fhp, page, &maxcount);
2685         if (nfserr == nfserr_isdir)
2686                 return nfserr_inval;
2687         if (nfserr)
2688                 return nfserr;
2689
2690         WRITE32(maxcount);
2691         ADJUST_ARGS();
2692         resp->xbuf->head[0].iov_len = (char*)p
2693                                 - (char*)resp->xbuf->head[0].iov_base;
2694         resp->xbuf->page_len = maxcount;
2695
2696         /* Use rest of head for padding and remaining ops: */
2697         resp->xbuf->tail[0].iov_base = p;
2698         resp->xbuf->tail[0].iov_len = 0;
2699         if (maxcount&3) {
2700                 RESERVE_SPACE(4);
2701                 WRITE32(0);
2702                 resp->xbuf->tail[0].iov_base += maxcount&3;
2703                 resp->xbuf->tail[0].iov_len = 4 - (maxcount&3);
2704                 ADJUST_ARGS();
2705         }
2706         return 0;
2707 }
2708
2709 static __be32
2710 nfsd4_encode_readdir(struct nfsd4_compoundres *resp, __be32 nfserr, struct nfsd4_readdir *readdir)
2711 {
2712         int maxcount;
2713         loff_t offset;
2714         __be32 *page, *savep, *tailbase;
2715         __be32 *p;
2716
2717         if (nfserr)
2718                 return nfserr;
2719         if (resp->xbuf->page_len)
2720                 return nfserr_resource;
2721
2722         RESERVE_SPACE(8);  /* verifier */
2723         savep = p;
2724
2725         /* XXX: Following NFSv3, we ignore the READDIR verifier for now. */
2726         WRITE32(0);
2727         WRITE32(0);
2728         ADJUST_ARGS();
2729         resp->xbuf->head[0].iov_len = ((char*)resp->p) - (char*)resp->xbuf->head[0].iov_base;
2730         tailbase = p;
2731
2732         maxcount = PAGE_SIZE;
2733         if (maxcount > readdir->rd_maxcount)
2734                 maxcount = readdir->rd_maxcount;
2735
2736         /*
2737          * Convert from bytes to words, account for the two words already
2738          * written, make sure to leave two words at the end for the next
2739          * pointer and eof field.
2740          */
2741         maxcount = (maxcount >> 2) - 4;
2742         if (maxcount < 0) {
2743                 nfserr =  nfserr_toosmall;
2744                 goto err_no_verf;
2745         }
2746
2747         page = page_address(resp->rqstp->rq_respages[resp->rqstp->rq_resused++]);
2748         readdir->common.err = 0;
2749         readdir->buflen = maxcount;
2750         readdir->buffer = page;
2751         readdir->offset = NULL;
2752
2753         offset = readdir->rd_cookie;
2754         nfserr = nfsd_readdir(readdir->rd_rqstp, readdir->rd_fhp,
2755                               &offset,
2756                               &readdir->common, nfsd4_encode_dirent);
2757         if (nfserr == nfs_ok &&
2758             readdir->common.err == nfserr_toosmall &&
2759             readdir->buffer == page) 
2760                 nfserr = nfserr_toosmall;
2761         if (nfserr == nfserr_symlink)
2762                 nfserr = nfserr_notdir;
2763         if (nfserr)
2764                 goto err_no_verf;
2765
2766         if (readdir->offset)
2767                 xdr_encode_hyper(readdir->offset, offset);
2768
2769         p = readdir->buffer;
2770         *p++ = 0;       /* no more entries */
2771         *p++ = htonl(readdir->common.err == nfserr_eof);
2772         resp->xbuf->page_len = ((char*)p) - (char*)page_address(
2773                 resp->rqstp->rq_respages[resp->rqstp->rq_resused-1]);
2774
2775         /* Use rest of head for padding and remaining ops: */
2776         resp->xbuf->tail[0].iov_base = tailbase;
2777         resp->xbuf->tail[0].iov_len = 0;
2778         resp->p = resp->xbuf->tail[0].iov_base;
2779         resp->end = resp->p + (PAGE_SIZE - resp->xbuf->head[0].iov_len)/4;
2780
2781         return 0;
2782 err_no_verf:
2783         p = savep;
2784         ADJUST_ARGS();
2785         return nfserr;
2786 }
2787
2788 static __be32
2789 nfsd4_encode_remove(struct nfsd4_compoundres *resp, __be32 nfserr, struct nfsd4_remove *remove)
2790 {
2791         __be32 *p;
2792
2793         if (!nfserr) {
2794                 RESERVE_SPACE(20);
2795                 write_cinfo(&p, &remove->rm_cinfo);
2796                 ADJUST_ARGS();
2797         }
2798         return nfserr;
2799 }
2800
2801 static __be32
2802 nfsd4_encode_rename(struct nfsd4_compoundres *resp, __be32 nfserr, struct nfsd4_rename *rename)
2803 {
2804         __be32 *p;
2805
2806         if (!nfserr) {
2807                 RESERVE_SPACE(40);
2808                 write_cinfo(&p, &rename->rn_sinfo);
2809                 write_cinfo(&p, &rename->rn_tinfo);
2810                 ADJUST_ARGS();
2811         }
2812         return nfserr;
2813 }
2814
2815 static __be32
2816 nfsd4_encode_secinfo(struct nfsd4_compoundres *resp, __be32 nfserr,
2817                      struct nfsd4_secinfo *secinfo)
2818 {
2819         int i = 0;
2820         struct svc_export *exp = secinfo->si_exp;
2821         u32 nflavs;
2822         struct exp_flavor_info *flavs;
2823         struct exp_flavor_info def_flavs[2];
2824         __be32 *p;
2825
2826         if (nfserr)
2827                 goto out;
2828         if (exp->ex_nflavors) {
2829                 flavs = exp->ex_flavors;
2830                 nflavs = exp->ex_nflavors;
2831         } else { /* Handling of some defaults in absence of real secinfo: */
2832                 flavs = def_flavs;
2833                 if (exp->ex_client->flavour->flavour == RPC_AUTH_UNIX) {
2834                         nflavs = 2;
2835                         flavs[0].pseudoflavor = RPC_AUTH_UNIX;
2836                         flavs[1].pseudoflavor = RPC_AUTH_NULL;
2837                 } else if (exp->ex_client->flavour->flavour == RPC_AUTH_GSS) {
2838                         nflavs = 1;
2839                         flavs[0].pseudoflavor
2840                                         = svcauth_gss_flavor(exp->ex_client);
2841                 } else {
2842                         nflavs = 1;
2843                         flavs[0].pseudoflavor
2844                                         = exp->ex_client->flavour->flavour;
2845                 }
2846         }
2847
2848         RESERVE_SPACE(4);
2849         WRITE32(nflavs);
2850         ADJUST_ARGS();
2851         for (i = 0; i < nflavs; i++) {
2852                 u32 flav = flavs[i].pseudoflavor;
2853                 struct gss_api_mech *gm = gss_mech_get_by_pseudoflavor(flav);
2854
2855                 if (gm) {
2856                         RESERVE_SPACE(4);
2857                         WRITE32(RPC_AUTH_GSS);
2858                         ADJUST_ARGS();
2859                         RESERVE_SPACE(4 + gm->gm_oid.len);
2860                         WRITE32(gm->gm_oid.len);
2861                         WRITEMEM(gm->gm_oid.data, gm->gm_oid.len);
2862                         ADJUST_ARGS();
2863                         RESERVE_SPACE(4);
2864                         WRITE32(0); /* qop */
2865                         ADJUST_ARGS();
2866                         RESERVE_SPACE(4);
2867                         WRITE32(gss_pseudoflavor_to_service(gm, flav));
2868                         ADJUST_ARGS();
2869                         gss_mech_put(gm);
2870                 } else {
2871                         RESERVE_SPACE(4);
2872                         WRITE32(flav);
2873                         ADJUST_ARGS();
2874                 }
2875         }
2876 out:
2877         if (exp)
2878                 exp_put(exp);
2879         return nfserr;
2880 }
2881
2882 /*
2883  * The SETATTR encode routine is special -- it always encodes a bitmap,
2884  * regardless of the error status.
2885  */
2886 static __be32
2887 nfsd4_encode_setattr(struct nfsd4_compoundres *resp, __be32 nfserr, struct nfsd4_setattr *setattr)
2888 {
2889         __be32 *p;
2890
2891         RESERVE_SPACE(12);
2892         if (nfserr) {
2893                 WRITE32(2);
2894                 WRITE32(0);
2895                 WRITE32(0);
2896         }
2897         else {
2898                 WRITE32(2);
2899                 WRITE32(setattr->sa_bmval[0]);
2900                 WRITE32(setattr->sa_bmval[1]);
2901         }
2902         ADJUST_ARGS();
2903         return nfserr;
2904 }
2905
2906 static __be32
2907 nfsd4_encode_setclientid(struct nfsd4_compoundres *resp, __be32 nfserr, struct nfsd4_setclientid *scd)
2908 {
2909         __be32 *p;
2910
2911         if (!nfserr) {
2912                 RESERVE_SPACE(8 + sizeof(nfs4_verifier));
2913                 WRITEMEM(&scd->se_clientid, 8);
2914                 WRITEMEM(&scd->se_confirm, sizeof(nfs4_verifier));
2915                 ADJUST_ARGS();
2916         }
2917         else if (nfserr == nfserr_clid_inuse) {
2918                 RESERVE_SPACE(8);
2919                 WRITE32(0);
2920                 WRITE32(0);
2921                 ADJUST_ARGS();
2922         }
2923         return nfserr;
2924 }
2925
2926 static __be32
2927 nfsd4_encode_write(struct nfsd4_compoundres *resp, __be32 nfserr, struct nfsd4_write *write)
2928 {
2929         __be32 *p;
2930
2931         if (!nfserr) {
2932                 RESERVE_SPACE(16);
2933                 WRITE32(write->wr_bytes_written);
2934                 WRITE32(write->wr_how_written);
2935                 WRITEMEM(write->wr_verifier.data, 8);
2936                 ADJUST_ARGS();
2937         }
2938         return nfserr;
2939 }
2940
2941 static __be32
2942 nfsd4_encode_exchange_id(struct nfsd4_compoundres *resp, int nfserr,
2943                          struct nfsd4_exchange_id *exid)
2944 {
2945         __be32 *p;
2946         char *major_id;
2947         char *server_scope;
2948         int major_id_sz;
2949         int server_scope_sz;
2950         uint64_t minor_id = 0;
2951
2952         if (nfserr)
2953                 return nfserr;
2954
2955         major_id = utsname()->nodename;
2956         major_id_sz = strlen(major_id);
2957         server_scope = utsname()->nodename;
2958         server_scope_sz = strlen(server_scope);
2959
2960         RESERVE_SPACE(
2961                 8 /* eir_clientid */ +
2962                 4 /* eir_sequenceid */ +
2963                 4 /* eir_flags */ +
2964                 4 /* spr_how (SP4_NONE) */ +
2965                 8 /* so_minor_id */ +
2966                 4 /* so_major_id.len */ +
2967                 (XDR_QUADLEN(major_id_sz) * 4) +
2968                 4 /* eir_server_scope.len */ +
2969                 (XDR_QUADLEN(server_scope_sz) * 4) +
2970                 4 /* eir_server_impl_id.count (0) */);
2971
2972         WRITEMEM(&exid->clientid, 8);
2973         WRITE32(exid->seqid);
2974         WRITE32(exid->flags);
2975
2976         /* state_protect4_r. Currently only support SP4_NONE */
2977         BUG_ON(exid->spa_how != SP4_NONE);
2978         WRITE32(exid->spa_how);
2979
2980         /* The server_owner struct */
2981         WRITE64(minor_id);      /* Minor id */
2982         /* major id */
2983         WRITE32(major_id_sz);
2984         WRITEMEM(major_id, major_id_sz);
2985
2986         /* Server scope */
2987         WRITE32(server_scope_sz);
2988         WRITEMEM(server_scope, server_scope_sz);
2989
2990         /* Implementation id */
2991         WRITE32(0);     /* zero length nfs_impl_id4 array */
2992         ADJUST_ARGS();
2993         return 0;
2994 }
2995
2996 static __be32
2997 nfsd4_encode_create_session(struct nfsd4_compoundres *resp, int nfserr,
2998                             struct nfsd4_create_session *sess)
2999 {
3000         __be32 *p;
3001
3002         if (nfserr)
3003                 return nfserr;
3004
3005         RESERVE_SPACE(24);
3006         WRITEMEM(sess->sessionid.data, NFS4_MAX_SESSIONID_LEN);
3007         WRITE32(sess->seqid);
3008         WRITE32(sess->flags);
3009         ADJUST_ARGS();
3010
3011         RESERVE_SPACE(28);
3012         WRITE32(0); /* headerpadsz */
3013         WRITE32(sess->fore_channel.maxreq_sz);
3014         WRITE32(sess->fore_channel.maxresp_sz);
3015         WRITE32(sess->fore_channel.maxresp_cached);
3016         WRITE32(sess->fore_channel.maxops);
3017         WRITE32(sess->fore_channel.maxreqs);
3018         WRITE32(sess->fore_channel.nr_rdma_attrs);
3019         ADJUST_ARGS();
3020
3021         if (sess->fore_channel.nr_rdma_attrs) {
3022                 RESERVE_SPACE(4);
3023                 WRITE32(sess->fore_channel.rdma_attrs);
3024                 ADJUST_ARGS();
3025         }
3026
3027         RESERVE_SPACE(28);
3028         WRITE32(0); /* headerpadsz */
3029         WRITE32(sess->back_channel.maxreq_sz);
3030         WRITE32(sess->back_channel.maxresp_sz);
3031         WRITE32(sess->back_channel.maxresp_cached);
3032         WRITE32(sess->back_channel.maxops);
3033         WRITE32(sess->back_channel.maxreqs);
3034         WRITE32(sess->back_channel.nr_rdma_attrs);
3035         ADJUST_ARGS();
3036
3037         if (sess->back_channel.nr_rdma_attrs) {
3038                 RESERVE_SPACE(4);
3039                 WRITE32(sess->back_channel.rdma_attrs);
3040                 ADJUST_ARGS();
3041         }
3042         return 0;
3043 }
3044
3045 static __be32
3046 nfsd4_encode_destroy_session(struct nfsd4_compoundres *resp, int nfserr,
3047                              struct nfsd4_destroy_session *destroy_session)
3048 {
3049         return nfserr;
3050 }
3051
3052 __be32
3053 nfsd4_encode_sequence(struct nfsd4_compoundres *resp, int nfserr,
3054                       struct nfsd4_sequence *seq)
3055 {
3056         __be32 *p;
3057
3058         if (nfserr)
3059                 return nfserr;
3060
3061         RESERVE_SPACE(NFS4_MAX_SESSIONID_LEN + 20);
3062         WRITEMEM(seq->sessionid.data, NFS4_MAX_SESSIONID_LEN);
3063         WRITE32(seq->seqid);
3064         WRITE32(seq->slotid);
3065         WRITE32(seq->maxslots);
3066         /*
3067          * FIXME: for now:
3068          *   target_maxslots = maxslots
3069          *   status_flags = 0
3070          */
3071         WRITE32(seq->maxslots);
3072         WRITE32(0);
3073
3074         ADJUST_ARGS();
3075         return 0;
3076 }
3077
3078 static __be32
3079 nfsd4_encode_noop(struct nfsd4_compoundres *resp, __be32 nfserr, void *p)
3080 {
3081         return nfserr;
3082 }
3083
3084 typedef __be32(* nfsd4_enc)(struct nfsd4_compoundres *, __be32, void *);
3085
3086 /*
3087  * Note: nfsd4_enc_ops vector is shared for v4.0 and v4.1
3088  * since we don't need to filter out obsolete ops as this is
3089  * done in the decoding phase.
3090  */
3091 static nfsd4_enc nfsd4_enc_ops[] = {
3092         [OP_ACCESS]             = (nfsd4_enc)nfsd4_encode_access,
3093         [OP_CLOSE]              = (nfsd4_enc)nfsd4_encode_close,
3094         [OP_COMMIT]             = (nfsd4_enc)nfsd4_encode_commit,
3095         [OP_CREATE]             = (nfsd4_enc)nfsd4_encode_create,
3096         [OP_DELEGPURGE]         = (nfsd4_enc)nfsd4_encode_noop,
3097         [OP_DELEGRETURN]        = (nfsd4_enc)nfsd4_encode_noop,
3098         [OP_GETATTR]            = (nfsd4_enc)nfsd4_encode_getattr,
3099         [OP_GETFH]              = (nfsd4_enc)nfsd4_encode_getfh,
3100         [OP_LINK]               = (nfsd4_enc)nfsd4_encode_link,
3101         [OP_LOCK]               = (nfsd4_enc)nfsd4_encode_lock,
3102         [OP_LOCKT]              = (nfsd4_enc)nfsd4_encode_lockt,
3103         [OP_LOCKU]              = (nfsd4_enc)nfsd4_encode_locku,
3104         [OP_LOOKUP]             = (nfsd4_enc)nfsd4_encode_noop,
3105         [OP_LOOKUPP]            = (nfsd4_enc)nfsd4_encode_noop,
3106         [OP_NVERIFY]            = (nfsd4_enc)nfsd4_encode_noop,
3107         [OP_OPEN]               = (nfsd4_enc)nfsd4_encode_open,
3108         [OP_OPENATTR]           = (nfsd4_enc)nfsd4_encode_noop,
3109         [OP_OPEN_CONFIRM]       = (nfsd4_enc)nfsd4_encode_open_confirm,
3110         [OP_OPEN_DOWNGRADE]     = (nfsd4_enc)nfsd4_encode_open_downgrade,
3111         [OP_PUTFH]              = (nfsd4_enc)nfsd4_encode_noop,
3112         [OP_PUTPUBFH]           = (nfsd4_enc)nfsd4_encode_noop,
3113         [OP_PUTROOTFH]          = (nfsd4_enc)nfsd4_encode_noop,
3114         [OP_READ]               = (nfsd4_enc)nfsd4_encode_read,
3115         [OP_READDIR]            = (nfsd4_enc)nfsd4_encode_readdir,
3116         [OP_READLINK]           = (nfsd4_enc)nfsd4_encode_readlink,
3117         [OP_REMOVE]             = (nfsd4_enc)nfsd4_encode_remove,
3118         [OP_RENAME]             = (nfsd4_enc)nfsd4_encode_rename,
3119         [OP_RENEW]              = (nfsd4_enc)nfsd4_encode_noop,
3120         [OP_RESTOREFH]          = (nfsd4_enc)nfsd4_encode_noop,
3121         [OP_SAVEFH]             = (nfsd4_enc)nfsd4_encode_noop,
3122         [OP_SECINFO]            = (nfsd4_enc)nfsd4_encode_secinfo,
3123         [OP_SETATTR]            = (nfsd4_enc)nfsd4_encode_setattr,
3124         [OP_SETCLIENTID]        = (nfsd4_enc)nfsd4_encode_setclientid,
3125         [OP_SETCLIENTID_CONFIRM] = (nfsd4_enc)nfsd4_encode_noop,
3126         [OP_VERIFY]             = (nfsd4_enc)nfsd4_encode_noop,
3127         [OP_WRITE]              = (nfsd4_enc)nfsd4_encode_write,
3128         [OP_RELEASE_LOCKOWNER]  = (nfsd4_enc)nfsd4_encode_noop,
3129
3130         /* NFSv4.1 operations */
3131         [OP_BACKCHANNEL_CTL]    = (nfsd4_enc)nfsd4_encode_noop,
3132         [OP_BIND_CONN_TO_SESSION] = (nfsd4_enc)nfsd4_encode_noop,
3133         [OP_EXCHANGE_ID]        = (nfsd4_enc)nfsd4_encode_exchange_id,
3134         [OP_CREATE_SESSION]     = (nfsd4_enc)nfsd4_encode_create_session,
3135         [OP_DESTROY_SESSION]    = (nfsd4_enc)nfsd4_encode_destroy_session,
3136         [OP_FREE_STATEID]       = (nfsd4_enc)nfsd4_encode_noop,
3137         [OP_GET_DIR_DELEGATION] = (nfsd4_enc)nfsd4_encode_noop,
3138         [OP_GETDEVICEINFO]      = (nfsd4_enc)nfsd4_encode_noop,
3139         [OP_GETDEVICELIST]      = (nfsd4_enc)nfsd4_encode_noop,
3140         [OP_LAYOUTCOMMIT]       = (nfsd4_enc)nfsd4_encode_noop,
3141         [OP_LAYOUTGET]          = (nfsd4_enc)nfsd4_encode_noop,
3142         [OP_LAYOUTRETURN]       = (nfsd4_enc)nfsd4_encode_noop,
3143         [OP_SECINFO_NO_NAME]    = (nfsd4_enc)nfsd4_encode_noop,
3144         [OP_SEQUENCE]           = (nfsd4_enc)nfsd4_encode_sequence,
3145         [OP_SET_SSV]            = (nfsd4_enc)nfsd4_encode_noop,
3146         [OP_TEST_STATEID]       = (nfsd4_enc)nfsd4_encode_noop,
3147         [OP_WANT_DELEGATION]    = (nfsd4_enc)nfsd4_encode_noop,
3148         [OP_DESTROY_CLIENTID]   = (nfsd4_enc)nfsd4_encode_noop,
3149         [OP_RECLAIM_COMPLETE]   = (nfsd4_enc)nfsd4_encode_noop,
3150 };
3151
3152 /*
3153  * Calculate the total amount of memory that the compound response has taken
3154  * after encoding the current operation.
3155  *
3156  * pad: add on 8 bytes for the next operation's op_code and status so that
3157  * there is room to cache a failure on the next operation.
3158  *
3159  * Compare this length to the session se_fmaxresp_cached.
3160  *
3161  * Our se_fmaxresp_cached will always be a multiple of PAGE_SIZE, and so
3162  * will be at least a page and will therefore hold the xdr_buf head.
3163  */
3164 static int nfsd4_check_drc_limit(struct nfsd4_compoundres *resp)
3165 {
3166         int status = 0;
3167         struct xdr_buf *xb = &resp->rqstp->rq_res;
3168         struct nfsd4_compoundargs *args = resp->rqstp->rq_argp;
3169         struct nfsd4_session *session = NULL;
3170         struct nfsd4_slot *slot = resp->cstate.slot;
3171         u32 length, tlen = 0, pad = 8;
3172
3173         if (!nfsd4_has_session(&resp->cstate))
3174                 return status;
3175
3176         session = resp->cstate.session;
3177         if (session == NULL || slot->sl_cache_entry.ce_cachethis == 0)
3178                 return status;
3179
3180         if (resp->opcnt >= args->opcnt)
3181                 pad = 0; /* this is the last operation */
3182
3183         if (xb->page_len == 0) {
3184                 length = (char *)resp->p - (char *)xb->head[0].iov_base + pad;
3185         } else {
3186                 if (xb->tail[0].iov_base && xb->tail[0].iov_len > 0)
3187                         tlen = (char *)resp->p - (char *)xb->tail[0].iov_base;
3188
3189                 length = xb->head[0].iov_len + xb->page_len + tlen + pad;
3190         }
3191         dprintk("%s length %u, xb->page_len %u tlen %u pad %u\n", __func__,
3192                 length, xb->page_len, tlen, pad);
3193
3194         if (length <= session->se_fmaxresp_cached)
3195                 return status;
3196         else
3197                 return nfserr_rep_too_big_to_cache;
3198 }
3199
3200 void
3201 nfsd4_encode_operation(struct nfsd4_compoundres *resp, struct nfsd4_op *op)
3202 {
3203         __be32 *statp;
3204         __be32 *p;
3205
3206         RESERVE_SPACE(8);
3207         WRITE32(op->opnum);
3208         statp = p++;    /* to be backfilled at the end */
3209         ADJUST_ARGS();
3210
3211         if (op->opnum == OP_ILLEGAL)
3212                 goto status;
3213         BUG_ON(op->opnum < 0 || op->opnum >= ARRAY_SIZE(nfsd4_enc_ops) ||
3214                !nfsd4_enc_ops[op->opnum]);
3215         op->status = nfsd4_enc_ops[op->opnum](resp, op->status, &op->u);
3216         /* nfsd4_check_drc_limit guarantees enough room for error status */
3217         if (!op->status && nfsd4_check_drc_limit(resp))
3218                 op->status = nfserr_rep_too_big_to_cache;
3219 status:
3220         /*
3221          * Note: We write the status directly, instead of using WRITE32(),
3222          * since it is already in network byte order.
3223          */
3224         *statp = op->status;
3225 }
3226
3227 /* 
3228  * Encode the reply stored in the stateowner reply cache 
3229  * 
3230  * XDR note: do not encode rp->rp_buflen: the buffer contains the
3231  * previously sent already encoded operation.
3232  *
3233  * called with nfs4_lock_state() held
3234  */
3235 void
3236 nfsd4_encode_replay(struct nfsd4_compoundres *resp, struct nfsd4_op *op)
3237 {
3238         __be32 *p;
3239         struct nfs4_replay *rp = op->replay;
3240
3241         BUG_ON(!rp);
3242
3243         RESERVE_SPACE(8);
3244         WRITE32(op->opnum);
3245         *p++ = rp->rp_status;  /* already xdr'ed */
3246         ADJUST_ARGS();
3247
3248         RESERVE_SPACE(rp->rp_buflen);
3249         WRITEMEM(rp->rp_buf, rp->rp_buflen);
3250         ADJUST_ARGS();
3251 }
3252
3253 int
3254 nfs4svc_encode_voidres(struct svc_rqst *rqstp, __be32 *p, void *dummy)
3255 {
3256         return xdr_ressize_check(rqstp, p);
3257 }
3258
3259 void nfsd4_release_compoundargs(struct nfsd4_compoundargs *args)
3260 {
3261         if (args->ops != args->iops) {
3262                 kfree(args->ops);
3263                 args->ops = args->iops;
3264         }
3265         kfree(args->tmpp);
3266         args->tmpp = NULL;
3267         while (args->to_free) {
3268                 struct tmpbuf *tb = args->to_free;
3269                 args->to_free = tb->next;
3270                 tb->release(tb->buf);
3271                 kfree(tb);
3272         }
3273 }
3274
3275 int
3276 nfs4svc_decode_compoundargs(struct svc_rqst *rqstp, __be32 *p, struct nfsd4_compoundargs *args)
3277 {
3278         __be32 status;
3279
3280         args->p = p;
3281         args->end = rqstp->rq_arg.head[0].iov_base + rqstp->rq_arg.head[0].iov_len;
3282         args->pagelist = rqstp->rq_arg.pages;
3283         args->pagelen = rqstp->rq_arg.page_len;
3284         args->tmpp = NULL;
3285         args->to_free = NULL;
3286         args->ops = args->iops;
3287         args->rqstp = rqstp;
3288
3289         status = nfsd4_decode_compound(args);
3290         if (status) {
3291                 nfsd4_release_compoundargs(args);
3292         }
3293         return !status;
3294 }
3295
3296 int
3297 nfs4svc_encode_compoundres(struct svc_rqst *rqstp, __be32 *p, struct nfsd4_compoundres *resp)
3298 {
3299         /*
3300          * All that remains is to write the tag and operation count...
3301          */
3302         struct kvec *iov;
3303         p = resp->tagp;
3304         *p++ = htonl(resp->taglen);
3305         memcpy(p, resp->tag, resp->taglen);
3306         p += XDR_QUADLEN(resp->taglen);
3307         *p++ = htonl(resp->opcnt);
3308
3309         if (rqstp->rq_res.page_len) 
3310                 iov = &rqstp->rq_res.tail[0];
3311         else
3312                 iov = &rqstp->rq_res.head[0];
3313         iov->iov_len = ((char*)resp->p) - (char*)iov->iov_base;
3314         BUG_ON(iov->iov_len > PAGE_SIZE);
3315         if (nfsd4_has_session(&resp->cstate)) {
3316                 if (resp->cstate.status == nfserr_replay_cache &&
3317                                 !nfsd4_not_cached(resp)) {
3318                         iov->iov_len = resp->cstate.iovlen;
3319                 } else {
3320                         nfsd4_store_cache_entry(resp);
3321                         dprintk("%s: SET SLOT STATE TO AVAILABLE\n", __func__);
3322                         resp->cstate.slot->sl_inuse = 0;
3323                 }
3324                 if (resp->cstate.session)
3325                         nfsd4_put_session(resp->cstate.session);
3326         }
3327         return 1;
3328 }
3329
3330 /*
3331  * Local variables:
3332  *  c-basic-offset: 8
3333  * End:
3334  */