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 "sysfs.h"
  67#include "nfs4idmap.h"
  68#include "nfs4session.h"
  69#include "fscache.h"
  70#include "nfs42.h"
  71
  72#include "nfs4trace.h"
  73
  74#define NFSDBG_FACILITY         NFSDBG_PROC
  75
  76#define NFS4_BITMASK_SZ         3
  77
  78#define NFS4_POLL_RETRY_MIN     (HZ/10)
  79#define NFS4_POLL_RETRY_MAX     (15*HZ)
  80
  81/* file attributes which can be mapped to nfs attributes */
  82#define NFS4_VALID_ATTRS (ATTR_MODE \
  83        | ATTR_UID \
  84        | ATTR_GID \
  85        | ATTR_SIZE \
  86        | ATTR_ATIME \
  87        | ATTR_MTIME \
  88        | ATTR_CTIME \
  89        | ATTR_ATIME_SET \
  90        | ATTR_MTIME_SET)
  91
  92struct nfs4_opendata;
  93static int _nfs4_recover_proc_open(struct nfs4_opendata *data);
  94static int nfs4_do_fsinfo(struct nfs_server *, struct nfs_fh *, struct nfs_fsinfo *);
  95static void nfs_fixup_referral_attributes(struct nfs_fattr *fattr);
  96static int _nfs4_proc_getattr(struct nfs_server *server, struct nfs_fh *fhandle, struct nfs_fattr *fattr, struct nfs4_label *label, struct inode *inode);
  97static int nfs4_do_setattr(struct inode *inode, const struct cred *cred,
  98                            struct nfs_fattr *fattr, struct iattr *sattr,
  99                            struct nfs_open_context *ctx, struct nfs4_label *ilabel,
 100                            struct nfs4_label *olabel);
 101#ifdef CONFIG_NFS_V4_1
 102static struct rpc_task *_nfs41_proc_sequence(struct nfs_client *clp,
 103                const struct cred *cred,
 104                struct nfs4_slot *slot,
 105                bool is_privileged);
 106static int nfs41_test_stateid(struct nfs_server *, nfs4_stateid *,
 107                const struct cred *);
 108static int nfs41_free_stateid(struct nfs_server *, const nfs4_stateid *,
 109                const struct cred *, bool);
 110#endif
 111static void nfs4_bitmask_set(__u32 bitmask[NFS4_BITMASK_SZ],
 112                             const __u32 *src, struct inode *inode,
 113                             struct nfs_server *server,
 114                             struct nfs4_label *label);
 115
 116#ifdef CONFIG_NFS_V4_SECURITY_LABEL
 117static inline struct nfs4_label *
 118nfs4_label_init_security(struct inode *dir, struct dentry *dentry,
 119        struct iattr *sattr, struct nfs4_label *label)
 120{
 121        int err;
 122
 123        if (label == NULL)
 124                return NULL;
 125
 126        if (nfs_server_capable(dir, NFS_CAP_SECURITY_LABEL) == 0)
 127                return NULL;
 128
 129        err = security_dentry_init_security(dentry, sattr->ia_mode,
 130                                &dentry->d_name, (void **)&label->label, &label->len);
 131        if (err == 0)
 132                return label;
 133
 134        return NULL;
 135}
 136static inline void
 137nfs4_label_release_security(struct nfs4_label *label)
 138{
 139        if (label)
 140                security_release_secctx(label->label, label->len);
 141}
 142static inline u32 *nfs4_bitmask(struct nfs_server *server, struct nfs4_label *label)
 143{
 144        if (label)
 145                return server->attr_bitmask;
 146
 147        return server->attr_bitmask_nl;
 148}
 149#else
 150static inline struct nfs4_label *
 151nfs4_label_init_security(struct inode *dir, struct dentry *dentry,
 152        struct iattr *sattr, struct nfs4_label *l)
 153{ return NULL; }
 154static inline void
 155nfs4_label_release_security(struct nfs4_label *label)
 156{ return; }
 157static inline u32 *
 158nfs4_bitmask(struct nfs_server *server, struct nfs4_label *label)
 159{ return server->attr_bitmask; }
 160#endif
 161
 162/* Prevent leaks of NFSv4 errors into userland */
 163static int nfs4_map_errors(int err)
 164{
 165        if (err >= -1000)
 166                return err;
 167        switch (err) {
 168        case -NFS4ERR_RESOURCE:
 169        case -NFS4ERR_LAYOUTTRYLATER:
 170        case -NFS4ERR_RECALLCONFLICT:
 171                return -EREMOTEIO;
 172        case -NFS4ERR_WRONGSEC:
 173        case -NFS4ERR_WRONG_CRED:
 174                return -EPERM;
 175        case -NFS4ERR_BADOWNER:
 176        case -NFS4ERR_BADNAME:
 177                return -EINVAL;
 178        case -NFS4ERR_SHARE_DENIED:
 179                return -EACCES;
 180        case -NFS4ERR_MINOR_VERS_MISMATCH:
 181                return -EPROTONOSUPPORT;
 182        case -NFS4ERR_FILE_OPEN:
 183                return -EBUSY;
 184        case -NFS4ERR_NOT_SAME:
 185                return -ENOTSYNC;
 186        default:
 187                dprintk("%s could not handle NFSv4 error %d\n",
 188                                __func__, -err);
 189                break;
 190        }
 191        return -EIO;
 192}
 193
 194/*
 195 * This is our standard bitmap for GETATTR requests.
 196 */
 197const u32 nfs4_fattr_bitmap[3] = {
 198        FATTR4_WORD0_TYPE
 199        | FATTR4_WORD0_CHANGE
 200        | FATTR4_WORD0_SIZE
 201        | FATTR4_WORD0_FSID
 202        | FATTR4_WORD0_FILEID,
 203        FATTR4_WORD1_MODE
 204        | FATTR4_WORD1_NUMLINKS
 205        | FATTR4_WORD1_OWNER
 206        | FATTR4_WORD1_OWNER_GROUP
 207        | FATTR4_WORD1_RAWDEV
 208        | FATTR4_WORD1_SPACE_USED
 209        | FATTR4_WORD1_TIME_ACCESS
 210        | FATTR4_WORD1_TIME_METADATA
 211        | FATTR4_WORD1_TIME_MODIFY
 212        | FATTR4_WORD1_MOUNTED_ON_FILEID,
 213#ifdef CONFIG_NFS_V4_SECURITY_LABEL
 214        FATTR4_WORD2_SECURITY_LABEL
 215#endif
 216};
 217
 218static const u32 nfs4_pnfs_open_bitmap[3] = {
 219        FATTR4_WORD0_TYPE
 220        | FATTR4_WORD0_CHANGE
 221        | FATTR4_WORD0_SIZE
 222        | FATTR4_WORD0_FSID
 223        | FATTR4_WORD0_FILEID,
 224        FATTR4_WORD1_MODE
 225        | FATTR4_WORD1_NUMLINKS
 226        | FATTR4_WORD1_OWNER
 227        | FATTR4_WORD1_OWNER_GROUP
 228        | FATTR4_WORD1_RAWDEV
 229        | FATTR4_WORD1_SPACE_USED
 230        | FATTR4_WORD1_TIME_ACCESS
 231        | FATTR4_WORD1_TIME_METADATA
 232        | FATTR4_WORD1_TIME_MODIFY,
 233        FATTR4_WORD2_MDSTHRESHOLD
 234#ifdef CONFIG_NFS_V4_SECURITY_LABEL
 235        | FATTR4_WORD2_SECURITY_LABEL
 236#endif
 237};
 238
 239static const u32 nfs4_open_noattr_bitmap[3] = {
 240        FATTR4_WORD0_TYPE
 241        | FATTR4_WORD0_FILEID,
 242};
 243
 244const u32 nfs4_statfs_bitmap[3] = {
 245        FATTR4_WORD0_FILES_AVAIL
 246        | FATTR4_WORD0_FILES_FREE
 247        | FATTR4_WORD0_FILES_TOTAL,
 248        FATTR4_WORD1_SPACE_AVAIL
 249        | FATTR4_WORD1_SPACE_FREE
 250        | FATTR4_WORD1_SPACE_TOTAL
 251};
 252
 253const u32 nfs4_pathconf_bitmap[3] = {
 254        FATTR4_WORD0_MAXLINK
 255        | FATTR4_WORD0_MAXNAME,
 256        0
 257};
 258
 259const u32 nfs4_fsinfo_bitmap[3] = { FATTR4_WORD0_MAXFILESIZE
 260                        | FATTR4_WORD0_MAXREAD
 261                        | FATTR4_WORD0_MAXWRITE
 262                        | FATTR4_WORD0_LEASE_TIME,
 263                        FATTR4_WORD1_TIME_DELTA
 264                        | FATTR4_WORD1_FS_LAYOUT_TYPES,
 265                        FATTR4_WORD2_LAYOUT_BLKSIZE
 266                        | FATTR4_WORD2_CLONE_BLKSIZE
 267                        | FATTR4_WORD2_CHANGE_ATTR_TYPE
 268                        | FATTR4_WORD2_XATTR_SUPPORT
 269};
 270
 271const u32 nfs4_fs_locations_bitmap[3] = {
 272        FATTR4_WORD0_CHANGE
 273        | FATTR4_WORD0_SIZE
 274        | FATTR4_WORD0_FSID
 275        | FATTR4_WORD0_FILEID
 276        | FATTR4_WORD0_FS_LOCATIONS,
 277        FATTR4_WORD1_OWNER
 278        | FATTR4_WORD1_OWNER_GROUP
 279        | FATTR4_WORD1_RAWDEV
 280        | FATTR4_WORD1_SPACE_USED
 281        | FATTR4_WORD1_TIME_ACCESS
 282        | FATTR4_WORD1_TIME_METADATA
 283        | FATTR4_WORD1_TIME_MODIFY
 284        | FATTR4_WORD1_MOUNTED_ON_FILEID,
 285};
 286
 287static void nfs4_bitmap_copy_adjust(__u32 *dst, const __u32 *src,
 288                                    struct inode *inode, unsigned long flags)
 289{
 290        unsigned long cache_validity;
 291
 292        memcpy(dst, src, NFS4_BITMASK_SZ*sizeof(*dst));
 293        if (!inode || !nfs4_have_delegation(inode, FMODE_READ))
 294                return;
 295
 296        cache_validity = READ_ONCE(NFS_I(inode)->cache_validity) | flags;
 297
 298        /* Remove the attributes over which we have full control */
 299        dst[1] &= ~FATTR4_WORD1_RAWDEV;
 300        if (!(cache_validity & NFS_INO_INVALID_SIZE))
 301                dst[0] &= ~FATTR4_WORD0_SIZE;
 302
 303        if (!(cache_validity & NFS_INO_INVALID_CHANGE))
 304                dst[0] &= ~FATTR4_WORD0_CHANGE;
 305
 306        if (!(cache_validity & NFS_INO_INVALID_MODE))
 307                dst[1] &= ~FATTR4_WORD1_MODE;
 308        if (!(cache_validity & NFS_INO_INVALID_OTHER))
 309                dst[1] &= ~(FATTR4_WORD1_OWNER | FATTR4_WORD1_OWNER_GROUP);
 310}
 311
 312static void nfs4_setup_readdir(u64 cookie, __be32 *verifier, struct dentry *dentry,
 313                struct nfs4_readdir_arg *readdir)
 314{
 315        unsigned int attrs = FATTR4_WORD0_FILEID | FATTR4_WORD0_TYPE;
 316        __be32 *start, *p;
 317
 318        if (cookie > 2) {
 319                readdir->cookie = cookie;
 320                memcpy(&readdir->verifier, verifier, sizeof(readdir->verifier));
 321                return;
 322        }
 323
 324        readdir->cookie = 0;
 325        memset(&readdir->verifier, 0, sizeof(readdir->verifier));
 326        if (cookie == 2)
 327                return;
 328        
 329        /*
 330         * NFSv4 servers do not return entries for '.' and '..'
 331         * Therefore, we fake these entries here.  We let '.'
 332         * have cookie 0 and '..' have cookie 1.  Note that
 333         * when talking to the server, we always send cookie 0
 334         * instead of 1 or 2.
 335         */
 336        start = p = kmap_atomic(*readdir->pages);
 337        
 338        if (cookie == 0) {
 339                *p++ = xdr_one;                                  /* next */
 340                *p++ = xdr_zero;                   /* cookie, first word */
 341                *p++ = xdr_one;                   /* cookie, second word */
 342                *p++ = xdr_one;                             /* entry len */
 343                memcpy(p, ".\0\0\0", 4);                        /* entry */
 344                p++;
 345                *p++ = xdr_one;                         /* bitmap length */
 346                *p++ = htonl(attrs);                           /* bitmap */
 347                *p++ = htonl(12);             /* attribute buffer length */
 348                *p++ = htonl(NF4DIR);
 349                p = xdr_encode_hyper(p, NFS_FILEID(d_inode(dentry)));
 350        }
 351        
 352        *p++ = xdr_one;                                  /* next */
 353        *p++ = xdr_zero;                   /* cookie, first word */
 354        *p++ = xdr_two;                   /* cookie, second word */
 355        *p++ = xdr_two;                             /* entry len */
 356        memcpy(p, "..\0\0", 4);                         /* entry */
 357        p++;
 358        *p++ = xdr_one;                         /* bitmap length */
 359        *p++ = htonl(attrs);                           /* bitmap */
 360        *p++ = htonl(12);             /* attribute buffer length */
 361        *p++ = htonl(NF4DIR);
 362        p = xdr_encode_hyper(p, NFS_FILEID(d_inode(dentry->d_parent)));
 363
 364        readdir->pgbase = (char *)p - (char *)start;
 365        readdir->count -= readdir->pgbase;
 366        kunmap_atomic(start);
 367}
 368
 369static void nfs4_test_and_free_stateid(struct nfs_server *server,
 370                nfs4_stateid *stateid,
 371                const struct cred *cred)
 372{
 373        const struct nfs4_minor_version_ops *ops = server->nfs_client->cl_mvops;
 374
 375        ops->test_and_free_expired(server, stateid, cred);
 376}
 377
 378static void __nfs4_free_revoked_stateid(struct nfs_server *server,
 379                nfs4_stateid *stateid,
 380                const struct cred *cred)
 381{
 382        stateid->type = NFS4_REVOKED_STATEID_TYPE;
 383        nfs4_test_and_free_stateid(server, stateid, cred);
 384}
 385
 386static void nfs4_free_revoked_stateid(struct nfs_server *server,
 387                const nfs4_stateid *stateid,
 388                const struct cred *cred)
 389{
 390        nfs4_stateid tmp;
 391
 392        nfs4_stateid_copy(&tmp, stateid);
 393        __nfs4_free_revoked_stateid(server, &tmp, cred);
 394}
 395
 396static long nfs4_update_delay(long *timeout)
 397{
 398        long ret;
 399        if (!timeout)
 400                return NFS4_POLL_RETRY_MAX;
 401        if (*timeout <= 0)
 402                *timeout = NFS4_POLL_RETRY_MIN;
 403        if (*timeout > NFS4_POLL_RETRY_MAX)
 404                *timeout = NFS4_POLL_RETRY_MAX;
 405        ret = *timeout;
 406        *timeout <<= 1;
 407        return ret;
 408}
 409
 410static int nfs4_delay_killable(long *timeout)
 411{
 412        might_sleep();
 413
 414        freezable_schedule_timeout_killable_unsafe(
 415                nfs4_update_delay(timeout));
 416        if (!__fatal_signal_pending(current))
 417                return 0;
 418        return -EINTR;
 419}
 420
 421static int nfs4_delay_interruptible(long *timeout)
 422{
 423        might_sleep();
 424
 425        freezable_schedule_timeout_interruptible_unsafe(nfs4_update_delay(timeout));
 426        if (!signal_pending(current))
 427                return 0;
 428        return __fatal_signal_pending(current) ? -EINTR :-ERESTARTSYS;
 429}
 430
 431static int nfs4_delay(long *timeout, bool interruptible)
 432{
 433        if (interruptible)
 434                return nfs4_delay_interruptible(timeout);
 435        return nfs4_delay_killable(timeout);
 436}
 437
 438static const nfs4_stateid *
 439nfs4_recoverable_stateid(const nfs4_stateid *stateid)
 440{
 441        if (!stateid)
 442                return NULL;
 443        switch (stateid->type) {
 444        case NFS4_OPEN_STATEID_TYPE:
 445        case NFS4_LOCK_STATEID_TYPE:
 446        case NFS4_DELEGATION_STATEID_TYPE:
 447                return stateid;
 448        default:
 449                break;
 450        }
 451        return NULL;
 452}
 453
 454/* This is the error handling routine for processes that are allowed
 455 * to sleep.
 456 */
 457static int nfs4_do_handle_exception(struct nfs_server *server,
 458                int errorcode, struct nfs4_exception *exception)
 459{
 460        struct nfs_client *clp = server->nfs_client;
 461        struct nfs4_state *state = exception->state;
 462        const nfs4_stateid *stateid;
 463        struct inode *inode = exception->inode;
 464        int ret = errorcode;
 465
 466        exception->delay = 0;
 467        exception->recovering = 0;
 468        exception->retry = 0;
 469
 470        stateid = nfs4_recoverable_stateid(exception->stateid);
 471        if (stateid == NULL && state != NULL)
 472                stateid = nfs4_recoverable_stateid(&state->stateid);
 473
 474        switch(errorcode) {
 475                case 0:
 476                        return 0;
 477                case -NFS4ERR_BADHANDLE:
 478                case -ESTALE:
 479                        if (inode != NULL && S_ISREG(inode->i_mode))
 480                                pnfs_destroy_layout(NFS_I(inode));
 481                        break;
 482                case -NFS4ERR_DELEG_REVOKED:
 483                case -NFS4ERR_ADMIN_REVOKED:
 484                case -NFS4ERR_EXPIRED:
 485                case -NFS4ERR_BAD_STATEID:
 486                case -NFS4ERR_PARTNER_NO_AUTH:
 487                        if (inode != NULL && stateid != NULL) {
 488                                nfs_inode_find_state_and_recover(inode,
 489                                                stateid);
 490                                goto wait_on_recovery;
 491                        }
 492                        fallthrough;
 493                case -NFS4ERR_OPENMODE:
 494                        if (inode) {
 495                                int err;
 496
 497                                err = nfs_async_inode_return_delegation(inode,
 498                                                stateid);
 499                                if (err == 0)
 500                                        goto wait_on_recovery;
 501                                if (stateid != NULL && stateid->type == NFS4_DELEGATION_STATEID_TYPE) {
 502                                        exception->retry = 1;
 503                                        break;
 504                                }
 505                        }
 506                        if (state == NULL)
 507                                break;
 508                        ret = nfs4_schedule_stateid_recovery(server, state);
 509                        if (ret < 0)
 510                                break;
 511                        goto wait_on_recovery;
 512                case -NFS4ERR_STALE_STATEID:
 513                case -NFS4ERR_STALE_CLIENTID:
 514                        nfs4_schedule_lease_recovery(clp);
 515                        goto wait_on_recovery;
 516                case -NFS4ERR_MOVED:
 517                        ret = nfs4_schedule_migration_recovery(server);
 518                        if (ret < 0)
 519                                break;
 520                        goto wait_on_recovery;
 521                case -NFS4ERR_LEASE_MOVED:
 522                        nfs4_schedule_lease_moved_recovery(clp);
 523                        goto wait_on_recovery;
 524#if defined(CONFIG_NFS_V4_1)
 525                case -NFS4ERR_BADSESSION:
 526                case -NFS4ERR_BADSLOT:
 527                case -NFS4ERR_BAD_HIGH_SLOT:
 528                case -NFS4ERR_CONN_NOT_BOUND_TO_SESSION:
 529                case -NFS4ERR_DEADSESSION:
 530                case -NFS4ERR_SEQ_FALSE_RETRY:
 531                case -NFS4ERR_SEQ_MISORDERED:
 532                        /* Handled in nfs41_sequence_process() */
 533                        goto wait_on_recovery;
 534#endif /* defined(CONFIG_NFS_V4_1) */
 535                case -NFS4ERR_FILE_OPEN:
 536                        if (exception->timeout > HZ) {
 537                                /* We have retried a decent amount, time to
 538                                 * fail
 539                                 */
 540                                ret = -EBUSY;
 541                                break;
 542                        }
 543                        fallthrough;
 544                case -NFS4ERR_DELAY:
 545                        nfs_inc_server_stats(server, NFSIOS_DELAY);
 546                        fallthrough;
 547                case -NFS4ERR_GRACE:
 548                case -NFS4ERR_LAYOUTTRYLATER:
 549                case -NFS4ERR_RECALLCONFLICT:
 550                        exception->delay = 1;
 551                        return 0;
 552
 553                case -NFS4ERR_RETRY_UNCACHED_REP:
 554                case -NFS4ERR_OLD_STATEID:
 555                        exception->retry = 1;
 556                        break;
 557                case -NFS4ERR_BADOWNER:
 558                        /* The following works around a Linux server bug! */
 559                case -NFS4ERR_BADNAME:
 560                        if (server->caps & NFS_CAP_UIDGID_NOMAP) {
 561                                server->caps &= ~NFS_CAP_UIDGID_NOMAP;
 562                                exception->retry = 1;
 563                                printk(KERN_WARNING "NFS: v4 server %s "
 564                                                "does not accept raw "
 565                                                "uid/gids. "
 566                                                "Reenabling the idmapper.\n",
 567                                                server->nfs_client->cl_hostname);
 568                        }
 569        }
 570        /* We failed to handle the error */
 571        return nfs4_map_errors(ret);
 572wait_on_recovery:
 573        exception->recovering = 1;
 574        return 0;
 575}
 576
 577/* This is the error handling routine for processes that are allowed
 578 * to sleep.
 579 */
 580int nfs4_handle_exception(struct nfs_server *server, int errorcode, struct nfs4_exception *exception)
 581{
 582        struct nfs_client *clp = server->nfs_client;
 583        int ret;
 584
 585        ret = nfs4_do_handle_exception(server, errorcode, exception);
 586        if (exception->delay) {
 587                ret = nfs4_delay(&exception->timeout,
 588                                exception->interruptible);
 589                goto out_retry;
 590        }
 591        if (exception->recovering) {
 592                if (exception->task_is_privileged)
 593                        return -EDEADLOCK;
 594                ret = nfs4_wait_clnt_recover(clp);
 595                if (test_bit(NFS_MIG_FAILED, &server->mig_status))
 596                        return -EIO;
 597                goto out_retry;
 598        }
 599        return ret;
 600out_retry:
 601        if (ret == 0)
 602                exception->retry = 1;
 603        return ret;
 604}
 605
 606static int
 607nfs4_async_handle_exception(struct rpc_task *task, struct nfs_server *server,
 608                int errorcode, struct nfs4_exception *exception)
 609{
 610        struct nfs_client *clp = server->nfs_client;
 611        int ret;
 612
 613        ret = nfs4_do_handle_exception(server, errorcode, exception);
 614        if (exception->delay) {
 615                rpc_delay(task, nfs4_update_delay(&exception->timeout));
 616                goto out_retry;
 617        }
 618        if (exception->recovering) {
 619                if (exception->task_is_privileged)
 620                        return -EDEADLOCK;
 621                rpc_sleep_on(&clp->cl_rpcwaitq, task, NULL);
 622                if (test_bit(NFS4CLNT_MANAGER_RUNNING, &clp->cl_state) == 0)
 623                        rpc_wake_up_queued_task(&clp->cl_rpcwaitq, task);
 624                goto out_retry;
 625        }
 626        if (test_bit(NFS_MIG_FAILED, &server->mig_status))
 627                ret = -EIO;
 628        return ret;
 629out_retry:
 630        if (ret == 0) {
 631                exception->retry = 1;
 632                /*
 633                 * For NFS4ERR_MOVED, the client transport will need to
 634                 * be recomputed after migration recovery has completed.
 635                 */
 636                if (errorcode == -NFS4ERR_MOVED)
 637                        rpc_task_release_transport(task);
 638        }
 639        return ret;
 640}
 641
 642int
 643nfs4_async_handle_error(struct rpc_task *task, struct nfs_server *server,
 644                        struct nfs4_state *state, long *timeout)
 645{
 646        struct nfs4_exception exception = {
 647                .state = state,
 648        };
 649
 650        if (task->tk_status >= 0)
 651                return 0;
 652        if (timeout)
 653                exception.timeout = *timeout;
 654        task->tk_status = nfs4_async_handle_exception(task, server,
 655                        task->tk_status,
 656                        &exception);
 657        if (exception.delay && timeout)
 658                *timeout = exception.timeout;
 659        if (exception.retry)
 660                return -EAGAIN;
 661        return 0;
 662}
 663
 664/*
 665 * Return 'true' if 'clp' is using an rpc_client that is integrity protected
 666 * or 'false' otherwise.
 667 */
 668static bool _nfs4_is_integrity_protected(struct nfs_client *clp)
 669{
 670        rpc_authflavor_t flavor = clp->cl_rpcclient->cl_auth->au_flavor;
 671        return (flavor == RPC_AUTH_GSS_KRB5I) || (flavor == RPC_AUTH_GSS_KRB5P);
 672}
 673
 674static void do_renew_lease(struct nfs_client *clp, unsigned long timestamp)
 675{
 676        spin_lock(&clp->cl_lock);
 677        if (time_before(clp->cl_last_renewal,timestamp))
 678                clp->cl_last_renewal = timestamp;
 679        spin_unlock(&clp->cl_lock);
 680}
 681
 682static void renew_lease(const struct nfs_server *server, unsigned long timestamp)
 683{
 684        struct nfs_client *clp = server->nfs_client;
 685
 686        if (!nfs4_has_session(clp))
 687                do_renew_lease(clp, timestamp);
 688}
 689
 690struct nfs4_call_sync_data {
 691        const struct nfs_server *seq_server;
 692        struct nfs4_sequence_args *seq_args;
 693        struct nfs4_sequence_res *seq_res;
 694};
 695
 696void nfs4_init_sequence(struct nfs4_sequence_args *args,
 697                        struct nfs4_sequence_res *res, int cache_reply,
 698                        int privileged)
 699{
 700        args->sa_slot = NULL;
 701        args->sa_cache_this = cache_reply;
 702        args->sa_privileged = privileged;
 703
 704        res->sr_slot = NULL;
 705}
 706
 707static void nfs40_sequence_free_slot(struct nfs4_sequence_res *res)
 708{
 709        struct nfs4_slot *slot = res->sr_slot;
 710        struct nfs4_slot_table *tbl;
 711
 712        tbl = slot->table;
 713        spin_lock(&tbl->slot_tbl_lock);
 714        if (!nfs41_wake_and_assign_slot(tbl, slot))
 715                nfs4_free_slot(tbl, slot);
 716        spin_unlock(&tbl->slot_tbl_lock);
 717
 718        res->sr_slot = NULL;
 719}
 720
 721static int nfs40_sequence_done(struct rpc_task *task,
 722                               struct nfs4_sequence_res *res)
 723{
 724        if (res->sr_slot != NULL)
 725                nfs40_sequence_free_slot(res);
 726        return 1;
 727}
 728
 729#if defined(CONFIG_NFS_V4_1)
 730
 731static void nfs41_release_slot(struct nfs4_slot *slot)
 732{
 733        struct nfs4_session *session;
 734        struct nfs4_slot_table *tbl;
 735        bool send_new_highest_used_slotid = false;
 736
 737        if (!slot)
 738                return;
 739        tbl = slot->table;
 740        session = tbl->session;
 741
 742        /* Bump the slot sequence number */
 743        if (slot->seq_done)
 744                slot->seq_nr++;
 745        slot->seq_done = 0;
 746
 747        spin_lock(&tbl->slot_tbl_lock);
 748        /* Be nice to the server: try to ensure that the last transmitted
 749         * value for highest_user_slotid <= target_highest_slotid
 750         */
 751        if (tbl->highest_used_slotid > tbl->target_highest_slotid)
 752                send_new_highest_used_slotid = true;
 753
 754        if (nfs41_wake_and_assign_slot(tbl, slot)) {
 755                send_new_highest_used_slotid = false;
 756                goto out_unlock;
 757        }
 758        nfs4_free_slot(tbl, slot);
 759
 760        if (tbl->highest_used_slotid != NFS4_NO_SLOT)
 761                send_new_highest_used_slotid = false;
 762out_unlock:
 763        spin_unlock(&tbl->slot_tbl_lock);
 764        if (send_new_highest_used_slotid)
 765                nfs41_notify_server(session->clp);
 766        if (waitqueue_active(&tbl->slot_waitq))
 767                wake_up_all(&tbl->slot_waitq);
 768}
 769
 770static void nfs41_sequence_free_slot(struct nfs4_sequence_res *res)
 771{
 772        nfs41_release_slot(res->sr_slot);
 773        res->sr_slot = NULL;
 774}
 775
 776static void nfs4_slot_sequence_record_sent(struct nfs4_slot *slot,
 777                u32 seqnr)
 778{
 779        if ((s32)(seqnr - slot->seq_nr_highest_sent) > 0)
 780                slot->seq_nr_highest_sent = seqnr;
 781}
 782static void nfs4_slot_sequence_acked(struct nfs4_slot *slot,
 783                u32 seqnr)
 784{
 785        slot->seq_nr_highest_sent = seqnr;
 786        slot->seq_nr_last_acked = seqnr;
 787}
 788
 789static void nfs4_probe_sequence(struct nfs_client *client, const struct cred *cred,
 790                                struct nfs4_slot *slot)
 791{
 792        struct rpc_task *task = _nfs41_proc_sequence(client, cred, slot, true);
 793        if (!IS_ERR(task))
 794                rpc_put_task_async(task);
 795}
 796
 797static int nfs41_sequence_process(struct rpc_task *task,
 798                struct nfs4_sequence_res *res)
 799{
 800        struct nfs4_session *session;
 801        struct nfs4_slot *slot = res->sr_slot;
 802        struct nfs_client *clp;
 803        int status;
 804        int ret = 1;
 805
 806        if (slot == NULL)
 807                goto out_noaction;
 808        /* don't increment the sequence number if the task wasn't sent */
 809        if (!RPC_WAS_SENT(task) || slot->seq_done)
 810                goto out;
 811
 812        session = slot->table->session;
 813        clp = session->clp;
 814
 815        trace_nfs4_sequence_done(session, res);
 816
 817        status = res->sr_status;
 818        if (task->tk_status == -NFS4ERR_DEADSESSION)
 819                status = -NFS4ERR_DEADSESSION;
 820
 821        /* Check the SEQUENCE operation status */
 822        switch (status) {
 823        case 0:
 824                /* Mark this sequence number as having been acked */
 825                nfs4_slot_sequence_acked(slot, slot->seq_nr);
 826                /* Update the slot's sequence and clientid lease timer */
 827                slot->seq_done = 1;
 828                do_renew_lease(clp, res->sr_timestamp);
 829                /* Check sequence flags */
 830                nfs41_handle_sequence_flag_errors(clp, res->sr_status_flags,
 831                                !!slot->privileged);
 832                nfs41_update_target_slotid(slot->table, slot, res);
 833                break;
 834        case 1:
 835                /*
 836                 * sr_status remains 1 if an RPC level error occurred.
 837                 * The server may or may not have processed the sequence
 838                 * operation..
 839                 */
 840                nfs4_slot_sequence_record_sent(slot, slot->seq_nr);
 841                slot->seq_done = 1;
 842                goto out;
 843        case -NFS4ERR_DELAY:
 844                /* The server detected a resend of the RPC call and
 845                 * returned NFS4ERR_DELAY as per Section 2.10.6.2
 846                 * of RFC5661.
 847                 */
 848                dprintk("%s: slot=%u seq=%u: Operation in progress\n",
 849                        __func__,
 850                        slot->slot_nr,
 851                        slot->seq_nr);
 852                nfs4_slot_sequence_acked(slot, slot->seq_nr);
 853                goto out_retry;
 854        case -NFS4ERR_RETRY_UNCACHED_REP:
 855        case -NFS4ERR_SEQ_FALSE_RETRY:
 856                /*
 857                 * The server thinks we tried to replay a request.
 858                 * Retry the call after bumping the sequence ID.
 859                 */
 860                nfs4_slot_sequence_acked(slot, slot->seq_nr);
 861                goto retry_new_seq;
 862        case -NFS4ERR_BADSLOT:
 863                /*
 864                 * The slot id we used was probably retired. Try again
 865                 * using a different slot id.
 866                 */
 867                if (slot->slot_nr < slot->table->target_highest_slotid)
 868                        goto session_recover;
 869                goto retry_nowait;
 870        case -NFS4ERR_SEQ_MISORDERED:
 871                nfs4_slot_sequence_record_sent(slot, slot->seq_nr);
 872                /*
 873                 * Were one or more calls using this slot interrupted?
 874                 * If the server never received the request, then our
 875                 * transmitted slot sequence number may be too high. However,
 876                 * if the server did receive the request then it might
 877                 * accidentally give us a reply with a mismatched operation.
 878                 * We can sort this out by sending a lone sequence operation
 879                 * to the server on the same slot.
 880                 */
 881                if ((s32)(slot->seq_nr - slot->seq_nr_last_acked) > 1) {
 882                        slot->seq_nr--;
 883                        if (task->tk_msg.rpc_proc != &nfs4_procedures[NFSPROC4_CLNT_SEQUENCE]) {
 884                                nfs4_probe_sequence(clp, task->tk_msg.rpc_cred, slot);
 885                                res->sr_slot = NULL;
 886                        }
 887                        goto retry_nowait;
 888                }
 889                /*
 890                 * RFC5661:
 891                 * A retry might be sent while the original request is
 892                 * still in progress on the replier. The replier SHOULD
 893                 * deal with the issue by returning NFS4ERR_DELAY as the
 894                 * reply to SEQUENCE or CB_SEQUENCE operation, but
 895                 * implementations MAY return NFS4ERR_SEQ_MISORDERED.
 896                 *
 897                 * Restart the search after a delay.
 898                 */
 899                slot->seq_nr = slot->seq_nr_highest_sent;
 900                goto out_retry;
 901        case -NFS4ERR_BADSESSION:
 902        case -NFS4ERR_DEADSESSION:
 903        case -NFS4ERR_CONN_NOT_BOUND_TO_SESSION:
 904                goto session_recover;
 905        default:
 906                /* Just update the slot sequence no. */
 907                slot->seq_done = 1;
 908        }
 909out:
 910        /* The session may be reset by one of the error handlers. */
 911        dprintk("%s: Error %d free the slot \n", __func__, res->sr_status);
 912out_noaction:
 913        return ret;
 914session_recover:
 915        nfs4_schedule_session_recovery(session, status);
 916        dprintk("%s ERROR: %d Reset session\n", __func__, status);
 917        nfs41_sequence_free_slot(res);
 918        goto out;
 919retry_new_seq:
 920        ++slot->seq_nr;
 921retry_nowait:
 922        if (rpc_restart_call_prepare(task)) {
 923                nfs41_sequence_free_slot(res);
 924                task->tk_status = 0;
 925                ret = 0;
 926        }
 927        goto out;
 928out_retry:
 929        if (!rpc_restart_call(task))
 930                goto out;
 931        rpc_delay(task, NFS4_POLL_RETRY_MAX);
 932        return 0;
 933}
 934
 935int nfs41_sequence_done(struct rpc_task *task, struct nfs4_sequence_res *res)
 936{
 937        if (!nfs41_sequence_process(task, res))
 938                return 0;
 939        if (res->sr_slot != NULL)
 940                nfs41_sequence_free_slot(res);
 941        return 1;
 942
 943}
 944EXPORT_SYMBOL_GPL(nfs41_sequence_done);
 945
 946static int nfs4_sequence_process(struct rpc_task *task, struct nfs4_sequence_res *res)
 947{
 948        if (res->sr_slot == NULL)
 949                return 1;
 950        if (res->sr_slot->table->session != NULL)
 951                return nfs41_sequence_process(task, res);
 952        return nfs40_sequence_done(task, res);
 953}
 954
 955static void nfs4_sequence_free_slot(struct nfs4_sequence_res *res)
 956{
 957        if (res->sr_slot != NULL) {
 958                if (res->sr_slot->table->session != NULL)
 959                        nfs41_sequence_free_slot(res);
 960                else
 961                        nfs40_sequence_free_slot(res);
 962        }
 963}
 964
 965int nfs4_sequence_done(struct rpc_task *task, struct nfs4_sequence_res *res)
 966{
 967        if (res->sr_slot == NULL)
 968                return 1;
 969        if (!res->sr_slot->table->session)
 970                return nfs40_sequence_done(task, res);
 971        return nfs41_sequence_done(task, res);
 972}
 973EXPORT_SYMBOL_GPL(nfs4_sequence_done);
 974
 975static void nfs41_call_sync_prepare(struct rpc_task *task, void *calldata)
 976{
 977        struct nfs4_call_sync_data *data = calldata;
 978
 979        dprintk("--> %s data->seq_server %p\n", __func__, data->seq_server);
 980
 981        nfs4_setup_sequence(data->seq_server->nfs_client,
 982                            data->seq_args, data->seq_res, task);
 983}
 984
 985static void nfs41_call_sync_done(struct rpc_task *task, void *calldata)
 986{
 987        struct nfs4_call_sync_data *data = calldata;
 988
 989        nfs41_sequence_done(task, data->seq_res);
 990}
 991
 992static const struct rpc_call_ops nfs41_call_sync_ops = {
 993        .rpc_call_prepare = nfs41_call_sync_prepare,
 994        .rpc_call_done = nfs41_call_sync_done,
 995};
 996
 997#else   /* !CONFIG_NFS_V4_1 */
 998
 999static int nfs4_sequence_process(struct rpc_task *task, struct nfs4_sequence_res *res)
