[PATCH] knfsd: nfsd4: actually use all the pieces to implement referrals
[safe/jmp/linux-2.6] / fs / nfsd / nfs4xdr.c
1 /*
2  *  fs/nfs/nfs4xdr.c
3  *
4  *  Server-side XDR for NFSv4
5  *
6  *  Copyright (c) 2002 The Regents of the University of Michigan.
7  *  All rights reserved.
8  *
9  *  Kendrick Smith <kmsmith@umich.edu>
10  *  Andy Adamson   <andros@umich.edu>
11  *
12  *  Redistribution and use in source and binary forms, with or without
13  *  modification, are permitted provided that the following conditions
14  *  are met:
15  *
16  *  1. Redistributions of source code must retain the above copyright
17  *     notice, this list of conditions and the following disclaimer.
18  *  2. Redistributions in binary form must reproduce the above copyright
19  *     notice, this list of conditions and the following disclaimer in the
20  *     documentation and/or other materials provided with the distribution.
21  *  3. Neither the name of the University nor the names of its
22  *     contributors may be used to endorse or promote products derived
23  *     from this software without specific prior written permission.
24  *
25  *  THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESS OR IMPLIED
26  *  WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
27  *  MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
28  *  DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
29  *  FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
30  *  CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
31  *  SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR
32  *  BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
33  *  LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
34  *  NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
35  *  SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
36  *
37  * TODO: Neil Brown made the following observation:  We currently
38  * initially reserve NFSD_BUFSIZE space on the transmit queue and
39  * never release any of that until the request is complete.
40  * It would be good to calculate a new maximum response size while
41  * decoding the COMPOUND, and call svc_reserve with this number
42  * at the end of nfs4svc_decode_compoundargs.
43  */
44
45 #include <linux/param.h>
46 #include <linux/smp.h>
47 #include <linux/smp_lock.h>
48 #include <linux/fs.h>
49 #include <linux/namei.h>
50 #include <linux/vfs.h>
51 #include <linux/sunrpc/xdr.h>
52 #include <linux/sunrpc/svc.h>
53 #include <linux/sunrpc/clnt.h>
54 #include <linux/nfsd/nfsd.h>
55 #include <linux/nfsd/state.h>
56 #include <linux/nfsd/xdr4.h>
57 #include <linux/nfsd_idmap.h>
58 #include <linux/nfs4.h>
59 #include <linux/nfs4_acl.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 int
72 check_filename(char *str, int len, int 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 /*
87  * START OF "GENERIC" DECODE ROUTINES.
88  *   These may look a little ugly since they are imported from a "generic"
89  * set of XDR encode/decode routines which are intended to be shared by
90  * all of our NFSv4 implementations (OpenBSD, MacOS X...).
91  *
92  * If the pain of reading these is too great, it should be a straightforward
93  * task to translate them into Linux-specific versions which are more
94  * consistent with the style used in NFSv2/v3...
95  */
96 #define DECODE_HEAD                             \
97         u32 *p;                                 \
98         int status
99 #define DECODE_TAIL                             \
100         status = 0;                             \
101 out:                                            \
102         return status;                          \
103 xdr_error:                                      \
104         printk(KERN_NOTICE "xdr error! (%s:%d)\n", __FILE__, __LINE__); \
105         status = nfserr_bad_xdr;                \
106         goto out
107
108 #define READ32(x)         (x) = ntohl(*p++)
109 #define READ64(x)         do {                  \
110         (x) = (u64)ntohl(*p++) << 32;           \
111         (x) |= ntohl(*p++);                     \
112 } while (0)
113 #define READTIME(x)       do {                  \
114         p++;                                    \
115         (x) = ntohl(*p++);                      \
116         p++;                                    \
117 } while (0)
118 #define READMEM(x,nbytes) do {                  \
119         x = (char *)p;                          \
120         p += XDR_QUADLEN(nbytes);               \
121 } while (0)
122 #define SAVEMEM(x,nbytes) do {                  \
123         if (!(x = (p==argp->tmp || p == argp->tmpp) ? \
124                 savemem(argp, p, nbytes) :      \
125                 (char *)p)) {                   \
126                 printk(KERN_NOTICE "xdr error! (%s:%d)\n", __FILE__, __LINE__); \
127                 goto xdr_error;                 \
128                 }                               \
129         p += XDR_QUADLEN(nbytes);               \
130 } while (0)
131 #define COPYMEM(x,nbytes) do {                  \
132         memcpy((x), p, nbytes);                 \
133         p += XDR_QUADLEN(nbytes);               \
134 } while (0)
135
136 /* READ_BUF, read_buf(): nbytes must be <= PAGE_SIZE */
137 #define READ_BUF(nbytes)  do {                  \
138         if (nbytes <= (u32)((char *)argp->end - (char *)argp->p)) {     \
139                 p = argp->p;                    \
140                 argp->p += XDR_QUADLEN(nbytes); \
141         } else if (!(p = read_buf(argp, nbytes))) { \
142                 printk(KERN_NOTICE "xdr error! (%s:%d)\n", __FILE__, __LINE__); \
143                 goto xdr_error;                 \
144         }                                       \
145 } while (0)
146
147 static u32 *read_buf(struct nfsd4_compoundargs *argp, int nbytes)
148 {
149         /* We want more bytes than seem to be available.
150          * Maybe we need a new page, maybe we have just run out
151          */
152         int avail = (char*)argp->end - (char*)argp->p;
153         u32 *p;
154         if (avail + argp->pagelen < nbytes)
155                 return NULL;
156         if (avail + PAGE_SIZE < nbytes) /* need more than a page !! */
157                 return NULL;
158         /* ok, we can do it with the current plus the next page */
159         if (nbytes <= sizeof(argp->tmp))
160                 p = argp->tmp;
161         else {
162                 kfree(argp->tmpp);
163                 p = argp->tmpp = kmalloc(nbytes, GFP_KERNEL);
164                 if (!p)
165                         return NULL;
166                 
167         }
168         memcpy(p, argp->p, avail);
169         /* step to next page */
170         argp->p = page_address(argp->pagelist[0]);
171         argp->pagelist++;
172         if (argp->pagelen < PAGE_SIZE) {
173                 argp->end = p + (argp->pagelen>>2);
174                 argp->pagelen = 0;
175         } else {
176                 argp->end = p + (PAGE_SIZE>>2);
177                 argp->pagelen -= PAGE_SIZE;
178         }
179         memcpy(((char*)p)+avail, argp->p, (nbytes - avail));
180         argp->p += XDR_QUADLEN(nbytes - avail);
181         return p;
182 }
183
184 static int
185 defer_free(struct nfsd4_compoundargs *argp,
186                 void (*release)(const void *), void *p)
187 {
188         struct tmpbuf *tb;
189
190         tb = kmalloc(sizeof(*tb), GFP_KERNEL);
191         if (!tb)
192                 return -ENOMEM;
193         tb->buf = p;
194         tb->release = release;
195         tb->next = argp->to_free;
196         argp->to_free = tb;
197         return 0;
198 }
199
200 static char *savemem(struct nfsd4_compoundargs *argp, u32 *p, int nbytes)
201 {
202         void *new = NULL;
203         if (p == argp->tmp) {
204                 new = kmalloc(nbytes, GFP_KERNEL);
205                 if (!new) return NULL;
206                 p = new;
207                 memcpy(p, argp->tmp, nbytes);
208         } else {
209                 BUG_ON(p != argp->tmpp);
210                 argp->tmpp = NULL;
211         }
212         if (defer_free(argp, kfree, p)) {
213                 kfree(new);
214                 return NULL;
215         } else
216                 return (char *)p;
217 }
218
219
220 static int
221 nfsd4_decode_bitmap(struct nfsd4_compoundargs *argp, u32 *bmval)
222 {
223         u32 bmlen;
224         DECODE_HEAD;
225
226         bmval[0] = 0;
227         bmval[1] = 0;
228
229         READ_BUF(4);
230         READ32(bmlen);
231         if (bmlen > 1000)
232                 goto xdr_error;
233
234         READ_BUF(bmlen << 2);
235         if (bmlen > 0)
236                 READ32(bmval[0]);
237         if (bmlen > 1)
238                 READ32(bmval[1]);
239
240         DECODE_TAIL;
241 }
242
243 static int
244 nfsd4_decode_fattr(struct nfsd4_compoundargs *argp, u32 *bmval, struct iattr *iattr,
245     struct nfs4_acl **acl)
246 {
247         int expected_len, len = 0;
248         u32 dummy32;
249         char *buf;
250
251         DECODE_HEAD;
252         iattr->ia_valid = 0;
253         if ((status = nfsd4_decode_bitmap(argp, bmval)))
254                 return status;
255
256         /*
257          * According to spec, unsupported attributes return ERR_NOTSUPP;
258          * read-only attributes return ERR_INVAL.
259          */
260         if ((bmval[0] & ~NFSD_SUPPORTED_ATTRS_WORD0) || (bmval[1] & ~NFSD_SUPPORTED_ATTRS_WORD1))
261                 return nfserr_attrnotsupp;
262         if ((bmval[0] & ~NFSD_WRITEABLE_ATTRS_WORD0) || (bmval[1] & ~NFSD_WRITEABLE_ATTRS_WORD1))
263                 return nfserr_inval;
264
265         READ_BUF(4);
266         READ32(expected_len);
267
268         if (bmval[0] & FATTR4_WORD0_SIZE) {
269                 READ_BUF(8);
270                 len += 8;
271                 READ64(iattr->ia_size);
272                 iattr->ia_valid |= ATTR_SIZE;
273         }
274         if (bmval[0] & FATTR4_WORD0_ACL) {
275                 int nace, i;
276                 struct nfs4_ace ace;
277
278                 READ_BUF(4); len += 4;
279                 READ32(nace);
280
281                 *acl = nfs4_acl_new();
282                 if (*acl == NULL) {
283                         status = -ENOMEM;
284                         goto out_nfserr;
285                 }
286                 defer_free(argp, (void (*)(const void *))nfs4_acl_free, *acl);
287
288                 for (i = 0; i < nace; i++) {
289                         READ_BUF(16); len += 16;
290                         READ32(ace.type);
291                         READ32(ace.flag);
292                         READ32(ace.access_mask);
293                         READ32(dummy32);
294                         READ_BUF(dummy32);
295                         len += XDR_QUADLEN(dummy32) << 2;
296                         READMEM(buf, dummy32);
297                         ace.whotype = nfs4_acl_get_whotype(buf, dummy32);
298                         status = 0;
299                         if (ace.whotype != NFS4_ACL_WHO_NAMED)
300                                 ace.who = 0;
301                         else if (ace.flag & NFS4_ACE_IDENTIFIER_GROUP)
302                                 status = nfsd_map_name_to_gid(argp->rqstp,
303                                                 buf, dummy32, &ace.who);
304                         else
305                                 status = nfsd_map_name_to_uid(argp->rqstp,
306                                                 buf, dummy32, &ace.who);
307                         if (status)
308                                 goto out_nfserr;
309                         status = nfs4_acl_add_ace(*acl, ace.type, ace.flag,
310                                  ace.access_mask, ace.whotype, ace.who);
311                         if (status)
312                                 goto out_nfserr;
313                 }
314         } else
315                 *acl = NULL;
316         if (bmval[1] & FATTR4_WORD1_MODE) {
317                 READ_BUF(4);
318                 len += 4;
319                 READ32(iattr->ia_mode);
320                 iattr->ia_mode &= (S_IFMT | S_IALLUGO);
321                 iattr->ia_valid |= ATTR_MODE;
322         }
323         if (bmval[1] & FATTR4_WORD1_OWNER) {
324                 READ_BUF(4);
325                 len += 4;
326                 READ32(dummy32);
327                 READ_BUF(dummy32);
328                 len += (XDR_QUADLEN(dummy32) << 2);
329                 READMEM(buf, dummy32);
330                 if ((status = nfsd_map_name_to_uid(argp->rqstp, buf, dummy32, &iattr->ia_uid)))
331                         goto out_nfserr;
332                 iattr->ia_valid |= ATTR_UID;
333         }
334         if (bmval[1] & FATTR4_WORD1_OWNER_GROUP) {
335                 READ_BUF(4);
336                 len += 4;
337                 READ32(dummy32);
338                 READ_BUF(dummy32);
339                 len += (XDR_QUADLEN(dummy32) << 2);
340                 READMEM(buf, dummy32);
341                 if ((status = nfsd_map_name_to_gid(argp->rqstp, buf, dummy32, &iattr->ia_gid)))
342                         goto out_nfserr;
343                 iattr->ia_valid |= ATTR_GID;
344         }
345         if (bmval[1] & FATTR4_WORD1_TIME_ACCESS_SET) {
346                 READ_BUF(4);
347                 len += 4;
348                 READ32(dummy32);
349                 switch (dummy32) {
350                 case NFS4_SET_TO_CLIENT_TIME:
351                         /* We require the high 32 bits of 'seconds' to be 0, and we ignore
352                            all 32 bits of 'nseconds'. */
353                         READ_BUF(12);
354                         len += 12;
355                         READ32(dummy32);
356                         if (dummy32)
357                                 return nfserr_inval;
358                         READ32(iattr->ia_atime.tv_sec);
359                         READ32(iattr->ia_atime.tv_nsec);
360                         if (iattr->ia_atime.tv_nsec >= (u32)1000000000)
361                                 return nfserr_inval;
362                         iattr->ia_valid |= (ATTR_ATIME | ATTR_ATIME_SET);
363                         break;
364                 case NFS4_SET_TO_SERVER_TIME:
365                         iattr->ia_valid |= ATTR_ATIME;
366                         break;
367                 default:
368                         goto xdr_error;
369                 }
370         }
371         if (bmval[1] & FATTR4_WORD1_TIME_METADATA) {
372                 /* We require the high 32 bits of 'seconds' to be 0, and we ignore
373                    all 32 bits of 'nseconds'. */
374                 READ_BUF(12);
375                 len += 12;
376                 READ32(dummy32);
377                 if (dummy32)
378                         return nfserr_inval;
379                 READ32(iattr->ia_ctime.tv_sec);
380                 READ32(iattr->ia_ctime.tv_nsec);
381                 if (iattr->ia_ctime.tv_nsec >= (u32)1000000000)
382                         return nfserr_inval;
383                 iattr->ia_valid |= ATTR_CTIME;
384         }
385         if (bmval[1] & FATTR4_WORD1_TIME_MODIFY_SET) {
386                 READ_BUF(4);
387                 len += 4;
388                 READ32(dummy32);
389                 switch (dummy32) {
390                 case NFS4_SET_TO_CLIENT_TIME:
391                         /* We require the high 32 bits of 'seconds' to be 0, and we ignore
392                            all 32 bits of 'nseconds'. */
393                         READ_BUF(12);
394                         len += 12;
395                         READ32(dummy32);
396                         if (dummy32)
397                                 return nfserr_inval;
398                         READ32(iattr->ia_mtime.tv_sec);
399                         READ32(iattr->ia_mtime.tv_nsec);
400                         if (iattr->ia_mtime.tv_nsec >= (u32)1000000000)
401                                 return nfserr_inval;
402                         iattr->ia_valid |= (ATTR_MTIME | ATTR_MTIME_SET);
403                         break;
404                 case NFS4_SET_TO_SERVER_TIME:
405                         iattr->ia_valid |= ATTR_MTIME;
406                         break;
407                 default:
408                         goto xdr_error;
409                 }
410         }
411         if (len != expected_len)
412                 goto xdr_error;
413
414         DECODE_TAIL;
415
416 out_nfserr:
417         status = nfserrno(status);
418         goto out;
419 }
420
421 static int
422 nfsd4_decode_access(struct nfsd4_compoundargs *argp, struct nfsd4_access *access)
423 {
424         DECODE_HEAD;
425
426         READ_BUF(4);
427         READ32(access->ac_req_access);
428
429         DECODE_TAIL;
430 }
431
432 static int
433 nfsd4_decode_close(struct nfsd4_compoundargs *argp, struct nfsd4_close *close)
434 {
435         DECODE_HEAD;
436
437         close->cl_stateowner = NULL;
438         READ_BUF(4 + sizeof(stateid_t));
439         READ32(close->cl_seqid);
440         READ32(close->cl_stateid.si_generation);
441         COPYMEM(&close->cl_stateid.si_opaque, sizeof(stateid_opaque_t));
442
443         DECODE_TAIL;
444 }
445
446
447 static int
448 nfsd4_decode_commit(struct nfsd4_compoundargs *argp, struct nfsd4_commit *commit)
449 {
450         DECODE_HEAD;
451
452         READ_BUF(12);
453         READ64(commit->co_offset);
454         READ32(commit->co_count);
455
456         DECODE_TAIL;
457 }
458
459 static int
460 nfsd4_decode_create(struct nfsd4_compoundargs *argp, struct nfsd4_create *create)
461 {
462         DECODE_HEAD;
463
464         READ_BUF(4);
465         READ32(create->cr_type);
466         switch (create->cr_type) {
467         case NF4LNK:
468                 READ_BUF(4);
469                 READ32(create->cr_linklen);
470                 READ_BUF(create->cr_linklen);
471                 SAVEMEM(create->cr_linkname, create->cr_linklen);
472                 break;
473         case NF4BLK:
474         case NF4CHR:
475                 READ_BUF(8);
476                 READ32(create->cr_specdata1);
477                 READ32(create->cr_specdata2);
478                 break;
479         case NF4SOCK:
480         case NF4FIFO:
481         case NF4DIR:
482         default:
483                 break;
484         }
485
486         READ_BUF(4);
487         READ32(create->cr_namelen);
488         READ_BUF(create->cr_namelen);
489         SAVEMEM(create->cr_name, create->cr_namelen);
490         if ((status = check_filename(create->cr_name, create->cr_namelen, nfserr_inval)))
491                 return status;
492
493         if ((status = nfsd4_decode_fattr(argp, create->cr_bmval, &create->cr_iattr, &create->cr_acl)))
494                 goto out;
495
496         DECODE_TAIL;
497 }
498
499 static inline int
500 nfsd4_decode_delegreturn(struct nfsd4_compoundargs *argp, struct nfsd4_delegreturn *dr)
501 {
502         DECODE_HEAD;
503
504         READ_BUF(sizeof(stateid_t));
505         READ32(dr->dr_stateid.si_generation);
506         COPYMEM(&dr->dr_stateid.si_opaque, sizeof(stateid_opaque_t));
507
508         DECODE_TAIL;
509 }
510
511 static inline int
512 nfsd4_decode_getattr(struct nfsd4_compoundargs *argp, struct nfsd4_getattr *getattr)
513 {
514         return nfsd4_decode_bitmap(argp, getattr->ga_bmval);
515 }
516
517 static int
518 nfsd4_decode_link(struct nfsd4_compoundargs *argp, struct nfsd4_link *link)
519 {
520         DECODE_HEAD;
521
522         READ_BUF(4);
523         READ32(link->li_namelen);
524         READ_BUF(link->li_namelen);
525         SAVEMEM(link->li_name, link->li_namelen);
526         if ((status = check_filename(link->li_name, link->li_namelen, nfserr_inval)))
527                 return status;
528
529         DECODE_TAIL;
530 }
531
532 static int
533 nfsd4_decode_lock(struct nfsd4_compoundargs *argp, struct nfsd4_lock *lock)
534 {
535         DECODE_HEAD;
536
537         lock->lk_replay_owner = NULL;
538         /*
539         * type, reclaim(boolean), offset, length, new_lock_owner(boolean)
540         */
541         READ_BUF(28);
542         READ32(lock->lk_type);
543         if ((lock->lk_type < NFS4_READ_LT) || (lock->lk_type > NFS4_WRITEW_LT))
544                 goto xdr_error;
545         READ32(lock->lk_reclaim);
546         READ64(lock->lk_offset);
547         READ64(lock->lk_length);
548         READ32(lock->lk_is_new);
549
550         if (lock->lk_is_new) {
551                 READ_BUF(36);
552                 READ32(lock->lk_new_open_seqid);
553                 READ32(lock->lk_new_open_stateid.si_generation);
554
555                 COPYMEM(&lock->lk_new_open_stateid.si_opaque, sizeof(stateid_opaque_t));
556                 READ32(lock->lk_new_lock_seqid);
557                 COPYMEM(&lock->lk_new_clientid, sizeof(clientid_t));
558                 READ32(lock->lk_new_owner.len);
559                 READ_BUF(lock->lk_new_owner.len);
560                 READMEM(lock->lk_new_owner.data, lock->lk_new_owner.len);
561         } else {
562                 READ_BUF(20);
563                 READ32(lock->lk_old_lock_stateid.si_generation);
564                 COPYMEM(&lock->lk_old_lock_stateid.si_opaque, sizeof(stateid_opaque_t));
565                 READ32(lock->lk_old_lock_seqid);
566         }
567
568         DECODE_TAIL;
569 }
570
571 static int
572 nfsd4_decode_lockt(struct nfsd4_compoundargs *argp, struct nfsd4_lockt *lockt)
573 {
574         DECODE_HEAD;
575                         
576         READ_BUF(32);
577         READ32(lockt->lt_type);
578         if((lockt->lt_type < NFS4_READ_LT) || (lockt->lt_type > NFS4_WRITEW_LT))
579                 goto xdr_error;
580         READ64(lockt->lt_offset);
581         READ64(lockt->lt_length);
582         COPYMEM(&lockt->lt_clientid, 8);
583         READ32(lockt->lt_owner.len);
584         READ_BUF(lockt->lt_owner.len);
585         READMEM(lockt->lt_owner.data, lockt->lt_owner.len);
586
587         DECODE_TAIL;
588 }
589
590 static int
591 nfsd4_decode_locku(struct nfsd4_compoundargs *argp, struct nfsd4_locku *locku)
592 {
593         DECODE_HEAD;
594
595         locku->lu_stateowner = NULL;
596         READ_BUF(24 + sizeof(stateid_t));
597         READ32(locku->lu_type);
598         if ((locku->lu_type < NFS4_READ_LT) || (locku->lu_type > NFS4_WRITEW_LT))
599                 goto xdr_error;
600         READ32(locku->lu_seqid);
601         READ32(locku->lu_stateid.si_generation);
602         COPYMEM(&locku->lu_stateid.si_opaque, sizeof(stateid_opaque_t));
603         READ64(locku->lu_offset);
604         READ64(locku->lu_length);
605
606         DECODE_TAIL;
607 }
608
609 static int
610 nfsd4_decode_lookup(struct nfsd4_compoundargs *argp, struct nfsd4_lookup *lookup)
611 {
612         DECODE_HEAD;
613
614         READ_BUF(4);
615         READ32(lookup->lo_len);
616         READ_BUF(lookup->lo_len);
617         SAVEMEM(lookup->lo_name, lookup->lo_len);
618         if ((status = check_filename(lookup->lo_name, lookup->lo_len, nfserr_noent)))
619                 return status;
620
621         DECODE_TAIL;
622 }
623
624 static int
625 nfsd4_decode_open(struct nfsd4_compoundargs *argp, struct nfsd4_open *open)
626 {
627         DECODE_HEAD;
628
629         memset(open->op_bmval, 0, sizeof(open->op_bmval));
630         open->op_iattr.ia_valid = 0;
631         open->op_stateowner = NULL;
632
633         /* seqid, share_access, share_deny, clientid, ownerlen */
634         READ_BUF(16 + sizeof(clientid_t));
635         READ32(open->op_seqid);
636         READ32(open->op_share_access);
637         READ32(open->op_share_deny);
638         COPYMEM(&open->op_clientid, sizeof(clientid_t));
639         READ32(open->op_owner.len);
640
641         /* owner, open_flag */
642         READ_BUF(open->op_owner.len + 4);
643         SAVEMEM(open->op_owner.data, open->op_owner.len);
644         READ32(open->op_create);
645         switch (open->op_create) {
646         case NFS4_OPEN_NOCREATE:
647                 break;
648         case NFS4_OPEN_CREATE:
649                 READ_BUF(4);
650                 READ32(open->op_createmode);
651                 switch (open->op_createmode) {
652                 case NFS4_CREATE_UNCHECKED:
653                 case NFS4_CREATE_GUARDED:
654                         if ((status = nfsd4_decode_fattr(argp, open->op_bmval, &open->op_iattr, &open->op_acl)))
655                                 goto out;
656                         break;
657                 case NFS4_CREATE_EXCLUSIVE:
658                         READ_BUF(8);
659                         COPYMEM(open->op_verf.data, 8);
660                         break;
661                 default:
662                         goto xdr_error;
663                 }
664                 break;
665         default:
666                 goto xdr_error;
667         }
668
669         /* open_claim */
670         READ_BUF(4);
671         READ32(open->op_claim_type);
672         switch (open->op_claim_type) {
673         case NFS4_OPEN_CLAIM_NULL:
674         case NFS4_OPEN_CLAIM_DELEGATE_PREV:
675                 READ_BUF(4);
676                 READ32(open->op_fname.len);
677                 READ_BUF(open->op_fname.len);
678                 SAVEMEM(open->op_fname.data, open->op_fname.len);
679                 if ((status = check_filename(open->op_fname.data, open->op_fname.len, nfserr_inval)))
680                         return status;
681                 break;
682         case NFS4_OPEN_CLAIM_PREVIOUS:
683                 READ_BUF(4);
684                 READ32(open->op_delegate_type);
685                 break;
686         case NFS4_OPEN_CLAIM_DELEGATE_CUR:
687                 READ_BUF(sizeof(stateid_t) + 4);
688                 COPYMEM(&open->op_delegate_stateid, sizeof(stateid_t));
689                 READ32(open->op_fname.len);
690                 READ_BUF(open->op_fname.len);
691                 SAVEMEM(open->op_fname.data, open->op_fname.len);
692                 if ((status = check_filename(open->op_fname.data, open->op_fname.len, nfserr_inval)))
693                         return status;
694                 break;
695         default:
696                 goto xdr_error;
697         }
698
699         DECODE_TAIL;
700 }
701
702 static int
703 nfsd4_decode_open_confirm(struct nfsd4_compoundargs *argp, struct nfsd4_open_confirm *open_conf)
704 {
705         DECODE_HEAD;
706                     
707         open_conf->oc_stateowner = NULL;
708         READ_BUF(4 + sizeof(stateid_t));
709         READ32(open_conf->oc_req_stateid.si_generation);
710         COPYMEM(&open_conf->oc_req_stateid.si_opaque, sizeof(stateid_opaque_t));
711         READ32(open_conf->oc_seqid);
712                                                         
713         DECODE_TAIL;
714 }
715
716 static int
717 nfsd4_decode_open_downgrade(struct nfsd4_compoundargs *argp, struct nfsd4_open_downgrade *open_down)
718 {
719         DECODE_HEAD;
720                     
721         open_down->od_stateowner = NULL;
722         READ_BUF(12 + sizeof(stateid_t));
723         READ32(open_down->od_stateid.si_generation);
724         COPYMEM(&open_down->od_stateid.si_opaque, sizeof(stateid_opaque_t));
725         READ32(open_down->od_seqid);
726         READ32(open_down->od_share_access);
727         READ32(open_down->od_share_deny);
728                                                         
729         DECODE_TAIL;
730 }
731
732 static int
733 nfsd4_decode_putfh(struct nfsd4_compoundargs *argp, struct nfsd4_putfh *putfh)
734 {
735         DECODE_HEAD;
736
737         READ_BUF(4);
738         READ32(putfh->pf_fhlen);
739         if (putfh->pf_fhlen > NFS4_FHSIZE)
740                 goto xdr_error;
741         READ_BUF(putfh->pf_fhlen);
742         SAVEMEM(putfh->pf_fhval, putfh->pf_fhlen);
743
744         DECODE_TAIL;
745 }
746
747 static int
748 nfsd4_decode_read(struct nfsd4_compoundargs *argp, struct nfsd4_read *read)
749 {
750         DECODE_HEAD;
751
752         READ_BUF(sizeof(stateid_t) + 12);
753         READ32(read->rd_stateid.si_generation);
754         COPYMEM(&read->rd_stateid.si_opaque, sizeof(stateid_opaque_t));
755         READ64(read->rd_offset);
756         READ32(read->rd_length);
757
758         DECODE_TAIL;
759 }
760
761 static int
762 nfsd4_decode_readdir(struct nfsd4_compoundargs *argp, struct nfsd4_readdir *readdir)
763 {
764         DECODE_HEAD;
765
766         READ_BUF(24);
767         READ64(readdir->rd_cookie);
768         COPYMEM(readdir->rd_verf.data, sizeof(readdir->rd_verf.data));
769         READ32(readdir->rd_dircount);    /* just in case you needed a useless field... */
770         READ32(readdir->rd_maxcount);
771         if ((status = nfsd4_decode_bitmap(argp, readdir->rd_bmval)))
772                 goto out;
773
774         DECODE_TAIL;
775 }
776
777 static int
778 nfsd4_decode_remove(struct nfsd4_compoundargs *argp, struct nfsd4_remove *remove)
779 {
780         DECODE_HEAD;
781
782         READ_BUF(4);
783         READ32(remove->rm_namelen);
784         READ_BUF(remove->rm_namelen);
785         SAVEMEM(remove->rm_name, remove->rm_namelen);
786         if ((status = check_filename(remove->rm_name, remove->rm_namelen, nfserr_noent)))
787                 return status;
788
789         DECODE_TAIL;
790 }
791
792 static int
793 nfsd4_decode_rename(struct nfsd4_compoundargs *argp, struct nfsd4_rename *rename)
794 {
795         DECODE_HEAD;
796
797         READ_BUF(4);
798         READ32(rename->rn_snamelen);
799         READ_BUF(rename->rn_snamelen + 4);
800         SAVEMEM(rename->rn_sname, rename->rn_snamelen);
801         READ32(rename->rn_tnamelen);
802         READ_BUF(rename->rn_tnamelen);
803         SAVEMEM(rename->rn_tname, rename->rn_tnamelen);
804         if ((status = check_filename(rename->rn_sname, rename->rn_snamelen, nfserr_noent)))
805                 return status;
806         if ((status = check_filename(rename->rn_tname, rename->rn_tnamelen, nfserr_inval)))
807                 return status;
808
809         DECODE_TAIL;
810 }
811
812 static int
813 nfsd4_decode_renew(struct nfsd4_compoundargs *argp, clientid_t *clientid)
814 {
815         DECODE_HEAD;
816
817         READ_BUF(sizeof(clientid_t));
818         COPYMEM(clientid, sizeof(clientid_t));
819
820         DECODE_TAIL;
821 }
822
823 static int
824 nfsd4_decode_setattr(struct nfsd4_compoundargs *argp, struct nfsd4_setattr *setattr)
825 {
826         DECODE_HEAD;
827
828         READ_BUF(sizeof(stateid_t));
829         READ32(setattr->sa_stateid.si_generation);
830         COPYMEM(&setattr->sa_stateid.si_opaque, sizeof(stateid_opaque_t));
831         if ((status = nfsd4_decode_fattr(argp, setattr->sa_bmval, &setattr->sa_iattr, &setattr->sa_acl)))
832                 goto out;
833
834         DECODE_TAIL;
835 }
836
837 static int
838 nfsd4_decode_setclientid(struct nfsd4_compoundargs *argp, struct nfsd4_setclientid *setclientid)
839 {
840         DECODE_HEAD;
841
842         READ_BUF(12);
843         COPYMEM(setclientid->se_verf.data, 8);
844         READ32(setclientid->se_namelen);
845
846         READ_BUF(setclientid->se_namelen + 8);
847         SAVEMEM(setclientid->se_name, setclientid->se_namelen);
848         READ32(setclientid->se_callback_prog);
849         READ32(setclientid->se_callback_netid_len);
850
851         READ_BUF(setclientid->se_callback_netid_len + 4);
852         SAVEMEM(setclientid->se_callback_netid_val, setclientid->se_callback_netid_len);
853         READ32(setclientid->se_callback_addr_len);
854
855         READ_BUF(setclientid->se_callback_addr_len + 4);
856         SAVEMEM(setclientid->se_callback_addr_val, setclientid->se_callback_addr_len);
857         READ32(setclientid->se_callback_ident);
858
859         DECODE_TAIL;
860 }
861
862 static int
863 nfsd4_decode_setclientid_confirm(struct nfsd4_compoundargs *argp, struct nfsd4_setclientid_confirm *scd_c)
864 {
865         DECODE_HEAD;
866
867         READ_BUF(8 + sizeof(nfs4_verifier));
868         COPYMEM(&scd_c->sc_clientid, 8);
869         COPYMEM(&scd_c->sc_confirm, sizeof(nfs4_verifier));
870
871         DECODE_TAIL;
872 }
873
874 /* Also used for NVERIFY */
875 static int
876 nfsd4_decode_verify(struct nfsd4_compoundargs *argp, struct nfsd4_verify *verify)
877 {
878 #if 0
879         struct nfsd4_compoundargs save = {
880                 .p = argp->p,
881                 .end = argp->end,
882                 .rqstp = argp->rqstp,
883         };
884         u32             ve_bmval[2];
885         struct iattr    ve_iattr;           /* request */
886         struct nfs4_acl *ve_acl;            /* request */
887 #endif
888         DECODE_HEAD;
889
890         if ((status = nfsd4_decode_bitmap(argp, verify->ve_bmval)))
891                 goto out;
892
893         /* For convenience's sake, we compare raw xdr'd attributes in
894          * nfsd4_proc_verify; however we still decode here just to return
895          * correct error in case of bad xdr. */
896 #if 0
897         status = nfsd4_decode_fattr(ve_bmval, &ve_iattr, &ve_acl);
898         if (status == nfserr_inval) {
899                 status = nfserrno(status);
900                 goto out;
901         }
902 #endif
903         READ_BUF(4);
904         READ32(verify->ve_attrlen);
905         READ_BUF(verify->ve_attrlen);
906         SAVEMEM(verify->ve_attrval, verify->ve_attrlen);
907
908         DECODE_TAIL;
909 }
910
911 static int
912 nfsd4_decode_write(struct nfsd4_compoundargs *argp, struct nfsd4_write *write)
913 {
914         int avail;
915         int v;
916         int len;
917         DECODE_HEAD;
918
919         READ_BUF(sizeof(stateid_opaque_t) + 20);
920         READ32(write->wr_stateid.si_generation);
921         COPYMEM(&write->wr_stateid.si_opaque, sizeof(stateid_opaque_t));
922         READ64(write->wr_offset);
923         READ32(write->wr_stable_how);
924         if (write->wr_stable_how > 2)
925                 goto xdr_error;
926         READ32(write->wr_buflen);
927
928         /* Sorry .. no magic macros for this.. *
929          * READ_BUF(write->wr_buflen);
930          * SAVEMEM(write->wr_buf, write->wr_buflen);
931          */
932         avail = (char*)argp->end - (char*)argp->p;
933         if (avail + argp->pagelen < write->wr_buflen) {
934                 printk(KERN_NOTICE "xdr error! (%s:%d)\n", __FILE__, __LINE__); 
935                 goto xdr_error;
936         }
937         argp->rqstp->rq_vec[0].iov_base = p;
938         argp->rqstp->rq_vec[0].iov_len = avail;
939         v = 0;
940         len = write->wr_buflen;
941         while (len > argp->rqstp->rq_vec[v].iov_len) {
942                 len -= argp->rqstp->rq_vec[v].iov_len;
943                 v++;
944                 argp->rqstp->rq_vec[v].iov_base = page_address(argp->pagelist[0]);
945                 argp->pagelist++;
946                 if (argp->pagelen >= PAGE_SIZE) {
947                         argp->rqstp->rq_vec[v].iov_len = PAGE_SIZE;
948                         argp->pagelen -= PAGE_SIZE;
949                 } else {
950                         argp->rqstp->rq_vec[v].iov_len = argp->pagelen;
951                         argp->pagelen -= len;
952                 }
953         }
954         argp->end = (u32*) (argp->rqstp->rq_vec[v].iov_base + argp->rqstp->rq_vec[v].iov_len);
955         argp->p = (u32*)  (argp->rqstp->rq_vec[v].iov_base + (XDR_QUADLEN(len) << 2));
956         argp->rqstp->rq_vec[v].iov_len = len;
957         write->wr_vlen = v+1;
958
959         DECODE_TAIL;
960 }
961
962 static int
963 nfsd4_decode_release_lockowner(struct nfsd4_compoundargs *argp, struct nfsd4_release_lockowner *rlockowner)
964 {
965         DECODE_HEAD;
966
967         READ_BUF(12);
968         COPYMEM(&rlockowner->rl_clientid, sizeof(clientid_t));
969         READ32(rlockowner->rl_owner.len);
970         READ_BUF(rlockowner->rl_owner.len);
971         READMEM(rlockowner->rl_owner.data, rlockowner->rl_owner.len);
972
973         DECODE_TAIL;
974 }
975
976 static int
977 nfsd4_decode_compound(struct nfsd4_compoundargs *argp)
978 {
979         DECODE_HEAD;
980         struct nfsd4_op *op;
981         int i;
982
983         /*
984          * XXX: According to spec, we should check the tag
985          * for UTF-8 compliance.  I'm postponing this for
986          * now because it seems that some clients do use
987          * binary tags.
988          */
989         READ_BUF(4);
990         READ32(argp->taglen);
991         READ_BUF(argp->taglen + 8);
992         SAVEMEM(argp->tag, argp->taglen);
993         READ32(argp->minorversion);
994         READ32(argp->opcnt);
995
996         if (argp->taglen > NFSD4_MAX_TAGLEN)
997                 goto xdr_error;
998         if (argp->opcnt > 100)
999                 goto xdr_error;
1000
1001         if (argp->opcnt > ARRAY_SIZE(argp->iops)) {
1002                 argp->ops = kmalloc(argp->opcnt * sizeof(*argp->ops), GFP_KERNEL);
1003                 if (!argp->ops) {
1004                         argp->ops = argp->iops;
1005                         printk(KERN_INFO "nfsd: couldn't allocate room for COMPOUND\n");
1006                         goto xdr_error;
1007                 }
1008         }
1009
1010         for (i = 0; i < argp->opcnt; i++) {
1011                 op = &argp->ops[i];
1012                 op->replay = NULL;
1013
1014                 /*
1015                  * We can't use READ_BUF() here because we need to handle
1016                  * a missing opcode as an OP_WRITE + 1. So we need to check
1017                  * to see if we're truly at the end of our buffer or if there
1018                  * is another page we need to flip to.
1019                  */
1020
1021                 if (argp->p == argp->end) {
1022                         if (argp->pagelen < 4) {
1023                                 /* There isn't an opcode still on the wire */
1024                                 op->opnum = OP_WRITE + 1;
1025                                 op->status = nfserr_bad_xdr;
1026                                 argp->opcnt = i+1;
1027                                 break;
1028                         }
1029
1030                         /*
1031                          * False alarm. We just hit a page boundary, but there
1032                          * is still data available.  Move pointer across page
1033                          * boundary.  *snip from READ_BUF*
1034                          */
1035                         argp->p = page_address(argp->pagelist[0]);
1036                         argp->pagelist++;
1037                         if (argp->pagelen < PAGE_SIZE) {
1038                                 argp->end = p + (argp->pagelen>>2);
1039                                 argp->pagelen = 0;
1040                         } else {
1041                                 argp->end = p + (PAGE_SIZE>>2);
1042                                 argp->pagelen -= PAGE_SIZE;
1043                         }
1044                 }
1045                 op->opnum = ntohl(*argp->p++);
1046
1047                 switch (op->opnum) {
1048                 case 2: /* Reserved operation */
1049                         op->opnum = OP_ILLEGAL;
1050                         if (argp->minorversion == 0)
1051                                 op->status = nfserr_op_illegal;
1052                         else
1053                                 op->status = nfserr_minor_vers_mismatch;
1054                         break;
1055                 case OP_ACCESS:
1056                         op->status = nfsd4_decode_access(argp, &op->u.access);
1057                         break;
1058                 case OP_CLOSE:
1059                         op->status = nfsd4_decode_close(argp, &op->u.close);
1060                         break;
1061                 case OP_COMMIT:
1062                         op->status = nfsd4_decode_commit(argp, &op->u.commit);
1063                         break;
1064                 case OP_CREATE:
1065                         op->status = nfsd4_decode_create(argp, &op->u.create);
1066                         break;
1067                 case OP_DELEGRETURN:
1068                         op->status = nfsd4_decode_delegreturn(argp, &op->u.delegreturn);
1069                         break;
1070                 case OP_GETATTR:
1071                         op->status = nfsd4_decode_getattr(argp, &op->u.getattr);
1072                         break;
1073                 case OP_GETFH:
1074                         op->status = nfs_ok;
1075                         break;
1076                 case OP_LINK:
1077                         op->status = nfsd4_decode_link(argp, &op->u.link);
1078                         break;
1079                 case OP_LOCK:
1080                         op->status = nfsd4_decode_lock(argp, &op->u.lock);
1081                         break;
1082                 case OP_LOCKT:
1083                         op->status = nfsd4_decode_lockt(argp, &op->u.lockt);
1084                         break;
1085                 case OP_LOCKU:
1086                         op->status = nfsd4_decode_locku(argp, &op->u.locku);
1087                         break;
1088                 case OP_LOOKUP:
1089                         op->status = nfsd4_decode_lookup(argp, &op->u.lookup);
1090                         break;
1091                 case OP_LOOKUPP:
1092                         op->status = nfs_ok;
1093                         break;
1094                 case OP_NVERIFY:
1095                         op->status = nfsd4_decode_verify(argp, &op->u.nverify);
1096                         break;
1097                 case OP_OPEN:
1098                         op->status = nfsd4_decode_open(argp, &op->u.open);
1099                         break;
1100                 case OP_OPEN_CONFIRM:
1101                         op->status = nfsd4_decode_open_confirm(argp, &op->u.open_confirm);
1102                         break;
1103                 case OP_OPEN_DOWNGRADE:
1104                         op->status = nfsd4_decode_open_downgrade(argp, &op->u.open_downgrade);
1105                         break;
1106                 case OP_PUTFH:
1107                         op->status = nfsd4_decode_putfh(argp, &op->u.putfh);
1108                         break;
1109                 case OP_PUTROOTFH:
1110                         op->status = nfs_ok;
1111                         break;
1112                 case OP_READ:
1113                         op->status = nfsd4_decode_read(argp, &op->u.read);
1114                         break;
1115                 case OP_READDIR:
1116                         op->status = nfsd4_decode_readdir(argp, &op->u.readdir);
1117                         break;
1118                 case OP_READLINK:
1119                         op->status = nfs_ok;
1120                         break;
1121                 case OP_REMOVE:
1122                         op->status = nfsd4_decode_remove(argp, &op->u.remove);
1123                         break;
1124                 case OP_RENAME:
1125                         op->status = nfsd4_decode_rename(argp, &op->u.rename);
1126                         break;
1127                 case OP_RESTOREFH:
1128                         op->status = nfs_ok;
1129                         break;
1130                 case OP_RENEW:
1131                         op->status = nfsd4_decode_renew(argp, &op->u.renew);
1132                         break;
1133                 case OP_SAVEFH:
1134                         op->status = nfs_ok;
1135                         break;
1136                 case OP_SETATTR:
1137                         op->status = nfsd4_decode_setattr(argp, &op->u.setattr);
1138                         break;
1139                 case OP_SETCLIENTID:
1140                         op->status = nfsd4_decode_setclientid(argp, &op->u.setclientid);
1141                         break;
1142                 case OP_SETCLIENTID_CONFIRM:
1143                         op->status = nfsd4_decode_setclientid_confirm(argp, &op->u.setclientid_confirm);
1144                         break;
1145                 case OP_VERIFY:
1146                         op->status = nfsd4_decode_verify(argp, &op->u.verify);
1147                         break;
1148                 case OP_WRITE:
1149                         op->status = nfsd4_decode_write(argp, &op->u.write);
1150                         break;
1151                 case OP_RELEASE_LOCKOWNER:
1152                         op->status = nfsd4_decode_release_lockowner(argp, &op->u.release_lockowner);
1153                         break;
1154                 default:
1155                         op->opnum = OP_ILLEGAL;
1156                         op->status = nfserr_op_illegal;
1157                         break;
1158                 }
1159
1160                 if (op->status) {
1161                         argp->opcnt = i+1;
1162                         break;
1163                 }
1164         }
1165
1166         DECODE_TAIL;
1167 }
1168 /*
1169  * END OF "GENERIC" DECODE ROUTINES.
1170  */
1171
1172 /*
1173  * START OF "GENERIC" ENCODE ROUTINES.
1174  *   These may look a little ugly since they are imported from a "generic"
1175  * set of XDR encode/decode routines which are intended to be shared by
1176  * all of our NFSv4 implementations (OpenBSD, MacOS X...).
1177  *
1178  * If the pain of reading these is too great, it should be a straightforward
1179  * task to translate them into Linux-specific versions which are more
1180  * consistent with the style used in NFSv2/v3...
1181  */
1182 #define ENCODE_HEAD              u32 *p
1183
1184 #define WRITE32(n)               *p++ = htonl(n)
1185 #define WRITE64(n)               do {                           \
1186         *p++ = htonl((u32)((n) >> 32));                         \
1187         *p++ = htonl((u32)(n));                                 \
1188 } while (0)
1189 #define WRITEMEM(ptr,nbytes)     do {                           \
1190         *(p + XDR_QUADLEN(nbytes) -1) = 0;                      \
1191         memcpy(p, ptr, nbytes);                                 \
1192         p += XDR_QUADLEN(nbytes);                               \
1193 } while (0)
1194 #define WRITECINFO(c)           do {                            \
1195         *p++ = htonl(c.atomic);                                 \
1196         *p++ = htonl(c.before_ctime_sec);                               \
1197         *p++ = htonl(c.before_ctime_nsec);                              \
1198         *p++ = htonl(c.after_ctime_sec);                                \
1199         *p++ = htonl(c.after_ctime_nsec);                               \
1200 } while (0)
1201
1202 #define RESERVE_SPACE(nbytes)   do {                            \
1203         p = resp->p;                                            \
1204         BUG_ON(p + XDR_QUADLEN(nbytes) > resp->end);            \
1205 } while (0)
1206 #define ADJUST_ARGS()           resp->p = p
1207
1208 /*
1209  * Header routine to setup seqid operation replay cache
1210  */
1211 #define ENCODE_SEQID_OP_HEAD                                    \
1212         u32 *p;                                                 \
1213         u32 *save;                                              \
1214                                                                 \
1215         save = resp->p;
1216
1217 /*
1218  * Routine for encoding the result of a "seqid-mutating" NFSv4 operation.  This
1219  * is where sequence id's are incremented, and the replay cache is filled.
1220  * Note that we increment sequence id's here, at the last moment, so we're sure
1221  * we know whether the error to be returned is a sequence id mutating error.
1222  */
1223
1224 #define ENCODE_SEQID_OP_TAIL(stateowner) do {                   \
1225         if (seqid_mutating_err(nfserr) && stateowner) {         \
1226                 stateowner->so_seqid++;                         \
1227                 stateowner->so_replay.rp_status = nfserr;       \
1228                 stateowner->so_replay.rp_buflen =               \
1229                           (((char *)(resp)->p - (char *)save)); \
1230                 memcpy(stateowner->so_replay.rp_buf, save,      \
1231                         stateowner->so_replay.rp_buflen);       \
1232         } } while (0);
1233
1234 /* Encode as an array of strings the string given with components
1235  * seperated @sep.
1236  */
1237 static int nfsd4_encode_components(char sep, char *components,
1238                                    u32 **pp, int *buflen)
1239 {
1240         u32 *p = *pp;
1241         u32 *countp = p;
1242         int strlen, count=0;
1243         char *str, *end;
1244
1245         dprintk("nfsd4_encode_components(%s)\n", components);
1246         if ((*buflen -= 4) < 0)
1247                 return nfserr_resource;
1248         WRITE32(0); /* We will fill this in with @count later */
1249         end = str = components;
1250         while (*end) {
1251                 for (; *end && (*end != sep); end++)
1252                         ; /* Point to end of component */
1253                 strlen = end - str;
1254                 if (strlen) {
1255                         if ((*buflen -= ((XDR_QUADLEN(strlen) << 2) + 4)) < 0)
1256                                 return nfserr_resource;
1257                         WRITE32(strlen);
1258                         WRITEMEM(str, strlen);
1259                         count++;
1260                 }
1261                 else
1262                         end++;
1263                 str = end;
1264         }
1265         *pp = p;
1266         p = countp;
1267         WRITE32(count);
1268         return 0;
1269 }
1270
1271 /*
1272  * encode a location element of a fs_locations structure
1273  */
1274 static int nfsd4_encode_fs_location4(struct nfsd4_fs_location *location,
1275                                     u32 **pp, int *buflen)
1276 {
1277         int status;
1278         u32 *p = *pp;
1279
1280         status = nfsd4_encode_components(':', location->hosts, &p, buflen);
1281         if (status)
1282                 return status;
1283         status = nfsd4_encode_components('/', location->path, &p, buflen);
1284         if (status)
1285                 return status;
1286         *pp = p;
1287         return 0;
1288 }
1289
1290 /*
1291  * Return the path to an export point in the pseudo filesystem namespace
1292  * Returned string is safe to use as long as the caller holds a reference
1293  * to @exp.
1294  */
1295 static char *nfsd4_path(struct svc_rqst *rqstp, struct svc_export *exp)
1296 {
1297         struct svc_fh tmp_fh;
1298         char *path, *rootpath;
1299         int stat;
1300
1301         fh_init(&tmp_fh, NFS4_FHSIZE);
1302         stat = exp_pseudoroot(rqstp->rq_client, &tmp_fh, &rqstp->rq_chandle);
1303         if (stat)
1304                 return ERR_PTR(stat);
1305         rootpath = tmp_fh.fh_export->ex_path;
1306
1307         path = exp->ex_path;
1308
1309         if (strncmp(path, rootpath, strlen(rootpath))) {
1310                 printk("nfsd: fs_locations failed;"
1311                         "%s is not contained in %s\n", path, rootpath);
1312                 return ERR_PTR(-EOPNOTSUPP);
1313         }
1314
1315         return path + strlen(rootpath);
1316 }
1317
1318 /*
1319  *  encode a fs_locations structure
1320  */
1321 static int nfsd4_encode_fs_locations(struct svc_rqst *rqstp,
1322                                      struct svc_export *exp,
1323                                      u32 **pp, int *buflen)
1324 {
1325         int status, i;
1326         u32 *p = *pp;
1327         struct nfsd4_fs_locations *fslocs = &exp->ex_fslocs;
1328         char *root = nfsd4_path(rqstp, exp);
1329
1330         if (IS_ERR(root))
1331                 return PTR_ERR(root);
1332         status = nfsd4_encode_components('/', root, &p, buflen);
1333         if (status)
1334                 return status;
1335         if ((*buflen -= 4) < 0)
1336                 return nfserr_resource;
1337         WRITE32(fslocs->locations_count);
1338         for (i=0; i<fslocs->locations_count; i++) {
1339                 status = nfsd4_encode_fs_location4(&fslocs->locations[i],
1340                                                    &p, buflen);
1341                 if (status)
1342                         return status;
1343         }
1344         *pp = p;
1345         return 0;
1346 }
1347
1348 static u32 nfs4_ftypes[16] = {
1349         NF4BAD,  NF4FIFO, NF4CHR, NF4BAD,
1350         NF4DIR,  NF4BAD,  NF4BLK, NF4BAD,
1351         NF4REG,  NF4BAD,  NF4LNK, NF4BAD,
1352         NF4SOCK, NF4BAD,  NF4LNK, NF4BAD,
1353 };
1354
1355 static int
1356 nfsd4_encode_name(struct svc_rqst *rqstp, int whotype, uid_t id, int group,
1357                         u32 **p, int *buflen)
1358 {
1359         int status;
1360
1361         if (*buflen < (XDR_QUADLEN(IDMAP_NAMESZ) << 2) + 4)
1362                 return nfserr_resource;
1363         if (whotype != NFS4_ACL_WHO_NAMED)
1364                 status = nfs4_acl_write_who(whotype, (u8 *)(*p + 1));
1365         else if (group)
1366                 status = nfsd_map_gid_to_name(rqstp, id, (u8 *)(*p + 1));
1367         else
1368                 status = nfsd_map_uid_to_name(rqstp, id, (u8 *)(*p + 1));
1369         if (status < 0)
1370                 return nfserrno(status);
1371         *p = xdr_encode_opaque(*p, NULL, status);
1372         *buflen -= (XDR_QUADLEN(status) << 2) + 4;
1373         BUG_ON(*buflen < 0);
1374         return 0;
1375 }
1376
1377 static inline int
1378 nfsd4_encode_user(struct svc_rqst *rqstp, uid_t uid, u32 **p, int *buflen)
1379 {
1380         return nfsd4_encode_name(rqstp, NFS4_ACL_WHO_NAMED, uid, 0, p, buflen);
1381 }
1382
1383 static inline int
1384 nfsd4_encode_group(struct svc_rqst *rqstp, uid_t gid, u32 **p, int *buflen)
1385 {
1386         return nfsd4_encode_name(rqstp, NFS4_ACL_WHO_NAMED, gid, 1, p, buflen);
1387 }
1388
1389 static inline int
1390 nfsd4_encode_aclname(struct svc_rqst *rqstp, int whotype, uid_t id, int group,
1391                 u32 **p, int *buflen)
1392 {
1393         return nfsd4_encode_name(rqstp, whotype, id, group, p, buflen);
1394 }
1395
1396 #define WORD0_ABSENT_FS_ATTRS (FATTR4_WORD0_FS_LOCATIONS | FATTR4_WORD0_FSID | \
1397                               FATTR4_WORD0_RDATTR_ERROR)
1398 #define WORD1_ABSENT_FS_ATTRS FATTR4_WORD1_MOUNTED_ON_FILEID
1399
1400 static int fattr_handle_absent_fs(u32 *bmval0, u32 *bmval1, u32 *rdattr_err)
1401 {
1402         /* As per referral draft:  */
1403         if (*bmval0 & ~WORD0_ABSENT_FS_ATTRS ||
1404             *bmval1 & ~WORD1_ABSENT_FS_ATTRS) {
1405                 if (*bmval0 & FATTR4_WORD0_RDATTR_ERROR ||
1406                     *bmval0 & FATTR4_WORD0_FS_LOCATIONS)
1407                         *rdattr_err = NFSERR_MOVED;
1408                 else
1409                         return nfserr_moved;
1410         }
1411         *bmval0 &= WORD0_ABSENT_FS_ATTRS;
1412         *bmval1 &= WORD1_ABSENT_FS_ATTRS;
1413         return 0;
1414 }
1415
1416 /*
1417  * Note: @fhp can be NULL; in this case, we might have to compose the filehandle
1418  * ourselves.
1419  *
1420  * @countp is the buffer size in _words_; upon successful return this becomes
1421  * replaced with the number of words written.
1422  */
1423 int
1424 nfsd4_encode_fattr(struct svc_fh *fhp, struct svc_export *exp,
1425                 struct dentry *dentry, u32 *buffer, int *countp, u32 *bmval,
1426                 struct svc_rqst *rqstp)
1427 {
1428         u32 bmval0 = bmval[0];
1429         u32 bmval1 = bmval[1];
1430         struct kstat stat;
1431         struct svc_fh tempfh;
1432         struct kstatfs statfs;
1433         int buflen = *countp << 2;
1434         u32 *attrlenp;
1435         u32 dummy;
1436         u64 dummy64;
1437         u32 rdattr_err = 0;
1438         u32 *p = buffer;
1439         int status;
1440         int aclsupport = 0;
1441         struct nfs4_acl *acl = NULL;
1442
1443         BUG_ON(bmval1 & NFSD_WRITEONLY_ATTRS_WORD1);
1444         BUG_ON(bmval0 & ~NFSD_SUPPORTED_ATTRS_WORD0);
1445         BUG_ON(bmval1 & ~NFSD_SUPPORTED_ATTRS_WORD1);
1446
1447         if (exp->ex_fslocs.migrated) {
1448                 status = fattr_handle_absent_fs(&bmval0, &bmval1, &rdattr_err);
1449                 if (status)
1450                         goto out;
1451         }
1452
1453         status = vfs_getattr(exp->ex_mnt, dentry, &stat);
1454         if (status)
1455                 goto out_nfserr;
1456         if ((bmval0 & (FATTR4_WORD0_FILES_FREE | FATTR4_WORD0_FILES_TOTAL)) ||
1457             (bmval1 & (FATTR4_WORD1_SPACE_AVAIL | FATTR4_WORD1_SPACE_FREE |
1458                        FATTR4_WORD1_SPACE_TOTAL))) {
1459                 status = vfs_statfs(dentry, &statfs);
1460                 if (status)
1461                         goto out_nfserr;
1462         }
1463         if ((bmval0 & (FATTR4_WORD0_FILEHANDLE | FATTR4_WORD0_FSID)) && !fhp) {
1464                 fh_init(&tempfh, NFS4_FHSIZE);
1465                 status = fh_compose(&tempfh, exp, dentry, NULL);
1466                 if (status)
1467                         goto out;
1468                 fhp = &tempfh;
1469         }
1470         if (bmval0 & (FATTR4_WORD0_ACL | FATTR4_WORD0_ACLSUPPORT
1471                         | FATTR4_WORD0_SUPPORTED_ATTRS)) {
1472                 status = nfsd4_get_nfs4_acl(rqstp, dentry, &acl);
1473                 aclsupport = (status == 0);
1474                 if (bmval0 & FATTR4_WORD0_ACL) {
1475                         if (status == -EOPNOTSUPP)
1476                                 bmval0 &= ~FATTR4_WORD0_ACL;
1477                         else if (status == -EINVAL) {
1478                                 status = nfserr_attrnotsupp;
1479                                 goto out;
1480                         } else if (status != 0)
1481                                 goto out_nfserr;
1482                 }
1483         }
1484         if (bmval0 & FATTR4_WORD0_FS_LOCATIONS) {
1485                 if (exp->ex_fslocs.locations == NULL) {
1486                         bmval0 &= ~FATTR4_WORD0_FS_LOCATIONS;
1487                 }
1488         }
1489         if ((buflen -= 16) < 0)
1490                 goto out_resource;
1491
1492         WRITE32(2);
1493         WRITE32(bmval0);
1494         WRITE32(bmval1);
1495         attrlenp = p++;                /* to be backfilled later */
1496
1497         if (bmval0 & FATTR4_WORD0_SUPPORTED_ATTRS) {
1498                 u32 word0 = NFSD_SUPPORTED_ATTRS_WORD0;
1499                 if ((buflen -= 12) < 0)
1500                         goto out_resource;
1501                 if (!aclsupport)
1502                         word0 &= ~FATTR4_WORD0_ACL;
1503                 if (!exp->ex_fslocs.locations)
1504                         word0 &= ~FATTR4_WORD0_FS_LOCATIONS;
1505                 WRITE32(2);
1506                 WRITE32(word0);
1507                 WRITE32(NFSD_SUPPORTED_ATTRS_WORD1);
1508         }
1509         if (bmval0 & FATTR4_WORD0_TYPE) {
1510                 if ((buflen -= 4) < 0)
1511                         goto out_resource;
1512                 dummy = nfs4_ftypes[(stat.mode & S_IFMT) >> 12];
1513                 if (dummy == NF4BAD)
1514                         goto out_serverfault;
1515                 WRITE32(dummy);
1516         }
1517         if (bmval0 & FATTR4_WORD0_FH_EXPIRE_TYPE) {
1518                 if ((buflen -= 4) < 0)
1519                         goto out_resource;
1520                 if (exp->ex_flags & NFSEXP_NOSUBTREECHECK)
1521                         WRITE32(NFS4_FH_PERSISTENT);
1522                 else
1523                         WRITE32(NFS4_FH_PERSISTENT|NFS4_FH_VOL_RENAME);
1524         }
1525         if (bmval0 & FATTR4_WORD0_CHANGE) {
1526                 /*
1527                  * Note: This _must_ be consistent with the scheme for writing
1528                  * change_info, so any changes made here must be reflected there
1529                  * as well.  (See xdr4.h:set_change_info() and the WRITECINFO()
1530                  * macro above.)
1531                  */
1532                 if ((buflen -= 8) < 0)
1533                         goto out_resource;
1534                 WRITE32(stat.ctime.tv_sec);
1535                 WRITE32(stat.ctime.tv_nsec);
1536         }
1537         if (bmval0 & FATTR4_WORD0_SIZE) {
1538                 if ((buflen -= 8) < 0)
1539                         goto out_resource;
1540                 WRITE64(stat.size);
1541         }
1542         if (bmval0 & FATTR4_WORD0_LINK_SUPPORT) {
1543                 if ((buflen -= 4) < 0)
1544                         goto out_resource;
1545                 WRITE32(1);
1546         }
1547         if (bmval0 & FATTR4_WORD0_SYMLINK_SUPPORT) {
1548                 if ((buflen -= 4) < 0)
1549                         goto out_resource;
1550                 WRITE32(1);
1551         }
1552         if (bmval0 & FATTR4_WORD0_NAMED_ATTR) {
1553                 if ((buflen -= 4) < 0)
1554                         goto out_resource;
1555                 WRITE32(0);
1556         }
1557         if (bmval0 & FATTR4_WORD0_FSID) {
1558                 if ((buflen -= 16) < 0)
1559                         goto out_resource;
1560                 if (exp->ex_fslocs.migrated) {
1561                         WRITE64(NFS4_REFERRAL_FSID_MAJOR);
1562                         WRITE64(NFS4_REFERRAL_FSID_MINOR);
1563                 } else if (is_fsid(fhp, rqstp->rq_reffh)) {
1564                         WRITE64((u64)exp->ex_fsid);
1565                         WRITE64((u64)0);
1566                 } else {
1567                         WRITE32(0);
1568                         WRITE32(MAJOR(stat.dev));
1569                         WRITE32(0);
1570                         WRITE32(MINOR(stat.dev));
1571                 }
1572         }
1573         if (bmval0 & FATTR4_WORD0_UNIQUE_HANDLES) {
1574                 if ((buflen -= 4) < 0)
1575                         goto out_resource;
1576                 WRITE32(0);
1577         }
1578         if (bmval0 & FATTR4_WORD0_LEASE_TIME) {
1579                 if ((buflen -= 4) < 0)
1580                         goto out_resource;
1581                 WRITE32(NFSD_LEASE_TIME);
1582         }
1583         if (bmval0 & FATTR4_WORD0_RDATTR_ERROR) {
1584                 if ((buflen -= 4) < 0)
1585                         goto out_resource;
1586                 WRITE32(rdattr_err);
1587         }
1588         if (bmval0 & FATTR4_WORD0_ACL) {
1589                 struct nfs4_ace *ace;
1590                 struct list_head *h;
1591
1592                 if (acl == NULL) {
1593                         if ((buflen -= 4) < 0)
1594                                 goto out_resource;
1595
1596                         WRITE32(0);
1597                         goto out_acl;
1598                 }
1599                 if ((buflen -= 4) < 0)
1600                         goto out_resource;
1601                 WRITE32(acl->naces);
1602
1603                 list_for_each(h, &acl->ace_head) {
1604                         ace = list_entry(h, struct nfs4_ace, l_ace);
1605
1606                         if ((buflen -= 4*3) < 0)
1607                                 goto out_resource;
1608                         WRITE32(ace->type);
1609                         WRITE32(ace->flag);
1610                         WRITE32(ace->access_mask & NFS4_ACE_MASK_ALL);
1611                         status = nfsd4_encode_aclname(rqstp, ace->whotype,
1612                                 ace->who, ace->flag & NFS4_ACE_IDENTIFIER_GROUP,
1613                                 &p, &buflen);
1614                         if (status == nfserr_resource)
1615                                 goto out_resource;
1616                         if (status)
1617                                 goto out;
1618                 }
1619         }
1620 out_acl:
1621         if (bmval0 & FATTR4_WORD0_ACLSUPPORT) {
1622                 if ((buflen -= 4) < 0)
1623                         goto out_resource;
1624                 WRITE32(aclsupport ?
1625                         ACL4_SUPPORT_ALLOW_ACL|ACL4_SUPPORT_DENY_ACL : 0);
1626         }
1627         if (bmval0 & FATTR4_WORD0_CANSETTIME) {
1628                 if ((buflen -= 4) < 0)
1629                         goto out_resource;
1630                 WRITE32(1);
1631         }
1632         if (bmval0 & FATTR4_WORD0_CASE_INSENSITIVE) {
1633                 if ((buflen -= 4) < 0)
1634                         goto out_resource;
1635                 WRITE32(1);
1636         }
1637         if (bmval0 & FATTR4_WORD0_CASE_PRESERVING) {
1638                 if ((buflen -= 4) < 0)
1639                         goto out_resource;
1640                 WRITE32(1);
1641         }
1642         if (bmval0 & FATTR4_WORD0_CHOWN_RESTRICTED) {
1643                 if ((buflen -= 4) < 0)
1644                         goto out_resource;
1645                 WRITE32(1);
1646         }
1647         if (bmval0 & FATTR4_WORD0_FILEHANDLE) {
1648                 buflen -= (XDR_QUADLEN(fhp->fh_handle.fh_size) << 2) + 4;
1649                 if (buflen < 0)
1650                         goto out_resource;
1651                 WRITE32(fhp->fh_handle.fh_size);
1652                 WRITEMEM(&fhp->fh_handle.fh_base, fhp->fh_handle.fh_size);
1653         }
1654         if (bmval0 & FATTR4_WORD0_FILEID) {
1655                 if ((buflen -= 8) < 0)
1656                         goto out_resource;
1657                 WRITE64((u64) stat.ino);
1658         }
1659         if (bmval0 & FATTR4_WORD0_FILES_AVAIL) {
1660                 if ((buflen -= 8) < 0)
1661                         goto out_resource;
1662                 WRITE64((u64) statfs.f_ffree);
1663         }
1664         if (bmval0 & FATTR4_WORD0_FILES_FREE) {
1665                 if ((buflen -= 8) < 0)
1666                         goto out_resource;
1667                 WRITE64((u64) statfs.f_ffree);
1668         }
1669         if (bmval0 & FATTR4_WORD0_FILES_TOTAL) {
1670                 if ((buflen -= 8) < 0)
1671                         goto out_resource;
1672                 WRITE64((u64) statfs.f_files);
1673         }
1674         if (bmval0 & FATTR4_WORD0_FS_LOCATIONS) {
1675                 status = nfsd4_encode_fs_locations(rqstp, exp, &p, &buflen);
1676                 if (status == nfserr_resource)
1677                         goto out_resource;
1678                 if (status)
1679                         goto out;
1680         }
1681         if (bmval0 & FATTR4_WORD0_HOMOGENEOUS) {
1682                 if ((buflen -= 4) < 0)
1683                         goto out_resource;
1684                 WRITE32(1);
1685         }
1686         if (bmval0 & FATTR4_WORD0_MAXFILESIZE) {
1687                 if ((buflen -= 8) < 0)
1688                         goto out_resource;
1689                 WRITE64(~(u64)0);
1690         }
1691         if (bmval0 & FATTR4_WORD0_MAXLINK) {
1692                 if ((buflen -= 4) < 0)
1693                         goto out_resource;
1694                 WRITE32(255);
1695         }
1696         if (bmval0 & FATTR4_WORD0_MAXNAME) {
1697                 if ((buflen -= 4) < 0)
1698                         goto out_resource;
1699                 WRITE32(~(u32) 0);
1700         }
1701         if (bmval0 & FATTR4_WORD0_MAXREAD) {
1702                 if ((buflen -= 8) < 0)
1703                         goto out_resource;
1704                 WRITE64((u64) svc_max_payload(rqstp));
1705         }
1706         if (bmval0 & FATTR4_WORD0_MAXWRITE) {
1707                 if ((buflen -= 8) < 0)
1708                         goto out_resource;
1709                 WRITE64((u64) svc_max_payload(rqstp));
1710         }
1711         if (bmval1 & FATTR4_WORD1_MODE) {
1712                 if ((buflen -= 4) < 0)
1713                         goto out_resource;
1714                 WRITE32(stat.mode & S_IALLUGO);
1715         }
1716         if (bmval1 & FATTR4_WORD1_NO_TRUNC) {
1717                 if ((buflen -= 4) < 0)
1718                         goto out_resource;
1719                 WRITE32(1);
1720         }
1721         if (bmval1 & FATTR4_WORD1_NUMLINKS) {
1722                 if ((buflen -= 4) < 0)
1723                         goto out_resource;
1724                 WRITE32(stat.nlink);
1725         }
1726         if (bmval1 & FATTR4_WORD1_OWNER) {
1727                 status = nfsd4_encode_user(rqstp, stat.uid, &p, &buflen);
1728                 if (status == nfserr_resource)
1729                         goto out_resource;
1730                 if (status)
1731                         goto out;
1732         }
1733         if (bmval1 & FATTR4_WORD1_OWNER_GROUP) {
1734                 status = nfsd4_encode_group(rqstp, stat.gid, &p, &buflen);
1735                 if (status == nfserr_resource)
1736                         goto out_resource;
1737                 if (status)
1738                         goto out;
1739         }
1740         if (bmval1 & FATTR4_WORD1_RAWDEV) {
1741                 if ((buflen -= 8) < 0)
1742                         goto out_resource;
1743                 WRITE32((u32) MAJOR(stat.rdev));
1744                 WRITE32((u32) MINOR(stat.rdev));
1745         }
1746         if (bmval1 & FATTR4_WORD1_SPACE_AVAIL) {
1747                 if ((buflen -= 8) < 0)
1748                         goto out_resource;
1749                 dummy64 = (u64)statfs.f_bavail * (u64)statfs.f_bsize;
1750                 WRITE64(dummy64);
1751         }
1752         if (bmval1 & FATTR4_WORD1_SPACE_FREE) {
1753                 if ((buflen -= 8) < 0)
1754                         goto out_resource;
1755                 dummy64 = (u64)statfs.f_bfree * (u64)statfs.f_bsize;
1756                 WRITE64(dummy64);
1757         }
1758         if (bmval1 & FATTR4_WORD1_SPACE_TOTAL) {
1759                 if ((buflen -= 8) < 0)
1760                         goto out_resource;
1761                 dummy64 = (u64)statfs.f_blocks * (u64)statfs.f_bsize;
1762                 WRITE64(dummy64);
1763         }
1764         if (bmval1 & FATTR4_WORD1_SPACE_USED) {
1765                 if ((buflen -= 8) < 0)
1766                         goto out_resource;
1767                 dummy64 = (u64)stat.blocks << 9;
1768                 WRITE64(dummy64);
1769         }
1770         if (bmval1 & FATTR4_WORD1_TIME_ACCESS) {
1771                 if ((buflen -= 12) < 0)
1772                         goto out_resource;
1773                 WRITE32(0);
1774                 WRITE32(stat.atime.tv_sec);
1775                 WRITE32(stat.atime.tv_nsec);
1776         }
1777         if (bmval1 & FATTR4_WORD1_TIME_DELTA) {
1778                 if ((buflen -= 12) < 0)
1779                         goto out_resource;
1780                 WRITE32(0);
1781                 WRITE32(1);
1782                 WRITE32(0);
1783         }
1784         if (bmval1 & FATTR4_WORD1_TIME_METADATA) {
1785                 if ((buflen -= 12) < 0)
1786                         goto out_resource;
1787                 WRITE32(0);
1788                 WRITE32(stat.ctime.tv_sec);
1789                 WRITE32(stat.ctime.tv_nsec);
1790         }
1791         if (bmval1 & FATTR4_WORD1_TIME_MODIFY) {
1792                 if ((buflen -= 12) < 0)
1793                         goto out_resource;
1794                 WRITE32(0);
1795                 WRITE32(stat.mtime.tv_sec);
1796                 WRITE32(stat.mtime.tv_nsec);
1797         }
1798         if (bmval1 & FATTR4_WORD1_MOUNTED_ON_FILEID) {
1799                 struct dentry *mnt_pnt, *mnt_root;
1800
1801                 if ((buflen -= 8) < 0)
1802                         goto out_resource;
1803                 mnt_root = exp->ex_mnt->mnt_root;
1804                 if (mnt_root->d_inode == dentry->d_inode) {
1805                         mnt_pnt = exp->ex_mnt->mnt_mountpoint;
1806                         WRITE64((u64) mnt_pnt->d_inode->i_ino);
1807                 } else
1808                         WRITE64((u64) stat.ino);
1809         }
1810         *attrlenp = htonl((char *)p - (char *)attrlenp - 4);
1811         *countp = p - buffer;
1812         status = nfs_ok;
1813
1814 out:
1815         nfs4_acl_free(acl);
1816         if (fhp == &tempfh)
1817                 fh_put(&tempfh);
1818         return status;
1819 out_nfserr:
1820         status = nfserrno(status);
1821         goto out;
1822 out_resource:
1823         *countp = 0;
1824         status = nfserr_resource;
1825         goto out;
1826 out_serverfault:
1827         status = nfserr_serverfault;
1828         goto out;
1829 }
1830
1831 static int
1832 nfsd4_encode_dirent_fattr(struct nfsd4_readdir *cd,
1833                 const char *name, int namlen, u32 *p, int *buflen)
1834 {
1835         struct svc_export *exp = cd->rd_fhp->fh_export;
1836         struct dentry *dentry;
1837         int nfserr;
1838
1839         dentry = lookup_one_len(name, cd->rd_fhp->fh_dentry, namlen);
1840         if (IS_ERR(dentry))
1841                 return nfserrno(PTR_ERR(dentry));
1842
1843         exp_get(exp);
1844         if (d_mountpoint(dentry)) {
1845                 if (nfsd_cross_mnt(cd->rd_rqstp, &dentry, &exp)) {
1846                 /*
1847                  * -EAGAIN is the only error returned from
1848                  * nfsd_cross_mnt() and it indicates that an
1849                  * up-call has  been initiated to fill in the export
1850                  * options on exp.  When the answer comes back,
1851                  * this call will be retried.
1852                  */
1853                         nfserr = nfserr_dropit;
1854                         goto out_put;
1855                 }
1856
1857         }
1858         nfserr = nfsd4_encode_fattr(NULL, exp, dentry, p, buflen, cd->rd_bmval,
1859                                         cd->rd_rqstp);
1860 out_put:
1861         dput(dentry);
1862         exp_put(exp);
1863         return nfserr;
1864 }
1865
1866 static u32 *
1867 nfsd4_encode_rdattr_error(u32 *p, int buflen, int nfserr)
1868 {
1869         u32 *attrlenp;
1870
1871         if (buflen < 6)
1872                 return NULL;
1873         *p++ = htonl(2);
1874         *p++ = htonl(FATTR4_WORD0_RDATTR_ERROR); /* bmval0 */
1875         *p++ = htonl(0);                         /* bmval1 */
1876
1877         attrlenp = p++;
1878         *p++ = nfserr;       /* no htonl */
1879         *attrlenp = htonl((char *)p - (char *)attrlenp - 4);
1880         return p;
1881 }
1882
1883 static int
1884 nfsd4_encode_dirent(struct readdir_cd *ccd, const char *name, int namlen,
1885                     loff_t offset, ino_t ino, unsigned int d_type)
1886 {
1887         struct nfsd4_readdir *cd = container_of(ccd, struct nfsd4_readdir, common);
1888         int buflen;
1889         u32 *p = cd->buffer;
1890         int nfserr = nfserr_toosmall;
1891
1892         /* In nfsv4, "." and ".." never make it onto the wire.. */
1893         if (name && isdotent(name, namlen)) {
1894                 cd->common.err = nfs_ok;
1895                 return 0;
1896         }
1897
1898         if (cd->offset)
1899                 xdr_encode_hyper(cd->offset, (u64) offset);
1900
1901         buflen = cd->buflen - 4 - XDR_QUADLEN(namlen);
1902         if (buflen < 0)
1903                 goto fail;
1904
1905         *p++ = xdr_one;                             /* mark entry present */
1906         cd->offset = p;                             /* remember pointer */
1907         p = xdr_encode_hyper(p, NFS_OFFSET_MAX);    /* offset of next entry */
1908         p = xdr_encode_array(p, name, namlen);      /* name length & name */
1909
1910         nfserr = nfsd4_encode_dirent_fattr(cd, name, namlen, p, &buflen);
1911         switch (nfserr) {
1912         case nfs_ok:
1913                 p += buflen;
1914                 break;
1915         case nfserr_resource:
1916                 nfserr = nfserr_toosmall;
1917                 goto fail;
1918         case nfserr_dropit:
1919                 goto fail;
1920         default:
1921                 /*
1922                  * If the client requested the RDATTR_ERROR attribute,
1923                  * we stuff the error code into this attribute
1924                  * and continue.  If this attribute was not requested,
1925                  * then in accordance with the spec, we fail the
1926                  * entire READDIR operation(!)
1927                  */
1928                 if (!(cd->rd_bmval[0] & FATTR4_WORD0_RDATTR_ERROR))
1929                         goto fail;
1930                 p = nfsd4_encode_rdattr_error(p, buflen, nfserr);
1931                 if (p == NULL) {
1932                         nfserr = nfserr_toosmall;
1933                         goto fail;
1934                 }
1935         }
1936         cd->buflen -= (p - cd->buffer);
1937         cd->buffer = p;
1938         cd->common.err = nfs_ok;
1939         return 0;
1940 fail:
1941         cd->common.err = nfserr;
1942         return -EINVAL;
1943 }
1944
1945 static void
1946 nfsd4_encode_access(struct nfsd4_compoundres *resp, int nfserr, struct nfsd4_access *access)
1947 {
1948         ENCODE_HEAD;
1949
1950         if (!nfserr) {
1951                 RESERVE_SPACE(8);
1952                 WRITE32(access->ac_supported);
1953                 WRITE32(access->ac_resp_access);
1954                 ADJUST_ARGS();
1955         }
1956 }
1957
1958 static void
1959 nfsd4_encode_close(struct nfsd4_compoundres *resp, int nfserr, struct nfsd4_close *close)
1960 {
1961         ENCODE_SEQID_OP_HEAD;
1962
1963         if (!nfserr) {
1964                 RESERVE_SPACE(sizeof(stateid_t));
1965                 WRITE32(close->cl_stateid.si_generation);
1966                 WRITEMEM(&close->cl_stateid.si_opaque, sizeof(stateid_opaque_t));
1967                 ADJUST_ARGS();
1968         }
1969         ENCODE_SEQID_OP_TAIL(close->cl_stateowner);
1970 }
1971
1972
1973 static void
1974 nfsd4_encode_commit(struct nfsd4_compoundres *resp, int nfserr, struct nfsd4_commit *commit)
1975 {
1976         ENCODE_HEAD;
1977
1978         if (!nfserr) {
1979                 RESERVE_SPACE(8);
1980                 WRITEMEM(commit->co_verf.data, 8);
1981                 ADJUST_ARGS();
1982         }
1983 }
1984
1985 static void
1986 nfsd4_encode_create(struct nfsd4_compoundres *resp, int nfserr, struct nfsd4_create *create)
1987 {
1988         ENCODE_HEAD;
1989
1990         if (!nfserr) {
1991                 RESERVE_SPACE(32);
1992                 WRITECINFO(create->cr_cinfo);
1993                 WRITE32(2);
1994                 WRITE32(create->cr_bmval[0]);
1995                 WRITE32(create->cr_bmval[1]);
1996                 ADJUST_ARGS();
1997         }
1998 }
1999
2000 static int
2001 nfsd4_encode_getattr(struct nfsd4_compoundres *resp, int nfserr, struct nfsd4_getattr *getattr)
2002 {
2003         struct svc_fh *fhp = getattr->ga_fhp;
2004         int buflen;
2005
2006         if (nfserr)
2007                 return nfserr;
2008
2009         buflen = resp->end - resp->p - (COMPOUND_ERR_SLACK_SPACE >> 2);
2010         nfserr = nfsd4_encode_fattr(fhp, fhp->fh_export, fhp->fh_dentry,
2011                                     resp->p, &buflen, getattr->ga_bmval,
2012                                     resp->rqstp);
2013         if (!nfserr)
2014                 resp->p += buflen;
2015         return nfserr;
2016 }
2017
2018 static void
2019 nfsd4_encode_getfh(struct nfsd4_compoundres *resp, int nfserr, struct svc_fh *fhp)
2020 {
2021         unsigned int len;
2022         ENCODE_HEAD;
2023
2024         if (!nfserr) {
2025                 len = fhp->fh_handle.fh_size;
2026                 RESERVE_SPACE(len + 4);
2027                 WRITE32(len);
2028                 WRITEMEM(&fhp->fh_handle.fh_base, len);
2029                 ADJUST_ARGS();
2030         }
2031 }
2032
2033 /*
2034 * Including all fields other than the name, a LOCK4denied structure requires
2035 *   8(clientid) + 4(namelen) + 8(offset) + 8(length) + 4(type) = 32 bytes.
2036 */
2037 static void
2038 nfsd4_encode_lock_denied(struct nfsd4_compoundres *resp, struct nfsd4_lock_denied *ld)
2039 {
2040         ENCODE_HEAD;
2041
2042         RESERVE_SPACE(32 + XDR_LEN(ld->ld_sop ? ld->ld_sop->so_owner.len : 0));
2043         WRITE64(ld->ld_start);
2044         WRITE64(ld->ld_length);
2045         WRITE32(ld->ld_type);
2046         if (ld->ld_sop) {
2047                 WRITEMEM(&ld->ld_clientid, 8);
2048                 WRITE32(ld->ld_sop->so_owner.len);
2049                 WRITEMEM(ld->ld_sop->so_owner.data, ld->ld_sop->so_owner.len);
2050                 kref_put(&ld->ld_sop->so_ref, nfs4_free_stateowner);
2051         }  else {  /* non - nfsv4 lock in conflict, no clientid nor owner */
2052                 WRITE64((u64)0); /* clientid */
2053                 WRITE32(0); /* length of owner name */
2054         }
2055         ADJUST_ARGS();
2056 }
2057
2058 static void
2059 nfsd4_encode_lock(struct nfsd4_compoundres *resp, int nfserr, struct nfsd4_lock *lock)
2060 {
2061         ENCODE_SEQID_OP_HEAD;
2062
2063         if (!nfserr) {
2064                 RESERVE_SPACE(4 + sizeof(stateid_t));
2065                 WRITE32(lock->lk_resp_stateid.si_generation);
2066                 WRITEMEM(&lock->lk_resp_stateid.si_opaque, sizeof(stateid_opaque_t));
2067                 ADJUST_ARGS();
2068         } else if (nfserr == nfserr_denied)
2069                 nfsd4_encode_lock_denied(resp, &lock->lk_denied);
2070
2071         ENCODE_SEQID_OP_TAIL(lock->lk_replay_owner);
2072 }
2073
2074 static void
2075 nfsd4_encode_lockt(struct nfsd4_compoundres *resp, int nfserr, struct nfsd4_lockt *lockt)
2076 {
2077         if (nfserr == nfserr_denied)
2078                 nfsd4_encode_lock_denied(resp, &lockt->lt_denied);
2079 }
2080
2081 static void
2082 nfsd4_encode_locku(struct nfsd4_compoundres *resp, int nfserr, struct nfsd4_locku *locku)
2083 {
2084         ENCODE_SEQID_OP_HEAD;
2085
2086         if (!nfserr) {
2087                 RESERVE_SPACE(sizeof(stateid_t));
2088                 WRITE32(locku->lu_stateid.si_generation);
2089                 WRITEMEM(&locku->lu_stateid.si_opaque, sizeof(stateid_opaque_t));
2090                 ADJUST_ARGS();
2091         }
2092                                         
2093         ENCODE_SEQID_OP_TAIL(locku->lu_stateowner);
2094 }
2095
2096
2097 static void
2098 nfsd4_encode_link(struct nfsd4_compoundres *resp, int nfserr, struct nfsd4_link *link)
2099 {
2100         ENCODE_HEAD;
2101
2102         if (!nfserr) {
2103                 RESERVE_SPACE(20);
2104                 WRITECINFO(link->li_cinfo);
2105                 ADJUST_ARGS();
2106         }
2107 }
2108
2109
2110 static void
2111 nfsd4_encode_open(struct nfsd4_compoundres *resp, int nfserr, struct nfsd4_open *open)
2112 {
2113         ENCODE_SEQID_OP_HEAD;
2114
2115         if (nfserr)
2116                 goto out;
2117
2118         RESERVE_SPACE(36 + sizeof(stateid_t));
2119         WRITE32(open->op_stateid.si_generation);
2120         WRITEMEM(&open->op_stateid.si_opaque, sizeof(stateid_opaque_t));
2121         WRITECINFO(open->op_cinfo);
2122         WRITE32(open->op_rflags);
2123         WRITE32(2);
2124         WRITE32(open->op_bmval[0]);
2125         WRITE32(open->op_bmval[1]);
2126         WRITE32(open->op_delegate_type);
2127         ADJUST_ARGS();
2128
2129         switch (open->op_delegate_type) {
2130         case NFS4_OPEN_DELEGATE_NONE:
2131                 break;
2132         case NFS4_OPEN_DELEGATE_READ:
2133                 RESERVE_SPACE(20 + sizeof(stateid_t));
2134                 WRITEMEM(&open->op_delegate_stateid, sizeof(stateid_t));
2135                 WRITE32(open->op_recall);
2136
2137                 /*
2138                  * TODO: ACE's in delegations
2139                  */
2140                 WRITE32(NFS4_ACE_ACCESS_ALLOWED_ACE_TYPE);
2141                 WRITE32(0);
2142                 WRITE32(0);
2143                 WRITE32(0);   /* XXX: is NULL principal ok? */
2144                 ADJUST_ARGS();
2145                 break;
2146         case NFS4_OPEN_DELEGATE_WRITE:
2147                 RESERVE_SPACE(32 + sizeof(stateid_t));
2148                 WRITEMEM(&open->op_delegate_stateid, sizeof(stateid_t));
2149                 WRITE32(0);
2150
2151                 /*
2152                  * TODO: space_limit's in delegations
2153                  */
2154                 WRITE32(NFS4_LIMIT_SIZE);
2155                 WRITE32(~(u32)0);
2156                 WRITE32(~(u32)0);
2157
2158                 /*
2159                  * TODO: ACE's in delegations
2160                  */
2161                 WRITE32(NFS4_ACE_ACCESS_ALLOWED_ACE_TYPE);
2162                 WRITE32(0);
2163                 WRITE32(0);
2164                 WRITE32(0);   /* XXX: is NULL principal ok? */
2165                 ADJUST_ARGS();
2166                 break;
2167         default:
2168                 BUG();
2169         }
2170         /* XXX save filehandle here */
2171 out:
2172         ENCODE_SEQID_OP_TAIL(open->op_stateowner);
2173 }
2174
2175 static void
2176 nfsd4_encode_open_confirm(struct nfsd4_compoundres *resp, int nfserr, struct nfsd4_open_confirm *oc)
2177 {
2178         ENCODE_SEQID_OP_HEAD;
2179                                         
2180         if (!nfserr) {
2181                 RESERVE_SPACE(sizeof(stateid_t));
2182                 WRITE32(oc->oc_resp_stateid.si_generation);
2183                 WRITEMEM(&oc->oc_resp_stateid.si_opaque, sizeof(stateid_opaque_t));
2184                 ADJUST_ARGS();
2185         }
2186
2187         ENCODE_SEQID_OP_TAIL(oc->oc_stateowner);
2188 }
2189
2190 static void
2191 nfsd4_encode_open_downgrade(struct nfsd4_compoundres *resp, int nfserr, struct nfsd4_open_downgrade *od)
2192 {
2193         ENCODE_SEQID_OP_HEAD;
2194                                         
2195         if (!nfserr) {
2196                 RESERVE_SPACE(sizeof(stateid_t));
2197                 WRITE32(od->od_stateid.si_generation);
2198                 WRITEMEM(&od->od_stateid.si_opaque, sizeof(stateid_opaque_t));
2199                 ADJUST_ARGS();
2200         }
2201
2202         ENCODE_SEQID_OP_TAIL(od->od_stateowner);
2203 }
2204
2205 static int
2206 nfsd4_encode_read(struct nfsd4_compoundres *resp, int nfserr,
2207                   struct nfsd4_read *read)
2208 {
2209         u32 eof;
2210         int v, pn;
2211         unsigned long maxcount; 
2212         long len;
2213         ENCODE_HEAD;
2214
2215         if (nfserr)
2216                 return nfserr;
2217         if (resp->xbuf->page_len)
2218                 return nfserr_resource;
2219
2220         RESERVE_SPACE(8); /* eof flag and byte count */
2221
2222         maxcount = svc_max_payload(resp->rqstp);
2223         if (maxcount > read->rd_length)
2224                 maxcount = read->rd_length;
2225
2226         len = maxcount;
2227         v = 0;
2228         while (len > 0) {
2229                 pn = resp->rqstp->rq_resused++;
2230                 resp->rqstp->rq_vec[v].iov_base =
2231                         page_address(resp->rqstp->rq_respages[pn]);
2232                 resp->rqstp->rq_vec[v].iov_len =
2233                         len < PAGE_SIZE ? len : PAGE_SIZE;
2234                 v++;
2235                 len -= PAGE_SIZE;
2236         }
2237         read->rd_vlen = v;
2238
2239         nfserr = nfsd_read(read->rd_rqstp, read->rd_fhp, read->rd_filp,
2240                         read->rd_offset, resp->rqstp->rq_vec, read->rd_vlen,
2241                         &maxcount);
2242
2243         if (nfserr == nfserr_symlink)
2244                 nfserr = nfserr_inval;
2245         if (nfserr)
2246                 return nfserr;
2247         eof = (read->rd_offset + maxcount >=
2248                read->rd_fhp->fh_dentry->d_inode->i_size);
2249
2250         WRITE32(eof);
2251         WRITE32(maxcount);
2252         ADJUST_ARGS();
2253         resp->xbuf->head[0].iov_len = (char*)p
2254                                         - (char*)resp->xbuf->head[0].iov_base;
2255         resp->xbuf->page_len = maxcount;
2256
2257         /* Use rest of head for padding and remaining ops: */
2258         resp->xbuf->tail[0].iov_base = p;
2259         resp->xbuf->tail[0].iov_len = 0;
2260         if (maxcount&3) {
2261                 RESERVE_SPACE(4);
2262                 WRITE32(0);
2263                 resp->xbuf->tail[0].iov_base += maxcount&3;
2264                 resp->xbuf->tail[0].iov_len = 4 - (maxcount&3);
2265                 ADJUST_ARGS();
2266         }
2267         return 0;
2268 }
2269
2270 static int
2271 nfsd4_encode_readlink(struct nfsd4_compoundres *resp, int nfserr, struct nfsd4_readlink *readlink)
2272 {
2273         int maxcount;
2274         char *page;
2275         ENCODE_HEAD;
2276
2277         if (nfserr)
2278                 return nfserr;
2279         if (resp->xbuf->page_len)
2280                 return nfserr_resource;
2281
2282         page = page_address(resp->rqstp->rq_respages[resp->rqstp->rq_resused++]);
2283
2284         maxcount = PAGE_SIZE;
2285         RESERVE_SPACE(4);
2286
2287         /*
2288          * XXX: By default, the ->readlink() VFS op will truncate symlinks
2289          * if they would overflow the buffer.  Is this kosher in NFSv4?  If
2290          * not, one easy fix is: if ->readlink() precisely fills the buffer,
2291          * assume that truncation occurred, and return NFS4ERR_RESOURCE.
2292          */
2293         nfserr = nfsd_readlink(readlink->rl_rqstp, readlink->rl_fhp, page, &maxcount);
2294         if (nfserr == nfserr_isdir)
2295                 return nfserr_inval;
2296         if (nfserr)
2297                 return nfserr;
2298
2299         WRITE32(maxcount);
2300         ADJUST_ARGS();
2301         resp->xbuf->head[0].iov_len = (char*)p
2302                                 - (char*)resp->xbuf->head[0].iov_base;
2303         resp->xbuf->page_len = maxcount;
2304
2305         /* Use rest of head for padding and remaining ops: */
2306         resp->xbuf->tail[0].iov_base = p;
2307         resp->xbuf->tail[0].iov_len = 0;
2308         if (maxcount&3) {
2309                 RESERVE_SPACE(4);
2310                 WRITE32(0);
2311                 resp->xbuf->tail[0].iov_base += maxcount&3;
2312                 resp->xbuf->tail[0].iov_len = 4 - (maxcount&3);
2313                 ADJUST_ARGS();
2314         }
2315         return 0;
2316 }
2317
2318 static int
2319 nfsd4_encode_readdir(struct nfsd4_compoundres *resp, int nfserr, struct nfsd4_readdir *readdir)
2320 {
2321         int maxcount;
2322         loff_t offset;
2323         u32 *page, *savep, *tailbase;
2324         ENCODE_HEAD;
2325
2326         if (nfserr)
2327                 return nfserr;
2328         if (resp->xbuf->page_len)
2329                 return nfserr_resource;
2330
2331         RESERVE_SPACE(8);  /* verifier */
2332         savep = p;
2333
2334         /* XXX: Following NFSv3, we ignore the READDIR verifier for now. */
2335         WRITE32(0);
2336         WRITE32(0);
2337         ADJUST_ARGS();
2338         resp->xbuf->head[0].iov_len = ((char*)resp->p) - (char*)resp->xbuf->head[0].iov_base;
2339         tailbase = p;
2340
2341         maxcount = PAGE_SIZE;
2342         if (maxcount > readdir->rd_maxcount)
2343                 maxcount = readdir->rd_maxcount;
2344
2345         /*
2346          * Convert from bytes to words, account for the two words already
2347          * written, make sure to leave two words at the end for the next
2348          * pointer and eof field.
2349          */
2350         maxcount = (maxcount >> 2) - 4;
2351         if (maxcount < 0) {
2352                 nfserr =  nfserr_toosmall;
2353                 goto err_no_verf;
2354         }
2355
2356         page = page_address(resp->rqstp->rq_respages[resp->rqstp->rq_resused++]);
2357         readdir->common.err = 0;
2358         readdir->buflen = maxcount;
2359         readdir->buffer = page;
2360         readdir->offset = NULL;
2361
2362         offset = readdir->rd_cookie;
2363         nfserr = nfsd_readdir(readdir->rd_rqstp, readdir->rd_fhp,
2364                               &offset,
2365                               &readdir->common, nfsd4_encode_dirent);
2366         if (nfserr == nfs_ok &&
2367             readdir->common.err == nfserr_toosmall &&
2368             readdir->buffer == page) 
2369                 nfserr = nfserr_toosmall;
2370         if (nfserr == nfserr_symlink)
2371                 nfserr = nfserr_notdir;
2372         if (nfserr)
2373                 goto err_no_verf;
2374
2375         if (readdir->offset)
2376                 xdr_encode_hyper(readdir->offset, offset);
2377
2378         p = readdir->buffer;
2379         *p++ = 0;       /* no more entries */
2380         *p++ = htonl(readdir->common.err == nfserr_eof);
2381         resp->xbuf->page_len = ((char*)p) - (char*)page_address(
2382                 resp->rqstp->rq_respages[resp->rqstp->rq_resused-1]);
2383
2384         /* Use rest of head for padding and remaining ops: */
2385         resp->xbuf->tail[0].iov_base = tailbase;
2386         resp->xbuf->tail[0].iov_len = 0;
2387         resp->p = resp->xbuf->tail[0].iov_base;
2388         resp->end = resp->p + (PAGE_SIZE - resp->xbuf->head[0].iov_len)/4;
2389
2390         return 0;
2391 err_no_verf:
2392         p = savep;
2393         ADJUST_ARGS();
2394         return nfserr;
2395 }
2396
2397 static void
2398 nfsd4_encode_remove(struct nfsd4_compoundres *resp, int nfserr, struct nfsd4_remove *remove)
2399 {
2400         ENCODE_HEAD;
2401
2402         if (!nfserr) {
2403                 RESERVE_SPACE(20);
2404                 WRITECINFO(remove->rm_cinfo);
2405                 ADJUST_ARGS();
2406         }
2407 }
2408
2409 static void
2410 nfsd4_encode_rename(struct nfsd4_compoundres *resp, int nfserr, struct nfsd4_rename *rename)
2411 {
2412         ENCODE_HEAD;
2413
2414         if (!nfserr) {
2415                 RESERVE_SPACE(40);
2416                 WRITECINFO(rename->rn_sinfo);
2417                 WRITECINFO(rename->rn_tinfo);
2418                 ADJUST_ARGS();
2419         }
2420 }
2421
2422 /*
2423  * The SETATTR encode routine is special -- it always encodes a bitmap,
2424  * regardless of the error status.
2425  */
2426 static void
2427 nfsd4_encode_setattr(struct nfsd4_compoundres *resp, int nfserr, struct nfsd4_setattr *setattr)
2428 {
2429         ENCODE_HEAD;
2430
2431         RESERVE_SPACE(12);
2432         if (nfserr) {
2433                 WRITE32(2);
2434                 WRITE32(0);
2435                 WRITE32(0);
2436         }
2437         else {
2438                 WRITE32(2);
2439                 WRITE32(setattr->sa_bmval[0]);
2440                 WRITE32(setattr->sa_bmval[1]);
2441         }
2442         ADJUST_ARGS();
2443 }
2444
2445 static void
2446 nfsd4_encode_setclientid(struct nfsd4_compoundres *resp, int nfserr, struct nfsd4_setclientid *scd)
2447 {
2448         ENCODE_HEAD;
2449
2450         if (!nfserr) {
2451                 RESERVE_SPACE(8 + sizeof(nfs4_verifier));
2452                 WRITEMEM(&scd->se_clientid, 8);
2453                 WRITEMEM(&scd->se_confirm, sizeof(nfs4_verifier));
2454                 ADJUST_ARGS();
2455         }
2456         else if (nfserr == nfserr_clid_inuse) {
2457                 RESERVE_SPACE(8);
2458                 WRITE32(0);
2459                 WRITE32(0);
2460                 ADJUST_ARGS();
2461         }
2462 }
2463
2464 static void
2465 nfsd4_encode_write(struct nfsd4_compoundres *resp, int nfserr, struct nfsd4_write *write)
2466 {
2467         ENCODE_HEAD;
2468
2469         if (!nfserr) {
2470                 RESERVE_SPACE(16);
2471                 WRITE32(write->wr_bytes_written);
2472                 WRITE32(write->wr_how_written);
2473                 WRITEMEM(write->wr_verifier.data, 8);
2474                 ADJUST_ARGS();
2475         }
2476 }
2477
2478 void
2479 nfsd4_encode_operation(struct nfsd4_compoundres *resp, struct nfsd4_op *op)
2480 {
2481         u32 *statp;
2482         ENCODE_HEAD;
2483
2484         RESERVE_SPACE(8);
2485         WRITE32(op->opnum);
2486         statp = p++;    /* to be backfilled at the end */
2487         ADJUST_ARGS();
2488
2489         switch (op->opnum) {
2490         case OP_ACCESS:
2491                 nfsd4_encode_access(resp, op->status, &op->u.access);
2492                 break;
2493         case OP_CLOSE:
2494                 nfsd4_encode_close(resp, op->status, &op->u.close);
2495                 break;
2496         case OP_COMMIT:
2497                 nfsd4_encode_commit(resp, op->status, &op->u.commit);
2498                 break;
2499         case OP_CREATE:
2500                 nfsd4_encode_create(resp, op->status, &op->u.create);
2501                 break;
2502         case OP_DELEGRETURN:
2503                 break;
2504         case OP_GETATTR:
2505                 op->status = nfsd4_encode_getattr(resp, op->status, &op->u.getattr);
2506                 break;
2507         case OP_GETFH:
2508                 nfsd4_encode_getfh(resp, op->status, op->u.getfh);
2509                 break;
2510         case OP_LINK:
2511                 nfsd4_encode_link(resp, op->status, &op->u.link);
2512                 break;
2513         case OP_LOCK:
2514                 nfsd4_encode_lock(resp, op->status, &op->u.lock);
2515                 break;
2516         case OP_LOCKT:
2517                 nfsd4_encode_lockt(resp, op->status, &op->u.lockt);
2518                 break;
2519         case OP_LOCKU:
2520                 nfsd4_encode_locku(resp, op->status, &op->u.locku);
2521                 break;
2522         case OP_LOOKUP:
2523                 break;
2524         case OP_LOOKUPP:
2525                 break;
2526         case OP_NVERIFY:
2527                 break;
2528         case OP_OPEN:
2529                 nfsd4_encode_open(resp, op->status, &op->u.open);
2530                 break;
2531         case OP_OPEN_CONFIRM:
2532                 nfsd4_encode_open_confirm(resp, op->status, &op->u.open_confirm);
2533                 break;
2534         case OP_OPEN_DOWNGRADE:
2535                 nfsd4_encode_open_downgrade(resp, op->status, &op->u.open_downgrade);
2536                 break;
2537         case OP_PUTFH:
2538                 break;
2539         case OP_PUTROOTFH:
2540                 break;
2541         case OP_READ:
2542                 op->status = nfsd4_encode_read(resp, op->status, &op->u.read);
2543                 break;
2544         case OP_READDIR:
2545                 op->status = nfsd4_encode_readdir(resp, op->status, &op->u.readdir);
2546                 break;
2547         case OP_READLINK:
2548                 op->status = nfsd4_encode_readlink(resp, op->status, &op->u.readlink);
2549                 break;
2550         case OP_REMOVE:
2551                 nfsd4_encode_remove(resp, op->status, &op->u.remove);
2552                 break;
2553         case OP_RENAME:
2554                 nfsd4_encode_rename(resp, op->status, &op->u.rename);
2555                 break;
2556         case OP_RENEW:
2557                 break;
2558         case OP_RESTOREFH:
2559                 break;
2560         case OP_SAVEFH:
2561                 break;
2562         case OP_SETATTR:
2563                 nfsd4_encode_setattr(resp, op->status, &op->u.setattr);
2564                 break;
2565         case OP_SETCLIENTID:
2566                 nfsd4_encode_setclientid(resp, op->status, &op->u.setclientid);
2567                 break;
2568         case OP_SETCLIENTID_CONFIRM:
2569                 break;
2570         case OP_VERIFY:
2571                 break;
2572         case OP_WRITE:
2573                 nfsd4_encode_write(resp, op->status, &op->u.write);
2574                 break;
2575         case OP_RELEASE_LOCKOWNER:
2576                 break;
2577         default:
2578                 break;
2579         }
2580
2581         /*
2582          * Note: We write the status directly, instead of using WRITE32(),
2583          * since it is already in network byte order.
2584          */
2585         *statp = op->status;
2586 }
2587
2588 /* 
2589  * Encode the reply stored in the stateowner reply cache 
2590  * 
2591  * XDR note: do not encode rp->rp_buflen: the buffer contains the
2592  * previously sent already encoded operation.
2593  *
2594  * called with nfs4_lock_state() held
2595  */
2596 void
2597 nfsd4_encode_replay(struct nfsd4_compoundres *resp, struct nfsd4_op *op)
2598 {
2599         ENCODE_HEAD;
2600         struct nfs4_replay *rp = op->replay;
2601
2602         BUG_ON(!rp);
2603
2604         RESERVE_SPACE(8);
2605         WRITE32(op->opnum);
2606         *p++ = rp->rp_status;  /* already xdr'ed */
2607         ADJUST_ARGS();
2608
2609         RESERVE_SPACE(rp->rp_buflen);
2610         WRITEMEM(rp->rp_buf, rp->rp_buflen);
2611         ADJUST_ARGS();
2612 }
2613
2614 /*
2615  * END OF "GENERIC" ENCODE ROUTINES.
2616  */
2617
2618 int
2619 nfs4svc_encode_voidres(struct svc_rqst *rqstp, u32 *p, void *dummy)
2620 {
2621         return xdr_ressize_check(rqstp, p);
2622 }
2623
2624 void nfsd4_release_compoundargs(struct nfsd4_compoundargs *args)
2625 {
2626         if (args->ops != args->iops) {
2627                 kfree(args->ops);
2628                 args->ops = args->iops;
2629         }
2630         kfree(args->tmpp);
2631         args->tmpp = NULL;
2632         while (args->to_free) {
2633                 struct tmpbuf *tb = args->to_free;
2634                 args->to_free = tb->next;
2635                 tb->release(tb->buf);
2636                 kfree(tb);
2637         }
2638 }
2639
2640 int
2641 nfs4svc_decode_compoundargs(struct svc_rqst *rqstp, u32 *p, struct nfsd4_compoundargs *args)
2642 {
2643         int status;
2644
2645         args->p = p;
2646         args->end = rqstp->rq_arg.head[0].iov_base + rqstp->rq_arg.head[0].iov_len;
2647         args->pagelist = rqstp->rq_arg.pages;
2648         args->pagelen = rqstp->rq_arg.page_len;
2649         args->tmpp = NULL;
2650         args->to_free = NULL;
2651         args->ops = args->iops;
2652         args->rqstp = rqstp;
2653
2654         status = nfsd4_decode_compound(args);
2655         if (status) {
2656                 nfsd4_release_compoundargs(args);
2657         }
2658         return !status;
2659 }
2660
2661 int
2662 nfs4svc_encode_compoundres(struct svc_rqst *rqstp, u32 *p, struct nfsd4_compoundres *resp)
2663 {
2664         /*
2665          * All that remains is to write the tag and operation count...
2666          */
2667         struct kvec *iov;
2668         p = resp->tagp;
2669         *p++ = htonl(resp->taglen);
2670         memcpy(p, resp->tag, resp->taglen);
2671         p += XDR_QUADLEN(resp->taglen);
2672         *p++ = htonl(resp->opcnt);
2673
2674         if (rqstp->rq_res.page_len) 
2675                 iov = &rqstp->rq_res.tail[0];
2676         else
2677                 iov = &rqstp->rq_res.head[0];
2678         iov->iov_len = ((char*)resp->p) - (char*)iov->iov_base;
2679         BUG_ON(iov->iov_len > PAGE_SIZE);
2680         return 1;
2681 }
2682
2683 /*
2684  * Local variables:
2685  *  c-basic-offset: 8
2686  * End:
2687  */