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