1000{
1001        return nfs40_sequence_done(task, res);
1002}
1003
1004static void nfs4_sequence_free_slot(struct nfs4_sequence_res *res)
1005{
1006        if (res->sr_slot != NULL)
1007                nfs40_sequence_free_slot(res);
1008}
1009
1010int nfs4_sequence_done(struct rpc_task *task,
1011                       struct nfs4_sequence_res *res)
1012{
1013        return nfs40_sequence_done(task, res);
1014}
1015EXPORT_SYMBOL_GPL(nfs4_sequence_done);
1016
1017#endif  /* !CONFIG_NFS_V4_1 */
1018
1019static void nfs41_sequence_res_init(struct nfs4_sequence_res *res)
1020{
1021        res->sr_timestamp = jiffies;
1022        res->sr_status_flags = 0;
1023        res->sr_status = 1;
1024}
1025
1026static
1027void nfs4_sequence_attach_slot(struct nfs4_sequence_args *args,
1028                struct nfs4_sequence_res *res,
1029                struct nfs4_slot *slot)
1030{
1031        if (!slot)
1032                return;
1033        slot->privileged = args->sa_privileged ? 1 : 0;
1034        args->sa_slot = slot;
1035
1036        res->sr_slot = slot;
1037}
1038
1039int nfs4_setup_sequence(struct nfs_client *client,
1040                        struct nfs4_sequence_args *args,
1041                        struct nfs4_sequence_res *res,
1042                        struct rpc_task *task)
1043{
1044        struct nfs4_session *session = nfs4_get_session(client);
1045        struct nfs4_slot_table *tbl  = client->cl_slot_tbl;
1046        struct nfs4_slot *slot;
1047
1048        /* slot already allocated? */
1049        if (res->sr_slot != NULL)
1050                goto out_start;
1051
1052        if (session)
1053                tbl = &session->fc_slot_table;
1054
1055        spin_lock(&tbl->slot_tbl_lock);
1056        /* The state manager will wait until the slot table is empty */
1057        if (nfs4_slot_tbl_draining(tbl) && !args->sa_privileged)
1058                goto out_sleep;
1059
1060        slot = nfs4_alloc_slot(tbl);
1061        if (IS_ERR(slot)) {
1062                if (slot == ERR_PTR(-ENOMEM))
1063                        goto out_sleep_timeout;
1064                goto out_sleep;
1065        }
1066        spin_unlock(&tbl->slot_tbl_lock);
1067
1068        nfs4_sequence_attach_slot(args, res, slot);
1069
1070        trace_nfs4_setup_sequence(session, args);
1071out_start:
1072        nfs41_sequence_res_init(res);
1073        rpc_call_start(task);
1074        return 0;
1075out_sleep_timeout:
1076        /* Try again in 1/4 second */
1077        if (args->sa_privileged)
1078                rpc_sleep_on_priority_timeout(&tbl->slot_tbl_waitq, task,
1079                                jiffies + (HZ >> 2), RPC_PRIORITY_PRIVILEGED);
1080        else
1081                rpc_sleep_on_timeout(&tbl->slot_tbl_waitq, task,
1082                                NULL, jiffies + (HZ >> 2));
1083        spin_unlock(&tbl->slot_tbl_lock);
1084        return -EAGAIN;
1085out_sleep:
1086        if (args->sa_privileged)
1087                rpc_sleep_on_priority(&tbl->slot_tbl_waitq, task,
1088                                RPC_PRIORITY_PRIVILEGED);
1089        else
1090                rpc_sleep_on(&tbl->slot_tbl_waitq, task, NULL);
1091        spin_unlock(&tbl->slot_tbl_lock);
1092        return -EAGAIN;
1093}
1094EXPORT_SYMBOL_GPL(nfs4_setup_sequence);
1095
1096static void nfs40_call_sync_prepare(struct rpc_task *task, void *calldata)
1097{
1098        struct nfs4_call_sync_data *data = calldata;
1099        nfs4_setup_sequence(data->seq_server->nfs_client,
1100                                data->seq_args, data->seq_res, task);
1101}
1102
1103static void nfs40_call_sync_done(struct rpc_task *task, void *calldata)
1104{
1105        struct nfs4_call_sync_data *data = calldata;
1106        nfs4_sequence_done(task, data->seq_res);
1107}
1108
1109static const struct rpc_call_ops nfs40_call_sync_ops = {
1110        .rpc_call_prepare = nfs40_call_sync_prepare,
1111        .rpc_call_done = nfs40_call_sync_done,
1112};
1113
1114static int nfs4_call_sync_custom(struct rpc_task_setup *task_setup)
1115{
1116        int ret;
1117        struct rpc_task *task;
1118
1119        task = rpc_run_task(task_setup);
1120        if (IS_ERR(task))
1121                return PTR_ERR(task);
1122
1123        ret = task->tk_status;
1124        rpc_put_task(task);
1125        return ret;
1126}
1127
1128static int nfs4_do_call_sync(struct rpc_clnt *clnt,
1129                             struct nfs_server *server,
1130                             struct rpc_message *msg,
1131                             struct nfs4_sequence_args *args,
1132                             struct nfs4_sequence_res *res,
1133                             unsigned short task_flags)
1134{
1135        struct nfs_client *clp = server->nfs_client;
1136        struct nfs4_call_sync_data data = {
1137                .seq_server = server,
1138                .seq_args = args,
1139                .seq_res = res,
1140        };
1141        struct rpc_task_setup task_setup = {
1142                .rpc_client = clnt,
1143                .rpc_message = msg,
1144                .callback_ops = clp->cl_mvops->call_sync_ops,
1145                .callback_data = &data,
1146                .flags = task_flags,
1147        };
1148
1149        return nfs4_call_sync_custom(&task_setup);
1150}
1151
1152static int nfs4_call_sync_sequence(struct rpc_clnt *clnt,
1153                                   struct nfs_server *server,
1154                                   struct rpc_message *msg,
1155                                   struct nfs4_sequence_args *args,
1156                                   struct nfs4_sequence_res *res)
1157{
1158        unsigned short task_flags = 0;
1159
1160        if (server->nfs_client->cl_minorversion)
1161                task_flags = RPC_TASK_MOVEABLE;
1162        return nfs4_do_call_sync(clnt, server, msg, args, res, task_flags);
1163}
1164
1165
1166int nfs4_call_sync(struct rpc_clnt *clnt,
1167                   struct nfs_server *server,
1168                   struct rpc_message *msg,
1169                   struct nfs4_sequence_args *args,
1170                   struct nfs4_sequence_res *res,
1171                   int cache_reply)
1172{
1173        nfs4_init_sequence(args, res, cache_reply, 0);
1174        return nfs4_call_sync_sequence(clnt, server, msg, args, res);
1175}
1176
1177static void
1178nfs4_inc_nlink_locked(struct inode *inode)
1179{
1180        nfs_set_cache_invalid(inode, NFS_INO_INVALID_CHANGE |
1181                                             NFS_INO_INVALID_CTIME |
1182                                             NFS_INO_INVALID_NLINK);
1183        inc_nlink(inode);
1184}
1185
1186static void
1187nfs4_inc_nlink(struct inode *inode)
1188{
1189        spin_lock(&inode->i_lock);
1190        nfs4_inc_nlink_locked(inode);
1191        spin_unlock(&inode->i_lock);
1192}
1193
1194static void
1195nfs4_dec_nlink_locked(struct inode *inode)
1196{
1197        nfs_set_cache_invalid(inode, NFS_INO_INVALID_CHANGE |
1198                                             NFS_INO_INVALID_CTIME |
1199                                             NFS_INO_INVALID_NLINK);
1200        drop_nlink(inode);
1201}
1202
1203static void
1204nfs4_update_changeattr_locked(struct inode *inode,
1205                struct nfs4_change_info *cinfo,
1206                unsigned long timestamp, unsigned long cache_validity)
1207{
1208        struct nfs_inode *nfsi = NFS_I(inode);
1209        u64 change_attr = inode_peek_iversion_raw(inode);
1210
1211        cache_validity |= NFS_INO_INVALID_CTIME | NFS_INO_INVALID_MTIME;
1212        if (S_ISDIR(inode->i_mode))
1213                cache_validity |= NFS_INO_INVALID_DATA;
1214
1215        switch (NFS_SERVER(inode)->change_attr_type) {
1216        case NFS4_CHANGE_TYPE_IS_UNDEFINED:
1217                if (cinfo->after == change_attr)
1218                        goto out;
1219                break;
1220        default:
1221                if ((s64)(change_attr - cinfo->after) >= 0)
1222                        goto out;
1223        }
1224
1225        inode_set_iversion_raw(inode, cinfo->after);
1226        if (!cinfo->atomic || cinfo->before != change_attr) {
1227                if (S_ISDIR(inode->i_mode))
1228                        nfs_force_lookup_revalidate(inode);
1229
1230                if (!NFS_PROTO(inode)->have_delegation(inode, FMODE_READ))
1231                        cache_validity |=
1232                                NFS_INO_INVALID_ACCESS | NFS_INO_INVALID_ACL |
1233                                NFS_INO_INVALID_SIZE | NFS_INO_INVALID_OTHER |
1234                                NFS_INO_INVALID_BLOCKS | NFS_INO_INVALID_NLINK |
1235                                NFS_INO_INVALID_MODE | NFS_INO_INVALID_XATTR |
1236                                NFS_INO_REVAL_PAGECACHE;
1237                nfsi->attrtimeo = NFS_MINATTRTIMEO(inode);
1238        }
1239        nfsi->attrtimeo_timestamp = jiffies;
1240        nfsi->read_cache_jiffies = timestamp;
1241        nfsi->attr_gencount = nfs_inc_attr_generation_counter();
1242        nfsi->cache_validity &= ~NFS_INO_INVALID_CHANGE;
1243out:
1244        nfs_set_cache_invalid(inode, cache_validity);
1245}
1246
1247void
1248nfs4_update_changeattr(struct inode *dir, struct nfs4_change_info *cinfo,
1249                unsigned long timestamp, unsigned long cache_validity)
1250{
1251        spin_lock(&dir->i_lock);
1252        nfs4_update_changeattr_locked(dir, cinfo, timestamp, cache_validity);
1253        spin_unlock(&dir->i_lock);
1254}
1255
1256struct nfs4_open_createattrs {
1257        struct nfs4_label *label;
1258        struct iattr *sattr;
1259        const __u32 verf[2];
1260};
1261
1262static bool nfs4_clear_cap_atomic_open_v1(struct nfs_server *server,
1263                int err, struct nfs4_exception *exception)
1264{
1265        if (err != -EINVAL)
1266                return false;
1267        if (!(server->caps & NFS_CAP_ATOMIC_OPEN_V1))
1268                return false;
1269        server->caps &= ~NFS_CAP_ATOMIC_OPEN_V1;
1270        exception->retry = 1;
1271        return true;
1272}
1273
1274static fmode_t _nfs4_ctx_to_accessmode(const struct nfs_open_context *ctx)
1275{
1276         return ctx->mode & (FMODE_READ|FMODE_WRITE|FMODE_EXEC);
1277}
1278
1279static fmode_t _nfs4_ctx_to_openmode(const struct nfs_open_context *ctx)
1280{
1281        fmode_t ret = ctx->mode & (FMODE_READ|FMODE_WRITE);
1282
1283        return (ctx->mode & FMODE_EXEC) ? FMODE_READ | ret : ret;
1284}
1285
1286static u32
1287nfs4_map_atomic_open_share(struct nfs_server *server,
1288                fmode_t fmode, int openflags)
1289{
1290        u32 res = 0;
1291
1292        switch (fmode & (FMODE_READ | FMODE_WRITE)) {
1293        case FMODE_READ:
1294                res = NFS4_SHARE_ACCESS_READ;
1295                break;
1296        case FMODE_WRITE:
1297                res = NFS4_SHARE_ACCESS_WRITE;
1298                break;
1299        case FMODE_READ|FMODE_WRITE:
1300                res = NFS4_SHARE_ACCESS_BOTH;
1301        }
1302        if (!(server->caps & NFS_CAP_ATOMIC_OPEN_V1))
1303                goto out;
1304        /* Want no delegation if we're using O_DIRECT */
1305        if (openflags & O_DIRECT)
1306                res |= NFS4_SHARE_WANT_NO_DELEG;
1307out:
1308        return res;
1309}
1310
1311static enum open_claim_type4
1312nfs4_map_atomic_open_claim(struct nfs_server *server,
1313                enum open_claim_type4 claim)
1314{
1315        if (server->caps & NFS_CAP_ATOMIC_OPEN_V1)
1316                return claim;
1317        switch (claim) {
1318        default:
1319                return claim;
1320        case NFS4_OPEN_CLAIM_FH:
1321                return NFS4_OPEN_CLAIM_NULL;
1322        case NFS4_OPEN_CLAIM_DELEG_CUR_FH:
1323                return NFS4_OPEN_CLAIM_DELEGATE_CUR;
1324        case NFS4_OPEN_CLAIM_DELEG_PREV_FH:
1325                return NFS4_OPEN_CLAIM_DELEGATE_PREV;
1326        }
1327}
1328
1329static void nfs4_init_opendata_res(struct nfs4_opendata *p)
1330{
1331        p->o_res.f_attr = &p->f_attr;
1332        p->o_res.f_label = p->f_label;
1333        p->o_res.seqid = p->o_arg.seqid;
1334        p->c_res.seqid = p->c_arg.seqid;
1335        p->o_res.server = p->o_arg.server;
1336        p->o_res.access_request = p->o_arg.access;
1337        nfs_fattr_init(&p->f_attr);
1338        nfs_fattr_init_names(&p->f_attr, &p->owner_name, &p->group_name);
1339}
1340
1341static struct nfs4_opendata *nfs4_opendata_alloc(struct dentry *dentry,
1342                struct nfs4_state_owner *sp, fmode_t fmode, int flags,
1343                const struct nfs4_open_createattrs *c,
1344                enum open_claim_type4 claim,
1345                gfp_t gfp_mask)
1346{
1347        struct dentry *parent = dget_parent(dentry);
1348        struct inode *dir = d_inode(parent);
1349        struct nfs_server *server = NFS_SERVER(dir);
1350        struct nfs_seqid *(*alloc_seqid)(struct nfs_seqid_counter *, gfp_t);
1351        struct nfs4_label *label = (c != NULL) ? c->label : NULL;
1352        struct nfs4_opendata *p;
1353
1354        p = kzalloc(sizeof(*p), gfp_mask);
1355        if (p == NULL)
1356                goto err;
1357
1358        p->f_label = nfs4_label_alloc(server, gfp_mask);
1359        if (IS_ERR(p->f_label))
1360                goto err_free_p;
1361
1362        p->a_label = nfs4_label_alloc(server, gfp_mask);
1363        if (IS_ERR(p->a_label))
1364                goto err_free_f;
1365
1366        alloc_seqid = server->nfs_client->cl_mvops->alloc_seqid;
1367        p->o_arg.seqid = alloc_seqid(&sp->so_seqid, gfp_mask);
1368        if (IS_ERR(p->o_arg.seqid))
1369                goto err_free_label;
1370        nfs_sb_active(dentry->d_sb);
1371        p->dentry = dget(dentry);
1372        p->dir = parent;
1373        p->owner = sp;
1374        atomic_inc(&sp->so_count);
1375        p->o_arg.open_flags = flags;
1376        p->o_arg.fmode = fmode & (FMODE_READ|FMODE_WRITE);
1377        p->o_arg.claim = nfs4_map_atomic_open_claim(server, claim);
1378        p->o_arg.share_access = nfs4_map_atomic_open_share(server,
1379                        fmode, flags);
1380        if (flags & O_CREAT) {
1381                p->o_arg.umask = current_umask();
1382                p->o_arg.label = nfs4_label_copy(p->a_label, label);
1383                if (c->sattr != NULL && c->sattr->ia_valid != 0) {
1384                        p->o_arg.u.attrs = &p->attrs;
1385                        memcpy(&p->attrs, c->sattr, sizeof(p->attrs));
1386
1387                        memcpy(p->o_arg.u.verifier.data, c->verf,
1388                                        sizeof(p->o_arg.u.verifier.data));
1389                }
1390        }
1391        /* don't put an ACCESS op in OPEN compound if O_EXCL, because ACCESS
1392         * will return permission denied for all bits until close */
1393        if (!(flags & O_EXCL)) {
1394                /* ask server to check for all possible rights as results
1395                 * are cached */
1396                switch (p->o_arg.claim) {
1397                default:
1398                        break;
1399                case NFS4_OPEN_CLAIM_NULL:
1400                case NFS4_OPEN_CLAIM_FH:
1401                        p->o_arg.access = NFS4_ACCESS_READ |
1402                                NFS4_ACCESS_MODIFY |
1403                                NFS4_ACCESS_EXTEND |
1404                                NFS4_ACCESS_EXECUTE;
1405#ifdef CONFIG_NFS_V4_2
1406                        if (server->caps & NFS_CAP_XATTR)
1407                                p->o_arg.access |= NFS4_ACCESS_XAREAD |
1408                                    NFS4_ACCESS_XAWRITE |
1409                                    NFS4_ACCESS_XALIST;
1410#endif
1411                }
1412        }
1413        p->o_arg.clientid = server->nfs_client->cl_clientid;
1414        p->o_arg.id.create_time = ktime_to_ns(sp->so_seqid.create_time);
1415        p->o_arg.id.uniquifier = sp->so_seqid.owner_id;
1416        p->o_arg.name = &dentry->d_name;
1417        p->o_arg.server = server;
1418        p->o_arg.bitmask = nfs4_bitmask(server, label);
1419        p->o_arg.open_bitmap = &nfs4_fattr_bitmap[0];
1420        switch (p->o_arg.claim) {
1421        case NFS4_OPEN_CLAIM_NULL:
1422        case NFS4_OPEN_CLAIM_DELEGATE_CUR:
1423        case NFS4_OPEN_CLAIM_DELEGATE_PREV:
1424                p->o_arg.fh = NFS_FH(dir);
1425                break;
1426        case NFS4_OPEN_CLAIM_PREVIOUS:
1427        case NFS4_OPEN_CLAIM_FH:
1428        case NFS4_OPEN_CLAIM_DELEG_CUR_FH:
1429        case NFS4_OPEN_CLAIM_DELEG_PREV_FH:
1430                p->o_arg.fh = NFS_FH(d_inode(dentry));
1431        }
1432        p->c_arg.fh = &p->o_res.fh;
1433        p->c_arg.stateid = &p->o_res.stateid;
1434        p->c_arg.seqid = p->o_arg.seqid;
1435        nfs4_init_opendata_res(p);
1436        kref_init(&p->kref);
1437        return p;
1438
1439err_free_label:
1440        nfs4_label_free(p->a_label);
1441err_free_f:
1442        nfs4_label_free(p->f_label);
1443err_free_p:
1444        kfree(p);
1445err:
1446        dput(parent);
1447        return NULL;
1448}
1449
1450static void nfs4_opendata_free(struct kref *kref)
1451{
1452        struct nfs4_opendata *p = container_of(kref,
1453                        struct nfs4_opendata, kref);
1454        struct super_block *sb = p->dentry->d_sb;
1455
1456        nfs4_lgopen_release(p->lgp);
1457        nfs_free_seqid(p->o_arg.seqid);
1458        nfs4_sequence_free_slot(&p->o_res.seq_res);
1459        if (p->state != NULL)
1460                nfs4_put_open_state(p->state);
1461        nfs4_put_state_owner(p->owner);
1462
1463        nfs4_label_free(p->a_label);
1464        nfs4_label_free(p->f_label);
1465
1466        dput(p->dir);
1467        dput(p->dentry);
1468        nfs_sb_deactive(sb);
1469        nfs_fattr_free_names(&p->f_attr);
1470        kfree(p->f_attr.mdsthreshold);
1471        kfree(p);
1472}
1473
1474static void nfs4_opendata_put(struct nfs4_opendata *p)
1475{
1476        if (p != NULL)
1477                kref_put(&p->kref, nfs4_opendata_free);
1478}
1479
1480static bool nfs4_mode_match_open_stateid(struct nfs4_state *state,
1481                fmode_t fmode)
1482{
1483        switch(fmode & (FMODE_READ|FMODE_WRITE)) {
1484        case FMODE_READ|FMODE_WRITE:
1485                return state->n_rdwr != 0;
1486        case FMODE_WRITE:
1487                return state->n_wronly != 0;
1488        case FMODE_READ:
1489                return state->n_rdonly != 0;
1490        }
1491        WARN_ON_ONCE(1);
1492        return false;
1493}
1494
1495static int can_open_cached(struct nfs4_state *state, fmode_t mode,
1496                int open_mode, enum open_claim_type4 claim)
1497{
1498        int ret = 0;
1499
1500        if (open_mode & (O_EXCL|O_TRUNC))
1501                goto out;
1502        switch (claim) {
1503        case NFS4_OPEN_CLAIM_NULL:
1504        case NFS4_OPEN_CLAIM_FH:
1505                goto out;
1506        default:
1507                break;
1508        }
1509        switch (mode & (FMODE_READ|FMODE_WRITE)) {
1510                case FMODE_READ:
1511                        ret |= test_bit(NFS_O_RDONLY_STATE, &state->flags) != 0
1512                                && state->n_rdonly != 0;
1513                        break;
1514                case FMODE_WRITE:
1515                        ret |= test_bit(NFS_O_WRONLY_STATE, &state->flags) != 0
1516                                && state->n_wronly != 0;
1517                        break;
1518                case FMODE_READ|FMODE_WRITE:
1519                        ret |= test_bit(NFS_O_RDWR_STATE, &state->flags) != 0
1520                                && state->n_rdwr != 0;
1521        }
1522out:
1523        return ret;
1524}
1525
1526static int can_open_delegated(struct nfs_delegation *delegation, fmode_t fmode,
1527                enum open_claim_type4 claim)
1528{
1529        if (delegation == NULL)
1530                return 0;
1531        if ((delegation->type & fmode) != fmode)
1532                return 0;
1533        switch (claim) {
1534        case NFS4_OPEN_CLAIM_NULL:
1535        case NFS4_OPEN_CLAIM_FH:
1536                break;
1537        case NFS4_OPEN_CLAIM_PREVIOUS:
1538                if (!test_bit(NFS_DELEGATION_NEED_RECLAIM, &delegation->flags))
1539                        break;
1540                fallthrough;
1541        default:
1542                return 0;
1543        }
1544        nfs_mark_delegation_referenced(delegation);
1545        return 1;
1546}
1547
1548static void update_open_stateflags(struct nfs4_state *state, fmode_t fmode)
1549{
1550        switch (fmode) {
1551                case FMODE_WRITE:
1552                        state->n_wronly++;
1553                        break;
1554                case FMODE_READ:
1555                        state->n_rdonly++;
1556                        break;
1557                case FMODE_READ|FMODE_WRITE:
1558                        state->n_rdwr++;
1559        }
1560        nfs4_state_set_mode_locked(state, state->state | fmode);
1561}
1562
1563#ifdef CONFIG_NFS_V4_1
1564static bool nfs_open_stateid_recover_openmode(struct nfs4_state *state)
1565{
1566        if (state->n_rdonly && !test_bit(NFS_O_RDONLY_STATE, &state->flags))
1567                return true;
1568        if (state->n_wronly && !test_bit(NFS_O_WRONLY_STATE, &state->flags))
1569                return true;
1570        if (state->n_rdwr && !test_bit(NFS_O_RDWR_STATE, &state->flags))
1571                return true;
1572        return false;
1573}
1574#endif /* CONFIG_NFS_V4_1 */
1575
1576static void nfs_state_log_update_open_stateid(struct nfs4_state *state)
1577{
1578        if (test_and_clear_bit(NFS_STATE_CHANGE_WAIT, &state->flags))
1579                wake_up_all(&state->waitq);
1580}
1581
1582static void nfs_test_and_clear_all_open_stateid(struct nfs4_state *state)
1583{
1584        struct nfs_client *clp = state->owner->so_server->nfs_client;
1585        bool need_recover = false;
1586
1587        if (test_and_clear_bit(NFS_O_RDONLY_STATE, &state->flags) && state->n_rdonly)
1588                need_recover = true;
1589        if (test_and_clear_bit(NFS_O_WRONLY_STATE, &state->flags) && state->n_wronly)
1590                need_recover = true;
1591        if (test_and_clear_bit(NFS_O_RDWR_STATE, &state->flags) && state->n_rdwr)
1592                need_recover = true;
1593        if (need_recover)
1594                nfs4_state_mark_reclaim_nograce(clp, state);
1595}
1596
1597/*
1598 * Check for whether or not the caller may update the open stateid
1599 * to the value passed in by stateid.
1600 *
1601 * Note: This function relies heavily on the server implementing
1602 * RFC7530 Section 9.1.4.2, and RFC5661 Section 8.2.2
1603 * correctly.
1604 * i.e. The stateid seqids have to be initialised to 1, and
1605 * are then incremented on every state transition.
1606 */
1607static bool nfs_stateid_is_sequential(struct nfs4_state *state,
1608                const nfs4_stateid *stateid)
1609{
1610        if (test_bit(NFS_OPEN_STATE, &state->flags)) {
1611                /* The common case - we're updating to a new sequence number */
1612                if (nfs4_stateid_match_other(stateid, &state->open_stateid) &&
1613                        nfs4_stateid_is_next(&state->open_stateid, stateid)) {
1614                        return true;
1615                }
1616        } else {
1617                /* This is the first OPEN in this generation */
1618                if (stateid->seqid == cpu_to_be32(1))
1619                        return true;
1620        }
1621        return false;
1622}
1623
1624static void nfs_resync_open_stateid_locked(struct nfs4_state *state)
1625{
1626        if (!(state->n_wronly || state->n_rdonly || state->n_rdwr))
1627                return;
1628        if (state->n_wronly)
1629                set_bit(NFS_O_WRONLY_STATE, &state->flags);
1630        if (state->n_rdonly)
1631                set_bit(NFS_O_RDONLY_STATE, &state->flags);
1632        if (state->n_rdwr)
1633                set_bit(NFS_O_RDWR_STATE, &state->flags);
1634        set_bit(NFS_OPEN_STATE, &state->flags);
1635}
1636
1637static void nfs_clear_open_stateid_locked(struct nfs4_state *state,
1638                nfs4_stateid *stateid, fmode_t fmode)
1639{
1640        clear_bit(NFS_O_RDWR_STATE, &state->flags);
1641        switch (fmode & (FMODE_READ|FMODE_WRITE)) {
1642        case FMODE_WRITE:
1643                clear_bit(NFS_O_RDONLY_STATE, &state->flags);
1644                break;
1645        case FMODE_READ:
1646                clear_bit(NFS_O_WRONLY_STATE, &state->flags);
1647                break;
1648        case 0:
1649                clear_bit(NFS_O_RDONLY_STATE, &state->flags);
1650                clear_bit(NFS_O_WRONLY_STATE, &state->flags);
1651                clear_bit(NFS_OPEN_STATE, &state->flags);
1652        }
1653        if (stateid == NULL)
1654                return;
1655        /* Handle OPEN+OPEN_DOWNGRADE races */
1656        if (nfs4_stateid_match_other(stateid, &state->open_stateid) &&
1657            !nfs4_stateid_is_newer(stateid, &state->open_stateid)) {
1658                nfs_resync_open_stateid_locked(state);
1659                goto out;
1660        }
1661        if (test_bit(NFS_DELEGATED_STATE, &state->flags) == 0)
1662                nfs4_stateid_copy(&state->stateid, stateid);
1663        nfs4_stateid_copy(&state->open_stateid, stateid);
1664        trace_nfs4_open_stateid_update(state->inode, stateid, 0);
1665out:
1666        nfs_state_log_update_open_stateid(state);
1667}
1668
1669static void nfs_clear_open_stateid(struct nfs4_state *state,
1670        nfs4_stateid *arg_stateid,
1671        nfs4_stateid *stateid, fmode_t fmode)
1672{
1673        write_seqlock(&state->seqlock);
1674        /* Ignore, if the CLOSE argment doesn't match the current stateid */
1675        if (nfs4_state_match_open_stateid_other(state, arg_stateid))
1676                nfs_clear_open_stateid_locked(state, stateid, fmode);
1677        write_sequnlock(&state->seqlock);
1678        if (test_bit(NFS_STATE_RECLAIM_NOGRACE, &state->flags))
1679                nfs4_schedule_state_manager(state->owner->so_server->nfs_client);
1680}
1681
1682static void nfs_set_open_stateid_locked(struct nfs4_state *state,
1683                const nfs4_stateid *stateid, nfs4_stateid *freeme)
1684        __must_hold(&state->owner->so_lock)
1685        __must_hold(&state->seqlock)
1686        __must_hold(RCU)
1687
1688{
1689        DEFINE_WAIT(wait);
1690        int status = 0;
1691        for (;;) {
1692
1693                if (nfs_stateid_is_sequential(state, stateid))
1694                        break;
1695
1696                if (status)
1697                        break;
1698                /* Rely on seqids for serialisation with NFSv4.0 */
1699                if (!nfs4_has_session(NFS_SERVER(state->inode)->nfs_client))
1700                        break;
1701
1702                set_bit(NFS_STATE_CHANGE_WAIT, &state->flags);
1703                prepare_to_wait(&state->waitq, &wait, TASK_KILLABLE);
1704                /*
1705                 * Ensure we process the state changes in the same order
1706                 * in which the server processed them by delaying the
1707                 * update of the stateid until we are in sequence.
1708                 */
1709                write_sequnlock(&state->seqlock);
1710                spin_unlock(&state->owner->so_lock);
1711                rcu_read_unlock();
1712                trace_nfs4_open_stateid_update_wait(state->inode, stateid, 0);
1713
1714                if (!fatal_signal_pending(current)) {
1715                        if (schedule_timeout(5*HZ) == 0)
1716                                status = -EAGAIN;
1717                        else
1718                                status = 0;
1719                } else
1720                        status = -EINTR;
1721                finish_wait(&state->waitq, &wait);
1722                rcu_read_lock();
1723                spin_lock(&state->owner->so_lock);
1724                write_seqlock(&state->seqlock);
1725        }
1726
1727        if (test_bit(NFS_OPEN_STATE, &state->flags) &&
1728            !nfs4_stateid_match_other(stateid, &state->open_stateid)) {
1729                nfs4_stateid_copy(freeme, &state->open_stateid);
1730                nfs_test_and_clear_all_open_stateid(state);
1731        }
1732
1733        if (test_bit(NFS_DELEGATED_STATE, &state->flags) == 0)
1734                nfs4_stateid_copy(&state->stateid, stateid);
1735        nfs4_stateid_copy(&state->open_stateid, stateid);
1736        trace_nfs4_open_stateid_update(state->inode, stateid, status);
1737        nfs_state_log_update_open_stateid(state);
1738}
1739
1740static void nfs_state_set_open_stateid(struct nfs4_state *state,
1741                const nfs4_stateid *open_stateid,
1742                fmode_t fmode,
1743                nfs4_stateid *freeme)
1744{
1745        /*
1746         * Protect the call to nfs4_state_set_mode_locked and
1747         * serialise the stateid update
1748         */
1749        write_seqlock(&state->seqlock);
1750        nfs_set_open_stateid_locked(state, open_stateid, freeme);
1751        switch (fmode) {
1752        case FMODE_READ:
1753                set_bit(NFS_O_RDONLY_STATE, &state->flags);
1754                break;
1755        case FMODE_WRITE:
1756                set_bit(NFS_O_WRONLY_STATE, &state->flags);
1757                break;
1758        case FMODE_READ|FMODE_WRITE:
1759                set_bit(NFS_O_RDWR_STATE, &state->flags);
1760        }
1761        set_bit(NFS_OPEN_STATE, &state->flags);
1762        write_sequnlock(&state->seqlock);
1763}
1764
1765static void nfs_state_clear_open_state_flags(struct nfs4_state *state)
1766{
1767        clear_bit(NFS_O_RDWR_STATE, &state->flags);
1768        clear_bit(NFS_O_WRONLY_STATE, &state->flags);
1769        clear_bit(NFS_O_RDONLY_STATE, &state->flags);
1770        clear_bit(NFS_OPEN_STATE, &state->flags);
1771}
1772
1773static void nfs_state_set_delegation(struct nfs4_state *state,
1774                const nfs4_stateid *deleg_stateid,
1775                fmode_t fmode)
1776{
1777        /*
1778         * Protect the call to nfs4_state_set_mode_locked and
1779         * serialise the stateid update
1780         */
1781        write_seqlock(&state->seqlock);
1782        nfs4_stateid_copy(&state->stateid, deleg_stateid);
1783        set_bit(NFS_DELEGATED_STATE, &state->flags);
1784        write_sequnlock(&state->seqlock);
1785}
1786
1787static void nfs_state_clear_delegation(struct nfs4_state *state)
1788{
1789        write_seqlock(&state->seqlock);
1790        nfs4_stateid_copy(&state->stateid, &state->open_stateid);
1791        clear_bit(NFS_DELEGATED_STATE, &state->flags);
1792        write_sequnlock(&state->seqlock);
1793}
1794
1795int update_open_stateid(struct nfs4_state *state,
1796                const nfs4_stateid *open_stateid,
1797                const nfs4_stateid *delegation,
1798                fmode_t fmode)
1799{
1800        struct nfs_server *server = NFS_SERVER(state->inode);
1801        struct nfs_client *clp = server->nfs_client;
1802        struct nfs_inode *nfsi = NFS_I(state->inode);
1803        struct nfs_delegation *deleg_cur;
1804        nfs4_stateid freeme = { };
1805        int ret = 0;
1806
1807        fmode &= (FMODE_READ|FMODE_WRITE);
1808
1809        rcu_read_lock();
1810        spin_lock(&state->owner->so_lock);
1811        if (open_stateid != NULL) {
1812                nfs_state_set_open_stateid(state, open_stateid, fmode, &freeme);
1813                ret = 1;
1814        }
1815
1816        deleg_cur = nfs4_get_valid_delegation(state->inode);
1817        if (deleg_cur == NULL)
1818                goto no_delegation;
1819
1820        spin_lock(&deleg_cur->lock);
1821        if (rcu_dereference(nfsi->delegation) != deleg_cur ||
1822           test_bit(NFS_DELEGATION_RETURNING, &deleg_cur->flags) ||
1823            (deleg_cur->type & fmode) != fmode)
1824                goto no_delegation_unlock;
1825
1826        if (delegation == NULL)
1827                delegation = &deleg_cur->stateid;
1828        else if (!nfs4_stateid_match_other(&deleg_cur->stateid, delegation))
1829                goto no_delegation_unlock;
1830
1831        nfs_mark_delegation_referenced(deleg_cur);
1832        nfs_state_set_delegation(state, &deleg_cur->stateid, fmode);
1833        ret = 1;
1834no_delegation_unlock:
1835        spin_unlock(&deleg_cur->lock);
1836no_delegation:
1837        if (ret)
1838                update_open_stateflags(state, fmode);
1839        spin_unlock(&state->owner->so_lock);
1840        rcu_read_unlock();
1841
1842        if (test_bit(NFS_STATE_RECLAIM_NOGRACE, &state->flags))
1843                nfs4_schedule_state_manager(clp);
1844        if (freeme.type != 0)
1845                nfs4_test_and_free_stateid(server, &freeme,
1846                                state->owner->so_cred);
1847
1848        return ret;
1849}
1850
1851static bool nfs4_update_lock_stateid(struct nfs4_lock_state *lsp,
1852                const nfs4_stateid *stateid)
1853{
1854        struct nfs4_state *state = lsp->ls_state;
1855        bool ret = false;
1856
1857        spin_lock(&state->state_lock);
1858        if (!nfs4_stateid_match_other(stateid, &lsp->ls_stateid))
1859                goto out_noupdate;
1860        if (!nfs4_stateid_is_newer(stateid, &lsp->ls_stateid))
1861                goto out_noupdate;
1862        nfs4_stateid_copy(&lsp->ls_stateid, stateid);
1863        ret = true;
1864out_noupdate:
1865        spin_unlock(&state->state_lock);
1866        return ret;
1867}
1868
1869static void nfs4_return_incompatible_delegation(struct inode *inode, fmode_t fmode)
1870{
1871        struct nfs_delegation *delegation;
1872
1873        fmode &= FMODE_READ|FMODE_WRITE;
1874        rcu_read_lock();
1875        delegation = nfs4_get_valid_delegation(inode);
1876        if (delegation == NULL || (delegation->type & fmode) == fmode) {
1877                rcu_read_unlock();
1878                return;
1879        }
1880        rcu_read_unlock();
1881        nfs4_inode_return_delegation(inode);
1882}
1883
1884static struct nfs4_state *nfs4_try_open_cached(struct nfs4_opendata *opendata)
1885{
1886        struct nfs4_state *state = opendata->state;
1887        struct nfs_delegation *delegation;
1888        int open_mode = opendata->o_arg.open_flags;
1889        fmode_t fmode = opendata->o_arg.fmode;
1890        enum open_claim_type4 claim = opendata->o_arg.claim;
1891        nfs4_stateid stateid;
1892        int ret = -EAGAIN;
1893
1894        for (;;) {
1895                spin_lock(&state->owner->so_lock);
1896                if (can_open_cached(state, fmode, open_mode, claim)) {
1897                        update_open_stateflags(state, fmode);
1898                        spin_unlock(&state->owner->so_lock);
1899                        goto out_return_state;
1900                }
1901                spin_unlock(&state->owner->so_lock);
1902                rcu_read_lock();
1903                delegation = nfs4_get_valid_delegation(state->inode);
1904                if (!can_open_delegated(delegation, fmode, claim)) {
1905                        rcu_read_unlock();
1906                        break;
1907                }
1908                /* Save the delegation */
1909                nfs4_stateid_copy(&stateid, &delegation->stateid);
1910                rcu_read_unlock();
1911                nfs_release_seqid(opendata->o_arg.seqid);
1912                if (!opendata->is_recover) {
1913                        ret = nfs_may_open(state->inode, state->owner->so_cred, open_mode);
1914                        if (ret != 0)
1915                                goto out;
1916                }
1917                ret = -EAGAIN;
1918
1919                /* Try to update the stateid using the delegation */
1920                if (update_open_stateid(state, NULL, &stateid, fmode))
1921                        goto out_return_state;
1922        }
1923out:
1924        return ERR_PTR(ret);
1925out_return_state:
1926        refcount_inc(&state->count);
1927        return state;
1928}
1929
1930static void
1931nfs4_opendata_check_deleg(struct nfs4_opendata *data, struct nfs4_state *state)
1932{
1933        struct nfs_client *clp = NFS_SERVER(state->inode)->nfs_client;
1934        struct nfs_delegation *delegation;
1935        int delegation_flags = 0;
1936
1937        rcu_read_lock();
1938        delegation = rcu_dereference(NFS_I(state->inode)->delegation);
1939        if (delegation)
1940                delegation_flags = delegation->flags;
1941        rcu_read_unlock();
1942        switch (data->o_arg.claim) {
1943        default:
1944                break;
1945        case NFS4_OPEN_CLAIM_DELEGATE_CUR:
1946        case NFS4_OPEN_CLAIM_DELEG_CUR_FH:
1947                pr_err_ratelimited("NFS: Broken NFSv4 server %s is "
1948                                   "returning a delegation for "
1949                                   "OPEN(CLAIM_DELEGATE_CUR)\n",
1950                                   clp->cl_hostname);
1951                return;
1952        }
1953        if ((delegation_flags & 1UL<<NFS_DELEGATION_NEED_RECLAIM) == 0)
1954                nfs_inode_set_delegation(state->inode,
1955                                data->owner->so_cred,
1956                                data->o_res.delegation_type,
1957                                &data->o_res.delegation,
1958                                data->o_res.pagemod_limit);
1959        else
1960                nfs_inode_reclaim_delegation(state->inode,
1961                                data->owner->so_cred,
1962                                data->o_res.delegation_type,
1963                                &data->o_res.delegation,
1964                                data->o_res.pagemod_limit);
1965
1966        if (data->o_res.do_recall)
1967                nfs_async_inode_return_delegation(state->inode,
1968                                                  &data->o_res.delegation);
1969}
1970
1971/*
1972 * Check the inode attributes against the CLAIM_PREVIOUS returned attributes
1973 * and update the nfs4_state.
1974 */
1975static struct nfs4_state *
1976_nfs4_opendata_reclaim_to_nfs4_state(struct nfs4_opendata *data)
1977{
1978        struct inode *inode = data->state->inode;
1979        struct nfs4_state *state = data->state;
1980        int ret;
1981
1982        if (!data->rpc_done) {
1983                if (data->rpc_status)
1984                        return ERR_PTR(data->rpc_status);
1985                /* cached opens have already been processed */
1986                goto update;
1987        }
1988
1989        ret = nfs_refresh_inode(inode, &data->f_attr);
1990        if (ret)
1991                return ERR_PTR(ret);
1992
1993        if (data->o_res.delegation_type != 0)
1994                nfs4_opendata_check_deleg(data, state);
1995update:
1996        if (!update_open_stateid(state, &data->o_res.stateid,
1997                                NULL, data->o_arg.fmode))
1998                return ERR_PTR(-EAGAIN);
1999        refcount_inc(&state->count);
2000
2001        return state;
2002}
2003
2004static struct inode *
2005nfs4_opendata_get_inode(struct nfs4_opendata *data)
2006{
2007        struct inode *inode;
2008
2009        switch (data->o_arg.claim) {
2010        case NFS4_OPEN_CLAIM_NULL:
2011        case NFS4_OPEN_CLAIM_DELEGATE_CUR:
2012        case NFS4_OPEN_CLAIM_DELEGATE_PREV:
2013                if (!(data->f_attr.valid & NFS_ATTR_FATTR))
2014                        return ERR_PTR(-EAGAIN);
2015                inode = nfs_fhget(data->dir->d_sb, &data->o_res.fh,
2016                                &data->f_attr, data->f_label);
2017                break;
2018        default:
2019                inode = d_inode(data->dentry);
2020                ihold(inode);
2021                nfs_refresh_inode(inode, &data->f_attr);
2022        }
2023        return inode;
2024}
2025
2026static struct nfs4_state *
2027nfs4_opendata_find_nfs4_state(struct nfs4_opendata *data)
2028{
2029        struct nfs4_state *state;
2030        struct inode *inode;
2031
2032        inode = nfs4_opendata_get_inode(data);
2033        if (IS_ERR(inode))
2034                return ERR_CAST(inode);
2035        if (data->state != NULL && data->state->inode == inode) {
2036                state = data->state;
2037                refcount_inc(&state->count);
2038        } else
2039                state = nfs4_get_open_state(inode, data->owner);
2040        iput(inode);
2041        if (state == NULL)
2042                state = ERR_PTR(-ENOMEM);
2043        return state;
2044}
2045
2046static struct nfs4_state *
2047_nfs4_opendata_to_nfs4_state(struct nfs4_opendata *data)
2048{
2049        struct nfs4_state *state;
2050
2051        if (!data->rpc_done) {
2052                state = nfs4_try_open_cached(data);
2053                trace_nfs4_cached_open(data->state);
2054                goto out;
2055        }
2056
2057        state = nfs4_opendata_find_nfs4_state(data);
2058        if (IS_ERR(state))
2059                goto out;
2060
2061        if (data->o_res.delegation_type != 0)
2062                nfs4_opendata_check_deleg(data, state);
2063        if (!update_open_stateid(state, &data->o_res.stateid,
2064                                NULL, data->o_arg.fmode)) {
2065                nfs4_put_open_state(state);
2066                state = ERR_PTR(-EAGAIN);
2067        }
2068out:
2069        nfs_release_seqid(data->o_arg.seqid);
2070        return state;
2071}
2072
2073static struct nfs4_state *
2074nfs4_opendata_to_nfs4_state(struct nfs4_opendata *data)
2075{
2076        struct nfs4_state *ret;
2077
2078        if (data->o_arg.claim == NFS4_OPEN_CLAIM_PREVIOUS)
2079                ret =_nfs4_opendata_reclaim_to_nfs4_state(data);
2080        else
2081                ret = _nfs4_opendata_to_nfs4_state(data);
2082        nfs4_sequence_free_slot(&data->o_res.seq_res);
2083        return ret;
2084}
2085
2086static struct nfs_open_context *
2087nfs4_state_find_open_context_mode(struct nfs4_state *state, fmode_t mode)
2088{
2089        struct nfs_inode *nfsi = NFS_I(state->inode);
2090        struct nfs_open_context *ctx;
2091
2092        rcu_read_lock();
2093        list_for_each_entry_rcu(ctx, &nfsi->open_files, list) {
2094                if (ctx->state != state)
2095                        continue;
2096                if ((ctx->mode & mode) != mode)
2097                        continue;
2098                if (!get_nfs_open_context(ctx))
2099                        continue;
2100                rcu_read_unlock();
2101                return ctx;
2102        }
2103        rcu_read_unlock();
2104        return ERR_PTR(-ENOENT);
2105}
2106
2107static struct nfs_open_context *
2108nfs4_state_find_open_context(struct nfs4_state *state)
2109{
2110        struct nfs_open_context *ctx;
2111
2112        ctx = nfs4_state_find_open_context_mode(state, FMODE_READ|FMODE_WRITE);
2113        if (!IS_ERR(ctx))
2114                return ctx;
2115        ctx = nfs4_state_find_open_context_mode(state, FMODE_WRITE);
2116        if (!IS_ERR(ctx))
2117                return ctx;
2118        return nfs4_state_find_open_context_mode(state, FMODE_READ);
2119}
2120
2121static struct nfs4_opendata *nfs4_open_recoverdata_alloc(struct nfs_open_context *ctx,
2122                struct nfs4_state *state, enum open_claim_type4 claim)
2123{
2124        struct nfs4_opendata *opendata;
2125
2126        opendata = nfs4_opendata_alloc(ctx->dentry, state->owner, 0, 0,
2127                        NULL, claim, GFP_NOFS);
2128        if (opendata == NULL)
2129                return ERR_PTR(-ENOMEM);
2130        opendata->state = state;
2131        refcount_inc(&state->count);
2132        return opendata;
2133}
2134
2135static int nfs4_open_recover_helper(struct nfs4_opendata *opendata,
2136                fmode_t fmode)
2137{
2138        struct nfs4_state *newstate;
2139        int ret;
2140
2141        if (!nfs4_mode_match_open_stateid(opendata->state, fmode))
2142                return 0;
2143        opendata->o_arg.open_flags = 0;
2144        opendata->o_arg.fmode = fmode;
2145        opendata->o_arg.share_access = nfs4_map_atomic_open_share(
2146                        NFS_SB(opendata->dentry->d_sb),
2147                        fmode, 0);
2148        memset(&opendata->o_res, 0, sizeof(opendata->o_res));
2149        memset(&opendata->c_res, 0, sizeof(opendata->c_res));
2150        nfs4_init_opendata_res(opendata);
2151        ret = _nfs4_recover_proc_open(opendata);
2152        if (ret != 0)
2153                return ret; 
2154        newstate = nfs4_opendata_to_nfs4_state(opendata);
2155        if (IS_ERR(newstate))
2156                return PTR_ERR(newstate);
2157        if (newstate != opendata->state)
2158                ret = -ESTALE;
2159        nfs4_close_state(newstate, fmode);
2160        return ret;
2161}
2162
2163static int nfs4_open_recover(struct nfs4_opendata *opendata, struct nfs4_state *state)
2164{
2165        int ret;
2166
2167        /* memory barrier prior to reading state->n_* */
2168        smp_rmb();
2169        ret = nfs4_open_recover_helper(opendata, FMODE_READ|FMODE_WRITE);
2170        if (ret != 0)
2171                return ret;
2172        ret = nfs4_open_recover_helper(opendata, FMODE_WRITE);
2173        if (ret != 0)
2174                return ret;
2175        ret = nfs4_open_recover_helper(opendata, FMODE_READ);
2176        if (ret != 0)
2177                return ret;
2178        /*
2179         * We may have performed cached opens for all three recoveries.
2180         * Check if we need to update the current stateid.
2181         */
2182        if (test_bit(NFS_DELEGATED_STATE, &state->flags) == 0 &&
2183            !nfs4_stateid_match(&state->stateid, &state->open_stateid)) {
2184                write_seqlock(&state->seqlock);
2185                if (test_bit(NFS_DELEGATED_STATE, &state->flags) == 0)
2186                        nfs4_stateid_copy(&state->stateid, &state->open_stateid);
2187                write_sequnlock(&state->seqlock);
2188        }
2189        return 0;
2190}
2191
2192/*
2193 * OPEN_RECLAIM:
2194 *      reclaim state on the server after a reboot.
2195 */
2196static int _nfs4_do_open_reclaim(struct nfs_open_context *ctx, struct nfs4_state *state)
2197{
2198        struct nfs_delegation *delegation;
2199        struct nfs4_opendata *opendata;
2200        fmode_t delegation_type = 0;
2201        int status;
2202
2203        opendata = nfs4_open_recoverdata_alloc(ctx, state,
2204                        NFS4_OPEN_CLAIM_PREVIOUS);
2205        if (IS_ERR(opendata))
2206                return PTR_ERR(opendata);
2207        rcu_read_lock();
2208        delegation = rcu_dereference(NFS_I(state->inode)->delegation);
2209        if (delegation != NULL && test_bit(NFS_DELEGATION_NEED_RECLAIM, &delegation->flags) != 0)
2210                delegation_type = delegation->type;
2211        rcu_read_unlock();
2212        opendata->o_arg.u.delegation_type = delegation_type;
2213        status = nfs4_open_recover(opendata, state);
2214        nfs4_opendata_put(opendata);
2215        return status;
2216}
2217
2218static int nfs4_do_open_reclaim(struct nfs_open_context *ctx, struct nfs4_state *state)
2219{
2220        struct nfs_server *server = NFS_SERVER(state->inode);
2221        struct nfs4_exception exception = { };
2222        int err;
2223        do {
2224                err = _nfs4_do_open_reclaim(ctx, state);
2225                trace_nfs4_open_reclaim(ctx, 0, err);
2226                if (nfs4_clear_cap_atomic_open_v1(server, err, &exception))
2227                        continue;
2228                if (err != -NFS4ERR_DELAY)
2229                        break;
2230                nfs4_handle_exception(server, err, &exception);
2231        } while (exception.retry);
2232        return err;
2233}
2234
2235static int nfs4_open_reclaim(struct nfs4_state_owner *sp, struct nfs4_state *state)
2236{
2237        struct nfs_open_context *ctx;
2238        int ret;
2239
2240        ctx = nfs4_state_find_open_context(state);
2241        if (IS_ERR(ctx))
2242                return -EAGAIN;
2243        clear_bit(NFS_DELEGATED_STATE, &state->flags);
2244        nfs_state_clear_open_state_flags(state);
2245        ret = nfs4_do_open_reclaim(ctx, state);
2246        put_nfs_open_context(ctx);
2247        return ret;
2248}
2249
2250static int nfs4_handle_delegation_recall_error(struct nfs_server *server, struct nfs4_state *state, const nfs4_stateid *stateid, struct file_lock *fl, int err)
2251{
2252        switch (err) {
2253                default:
2254                        printk(KERN_ERR "NFS: %s: unhandled error "
2255                                        "%d.\n", __func__, err);
2256                        fallthrough;
2257                case 0:
2258                case -ENOENT:
2259                case -EAGAIN:
2260                case -ESTALE:
2261                case -ETIMEDOUT:
2262                        break;
2263                case -NFS4ERR_BADSESSION:
2264                case -NFS4ERR_BADSLOT:
2265                case -NFS4ERR_BAD_HIGH_SLOT:
2266                case -NFS4ERR_CONN_NOT_BOUND_TO_SESSION:
2267                case -NFS4ERR_DEADSESSION:
2268                        return -EAGAIN;
2269                case -NFS4ERR_STALE_CLIENTID:
2270                case -NFS4ERR_STALE_STATEID:
2271                        /* Don't recall a delegation if it was lost */
2272                        nfs4_schedule_lease_recovery(server->nfs_client);
2273                        return -EAGAIN;
2274                case -NFS4ERR_MOVED:
2275                        nfs4_schedule_migration_recovery(server);
2276                        return -EAGAIN;
2277                case -NFS4ERR_LEASE_MOVED:
2278                        nfs4_schedule_lease_moved_recovery(server->nfs_client);
2279                        return -EAGAIN;
2280                case -NFS4ERR_DELEG_REVOKED:
2281                case -NFS4ERR_ADMIN_REVOKED:
2282                case -NFS4ERR_EXPIRED:
2283                case -NFS4ERR_BAD_STATEID:
2284                case -NFS4ERR_OPENMODE:
2285                        nfs_inode_find_state_and_recover(state->inode,
2286                                        stateid);
2287                        nfs4_schedule_stateid_recovery(server, state);
2288                        return -EAGAIN;
2289                case -NFS4ERR_DELAY:
2290                case -NFS4ERR_GRACE:
2291                        ssleep(1);
2292                        return -EAGAIN;
2293                case -ENOMEM:
2294                case -NFS4ERR_DENIED:
2295                        if (fl) {
2296                                struct nfs4_lock_state *lsp = fl->fl_u.nfs4_fl.owner;
2297                                if (lsp)
2298                                        set_bit(NFS_LOCK_LOST, &lsp->ls_flags);
2299                        }
2300                        return 0;
2301        }
2302        return err;
2303}
2304
2305int nfs4_open_delegation_recall(struct nfs_open_context *ctx,
2306                struct nfs4_state *state, const nfs4_stateid *stateid)
2307{
2308        struct nfs_server *server = NFS_SERVER(state->inode);
2309        struct nfs4_opendata *opendata;
2310        int err = 0;
2311
2312        opendata = nfs4_open_recoverdata_alloc(ctx, state,
2313                        NFS4_OPEN_CLAIM_DELEG_CUR_FH);
2314        if (IS_ERR(opendata))
2315                return PTR_ERR(opendata);
2316        nfs4_stateid_copy(&opendata->o_arg.u.delegation, stateid);
2317        if (!test_bit(NFS_O_RDWR_STATE, &state->flags)) {
2318                err = nfs4_open_recover_helper(opendata, FMODE_READ|FMODE_WRITE);
2319                if (err)
2320                        goto out;
2321        }
2322        if (!test_bit(NFS_O_WRONLY_STATE, &state->flags)) {
2323                err = nfs4_open_recover_helper(opendata, FMODE_WRITE);
2324                if (err)
2325                        goto out;
2326        }
2327        if (!test_bit(NFS_O_RDONLY_STATE, &state->flags)) {
2328                err = nfs4_open_recover_helper(opendata, FMODE_READ);
2329                if (err)
2330                        goto out;
2331        }
2332        nfs_state_clear_delegation(state);
2333out:
2334        nfs4_opendata_put(opendata);
2335        return nfs4_handle_delegation_recall_error(server, state, stateid, NULL, err);
2336}
2337
2338static void nfs4_open_confirm_prepare(struct rpc_task *task, void *calldata)
2339{
2340        struct nfs4_opendata *data = calldata;
2341
2342        nfs4_setup_sequence(data->o_arg.server->nfs_client,
2343                           &data->c_arg.seq_args, &data->c_res.seq_res, task);
2344}
2345
2346static void nfs4_open_confirm_done(struct rpc_task *task, void *calldata)
2347{
2348        struct nfs4_opendata *data = calldata;
2349
2350        nfs40_sequence_done(task, &data->c_res.seq_res);
2351
2352        data->rpc_status = task->tk_status;
2353        if (data->rpc_status == 0) {
2354                nfs4_stateid_copy(&data->o_res.stateid, &data->c_res.stateid);
2355                nfs_confirm_seqid(&data->owner->so_seqid, 0);
2356                renew_lease(data->o_res.server, data->timestamp);
2357                data->rpc_done = true;
2358        }
2359}
2360
2361static void nfs4_open_confirm_release(void *calldata)
2362{
2363        struct nfs4_opendata *data = calldata;
2364        struct nfs4_state *state = NULL;
2365
2366        /* If this request hasn't been cancelled, do nothing */
2367        if (!data->cancelled)
2368                goto out_free;
2369        /* In case of error, no cleanup! */
2370        if (!data->rpc_done)
2371                goto out_free;
2372        state = nfs4_opendata_to_nfs4_state(data);
2373        if (!IS_ERR(state))
2374                nfs4_close_state(state, data->o_arg.fmode);
2375out_free:
2376        nfs4_opendata_put(data);
2377}
2378
2379static const struct rpc_call_ops nfs4_open_confirm_ops = {
2380        .rpc_call_prepare = nfs4_open_confirm_prepare,
2381        .rpc_call_done = nfs4_open_confirm_done,
2382        .rpc_release = nfs4_open_confirm_release,
2383};
2384
2385/*
2386 * Note: On error, nfs4_proc_open_confirm will free the struct nfs4_opendata
2387 */
2388static int _nfs4_proc_open_confirm(struct nfs4_opendata *data)
2389{
2390        struct nfs_server *server = NFS_SERVER(d_inode(data->dir));
2391        struct rpc_task *task;
2392        struct  rpc_message msg = {
2393                .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_OPEN_CONFIRM],
2394                .rpc_argp = &data->c_arg,
2395                .rpc_resp = &data->c_res,
2396                .rpc_cred = data->owner->so_cred,
2397        };
2398        struct rpc_task_setup task_setup_data = {
2399                .rpc_client = server->client,
2400                .rpc_message = &msg,
2401                .callback_ops = &nfs4_open_confirm_ops,
2402                .callback_data = data,
2403                .workqueue = nfsiod_workqueue,
2404                .flags = RPC_TASK_ASYNC | RPC_TASK_CRED_NOREF,
2405        };
2406        int status;
2407
2408        nfs4_init_sequence(&data->c_arg.seq_args, &data->c_res.seq_res, 1,
2409                                data->is_recover);
2410        kref_get(&data->kref);
2411        data->rpc_done = false;
2412        data->rpc_status = 0;
2413        data->timestamp = jiffies;
2414        task = rpc_run_task(&task_setup_data);
2415        if (IS_ERR(task))
2416                return PTR_ERR(task);
2417        status = rpc_wait_for_completion_task(task);
2418        if (status != 0) {
2419                data->cancelled = true;
2420                smp_wmb();
2421        } else
2422                status = data->rpc_status;
2423        rpc_put_task(task);
2424        return status;
2425}
2426
2427static void nfs4_open_prepare(struct rpc_task *task, void *calldata)
2428{
2429        struct nfs4_opendata *data = calldata;
2430        struct nfs4_state_owner *sp = data->owner;
2431        struct nfs_client *clp = sp->so_server->nfs_client;
2432        enum open_claim_type4 claim = data->o_arg.claim;
2433
2434        if (nfs_wait_on_sequence(data->o_arg.seqid, task) != 0)
2435                goto out_wait;
2436        /*
2437         * Check if we still need to send an OPEN call, or if we can use
2438         * a delegation instead.
2439         */
2440        if (data->state != NULL) {
2441                struct nfs_delegation *delegation;
2442
2443                if (can_open_cached(data->state, data->o_arg.fmode,
2444                                        data->o_arg.open_flags, claim))
2445                        goto out_no_action;
2446                rcu_read_lock();
2447                delegation = nfs4_get_valid_delegation(data->state->inode);
2448                if (can_open_delegated(delegation, data->o_arg.fmode, claim))
2449                        goto unlock_no_action;
2450                rcu_read_unlock();
2451        }
2452        /* Update client id. */
2453        data->o_arg.clientid = clp->cl_clientid;
2454        switch (claim) {
2455        default:
2456                break;
2457        case NFS4_OPEN_CLAIM_PREVIOUS:
2458        case NFS4_OPEN_CLAIM_DELEG_CUR_FH:
2459        case NFS4_OPEN_CLAIM_DELEG_PREV_FH:
2460                data->o_arg.open_bitmap = &nfs4_open_noattr_bitmap[0];
2461                fallthrough;
2462        case NFS4_OPEN_CLAIM_FH:
2463                task->tk_msg.rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_OPEN_NOATTR];
2464        }
2465        data->timestamp = jiffies;
2466        if (nfs4_setup_sequence(data->o_arg.server->nfs_client,
2467                                &data->o_arg.seq_args,
2468                                &data->o_res.seq_res,
2469                                task) != 0)
2470                nfs_release_seqid(data->o_arg.seqid);
2471
2472        /* Set the create mode (note dependency on the session type) */
2473        data->o_arg.createmode = NFS4_CREATE_UNCHECKED;
2474        if (data->o_arg.open_flags & O_EXCL) {
2475                data->o_arg.createmode = NFS4_CREATE_EXCLUSIVE;
2476                if (nfs4_has_persistent_session(clp))
2477                        data->o_arg.createmode = NFS4_CREATE_GUARDED;
2478                else if (clp->cl_mvops->minor_version > 0)
2479                        data->o_arg.createmode = NFS4_CREATE_EXCLUSIVE4_1;
2480        }
2481        return;
2482unlock_no_action:
2483        trace_nfs4_cached_open(data->state);
2484        rcu_read_unlock();
2485out_no_action:
2486        task->tk_action = NULL;
2487out_wait:
2488        nfs4_sequence_done(task, &data->o_res.seq_res);
2489}
2490
2491static void nfs4_open_done(struct rpc_task *task, void *calldata)
2492{
2493        struct nfs4_opendata *data = calldata;
2494
2495        data->rpc_status = task->tk_status;
2496
2497        if (!nfs4_sequence_process(task, &data->o_res.seq_res))
2498                return;
2499
2500        if (task->tk_status == 0) {
2501                if (data->o_res.f_attr->valid & NFS_ATTR_FATTR_TYPE) {
2502                        switch (data->o_res.f_attr->mode & S_IFMT) {
2503                        case S_IFREG:
2504                                break;
2505                        case S_IFLNK:
2506                                data->rpc_status = -ELOOP;
2507                                break;
2508                        case S_IFDIR:
2509                                data->rpc_status = -EISDIR;
2510                                break;
2511                        default:
2512                                data->rpc_status = -ENOTDIR;
2513                        }
2514                }
2515                renew_lease(data->o_res.server, data->timestamp);
2516                if (!(data->o_res.rflags & NFS4_OPEN_RESULT_CONFIRM))
2517                        nfs_confirm_seqid(&data->owner->so_seqid, 0);
2518        }
2519        data->rpc_done = true;
2520}
2521
2522static void nfs4_open_release(void *calldata)
2523{
2524        struct nfs4_opendata *data = calldata;
2525        struct nfs4_state *state = NULL;
2526
2527        /* If this request hasn't been cancelled, do nothing */
2528        if (!data->cancelled)
2529                goto out_free;
2530        /* In case of error, no cleanup! */
2531        if (data->rpc_status != 0 || !data->rpc_done)
2532                goto out_free;
2533        /* In case we need an open_confirm, no cleanup! */
2534        if (data->o_res.rflags & NFS4_OPEN_RESULT_CONFIRM)
2535                goto out_free;
2536        state = nfs4_opendata_to_nfs4_state(data);
2537        if (!IS_ERR(state))
2538                nfs4_close_state(state, data->o_arg.fmode);
2539out_free:
2540        nfs4_opendata_put(data);
2541}
2542
2543static const struct rpc_call_ops nfs4_open_ops = {
2544        .rpc_call_prepare = nfs4_open_prepare,
2545        .rpc_call_done = nfs4_open_done,
2546        .rpc_release = nfs4_open_release,
2547};
2548
2549static int nfs4_run_open_task(struct nfs4_opendata *data,
2550                              struct nfs_open_context *ctx)
2551{
2552        struct inode *dir = d_inode(data->dir);
2553        struct nfs_server *server = NFS_SERVER(dir);
2554        struct nfs_openargs *o_arg = &data->o_arg;
2555        struct nfs_openres *o_res = &data->o_res;
2556        struct rpc_task *task;
2557        struct rpc_message msg = {
2558                .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_OPEN],
2559                .rpc_argp = o_arg,
2560                .rpc_resp = o_res,
2561                .rpc_cred = data->owner->so_cred,
2562        };
2563        struct rpc_task_setup task_setup_data = {
2564                .rpc_client = server->client,
2565                .rpc_message = &msg,
2566                .callback_ops = &nfs4_open_ops,
2567                .callback_data = data,
2568                .workqueue = nfsiod_workqueue,
2569                .flags = RPC_TASK_ASYNC | RPC_TASK_CRED_NOREF,
2570        };
2571        int status;
2572
2573        if (server->nfs_client->cl_minorversion)
2574                task_setup_data.flags |= RPC_TASK_MOVEABLE;
2575
2576        kref_get(&data->kref);
2577        data->rpc_done = false;
2578        data->rpc_status = 0;
2579        data->cancelled = false;
2580        data->is_recover = false;
2581        if (!ctx) {
2582                nfs4_init_sequence(&o_arg->seq_args, &o_res->seq_res, 1, 1);
2583                data->is_recover = true;
2584                task_setup_data.flags |= RPC_TASK_TIMEOUT;
2585        } else {
2586                nfs4_init_sequence(&o_arg->seq_args, &o_res->seq_res, 1, 0);
2587                pnfs_lgopen_prepare(data, ctx);
2588        }
2589        task = rpc_run_task(&task_setup_data);
2590        if (IS_ERR(task))
2591                return PTR_ERR(task);
2592        status = rpc_wait_for_completion_task(task);
2593        if (status != 0) {
2594                data->cancelled = true;
2595                smp_wmb();
2596        } else
2597                status = data->rpc_status;
2598        rpc_put_task(task);
2599
2600        return status;
2601}
2602
2603static int _nfs4_recover_proc_open(struct nfs4_opendata *data)
2604{
2605        struct inode *dir = d_inode(data->dir);
2606        struct nfs_openres *o_res = &data->o_res;
2607        int status;
2608
2609        status = nfs4_run_open_task(data, NULL);
2610        if (status != 0 || !data->rpc_done)
2611                return status;
2612
2613        nfs_fattr_map_and_free_names(NFS_SERVER(dir), &data->f_attr);
2614
2615        if (o_res->rflags & NFS4_OPEN_RESULT_CONFIRM)
2616                status = _nfs4_proc_open_confirm(data);
2617
2618        return status;
2619}
2620
2621/*
2622 * Additional permission checks in order to distinguish between an
2623 * open for read, and an open for execute. This works around the
2624 * fact that NFSv4 OPEN treats read and execute permissions as being
2625 * the same.
2626 * Note that in the non-execute case, we want to turn off permission
2627 * checking if we just created a new file (POSIX open() semantics).
2628 */
2629static int nfs4_opendata_access(const struct cred *cred,
2630                                struct nfs4_opendata *opendata,
2631                                struct nfs4_state *state, fmode_t fmode,
2632                                int openflags)
2633{
2634        struct nfs_access_entry cache;
2635        u32 mask, flags;
2636
2637        /* access call failed or for some reason the server doesn't
2638         * support any access modes -- defer access call until later */
2639        if (opendata->o_res.access_supported == 0)
2640                return 0;
2641
2642        mask = 0;
2643        /*
2644         * Use openflags to check for exec, because fmode won't
2645         * always have FMODE_EXEC set when file open for exec.
2646         */
2647        if (openflags & __FMODE_EXEC) {
2648                /* ONLY check for exec rights */
2649                if (S_ISDIR(state->inode->i_mode))
2650                        mask = NFS4_ACCESS_LOOKUP;
2651                else
2652                        mask = NFS4_ACCESS_EXECUTE;
2653        } else if ((fmode & FMODE_READ) && !opendata->file_created)
2654                mask = NFS4_ACCESS_READ;
2655
2656        cache.cred = cred;
2657        nfs_access_set_mask(&cache, opendata->o_res.access_result);
2658        nfs_access_add_cache(state->inode, &cache);
2659
2660        flags = NFS4_ACCESS_READ | NFS4_ACCESS_EXECUTE | NFS4_ACCESS_LOOKUP;
2661        if ((mask & ~cache.mask & flags) == 0)
2662                return 0;
2663
2664        return -EACCES;
2665}
2666
2667/*
2668 * Note: On error, nfs4_proc_open will free the struct nfs4_opendata
2669 */
2670static int _nfs4_proc_open(struct nfs4_opendata *data,
2671                           struct nfs_open_context *ctx)
2672{
2673        struct inode *dir = d_inode(data->dir);
2674        struct nfs_server *server = NFS_SERVER(dir);
2675        struct nfs_openargs *o_arg = &data->o_arg;
2676        struct nfs_openres *o_res = &data->o_res;
2677        int status;
2678
2679        status = nfs4_run_open_task(data, ctx);
2680        if (!data->rpc_done)
2681                return status;
2682        if (status != 0) {
2683                if (status == -NFS4ERR_BADNAME &&
2684                                !(o_arg->open_flags & O_CREAT))
2685                        return -ENOENT;
2686                return status;
2687        }
2688
2689        nfs_fattr_map_and_free_names(server, &data->f_attr);
2690
2691        if (o_arg->open_flags & O_CREAT) {
2692                if (o_arg->open_flags & O_EXCL)
2693                        data->file_created = true;
2694                else if (o_res->cinfo.before != o_res->cinfo.after)
2695                        data->file_created = true;
2696                if (data->file_created ||
2697                    inode_peek_iversion_raw(dir) != o_res->cinfo.after)
2698                        nfs4_update_changeattr(dir, &o_res->cinfo,
2699                                        o_res->f_attr->time_start,
2700                                        NFS_INO_INVALID_DATA);
2701        }
2702        if ((o_res->rflags & NFS4_OPEN_RESULT_LOCKTYPE_POSIX) == 0)
2703                server->caps &= ~NFS_CAP_POSIX_LOCK;
2704        if(o_res->rflags & NFS4_OPEN_RESULT_CONFIRM) {
2705                status = _nfs4_proc_open_confirm(data);
2706                if (status != 0)
2707                        return status;
2708        }
2709        if (!(o_res->f_attr->valid & NFS_ATTR_FATTR)) {
2710                nfs4_sequence_free_slot(&o_res->seq_res);
2711                nfs4_proc_getattr(server, &o_res->fh, o_res->f_attr,
2712                                o_res->f_label, NULL);
2713        }
2714        return 0;
2715}
2716
2717/*
2718 * OPEN_EXPIRED:
2719 *      reclaim state on the server after a network partition.
2720 *      Assumes caller holds the appropriate lock
2721 */
2722static int _nfs4_open_expired(struct nfs_open_context *ctx, struct nfs4_state *state)
2723{
2724        struct nfs4_opendata *opendata;
2725        int ret;
2726
2727        opendata = nfs4_open_recoverdata_alloc(ctx, state,
2728                        NFS4_OPEN_CLAIM_FH);
2729        if (IS_ERR(opendata))
2730                return PTR_ERR(opendata);
2731        ret = nfs4_open_recover(opendata, state);
2732        if (ret == -ESTALE)
2733                d_drop(ctx->dentry);
2734        nfs4_opendata_put(opendata);
2735        return ret;
2736}
2737
2738static int nfs4_do_open_expired(struct nfs_open_context *ctx, struct nfs4_state *state)
2739{
2740        struct nfs_server *server = NFS_SERVER(state->inode);
2741        struct nfs4_exception exception = { };
2742        int err;
2743
2744        do {
2745                err = _nfs4_open_expired(ctx, state);
2746                trace_nfs4_open_expired(ctx, 0, err);
2747                if (nfs4_clear_cap_atomic_open_v1(server, err, &exception))
2748                        continue;
2749                switch (err) {
2750                default:
2751                        goto out;
2752                case -NFS4ERR_GRACE:
2753                case -NFS4ERR_DELAY:
2754                        nfs4_handle_exception(server, err, &exception);
2755                        err = 0;
2756                }
2757        } while (exception.retry);
2758out:
2759        return err;
2760}
2761
2762static int nfs4_open_expired(struct nfs4_state_owner *sp, struct nfs4_state *state)
2763{
2764        struct nfs_open_context *ctx;
2765        int ret;
2766
2767        ctx = nfs4_state_find_open_context(state);
2768        if (IS_ERR(ctx))
2769                return -EAGAIN;
2770        ret = nfs4_do_open_expired(ctx, state);
2771        put_nfs_open_context(ctx);
2772        return ret;
2773}
2774
2775static void nfs_finish_clear_delegation_stateid(struct nfs4_state *state,
2776                const nfs4_stateid *stateid)
2777{
2778        nfs_remove_bad_delegation(state->inode, stateid);
2779        nfs_state_clear_delegation(state);
2780}
2781
2782static void nfs40_clear_delegation_stateid(struct nfs4_state *state)
2783{
2784        if (rcu_access_pointer(NFS_I(state->inode)->delegation) != NULL)
2785                nfs_finish_clear_delegation_stateid(state, NULL);
2786}
2787
2788static int nfs40_open_expired(struct nfs4_state_owner *sp, struct nfs4_state *state)
2789{
2790        /* NFSv4.0 doesn't allow for delegation recovery on open expire */
2791        nfs40_clear_delegation_stateid(state);
2792        nfs_state_clear_open_state_flags(state);
2793        return nfs4_open_expired(sp, state);
2794}
2795
2796static int nfs40_test_and_free_expired_stateid(struct nfs_server *server,
2797                nfs4_stateid *stateid,
2798                const struct cred *cred)
2799{
2800        return -NFS4ERR_BAD_STATEID;
2801}
2802
2803#if defined(CONFIG_NFS_V4_1)
2804static int nfs41_test_and_free_expired_stateid(struct nfs_server *server,
2805                nfs4_stateid *stateid,
2806                const struct cred *cred)
2807{
2808        int status;
2809
2810        switch (stateid->type) {
2811        default:
2812                break;
2813        case NFS4_INVALID_STATEID_TYPE:
2814        case NFS4_SPECIAL_STATEID_TYPE:
2815                return -NFS4ERR_BAD_STATEID;
2816        case NFS4_REVOKED_STATEID_TYPE:
2817                goto out_free;
2818        }
2819
2820        status = nfs41_test_stateid(server, stateid, cred);
2821        switch (status) {
2822        case -NFS4ERR_EXPIRED:
2823        case -NFS4ERR_ADMIN_REVOKED:
2824        case -NFS4ERR_DELEG_REVOKED:
2825                break;
2826        default:
2827                return status;
2828        }
2829out_free:
2830        /* Ack the revoked state to the server */
2831        nfs41_free_stateid(server, stateid, cred, true);
2832        return -NFS4ERR_EXPIRED;
2833}
2834
2835static int nfs41_check_delegation_stateid(struct nfs4_state *state)
2836{
2837        struct nfs_server *server = NFS_SERVER(state->inode);
2838        nfs4_stateid stateid;
2839        struct nfs_delegation *delegation;
2840        const struct cred *cred = NULL;
2841        int status, ret = NFS_OK;
2842
2843        /* Get the delegation credential for use by test/free_stateid */
2844        rcu_read_lock();
2845        delegation = rcu_dereference(NFS_I(state->inode)->delegation);
2846        if (delegation == NULL) {
2847                rcu_read_unlock();
2848                nfs_state_clear_delegation(state);
2849                return NFS_OK;
2850        }
2851
2852        spin_lock(&delegation->lock);
2853        nfs4_stateid_copy(&stateid, &delegation->stateid);
2854
2855        if (!test_and_clear_bit(NFS_DELEGATION_TEST_EXPIRED,
2856                                &delegation->flags)) {
2857                spin_unlock(&delegation->lock);
2858                rcu_read_unlock();
2859                return NFS_OK;
2860        }
2861
2862        if (delegation->cred)
2863                cred = get_cred(delegation->cred);
2864        spin_unlock(&delegation->lock);
2865        rcu_read_unlock();
2866        status = nfs41_test_and_free_expired_stateid(server, &stateid, cred);
2867        trace_nfs4_test_delegation_stateid(state, NULL, status);
2868        if (status == -NFS4ERR_EXPIRED || status == -NFS4ERR_BAD_STATEID)
2869                nfs_finish_clear_delegation_stateid(state, &stateid);
2870        else
2871                ret = status;
2872
2873        put_cred(cred);
2874        return ret;
2875}
2876
2877static void nfs41_delegation_recover_stateid(struct nfs4_state *state)
2878{
2879        nfs4_stateid tmp;
2880
2881        if (test_bit(NFS_DELEGATED_STATE, &state->flags) &&
2882            nfs4_copy_delegation_stateid(state->inode, state->state,
2883                                &tmp, NULL) &&
2884            nfs4_stateid_match_other(&state->stateid, &tmp))
2885                nfs_state_set_delegation(state, &tmp, state->state);
2886        else
2887                nfs_state_clear_delegation(state);
2888}
2889
2890/**
2891 * nfs41_check_expired_locks - possibly free a lock stateid
2892 *
2893 * @state: NFSv4 state for an inode
2894 *
2895 * Returns NFS_OK if recovery for this stateid is now finished.
2896 * Otherwise a negative NFS4ERR value is returned.
2897 */
2898static int nfs41_check_expired_locks(struct nfs4_state *state)
2899{
2900        int status, ret = NFS_OK;
2901        struct nfs4_lock_state *lsp, *prev = NULL;
2902        struct nfs_server *server = NFS_SERVER(state->inode);
2903
2904        if (!test_bit(LK_STATE_IN_USE, &state->flags))
2905                goto out;
2906
2907        spin_lock(&state->state_lock);
2908        list_for_each_entry(lsp, &state->lock_states, ls_locks) {
2909                if (test_bit(NFS_LOCK_INITIALIZED, &lsp->ls_flags)) {
2910                        const struct cred *cred = lsp->ls_state->owner->so_cred;
2911
2912                        refcount_inc(&lsp->ls_count);
2913                        spin_unlock(&state->state_lock);
2914
2915                        nfs4_put_lock_state(prev);
2916                        prev = lsp;
2917
2918                        status = nfs41_test_and_free_expired_stateid(server,
2919                                        &lsp->ls_stateid,
2920                                        cred);
2921                        trace_nfs4_test_lock_stateid(state, lsp, status);
2922                        if (status == -NFS4ERR_EXPIRED ||
2923                            status == -NFS4ERR_BAD_STATEID) {
2924                                clear_bit(NFS_LOCK_INITIALIZED, &lsp->ls_flags);
2925                                lsp->ls_stateid.type = NFS4_INVALID_STATEID_TYPE;
2926                                if (!recover_lost_locks)
2927                                        set_bit(NFS_LOCK_LOST, &lsp->ls_flags);
2928                        } else if (status != NFS_OK) {
2929                                ret = status;
2930                                nfs4_put_lock_state(prev);
2931                                goto out;
2932                        }
2933                        spin_lock(&state->state_lock);
2934                }
2935        }
2936        spin_unlock(&state->state_lock);
2937        nfs4_put_lock_state(prev);
2938out:
2939        return ret;
2940}
2941
2942/**
2943 * nfs41_check_open_stateid - possibly free an open stateid
2944 *
2945 * @state: NFSv4 state for an inode
2946 *
2947 * Returns NFS_OK if recovery for this stateid is now finished.
2948 * Otherwise a negative NFS4ERR value is returned.
2949 */
2950static int nfs41_check_open_stateid(struct nfs4_state *state)
2951{
2952        struct nfs_server *server = NFS_SERVER(state->inode);
2953        nfs4_stateid *stateid = &state->open_stateid;
2954        const struct cred *cred = state->owner->so_cred;
2955        int status;
2956
2957        if (test_bit(NFS_OPEN_STATE, &state->flags) == 0)
2958                return -NFS4ERR_BAD_STATEID;
2959        status = nfs41_test_and_free_expired_stateid(server, stateid, cred);
2960        trace_nfs4_test_open_stateid(state, NULL, status);
2961        if (status == -NFS4ERR_EXPIRED || status == -NFS4ERR_BAD_STATEID) {
2962                nfs_state_clear_open_state_flags(state);
2963                stateid->type = NFS4_INVALID_STATEID_TYPE;
2964                return status;
2965        }
2966        if (nfs_open_stateid_recover_openmode(state))
2967                return -NFS4ERR_OPENMODE;
2968        return NFS_OK;
2969}
2970
2971static int nfs41_open_expired(struct nfs4_state_owner *sp, struct nfs4_state *state)
2972{
2973        int status;
2974
2975        status = nfs41_check_delegation_stateid(state);
2976        if (status != NFS_OK)
2977                return status;
2978        nfs41_delegation_recover_stateid(state);
2979
2980        status = nfs41_check_expired_locks(state);
2981        if (status != NFS_OK)
2982                return status;
2983        status = nfs41_check_open_stateid(state);
2984        if (status != NFS_OK)
2985                status = nfs4_open_expired(sp, state);
2986        return status;
2987}
2988#endif
2989
2990/*
2991 * on an EXCLUSIVE create, the server should send back a bitmask with FATTR4-*
2992 * fields corresponding to attributes that were used to store the verifier.
2993 * Make sure we clobber those fields in the later setattr call
2994 */
2995static unsigned nfs4_exclusive_attrset(struct nfs4_opendata *opendata,
2996                                struct iattr *sattr, struct nfs4_label **label)
2997{
2998        const __u32 *bitmask = opendata->o_arg.server->exclcreat_bitmask;
2999        __u32 attrset[3];
3000        unsigned ret;
3001        unsigned i;
3002
3003        for (i = 0; i < ARRAY_SIZE(attrset); i++) {
3004                attrset[i] = opendata->o_res.attrset[i];
3005                if (opendata->o_arg.createmode == NFS4_CREATE_EXCLUSIVE4_1)
3006                        attrset[i] &= ~bitmask[i];
3007        }
3008
3009        ret = (opendata->o_arg.createmode == NFS4_CREATE_EXCLUSIVE) ?
3010                sattr->ia_valid : 0;
3011
3012        if ((attrset[1] & (FATTR4_WORD1_TIME_ACCESS|FATTR4_WORD1_TIME_ACCESS_SET))) {
3013                if (sattr->ia_valid & ATTR_ATIME_SET)
3014                        ret |= ATTR_ATIME_SET;
3015                else
3016                        ret |= ATTR_ATIME;
3017        }
3018
3019        if ((attrset[1] & (FATTR4_WORD1_TIME_MODIFY|FATTR4_WORD1_TIME_MODIFY_SET))) {
3020                if (sattr->ia_valid & ATTR_MTIME_SET)
3021                        ret |= ATTR_MTIME_SET;
3022                else
3023                        ret |= ATTR_MTIME;
3024        }
3025
3026        if (!(attrset[2] & FATTR4_WORD2_SECURITY_LABEL))
3027                *label = NULL;
3028        return ret;
3029}
3030
3031static int _nfs4_open_and_get_state(struct nfs4_opendata *opendata,
3032                int flags, struct nfs_open_context *ctx)
3033{
3034        struct nfs4_state_owner *sp = opendata->owner;
3035        struct nfs_server *server = sp->so_server;
3036        struct dentry *dentry;
3037        struct nfs4_state *state;
3038        fmode_t acc_mode = _nfs4_ctx_to_accessmode(ctx);
3039        struct inode *dir = d_inode(opendata->dir);
3040        unsigned long dir_verifier;
3041        unsigned int seq;
3042        int ret;
3043
3044        seq = raw_seqcount_begin(&sp->so_reclaim_seqcount);
3045        dir_verifier = nfs_save_change_attribute(dir);
3046
3047        ret = _nfs4_proc_open(opendata, ctx);
3048        if (ret != 0)
3049                goto out;
3050
3051        state = _nfs4_opendata_to_nfs4_state(opendata);
3052        ret = PTR_ERR(state);
3053        if (IS_ERR(state))
3054                goto out;
3055        ctx->state = state;
3056        if (server->caps & NFS_CAP_POSIX_LOCK)
3057                set_bit(NFS_STATE_POSIX_LOCKS, &state->flags);
3058        if (opendata->o_res.rflags & NFS4_OPEN_RESULT_MAY_NOTIFY_LOCK)
3059                set_bit(NFS_STATE_MAY_NOTIFY_LOCK, &state->flags);
3060
3061        dentry = opendata->dentry;
3062        if (d_really_is_negative(dentry)) {
3063                struct dentry *alias;
3064                d_drop(dentry);
3065                alias = d_exact_alias(dentry, state->inode);
3066                if (!alias)
3067                        alias = d_splice_alias(igrab(state->inode), dentry);
3068                /* d_splice_alias() can't fail here - it's a non-directory */
3069                if (alias) {
3070                        dput(ctx->dentry);
3071                        ctx->dentry = dentry = alias;
3072                }
3073        }
3074
3075        switch(opendata->o_arg.claim) {
3076        default:
3077                break;
3078        case NFS4_OPEN_CLAIM_NULL:
3079        case NFS4_OPEN_CLAIM_DELEGATE_CUR:
3080        case NFS4_OPEN_CLAIM_DELEGATE_PREV:
3081                if (!opendata->rpc_done)
3082                        break;
3083                if (opendata->o_res.delegation_type != 0)
3084                        dir_verifier = nfs_save_change_attribute(dir);
3085                nfs_set_verifier(dentry, dir_verifier);
3086        }
3087
3088        /* Parse layoutget results before we check for access */
3089        pnfs_parse_lgopen(state->inode, opendata->lgp, ctx);
3090
3091        ret = nfs4_opendata_access(sp->so_cred, opendata, state,
3092                        acc_mode, flags);
3093        if (ret != 0)
3094                goto out;
3095
3096        if (d_inode(dentry) == state->inode) {
3097                nfs_inode_attach_open_context(ctx);
3098                if (read_seqcount_retry(&sp->so_reclaim_seqcount, seq))
3099                        nfs4_schedule_stateid_recovery(server, state);
3100        }
3101
3102out:
3103        if (!opendata->cancelled)
3104                nfs4_sequence_free_slot(&opendata->o_res.seq_res);
3105        return ret;
3106}
3107
3108/*
3109 * Returns a referenced nfs4_state
3110 */
3111static int _nfs4_do_open(struct inode *dir,
3112                        struct nfs_open_context *ctx,
3113                        int flags,
3114                        const struct nfs4_open_createattrs *c,
3115                        int *opened)
3116{
3117        struct nfs4_state_owner  *sp;
3118        struct nfs4_state     *state = NULL;
3119        struct nfs_server       *server = NFS_SERVER(dir);
3120        struct nfs4_opendata *opendata;
3121        struct dentry *dentry = ctx->dentry;
3122        const struct cred *cred = ctx->cred;
3123        struct nfs4_threshold **ctx_th = &ctx->mdsthreshold;
3124        fmode_t fmode = _nfs4_ctx_to_openmode(ctx);
3125        enum open_claim_type4 claim = NFS4_OPEN_CLAIM_NULL;
3126        struct iattr *sattr = c->sattr;
3127        struct nfs4_label *label = c->label;
3128        struct nfs4_label *olabel = NULL;
3129        int status;
3130
3131        /* Protect against reboot recovery conflicts */
3132        status = -ENOMEM;
3133        sp = nfs4_get_state_owner(server, cred, GFP_KERNEL);
3134        if (sp == NULL) {
3135                dprintk("nfs4_do_open: nfs4_get_state_owner failed!\n");
3136                goto out_err;
3137        }
3138        status = nfs4_client_recover_expired_lease(server->nfs_client);
3139        if (status != 0)
3140                goto err_put_state_owner;
3141        if (d_really_is_positive(dentry))
3142                nfs4_return_incompatible_delegation(d_inode(dentry), fmode);
3143        status = -ENOMEM;
3144        if (d_really_is_positive(dentry))
3145                claim = NFS4_OPEN_CLAIM_FH;
3146        opendata = nfs4_opendata_alloc(dentry, sp, fmode, flags,
3147                        c, claim, GFP_KERNEL);
3148        if (opendata == NULL)
3149                goto err_put_state_owner;
3150
3151        if (label) {
3152                olabel = nfs4_label_alloc(server, GFP_KERNEL);
3153                if (IS_ERR(olabel)) {
3154                        status = PTR_ERR(olabel);
3155                        goto err_opendata_put;
3156                }
3157        }
3158
3159        if (server->attr_bitmask[2] & FATTR4_WORD2_MDSTHRESHOLD) {
3160                if (!opendata->f_attr.mdsthreshold) {
3161                        opendata->f_attr.mdsthreshold = pnfs_mdsthreshold_alloc();
3162                        if (!opendata->f_attr.mdsthreshold)
3163                                goto err_free_label;
3164                }
3165                opendata->o_arg.open_bitmap = &nfs4_pnfs_open_bitmap[0];
3166        }
3167        if (d_really_is_positive(dentry))
3168                opendata->state = nfs4_get_open_state(d_inode(dentry), sp);
3169
3170        status = _nfs4_open_and_get_state(opendata, flags, ctx);
3171        if (status != 0)
3172                goto err_free_label;
3173        state = ctx->state;
3174
3175        if ((opendata->o_arg.open_flags & (O_CREAT|O_EXCL)) == (O_CREAT|O_EXCL) &&
3176            (opendata->o_arg.createmode != NFS4_CREATE_GUARDED)) {
3177                unsigned attrs = nfs4_exclusive_attrset(opendata, sattr, &label);
3178                /*
3179                 * send create attributes which was not set by open
3180                 * with an extra setattr.
3181                 */
3182                if (attrs || label) {
3183                        unsigned ia_old = sattr->ia_valid;
3184
3185                        sattr->ia_valid = attrs;
3186                        nfs_fattr_init(opendata->o_res.f_attr);
3187                        status = nfs4_do_setattr(state->inode, cred,
3188                                        opendata->o_res.f_attr, sattr,
3189                                        ctx, label, olabel);
3190                        if (status == 0) {
3191                                nfs_setattr_update_inode(state->inode, sattr,
3192                                                opendata->o_res.f_attr);
3193                                nfs_setsecurity(state->inode, opendata->o_res.f_attr, olabel);
3194                        }
3195                        sattr->ia_valid = ia_old;
3196                }
3197        }
3198        if (opened && opendata->file_created)
3199                *opened = 1;
3200
3201        if (pnfs_use_threshold(ctx_th, opendata->f_attr.mdsthreshold, server)) {
3202                *ctx_th = opendata->f_attr.mdsthreshold;
3203                opendata->f_attr.mdsthreshold = NULL;
3204        }
3205
3206        nfs4_label_free(olabel);
3207
3208        nfs4_opendata_put(opendata);
3209        nfs4_put_state_owner(sp);
3210        return 0;
3211err_free_label:
3212        nfs4_label_free(olabel);
3213err_opendata_put:
3214        nfs4_opendata_put(opendata);
3215err_put_state_owner:
3216        nfs4_put_state_owner(sp);
3217out_err:
3218        return status;
3219}
3220
3221
3222static struct nfs4_state *nfs4_do_open(struct inode *dir,
3223                                        struct nfs_open_context *ctx,
3224                                        int flags,
3225                                        struct iattr *sattr,
3226                                        struct nfs4_label *label,
3227                                        int *opened)
3228{
3229        struct nfs_server *server = NFS_SERVER(dir);
3230        struct nfs4_exception exception = {
3231                .interruptible = true,
3232        };
3233        struct nfs4_state *res;
3234        struct nfs4_open_createattrs c = {
3235                .label = label,
3236                .sattr = sattr,
3237                .verf = {
3238                        [0] = (__u32)jiffies,
3239                        [1] = (__u32)current->pid,
3240                },
3241        };
3242        int status;
3243
3244        do {
3245                status = _nfs4_do_open(dir, ctx, flags, &c, opened);
3246                res = ctx->state;
3247                trace_nfs4_open_file(ctx, flags, status);
3248                if (status == 0)
3249                        break;
3250                /* NOTE: BAD_SEQID means the server and client disagree about the
3251                 * book-keeping w.r.t. state-changing operations
3252                 * (OPEN/CLOSE/LOCK/LOCKU...)
3253                 * It is actually a sign of a bug on the client or on the server.
3254                 *
3255                 * If we receive a BAD_SEQID error in the particular case of
3256                 * doing an OPEN, we assume that nfs_increment_open_seqid() will
3257                 * have unhashed the old state_owner for us, and that we can
3258                 * therefore safely retry using a new one. We should still warn
3259                 * the user though...
3260                 */
3261                if (status == -NFS4ERR_BAD_SEQID) {
3262                        pr_warn_ratelimited("NFS: v4 server %s "
3263                                        " returned a bad sequence-id error!\n",
3264                                        NFS_SERVER(dir)->nfs_client->cl_hostname);
3265                        exception.retry = 1;
3266                        continue;
3267                }
3268                /*
3269                 * BAD_STATEID on OPEN means that the server cancelled our
3270                 * state before it received the OPEN_CONFIRM.
3271                 * Recover by retrying the request as per the discussion
3272                 * on Page 181 of RFC3530.
3273                 */
3274                if (status == -NFS4ERR_BAD_STATEID) {
3275                        exception.retry = 1;
3276                        continue;
3277                }
3278                if (status == -NFS4ERR_EXPIRED) {
3279                        nfs4_schedule_lease_recovery(server->nfs_client);
3280                        exception.retry = 1;
3281                        continue;
3282                }
3283                if (status == -EAGAIN) {
3284                        /* We must have found a delegation */
3285                        exception.retry = 1;
3286                        continue;
3287                }
3288                if (nfs4_clear_cap_atomic_open_v1(server, status, &exception))
3289                        continue;
3290                res = ERR_PTR(nfs4_handle_exception(server,
3291                                        status, &exception));
3292        } while (exception.retry);
3293        return res;
3294}
3295
3296static int _nfs4_do_setattr(struct inode *inode,
3297                            struct nfs_setattrargs *arg,
3298                            struct nfs_setattrres *res,
3299                            const struct cred *cred,
3300                            struct nfs_open_context *ctx)
3301{
3302        struct nfs_server *server = NFS_SERVER(inode);
3303        struct rpc_message msg = {
3304                .rpc_proc       = &nfs4_procedures[NFSPROC4_CLNT_SETATTR],
3305                .rpc_argp       = arg,
3306                .rpc_resp       = res,
3307                .rpc_cred       = cred,
3308        };
3309        const struct cred *delegation_cred = NULL;
3310        unsigned long timestamp = jiffies;
3311        bool truncate;
3312        int status;
3313
3314        nfs_fattr_init(res->fattr);
3315
3316        /* Servers should only apply open mode checks for file size changes */
3317        truncate = (arg->iap->ia_valid & ATTR_SIZE) ? true : false;
3318        if (!truncate) {
3319                nfs4_inode_make_writeable(inode);
3320                goto zero_stateid;
3321        }
3322
3323        if (nfs4_copy_delegation_stateid(inode, FMODE_WRITE, &arg->stateid, &delegation_cred)) {
3324                /* Use that stateid */
3325        } else if (ctx != NULL && ctx->state) {
3326                struct nfs_lock_context *l_ctx;
3327                if (!nfs4_valid_open_stateid(ctx->state))
3328                        return -EBADF;
3329                l_ctx = nfs_get_lock_context(ctx);
3330                if (IS_ERR(l_ctx))
3331                        return PTR_ERR(l_ctx);
3332                status = nfs4_select_rw_stateid(ctx->state, FMODE_WRITE, l_ctx,
3333                                                &arg->stateid, &delegation_cred);
3334                nfs_put_lock_context(l_ctx);
3335                if (status == -EIO)
3336                        return -EBADF;
3337                else if (status == -EAGAIN)
3338                        goto zero_stateid;
3339        } else {
3340zero_stateid:
3341                nfs4_stateid_copy(&arg->stateid, &zero_stateid);
3342        }
3343        if (delegation_cred)
3344                msg.rpc_cred = delegation_cred;
3345
3346        status = nfs4_call_sync(server->client, server, &msg, &arg->seq_args, &res->seq_res, 1);
3347
3348        put_cred(delegation_cred);
3349        if (status == 0 && ctx != NULL)
3350                renew_lease(server, timestamp);
3351        trace_nfs4_setattr(inode, &arg->stateid, status);
3352        return status;
3353}
3354
3355static int nfs4_do_setattr(struct inode *inode, const struct cred *cred,
3356                           struct nfs_fattr *fattr, struct iattr *sattr,
3357                           struct nfs_open_context *ctx, struct nfs4_label *ilabel,
3358                           struct nfs4_label *olabel)
3359{
3360        struct nfs_server *server = NFS_SERVER(inode);
3361        __u32 bitmask[NFS4_BITMASK_SZ];
3362        struct nfs4_state *state = ctx ? ctx->state : NULL;
3363        struct nfs_setattrargs  arg = {
3364                .fh             = NFS_FH(inode),
3365                .iap            = sattr,
3366                .server         = server,
3367                .bitmask = bitmask,
3368                .label          = ilabel,
3369        };
3370        struct nfs_setattrres  res = {
3371                .fattr          = fattr,
3372                .label          = olabel,
3373                .server         = server,
3374        };
3375        struct nfs4_exception exception = {
3376                .state = state,
3377                .inode = inode,
3378                .stateid = &arg.stateid,
3379        };
3380        unsigned long adjust_flags = NFS_INO_INVALID_CHANGE;
3381        int err;
3382
3383        if (sattr->ia_valid & (ATTR_MODE | ATTR_KILL_SUID | ATTR_KILL_SGID))
3384                adjust_flags |= NFS_INO_INVALID_MODE;
3385        if (sattr->ia_valid & (ATTR_UID | ATTR_GID))
3386                adjust_flags |= NFS_INO_INVALID_OTHER;
3387
3388        do {
3389                nfs4_bitmap_copy_adjust(bitmask, nfs4_bitmask(server, olabel),
3390                                        inode, adjust_flags);
3391
3392                err = _nfs4_do_setattr(inode, &arg, &res, cred, ctx);
3393                switch (err) {
3394                case -NFS4ERR_OPENMODE:
3395                        if (!(sattr->ia_valid & ATTR_SIZE)) {
3396                                pr_warn_once("NFSv4: server %s is incorrectly "
3397                                                "applying open mode checks to "
3398                                                "a SETATTR that is not "
3399                                                "changing file size.\n",
3400                                                server->nfs_client->cl_hostname);
3401                        }
3402                        if (state && !(state->state & FMODE_WRITE)) {
3403                                err = -EBADF;
3404                                if (sattr->ia_valid & ATTR_OPEN)
3405                                        err = -EACCES;
3406                                goto out;
3407                        }
3408                }
3409                err = nfs4_handle_exception(server, err, &exception);
3410        } while (exception.retry);
3411out:
3412        return err;
3413}
3414
3415static bool
3416nfs4_wait_on_layoutreturn(struct inode *inode, struct rpc_task *task)
3417{
3418        if (inode == NULL || !nfs_have_layout(inode))
3419                return false;
3420
3421        return pnfs_wait_on_layoutreturn(inode, task);
3422}
3423
3424/*
3425 * Update the seqid of an open stateid
3426 */
3427static void nfs4_sync_open_stateid(nfs4_stateid *dst,
3428                struct nfs4_state *state)
3429{
3430        __be32 seqid_open;
3431        u32 dst_seqid;
3432        int seq;
3433
3434        for (;;) {
3435                if (!nfs4_valid_open_stateid(state))
3436                        break;
3437                seq = read_seqbegin(&state->seqlock);
3438                if (!nfs4_state_match_open_stateid_other(state, dst)) {
3439                        nfs4_stateid_copy(dst, &state->open_stateid);
3440                        if (read_seqretry(&state->seqlock, seq))
3441                                continue;
3442                        break;
3443                }
3444                seqid_open = state->open_stateid.seqid;
3445                if (read_seqretry(&state->seqlock, seq))
3446                        continue;
3447
3448                dst_seqid = be32_to_cpu(dst->seqid);
3449                if ((s32)(dst_seqid - be32_to_cpu(seqid_open)) < 0)
3450                        dst->seqid = seqid_open;
3451                break;
3452        }
3453}
3454
3455/*
3456 * Update the seqid of an open stateid after receiving
3457 * NFS4ERR_OLD_STATEID
3458 */
3459static bool nfs4_refresh_open_old_stateid(nfs4_stateid *dst,
3460                struct nfs4_state *state)
3461{
3462        __be32 seqid_open;
3463        u32 dst_seqid;
3464        bool ret;
3465        int seq, status = -EAGAIN;
3466        DEFINE_WAIT(wait);
3467
3468        for (;;) {
3469                ret = false;
3470                if (!nfs4_valid_open_stateid(state))
3471                        break;
3472                seq = read_seqbegin(&state->seqlock);
3473                if (!nfs4_state_match_open_stateid_other(state, dst)) {
3474                        if (read_seqretry(&state->seqlock, seq))
3475                                continue;
3476                        break;
3477                }
3478
3479                write_seqlock(&state->seqlock);
3480                seqid_open = state->open_stateid.seqid;
3481
3482                dst_seqid = be32_to_cpu(dst->seqid);
3483
3484                /* Did another OPEN bump the state's seqid?  try again: */
3485                if ((s32)(be32_to_cpu(seqid_open) - dst_seqid) > 0) {
3486                        dst->seqid = seqid_open;
3487                        write_sequnlock(&state->seqlock);
3488                        ret = true;
3489                        break;
3490                }
3491
3492                /* server says we're behind but we haven't seen the update yet */
3493                set_bit(NFS_STATE_CHANGE_WAIT, &state->flags);
3494                prepare_to_wait(&state->waitq, &wait, TASK_KILLABLE);
3495                write_sequnlock(&state->seqlock);
3496                trace_nfs4_close_stateid_update_wait(state->inode, dst, 0);
3497
3498                if (fatal_signal_pending(current))
3499                        status = -EINTR;
3500                else
3501                        if (schedule_timeout(5*HZ) != 0)
3502                                status = 0;
3503
3504                finish_wait(&state->waitq, &wait);
3505
3506                if (!status)
3507                        continue;
3508                if (status == -EINTR)
3509                        break;
3510
3511                /* we slept the whole 5 seconds, we must have lost a seqid */
3512                dst->seqid = cpu_to_be32(dst_seqid + 1);
3513                ret = true;
3514                break;
3515        }
3516
3517        return ret;
3518}
3519
3520struct nfs4_closedata {
3521        struct inode *inode;
3522        struct nfs4_state *state;
3523        struct nfs_closeargs arg;
3524        struct nfs_closeres res;
3525        struct {
3526                struct nfs4_layoutreturn_args arg;
3527                struct nfs4_layoutreturn_res res;
3528                struct nfs4_xdr_opaque_data ld_private;
3529                u32 roc_barrier;
3530                bool roc;
3531        } lr;
3532        struct nfs_fattr fattr;
3533        unsigned long timestamp;
3534};
3535
3536static void nfs4_free_closedata(void *data)
3537{
3538        struct nfs4_closedata *calldata = data;
3539        struct nfs4_state_owner *sp = calldata->state->owner;
3540        struct super_block *sb = calldata->state->inode->i_sb;
3541
3542        if (calldata->lr.roc)
3543                pnfs_roc_release(&calldata->lr.arg, &calldata->lr.res,
3544                                calldata->res.lr_ret);
3545        nfs4_put_open_state(calldata->state);
3546        nfs_free_seqid(calldata->arg.seqid);
3547        nfs4_put_state_owner(sp);
3548        nfs_sb_deactive(sb);
3549        kfree(calldata);
3550}
3551
3552static void nfs4_close_done(struct rpc_task *task, void *data)
3553{
3554        struct nfs4_closedata *calldata = data;
3555        struct nfs4_state *state = calldata->state;
3556        struct nfs_server *server = NFS_SERVER(calldata->inode);
3557        nfs4_stateid *res_stateid = NULL;
3558        struct nfs4_exception exception = {
3559                .state = state,
3560                .inode = calldata->inode,
3561                .stateid = &calldata->arg.stateid,
3562        };
3563
3564        dprintk("%s: begin!\n", __func__);
3565        if (!nfs4_sequence_done(task, &calldata->res.seq_res))
3566                return;
3567        trace_nfs4_close(state, &calldata->arg, &calldata->res, task->tk_status);
3568
3569        /* Handle Layoutreturn errors */
3570        if (pnfs_roc_done(task, &calldata->arg.lr_args, &calldata->res.lr_res,
3571                          &calldata->res.lr_ret) == -EAGAIN)
3572                goto out_restart;
3573
3574        /* hmm. we are done with the inode, and in the process of freeing
3575         * the state_owner. we keep this around to process errors
3576         */
3577        switch (task->tk_status) {
3578                case 0:
3579                        res_stateid = &calldata->res.stateid;
3580                        renew_lease(server, calldata->timestamp);
3581                        break;
3582                case -NFS4ERR_ACCESS:
3583                        if (calldata->arg.bitmask != NULL) {
3584                                calldata->arg.bitmask = NULL;
3585                                calldata->res.fattr = NULL;
3586                                goto out_restart;
3587
3588                        }
3589                        break;
3590                case -NFS4ERR_OLD_STATEID:
3591                        /* Did we race with OPEN? */
3592                        if (nfs4_refresh_open_old_stateid(&calldata->arg.stateid,
3593                                                state))
3594                                goto out_restart;
3595                        goto out_release;
3596                case -NFS4ERR_ADMIN_REVOKED:
3597                case -NFS4ERR_STALE_STATEID:
3598                case -NFS4ERR_EXPIRED:
3599                        nfs4_free_revoked_stateid(server,
3600                                        &calldata->arg.stateid,
3601                                        task->tk_msg.rpc_cred);
3602                        fallthrough;
3603                case -NFS4ERR_BAD_STATEID:
3604                        if (calldata->arg.fmode == 0)
3605                                break;
3606                        fallthrough;
3607                default:
3608                        task->tk_status = nfs4_async_handle_exception(task,
3609                                        server, task->tk_status, &exception);
3610                        if (exception.retry)
3611                                goto out_restart;
3612        }
3613        nfs_clear_open_stateid(state, &calldata->arg.stateid,
3614                        res_stateid, calldata->arg.fmode);
3615out_release:
3616        task->tk_status = 0;
3617        nfs_release_seqid(calldata->arg.seqid);
3618        nfs_refresh_inode(calldata->inode, &calldata->fattr);
3619        dprintk("%s: done, ret = %d!\n", __func__, task->tk_status);
3620        return;
3621out_restart:
3622        task->tk_status = 0;
3623        rpc_restart_call_prepare(task);
3624        goto out_release;
3625}
3626
3627static void nfs4_close_prepare(struct rpc_task *task, void *data)
3628{
3629        struct nfs4_closedata *calldata = data;
3630        struct nfs4_state *state = calldata->state;
3631        struct inode *inode = calldata->inode;
3632        struct nfs_server *server = NFS_SERVER(inode);
3633        struct pnfs_layout_hdr *lo;
3634        bool is_rdonly, is_wronly, is_rdwr;
3635        int call_close = 0;
3636
3637        dprintk("%s: begin!\n", __func__);
3638        if (nfs_wait_on_sequence(calldata->arg.seqid, task) != 0)
3639                goto out_wait;
3640
3641        task->tk_msg.rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_OPEN_DOWNGRADE];
3642        spin_lock(&state->owner->so_lock);
3643        is_rdwr = test_bit(NFS_O_RDWR_STATE, &state->flags);
3644        is_rdonly = test_bit(NFS_O_RDONLY_STATE, &state->flags);
3645        is_wronly = test_bit(NFS_O_WRONLY_STATE, &state->flags);
3646        /* Calculate the change in open mode */
3647        calldata->arg.fmode = 0;
3648        if (state->n_rdwr == 0) {
3649                if (state->n_rdonly == 0)
3650                        call_close |= is_rdonly;
3651                else if (is_rdonly)
3652                        calldata->arg.fmode |= FMODE_READ;
3653                if (state->n_wronly == 0)
3654                        call_close |= is_wronly;
3655                else if (is_wronly)
3656                        calldata->arg.fmode |= FMODE_WRITE;
3657                if (calldata->arg.fmode != (FMODE_READ|FMODE_WRITE))
3658                        call_close |= is_rdwr;
3659        } else if (is_rdwr)
3660                calldata->arg.fmode |= FMODE_READ|FMODE_WRITE;
3661
3662        nfs4_sync_open_stateid(&calldata->arg.stateid, state);
3663        if (!nfs4_valid_open_stateid(state))
3664                call_close = 0;
3665        spin_unlock(&state->owner->so_lock);
3666
3667        if (!call_close) {
3668                /* Note: exit _without_ calling nfs4_close_done */
3669                goto out_no_action;
3670        }
3671
3672        if (!calldata->lr.roc && nfs4_wait_on_layoutreturn(inode, task)) {
3673                nfs_release_seqid(calldata->arg.seqid);
3674                goto out_wait;
3675        }
3676
3677        lo = calldata->arg.lr_args ? calldata->arg.lr_args->layout : NULL;
3678        if (lo && !pnfs_layout_is_valid(lo)) {
3679                calldata->arg.lr_args = NULL;
3680                calldata->res.lr_res = NULL;
3681        }
3682
3683        if (calldata->arg.fmode == 0)
3684                task->tk_msg.rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_CLOSE];
3685
3686        if (calldata->arg.fmode == 0 || calldata->arg.fmode == FMODE_READ) {
3687                /* Close-to-open cache consistency revalidation */
3688                if (!nfs4_have_delegation(inode, FMODE_READ)) {
3689                        nfs4_bitmask_set(calldata->arg.bitmask_store,
3690                                         server->cache_consistency_bitmask,
3691                                         inode, server, NULL);
3692                        calldata->arg.bitmask = calldata->arg.bitmask_store;
3693                } else
3694                        calldata->arg.bitmask = NULL;
3695        }
3696
3697        calldata->arg.share_access =
3698                nfs4_map_atomic_open_share(NFS_SERVER(inode),
3699                                calldata->arg.fmode, 0);
3700
3701        if (calldata->res.fattr == NULL)
3702                calldata->arg.bitmask = NULL;
3703        else if (calldata->arg.bitmask == NULL)
3704                calldata->res.fattr = NULL;
3705        calldata->timestamp = jiffies;
3706        if (nfs4_setup_sequence(NFS_SERVER(inode)->nfs_client,
3707                                &calldata->arg.seq_args,
3708                                &calldata->res.seq_res,
3709                                task) != 0)
3710                nfs_release_seqid(calldata->arg.seqid);
3711        dprintk("%s: done!\n", __func__);
3712        return;
3713out_no_action:
3714        task->tk_action = NULL;
3715out_wait:
3716        nfs4_sequence_done(task, &calldata->res.seq_res);
3717}
3718
3719static const struct rpc_call_ops nfs4_close_ops = {
3720        .rpc_call_prepare = nfs4_close_prepare,
3721        .rpc_call_done = nfs4_close_done,
3722        .rpc_release = nfs4_free_closedata,
3723};
3724
3725/* 
3726 * It is possible for data to be read/written from a mem-mapped file 
3727 * after the sys_close call (which hits the vfs layer as a flush).
3728 * This means that we can't safely call nfsv4 close on a file until 
3729 * the inode is cleared. This in turn means that we are not good
3730 * NFSv4 citizens - we do not indicate to the server to update the file's 
3731 * share state even when we are done with one of the three share 
3732 * stateid's in the inode.
3733 *
3734 * NOTE: Caller must be holding the sp->so_owner semaphore!
3735 */
3736int nfs4_do_close(struct nfs4_state *state, gfp_t gfp_mask, int wait)
3737{
3738        struct nfs_server *server = NFS_SERVER(state->inode);
3739        struct nfs_seqid *(*alloc_seqid)(struct nfs_seqid_counter *, gfp_t);
3740        struct nfs4_closedata *calldata;
3741        struct nfs4_state_owner *sp = state->owner;
3742        struct rpc_task *task;
3743        struct rpc_message msg = {
3744                .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_CLOSE],
3745                .rpc_cred = state->owner->so_cred,
3746        };
3747        struct rpc_task_setup task_setup_data = {
3748                .rpc_client = server->client,
3749                .rpc_message = &msg,
3750                .callback_ops = &nfs4_close_ops,
3751                .workqueue = nfsiod_workqueue,
3752                .flags = RPC_TASK_ASYNC | RPC_TASK_CRED_NOREF,
3753        };
3754        int status = -ENOMEM;
3755
3756        if (server->nfs_client->cl_minorversion)
3757                task_setup_data.flags |= RPC_TASK_MOVEABLE;
3758
3759        nfs4_state_protect(server->nfs_client, NFS_SP4_MACH_CRED_CLEANUP,
3760                &task_setup_data.rpc_client, &msg);
3761
3762        calldata = kzalloc(sizeof(*calldata), gfp_mask);
3763        if (calldata == NULL)
3764                goto out;
3765        nfs4_init_sequence(&calldata->arg.seq_args, &calldata->res.seq_res, 1, 0);
3766        calldata->inode = state->inode;
3767        calldata->state = state;
3768        calldata->arg.fh = NFS_FH(state->inode);
3769        if (!nfs4_copy_open_stateid(&calldata->arg.stateid, state))
3770                goto out_free_calldata;
3771        /* Serialization for the sequence id */
3772        alloc_seqid = server->nfs_client->cl_mvops->alloc_seqid;
3773        calldata->arg.seqid = alloc_seqid(&state->owner->so_seqid, gfp_mask);
3774        if (IS_ERR(calldata->arg.seqid))
3775                goto out_free_calldata;
3776        nfs_fattr_init(&calldata->fattr);
3777        calldata->arg.fmode = 0;
3778        calldata->lr.arg.ld_private = &calldata->lr.ld_private;
3779        calldata->res.fattr = &calldata->fattr;
3780        calldata->res.seqid = calldata->arg.seqid;
3781        calldata->res.server = server;
3782        calldata->res.lr_ret = -NFS4ERR_NOMATCHING_LAYOUT;
3783        calldata->lr.roc = pnfs_roc(state->inode,
3784                        &calldata->lr.arg, &calldata->lr.res, msg.rpc_cred);
3785        if (calldata->lr.roc) {
3786                calldata->arg.lr_args = &calldata->lr.arg;
3787                calldata->res.lr_res = &calldata->lr.res;
3788        }
3789        nfs_sb_active(calldata->inode->i_sb);
3790
3791        msg.rpc_argp = &calldata->arg;
3792        msg.rpc_resp = &calldata->res;
3793        task_setup_data.callback_data = calldata;
3794        task = rpc_run_task(&task_setup_data);
3795        if (IS_ERR(task))
3796                return PTR_ERR(task);
3797        status = 0;
3798        if (wait)
3799                status = rpc_wait_for_completion_task(task);
3800        rpc_put_task(task);
3801        return status;
3802out_free_calldata:
3803        kfree(calldata);
3804out:
3805        nfs4_put_open_state(state);
3806        nfs4_put_state_owner(sp);
3807        return status;
3808}
3809
3810static struct inode *
3811nfs4_atomic_open(struct inode *dir, struct nfs_open_context *ctx,
3812                int open_flags, struct iattr *attr, int *opened)
3813{
3814        struct nfs4_state *state;
3815        struct nfs4_label l = {0, 0, 0, NULL}, *label = NULL;
3816
3817        label = nfs4_label_init_security(dir, ctx->dentry, attr, &l);
3818
3819        /* Protect against concurrent sillydeletes */
3820        state = nfs4_do_open(dir, ctx, open_flags, attr, label, opened);
3821
3822        nfs4_label_release_security(label);
3823
3824        if (IS_ERR(state))
3825                return ERR_CAST(state);
3826        return state->inode;
3827}
3828
3829static void nfs4_close_context(struct nfs_open_context *ctx, int is_sync)
3830{
3831        if (ctx->state == NULL)
3832                return;
3833        if (is_sync)
3834                nfs4_close_sync(ctx->state, _nfs4_ctx_to_openmode(ctx));
3835        else
3836                nfs4_close_state(ctx->state, _nfs4_ctx_to_openmode(ctx));
3837}
3838
3839#define FATTR4_WORD1_NFS40_MASK (2*FATTR4_WORD1_MOUNTED_ON_FILEID - 1UL)
3840#define FATTR4_WORD2_NFS41_MASK (2*FATTR4_WORD2_SUPPATTR_EXCLCREAT - 1UL)
3841#define FATTR4_WORD2_NFS42_MASK (2*FATTR4_WORD2_XATTR_SUPPORT - 1UL)
3842
3843static int _nfs4_server_capabilities(struct nfs_server *server, struct nfs_fh *fhandle)
3844{
3845        u32 bitmask[3] = {}, minorversion = server->nfs_client->cl_minorversion;
3846        struct nfs4_server_caps_arg args = {
3847                .fhandle = fhandle,
3848                .bitmask = bitmask,
3849        };
3850        struct nfs4_server_caps_res res = {};
3851        struct rpc_message msg = {
3852                .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_SERVER_CAPS],
3853                .rpc_argp = &args,
3854                .rpc_resp = &res,
3855        };
3856        int status;
3857        int i;
3858
3859        bitmask[0] = FATTR4_WORD0_SUPPORTED_ATTRS |
3860                     FATTR4_WORD0_FH_EXPIRE_TYPE |
3861                     FATTR4_WORD0_LINK_SUPPORT |
3862                     FATTR4_WORD0_SYMLINK_SUPPORT |
3863                     FATTR4_WORD0_ACLSUPPORT;
3864        if (minorversion)
3865                bitmask[2] = FATTR4_WORD2_SUPPATTR_EXCLCREAT;
3866
3867        status = nfs4_call_sync(server->client, server, &msg, &args.seq_args, &res.seq_res, 0);
3868        if (status == 0) {
3869                /* Sanity check the server answers */
3870                switch (minorversion) {
3871                case 0:
3872                        res.attr_bitmask[1] &= FATTR4_WORD1_NFS40_MASK;
3873                        res.attr_bitmask[2] = 0;
3874                        break;
3875                case 1:
3876                        res.attr_bitmask[2] &= FATTR4_WORD2_NFS41_MASK;
3877                        break;
3878                case 2:
3879                        res.attr_bitmask[2] &= FATTR4_WORD2_NFS42_MASK;
3880                }
3881                memcpy(server->attr_bitmask, res.attr_bitmask, sizeof(server->attr_bitmask));
3882                server->caps &= ~(NFS_CAP_ACLS | NFS_CAP_HARDLINKS |
3883                                  NFS_CAP_SYMLINKS| NFS_CAP_SECURITY_LABEL);
3884                server->fattr_valid = NFS_ATTR_FATTR_V4;
3885                if (res.attr_bitmask[0] & FATTR4_WORD0_ACL &&
3886                                res.acl_bitmask & ACL4_SUPPORT_ALLOW_ACL)
3887                        server->caps |= NFS_CAP_ACLS;
3888                if (res.has_links != 0)
3889                        server->caps |= NFS_CAP_HARDLINKS;
3890                if (res.has_symlinks != 0)
3891                        server->caps |= NFS_CAP_SYMLINKS;
3892#ifdef CONFIG_NFS_V4_SECURITY_LABEL
3893                if (res.attr_bitmask[2] & FATTR4_WORD2_SECURITY_LABEL)
3894                        server->caps |= NFS_CAP_SECURITY_LABEL;
3895#endif
3896                if (!(res.attr_bitmask[0] & FATTR4_WORD0_FILEID))
3897                        server->fattr_valid &= ~NFS_ATTR_FATTR_FILEID;
3898                if (!(res.attr_bitmask[1] & FATTR4_WORD1_MODE))
3899                        server->fattr_valid &= ~NFS_ATTR_FATTR_MODE;
3900                if (!(res.attr_bitmask[1] & FATTR4_WORD1_NUMLINKS))
3901                        server->fattr_valid &= ~NFS_ATTR_FATTR_NLINK;
3902                if (!(res.attr_bitmask[1] & FATTR4_WORD1_OWNER))
3903                        server->fattr_valid &= ~(NFS_ATTR_FATTR_OWNER |
3904                                NFS_ATTR_FATTR_OWNER_NAME);
3905                if (!(res.attr_bitmask[1] & FATTR4_WORD1_OWNER_GROUP))
3906                        server->fattr_valid &= ~(NFS_ATTR_FATTR_GROUP |
3907                                NFS_ATTR_FATTR_GROUP_NAME);
3908                if (!(res.attr_bitmask[1] & FATTR4_WORD1_SPACE_USED))
3909                        server->fattr_valid &= ~NFS_ATTR_FATTR_SPACE_USED;
3910                if (!(res.attr_bitmask[1] & FATTR4_WORD1_TIME_ACCESS))
3911                        server->fattr_valid &= ~NFS_ATTR_FATTR_ATIME;
3912                if (!(res.attr_bitmask[1] & FATTR4_WORD1_TIME_METADATA))
3913                        server->fattr_valid &= ~NFS_ATTR_FATTR_CTIME;
3914                if (!(res.attr_bitmask[1] & FATTR4_WORD1_TIME_MODIFY))
3915                        server->fattr_valid &= ~NFS_ATTR_FATTR_MTIME;
3916                memcpy(server->attr_bitmask_nl, res.attr_bitmask,
3917                                sizeof(server->attr_bitmask));
3918                server->attr_bitmask_nl[2] &= ~FATTR4_WORD2_SECURITY_LABEL;
3919
3920                memcpy(server->cache_consistency_bitmask, res.attr_bitmask, sizeof(server->cache_consistency_bitmask));
3921                server->cache_consistency_bitmask[0] &= FATTR4_WORD0_CHANGE|FATTR4_WORD0_SIZE;
3922                server->cache_consistency_bitmask[1] &= FATTR4_WORD1_TIME_METADATA|FATTR4_WORD1_TIME_MODIFY;
3923                server->cache_consistency_bitmask[2] = 0;
3924
3925                /* Avoid a regression due to buggy server */
3926                for (i = 0; i < ARRAY_SIZE(res.exclcreat_bitmask); i++)
3927                        res.exclcreat_bitmask[i] &= res.attr_bitmask[i];
3928                memcpy(server->exclcreat_bitmask, res.exclcreat_bitmask,
3929                        sizeof(server->exclcreat_bitmask));
3930
3931                server->acl_bitmask = res.acl_bitmask;
3932                server->fh_expire_type = res.fh_expire_type;
3933        }
3934
3935        return status;
3936}
3937
3938int nfs4_server_capabilities(struct nfs_server *server, struct nfs_fh *fhandle)
3939{
3940        struct nfs4_exception exception = {
3941                .interruptible = true,
3942        };
3943        int err;
3944        do {
3945                err = nfs4_handle_exception(server,
3946                                _nfs4_server_capabilities(server, fhandle),
3947                                &exception);
3948        } while (exception.retry);
3949        return err;
3950}
3951
3952static int _nfs4_lookup_root(struct nfs_server *server, struct nfs_fh *fhandle,
3953                struct nfs_fsinfo *info)
3954{
3955        u32 bitmask[3];
3956        struct nfs4_lookup_root_arg args = {
3957                .bitmask = bitmask,
3958        };
3959        struct nfs4_lookup_res res = {
3960                .server = server,
3961                .fattr = info->fattr,
3962                .fh = fhandle,
3963        };
3964        struct rpc_message msg = {
3965                .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_LOOKUP_ROOT],
3966                .rpc_argp = &args,
3967                .rpc_resp = &res,
3968        };
3969
3970        bitmask[0] = nfs4_fattr_bitmap[0];
3971        bitmask[1] = nfs4_fattr_bitmap[1];
3972        /*
3973         * Process the label in the upcoming getfattr
3974         */
3975        bitmask[2] = nfs4_fattr_bitmap[2] & ~FATTR4_WORD2_SECURITY_LABEL;
3976
3977        nfs_fattr_init(info->fattr);
3978        return nfs4_call_sync(server->client, server, &msg, &args.seq_args, &res.seq_res, 0);
3979}
3980
3981static int nfs4_lookup_root(struct nfs_server *server, struct nfs_fh *fhandle,
3982                struct nfs_fsinfo *info)
3983{
3984        struct nfs4_exception exception = {
3985                .interruptible = true,
3986        };
3987        int err;
3988        do {
3989                err = _nfs4_lookup_root(server, fhandle, info);
3990                trace_nfs4_lookup_root(server, fhandle, info->fattr, err);
3991                switch (err) {
3992                case 0:
3993                case -NFS4ERR_WRONGSEC:
3994                        goto out;
3995                default:
3996                        err = nfs4_handle_exception(server, err, &exception);
3997                }
3998        } while (exception.retry);
3999out:
4000        return err;
4001}
4002
4003static int nfs4_lookup_root_sec(struct nfs_server *server, struct nfs_fh *fhandle,
4004                                struct nfs_fsinfo *info, rpc_authflavor_t flavor)
4005{
4006        struct rpc_auth_create_args auth_args = {
4007                .pseudoflavor = flavor,
4008        };
4009        struct rpc_auth *auth;
4010
4011        auth = rpcauth_create(&auth_args, server->client);
4012        if (IS_ERR(auth))
4013                return -EACCES;
4014        return nfs4_lookup_root(server, fhandle, info);
4015}
4016
4017/*
4018 * Retry pseudoroot lookup with various security flavors.  We do this when:
4019 *
4020 *   NFSv4.0: the PUTROOTFH operation returns NFS4ERR_WRONGSEC
4021 *   NFSv4.1: the server does not support the SECINFO_NO_NAME operation
4022 *
4023 * Returns zero on success, or a negative NFS4ERR value, or a
4024 * negative errno value.
4025 */
4026static int nfs4_find_root_sec(struct nfs_server *server, struct nfs_fh *fhandle,
4027                              struct nfs_fsinfo *info)
4028{
4029        /* Per 3530bis 15.33.5 */
4030        static const rpc_authflavor_t flav_array[] = {
4031                RPC_AUTH_GSS_KRB5P,
4032                RPC_AUTH_GSS_KRB5I,
4033                RPC_AUTH_GSS_KRB5,
4034                RPC_AUTH_UNIX,                  /* courtesy */
4035                RPC_AUTH_NULL,
4036        };
4037        int status = -EPERM;
4038        size_t i;
4039
4040        if (server->auth_info.flavor_len > 0) {
4041                /* try each flavor specified by user */
4042                for (i = 0; i < server->auth_info.flavor_len; i++) {
4043                        status = nfs4_lookup_root_sec(server, fhandle, info,
4044                                                server->auth_info.flavors[i]);
4045                        if (status == -NFS4ERR_WRONGSEC || status == -EACCES)
4046                                continue;
4047                        break;
4048                }
4049        } else {
4050                /* no flavors specified by user, try default list */
4051                for (i = 0; i < ARRAY_SIZE(flav_array); i++) {
4052                        status = nfs4_lookup_root_sec(server, fhandle, info,
4053                                                      flav_array[i]);
4054                        if (status == -NFS4ERR_WRONGSEC || status == -EACCES)
4055                                continue;
4056                        break;
4057                }
4058        }
4059
4060        /*
4061         * -EACCES could mean that the user doesn't have correct permissions
4062         * to access the mount.  It could also mean that we tried to mount
4063         * with a gss auth flavor, but rpc.gssd isn't running.  Either way,
4064         * existing mount programs don't handle -EACCES very well so it should
4065         * be mapped to -EPERM instead.
4066         */
4067        if (status == -EACCES)
4068                status = -EPERM;
4069        return status;
4070}
4071
4072/**
4073 * nfs4_proc_get_rootfh - get file handle for server's pseudoroot
4074 * @server: initialized nfs_server handle
4075 * @fhandle: we fill in the pseudo-fs root file handle
4076 * @info: we fill in an FSINFO struct
4077 * @auth_probe: probe the auth flavours
4078 *
4079 * Returns zero on success, or a negative errno.
4080 */
4081int nfs4_proc_get_rootfh(struct nfs_server *server, struct nfs_fh *fhandle,
4082                         struct nfs_fsinfo *info,
4083                         bool auth_probe)
4084{
4085        int status = 0;
4086
4087        if (!auth_probe)
4088                status = nfs4_lookup_root(server, fhandle, info);
4089
4090        if (auth_probe || status == NFS4ERR_WRONGSEC)
4091                status = server->nfs_client->cl_mvops->find_root_sec(server,
4092                                fhandle, info);
4093
4094        if (status == 0)
4095                status = nfs4_server_capabilities(server, fhandle);
4096        if (status == 0)
4097                status = nfs4_do_fsinfo(server, fhandle, info);
4098
4099        return nfs4_map_errors(status);
4100}
4101
4102static int nfs4_proc_get_root(struct nfs_server *server, struct nfs_fh *mntfh,
4103                              struct nfs_fsinfo *info)
4104{
4105        int error;
4106        struct nfs_fattr *fattr = info->fattr;
4107        struct nfs4_label *label = fattr->label;
4108
4109        error = nfs4_server_capabilities(server, mntfh);
4110        if (error < 0) {
4111                dprintk("nfs4_get_root: getcaps error = %d\n", -error);
4112                return error;
4113        }
4114
4115        error = nfs4_proc_getattr(server, mntfh, fattr, label, NULL);
4116        if (error < 0) {
4117                dprintk("nfs4_get_root: getattr error = %d\n", -error);
4118                goto out;
4119        }
4120
4121        if (fattr->valid & NFS_ATTR_FATTR_FSID &&
4122            !nfs_fsid_equal(&server->fsid, &fattr->fsid))
4123                memcpy(&server->fsid, &fattr->fsid, sizeof(server->fsid));
4124
4125out:
4126        return error;
4127}
4128
4129/*
4130 * Get locations and (maybe) other attributes of a referral.
4131 * Note that we'll actually follow the referral later when
4132 * we detect fsid mismatch in inode revalidation
4133 */
4134static int nfs4_get_referral(struct rpc_clnt *client, struct inode *dir,
4135                             const struct qstr *name, struct nfs_fattr *fattr,
4136                             struct nfs_fh *fhandle)
4137{
4138        int status = -ENOMEM;
4139        struct page *page = NULL;
4140        struct nfs4_fs_locations *locations = NULL;
4141
4142        page = alloc_page(GFP_KERNEL);
4143        if (page == NULL)
4144                goto out;
4145        locations = kmalloc(sizeof(struct nfs4_fs_locations), GFP_KERNEL);
4146        if (locations == NULL)
4147                goto out;
4148
4149        status = nfs4_proc_fs_locations(client, dir, name, locations, page);
4150        if (status != 0)
4151                goto out;
4152
4153        /*
4154         * If the fsid didn't change, this is a migration event, not a
4155         * referral.  Cause us to drop into the exception handler, which
4156         * will kick off migration recovery.
4157         */
4158        if (nfs_fsid_equal(&NFS_SERVER(dir)->fsid, &locations->fattr.fsid)) {
4159                dprintk("%s: server did not return a different fsid for"
4160                        " a referral at %s\n", __func__, name->name);
4161                status = -NFS4ERR_MOVED;
4162                goto out;
4163        }
4164        /* Fixup attributes for the nfs_lookup() call to nfs_fhget() */
4165        nfs_fixup_referral_attributes(&locations->fattr);
4166
4167        /* replace the lookup nfs_fattr with the locations nfs_fattr */
4168        memcpy(fattr, &locations->fattr, sizeof(struct nfs_fattr));
4169        memset(fhandle, 0, sizeof(struct nfs_fh));
4170out:
4171        if (page)
4172                __free_page(page);
4173        kfree(locations);
4174        return status;
4175}
4176
4177static int _nfs4_proc_getattr(struct nfs_server *server, struct nfs_fh *fhandle,
4178                                struct nfs_fattr *fattr, struct nfs4_label *label,
4179                                struct inode *inode)
4180{
4181        __u32 bitmask[NFS4_BITMASK_SZ];
4182        struct nfs4_getattr_arg args = {
4183                .fh = fhandle,
4184                .bitmask = bitmask,
4185        };
4186        struct nfs4_getattr_res res = {
4187                .fattr = fattr,
4188                .label = label,
4189                .server = server,
4190        };
4191        struct rpc_message msg = {
4192                .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_GETATTR],
4193                .rpc_argp = &args,
4194                .rpc_resp = &res,
4195        };
4196        unsigned short task_flags = 0;
4197
4198        if (nfs4_has_session(server->nfs_client))
4199                task_flags = RPC_TASK_MOVEABLE;
4200
4201        /* Is this is an attribute revalidation, subject to softreval? */
4202        if (inode && (server->flags & NFS_MOUNT_SOFTREVAL))
4203                task_flags |= RPC_TASK_TIMEOUT;
4204
4205        nfs4_bitmap_copy_adjust(bitmask, nfs4_bitmask(server, label), inode, 0);
4206        nfs_fattr_init(fattr);
4207        nfs4_init_sequence(&args.seq_args, &res.seq_res, 0, 0);
4208        return nfs4_do_call_sync(server->client, server, &msg,
4209                        &args.seq_args, &res.seq_res, task_flags);
4210}
4211
4212int nfs4_proc_getattr(struct nfs_server *server, struct nfs_fh *fhandle,
4213                                struct nfs_fattr *fattr, struct nfs4_label *label,
4214                                struct