linux/fs/nfs/nfs4proc.c
<<
>>
Prefs
   1/*
   2 *  fs/nfs/nfs4proc.c
   3 *
   4 *  Client-side procedure declarations 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
  38#include <linux/mm.h>
  39#include <linux/delay.h>
  40#include <linux/errno.h>
  41#include <linux/string.h>
  42#include <linux/ratelimit.h>
  43#include <linux/printk.h>
  44#include <linux/slab.h>
  45#include <linux/sunrpc/clnt.h>
  46#include <linux/nfs.h>
  47#include <linux/nfs4.h>
  48#include <linux/nfs_fs.h>
  49#include <linux/nfs_page.h>
  50#include <linux/nfs_mount.h>
  51#include <linux/namei.h>
  52#include <linux/mount.h>
  53#include <linux/module.h>
  54#include <linux/xattr.h>
  55#include <linux/utsname.h>
  56#include <linux/freezer.h>
  57#include <linux/iversion.h>
  58
  59#include "nfs4_fs.h"
  60#include "delegation.h"
  61#include "internal.h"
  62#include "iostat.h"
  63#include "callback.h"
  64#include "pnfs.h"
  65#include "netns.h"
  66#include "nfs4idmap.h"
  67#include "nfs4session.h"
  68#include "fscache.h"
  69
  70#include "nfs4trace.h"
  71
  72#define NFSDBG_FACILITY         NFSDBG_PROC
  73
  74#define NFS4_BITMASK_SZ         3
  75
  76#define NFS4_POLL_RETRY_MIN     (HZ/10)
  77#define NFS4_POLL_RETRY_MAX     (15*HZ)
  78
  79/* file attributes which can be mapped to nfs attributes */
  80#define NFS4_VALID_ATTRS (ATTR_MODE \
  81        | ATTR_UID \
  82        | ATTR_GID \
  83        | ATTR_SIZE \
  84        | ATTR_ATIME \
  85        | ATTR_MTIME \
  86        | ATTR_CTIME \
  87        | ATTR_ATIME_SET \
  88        | ATTR_MTIME_SET)
  89
  90struct nfs4_opendata;
  91static int _nfs4_recover_proc_open(struct nfs4_opendata *data);
  92static int nfs4_do_fsinfo(struct nfs_server *, struct nfs_fh *, struct nfs_fsinfo *);
  93static void nfs_fixup_referral_attributes(struct nfs_fattr *fattr);
  94static int nfs4_proc_getattr(struct nfs_server *, struct nfs_fh *, struct nfs_fattr *, struct nfs4_label *label, struct inode *inode);
  95static int _nfs4_proc_getattr(struct nfs_server *server, struct nfs_fh *fhandle, struct nfs_fattr *fattr, struct nfs4_label *label, struct inode *inode);
  96static int nfs4_do_setattr(struct inode *inode, const struct cred *cred,
  97                            struct nfs_fattr *fattr, struct iattr *sattr,
  98                            struct nfs_open_context *ctx, struct nfs4_label *ilabel,
  99                            struct nfs4_label *olabel);
 100#ifdef CONFIG_NFS_V4_1
 101static struct rpc_task *_nfs41_proc_sequence(struct nfs_client *clp,
 102                const struct cred *cred,
 103                struct nfs4_slot *slot,
 104                bool is_privileged);
 105static int nfs41_test_stateid(struct nfs_server *, nfs4_stateid *,
 106                const struct cred *);
 107static int nfs41_free_stateid(struct nfs_server *, const nfs4_stateid *,
 108                const struct cred *, bool);
 109#endif
 110
 111#ifdef CONFIG_NFS_V4_SECURITY_LABEL
 112static inline struct nfs4_label *
 113nfs4_label_init_security(struct inode *dir, struct dentry *dentry,
 114        struct iattr *sattr, struct nfs4_label *label)
 115{
 116        int err;
 117
 118        if (label == NULL)
 119                return NULL;
 120
 121        if (nfs_server_capable(dir, NFS_CAP_SECURITY_LABEL) == 0)
 122                return NULL;
 123
 124        err = security_dentry_init_security(dentry, sattr->ia_mode,
 125                                &dentry->d_name, (void **)&label->label, &label->len);
 126        if (err == 0)
 127                return label;
 128
 129        return NULL;
 130}
 131static inline void
 132nfs4_label_release_security(struct nfs4_label *label)
 133{
 134        if (label)
 135                security_release_secctx(label->label, label->len);
 136}
 137static inline u32 *nfs4_bitmask(struct nfs_server *server, struct nfs4_label *label)
 138{
 139        if (label)
 140                return server->attr_bitmask;
 141
 142        return server->attr_bitmask_nl;
 143}
 144#else
 145static inline struct nfs4_label *
 146nfs4_label_init_security(struct inode *dir, struct dentry *dentry,
 147        struct iattr *sattr, struct nfs4_label *l)
 148{ return NULL; }
 149static inline void
 150nfs4_label_release_security(struct nfs4_label *label)
 151{ return; }
 152static inline u32 *
 153nfs4_bitmask(struct nfs_server *server, struct nfs4_label *label)
 154{ return server->attr_bitmask; }
 155#endif
 156
 157/* Prevent leaks of NFSv4 errors into userland */
 158static int nfs4_map_errors(int err)
 159{
 160        if (err >= -1000)
 161                return err;
 162        switch (err) {
 163        case -NFS4ERR_RESOURCE:
 164        case -NFS4ERR_LAYOUTTRYLATER:
 165        case -NFS4ERR_RECALLCONFLICT:
 166                return -EREMOTEIO;
 167        case -NFS4ERR_WRONGSEC:
 168        case -NFS4ERR_WRONG_CRED:
 169                return -EPERM;
 170        case -NFS4ERR_BADOWNER:
 171        case -NFS4ERR_BADNAME:
 172                return -EINVAL;
 173        case -NFS4ERR_SHARE_DENIED:
 174                return -EACCES;
 175        case -NFS4ERR_MINOR_VERS_MISMATCH:
 176                return -EPROTONOSUPPORT;
 177        case -NFS4ERR_FILE_OPEN:
 178                return -EBUSY;
 179        default:
 180                dprintk("%s could not handle NFSv4 error %d\n",
 181                                __func__, -err);
 182                break;
 183        }
 184        return -EIO;
 185}
 186
 187/*
 188 * This is our standard bitmap for GETATTR requests.
 189 */
 190const u32 nfs4_fattr_bitmap[3] = {
 191        FATTR4_WORD0_TYPE
 192        | FATTR4_WORD0_CHANGE
 193        | FATTR4_WORD0_SIZE
 194        | FATTR4_WORD0_FSID
 195        | FATTR4_WORD0_FILEID,
 196        FATTR4_WORD1_MODE
 197        | FATTR4_WORD1_NUMLINKS
 198        | FATTR4_WORD1_OWNER
 199        | FATTR4_WORD1_OWNER_GROUP
 200        | FATTR4_WORD1_RAWDEV
 201        | FATTR4_WORD1_SPACE_USED
 202        | FATTR4_WORD1_TIME_ACCESS
 203        | FATTR4_WORD1_TIME_METADATA
 204        | FATTR4_WORD1_TIME_MODIFY
 205        | FATTR4_WORD1_MOUNTED_ON_FILEID,
 206#ifdef CONFIG_NFS_V4_SECURITY_LABEL
 207        FATTR4_WORD2_SECURITY_LABEL
 208#endif
 209};
 210
 211static const u32 nfs4_pnfs_open_bitmap[3] = {
 212        FATTR4_WORD0_TYPE
 213        | FATTR4_WORD0_CHANGE
 214        | FATTR4_WORD0_SIZE
 215        | FATTR4_WORD0_FSID
 216        | FATTR4_WORD0_FILEID,
 217        FATTR4_WORD1_MODE
 218        | FATTR4_WORD1_NUMLINKS
 219        | FATTR4_WORD1_OWNER
 220        | FATTR4_WORD1_OWNER_GROUP
 221        | FATTR4_WORD1_RAWDEV
 222        | FATTR4_WORD1_SPACE_USED
 223        | FATTR4_WORD1_TIME_ACCESS
 224        | FATTR4_WORD1_TIME_METADATA
 225        | FATTR4_WORD1_TIME_MODIFY,
 226        FATTR4_WORD2_MDSTHRESHOLD
 227#ifdef CONFIG_NFS_V4_SECURITY_LABEL
 228        | FATTR4_WORD2_SECURITY_LABEL
 229#endif
 230};
 231
 232static const u32 nfs4_open_noattr_bitmap[3] = {
 233        FATTR4_WORD0_TYPE
 234        | FATTR4_WORD0_FILEID,
 235};
 236
 237const u32 nfs4_statfs_bitmap[3] = {
 238        FATTR4_WORD0_FILES_AVAIL
 239        | FATTR4_WORD0_FILES_FREE
 240        | FATTR4_WORD0_FILES_TOTAL,
 241        FATTR4_WORD1_SPACE_AVAIL
 242        | FATTR4_WORD1_SPACE_FREE
 243        | FATTR4_WORD1_SPACE_TOTAL
 244};
 245
 246const u32 nfs4_pathconf_bitmap[3] = {
 247        FATTR4_WORD0_MAXLINK
 248        | FATTR4_WORD0_MAXNAME,
 249        0
 250};
 251
 252const u32 nfs4_fsinfo_bitmap[3] = { FATTR4_WORD0_MAXFILESIZE
 253                        | FATTR4_WORD0_MAXREAD
 254                        | FATTR4_WORD0_MAXWRITE
 255                        | FATTR4_WORD0_LEASE_TIME,
 256                        FATTR4_WORD1_TIME_DELTA
 257                        | FATTR4_WORD1_FS_LAYOUT_TYPES,
 258                        FATTR4_WORD2_LAYOUT_BLKSIZE
 259                        | FATTR4_WORD2_CLONE_BLKSIZE
 260};
 261
 262const u32 nfs4_fs_locations_bitmap[3] = {
 263        FATTR4_WORD0_CHANGE
 264        | FATTR4_WORD0_SIZE
 265        | FATTR4_WORD0_FSID
 266        | FATTR4_WORD0_FILEID
 267        | FATTR4_WORD0_FS_LOCATIONS,
 268        FATTR4_WORD1_OWNER
 269        | FATTR4_WORD1_OWNER_GROUP
 270        | FATTR4_WORD1_RAWDEV
 271        | FATTR4_WORD1_SPACE_USED
 272        | FATTR4_WORD1_TIME_ACCESS
 273        | FATTR4_WORD1_TIME_METADATA
 274        | FATTR4_WORD1_TIME_MODIFY
 275        | FATTR4_WORD1_MOUNTED_ON_FILEID,
 276};
 277
 278static void nfs4_bitmap_copy_adjust(__u32 *dst, const __u32 *src,
 279                struct inode *inode)
 280{
 281        unsigned long cache_validity;
 282
 283        memcpy(dst, src, NFS4_BITMASK_SZ*sizeof(*dst));
 284        if (!inode || !nfs4_have_delegation(inode, FMODE_READ))
 285                return;
 286
 287        cache_validity = READ_ONCE(NFS_I(inode)->cache_validity);
 288        if (!(cache_validity & NFS_INO_REVAL_FORCED))
 289                cache_validity &= ~(NFS_INO_INVALID_CHANGE
 290                                | NFS_INO_INVALID_SIZE);
 291
 292        if (!(cache_validity & NFS_INO_INVALID_SIZE))
 293                dst[0] &= ~FATTR4_WORD0_SIZE;
 294
 295        if (!(cache_validity & NFS_INO_INVALID_CHANGE))
 296                dst[0] &= ~FATTR4_WORD0_CHANGE;
 297}
 298
 299static void nfs4_bitmap_copy_adjust_setattr(__u32 *dst,
 300                const __u32 *src, struct inode *inode)
 301{
 302        nfs4_bitmap_copy_adjust(dst, src, inode);
 303}
 304
 305static void nfs4_setup_readdir(u64 cookie, __be32 *verifier, struct dentry *dentry,
 306                struct nfs4_readdir_arg *readdir)
 307{
 308        unsigned int attrs = FATTR4_WORD0_FILEID | FATTR4_WORD0_TYPE;
 309        __be32 *start, *p;
 310
 311        if (cookie > 2) {
 312                readdir->cookie = cookie;
 313                memcpy(&readdir->verifier, verifier, sizeof(readdir->verifier));
 314                return;
 315        }
 316
 317        readdir->cookie = 0;
 318        memset(&readdir->verifier, 0, sizeof(readdir->verifier));
 319        if (cookie == 2)
 320                return;
 321        
 322        /*
 323         * NFSv4 servers do not return entries for '.' and '..'
 324         * Therefore, we fake these entries here.  We let '.'
 325         * have cookie 0 and '..' have cookie 1.  Note that
 326         * when talking to the server, we always send cookie 0
 327         * instead of 1 or 2.
 328         */
 329        start = p = kmap_atomic(*readdir->pages);
 330        
 331        if (cookie == 0) {
 332                *p++ = xdr_one;                                  /* next */
 333                *p++ = xdr_zero;                   /* cookie, first word */
 334                *p++ = xdr_one;                   /* cookie, second word */
 335                *p++ = xdr_one;                             /* entry len */
 336                memcpy(p, ".\0\0\0", 4);                        /* entry */
 337                p++;
 338                *p++ = xdr_one;                         /* bitmap length */
 339                *p++ = htonl(attrs);                           /* bitmap */
 340                *p++ = htonl(12);             /* attribute buffer length */
 341                *p++ = htonl(NF4DIR);
 342                p = xdr_encode_hyper(p, NFS_FILEID(d_inode(dentry)));
 343        }
 344        
 345        *p++ = xdr_one;                                  /* next */
 346        *p++ = xdr_zero;                   /* cookie, first word */
 347        *p++ = xdr_two;                   /* cookie, second word */
 348        *p++ = xdr_two;                             /* entry len */
 349        memcpy(p, "..\0\0", 4);                         /* entry */
 350        p++;
 351        *p++ = xdr_one;                         /* bitmap length */
 352        *p++ = htonl(attrs);                           /* bitmap */
 353        *p++ = htonl(12);             /* attribute buffer length */
 354        *p++ = htonl(NF4DIR);
 355        p = xdr_encode_hyper(p, NFS_FILEID(d_inode(dentry->d_parent)));
 356
 357        readdir->pgbase = (char *)p - (char *)start;
 358        readdir->count -= readdir->pgbase;
 359        kunmap_atomic(start);
 360}
 361
 362static void nfs4_test_and_free_stateid(struct nfs_server *server,
 363                nfs4_stateid *stateid,
 364                const struct cred *cred)
 365{
 366        const struct nfs4_minor_version_ops *ops = server->nfs_client->cl_mvops;
 367
 368        ops->test_and_free_expired(server, stateid, cred);
 369}
 370
 371static void __nfs4_free_revoked_stateid(struct nfs_server *server,
 372                nfs4_stateid *stateid,
 373                const struct cred *cred)
 374{
 375        stateid->type = NFS4_REVOKED_STATEID_TYPE;
 376        nfs4_test_and_free_stateid(server, stateid, cred);
 377}
 378
 379static void nfs4_free_revoked_stateid(struct nfs_server *server,
 380                const nfs4_stateid *stateid,
 381                const struct cred *cred)
 382{
 383        nfs4_stateid tmp;
 384
 385        nfs4_stateid_copy(&tmp, stateid);
 386        __nfs4_free_revoked_stateid(server, &tmp, cred);
 387}
 388
 389static long nfs4_update_delay(long *timeout)
 390{
 391        long ret;
 392        if (!timeout)
 393                return NFS4_POLL_RETRY_MAX;
 394        if (*timeout <= 0)
 395                *timeout = NFS4_POLL_RETRY_MIN;
 396        if (*timeout > NFS4_POLL_RETRY_MAX)
 397                *timeout = NFS4_POLL_RETRY_MAX;
 398        ret = *timeout;
 399        *timeout <<= 1;
 400        return ret;
 401}
 402
 403static int nfs4_delay_killable(long *timeout)
 404{
 405        might_sleep();
 406
 407        freezable_schedule_timeout_killable_unsafe(
 408                nfs4_update_delay(timeout));
 409        if (!__fatal_signal_pending(current))
 410                return 0;
 411        return -EINTR;
 412}
 413
 414static int nfs4_delay_interruptible(long *timeout)
 415{
 416        might_sleep();
 417
 418        freezable_schedule_timeout_interruptible(nfs4_update_delay(timeout));
 419        if (!signal_pending(current))
 420                return 0;
 421        return __fatal_signal_pending(current) ? -EINTR :-ERESTARTSYS;
 422}
 423
 424static int nfs4_delay(long *timeout, bool interruptible)
 425{
 426        if (interruptible)
 427                return nfs4_delay_interruptible(timeout);
 428        return nfs4_delay_killable(timeout);
 429}
 430
 431static const nfs4_stateid *
 432nfs4_recoverable_stateid(const nfs4_stateid *stateid)
 433{
 434        if (!stateid)
 435                return NULL;
 436        switch (stateid->type) {
 437        case NFS4_OPEN_STATEID_TYPE:
 438        case NFS4_LOCK_STATEID_TYPE:
 439        case NFS4_DELEGATION_STATEID_TYPE:
 440                return stateid;
 441        default:
 442                break;
 443        }
 444        return NULL;
 445}
 446
 447/* This is the error handling routine for processes that are allowed
 448 * to sleep.
 449 */
 450static int nfs4_do_handle_exception(struct nfs_server *server,
 451                int errorcode, struct nfs4_exception *exception)
 452{
 453        struct nfs_client *clp = server->nfs_client;
 454        struct nfs4_state *state = exception->state;
 455        const nfs4_stateid *stateid;
 456        struct inode *inode = exception->inode;
 457        int ret = errorcode;
 458
 459        exception->delay = 0;
 460        exception->recovering = 0;
 461        exception->retry = 0;
 462
 463        stateid = nfs4_recoverable_stateid(exception->stateid);
 464        if (stateid == NULL && state != NULL)
 465                stateid = nfs4_recoverable_stateid(&state->stateid);
 466
 467        switch(errorcode) {
 468                case 0:
 469                        return 0;
 470                case -NFS4ERR_BADHANDLE:
 471                case -ESTALE:
 472                        if (inode != NULL && S_ISREG(inode->i_mode))
 473                                pnfs_destroy_layout(NFS_I(inode));
 474                        break;
 475                case -NFS4ERR_DELEG_REVOKED:
 476                case -NFS4ERR_ADMIN_REVOKED:
 477                case -NFS4ERR_EXPIRED:
 478                case -NFS4ERR_BAD_STATEID:
 479                        if (inode != NULL && stateid != NULL) {
 480                                nfs_inode_find_state_and_recover(inode,
 481                                                stateid);
 482                                goto wait_on_recovery;
 483                        }
 484                        /* Fall through */
 485                case -NFS4ERR_OPENMODE:
 486                        if (inode) {
 487                                int err;
 488
 489                                err = nfs_async_inode_return_delegation(inode,
 490                                                stateid);
 491                                if (err == 0)
 492                                        goto wait_on_recovery;
 493                                if (stateid != NULL && stateid->type == NFS4_DELEGATION_STATEID_TYPE) {
 494                                        exception->retry = 1;
 495                                        break;
 496                                }
 497                        }
 498                        if (state == NULL)
 499                                break;
 500                        ret = nfs4_schedule_stateid_recovery(server, state);
 501                        if (ret < 0)
 502                                break;
 503                        goto wait_on_recovery;
 504                case -NFS4ERR_STALE_STATEID:
 505                case -NFS4ERR_STALE_CLIENTID:
 506                        nfs4_schedule_lease_recovery(clp);
 507                        goto wait_on_recovery;
 508                case -NFS4ERR_MOVED:
 509                        ret = nfs4_schedule_migration_recovery(server);
 510                        if (ret < 0)
 511                                break;
 512                        goto wait_on_recovery;
 513                case -NFS4ERR_LEASE_MOVED:
 514                        nfs4_schedule_lease_moved_recovery(clp);
 515                        goto wait_on_recovery;
 516#if defined(CONFIG_NFS_V4_1)
 517                case -NFS4ERR_BADSESSION:
 518                case -NFS4ERR_BADSLOT:
 519                case -NFS4ERR_BAD_HIGH_SLOT:
 520                case -NFS4ERR_CONN_NOT_BOUND_TO_SESSION:
 521                case -NFS4ERR_DEADSESSION:
 522                case -NFS4ERR_SEQ_FALSE_RETRY:
 523                case -NFS4ERR_SEQ_MISORDERED:
 524                        dprintk("%s ERROR: %d Reset session\n", __func__,
 525                                errorcode);
 526                        nfs4_schedule_session_recovery(clp->cl_session, errorcode);
 527                        goto wait_on_recovery;
 528#endif /* defined(CONFIG_NFS_V4_1) */
 529                case -NFS4ERR_FILE_OPEN:
 530                        if (exception->timeout > HZ) {
 531                                /* We have retried a decent amount, time to
 532                                 * fail
 533                                 */
 534                                ret = -EBUSY;
 535                                break;
 536                        }
 537                        /* Fall through */
 538                case -NFS4ERR_DELAY:
 539                        nfs_inc_server_stats(server, NFSIOS_DELAY);
 540                        /* Fall through */
 541                case -NFS4ERR_GRACE:
 542                case -NFS4ERR_LAYOUTTRYLATER:
 543                case -NFS4ERR_RECALLCONFLICT:
 544                        exception->delay = 1;
 545                        return 0;
 546
 547                case -NFS4ERR_RETRY_UNCACHED_REP:
 548                case -NFS4ERR_OLD_STATEID:
 549                        exception->retry = 1;
 550                        break;
 551                case -NFS4ERR_BADOWNER:
 552                        /* The following works around a Linux server bug! */
 553                case -NFS4ERR_BADNAME:
 554                        if (server->caps & NFS_CAP_UIDGID_NOMAP) {
 555                                server->caps &= ~NFS_CAP_UIDGID_NOMAP;
 556                                exception->retry = 1;
 557                                printk(KERN_WARNING "NFS: v4 server %s "
 558                                                "does not accept raw "
 559                                                "uid/gids. "
 560                                                "Reenabling the idmapper.\n",
 561                                                server->nfs_client->cl_hostname);
 562                        }
 563        }
 564        /* We failed to handle the error */
 565        return nfs4_map_errors(ret);
 566wait_on_recovery:
 567        exception->recovering = 1;
 568        return 0;
 569}
 570
 571/* This is the error handling routine for processes that are allowed
 572 * to sleep.
 573 */
 574int nfs4_handle_exception(struct nfs_server *server, int errorcode, struct nfs4_exception *exception)
 575{
 576        struct nfs_client *clp = server->nfs_client;
 577        int ret;
 578
 579        ret = nfs4_do_handle_exception(server, errorcode, exception);
 580        if (exception->delay) {
 581                ret = nfs4_delay(&exception->timeout,
 582                                exception->interruptible);
 583                goto out_retry;
 584        }
 585        if (exception->recovering) {
 586                ret = nfs4_wait_clnt_recover(clp);
 587                if (test_bit(NFS_MIG_FAILED, &server->mig_status))
 588                        return -EIO;
 589                goto out_retry;
 590        }
 591        return ret;
 592out_retry:
 593        if (ret == 0)
 594                exception->retry = 1;
 595        return ret;
 596}
 597
 598static int
 599nfs4_async_handle_exception(struct rpc_task *task, struct nfs_server *server,
 600                int errorcode, struct nfs4_exception *exception)
 601{
 602        struct nfs_client *clp = server->nfs_client;
 603        int ret;
 604
 605        ret = nfs4_do_handle_exception(server, errorcode, exception);
 606        if (exception->delay) {
 607                rpc_delay(task, nfs4_update_delay(&exception->timeout));
 608                goto out_retry;
 609        }
 610        if (exception->recovering) {
 611                rpc_sleep_on(&clp->cl_rpcwaitq, task, NULL);
 612                if (test_bit(NFS4CLNT_MANAGER_RUNNING, &clp->cl_state) == 0)
 613                        rpc_wake_up_queued_task(&clp->cl_rpcwaitq, task);
 614                goto out_retry;
 615        }
 616        if (test_bit(NFS_MIG_FAILED, &server->mig_status))
 617                ret = -EIO;
 618        return ret;
 619out_retry:
 620        if (ret == 0) {
 621                exception->retry = 1;
 622                /*
 623                 * For NFS4ERR_MOVED, the client transport will need to
 624                 * be recomputed after migration recovery has completed.
 625                 */
 626                if (errorcode == -NFS4ERR_MOVED)
 627                        rpc_task_release_transport(task);
 628        }
 629        return ret;
 630}
 631
 632int
 633nfs4_async_handle_error(struct rpc_task *task, struct nfs_server *server,
 634                        struct nfs4_state *state, long *timeout)
 635{
 636        struct nfs4_exception exception = {
 637                .state = state,
 638        };
 639
 640        if (task->tk_status >= 0)
 641                return 0;
 642        if (timeout)
 643                exception.timeout = *timeout;
 644        task->tk_status = nfs4_async_handle_exception(task, server,
 645                        task->tk_status,
 646                        &exception);
 647        if (exception.delay && timeout)
 648                *timeout = exception.timeout;
 649        if (exception.retry)
 650                return -EAGAIN;
 651        return 0;
 652}
 653
 654/*
 655 * Return 'true' if 'clp' is using an rpc_client that is integrity protected
 656 * or 'false' otherwise.
 657 */
 658static bool _nfs4_is_integrity_protected(struct nfs_client *clp)
 659{
 660        rpc_authflavor_t flavor = clp->cl_rpcclient->cl_auth->au_flavor;
 661        return (flavor == RPC_AUTH_GSS_KRB5I) || (flavor == RPC_AUTH_GSS_KRB5P);
 662}
 663
 664static void do_renew_lease(struct nfs_client *clp, unsigned long timestamp)
 665{
 666        spin_lock(&clp->cl_lock);
 667        if (time_before(clp->cl_last_renewal,timestamp))
 668                clp->cl_last_renewal = timestamp;
 669        spin_unlock(&clp->cl_lock);
 670}
 671
 672static void renew_lease(const struct nfs_server *server, unsigned long timestamp)
 673{
 674        struct nfs_client *clp = server->nfs_client;
 675
 676        if (!nfs4_has_session(clp))
 677                do_renew_lease(clp, timestamp);
 678}
 679
 680struct nfs4_call_sync_data {
 681        const struct nfs_server *seq_server;
 682        struct nfs4_sequence_args *seq_args;
 683        struct nfs4_sequence_res *seq_res;
 684};
 685
 686void nfs4_init_sequence(struct nfs4_sequence_args *args,
 687                        struct nfs4_sequence_res *res, int cache_reply,
 688                        int privileged)
 689{
 690        args->sa_slot = NULL;
 691        args->sa_cache_this = cache_reply;
 692        args->sa_privileged = privileged;
 693
 694        res->sr_slot = NULL;
 695}
 696
 697static void nfs40_sequence_free_slot(struct nfs4_sequence_res *res)
 698{
 699        struct nfs4_slot *slot = res->sr_slot;
 700        struct nfs4_slot_table *tbl;
 701
 702        tbl = slot->table;
 703        spin_lock(&tbl->slot_tbl_lock);
 704        if (!nfs41_wake_and_assign_slot(tbl, slot))
 705                nfs4_free_slot(tbl, slot);
 706        spin_unlock(&tbl->slot_tbl_lock);
 707
 708        res->sr_slot = NULL;
 709}
 710
 711static int nfs40_sequence_done(struct rpc_task *task,
 712                               struct nfs4_sequence_res *res)
 713{
 714        if (res->sr_slot != NULL)
 715                nfs40_sequence_free_slot(res);
 716        return 1;
 717}
 718
 719#if defined(CONFIG_NFS_V4_1)
 720
 721static void nfs41_release_slot(struct nfs4_slot *slot)
 722{
 723        struct nfs4_session *session;
 724        struct nfs4_slot_table *tbl;
 725        bool send_new_highest_used_slotid = false;
 726
 727        if (!slot)
 728                return;
 729        tbl = slot->table;
 730        session = tbl->session;
 731
 732        /* Bump the slot sequence number */
 733        if (slot->seq_done)
 734                slot->seq_nr++;
 735        slot->seq_done = 0;
 736
 737        spin_lock(&tbl->slot_tbl_lock);
 738        /* Be nice to the server: try to ensure that the last transmitted
 739         * value for highest_user_slotid <= target_highest_slotid
 740         */
 741        if (tbl->highest_used_slotid > tbl->target_highest_slotid)
 742                send_new_highest_used_slotid = true;
 743
 744        if (nfs41_wake_and_assign_slot(tbl, slot)) {
 745                send_new_highest_used_slotid = false;
 746                goto out_unlock;
 747        }
 748        nfs4_free_slot(tbl, slot);
 749
 750        if (tbl->highest_used_slotid != NFS4_NO_SLOT)
 751                send_new_highest_used_slotid = false;
 752out_unlock:
 753        spin_unlock(&tbl->slot_tbl_lock);
 754        if (send_new_highest_used_slotid)
 755                nfs41_notify_server(session->clp);
 756        if (waitqueue_active(&tbl->slot_waitq))
 757                wake_up_all(&tbl->slot_waitq);
 758}
 759
 760static void nfs41_sequence_free_slot(struct nfs4_sequence_res *res)
 761{
 762        nfs41_release_slot(res->sr_slot);
 763        res->sr_slot = NULL;
 764}
 765
 766static void nfs4_slot_sequence_record_sent(struct nfs4_slot *slot,
 767                u32 seqnr)
 768{
 769        if ((s32)(seqnr - slot->seq_nr_highest_sent) > 0)
 770                slot->seq_nr_highest_sent = seqnr;
 771}
 772static void nfs4_slot_sequence_acked(struct nfs4_slot *slot,
 773                u32 seqnr)
 774{
 775        slot->seq_nr_highest_sent = seqnr;
 776        slot->seq_nr_last_acked = seqnr;
 777}
 778
 779static int nfs41_sequence_process(struct rpc_task *task,
 780                struct nfs4_sequence_res *res)
 781{
 782        struct nfs4_session *session;
 783        struct nfs4_slot *slot = res->sr_slot;
 784        struct nfs_client *clp;
 785        int ret = 1;
 786
 787        if (slot == NULL)
 788                goto out_noaction;
 789        /* don't increment the sequence number if the task wasn't sent */
 790        if (!RPC_WAS_SENT(task) || slot->seq_done)
 791                goto out;
 792
 793        session = slot->table->session;
 794
 795        trace_nfs4_sequence_done(session, res);
 796        /* Check the SEQUENCE operation status */
 797        switch (res->sr_status) {
 798        case 0:
 799                /* Mark this sequence number as having been acked */
 800                nfs4_slot_sequence_acked(slot, slot->seq_nr);
 801                /* Update the slot's sequence and clientid lease timer */
 802                slot->seq_done = 1;
 803                clp = session->clp;
 804                do_renew_lease(clp, res->sr_timestamp);
 805                /* Check sequence flags */
 806                nfs41_handle_sequence_flag_errors(clp, res->sr_status_flags,
 807                                !!slot->privileged);
 808                nfs41_update_target_slotid(slot->table, slot, res);
 809                break;
 810        case 1:
 811                /*
 812                 * sr_status remains 1 if an RPC level error occurred.
 813                 * The server may or may not have processed the sequence
 814                 * operation..
 815                 */
 816                nfs4_slot_sequence_record_sent(slot, slot->seq_nr);
 817                slot->seq_done = 1;
 818                goto out;
 819        case -NFS4ERR_DELAY:
 820                /* The server detected a resend of the RPC call and
 821                 * returned NFS4ERR_DELAY as per Section 2.10.6.2
 822                 * of RFC5661.
 823                 */
 824                dprintk("%s: slot=%u seq=%u: Operation in progress\n",
 825                        __func__,
 826                        slot->slot_nr,
 827                        slot->seq_nr);
 828                nfs4_slot_sequence_acked(slot, slot->seq_nr);
 829                goto out_retry;
 830        case -NFS4ERR_RETRY_UNCACHED_REP:
 831        case -NFS4ERR_SEQ_FALSE_RETRY:
 832                /*
 833                 * The server thinks we tried to replay a request.
 834                 * Retry the call after bumping the sequence ID.
 835                 */
 836                nfs4_slot_sequence_acked(slot, slot->seq_nr);
 837                goto retry_new_seq;
 838        case -NFS4ERR_BADSLOT:
 839                /*
 840                 * The slot id we used was probably retired. Try again
 841                 * using a different slot id.
 842                 */
 843                if (slot->slot_nr < slot->table->target_highest_slotid)
 844                        goto session_recover;
 845                goto retry_nowait;
 846        case -NFS4ERR_SEQ_MISORDERED:
 847                nfs4_slot_sequence_record_sent(slot, slot->seq_nr);
 848                /*
 849                 * Were one or more calls using this slot interrupted?
 850                 * If the server never received the request, then our
 851                 * transmitted slot sequence number may be too high.
 852                 */
 853                if ((s32)(slot->seq_nr - slot->seq_nr_last_acked) > 1) {
 854                        slot->seq_nr--;
 855                        goto retry_nowait;
 856                }
 857                /*
 858                 * RFC5661:
 859                 * A retry might be sent while the original request is
 860                 * still in progress on the replier. The replier SHOULD
 861                 * deal with the issue by returning NFS4ERR_DELAY as the
 862                 * reply to SEQUENCE or CB_SEQUENCE operation, but
 863                 * implementations MAY return NFS4ERR_SEQ_MISORDERED.
 864                 *
 865                 * Restart the search after a delay.
 866                 */
 867                slot->seq_nr = slot->seq_nr_highest_sent;
 868                goto out_retry;
 869        default:
 870                /* Just update the slot sequence no. */
 871                slot->seq_done = 1;
 872        }
 873out:
 874        /* The session may be reset by one of the error handlers. */
 875        dprintk("%s: Error %d free the slot \n", __func__, res->sr_status);
 876out_noaction:
 877        return ret;
 878session_recover:
 879        nfs4_schedule_session_recovery(session, res->sr_status);
 880        goto retry_nowait;
 881retry_new_seq:
 882        ++slot->seq_nr;
 883retry_nowait:
 884        if (rpc_restart_call_prepare(task)) {
 885                nfs41_sequence_free_slot(res);
 886                task->tk_status = 0;
 887                ret = 0;
 888        }
 889        goto out;
 890out_retry:
 891        if (!rpc_restart_call(task))
 892                goto out;
 893        rpc_delay(task, NFS4_POLL_RETRY_MAX);
 894        return 0;
 895}
 896
 897int nfs41_sequence_done(struct rpc_task *task, struct nfs4_sequence_res *res)
 898{
 899        if (!nfs41_sequence_process(task, res))
 900                return 0;
 901        if (res->sr_slot != NULL)
 902                nfs41_sequence_free_slot(res);
 903        return 1;
 904
 905}
 906EXPORT_SYMBOL_GPL(nfs41_sequence_done);
 907
 908static int nfs4_sequence_process(struct rpc_task *task, struct nfs4_sequence_res *res)
 909{
 910        if (res->sr_slot == NULL)
 911                return 1;
 912        if (res->sr_slot->table->session != NULL)
 913                return nfs41_sequence_process(task, res);
 914        return nfs40_sequence_done(task, res);
 915}
 916
 917static void nfs4_sequence_free_slot(struct nfs4_sequence_res *res)
 918{
 919        if (res->sr_slot != NULL) {
 920                if (res->sr_slot->table->session != NULL)
 921                        nfs41_sequence_free_slot(res);
 922                else
 923                        nfs40_sequence_free_slot(res);
 924        }
 925}
 926
 927int nfs4_sequence_done(struct rpc_task *task, struct nfs4_sequence_res *res)
 928{
 929        if (res->sr_slot == NULL)
 930                return 1;
 931        if (!res->sr_slot->table->session)
 932                return nfs40_sequence_done(task, res);
 933        return nfs41_sequence_done(task, res);
 934}
 935EXPORT_SYMBOL_GPL(nfs4_sequence_done);
 936
 937static void nfs41_call_sync_prepare(struct rpc_task *task, void *calldata)
 938{
 939        struct nfs4_call_sync_data *data = calldata;
 940
 941        dprintk("--> %s data->seq_server %p\n", __func__, data->seq_server);
 942
 943        nfs4_setup_sequence(data->seq_server->nfs_client,
 944                            data->seq_args, data->seq_res, task);
 945}
 946
 947static void nfs41_call_sync_done(struct rpc_task *task, void *calldata)
 948{
 949        struct nfs4_call_sync_data *data = calldata;
 950
 951        nfs41_sequence_done(task, data->seq_res);
 952}
 953
 954static const struct rpc_call_ops nfs41_call_sync_ops = {
 955        .rpc_call_prepare = nfs41_call_sync_prepare,
 956        .rpc_call_done = nfs41_call_sync_done,
 957};
 958
 959#else   /* !CONFIG_NFS_V4_1 */
 960
 961static int nfs4_sequence_process(struct rpc_task *task, struct nfs4_sequence_res *res)
 962{
 963        return nfs40_sequence_done(task, res);
 964}
 965
 966static void nfs4_sequence_free_slot(struct nfs4_sequence_res *res)
 967{
 968        if (res->sr_slot != NULL)
 969                nfs40_sequence_free_slot(res);
 970}
 971
 972int nfs4_sequence_done(struct rpc_task *task,
 973                       struct nfs4_sequence_res *res)
 974{
 975        return nfs40_sequence_done(task, res);
 976}
 977EXPORT_SYMBOL_GPL(nfs4_sequence_done);
 978
 979#endif  /* !CONFIG_NFS_V4_1 */
 980
 981static void nfs41_sequence_res_init(struct nfs4_sequence_res *res)
 982{
 983        res->sr_timestamp = jiffies;
 984        res->sr_status_flags = 0;
 985        res->sr_status = 1;
 986}
 987
 988static
 989void nfs4_sequence_attach_slot(struct nfs4_sequence_args *args,
 990                struct nfs4_sequence_res *res,
 991                struct nfs4_slot *slot)
 992{
 993        if (!slot)
 994                return;
 995        slot->privileged = args->sa_privileged ? 1 : 0;
 996        args->sa_slot = slot;
 997
 998        res->sr_slot = slot;
 999}
1000
1001int nfs4_setup_sequence(struct nfs_client *client,
1002                        struct nfs4_sequence_args *args,
1003                        struct nfs4_sequence_res *res,
1004                        struct rpc_task *task)
1005{
1006        struct nfs4_session *session = nfs4_get_session(client);
1007        struct nfs4_slot_table *tbl  = client->cl_slot_tbl;
1008        struct nfs4_slot *slot;
1009
1010        /* slot already allocated? */
1011        if (res->sr_slot != NULL)
1012                goto out_start;
1013
1014        if (session)
1015                tbl = &session->fc_slot_table;
1016
1017        spin_lock(&tbl->slot_tbl_lock);
1018        /* The state manager will wait until the slot table is empty */
1019        if (nfs4_slot_tbl_draining(tbl) && !args->sa_privileged)
1020                goto out_sleep;
1021
1022        slot = nfs4_alloc_slot(tbl);
1023        if (IS_ERR(slot)) {
1024                if (slot == ERR_PTR(-ENOMEM))
1025                        goto out_sleep_timeout;
1026                goto out_sleep;
1027        }
1028        spin_unlock(&tbl->slot_tbl_lock);
1029
1030        nfs4_sequence_attach_slot(args, res, slot);
1031
1032        trace_nfs4_setup_sequence(session, args);
1033out_start:
1034        nfs41_sequence_res_init(res);
1035        rpc_call_start(task);
1036        return 0;
1037out_sleep_timeout:
1038        /* Try again in 1/4 second */
1039        if (args->sa_privileged)
1040                rpc_sleep_on_priority_timeout(&tbl->slot_tbl_waitq, task,
1041                                jiffies + (HZ >> 2), RPC_PRIORITY_PRIVILEGED);
1042        else
1043                rpc_sleep_on_timeout(&tbl->slot_tbl_waitq, task,
1044                                NULL, jiffies + (HZ >> 2));
1045        spin_unlock(&tbl->slot_tbl_lock);
1046        return -EAGAIN;
1047out_sleep:
1048        if (args->sa_privileged)
1049                rpc_sleep_on_priority(&tbl->slot_tbl_waitq, task,
1050                                RPC_PRIORITY_PRIVILEGED);
1051        else
1052                rpc_sleep_on(&tbl->slot_tbl_waitq, task, NULL);
1053        spin_unlock(&tbl->slot_tbl_lock);
1054        return -EAGAIN;
1055}
1056EXPORT_SYMBOL_GPL(nfs4_setup_sequence);
1057
1058static void nfs40_call_sync_prepare(struct rpc_task *task, void *calldata)
1059{
1060        struct nfs4_call_sync_data *data = calldata;
1061        nfs4_setup_sequence(data->seq_server->nfs_client,
1062                                data->seq_args, data->seq_res, task);
1063}
1064
1065static void nfs40_call_sync_done(struct rpc_task *task, void *calldata)
1066{
1067        struct nfs4_call_sync_data *data = calldata;
1068        nfs4_sequence_done(task, data->seq_res);
1069}
1070
1071static const struct rpc_call_ops nfs40_call_sync_ops = {
1072        .rpc_call_prepare = nfs40_call_sync_prepare,
1073        .rpc_call_done = nfs40_call_sync_done,
1074};
1075
1076static int nfs4_call_sync_custom(struct rpc_task_setup *task_setup)
1077{
1078        int ret;
1079        struct rpc_task *task;
1080
1081        task = rpc_run_task(task_setup);
1082        if (IS_ERR(task))
1083                return PTR_ERR(task);
1084
1085        ret = task->tk_status;
1086        rpc_put_task(task);
1087        return ret;
1088}
1089
1090static int nfs4_call_sync_sequence(struct rpc_clnt *clnt,
1091                                   struct nfs_server *server,
1092                                   struct rpc_message *msg,
1093                                   struct nfs4_sequence_args *args,
1094                                   struct nfs4_sequence_res *res)
1095{
1096        struct nfs_client *clp = server->nfs_client;
1097        struct nfs4_call_sync_data data = {
1098                .seq_server = server,
1099                .seq_args = args,
1100                .seq_res = res,
1101        };
1102        struct rpc_task_setup task_setup = {
1103                .rpc_client = clnt,
1104                .rpc_message = msg,
1105                .callback_ops = clp->cl_mvops->call_sync_ops,
1106                .callback_data = &data
1107        };
1108
1109        return nfs4_call_sync_custom(&task_setup);
1110}
1111
1112int nfs4_call_sync(struct rpc_clnt *clnt,
1113                   struct nfs_server *server,
1114                   struct rpc_message *msg,
1115                   struct nfs4_sequence_args *args,
1116                   struct nfs4_sequence_res *res,
1117                   int cache_reply)
1118{
1119        nfs4_init_sequence(args, res, cache_reply, 0);
1120        return nfs4_call_sync_sequence(clnt, server, msg, args, res);
1121}
1122
1123static void
1124nfs4_inc_nlink_locked(struct inode *inode)
1125{
1126        NFS_I(inode)->cache_validity |= NFS_INO_INVALID_OTHER;
1127        inc_nlink(inode);
1128}
1129
1130static void
1131nfs4_dec_nlink_locked(struct inode *inode)
1132{
1133        NFS_I(inode)->cache_validity |= NFS_INO_INVALID_OTHER;
1134        drop_nlink(inode);
1135}
1136
1137static void
1138update_changeattr_locked(struct inode *dir, struct nfs4_change_info *cinfo,
1139                unsigned long timestamp, unsigned long cache_validity)
1140{
1141        struct nfs_inode *nfsi = NFS_I(dir);
1142
1143        nfsi->cache_validity |= NFS_INO_INVALID_CTIME
1144                | NFS_INO_INVALID_MTIME
1145                | NFS_INO_INVALID_DATA
1146                | cache_validity;
1147        if (cinfo->atomic && cinfo->before == inode_peek_iversion_raw(dir)) {
1148                nfsi->cache_validity &= ~NFS_INO_REVAL_PAGECACHE;
1149                nfsi->attrtimeo_timestamp = jiffies;
1150        } else {
1151                nfs_force_lookup_revalidate(dir);
1152                if (cinfo->before != inode_peek_iversion_raw(dir))
1153                        nfsi->cache_validity |= NFS_INO_INVALID_ACCESS |
1154                                NFS_INO_INVALID_ACL;
1155        }
1156        inode_set_iversion_raw(dir, cinfo->after);
1157        nfsi->read_cache_jiffies = timestamp;
1158        nfsi->attr_gencount = nfs_inc_attr_generation_counter();
1159        nfsi->cache_validity &= ~NFS_INO_INVALID_CHANGE;
1160        nfs_fscache_invalidate(dir);
1161}
1162
1163static void
1164update_changeattr(struct inode *dir, struct nfs4_change_info *cinfo,
1165                unsigned long timestamp, unsigned long cache_validity)
1166{
1167        spin_lock(&dir->i_lock);
1168        update_changeattr_locked(dir, cinfo, timestamp, cache_validity);
1169        spin_unlock(&dir->i_lock);
1170}
1171
1172struct nfs4_open_createattrs {
1173        struct nfs4_label *label;
1174        struct iattr *sattr;
1175        const __u32 verf[2];
1176};
1177
1178static bool nfs4_clear_cap_atomic_open_v1(struct nfs_server *server,
1179                int err, struct nfs4_exception *exception)
1180{
1181        if (err != -EINVAL)
1182                return false;
1183        if (!(server->caps & NFS_CAP_ATOMIC_OPEN_V1))
1184                return false;
1185        server->caps &= ~NFS_CAP_ATOMIC_OPEN_V1;
1186        exception->retry = 1;
1187        return true;
1188}
1189
1190static fmode_t _nfs4_ctx_to_accessmode(const struct nfs_open_context *ctx)
1191{
1192         return ctx->mode & (FMODE_READ|FMODE_WRITE|FMODE_EXEC);
1193}
1194
1195static fmode_t _nfs4_ctx_to_openmode(const struct nfs_open_context *ctx)
1196{
1197        fmode_t ret = ctx->mode & (FMODE_READ|FMODE_WRITE);
1198
1199        return (ctx->mode & FMODE_EXEC) ? FMODE_READ | ret : ret;
1200}
1201
1202static u32
1203nfs4_map_atomic_open_share(struct nfs_server *server,
1204                fmode_t fmode, int openflags)
1205{
1206        u32 res = 0;
1207
1208        switch (fmode & (FMODE_READ | FMODE_WRITE)) {
1209        case FMODE_READ:
1210                res = NFS4_SHARE_ACCESS_READ;
1211                break;
1212        case FMODE_WRITE:
1213                res = NFS4_SHARE_ACCESS_WRITE;
1214                break;
1215        case FMODE_READ|FMODE_WRITE:
1216                res = NFS4_SHARE_ACCESS_BOTH;
1217        }
1218        if (!(server->caps & NFS_CAP_ATOMIC_OPEN_V1))
1219                goto out;
1220        /* Want no delegation if we're using O_DIRECT */
1221        if (openflags & O_DIRECT)
1222                res |= NFS4_SHARE_WANT_NO_DELEG;
1223out:
1224        return res;
1225}
1226
1227static enum open_claim_type4
1228nfs4_map_atomic_open_claim(struct nfs_server *server,
1229                enum open_claim_type4 claim)
1230{
1231        if (server->caps & NFS_CAP_ATOMIC_OPEN_V1)
1232                return claim;
1233        switch (claim) {
1234        default:
1235                return claim;
1236        case NFS4_OPEN_CLAIM_FH:
1237                return NFS4_OPEN_CLAIM_NULL;
1238        case NFS4_OPEN_CLAIM_DELEG_CUR_FH:
1239                return NFS4_OPEN_CLAIM_DELEGATE_CUR;
1240        case NFS4_OPEN_CLAIM_DELEG_PREV_FH:
1241                return NFS4_OPEN_CLAIM_DELEGATE_PREV;
1242        }
1243}
1244
1245static void nfs4_init_opendata_res(struct nfs4_opendata *p)
1246{
1247        p->o_res.f_attr = &p->f_attr;
1248        p->o_res.f_label = p->f_label;
1249        p->o_res.seqid = p->o_arg.seqid;
1250        p->c_res.seqid = p->c_arg.seqid;
1251        p->o_res.server = p->o_arg.server;
1252        p->o_res.access_request = p->o_arg.access;
1253        nfs_fattr_init(&p->f_attr);
1254        nfs_fattr_init_names(&p->f_attr, &p->owner_name, &p->group_name);
1255}
1256
1257static struct nfs4_opendata *nfs4_opendata_alloc(struct dentry *dentry,
1258                struct nfs4_state_owner *sp, fmode_t fmode, int flags,
1259                const struct nfs4_open_createattrs *c,
1260                enum open_claim_type4 claim,
1261                gfp_t gfp_mask)
1262{
1263        struct dentry *parent = dget_parent(dentry);
1264        struct inode *dir = d_inode(parent);
1265        struct nfs_server *server = NFS_SERVER(dir);
1266        struct nfs_seqid *(*alloc_seqid)(struct nfs_seqid_counter *, gfp_t);
1267        struct nfs4_label *label = (c != NULL) ? c->label : NULL;
1268        struct nfs4_opendata *p;
1269
1270        p = kzalloc(sizeof(*p), gfp_mask);
1271        if (p == NULL)
1272                goto err;
1273
1274        p->f_label = nfs4_label_alloc(server, gfp_mask);
1275        if (IS_ERR(p->f_label))
1276                goto err_free_p;
1277
1278        p->a_label = nfs4_label_alloc(server, gfp_mask);
1279        if (IS_ERR(p->a_label))
1280                goto err_free_f;
1281
1282        alloc_seqid = server->nfs_client->cl_mvops->alloc_seqid;
1283        p->o_arg.seqid = alloc_seqid(&sp->so_seqid, gfp_mask);
1284        if (IS_ERR(p->o_arg.seqid))
1285                goto err_free_label;
1286        nfs_sb_active(dentry->d_sb);
1287        p->dentry = dget(dentry);
1288        p->dir = parent;
1289        p->owner = sp;
1290        atomic_inc(&sp->so_count);
1291        p->o_arg.open_flags = flags;
1292        p->o_arg.fmode = fmode & (FMODE_READ|FMODE_WRITE);
1293        p->o_arg.claim = nfs4_map_atomic_open_claim(server, claim);
1294        p->o_arg.share_access = nfs4_map_atomic_open_share(server,
1295                        fmode, flags);
1296        if (flags & O_CREAT) {
1297                p->o_arg.umask = current_umask();
1298                p->o_arg.label = nfs4_label_copy(p->a_label, label);
1299                if (c->sattr != NULL && c->sattr->ia_valid != 0) {
1300                        p->o_arg.u.attrs = &p->attrs;
1301                        memcpy(&p->attrs, c->sattr, sizeof(p->attrs));
1302
1303                        memcpy(p->o_arg.u.verifier.data, c->verf,
1304                                        sizeof(p->o_arg.u.verifier.data));
1305                }
1306        }
1307        /* don't put an ACCESS op in OPEN compound if O_EXCL, because ACCESS
1308         * will return permission denied for all bits until close */
1309        if (!(flags & O_EXCL)) {
1310                /* ask server to check for all possible rights as results
1311                 * are cached */
1312                switch (p->o_arg.claim) {
1313                default:
1314                        break;
1315                case NFS4_OPEN_CLAIM_NULL:
1316                case NFS4_OPEN_CLAIM_FH:
1317                        p->o_arg.access = NFS4_ACCESS_READ |
1318                                NFS4_ACCESS_MODIFY |
1319                                NFS4_ACCESS_EXTEND |
1320                                NFS4_ACCESS_EXECUTE;
1321                }
1322        }
1323        p->o_arg.clientid = server->nfs_client->cl_clientid;
1324        p->o_arg.id.create_time = ktime_to_ns(sp->so_seqid.create_time);
1325        p->o_arg.id.uniquifier = sp->so_seqid.owner_id;
1326        p->o_arg.name = &dentry->d_name;
1327        p->o_arg.server = server;
1328        p->o_arg.bitmask = nfs4_bitmask(server, label);
1329        p->o_arg.open_bitmap = &nfs4_fattr_bitmap[0];
1330        switch (p->o_arg.claim) {
1331        case NFS4_OPEN_CLAIM_NULL:
1332        case NFS4_OPEN_CLAIM_DELEGATE_CUR:
1333        case NFS4_OPEN_CLAIM_DELEGATE_PREV:
1334                p->o_arg.fh = NFS_FH(dir);
1335                break;
1336        case NFS4_OPEN_CLAIM_PREVIOUS:
1337        case NFS4_OPEN_CLAIM_FH:
1338        case NFS4_OPEN_CLAIM_DELEG_CUR_FH:
1339        case NFS4_OPEN_CLAIM_DELEG_PREV_FH:
1340                p->o_arg.fh = NFS_FH(d_inode(dentry));
1341        }
1342        p->c_arg.fh = &p->o_res.fh;
1343        p->c_arg.stateid = &p->o_res.stateid;
1344        p->c_arg.seqid = p->o_arg.seqid;
1345        nfs4_init_opendata_res(p);
1346        kref_init(&p->kref);
1347        return p;
1348
1349err_free_label:
1350        nfs4_label_free(p->a_label);
1351err_free_f:
1352        nfs4_label_free(p->f_label);
1353err_free_p:
1354        kfree(p);
1355err:
1356        dput(parent);
1357        return NULL;
1358}
1359
1360static void nfs4_opendata_free(struct kref *kref)
1361{
1362        struct nfs4_opendata *p = container_of(kref,
1363                        struct nfs4_opendata, kref);
1364        struct super_block *sb = p->dentry->d_sb;
1365
1366        nfs4_lgopen_release(p->lgp);
1367        nfs_free_seqid(p->o_arg.seqid);
1368        nfs4_sequence_free_slot(&p->o_res.seq_res);
1369        if (p->state != NULL)
1370                nfs4_put_open_state(p->state);
1371        nfs4_put_state_owner(p->owner);
1372
1373        nfs4_label_free(p->a_label);
1374        nfs4_label_free(p->f_label);
1375
1376        dput(p->dir);
1377        dput(p->dentry);
1378        nfs_sb_deactive(sb);
1379        nfs_fattr_free_names(&p->f_attr);
1380        kfree(p->f_attr.mdsthreshold);
1381        kfree(p);
1382}
1383
1384static void nfs4_opendata_put(struct nfs4_opendata *p)
1385{
1386        if (p != NULL)
1387                kref_put(&p->kref, nfs4_opendata_free);
1388}
1389
1390static bool nfs4_mode_match_open_stateid(struct nfs4_state *state,
1391                fmode_t fmode)
1392{
1393        switch(fmode & (FMODE_READ|FMODE_WRITE)) {
1394        case FMODE_READ|FMODE_WRITE:
1395                return state->n_rdwr != 0;
1396        case FMODE_WRITE:
1397                return state->n_wronly != 0;
1398        case FMODE_READ:
1399                return state->n_rdonly != 0;
1400        }
1401        WARN_ON_ONCE(1);
1402        return false;
1403}
1404
1405static int can_open_cached(struct nfs4_state *state, fmode_t mode,
1406                int open_mode, enum open_claim_type4 claim)
1407{
1408        int ret = 0;
1409
1410        if (open_mode & (O_EXCL|O_TRUNC))
1411                goto out;
1412        switch (claim) {
1413        case NFS4_OPEN_CLAIM_NULL:
1414        case NFS4_OPEN_CLAIM_FH:
1415                goto out;
1416        default:
1417                break;
1418        }
1419        switch (mode & (FMODE_READ|FMODE_WRITE)) {
1420                case FMODE_READ:
1421                        ret |= test_bit(NFS_O_RDONLY_STATE, &state->flags) != 0
1422                                && state->n_rdonly != 0;
1423                        break;
1424                case FMODE_WRITE:
1425                        ret |= test_bit(NFS_O_WRONLY_STATE, &state->flags) != 0
1426                                && state->n_wronly != 0;
1427                        break;
1428                case FMODE_READ|FMODE_WRITE:
1429                        ret |= test_bit(NFS_O_RDWR_STATE, &state->flags) != 0
1430                                && state->n_rdwr != 0;
1431        }
1432out:
1433        return ret;
1434}
1435
1436static int can_open_delegated(struct nfs_delegation *delegation, fmode_t fmode,
1437                enum open_claim_type4 claim)
1438{
1439        if (delegation == NULL)
1440                return 0;
1441        if ((delegation->type & fmode) != fmode)
1442                return 0;
1443        switch (claim) {
1444        case NFS4_OPEN_CLAIM_NULL:
1445        case NFS4_OPEN_CLAIM_FH:
1446                break;
1447        case NFS4_OPEN_CLAIM_PREVIOUS:
1448                if (!test_bit(NFS_DELEGATION_NEED_RECLAIM, &delegation->flags))
1449                        break;
1450                /* Fall through */
1451        default:
1452                return 0;
1453        }
1454        nfs_mark_delegation_referenced(delegation);
1455        return 1;
1456}
1457
1458static void update_open_stateflags(struct nfs4_state *state, fmode_t fmode)
1459{
1460        switch (fmode) {
1461                case FMODE_WRITE:
1462                        state->n_wronly++;
1463                        break;
1464                case FMODE_READ:
1465                        state->n_rdonly++;
1466                        break;
1467                case FMODE_READ|FMODE_WRITE:
1468                        state->n_rdwr++;
1469        }
1470        nfs4_state_set_mode_locked(state, state->state | fmode);
1471}
1472
1473#ifdef CONFIG_NFS_V4_1
1474static bool nfs_open_stateid_recover_openmode(struct nfs4_state *state)
1475{
1476        if (state->n_rdonly && !test_bit(NFS_O_RDONLY_STATE, &state->flags))
1477                return true;
1478        if (state->n_wronly && !test_bit(NFS_O_WRONLY_STATE, &state->flags))
1479                return true;
1480        if (state->n_rdwr && !test_bit(NFS_O_RDWR_STATE, &state->flags))
1481                return true;
1482        return false;
1483}
1484#endif /* CONFIG_NFS_V4_1 */
1485
1486static void nfs_state_log_update_open_stateid(struct nfs4_state *state)
1487{
1488        if (test_and_clear_bit(NFS_STATE_CHANGE_WAIT, &state->flags))
1489                wake_up_all(&state->waitq);
1490}
1491
1492static void nfs_state_log_out_of_order_open_stateid(struct nfs4_state *state,
1493                const nfs4_stateid *stateid)
1494{
1495        u32 state_seqid = be32_to_cpu(state->open_stateid.seqid);
1496        u32 stateid_seqid = be32_to_cpu(stateid->seqid);
1497
1498        if (stateid_seqid == state_seqid + 1U ||
1499            (stateid_seqid == 1U && state_seqid == 0xffffffffU))
1500                nfs_state_log_update_open_stateid(state);
1501        else
1502                set_bit(NFS_STATE_CHANGE_WAIT, &state->flags);
1503}
1504
1505static void nfs_test_and_clear_all_open_stateid(struct nfs4_state *state)
1506{
1507        struct nfs_client *clp = state->owner->so_server->nfs_client;
1508        bool need_recover = false;
1509
1510        if (test_and_clear_bit(NFS_O_RDONLY_STATE, &state->flags) && state->n_rdonly)
1511                need_recover = true;
1512        if (test_and_clear_bit(NFS_O_WRONLY_STATE, &state->flags) && state->n_wronly)
1513                need_recover = true;
1514        if (test_and_clear_bit(NFS_O_RDWR_STATE, &state->flags) && state->n_rdwr)
1515                need_recover = true;
1516        if (need_recover)
1517                nfs4_state_mark_reclaim_nograce(clp, state);
1518}
1519
1520/*
1521 * Check for whether or not the caller may update the open stateid
1522 * to the value passed in by stateid.
1523 *
1524 * Note: This function relies heavily on the server implementing
1525 * RFC7530 Section 9.1.4.2, and RFC5661 Section 8.2.2
1526 * correctly.
1527 * i.e. The stateid seqids have to be initialised to 1, and
1528 * are then incremented on every state transition.
1529 */
1530static bool nfs_need_update_open_stateid(struct nfs4_state *state,
1531                const nfs4_stateid *stateid)
1532{
1533        if (test_bit(NFS_OPEN_STATE, &state->flags) == 0 ||
1534            !nfs4_stateid_match_other(stateid, &state->open_stateid)) {
1535                if (stateid->seqid == cpu_to_be32(1))
1536                        nfs_state_log_update_open_stateid(state);
1537                else
1538                        set_bit(NFS_STATE_CHANGE_WAIT, &state->flags);
1539                return true;
1540        }
1541
1542        if (nfs4_stateid_is_newer(stateid, &state->open_stateid)) {
1543                nfs_state_log_out_of_order_open_stateid(state, stateid);
1544                return true;
1545        }
1546        return false;
1547}
1548
1549static void nfs_resync_open_stateid_locked(struct nfs4_state *state)
1550{
1551        if (!(state->n_wronly || state->n_rdonly || state->n_rdwr))
1552                return;
1553        if (state->n_wronly)
1554                set_bit(NFS_O_WRONLY_STATE, &state->flags);
1555        if (state->n_rdonly)
1556                set_bit(NFS_O_RDONLY_STATE, &state->flags);
1557        if (state->n_rdwr)
1558                set_bit(NFS_O_RDWR_STATE, &state->flags);
1559        set_bit(NFS_OPEN_STATE, &state->flags);
1560}
1561
1562static void nfs_clear_open_stateid_locked(struct nfs4_state *state,
1563                nfs4_stateid *stateid, fmode_t fmode)
1564{
1565        clear_bit(NFS_O_RDWR_STATE, &state->flags);
1566        switch (fmode & (FMODE_READ|FMODE_WRITE)) {
1567        case FMODE_WRITE:
1568                clear_bit(NFS_O_RDONLY_STATE, &state->flags);
1569                break;
1570        case FMODE_READ:
1571                clear_bit(NFS_O_WRONLY_STATE, &state->flags);
1572                break;
1573        case 0:
1574                clear_bit(NFS_O_RDONLY_STATE, &state->flags);
1575                clear_bit(NFS_O_WRONLY_STATE, &state->flags);
1576                clear_bit(NFS_OPEN_STATE, &state->flags);
1577        }
1578        if (stateid == NULL)
1579                return;
1580        /* Handle OPEN+OPEN_DOWNGRADE races */
1581        if (nfs4_stateid_match_other(stateid, &state->open_stateid) &&
1582            !nfs4_stateid_is_newer(stateid, &state->open_stateid)) {
1583                nfs_resync_open_stateid_locked(state);
1584                goto out;
1585        }
1586        if (test_bit(NFS_DELEGATED_STATE, &state->flags) == 0)
1587                nfs4_stateid_copy(&state->stateid, stateid);
1588        nfs4_stateid_copy(&state->open_stateid, stateid);
1589        trace_nfs4_open_stateid_update(state->inode, stateid, 0);
1590out:
1591        nfs_state_log_update_open_stateid(state);
1592}
1593
1594static void nfs_clear_open_stateid(struct nfs4_state *state,
1595        nfs4_stateid *arg_stateid,
1596        nfs4_stateid *stateid, fmode_t fmode)
1597{
1598        write_seqlock(&state->seqlock);
1599        /* Ignore, if the CLOSE argment doesn't match the current stateid */
1600        if (nfs4_state_match_open_stateid_other(state, arg_stateid))
1601                nfs_clear_open_stateid_locked(state, stateid, fmode);
1602        write_sequnlock(&state->seqlock);
1603        if (test_bit(NFS_STATE_RECLAIM_NOGRACE, &state->flags))
1604                nfs4_schedule_state_manager(state->owner->so_server->nfs_client);
1605}
1606
1607static void nfs_set_open_stateid_locked(struct nfs4_state *state,
1608                const nfs4_stateid *stateid, nfs4_stateid *freeme)
1609        __must_hold(&state->owner->so_lock)
1610        __must_hold(&state->seqlock)
1611        __must_hold(RCU)
1612
1613{
1614        DEFINE_WAIT(wait);
1615        int status = 0;
1616        for (;;) {
1617
1618                if (!nfs_need_update_open_stateid(state, stateid))
1619                        return;
1620                if (!test_bit(NFS_STATE_CHANGE_WAIT, &state->flags))
1621                        break;
1622                if (status)
1623                        break;
1624                /* Rely on seqids for serialisation with NFSv4.0 */
1625                if (!nfs4_has_session(NFS_SERVER(state->inode)->nfs_client))
1626                        break;
1627
1628                prepare_to_wait(&state->waitq, &wait, TASK_KILLABLE);
1629                /*
1630                 * Ensure we process the state changes in the same order
1631                 * in which the server processed them by delaying the
1632                 * update of the stateid until we are in sequence.
1633                 */
1634                write_sequnlock(&state->seqlock);
1635                spin_unlock(&state->owner->so_lock);
1636                rcu_read_unlock();
1637                trace_nfs4_open_stateid_update_wait(state->inode, stateid, 0);
1638                if (!signal_pending(current)) {
1639                        if (schedule_timeout(5*HZ) == 0)
1640                                status = -EAGAIN;
1641                        else
1642                                status = 0;
1643                } else
1644                        status = -EINTR;
1645                finish_wait(&state->waitq, &wait);
1646                rcu_read_lock();
1647                spin_lock(&state->owner->so_lock);
1648                write_seqlock(&state->seqlock);
1649        }
1650
1651        if (test_bit(NFS_OPEN_STATE, &state->flags) &&
1652            !nfs4_stateid_match_other(stateid, &state->open_stateid)) {
1653                nfs4_stateid_copy(freeme, &state->open_stateid);
1654                nfs_test_and_clear_all_open_stateid(state);
1655        }
1656
1657        if (test_bit(NFS_DELEGATED_STATE, &state->flags) == 0)
1658                nfs4_stateid_copy(&state->stateid, stateid);
1659        nfs4_stateid_copy(&state->open_stateid, stateid);
1660        trace_nfs4_open_stateid_update(state->inode, stateid, status);
1661        nfs_state_log_update_open_stateid(state);
1662}
1663
1664static void nfs_state_set_open_stateid(struct nfs4_state *state,
1665                const nfs4_stateid *open_stateid,
1666                fmode_t fmode,
1667                nfs4_stateid *freeme)
1668{
1669        /*
1670         * Protect the call to nfs4_state_set_mode_locked and
1671         * serialise the stateid update
1672         */
1673        write_seqlock(&state->seqlock);
1674        nfs_set_open_stateid_locked(state, open_stateid, freeme);
1675        switch (fmode) {
1676        case FMODE_READ:
1677                set_bit(NFS_O_RDONLY_STATE, &state->flags);
1678                break;
1679        case FMODE_WRITE:
1680                set_bit(NFS_O_WRONLY_STATE, &state->flags);
1681                break;
1682        case FMODE_READ|FMODE_WRITE:
1683                set_bit(NFS_O_RDWR_STATE, &state->flags);
1684        }
1685        set_bit(NFS_OPEN_STATE, &state->flags);
1686        write_sequnlock(&state->seqlock);
1687}
1688
1689static void nfs_state_clear_open_state_flags(struct nfs4_state *state)
1690{
1691        clear_bit(NFS_O_RDWR_STATE, &state->flags);
1692        clear_bit(NFS_O_WRONLY_STATE, &state->flags);
1693        clear_bit(NFS_O_RDONLY_STATE, &state->flags);
1694        clear_bit(NFS_OPEN_STATE, &state->flags);
1695}
1696
1697static void nfs_state_set_delegation(struct nfs4_state *state,
1698                const nfs4_stateid *deleg_stateid,
1699                fmode_t fmode)
1700{
1701        /*
1702         * Protect the call to nfs4_state_set_mode_locked and
1703         * serialise the stateid update
1704         */
1705        write_seqlock(&state->seqlock);
1706        nfs4_stateid_copy(&state->stateid, deleg_stateid);
1707        set_bit(NFS_DELEGATED_STATE, &state->flags);
1708        write_sequnlock(&state->seqlock);
1709}
1710
1711static void nfs_state_clear_delegation(struct nfs4_state *state)
1712{
1713        write_seqlock(&state->seqlock);
1714        nfs4_stateid_copy(&state->stateid, &state->open_stateid);
1715        clear_bit(NFS_DELEGATED_STATE, &state->flags);
1716        write_sequnlock(&state->seqlock);
1717}
1718
1719static int update_open_stateid(struct nfs4_state *state,
1720                const nfs4_stateid *open_stateid,
1721                const nfs4_stateid *delegation,
1722                fmode_t fmode)
1723{
1724        struct nfs_server *server = NFS_SERVER(state->inode);
1725        struct nfs_client *clp = server->nfs_client;
1726        struct nfs_inode *nfsi = NFS_I(state->inode);
1727        struct nfs_delegation *deleg_cur;
1728        nfs4_stateid freeme = { };
1729        int ret = 0;
1730
1731        fmode &= (FMODE_READ|FMODE_WRITE);
1732
1733        rcu_read_lock();
1734        spin_lock(&state->owner->so_lock);
1735        if (open_stateid != NULL) {
1736                nfs_state_set_open_stateid(state, open_stateid, fmode, &freeme);
1737                ret = 1;
1738        }
1739
1740        deleg_cur = rcu_dereference(nfsi->delegation);
1741        if (deleg_cur == NULL)
1742                goto no_delegation;
1743
1744        spin_lock(&deleg_cur->lock);
1745        if (rcu_dereference(nfsi->delegation) != deleg_cur ||
1746           test_bit(NFS_DELEGATION_RETURNING, &deleg_cur->flags) ||
1747            (deleg_cur->type & fmode) != fmode)
1748                goto no_delegation_unlock;
1749
1750        if (delegation == NULL)
1751                delegation = &deleg_cur->stateid;
1752        else if (!nfs4_stateid_match(&deleg_cur->stateid, delegation))
1753                goto no_delegation_unlock;
1754
1755        nfs_mark_delegation_referenced(deleg_cur);
1756        nfs_state_set_delegation(state, &deleg_cur->stateid, fmode);
1757        ret = 1;
1758no_delegation_unlock:
1759        spin_unlock(&deleg_cur->lock);
1760no_delegation:
1761        if (ret)
1762                update_open_stateflags(state, fmode);
1763        spin_unlock(&state->owner->so_lock);
1764        rcu_read_unlock();
1765
1766        if (test_bit(NFS_STATE_RECLAIM_NOGRACE, &state->flags))
1767                nfs4_schedule_state_manager(clp);
1768        if (freeme.type != 0)
1769                nfs4_test_and_free_stateid(server, &freeme,
1770                                state->owner->so_cred);
1771
1772        return ret;
1773}
1774
1775static bool nfs4_update_lock_stateid(struct nfs4_lock_state *lsp,
1776                const nfs4_stateid *stateid)
1777{
1778        struct nfs4_state *state = lsp->ls_state;
1779        bool ret = false;
1780
1781        spin_lock(&state->state_lock);
1782        if (!nfs4_stateid_match_other(stateid, &lsp->ls_stateid))
1783                goto out_noupdate;
1784        if (!nfs4_stateid_is_newer(stateid, &lsp->ls_stateid))
1785                goto out_noupdate;
1786        nfs4_stateid_copy(&lsp->ls_stateid, stateid);
1787        ret = true;
1788out_noupdate:
1789        spin_unlock(&state->state_lock);
1790        return ret;
1791}
1792
1793static void nfs4_return_incompatible_delegation(struct inode *inode, fmode_t fmode)
1794{
1795        struct nfs_delegation *delegation;
1796
1797        fmode &= FMODE_READ|FMODE_WRITE;
1798        rcu_read_lock();
1799        delegation = rcu_dereference(NFS_I(inode)->delegation);
1800        if (delegation == NULL || (delegation->type & fmode) == fmode) {
1801                rcu_read_unlock();
1802                return;
1803        }
1804        rcu_read_unlock();
1805        nfs4_inode_return_delegation(inode);
1806}
1807
1808static struct nfs4_state *nfs4_try_open_cached(struct nfs4_opendata *opendata)
1809{
1810        struct nfs4_state *state = opendata->state;
1811        struct nfs_delegation *delegation;
1812        int open_mode = opendata->o_arg.open_flags;
1813        fmode_t fmode = opendata->o_arg.fmode;
1814        enum open_claim_type4 claim = opendata->o_arg.claim;
1815        nfs4_stateid stateid;
1816        int ret = -EAGAIN;
1817
1818        for (;;) {
1819                spin_lock(&state->owner->so_lock);
1820                if (can_open_cached(state, fmode, open_mode, claim)) {
1821                        update_open_stateflags(state, fmode);
1822                        spin_unlock(&state->owner->so_lock);
1823                        goto out_return_state;
1824                }
1825                spin_unlock(&state->owner->so_lock);
1826                rcu_read_lock();
1827                delegation = nfs4_get_valid_delegation(state->inode);
1828                if (!can_open_delegated(delegation, fmode, claim)) {
1829                        rcu_read_unlock();
1830                        break;
1831                }
1832                /* Save the delegation */
1833                nfs4_stateid_copy(&stateid, &delegation->stateid);
1834                rcu_read_unlock();
1835                nfs_release_seqid(opendata->o_arg.seqid);
1836                if (!opendata->is_recover) {
1837                        ret = nfs_may_open(state->inode, state->owner->so_cred, open_mode);
1838                        if (ret != 0)
1839                                goto out;
1840                }
1841                ret = -EAGAIN;
1842
1843                /* Try to update the stateid using the delegation */
1844                if (update_open_stateid(state, NULL, &stateid, fmode))
1845                        goto out_return_state;
1846        }
1847out:
1848        return ERR_PTR(ret);
1849out_return_state:
1850        refcount_inc(&state->count);
1851        return state;
1852}
1853
1854static void
1855nfs4_opendata_check_deleg(struct nfs4_opendata *data, struct nfs4_state *state)
1856{
1857        struct nfs_client *clp = NFS_SERVER(state->inode)->nfs_client;
1858        struct nfs_delegation *delegation;
1859        int delegation_flags = 0;
1860
1861        rcu_read_lock();
1862        delegation = rcu_dereference(NFS_I(state->inode)->delegation);
1863        if (delegation)
1864                delegation_flags = delegation->flags;
1865        rcu_read_unlock();
1866        switch (data->o_arg.claim) {
1867        default:
1868                break;
1869        case NFS4_OPEN_CLAIM_DELEGATE_CUR:
1870        case NFS4_OPEN_CLAIM_DELEG_CUR_FH:
1871                pr_err_ratelimited("NFS: Broken NFSv4 server %s is "
1872                                   "returning a delegation for "
1873                                   "OPEN(CLAIM_DELEGATE_CUR)\n",
1874                                   clp->cl_hostname);
1875                return;
1876        }
1877        if ((delegation_flags & 1UL<<NFS_DELEGATION_NEED_RECLAIM) == 0)
1878                nfs_inode_set_delegation(state->inode,
1879                                data->owner->so_cred,
1880                                data->o_res.delegation_type,
1881                                &data->o_res.delegation,
1882                                data->o_res.pagemod_limit);
1883        else
1884                nfs_inode_reclaim_delegation(state->inode,
1885                                data->owner->so_cred,
1886                                data->o_res.delegation_type,
1887                                &data->o_res.delegation,
1888                                data->o_res.pagemod_limit);
1889
1890        if (data->o_res.do_recall)
1891                nfs_async_inode_return_delegation(state->inode,
1892                                                  &data->o_res.delegation);
1893}
1894
1895/*
1896 * Check the inode attributes against the CLAIM_PREVIOUS returned attributes
1897 * and update the nfs4_state.
1898 */
1899static struct nfs4_state *
1900_nfs4_opendata_reclaim_to_nfs4_state(struct nfs4_opendata *data)
1901{
1902        struct inode *inode = data->state->inode;
1903        struct nfs4_state *state = data->state;
1904        int ret;
1905
1906        if (!data->rpc_done) {
1907                if (data->rpc_status)
1908                        return ERR_PTR(data->rpc_status);
1909                /* cached opens have already been processed */
1910                goto update;
1911        }
1912
1913        ret = nfs_refresh_inode(inode, &data->f_attr);
1914        if (ret)
1915                return ERR_PTR(ret);
1916
1917        if (data->o_res.delegation_type != 0)
1918                nfs4_opendata_check_deleg(data, state);
1919update:
1920        if (!update_open_stateid(state, &data->o_res.stateid,
1921                                NULL, data->o_arg.fmode))
1922                return ERR_PTR(-EAGAIN);
1923        refcount_inc(&state->count);
1924
1925        return state;
1926}
1927
1928static struct inode *
1929nfs4_opendata_get_inode(struct nfs4_opendata *data)
1930{
1931        struct inode *inode;
1932
1933        switch (data->o_arg.claim) {
1934        case NFS4_OPEN_CLAIM_NULL:
1935        case NFS4_OPEN_CLAIM_DELEGATE_CUR:
1936        case NFS4_OPEN_CLAIM_DELEGATE_PREV:
1937                if (!(data->f_attr.valid & NFS_ATTR_FATTR))
1938                        return ERR_PTR(-EAGAIN);
1939                inode = nfs_fhget(data->dir->d_sb, &data->o_res.fh,
1940                                &data->f_attr, data->f_label);
1941                break;
1942        default:
1943                inode = d_inode(data->dentry);
1944                ihold(inode);
1945                nfs_refresh_inode(inode, &data->f_attr);
1946        }
1947        return inode;
1948}
1949
1950static struct nfs4_state *
1951nfs4_opendata_find_nfs4_state(struct nfs4_opendata *data)
1952{
1953        struct nfs4_state *state;
1954        struct inode *inode;
1955
1956        inode = nfs4_opendata_get_inode(data);
1957        if (IS_ERR(inode))
1958                return ERR_CAST(inode);
1959        if (data->state != NULL && data->state->inode == inode) {
1960                state = data->state;
1961                refcount_inc(&state->count);
1962        } else
1963                state = nfs4_get_open_state(inode, data->owner);
1964        iput(inode);
1965        if (state == NULL)
1966                state = ERR_PTR(-ENOMEM);
1967        return state;
1968}
1969
1970static struct nfs4_state *
1971_nfs4_opendata_to_nfs4_state(struct nfs4_opendata *data)
1972{
1973        struct nfs4_state *state;
1974
1975        if (!data->rpc_done) {
1976                state = nfs4_try_open_cached(data);
1977                trace_nfs4_cached_open(data->state);
1978                goto out;
1979        }
1980
1981        state = nfs4_opendata_find_nfs4_state(data);
1982        if (IS_ERR(state))
1983                goto out;
1984
1985        if (data->o_res.delegation_type != 0)
1986                nfs4_opendata_check_deleg(data, state);
1987        if (!update_open_stateid(state, &data->o_res.stateid,
1988                                NULL, data->o_arg.fmode)) {
1989                nfs4_put_open_state(state);
1990                state = ERR_PTR(-EAGAIN);
1991        }
1992out:
1993        nfs_release_seqid(data->o_arg.seqid);
1994        return state;
1995}
1996
1997static struct nfs4_state *
1998nfs4_opendata_to_nfs4_state(struct nfs4_opendata *data)
1999{
2000        struct nfs4_state *ret;
2001
2002        if (data->o_arg.claim == NFS4_OPEN_CLAIM_PREVIOUS)
2003                ret =_nfs4_opendata_reclaim_to_nfs4_state(data);
2004        else
2005                ret = _nfs4_opendata_to_nfs4_state(data);
2006        nfs4_sequence_free_slot(&data->o_res.seq_res);
2007        return ret;
2008}
2009
2010static struct nfs_open_context *
2011nfs4_state_find_open_context_mode(struct nfs4_state *state, fmode_t mode)
2012{
2013        struct nfs_inode *nfsi = NFS_I(state->inode);
2014        struct nfs_open_context *ctx;
2015
2016        rcu_read_lock();
2017        list_for_each_entry_rcu(ctx, &nfsi->open_files, list) {
2018                if (ctx->state != state)
2019                        continue;
2020                if ((ctx->mode & mode) != mode)
2021                        continue;
2022                if (!get_nfs_open_context(ctx))
2023                        continue;
2024                rcu_read_unlock();
2025                return ctx;
2026        }
2027        rcu_read_unlock();
2028        return ERR_PTR(-ENOENT);
2029}
2030
2031static struct nfs_open_context *
2032nfs4_state_find_open_context(struct nfs4_state *state)
2033{
2034        struct nfs_open_context *ctx;
2035
2036        ctx = nfs4_state_find_open_context_mode(state, FMODE_READ|FMODE_WRITE);
2037        if (!IS_ERR(ctx))
2038                return ctx;
2039        ctx = nfs4_state_find_open_context_mode(state, FMODE_WRITE);
2040        if (!IS_ERR(ctx))
2041                return ctx;
2042        return nfs4_state_find_open_context_mode(state, FMODE_READ);
2043}
2044
2045static struct nfs4_opendata *nfs4_open_recoverdata_alloc(struct nfs_open_context *ctx,
2046                struct nfs4_state *state, enum open_claim_type4 claim)
2047{
2048        struct nfs4_opendata *opendata;
2049
2050        opendata = nfs4_opendata_alloc(ctx->dentry, state->owner, 0, 0,
2051                        NULL, claim, GFP_NOFS);
2052        if (opendata == NULL)
2053                return ERR_PTR(-ENOMEM);
2054        opendata->state = state;
2055        refcount_inc(&state->count);
2056        return opendata;
2057}
2058
2059static int nfs4_open_recover_helper(struct nfs4_opendata *opendata,
2060                fmode_t fmode)
2061{
2062        struct nfs4_state *newstate;
2063        int ret;
2064
2065        if (!nfs4_mode_match_open_stateid(opendata->state, fmode))
2066                return 0;
2067        opendata->o_arg.open_flags = 0;
2068        opendata->o_arg.fmode = fmode;
2069        opendata->o_arg.share_access = nfs4_map_atomic_open_share(
2070                        NFS_SB(opendata->dentry->d_sb),
2071                        fmode, 0);
2072        memset(&opendata->o_res, 0, sizeof(opendata->o_res));
2073        memset(&opendata->c_res, 0, sizeof(opendata->c_res));
2074        nfs4_init_opendata_res(opendata);
2075        ret = _nfs4_recover_proc_open(opendata);
2076        if (ret != 0)
2077                return ret; 
2078        newstate = nfs4_opendata_to_nfs4_state(opendata);
2079        if (IS_ERR(newstate))
2080                return PTR_ERR(newstate);
2081        if (newstate != opendata->state)
2082                ret = -ESTALE;
2083        nfs4_close_state(newstate, fmode);
2084        return ret;
2085}
2086
2087static int nfs4_open_recover(struct nfs4_opendata *opendata, struct nfs4_state *state)
2088{
2089        int ret;
2090
2091        /* memory barrier prior to reading state->n_* */
2092        smp_rmb();
2093        ret = nfs4_open_recover_helper(opendata, FMODE_READ|FMODE_WRITE);
2094        if (ret != 0)
2095                return ret;
2096        ret = nfs4_open_recover_helper(opendata, FMODE_WRITE);
2097        if (ret != 0)
2098                return ret;
2099        ret = nfs4_open_recover_helper(opendata, FMODE_READ);
2100        if (ret != 0)
2101                return ret;
2102        /*
2103         * We may have performed cached opens for all three recoveries.
2104         * Check if we need to update the current stateid.
2105         */
2106        if (test_bit(NFS_DELEGATED_STATE, &state->flags) == 0 &&
2107            !nfs4_stateid_match(&state->stateid, &state->open_stateid)) {
2108                write_seqlock(&state->seqlock);
2109                if (test_bit(NFS_DELEGATED_STATE, &state->flags) == 0)
2110                        nfs4_stateid_copy(&state->stateid, &state->open_stateid);
2111                write_sequnlock(&state->seqlock);
2112        }
2113        return 0;
2114}
2115
2116/*
2117 * OPEN_RECLAIM:
2118 *      reclaim state on the server after a reboot.
2119 */
2120static int _nfs4_do_open_reclaim(struct nfs_open_context *ctx, struct nfs4_state *state)
2121{
2122        struct nfs_delegation *delegation;
2123        struct nfs4_opendata *opendata;
2124        fmode_t delegation_type = 0;
2125        int status;
2126
2127        opendata = nfs4_open_recoverdata_alloc(ctx, state,
2128                        NFS4_OPEN_CLAIM_PREVIOUS);
2129        if (IS_ERR(opendata))
2130                return PTR_ERR(opendata);
2131        rcu_read_lock();
2132        delegation = rcu_dereference(NFS_I(state->inode)->delegation);
2133        if (delegation != NULL && test_bit(NFS_DELEGATION_NEED_RECLAIM, &delegation->flags) != 0)
2134                delegation_type = delegation->type;
2135        rcu_read_unlock();
2136        opendata->o_arg.u.delegation_type = delegation_type;
2137        status = nfs4_open_recover(opendata, state);
2138        nfs4_opendata_put(opendata);
2139        return status;
2140}
2141
2142static int nfs4_do_open_reclaim(struct nfs_open_context *ctx, struct nfs4_state *state)
2143{
2144        struct nfs_server *server = NFS_SERVER(state->inode);
2145        struct nfs4_exception exception = { };
2146        int err;
2147        do {
2148                err = _nfs4_do_open_reclaim(ctx, state);
2149                trace_nfs4_open_reclaim(ctx, 0, err);
2150                if (nfs4_clear_cap_atomic_open_v1(server, err, &exception))
2151                        continue;
2152                if (err != -NFS4ERR_DELAY)
2153                        break;
2154                nfs4_handle_exception(server, err, &exception);
2155        } while (exception.retry);
2156        return err;
2157}
2158
2159static int nfs4_open_reclaim(struct nfs4_state_owner *sp, struct nfs4_state *state)
2160{
2161        struct nfs_open_context *ctx;
2162        int ret;
2163
2164        ctx = nfs4_state_find_open_context(state);
2165        if (IS_ERR(ctx))
2166                return -EAGAIN;
2167        clear_bit(NFS_DELEGATED_STATE, &state->flags);
2168        nfs_state_clear_open_state_flags(state);
2169        ret = nfs4_do_open_reclaim(ctx, state);
2170        put_nfs_open_context(ctx);
2171        return ret;
2172}
2173
2174static int nfs4_handle_delegation_recall_error(struct nfs_server *server, struct nfs4_state *state, const nfs4_stateid *stateid, struct file_lock *fl, int err)
2175{
2176        switch (err) {
2177                default:
2178                        printk(KERN_ERR "NFS: %s: unhandled error "
2179                                        "%d.\n", __func__, err);
2180                case 0:
2181                case -ENOENT:
2182                case -EAGAIN:
2183                case -ESTALE:
2184                case -ETIMEDOUT:
2185                        break;
2186                case -NFS4ERR_BADSESSION:
2187                case -NFS4ERR_BADSLOT:
2188                case -NFS4ERR_BAD_HIGH_SLOT:
2189                case -NFS4ERR_CONN_NOT_BOUND_TO_SESSION:
2190                case -NFS4ERR_DEADSESSION:
2191                        nfs4_schedule_session_recovery(server->nfs_client->cl_session, err);
2192                        return -EAGAIN;
2193                case -NFS4ERR_STALE_CLIENTID:
2194                case -NFS4ERR_STALE_STATEID:
2195                        /* Don't recall a delegation if it was lost */
2196                        nfs4_schedule_lease_recovery(server->nfs_client);
2197                        return -EAGAIN;
2198                case -NFS4ERR_MOVED:
2199                        nfs4_schedule_migration_recovery(server);
2200                        return -EAGAIN;
2201                case -NFS4ERR_LEASE_MOVED:
2202                        nfs4_schedule_lease_moved_recovery(server->nfs_client);
2203                        return -EAGAIN;
2204                case -NFS4ERR_DELEG_REVOKED:
2205                case -NFS4ERR_ADMIN_REVOKED:
2206                case -NFS4ERR_EXPIRED:
2207                case -NFS4ERR_BAD_STATEID:
2208                case -NFS4ERR_OPENMODE:
2209                        nfs_inode_find_state_and_recover(state->inode,
2210                                        stateid);
2211                        nfs4_schedule_stateid_recovery(server, state);
2212                        return -EAGAIN;
2213                case -NFS4ERR_DELAY:
2214                case -NFS4ERR_GRACE:
2215                        ssleep(1);
2216                        return -EAGAIN;
2217                case -ENOMEM:
2218                case -NFS4ERR_DENIED:
2219                        if (fl) {
2220                                struct nfs4_lock_state *lsp = fl->fl_u.nfs4_fl.owner;
2221                                if (lsp)
2222                                        set_bit(NFS_LOCK_LOST, &lsp->ls_flags);
2223                        }
2224                        return 0;
2225        }
2226        return err;
2227}
2228
2229int nfs4_open_delegation_recall(struct nfs_open_context *ctx,
2230                struct nfs4_state *state, const nfs4_stateid *stateid)
2231{
2232        struct nfs_server *server = NFS_SERVER(state->inode);
2233        struct nfs4_opendata *opendata;
2234        int err = 0;
2235
2236        opendata = nfs4_open_recoverdata_alloc(ctx, state,
2237                        NFS4_OPEN_CLAIM_DELEG_CUR_FH);
2238        if (IS_ERR(opendata))
2239                return PTR_ERR(opendata);
2240        nfs4_stateid_copy(&opendata->o_arg.u.delegation, stateid);
2241        if (!test_bit(NFS_O_RDWR_STATE, &state->flags)) {
2242                err = nfs4_open_recover_helper(opendata, FMODE_READ|FMODE_WRITE);
2243                if (err)
2244                        goto out;
2245        }
2246        if (!test_bit(NFS_O_WRONLY_STATE, &state->flags)) {
2247                err = nfs4_open_recover_helper(opendata, FMODE_WRITE);
2248                if (err)
2249                        goto out;
2250        }
2251        if (!test_bit(NFS_O_RDONLY_STATE, &state->flags)) {
2252                err = nfs4_open_recover_helper(opendata, FMODE_READ);
2253                if (err)
2254                        goto out;
2255        }
2256        nfs_state_clear_delegation(state);
2257out:
2258        nfs4_opendata_put(opendata);
2259        return nfs4_handle_delegation_recall_error(server, state, stateid, NULL, err);
2260}
2261
2262static void nfs4_open_confirm_prepare(struct rpc_task *task, void *calldata)
2263{
2264        struct nfs4_opendata *data = calldata;
2265
2266        nfs4_setup_sequence(data->o_arg.server->nfs_client,
2267                           &data->c_arg.seq_args, &data->c_res.seq_res, task);
2268}
2269
2270static void nfs4_open_confirm_done(struct rpc_task *task, void *calldata)
2271{
2272        struct nfs4_opendata *data = calldata;
2273
2274        nfs40_sequence_done(task, &data->c_res.seq_res);
2275
2276        data->rpc_status = task->tk_status;
2277        if (data->rpc_status == 0) {
2278                nfs4_stateid_copy(&data->o_res.stateid, &data->c_res.stateid);
2279                nfs_confirm_seqid(&data->owner->so_seqid, 0);
2280                renew_lease(data->o_res.server, data->timestamp);
2281                data->rpc_done = true;
2282        }
2283}
2284
2285static void nfs4_open_confirm_release(void *calldata)
2286{
2287        struct nfs4_opendata *data = calldata;
2288        struct nfs4_state *state = NULL;
2289
2290        /* If this request hasn't been cancelled, do nothing */
2291        if (!data->cancelled)
2292                goto out_free;
2293        /* In case of error, no cleanup! */
2294        if (!data->rpc_done)
2295                goto out_free;
2296        state = nfs4_opendata_to_nfs4_state(data);
2297        if (!IS_ERR(state))
2298                nfs4_close_state(state, data->o_arg.fmode);
2299out_free:
2300        nfs4_opendata_put(data);
2301}
2302
2303static const struct rpc_call_ops nfs4_open_confirm_ops = {
2304        .rpc_call_prepare = nfs4_open_confirm_prepare,
2305        .rpc_call_done = nfs4_open_confirm_done,
2306        .rpc_release = nfs4_open_confirm_release,
2307};
2308
2309/*
2310 * Note: On error, nfs4_proc_open_confirm will free the struct nfs4_opendata
2311 */
2312static int _nfs4_proc_open_confirm(struct nfs4_opendata *data)
2313{
2314        struct nfs_server *server = NFS_SERVER(d_inode(data->dir));
2315        struct rpc_task *task;
2316        struct  rpc_message msg = {
2317                .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_OPEN_CONFIRM],
2318                .rpc_argp = &data->c_arg,
2319                .rpc_resp = &data->c_res,
2320                .rpc_cred = data->owner->so_cred,
2321        };
2322        struct rpc_task_setup task_setup_data = {
2323                .rpc_client = server->client,
2324                .rpc_message = &msg,
2325                .callback_ops = &nfs4_open_confirm_ops,
2326                .callback_data = data,
2327                .workqueue = nfsiod_workqueue,
2328                .flags = RPC_TASK_ASYNC,
2329        };
2330        int status;
2331
2332        nfs4_init_sequence(&data->c_arg.seq_args, &data->c_res.seq_res, 1,
2333                                data->is_recover);
2334        kref_get(&data->kref);
2335        data->rpc_done = false;
2336        data->rpc_status = 0;
2337        data->timestamp = jiffies;
2338        task = rpc_run_task(&task_setup_data);
2339        if (IS_ERR(task))
2340                return PTR_ERR(task);
2341        status = rpc_wait_for_completion_task(task);
2342        if (status != 0) {
2343                data->cancelled = true;
2344                smp_wmb();
2345        } else
2346                status = data->rpc_status;
2347        rpc_put_task(task);
2348        return status;
2349}
2350
2351static void nfs4_open_prepare(struct rpc_task *task, void *calldata)
2352{
2353        struct nfs4_opendata *data = calldata;
2354        struct nfs4_state_owner *sp = data->owner;
2355        struct nfs_client *clp = sp->so_server->nfs_client;
2356        enum open_claim_type4 claim = data->o_arg.claim;
2357
2358        if (nfs_wait_on_sequence(data->o_arg.seqid, task) != 0)
2359                goto out_wait;
2360        /*
2361         * Check if we still need to send an OPEN call, or if we can use
2362         * a delegation instead.
2363         */
2364        if (data->state != NULL) {
2365                struct nfs_delegation *delegation;
2366
2367                if (can_open_cached(data->state, data->o_arg.fmode,
2368                                        data->o_arg.open_flags, claim))
2369                        goto out_no_action;
2370                rcu_read_lock();
2371                delegation = nfs4_get_valid_delegation(data->state->inode);
2372                if (can_open_delegated(delegation, data->o_arg.fmode, claim))
2373                        goto unlock_no_action;
2374                rcu_read_unlock();
2375        }
2376        /* Update client id. */
2377        data->o_arg.clientid = clp->cl_clientid;
2378        switch (claim) {
2379        default:
2380                break;
2381        case NFS4_OPEN_CLAIM_PREVIOUS:
2382        case NFS4_OPEN_CLAIM_DELEG_CUR_FH:
2383        case NFS4_OPEN_CLAIM_DELEG_PREV_FH:
2384                data->o_arg.open_bitmap = &nfs4_open_noattr_bitmap[0];
2385                /* Fall through */
2386        case NFS4_OPEN_CLAIM_FH:
2387                task->tk_msg.rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_OPEN_NOATTR];
2388        }
2389        data->timestamp = jiffies;
2390        if (nfs4_setup_sequence(data->o_arg.server->nfs_client,
2391                                &data->o_arg.seq_args,
2392                                &data->o_res.seq_res,
2393                                task) != 0)
2394                nfs_release_seqid(data->o_arg.seqid);
2395
2396        /* Set the create mode (note dependency on the session type) */
2397        data->o_arg.createmode = NFS4_CREATE_UNCHECKED;
2398        if (data->o_arg.open_flags & O_EXCL) {
2399                data->o_arg.createmode = NFS4_CREATE_EXCLUSIVE;
2400                if (nfs4_has_persistent_session(clp))
2401                        data->o_arg.createmode = NFS4_CREATE_GUARDED;
2402                else if (clp->cl_mvops->minor_version > 0)
2403                        data->o_arg.createmode = NFS4_CREATE_EXCLUSIVE4_1;
2404        }
2405        return;
2406unlock_no_action:
2407        trace_nfs4_cached_open(data->state);
2408        rcu_read_unlock();
2409out_no_action:
2410        task->tk_action = NULL;
2411out_wait:
2412        nfs4_sequence_done(task, &data->o_res.seq_res);
2413}
2414
2415static void nfs4_open_done(struct rpc_task *task, void *calldata)
2416{
2417        struct nfs4_opendata *data = calldata;
2418
2419        data->rpc_status = task->tk_status;
2420
2421        if (!nfs4_sequence_process(task, &data->o_res.seq_res))
2422                return;
2423
2424        if (task->tk_status == 0) {
2425                if (data->o_res.f_attr->valid & NFS_ATTR_FATTR_TYPE) {
2426                        switch (data->o_res.f_attr->mode & S_IFMT) {
2427                        case S_IFREG:
2428                                break;
2429                        case S_IFLNK:
2430                                data->rpc_status = -ELOOP;
2431                                break;
2432                        case S_IFDIR:
2433                                data->rpc_status = -EISDIR;
2434                                break;
2435                        default:
2436                                data->rpc_status = -ENOTDIR;
2437                        }
2438                }
2439                renew_lease(data->o_res.server, data->timestamp);
2440                if (!(data->o_res.rflags & NFS4_OPEN_RESULT_CONFIRM))
2441                        nfs_confirm_seqid(&data->owner->so_seqid, 0);
2442        }
2443        data->rpc_done = true;
2444}
2445
2446static void nfs4_open_release(void *calldata)
2447{
2448        struct nfs4_opendata *data = calldata;
2449        struct nfs4_state *state = NULL;
2450
2451        /* If this request hasn't been cancelled, do nothing */
2452        if (!data->cancelled)
2453                goto out_free;
2454        /* In case of error, no cleanup! */
2455        if (data->rpc_status != 0 || !data->rpc_done)
2456                goto out_free;
2457        /* In case we need an open_confirm, no cleanup! */
2458        if (data->o_res.rflags & NFS4_OPEN_RESULT_CONFIRM)
2459                goto out_free;
2460        state = nfs4_opendata_to_nfs4_state(data);
2461        if (!IS_ERR(state))
2462                nfs4_close_state(state, data->o_arg.fmode);
2463out_free:
2464        nfs4_opendata_put(data);
2465}
2466
2467static const struct rpc_call_ops nfs4_open_ops = {
2468        .rpc_call_prepare = nfs4_open_prepare,
2469        .rpc_call_done = nfs4_open_done,
2470        .rpc_release = nfs4_open_release,
2471};
2472
2473static int nfs4_run_open_task(struct nfs4_opendata *data,
2474                              struct nfs_open_context *ctx)
2475{
2476        struct inode *dir = d_inode(data->dir);
2477        struct nfs_server *server = NFS_SERVER(dir);
2478        struct nfs_openargs *o_arg = &data->o_arg;
2479        struct nfs_openres *o_res = &data->o_res;
2480        struct rpc_task *task;
2481        struct rpc_message msg = {
2482                .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_OPEN],
2483                .rpc_argp = o_arg,
2484                .rpc_resp = o_res,
2485                .rpc_cred = data->owner->so_cred,
2486        };
2487        struct rpc_task_setup task_setup_data = {
2488                .rpc_client = server->client,
2489                .rpc_message = &msg,
2490                .callback_ops = &nfs4_open_ops,
2491                .callback_data = data,
2492                .workqueue = nfsiod_workqueue,
2493                .flags = RPC_TASK_ASYNC,
2494        };
2495        int status;
2496
2497        kref_get(&data->kref);
2498        data->rpc_done = false;
2499        data->rpc_status = 0;
2500        data->cancelled = false;
2501        data->is_recover = false;
2502        if (!ctx) {
2503                nfs4_init_sequence(&o_arg->seq_args, &o_res->seq_res, 1, 1);
2504                data->is_recover = true;
2505                task_setup_data.flags |= RPC_TASK_TIMEOUT;
2506        } else {
2507                nfs4_init_sequence(&o_arg->seq_args, &o_res->seq_res, 1, 0);
2508                pnfs_lgopen_prepare(data, ctx);
2509        }
2510        task = rpc_run_task(&task_setup_data);
2511        if (IS_ERR(task))
2512                return PTR_ERR(task);
2513        status = rpc_wait_for_completion_task(task);
2514        if (status != 0) {
2515                data->cancelled = true;
2516                smp_wmb();
2517        } else
2518                status = data->rpc_status;
2519        rpc_put_task(task);
2520
2521        return status;
2522}
2523
2524static int _nfs4_recover_proc_open(struct nfs4_opendata *data)
2525{
2526        struct inode *dir = d_inode(data->dir);
2527        struct nfs_openres *o_res = &data->o_res;
2528        int status;
2529
2530        status = nfs4_run_open_task(data, NULL);
2531        if (status != 0 || !data->rpc_done)
2532                return status;
2533
2534        nfs_fattr_map_and_free_names(NFS_SERVER(dir), &data->f_attr);
2535
2536        if (o_res->rflags & NFS4_OPEN_RESULT_CONFIRM)
2537                status = _nfs4_proc_open_confirm(data);
2538
2539        return status;
2540}
2541
2542/*
2543 * Additional permission checks in order to distinguish between an
2544 * open for read, and an open for execute. This works around the
2545 * fact that NFSv4 OPEN treats read and execute permissions as being
2546 * the same.
2547 * Note that in the non-execute case, we want to turn off permission
2548 * checking if we just created a new file (POSIX open() semantics).
2549 */
2550static int nfs4_opendata_access(const struct cred *cred,
2551                                struct nfs4_opendata *opendata,
2552                                struct nfs4_state *state, fmode_t fmode,
2553                                int openflags)
2554{
2555        struct nfs_access_entry cache;
2556        u32 mask, flags;
2557
2558        /* access call failed or for some reason the server doesn't
2559         * support any access modes -- defer access call until later */
2560        if (opendata->o_res.access_supported == 0)
2561                return 0;
2562
2563        mask = 0;
2564        /*
2565         * Use openflags to check for exec, because fmode won't
2566         * always have FMODE_EXEC set when file open for exec.
2567         */
2568        if (openflags & __FMODE_EXEC) {
2569                /* ONLY check for exec rights */
2570                if (S_ISDIR(state->inode->i_mode))
2571                        mask = NFS4_ACCESS_LOOKUP;
2572                else
2573                        mask = NFS4_ACCESS_EXECUTE;
2574        } else if ((fmode & FMODE_READ) && !opendata->file_created)
2575                mask = NFS4_ACCESS_READ;
2576
2577        cache.cred = cred;
2578        nfs_access_set_mask(&cache, opendata->o_res.access_result);
2579        nfs_access_add_cache(state->inode, &cache);
2580
2581        flags = NFS4_ACCESS_READ | NFS4_ACCESS_EXECUTE | NFS4_ACCESS_LOOKUP;
2582        if ((mask & ~cache.mask & flags) == 0)
2583                return 0;
2584
2585        return -EACCES;
2586}
2587
2588/*
2589 * Note: On error, nfs4_proc_open will free the struct nfs4_opendata
2590 */
2591static int _nfs4_proc_open(struct nfs4_opendata *data,
2592                           struct nfs_open_context *ctx)
2593{
2594        struct inode *dir = d_inode(data->dir);
2595        struct nfs_server *server = NFS_SERVER(dir);
2596        struct nfs_openargs *o_arg = &data->o_arg;
2597        struct nfs_openres *o_res = &data->o_res;
2598        int status;
2599
2600        status = nfs4_run_open_task(data, ctx);
2601        if (!data->rpc_done)
2602                return status;
2603        if (status != 0) {
2604                if (status == -NFS4ERR_BADNAME &&
2605                                !(o_arg->open_flags & O_CREAT))
2606                        return -ENOENT;
2607                return status;
2608        }
2609
2610        nfs_fattr_map_and_free_names(server, &data->f_attr);
2611
2612        if (o_arg->open_flags & O_CREAT) {
2613                if (o_arg->open_flags & O_EXCL)
2614                        data->file_created = true;
2615                else if (o_res->cinfo.before != o_res->cinfo.after)
2616                        data->file_created = true;
2617                if (data->file_created ||
2618                    inode_peek_iversion_raw(dir) != o_res->cinfo.after)
2619                        update_changeattr(dir, &o_res->cinfo,
2620                                        o_res->f_attr->time_start, 0);
2621        }
2622        if ((o_res->rflags & NFS4_OPEN_RESULT_LOCKTYPE_POSIX) == 0)
2623                server->caps &= ~NFS_CAP_POSIX_LOCK;
2624        if(o_res->rflags & NFS4_OPEN_RESULT_CONFIRM) {
2625                status = _nfs4_proc_open_confirm(data);
2626                if (status != 0)
2627                        return status;
2628        }
2629        if (!(o_res->f_attr->valid & NFS_ATTR_FATTR)) {
2630                nfs4_sequence_free_slot(&o_res->seq_res);
2631                nfs4_proc_getattr(server, &o_res->fh, o_res->f_attr,
2632                                o_res->f_label, NULL);
2633        }
2634        return 0;
2635}
2636
2637/*
2638 * OPEN_EXPIRED:
2639 *      reclaim state on the server after a network partition.
2640 *      Assumes caller holds the appropriate lock
2641 */
2642static int _nfs4_open_expired(struct nfs_open_context *ctx, struct nfs4_state *state)
2643{
2644        struct nfs4_opendata *opendata;
2645        int ret;
2646
2647        opendata = nfs4_open_recoverdata_alloc(ctx, state,
2648                        NFS4_OPEN_CLAIM_FH);
2649        if (IS_ERR(opendata))
2650                return PTR_ERR(opendata);
2651        ret = nfs4_open_recover(opendata, state);
2652        if (ret == -ESTALE)
2653                d_drop(ctx->dentry);
2654        nfs4_opendata_put(opendata);
2655        return ret;
2656}
2657
2658static int nfs4_do_open_expired(struct nfs_open_context *ctx, struct nfs4_state *state)
2659{
2660        struct nfs_server *server = NFS_SERVER(state->inode);
2661        struct nfs4_exception exception = { };
2662        int err;
2663
2664        do {
2665                err = _nfs4_open_expired(ctx, state);
2666                trace_nfs4_open_expired(ctx, 0, err);
2667                if (nfs4_clear_cap_atomic_open_v1(server, err, &exception))
2668                        continue;
2669                switch (err) {
2670                default:
2671                        goto out;
2672                case -NFS4ERR_GRACE:
2673                case -NFS4ERR_DELAY:
2674                        nfs4_handle_exception(server, err, &exception);
2675                        err = 0;
2676                }
2677        } while (exception.retry);
2678out:
2679        return err;
2680}
2681
2682static int nfs4_open_expired(struct nfs4_state_owner *sp, struct nfs4_state *state)
2683{
2684        struct nfs_open_context *ctx;
2685        int ret;
2686
2687        ctx = nfs4_state_find_open_context(state);
2688        if (IS_ERR(ctx))
2689                return -EAGAIN;
2690        ret = nfs4_do_open_expired(ctx, state);
2691        put_nfs_open_context(ctx);
2692        return ret;
2693}
2694
2695static void nfs_finish_clear_delegation_stateid(struct nfs4_state *state,
2696                const nfs4_stateid *stateid)
2697{
2698        nfs_remove_bad_delegation(state->inode, stateid);
2699        nfs_state_clear_delegation(state);
2700}
2701
2702static void nfs40_clear_delegation_stateid(struct nfs4_state *state)
2703{
2704        if (rcu_access_pointer(NFS_I(state->inode)->delegation) != NULL)
2705                nfs_finish_clear_delegation_stateid(state, NULL);
2706}
2707
2708static int nfs40_open_expired(struct nfs4_state_owner *sp, struct nfs4_state *state)
2709{
2710        /* NFSv4.0 doesn't allow for delegation recovery on open expire */
2711        nfs40_clear_delegation_stateid(state);
2712        nfs_state_clear_open_state_flags(state);
2713        return nfs4_open_expired(sp, state);
2714}
2715
2716static int nfs40_test_and_free_expired_stateid(struct nfs_server *server,
2717                nfs4_stateid *stateid,
2718                const struct cred *cred)
2719{
2720        return -NFS4ERR_BAD_STATEID;
2721}
2722
2723#if defined(CONFIG_NFS_V4_1)
2724static int nfs41_test_and_free_expired_stateid(struct nfs_server *server,
2725                nfs4_stateid *stateid,
2726                const struct cred *cred)
2727{
2728        int status;
2729
2730        switch (stateid->type) {
2731        default:
2732                break;
2733        case NFS4_INVALID_STATEID_TYPE:
2734        case NFS4_SPECIAL_STATEID_TYPE:
2735                return -NFS4ERR_BAD_STATEID;
2736        case NFS4_REVOKED_STATEID_TYPE:
2737                goto out_free;
2738        }
2739
2740        status = nfs41_test_stateid(server, stateid, cred);
2741        switch (status) {
2742        case -NFS4ERR_EXPIRED:
2743        case -NFS4ERR_ADMIN_REVOKED:
2744        case -NFS4ERR_DELEG_REVOKED:
2745                break;
2746        default:
2747                return status;
2748        }
2749out_free:
2750        /* Ack the revoked state to the server */
2751        nfs41_free_stateid(server, stateid, cred, true);
2752        return -NFS4ERR_EXPIRED;
2753}
2754
2755static int nfs41_check_delegation_stateid(struct nfs4_state *state)
2756{
2757        struct nfs_server *server = NFS_SERVER(state->inode);
2758        nfs4_stateid stateid;
2759        struct nfs_delegation *delegation;
2760        const struct cred *cred = NULL;
2761        int status, ret = NFS_OK;
2762
2763        /* Get the delegation credential for use by test/free_stateid */
2764        rcu_read_lock();
2765        delegation = rcu_dereference(NFS_I(state->inode)->delegation);
2766        if (delegation == NULL) {
2767                rcu_read_unlock();
2768                nfs_state_clear_delegation(state);
2769                return NFS_OK;
2770        }
2771
2772        nfs4_stateid_copy(&stateid, &delegation->stateid);
2773
2774        if (!test_and_clear_bit(NFS_DELEGATION_TEST_EXPIRED,
2775                                &delegation->flags)) {
2776                rcu_read_unlock();
2777                return NFS_OK;
2778        }
2779
2780        if (delegation->cred)
2781                cred = get_cred(delegation->cred);
2782        rcu_read_unlock();
2783        status = nfs41_test_and_free_expired_stateid(server, &stateid, cred);
2784        trace_nfs4_test_delegation_stateid(state, NULL, status);
2785        if (status == -NFS4ERR_EXPIRED || status == -NFS4ERR_BAD_STATEID)
2786                nfs_finish_clear_delegation_stateid(state, &stateid);
2787        else
2788                ret = status;
2789
2790        put_cred(cred);
2791        return ret;
2792}
2793
2794static void nfs41_delegation_recover_stateid(struct nfs4_state *state)
2795{
2796        nfs4_stateid tmp;
2797
2798        if (test_bit(NFS_DELEGATED_STATE, &state->flags) &&
2799            nfs4_copy_delegation_stateid(state->inode, state->state,
2800                                &tmp, NULL) &&
2801            nfs4_stateid_match_other(&state->stateid, &tmp))
2802                nfs_state_set_delegation(state, &tmp, state->state);
2803        else
2804                nfs_state_clear_delegation(state);
2805}
2806
2807/**
2808 * nfs41_check_expired_locks - possibly free a lock stateid
2809 *
2810 * @state: NFSv4 state for an inode
2811 *
2812 * Returns NFS_OK if recovery for this stateid is now finished.
2813 * Otherwise a negative NFS4ERR value is returned.
2814 */
2815static int nfs41_check_expired_locks(struct nfs4_state *state)
2816{
2817        int status, ret = NFS_OK;
2818        struct nfs4_lock_state *lsp, *prev = NULL;
2819        struct nfs_server *server = NFS_SERVER(state->inode);
2820
2821        if (!test_bit(LK_STATE_IN_USE, &state->flags))
2822                goto out;
2823
2824        spin_lock(&state->state_lock);
2825        list_for_each_entry(lsp, &state->lock_states, ls_locks) {
2826                if (test_bit(NFS_LOCK_INITIALIZED, &lsp->ls_flags)) {
2827                        const struct cred *cred = lsp->ls_state->owner->so_cred;
2828
2829                        refcount_inc(&lsp->ls_count);
2830                        spin_unlock(&state->state_lock);
2831
2832                        nfs4_put_lock_state(prev);
2833                        prev = lsp;
2834
2835                        status = nfs41_test_and_free_expired_stateid(server,
2836                                        &lsp->ls_stateid,
2837                                        cred);
2838                        trace_nfs4_test_lock_stateid(state, lsp, status);
2839                        if (status == -NFS4ERR_EXPIRED ||
2840                            status == -NFS4ERR_BAD_STATEID) {
2841                                clear_bit(NFS_LOCK_INITIALIZED, &lsp->ls_flags);
2842                                lsp->ls_stateid.type = NFS4_INVALID_STATEID_TYPE;
2843                                if (!recover_lost_locks)
2844                                        set_bit(NFS_LOCK_LOST, &lsp->ls_flags);
2845                        } else if (status != NFS_OK) {
2846                                ret = status;
2847                                nfs4_put_lock_state(prev);
2848                                goto out;
2849                        }
2850                        spin_lock(&state->state_lock);
2851                }
2852        }
2853        spin_unlock(&state->state_lock);
2854        nfs4_put_lock_state(prev);
2855out:
2856        return ret;
2857}
2858
2859/**
2860 * nfs41_check_open_stateid - possibly free an open stateid
2861 *
2862 * @state: NFSv4 state for an inode
2863 *
2864 * Returns NFS_OK if recovery for this stateid is now finished.
2865 * Otherwise a negative NFS4ERR value is returned.
2866 */
2867static int nfs41_check_open_stateid(struct nfs4_state *state)
2868{
2869        struct nfs_server *server = NFS_SERVER(state->inode);
2870        nfs4_stateid *stateid = &state->open_stateid;
2871        const struct cred *cred = state->owner->so_cred;
2872        int status;
2873
2874        if (test_bit(NFS_OPEN_STATE, &state->flags) == 0)
2875                return -NFS4ERR_BAD_STATEID;
2876        status = nfs41_test_and_free_expired_stateid(server, stateid, cred);
2877        trace_nfs4_test_open_stateid(state, NULL, status);
2878        if (status == -NFS4ERR_EXPIRED || status == -NFS4ERR_BAD_STATEID) {
2879                nfs_state_clear_open_state_flags(state);
2880                stateid->type = NFS4_INVALID_STATEID_TYPE;
2881                return status;
2882        }
2883        if (nfs_open_stateid_recover_openmode(state))
2884                return -NFS4ERR_OPENMODE;
2885        return NFS_OK;
2886}
2887
2888static int nfs41_open_expired(struct nfs4_state_owner *sp, struct nfs4_state *state)
2889{
2890        int status;
2891
2892        status = nfs41_check_delegation_stateid(state);
2893        if (status != NFS_OK)
2894                return status;
2895        nfs41_delegation_recover_stateid(state);
2896
2897        status = nfs41_check_expired_locks(state);
2898        if (status != NFS_OK)
2899                return status;
2900        status = nfs41_check_open_stateid(state);
2901        if (status != NFS_OK)
2902                status = nfs4_open_expired(sp, state);
2903        return status;
2904}
2905#endif
2906
2907/*
2908 * on an EXCLUSIVE create, the server should send back a bitmask with FATTR4-*
2909 * fields corresponding to attributes that were used to store the verifier.
2910 * Make sure we clobber those fields in the later setattr call
2911 */
2912static unsigned nfs4_exclusive_attrset(struct nfs4_opendata *opendata,
2913                                struct iattr *sattr, struct nfs4_label **label)
2914{
2915        const __u32 *bitmask = opendata->o_arg.server->exclcreat_bitmask;
2916        __u32 attrset[3];
2917        unsigned ret;
2918        unsigned i;
2919
2920        for (i = 0; i < ARRAY_SIZE(attrset); i++) {
2921                attrset[i] = opendata->o_res.attrset[i];
2922                if (opendata->o_arg.createmode == NFS4_CREATE_EXCLUSIVE4_1)
2923                        attrset[i] &= ~bitmask[i];
2924        }
2925
2926        ret = (opendata->o_arg.createmode == NFS4_CREATE_EXCLUSIVE) ?
2927                sattr->ia_valid : 0;
2928
2929        if ((attrset[1] & (FATTR4_WORD1_TIME_ACCESS|FATTR4_WORD1_TIME_ACCESS_SET))) {
2930                if (sattr->ia_valid & ATTR_ATIME_SET)
2931                        ret |= ATTR_ATIME_SET;
2932                else
2933                        ret |= ATTR_ATIME;
2934        }
2935
2936        if ((attrset[1] & (FATTR4_WORD1_TIME_MODIFY|FATTR4_WORD1_TIME_MODIFY_SET))) {
2937                if (sattr->ia_valid & ATTR_MTIME_SET)
2938                        ret |= ATTR_MTIME_SET;
2939                else
2940                        ret |= ATTR_MTIME;
2941        }
2942
2943        if (!(attrset[2] & FATTR4_WORD2_SECURITY_LABEL))
2944                *label = NULL;
2945        return ret;
2946}
2947
2948static int _nfs4_open_and_get_state(struct nfs4_opendata *opendata,
2949                int flags, struct nfs_open_context *ctx)
2950{
2951        struct nfs4_state_owner *sp = opendata->owner;
2952        struct nfs_server *server = sp->so_server;
2953        struct dentry *dentry;
2954        struct nfs4_state *state;
2955        fmode_t acc_mode = _nfs4_ctx_to_accessmode(ctx);
2956        unsigned int seq;
2957        int ret;
2958
2959        seq = raw_seqcount_begin(&sp->so_reclaim_seqcount);
2960
2961        ret = _nfs4_proc_open(opendata, ctx);
2962        if (ret != 0)
2963                goto out;
2964
2965        state = _nfs4_opendata_to_nfs4_state(opendata);
2966        ret = PTR_ERR(state);
2967        if (IS_ERR(state))
2968                goto out;
2969        ctx->state = state;
2970        if (server->caps & NFS_CAP_POSIX_LOCK)
2971                set_bit(NFS_STATE_POSIX_LOCKS, &state->flags);
2972        if (opendata->o_res.rflags & NFS4_OPEN_RESULT_MAY_NOTIFY_LOCK)
2973                set_bit(NFS_STATE_MAY_NOTIFY_LOCK, &state->flags);
2974
2975        dentry = opendata->dentry;
2976        if (d_really_is_negative(dentry)) {
2977                struct dentry *alias;
2978                d_drop(dentry);
2979                alias = d_exact_alias(dentry, state->inode);
2980                if (!alias)
2981                        alias = d_splice_alias(igrab(state->inode), dentry);
2982                /* d_splice_alias() can't fail here - it's a non-directory */
2983                if (alias) {
2984                        dput(ctx->dentry);
2985                        ctx->dentry = dentry = alias;
2986                }
2987                nfs_set_verifier(dentry,
2988                                nfs_save_change_attribute(d_inode(opendata->dir)));
2989        }
2990
2991        /* Parse layoutget results before we check for access */
2992        pnfs_parse_lgopen(state->inode, opendata->lgp, ctx);
2993
2994        ret = nfs4_opendata_access(sp->so_cred, opendata, state,
2995                        acc_mode, flags);
2996        if (ret != 0)
2997                goto out;
2998
2999        if (d_inode(dentry) == state->inode) {
3000                nfs_inode_attach_open_context(ctx);
3001                if (read_seqcount_retry(&sp->so_reclaim_seqcount, seq))
3002                        nfs4_schedule_stateid_recovery(server, state);
3003        }
3004
3005out:
3006        if (!opendata->cancelled)
3007                nfs4_sequence_free_slot(&opendata->o_res.seq_res);
3008        return ret;
3009}
3010
3011/*
3012 * Returns a referenced nfs4_state
3013 */
3014static int _nfs4_do_open(struct inode *dir,
3015                        struct nfs_open_context *ctx,
3016                        int flags,
3017                        const struct nfs4_open_createattrs *c,
3018                        int *opened)
3019{
3020        struct nfs4_state_owner  *sp;
3021        struct nfs4_state     *state = NULL;
3022        struct nfs_server       *server = NFS_SERVER(dir);
3023        struct nfs4_opendata *opendata;
3024        struct dentry *dentry = ctx->dentry;
3025        const struct cred *cred = ctx->cred;
3026        struct nfs4_threshold **ctx_th = &ctx->mdsthreshold;
3027        fmode_t fmode = _nfs4_ctx_to_openmode(ctx);
3028        enum open_claim_type4 claim = NFS4_OPEN_CLAIM_NULL;
3029        struct iattr *sattr = c->sattr;
3030        struct nfs4_label *label = c->label;
3031        struct nfs4_label *olabel = NULL;
3032        int status;
3033
3034        /* Protect against reboot recovery conflicts */
3035        status = -ENOMEM;
3036        sp = nfs4_get_state_owner(server, cred, GFP_KERNEL);
3037        if (sp == NULL) {
3038                dprintk("nfs4_do_open: nfs4_get_state_owner failed!\n");
3039                goto out_err;
3040        }
3041        status = nfs4_client_recover_expired_lease(server->nfs_client);
3042        if (status != 0)
3043                goto err_put_state_owner;
3044        if (d_really_is_positive(dentry))
3045                nfs4_return_incompatible_delegation(d_inode(dentry), fmode);
3046        status = -ENOMEM;
3047        if (d_really_is_positive(dentry))
3048                claim = NFS4_OPEN_CLAIM_FH;
3049        opendata = nfs4_opendata_alloc(dentry, sp, fmode, flags,
3050                        c, claim, GFP_KERNEL);
3051        if (opendata == NULL)
3052                goto err_put_state_owner;
3053
3054        if (label) {
3055                olabel = nfs4_label_alloc(server, GFP_KERNEL);
3056                if (IS_ERR(olabel)) {
3057                        status = PTR_ERR(olabel);
3058                        goto err_opendata_put;
3059                }
3060        }
3061
3062        if (server->attr_bitmask[2] & FATTR4_WORD2_MDSTHRESHOLD) {
3063                if (!opendata->f_attr.mdsthreshold) {
3064                        opendata->f_attr.mdsthreshold = pnfs_mdsthreshold_alloc();
3065                        if (!opendata->f_attr.mdsthreshold)
3066                                goto err_free_label;
3067                }
3068                opendata->o_arg.open_bitmap = &nfs4_pnfs_open_bitmap[0];
3069        }
3070        if (d_really_is_positive(dentry))
3071                opendata->state = nfs4_get_open_state(d_inode(dentry), sp);
3072
3073        status = _nfs4_open_and_get_state(opendata, flags, ctx);
3074        if (status != 0)
3075                goto err_free_label;
3076        state = ctx->state;
3077
3078        if ((opendata->o_arg.open_flags & (O_CREAT|O_EXCL)) == (O_CREAT|O_EXCL) &&
3079            (opendata->o_arg.createmode != NFS4_CREATE_GUARDED)) {
3080                unsigned attrs = nfs4_exclusive_attrset(opendata, sattr, &label);
3081                /*
3082                 * send create attributes which was not set by open
3083                 * with an extra setattr.
3084                 */
3085                if (attrs || label) {
3086                        unsigned ia_old = sattr->ia_valid;
3087
3088                        sattr->ia_valid = attrs;
3089                        nfs_fattr_init(opendata->o_res.f_attr);
3090                        status = nfs4_do_setattr(state->inode, cred,
3091                                        opendata->o_res.f_attr, sattr,
3092                                        ctx, label, olabel);
3093                        if (status == 0) {
3094                                nfs_setattr_update_inode(state->inode, sattr,
3095                                                opendata->o_res.f_attr);
3096                                nfs_setsecurity(state->inode, opendata->o_res.f_attr, olabel);
3097                        }
3098                        sattr->ia_valid = ia_old;
3099                }
3100        }
3101        if (opened && opendata->file_created)
3102                *opened = 1;
3103
3104        if (pnfs_use_threshold(ctx_th, opendata->f_attr.mdsthreshold, server)) {
3105                *ctx_th = opendata->f_attr.mdsthreshold;
3106                opendata->f_attr.mdsthreshold = NULL;
3107        }
3108
3109        nfs4_label_free(olabel);
3110
3111        nfs4_opendata_put(opendata);
3112        nfs4_put_state_owner(sp);
3113        return 0;
3114err_free_label:
3115        nfs4_label_free(olabel);
3116err_opendata_put:
3117        nfs4_opendata_put(opendata);
3118err_put_state_owner:
3119        nfs4_put_state_owner(sp);
3120out_err:
3121        return status;
3122}
3123
3124
3125static struct nfs4_state *nfs4_do_open(struct inode *dir,
3126                                        struct nfs_open_context *ctx,
3127                                        int flags,
3128                                        struct iattr *sattr,
3129                                        struct nfs4_label *label,
3130                                        int *opened)
3131{
3132        struct nfs_server *server = NFS_SERVER(dir);
3133        struct nfs4_exception exception = {
3134                .interruptible = true,
3135        };
3136        struct nfs4_state *res;
3137        struct nfs4_open_createattrs c = {
3138                .label = label,
3139                .sattr = sattr,
3140                .verf = {
3141                        [0] = (__u32)jiffies,
3142                        [1] = (__u32)current->pid,
3143                },
3144        };
3145        int status;
3146
3147        do {
3148                status = _nfs4_do_open(dir, ctx, flags, &c, opened);
3149                res = ctx->state;
3150                trace_nfs4_open_file(ctx, flags, status);
3151                if (status == 0)
3152                        break;
3153                /* NOTE: BAD_SEQID means the server and client disagree about the
3154                 * book-keeping w.r.t. state-changing operations
3155                 * (OPEN/CLOSE/LOCK/LOCKU...)
3156                 * It is actually a sign of a bug on the client or on the server.
3157                 *
3158                 * If we receive a BAD_SEQID error in the particular case of
3159                 * doing an OPEN, we assume that nfs_increment_open_seqid() will
3160                 * have unhashed the old state_owner for us, and that we can
3161                 * therefore safely retry using a new one. We should still warn
3162                 * the user though...
3163                 */
3164                if (status == -NFS4ERR_BAD_SEQID) {
3165                        pr_warn_ratelimited("NFS: v4 server %s "
3166                                        " returned a bad sequence-id error!\n",
3167                                        NFS_SERVER(dir)->nfs_client->cl_hostname);
3168                        exception.retry = 1;
3169                        continue;
3170                }
3171                /*
3172                 * BAD_STATEID on OPEN means that the server cancelled our
3173                 * state before it received the OPEN_CONFIRM.
3174                 * Recover by retrying the request as per the discussion
3175                 * on Page 181 of RFC3530.
3176                 */
3177                if (status == -NFS4ERR_BAD_STATEID) {
3178                        exception.retry = 1;
3179                        continue;
3180                }
3181                if (status == -EAGAIN) {
3182                        /* We must have found a delegation */
3183                        exception.retry = 1;
3184                        continue;
3185                }
3186                if (nfs4_clear_cap_atomic_open_v1(server, status, &exception))
3187                        continue;
3188                res = ERR_PTR(nfs4_handle_exception(server,
3189                                        status, &exception));
3190        } while (exception.retry);
3191        return res;
3192}
3193
3194static int _nfs4_do_setattr(struct inode *inode,
3195                            struct nfs_setattrargs *arg,
3196                            struct nfs_setattrres *res,
3197                            const struct cred *cred,
3198                            struct nfs_open_context *ctx)
3199{
3200        struct nfs_server *server = NFS_SERVER(inode);
3201        struct rpc_message msg = {
3202                .rpc_proc       = &nfs4_procedures[NFSPROC4_CLNT_SETATTR],
3203                .rpc_argp       = arg,
3204                .rpc_resp       = res,
3205                .rpc_cred       = cred,
3206        };
3207        const struct cred *delegation_cred = NULL;
3208        unsigned long timestamp = jiffies;
3209        bool truncate;
3210        int status;
3211
3212        nfs_fattr_init(res->fattr);
3213
3214        /* Servers should only apply open mode checks for file size changes */
3215        truncate = (arg->iap->ia_valid & ATTR_SIZE) ? true : false;
3216        if (!truncate)
3217                goto zero_stateid;
3218
3219        if (nfs4_copy_delegation_stateid(inode, FMODE_WRITE, &arg->stateid, &delegation_cred)) {
3220                /* Use that stateid */
3221        } else if (ctx != NULL && ctx->state) {
3222                struct nfs_lock_context *l_ctx;
3223                if (!nfs4_valid_open_stateid(ctx->state))
3224                        return -EBADF;
3225                l_ctx = nfs_get_lock_context(ctx);
3226                if (IS_ERR(l_ctx))
3227                        return PTR_ERR(l_ctx);
3228                status = nfs4_select_rw_stateid(ctx->state, FMODE_WRITE, l_ctx,
3229                                                &arg->stateid, &delegation_cred);
3230                nfs_put_lock_context(l_ctx);
3231                if (status == -EIO)
3232                        return -EBADF;
3233        } else {
3234zero_stateid:
3235                nfs4_stateid_copy(&arg->stateid, &zero_stateid);
3236        }
3237        if (delegation_cred)
3238                msg.rpc_cred = delegation_cred;
3239
3240        status = nfs4_call_sync(server->client, server, &msg, &arg->seq_args, &res->seq_res, 1);
3241
3242        put_cred(delegation_cred);
3243        if (status == 0 && ctx != NULL)
3244                renew_lease(server, timestamp);
3245        trace_nfs4_setattr(inode, &arg->stateid, status);
3246        return status;
3247}
3248
3249static int nfs4_do_setattr(struct inode *inode, const struct cred *cred,
3250                           struct nfs_fattr *fattr, struct iattr *sattr,
3251                           struct nfs_open_context *ctx, struct nfs4_label *ilabel,
3252                           struct nfs4_label *olabel)
3253{
3254        struct nfs_server *server = NFS_SERVER(inode);
3255        __u32 bitmask[NFS4_BITMASK_SZ];
3256        struct nfs4_state *state = ctx ? ctx->state : NULL;
3257        struct nfs_setattrargs  arg = {
3258                .fh             = NFS_FH(inode),
3259                .iap            = sattr,
3260                .server         = server,
3261                .bitmask = bitmask,
3262                .label          = ilabel,
3263        };
3264        struct nfs_setattrres  res = {
3265                .fattr          = fattr,
3266                .label          = olabel,
3267                .server         = server,
3268        };
3269        struct nfs4_exception exception = {
3270                .state = state,
3271                .inode = inode,
3272                .stateid = &arg.stateid,
3273        };
3274        int err;
3275
3276        do {
3277                nfs4_bitmap_copy_adjust_setattr(bitmask,
3278                                nfs4_bitmask(server, olabel),
3279                                inode);
3280
3281                err = _nfs4_do_setattr(inode, &arg, &res, cred, ctx);
3282                switch (err) {
3283                case -NFS4ERR_OPENMODE:
3284                        if (!(sattr->ia_valid & ATTR_SIZE)) {
3285                                pr_warn_once("NFSv4: server %s is incorrectly "
3286                                                "applying open mode checks to "
3287                                                "a SETATTR that is not "
3288                                                "changing file size.\n",
3289                                                server->nfs_client->cl_hostname);
3290                        }
3291                        if (state && !(state->state & FMODE_WRITE)) {
3292                                err = -EBADF;
3293                                if (sattr->ia_valid & ATTR_OPEN)
3294                                        err = -EACCES;
3295                                goto out;
3296                        }
3297                }
3298                err = nfs4_handle_exception(server, err, &exception);
3299        } while (exception.retry);
3300out:
3301        return err;
3302}
3303
3304static bool
3305nfs4_wait_on_layoutreturn(struct inode *inode, struct rpc_task *task)
3306{
3307        if (inode == NULL || !nfs_have_layout(inode))
3308                return false;
3309
3310        return pnfs_wait_on_layoutreturn(inode, task);
3311}
3312
3313/*
3314 * Update the seqid of an open stateid
3315 */
3316static void nfs4_sync_open_stateid(nfs4_stateid *dst,
3317                struct nfs4_state *state)
3318{
3319        __be32 seqid_open;
3320        u32 dst_seqid;
3321        int seq;
3322
3323        for (;;) {
3324                if (!nfs4_valid_open_stateid(state))
3325                        break;
3326                seq = read_seqbegin(&state->seqlock);
3327                if (!nfs4_state_match_open_stateid_other(state, dst)) {
3328                        nfs4_stateid_copy(dst, &state->open_stateid);
3329                        if (read_seqretry(&state->seqlock, seq))
3330                                continue;
3331                        break;
3332                }
3333                seqid_open = state->open_stateid.seqid;
3334                if (read_seqretry(&state->seqlock, seq))
3335                        continue;
3336
3337                dst_seqid = be32_to_cpu(dst->seqid);
3338                if ((s32)(dst_seqid - be32_to_cpu(seqid_open)) < 0)
3339                        dst->seqid = seqid_open;
3340                break;
3341        }
3342}
3343
3344/*
3345 * Update the seqid of an open stateid after receiving
3346 * NFS4ERR_OLD_STATEID
3347 */
3348static bool nfs4_refresh_open_old_stateid(nfs4_stateid *dst,
3349                struct nfs4_state *state)
3350{
3351        __be32 seqid_open;
3352        u32 dst_seqid;
3353        bool ret;
3354        int seq;
3355
3356        for (;;) {
3357                ret = false;
3358                if (!nfs4_valid_open_stateid(state))
3359                        break;
3360                seq = read_seqbegin(&state->seqlock);
3361                if (!nfs4_state_match_open_stateid_other(state, dst)) {
3362                        if (read_seqretry(&state->seqlock, seq))
3363                                continue;
3364                        break;
3365                }
3366                seqid_open = state->open_stateid.seqid;
3367                if (read_seqretry(&state->seqlock, seq))
3368                        continue;
3369
3370                dst_seqid = be32_to_cpu(dst->seqid);
3371                if ((s32)(dst_seqid - be32_to_cpu(seqid_open)) >= 0)
3372                        dst->seqid = cpu_to_be32(dst_seqid + 1);
3373                else
3374                        dst->seqid = seqid_open;
3375                ret = true;
3376                break;
3377        }
3378
3379        return ret;
3380}
3381
3382struct nfs4_closedata {
3383        struct inode *inode;
3384        struct nfs4_state *state;
3385        struct nfs_closeargs arg;
3386        struct nfs_closeres res;
3387        struct {
3388                struct nfs4_layoutreturn_args arg;
3389                struct nfs4_layoutreturn_res res;
3390                struct nfs4_xdr_opaque_data ld_private;
3391                u32 roc_barrier;
3392                bool roc;
3393        } lr;
3394        struct nfs_fattr fattr;
3395        unsigned long timestamp;
3396};
3397
3398static void nfs4_free_closedata(void *data)
3399{
3400        struct nfs4_closedata *calldata = data;
3401        struct nfs4_state_owner *sp = calldata->state->owner;
3402        struct super_block *sb = calldata->state->inode->i_sb;
3403
3404        if (calldata->lr.roc)
3405                pnfs_roc_release(&calldata->lr.arg, &calldata->lr.res,
3406                                calldata->res.lr_ret);
3407        nfs4_put_open_state(calldata->state);
3408        nfs_free_seqid(calldata->arg.seqid);
3409        nfs4_put_state_owner(sp);
3410        nfs_sb_deactive(sb);
3411        kfree(calldata);
3412}
3413
3414static void nfs4_close_done(struct rpc_task *task, void *data)
3415{
3416        struct nfs4_closedata *calldata = data;
3417        struct nfs4_state *state = calldata->state;
3418        struct nfs_server *server = NFS_SERVER(calldata->inode);
3419        nfs4_stateid *res_stateid = NULL;
3420        struct nfs4_exception exception = {
3421                .state = state,
3422                .inode = calldata->inode,
3423                .stateid = &calldata->arg.stateid,
3424        };
3425
3426        dprintk("%s: begin!\n", __func__);
3427        if (!nfs4_sequence_done(task, &calldata->res.seq_res))
3428                return;
3429        trace_nfs4_close(state, &calldata->arg, &calldata->res, task->tk_status);
3430
3431        /* Handle Layoutreturn errors */
3432        if (pnfs_roc_done(task, calldata->inode,
3433                                &calldata->arg.lr_args,
3434                                &calldata->res.lr_res,
3435                                &calldata->res.lr_ret) == -EAGAIN)
3436                goto out_restart;
3437
3438        /* hmm. we are done with the inode, and in the process of freeing
3439         * the state_owner. we keep this around to process errors
3440         */
3441        switch (task->tk_status) {
3442                case 0:
3443                        res_stateid = &calldata->res.stateid;
3444                        renew_lease(server, calldata->timestamp);
3445                        break;
3446                case -NFS4ERR_ACCESS:
3447                        if (calldata->arg.bitmask != NULL) {
3448                                calldata->arg.bitmask = NULL;
3449                                calldata->res.fattr = NULL;
3450                                goto out_restart;
3451
3452                        }
3453                        break;
3454                case -NFS4ERR_OLD_STATEID:
3455                        /* Did we race with OPEN? */
3456                        if (nfs4_refresh_open_old_stateid(&calldata->arg.stateid,
3457                                                state))
3458                                goto out_restart;
3459                        goto out_release;
3460                case -NFS4ERR_ADMIN_REVOKED:
3461                case -NFS4ERR_STALE_STATEID:
3462                case -NFS4ERR_EXPIRED:
3463                        nfs4_free_revoked_stateid(server,
3464                                        &calldata->arg.stateid,
3465                                        task->tk_msg.rpc_cred);
3466                        /* Fallthrough */
3467                case -NFS4ERR_BAD_STATEID:
3468                        if (calldata->arg.fmode == 0)
3469                                break;
3470                        /* Fallthrough */
3471                default:
3472                        task->tk_status = nfs4_async_handle_exception(task,
3473                                        server, task->tk_status, &exception);
3474                        if (exception.retry)
3475                                goto out_restart;
3476        }
3477        nfs_clear_open_stateid(state, &calldata->arg.stateid,
3478                        res_stateid, calldata->arg.fmode);
3479out_release:
3480        task->tk_status = 0;
3481        nfs_release_seqid(calldata->arg.seqid);
3482        nfs_refresh_inode(calldata->inode, &calldata->fattr);
3483        dprintk("%s: done, ret = %d!\n", __func__, task->tk_status);
3484        return;
3485out_restart:
3486        task->tk_status = 0;
3487        rpc_restart_call_prepare(task);
3488        goto out_release;
3489}
3490
3491static void nfs4_close_prepare(struct rpc_task *task, void *data)
3492{
3493        struct nfs4_closedata *calldata = data;
3494        struct nfs4_state *state = calldata->state;
3495        struct inode *inode = calldata->inode;
3496        struct pnfs_layout_hdr *lo;
3497        bool is_rdonly, is_wronly, is_rdwr;
3498        int call_close = 0;
3499
3500        dprintk("%s: begin!\n", __func__);
3501        if (nfs_wait_on_sequence(calldata->arg.seqid, task) != 0)
3502                goto out_wait;
3503
3504        task->tk_msg.rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_OPEN_DOWNGRADE];
3505        spin_lock(&state->owner->so_lock);
3506        is_rdwr = test_bit(NFS_O_RDWR_STATE, &state->flags);
3507        is_rdonly = test_bit(NFS_O_RDONLY_STATE, &state->flags);
3508        is_wronly = test_bit(NFS_O_WRONLY_STATE, &state->flags);
3509        /* Calculate the change in open mode */
3510        calldata->arg.fmode = 0;
3511        if (state->n_rdwr == 0) {
3512                if (state->n_rdonly == 0)
3513                        call_close |= is_rdonly;
3514                else if (is_rdonly)
3515                        calldata->arg.fmode |= FMODE_READ;
3516                if (state->n_wronly == 0)
3517                        call_close |= is_wronly;
3518                else if (is_wronly)
3519                        calldata->arg.fmode |= FMODE_WRITE;
3520                if (calldata->arg.fmode != (FMODE_READ|FMODE_WRITE))
3521                        call_close |= is_rdwr;
3522        } else if (is_rdwr)
3523                calldata->arg.fmode |= FMODE_READ|FMODE_WRITE;
3524
3525        nfs4_sync_open_stateid(&calldata->arg.stateid, state);
3526        if (!nfs4_valid_open_stateid(state))
3527                call_close = 0;
3528        spin_unlock(&state->owner->so_lock);
3529
3530        if (!call_close) {
3531                /* Note: exit _without_ calling nfs4_close_done */
3532                goto out_no_action;
3533        }
3534
3535        if (!calldata->lr.roc && nfs4_wait_on_layoutreturn(inode, task)) {
3536                nfs_release_seqid(calldata->arg.seqid);
3537                goto out_wait;
3538        }
3539
3540        lo = calldata->arg.lr_args ? calldata->arg.lr_args->layout : NULL;
3541        if (lo && !pnfs_layout_is_valid(lo)) {
3542                calldata->arg.lr_args = NULL;
3543                calldata->res.lr_res = NULL;
3544        }
3545
3546        if (calldata->arg.fmode == 0)
3547                task->tk_msg.rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_CLOSE];
3548
3549        if (calldata->arg.fmode == 0 || calldata->arg.fmode == FMODE_READ) {
3550                /* Close-to-open cache consistency revalidation */
3551                if (!nfs4_have_delegation(inode, FMODE_READ))
3552                        calldata->arg.bitmask = NFS_SERVER(inode)->cache_consistency_bitmask;
3553                else
3554                        calldata->arg.bitmask = NULL;
3555        }
3556
3557        calldata->arg.share_access =
3558                nfs4_map_atomic_open_share(NFS_SERVER(inode),
3559                                calldata->arg.fmode, 0);
3560
3561        if (calldata->res.fattr == NULL)
3562                calldata->arg.bitmask = NULL;
3563        else if (calldata->arg.bitmask == NULL)
3564                calldata->res.fattr = NULL;
3565        calldata->timestamp = jiffies;
3566        if (nfs4_setup_sequence(NFS_SERVER(inode)->nfs_client,
3567                                &calldata->arg.seq_args,
3568                                &calldata->res.seq_res,
3569                                task) != 0)
3570                nfs_release_seqid(calldata->arg.seqid);
3571        dprintk("%s: done!\n", __func__);
3572        return;
3573out_no_action:
3574        task->tk_action = NULL;
3575out_wait:
3576        nfs4_sequence_done(task, &calldata->res.seq_res);
3577}
3578
3579static const struct rpc_call_ops nfs4_close_ops = {
3580        .rpc_call_prepare = nfs4_close_prepare,
3581        .rpc_call_done = nfs4_close_done,
3582        .rpc_release = nfs4_free_closedata,
3583};
3584
3585/* 
3586 * It is possible for data to be read/written from a mem-mapped file 
3587 * after the sys_close call (which hits the vfs layer as a flush).
3588 * This means that we can't safely call nfsv4 close on a file until 
3589 * the inode is cleared. This in turn means that we are not good
3590 * NFSv4 citizens - we do not indicate to the server to update the file's 
3591 * share state even when we are done with one of the three share 
3592 * stateid's in the inode.
3593 *
3594 * NOTE: Caller must be holding the sp->so_owner semaphore!
3595 */
3596int nfs4_do_close(struct nfs4_state *state, gfp_t gfp_mask, int wait)
3597{
3598        struct nfs_server *server = NFS_SERVER(state->inode);
3599        struct nfs_seqid *(*alloc_seqid)(struct nfs_seqid_counter *, gfp_t);
3600        struct nfs4_closedata *calldata;
3601        struct nfs4_state_owner *sp = state->owner;
3602        struct rpc_task *task;
3603        struct rpc_message msg = {
3604                .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_CLOSE],
3605                .rpc_cred = state->owner->so_cred,
3606        };
3607        struct rpc_task_setup task_setup_data = {
3608                .rpc_client = server->client,
3609                .rpc_message = &msg,
3610                .callback_ops = &nfs4_close_ops,
3611                .workqueue = nfsiod_workqueue,
3612                .flags = RPC_TASK_ASYNC,
3613        };
3614        int status = -ENOMEM;
3615
3616        nfs4_state_protect(server->nfs_client, NFS_SP4_MACH_CRED_CLEANUP,
3617                &task_setup_data.rpc_client, &msg);
3618
3619        calldata = kzalloc(sizeof(*calldata), gfp_mask);
3620        if (calldata == NULL)
3621                goto out;
3622        nfs4_init_sequence(&calldata->arg.seq_args, &calldata->res.seq_res, 1, 0);
3623        calldata->inode = state->inode;
3624        calldata->state = state;
3625        calldata->arg.fh = NFS_FH(state->inode);
3626        if (!nfs4_copy_open_stateid(&calldata->arg.stateid, state))
3627                goto out_free_calldata;
3628        /* Serialization for the sequence id */
3629        alloc_seqid = server->nfs_client->cl_mvops->alloc_seqid;
3630        calldata->arg.seqid = alloc_seqid(&state->owner->so_seqid, gfp_mask);
3631        if (IS_ERR(calldata->arg.seqid))
3632                goto out_free_calldata;
3633        nfs_fattr_init(&calldata->fattr);
3634        calldata->arg.fmode = 0;
3635        calldata->lr.arg.ld_private = &calldata->lr.ld_private;
3636        calldata->res.fattr = &calldata->fattr;
3637        calldata->res.seqid = calldata->arg.seqid;
3638        calldata->res.server = server;
3639        calldata->res.lr_ret = -NFS4ERR_NOMATCHING_LAYOUT;
3640        calldata->lr.roc = pnfs_roc(state->inode,
3641                        &calldata->lr.arg, &calldata->lr.res, msg.rpc_cred);
3642        if (calldata->lr.roc) {
3643                calldata->arg.lr_args = &calldata->lr.arg;
3644                calldata->res.lr_res = &calldata->lr.res;
3645        }
3646        nfs_sb_active(calldata->inode->i_sb);
3647
3648        msg.rpc_argp = &calldata->arg;
3649        msg.rpc_resp = &calldata->res;
3650        task_setup_data.callback_data = calldata;
3651        task = rpc_run_task(&task_setup_data);
3652        if (IS_ERR(task))
3653                return PTR_ERR(task);
3654        status = 0;
3655        if (wait)
3656                status = rpc_wait_for_completion_task(task);
3657        rpc_put_task(task);
3658        return status;
3659out_free_calldata:
3660        kfree(calldata);
3661out:
3662        nfs4_put_open_state(state);
3663        nfs4_put_state_owner(sp);
3664        return status;
3665}
3666
3667static struct inode *
3668nfs4_atomic_open(struct inode *dir, struct nfs_open_context *ctx,
3669                int open_flags, struct iattr *attr, int *opened)
3670{
3671        struct nfs4_state *state;
3672        struct nfs4_label l = {0, 0, 0, NULL}, *label = NULL;
3673
3674        label = nfs4_label_init_security(dir, ctx->dentry, attr, &l);
3675
3676        /* Protect against concurrent sillydeletes */
3677        state = nfs4_do_open(dir, ctx, open_flags, attr, label, opened);
3678
3679        nfs4_label_release_security(label);
3680
3681        if (IS_ERR(state))
3682                return ERR_CAST(state);
3683        return state->inode;
3684}
3685
3686static void nfs4_close_context(struct nfs_open_context *ctx, int is_sync)
3687{
3688        if (ctx->state == NULL)
3689                return;
3690        if (is_sync)
3691                nfs4_close_sync(ctx->state, _nfs4_ctx_to_openmode(ctx));
3692        else
3693                nfs4_close_state(ctx->state, _nfs4_ctx_to_openmode(ctx));
3694}
3695
3696#define FATTR4_WORD1_NFS40_MASK (2*FATTR4_WORD1_MOUNTED_ON_FILEID - 1UL)
3697#define FATTR4_WORD2_NFS41_MASK (2*FATTR4_WORD2_SUPPATTR_EXCLCREAT - 1UL)
3698#define FATTR4_WORD2_NFS42_MASK (2*FATTR4_WORD2_MODE_UMASK - 1UL)
3699
3700static int _nfs4_server_capabilities(struct nfs_server *server, struct nfs_fh *fhandle)
3701{
3702        u32 bitmask[3] = {}, minorversion = server->nfs_client->cl_minorversion;
3703        struct nfs4_server_caps_arg args = {
3704                .fhandle = fhandle,
3705                .bitmask = bitmask,
3706        };
3707        struct nfs4_server_caps_res res = {};
3708        struct rpc_message msg = {
3709                .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_SERVER_CAPS],
3710                .rpc_argp = &args,
3711                .rpc_resp = &res,
3712        };
3713        int status;
3714        int i;
3715
3716        bitmask[0] = FATTR4_WORD0_SUPPORTED_ATTRS |
3717                     FATTR4_WORD0_FH_EXPIRE_TYPE |
3718                     FATTR4_WORD0_LINK_SUPPORT |
3719                     FATTR4_WORD0_SYMLINK_SUPPORT |
3720                     FATTR4_WORD0_ACLSUPPORT;
3721        if (minorversion)
3722                bitmask[2] = FATTR4_WORD2_SUPPATTR_EXCLCREAT;
3723
3724        status = nfs4_call_sync(server->client, server, &msg, &args.seq_args, &res.seq_res, 0);
3725        if (status == 0) {
3726                /* Sanity check the server answers */
3727                switch (minorversion) {
3728                case 0:
3729                        res.attr_bitmask[1] &= FATTR4_WORD1_NFS40_MASK;
3730                        res.attr_bitmask[2] = 0;
3731                        break;
3732                case 1:
3733                        res.attr_bitmask[2] &= FATTR4_WORD2_NFS41_MASK;
3734                        break;
3735                case 2:
3736                        res.attr_bitmask[2] &= FATTR4_WORD2_NFS42_MASK;
3737                }
3738                memcpy(server->attr_bitmask, res.attr_bitmask, sizeof(server->attr_bitmask));
3739                server->caps &= ~(NFS_CAP_ACLS|NFS_CAP_HARDLINKS|
3740                                NFS_CAP_SYMLINKS|NFS_CAP_FILEID|
3741                                NFS_CAP_MODE|NFS_CAP_NLINK|NFS_CAP_OWNER|
3742                                NFS_CAP_OWNER_GROUP|NFS_CAP_ATIME|
3743                                NFS_CAP_CTIME|NFS_CAP_MTIME|
3744                                NFS_CAP_SECURITY_LABEL);
3745                if (res.attr_bitmask[0] & FATTR4_WORD0_ACL &&
3746                                res.acl_bitmask & ACL4_SUPPORT_ALLOW_ACL)
3747                        server->caps |= NFS_CAP_ACLS;
3748                if (res.has_links != 0)
3749                        server->caps |= NFS_CAP_HARDLINKS;
3750                if (res.has_symlinks != 0)
3751                        server->caps |= NFS_CAP_SYMLINKS;
3752                if (res.attr_bitmask[0] & FATTR4_WORD0_FILEID)
3753                        server->caps |= NFS_CAP_FILEID;
3754                if (res.attr_bitmask[1] & FATTR4_WORD1_MODE)
3755                        server->caps |= NFS_CAP_MODE;
3756                if (res.attr_bitmask[1] & FATTR4_WORD1_NUMLINKS)
3757                        server->caps |= NFS_CAP_NLINK;
3758                if (res.attr_bitmask[1] & FATTR4_WORD1_OWNER)
3759                        server->caps |= NFS_CAP_OWNER;
3760                if (res.attr_bitmask[1] & FATTR4_WORD1_OWNER_GROUP)
3761                        server->caps |= NFS_CAP_OWNER_GROUP;
3762                if (res.attr_bitmask[1] & FATTR4_WORD1_TIME_ACCESS)
3763                        server->caps |= NFS_CAP_ATIME;
3764                if (res.attr_bitmask[1] & FATTR4_WORD1_TIME_METADATA)
3765                        server->caps |= NFS_CAP_CTIME;
3766                if (res.attr_bitmask[1] & FATTR4_WORD1_TIME_MODIFY)
3767                        server->caps |= NFS_CAP_MTIME;
3768#ifdef CONFIG_NFS_V4_SECURITY_LABEL
3769                if (res.attr_bitmask[2] & FATTR4_WORD2_SECURITY_LABEL)
3770                        server->caps |= NFS_CAP_SECURITY_LABEL;
3771#endif
3772                memcpy(server->attr_bitmask_nl, res.attr_bitmask,
3773                                sizeof(server->attr_bitmask));
3774                server->attr_bitmask_nl[2] &= ~FATTR4_WORD2_SECURITY_LABEL;
3775
3776                memcpy(server->cache_consistency_bitmask, res.attr_bitmask, sizeof(server->cache_consistency_bitmask));
3777                server->cache_consistency_bitmask[0] &= FATTR4_WORD0_CHANGE|FATTR4_WORD0_SIZE;
3778                server->cache_consistency_bitmask[1] &= FATTR4_WORD1_TIME_METADATA|FATTR4_WORD1_TIME_MODIFY;
3779                server->cache_consistency_bitmask[2] = 0;
3780
3781                /* Avoid a regression due to buggy server */
3782                for (i = 0; i < ARRAY_SIZE(res.exclcreat_bitmask); i++)
3783                        res.exclcreat_bitmask[i] &= res.attr_bitmask[i];
3784                memcpy(server->exclcreat_bitmask, res.exclcreat_bitmask,
3785                        sizeof(server->exclcreat_bitmask));
3786
3787                server->acl_bitmask = res.acl_bitmask;
3788                server->fh_expire_type = res.fh_expire_type;
3789        }
3790
3791        return status;
3792}
3793
3794int nfs4_server_capabilities(struct nfs_server *server, struct nfs_fh *fhandle)
3795{
3796        struct nfs4_exception exception = {
3797                .interruptible = true,
3798        };
3799        int err;
3800        do {
3801                err = nfs4_handle_exception(server,
3802                                _nfs4_server_capabilities(server, fhandle),
3803                                &exception);
3804        } while (exception.retry);
3805        return err;
3806}
3807
3808static int _nfs4_lookup_root(struct nfs_server *server, struct nfs_fh *fhandle,
3809                struct nfs_fsinfo *info)
3810{
3811        u32 bitmask[3];
3812        struct nfs4_lookup_root_arg args = {
3813                .bitmask = bitmask,
3814        };
3815        struct nfs4_lookup_res res = {
3816                .server = server,
3817                .fattr = info->fattr,
3818                .fh = fhandle,
3819        };
3820        struct rpc_message msg = {
3821                .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_LOOKUP_ROOT],
3822                .rpc_argp = &args,
3823                .rpc_resp = &res,
3824        };
3825
3826        bitmask[0] = nfs4_fattr_bitmap[0];
3827        bitmask[1] = nfs4_fattr_bitmap[1];
3828        /*
3829         * Process the label in the upcoming getfattr
3830         */
3831        bitmask[2] = nfs4_fattr_bitmap[2] & ~FATTR4_WORD2_SECURITY_LABEL;
3832
3833        nfs_fattr_init(info->fattr);
3834        return nfs4_call_sync(server->client, server, &msg, &args.seq_args, &res.seq_res, 0);
3835}
3836
3837static int nfs4_lookup_root(struct nfs_server *server, struct nfs_fh *fhandle,
3838                struct nfs_fsinfo *info)
3839{
3840        struct nfs4_exception exception = {
3841                .interruptible = true,
3842        };
3843        int err;
3844        do {
3845                err = _nfs4_lookup_root(server, fhandle, info);
3846                trace_nfs4_lookup_root(server, fhandle, info->fattr, err);
3847                switch (err) {
3848                case 0:
3849                case -NFS4ERR_WRONGSEC:
3850                        goto out;
3851                default:
3852                        err = nfs4_handle_exception(server, err, &exception);
3853                }
3854        } while (exception.retry);
3855out:
3856        return err;
3857}
3858
3859static int nfs4_lookup_root_sec(struct nfs_server *server, struct nfs_fh *fhandle,
3860                                struct nfs_fsinfo *info, rpc_authflavor_t flavor)
3861{
3862        struct rpc_auth_create_args auth_args = {
3863                .pseudoflavor = flavor,
3864        };
3865        struct rpc_auth *auth;
3866
3867        auth = rpcauth_create(&auth_args, server->client);
3868        if (IS_ERR(auth))
3869                return -EACCES;
3870        return nfs4_lookup_root(server, fhandle, info);
3871}
3872
3873/*
3874 * Retry pseudoroot lookup with various security flavors.  We do this when:
3875 *
3876 *   NFSv4.0: the PUTROOTFH operation returns NFS4ERR_WRONGSEC
3877 *   NFSv4.1: the server does not support the SECINFO_NO_NAME operation
3878 *
3879 * Returns zero on success, or a negative NFS4ERR value, or a
3880 * negative errno value.
3881 */
3882static int nfs4_find_root_sec(struct nfs_server *server, struct nfs_fh *fhandle,
3883                              struct nfs_fsinfo *info)
3884{
3885        /* Per 3530bis 15.33.5 */
3886        static const rpc_authflavor_t flav_array[] = {
3887                RPC_AUTH_GSS_KRB5P,
3888                RPC_AUTH_GSS_KRB5I,
3889                RPC_AUTH_GSS_KRB5,
3890                RPC_AUTH_UNIX,                  /* courtesy */
3891                RPC_AUTH_NULL,
3892        };
3893        int status = -EPERM;
3894        size_t i;
3895
3896        if (server->auth_info.flavor_len > 0) {
3897                /* try each flavor specified by user */
3898                for (i = 0; i < server->auth_info.flavor_len; i++) {
3899                        status = nfs4_lookup_root_sec(server, fhandle, info,
3900                                                server->auth_info.flavors[i]);
3901                        if (status == -NFS4ERR_WRONGSEC || status == -EACCES)
3902                                continue;
3903                        break;
3904                }
3905        } else {
3906                /* no flavors specified by user, try default list */
3907                for (i = 0; i < ARRAY_SIZE(flav_array); i++) {
3908                        status = nfs4_lookup_root_sec(server, fhandle, info,
3909                                                      flav_array[i]);
3910                        if (status == -NFS4ERR_WRONGSEC || status == -EACCES)
3911                                continue;
3912                        break;
3913                }
3914        }
3915
3916        /*
3917         * -EACCES could mean that the user doesn't have correct permissions
3918         * to access the mount.  It could also mean that we tried to mount
3919         * with a gss auth flavor, but rpc.gssd isn't running.  Either way,
3920         * existing mount programs don't handle -EACCES very well so it should
3921         * be mapped to -EPERM instead.
3922         */
3923        if (status == -EACCES)
3924                status = -EPERM;
3925        return status;
3926}
3927
3928/**
3929 * nfs4_proc_get_rootfh - get file handle for server's pseudoroot
3930 * @server: initialized nfs_server handle
3931 * @fhandle: we fill in the pseudo-fs root file handle
3932 * @info: we fill in an FSINFO struct
3933 * @auth_probe: probe the auth flavours
3934 *
3935 * Returns zero on success, or a negative errno.
3936 */
3937int nfs4_proc_get_rootfh(struct nfs_server *server, struct nfs_fh *fhandle,
3938                         struct nfs_fsinfo *info,
3939                         bool auth_probe)
3940{
3941        int status = 0;
3942
3943        if (!auth_probe)
3944                status = nfs4_lookup_root(server, fhandle, info);
3945
3946        if (auth_probe || status == NFS4ERR_WRONGSEC)
3947                status = server->nfs_client->cl_mvops->find_root_sec(server,
3948                                fhandle, info);
3949
3950        if (status == 0)
3951                status = nfs4_server_capabilities(server, fhandle);
3952        if (status == 0)
3953                status = nfs4_do_fsinfo(server, fhandle, info);
3954
3955        return nfs4_map_errors(status);
3956}
3957
3958static int nfs4_proc_get_root(struct nfs_server *server, struct nfs_fh *mntfh,
3959                              struct nfs_fsinfo *info)
3960{
3961        int error;
3962        struct nfs_fattr *fattr = info->fattr;
3963        struct nfs4_label *label = NULL;
3964
3965        error = nfs4_server_capabilities(server, mntfh);
3966        if (error < 0) {
3967                dprintk("nfs4_get_root: getcaps error = %d\n", -error);
3968                return error;
3969        }
3970
3971        label = nfs4_label_alloc(server, GFP_KERNEL);
3972        if (IS_ERR(label))
3973                return PTR_ERR(label);
3974
3975        error = nfs4_proc_getattr(server, mntfh, fattr, label, NULL);
3976        if (error < 0) {
3977                dprintk("nfs4_get_root: getattr error = %d\n", -error);
3978                goto err_free_label;
3979        }
3980
3981        if (fattr->valid & NFS_ATTR_FATTR_FSID &&
3982            !nfs_fsid_equal(&server->fsid, &fattr->fsid))
3983                memcpy(&server->fsid, &fattr->fsid, sizeof(server->fsid));
3984
3985err_free_label:
3986        nfs4_label_free(label);
3987
3988        return error;
3989}
3990
3991/*
3992 * Get locations and (maybe) other attributes of a referral.
3993 * Note that we'll actually follow the referral later when
3994 * we detect fsid mismatch in inode revalidation
3995 */
3996static int nfs4_get_referral(struct rpc_clnt *client, struct inode *dir,
3997                             const struct qstr *name, struct nfs_fattr *fattr,
3998                             struct nfs_fh *fhandle)
3999{
4000        int status = -ENOMEM;
4001        struct page *page = NULL;
4002        struct nfs4_fs_locations *locations = NULL;
4003
4004        page = alloc_page(GFP_KERNEL);
4005        if (page == NULL)
4006                goto out;
4007        locations = kmalloc(sizeof(struct nfs4_fs_locations), GFP_KERNEL);
4008        if (locations == NULL)
4009                goto out;
4010
4011        status = nfs4_proc_fs_locations(client, dir, name, locations, page);
4012        if (status != 0)
4013                goto out;
4014
4015        /*
4016         * If the fsid didn't change, this is a migration event, not a
4017         * referral.  Cause us to drop into the exception handler, which
4018         * will kick off migration recovery.
4019         */
4020        if (nfs_fsid_equal(&NFS_SERVER(dir)->fsid, &locations->fattr.fsid)) {
4021                dprintk("%s: server did not return a different fsid for"
4022                        " a referral at %s\n", __func__, name->name);
4023                status = -NFS4ERR_MOVED;
4024                goto out;
4025        }
4026        /* Fixup attributes for the nfs_lookup() call to nfs_fhget() */
4027        nfs_fixup_referral_attributes(&locations->fattr);
4028
4029        /* replace the lookup nfs_fattr with the locations nfs_fattr */
4030        memcpy(fattr, &locations->fattr, sizeof(struct nfs_fattr));
4031        memset(fhandle, 0, sizeof(struct nfs_fh));
4032out:
4033        if (page)
4034                __free_page(page);
4035        kfree(locations);
4036        return status;
4037}
4038
4039static int _nfs4_proc_getattr(struct nfs_server *server, struct nfs_fh *fhandle,
4040                                struct nfs_fattr *fattr, struct nfs4_label *label,
4041                                struct inode *inode)
4042{
4043        __u32 bitmask[NFS4_BITMASK_SZ];
4044        struct nfs4_getattr_arg args = {
4045                .fh = fhandle,
4046                .bitmask = bitmask,
4047        };
4048        struct nfs4_getattr_res res = {
4049                .fattr = fattr,
4050                .label = label,
4051                .server = server,
4052        };
4053        struct rpc_message msg = {
4054                .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_GETATTR],
4055                .rpc_argp = &args,
4056                .rpc_resp = &res,
4057        };
4058
4059        nfs4_bitmap_copy_adjust(bitmask, nfs4_bitmask(server, label), inode);
4060
4061        nfs_fattr_init(fattr);
4062        return nfs4_call_sync(server->client, server, &msg, &args.seq_args, &res.seq_res, 0);
4063}
4064
4065static int nfs4_proc_getattr(struct nfs_server *server, struct nfs_fh *fhandle,
4066                                struct nfs_fattr *fattr, struct nfs4_label *label,
4067                                struct inode *inode)
4068{
4069        struct nfs4_exception exception = {
4070                .interruptible = true,
4071        };
4072        int err;
4073        do {
4074                err = _nfs4_proc_getattr(server, fhandle, fattr, label, inode);
4075                trace_nfs4_getattr(server, fhandle, fattr, err);
4076                err = nfs4_handle_exception(server, err,
4077                                &exception);
4078        } while (exception.retry);
4079        return err;
4080}
4081
4082/* 
4083 * The file is not closed if it is opened due to the a request to change
4084 * the size of the file. The open call will not be needed once the
4085 * VFS layer lookup-intents are implemented.
4086 *
4087 * Close is called when the inode is destroyed.
4088 * If we haven't opened the file for O_WRONLY, we
4089 * need to in the size_change case to obtain a stateid.
4090 *
4091 * Got race?
4092 * Because OPEN is always done by name in nfsv4, it is
4093 * possible that we opened a different file by the same
4094 * name.  We can recognize this race condition, but we
4095 * can't do anything about it besides returning an error.
4096 *
4097 * This will be fixed with VFS changes (lookup-intent).
4098 */
4099static int
4100nfs4_proc_setattr(struct dentry *dentry, struct nfs_fattr *fattr,
4101                  struct iattr *sattr)
4102{
4103        struct inode *inode = d_inode(dentry);
4104        const struct cred *cred = NULL;
4105        struct nfs_open_context *ctx = NULL;
4106        struct nfs4_label *label = NULL;
4107        int status;
4108
4109        if (pnfs_ld_layoutret_on_setattr(inode) &&
4110            sattr->ia_valid & ATTR_SIZE &&
4111            sattr->ia_size < i_size_read(inode))
4112                pnfs_commit_and_return_layout(inode);
4113
4114        nfs_fattr_init(fattr);
4115        
4116        /* Deal with open(O_TRUNC) */
4117        if (sattr->ia_valid & ATTR_OPEN)
4118                sattr->ia_valid &= ~(ATTR_MTIME|ATTR_CTIME);
4119
4120        /* Optimization: if the end result is no change, don't RPC */
4121        if ((sattr->ia_valid & ~(ATTR_FILE|ATTR_OPEN)) == 0)
4122                return 0;
4123
4124        /* Search for an existing open(O_WRITE) file */
4125        if (sattr->ia_valid & ATTR_FILE) {
4126
4127                ctx = nfs_file_open_context(sattr->ia_file);
4128                if (ctx)
4129                        cred = ctx->cred;
4130        }
4131
4132        label = nfs4_label_alloc(NFS_SERVER(inode), GFP_KERNEL);
4133        if (IS_ERR(label))
4134                return PTR_ERR(label);
4135
4136        /* Return any delegations if we're going to change ACLs */
4137        if ((sattr->ia_valid & (ATTR_MODE|ATTR_UID|ATTR_GID)) != 0)
4138                nfs4_inode_make_writeable(inode);
4139
4140        status = nfs4_do_setattr(inode, cred, fattr, sattr, ctx, NULL, label);
4141        if (status == 0) {
4142                nfs_setattr_update_inode(inode, sattr, fattr);
4143                nfs_setsecurity(inode, fattr, label);
4144        }
4145        nfs4_label_free(label);
4146        return status;
4147}
4148
4149static int _nfs4_proc_lookup(struct rpc_clnt *clnt, struct inode *dir,
4150                const struct qstr *name, struct nfs_fh *fhandle,
4151                struct nfs_fattr *fattr, struct nfs4_label *label)
4152{
4153        struct nfs_server *server = NFS_SERVER(dir);
4154        int                    status;
4155        struct nfs4_lookup_arg args = {
4156                .bitmask = server->attr_bitmask,
4157                .dir_fh = NFS_FH(dir),
4158                .name = name,
4159        };
4160        struct nfs4_lookup_res res = {
4161                .server = server,
4162                .fattr = fattr,
4163                .label = label,
4164                .fh = fhandle,
4165        };
4166        struct rpc_message msg = {
4167                .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_LOOKUP],
4168                .rpc_argp = &args,
4169                .rpc_resp = &res,
4170        };
4171
4172        args.bitmask = nfs4_bitmask(server, label);
4173
4174        nfs_fattr_init(fattr);
4175
4176        dprintk("NFS call  lookup %s\n", name->name);
4177        status = nfs4_call_sync(clnt, server, &msg, &args.seq_args, &res.seq_res, 0);
4178        dprintk("NFS reply lookup: %d\n", status);
4179        return status;
4180}
4181
4182static void nfs_fixup_secinfo_attributes(struct nfs_fattr *fattr)
4183{
4184        fattr->valid |= NFS_ATTR_FATTR_TYPE | NFS_ATTR_FATTR_MODE |
4185                NFS_ATTR_FATTR_NLINK | NFS_ATTR_FATTR_MOUNTPOINT;
4186        fattr->mode = S_IFDIR | S_IRUGO | S_IXUGO;
4187        fattr->nlink = 2;
4188}
4189
4190static int nfs4_proc_lookup_common(struct rpc_clnt **clnt, struct inode *dir,
4191                                   const struct qstr *name, struct nfs_fh *fhandle,
4192                                   struct nfs_fattr *fattr, struct nfs4_label *label)
4193{
4194        struct nfs4_exception exception = {
4195                .interruptible = true,
4196        };
4197        struct rpc_clnt *client = *clnt;
4198        int err;
4199        do {
4200                err = _nfs4_proc_lookup(client, dir, name, fhandle, fattr, label);
4201                trace_nfs4_lookup(dir, name, err);
4202                switch (err) {
4203                case -NFS4ERR_BADNAME:
4204                        err = -ENOENT;
4205                        goto out;
4206                case -NFS4ERR_MOVED:
4207                        err = nfs4_get_referral(client, dir, name, fattr, fhandle);
4208                        if (err == -NFS4ERR_MOVED)
4209                                err = nfs4_handle_exception(NFS_SERVER(dir), err, &exception);
4210                        goto out;
4211                case -NFS4ERR_WRONGSEC:
4212                        err = -EPERM;
4213                        if (client != *clnt)
4214                                goto out;
4215                        client = nfs4_negotiate_security(client, dir, name);
4216                        if (IS_ERR(client))
4217                                return PTR_ERR(client);
4218
4219                        exception.retry = 1;
4220                        break;
4221                default:
4222                        err = nfs4_handle_exception(NFS_SERVER(dir), err, &exception);
4223                }
4224        } while (exception.retry);
4225
4226out:
4227        if (err == 0)
4228                *clnt = client;
4229        else if (client != *clnt)
4230                rpc_shutdown_client(client);
4231
4232        return err;
4233}
4234
4235static int nfs4_proc_lookup(struct inode *dir, const struct qstr *name,
4236                            struct nfs_fh *fhandle, struct nfs_fattr *fattr,
4237                            struct nfs4_label *label)
4238{
4239        int status;
4240        struct rpc_clnt *client = NFS_CLIENT(dir);
4241
4242        status = nfs4_proc_lookup_common(&client, dir, name, fhandle, fattr, label);
4243        if (client != NFS_CLIENT(dir)) {
4244                rpc_shutdown_client(client);
4245                nfs_fixup_secinfo_attributes(fattr);
4246        }
4247        return status;
4248}
4249
4250struct rpc_clnt *
4251nfs4_proc_lookup_mountpoint(struct inode *dir, const struct qstr *name,
4252                            struct nfs_fh *fhandle, struct nfs_fattr *fattr)
4253{
4254        struct rpc_clnt *client = NFS_CLIENT(dir);
4255        int status;
4256
4257        status = nfs4_proc_lookup_common(&client, dir, name, fhandle, fattr, NULL);
4258        if (status < 0)
4259                return ERR_PTR(status);
4260        return (client == NFS_CLIENT(dir)) ? rpc_clone_client(client) : client;
4261}
4262
4263static int _nfs4_proc_lookupp(struct inode *inode,
4264                struct nfs_fh *fhandle, struct nfs_fattr *fattr,
4265                struct nfs4_label *label)
4266{
4267        struct rpc_clnt *clnt = NFS_CLIENT(inode);
4268        struct nfs_server *server = NFS_SERVER(inode);
4269        int                    status;
4270        struct nfs4_lookupp_arg args = {
4271                .bitmask = server->attr_bitmask,
4272                .fh = NFS_FH(inode),
4273        };
4274        struct nfs4_lookupp_res res = {
4275                .server = server,
4276                .fattr = fattr,
4277                .label = label,
4278                .fh = fhandle,
4279        };
4280        struct rpc_message msg = {
4281                .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_LOOKUPP],
4282                .rpc_argp = &args,
4283                .rpc_resp = &res,
4284        };
4285
4286        args.bitmask = nfs4_bitmask(server, label);
4287
4288        nfs_fattr_init(fattr);
4289
4290        dprintk("NFS call  lookupp ino=0x%lx\n", inode->i_ino);
4291        status = nfs4_call_sync(clnt, server, &msg, &args.seq_args,
4292                                &res.seq_res, 0);
4293        dprintk("NFS reply lookupp: %d\n", status);
4294        return status;
4295}
4296
4297static int nfs4_proc_lookupp(struct inode *inode, struct nfs_fh *fhandle,
4298                             struct nfs_fattr *fattr, struct nfs4_label *label)
4299{
4300        struct nfs4_exception exception = {
4301                .interruptible = true,
4302        };
4303        int err;
4304        do {
4305                err = _nfs4_proc_lookupp(inode, fhandle, fattr, label);
4306                trace_nfs4_lookupp(inode, err);
4307                err = nfs4_handle_exception(NFS_SERVER(inode), err,
4308                                &exception);
4309        } while (exception.retry);
4310        return err;
4311}
4312
4313static int _nfs4_proc_access(struct inode *inode, struct nfs_access_entry *entry)
4314{
4315        struct nfs_server *server = NFS_SERVER(inode);
4316        struct nfs4_accessargs args = {
4317                .fh = NFS_FH(inode),
4318                .access = entry->mask,
4319        };
4320        struct nfs4_accessres res = {
4321                .server = server,
4322        };
4323        struct rpc_message msg = {
4324                .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_ACCESS],
4325                .rpc_argp = &args,
4326                .rpc_resp = &res,
4327                .rpc_cred = entry->cred,
4328        };
4329        int status = 0;
4330
4331        if (!nfs4_have_delegation(inode, FMODE_READ)) {
4332                res.fattr = nfs_alloc_fattr();
4333                if (res.fattr == NULL)
4334                        return -ENOMEM;
4335                args.bitmask = server->cache_consistency_bitmask;
4336        }
4337        status = nfs4_call_sync(server->client, server, &msg, &args.seq_args, &res.seq_res, 0);
4338        if (!status) {
4339                nfs_access_set_mask(entry, res.access);
4340                if (res.fattr)
4341                        nfs_refresh_inode(inode, res.fattr);
4342        }
4343        nfs_free_fattr(res.fattr);
4344        return status;
4345}
4346
4347static int nfs4_proc_access(struct inode *inode, struct nfs_access_entry *entry)
4348{
4349        struct nfs4_exception exception = {
4350                .interruptible = true,
4351        };
4352        int err;
4353        do {
4354                err = _nfs4_proc_access(inode, entry);
4355                trace_nfs4_access(inode, err);
4356                err = nfs4_handle_exception(NFS_SERVER(inode), err,
4357                                &exception);
4358        } while (exception.retry);
4359        return err;
4360}
4361
4362/*
4363 * TODO: For the time being, we don't try to get any attributes
4364 * along with any of the zero-copy operations READ, READDIR,
4365 * READLINK, WRITE.
4366 *
4367 * In the case of the first three, we want to put the GETATTR
4368 * after the read-type operation -- this is because it is hard
4369 * to predict the length of a GETATTR response in v4, and thus
4370 * align the READ data correctly.  This means that the GETATTR
4371 * may end up partially falling into the page cache, and we should
4372 * shift it into the 'tail' of the xdr_buf before processing.
4373 * To do this efficiently, we need to know the total length
4374 * of data received, which doesn't seem to be available outside
4375 * of the RPC layer.
4376 *
4377 * In the case of WRITE, we also want to put the GETATTR after
4378 * the operation -- in this case because we want to make sure
4379 * we get the post-operation mtime and size.
4380 *
4381 * Both of these changes to the XDR layer would in fact be quite
4382 * minor, but I decided to leave them for a subsequent patch.
4383 */
4384static int _nfs4_proc_readlink(struct inode *inode, struct page *page,
4385                unsigned int pgbase, unsigned int pglen)
4386{
4387        struct nfs4_readlink args = {
4388                .fh       = NFS_FH(inode),
4389                .pgbase   = pgbase,
4390                .pglen    = pglen,
4391                .pages    = &page,
4392        };
4393        struct nfs4_readlink_res res;
4394        struct rpc_message msg = {
4395                .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_READLINK],
4396                .rpc_argp = &args,
4397                .rpc_resp = &res,
4398        };
4399
4400        return nfs4_call_sync(NFS_SERVER(inode)->client, NFS_SERVER(inode), &msg, &args.seq_args, &res.seq_res, 0);
4401}
4402
4403static int nfs4_proc_readlink(struct inode *inode, struct page *page,
4404                unsigned int pgbase, unsigned int pglen)
4405{
4406        struct nfs4_exception exception = {
4407                .interruptible = true,
4408        };
4409        int err;
4410        do {
4411                err = _nfs4_proc_readlink(inode, page, pgbase, pglen);
4412                trace_nfs4_readlink(inode, err);
4413                err = nfs4_handle_exception(NFS_SERVER(inode), err,
4414                                &exception);
4415        } while (exception.retry);
4416        return err;
4417}
4418
4419/*
4420 * This is just for mknod.  open(O_CREAT) will always do ->open_context().
4421 */
4422static int
4423nfs4_proc_create(struct inode *dir, struct dentry *dentry, struct iattr *sattr,
4424                 int flags)
4425{
4426        struct nfs_server *server = NFS_SERVER(dir);
4427        struct nfs4_label l, *ilabel = NULL;
4428        struct nfs_open_context *ctx;
4429        struct nfs4_state *state;
4430        int status = 0;
4431
4432        ctx = alloc_nfs_open_context(dentry, FMODE_READ, NULL);
4433        if (IS_ERR(ctx))
4434                return PTR_ERR(ctx);
4435
4436        ilabel = nfs4_label_init_security(dir, dentry, sattr, &l);
4437
4438        if (!(server->attr_bitmask[2] & FATTR4_WORD2_MODE_UMASK))
4439                sattr->ia_mode &= ~current_umask();
4440        state = nfs4_do_open(dir, ctx, flags, sattr, ilabel, NULL);
4441        if (IS_ERR(state)) {
4442                status = PTR_ERR(state);
4443                goto out;
4444        }
4445out:
4446        nfs4_label_release_security(ilabel);
4447        put_nfs_open_context(ctx);
4448        return status;
4449}
4450
4451static int
4452_nfs4_proc_remove(struct inode *dir, const struct qstr *name, u32 ftype)
4453{
4454        struct nfs_server *server = NFS_SERVER(dir);
4455        struct nfs_removeargs args = {
4456                .fh = NFS_FH(dir),
4457                .name = *name,
4458        };
4459        struct nfs_removeres res = {
4460                .server = server,
4461        };
4462        struct rpc_message msg = {
4463                .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_REMOVE],
4464                .rpc_argp = &args,
4465                .rpc_resp = &res,
4466        };
4467        unsigned long timestamp = jiffies;
4468        int status;
4469
4470        status = nfs4_call_sync(server->client, server, &msg, &args.seq_args, &res.seq_res, 1);
4471        if (status == 0) {
4472                spin_lock(&dir->i_lock);
4473                update_changeattr_locked(dir, &res.cinfo, timestamp, 0);
4474                /* Removing a directory decrements nlink in the parent */
4475                if (ftype == NF4DIR && dir->i_nlink > 2)
4476                        nfs4_dec_nlink_locked(dir);
4477                spin_unlock(&dir->i_lock);
4478        }
4479        return status;
4480}
4481
4482static int nfs4_proc_remove(struct inode *dir, struct dentry *dentry)
4483{
4484        struct nfs4_exception exception = {
4485                .interruptible = true,
4486        };
4487        struct inode *inode = d_inode(dentry);
4488        int err;
4489
4490        if (inode) {
4491                if (inode->i_nlink == 1)
4492                        nfs4_inode_return_delegation(inode);
4493                else
4494                        nfs4_inode_make_writeable(inode);
4495        }
4496        do {
4497                err = _nfs4_proc_remove(dir, &dentry->d_name, NF4REG);
4498                trace_nfs4_remove(dir, &dentry->d_name, err);
4499                err = nfs4_handle_exception(NFS_SERVER(dir), err,
4500                                &exception);
4501        } while (exception.retry);
4502        return err;
4503}
4504
4505static int nfs4_proc_rmdir(struct inode *dir, const struct qstr *name)
4506{
4507        struct nfs4_exception exception = {
4508                .interruptible = true,
4509        };
4510        int err;
4511
4512        do {
4513                err = _nfs4_proc_remove(dir, name, NF4DIR);
4514                trace_nfs4_remove(dir, name, err);
4515                err = nfs4_handle_exception(NFS_SERVER(dir), err,
4516                                &exception);
4517        } while (exception.retry);
4518        return err;
4519}
4520
4521static void nfs4_proc_unlink_setup(struct rpc_message *msg,
4522                struct dentry *dentry,
4523                struct inode *inode)
4524{
4525        struct nfs_removeargs *args = msg->rpc_argp;
4526        struct nfs_removeres *res = msg->rpc_resp;
4527
4528        res->server = NFS_SB(dentry->d_sb);
4529        msg->rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_REMOVE];
4530        nfs4_init_sequence(&args->seq_args, &res->seq_res, 1, 0);
4531
4532        nfs_fattr_init(res->dir_attr);
4533
4534        if (inode)
4535                nfs4_inode_return_delegation(inode);
4536}
4537
4538static void nfs4_proc_unlink_rpc_prepare(struct rpc_task *task, struct nfs_unlinkdata *data)
4539{
4540        nfs4_setup_sequence(NFS_SB(data->dentry->d_sb)->nfs_client,
4541                        &data->args.seq_args,
4542                        &data->res.seq_res,
4543                        task);
4544}
4545
4546static int nfs4_proc_unlink_done(struct rpc_task *task, struct inode *dir)
4547{
4548        struct nfs_unlinkdata *data = task->tk_calldata;
4549        struct nfs_removeres *res = &data->res;
4550
4551        if (!nfs4_sequence_done(task, &res->seq_res))
4552                return 0;
4553        if (nfs4_async_handle_error(task, res->server, NULL,
4554                                    &data->timeout) == -EAGAIN)
4555                return 0;
4556        if (task->tk_status == 0)
4557                update_changeattr(dir, &res->cinfo,
4558                                res->dir_attr->time_start, 0);
4559        return 1;
4560}
4561
4562static void nfs4_proc_rename_setup(struct rpc_message *msg,
4563                struct dentry *old_dentry,
4564                struct dentry *new_dentry)
4565{
4566        struct nfs_renameargs *arg = msg->rpc_argp;
4567        struct nfs_renameres *res = msg->rpc_resp;
4568        struct inode *old_inode = d_inode(old_dentry);
4569        struct inode *new_inode = d_inode(new_dentry);
4570
4571        if (old_inode)
4572                nfs4_inode_make_writeable(old_inode);
4573        if (new_inode)
4574                nfs4_inode_return_delegation(new_inode);
4575        msg->rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_RENAME];
4576        res->server = NFS_SB(old_dentry->d_sb);
4577        nfs4_init_sequence(&arg->seq_args, &res->seq_res, 1, 0);
4578}
4579
4580static void nfs4_proc_rename_rpc_prepare(struct rpc_task *task, struct nfs_renamedata *data)
4581{
4582        nfs4_setup_sequence(NFS_SERVER(data->old_dir)->nfs_client,
4583                        &data->args.seq_args,
4584                        &data->res.seq_res,
4585                        task);
4586}
4587
4588static int nfs4_proc_rename_done(struct rpc_task *task, struct inode *old_dir,
4589                                 struct inode *new_dir)
4590{
4591        struct nfs_renamedata *data = task->tk_calldata;
4592        struct nfs_renameres *res = &data->res;
4593
4594        if (!nfs4_sequence_done(task, &res->seq_res))
4595                return 0;
4596        if (nfs4_async_handle_error(task, res->server, NULL, &data->timeout) == -EAGAIN)
4597                return 0;
4598
4599        if (task->tk_status == 0) {
4600                if (new_dir != old_dir) {
4601                        /* Note: If we moved a directory, nlink will change */
4602                        update_changeattr(old_dir, &res->old_cinfo,
4603                                        res->old_fattr->time_start,
4604                                        NFS_INO_INVALID_OTHER);
4605                        update_changeattr(new_dir, &res->new_cinfo,
4606                                        res->new_fattr->time_start,
4607                                        NFS_INO_INVALID_OTHER);
4608                } else
4609                        update_changeattr(old_dir, &res->old_cinfo,
4610                                        res->old_fattr->time_start,
4611                                        0);
4612        }
4613        return 1;
4614}
4615
4616static int _nfs4_proc_link(struct inode *inode, struct inode *dir, const struct qstr *name)
4617{
4618        struct nfs_server *server = NFS_SERVER(inode);
4619        __u32 bitmask[NFS4_BITMASK_SZ];
4620        struct nfs4_link_arg arg = {
4621                .fh     = NFS_FH(inode),
4622                .dir_fh = NFS_FH(dir),
4623                .name   = name,
4624                .bitmask = bitmask,
4625        };
4626        struct nfs4_link_res res = {
4627                .server = server,
4628                .label = NULL,
4629        };
4630        struct rpc_message msg = {
4631                .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_LINK],
4632                .rpc_argp = &arg,
4633                .rpc_resp = &res,
4634        };
4635        int status = -ENOMEM;
4636
4637        res.fattr = nfs_alloc_fattr();
4638        if (res.fattr == NULL)
4639                goto out;
4640
4641        res.label = nfs4_label_alloc(server, GFP_KERNEL);
4642        if (IS_ERR(res.label)) {
4643                status = PTR_ERR(res.label);
4644                goto out;
4645        }
4646
4647        nfs4_inode_make_writeable(inode);
4648        nfs4_bitmap_copy_adjust_setattr(bitmask, nfs4_bitmask(server, res.label), inode);
4649
4650        status = nfs4_call_sync(server->client, server, &msg, &arg.seq_args, &res.seq_res, 1);
4651        if (!status) {
4652                update_changeattr(dir, &res.cinfo, res.fattr->time_start, 0);
4653                status = nfs_post_op_update_inode(inode, res.fattr);
4654                if (!status)
4655                        nfs_setsecurity(inode, res.fattr, res.label);
4656        }
4657
4658
4659        nfs4_label_free(res.label);
4660
4661out:
4662        nfs_free_fattr(res.fattr);
4663        return status;
4664}
4665
4666static int nfs4_proc_link(struct inode *inode, struct inode *dir, const struct qstr *name)
4667{
4668        struct nfs4_exception exception = {
4669                .interruptible = true,
4670        };
4671        int err;
4672        do {
4673                err = nfs4_handle_exception(NFS_SERVER(inode),
4674                                _nfs4_proc_link(inode, dir, name),
4675                                &exception);
4676        } while (exception.retry);
4677        return err;
4678}
4679
4680struct nfs4_createdata {
4681        struct rpc_message msg;
4682        struct nfs4_create_arg arg;
4683        struct nfs4_create_res res;
4684        struct nfs_fh fh;
4685        struct nfs_fattr fattr;
4686        struct nfs4_label *label;
4687};
4688
4689static struct nfs4_createdata *nfs4_alloc_createdata(struct inode *dir,
4690                const struct qstr *name, struct iattr *sattr, u32 ftype)
4691{
4692        struct nfs4_createdata *data;
4693
4694        data = kzalloc(sizeof(*data), GFP_KERNEL);
4695        if (data != NULL) {
4696                struct nfs_server *server = NFS_SERVER(dir);
4697
4698                data->label = nfs4_label_alloc(server, GFP_KERNEL);
4699                if (IS_ERR(data->label))
4700                        goto out_free;
4701
4702                data->msg.rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_CREATE];
4703                data->msg.rpc_argp = &data->arg;
4704                data->msg.rpc_resp = &data->res;
4705                data->arg.dir_fh = NFS_FH(dir);
4706                data->arg.server = server;
4707                data->arg.name = name;
4708                data->arg.attrs = sattr;
4709                data->arg.ftype = ftype;
4710                data->arg.bitmask = nfs4_bitmask(server, data->label);
4711                data->arg.umask = current_umask();
4712                data->res.server = server;
4713                data->res.fh = &data->fh;
4714                data->res.fattr = &data->fattr;
4715                data->res.label = data->label;
4716                nfs_fattr_init(data->res.fattr);
4717        }
4718        return data;
4719out_free:
4720        kfree(data);
4721        return NULL;
4722}
4723
4724static int nfs4_do_create(struct inode *dir, struct dentry *dentry, struct nfs4_createdata *data)
4725{
4726        int status = nfs4_call_sync(NFS_SERVER(dir)->client, NFS_SERVER(dir), &data->msg,
4727                                    &data->arg.seq_args, &data->res.seq_res, 1);
4728        if (status == 0) {
4729                spin_lock(&dir->i_lock);
4730                update_changeattr_locked(dir, &data->res.dir_cinfo,
4731                                data->res.fattr->time_start, 0);
4732                /* Creating a directory bumps nlink in the parent */
4733                if (data->arg.ftype == NF4DIR)
4734                        nfs4_inc_nlink_locked(dir);
4735                spin_unlock(&dir->i_lock);
4736                status = nfs_instantiate(dentry, data->res.fh, data->res.fattr, data->res.label);
4737        }
4738        return status;
4739}
4740
4741static void nfs4_free_createdata(struct nfs4_createdata *data)
4742{
4743        nfs4_label_free(data->label);
4744        kfree(data);
4745}
4746
4747static int _nfs4_proc_symlink(struct inode *dir, struct dentry *dentry,
4748                struct page *page, unsigned int len, struct iattr *sattr,
4749                struct nfs4_label *label)
4750{
4751        struct nfs4_createdata *data;
4752        int status = -ENAMETOOLONG;
4753
4754        if (len > NFS4_MAXPATHLEN)
4755                goto out;
4756
4757        status = -ENOMEM;
4758        data = nfs4_alloc_createdata(dir, &dentry->d_name, sattr, NF4LNK);
4759        if (data == NULL)
4760                goto out;
4761
4762        data->msg.rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_SYMLINK];
4763        data->arg.u.symlink.pages = &page;
4764        data->arg.u.symlink.len = len;
4765        data->arg.label = label;
4766        
4767        status = nfs4_do_create(dir, dentry, data);
4768
4769        nfs4_free_createdata(data);
4770out:
4771        return status;
4772}
4773
4774static int nfs4_proc_symlink(struct inode *dir, struct dentry *dentry,
4775                struct page *page, unsigned int len, struct iattr *sattr)
4776{
4777        struct nfs4_exception exception = {
4778                .interruptible = true,
4779        };
4780        struct nfs4_label l, *label = NULL;
4781        int err;
4782
4783        label = nfs4_label_init_security(dir, dentry, sattr, &l);
4784
4785        do {
4786                err = _nfs4_proc_symlink(dir, dentry, page, len, sattr, label);
4787                trace_nfs4_symlink(dir, &dentry->d_name, err);
4788                err = nfs4_handle_exception(NFS_SERVER(dir), err,
4789                                &exception);
4790        } while (exception.retry);
4791
4792        nfs4_label_release_security(label);
4793        return err;
4794}
4795
4796static int _nfs4_proc_mkdir(struct inode *dir, struct dentry *dentry,
4797                struct iattr *sattr, struct nfs4_label *label)
4798{
4799        struct nfs4_createdata *data;
4800        int status = -ENOMEM;
4801
4802        data = nfs4_alloc_createdata(dir, &dentry->d_name, sattr, NF4DIR);
4803        if (data == NULL)
4804                goto out;
4805
4806        data->arg.label = label;
4807        status = nfs4_do_create(dir, dentry, data);
4808
4809        nfs4_free_createdata(data);
4810out:
4811        return status;
4812}
4813
4814static int nfs4_proc_mkdir(struct inode *dir, struct dentry *dentry,
4815                struct iattr *sattr)
4816{
4817        struct nfs_server *server = NFS_SERVER(dir);
4818        struct nfs4_exception exception = {
4819                .interruptible = true,
4820        };
4821        struct nfs4_label l, *label = NULL;
4822        int err;
4823
4824        label = nfs4_label_init_security(dir, dentry, sattr, &l);
4825
4826        if (!(server->attr_bitmask[2] & FATTR4_WORD2_MODE_UMASK))
4827                sattr->ia_mode &= ~current_umask();
4828        do {
4829                err = _nfs4_proc_mkdir(dir, dentry, sattr, label);
4830                trace_nfs4_mkdir(dir, &dentry->d_name, err);
4831                err = nfs4_handle_exception(NFS_SERVER(dir), err,
4832                                &exception);
4833        } while (exception.retry);
4834        nfs4_label_release_security(label);
4835
4836        return err;
4837}
4838
4839static int _nfs4_proc_readdir(struct dentry *dentry, const struct cred *cred,
4840                u64 cookie, struct page **pages, unsigned int count, bool plus)
4841{
4842        struct inode            *dir = d_inode(dentry);
4843        struct nfs4_readdir_arg args = {
4844                .fh = NFS_FH(dir),
4845                .pages = pages,
4846                .pgbase = 0,
4847                .count = count,
4848                .bitmask = NFS_SERVER(d_inode(dentry))->attr_bitmask,
4849                .plus = plus,
4850        };
4851        struct nfs4_readdir_res res;
4852        struct rpc_message msg = {
4853                .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_READDIR],
4854                .rpc_argp = &args,
4855                .rpc_resp = &res,
4856                .rpc_cred = cred,
4857        };
4858        int                     status;
4859
4860        dprintk("%s: dentry = %pd2, cookie = %Lu\n", __func__,
4861                        dentry,
4862                        (unsigned long long)cookie);
4863        nfs4_setup_readdir(cookie, NFS_I(dir)->cookieverf, dentry, &args);
4864        res.pgbase = args.pgbase;
4865        status = nfs4_call_sync(NFS_SERVER(dir)->client, NFS_SERVER(dir), &msg, &args.seq_args, &res.seq_res, 0);
4866        if (status >= 0) {
4867                memcpy(NFS_I(dir)->cookieverf, res.verifier.data, NFS4_VERIFIER_SIZE);
4868                status += args.pgbase;
4869        }
4870
4871        nfs_invalidate_atime(dir);
4872
4873        dprintk("%s: returns %d\n", __func__, status);
4874        return status;
4875}
4876
4877static int nfs4_proc_readdir(struct dentry *dentry, const struct cred *cred,
4878                u64 cookie, struct page **pages, unsigned int count, bool plus)
4879{
4880        struct nfs4_exception exception = {
4881                .interruptible = true,
4882        };
4883        int err;
4884        do {
4885                err = _nfs4_proc_readdir(dentry, cred, cookie,
4886                                pages, count, plus);
4887                trace_nfs4_readdir(d_inode(dentry), err);
4888                err = nfs4_handle_exception(NFS_SERVER(d_inode(dentry)), err,
4889                                &exception);
4890        } while (exception.retry);
4891        return err;
4892}
4893
4894static int _nfs4_proc_mknod(struct inode *dir, struct dentry *dentry,
4895                struct iattr *sattr, struct nfs4_label *label, dev_t rdev)
4896{
4897        struct nfs4_createdata *data;
4898        int mode = sattr->ia_mode;
4899        int status = -ENOMEM;
4900
4901        data = nfs4_alloc_createdata(dir, &dentry->d_name, sattr, NF4SOCK);
4902        if (data == NULL)
4903                goto out;
4904
4905        if (S_ISFIFO(mode))
4906                data->arg.ftype = NF4FIFO;
4907        else if (S_ISBLK(mode)) {
4908                data->arg.ftype = NF4BLK;
4909                data->arg.u.device.specdata1 = MAJOR(rdev);
4910                data->arg.u.device.specdata2 = MINOR(rdev);
4911        }
4912        else if (S_ISCHR(mode)) {
4913                data->arg.ftype = NF4CHR;
4914                data->arg.u.device.specdata1 = MAJOR(rdev);
4915                data->arg.u.device.specdata2 = MINOR(rdev);
4916        } else if (!S_ISSOCK(mode)) {
4917                status = -EINVAL;
4918                goto out_free;
4919        }
4920
4921        data->arg.label = label;
4922        status = nfs4_do_create(dir, dentry, data);
4923out_free:
4924        nfs4_free_createdata(data);
4925out:
4926        return status;
4927}
4928
4929static int nfs4_proc_mknod(struct inode *dir, struct dentry *dentry,
4930                struct iattr *sattr, dev_t rdev)
4931{
4932        struct nfs_server *server = NFS_SERVER(dir);
4933        struct nfs4_exception exception = {
4934                .interruptible = true,
4935        };
4936        struct nfs4_label l, *label = NULL;
4937        int err;
4938
4939        label = nfs4_label_init_security(dir, dentry, sattr, &l);
4940
4941        if (!(server->attr_bitmask[2] & FATTR4_WORD2_MODE_UMASK))
4942                sattr->ia_mode &= ~current_umask();
4943        do {
4944                err = _nfs4_proc_mknod(dir, dentry, sattr, label, rdev);
4945                trace_nfs4_mknod(dir, &dentry->d_name, err);
4946                err = nfs4_handle_exception(NFS_SERVER(dir), err,
4947                                &exception);
4948        } while (exception.retry);
4949
4950        nfs4_label_release_security(label);
4951
4952        return err;
4953}
4954
4955static int _nfs4_proc_statfs(struct nfs_server *server, struct nfs_fh *fhandle,
4956                 struct nfs_fsstat *fsstat)
4957{
4958        struct nfs4_statfs_arg args = {
4959                .fh = fhandle,
4960                .bitmask = server->attr_bitmask,
4961        };
4962        struct nfs4_statfs_res res = {
4963                .fsstat = fsstat,
4964        };
4965        struct rpc_message msg = {
4966                .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_STATFS],
4967                .rpc_argp = &args,
4968                .rpc_resp = &res,
4969        };
4970
4971        nfs_fattr_init(fsstat->fattr);
4972        return  nfs4_call_sync(server->client, server, &msg, &args.seq_args, &res.seq_res, 0);
4973}
4974
4975static int nfs4_proc_statfs(struct nfs_server *server, struct nfs_fh *fhandle, struct nfs_fsstat *fsstat)
4976{
4977        struct nfs4_exception exception = {
4978                .interruptible = true,
4979        };
4980        int err;
4981        do {
4982                err = nfs4_handle_exception(server,
4983                                _nfs4_proc_statfs(server, fhandle, fsstat),
4984                                &exception);
4985        } while (exception.retry);
4986        return err;
4987}
4988
4989static int _nfs4_do_fsinfo(struct nfs_server *server, struct nfs_fh *fhandle,
4990                struct nfs_fsinfo *fsinfo)
4991{
4992        struct nfs4_fsinfo_arg args = {
4993                .fh = fhandle,
4994                .bitmask = server->attr_bitmask,
4995        };
4996        struct nfs4_fsinfo_res res = {
4997                .fsinfo = fsinfo,
4998        };
4999        struct rpc_message msg = {
5000                .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_FSINFO],
5001                .rpc_argp = &args,
5002                .rpc_resp = &res,
5003        };
5004
5005        return nfs4_call_sync(server->client, server, &msg, &args.seq_args, &res.seq_res, 0);
5006}
5007
5008static int nfs4_do_fsinfo(struct nfs_server *server, struct nfs_fh *fhandle, struct nfs_fsinfo *fsinfo)
5009{
5010        struct nfs4_exception exception = {
5011                .interruptible = true,
5012        };
5013        unsigned long now = jiffies;
5014        int err;
5015
5016        do {
5017                err = _nfs4_do_fsinfo(server, fhandle, fsinfo);
5018                trace_nfs4_fsinfo(server, fhandle, fsinfo->fattr, err);
5019                if (err == 0) {
5020                        nfs4_set_lease_period(server->nfs_client,
5021                                        fsinfo->lease_time * HZ,
5022                                        now);
5023                        break;
5024                }
5025                err = nfs4_handle_exception(server, err, &exception);
5026        } while (exception.retry);
5027        return err;
5028}
5029
5030static int nfs4_proc_fsinfo(struct nfs_server *server, struct nfs_fh *fhandle, struct nfs_fsinfo *fsinfo)
5031{
5032        int error;
5033
5034        nfs_fattr_init(fsinfo->fattr);
5035        error = nfs4_do_fsinfo(server, fhandle, fsinfo);
5036        if (error == 0) {
5037                /* block layout checks this! */
5038                server->pnfs_blksize = fsinfo->blksize;
5039                set_pnfs_layoutdriver(server, fhandle, fsinfo);
5040        }
5041
5042        return error;
5043}
5044
5045static int _nfs4_proc_pathconf(struct nfs_server *server, struct nfs_fh *fhandle,
5046                struct nfs_pathconf *pathconf)
5047{
5048        struct nfs4_pathconf_arg args = {
5049                .fh = fhandle,
5050                .bitmask = server->attr_bitmask,
5051        };
5052        struct nfs4_pathconf_res res = {
5053                .pathconf = pathconf,
5054        };
5055        struct rpc_message msg = {
5056                .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_PATHCONF],
5057                .rpc_argp = &args,
5058                .rpc_resp = &res,
5059        };
5060
5061        /* None of the pathconf attributes are mandatory to implement */
5062        if ((args.bitmask[0] & nfs4_pathconf_bitmap[0]) == 0) {
5063                memset(pathconf, 0, sizeof(*pathconf));
5064                return 0;
5065        }
5066
5067        nfs_fattr_init(pathconf->fattr);
5068        return nfs4_call_sync(server->client, server, &msg, &args.seq_args, &res.seq_res, 0);
5069}
5070
5071static int nfs4_proc_pathconf(struct nfs_server *server, struct nfs_fh *fhandle,
5072                struct nfs_pathconf *pathconf)
5073{
5074        struct nfs4_exception exception = {
5075                .interruptible = true,
5076        };
5077        int err;
5078
5079        do {
5080                err = nfs4_handle_exception(server,
5081                                _nfs4_proc_pathconf(server, fhandle, pathconf),
5082                                &exception);
5083        } while (exception.retry);
5084        return err;
5085}
5086
5087int nfs4_set_rw_stateid(nfs4_stateid *stateid,
5088                const struct nfs_open_context *ctx,
5089                const struct nfs_lock_context *l_ctx,
5090                fmode_t fmode)
5091{
5092        return nfs4_select_rw_stateid(ctx->state, fmode, l_ctx, stateid, NULL);
5093}
5094EXPORT_SYMBOL_GPL(nfs4_set_rw_stateid);
5095
5096static bool nfs4_stateid_is_current(nfs4_stateid *stateid,
5097                const struct nfs_open_context *ctx,
5098                const struct nfs_lock_context *l_ctx,
5099                fmode_t fmode)
5100{
5101        nfs4_stateid current_stateid;
5102
5103        /* If the current stateid represents a lost lock, then exit */
5104        if (nfs4_set_rw_stateid(&current_stateid, ctx, l_ctx, fmode) == -EIO)
5105                return true;
5106        return nfs4_stateid_match(stateid, &current_stateid);
5107}
5108
5109static bool nfs4_error_stateid_expired(int err)
5110{
5111        switch (err) {
5112        case -NFS4ERR_DELEG_REVOKED:
5113        case -NFS4ERR_ADMIN_REVOKED:
5114        case -NFS4ERR_BAD_STATEID:
5115        case -NFS4ERR_STALE_STATEID:
5116        case -NFS4ERR_OLD_STATEID:
5117        case -NFS4ERR_OPENMODE:
5118        case -NFS4ERR_EXPIRED:
5119                return true;
5120        }
5121        return false;
5122}
5123
5124static int nfs4_read_done_cb(struct rpc_task *task, struct nfs_pgio_header *hdr)
5125{
5126        struct nfs_server *server = NFS_SERVER(hdr->inode);
5127
5128        trace_nfs4_read(hdr, task->tk_status);
5129        if (task->tk_status < 0) {
5130                struct nfs4_exception exception = {
5131                        .inode = hdr->inode,
5132                        .state = hdr->args.context->state,
5133                        .stateid = &hdr->args.stateid,
5134                };
5135                task->tk_status = nfs4_async_handle_exception(task,
5136                                server, task->tk_status, &exception);
5137                if (exception.retry) {
5138                        rpc_restart_call_prepare(task);
5139                        return -EAGAIN;
5140                }
5141        }
5142
5143        if (task->tk_status > 0)
5144                renew_lease(server, hdr->timestamp);
5145        return 0;
5146}
5147
5148static bool nfs4_read_stateid_changed(struct rpc_task *task,
5149                struct nfs_pgio_args *args)
5150{
5151
5152        if (!nfs4_error_stateid_expired(task->tk_status) ||
5153                nfs4_stateid_is_current(&args->stateid,
5154                                args->context,
5155                                args->lock_context,
5156                                FMODE_READ))
5157                return false;
5158        rpc_restart_call_prepare(task);
5159        return true;
5160}
5161
5162static int nfs4_read_done(struct rpc_task *task, struct nfs_pgio_header *hdr)
5163{
5164
5165        dprintk("--> %s\n", __func__);
5166
5167        if (!nfs4_sequence_done(task, &hdr->res.seq_res))
5168                return -EAGAIN;
5169        if (nfs4_read_stateid_changed(task, &hdr->args))
5170                return -EAGAIN;
5171        if (task->tk_status > 0)
5172                nfs_invalidate_atime(hdr->inode);
5173        return hdr->pgio_done_cb ? hdr->pgio_done_cb(task, hdr) :
5174                                    nfs4_read_done_cb(task, hdr);
5175}
5176
5177static void nfs4_proc_read_setup(struct nfs_pgio_header *hdr,
5178                                 struct rpc_message *msg)
5179{
5180        hdr->timestamp   = jiffies;
5181        if (!hdr->pgio_done_cb)
5182                hdr->pgio_done_cb = nfs4_read_done_cb;
5183        msg->rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_READ];
5184        nfs4_init_sequence(&hdr->args.seq_args, &hdr->res.seq_res, 0, 0);
5185}
5186
5187static int nfs4_proc_pgio_rpc_prepare(struct rpc_task *task,
5188                                      struct nfs_pgio_header *hdr)
5189{
5190        if (nfs4_setup_sequence(NFS_SERVER(hdr->inode)->nfs_client,
5191                        &hdr->args.seq_args,
5192                        &hdr->res.seq_res,
5193                        task))
5194                return 0;
5195        if (nfs4_set_rw_stateid(&hdr->args.stateid, hdr->args.context,
5196                                hdr->args.lock_context,
5197                                hdr->rw_mode) == -EIO)
5198                return -EIO;
5199        if (unlikely(test_bit(NFS_CONTEXT_BAD, &hdr->args.context->flags)))
5200                return -EIO;
5201        return 0;
5202}
5203
5204static int nfs4_write_done_cb(struct rpc_task *task,
5205                              struct nfs_pgio_header *hdr)
5206{
5207        struct inode *inode = hdr->inode;
5208
5209        trace_nfs4_write(hdr, task->tk_status);
5210        if (task->tk_status < 0) {
5211                struct nfs4_exception exception = {
5212                        .inode = hdr->inode,
5213                        .state = hdr->args.context->state,
5214                        .stateid = &hdr->args.stateid,
5215                };
5216                task->tk_status = nfs4_async_handle_exception(task,
5217                                NFS_SERVER(inode), task->tk_status,
5218                                &exception);
5219                if (exception.retry) {
5220                        rpc_restart_call_prepare(task);
5221                        return -EAGAIN;
5222                }
5223        }
5224        if (task->tk_status >= 0) {
5225                renew_lease(NFS_SERVER(inode), hdr->timestamp);
5226                nfs_writeback_update_inode(hdr);
5227        }
5228        return 0;
5229}
5230
5231static bool nfs4_write_stateid_changed(struct rpc_task *task,
5232                struct nfs_pgio_args *args)
5233{
5234
5235        if (!nfs4_error_stateid_expired(task->tk_status) ||
5236                nfs4_stateid_is_current(&args->stateid,
5237                                args->context,
5238                                args->lock_context,
5239                                FMODE_WRITE))
5240                return false;
5241        rpc_restart_call_prepare(task);
5242        return true;
5243}
5244
5245static int nfs4_write_done(struct rpc_task *task, struct nfs_pgio_header *hdr)
5246{
5247        if (!nfs4_sequence_done(task, &hdr->res.seq_res))
5248                return -EAGAIN;
5249        if (nfs4_write_stateid_changed(task, &hdr->args))
5250                return -EAGAIN;
5251        return hdr->pgio_done_cb ? hdr->pgio_done_cb(task, hdr) :
5252                nfs4_write_done_cb(task, hdr);
5253}
5254
5255static
5256bool nfs4_write_need_cache_consistency_data(struct nfs_pgio_header *hdr)
5257{
5258        /* Don't request attributes for pNFS or O_DIRECT writes */
5259        if (hdr->ds_clp != NULL || hdr->dreq != NULL)
5260                return false;
5261        /* Otherwise, request attributes if and only if we don't hold
5262         * a delegation
5263         */
5264        return nfs4_have_delegation(hdr->inode, FMODE_READ) == 0;
5265}
5266
5267static void nfs4_proc_write_setup(struct nfs_pgio_header *hdr,
5268                                  struct rpc_message *msg,
5269                                  struct rpc_clnt **clnt)
5270{
5271        struct nfs_server *server = NFS_SERVER(hdr->inode);
5272
5273        if (!nfs4_write_need_cache_consistency_data(hdr)) {
5274                hdr->args.bitmask = NULL;
5275                hdr->res.fattr = NULL;
5276        } else
5277                hdr->args.bitmask = server->cache_consistency_bitmask;
5278
5279        if (!hdr->pgio_done_cb)
5280                hdr->pgio_done_cb = nfs4_write_done_cb;
5281        hdr->res.server = server;
5282        hdr->timestamp   = jiffies;
5283
5284        msg->rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_WRITE];
5285        nfs4_init_sequence(&hdr->args.seq_args, &hdr->res.seq_res, 1, 0);
5286        nfs4_state_protect_write(server->nfs_client, clnt, msg, hdr);
5287}
5288
5289static void nfs4_proc_commit_rpc_prepare(struct rpc_task *task, struct nfs_commit_data *data)
5290{
5291        nfs4_setup_sequence(NFS_SERVER(data->inode)->nfs_client,
5292                        &data->args.seq_args,
5293                        &data->res.seq_res,
5294                        task);
5295}
5296
5297static int nfs4_commit_done_cb(struct rpc_task *task, struct nfs_commit_data *data)
5298{
5299        struct inode *inode = data->inode;
5300
5301        trace_nfs4_commit(data, task->tk_status);
5302        if (nfs4_async_handle_error(task, NFS_SERVER(inode),
5303                                    NULL, NULL) == -EAGAIN) {
5304                rpc_restart_call_prepare(task);
5305                return -EAGAIN;
5306        }
5307        return 0;
5308}
5309
5310static int nfs4_commit_done(struct rpc_task *task, struct nfs_commit_data *data)
5311{
5312        if (!nfs4_sequence_done(task, &data->res.seq_res))
5313                return -EAGAIN;
5314        return data->commit_done_cb(task, data);
5315}
5316
5317static void nfs4_proc_commit_setup(struct nfs_commit_data *data, struct rpc_message *msg,
5318                                   struct rpc_clnt **clnt)
5319{
5320        struct nfs_server *server = NFS_SERVER(data->inode);
5321
5322        if (data->commit_done_cb == NULL)
5323                data->commit_done_cb = nfs4_commit_done_cb;
5324        data->res.server = server;
5325        msg->rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_COMMIT];
5326        nfs4_init_sequence(&data->args.seq_args, &data->res.seq_res, 1, 0);
5327        nfs4_state_protect(server->nfs_client, NFS_SP4_MACH_CRED_COMMIT, clnt, msg);
5328}
5329
5330static int _nfs4_proc_commit(struct file *dst, struct nfs_commitargs *args,
5331                                struct nfs_commitres *res)
5332{
5333        struct inode *dst_inode = file_inode(dst);
5334        struct nfs_server *server = NFS_SERVER(dst_inode);
5335        struct rpc_message msg = {
5336                .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_COMMIT],
5337                .rpc_argp = args,
5338                .rpc_resp = res,
5339        };
5340
5341        args->fh = NFS_FH(dst_inode);
5342        return nfs4_call_sync(server->client, server, &msg,
5343                        &args->seq_args, &res->seq_res, 1);
5344}
5345
5346int nfs4_proc_commit(struct file *dst, __u64 offset, __u32 count, struct nfs_commitres *res)
5347{
5348        struct nfs_commitargs args = {
5349                .offset = offset,
5350                .count = count,
5351        };
5352        struct nfs_server *dst_server = NFS_SERVER(file_inode(dst));
5353        struct nfs4_exception exception = { };
5354        int status;
5355
5356        do {
5357                status = _nfs4_proc_commit(dst, &args, res);
5358                status = nfs4_handle_exception(dst_server, status, &exception);
5359        } while (exception.retry);
5360
5361        return status;
5362}
5363
5364struct nfs4_renewdata {
5365        struct nfs_client       *client;
5366        unsigned long           timestamp;
5367};
5368
5369/*
5370 * nfs4_proc_async_renew(): This is not one of the nfs_rpc_ops; it is a special
5371 * standalone procedure for queueing an asynchronous RENEW.
5372 */
5373static void nfs4_renew_release(void *calldata)
5374{
5375        struct nfs4_renewdata *data = calldata;
5376        struct nfs_client *clp = data->client;
5377
5378        if (refcount_read(&clp->cl_count) > 1)
5379                nfs4_schedule_state_renewal(clp);
5380        nfs_put_client(clp);
5381        kfree(data);
5382}
5383
5384static void nfs4_renew_done(struct rpc_task *task, void *calldata)
5385{
5386        struct nfs4_renewdata *data = calldata;
5387        struct nfs_client *clp = data->client;
5388        unsigned long timestamp = data->timestamp;
5389
5390        trace_nfs4_renew_async(clp, task->tk_status);
5391        switch (task->tk_status) {
5392        case 0:
5393                break;
5394        case -NFS4ERR_LEASE_MOVED:
5395                nfs4_schedule_lease_moved_recovery(clp);
5396                break;
5397        default:
5398                /* Unless we're shutting down, schedule state recovery! */
5399                if (test_bit(NFS_CS_RENEWD, &clp->cl_res_state) == 0)
5400                        return;
5401                if (task->tk_status != NFS4ERR_CB_PATH_DOWN) {
5402                        nfs4_schedule_lease_recovery(clp);
5403                        return;
5404                }
5405                nfs4_schedule_path_down_recovery(clp);
5406        }
5407        do_renew_lease(clp, timestamp);
5408}
5409
5410static const struct rpc_call_ops nfs4_renew_ops = {
5411        .rpc_call_done = nfs4_renew_done,
5412        .rpc_release = nfs4_renew_release,
5413};
5414
5415static int nfs4_proc_async_renew(struct nfs_client *clp, const struct cred *cred, unsigned renew_flags)
5416{
5417        struct rpc_message msg = {
5418                .rpc_proc       = &nfs4_procedures[NFSPROC4_CLNT_RENEW],
5419                .rpc_argp       = clp,
5420                .rpc_cred       = cred,
5421        };
5422        struct nfs4_renewdata *data;
5423
5424        if (renew_flags == 0)
5425                return 0;
5426        if (!refcount_inc_not_zero(&clp->cl_count))
5427                return -EIO;
5428        data = kmalloc(sizeof(*data), GFP_NOFS);
5429        if (data == NULL) {
5430                nfs_put_client(clp);
5431                return -ENOMEM;
5432        }
5433        data->client = clp;
5434        data->timestamp = jiffies;
5435        return rpc_call_async(clp->cl_rpcclient, &msg, RPC_TASK_TIMEOUT,
5436                        &nfs4_renew_ops, data);
5437}
5438
5439static int nfs4_proc_renew(struct nfs_client *clp, const struct cred *cred)
5440{
5441        struct rpc_message msg = {
5442                .rpc_proc       = &nfs4_procedures[NFSPROC4_CLNT_RENEW],
5443                .rpc_argp       = clp,
5444                .rpc_cred       = cred,
5445        };
5446        unsigned long now = jiffies;
5447        int status;
5448
5449        status = rpc_call_sync(clp->cl_rpcclient, &msg, RPC_TASK_TIMEOUT);
5450        if (status < 0)
5451                return status;
5452        do_renew_lease(clp, now);
5453        return 0;
5454}
5455
5456static inline int nfs4_server_supports_acls(struct nfs_server *server)
5457{
5458        return server->caps & NFS_CAP_ACLS;
5459}
5460
5461/* Assuming that XATTR_SIZE_MAX is a multiple of PAGE_SIZE, and that
5462 * it's OK to put sizeof(void) * (XATTR_SIZE_MAX/PAGE_SIZE) bytes on
5463 * the stack.
5464 */
5465#define NFS4ACL_MAXPAGES DIV_ROUND_UP(XATTR_SIZE_MAX, PAGE_SIZE)
5466
5467static int buf_to_pages_noslab(const void *buf, size_t buflen,
5468                struct page **pages)
5469{
5470        struct page *newpage, **spages;
5471        int rc = 0;
5472        size_t len;
5473        spages = pages;
5474
5475        do {
5476                len = min_t(size_t, PAGE_SIZE, buflen);
5477                newpage = alloc_page(GFP_KERNEL);
5478
5479                if (newpage == NULL)
5480                        goto unwind;
5481                memcpy(page_address(newpage), buf, len);
5482                buf += len;
5483                buflen -= len;
5484                *pages++ = newpage;
5485                rc++;
5486        } while (buflen != 0);
5487
5488        return rc;
5489
5490unwind:
5491        for(; rc > 0; rc--)
5492                __free_page(spages[rc-1]);
5493        return -ENOMEM;
5494}
5495
5496struct nfs4_cached_acl {
5497        int cached;
5498        size_t len;
5499        char data[0];
5500};
5501
5502static void nfs4_set_cached_acl(struct inode *inode, struct nfs4_cached_acl *acl)
5503{
5504        struct nfs_inode *nfsi = NFS_I(inode);
5505
5506        spin_lock(&inode->i_lock);
5507        kfree(nfsi->nfs4_acl);
5508        nfsi->nfs4_acl = acl;
5509        spin_unlock(&inode->i_lock);
5510}
5511
5512static void nfs4_zap_acl_attr(struct inode *inode)
5513{
5514        nfs4_set_cached_acl(inode, NULL);
5515}
5516
5517static inline ssize_t nfs4_read_cached_acl(struct inode *inode, char *buf, size_t buflen)
5518{
5519        struct nfs_inode *nfsi = NFS_I(inode);
5520        struct nfs4_cached_acl *acl;
5521        int ret = -ENOENT;
5522
5523        spin_lock(&inode->i_lock);
5524        acl = nfsi->nfs4_acl;
5525        if (acl == NULL)
5526                goto out;
5527        if (buf == NULL) /* user is just asking for length */
5528                goto out_len;
5529        if (acl->cached == 0)
5530                goto out;
5531        ret = -ERANGE; /* see getxattr(2) man page */
5532        if (acl->len > buflen)
5533                goto out;
5534        memcpy(buf, acl->data, acl->len);
5535out_len:
5536        ret = acl->len;
5537out:
5538        spin_unlock(&inode->i_lock);
5539        return ret;
5540}
5541
5542static void nfs4_write_cached_acl(struct inode *inode, struct page **pages, size_t pgbase, size_t acl_len)
5543{
5544        struct nfs4_cached_acl *acl;
5545        size_t buflen = sizeof(*acl) + acl_len;
5546
5547        if (buflen <= PAGE_SIZE) {
5548                acl = kmalloc(buflen, GFP_KERNEL);
5549                if (acl == NULL)
5550                        goto out;
5551                acl->cached = 1;
5552                _copy_from_pages(acl->data, pages, pgbase, acl_len);
5553        } else {
5554                acl = kmalloc(sizeof(*acl), GFP_KERNEL);
5555                if (acl == NULL)
5556                        goto out;
5557                acl->cached = 0;
5558        }
5559        acl->len = acl_len;
5560out:
5561        nfs4_set_cached_acl(inode, acl);
5562}
5563
5564/*
5565 * The getxattr API returns the required buffer length when called with a
5566 * NULL buf. The NFSv4 acl tool then calls getxattr again after allocating
5567 * the required buf.  On a NULL buf, we send a page of data to the server
5568 * guessing that the ACL request can be serviced by a page. If so, we cache
5569 * up to the page of ACL data, and the 2nd call to getxattr is serviced by
5570 * the cache. If not so, we throw away the page, and cache the required
5571 * length. The next getxattr call will then produce another round trip to
5572 * the server, this time with the input buf of the required size.
5573 */
5574static ssize_t __nfs4_get_acl_uncached(struct inode *inode, void *buf, size_t buflen)
5575{
5576        struct page *pages[NFS4ACL_MAXPAGES + 1] = {NULL, };
5577        struct nfs_getaclargs args = {
5578                .fh = NFS_FH(inode),
5579                .acl_pages = pages,
5580                .acl_len = buflen,
5581        };
5582        struct nfs_getaclres res = {
5583                .acl_len = buflen,
5584        };
5585        struct rpc_message msg = {
5586                .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_GETACL],
5587                .rpc_argp = &args,
5588                .rpc_resp = &res,
5589        };
5590        unsigned int npages = DIV_ROUND_UP(buflen, PAGE_SIZE) + 1;
5591        int ret = -ENOMEM, i;
5592
5593        if (npages > ARRAY_SIZE(pages))
5594                return -ERANGE;
5595
5596        for (i = 0; i < npages; i++) {
5597                pages[i] = alloc_page(GFP_KERNEL);
5598                if (!pages[i])
5599                        goto out_free;
5600        }
5601
5602        /* for decoding across pages */
5603        res.acl_scratch = alloc_page(GFP_KERNEL);
5604        if (!res.acl_scratch)
5605                goto out_free;
5606
5607        args.acl_len = npages * PAGE_SIZE;
5608
5609        dprintk("%s  buf %p buflen %zu npages %d args.acl_len %zu\n",
5610                __func__, buf, buflen, npages, args.acl_len);
5611        ret = nfs4_call_sync(NFS_SERVER(inode)->client, NFS_SERVER(inode),
5612                             &msg, &args.seq_args, &res.seq_res, 0);
5613        if (ret)
5614                goto out_free;
5615
5616        /* Handle the case where the passed-in buffer is too short */
5617        if (res.acl_flags & NFS4_ACL_TRUNC) {
5618                /* Did the user only issue a request for the acl length? */
5619                if (buf == NULL)
5620                        goto out_ok;
5621                ret = -ERANGE;
5622                goto out_free;
5623        }
5624        nfs4_write_cached_acl(inode, pages, res.acl_data_offset, res.acl_len);
5625        if (buf) {
5626                if (res.acl_len > buflen) {
5627                        ret = -ERANGE;
5628                        goto out_free;
5629                }
5630                _copy_from_pages(buf, pages, res.acl_data_offset, res.acl_len);
5631        }
5632out_ok:
5633        ret = res.acl_len;
5634out_free:
5635        for (i = 0; i < npages; i++)
5636                if (pages[i])
5637                        __free_page(pages[i]);
5638        if (res.acl_scratch)
5639                __free_page(res.acl_scratch);
5640        return ret;
5641}
5642
5643static ssize_t nfs4_get_acl_uncached(struct inode *inode, void *buf, size_t buflen)
5644{
5645        struct nfs4_exception exception = {
5646                .interruptible = true,
5647        };
5648        ssize_t ret;
5649        do {
5650                ret = __nfs4_get_acl_uncached(inode, buf, buflen);
5651                trace_nfs4_get_acl(inode, ret);
5652                if (ret >= 0)
5653                        break;
5654                ret = nfs4_handle_exception(NFS_SERVER(inode), ret, &exception);
5655        } while (exception.retry);
5656        return ret;
5657}
5658
5659static ssize_t nfs4_proc_get_acl(struct inode *inode, void *buf, size_t buflen)
5660{
5661        struct nfs_server *server = NFS_SERVER(inode);
5662        int ret;
5663
5664        if (!nfs4_server_supports_acls(server))
5665                return -EOPNOTSUPP;
5666        ret = nfs_revalidate_inode(server, inode);
5667        if (ret < 0)
5668                return ret;
5669        if (NFS_I(inode)->cache_validity & NFS_INO_INVALID_ACL)
5670                nfs_zap_acl_cache(inode);
5671        ret = nfs4_read_cached_acl(inode, buf, buflen);
5672        if (ret != -ENOENT)
5673                /* -ENOENT is returned if there is no ACL or if there is an ACL
5674                 * but no cached acl data, just the acl length */
5675                return ret;
5676        return nfs4_get_acl_uncached(inode, buf, buflen);
5677}
5678
5679static int __nfs4_proc_set_acl(struct inode *inode, const void *buf, size_t buflen)
5680{
5681        struct nfs_server *server = NFS_SERVER(inode);
5682        struct page *pages[NFS4ACL_MAXPAGES];
5683        struct nfs_setaclargs arg = {
5684                .fh             = NFS_FH(inode),
5685                .acl_pages      = pages,
5686                .acl_len        = buflen,
5687        };
5688        struct nfs_setaclres res;
5689        struct rpc_message msg = {
5690                .rpc_proc       = &nfs4_procedures[NFSPROC4_CLNT_SETACL],
5691                .rpc_argp       = &arg,
5692                .rpc_resp       = &res,
5693        };
5694        unsigned int npages = DIV_ROUND_UP(buflen, PAGE_SIZE);
5695        int ret, i;
5696
5697        if (!nfs4_server_supports_acls(server))
5698                return -EOPNOTSUPP;
5699        if (npages > ARRAY_SIZE(pages))
5700                return -ERANGE;
5701        i = buf_to_pages_noslab(buf, buflen, arg.acl_pages);
5702        if (i < 0)
5703                return i;
5704        nfs4_inode_make_writeable(inode);
5705        ret = nfs4_call_sync(server->client, server, &msg, &arg.seq_args, &res.seq_res, 1);
5706
5707        /*
5708         * Free each page after tx, so the only ref left is
5709         * held by the network stack
5710         */
5711        for (; i > 0; i--)
5712                put_page(pages[i-1]);
5713
5714        /*
5715         * Acl update can result in inode attribute update.
5716         * so mark the attribute cache invalid.
5717         */
5718        spin_lock(&inode->i_lock);
5719        NFS_I(inode)->cache_validity |= NFS_INO_INVALID_CHANGE
5720                | NFS_INO_INVALID_CTIME
5721                | NFS_INO_REVAL_FORCED;
5722        spin_unlock(&inode->i_lock);
5723        nfs_access_zap_cache(inode);
5724        nfs_zap_acl_cache(inode);
5725        return ret;
5726}
5727
5728static int nfs4_proc_set_acl(struct inode *inode, const void *buf, size_t buflen)
5729{
5730        struct nfs4_exception exception = { };
5731        int err;
5732        do {
5733                err = __nfs4_proc_set_acl(inode, buf, buflen);
5734                trace_nfs4_set_acl(inode, err);
5735                err = nfs4_handle_exception(NFS_SERVER(inode), err,
5736                                &exception);
5737        } while (exception.retry);
5738        return err;
5739}
5740
5741#ifdef CONFIG_NFS_V4_SECURITY_LABEL
5742static int _nfs4_get_security_label(struct inode *inode, void *buf,
5743                                        size_t buflen)
5744{
5745        struct nfs_server *server = NFS_SERVER(inode);
5746        struct nfs_fattr fattr;
5747        struct nfs4_label label = {0, 0, buflen, buf};
5748
5749        u32 bitmask[3] = { 0, 0, FATTR4_WORD2_SECURITY_LABEL };
5750        struct nfs4_getattr_arg arg = {
5751                .fh             = NFS_FH(inode),
5752                .bitmask        = bitmask,
5753        };
5754        struct nfs4_getattr_res res = {
5755                .fattr          = &fattr,
5756                .label          = &label,
5757                .server         = server,
5758        };
5759        struct rpc_message msg = {
5760                .rpc_proc       = &nfs4_procedures[NFSPROC4_CLNT_GETATTR],
5761                .rpc_argp       = &arg,
5762                .rpc_resp       = &res,
5763        };
5764        int ret;
5765
5766        nfs_fattr_init(&fattr);
5767
5768        ret = nfs4_call_sync(server->client, server, &msg, &arg.seq_args, &res.seq_res, 0);
5769        if (ret)
5770                return ret;
5771        if (!(fattr.valid & NFS_ATTR_FATTR_V4_SECURITY_LABEL))
5772                return -ENOENT;
5773        if (buflen < label.len)
5774                return -ERANGE;
5775        return 0;
5776}
5777
5778static int nfs4_get_security_label(struct inode *inode, void *buf,
5779                                        size_t buflen)
5780{
5781        struct nfs4_exception exception = {
5782                .interruptible = true,
5783        };
5784        int err;
5785
5786        if (!nfs_server_capable(inode, NFS_CAP_SECURITY_LABEL))
5787                return -EOPNOTSUPP;
5788
5789        do {
5790                err = _nfs4_get_security_label(inode, buf, buflen);
5791                trace_nfs4_get_security_label(inode, err);
5792                err = nfs4_handle_exception(NFS_SERVER(inode), err,
5793                                &exception);
5794        } while (exception.retry);
5795        return err;
5796}
5797
5798static int _nfs4_do_set_security_label(struct inode *inode,
5799                struct nfs4_label *ilabel,
5800                struct nfs_fattr *fattr,
5801                struct nfs4_label *olabel)
5802{
5803
5804        struct iattr sattr = {0};
5805        struct nfs_server *server = NFS_SERVER(inode);
5806        const u32 bitmask[3] = { 0, 0, FATTR4_WORD2_SECURITY_LABEL };
5807        struct nfs_setattrargs arg = {
5808                .fh             = NFS_FH(inode),
5809                .iap            = &sattr,
5810                .server         = server,
5811                .bitmask        = bitmask,
5812                .label          = ilabel,
5813        };
5814        struct nfs_setattrres res = {
5815                .fattr          = fattr,
5816                .label          = olabel,
5817                .server         = server,
5818        };
5819        struct rpc_message msg = {
5820                .rpc_proc       = &nfs4_procedures[NFSPROC4_CLNT_SETATTR],
5821                .rpc_argp       = &arg,
5822                .rpc_resp       = &res,
5823        };
5824        int status;
5825
5826        nfs4_stateid_copy(&arg.stateid, &zero_stateid);
5827
5828        status = nfs4_call_sync(server->client, server, &msg, &arg.seq_args, &res.seq_res, 1);
5829        if (status)
5830                dprintk("%s failed: %d\n", __func__, status);
5831
5832        return status;
5833}
5834
5835static int nfs4_do_set_security_label(struct inode *inode,
5836                struct nfs4_label *ilabel,
5837                struct nfs_fattr *fattr,
5838                struct nfs4_label *olabel)
5839{
5840        struct nfs4_exception exception = { };
5841        int err;
5842
5843        do {
5844                err = _nfs4_do_set_security_label(inode, ilabel,
5845                                fattr, olabel);
5846                trace_nfs4_set_security_label(inode, err);
5847                err = nfs4_handle_exception(NFS_SERVER(inode), err,
5848                                &exception);
5849        } while (exception.retry);
5850        return err;
5851}
5852
5853static int
5854nfs4_set_security_label(struct inode *inode, const void *buf, size_t buflen)
5855{
5856        struct nfs4_label ilabel, *olabel = NULL;
5857        struct nfs_fattr fattr;
5858        int status;
5859
5860        if (!nfs_server_capable(inode, NFS_CAP_SECURITY_LABEL))
5861                return -EOPNOTSUPP;
5862
5863        nfs_fattr_init(&fattr);
5864
5865        ilabel.pi = 0;
5866        ilabel.lfs = 0;
5867        ilabel.label = (char *)buf;
5868        ilabel.len = buflen;
5869
5870        olabel = nfs4_label_alloc(NFS_SERVER(inode), GFP_KERNEL);
5871        if (IS_ERR(olabel)) {
5872                status = -PTR_ERR(olabel);
5873                goto out;
5874        }
5875
5876        status = nfs4_do_set_security_label(inode, &ilabel, &fattr, olabel);
5877        if (status == 0)
5878                nfs_setsecurity(inode, &fattr, olabel);
5879
5880        nfs4_label_free(olabel);
5881out:
5882        return status;
5883}
5884#endif  /* CONFIG_NFS_V4_SECURITY_LABEL */
5885
5886
5887static void nfs4_init_boot_verifier(const struct nfs_client *clp,
5888                                    nfs4_verifier *bootverf)
5889{
5890        __be32 verf[2];
5891
5892        if (test_bit(NFS4CLNT_PURGE_STATE, &clp->cl_state)) {
5893                /* An impossible timestamp guarantees this value
5894                 * will never match a generated boot time. */
5895                verf[0] = cpu_to_be32(U32_MAX);
5896                verf[1] = cpu_to_be32(U32_MAX);
5897        } else {
5898                struct nfs_net *nn = net_generic(clp->cl_net, nfs_net_id);
5899                u64 ns = ktime_to_ns(nn->boot_time);
5900
5901                verf[0] = cpu_to_be32(ns >> 32);
5902                verf[1] = cpu_to_be32(ns);
5903        }
5904        memcpy(bootverf->data, verf, sizeof(bootverf->data));
5905}
5906
5907static int
5908nfs4_init_nonuniform_client_string(struct nfs_client *clp)
5909{
5910        size_t len;
5911        char *str;
5912
5913        if (clp->cl_owner_id != NULL)
5914                return 0;
5915
5916        rcu_read_lock();
5917        len = 14 +
5918                strlen(clp->cl_rpcclient->cl_nodename) +
5919                1 +
5920                strlen(rpc_peeraddr2str(clp->cl_rpcclient, RPC_DISPLAY_ADDR)) +
5921                1;
5922        rcu_read_unlock();
5923        if (nfs4_client_id_uniquifier[0] != '\0')
5924                len += strlen(nfs4_client_id_uniquifier) + 1;
5925        if (len > NFS4_OPAQUE_LIMIT + 1)
5926                return -EINVAL;
5927
5928        /*
5929         * Since this string is allocated at mount time, and held until the
5930         * nfs_client is destroyed, we can use GFP_KERNEL here w/o worrying
5931         * about a memory-reclaim deadlock.
5932         */
5933        str = kmalloc(len, GFP_KERNEL);
5934        if (!str)
5935                return -ENOMEM;
5936
5937        rcu_read_lock();
5938        if (nfs4_client_id_uniquifier[0] != '\0')
5939                scnprintf(str, len, "Linux NFSv4.0 %s/%s/%s",
5940                          clp->cl_rpcclient->cl_nodename,
5941                          nfs4_client_id_uniquifier,
5942                          rpc_peeraddr2str(clp->cl_rpcclient,
5943                                           RPC_DISPLAY_ADDR));
5944        else
5945                scnprintf(str, len, "Linux NFSv4.0 %s/%s",
5946                          clp->cl_rpcclient->cl_nodename,
5947                          rpc_peeraddr2str(clp->cl_rpcclient,
5948                                           RPC_DISPLAY_ADDR));
5949        rcu_read_unlock();
5950
5951        clp->cl_owner_id = str;
5952        return 0;
5953}
5954
5955static int
5956nfs4_init_uniquifier_client_string(struct nfs_client *clp)
5957{
5958        size_t len;
5959        char *str;
5960
5961        len = 10 + 10 + 1 + 10 + 1 +
5962                strlen(nfs4_client_id_uniquifier) + 1 +
5963                strlen(clp->cl_rpcclient->cl_nodename) + 1;
5964
5965        if (len > NFS4_OPAQUE_LIMIT + 1)
5966                return -EINVAL;
5967
5968        /*
5969         * Since this string is allocated at mount time, and held until the
5970         * nfs_client is destroyed, we can use GFP_KERNEL here w/o worrying
5971         * about a memory-reclaim deadlock.
5972         */
5973        str = kmalloc(len, GFP_KERNEL);
5974        if (!str)
5975                return -ENOMEM;
5976
5977        scnprintf(str, len, "Linux NFSv%u.%u %s/%s",
5978                        clp->rpc_ops->version, clp->cl_minorversion,
5979                        nfs4_client_id_uniquifier,
5980                        clp->cl_rpcclient->cl_nodename);
5981        clp->cl_owner_id = str;
5982        return 0;
5983}
5984
5985static int
5986nfs4_init_uniform_client_string(struct nfs_client *clp)
5987{
5988        size_t len;
5989        char *str;
5990
5991        if (clp->cl_owner_id != NULL)
5992                return 0;
5993
5994        if (nfs4_client_id_uniquifier[0] != '\0')
5995                return nfs4_init_uniquifier_client_string(clp);
5996
5997        len = 10 + 10 + 1 + 10 + 1 +
5998                strlen(clp->cl_rpcclient->cl_nodename) + 1;
5999
6000        if (len > NFS4_OPAQUE_LIMIT + 1)
6001                return -EINVAL;
6002
6003        /*
6004         * Since this string is allocated at mount time, and held until the
6005         * nfs_client is destroyed, we can use GFP_KERNEL here w/o worrying
6006         * about a memory-reclaim deadlock.
6007         */
6008        str = kmalloc(len, GFP_KERNEL);
6009        if (!str)
6010                return -ENOMEM;
6011
6012        scnprintf(str, len, "Linux NFSv%u.%u %s",
6013                        clp->rpc_ops->version, clp->cl_minorversion,
6014                        clp->cl_rpcclient->cl_nodename);
6015        clp->cl_owner_id = str;
6016        return 0;
6017}
6018
6019/*
6020 * nfs4_callback_up_net() starts only "tcp" and "tcp6" callback
6021 * services.  Advertise one based on the address family of the
6022 * clientaddr.
6023 */
6024static unsigned int
6025nfs4_init_callback_netid(const struct nfs_client *clp, char *buf, size_t len)
6026{
6027        if (strchr(clp->cl_ipaddr, ':') != NULL)
6028                return scnprintf(buf, len, "tcp6");
6029        else
6030                return scnprintf(buf, len, "tcp");
6031}
6032
6033static void nfs4_setclientid_done(struct rpc_task *task, void *calldata)
6034{
6035        struct nfs4_setclientid *sc = calldata;
6036
6037        if (task->tk_status == 0)
6038                sc->sc_cred = get_rpccred(task->tk_rqstp->rq_cred);
6039}
6040
6041static const struct rpc_call_ops nfs4_setclientid_ops = {
6042        .rpc_call_done = nfs4_setclientid_done,
6043};
6044
6045/**
6046 * nfs4_proc_setclientid - Negotiate client ID
6047 * @clp: state data structure
6048 * @program: RPC program for NFSv4 callback service
6049 * @port: IP port number for NFS4 callback service
6050 * @cred: credential to use for this call
6051 * @res: where to place the result
6052 *
6053 * Returns zero, a negative errno, or a negative NFS4ERR status code.
6054 */
6055int nfs4_proc_setclientid(struct nfs_client *clp, u32 program,
6056                unsigned short port, const struct cred *cred,
6057                struct nfs4_setclientid_res *res)
6058{
6059        nfs4_verifier sc_verifier;
6060        struct nfs4_setclientid setclientid = {
6061                .sc_verifier = &sc_verifier,
6062                .sc_prog = program,
6063                .sc_clnt = clp,
6064        };
6065        struct rpc_message msg = {
6066                .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_SETCLIENTID],
6067                .rpc_argp = &setclientid,
6068                .rpc_resp = res,
6069                .rpc_cred = cred,
6070        };
6071        struct rpc_task_setup task_setup_data = {
6072                .rpc_client = clp->cl_rpcclient,
6073                .rpc_message = &msg,
6074                .callback_ops = &nfs4_setclientid_ops,
6075                .callback_data = &setclientid,
6076                .flags = RPC_TASK_TIMEOUT | RPC_TASK_NO_ROUND_ROBIN,
6077        };
6078        int status;
6079
6080        /* nfs_client_id4 */
6081        nfs4_init_boot_verifier(clp, &sc_verifier);
6082
6083        if (test_bit(NFS_CS_MIGRATION, &clp->cl_flags))
6084                status = nfs4_init_uniform_client_string(clp);
6085        else
6086                status = nfs4_init_nonuniform_client_string(clp);
6087
6088        if (status)
6089                goto out;
6090
6091        /* cb_client4 */
6092        setclientid.sc_netid_len =
6093                                nfs4_init_callback_netid(clp,
6094                                                setclientid.sc_netid,
6095                                                sizeof(setclientid.sc_netid));
6096        setclientid.sc_uaddr_len = scnprintf(setclientid.sc_uaddr,
6097                                sizeof(setclientid.sc_uaddr), "%s.%u.%u",
6098                                clp->cl_ipaddr, port >> 8, port & 255);
6099
6100        dprintk("NFS call  setclientid auth=%s, '%s'\n",
6101                clp->cl_rpcclient->cl_auth->au_ops->au_name,
6102                clp->cl_owner_id);
6103
6104        status = nfs4_call_sync_custom(&task_setup_data);
6105        if (setclientid.sc_cred) {
6106                kfree(clp->cl_acceptor);
6107                clp->cl_acceptor = rpcauth_stringify_acceptor(setclientid.sc_cred);
6108                put_rpccred(setclientid.sc_cred);
6109        }
6110out:
6111        trace_nfs4_setclientid(clp, status);
6112        dprintk("NFS reply setclientid: %d\n", status);
6113        return status;
6114}
6115
6116/**
6117 * nfs4_proc_setclientid_confirm - Confirm client ID
6118 * @clp: state data structure
6119 * @arg: result of a previous SETCLIENTID
6120 * @cred: credential to use for this call
6121 *
6122 * Returns zero, a negative errno, or a negative NFS4ERR status code.
6123 */
6124int nfs4_proc_setclientid_confirm(struct nfs_client *clp,
6125                struct nfs4_setclientid_res *arg,
6126                const struct cred *cred)
6127{
6128        struct rpc_message msg = {
6129                .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_SETCLIENTID_CONFIRM],
6130                .rpc_argp = arg,
6131                .rpc_cred = cred,
6132        };
6133        int status;
6134
6135        dprintk("NFS call  setclientid_confirm auth=%s, (client ID %llx)\n",
6136                clp->cl_rpcclient->cl_auth->au_ops->au_name,
6137                clp->cl_clientid);
6138        status = rpc_call_sync(clp->cl_rpcclient, &msg,
6139                               RPC_TASK_TIMEOUT | RPC_TASK_NO_ROUND_ROBIN);
6140        trace_nfs4_setclientid_confirm(clp, status);
6141        dprintk("NFS reply setclientid_confirm: %d\n", status);
6142        return status;
6143}
6144
6145struct nfs4_delegreturndata {
6146        struct nfs4_delegreturnargs args;
6147        struct nfs4_delegreturnres res;
6148        struct nfs_fh fh;
6149        nfs4_stateid stateid;
6150        unsigned long timestamp;
6151        struct {
6152                struct nfs4_layoutreturn_args arg;
6153                struct nfs4_layoutreturn_res res;
6154                struct nfs4_xdr_opaque_data ld_private;
6155                u32 roc_barrier;
6156                bool roc;
6157        } lr;
6158        struct nfs_fattr fattr;
6159        int rpc_status;
6160        struct inode *inode;
6161};
6162
6163static void nfs4_delegreturn_done(struct rpc_task *task, void *calldata)
6164{
6165        struct nfs4_delegreturndata *data = calldata;
6166        struct nfs4_exception exception = {
6167                .inode = data->inode,
6168                .stateid = &data->stateid,
6169        };
6170
6171        if (!nfs4_sequence_done(task, &data->res.seq_res))
6172                return;
6173
6174        trace_nfs4_delegreturn_exit(&data->args, &data->res, task->tk_status);
6175
6176        /* Handle Layoutreturn errors */
6177        if (pnfs_roc_done(task, data->inode,
6178                                &data->args.lr_args,
6179                                &data->res.lr_res,
6180                                &data->res.lr_ret) == -EAGAIN)
6181                goto out_restart;
6182
6183        switch (task->tk_status) {
6184        case 0:
6185                renew_lease(data->res.server, data->timestamp);
6186                break;
6187        case -NFS4ERR_ADMIN_REVOKED:
6188        case -NFS4ERR_DELEG_REVOKED:
6189        case -NFS4ERR_EXPIRED:
6190                nfs4_free_revoked_stateid(data->res.server,
6191                                data->args.stateid,
6192                                task->tk_msg.rpc_cred);
6193                /* Fallthrough */
6194        case -NFS4ERR_BAD_STATEID:
6195        case -NFS4ERR_STALE_STATEID:
6196                task->tk_status = 0;
6197                break;
6198        case -NFS4ERR_OLD_STATEID:
6199                if (nfs4_refresh_delegation_stateid(&data->stateid, data->inode))
6200                        goto out_restart;
6201                task->tk_status = 0;
6202                break;
6203        case -NFS4ERR_ACCESS:
6204                if (data->args.bitmask) {
6205                        data->args.bitmask = NULL;
6206                        data->res.fattr = NULL;
6207                        goto out_restart;
6208                }
6209                /* Fallthrough */
6210        default:
6211                task->tk_status = nfs4_async_handle_exception(task,
6212                                data->res.server, task->tk_status,
6213                                &exception);
6214                if (exception.retry)
6215                        goto out_restart;
6216        }
6217        data->rpc_status = task->tk_status;
6218        return;
6219out_restart:
6220        task->tk_status = 0;
6221        rpc_restart_call_prepare(task);
6222}
6223
6224static void nfs4_delegreturn_release(void *calldata)
6225{
6226        struct nfs4_delegreturndata *data = calldata;
6227        struct inode *inode = data->inode;
6228
6229        if (inode) {
6230                if (data->lr.roc)
6231                        pnfs_roc_release(&data->lr.arg, &data->lr.res,
6232                                        data->res.lr_ret);
6233                nfs_post_op_update_inode_force_wcc(inode, &data->fattr);
6234                nfs_iput_and_deactive(inode);
6235        }
6236        kfree(calldata);
6237}
6238
6239static void nfs4_delegreturn_prepare(struct rpc_task *task, void *data)
6240{
6241        struct nfs4_delegreturndata *d_data;
6242        struct pnfs_layout_hdr *lo;
6243
6244        d_data = (struct nfs4_delegreturndata *)data;
6245
6246        if (!d_data->lr.roc && nfs4_wait_on_layoutreturn(d_data->inode, task))
6247                return;
6248
6249        lo = d_data->args.lr_args ? d_data->args.lr_args->layout : NULL;
6250        if (lo && !pnfs_layout_is_valid(lo)) {
6251                d_data->args.lr_args = NULL;
6252                d_data->res.lr_res = NULL;
6253        }
6254
6255        nfs4_setup_sequence(d_data->res.server->nfs_client,
6256                        &d_data->args.seq_args,
6257                        &d_data->res.seq_res,
6258                        task);
6259}
6260
6261static const struct rpc_call_ops nfs4_delegreturn_ops = {
6262        .rpc_call_prepare = nfs4_delegreturn_prepare,
6263        .rpc_call_done = nfs4_delegreturn_done,
6264        .rpc_release = nfs4_delegreturn_release,
6265};
6266
6267static int _nfs4_proc_delegreturn(struct inode *inode, const struct cred *cred, const nfs4_stateid *stateid, int issync)
6268{
6269        struct nfs4_delegreturndata *data;
6270        struct nfs_server *server = NFS_SERVER(inode);
6271        struct rpc_task *task;
6272        struct rpc_message msg = {
6273                .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_DELEGRETURN],
6274                .rpc_cred = cred,
6275        };
6276        struct rpc_task_setup task_setup_data = {
6277                .rpc_client = server->client,
6278                .rpc_message = &msg,
6279                .callback_ops = &nfs4_delegreturn_ops,
6280                .flags = RPC_TASK_ASYNC,
6281        };
6282        int status = 0;
6283
6284        data = kzalloc(sizeof(*data), GFP_NOFS);
6285        if (data == NULL)
6286                return -ENOMEM;
6287        nfs4_init_sequence(&data->args.seq_args, &data->res.seq_res, 1, 0);
6288
6289        nfs4_state_protect(server->nfs_client,
6290                        NFS_SP4_MACH_CRED_CLEANUP,
6291                        &task_setup_data.rpc_client, &msg);
6292
6293        data->args.fhandle = &data->fh;
6294        data->args.stateid = &data->stateid;
6295        data->args.bitmask = server->cache_consistency_bitmask;
6296        nfs_copy_fh(&data->fh, NFS_FH(inode));
6297        nfs4_stateid_copy(&data->stateid, stateid);
6298        data->res.fattr = &data->fattr;
6299        data->res.server = server;
6300        data->res.lr_ret = -NFS4ERR_NOMATCHING_LAYOUT;
6301        data->lr.arg.ld_private = &data->lr.ld_private;
6302        nfs_fattr_init(data->res.fattr);
6303        data->timestamp = jiffies;
6304        data->rpc_status = 0;
6305        data->lr.roc = pnfs_roc(inode, &data->lr.arg, &data->lr.res, cred);
6306        data->inode = nfs_igrab_and_active(inode);
6307        if (data->inode) {
6308                if (data->lr.roc) {
6309                        data->args.lr_args = &data->lr.arg;
6310                        data->res.lr_res = &data->lr.res;
6311                }
6312        } else if (data->lr.roc) {
6313                pnfs_roc_release(&data->lr.arg, &data->lr.res, 0);
6314                data->lr.roc = false;
6315        }
6316
6317        task_setup_data.callback_data = data;
6318        msg.rpc_argp = &data->args;
6319        msg.rpc_resp = &data->res;
6320        task = rpc_run_task(&task_setup_data);
6321        if (IS_ERR(task))
6322                return PTR_ERR(task);
6323        if (!issync)
6324                goto out;
6325        status = rpc_wait_for_completion_task(task);
6326        if (status != 0)
6327                goto out;
6328        status = data->rpc_status;
6329out:
6330        rpc_put_task(task);
6331        return status;
6332}
6333
6334int nfs4_proc_delegreturn(struct inode *inode, const struct cred *cred, const nfs4_stateid *stateid, int issync)
6335{
6336        struct nfs_server *server = NFS_SERVER(inode);
6337        struct nfs4_exception exception = { };
6338        int err;
6339        do {
6340                err = _nfs4_proc_delegreturn(inode, cred, stateid, issync);
6341                trace_nfs4_delegreturn(inode, stateid, err);
6342                switch (err) {
6343                        case -NFS4ERR_STALE_STATEID:
6344                        case -NFS4ERR_EXPIRED:
6345                        case 0:
6346                                return 0;
6347                }
6348                err = nfs4_handle_exception(server, err, &exception);
6349        } while (exception.retry);
6350        return err;
6351}
6352
6353static int _nfs4_proc_getlk(struct nfs4_state *state, int cmd, struct file_lock *request)
6354{
6355        struct inode *inode = state->inode;
6356        struct nfs_server *server = NFS_SERVER(inode);
6357        struct nfs_client *clp = server->nfs_client;
6358        struct nfs_lockt_args arg = {
6359                .fh = NFS_FH(inode),
6360                .fl = request,
6361        };
6362        struct nfs_lockt_res res = {
6363                .denied = request,
6364        };
6365        struct rpc_message msg = {
6366                .rpc_proc       = &nfs4_procedures[NFSPROC4_CLNT_LOCKT],
6367                .rpc_argp       = &arg,
6368                .rpc_resp       = &res,
6369                .rpc_cred       = state->owner->so_cred,
6370        };
6371        struct nfs4_lock_state *lsp;
6372        int status;
6373
6374        arg.lock_owner.clientid = clp->cl_clientid;
6375        status = nfs4_set_lock_state(state, request);
6376        if (status != 0)
6377                goto out;
6378        lsp = request->fl_u.nfs4_fl.owner;
6379        arg.lock_owner.id = lsp->ls_seqid.owner_id;
6380        arg.lock_owner.s_dev = server->s_dev;
6381        status = nfs4_call_sync(server->client, server, &msg, &arg.seq_args, &res.seq_res, 1);
6382        switch (status) {
6383                case 0:
6384                        request->fl_type = F_UNLCK;
6385                        break;
6386                case -NFS4ERR_DENIED:
6387                        status = 0;
6388        }
6389        request->fl_ops->fl_release_private(request);
6390        request->fl_ops = NULL;
6391out:
6392        return status;
6393}
6394
6395static int nfs4_proc_getlk(struct nfs4_state *state, int cmd, struct file_lock *request)
6396{
6397        struct nfs4_exception exception = {
6398                .interruptible = true,
6399        };
6400        int err;
6401
6402        do {
6403                err = _nfs4_proc_getlk(state, cmd, request);
6404                trace_nfs4_get_lock(request, state, cmd, err);
6405                err = nfs4_handle_exception(NFS_SERVER(state->inode), err,
6406                                &exception);
6407        } while (exception.retry);
6408        return err;
6409}
6410
6411/*
6412 * Update the seqid of a lock stateid after receiving
6413 * NFS4ERR_OLD_STATEID
6414 */
6415static bool nfs4_refresh_lock_old_stateid(nfs4_stateid *dst,
6416                struct nfs4_lock_state *lsp)
6417{
6418        struct nfs4_state *state = lsp->ls_state;
6419        bool ret = false;
6420
6421        spin_lock(&state->state_lock);
6422        if (!nfs4_stateid_match_other(dst, &lsp->ls_stateid))
6423                goto out;
6424        if (!nfs4_stateid_is_newer(&lsp->ls_stateid, dst))
6425                nfs4_stateid_seqid_inc(dst);
6426        else
6427                dst->seqid = lsp->ls_stateid.seqid;
6428        ret = true;
6429out:
6430        spin_unlock(&state->state_lock);
6431        return ret;
6432}
6433
6434static bool nfs4_sync_lock_stateid(nfs4_stateid *dst,
6435                struct nfs4_lock_state *lsp)
6436{
6437        struct nfs4_state *state = lsp->ls_state;
6438        bool ret;
6439
6440        spin_lock(&state->state_lock);
6441        ret = !nfs4_stateid_match_other(dst, &lsp->ls_stateid);
6442        nfs4_stateid_copy(dst, &lsp->ls_stateid);
6443        spin_unlock(&state->state_lock);
6444        return ret;
6445}
6446
6447struct nfs4_unlockdata {
6448        struct nfs_locku_args arg;
6449        struct nfs_locku_res res;
6450        struct nfs4_lock_state *lsp;
6451        struct nfs_open_context *ctx;
6452        struct nfs_lock_context *l_ctx;
6453        struct file_lock fl;
6454        struct nfs_server *server;
6455        unsigned long timestamp;
6456};
6457
6458static struct nfs4_unlockdata *nfs4_alloc_unlockdata(struct file_lock *fl,
6459                struct nfs_open_context *ctx,
6460                struct nfs4_lock_state *lsp,
6461                struct nfs_seqid *seqid)
6462{
6463        struct nfs4_unlockdata *p;
6464        struct nfs4_state *state = lsp->ls_state;
6465        struct inode *inode = state->inode;
6466
6467        p = kzalloc(sizeof(*p), GFP_NOFS);
6468        if (p == NULL)
6469                return NULL;
6470        p->arg.fh = NFS_FH(inode);
6471        p->arg.fl = &p->fl;
6472        p->arg.seqid = seqid;
6473        p->res.seqid = seqid;
6474        p->lsp = lsp;
6475        /* Ensure we don't close file until we're done freeing locks! */
6476        p->ctx = get_nfs_open_context(ctx);
6477        p->l_ctx = nfs_get_lock_context(ctx);
6478        locks_init_lock(&p->fl);
6479        locks_copy_lock(&p->fl, fl);
6480        p->server = NFS_SERVER(inode);
6481        spin_lock(&state->state_lock);
6482        nfs4_stateid_copy(&p->arg.stateid, &lsp->ls_stateid);
6483        spin_unlock(&state->state_lock);
6484        return p;
6485}
6486
6487static void nfs4_locku_release_calldata(void *data)
6488{
6489        struct nfs4_unlockdata *calldata = data;
6490        nfs_free_seqid(calldata->arg.seqid);
6491        nfs4_put_lock_state(calldata->lsp);
6492        nfs_put_lock_context(calldata->l_ctx);
6493        put_nfs_open_context(calldata->ctx);
6494        kfree(calldata);
6495}
6496
6497static void nfs4_locku_done(struct rpc_task *task, void *data)
6498{
6499        struct nfs4_unlockdata *calldata = data;
6500        struct nfs4_exception exception = {
6501                .inode = calldata->lsp->ls_state->inode,
6502                .stateid = &calldata->arg.stateid,
6503        };
6504
6505        if (!nfs4_sequence_done(task, &calldata->res.seq_res))
6506                return;
6507        switch (task->tk_status) {
6508                case 0:
6509                        renew_lease(calldata->server, calldata->timestamp);
6510                        locks_lock_inode_wait(calldata->lsp->ls_state->inode, &calldata->fl);
6511                        if (nfs4_update_lock_stateid(calldata->lsp,
6512                                        &calldata->res.stateid))
6513                                break;
6514                        /* Fall through */
6515                case -NFS4ERR_ADMIN_REVOKED:
6516                case -NFS4ERR_EXPIRED:
6517                        nfs4_free_revoked_stateid(calldata->server,
6518                                        &calldata->arg.stateid,
6519                                        task->tk_msg.rpc_cred);
6520                        /* Fall through */
6521                case -NFS4ERR_BAD_STATEID:
6522                case -NFS4ERR_STALE_STATEID:
6523                        if (nfs4_sync_lock_stateid(&calldata->arg.stateid,
6524                                                calldata->lsp))
6525                                rpc_restart_call_prepare(task);
6526                        break;
6527                case -NFS4ERR_OLD_STATEID:
6528                        if (nfs4_refresh_lock_old_stateid(&calldata->arg.stateid,
6529                                                calldata->lsp))
6530                                rpc_restart_call_prepare(task);
6531                        break;
6532                default:
6533                        task->tk_status = nfs4_async_handle_exception(task,
6534                                        calldata->server, task->tk_status,
6535                                        &exception);
6536                        if (exception.retry)
6537                                rpc_restart_call_prepare(task);
6538        }
6539        nfs_release_seqid(calldata->arg.seqid);
6540}
6541
6542static void nfs4_locku_prepare(struct rpc_task *task, void *data)
6543{
6544        struct nfs4_unlockdata *calldata = data;
6545
6546        if (test_bit(NFS_CONTEXT_UNLOCK, &calldata->l_ctx->open_context->flags) &&
6547                nfs_async_iocounter_wait(task, calldata->l_ctx))
6548                return;
6549
6550        if (nfs_wait_on_sequence(calldata->arg.seqid, task) != 0)
6551                goto out_wait;
6552        if (test_bit(NFS_LOCK_INITIALIZED, &calldata->lsp->ls_flags) == 0) {
6553                /* Note: exit _without_ running nfs4_locku_done */
6554                goto out_no_action;
6555        }
6556        calldata->timestamp = jiffies;
6557        if (nfs4_setup_sequence(calldata->server->nfs_client,
6558                                &calldata->arg.seq_args,
6559                                &calldata->res.seq_res,
6560                                task) != 0)
6561                nfs_release_seqid(calldata->arg.seqid);
6562        return;
6563out_no_action:
6564        task->tk_action = NULL;
6565out_wait:
6566        nfs4_sequence_done(task, &calldata->res.seq_res);
6567}
6568
6569static const struct rpc_call_ops nfs4_locku_ops = {
6570        .rpc_call_prepare = nfs4_locku_prepare,
6571        .rpc_call_done = nfs4_locku_done,
6572        .rpc_release = nfs4_locku_release_calldata,
6573};
6574
6575static struct rpc_task *nfs4_do_unlck(struct file_lock *fl,
6576                struct nfs_open_context *ctx,
6577                struct nfs4_lock_state *lsp,
6578                struct nfs_seqid *seqid)
6579{
6580        struct nfs4_unlockdata *data;
6581        struct rpc_message msg = {
6582                .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_LOCKU],
6583                .rpc_cred = ctx->cred,
6584        };
6585        struct rpc_task_setup task_setup_data = {
6586                .rpc_client = NFS_CLIENT(lsp->ls_state->inode),
6587                .rpc_message = &msg,
6588                .callback_ops = &nfs4_locku_ops,
6589                .workqueue = nfsiod_workqueue,
6590                .flags = RPC_TASK_ASYNC,
6591        };
6592
6593        nfs4_state_protect(NFS_SERVER(lsp->ls_state->inode)->nfs_client,
6594                NFS_SP4_MACH_CRED_CLEANUP, &task_setup_data.rpc_client, &msg);
6595
6596        /* Ensure this is an unlock - when canceling a lock, the
6597         * canceled lock is passed in, and it won't be an unlock.
6598         */
6599        fl->fl_type = F_UNLCK;
6600        if (fl->fl_flags & FL_CLOSE)
6601                set_bit(NFS_CONTEXT_UNLOCK, &ctx->flags);
6602
6603        data = nfs4_alloc_unlockdata(fl, ctx, lsp, seqid);
6604        if (data == NULL) {
6605                nfs_free_seqid(seqid);
6606                return ERR_PTR(-ENOMEM);
6607        }
6608
6609        nfs4_init_sequence(&data->arg.seq_args, &data->res.seq_res, 1, 0);
6610        msg.rpc_argp = &data->arg;
6611        msg.rpc_resp = &data->res;
6612        task_setup_data.callback_data = data;
6613        return rpc_run_task(&task_setup_data);
6614}
6615
6616static int nfs4_proc_unlck(struct nfs4_state *state, int cmd, struct file_lock *request)
6617{
6618        struct inode *inode = state->inode;
6619        struct nfs4_state_owner *sp = state->owner;
6620        struct nfs_inode *nfsi = NFS_I(inode);
6621        struct nfs_seqid *seqid;
6622        struct nfs4_lock_state *lsp;
6623        struct rpc_task *task;
6624        struct nfs_seqid *(*alloc_seqid)(struct nfs_seqid_counter *, gfp_t);
6625        int status = 0;
6626        unsigned char fl_flags = request->fl_flags;
6627
6628        status = nfs4_set_lock_state(state, request);
6629        /* Unlock _before_ we do the RPC call */
6630        request->fl_flags |= FL_EXISTS;
6631        /* Exclude nfs_delegation_claim_locks() */
6632        mutex_lock(&sp->so_delegreturn_mutex);
6633        /* Exclude nfs4_reclaim_open_stateid() - note nesting! */
6634        down_read(&nfsi->rwsem);
6635        if (locks_lock_inode_wait(inode, request) == -ENOENT) {
6636                up_read(&nfsi->rwsem);
6637                mutex_unlock(&sp->so_delegreturn_mutex);
6638                goto out;
6639        }
6640        up_read(&nfsi->rwsem);
6641        mutex_unlock(&sp->so_delegreturn_mutex);
6642        if (status != 0)
6643                goto out;
6644        /* Is this a delegated lock? */
6645        lsp = request->fl_u.nfs4_fl.owner;
6646        if (test_bit(NFS_LOCK_INITIALIZED, &lsp->ls_flags) == 0)
6647                goto out;
6648        alloc_seqid = NFS_SERVER(inode)->nfs_client->cl_mvops->alloc_seqid;
6649        seqid = alloc_seqid(&lsp->ls_seqid, GFP_KERNEL);
6650        status = -ENOMEM;
6651        if (IS_ERR(seqid))
6652                goto out;
6653        task = nfs4_do_unlck(request, nfs_file_open_context(request->fl_file), lsp, seqid);
6654        status = PTR_ERR(task);
6655        if (IS_ERR(task))
6656                goto out;
6657        status = rpc_wait_for_completion_task(task);
6658        rpc_put_task(task);
6659out:
6660        request->fl_flags = fl_flags;
6661        trace_nfs4_unlock(request, state, F_SETLK, status);
6662        return status;
6663}
6664
6665struct nfs4_lockdata {
6666        struct nfs_lock_args arg;
6667        struct nfs_lock_res res;
6668        struct nfs4_lock_state *lsp;
6669        struct nfs_open_context *ctx;
6670        struct file_lock fl;
6671        unsigned long timestamp;
6672        int rpc_status;
6673        int cancelled;
6674        struct nfs_server *server;
6675};
6676
6677static struct nfs4_lockdata *nfs4_alloc_lockdata(struct file_lock *fl,
6678                struct nfs_open_context *ctx, struct nfs4_lock_state *lsp,
6679                gfp_t gfp_mask)
6680{
6681        struct nfs4_lockdata *p;
6682        struct inode *inode = lsp->ls_state->inode;
6683        struct nfs_server *server = NFS_SERVER(inode);
6684        struct nfs_seqid *(*alloc_seqid)(struct nfs_seqid_counter *, gfp_t);
6685
6686        p = kzalloc(sizeof(*p), gfp_mask);
6687        if (p == NULL)
6688                return NULL;
6689
6690        p->arg.fh = NFS_FH(inode);
6691        p->arg.fl = &p->fl;
6692        p->arg.open_seqid = nfs_alloc_seqid(&lsp->ls_state->owner->so_seqid, gfp_mask);
6693        if (IS_ERR(p->arg.open_seqid))
6694                goto out_free;
6695        alloc_seqid = server->nfs_client->cl_mvops->alloc_seqid;
6696        p->arg.lock_seqid = alloc_seqid(&lsp->ls_seqid, gfp_mask);
6697        if (IS_ERR(p->arg.lock_seqid))
6698                goto out_free_seqid;
6699        p->arg.lock_owner.clientid = server->nfs_client->cl_clientid;
6700        p->arg.lock_owner.id = lsp->ls_seqid.owner_id;
6701        p->arg.lock_owner.s_dev = server->s_dev;
6702        p->res.lock_seqid = p->arg.lock_seqid;
6703        p->lsp = lsp;
6704        p->server = server;
6705        p->ctx = get_nfs_open_context(ctx);
6706        locks_init_lock(&p->fl);
6707        locks_copy_lock(&p->fl, fl);
6708        return p;
6709out_free_seqid:
6710        nfs_free_seqid(p->arg.open_seqid);
6711out_free:
6712        kfree(p);
6713        return NULL;
6714}
6715
6716static void nfs4_lock_prepare(struct rpc_task *task, void *calldata)
6717{
6718        struct nfs4_lockdata *data = calldata;
6719        struct nfs4_state *state = data->lsp->ls_state;
6720
6721        dprintk("%s: begin!\n", __func__);
6722        if (nfs_wait_on_sequence(data->arg.lock_seqid, task) != 0)
6723                goto out_wait;
6724        /* Do we need to do an open_to_lock_owner? */
6725        if (!test_bit(NFS_LOCK_INITIALIZED, &data->lsp->ls_flags)) {
6726                if (nfs_wait_on_sequence(data->arg.open_seqid, task) != 0) {
6727                        goto out_release_lock_seqid;
6728                }
6729                nfs4_stateid_copy(&data->arg.open_stateid,
6730                                &state->open_stateid);
6731                data->arg.new_lock_owner = 1;
6732                data->res.open_seqid = data->arg.open_seqid;
6733        } else {
6734                data->arg.new_lock_owner = 0;
6735                nfs4_stateid_copy(&data->arg.lock_stateid,
6736                                &data->lsp->ls_stateid);
6737        }
6738        if (!nfs4_valid_open_stateid(state)) {
6739                data->rpc_status = -EBADF;
6740                task->tk_action = NULL;
6741                goto out_release_open_seqid;
6742        }
6743        data->timestamp = jiffies;
6744        if (nfs4_setup_sequence(data->server->nfs_client,
6745                                &data->arg.seq_args,
6746                                &data->res.seq_res,
6747                                task) == 0)
6748                return;
6749out_release_open_seqid:
6750        nfs_release_seqid(data->arg.open_seqid);
6751out_release_lock_seqid:
6752        nfs_release_seqid(data->arg.lock_seqid);
6753out_wait:
6754        nfs4_sequence_done(task, &data->res.seq_res);
6755        dprintk("%s: done!, ret = %d\n", __func__, data->rpc_status);
6756}
6757
6758static void nfs4_lock_done(struct rpc_task *task, void *calldata)
6759{
6760        struct nfs4_lockdata *data = calldata;
6761        struct nfs4_lock_state *lsp = data->lsp;
6762
6763        dprintk("%s: begin!\n", __func__);
6764
6765        if (!nfs4_sequence_done(task, &data->res.seq_res))
6766                return;
6767
6768        data->rpc_status = task->tk_status;
6769        switch (task->tk_status) {
6770        case 0:
6771                renew_lease(NFS_SERVER(d_inode(data->ctx->dentry)),
6772                                data->timestamp);
6773                if (data->arg.new_lock && !data->cancelled) {
6774                        data->fl.fl_flags &= ~(FL_SLEEP | FL_ACCESS);
6775                        if (locks_lock_inode_wait(lsp->ls_state->inode, &data->fl) < 0)
6776                                goto out_restart;
6777                }
6778                if (data->arg.new_lock_owner != 0) {
6779                        nfs_confirm_seqid(&lsp->ls_seqid, 0);
6780                        nfs4_stateid_copy(&lsp->ls_stateid, &data->res.stateid);
6781                        set_bit(NFS_LOCK_INITIALIZED, &lsp->ls_flags);
6782                } else if (!nfs4_update_lock_stateid(lsp, &data->res.stateid))
6783                        goto out_restart;
6784                break;
6785        case -NFS4ERR_BAD_STATEID:
6786        case -NFS4ERR_OLD_STATEID:
6787        case -NFS4ERR_STALE_STATEID:
6788        case -NFS4ERR_EXPIRED:
6789                if (data->arg.new_lock_owner != 0) {
6790                        if (!nfs4_stateid_match(&data->arg.open_stateid,
6791                                                &lsp->ls_state->open_stateid))
6792                                goto out_restart;
6793                } else if (!nfs4_stateid_match(&data->arg.lock_stateid,
6794                                                &lsp->ls_stateid))
6795                                goto out_restart;
6796        }
6797out_done:
6798        dprintk("%s: done, ret = %d!\n", __func__, data->rpc_status);
6799        return;
6800out_restart:
6801        if (!data->cancelled)
6802                rpc_restart_call_prepare(task);
6803        goto out_done;
6804}
6805
6806static void nfs4_lock_release(void *calldata)
6807{
6808        struct nfs4_lockdata *data = calldata;
6809
6810        dprintk("%s: begin!\n", __func__);
6811        nfs_free_seqid(data->arg.open_seqid);
6812        if (data->cancelled && data->rpc_status == 0) {
6813                struct rpc_task *task;
6814                task = nfs4_do_unlck(&data->fl, data->ctx, data->lsp,
6815                                data->arg.lock_seqid);
6816                if (!IS_ERR(task))
6817                        rpc_put_task_async(task);
6818                dprintk("%s: cancelling lock!\n", __func__);
6819        } else
6820                nfs_free_seqid(data->arg.lock_seqid);
6821        nfs4_put_lock_state(data->lsp);
6822        put_nfs_open_context(data->ctx);
6823        kfree(data);
6824        dprintk("%s: done!\n", __func__);
6825}
6826
6827static const struct rpc_call_ops nfs4_lock_ops = {
6828        .rpc_call_prepare = nfs4_lock_prepare,
6829        .rpc_call_done = nfs4_lock_done,
6830        .rpc_release = nfs4_lock_release,
6831};
6832
6833static void nfs4_handle_setlk_error(struct nfs_server *server, struct nfs4_lock_state *lsp, int new_lock_owner, int error)
6834{
6835        switch (error) {
6836        case -NFS4ERR_ADMIN_REVOKED:
6837        case -NFS4ERR_EXPIRED:
6838        case -NFS4ERR_BAD_STATEID:
6839                lsp->ls_seqid.flags &= ~NFS_SEQID_CONFIRMED;
6840                if (new_lock_owner != 0 ||
6841                   test_bit(NFS_LOCK_INITIALIZED, &lsp->ls_flags) != 0)
6842                        nfs4_schedule_stateid_recovery(server, lsp->ls_state);
6843                break;
6844        case -NFS4ERR_STALE_STATEID:
6845                lsp->ls_seqid.flags &= ~NFS_SEQID_CONFIRMED;
6846                nfs4_schedule_lease_recovery(server->nfs_client);
6847        };
6848}
6849
6850static int _nfs4_do_setlk(struct nfs4_state *state, int cmd, struct file_lock *fl, int recovery_type)
6851{
6852        struct nfs4_lockdata *data;
6853        struct rpc_task *task;
6854        struct rpc_message msg = {
6855                .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_LOCK],
6856                .rpc_cred = state->owner->so_cred,
6857        };
6858        struct rpc_task_setup task_setup_data = {
6859                .rpc_client = NFS_CLIENT(state->inode),
6860                .rpc_message = &msg,
6861                .callback_ops = &nfs4_lock_ops,
6862                .workqueue = nfsiod_workqueue,
6863                .flags = RPC_TASK_ASYNC,
6864        };
6865        int ret;
6866
6867        dprintk("%s: begin!\n", __func__);
6868        data = nfs4_alloc_lockdata(fl, nfs_file_open_context(fl->fl_file),
6869                        fl->fl_u.nfs4_fl.owner,
6870                        recovery_type == NFS_LOCK_NEW ? GFP_KERNEL : GFP_NOFS);
6871        if (data == NULL)
6872                return -ENOMEM;
6873        if (IS_SETLKW(cmd))
6874                data->arg.block = 1;
6875        nfs4_init_sequence(&data->arg.seq_args, &data->res.seq_res, 1,
6876                                recovery_type > NFS_LOCK_NEW);
6877        msg.rpc_argp = &data->arg;
6878        msg.rpc_resp = &data->res;
6879        task_setup_data.callback_data = data;
6880        if (recovery_type > NFS_LOCK_NEW) {
6881                if (recovery_type == NFS_LOCK_RECLAIM)
6882                        data->arg.reclaim = NFS_LOCK_RECLAIM;
6883        } else
6884                data->arg.new_lock = 1;
6885        task = rpc_run_task(&task_setup_data);
6886        if (IS_ERR(task))
6887                return PTR_ERR(task);
6888        ret = rpc_wait_for_completion_task(task);
6889        if (ret == 0) {
6890                ret = data->rpc_status;
6891                if (ret)
6892                        nfs4_handle_setlk_error(data->server, data->lsp,
6893                                        data->arg.new_lock_owner, ret);
6894        } else
6895                data->cancelled = true;
6896        rpc_put_task(task);
6897        dprintk("%s: done, ret = %d!\n", __func__, ret);
6898        trace_nfs4_set_lock(fl, state, &data->res.stateid, cmd, ret);
6899        return ret;
6900}
6901
6902static int nfs4_lock_reclaim(struct nfs4_state *state, struct file_lock *request)
6903{
6904        struct nfs_server *server = NFS_SERVER(state->inode);
6905        struct nfs4_exception exception = {
6906                .inode = state->inode,
6907        };
6908        int err;
6909
6910        do {
6911                /* Cache the lock if possible... */
6912                if (test_bit(NFS_DELEGATED_STATE, &state->flags) != 0)
6913                        return 0;
6914                err = _nfs4_do_setlk(state, F_SETLK, request, NFS_LOCK_RECLAIM);
6915                if (err != -NFS4ERR_DELAY)
6916                        break;
6917                nfs4_handle_exception(server, err, &exception);
6918        } while (exception.retry);
6919        return err;
6920}
6921
6922static int nfs4_lock_expired(struct nfs4_state *state, struct file_lock *request)
6923{
6924        struct nfs_server *server = NFS_SERVER(state->inode);
6925        struct nfs4_exception exception = {
6926                .inode = state->inode,
6927        };
6928        int err;
6929
6930        err = nfs4_set_lock_state(state, request);
6931        if (err != 0)
6932                return err;
6933        if (!recover_lost_locks) {
6934                set_bit(NFS_LOCK_LOST, &request->fl_u.nfs4_fl.owner->ls_flags);
6935                return 0;
6936        }
6937        do {
6938                if (test_bit(NFS_DELEGATED_STATE, &state->flags) != 0)
6939                        return 0;
6940                err = _nfs4_do_setlk(state, F_SETLK, request, NFS_LOCK_EXPIRED);
6941                switch (err) {
6942                default:
6943                        goto out;
6944                case -NFS4ERR_GRACE:
6945                case -NFS4ERR_DELAY:
6946                        nfs4_handle_exception(server, err, &exception);
6947                        err = 0;
6948                }
6949        } while (exception.retry);
6950out:
6951        return err;
6952}
6953
6954#if defined(CONFIG_NFS_V4_1)
6955static int nfs41_lock_expired(struct nfs4_state *state, struct file_lock *request)
6956{
6957        struct nfs4_lock_state *lsp;
6958        int status;
6959
6960        status = nfs4_set_lock_state(state, request);
6961        if (status != 0)
6962                return status;
6963        lsp = request->fl_u.nfs4_fl.owner;
6964        if (test_bit(NFS_LOCK_INITIALIZED, &lsp->ls_flags) ||
6965            test_bit(NFS_LOCK_LOST, &lsp->ls_flags))
6966                return 0;
6967        return nfs4_lock_expired(state, request);
6968}
6969#endif
6970
6971static int _nfs4_proc_setlk(struct nfs4_state *state, int cmd, struct file_lock *request)
6972{
6973        struct nfs_inode *nfsi = NFS_I(state->inode);
6974        struct nfs4_state_owner *sp = state->owner;
6975        unsigned char fl_flags = request->fl_flags;
6976        int status;
6977
6978        request->fl_flags |= FL_ACCESS;
6979        status = locks_lock_inode_wait(state->inode, request);
6980        if (status < 0)
6981                goto out;
6982        mutex_lock(&sp->so_delegreturn_mutex);
6983        down_read(&nfsi->rwsem);
6984        if (test_bit(NFS_DELEGATED_STATE, &state->flags)) {
6985                /* Yes: cache locks! */
6986                /* ...but avoid races with delegation recall... */
6987                request->fl_flags = fl_flags & ~FL_SLEEP;
6988                status = locks_lock_inode_wait(state->inode, request);
6989                up_read(&nfsi->rwsem);
6990                mutex_unlock(&sp->so_delegreturn_mutex);
6991                goto out;
6992        }
6993        up_read(&nfsi->rwsem);
6994        mutex_unlock(&sp->so_delegreturn_mutex);
6995        status = _nfs4_do_setlk(state, cmd, request, NFS_LOCK_NEW);
6996out:
6997        request->fl_flags = fl_flags;
6998        return status;
6999}
7000
7001static int nfs4_proc_setlk(struct nfs4_state *state, int cmd, struct file_lock *request)
7002{
7003        struct nfs4_exception exception = {
7004                .state = state,
7005                .inode = state->inode,
7006                .interruptible = true,
7007        };
7008        int err;
7009
7010        do {
7011                err = _nfs4_proc_setlk(state, cmd, request);
7012                if (err == -NFS4ERR_DENIED)
7013                        err = -EAGAIN;
7014                err = nfs4_handle_exception(NFS_SERVER(state->inode),
7015                                err, &exception);
7016        } while (exception.retry);
7017        return err;
7018}
7019
7020#define NFS4_LOCK_MINTIMEOUT (1 * HZ)
7021#define NFS4_LOCK_MAXTIMEOUT (30 * HZ)
7022
7023static int
7024nfs4_retry_setlk_simple(struct nfs4_state *state, int cmd,
7025                        struct file_lock *request)
7026{
7027        int             status = -ERESTARTSYS;
7028        unsigned long   timeout = NFS4_LOCK_MINTIMEOUT;
7029
7030        while(!signalled()) {
7031                status = nfs4_proc_setlk(state, cmd, request);
7032                if ((status != -EAGAIN) || IS_SETLK(cmd))
7033                        break;
7034                freezable_schedule_timeout_interruptible(timeout);
7035                timeout *= 2;
7036                timeout = min_t(unsigned long, NFS4_LOCK_MAXTIMEOUT, timeout);
7037                status = -ERESTARTSYS;
7038        }
7039        return status;
7040}
7041
7042#ifdef CONFIG_NFS_V4_1
7043struct nfs4_lock_waiter {
7044        struct task_struct      *task;
7045        struct inode            *inode;
7046        struct nfs_lowner       *owner;
7047};
7048
7049static int
7050nfs4_wake_lock_waiter(wait_queue_entry_t *wait, unsigned int mode, int flags, void *key)
7051{
7052        int ret;
7053        struct nfs4_lock_waiter *waiter = wait->private;
7054
7055        /* NULL key means to wake up everyone */
7056        if (key) {
7057                struct cb_notify_lock_args      *cbnl = key;
7058                struct nfs_lowner               *lowner = &cbnl->cbnl_owner,
7059                                                *wowner = waiter->owner;
7060
7061                /* Only wake if the callback was for the same owner. */
7062                if (lowner->id != wowner->id || lowner->s_dev != wowner->s_dev)
7063                        return 0;
7064
7065                /* Make sure it's for the right inode */
7066                if (nfs_compare_fh(NFS_FH(waiter->inode), &cbnl->cbnl_fh))
7067                        return 0;
7068        }
7069
7070        /* override "private" so we can use default_wake_function */
7071        wait->private = waiter->task;
7072        ret = woken_wake_function(wait, mode, flags, key);
7073        if (ret)
7074                list_del_init(&wait->entry);
7075        wait->private = waiter;
7076        return ret;
7077}
7078
7079static int
7080nfs4_retry_setlk(struct nfs4_state *state, int cmd, struct file_lock *request)
7081{
7082        int status = -ERESTARTSYS;
7083        struct nfs4_lock_state *lsp = request->fl_u.nfs4_fl.owner;
7084        struct nfs_server *server = NFS_SERVER(state->inode);
7085        struct nfs_client *clp = server->nfs_client;
7086        wait_queue_head_t *q = &clp->cl_lock_waitq;
7087        struct nfs_lowner owner = { .clientid = clp->cl_clientid,
7088                                    .id = lsp->ls_seqid.owner_id,
7089                                    .s_dev = server->s_dev };
7090        struct nfs4_lock_waiter waiter = { .task  = current,
7091                                           .inode = state->inode,
7092                                           .owner = &owner};
7093        wait_queue_entry_t wait;
7094
7095        /* Don't bother with waitqueue if we don't expect a callback */
7096        if (!test_bit(NFS_STATE_MAY_NOTIFY_LOCK, &state->flags))
7097                return nfs4_retry_setlk_simple(state, cmd, request);
7098
7099        init_wait(&wait);
7100        wait.private = &waiter;
7101        wait.func = nfs4_wake_lock_waiter;
7102
7103        while(!signalled()) {
7104                add_wait_queue(q, &wait);
7105                status = nfs4_proc_setlk(state, cmd, request);
7106                if ((status != -EAGAIN) || IS_SETLK(cmd)) {
7107                        finish_wait(q, &wait);
7108                        break;
7109                }
7110
7111                status = -ERESTARTSYS;
7112                freezer_do_not_count();
7113                wait_woken(&wait, TASK_INTERRUPTIBLE, NFS4_LOCK_MAXTIMEOUT);
7114                freezer_count();
7115                finish_wait(q, &wait);
7116        }
7117
7118        return status;
7119}
7120#else /* !CONFIG_NFS_V4_1 */
7121static inline int
7122nfs4_retry_setlk(struct nfs4_state *state, int cmd, struct file_lock *request)
7123{
7124        return nfs4_retry_setlk_simple(state, cmd, request);
7125}
7126#endif
7127
7128static int
7129nfs4_proc_lock(struct file *filp, int cmd, struct file_lock *request)
7130{
7131        struct nfs_open_context *ctx;
7132        struct nfs4_state *state;
7133        int status;
7134
7135        /* verify open state */
7136        ctx = nfs_file_open_context(filp);
7137        state = ctx->state;
7138
7139        if (IS_GETLK(cmd)) {
7140                if (state != NULL)
7141                        return nfs4_proc_getlk(state, F_GETLK, request);
7142                return 0;
7143        }
7144
7145        if (!(IS_SETLK(cmd) || IS_SETLKW(cmd)))
7146                return -EINVAL;
7147
7148        if (request->fl_type == F_UNLCK) {
7149                if (state != NULL)
7150                        return nfs4_proc_unlck(state, cmd, request);
7151                return 0;
7152        }
7153
7154        if (state == NULL)
7155                return -ENOLCK;
7156
7157        if ((request->fl_flags & FL_POSIX) &&
7158            !test_bit(NFS_STATE_POSIX_LOCKS, &state->flags))
7159                return -ENOLCK;
7160
7161        /*
7162         * Don't rely on the VFS having checked the file open mode,
7163         * since it won't do this for flock() locks.
7164         */
7165        switch (request->fl_type) {
7166        case F_RDLCK:
7167                if (!(filp->f_mode & FMODE_READ))
7168                        return -EBADF;
7169                break;
7170        case F_WRLCK:
7171                if (!(filp->f_mode & FMODE_WRITE))
7172                        return -EBADF;
7173        }
7174
7175        status = nfs4_set_lock_state(state, request);
7176        if (status != 0)
7177                return status;
7178
7179        return nfs4_retry_setlk(state, cmd, request);
7180}
7181
7182int nfs4_lock_delegation_recall(struct file_lock *fl, struct nfs4_state *state, const nfs4_stateid *stateid)
7183{
7184        struct nfs_server *server = NFS_SERVER(state->inode);
7185        int err;
7186
7187        err = nfs4_set_lock_state(state, fl);
7188        if (err != 0)
7189                return err;
7190        err = _nfs4_do_setlk(state, F_SETLK, fl, NFS_LOCK_NEW);
7191        return nfs4_handle_delegation_recall_error(server, state, stateid, fl, err);
7192}
7193
7194struct nfs_release_lockowner_data {
7195        struct nfs4_lock_state *lsp;
7196        struct nfs_server *server;
7197        struct nfs_release_lockowner_args args;
7198        struct nfs_release_lockowner_res res;
7199        unsigned long timestamp;
7200};
7201
7202static void nfs4_release_lockowner_prepare(struct rpc_task *task, void *calldata)
7203{
7204        struct nfs_release_lockowner_data *data = calldata;
7205        struct nfs_server *server = data->server;
7206        nfs4_setup_sequence(server->nfs_client, &data->args.seq_args,
7207                           &data->res.seq_res, task);
7208        data->args.lock_owner.clientid = server->nfs_client->cl_clientid;
7209        data->timestamp = jiffies;
7210}
7211
7212static void nfs4_release_lockowner_done(struct rpc_task *task, void *calldata)
7213{
7214        struct nfs_release_lockowner_data *data = calldata;
7215        struct nfs_server *server = data->server;
7216
7217        nfs40_sequence_done(task, &data->res.seq_res);
7218
7219        switch (task->tk_status) {
7220        case 0:
7221                renew_lease(server, data->timestamp);
7222                break;
7223        case -NFS4ERR_STALE_CLIENTID:
7224        case -NFS4ERR_EXPIRED:
7225                nfs4_schedule_lease_recovery(server->nfs_client);
7226                break;
7227        case -NFS4ERR_LEASE_MOVED:
7228        case -NFS4ERR_DELAY:
7229                if (nfs4_async_handle_error(task, server,
7230                                            NULL, NULL) == -EAGAIN)
7231                        rpc_restart_call_prepare(task);
7232        }
7233}
7234
7235static void nfs4_release_lockowner_release(void *calldata)
7236{
7237        struct nfs_release_lockowner_data *data = calldata;
7238        nfs4_free_lock_state(data->server, data->lsp);
7239        kfree(calldata);
7240}
7241
7242static const struct rpc_call_ops nfs4_release_lockowner_ops = {
7243        .rpc_call_prepare = nfs4_release_lockowner_prepare,
7244        .rpc_call_done = nfs4_release_lockowner_done,
7245        .rpc_release = nfs4_release_lockowner_release,
7246};
7247
7248static void
7249nfs4_release_lockowner(struct nfs_server *server, struct nfs4_lock_state *lsp)
7250{
7251        struct nfs_release_lockowner_data *data;
7252        struct rpc_message msg = {
7253                .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_RELEASE_LOCKOWNER],
7254        };
7255
7256        if (server->nfs_client->cl_mvops->minor_version != 0)
7257                return;
7258
7259        data = kmalloc(sizeof(*data), GFP_NOFS);
7260        if (!data)
7261                return;
7262        data->lsp = lsp;
7263        data->server = server;
7264        data->args.lock_owner.clientid = server->nfs_client->cl_clientid;
7265        data->args.lock_owner.id = lsp->ls_seqid.owner_id;
7266        data->args.lock_owner.s_dev = server->s_dev;
7267
7268        msg.rpc_argp = &data->args;
7269        msg.rpc_resp = &data->res;
7270        nfs4_init_sequence(&data->args.seq_args, &data->res.seq_res, 0, 0);
7271        rpc_call_async(server->client, &msg, 0, &nfs4_release_lockowner_ops, data);
7272}
7273
7274#define XATTR_NAME_NFSV4_ACL "system.nfs4_acl"
7275
7276static int nfs4_xattr_set_nfs4_acl(const struct xattr_handler *handler,
7277                                   struct dentry *unused, struct inode *inode,
7278                                   const char *key, const void *buf,
7279                                   size_t buflen, int flags)
7280{
7281        return nfs4_proc_set_acl(inode, buf, buflen);
7282}
7283
7284static int nfs4_xattr_get_nfs4_acl(const struct xattr_handler *handler,
7285                                   struct dentry *unused, struct inode *inode,
7286                                   const char *key, void *buf, size_t buflen)
7287{
7288        return nfs4_proc_get_acl(inode, buf, buflen);
7289}
7290
7291static bool nfs4_xattr_list_nfs4_acl(struct dentry *dentry)
7292{
7293        return nfs4_server_supports_acls(NFS_SERVER(d_inode(dentry)));
7294}
7295
7296#ifdef CONFIG_NFS_V4_SECURITY_LABEL
7297
7298static int nfs4_xattr_set_nfs4_label(const struct xattr_handler *handler,
7299                                     struct dentry *unused, struct inode *inode,
7300                                     const char *key, const void *buf,
7301                                     size_t buflen, int flags)
7302{
7303        if (security_ismaclabel(key))
7304                return nfs4_set_security_label(inode, buf, buflen);
7305
7306        return -EOPNOTSUPP;
7307}
7308
7309static int nfs4_xattr_get_nfs4_label(const struct xattr_handler *handler,
7310                                     struct dentry *unused, struct inode *inode,
7311                                     const char *key, void *buf, size_t buflen)
7312{
7313        if (security_ismaclabel(key))
7314                return nfs4_get_security_label(inode, buf, buflen);
7315        return -EOPNOTSUPP;
7316}
7317
7318static ssize_t
7319nfs4_listxattr_nfs4_label(struct inode *inode, char *list, size_t list_len)
7320{
7321        int len = 0;
7322
7323        if (nfs_server_capable(inode, NFS_CAP_SECURITY_LABEL)) {
7324                len = security_inode_listsecurity(inode, list, list_len);
7325                if (list_len && len > list_len)
7326                        return -ERANGE;
7327        }
7328        return len;
7329}
7330
7331static const struct xattr_handler nfs4_xattr_nfs4_label_handler = {
7332        .prefix = XATTR_SECURITY_PREFIX,
7333        .get    = nfs4_xattr_get_nfs4_label,
7334        .set    = nfs4_xattr_set_nfs4_label,
7335};
7336
7337#else
7338
7339static ssize_t
7340nfs4_listxattr_nfs4_label(struct inode *inode, char *list, size_t list_len)
7341{
7342        return 0;
7343}
7344
7345#endif
7346
7347/*
7348 * nfs_fhget will use either the mounted_on_fileid or the fileid
7349 */
7350static void nfs_fixup_referral_attributes(struct nfs_fattr *fattr)
7351{
7352        if (!(((fattr->valid & NFS_ATTR_FATTR_MOUNTED_ON_FILEID) ||
7353               (fattr->valid & NFS_ATTR_FATTR_FILEID)) &&
7354              (fattr->valid & NFS_ATTR_FATTR_FSID) &&
7355              (fattr->valid & NFS_ATTR_FATTR_V4_LOCATIONS)))
7356                return;
7357
7358        fattr->valid |= NFS_ATTR_FATTR_TYPE | NFS_ATTR_FATTR_MODE |
7359                NFS_ATTR_FATTR_NLINK | NFS_ATTR_FATTR_V4_REFERRAL;
7360        fattr->mode = S_IFDIR | S_IRUGO | S_IXUGO;
7361        fattr->nlink = 2;
7362}
7363
7364static int _nfs4_proc_fs_locations(struct rpc_clnt *client, struct inode *dir,
7365                                   const struct qstr *name,
7366                                   struct nfs4_fs_locations *fs_locations,
7367                                   struct page *page)
7368{
7369        struct nfs_server *server = NFS_SERVER(dir);
7370        u32 bitmask[3];
7371        struct nfs4_fs_locations_arg args = {
7372                .dir_fh = NFS_FH(dir),
7373                .name = name,
7374                .page = page,
7375                .bitmask = bitmask,
7376        };
7377        struct nfs4_fs_locations_res res = {
7378                .fs_locations = fs_locations,
7379        };
7380        struct rpc_message msg = {
7381                .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_FS_LOCATIONS],
7382                .rpc_argp = &args,
7383                .rpc_resp = &res,
7384        };
7385        int status;
7386
7387        dprintk("%s: start\n", __func__);
7388
7389        bitmask[0] = nfs4_fattr_bitmap[0] | FATTR4_WORD0_FS_LOCATIONS;
7390        bitmask[1] = nfs4_fattr_bitmap[1];
7391
7392        /* Ask for the fileid of the absent filesystem if mounted_on_fileid
7393         * is not supported */
7394        if (NFS_SERVER(dir)->attr_bitmask[1] & FATTR4_WORD1_MOUNTED_ON_FILEID)
7395                bitmask[0] &= ~FATTR4_WORD0_FILEID;
7396        else
7397                bitmask[1] &= ~FATTR4_WORD1_MOUNTED_ON_FILEID;
7398
7399        nfs_fattr_init(&fs_locations->fattr);
7400        fs_locations->server = server;
7401        fs_locations->nlocations = 0;
7402        status = nfs4_call_sync(client, server, &msg, &args.seq_args, &res.seq_res, 0);
7403        dprintk("%s: returned status = %d\n", __func__, status);
7404        return status;
7405}
7406
7407int nfs4_proc_fs_locations(struct rpc_clnt *client, struct inode *dir,
7408                           const struct qstr *name,
7409                           struct nfs4_fs_locations *fs_locations,
7410                           struct page *page)
7411{
7412        struct nfs4_exception exception = {
7413                .interruptible = true,
7414        };
7415        int err;
7416        do {
7417                err = _nfs4_proc_fs_locations(client, dir, name,
7418                                fs_locations, page);
7419                trace_nfs4_get_fs_locations(dir, name, err);
7420                err = nfs4_handle_exception(NFS_SERVER(dir), err,
7421                                &exception);
7422        } while (exception.retry);
7423        return err;
7424}
7425
7426/*
7427 * This operation also signals the server that this client is
7428 * performing migration recovery.  The server can stop returning
7429 * NFS4ERR_LEASE_MOVED to this client.  A RENEW operation is
7430 * appended to this compound to identify the client ID which is
7431 * performing recovery.
7432 */
7433static int _nfs40_proc_get_locations(struct inode *inode,
7434                                     struct nfs4_fs_locations *locations,
7435                                     struct page *page, const struct cred *cred)
7436{
7437        struct nfs_server *server = NFS_SERVER(inode);
7438        struct rpc_clnt *clnt = server->client;
7439        u32 bitmask[2] = {
7440                [0] = FATTR4_WORD0_FSID | FATTR4_WORD0_FS_LOCATIONS,
7441        };
7442        struct nfs4_fs_locations_arg args = {
7443                .clientid       = server->nfs_client->cl_clientid,
7444                .fh             = NFS_FH(inode),
7445                .page           = page,
7446                .bitmask        = bitmask,
7447                .migration      = 1,            /* skip LOOKUP */
7448                .renew          = 1,            /* append RENEW */
7449        };
7450        struct nfs4_fs_locations_res res = {
7451                .fs_locations   = locations,
7452                .migration      = 1,
7453                .renew          = 1,
7454        };
7455        struct rpc_message msg = {
7456                .rpc_proc       = &nfs4_procedures[NFSPROC4_CLNT_FS_LOCATIONS],
7457                .rpc_argp       = &args,
7458                .rpc_resp       = &res,
7459                .rpc_cred       = cred,
7460        };
7461        unsigned long now = jiffies;
7462        int status;
7463
7464        nfs_fattr_init(&locations->fattr);
7465        locations->server = server;
7466        locations->nlocations = 0;
7467
7468        nfs4_init_sequence(&args.seq_args, &res.seq_res, 0, 1);
7469        status = nfs4_call_sync_sequence(clnt, server, &msg,
7470                                        &args.seq_args, &res.seq_res);
7471        if (status)
7472                return status;
7473
7474        renew_lease(server, now);
7475        return 0;
7476}
7477
7478#ifdef CONFIG_NFS_V4_1
7479
7480/*
7481 * This operation also signals the server that this client is
7482 * performing migration recovery.  The server can stop asserting
7483 * SEQ4_STATUS_LEASE_MOVED for this client.  The client ID
7484 * performing this operation is identified in the SEQUENCE
7485 * operation in this compound.
7486 *
7487 * When the client supports GETATTR(fs_locations_info), it can
7488 * be plumbed in here.
7489 */
7490static int _nfs41_proc_get_locations(struct inode *inode,
7491                                     struct nfs4_fs_locations *locations,
7492                                     struct page *page, const struct cred *cred)
7493{
7494        struct nfs_server *server = NFS_SERVER(inode);
7495        struct rpc_clnt *clnt = server->client;
7496        u32 bitmask[2] = {
7497                [0] = FATTR4_WORD0_FSID | FATTR4_WORD0_FS_LOCATIONS,
7498        };
7499        struct nfs4_fs_locations_arg args = {
7500                .fh             = NFS_FH(inode),
7501                .page           = page,
7502                .bitmask        = bitmask,
7503                .migration      = 1,            /* skip LOOKUP */
7504        };
7505        struct nfs4_fs_locations_res res = {
7506                .fs_locations   = locations,
7507                .migration      = 1,
7508        };
7509        struct rpc_message msg = {
7510                .rpc_proc       = &nfs4_procedures[NFSPROC4_CLNT_FS_LOCATIONS],
7511                .rpc_argp       = &args,
7512                .rpc_resp       = &res,
7513                .rpc_cred       = cred,
7514        };
7515        int status;
7516
7517        nfs_fattr_init(&locations->fattr);
7518        locations->server = server;
7519        locations->nlocations = 0;
7520
7521        nfs4_init_sequence(&args.seq_args, &res.seq_res, 0, 1);
7522        status = nfs4_call_sync_sequence(clnt, server, &msg,
7523                                        &args.seq_args, &res.seq_res);
7524        if (status == NFS4_OK &&
7525            res.seq_res.sr_status_flags & SEQ4_STATUS_LEASE_MOVED)
7526                status = -NFS4ERR_LEASE_MOVED;
7527        return status;
7528}
7529
7530#endif  /* CONFIG_NFS_V4_1 */
7531
7532/**
7533 * nfs4_proc_get_locations - discover locations for a migrated FSID
7534 * @inode: inode on FSID that is migrating
7535 * @locations: result of query
7536 * @page: buffer
7537 * @cred: credential to use for this operation
7538 *
7539 * Returns NFS4_OK on success, a negative NFS4ERR status code if the
7540 * operation failed, or a negative errno if a local error occurred.
7541 *
7542 * On success, "locations" is filled in, but if the server has
7543 * no locations information, NFS_ATTR_FATTR_V4_LOCATIONS is not
7544 * asserted.
7545 *
7546 * -NFS4ERR_LEASE_MOVED is returned if the server still has leases
7547 * from this client that require migration recovery.
7548 */
7549int nfs4_proc_get_locations(struct inode *inode,
7550                            struct nfs4_fs_locations *locations,
7551                            struct page *page, const struct cred *cred)
7552{
7553        struct nfs_server *server = NFS_SERVER(inode);
7554        struct nfs_client *clp = server->nfs_client;
7555        const struct nfs4_mig_recovery_ops *ops =
7556                                        clp->cl_mvops->mig_recovery_ops;
7557        struct nfs4_exception exception = {
7558                .interruptible = true,
7559        };
7560        int status;
7561
7562        dprintk("%s: FSID %llx:%llx on \"%s\"\n", __func__,
7563                (unsigned long long)server->fsid.major,
7564                (unsigned long long)server->fsid.minor,
7565                clp->cl_hostname);
7566        nfs_display_fhandle(NFS_FH(inode), __func__);
7567
7568        do {
7569                status = ops->get_locations(inode, locations, page, cred);
7570                if (status != -NFS4ERR_DELAY)
7571                        break;
7572                nfs4_handle_exception(server, status, &exception);
7573        } while (exception.retry);
7574        return status;
7575}
7576
7577/*
7578 * This operation also signals the server that this client is
7579 * performing "lease moved" recovery.  The server can stop
7580 * returning NFS4ERR_LEASE_MOVED to this client.  A RENEW operation
7581 * is appended to this compound to identify the client ID which is
7582 * performing recovery.
7583 */
7584static int _nfs40_proc_fsid_present(struct inode *inode, const struct cred *cred)
7585{
7586        struct nfs_server *server = NFS_SERVER(inode);
7587        struct nfs_client *clp = NFS_SERVER(inode)->nfs_client;
7588        struct rpc_clnt *clnt = server->client;
7589        struct nfs4_fsid_present_arg args = {
7590                .fh             = NFS_FH(inode),
7591                .clientid       = clp->cl_clientid,
7592                .renew          = 1,            /* append RENEW */
7593        };
7594        struct nfs4_fsid_present_res res = {
7595                .renew          = 1,
7596        };
7597        struct rpc_message msg = {
7598                .rpc_proc       = &nfs4_procedures[NFSPROC4_CLNT_FSID_PRESENT],
7599                .rpc_argp       = &args,
7600                .rpc_resp       = &res,
7601                .rpc_cred       = cred,
7602        };
7603        unsigned long now = jiffies;
7604        int status;
7605
7606        res.fh = nfs_alloc_fhandle();
7607        if (res.fh == NULL)
7608                return -ENOMEM;
7609
7610        nfs4_init_sequence(&args.seq_args, &res.seq_res, 0, 1);
7611        status = nfs4_call_sync_sequence(clnt, server, &msg,
7612                                                &args.seq_args, &res.seq_res);
7613        nfs_free_fhandle(res.fh);
7614        if (status)
7615                return status;
7616
7617        do_renew_lease(clp, now);
7618        return 0;
7619}
7620
7621#ifdef CONFIG_NFS_V4_1
7622
7623/*
7624 * This operation also signals the server that this client is
7625 * performing "lease moved" recovery.  The server can stop asserting
7626 * SEQ4_STATUS_LEASE_MOVED for this client.  The client ID performing
7627 * this operation is identified in the SEQUENCE operation in this
7628 * compound.
7629 */
7630static int _nfs41_proc_fsid_present(struct inode *inode, const struct cred *cred)
7631{
7632        struct nfs_server *server = NFS_SERVER(inode);
7633        struct rpc_clnt *clnt = server->client;
7634        struct nfs4_fsid_present_arg args = {
7635                .fh             = NFS_FH(inode),
7636        };
7637        struct nfs4_fsid_present_res res = {
7638        };
7639        struct rpc_message msg = {
7640                .rpc_proc       = &nfs4_procedures[NFSPROC4_CLNT_FSID_PRESENT],
7641                .rpc_argp       = &args,
7642                .rpc_resp       = &res,
7643                .rpc_cred       = cred,
7644        };
7645        int status;
7646
7647        res.fh = nfs_alloc_fhandle();
7648        if (res.fh == NULL)
7649                return -ENOMEM;
7650
7651        nfs4_init_sequence(&args.seq_args, &res.seq_res, 0, 1);
7652        status = nfs4_call_sync_sequence(clnt, server, &msg,
7653                                                &args.seq_args, &res.seq_res);
7654        nfs_free_fhandle(res.fh);
7655        if (status == NFS4_OK &&
7656            res.seq_res.sr_status_flags & SEQ4_STATUS_LEASE_MOVED)
7657                status = -NFS4ERR_LEASE_MOVED;
7658        return status;
7659}
7660
7661#endif  /* CONFIG_NFS_V4_1 */
7662
7663/**
7664 * nfs4_proc_fsid_present - Is this FSID present or absent on server?
7665 * @inode: inode on FSID to check
7666 * @cred: credential to use for this operation
7667 *
7668 * Server indicates whether the FSID is present, moved, or not
7669 * recognized.  This operation is necessary to clear a LEASE_MOVED
7670 * condition for this client ID.
7671 *
7672 * Returns NFS4_OK if the FSID is present on this server,
7673 * -NFS4ERR_MOVED if the FSID is no longer present, a negative
7674 *  NFS4ERR code if some error occurred on the server, or a
7675 *  negative errno if a local failure occurred.
7676 */
7677int nfs4_proc_fsid_present(struct inode *inode, const struct cred *cred)
7678{
7679        struct nfs_server *server = NFS_SERVER(inode);
7680        struct nfs_client *clp = server->nfs_client;
7681        const struct nfs4_mig_recovery_ops *ops =
7682                                        clp->cl_mvops->mig_recovery_ops;
7683        struct nfs4_exception exception = {
7684                .interruptible = true,
7685        };
7686        int status;
7687
7688        dprintk("%s: FSID %llx:%llx on \"%s\"\n", __func__,
7689                (unsigned long long)server->fsid.major,
7690                (unsigned long long)server->fsid.minor,
7691                clp->cl_hostname);
7692        nfs_display_fhandle(NFS_FH(inode), __func__);
7693
7694        do {
7695                status = ops->fsid_present(inode, cred);
7696                if (status != -NFS4ERR_DELAY)
7697                        break;
7698                nfs4_handle_exception(server, status, &exception);
7699        } while (exception.retry);
7700        return status;
7701}
7702
7703/*
7704 * If 'use_integrity' is true and the state managment nfs_client
7705 * cl_rpcclient is using krb5i/p, use the integrity protected cl_rpcclient
7706 * and the machine credential as per RFC3530bis and RFC5661 Security
7707 * Considerations sections. Otherwise, just use the user cred with the
7708 * filesystem's rpc_client.
7709 */
7710static int _nfs4_proc_secinfo(struct inode *dir, const struct qstr *name, struct nfs4_secinfo_flavors *flavors, bool use_integrity)
7711{
7712        int status;
7713        struct rpc_clnt *clnt = NFS_SERVER(dir)->client;
7714        struct nfs_client *clp = NFS_SERVER(dir)->nfs_client;
7715        struct nfs4_secinfo_arg args = {
7716                .dir_fh = NFS_FH(dir),
7717                .name   = name,
7718        };
7719        struct nfs4_secinfo_res res = {
7720                .flavors     = flavors,
7721        };
7722        struct rpc_message msg = {
7723                .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_SECINFO],
7724                .rpc_argp = &args,
7725                .rpc_resp = &res,
7726        };
7727        struct nfs4_call_sync_data data = {
7728                .seq_server = NFS_SERVER(dir),
7729                .seq_args = &args.seq_args,
7730                .seq_res = &res.seq_res,
7731        };
7732        struct rpc_task_setup task_setup = {
7733                .rpc_client = clnt,
7734                .rpc_message = &msg,
7735                .callback_ops = clp->cl_mvops->call_sync_ops,
7736                .callback_data = &data,
7737                .flags = RPC_TASK_NO_ROUND_ROBIN,
7738        };
7739        const struct cred *cred = NULL;
7740
7741        if (use_integrity) {
7742                clnt = clp->cl_rpcclient;
7743                task_setup.rpc_client = clnt;
7744
7745                cred = nfs4_get_clid_cred(clp);
7746                msg.rpc_cred = cred;
7747        }
7748
7749        dprintk("NFS call  secinfo %s\n", name->name);
7750
7751        nfs4_state_protect(clp, NFS_SP4_MACH_CRED_SECINFO, &clnt, &msg);
7752        nfs4_init_sequence(&args.seq_args, &res.seq_res, 0, 0);
7753        status = nfs4_call_sync_custom(&task_setup);
7754
7755        dprintk("NFS reply  secinfo: %d\n", status);
7756
7757        put_cred(cred);
7758        return status;
7759}
7760
7761int nfs4_proc_secinfo(struct inode *dir, const struct qstr *name,
7762                      struct nfs4_secinfo_flavors *flavors)
7763{
7764        struct nfs4_exception exception = {
7765                .interruptible = true,
7766        };
7767        int err;
7768        do {
7769                err = -NFS4ERR_WRONGSEC;
7770
7771                /* try to use integrity protection with machine cred */
7772                if (_nfs4_is_integrity_protected(NFS_SERVER(dir)->nfs_client))
7773                        err = _nfs4_proc_secinfo(dir, name, flavors, true);
7774
7775                /*
7776                 * if unable to use integrity protection, or SECINFO with
7777                 * integrity protection returns NFS4ERR_WRONGSEC (which is
7778                 * disallowed by spec, but exists in deployed servers) use
7779                 * the current filesystem's rpc_client and the user cred.
7780                 */
7781                if (err == -NFS4ERR_WRONGSEC)
7782                        err = _nfs4_proc_secinfo(dir, name, flavors, false);
7783
7784                trace_nfs4_secinfo(dir, name, err);
7785                err = nfs4_handle_exception(NFS_SERVER(dir), err,
7786                                &exception);
7787        } while (exception.retry);
7788        return err;
7789}
7790
7791#ifdef CONFIG_NFS_V4_1
7792/*
7793 * Check the exchange flags returned by the server for invalid flags, having
7794 * both PNFS and NON_PNFS flags set, and not having one of NON_PNFS, PNFS, or
7795 * DS flags set.
7796 */
7797static int nfs4_check_cl_exchange_flags(u32 flags)
7798{
7799        if (flags & ~EXCHGID4_FLAG_MASK_R)
7800                goto out_inval;
7801        if ((flags & EXCHGID4_FLAG_USE_PNFS_MDS) &&
7802            (flags & EXCHGID4_FLAG_USE_NON_PNFS))
7803                goto out_inval;
7804        if (!(flags & (EXCHGID4_FLAG_MASK_PNFS)))
7805                goto out_inval;
7806        return NFS_OK;
7807out_inval:
7808        return -NFS4ERR_INVAL;
7809}
7810
7811static bool
7812nfs41_same_server_scope(struct nfs41_server_scope *a,
7813                        struct nfs41_server_scope *b)
7814{
7815        if (a->server_scope_sz != b->server_scope_sz)
7816                return false;
7817        return memcmp(a->server_scope, b->server_scope, a->server_scope_sz) == 0;
7818}
7819
7820static void
7821nfs4_bind_one_conn_to_session_done(struct rpc_task *task, void *calldata)
7822{
7823}
7824
7825static const struct rpc_call_ops nfs4_bind_one_conn_to_session_ops = {
7826        .rpc_call_done =  &nfs4_bind_one_conn_to_session_done,
7827};
7828
7829/*
7830 * nfs4_proc_bind_one_conn_to_session()
7831 *
7832 * The 4.1 client currently uses the same TCP connection for the
7833 * fore and backchannel.
7834 */
7835static
7836int nfs4_proc_bind_one_conn_to_session(struct rpc_clnt *clnt,
7837                struct rpc_xprt *xprt,
7838                struct nfs_client *clp,
7839                const struct cred *cred)
7840{
7841        int status;
7842        struct nfs41_bind_conn_to_session_args args = {
7843                .client = clp,
7844                .dir = NFS4_CDFC4_FORE_OR_BOTH,
7845        };
7846        struct nfs41_bind_conn_to_session_res res;
7847        struct rpc_message msg = {
7848                .rpc_proc =
7849                        &nfs4_procedures[NFSPROC4_CLNT_BIND_CONN_TO_SESSION],
7850                .rpc_argp = &args,
7851                .rpc_resp = &res,
7852                .rpc_cred = cred,
7853        };
7854        struct rpc_task_setup task_setup_data = {
7855                .rpc_client = clnt,
7856                .rpc_xprt = xprt,
7857                .callback_ops = &nfs4_bind_one_conn_to_session_ops,
7858                .rpc_message = &msg,
7859                .flags = RPC_TASK_TIMEOUT,
7860        };
7861        struct rpc_task *task;
7862
7863        nfs4_copy_sessionid(&args.sessionid, &clp->cl_session->sess_id);
7864        if (!(clp->cl_session->flags & SESSION4_BACK_CHAN))
7865                args.dir = NFS4_CDFC4_FORE;
7866
7867        /* Do not set the backchannel flag unless this is clnt->cl_xprt */
7868        if (xprt != rcu_access_pointer(clnt->cl_xprt))
7869                args.dir = NFS4_CDFC4_FORE;
7870
7871        task = rpc_run_task(&task_setup_data);
7872        if (!IS_ERR(task)) {
7873                status = task->tk_status;
7874                rpc_put_task(task);
7875        } else
7876                status = PTR_ERR(task);
7877        trace_nfs4_bind_conn_to_session(clp, status);
7878        if (status == 0) {
7879                if (memcmp(res.sessionid.data,
7880                    clp->cl_session->sess_id.data, NFS4_MAX_SESSIONID_LEN)) {
7881                        dprintk("NFS: %s: Session ID mismatch\n", __func__);
7882                        return -EIO;
7883                }
7884                if ((res.dir & args.dir) != res.dir || res.dir == 0) {
7885                        dprintk("NFS: %s: Unexpected direction from server\n",
7886                                __func__);
7887                        return -EIO;
7888                }
7889                if (res.use_conn_in_rdma_mode != args.use_conn_in_rdma_mode) {
7890                        dprintk("NFS: %s: Server returned RDMA mode = true\n",
7891                                __func__);
7892                        return -EIO;
7893                }
7894        }
7895
7896        return status;
7897}
7898
7899struct rpc_bind_conn_calldata {
7900        struct nfs_client *clp;
7901        const struct cred *cred;
7902};
7903
7904static int
7905nfs4_proc_bind_conn_to_session_callback(struct rpc_clnt *clnt,
7906                struct rpc_xprt *xprt,
7907                void *calldata)
7908{
7909        struct rpc_bind_conn_calldata *p = calldata;
7910
7911        return nfs4_proc_bind_one_conn_to_session(clnt, xprt, p->clp, p->cred);
7912}
7913
7914int nfs4_proc_bind_conn_to_session(struct nfs_client *clp, const struct cred *cred)
7915{
7916        struct rpc_bind_conn_calldata data = {
7917                .clp = clp,
7918                .cred = cred,
7919        };
7920        return rpc_clnt_iterate_for_each_xprt(clp->cl_rpcclient,
7921                        nfs4_proc_bind_conn_to_session_callback, &data);
7922}
7923
7924/*
7925 * Minimum set of SP4_MACH_CRED operations from RFC 5661 in the enforce map
7926 * and operations we'd like to see to enable certain features in the allow map
7927 */
7928static const struct nfs41_state_protection nfs4_sp4_mach_cred_request = {
7929        .how = SP4_MACH_CRED,
7930        .enforce.u.words = {
7931                [1] = 1 << (OP_BIND_CONN_TO_SESSION - 32) |
7932                      1 << (OP_EXCHANGE_ID - 32) |
7933                      1 << (OP_CREATE_SESSION - 32) |
7934                      1 << (OP_DESTROY_SESSION - 32) |
7935                      1 << (OP_DESTROY_CLIENTID - 32)
7936        },
7937        .allow.u.words = {
7938                [0] = 1 << (OP_CLOSE) |
7939                      1 << (OP_OPEN_DOWNGRADE) |
7940                      1 << (OP_LOCKU) |
7941                      1 << (OP_DELEGRETURN) |
7942                      1 << (OP_COMMIT),
7943                [1] = 1 << (OP_SECINFO - 32) |
7944                      1 << (OP_SECINFO_NO_NAME - 32) |
7945                      1 << (OP_LAYOUTRETURN - 32) |
7946                      1 << (OP_TEST_STATEID - 32) |
7947                      1 << (OP_FREE_STATEID - 32) |
7948                      1 << (OP_WRITE - 32)
7949        }
7950};
7951
7952/*
7953 * Select the state protection mode for client `clp' given the server results
7954 * from exchange_id in `sp'.
7955 *
7956 * Returns 0 on success, negative errno otherwise.
7957 */
7958static int nfs4_sp4_select_mode(struct nfs_client *clp,
7959                                 struct nfs41_state_protection *sp)
7960{
7961        static const u32 supported_enforce[NFS4_OP_MAP_NUM_WORDS] = {
7962                [1] = 1 << (OP_BIND_CONN_TO_SESSION - 32) |
7963                      1 << (OP_EXCHANGE_ID - 32) |
7964                      1 << (OP_CREATE_SESSION - 32) |
7965                      1 << (OP_DESTROY_SESSION - 32) |
7966                      1 << (OP_DESTROY_CLIENTID - 32)
7967        };
7968        unsigned long flags = 0;
7969        unsigned int i;
7970        int ret = 0;
7971
7972        if (sp->how == SP4_MACH_CRED) {
7973                /* Print state protect result */
7974                dfprintk(MOUNT, "Server SP4_MACH_CRED support:\n");
7975                for (i = 0; i <= LAST_NFS4_OP; i++) {
7976                        if (test_bit(i, sp->enforce.u.longs))
7977                                dfprintk(MOUNT, "  enforce op %d\n", i);
7978                        if (test_bit(i, sp->allow.u.longs))
7979                                dfprintk(MOUNT, "  allow op %d\n", i);
7980                }
7981
7982                /* make sure nothing is on enforce list that isn't supported */
7983                for (i = 0; i < NFS4_OP_MAP_NUM_WORDS; i++) {
7984                        if (sp->enforce.u.words[i] & ~supported_enforce[i]) {
7985                                dfprintk(MOUNT, "sp4_mach_cred: disabled\n");
7986                                ret = -EINVAL;
7987                                goto out;
7988                        }
7989                }
7990
7991                /*
7992                 * Minimal mode - state operations are allowed to use machine
7993                 * credential.  Note this already happens by default, so the
7994                 * client doesn't have to do anything more than the negotiation.
7995                 *
7996                 * NOTE: we don't care if EXCHANGE_ID is in the list -
7997                 *       we're already using the machine cred for exchange_id
7998                 *       and will never use a different cred.
7999                 */
8000                if (test_bit(OP_BIND_CONN_TO_SESSION, sp->enforce.u.longs) &&
8001                    test_bit(OP_CREATE_SESSION, sp->enforce.u.longs) &&
8002                    test_bit(OP_DESTROY_SESSION, sp->enforce.u.longs) &&
8003                    test_bit(OP_DESTROY_CLIENTID, sp->enforce.u.longs)) {
8004                        dfprintk(MOUNT, "sp4_mach_cred:\n");
8005                        dfprintk(MOUNT, "  minimal mode enabled\n");
8006                        __set_bit(NFS_SP4_MACH_CRED_MINIMAL, &flags);
8007                } else {
8008                        dfprintk(MOUNT, "sp4_mach_cred: disabled\n");
8009                        ret = -EINVAL;
8010                        goto out;
8011                }
8012
8013                if (test_bit(OP_CLOSE, sp->allow.u.longs) &&
8014                    test_bit(OP_OPEN_DOWNGRADE, sp->allow.u.longs) &&
8015                    test_bit(OP_DELEGRETURN, sp->allow.u.longs) &&
8016                    test_bit(OP_LOCKU, sp->allow.u.longs)) {
8017                        dfprintk(MOUNT, "  cleanup mode enabled\n");
8018                        __set_bit(NFS_SP4_MACH_CRED_CLEANUP, &flags);
8019                }
8020
8021                if (test_bit(OP_LAYOUTRETURN, sp->allow.u.longs)) {
8022                        dfprintk(MOUNT, "  pnfs cleanup mode enabled\n");
8023                        __set_bit(NFS_SP4_MACH_CRED_PNFS_CLEANUP, &flags);
8024                }
8025
8026                if (test_bit(OP_SECINFO, sp->allow.u.longs) &&
8027                    test_bit(OP_SECINFO_NO_NAME, sp->allow.u.longs)) {
8028                        dfprintk(MOUNT, "  secinfo mode enabled\n");
8029                        __set_bit(NFS_SP4_MACH_CRED_SECINFO, &flags);
8030                }
8031
8032                if (test_bit(OP_TEST_STATEID, sp->allow.u.longs) &&
8033                    test_bit(OP_FREE_STATEID, sp->allow.u.longs)) {
8034                        dfprintk(MOUNT, "  stateid mode enabled\n");
8035                        __set_bit(NFS_SP4_MACH_CRED_STATEID, &flags);
8036                }
8037
8038                if (test_bit(OP_WRITE, sp->allow.u.longs)) {
8039                        dfprintk(MOUNT, "  write mode enabled\n");
8040                        __set_bit(NFS_SP4_MACH_CRED_WRITE, &flags);
8041                }
8042
8043                if (test_bit(OP_COMMIT, sp->allow.u.longs)) {
8044                        dfprintk(MOUNT, "  commit mode enabled\n");
8045                        __set_bit(NFS_SP4_MACH_CRED_COMMIT, &flags);
8046                }
8047        }
8048out:
8049        clp->cl_sp4_flags = flags;
8050        return ret;
8051}
8052
8053struct nfs41_exchange_id_data {
8054        struct nfs41_exchange_id_res res;
8055        struct nfs41_exchange_id_args args;
8056};
8057
8058static void nfs4_exchange_id_release(void *data)
8059{
8060        struct nfs41_exchange_id_data *cdata =
8061                                        (struct nfs41_exchange_id_data *)data;
8062
8063        nfs_put_client(cdata->args.client);
8064        kfree(cdata->res.impl_id);
8065        kfree(cdata->res.server_scope);
8066        kfree(cdata->res.server_owner);
8067        kfree(cdata);
8068}
8069
8070static const struct rpc_call_ops nfs4_exchange_id_call_ops = {
8071        .rpc_release = nfs4_exchange_id_release,
8072};
8073
8074/*
8075 * _nfs4_proc_exchange_id()
8076 *
8077 * Wrapper for EXCHANGE_ID operation.
8078 */
8079static struct rpc_task *
8080nfs4_run_exchange_id(struct nfs_client *clp, const struct cred *cred,
8081                        u32 sp4_how, struct rpc_xprt *xprt)
8082{
8083        struct rpc_message msg = {
8084                .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_EXCHANGE_ID],
8085                .rpc_cred = cred,
8086        };
8087        struct rpc_task_setup task_setup_data = {
8088                .rpc_client = clp->cl_rpcclient,
8089                .callback_ops = &nfs4_exchange_id_call_ops,
8090                .rpc_message = &msg,
8091                .flags = RPC_TASK_TIMEOUT | RPC_TASK_NO_ROUND_ROBIN,
8092        };
8093        struct nfs41_exchange_id_data *calldata;
8094        int status;
8095
8096        if (!refcount_inc_not_zero(&clp->cl_count))
8097                return ERR_PTR(-EIO);
8098
8099        status = -ENOMEM;
8100        calldata = kzalloc(sizeof(*calldata), GFP_NOFS);
8101        if (!calldata)
8102                goto out;
8103
8104        nfs4_init_boot_verifier(clp, &calldata->args.verifier);
8105
8106        status = nfs4_init_uniform_client_string(clp);
8107        if (status)
8108                goto out_calldata;
8109
8110        calldata->res.server_owner = kzalloc(sizeof(struct nfs41_server_owner),
8111                                                GFP_NOFS);
8112        status = -ENOMEM;
8113        if (unlikely(calldata->res.server_owner == NULL))
8114                goto out_calldata;
8115
8116        calldata->res.server_scope = kzalloc(sizeof(struct nfs41_server_scope),
8117                                        GFP_NOFS);
8118        if (unlikely(calldata->res.server_scope == NULL))
8119                goto out_server_owner;
8120
8121        calldata->res.impl_id = kzalloc(sizeof(struct nfs41_impl_id), GFP_NOFS);
8122        if (unlikely(calldata->res.impl_id == NULL))
8123                goto out_server_scope;
8124
8125        switch (sp4_how) {
8126        case SP4_NONE:
8127                calldata->args.state_protect.how = SP4_NONE;
8128                break;
8129
8130        case SP4_MACH_CRED:
8131                calldata->args.state_protect = nfs4_sp4_mach_cred_request;
8132                break;
8133
8134        default:
8135                /* unsupported! */
8136                WARN_ON_ONCE(1);
8137                status = -EINVAL;
8138                goto out_impl_id;
8139        }
8140        if (xprt) {
8141                task_setup_data.rpc_xprt = xprt;
8142                task_setup_data.flags |= RPC_TASK_SOFTCONN;
8143                memcpy(calldata->args.verifier.data, clp->cl_confirm.data,
8144                                sizeof(calldata->args.verifier.data));
8145        }
8146        calldata->args.client = clp;
8147        calldata->args.flags = EXCHGID4_FLAG_SUPP_MOVED_REFER |
8148        EXCHGID4_FLAG_BIND_PRINC_STATEID;
8149#ifdef CONFIG_NFS_V4_1_MIGRATION
8150        calldata->args.flags |= EXCHGID4_FLAG_SUPP_MOVED_MIGR;
8151#endif
8152        msg.rpc_argp = &calldata->args;
8153        msg.rpc_resp = &calldata->res;
8154        task_setup_data.callback_data = calldata;
8155
8156        return rpc_run_task(&task_setup_data);
8157
8158out_impl_id:
8159        kfree(calldata->res.impl_id);
8160out_server_scope:
8161        kfree(calldata->res.server_scope);
8162out_server_owner:
8163        kfree(calldata->res.server_owner);
8164out_calldata:
8165        kfree(calldata);
8166out:
8167        nfs_put_client(clp);
8168        return ERR_PTR(status);
8169}
8170
8171/*
8172 * _nfs4_proc_exchange_id()
8173 *
8174 * Wrapper for EXCHANGE_ID operation.
8175 */
8176static int _nfs4_proc_exchange_id(struct nfs_client *clp, const struct cred *cred,
8177                        u32 sp4_how)
8178{
8179        struct rpc_task *task;
8180        struct nfs41_exchange_id_args *argp;
8181        struct nfs41_exchange_id_res *resp;
8182        int status;
8183
8184        task = nfs4_run_exchange_id(clp, cred, sp4_how, NULL);
8185        if (IS_ERR(task))
8186                return PTR_ERR(task);
8187
8188        argp = task->tk_msg.rpc_argp;
8189        resp = task->tk_msg.rpc_resp;
8190        status = task->tk_status;
8191        if (status  != 0)
8192                goto out;
8193
8194        status = nfs4_check_cl_exchange_flags(resp->flags);
8195        if (status  != 0)
8196                goto out;
8197
8198        status = nfs4_sp4_select_mode(clp, &resp->state_protect);
8199        if (status != 0)
8200                goto out;
8201
8202        clp->cl_clientid = resp->clientid;
8203        clp->cl_exchange_flags = resp->flags;
8204        clp->cl_seqid = resp->seqid;
8205        /* Client ID is not confirmed */
8206        if (!(resp->flags & EXCHGID4_FLAG_CONFIRMED_R))
8207                clear_bit(NFS4_SESSION_ESTABLISHED,
8208                          &clp->cl_session->session_state);
8209
8210        if (clp->cl_serverscope != NULL &&
8211            !nfs41_same_server_scope(clp->cl_serverscope,
8212                                resp->server_scope)) {
8213                dprintk("%s: server_scope mismatch detected\n",
8214                        __func__);
8215                set_bit(NFS4CLNT_SERVER_SCOPE_MISMATCH, &clp->cl_state);
8216        }
8217
8218        swap(clp->cl_serverowner, resp->server_owner);
8219        swap(clp->cl_serverscope, resp->server_scope);
8220        swap(clp->cl_implid, resp->impl_id);
8221
8222        /* Save the EXCHANGE_ID verifier session trunk tests */
8223        memcpy(clp->cl_confirm.data, argp->verifier.data,
8224               sizeof(clp->cl_confirm.data));
8225out:
8226        trace_nfs4_exchange_id(clp, status);
8227        rpc_put_task(task);
8228        return status;
8229}
8230
8231/*
8232 * nfs4_proc_exchange_id()
8233 *
8234 * Returns zero, a negative errno, or a negative NFS4ERR status code.
8235 *
8236 * Since the clientid has expired, all compounds using sessions
8237 * associated with the stale clientid will be returning
8238 * NFS4ERR_BADSESSION in the sequence operation, and will therefore
8239 * be in some phase of session reset.
8240 *
8241 * Will attempt to negotiate SP4_MACH_CRED if krb5i / krb5p auth is used.
8242 */
8243int nfs4_proc_exchange_id(struct nfs_client *clp, const struct cred *cred)
8244{
8245        rpc_authflavor_t authflavor = clp->cl_rpcclient->cl_auth->au_flavor;
8246        int status;
8247
8248        /* try SP4_MACH_CRED if krb5i/p */
8249        if (authflavor == RPC_AUTH_GSS_KRB5I ||
8250            authflavor == RPC_AUTH_GSS_KRB5P) {
8251                status = _nfs4_proc_exchange_id(clp, cred, SP4_MACH_CRED);
8252                if (!status)
8253                        return 0;
8254        }
8255
8256        /* try SP4_NONE */
8257        return _nfs4_proc_exchange_id(clp, cred, SP4_NONE);
8258}
8259
8260/**
8261 * nfs4_test_session_trunk
8262 *
8263 * This is an add_xprt_test() test function called from
8264 * rpc_clnt_setup_test_and_add_xprt.
8265 *
8266 * The rpc_xprt_switch is referrenced by rpc_clnt_setup_test_and_add_xprt
8267 * and is dereferrenced in nfs4_exchange_id_release
8268 *
8269 * Upon success, add the new transport to the rpc_clnt
8270 *
8271 * @clnt: struct rpc_clnt to get new transport
8272 * @xprt: the rpc_xprt to test
8273 * @data: call data for _nfs4_proc_exchange_id.
8274 */
8275void nfs4_test_session_trunk(struct rpc_clnt *clnt, struct rpc_xprt *xprt,
8276                            void *data)
8277{
8278        struct nfs4_add_xprt_data *adata = (struct nfs4_add_xprt_data *)data;
8279        struct rpc_task *task;
8280        int status;
8281
8282        u32 sp4_how;
8283
8284        dprintk("--> %s try %s\n", __func__,
8285                xprt->address_strings[RPC_DISPLAY_ADDR]);
8286
8287        sp4_how = (adata->clp->cl_sp4_flags == 0 ? SP4_NONE : SP4_MACH_CRED);
8288
8289        /* Test connection for session trunking. Async exchange_id call */
8290        task = nfs4_run_exchange_id(adata->clp, adata->cred, sp4_how, xprt);
8291        if (IS_ERR(task))
8292                return;
8293
8294        status = task->tk_status;
8295        if (status == 0)
8296                status = nfs4_detect_session_trunking(adata->clp,
8297                                task->tk_msg.rpc_resp, xprt);
8298
8299        if (status == 0)
8300                rpc_clnt_xprt_switch_add_xprt(clnt, xprt);
8301
8302        rpc_put_task(task);
8303}
8304EXPORT_SYMBOL_GPL(nfs4_test_session_trunk);
8305
8306static int _nfs4_proc_destroy_clientid(struct nfs_client *clp,
8307                const struct cred *cred)
8308{
8309        struct rpc_message msg = {
8310                .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_DESTROY_CLIENTID],
8311                .rpc_argp = clp,
8312                .rpc_cred = cred,
8313        };
8314        int status;
8315
8316        status = rpc_call_sync(clp->cl_rpcclient, &msg,
8317                               RPC_TASK_TIMEOUT | RPC_TASK_NO_ROUND_ROBIN);
8318        trace_nfs4_destroy_clientid(clp, status);
8319        if (status)
8320                dprintk("NFS: Got error %d from the server %s on "
8321                        "DESTROY_CLIENTID.", status, clp->cl_hostname);
8322        return status;
8323}
8324
8325static int nfs4_proc_destroy_clientid(struct nfs_client *clp,
8326                const struct cred *cred)
8327{
8328        unsigned int loop;
8329        int ret;
8330
8331        for (loop = NFS4_MAX_LOOP_ON_RECOVER; loop != 0; loop--) {
8332                ret = _nfs4_proc_destroy_clientid(clp, cred);
8333                switch (ret) {
8334                case -NFS4ERR_DELAY:
8335                case -NFS4ERR_CLIENTID_BUSY:
8336                        ssleep(1);
8337                        break;
8338                default:
8339                        return ret;
8340                }
8341        }
8342        return 0;
8343}
8344
8345int nfs4_destroy_clientid(struct nfs_client *clp)
8346{
8347        const struct cred *cred;
8348        int ret = 0;
8349
8350        if (clp->cl_mvops->minor_version < 1)
8351                goto out;
8352        if (clp->cl_exchange_flags == 0)
8353                goto out;
8354        if (clp->cl_preserve_clid)
8355                goto out;
8356        cred = nfs4_get_clid_cred(clp);
8357        ret = nfs4_proc_destroy_clientid(clp, cred);
8358        put_cred(cred);
8359        switch (ret) {
8360        case 0:
8361        case -NFS4ERR_STALE_CLIENTID:
8362                clp->cl_exchange_flags = 0;
8363        }
8364out:
8365        return ret;
8366}
8367
8368#endif /* CONFIG_NFS_V4_1 */
8369
8370struct nfs4_get_lease_time_data {
8371        struct nfs4_get_lease_time_args *args;
8372        struct nfs4_get_lease_time_res *res;
8373        struct nfs_client *clp;
8374};
8375
8376static void nfs4_get_lease_time_prepare(struct rpc_task *task,
8377                                        void *calldata)
8378{
8379        struct nfs4_get_lease_time_data *data =
8380                        (struct nfs4_get_lease_time_data *)calldata;
8381
8382        dprintk("--> %s\n", __func__);
8383        /* just setup sequence, do not trigger session recovery
8384           since we're invoked within one */
8385        nfs4_setup_sequence(data->clp,
8386                        &data->args->la_seq_args,
8387                        &data->res->lr_seq_res,
8388                        task);
8389        dprintk("<-- %s\n", __func__);
8390}
8391
8392/*
8393 * Called from nfs4_state_manager thread for session setup, so don't recover
8394 * from sequence operation or clientid errors.
8395 */
8396static void nfs4_get_lease_time_done(struct rpc_task *task, void *calldata)
8397{
8398        struct nfs4_get_lease_time_data *data =
8399                        (struct nfs4_get_lease_time_data *)calldata;
8400
8401        dprintk("--> %s\n", __func__);
8402        if (!nfs4_sequence_done(task, &data->res->lr_seq_res))
8403                return;
8404        switch (task->tk_status) {
8405        case -NFS4ERR_DELAY:
8406        case -NFS4ERR_GRACE:
8407                dprintk("%s Retry: tk_status %d\n", __func__, task->tk_status);
8408                rpc_delay(task, NFS4_POLL_RETRY_MIN);
8409                task->tk_status = 0;
8410                /* fall through */
8411        case -NFS4ERR_RETRY_UNCACHED_REP:
8412                rpc_restart_call_prepare(task);
8413                return;
8414        }
8415        dprintk("<-- %s\n", __func__);
8416}
8417
8418static const struct rpc_call_ops nfs4_get_lease_time_ops = {
8419        .rpc_call_prepare = nfs4_get_lease_time_prepare,
8420        .rpc_call_done = nfs4_get_lease_time_done,
8421};
8422
8423int nfs4_proc_get_lease_time(struct nfs_client *clp, struct nfs_fsinfo *fsinfo)
8424{
8425        struct nfs4_get_lease_time_args args;
8426        struct nfs4_get_lease_time_res res = {
8427                .lr_fsinfo = fsinfo,
8428        };
8429        struct nfs4_get_lease_time_data data = {
8430                .args = &args,
8431                .res = &res,
8432                .clp = clp,
8433        };
8434        struct rpc_message msg = {
8435                .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_GET_LEASE_TIME],
8436                .rpc_argp = &args,
8437                .rpc_resp = &res,
8438        };
8439        struct rpc_task_setup task_setup = {
8440                .rpc_client = clp->cl_rpcclient,
8441                .rpc_message = &msg,
8442                .callback_ops = &nfs4_get_lease_time_ops,
8443                .callback_data = &data,
8444                .flags = RPC_TASK_TIMEOUT,
8445        };
8446
8447        nfs4_init_sequence(&args.la_seq_args, &res.lr_seq_res, 0, 1);
8448        return nfs4_call_sync_custom(&task_setup);
8449}
8450
8451#ifdef CONFIG_NFS_V4_1
8452
8453/*
8454 * Initialize the values to be used by the client in CREATE_SESSION
8455 * If nfs4_init_session set the fore channel request and response sizes,
8456 * use them.
8457 *
8458 * Set the back channel max_resp_sz_cached to zero to force the client to
8459 * always set csa_cachethis to FALSE because the current implementation
8460 * of the back channel DRC only supports caching the CB_SEQUENCE operation.
8461 */
8462static void nfs4_init_channel_attrs(struct nfs41_create_session_args *args,
8463                                    struct rpc_clnt *clnt)
8464{
8465        unsigned int max_rqst_sz, max_resp_sz;
8466        unsigned int max_bc_payload = rpc_max_bc_payload(clnt);
8467        unsigned int max_bc_slots = rpc_num_bc_slots(clnt);
8468
8469        max_rqst_sz = NFS_MAX_FILE_IO_SIZE + nfs41_maxwrite_overhead;
8470        max_resp_sz = NFS_MAX_FILE_IO_SIZE + nfs41_maxread_overhead;
8471
8472        /* Fore channel attributes */
8473        args->fc_attrs.max_rqst_sz = max_rqst_sz;
8474        args->fc_attrs.max_resp_sz = max_resp_sz;
8475        args->fc_attrs.max_ops = NFS4_MAX_OPS;
8476        args->fc_attrs.max_reqs = max_session_slots;
8477
8478        dprintk("%s: Fore Channel : max_rqst_sz=%u max_resp_sz=%u "
8479                "max_ops=%u max_reqs=%u\n",
8480                __func__,
8481                args->fc_attrs.max_rqst_sz, args->fc_attrs.max_resp_sz,
8482                args->fc_attrs.max_ops, args->fc_attrs.max_reqs);
8483
8484        /* Back channel attributes */
8485        args->bc_attrs.max_rqst_sz = max_bc_payload;
8486        args->bc_attrs.max_resp_sz = max_bc_payload;
8487        args->bc_attrs.max_resp_sz_cached = 0;
8488        args->bc_attrs.max_ops = NFS4_MAX_BACK_CHANNEL_OPS;
8489        args->bc_attrs.max_reqs = max_t(unsigned short, max_session_cb_slots, 1);
8490        if (args->bc_attrs.max_reqs > max_bc_slots)
8491                args->bc_attrs.max_reqs = max_bc_slots;
8492
8493        dprintk("%s: Back Channel : max_rqst_sz=%u max_resp_sz=%u "
8494                "max_resp_sz_cached=%u max_ops=%u max_reqs=%u\n",
8495                __func__,
8496                args->bc_attrs.max_rqst_sz, args->bc_attrs.max_resp_sz,
8497                args->bc_attrs.max_resp_sz_cached, args->bc_attrs.max_ops,
8498                args->bc_attrs.max_reqs);
8499}
8500
8501static int nfs4_verify_fore_channel_attrs(struct nfs41_create_session_args *args,
8502                struct nfs41_create_session_res *res)
8503{
8504        struct nfs4_channel_attrs *sent = &args->fc_attrs;
8505        struct nfs4_channel_attrs *rcvd = &res->fc_attrs;
8506
8507        if (rcvd->max_resp_sz > sent->max_resp_sz)
8508                return -EINVAL;
8509        /*
8510         * Our requested max_ops is the minimum we need; we're not
8511         * prepared to break up compounds into smaller pieces than that.
8512         * So, no point even trying to continue if the server won't
8513         * cooperate:
8514         */
8515        if (rcvd->max_ops < sent->max_ops)
8516                return -EINVAL;
8517        if (rcvd->max_reqs == 0)
8518                return -EINVAL;
8519        if (rcvd->max_reqs > NFS4_MAX_SLOT_TABLE)
8520                rcvd->max_reqs = NFS4_MAX_SLOT_TABLE;
8521        return 0;
8522}
8523
8524static int nfs4_verify_back_channel_attrs(struct nfs41_create_session_args *args,
8525                struct nfs41_create_session_res *res)
8526{
8527        struct nfs4_channel_attrs *sent = &args->bc_attrs;
8528        struct nfs4_channel_attrs *rcvd = &res->bc_attrs;
8529
8530        if (!(res->flags & SESSION4_BACK_CHAN))
8531                goto out;
8532        if (rcvd->max_rqst_sz > sent->max_rqst_sz)
8533                return -EINVAL;
8534        if (rcvd->max_resp_sz < sent->max_resp_sz)
8535                return -EINVAL;
8536        if (rcvd->max_resp_sz_cached > sent->max_resp_sz_cached)
8537                return -EINVAL;
8538        if (rcvd->max_ops > sent->max_ops)
8539                return -EINVAL;
8540        if (rcvd->max_reqs > sent->max_reqs)
8541                return -EINVAL;
8542out:
8543        return 0;
8544}
8545
8546static int nfs4_verify_channel_attrs(struct nfs41_create_session_args *args,
8547                                     struct nfs41_create_session_res *res)
8548{
8549        int ret;
8550
8551        ret = nfs4_verify_fore_channel_attrs(args, res);
8552        if (ret)
8553                return ret;
8554        return nfs4_verify_back_channel_attrs(args, res);
8555}
8556
8557static void nfs4_update_session(struct nfs4_session *session,
8558                struct nfs41_create_session_res *res)
8559{
8560        nfs4_copy_sessionid(&session->sess_id, &res->sessionid);
8561        /* Mark client id and session as being confirmed */
8562        session->clp->cl_exchange_flags |= EXCHGID4_FLAG_CONFIRMED_R;
8563        set_bit(NFS4_SESSION_ESTABLISHED, &session->session_state);
8564        session->flags = res->flags;
8565        memcpy(&session->fc_attrs, &res->fc_attrs, sizeof(session->fc_attrs));
8566        if (res->flags & SESSION4_BACK_CHAN)
8567                memcpy(&session->bc_attrs, &res->bc_attrs,
8568                                sizeof(session->bc_attrs));
8569}
8570
8571static int _nfs4_proc_create_session(struct nfs_client *clp,
8572                const struct cred *cred)
8573{
8574        struct nfs4_session *session = clp->cl_session;
8575        struct nfs41_create_session_args args = {
8576                .client = clp,
8577                .clientid = clp->cl_clientid,
8578                .seqid = clp->cl_seqid,
8579                .cb_program = NFS4_CALLBACK,
8580        };
8581        struct nfs41_create_session_res res;
8582
8583        struct rpc_message msg = {
8584                .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_CREATE_SESSION],
8585                .rpc_argp = &args,
8586                .rpc_resp = &res,
8587                .rpc_cred = cred,
8588        };
8589        int status;
8590
8591        nfs4_init_channel_attrs(&args, clp->cl_rpcclient);
8592        args.flags = (SESSION4_PERSIST | SESSION4_BACK_CHAN);
8593
8594        status = rpc_call_sync(session->clp->cl_rpcclient, &msg,
8595                               RPC_TASK_TIMEOUT | RPC_TASK_NO_ROUND_ROBIN);
8596        trace_nfs4_create_session(clp, status);
8597
8598        switch (status) {
8599        case -NFS4ERR_STALE_CLIENTID:
8600        case -NFS4ERR_DELAY:
8601        case -ETIMEDOUT:
8602        case -EACCES:
8603        case -EAGAIN:
8604                goto out;
8605        };
8606
8607        clp->cl_seqid++;
8608        if (!status) {
8609                /* Verify the session's negotiated channel_attrs values */
8610                status = nfs4_verify_channel_attrs(&args, &res);
8611                /* Increment the clientid slot sequence id */
8612                if (status)
8613                        goto out;
8614                nfs4_update_session(session, &res);
8615        }
8616out:
8617        return status;
8618}
8619
8620/*
8621 * Issues a CREATE_SESSION operation to the server.
8622 * It is the responsibility of the caller to verify the session is
8623 * expired before calling this routine.
8624 */
8625int nfs4_proc_create_session(struct nfs_client *clp, const struct cred *cred)
8626{
8627        int status;
8628        unsigned *ptr;
8629        struct nfs4_session *session = clp->cl_session;
8630
8631        dprintk("--> %s clp=%p session=%p\n", __func__, clp, session);
8632
8633        status = _nfs4_proc_create_session(clp, cred);
8634        if (status)
8635                goto out;
8636
8637        /* Init or reset the session slot tables */
8638        status = nfs4_setup_session_slot_tables(session);
8639        dprintk("slot table setup returned %d\n", status);
8640        if (status)
8641                goto out;
8642
8643        ptr = (unsigned *)&session->sess_id.data[0];
8644        dprintk("%s client>seqid %d sessionid %u:%u:%u:%u\n", __func__,
8645                clp->cl_seqid, ptr[0], ptr[1], ptr[2], ptr[3]);
8646out:
8647        dprintk("<-- %s\n", __func__);
8648        return status;
8649}
8650
8651/*
8652 * Issue the over-the-wire RPC DESTROY_SESSION.
8653 * The caller must serialize access to this routine.
8654 */
8655int nfs4_proc_destroy_session(struct nfs4_session *session,
8656                const struct cred *cred)
8657{
8658        struct rpc_message msg = {
8659                .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_DESTROY_SESSION],
8660                .rpc_argp = session,
8661                .rpc_cred = cred,
8662        };
8663        int status = 0;
8664
8665        dprintk("--> nfs4_proc_destroy_session\n");
8666
8667        /* session is still being setup */
8668        if (!test_and_clear_bit(NFS4_SESSION_ESTABLISHED, &session->session_state))
8669                return 0;
8670
8671        status = rpc_call_sync(session->clp->cl_rpcclient, &msg,
8672                               RPC_TASK_TIMEOUT | RPC_TASK_NO_ROUND_ROBIN);
8673        trace_nfs4_destroy_session(session->clp, status);
8674
8675        if (status)
8676                dprintk("NFS: Got error %d from the server on DESTROY_SESSION. "
8677                        "Session has been destroyed regardless...\n", status);
8678
8679        dprintk("<-- nfs4_proc_destroy_session\n");
8680        return status;
8681}
8682
8683/*
8684 * Renew the cl_session lease.
8685 */
8686struct nfs4_sequence_data {
8687        struct nfs_client *clp;
8688        struct nfs4_sequence_args args;
8689        struct nfs4_sequence_res res;
8690};
8691
8692static void nfs41_sequence_release(void *data)
8693{
8694        struct nfs4_sequence_data *calldata = data;
8695        struct nfs_client *clp = calldata->clp;
8696
8697        if (refcount_read(&clp->cl_count) > 1)
8698                nfs4_schedule_state_renewal(clp);
8699        nfs_put_client(clp);
8700        kfree(calldata);
8701}
8702
8703static int nfs41_sequence_handle_errors(struct rpc_task *task, struct nfs_client *clp)
8704{
8705        switch(task->tk_status) {
8706        case -NFS4ERR_DELAY:
8707                rpc_delay(task, NFS4_POLL_RETRY_MAX);
8708                return -EAGAIN;
8709        default:
8710                nfs4_schedule_lease_recovery(clp);
8711        }
8712        return 0;
8713}
8714
8715static void nfs41_sequence_call_done(struct rpc_task *task, void *data)
8716{
8717        struct nfs4_sequence_data *calldata = data;
8718        struct nfs_client *clp = calldata->clp;
8719
8720        if (!nfs41_sequence_done(task, task->tk_msg.rpc_resp))
8721                return;
8722
8723        trace_nfs4_sequence(clp, task->tk_status);
8724        if (task->tk_status < 0) {
8725                dprintk("%s ERROR %d\n", __func__, task->tk_status);
8726                if (refcount_read(&clp->cl_count) == 1)
8727                        goto out;
8728
8729                if (nfs41_sequence_handle_errors(task, clp) == -EAGAIN) {
8730                        rpc_restart_call_prepare(task);
8731                        return;
8732                }
8733        }
8734        dprintk("%s rpc_cred %p\n", __func__, task->tk_msg.rpc_cred);
8735out:
8736        dprintk("<-- %s\n", __func__);
8737}
8738
8739static void nfs41_sequence_prepare(struct rpc_task *task, void *data)
8740{
8741        struct nfs4_sequence_data *calldata = data;
8742        struct nfs_client *clp = calldata->clp;
8743        struct nfs4_sequence_args *args;
8744        struct nfs4_sequence_res *res;
8745
8746        args = task->tk_msg.rpc_argp;
8747        res = task->tk_msg.rpc_resp;
8748
8749        nfs4_setup_sequence(clp, args, res, task);
8750}
8751
8752static const struct rpc_call_ops nfs41_sequence_ops = {
8753        .rpc_call_done = nfs41_sequence_call_done,
8754        .rpc_call_prepare = nfs41_sequence_prepare,
8755        .rpc_release = nfs41_sequence_release,
8756};
8757
8758static struct rpc_task *_nfs41_proc_sequence(struct nfs_client *clp,
8759                const struct cred *cred,
8760                struct nfs4_slot *slot,
8761                bool is_privileged)
8762{
8763        struct nfs4_sequence_data *calldata;
8764        struct rpc_message msg = {
8765                .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_SEQUENCE],
8766                .rpc_cred = cred,
8767        };
8768        struct rpc_task_setup task_setup_data = {
8769                .rpc_client = clp->cl_rpcclient,
8770                .rpc_message = &msg,
8771                .callback_ops = &nfs41_sequence_ops,
8772                .flags = RPC_TASK_ASYNC | RPC_TASK_TIMEOUT,
8773        };
8774        struct rpc_task *ret;
8775
8776        ret = ERR_PTR(-EIO);
8777        if (!refcount_inc_not_zero(&clp->cl_count))
8778                goto out_err;
8779
8780        ret = ERR_PTR(-ENOMEM);
8781        calldata = kzalloc(sizeof(*calldata), GFP_NOFS);
8782        if (calldata == NULL)
8783                goto out_put_clp;
8784        nfs4_init_sequence(&calldata->args, &calldata->res, 0, is_privileged);
8785        nfs4_sequence_attach_slot(&calldata->args, &calldata->res, slot);
8786        msg.rpc_argp = &calldata->args;
8787        msg.rpc_resp = &calldata->res;
8788        calldata->clp = clp;
8789        task_setup_data.callback_data = calldata;
8790
8791        ret = rpc_run_task(&task_setup_data);
8792        if (IS_ERR(ret))
8793                goto out_err;
8794        return ret;
8795out_put_clp:
8796        nfs_put_client(clp);
8797out_err:
8798        nfs41_release_slot(slot);
8799        return ret;
8800}
8801
8802static int nfs41_proc_async_sequence(struct nfs_client *clp, const struct cred *cred, unsigned renew_flags)
8803{
8804        struct rpc_task *task;
8805        int ret = 0;
8806
8807        if ((renew_flags & NFS4_RENEW_TIMEOUT) == 0)
8808                return -EAGAIN;
8809        task = _nfs41_proc_sequence(clp, cred, NULL, false);
8810        if (IS_ERR(task))
8811                ret = PTR_ERR(task);
8812        else
8813                rpc_put_task_async(task);
8814        dprintk("<-- %s status=%d\n", __func__, ret);
8815        return ret;
8816}
8817
8818static int nfs4_proc_sequence(struct nfs_client *clp, const struct cred *cred)
8819{
8820        struct rpc_task *task;
8821        int ret;
8822
8823        task = _nfs41_proc_sequence(clp, cred, NULL, true);
8824        if (IS_ERR(task)) {
8825                ret = PTR_ERR(task);
8826                goto out;
8827        }
8828        ret = rpc_wait_for_completion_task(task);
8829        if (!ret)
8830                ret = task->tk_status;
8831        rpc_put_task(task);
8832out:
8833        dprintk("<-- %s status=%d\n", __func__, ret);
8834        return ret;
8835}
8836
8837struct nfs4_reclaim_complete_data {
8838        struct nfs_client *clp;
8839        struct nfs41_reclaim_complete_args arg;
8840        struct nfs41_reclaim_complete_res res;
8841};
8842
8843static void nfs4_reclaim_complete_prepare(struct rpc_task *task, void *data)
8844{
8845        struct nfs4_reclaim_complete_data *calldata = data;
8846
8847        nfs4_setup_sequence(calldata->clp,
8848                        &calldata->arg.seq_args,
8849                        &calldata->res.seq_res,
8850                        task);
8851}
8852
8853static int nfs41_reclaim_complete_handle_errors(struct rpc_task *task, struct nfs_client *clp)
8854{
8855        switch(task->tk_status) {
8856        case 0:
8857                wake_up_all(&clp->cl_lock_waitq);
8858                /* Fallthrough */
8859        case -NFS4ERR_COMPLETE_ALREADY:
8860        case -NFS4ERR_WRONG_CRED: /* What to do here? */
8861                break;
8862        case -NFS4ERR_DELAY:
8863                rpc_delay(task, NFS4_POLL_RETRY_MAX);
8864                /* fall through */
8865        case -NFS4ERR_RETRY_UNCACHED_REP:
8866                return -EAGAIN;
8867        case -NFS4ERR_BADSESSION:
8868        case -NFS4ERR_DEADSESSION:
8869        case -NFS4ERR_CONN_NOT_BOUND_TO_SESSION:
8870                nfs4_schedule_session_recovery(clp->cl_session,
8871                                task->tk_status);
8872                break;
8873        default:
8874                nfs4_schedule_lease_recovery(clp);
8875        }
8876        return 0;
8877}
8878
8879static void nfs4_reclaim_complete_done(struct rpc_task *task, void *data)
8880{
8881        struct nfs4_reclaim_complete_data *calldata = data;
8882        struct nfs_client *clp = calldata->clp;
8883        struct nfs4_sequence_res *res = &calldata->res.seq_res;
8884
8885        dprintk("--> %s\n", __func__);
8886        if (!nfs41_sequence_done(task, res))
8887                return;
8888
8889        trace_nfs4_reclaim_complete(clp, task->tk_status);
8890        if (nfs41_reclaim_complete_handle_errors(task, clp) == -EAGAIN) {
8891                rpc_restart_call_prepare(task);
8892                return;
8893        }
8894        dprintk("<-- %s\n", __func__);
8895}
8896
8897static void nfs4_free_reclaim_complete_data(void *data)
8898{
8899        struct nfs4_reclaim_complete_data *calldata = data;
8900
8901        kfree(calldata);
8902}
8903
8904static const struct rpc_call_ops nfs4_reclaim_complete_call_ops = {
8905        .rpc_call_prepare = nfs4_reclaim_complete_prepare,
8906        .rpc_call_done = nfs4_reclaim_complete_done,
8907        .rpc_release = nfs4_free_reclaim_complete_data,
8908};
8909
8910/*
8911 * Issue a global reclaim complete.
8912 */
8913static int nfs41_proc_reclaim_complete(struct nfs_client *clp,
8914                const struct cred *cred)
8915{
8916        struct nfs4_reclaim_complete_data *calldata;
8917        struct rpc_message msg = {
8918                .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_RECLAIM_COMPLETE],
8919                .rpc_cred = cred,
8920        };
8921        struct rpc_task_setup task_setup_data = {
8922                .rpc_client = clp->cl_rpcclient,
8923                .rpc_message = &msg,
8924                .callback_ops = &nfs4_reclaim_complete_call_ops,
8925                .flags = RPC_TASK_NO_ROUND_ROBIN,
8926        };
8927        int status = -ENOMEM;
8928
8929        dprintk("--> %s\n", __func__);
8930        calldata = kzalloc(sizeof(*calldata), GFP_NOFS);
8931        if (calldata == NULL)
8932                goto out;
8933        calldata->clp = clp;
8934        calldata->arg.one_fs = 0;
8935
8936        nfs4_init_sequence(&calldata->arg.seq_args, &calldata->res.seq_res, 0, 1);
8937        msg.rpc_argp = &calldata->arg;
8938        msg.rpc_resp = &calldata->res;
8939        task_setup_data.callback_data = calldata;
8940        status = nfs4_call_sync_custom(&task_setup_data);
8941out:
8942        dprintk("<-- %s status=%d\n", __func__, status);
8943        return status;
8944}
8945
8946static void
8947nfs4_layoutget_prepare(struct rpc_task *task, void *calldata)
8948{
8949        struct nfs4_layoutget *lgp = calldata;
8950        struct nfs_server *server = NFS_SERVER(lgp->args.inode);
8951
8952        dprintk("--> %s\n", __func__);
8953        nfs4_setup_sequence(server->nfs_client, &lgp->args.seq_args,
8954                                &lgp->res.seq_res, task);
8955        dprintk("<-- %s\n", __func__);
8956}
8957
8958static void nfs4_layoutget_done(struct rpc_task *task, void *calldata)
8959{
8960        struct nfs4_layoutget *lgp = calldata;
8961
8962        dprintk("--> %s\n", __func__);
8963        nfs41_sequence_process(task, &lgp->res.seq_res);
8964        dprintk("<-- %s\n", __func__);
8965}
8966
8967static int
8968nfs4_layoutget_handle_exception(struct rpc_task *task,
8969                struct nfs4_layoutget *lgp, struct nfs4_exception *exception)
8970{
8971        struct inode *inode = lgp->args.inode;
8972        struct nfs_server *server = NFS_SERVER(inode);
8973        struct pnfs_layout_hdr *lo;
8974        int nfs4err = task->tk_status;
8975        int err, status = 0;
8976        LIST_HEAD(head);
8977
8978        dprintk("--> %s tk_status => %d\n", __func__, -task->tk_status);
8979
8980        nfs4_sequence_free_slot(&lgp->res.seq_res);
8981
8982        switch (nfs4err) {
8983        case 0:
8984                goto out;
8985
8986        /*
8987         * NFS4ERR_LAYOUTUNAVAILABLE means we are not supposed to use pnfs
8988         * on the file. set tk_status to -ENODATA to tell upper layer to
8989         * retry go inband.
8990         */
8991        case -NFS4ERR_LAYOUTUNAVAILABLE:
8992                status = -ENODATA;
8993                goto out;
8994        /*
8995         * NFS4ERR_BADLAYOUT means the MDS cannot return a layout of
8996         * length lgp->args.minlength != 0 (see RFC5661 section 18.43.3).
8997         */
8998        case -NFS4ERR_BADLAYOUT:
8999                status = -EOVERFLOW;
9000                goto out;
9001        /*
9002         * NFS4ERR_LAYOUTTRYLATER is a conflict with another client
9003         * (or clients) writing to the same RAID stripe except when
9004         * the minlength argument is 0 (see RFC5661 section 18.43.3).
9005         *
9006         * Treat it like we would RECALLCONFLICT -- we retry for a little
9007         * while, and then eventually give up.
9008         */
9009        case -NFS4ERR_LAYOUTTRYLATER:
9010                if (lgp->args.minlength == 0) {
9011                        status = -EOVERFLOW;
9012                        goto out;
9013                }
9014                status = -EBUSY;
9015                break;
9016        case -NFS4ERR_RECALLCONFLICT:
9017                status = -ERECALLCONFLICT;
9018                break;
9019        case -NFS4ERR_DELEG_REVOKED:
9020        case -NFS4ERR_ADMIN_REVOKED:
9021        case -NFS4ERR_EXPIRED:
9022        case -NFS4ERR_BAD_STATEID:
9023                exception->timeout = 0;
9024                spin_lock(&inode->i_lock);
9025                lo = NFS_I(inode)->layout;
9026                /* If the open stateid was bad, then recover it. */
9027                if (!lo || test_bit(NFS_LAYOUT_INVALID_STID, &lo->plh_flags) ||
9028                    !nfs4_stateid_match_other(&lgp->args.stateid, &lo->plh_stateid)) {
9029                        spin_unlock(&inode->i_lock);
9030                        exception->state = lgp->args.ctx->state;
9031                        exception->stateid = &lgp->args.stateid;
9032                        break;
9033                }
9034
9035                /*
9036                 * Mark the bad layout state as invalid, then retry
9037                 */
9038                pnfs_mark_layout_stateid_invalid(lo, &head);
9039                spin_unlock(&inode->i_lock);
9040                nfs_commit_inode(inode, 0);
9041                pnfs_free_lseg_list(&head);
9042                status = -EAGAIN;
9043                goto out;
9044        }
9045
9046        err = nfs4_handle_exception(server, nfs4err, exception);
9047        if (!status) {
9048                if (exception->retry)
9049                        status = -EAGAIN;
9050                else
9051                        status = err;
9052        }
9053out:
9054        dprintk("<-- %s\n", __func__);
9055        return status;
9056}
9057
9058size_t max_response_pages(struct nfs_server *server)
9059{
9060        u32 max_resp_sz = server->nfs_client->cl_session->fc_attrs.max_resp_sz;
9061        return nfs_page_array_len(0, max_resp_sz);
9062}
9063
9064static void nfs4_layoutget_release(void *calldata)
9065{
9066        struct nfs4_layoutget *lgp = calldata;
9067
9068        dprintk("--> %s\n", __func__);
9069        nfs4_sequence_free_slot(&lgp->res.seq_res);
9070        pnfs_layoutget_free(lgp);
9071        dprintk("<-- %s\n", __func__);
9072}
9073
9074static const struct rpc_call_ops nfs4_layoutget_call_ops = {
9075        .rpc_call_prepare = nfs4_layoutget_prepare,
9076        .rpc_call_done = nfs4_layoutget_done,
9077        .rpc_release = nfs4_layoutget_release,
9078};
9079
9080struct pnfs_layout_segment *
9081nfs4_proc_layoutget(struct nfs4_layoutget *lgp, long *timeout)
9082{
9083        struct inode *inode = lgp->args.inode;
9084        struct nfs_server *server = NFS_SERVER(inode);
9085        struct rpc_task *task;
9086        struct rpc_message msg = {
9087                .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_LAYOUTGET],
9088                .rpc_argp = &lgp->args,
9089                .rpc_resp = &lgp->res,
9090                .rpc_cred = lgp->cred,
9091        };
9092        struct rpc_task_setup task_setup_data = {
9093                .rpc_client = server->client,
9094                .rpc_message = &msg,
9095                .callback_ops = &nfs4_layoutget_call_ops,
9096                .callback_data = lgp,
9097                .flags = RPC_TASK_ASYNC,
9098        };
9099        struct pnfs_layout_segment *lseg = NULL;
9100        struct nfs4_exception exception = {
9101                .inode = inode,
9102                .timeout = *timeout,
9103        };
9104        int status = 0;
9105
9106        dprintk("--> %s\n", __func__);
9107
9108        /* nfs4_layoutget_release calls pnfs_put_layout_hdr */
9109        pnfs_get_layout_hdr(NFS_I(inode)->layout);
9110
9111        nfs4_init_sequence(&lgp->args.seq_args, &lgp->res.seq_res, 0, 0);
9112
9113        task = rpc_run_task(&task_setup_data);
9114        if (IS_ERR(task))
9115                return ERR_CAST(task);
9116        status = rpc_wait_for_completion_task(task);
9117        if (status != 0)
9118                goto out;
9119
9120        if (task->tk_status < 0) {
9121                status = nfs4_layoutget_handle_exception(task, lgp, &exception);
9122                *timeout = exception.timeout;
9123        } else if (lgp->res.layoutp->len == 0) {
9124                status = -EAGAIN;
9125                *timeout = nfs4_update_delay(&exception.timeout);
9126        } else
9127                lseg = pnfs_layout_process(lgp);
9128out:
9129        trace_nfs4_layoutget(lgp->args.ctx,
9130                        &lgp->args.range,
9131                        &lgp->res.range,
9132                        &lgp->res.stateid,
9133                        status);
9134
9135        rpc_put_task(task);
9136        dprintk("<-- %s status=%d\n", __func__, status);
9137        if (status)
9138                return ERR_PTR(status);
9139        return lseg;
9140}
9141
9142static void
9143nfs4_layoutreturn_prepare(struct rpc_task *task, void *calldata)
9144{
9145        struct nfs4_layoutreturn *lrp = calldata;
9146
9147        dprintk("--> %s\n", __func__);
9148        nfs4_setup_sequence(lrp->clp,
9149                        &lrp->args.seq_args,
9150                        &lrp->res.seq_res,
9151                        task);
9152        if (!pnfs_layout_is_valid(lrp->args.layout))
9153                rpc_exit(task, 0);
9154}
9155
9156static void nfs4_layoutreturn_done(struct rpc_task *task, void *calldata)
9157{
9158        struct nfs4_layoutreturn *lrp = calldata;
9159        struct nfs_server *server;
9160
9161        dprintk("--> %s\n", __func__);
9162
9163        if (!nfs41_sequence_process(task, &lrp->res.seq_res))
9164                return;
9165
9166        /*
9167         * Was there an RPC level error? Assume the call succeeded,
9168         * and that we need to release the layout
9169         */
9170        if (task->tk_rpc_status != 0 && RPC_WAS_SENT(task)) {
9171                lrp->res.lrs_present = 0;
9172                return;
9173        }
9174
9175        server = NFS_SERVER(lrp->args.inode);
9176        switch (task->tk_status) {
9177        case -NFS4ERR_OLD_STATEID:
9178                if (nfs4_layout_refresh_old_stateid(&lrp->args.stateid,
9179                                        &lrp->args.range,
9180                                        lrp->args.inode))
9181                        goto out_restart;
9182                /* Fallthrough */
9183        default:
9184                task->tk_status = 0;
9185                /* Fallthrough */
9186        case 0:
9187                break;
9188        case -NFS4ERR_DELAY:
9189                if (nfs4_async_handle_error(task, server, NULL, NULL) != -EAGAIN)
9190                        break;
9191                goto out_restart;
9192        }
9193        dprintk("<-- %s\n", __func__);
9194        return;
9195out_restart:
9196        task->tk_status = 0;
9197        nfs4_sequence_free_slot(&lrp->res.seq_res);
9198        rpc_restart_call_prepare(task);
9199}
9200
9201static void nfs4_layoutreturn_release(void *calldata)
9202{
9203        struct nfs4_layoutreturn *lrp = calldata;
9204        struct pnfs_layout_hdr *lo = lrp->args.layout;
9205
9206        dprintk("--> %s\n", __func__);
9207        pnfs_layoutreturn_free_lsegs(lo, &lrp->args.stateid, &lrp->args.range,
9208                        lrp->res.lrs_present ? &lrp->res.stateid : NULL);
9209        nfs4_sequence_free_slot(&lrp->res.seq_res);
9210        if (lrp->ld_private.ops && lrp->ld_private.ops->free)
9211                lrp->ld_private.ops->free(&lrp->ld_private);
9212        pnfs_put_layout_hdr(lrp->args.layout);
9213        nfs_iput_and_deactive(lrp->inode);
9214        kfree(calldata);
9215        dprintk("<-- %s\n", __func__);
9216}
9217
9218static const struct rpc_call_ops nfs4_layoutreturn_call_ops = {
9219        .rpc_call_prepare = nfs4_layoutreturn_prepare,
9220        .rpc_call_done = nfs4_layoutreturn_done,
9221        .rpc_release = nfs4_layoutreturn_release,
9222};
9223
9224int nfs4_proc_layoutreturn(struct nfs4_layoutreturn *lrp, bool sync)
9225{
9226        struct rpc_task *task;
9227        struct rpc_message msg = {
9228                .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_LAYOUTRETURN],
9229                .rpc_argp = &lrp->args,
9230                .rpc_resp = &lrp->res,
9231                .rpc_cred = lrp->cred,
9232        };
9233        struct rpc_task_setup task_setup_data = {
9234                .rpc_client = NFS_SERVER(lrp->args.inode)->client,
9235                .rpc_message = &msg,
9236                .callback_ops = &nfs4_layoutreturn_call_ops,
9237                .callback_data = lrp,
9238        };
9239        int status = 0;
9240
9241        nfs4_state_protect(NFS_SERVER(lrp->args.inode)->nfs_client,
9242                        NFS_SP4_MACH_CRED_PNFS_CLEANUP,
9243                        &task_setup_data.rpc_client, &msg);
9244
9245        dprintk("--> %s\n", __func__);
9246        if (!sync) {
9247                lrp->inode = nfs_igrab_and_active(lrp->args.inode);
9248                if (!lrp->inode) {
9249                        nfs4_layoutreturn_release(lrp);
9250                        return -EAGAIN;
9251                }
9252                task_setup_data.flags |= RPC_TASK_ASYNC;
9253        }
9254        nfs4_init_sequence(&lrp->args.seq_args, &lrp->res.seq_res, 1, 0);
9255        task = rpc_run_task(&task_setup_data);
9256        if (IS_ERR(task))
9257                return PTR_ERR(task);
9258        if (sync)
9259                status = task->tk_status;
9260        trace_nfs4_layoutreturn(lrp->args.inode, &lrp->args.stateid, status);
9261        dprintk("<-- %s status=%d\n", __func__, status);
9262        rpc_put_task(task);
9263        return status;
9264}
9265
9266static int
9267_nfs4_proc_getdeviceinfo(struct nfs_server *server,
9268                struct pnfs_device *pdev,
9269                const struct cred *cred)
9270{
9271        struct nfs4_getdeviceinfo_args args = {
9272                .pdev = pdev,
9273                .notify_types = NOTIFY_DEVICEID4_CHANGE |
9274                        NOTIFY_DEVICEID4_DELETE,
9275        };
9276        struct nfs4_getdeviceinfo_res res = {
9277                .pdev = pdev,
9278        };
9279        struct rpc_message msg = {
9280                .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_GETDEVICEINFO],
9281                .rpc_argp = &args,
9282                .rpc_resp = &res,
9283                .rpc_cred = cred,
9284        };
9285        int status;
9286
9287        dprintk("--> %s\n", __func__);
9288        status = nfs4_call_sync(server->client, server, &msg, &args.seq_args, &res.seq_res, 0);
9289        if (res.notification & ~args.notify_types)
9290                dprintk("%s: unsupported notification\n", __func__);
9291        if (res.notification != args.notify_types)
9292                pdev->nocache = 1;
9293
9294        dprintk("<-- %s status=%d\n", __func__, status);
9295
9296        return status;
9297}
9298
9299int nfs4_proc_getdeviceinfo(struct nfs_server *server,
9300                struct pnfs_device *pdev,
9301                const struct cred *cred)
9302{
9303        struct nfs4_exception exception = { };
9304        int err;
9305
9306        do {
9307                err = nfs4_handle_exception(server,
9308                                        _nfs4_proc_getdeviceinfo(server, pdev, cred),
9309                                        &exception);
9310        } while (exception.retry);
9311        return err;
9312}
9313EXPORT_SYMBOL_GPL(nfs4_proc_getdeviceinfo);
9314
9315static void nfs4_layoutcommit_prepare(struct rpc_task *task, void *calldata)
9316{
9317        struct nfs4_layoutcommit_data *data = calldata;
9318        struct nfs_server *server = NFS_SERVER(data->args.inode);
9319
9320        nfs4_setup_sequence(server->nfs_client,
9321                        &data->args.seq_args,
9322                        &data->res.seq_res,
9323                        task);
9324}
9325
9326static void
9327nfs4_layoutcommit_done(struct rpc_task *task, void *calldata)
9328{
9329        struct nfs4_layoutcommit_data *data = calldata;
9330        struct nfs_server *server = NFS_SERVER(data->args.inode);
9331
9332        if (!nfs41_sequence_done(task, &data->res.seq_res))
9333                return;
9334
9335        switch (task->tk_status) { /* Just ignore these failures */
9336        case -NFS4ERR_DELEG_REVOKED: /* layout was recalled */
9337        case -NFS4ERR_BADIOMODE:     /* no IOMODE_RW layout for range */
9338        case -NFS4ERR_BADLAYOUT:     /* no layout */
9339        case -NFS4ERR_GRACE:        /* loca_recalim always false */
9340                task->tk_status = 0;
9341        case 0:
9342                break;
9343        default:
9344                if (nfs4_async_handle_error(task, server, NULL, NULL) == -EAGAIN) {
9345                        rpc_restart_call_prepare(task);
9346                        return;
9347                }
9348        }
9349}
9350
9351static void nfs4_layoutcommit_release(void *calldata)
9352{
9353        struct nfs4_layoutcommit_data *data = calldata;
9354
9355        pnfs_cleanup_layoutcommit(data);
9356        nfs_post_op_update_inode_force_wcc(data->args.inode,
9357                                           data->res.fattr);
9358        put_cred(data->cred);
9359        nfs_iput_and_deactive(data->inode);
9360        kfree(data);
9361}
9362
9363static const struct rpc_call_ops nfs4_layoutcommit_ops = {
9364        .rpc_call_prepare = nfs4_layoutcommit_prepare,
9365        .rpc_call_done = nfs4_layoutcommit_done,
9366        .rpc_release = nfs4_layoutcommit_release,
9367};
9368
9369int
9370nfs4_proc_layoutcommit(struct nfs4_layoutcommit_data *data, bool sync)
9371{
9372        struct rpc_message msg = {
9373                .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_LAYOUTCOMMIT],
9374                .rpc_argp = &data->args,
9375                .rpc_resp = &data->res,
9376                .rpc_cred = data->cred,
9377        };
9378        struct rpc_task_setup task_setup_data = {
9379                .task = &data->task,
9380                .rpc_client = NFS_CLIENT(data->args.inode),
9381                .rpc_message = &msg,
9382                .callback_ops = &nfs4_layoutcommit_ops,
9383                .callback_data = data,
9384        };
9385        struct rpc_task *task;
9386        int status = 0;
9387
9388        dprintk("NFS: initiating layoutcommit call. sync %d "
9389                "lbw: %llu inode %lu\n", sync,
9390                data->args.lastbytewritten,
9391                data->args.inode->i_ino);
9392
9393        if (!sync) {
9394                data->inode = nfs_igrab_and_active(data->args.inode);
9395                if (data->inode == NULL) {
9396                        nfs4_layoutcommit_release(data);
9397                        return -EAGAIN;
9398                }
9399                task_setup_data.flags = RPC_TASK_ASYNC;
9400        }
9401        nfs4_init_sequence(&data->args.seq_args, &data->res.seq_res, 1, 0);
9402        task = rpc_run_task(&task_setup_data);
9403        if (IS_ERR(task))
9404                return PTR_ERR(task);
9405        if (sync)
9406                status = task->tk_status;
9407        trace_nfs4_layoutcommit(data->args.inode, &data->args.stateid, status);
9408        dprintk("%s: status %d\n", __func__, status);
9409        rpc_put_task(task);
9410        return status;
9411}
9412
9413/*
9414 * Use the state managment nfs_client cl_rpcclient, which uses krb5i (if
9415 * possible) as per RFC3530bis and RFC5661 Security Considerations sections
9416 */
9417static int
9418_nfs41_proc_secinfo_no_name(struct nfs_server *server, struct nfs_fh *fhandle,
9419                    struct nfs_fsinfo *info,
9420                    struct nfs4_secinfo_flavors *flavors, bool use_integrity)
9421{
9422        struct nfs41_secinfo_no_name_args args = {
9423                .style = SECINFO_STYLE_CURRENT_FH,
9424        };
9425        struct nfs4_secinfo_res res = {
9426                .flavors = flavors,
9427        };
9428        struct rpc_message msg = {
9429                .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_SECINFO_NO_NAME],
9430                .rpc_argp = &args,
9431                .rpc_resp = &res,
9432        };
9433        struct rpc_clnt *clnt = server->client;
9434        struct nfs4_call_sync_data data = {
9435                .seq_server = server,
9436                .seq_args = &args.seq_args,
9437                .seq_res = &res.seq_res,
9438        };
9439        struct rpc_task_setup task_setup = {
9440                .rpc_client = server->client,
9441                .rpc_message = &msg,
9442                .callback_ops = server->nfs_client->cl_mvops->call_sync_ops,
9443                .callback_data = &data,
9444                .flags = RPC_TASK_NO_ROUND_ROBIN,
9445        };
9446        const struct cred *cred = NULL;
9447        int status;
9448
9449        if (use_integrity) {
9450                clnt = server->nfs_client->cl_rpcclient;
9451                task_setup.rpc_client = clnt;
9452
9453                cred = nfs4_get_clid_cred(server->nfs_client);
9454                msg.rpc_cred = cred;
9455        }
9456
9457        dprintk("--> %s\n", __func__);
9458        nfs4_init_sequence(&args.seq_args, &res.seq_res, 0, 0);
9459        status = nfs4_call_sync_custom(&task_setup);
9460        dprintk("<-- %s status=%d\n", __func__, status);
9461
9462        put_cred(cred);
9463
9464        return status;
9465}
9466
9467static int
9468nfs41_proc_secinfo_no_name(struct nfs_server *server, struct nfs_fh *fhandle,
9469                           struct nfs_fsinfo *info, struct nfs4_secinfo_flavors *flavors)
9470{
9471        struct nfs4_exception exception = {
9472                .interruptible = true,
9473        };
9474        int err;
9475        do {
9476                /* first try using integrity protection */
9477                err = -NFS4ERR_WRONGSEC;
9478
9479                /* try to use integrity protection with machine cred */
9480                if (_nfs4_is_integrity_protected(server->nfs_client))
9481                        err = _nfs41_proc_secinfo_no_name(server, fhandle, info,
9482                                                          flavors, true);
9483
9484                /*
9485                 * if unable to use integrity protection, or SECINFO with
9486                 * integrity protection returns NFS4ERR_WRONGSEC (which is
9487                 * disallowed by spec, but exists in deployed servers) use
9488                 * the current filesystem's rpc_client and the user cred.
9489                 */
9490                if (err == -NFS4ERR_WRONGSEC)
9491                        err = _nfs41_proc_secinfo_no_name(server, fhandle, info,
9492                                                          flavors, false);
9493
9494                switch (err) {
9495                case 0:
9496                case -NFS4ERR_WRONGSEC:
9497                case -ENOTSUPP:
9498                        goto out;
9499                default:
9500                        err = nfs4_handle_exception(server, err, &exception);
9501                }
9502        } while (exception.retry);
9503out:
9504        return err;
9505}
9506
9507static int
9508nfs41_find_root_sec(struct nfs_server *server, struct nfs_fh *fhandle,
9509                    struct nfs_fsinfo *info)
9510{
9511        int err;
9512        struct page *page;
9513        rpc_authflavor_t flavor = RPC_AUTH_MAXFLAVOR;
9514        struct nfs4_secinfo_flavors *flavors;
9515        struct nfs4_secinfo4 *secinfo;
9516        int i;
9517
9518        page = alloc_page(GFP_KERNEL);
9519        if (!page) {
9520                err = -ENOMEM;
9521                goto out;
9522        }
9523
9524        flavors = page_address(page);
9525        err = nfs41_proc_secinfo_no_name(server, fhandle, info, flavors);
9526
9527        /*
9528         * Fall back on "guess and check" method if
9529         * the server doesn't support SECINFO_NO_NAME
9530         */
9531        if (err == -NFS4ERR_WRONGSEC || err == -ENOTSUPP) {
9532                err = nfs4_find_root_sec(server, fhandle, info);
9533                goto out_freepage;
9534        }
9535        if (err)
9536                goto out_freepage;
9537
9538        for (i = 0; i < flavors->num_flavors; i++) {
9539                secinfo = &flavors->flavors[i];
9540
9541                switch (secinfo->flavor) {
9542                case RPC_AUTH_NULL:
9543                case RPC_AUTH_UNIX:
9544                case RPC_AUTH_GSS:
9545                        flavor = rpcauth_get_pseudoflavor(secinfo->flavor,
9546                                        &secinfo->flavor_info);
9547                        break;
9548                default:
9549                        flavor = RPC_AUTH_MAXFLAVOR;
9550                        break;
9551                }
9552
9553                if (!nfs_auth_info_match(&server->auth_info, flavor))
9554                        flavor = RPC_AUTH_MAXFLAVOR;
9555
9556                if (flavor != RPC_AUTH_MAXFLAVOR) {
9557                        err = nfs4_lookup_root_sec(server, fhandle,
9558                                                   info, flavor);
9559                        if (!err)
9560                                break;
9561                }
9562        }
9563
9564        if (flavor == RPC_AUTH_MAXFLAVOR)
9565                err = -EPERM;
9566
9567out_freepage:
9568        put_page(page);
9569        if (err == -EACCES)
9570                return -EPERM;
9571out:
9572        return err;
9573}
9574
9575static int _nfs41_test_stateid(struct nfs_server *server,
9576                nfs4_stateid *stateid,
9577                const struct cred *cred)
9578{
9579        int status;
9580        struct nfs41_test_stateid_args args = {
9581                .stateid = stateid,
9582        };
9583        struct nfs41_test_stateid_res res;
9584        struct rpc_message msg = {
9585                .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_TEST_STATEID],
9586                .rpc_argp = &args,
9587                .rpc_resp = &res,
9588                .rpc_cred = cred,
9589        };
9590        struct rpc_clnt *rpc_client = server->client;
9591
9592        nfs4_state_protect(server->nfs_client, NFS_SP4_MACH_CRED_STATEID,
9593                &rpc_client, &msg);
9594
9595        dprintk("NFS call  test_stateid %p\n", stateid);
9596        nfs4_init_sequence(&args.seq_args, &res.seq_res, 0, 1);
9597        status = nfs4_call_sync_sequence(rpc_client, server, &msg,
9598                        &args.seq_args, &res.seq_res);
9599        if (status != NFS_OK) {
9600                dprintk("NFS reply test_stateid: failed, %d\n", status);
9601                return status;
9602        }
9603        dprintk("NFS reply test_stateid: succeeded, %d\n", -res.status);
9604        return -res.status;
9605}
9606
9607static void nfs4_handle_delay_or_session_error(struct nfs_server *server,
9608                int err, struct nfs4_exception *exception)
9609{
9610        exception->retry = 0;
9611        switch(err) {
9612        case -NFS4ERR_DELAY:
9613        case -NFS4ERR_RETRY_UNCACHED_REP:
9614                nfs4_handle_exception(server, err, exception);
9615                break;
9616        case -NFS4ERR_BADSESSION:
9617        case -NFS4ERR_BADSLOT:
9618        case -NFS4ERR_BAD_HIGH_SLOT:
9619        case -NFS4ERR_CONN_NOT_BOUND_TO_SESSION:
9620        case -NFS4ERR_DEADSESSION:
9621                nfs4_do_handle_exception(server, err, exception);
9622        }
9623}
9624
9625/**
9626 * nfs41_test_stateid - perform a TEST_STATEID operation
9627 *
9628 * @server: server / transport on which to perform the operation
9629 * @stateid: state ID to test
9630 * @cred: credential
9631 *
9632 * Returns NFS_OK if the server recognizes that "stateid" is valid.
9633 * Otherwise a negative NFS4ERR value is returned if the operation
9634 * failed or the state ID is not currently valid.
9635 */
9636static int nfs41_test_stateid(struct nfs_server *server,
9637                nfs4_stateid *stateid,
9638                const struct cred *cred)
9639{
9640        struct nfs4_exception exception = {
9641                .interruptible = true,
9642        };
9643        int err;
9644        do {
9645                err = _nfs41_test_stateid(server, stateid, cred);
9646                nfs4_handle_delay_or_session_error(server, err, &exception);
9647        } while (exception.retry);
9648        return err;
9649}
9650
9651struct nfs_free_stateid_data {
9652        struct nfs_server *server;
9653        struct nfs41_free_stateid_args args;
9654        struct nfs41_free_stateid_res res;
9655};
9656
9657static void nfs41_free_stateid_prepare(struct rpc_task *task, void *calldata)
9658{
9659        struct nfs_free_stateid_data *data = calldata;
9660        nfs4_setup_sequence(data->server->nfs_client,
9661                        &data->args.seq_args,
9662                        &data->res.seq_res,
9663                        task);
9664}
9665
9666static void nfs41_free_stateid_done(struct rpc_task *task, void *calldata)
9667{
9668        struct nfs_free_stateid_data *data = calldata;
9669
9670        nfs41_sequence_done(task, &data->res.seq_res);
9671
9672        switch (task->tk_status) {
9673        case -NFS4ERR_DELAY:
9674                if (nfs4_async_handle_error(task, data->server, NULL, NULL) == -EAGAIN)
9675                        rpc_restart_call_prepare(task);
9676        }
9677}
9678
9679static void nfs41_free_stateid_release(void *calldata)
9680{
9681        kfree(calldata);
9682}
9683
9684static const struct rpc_call_ops nfs41_free_stateid_ops = {
9685        .rpc_call_prepare = nfs41_free_stateid_prepare,
9686        .rpc_call_done = nfs41_free_stateid_done,
9687        .rpc_release = nfs41_free_stateid_release,
9688};
9689
9690/**
9691 * nfs41_free_stateid - perform a FREE_STATEID operation
9692 *
9693 * @server: server / transport on which to perform the operation
9694 * @stateid: state ID to release
9695 * @cred: credential
9696 * @privileged: set to true if this call needs to be privileged
9697 *
9698 * Note: this function is always asynchronous.
9699 */
9700static int nfs41_free_stateid(struct nfs_server *server,
9701                const nfs4_stateid *stateid,
9702                const struct cred *cred,
9703                bool privileged)
9704{
9705        struct rpc_message msg = {
9706                .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_FREE_STATEID],
9707                .rpc_cred = cred,
9708        };
9709        struct rpc_task_setup task_setup = {
9710                .rpc_client = server->client,
9711                .rpc_message = &msg,
9712                .callback_ops = &nfs41_free_stateid_ops,
9713                .flags = RPC_TASK_ASYNC,
9714        };
9715        struct nfs_free_stateid_data *data;
9716        struct rpc_task *task;
9717
9718        nfs4_state_protect(server->nfs_client, NFS_SP4_MACH_CRED_STATEID,
9719                &task_setup.rpc_client, &msg);
9720
9721        dprintk("NFS call  free_stateid %p\n", stateid);
9722        data = kmalloc(sizeof(*data), GFP_NOFS);
9723        if (!data)
9724                return -ENOMEM;
9725        data->server = server;
9726        nfs4_stateid_copy(&data->args.stateid, stateid);
9727
9728        task_setup.callback_data = data;
9729
9730        msg.rpc_argp = &data->args;
9731        msg.rpc_resp = &data->res;
9732        nfs4_init_sequence(&data->args.seq_args, &data->res.seq_res, 1, privileged);
9733        task = rpc_run_task(&task_setup);
9734        if (IS_ERR(task))
9735                return PTR_ERR(task);
9736        rpc_put_task(task);
9737        return 0;
9738}
9739
9740static void
9741nfs41_free_lock_state(struct nfs_server *server, struct nfs4_lock_state *lsp)
9742{
9743        const struct cred *cred = lsp->ls_state->owner->so_cred;
9744
9745        nfs41_free_stateid(server, &lsp->ls_stateid, cred, false);
9746        nfs4_free_lock_state(server, lsp);
9747}
9748
9749static bool nfs41_match_stateid(const nfs4_stateid *s1,
9750                const nfs4_stateid *s2)
9751{
9752        if (s1->type != s2->type)
9753                return false;
9754
9755        if (memcmp(s1->other, s2->other, sizeof(s1->other)) != 0)
9756                return false;
9757
9758        if (s1->seqid == s2->seqid)
9759                return true;
9760
9761        return s1->seqid == 0 || s2->seqid == 0;
9762}
9763
9764#endif /* CONFIG_NFS_V4_1 */
9765
9766static bool nfs4_match_stateid(const nfs4_stateid *s1,
9767                const nfs4_stateid *s2)
9768{
9769        return nfs4_stateid_match(s1, s2);
9770}
9771
9772
9773static const struct nfs4_state_recovery_ops nfs40_reboot_recovery_ops = {
9774        .owner_flag_bit = NFS_OWNER_RECLAIM_REBOOT,
9775        .state_flag_bit = NFS_STATE_RECLAIM_REBOOT,
9776        .recover_open   = nfs4_open_reclaim,
9777        .recover_lock   = nfs4_lock_reclaim,
9778        .establish_clid = nfs4_init_clientid,
9779        .detect_trunking = nfs40_discover_server_trunking,
9780};
9781
9782#if defined(CONFIG_NFS_V4_1)
9783static const struct nfs4_state_recovery_ops nfs41_reboot_recovery_ops = {
9784        .owner_flag_bit = NFS_OWNER_RECLAIM_REBOOT,
9785        .state_flag_bit = NFS_STATE_RECLAIM_REBOOT,
9786        .recover_open   = nfs4_open_reclaim,
9787        .recover_lock   = nfs4_lock_reclaim,
9788        .establish_clid = nfs41_init_clientid,
9789        .reclaim_complete = nfs41_proc_reclaim_complete,
9790        .detect_trunking = nfs41_discover_server_trunking,
9791};
9792#endif /* CONFIG_NFS_V4_1 */
9793
9794static const struct nfs4_state_recovery_ops nfs40_nograce_recovery_ops = {
9795        .owner_flag_bit = NFS_OWNER_RECLAIM_NOGRACE,
9796        .state_flag_bit = NFS_STATE_RECLAIM_NOGRACE,
9797        .recover_open   = nfs40_open_expired,
9798        .recover_lock   = nfs4_lock_expired,
9799        .establish_clid = nfs4_init_clientid,
9800};
9801
9802#if defined(CONFIG_NFS_V4_1)
9803static const struct nfs4_state_recovery_ops nfs41_nograce_recovery_ops = {
9804        .owner_flag_bit = NFS_OWNER_RECLAIM_NOGRACE,
9805        .state_flag_bit = NFS_STATE_RECLAIM_NOGRACE,
9806        .recover_open   = nfs41_open_expired,
9807        .recover_lock   = nfs41_lock_expired,
9808        .establish_clid = nfs41_init_clientid,
9809};
9810#endif /* CONFIG_NFS_V4_1 */
9811
9812static const struct nfs4_state_maintenance_ops nfs40_state_renewal_ops = {
9813        .sched_state_renewal = nfs4_proc_async_renew,
9814        .get_state_renewal_cred = nfs4_get_renew_cred,
9815        .renew_lease = nfs4_proc_renew,
9816};
9817
9818#if defined(CONFIG_NFS_V4_1)
9819static const struct nfs4_state_maintenance_ops nfs41_state_renewal_ops = {
9820        .sched_state_renewal = nfs41_proc_async_sequence,
9821        .get_state_renewal_cred = nfs4_get_machine_cred,
9822        .renew_lease = nfs4_proc_sequence,
9823};
9824#endif
9825
9826static const struct nfs4_mig_recovery_ops nfs40_mig_recovery_ops = {
9827        .get_locations = _nfs40_proc_get_locations,
9828        .fsid_present = _nfs40_proc_fsid_present,
9829};
9830
9831#if defined(CONFIG_NFS_V4_1)
9832static const struct nfs4_mig_recovery_ops nfs41_mig_recovery_ops = {
9833        .get_locations = _nfs41_proc_get_locations,
9834        .fsid_present = _nfs41_proc_fsid_present,
9835};
9836#endif  /* CONFIG_NFS_V4_1 */
9837
9838static const struct nfs4_minor_version_ops nfs_v4_0_minor_ops = {
9839        .minor_version = 0,
9840        .init_caps = NFS_CAP_READDIRPLUS
9841                | NFS_CAP_ATOMIC_OPEN
9842                | NFS_CAP_POSIX_LOCK,
9843        .init_client = nfs40_init_client,
9844        .shutdown_client = nfs40_shutdown_client,
9845        .match_stateid = nfs4_match_stateid,
9846        .find_root_sec = nfs4_find_root_sec,
9847        .free_lock_state = nfs4_release_lockowner,
9848        .test_and_free_expired = nfs40_test_and_free_expired_stateid,
9849        .alloc_seqid = nfs_alloc_seqid,
9850        .call_sync_ops = &nfs40_call_sync_ops,
9851        .reboot_recovery_ops = &nfs40_reboot_recovery_ops,
9852        .nograce_recovery_ops = &nfs40_nograce_recovery_ops,
9853        .state_renewal_ops = &nfs40_state_renewal_ops,
9854        .mig_recovery_ops = &nfs40_mig_recovery_ops,
9855};
9856
9857#if defined(CONFIG_NFS_V4_1)
9858static struct nfs_seqid *
9859nfs_alloc_no_seqid(struct nfs_seqid_counter *arg1, gfp_t arg2)
9860{
9861        return NULL;
9862}
9863
9864static const struct nfs4_minor_version_ops nfs_v4_1_minor_ops = {
9865        .minor_version = 1,
9866        .init_caps = NFS_CAP_READDIRPLUS
9867                | NFS_CAP_ATOMIC_OPEN
9868                | NFS_CAP_POSIX_LOCK
9869                | NFS_CAP_STATEID_NFSV41
9870                | NFS_CAP_ATOMIC_OPEN_V1
9871                | NFS_CAP_LGOPEN,
9872        .init_client = nfs41_init_client,
9873        .shutdown_client = nfs41_shutdown_client,
9874        .match_stateid = nfs41_match_stateid,
9875        .find_root_sec = nfs41_find_root_sec,
9876        .free_lock_state = nfs41_free_lock_state,
9877        .test_and_free_expired = nfs41_test_and_free_expired_stateid,
9878        .alloc_seqid = nfs_alloc_no_seqid,
9879        .session_trunk = nfs4_test_session_trunk,
9880        .call_sync_ops = &nfs41_call_sync_ops,
9881        .reboot_recovery_ops = &nfs41_reboot_recovery_ops,
9882        .nograce_recovery_ops = &nfs41_nograce_recovery_ops,
9883        .state_renewal_ops = &nfs41_state_renewal_ops,
9884        .mig_recovery_ops = &nfs41_mig_recovery_ops,
9885};
9886#endif
9887
9888#if defined(CONFIG_NFS_V4_2)
9889static const struct nfs4_minor_version_ops nfs_v4_2_minor_ops = {
9890        .minor_version = 2,
9891        .init_caps = NFS_CAP_READDIRPLUS
9892                | NFS_CAP_ATOMIC_OPEN
9893                | NFS_CAP_POSIX_LOCK
9894                | NFS_CAP_STATEID_NFSV41
9895                | NFS_CAP_ATOMIC_OPEN_V1
9896                | NFS_CAP_LGOPEN
9897                | NFS_CAP_ALLOCATE
9898                | NFS_CAP_COPY
9899                | NFS_CAP_OFFLOAD_CANCEL
9900                | NFS_CAP_DEALLOCATE
9901                | NFS_CAP_SEEK
9902                | NFS_CAP_LAYOUTSTATS
9903                | NFS_CAP_CLONE
9904                | NFS_CAP_LAYOUTERROR,
9905        .init_client = nfs41_init_client,
9906        .shutdown_client = nfs41_shutdown_client,
9907        .match_stateid = nfs41_match_stateid,
9908        .find_root_sec = nfs41_find_root_sec,
9909        .free_lock_state = nfs41_free_lock_state,
9910        .call_sync_ops = &nfs41_call_sync_ops,
9911        .test_and_free_expired = nfs41_test_and_free_expired_stateid,
9912        .alloc_seqid = nfs_alloc_no_seqid,
9913        .session_trunk = nfs4_test_session_trunk,
9914        .reboot_recovery_ops = &nfs41_reboot_recovery_ops,
9915        .nograce_recovery_ops = &nfs41_nograce_recovery_ops,
9916        .state_renewal_ops = &nfs41_state_renewal_ops,
9917        .mig_recovery_ops = &nfs41_mig_recovery_ops,
9918};
9919#endif
9920
9921const struct nfs4_minor_version_ops *nfs_v4_minor_ops[] = {
9922        [0] = &nfs_v4_0_minor_ops,
9923#if defined(CONFIG_NFS_V4_1)
9924        [1] = &nfs_v4_1_minor_ops,
9925#endif
9926#if defined(CONFIG_NFS_V4_2)
9927        [2] = &nfs_v4_2_minor_ops,
9928#endif
9929};
9930
9931static ssize_t nfs4_listxattr(struct dentry *dentry, char *list, size_t size)
9932{
9933        ssize_t error, error2;
9934
9935        error = generic_listxattr(dentry, list, size);
9936        if (error < 0)
9937                return error;
9938        if (list) {
9939                list += error;
9940                size -= error;
9941        }
9942
9943        error2 = nfs4_listxattr_nfs4_label(d_inode(dentry), list, size);
9944        if (error2 < 0)
9945                return error2;
9946        return error + error2;
9947}
9948
9949static const struct inode_operations nfs4_dir_inode_operations = {
9950        .create         = nfs_create,
9951        .lookup         = nfs_lookup,
9952        .atomic_open    = nfs_atomic_open,
9953        .link           = nfs_link,
9954        .unlink         = nfs_unlink,
9955        .symlink        = nfs_symlink,
9956        .mkdir          = nfs_mkdir,
9957        .rmdir          = nfs_rmdir,
9958        .mknod          = nfs_mknod,
9959        .rename         = nfs_rename,
9960        .permission     = nfs_permission,
9961        .getattr        = nfs_getattr,
9962        .setattr        = nfs_setattr,
9963        .listxattr      = nfs4_listxattr,
9964};
9965
9966static const struct inode_operations nfs4_file_inode_operations = {
9967        .permission     = nfs_permission,
9968        .getattr        = nfs_getattr,
9969        .setattr        = nfs_setattr,
9970        .listxattr      = nfs4_listxattr,
9971};
9972
9973const struct nfs_rpc_ops nfs_v4_clientops = {
9974        .version        = 4,                    /* protocol version */
9975        .dentry_ops     = &nfs4_dentry_operations,
9976        .dir_inode_ops  = &nfs4_dir_inode_operations,
9977        .file_inode_ops = &nfs4_file_inode_operations,
9978        .file_ops       = &nfs4_file_operations,
9979        .getroot        = nfs4_proc_get_root,
9980        .submount       = nfs4_submount,
9981        .try_mount      = nfs4_try_mount,
9982        .getattr        = nfs4_proc_getattr,
9983        .setattr        = nfs4_proc_setattr,
9984        .lookup         = nfs4_proc_lookup,
9985        .lookupp        = nfs4_proc_lookupp,
9986        .access         = nfs4_proc_access,
9987        .readlink       = nfs4_proc_readlink,
9988        .create         = nfs4_proc_create,
9989        .remove         = nfs4_proc_remove,
9990        .unlink_setup   = nfs4_proc_unlink_setup,
9991        .unlink_rpc_prepare = nfs4_proc_unlink_rpc_prepare,
9992        .unlink_done    = nfs4_proc_unlink_done,
9993        .rename_setup   = nfs4_proc_rename_setup,
9994        .rename_rpc_prepare = nfs4_proc_rename_rpc_prepare,
9995        .rename_done    = nfs4_proc_rename_done,
9996        .link           = nfs4_proc_link,
9997        .symlink        = nfs4_proc_symlink,
9998        .mkdir          = nfs4_proc_mkdir,
9999        .rmdir          = nfs4_proc_rmdir,
10000        .readdir        = nfs4_proc_readdir,
10001        .mknod          = nfs4_proc_mknod,
10002        .statfs         = nfs4_proc_statfs,
10003        .fsinfo         = nfs4_proc_fsinfo,
10004        .pathconf       = nfs4_proc_pathconf,
10005        .set_capabilities = nfs4_server_capabilities,
10006        .decode_dirent  = nfs4_decode_dirent,
10007        .pgio_rpc_prepare = nfs4_proc_pgio_rpc_prepare,
10008        .read_setup     = nfs4_proc_read_setup,
10009        .read_done      = nfs4_read_done,
10010        .write_setup    = nfs4_proc_write_setup,
10011        .write_done     = nfs4_write_done,
10012        .commit_setup   = nfs4_proc_commit_setup,
10013        .commit_rpc_prepare = nfs4_proc_commit_rpc_prepare,
10014        .commit_done    = nfs4_commit_done,
10015        .lock           = nfs4_proc_lock,
10016        .clear_acl_cache = nfs4_zap_acl_attr,
10017        .close_context  = nfs4_close_context,
10018        .open_context   = nfs4_atomic_open,
10019        .have_delegation = nfs4_have_delegation,
10020        .alloc_client   = nfs4_alloc_client,
10021        .init_client    = nfs4_init_client,
10022        .free_client    = nfs4_free_client,
10023        .create_server  = nfs4_create_server,
10024        .clone_server   = nfs_clone_server,
10025};
10026
10027static const struct xattr_handler nfs4_xattr_nfs4_acl_handler = {
10028        .name   = XATTR_NAME_NFSV4_ACL,
10029        .list   = nfs4_xattr_list_nfs4_acl,
10030        .get    = nfs4_xattr_get_nfs4_acl,
10031        .set    = nfs4_xattr_set_nfs4_acl,
10032};
10033
10034const struct xattr_handler *nfs4_xattr_handlers[] = {
10035        &nfs4_xattr_nfs4_acl_handler,
10036#ifdef CONFIG_NFS_V4_SECURITY_LABEL
10037        &nfs4_xattr_nfs4_label_handler,
10038#endif
10039        NULL
10040};
10041
10042/*
10043 * Local variables:
10044 *  c-basic-offset: 8
10045 * End:
10046 */
10047