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/nfs_idmap.h>
  55#include <linux/sunrpc/bc_xprt.h>
  56#include <linux/xattr.h>
  57#include <linux/utsname.h>
  58#include <linux/freezer.h>
  59
  60#include "nfs4_fs.h"
  61#include "delegation.h"
  62#include "internal.h"
  63#include "iostat.h"
  64#include "callback.h"
  65#include "pnfs.h"
  66#include "netns.h"
  67
  68#define NFSDBG_FACILITY         NFSDBG_PROC
  69
  70#define NFS4_POLL_RETRY_MIN     (HZ/10)
  71#define NFS4_POLL_RETRY_MAX     (15*HZ)
  72
  73#define NFS4_MAX_LOOP_ON_RECOVER (10)
  74
  75struct nfs4_opendata;
  76static int _nfs4_proc_open(struct nfs4_opendata *data);
  77static int _nfs4_recover_proc_open(struct nfs4_opendata *data);
  78static int nfs4_do_fsinfo(struct nfs_server *, struct nfs_fh *, struct nfs_fsinfo *);
  79static int nfs4_async_handle_error(struct rpc_task *, const struct nfs_server *, struct nfs4_state *);
  80static void nfs_fixup_referral_attributes(struct nfs_fattr *fattr);
  81static int nfs4_proc_getattr(struct nfs_server *, struct nfs_fh *, struct nfs_fattr *);
  82static int _nfs4_proc_getattr(struct nfs_server *server, struct nfs_fh *fhandle, struct nfs_fattr *fattr);
  83static int nfs4_do_setattr(struct inode *inode, struct rpc_cred *cred,
  84                            struct nfs_fattr *fattr, struct iattr *sattr,
  85                            struct nfs4_state *state);
  86#ifdef CONFIG_NFS_V4_1
  87static int nfs41_test_stateid(struct nfs_server *, nfs4_stateid *);
  88static int nfs41_free_stateid(struct nfs_server *, nfs4_stateid *);
  89#endif
  90/* Prevent leaks of NFSv4 errors into userland */
  91static int nfs4_map_errors(int err)
  92{
  93        if (err >= -1000)
  94                return err;
  95        switch (err) {
  96        case -NFS4ERR_RESOURCE:
  97                return -EREMOTEIO;
  98        case -NFS4ERR_WRONGSEC:
  99                return -EPERM;
 100        case -NFS4ERR_BADOWNER:
 101        case -NFS4ERR_BADNAME:
 102                return -EINVAL;
 103        case -NFS4ERR_SHARE_DENIED:
 104                return -EACCES;
 105        case -NFS4ERR_MINOR_VERS_MISMATCH:
 106                return -EPROTONOSUPPORT;
 107        case -NFS4ERR_ACCESS:
 108                return -EACCES;
 109        default:
 110                dprintk("%s could not handle NFSv4 error %d\n",
 111                                __func__, -err);
 112                break;
 113        }
 114        return -EIO;
 115}
 116
 117/*
 118 * This is our standard bitmap for GETATTR requests.
 119 */
 120const u32 nfs4_fattr_bitmap[3] = {
 121        FATTR4_WORD0_TYPE
 122        | FATTR4_WORD0_CHANGE
 123        | FATTR4_WORD0_SIZE
 124        | FATTR4_WORD0_FSID
 125        | FATTR4_WORD0_FILEID,
 126        FATTR4_WORD1_MODE
 127        | FATTR4_WORD1_NUMLINKS
 128        | FATTR4_WORD1_OWNER
 129        | FATTR4_WORD1_OWNER_GROUP
 130        | FATTR4_WORD1_RAWDEV
 131        | FATTR4_WORD1_SPACE_USED
 132        | FATTR4_WORD1_TIME_ACCESS
 133        | FATTR4_WORD1_TIME_METADATA
 134        | FATTR4_WORD1_TIME_MODIFY
 135};
 136
 137static const u32 nfs4_pnfs_open_bitmap[3] = {
 138        FATTR4_WORD0_TYPE
 139        | FATTR4_WORD0_CHANGE
 140        | FATTR4_WORD0_SIZE
 141        | FATTR4_WORD0_FSID
 142        | FATTR4_WORD0_FILEID,
 143        FATTR4_WORD1_MODE
 144        | FATTR4_WORD1_NUMLINKS
 145        | FATTR4_WORD1_OWNER
 146        | FATTR4_WORD1_OWNER_GROUP
 147        | FATTR4_WORD1_RAWDEV
 148        | FATTR4_WORD1_SPACE_USED
 149        | FATTR4_WORD1_TIME_ACCESS
 150        | FATTR4_WORD1_TIME_METADATA
 151        | FATTR4_WORD1_TIME_MODIFY,
 152        FATTR4_WORD2_MDSTHRESHOLD
 153};
 154
 155static const u32 nfs4_open_noattr_bitmap[3] = {
 156        FATTR4_WORD0_TYPE
 157        | FATTR4_WORD0_CHANGE
 158        | FATTR4_WORD0_FILEID,
 159};
 160
 161const u32 nfs4_statfs_bitmap[2] = {
 162        FATTR4_WORD0_FILES_AVAIL
 163        | FATTR4_WORD0_FILES_FREE
 164        | FATTR4_WORD0_FILES_TOTAL,
 165        FATTR4_WORD1_SPACE_AVAIL
 166        | FATTR4_WORD1_SPACE_FREE
 167        | FATTR4_WORD1_SPACE_TOTAL
 168};
 169
 170const u32 nfs4_pathconf_bitmap[2] = {
 171        FATTR4_WORD0_MAXLINK
 172        | FATTR4_WORD0_MAXNAME,
 173        0
 174};
 175
 176const u32 nfs4_fsinfo_bitmap[3] = { FATTR4_WORD0_MAXFILESIZE
 177                        | FATTR4_WORD0_MAXREAD
 178                        | FATTR4_WORD0_MAXWRITE
 179                        | FATTR4_WORD0_LEASE_TIME,
 180                        FATTR4_WORD1_TIME_DELTA
 181                        | FATTR4_WORD1_FS_LAYOUT_TYPES,
 182                        FATTR4_WORD2_LAYOUT_BLKSIZE
 183};
 184
 185const u32 nfs4_fs_locations_bitmap[2] = {
 186        FATTR4_WORD0_TYPE
 187        | FATTR4_WORD0_CHANGE
 188        | FATTR4_WORD0_SIZE
 189        | FATTR4_WORD0_FSID
 190        | FATTR4_WORD0_FILEID
 191        | FATTR4_WORD0_FS_LOCATIONS,
 192        FATTR4_WORD1_MODE
 193        | FATTR4_WORD1_NUMLINKS
 194        | FATTR4_WORD1_OWNER
 195        | FATTR4_WORD1_OWNER_GROUP
 196        | FATTR4_WORD1_RAWDEV
 197        | FATTR4_WORD1_SPACE_USED
 198        | FATTR4_WORD1_TIME_ACCESS
 199        | FATTR4_WORD1_TIME_METADATA
 200        | FATTR4_WORD1_TIME_MODIFY
 201        | FATTR4_WORD1_MOUNTED_ON_FILEID
 202};
 203
 204static void nfs4_setup_readdir(u64 cookie, __be32 *verifier, struct dentry *dentry,
 205                struct nfs4_readdir_arg *readdir)
 206{
 207        __be32 *start, *p;
 208
 209        BUG_ON(readdir->count < 80);
 210        if (cookie > 2) {
 211                readdir->cookie = cookie;
 212                memcpy(&readdir->verifier, verifier, sizeof(readdir->verifier));
 213                return;
 214        }
 215
 216        readdir->cookie = 0;
 217        memset(&readdir->verifier, 0, sizeof(readdir->verifier));
 218        if (cookie == 2)
 219                return;
 220        
 221        /*
 222         * NFSv4 servers do not return entries for '.' and '..'
 223         * Therefore, we fake these entries here.  We let '.'
 224         * have cookie 0 and '..' have cookie 1.  Note that
 225         * when talking to the server, we always send cookie 0
 226         * instead of 1 or 2.
 227         */
 228        start = p = kmap_atomic(*readdir->pages);
 229        
 230        if (cookie == 0) {
 231                *p++ = xdr_one;                                  /* next */
 232                *p++ = xdr_zero;                   /* cookie, first word */
 233                *p++ = xdr_one;                   /* cookie, second word */
 234                *p++ = xdr_one;                             /* entry len */
 235                memcpy(p, ".\0\0\0", 4);                        /* entry */
 236                p++;
 237                *p++ = xdr_one;                         /* bitmap length */
 238                *p++ = htonl(FATTR4_WORD0_FILEID);             /* bitmap */
 239                *p++ = htonl(8);              /* attribute buffer length */
 240                p = xdr_encode_hyper(p, NFS_FILEID(dentry->d_inode));
 241        }
 242        
 243        *p++ = xdr_one;                                  /* next */
 244        *p++ = xdr_zero;                   /* cookie, first word */
 245        *p++ = xdr_two;                   /* cookie, second word */
 246        *p++ = xdr_two;                             /* entry len */
 247        memcpy(p, "..\0\0", 4);                         /* entry */
 248        p++;
 249        *p++ = xdr_one;                         /* bitmap length */
 250        *p++ = htonl(FATTR4_WORD0_FILEID);             /* bitmap */
 251        *p++ = htonl(8);              /* attribute buffer length */
 252        p = xdr_encode_hyper(p, NFS_FILEID(dentry->d_parent->d_inode));
 253
 254        readdir->pgbase = (char *)p - (char *)start;
 255        readdir->count -= readdir->pgbase;
 256        kunmap_atomic(start);
 257}
 258
 259static int nfs4_wait_clnt_recover(struct nfs_client *clp)
 260{
 261        int res;
 262
 263        might_sleep();
 264
 265        res = wait_on_bit(&clp->cl_state, NFS4CLNT_MANAGER_RUNNING,
 266                        nfs_wait_bit_killable, TASK_KILLABLE);
 267        if (res)
 268                return res;
 269
 270        if (clp->cl_cons_state < 0)
 271                return clp->cl_cons_state;
 272        return 0;
 273}
 274
 275static int nfs4_delay(struct rpc_clnt *clnt, long *timeout)
 276{
 277        int res = 0;
 278
 279        might_sleep();
 280
 281        if (*timeout <= 0)
 282                *timeout = NFS4_POLL_RETRY_MIN;
 283        if (*timeout > NFS4_POLL_RETRY_MAX)
 284                *timeout = NFS4_POLL_RETRY_MAX;
 285        freezable_schedule_timeout_killable(*timeout);
 286        if (fatal_signal_pending(current))
 287                res = -ERESTARTSYS;
 288        *timeout <<= 1;
 289        return res;
 290}
 291
 292/* This is the error handling routine for processes that are allowed
 293 * to sleep.
 294 */
 295static int nfs4_handle_exception(struct nfs_server *server, int errorcode, struct nfs4_exception *exception)
 296{
 297        struct nfs_client *clp = server->nfs_client;
 298        struct nfs4_state *state = exception->state;
 299        struct inode *inode = exception->inode;
 300        int ret = errorcode;
 301
 302        exception->retry = 0;
 303        switch(errorcode) {
 304                case 0:
 305                        return 0;
 306                case -NFS4ERR_OPENMODE:
 307                        if (inode && nfs4_have_delegation(inode, FMODE_READ)) {
 308                                nfs4_inode_return_delegation(inode);
 309                                exception->retry = 1;
 310                                return 0;
 311                        }
 312                        if (state == NULL)
 313                                break;
 314                        nfs4_schedule_stateid_recovery(server, state);
 315                        goto wait_on_recovery;
 316                case -NFS4ERR_DELEG_REVOKED:
 317                case -NFS4ERR_ADMIN_REVOKED:
 318                case -NFS4ERR_BAD_STATEID:
 319                        if (state == NULL)
 320                                break;
 321                        nfs_remove_bad_delegation(state->inode);
 322                        nfs4_schedule_stateid_recovery(server, state);
 323                        goto wait_on_recovery;
 324                case -NFS4ERR_EXPIRED:
 325                        if (state != NULL)
 326                                nfs4_schedule_stateid_recovery(server, state);
 327                case -NFS4ERR_STALE_STATEID:
 328                case -NFS4ERR_STALE_CLIENTID:
 329                        nfs4_schedule_lease_recovery(clp);
 330                        goto wait_on_recovery;
 331#if defined(CONFIG_NFS_V4_1)
 332                case -NFS4ERR_BADSESSION:
 333                case -NFS4ERR_BADSLOT:
 334                case -NFS4ERR_BAD_HIGH_SLOT:
 335                case -NFS4ERR_CONN_NOT_BOUND_TO_SESSION:
 336                case -NFS4ERR_DEADSESSION:
 337                case -NFS4ERR_SEQ_FALSE_RETRY:
 338                case -NFS4ERR_SEQ_MISORDERED:
 339                        dprintk("%s ERROR: %d Reset session\n", __func__,
 340                                errorcode);
 341                        nfs4_schedule_session_recovery(clp->cl_session, errorcode);
 342                        goto wait_on_recovery;
 343#endif /* defined(CONFIG_NFS_V4_1) */
 344                case -NFS4ERR_FILE_OPEN:
 345                        if (exception->timeout > HZ) {
 346                                /* We have retried a decent amount, time to
 347                                 * fail
 348                                 */
 349                                ret = -EBUSY;
 350                                break;
 351                        }
 352                case -NFS4ERR_GRACE:
 353                case -NFS4ERR_DELAY:
 354                case -EKEYEXPIRED:
 355                        ret = nfs4_delay(server->client, &exception->timeout);
 356                        if (ret != 0)
 357                                break;
 358                case -NFS4ERR_RETRY_UNCACHED_REP:
 359                case -NFS4ERR_OLD_STATEID:
 360                        exception->retry = 1;
 361                        break;
 362                case -NFS4ERR_BADOWNER:
 363                        /* The following works around a Linux server bug! */
 364                case -NFS4ERR_BADNAME:
 365                        if (server->caps & NFS_CAP_UIDGID_NOMAP) {
 366                                server->caps &= ~NFS_CAP_UIDGID_NOMAP;
 367                                exception->retry = 1;
 368                                printk(KERN_WARNING "NFS: v4 server %s "
 369                                                "does not accept raw "
 370                                                "uid/gids. "
 371                                                "Reenabling the idmapper.\n",
 372                                                server->nfs_client->cl_hostname);
 373                        }
 374        }
 375        /* We failed to handle the error */
 376        return nfs4_map_errors(ret);
 377wait_on_recovery:
 378        ret = nfs4_wait_clnt_recover(clp);
 379        if (ret == 0)
 380                exception->retry = 1;
 381        return ret;
 382}
 383
 384
 385static void do_renew_lease(struct nfs_client *clp, unsigned long timestamp)
 386{
 387        spin_lock(&clp->cl_lock);
 388        if (time_before(clp->cl_last_renewal,timestamp))
 389                clp->cl_last_renewal = timestamp;
 390        spin_unlock(&clp->cl_lock);
 391}
 392
 393static void renew_lease(const struct nfs_server *server, unsigned long timestamp)
 394{
 395        do_renew_lease(server->nfs_client, timestamp);
 396}
 397
 398#if defined(CONFIG_NFS_V4_1)
 399
 400/*
 401 * nfs4_free_slot - free a slot and efficiently update slot table.
 402 *
 403 * freeing a slot is trivially done by clearing its respective bit
 404 * in the bitmap.
 405 * If the freed slotid equals highest_used_slotid we want to update it
 406 * so that the server would be able to size down the slot table if needed,
 407 * otherwise we know that the highest_used_slotid is still in use.
 408 * When updating highest_used_slotid there may be "holes" in the bitmap
 409 * so we need to scan down from highest_used_slotid to 0 looking for the now
 410 * highest slotid in use.
 411 * If none found, highest_used_slotid is set to NFS4_NO_SLOT.
 412 *
 413 * Must be called while holding tbl->slot_tbl_lock
 414 */
 415static void
 416nfs4_free_slot(struct nfs4_slot_table *tbl, u32 slotid)
 417{
 418        BUG_ON(slotid >= NFS4_MAX_SLOT_TABLE);
 419        /* clear used bit in bitmap */
 420        __clear_bit(slotid, tbl->used_slots);
 421
 422        /* update highest_used_slotid when it is freed */
 423        if (slotid == tbl->highest_used_slotid) {
 424                slotid = find_last_bit(tbl->used_slots, tbl->max_slots);
 425                if (slotid < tbl->max_slots)
 426                        tbl->highest_used_slotid = slotid;
 427                else
 428                        tbl->highest_used_slotid = NFS4_NO_SLOT;
 429        }
 430        dprintk("%s: slotid %u highest_used_slotid %d\n", __func__,
 431                slotid, tbl->highest_used_slotid);
 432}
 433
 434bool nfs4_set_task_privileged(struct rpc_task *task, void *dummy)
 435{
 436        rpc_task_set_priority(task, RPC_PRIORITY_PRIVILEGED);
 437        return true;
 438}
 439
 440/*
 441 * Signal state manager thread if session fore channel is drained
 442 */
 443static void nfs4_check_drain_fc_complete(struct nfs4_session *ses)
 444{
 445        if (!test_bit(NFS4_SESSION_DRAINING, &ses->session_state)) {
 446                rpc_wake_up_first(&ses->fc_slot_table.slot_tbl_waitq,
 447                                nfs4_set_task_privileged, NULL);
 448                return;
 449        }
 450
 451        if (ses->fc_slot_table.highest_used_slotid != NFS4_NO_SLOT)
 452                return;
 453
 454        dprintk("%s COMPLETE: Session Fore Channel Drained\n", __func__);
 455        complete(&ses->fc_slot_table.complete);
 456}
 457
 458/*
 459 * Signal state manager thread if session back channel is drained
 460 */
 461void nfs4_check_drain_bc_complete(struct nfs4_session *ses)
 462{
 463        if (!test_bit(NFS4_SESSION_DRAINING, &ses->session_state) ||
 464            ses->bc_slot_table.highest_used_slotid != NFS4_NO_SLOT)
 465                return;
 466        dprintk("%s COMPLETE: Session Back Channel Drained\n", __func__);
 467        complete(&ses->bc_slot_table.complete);
 468}
 469
 470static void nfs41_sequence_free_slot(struct nfs4_sequence_res *res)
 471{
 472        struct nfs4_slot_table *tbl;
 473
 474        tbl = &res->sr_session->fc_slot_table;
 475        if (!res->sr_slot) {
 476                /* just wake up the next guy waiting since
 477                 * we may have not consumed a slot after all */
 478                dprintk("%s: No slot\n", __func__);
 479                return;
 480        }
 481
 482        spin_lock(&tbl->slot_tbl_lock);
 483        nfs4_free_slot(tbl, res->sr_slot - tbl->slots);
 484        nfs4_check_drain_fc_complete(res->sr_session);
 485        spin_unlock(&tbl->slot_tbl_lock);
 486        res->sr_slot = NULL;
 487}
 488
 489static int nfs41_sequence_done(struct rpc_task *task, struct nfs4_sequence_res *res)
 490{
 491        unsigned long timestamp;
 492        struct nfs_client *clp;
 493
 494        /*
 495         * sr_status remains 1 if an RPC level error occurred. The server
 496         * may or may not have processed the sequence operation..
 497         * Proceed as if the server received and processed the sequence
 498         * operation.
 499         */
 500        if (res->sr_status == 1)
 501                res->sr_status = NFS_OK;
 502
 503        /* don't increment the sequence number if the task wasn't sent */
 504        if (!RPC_WAS_SENT(task))
 505                goto out;
 506
 507        /* Check the SEQUENCE operation status */
 508        switch (res->sr_status) {
 509        case 0:
 510                /* Update the slot's sequence and clientid lease timer */
 511                ++res->sr_slot->seq_nr;
 512                timestamp = res->sr_renewal_time;
 513                clp = res->sr_session->clp;
 514                do_renew_lease(clp, timestamp);
 515                /* Check sequence flags */
 516                if (res->sr_status_flags != 0)
 517                        nfs4_schedule_lease_recovery(clp);
 518                break;
 519        case -NFS4ERR_DELAY:
 520                /* The server detected a resend of the RPC call and
 521                 * returned NFS4ERR_DELAY as per Section 2.10.6.2
 522                 * of RFC5661.
 523                 */
 524                dprintk("%s: slot=%td seq=%d: Operation in progress\n",
 525                        __func__,
 526                        res->sr_slot - res->sr_session->fc_slot_table.slots,
 527                        res->sr_slot->seq_nr);
 528                goto out_retry;
 529        default:
 530                /* Just update the slot sequence no. */
 531                ++res->sr_slot->seq_nr;
 532        }
 533out:
 534        /* The session may be reset by one of the error handlers. */
 535        dprintk("%s: Error %d free the slot \n", __func__, res->sr_status);
 536        nfs41_sequence_free_slot(res);
 537        return 1;
 538out_retry:
 539        if (!rpc_restart_call(task))
 540                goto out;
 541        rpc_delay(task, NFS4_POLL_RETRY_MAX);
 542        return 0;
 543}
 544
 545static int nfs4_sequence_done(struct rpc_task *task,
 546                               struct nfs4_sequence_res *res)
 547{
 548        if (res->sr_session == NULL)
 549                return 1;
 550        return nfs41_sequence_done(task, res);
 551}
 552
 553/*
 554 * nfs4_find_slot - efficiently look for a free slot
 555 *
 556 * nfs4_find_slot looks for an unset bit in the used_slots bitmap.
 557 * If found, we mark the slot as used, update the highest_used_slotid,
 558 * and respectively set up the sequence operation args.
 559 * The slot number is returned if found, or NFS4_NO_SLOT otherwise.
 560 *
 561 * Note: must be called with under the slot_tbl_lock.
 562 */
 563static u32
 564nfs4_find_slot(struct nfs4_slot_table *tbl)
 565{
 566        u32 slotid;
 567        u32 ret_id = NFS4_NO_SLOT;
 568
 569        dprintk("--> %s used_slots=%04lx highest_used=%u max_slots=%u\n",
 570                __func__, tbl->used_slots[0], tbl->highest_used_slotid,
 571                tbl->max_slots);
 572        slotid = find_first_zero_bit(tbl->used_slots, tbl->max_slots);
 573        if (slotid >= tbl->max_slots)
 574                goto out;
 575        __set_bit(slotid, tbl->used_slots);
 576        if (slotid > tbl->highest_used_slotid ||
 577                        tbl->highest_used_slotid == NFS4_NO_SLOT)
 578                tbl->highest_used_slotid = slotid;
 579        ret_id = slotid;
 580out:
 581        dprintk("<-- %s used_slots=%04lx highest_used=%d slotid=%d \n",
 582                __func__, tbl->used_slots[0], tbl->highest_used_slotid, ret_id);
 583        return ret_id;
 584}
 585
 586static void nfs41_init_sequence(struct nfs4_sequence_args *args,
 587                struct nfs4_sequence_res *res, int cache_reply)
 588{
 589        args->sa_session = NULL;
 590        args->sa_cache_this = 0;
 591        if (cache_reply)
 592                args->sa_cache_this = 1;
 593        res->sr_session = NULL;
 594        res->sr_slot = NULL;
 595}
 596
 597int nfs41_setup_sequence(struct nfs4_session *session,
 598                                struct nfs4_sequence_args *args,
 599                                struct nfs4_sequence_res *res,
 600                                struct rpc_task *task)
 601{
 602        struct nfs4_slot *slot;
 603        struct nfs4_slot_table *tbl;
 604        u32 slotid;
 605
 606        dprintk("--> %s\n", __func__);
 607        /* slot already allocated? */
 608        if (res->sr_slot != NULL)
 609                return 0;
 610
 611        tbl = &session->fc_slot_table;
 612
 613        spin_lock(&tbl->slot_tbl_lock);
 614        if (test_bit(NFS4_SESSION_DRAINING, &session->session_state) &&
 615            !rpc_task_has_priority(task, RPC_PRIORITY_PRIVILEGED)) {
 616                /* The state manager will wait until the slot table is empty */
 617                rpc_sleep_on(&tbl->slot_tbl_waitq, task, NULL);
 618                spin_unlock(&tbl->slot_tbl_lock);
 619                dprintk("%s session is draining\n", __func__);
 620                return -EAGAIN;
 621        }
 622
 623        if (!rpc_queue_empty(&tbl->slot_tbl_waitq) &&
 624            !rpc_task_has_priority(task, RPC_PRIORITY_PRIVILEGED)) {
 625                rpc_sleep_on(&tbl->slot_tbl_waitq, task, NULL);
 626                spin_unlock(&tbl->slot_tbl_lock);
 627                dprintk("%s enforce FIFO order\n", __func__);
 628                return -EAGAIN;
 629        }
 630
 631        slotid = nfs4_find_slot(tbl);
 632        if (slotid == NFS4_NO_SLOT) {
 633                rpc_sleep_on(&tbl->slot_tbl_waitq, task, NULL);
 634                spin_unlock(&tbl->slot_tbl_lock);
 635                dprintk("<-- %s: no free slots\n", __func__);
 636                return -EAGAIN;
 637        }
 638        spin_unlock(&tbl->slot_tbl_lock);
 639
 640        rpc_task_set_priority(task, RPC_PRIORITY_NORMAL);
 641        slot = tbl->slots + slotid;
 642        args->sa_session = session;
 643        args->sa_slotid = slotid;
 644
 645        dprintk("<-- %s slotid=%d seqid=%d\n", __func__, slotid, slot->seq_nr);
 646
 647        res->sr_session = session;
 648        res->sr_slot = slot;
 649        res->sr_renewal_time = jiffies;
 650        res->sr_status_flags = 0;
 651        /*
 652         * sr_status is only set in decode_sequence, and so will remain
 653         * set to 1 if an rpc level failure occurs.
 654         */
 655        res->sr_status = 1;
 656        return 0;
 657}
 658EXPORT_SYMBOL_GPL(nfs41_setup_sequence);
 659
 660int nfs4_setup_sequence(const struct nfs_server *server,
 661                        struct nfs4_sequence_args *args,
 662                        struct nfs4_sequence_res *res,
 663                        struct rpc_task *task)
 664{
 665        struct nfs4_session *session = nfs4_get_session(server);
 666        int ret = 0;
 667
 668        if (session == NULL)
 669                goto out;
 670
 671        dprintk("--> %s clp %p session %p sr_slot %td\n",
 672                __func__, session->clp, session, res->sr_slot ?
 673                        res->sr_slot - session->fc_slot_table.slots : -1);
 674
 675        ret = nfs41_setup_sequence(session, args, res, task);
 676out:
 677        dprintk("<-- %s status=%d\n", __func__, ret);
 678        return ret;
 679}
 680
 681struct nfs41_call_sync_data {
 682        const struct nfs_server *seq_server;
 683        struct nfs4_sequence_args *seq_args;
 684        struct nfs4_sequence_res *seq_res;
 685};
 686
 687static void nfs41_call_sync_prepare(struct rpc_task *task, void *calldata)
 688{
 689        struct nfs41_call_sync_data *data = calldata;
 690
 691        dprintk("--> %s data->seq_server %p\n", __func__, data->seq_server);
 692
 693        if (nfs4_setup_sequence(data->seq_server, data->seq_args,
 694                                data->seq_res, task))
 695                return;
 696        rpc_call_start(task);
 697}
 698
 699static void nfs41_call_priv_sync_prepare(struct rpc_task *task, void *calldata)
 700{
 701        rpc_task_set_priority(task, RPC_PRIORITY_PRIVILEGED);
 702        nfs41_call_sync_prepare(task, calldata);
 703}
 704
 705static void nfs41_call_sync_done(struct rpc_task *task, void *calldata)
 706{
 707        struct nfs41_call_sync_data *data = calldata;
 708
 709        nfs41_sequence_done(task, data->seq_res);
 710}
 711
 712static const struct rpc_call_ops nfs41_call_sync_ops = {
 713        .rpc_call_prepare = nfs41_call_sync_prepare,
 714        .rpc_call_done = nfs41_call_sync_done,
 715};
 716
 717static const struct rpc_call_ops nfs41_call_priv_sync_ops = {
 718        .rpc_call_prepare = nfs41_call_priv_sync_prepare,
 719        .rpc_call_done = nfs41_call_sync_done,
 720};
 721
 722static int nfs4_call_sync_sequence(struct rpc_clnt *clnt,
 723                                   struct nfs_server *server,
 724                                   struct rpc_message *msg,
 725                                   struct nfs4_sequence_args *args,
 726                                   struct nfs4_sequence_res *res,
 727                                   int privileged)
 728{
 729        int ret;
 730        struct rpc_task *task;
 731        struct nfs41_call_sync_data data = {
 732                .seq_server = server,
 733                .seq_args = args,
 734                .seq_res = res,
 735        };
 736        struct rpc_task_setup task_setup = {
 737                .rpc_client = clnt,
 738                .rpc_message = msg,
 739                .callback_ops = &nfs41_call_sync_ops,
 740                .callback_data = &data
 741        };
 742
 743        if (privileged)
 744                task_setup.callback_ops = &nfs41_call_priv_sync_ops;
 745        task = rpc_run_task(&task_setup);
 746        if (IS_ERR(task))
 747                ret = PTR_ERR(task);
 748        else {
 749                ret = task->tk_status;
 750                rpc_put_task(task);
 751        }
 752        return ret;
 753}
 754
 755int _nfs4_call_sync_session(struct rpc_clnt *clnt,
 756                            struct nfs_server *server,
 757                            struct rpc_message *msg,
 758                            struct nfs4_sequence_args *args,
 759                            struct nfs4_sequence_res *res,
 760                            int cache_reply)
 761{
 762        nfs41_init_sequence(args, res, cache_reply);
 763        return nfs4_call_sync_sequence(clnt, server, msg, args, res, 0);
 764}
 765
 766#else
 767static inline
 768void nfs41_init_sequence(struct nfs4_sequence_args *args,
 769                struct nfs4_sequence_res *res, int cache_reply)
 770{
 771}
 772
 773static int nfs4_sequence_done(struct rpc_task *task,
 774                               struct nfs4_sequence_res *res)
 775{
 776        return 1;
 777}
 778#endif /* CONFIG_NFS_V4_1 */
 779
 780int _nfs4_call_sync(struct rpc_clnt *clnt,
 781                    struct nfs_server *server,
 782                    struct rpc_message *msg,
 783                    struct nfs4_sequence_args *args,
 784                    struct nfs4_sequence_res *res,
 785                    int cache_reply)
 786{
 787        nfs41_init_sequence(args, res, cache_reply);
 788        return rpc_call_sync(clnt, msg, 0);
 789}
 790
 791static inline
 792int nfs4_call_sync(struct rpc_clnt *clnt,
 793                   struct nfs_server *server,
 794                   struct rpc_message *msg,
 795                   struct nfs4_sequence_args *args,
 796                   struct nfs4_sequence_res *res,
 797                   int cache_reply)
 798{
 799        return server->nfs_client->cl_mvops->call_sync(clnt, server, msg,
 800                                                args, res, cache_reply);
 801}
 802
 803static void update_changeattr(struct inode *dir, struct nfs4_change_info *cinfo)
 804{
 805        struct nfs_inode *nfsi = NFS_I(dir);
 806
 807        spin_lock(&dir->i_lock);
 808        nfsi->cache_validity |= NFS_INO_INVALID_ATTR|NFS_INO_INVALID_DATA;
 809        if (!cinfo->atomic || cinfo->before != dir->i_version)
 810                nfs_force_lookup_revalidate(dir);
 811        dir->i_version = cinfo->after;
 812        spin_unlock(&dir->i_lock);
 813}
 814
 815struct nfs4_opendata {
 816        struct kref kref;
 817        struct nfs_openargs o_arg;
 818        struct nfs_openres o_res;
 819        struct nfs_open_confirmargs c_arg;
 820        struct nfs_open_confirmres c_res;
 821        struct nfs4_string owner_name;
 822        struct nfs4_string group_name;
 823        struct nfs_fattr f_attr;
 824        struct dentry *dir;
 825        struct dentry *dentry;
 826        struct nfs4_state_owner *owner;
 827        struct nfs4_state *state;
 828        struct iattr attrs;
 829        unsigned long timestamp;
 830        unsigned int rpc_done : 1;
 831        int rpc_status;
 832        int cancelled;
 833};
 834
 835
 836static void nfs4_init_opendata_res(struct nfs4_opendata *p)
 837{
 838        p->o_res.f_attr = &p->f_attr;
 839        p->o_res.seqid = p->o_arg.seqid;
 840        p->c_res.seqid = p->c_arg.seqid;
 841        p->o_res.server = p->o_arg.server;
 842        p->o_res.access_request = p->o_arg.access;
 843        nfs_fattr_init(&p->f_attr);
 844        nfs_fattr_init_names(&p->f_attr, &p->owner_name, &p->group_name);
 845}
 846
 847static struct nfs4_opendata *nfs4_opendata_alloc(struct dentry *dentry,
 848                struct nfs4_state_owner *sp, fmode_t fmode, int flags,
 849                const struct iattr *attrs,
 850                gfp_t gfp_mask)
 851{
 852        struct dentry *parent = dget_parent(dentry);
 853        struct inode *dir = parent->d_inode;
 854        struct nfs_server *server = NFS_SERVER(dir);
 855        struct nfs4_opendata *p;
 856
 857        p = kzalloc(sizeof(*p), gfp_mask);
 858        if (p == NULL)
 859                goto err;
 860        p->o_arg.seqid = nfs_alloc_seqid(&sp->so_seqid, gfp_mask);
 861        if (p->o_arg.seqid == NULL)
 862                goto err_free;
 863        nfs_sb_active(dentry->d_sb);
 864        p->dentry = dget(dentry);
 865        p->dir = parent;
 866        p->owner = sp;
 867        atomic_inc(&sp->so_count);
 868        p->o_arg.fh = NFS_FH(dir);
 869        p->o_arg.open_flags = flags;
 870        p->o_arg.fmode = fmode & (FMODE_READ|FMODE_WRITE);
 871        /* don't put an ACCESS op in OPEN compound if O_EXCL, because ACCESS
 872         * will return permission denied for all bits until close */
 873        if (!(flags & O_EXCL)) {
 874                /* ask server to check for all possible rights as results
 875                 * are cached */
 876                p->o_arg.access = NFS4_ACCESS_READ | NFS4_ACCESS_MODIFY |
 877                                  NFS4_ACCESS_EXTEND | NFS4_ACCESS_EXECUTE;
 878        }
 879        p->o_arg.clientid = server->nfs_client->cl_clientid;
 880        p->o_arg.id.create_time = ktime_to_ns(sp->so_seqid.create_time);
 881        p->o_arg.id.uniquifier = sp->so_seqid.owner_id;
 882        p->o_arg.name = &dentry->d_name;
 883        p->o_arg.server = server;
 884        p->o_arg.bitmask = server->attr_bitmask;
 885        p->o_arg.open_bitmap = &nfs4_fattr_bitmap[0];
 886        p->o_arg.claim = NFS4_OPEN_CLAIM_NULL;
 887        if (attrs != NULL && attrs->ia_valid != 0) {
 888                __be32 verf[2];
 889
 890                p->o_arg.u.attrs = &p->attrs;
 891                memcpy(&p->attrs, attrs, sizeof(p->attrs));
 892
 893                verf[0] = jiffies;
 894                verf[1] = current->pid;
 895                memcpy(p->o_arg.u.verifier.data, verf,
 896                                sizeof(p->o_arg.u.verifier.data));
 897        }
 898        p->c_arg.fh = &p->o_res.fh;
 899        p->c_arg.stateid = &p->o_res.stateid;
 900        p->c_arg.seqid = p->o_arg.seqid;
 901        nfs4_init_opendata_res(p);
 902        kref_init(&p->kref);
 903        return p;
 904err_free:
 905        kfree(p);
 906err:
 907        dput(parent);
 908        return NULL;
 909}
 910
 911static void nfs4_opendata_free(struct kref *kref)
 912{
 913        struct nfs4_opendata *p = container_of(kref,
 914                        struct nfs4_opendata, kref);
 915        struct super_block *sb = p->dentry->d_sb;
 916
 917        nfs_free_seqid(p->o_arg.seqid);
 918        if (p->state != NULL)
 919                nfs4_put_open_state(p->state);
 920        nfs4_put_state_owner(p->owner);
 921        dput(p->dir);
 922        dput(p->dentry);
 923        nfs_sb_deactive(sb);
 924        nfs_fattr_free_names(&p->f_attr);
 925        kfree(p);
 926}
 927
 928static void nfs4_opendata_put(struct nfs4_opendata *p)
 929{
 930        if (p != NULL)
 931                kref_put(&p->kref, nfs4_opendata_free);
 932}
 933
 934static int nfs4_wait_for_completion_rpc_task(struct rpc_task *task)
 935{
 936        int ret;
 937
 938        ret = rpc_wait_for_completion_task(task);
 939        return ret;
 940}
 941
 942static int can_open_cached(struct nfs4_state *state, fmode_t mode, int open_mode)
 943{
 944        int ret = 0;
 945
 946        if (open_mode & (O_EXCL|O_TRUNC))
 947                goto out;
 948        switch (mode & (FMODE_READ|FMODE_WRITE)) {
 949                case FMODE_READ:
 950                        ret |= test_bit(NFS_O_RDONLY_STATE, &state->flags) != 0
 951                                && state->n_rdonly != 0;
 952                        break;
 953                case FMODE_WRITE:
 954                        ret |= test_bit(NFS_O_WRONLY_STATE, &state->flags) != 0
 955                                && state->n_wronly != 0;
 956                        break;
 957                case FMODE_READ|FMODE_WRITE:
 958                        ret |= test_bit(NFS_O_RDWR_STATE, &state->flags) != 0
 959                                && state->n_rdwr != 0;
 960        }
 961out:
 962        return ret;
 963}
 964
 965static int can_open_delegated(struct nfs_delegation *delegation, fmode_t fmode)
 966{
 967        if (delegation == NULL)
 968                return 0;
 969        if ((delegation->type & fmode) != fmode)
 970                return 0;
 971        if (test_bit(NFS_DELEGATION_NEED_RECLAIM, &delegation->flags))
 972                return 0;
 973        nfs_mark_delegation_referenced(delegation);
 974        return 1;
 975}
 976
 977static void update_open_stateflags(struct nfs4_state *state, fmode_t fmode)
 978{
 979        switch (fmode) {
 980                case FMODE_WRITE:
 981                        state->n_wronly++;
 982                        break;
 983                case FMODE_READ:
 984                        state->n_rdonly++;
 985                        break;
 986                case FMODE_READ|FMODE_WRITE:
 987                        state->n_rdwr++;
 988        }
 989        nfs4_state_set_mode_locked(state, state->state | fmode);
 990}
 991
 992static void nfs_set_open_stateid_locked(struct nfs4_state *state, nfs4_stateid *stateid, fmode_t fmode)
 993{
 994        if (test_bit(NFS_DELEGATED_STATE, &state->flags) == 0)
 995                nfs4_stateid_copy(&state->stateid, stateid);
 996        nfs4_stateid_copy(&state->open_stateid, stateid);
 997        switch (fmode) {
 998                case FMODE_READ:
 999                        set_bit(NFS_O_RDONLY_STATE, &state->flags);
1000                        break;
1001                case FMODE_WRITE:
1002                        set_bit(NFS_O_WRONLY_STATE, &state->flags);
1003                        break;
1004                case FMODE_READ|FMODE_WRITE:
1005                        set_bit(NFS_O_RDWR_STATE, &state->flags);
1006        }
1007}
1008
1009static void nfs_set_open_stateid(struct nfs4_state *state, nfs4_stateid *stateid, fmode_t fmode)
1010{
1011        write_seqlock(&state->seqlock);
1012        nfs_set_open_stateid_locked(state, stateid, fmode);
1013        write_sequnlock(&state->seqlock);
1014}
1015
1016static void __update_open_stateid(struct nfs4_state *state, nfs4_stateid *open_stateid, const nfs4_stateid *deleg_stateid, fmode_t fmode)
1017{
1018        /*
1019         * Protect the call to nfs4_state_set_mode_locked and
1020         * serialise the stateid update
1021         */
1022        write_seqlock(&state->seqlock);
1023        if (deleg_stateid != NULL) {
1024                nfs4_stateid_copy(&state->stateid, deleg_stateid);
1025                set_bit(NFS_DELEGATED_STATE, &state->flags);
1026        }
1027        if (open_stateid != NULL)
1028                nfs_set_open_stateid_locked(state, open_stateid, fmode);
1029        write_sequnlock(&state->seqlock);
1030        spin_lock(&state->owner->so_lock);
1031        update_open_stateflags(state, fmode);
1032        spin_unlock(&state->owner->so_lock);
1033}
1034
1035static int update_open_stateid(struct nfs4_state *state, nfs4_stateid *open_stateid, nfs4_stateid *delegation, fmode_t fmode)
1036{
1037        struct nfs_inode *nfsi = NFS_I(state->inode);
1038        struct nfs_delegation *deleg_cur;
1039        int ret = 0;
1040
1041        fmode &= (FMODE_READ|FMODE_WRITE);
1042
1043        rcu_read_lock();
1044        deleg_cur = rcu_dereference(nfsi->delegation);
1045        if (deleg_cur == NULL)
1046                goto no_delegation;
1047
1048        spin_lock(&deleg_cur->lock);
1049        if (nfsi->delegation != deleg_cur ||
1050            (deleg_cur->type & fmode) != fmode)
1051                goto no_delegation_unlock;
1052
1053        if (delegation == NULL)
1054                delegation = &deleg_cur->stateid;
1055        else if (!nfs4_stateid_match(&deleg_cur->stateid, delegation))
1056                goto no_delegation_unlock;
1057
1058        nfs_mark_delegation_referenced(deleg_cur);
1059        __update_open_stateid(state, open_stateid, &deleg_cur->stateid, fmode);
1060        ret = 1;
1061no_delegation_unlock:
1062        spin_unlock(&deleg_cur->lock);
1063no_delegation:
1064        rcu_read_unlock();
1065
1066        if (!ret && open_stateid != NULL) {
1067                __update_open_stateid(state, open_stateid, NULL, fmode);
1068                ret = 1;
1069        }
1070
1071        return ret;
1072}
1073
1074
1075static void nfs4_return_incompatible_delegation(struct inode *inode, fmode_t fmode)
1076{
1077        struct nfs_delegation *delegation;
1078
1079        rcu_read_lock();
1080        delegation = rcu_dereference(NFS_I(inode)->delegation);
1081        if (delegation == NULL || (delegation->type & fmode) == fmode) {
1082                rcu_read_unlock();
1083                return;
1084        }
1085        rcu_read_unlock();
1086        nfs4_inode_return_delegation(inode);
1087}
1088
1089static struct nfs4_state *nfs4_try_open_cached(struct nfs4_opendata *opendata)
1090{
1091        struct nfs4_state *state = opendata->state;
1092        struct nfs_inode *nfsi = NFS_I(state->inode);
1093        struct nfs_delegation *delegation;
1094        int open_mode = opendata->o_arg.open_flags & (O_EXCL|O_TRUNC);
1095        fmode_t fmode = opendata->o_arg.fmode;
1096        nfs4_stateid stateid;
1097        int ret = -EAGAIN;
1098
1099        for (;;) {
1100                if (can_open_cached(state, fmode, open_mode)) {
1101                        spin_lock(&state->owner->so_lock);
1102                        if (can_open_cached(state, fmode, open_mode)) {
1103                                update_open_stateflags(state, fmode);
1104                                spin_unlock(&state->owner->so_lock);
1105                                goto out_return_state;
1106                        }
1107                        spin_unlock(&state->owner->so_lock);
1108                }
1109                rcu_read_lock();
1110                delegation = rcu_dereference(nfsi->delegation);
1111                if (!can_open_delegated(delegation, fmode)) {
1112                        rcu_read_unlock();
1113                        break;
1114                }
1115                /* Save the delegation */
1116                nfs4_stateid_copy(&stateid, &delegation->stateid);
1117                rcu_read_unlock();
1118                ret = nfs_may_open(state->inode, state->owner->so_cred, open_mode);
1119                if (ret != 0)
1120                        goto out;
1121                ret = -EAGAIN;
1122
1123                /* Try to update the stateid using the delegation */
1124                if (update_open_stateid(state, NULL, &stateid, fmode))
1125                        goto out_return_state;
1126        }
1127out:
1128        return ERR_PTR(ret);
1129out_return_state:
1130        atomic_inc(&state->count);
1131        return state;
1132}
1133
1134static void
1135nfs4_opendata_check_deleg(struct nfs4_opendata *data, struct nfs4_state *state)
1136{
1137        struct nfs_client *clp = NFS_SERVER(state->inode)->nfs_client;
1138        struct nfs_delegation *delegation;
1139        int delegation_flags = 0;
1140
1141        rcu_read_lock();
1142        delegation = rcu_dereference(NFS_I(state->inode)->delegation);
1143        if (delegation)
1144                delegation_flags = delegation->flags;
1145        rcu_read_unlock();
1146        if (data->o_arg.claim == NFS4_OPEN_CLAIM_DELEGATE_CUR) {
1147                pr_err_ratelimited("NFS: Broken NFSv4 server %s is "
1148                                   "returning a delegation for "
1149                                   "OPEN(CLAIM_DELEGATE_CUR)\n",
1150                                   clp->cl_hostname);
1151        } else if ((delegation_flags & 1UL<<NFS_DELEGATION_NEED_RECLAIM) == 0)
1152                nfs_inode_set_delegation(state->inode,
1153                                         data->owner->so_cred,
1154                                         &data->o_res);
1155        else
1156                nfs_inode_reclaim_delegation(state->inode,
1157                                             data->owner->so_cred,
1158                                             &data->o_res);
1159}
1160
1161/*
1162 * Check the inode attributes against the CLAIM_PREVIOUS returned attributes
1163 * and update the nfs4_state.
1164 */
1165static struct nfs4_state *
1166_nfs4_opendata_reclaim_to_nfs4_state(struct nfs4_opendata *data)
1167{
1168        struct inode *inode = data->state->inode;
1169        struct nfs4_state *state = data->state;
1170        int ret;
1171
1172        if (!data->rpc_done) {
1173                ret = data->rpc_status;
1174                goto err;
1175        }
1176
1177        ret = -ESTALE;
1178        if (!(data->f_attr.valid & NFS_ATTR_FATTR_TYPE) ||
1179            !(data->f_attr.valid & NFS_ATTR_FATTR_FILEID) ||
1180            !(data->f_attr.valid & NFS_ATTR_FATTR_CHANGE))
1181                goto err;
1182
1183        ret = -ENOMEM;
1184        state = nfs4_get_open_state(inode, data->owner);
1185        if (state == NULL)
1186                goto err;
1187
1188        ret = nfs_refresh_inode(inode, &data->f_attr);
1189        if (ret)
1190                goto err;
1191
1192        if (data->o_res.delegation_type != 0)
1193                nfs4_opendata_check_deleg(data, state);
1194        update_open_stateid(state, &data->o_res.stateid, NULL,
1195                            data->o_arg.fmode);
1196
1197        return state;
1198err:
1199        return ERR_PTR(ret);
1200
1201}
1202
1203static struct nfs4_state *
1204_nfs4_opendata_to_nfs4_state(struct nfs4_opendata *data)
1205{
1206        struct inode *inode;
1207        struct nfs4_state *state = NULL;
1208        int ret;
1209
1210        if (!data->rpc_done) {
1211                state = nfs4_try_open_cached(data);
1212                goto out;
1213        }
1214
1215        ret = -EAGAIN;
1216        if (!(data->f_attr.valid & NFS_ATTR_FATTR))
1217                goto err;
1218        inode = nfs_fhget(data->dir->d_sb, &data->o_res.fh, &data->f_attr);
1219        ret = PTR_ERR(inode);
1220        if (IS_ERR(inode))
1221                goto err;
1222        ret = -ENOMEM;
1223        state = nfs4_get_open_state(inode, data->owner);
1224        if (state == NULL)
1225                goto err_put_inode;
1226        if (data->o_res.delegation_type != 0)
1227                nfs4_opendata_check_deleg(data, state);
1228        update_open_stateid(state, &data->o_res.stateid, NULL,
1229                        data->o_arg.fmode);
1230        iput(inode);
1231out:
1232        return state;
1233err_put_inode:
1234        iput(inode);
1235err:
1236        return ERR_PTR(ret);
1237}
1238
1239static struct nfs4_state *
1240nfs4_opendata_to_nfs4_state(struct nfs4_opendata *data)
1241{
1242        if (data->o_arg.claim == NFS4_OPEN_CLAIM_PREVIOUS)
1243                return _nfs4_opendata_reclaim_to_nfs4_state(data);
1244        return _nfs4_opendata_to_nfs4_state(data);
1245}
1246
1247static struct nfs_open_context *nfs4_state_find_open_context(struct nfs4_state *state)
1248{
1249        struct nfs_inode *nfsi = NFS_I(state->inode);
1250        struct nfs_open_context *ctx;
1251
1252        spin_lock(&state->inode->i_lock);
1253        list_for_each_entry(ctx, &nfsi->open_files, list) {
1254                if (ctx->state != state)
1255                        continue;
1256                get_nfs_open_context(ctx);
1257                spin_unlock(&state->inode->i_lock);
1258                return ctx;
1259        }
1260        spin_unlock(&state->inode->i_lock);
1261        return ERR_PTR(-ENOENT);
1262}
1263
1264static struct nfs4_opendata *nfs4_open_recoverdata_alloc(struct nfs_open_context *ctx, struct nfs4_state *state)
1265{
1266        struct nfs4_opendata *opendata;
1267
1268        opendata = nfs4_opendata_alloc(ctx->dentry, state->owner, 0, 0, NULL, GFP_NOFS);
1269        if (opendata == NULL)
1270                return ERR_PTR(-ENOMEM);
1271        opendata->state = state;
1272        atomic_inc(&state->count);
1273        return opendata;
1274}
1275
1276static int nfs4_open_recover_helper(struct nfs4_opendata *opendata, fmode_t fmode, struct nfs4_state **res)
1277{
1278        struct nfs4_state *newstate;
1279        int ret;
1280
1281        opendata->o_arg.open_flags = 0;
1282        opendata->o_arg.fmode = fmode;
1283        memset(&opendata->o_res, 0, sizeof(opendata->o_res));
1284        memset(&opendata->c_res, 0, sizeof(opendata->c_res));
1285        nfs4_init_opendata_res(opendata);
1286        ret = _nfs4_recover_proc_open(opendata);
1287        if (ret != 0)
1288                return ret; 
1289        newstate = nfs4_opendata_to_nfs4_state(opendata);
1290        if (IS_ERR(newstate))
1291                return PTR_ERR(newstate);
1292        nfs4_close_state(newstate, fmode);
1293        *res = newstate;
1294        return 0;
1295}
1296
1297static int nfs4_open_recover(struct nfs4_opendata *opendata, struct nfs4_state *state)
1298{
1299        struct nfs4_state *newstate;
1300        int ret;
1301
1302        /* memory barrier prior to reading state->n_* */
1303        clear_bit(NFS_DELEGATED_STATE, &state->flags);
1304        smp_rmb();
1305        if (state->n_rdwr != 0) {
1306                clear_bit(NFS_O_RDWR_STATE, &state->flags);
1307                ret = nfs4_open_recover_helper(opendata, FMODE_READ|FMODE_WRITE, &newstate);
1308                if (ret != 0)
1309                        return ret;
1310                if (newstate != state)
1311                        return -ESTALE;
1312        }
1313        if (state->n_wronly != 0) {
1314                clear_bit(NFS_O_WRONLY_STATE, &state->flags);
1315                ret = nfs4_open_recover_helper(opendata, FMODE_WRITE, &newstate);
1316                if (ret != 0)
1317                        return ret;
1318                if (newstate != state)
1319                        return -ESTALE;
1320        }
1321        if (state->n_rdonly != 0) {
1322                clear_bit(NFS_O_RDONLY_STATE, &state->flags);
1323                ret = nfs4_open_recover_helper(opendata, FMODE_READ, &newstate);
1324                if (ret != 0)
1325                        return ret;
1326                if (newstate != state)
1327                        return -ESTALE;
1328        }
1329        /*
1330         * We may have performed cached opens for all three recoveries.
1331         * Check if we need to update the current stateid.
1332         */
1333        if (test_bit(NFS_DELEGATED_STATE, &state->flags) == 0 &&
1334            !nfs4_stateid_match(&state->stateid, &state->open_stateid)) {
1335                write_seqlock(&state->seqlock);
1336                if (test_bit(NFS_DELEGATED_STATE, &state->flags) == 0)
1337                        nfs4_stateid_copy(&state->stateid, &state->open_stateid);
1338                write_sequnlock(&state->seqlock);
1339        }
1340        return 0;
1341}
1342
1343/*
1344 * OPEN_RECLAIM:
1345 *      reclaim state on the server after a reboot.
1346 */
1347static int _nfs4_do_open_reclaim(struct nfs_open_context *ctx, struct nfs4_state *state)
1348{
1349        struct nfs_delegation *delegation;
1350        struct nfs4_opendata *opendata;
1351        fmode_t delegation_type = 0;
1352        int status;
1353
1354        opendata = nfs4_open_recoverdata_alloc(ctx, state);
1355        if (IS_ERR(opendata))
1356                return PTR_ERR(opendata);
1357        opendata->o_arg.claim = NFS4_OPEN_CLAIM_PREVIOUS;
1358        opendata->o_arg.fh = NFS_FH(state->inode);
1359        rcu_read_lock();
1360        delegation = rcu_dereference(NFS_I(state->inode)->delegation);
1361        if (delegation != NULL && test_bit(NFS_DELEGATION_NEED_RECLAIM, &delegation->flags) != 0)
1362                delegation_type = delegation->type;
1363        rcu_read_unlock();
1364        opendata->o_arg.u.delegation_type = delegation_type;
1365        status = nfs4_open_recover(opendata, state);
1366        nfs4_opendata_put(opendata);
1367        return status;
1368}
1369
1370static int nfs4_do_open_reclaim(struct nfs_open_context *ctx, struct nfs4_state *state)
1371{
1372        struct nfs_server *server = NFS_SERVER(state->inode);
1373        struct nfs4_exception exception = { };
1374        int err;
1375        do {
1376                err = _nfs4_do_open_reclaim(ctx, state);
1377                if (err != -NFS4ERR_DELAY)
1378                        break;
1379                nfs4_handle_exception(server, err, &exception);
1380        } while (exception.retry);
1381        return err;
1382}
1383
1384static int nfs4_open_reclaim(struct nfs4_state_owner *sp, struct nfs4_state *state)
1385{
1386        struct nfs_open_context *ctx;
1387        int ret;
1388
1389        ctx = nfs4_state_find_open_context(state);
1390        if (IS_ERR(ctx))
1391                return PTR_ERR(ctx);
1392        ret = nfs4_do_open_reclaim(ctx, state);
1393        put_nfs_open_context(ctx);
1394        return ret;
1395}
1396
1397static int _nfs4_open_delegation_recall(struct nfs_open_context *ctx, struct nfs4_state *state, const nfs4_stateid *stateid)
1398{
1399        struct nfs4_opendata *opendata;
1400        int ret;
1401
1402        opendata = nfs4_open_recoverdata_alloc(ctx, state);
1403        if (IS_ERR(opendata))
1404                return PTR_ERR(opendata);
1405        opendata->o_arg.claim = NFS4_OPEN_CLAIM_DELEGATE_CUR;
1406        nfs4_stateid_copy(&opendata->o_arg.u.delegation, stateid);
1407        ret = nfs4_open_recover(opendata, state);
1408        nfs4_opendata_put(opendata);
1409        return ret;
1410}
1411
1412int nfs4_open_delegation_recall(struct nfs_open_context *ctx, struct nfs4_state *state, const nfs4_stateid *stateid)
1413{
1414        struct nfs4_exception exception = { };
1415        struct nfs_server *server = NFS_SERVER(state->inode);
1416        int err;
1417        do {
1418                err = _nfs4_open_delegation_recall(ctx, state, stateid);
1419                switch (err) {
1420                        case 0:
1421                        case -ENOENT:
1422                        case -ESTALE:
1423                                goto out;
1424                        case -NFS4ERR_BADSESSION:
1425                        case -NFS4ERR_BADSLOT:
1426                        case -NFS4ERR_BAD_HIGH_SLOT:
1427                        case -NFS4ERR_CONN_NOT_BOUND_TO_SESSION:
1428                        case -NFS4ERR_DEADSESSION:
1429                                nfs4_schedule_session_recovery(server->nfs_client->cl_session, err);
1430                                goto out;
1431                        case -NFS4ERR_STALE_CLIENTID:
1432                        case -NFS4ERR_STALE_STATEID:
1433                        case -NFS4ERR_EXPIRED:
1434                                /* Don't recall a delegation if it was lost */
1435                                nfs4_schedule_lease_recovery(server->nfs_client);
1436                                goto out;
1437                        case -ERESTARTSYS:
1438                                /*
1439                                 * The show must go on: exit, but mark the
1440                                 * stateid as needing recovery.
1441                                 */
1442                        case -NFS4ERR_DELEG_REVOKED:
1443                        case -NFS4ERR_ADMIN_REVOKED:
1444                        case -NFS4ERR_BAD_STATEID:
1445                                nfs_inode_find_state_and_recover(state->inode,
1446                                                stateid);
1447                                nfs4_schedule_stateid_recovery(server, state);
1448                        case -EKEYEXPIRED:
1449                                /*
1450                                 * User RPCSEC_GSS context has expired.
1451                                 * We cannot recover this stateid now, so
1452                                 * skip it and allow recovery thread to
1453                                 * proceed.
1454                                 */
1455                        case -ENOMEM:
1456                                err = 0;
1457                                goto out;
1458                }
1459                err = nfs4_handle_exception(server, err, &exception);
1460        } while (exception.retry);
1461out:
1462        return err;
1463}
1464
1465static void nfs4_open_confirm_done(struct rpc_task *task, void *calldata)
1466{
1467        struct nfs4_opendata *data = calldata;
1468
1469        data->rpc_status = task->tk_status;
1470        if (data->rpc_status == 0) {
1471                nfs4_stateid_copy(&data->o_res.stateid, &data->c_res.stateid);
1472                nfs_confirm_seqid(&data->owner->so_seqid, 0);
1473                renew_lease(data->o_res.server, data->timestamp);
1474                data->rpc_done = 1;
1475        }
1476}
1477
1478static void nfs4_open_confirm_release(void *calldata)
1479{
1480        struct nfs4_opendata *data = calldata;
1481        struct nfs4_state *state = NULL;
1482
1483        /* If this request hasn't been cancelled, do nothing */
1484        if (data->cancelled == 0)
1485                goto out_free;
1486        /* In case of error, no cleanup! */
1487        if (!data->rpc_done)
1488                goto out_free;
1489        state = nfs4_opendata_to_nfs4_state(data);
1490        if (!IS_ERR(state))
1491                nfs4_close_state(state, data->o_arg.fmode);
1492out_free:
1493        nfs4_opendata_put(data);
1494}
1495
1496static const struct rpc_call_ops nfs4_open_confirm_ops = {
1497        .rpc_call_done = nfs4_open_confirm_done,
1498        .rpc_release = nfs4_open_confirm_release,
1499};
1500
1501/*
1502 * Note: On error, nfs4_proc_open_confirm will free the struct nfs4_opendata
1503 */
1504static int _nfs4_proc_open_confirm(struct nfs4_opendata *data)
1505{
1506        struct nfs_server *server = NFS_SERVER(data->dir->d_inode);
1507        struct rpc_task *task;
1508        struct  rpc_message msg = {
1509                .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_OPEN_CONFIRM],
1510                .rpc_argp = &data->c_arg,
1511                .rpc_resp = &data->c_res,
1512                .rpc_cred = data->owner->so_cred,
1513        };
1514        struct rpc_task_setup task_setup_data = {
1515                .rpc_client = server->client,
1516                .rpc_message = &msg,
1517                .callback_ops = &nfs4_open_confirm_ops,
1518                .callback_data = data,
1519                .workqueue = nfsiod_workqueue,
1520                .flags = RPC_TASK_ASYNC,
1521        };
1522        int status;
1523
1524        kref_get(&data->kref);
1525        data->rpc_done = 0;
1526        data->rpc_status = 0;
1527        data->timestamp = jiffies;
1528        task = rpc_run_task(&task_setup_data);
1529        if (IS_ERR(task))
1530                return PTR_ERR(task);
1531        status = nfs4_wait_for_completion_rpc_task(task);
1532        if (status != 0) {
1533                data->cancelled = 1;
1534                smp_wmb();
1535        } else
1536                status = data->rpc_status;
1537        rpc_put_task(task);
1538        return status;
1539}
1540
1541static void nfs4_open_prepare(struct rpc_task *task, void *calldata)
1542{
1543        struct nfs4_opendata *data = calldata;
1544        struct nfs4_state_owner *sp = data->owner;
1545
1546        if (nfs_wait_on_sequence(data->o_arg.seqid, task) != 0)
1547                return;
1548        /*
1549         * Check if we still need to send an OPEN call, or if we can use
1550         * a delegation instead.
1551         */
1552        if (data->state != NULL) {
1553                struct nfs_delegation *delegation;
1554
1555                if (can_open_cached(data->state, data->o_arg.fmode, data->o_arg.open_flags))
1556                        goto out_no_action;
1557                rcu_read_lock();
1558                delegation = rcu_dereference(NFS_I(data->state->inode)->delegation);
1559                if (data->o_arg.claim != NFS4_OPEN_CLAIM_DELEGATE_CUR &&
1560                    can_open_delegated(delegation, data->o_arg.fmode))
1561                        goto unlock_no_action;
1562                rcu_read_unlock();
1563        }
1564        /* Update client id. */
1565        data->o_arg.clientid = sp->so_server->nfs_client->cl_clientid;
1566        if (data->o_arg.claim == NFS4_OPEN_CLAIM_PREVIOUS) {
1567                task->tk_msg.rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_OPEN_NOATTR];
1568                data->o_arg.open_bitmap = &nfs4_open_noattr_bitmap[0];
1569                nfs_copy_fh(&data->o_res.fh, data->o_arg.fh);
1570        }
1571        data->timestamp = jiffies;
1572        if (nfs4_setup_sequence(data->o_arg.server,
1573                                &data->o_arg.seq_args,
1574                                &data->o_res.seq_res,
1575                                task) != 0)
1576                nfs_release_seqid(data->o_arg.seqid);
1577        else
1578                rpc_call_start(task);
1579        return;
1580unlock_no_action:
1581        rcu_read_unlock();
1582out_no_action:
1583        task->tk_action = NULL;
1584
1585}
1586
1587static void nfs4_recover_open_prepare(struct rpc_task *task, void *calldata)
1588{
1589        rpc_task_set_priority(task, RPC_PRIORITY_PRIVILEGED);
1590        nfs4_open_prepare(task, calldata);
1591}
1592
1593static void nfs4_open_done(struct rpc_task *task, void *calldata)
1594{
1595        struct nfs4_opendata *data = calldata;
1596
1597        data->rpc_status = task->tk_status;
1598
1599        if (!nfs4_sequence_done(task, &data->o_res.seq_res))
1600                return;
1601
1602        if (task->tk_status == 0) {
1603                if (data->o_res.f_attr->valid & NFS_ATTR_FATTR_TYPE) {
1604                        switch (data->o_res.f_attr->mode & S_IFMT) {
1605                        case S_IFREG:
1606                                break;
1607                        case S_IFLNK:
1608                                data->rpc_status = -ELOOP;
1609                                break;
1610                        case S_IFDIR:
1611                                data->rpc_status = -EISDIR;
1612                                break;
1613                        default:
1614                                data->rpc_status = -ENOTDIR;
1615                        }
1616                }
1617                renew_lease(data->o_res.server, data->timestamp);
1618                if (!(data->o_res.rflags & NFS4_OPEN_RESULT_CONFIRM))
1619                        nfs_confirm_seqid(&data->owner->so_seqid, 0);
1620        }
1621        data->rpc_done = 1;
1622}
1623
1624static void nfs4_open_release(void *calldata)
1625{
1626        struct nfs4_opendata *data = calldata;
1627        struct nfs4_state *state = NULL;
1628
1629        /* If this request hasn't been cancelled, do nothing */
1630        if (data->cancelled == 0)
1631                goto out_free;
1632        /* In case of error, no cleanup! */
1633        if (data->rpc_status != 0 || !data->rpc_done)
1634                goto out_free;
1635        /* In case we need an open_confirm, no cleanup! */
1636        if (data->o_res.rflags & NFS4_OPEN_RESULT_CONFIRM)
1637                goto out_free;
1638        state = nfs4_opendata_to_nfs4_state(data);
1639        if (!IS_ERR(state))
1640                nfs4_close_state(state, data->o_arg.fmode);
1641out_free:
1642        nfs4_opendata_put(data);
1643}
1644
1645static const struct rpc_call_ops nfs4_open_ops = {
1646        .rpc_call_prepare = nfs4_open_prepare,
1647        .rpc_call_done = nfs4_open_done,
1648        .rpc_release = nfs4_open_release,
1649};
1650
1651static const struct rpc_call_ops nfs4_recover_open_ops = {
1652        .rpc_call_prepare = nfs4_recover_open_prepare,
1653        .rpc_call_done = nfs4_open_done,
1654        .rpc_release = nfs4_open_release,
1655};
1656
1657static int nfs4_run_open_task(struct nfs4_opendata *data, int isrecover)
1658{
1659        struct inode *dir = data->dir->d_inode;
1660        struct nfs_server *server = NFS_SERVER(dir);
1661        struct nfs_openargs *o_arg = &data->o_arg;
1662        struct nfs_openres *o_res = &data->o_res;
1663        struct rpc_task *task;
1664        struct rpc_message msg = {
1665                .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_OPEN],
1666                .rpc_argp = o_arg,
1667                .rpc_resp = o_res,
1668                .rpc_cred = data->owner->so_cred,
1669        };
1670        struct rpc_task_setup task_setup_data = {
1671                .rpc_client = server->client,
1672                .rpc_message = &msg,
1673                .callback_ops = &nfs4_open_ops,
1674                .callback_data = data,
1675                .workqueue = nfsiod_workqueue,
1676                .flags = RPC_TASK_ASYNC,
1677        };
1678        int status;
1679
1680        nfs41_init_sequence(&o_arg->seq_args, &o_res->seq_res, 1);
1681        kref_get(&data->kref);
1682        data->rpc_done = 0;
1683        data->rpc_status = 0;
1684        data->cancelled = 0;
1685        if (isrecover)
1686                task_setup_data.callback_ops = &nfs4_recover_open_ops;
1687        task = rpc_run_task(&task_setup_data);
1688        if (IS_ERR(task))
1689                return PTR_ERR(task);
1690        status = nfs4_wait_for_completion_rpc_task(task);
1691        if (status != 0) {
1692                data->cancelled = 1;
1693                smp_wmb();
1694        } else
1695                status = data->rpc_status;
1696        rpc_put_task(task);
1697
1698        return status;
1699}
1700
1701static int _nfs4_recover_proc_open(struct nfs4_opendata *data)
1702{
1703        struct inode *dir = data->dir->d_inode;
1704        struct nfs_openres *o_res = &data->o_res;
1705        int status;
1706
1707        status = nfs4_run_open_task(data, 1);
1708        if (status != 0 || !data->rpc_done)
1709                return status;
1710
1711        nfs_fattr_map_and_free_names(NFS_SERVER(dir), &data->f_attr);
1712
1713        if (o_res->rflags & NFS4_OPEN_RESULT_CONFIRM) {
1714                status = _nfs4_proc_open_confirm(data);
1715                if (status != 0)
1716                        return status;
1717        }
1718
1719        return status;
1720}
1721
1722static int nfs4_opendata_access(struct rpc_cred *cred,
1723                                struct nfs4_opendata *opendata,
1724                                struct nfs4_state *state, fmode_t fmode,
1725                                int openflags)
1726{
1727        struct nfs_access_entry cache;
1728        u32 mask;
1729
1730        /* access call failed or for some reason the server doesn't
1731         * support any access modes -- defer access call until later */
1732        if (opendata->o_res.access_supported == 0)
1733                return 0;
1734
1735        mask = 0;
1736        /* don't check MAY_WRITE - a newly created file may not have
1737         * write mode bits, but POSIX allows the creating process to write.
1738         * use openflags to check for exec, because fmode won't
1739         * always have FMODE_EXEC set when file open for exec. */
1740        if (openflags & __FMODE_EXEC) {
1741                /* ONLY check for exec rights */
1742                mask = MAY_EXEC;
1743        } else if (fmode & FMODE_READ)
1744                mask = MAY_READ;
1745
1746        cache.cred = cred;
1747        cache.jiffies = jiffies;
1748        nfs_access_set_mask(&cache, opendata->o_res.access_result);
1749        nfs_access_add_cache(state->inode, &cache);
1750
1751        if ((mask & ~cache.mask & (MAY_READ | MAY_EXEC)) == 0)
1752                return 0;
1753
1754        /* even though OPEN succeeded, access is denied. Close the file */
1755        nfs4_close_state(state, fmode);
1756        return -EACCES;
1757}
1758
1759/*
1760 * Note: On error, nfs4_proc_open will free the struct nfs4_opendata
1761 */
1762static int _nfs4_proc_open(struct nfs4_opendata *data)
1763{
1764        struct inode *dir = data->dir->d_inode;
1765        struct nfs_server *server = NFS_SERVER(dir);
1766        struct nfs_openargs *o_arg = &data->o_arg;
1767        struct nfs_openres *o_res = &data->o_res;
1768        int status;
1769
1770        status = nfs4_run_open_task(data, 0);
1771        if (!data->rpc_done)
1772                return status;
1773        if (status != 0) {
1774                if (status == -NFS4ERR_BADNAME &&
1775                                !(o_arg->open_flags & O_CREAT))
1776                        return -ENOENT;
1777                return status;
1778        }
1779
1780        nfs_fattr_map_and_free_names(server, &data->f_attr);
1781
1782        if (o_arg->open_flags & O_CREAT)
1783                update_changeattr(dir, &o_res->cinfo);
1784        if ((o_res->rflags & NFS4_OPEN_RESULT_LOCKTYPE_POSIX) == 0)
1785                server->caps &= ~NFS_CAP_POSIX_LOCK;
1786        if(o_res->rflags & NFS4_OPEN_RESULT_CONFIRM) {
1787                status = _nfs4_proc_open_confirm(data);
1788                if (status != 0)
1789                        return status;
1790        }
1791        if (!(o_res->f_attr->valid & NFS_ATTR_FATTR))
1792                _nfs4_proc_getattr(server, &o_res->fh, o_res->f_attr);
1793        return 0;
1794}
1795
1796static int nfs4_client_recover_expired_lease(struct nfs_client *clp)
1797{
1798        unsigned int loop;
1799        int ret;
1800
1801        for (loop = NFS4_MAX_LOOP_ON_RECOVER; loop != 0; loop--) {
1802                ret = nfs4_wait_clnt_recover(clp);
1803                if (ret != 0)
1804                        break;
1805                if (!test_bit(NFS4CLNT_LEASE_EXPIRED, &clp->cl_state) &&
1806                    !test_bit(NFS4CLNT_CHECK_LEASE,&clp->cl_state))
1807                        break;
1808                nfs4_schedule_state_manager(clp);
1809                ret = -EIO;
1810        }
1811        return ret;
1812}
1813
1814static int nfs4_recover_expired_lease(struct nfs_server *server)
1815{
1816        return nfs4_client_recover_expired_lease(server->nfs_client);
1817}
1818
1819/*
1820 * OPEN_EXPIRED:
1821 *      reclaim state on the server after a network partition.
1822 *      Assumes caller holds the appropriate lock
1823 */
1824static int _nfs4_open_expired(struct nfs_open_context *ctx, struct nfs4_state *state)
1825{
1826        struct nfs4_opendata *opendata;
1827        int ret;
1828
1829        opendata = nfs4_open_recoverdata_alloc(ctx, state);
1830        if (IS_ERR(opendata))
1831                return PTR_ERR(opendata);
1832        ret = nfs4_open_recover(opendata, state);
1833        if (ret == -ESTALE)
1834                d_drop(ctx->dentry);
1835        nfs4_opendata_put(opendata);
1836        return ret;
1837}
1838
1839static int nfs4_do_open_expired(struct nfs_open_context *ctx, struct nfs4_state *state)
1840{
1841        struct nfs_server *server = NFS_SERVER(state->inode);
1842        struct nfs4_exception exception = { };
1843        int err;
1844
1845        do {
1846                err = _nfs4_open_expired(ctx, state);
1847                switch (err) {
1848                default:
1849                        goto out;
1850                case -NFS4ERR_GRACE:
1851                case -NFS4ERR_DELAY:
1852                        nfs4_handle_exception(server, err, &exception);
1853                        err = 0;
1854                }
1855        } while (exception.retry);
1856out:
1857        return err;
1858}
1859
1860static int nfs4_open_expired(struct nfs4_state_owner *sp, struct nfs4_state *state)
1861{
1862        struct nfs_open_context *ctx;
1863        int ret;
1864
1865        ctx = nfs4_state_find_open_context(state);
1866        if (IS_ERR(ctx))
1867                return PTR_ERR(ctx);
1868        ret = nfs4_do_open_expired(ctx, state);
1869        put_nfs_open_context(ctx);
1870        return ret;
1871}
1872
1873#if defined(CONFIG_NFS_V4_1)
1874static void nfs41_clear_delegation_stateid(struct nfs4_state *state)
1875{
1876        struct nfs_server *server = NFS_SERVER(state->inode);
1877        nfs4_stateid *stateid = &state->stateid;
1878        int status;
1879
1880        /* If a state reset has been done, test_stateid is unneeded */
1881        if (test_bit(NFS_DELEGATED_STATE, &state->flags) == 0)
1882                return;
1883
1884        status = nfs41_test_stateid(server, stateid);
1885        if (status != NFS_OK) {
1886                /* Free the stateid unless the server explicitly
1887                 * informs us the stateid is unrecognized. */
1888                if (status != -NFS4ERR_BAD_STATEID)
1889                        nfs41_free_stateid(server, stateid);
1890                nfs_remove_bad_delegation(state->inode);
1891
1892                write_seqlock(&state->seqlock);
1893                nfs4_stateid_copy(&state->stateid, &state->open_stateid);
1894                write_sequnlock(&state->seqlock);
1895                clear_bit(NFS_DELEGATED_STATE, &state->flags);
1896        }
1897}
1898
1899/**
1900 * nfs41_check_open_stateid - possibly free an open stateid
1901 *
1902 * @state: NFSv4 state for an inode
1903 *
1904 * Returns NFS_OK if recovery for this stateid is now finished.
1905 * Otherwise a negative NFS4ERR value is returned.
1906 */
1907static int nfs41_check_open_stateid(struct nfs4_state *state)
1908{
1909        struct nfs_server *server = NFS_SERVER(state->inode);
1910        nfs4_stateid *stateid = &state->open_stateid;
1911        int status;
1912
1913        /* If a state reset has been done, test_stateid is unneeded */
1914        if ((test_bit(NFS_O_RDONLY_STATE, &state->flags) == 0) &&
1915            (test_bit(NFS_O_WRONLY_STATE, &state->flags) == 0) &&
1916            (test_bit(NFS_O_RDWR_STATE, &state->flags) == 0))
1917                return -NFS4ERR_BAD_STATEID;
1918
1919        status = nfs41_test_stateid(server, stateid);
1920        if (status != NFS_OK) {
1921                /* Free the stateid unless the server explicitly
1922                 * informs us the stateid is unrecognized. */
1923                if (status != -NFS4ERR_BAD_STATEID)
1924                        nfs41_free_stateid(server, stateid);
1925
1926                clear_bit(NFS_O_RDONLY_STATE, &state->flags);
1927                clear_bit(NFS_O_WRONLY_STATE, &state->flags);
1928                clear_bit(NFS_O_RDWR_STATE, &state->flags);
1929        }
1930        return status;
1931}
1932
1933static int nfs41_open_expired(struct nfs4_state_owner *sp, struct nfs4_state *state)
1934{
1935        int status;
1936
1937        nfs41_clear_delegation_stateid(state);
1938        status = nfs41_check_open_stateid(state);
1939        if (status != NFS_OK)
1940                status = nfs4_open_expired(sp, state);
1941        return status;
1942}
1943#endif
1944
1945/*
1946 * on an EXCLUSIVE create, the server should send back a bitmask with FATTR4-*
1947 * fields corresponding to attributes that were used to store the verifier.
1948 * Make sure we clobber those fields in the later setattr call
1949 */
1950static inline void nfs4_exclusive_attrset(struct nfs4_opendata *opendata, struct iattr *sattr)
1951{
1952        if ((opendata->o_res.attrset[1] & FATTR4_WORD1_TIME_ACCESS) &&
1953            !(sattr->ia_valid & ATTR_ATIME_SET))
1954                sattr->ia_valid |= ATTR_ATIME;
1955
1956        if ((opendata->o_res.attrset[1] & FATTR4_WORD1_TIME_MODIFY) &&
1957            !(sattr->ia_valid & ATTR_MTIME_SET))
1958                sattr->ia_valid |= ATTR_MTIME;
1959}
1960
1961/*
1962 * Returns a referenced nfs4_state
1963 */
1964static int _nfs4_do_open(struct inode *dir,
1965                        struct dentry *dentry,
1966                        fmode_t fmode,
1967                        int flags,
1968                        struct iattr *sattr,
1969                        struct rpc_cred *cred,
1970                        struct nfs4_state **res,
1971                        struct nfs4_threshold **ctx_th)
1972{
1973        struct nfs4_state_owner  *sp;
1974        struct nfs4_state     *state = NULL;
1975        struct nfs_server       *server = NFS_SERVER(dir);
1976        struct nfs4_opendata *opendata;
1977        int status;
1978
1979        /* Protect against reboot recovery conflicts */
1980        status = -ENOMEM;
1981        sp = nfs4_get_state_owner(server, cred, GFP_KERNEL);
1982        if (sp == NULL) {
1983                dprintk("nfs4_do_open: nfs4_get_state_owner failed!\n");
1984                goto out_err;
1985        }
1986        status = nfs4_recover_expired_lease(server);
1987        if (status != 0)
1988                goto err_put_state_owner;
1989        if (dentry->d_inode != NULL)
1990                nfs4_return_incompatible_delegation(dentry->d_inode, fmode);
1991        status = -ENOMEM;
1992        opendata = nfs4_opendata_alloc(dentry, sp, fmode, flags, sattr, GFP_KERNEL);
1993        if (opendata == NULL)
1994                goto err_put_state_owner;
1995
1996        if (ctx_th && server->attr_bitmask[2] & FATTR4_WORD2_MDSTHRESHOLD) {
1997                opendata->f_attr.mdsthreshold = pnfs_mdsthreshold_alloc();
1998                if (!opendata->f_attr.mdsthreshold)
1999                        goto err_opendata_put;
2000                opendata->o_arg.open_bitmap = &nfs4_pnfs_open_bitmap[0];
2001        }
2002        if (dentry->d_inode != NULL)
2003                opendata->state = nfs4_get_open_state(dentry->d_inode, sp);
2004
2005        status = _nfs4_proc_open(opendata);
2006        if (status != 0)
2007                goto err_opendata_put;
2008
2009        state = nfs4_opendata_to_nfs4_state(opendata);
2010        status = PTR_ERR(state);
2011        if (IS_ERR(state))
2012                goto err_opendata_put;
2013        if (server->caps & NFS_CAP_POSIX_LOCK)
2014                set_bit(NFS_STATE_POSIX_LOCKS, &state->flags);
2015
2016        status = nfs4_opendata_access(cred, opendata, state, fmode, flags);
2017        if (status != 0)
2018                goto err_opendata_put;
2019
2020        if (opendata->o_arg.open_flags & O_EXCL) {
2021                nfs4_exclusive_attrset(opendata, sattr);
2022
2023                nfs_fattr_init(opendata->o_res.f_attr);
2024                status = nfs4_do_setattr(state->inode, cred,
2025                                opendata->o_res.f_attr, sattr,
2026                                state);
2027                if (status == 0)
2028                        nfs_setattr_update_inode(state->inode, sattr);
2029                nfs_post_op_update_inode(state->inode, opendata->o_res.f_attr);
2030        }
2031
2032        if (pnfs_use_threshold(ctx_th, opendata->f_attr.mdsthreshold, server))
2033                *ctx_th = opendata->f_attr.mdsthreshold;
2034        else
2035                kfree(opendata->f_attr.mdsthreshold);
2036        opendata->f_attr.mdsthreshold = NULL;
2037
2038        nfs4_opendata_put(opendata);
2039        nfs4_put_state_owner(sp);
2040        *res = state;
2041        return 0;
2042err_opendata_put:
2043        kfree(opendata->f_attr.mdsthreshold);
2044        nfs4_opendata_put(opendata);
2045err_put_state_owner:
2046        nfs4_put_state_owner(sp);
2047out_err:
2048        *res = NULL;
2049        return status;
2050}
2051
2052
2053static struct nfs4_state *nfs4_do_open(struct inode *dir,
2054                                        struct dentry *dentry,
2055                                        fmode_t fmode,
2056                                        int flags,
2057                                        struct iattr *sattr,
2058                                        struct rpc_cred *cred,
2059                                        struct nfs4_threshold **ctx_th)
2060{
2061        struct nfs4_exception exception = { };
2062        struct nfs4_state *res;
2063        int status;
2064
2065        fmode &= FMODE_READ|FMODE_WRITE|FMODE_EXEC;
2066        do {
2067                status = _nfs4_do_open(dir, dentry, fmode, flags, sattr, cred,
2068                                       &res, ctx_th);
2069                if (status == 0)
2070                        break;
2071                /* NOTE: BAD_SEQID means the server and client disagree about the
2072                 * book-keeping w.r.t. state-changing operations
2073                 * (OPEN/CLOSE/LOCK/LOCKU...)
2074                 * It is actually a sign of a bug on the client or on the server.
2075                 *
2076                 * If we receive a BAD_SEQID error in the particular case of
2077                 * doing an OPEN, we assume that nfs_increment_open_seqid() will
2078                 * have unhashed the old state_owner for us, and that we can
2079                 * therefore safely retry using a new one. We should still warn
2080                 * the user though...
2081                 */
2082                if (status == -NFS4ERR_BAD_SEQID) {
2083                        pr_warn_ratelimited("NFS: v4 server %s "
2084                                        " returned a bad sequence-id error!\n",
2085                                        NFS_SERVER(dir)->nfs_client->cl_hostname);
2086                        exception.retry = 1;
2087                        continue;
2088                }
2089                /*
2090                 * BAD_STATEID on OPEN means that the server cancelled our
2091                 * state before it received the OPEN_CONFIRM.
2092                 * Recover by retrying the request as per the discussion
2093                 * on Page 181 of RFC3530.
2094                 */
2095                if (status == -NFS4ERR_BAD_STATEID) {
2096                        exception.retry = 1;
2097                        continue;
2098                }
2099                if (status == -EAGAIN) {
2100                        /* We must have found a delegation */
2101                        exception.retry = 1;
2102                        continue;
2103                }
2104                res = ERR_PTR(nfs4_handle_exception(NFS_SERVER(dir),
2105                                        status, &exception));
2106        } while (exception.retry);
2107        return res;
2108}
2109
2110static int _nfs4_do_setattr(struct inode *inode, struct rpc_cred *cred,
2111                            struct nfs_fattr *fattr, struct iattr *sattr,
2112                            struct nfs4_state *state)
2113{
2114        struct nfs_server *server = NFS_SERVER(inode);
2115        struct nfs_setattrargs  arg = {
2116                .fh             = NFS_FH(inode),
2117                .iap            = sattr,
2118                .server         = server,
2119                .bitmask = server->attr_bitmask,
2120        };
2121        struct nfs_setattrres  res = {
2122                .fattr          = fattr,
2123                .server         = server,
2124        };
2125        struct rpc_message msg = {
2126                .rpc_proc       = &nfs4_procedures[NFSPROC4_CLNT_SETATTR],
2127                .rpc_argp       = &arg,
2128                .rpc_resp       = &res,
2129                .rpc_cred       = cred,
2130        };
2131        unsigned long timestamp = jiffies;
2132        int status;
2133
2134        nfs_fattr_init(fattr);
2135
2136        if (state != NULL) {
2137                struct nfs_lockowner lockowner = {
2138                        .l_owner = current->files,
2139                        .l_pid = current->tgid,
2140                };
2141                nfs4_select_rw_stateid(&arg.stateid, state, FMODE_WRITE,
2142                                &lockowner);
2143        } else if (nfs4_copy_delegation_stateid(&arg.stateid, inode,
2144                                FMODE_WRITE)) {
2145                /* Use that stateid */
2146        } else
2147                nfs4_stateid_copy(&arg.stateid, &zero_stateid);
2148
2149        status = nfs4_call_sync(server->client, server, &msg, &arg.seq_args, &res.seq_res, 1);
2150        if (status == 0 && state != NULL)
2151                renew_lease(server, timestamp);
2152        return status;
2153}
2154
2155static int nfs4_do_setattr(struct inode *inode, struct rpc_cred *cred,
2156                           struct nfs_fattr *fattr, struct iattr *sattr,
2157                           struct nfs4_state *state)
2158{
2159        struct nfs_server *server = NFS_SERVER(inode);
2160        struct nfs4_exception exception = {
2161                .state = state,
2162                .inode = inode,
2163        };
2164        int err;
2165        do {
2166                err = _nfs4_do_setattr(inode, cred, fattr, sattr, state);
2167                switch (err) {
2168                case -NFS4ERR_OPENMODE:
2169                        if (state && !(state->state & FMODE_WRITE)) {
2170                                err = -EBADF;
2171                                if (sattr->ia_valid & ATTR_OPEN)
2172                                        err = -EACCES;
2173                                goto out;
2174                        }
2175                }
2176                err = nfs4_handle_exception(server, err, &exception);
2177        } while (exception.retry);
2178out:
2179        return err;
2180}
2181
2182struct nfs4_closedata {
2183        struct inode *inode;
2184        struct nfs4_state *state;
2185        struct nfs_closeargs arg;
2186        struct nfs_closeres res;
2187        struct nfs_fattr fattr;
2188        unsigned long timestamp;
2189        bool roc;
2190        u32 roc_barrier;
2191};
2192
2193static void nfs4_free_closedata(void *data)
2194{
2195        struct nfs4_closedata *calldata = data;
2196        struct nfs4_state_owner *sp = calldata->state->owner;
2197        struct super_block *sb = calldata->state->inode->i_sb;
2198
2199        if (calldata->roc)
2200                pnfs_roc_release(calldata->state->inode);
2201        nfs4_put_open_state(calldata->state);
2202        nfs_free_seqid(calldata->arg.seqid);
2203        nfs4_put_state_owner(sp);
2204        nfs_sb_deactive_async(sb);
2205        kfree(calldata);
2206}
2207
2208static void nfs4_close_clear_stateid_flags(struct nfs4_state *state,
2209                fmode_t fmode)
2210{
2211        spin_lock(&state->owner->so_lock);
2212        if (!(fmode & FMODE_READ))
2213                clear_bit(NFS_O_RDONLY_STATE, &state->flags);
2214        if (!(fmode & FMODE_WRITE))
2215                clear_bit(NFS_O_WRONLY_STATE, &state->flags);
2216        clear_bit(NFS_O_RDWR_STATE, &state->flags);
2217        spin_unlock(&state->owner->so_lock);
2218}
2219
2220static void nfs4_close_done(struct rpc_task *task, void *data)
2221{
2222        struct nfs4_closedata *calldata = data;
2223        struct nfs4_state *state = calldata->state;
2224        struct nfs_server *server = NFS_SERVER(calldata->inode);
2225
2226        dprintk("%s: begin!\n", __func__);
2227        if (!nfs4_sequence_done(task, &calldata->res.seq_res))
2228                return;
2229        /* hmm. we are done with the inode, and in the process of freeing
2230         * the state_owner. we keep this around to process errors
2231         */
2232        switch (task->tk_status) {
2233                case 0:
2234                        if (calldata->roc)
2235                                pnfs_roc_set_barrier(state->inode,
2236                                                     calldata->roc_barrier);
2237                        nfs_set_open_stateid(state, &calldata->res.stateid, 0);
2238                        renew_lease(server, calldata->timestamp);
2239                        nfs4_close_clear_stateid_flags(state,
2240                                        calldata->arg.fmode);
2241                        break;
2242                case -NFS4ERR_STALE_STATEID:
2243                case -NFS4ERR_OLD_STATEID:
2244                case -NFS4ERR_BAD_STATEID:
2245                case -NFS4ERR_EXPIRED:
2246                        if (calldata->arg.fmode == 0)
2247                                break;
2248                default:
2249                        if (nfs4_async_handle_error(task, server, state) == -EAGAIN)
2250                                rpc_restart_call_prepare(task);
2251        }
2252        nfs_release_seqid(calldata->arg.seqid);
2253        nfs_refresh_inode(calldata->inode, calldata->res.fattr);
2254        dprintk("%s: done, ret = %d!\n", __func__, task->tk_status);
2255}
2256
2257static void nfs4_close_prepare(struct rpc_task *task, void *data)
2258{
2259        struct nfs4_closedata *calldata = data;
2260        struct nfs4_state *state = calldata->state;
2261        struct inode *inode = calldata->inode;
2262        int call_close = 0;
2263
2264        dprintk("%s: begin!\n", __func__);
2265        if (nfs_wait_on_sequence(calldata->arg.seqid, task) != 0)
2266                return;
2267
2268        task->tk_msg.rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_OPEN_DOWNGRADE];
2269        calldata->arg.fmode = FMODE_READ|FMODE_WRITE;
2270        spin_lock(&state->owner->so_lock);
2271        /* Calculate the change in open mode */
2272        if (state->n_rdwr == 0) {
2273                if (state->n_rdonly == 0) {
2274                        call_close |= test_bit(NFS_O_RDONLY_STATE, &state->flags);
2275                        call_close |= test_bit(NFS_O_RDWR_STATE, &state->flags);
2276                        calldata->arg.fmode &= ~FMODE_READ;
2277                }
2278                if (state->n_wronly == 0) {
2279                        call_close |= test_bit(NFS_O_WRONLY_STATE, &state->flags);
2280                        call_close |= test_bit(NFS_O_RDWR_STATE, &state->flags);
2281                        calldata->arg.fmode &= ~FMODE_WRITE;
2282                }
2283        }
2284        spin_unlock(&state->owner->so_lock);
2285
2286        if (!call_close) {
2287                /* Note: exit _without_ calling nfs4_close_done */
2288                task->tk_action = NULL;
2289                goto out;
2290        }
2291
2292        if (calldata->arg.fmode == 0) {
2293                task->tk_msg.rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_CLOSE];
2294                if (calldata->roc &&
2295                    pnfs_roc_drain(inode, &calldata->roc_barrier, task))
2296                        goto out;
2297        }
2298
2299        nfs_fattr_init(calldata->res.fattr);
2300        calldata->timestamp = jiffies;
2301        if (nfs4_setup_sequence(NFS_SERVER(inode),
2302                                &calldata->arg.seq_args,
2303                                &calldata->res.seq_res,
2304                                task) != 0)
2305                nfs_release_seqid(calldata->arg.seqid);
2306        else
2307                rpc_call_start(task);
2308out:
2309        dprintk("%s: done!\n", __func__);
2310}
2311
2312static const struct rpc_call_ops nfs4_close_ops = {
2313        .rpc_call_prepare = nfs4_close_prepare,
2314        .rpc_call_done = nfs4_close_done,
2315        .rpc_release = nfs4_free_closedata,
2316};
2317
2318/* 
2319 * It is possible for data to be read/written from a mem-mapped file 
2320 * after the sys_close call (which hits the vfs layer as a flush).
2321 * This means that we can't safely call nfsv4 close on a file until 
2322 * the inode is cleared. This in turn means that we are not good
2323 * NFSv4 citizens - we do not indicate to the server to update the file's 
2324 * share state even when we are done with one of the three share 
2325 * stateid's in the inode.
2326 *
2327 * NOTE: Caller must be holding the sp->so_owner semaphore!
2328 */
2329int nfs4_do_close(struct nfs4_state *state, gfp_t gfp_mask, int wait)
2330{
2331        struct nfs_server *server = NFS_SERVER(state->inode);
2332        struct nfs4_closedata *calldata;
2333        struct nfs4_state_owner *sp = state->owner;
2334        struct rpc_task *task;
2335        struct rpc_message msg = {
2336                .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_CLOSE],
2337                .rpc_cred = state->owner->so_cred,
2338        };
2339        struct rpc_task_setup task_setup_data = {
2340                .rpc_client = server->client,
2341                .rpc_message = &msg,
2342                .callback_ops = &nfs4_close_ops,
2343                .workqueue = nfsiod_workqueue,
2344                .flags = RPC_TASK_ASYNC,
2345        };
2346        int status = -ENOMEM;
2347
2348        calldata = kzalloc(sizeof(*calldata), gfp_mask);
2349        if (calldata == NULL)
2350                goto out;
2351        nfs41_init_sequence(&calldata->arg.seq_args, &calldata->res.seq_res, 1);
2352        calldata->inode = state->inode;
2353        calldata->state = state;
2354        calldata->arg.fh = NFS_FH(state->inode);
2355        calldata->arg.stateid = &state->open_stateid;
2356        /* Serialization for the sequence id */
2357        calldata->arg.seqid = nfs_alloc_seqid(&state->owner->so_seqid, gfp_mask);
2358        if (calldata->arg.seqid == NULL)
2359                goto out_free_calldata;
2360        calldata->arg.fmode = 0;
2361        calldata->arg.bitmask = server->cache_consistency_bitmask;
2362        calldata->res.fattr = &calldata->fattr;
2363        calldata->res.seqid = calldata->arg.seqid;
2364        calldata->res.server = server;
2365        calldata->roc = pnfs_roc(state->inode);
2366        nfs_sb_active(calldata->inode->i_sb);
2367
2368        msg.rpc_argp = &calldata->arg;
2369        msg.rpc_resp = &calldata->res;
2370        task_setup_data.callback_data = calldata;
2371        task = rpc_run_task(&task_setup_data);
2372        if (IS_ERR(task))
2373                return PTR_ERR(task);
2374        status = 0;
2375        if (wait)
2376                status = rpc_wait_for_completion_task(task);
2377        rpc_put_task(task);
2378        return status;
2379out_free_calldata:
2380        kfree(calldata);
2381out:
2382        nfs4_put_open_state(state);
2383        nfs4_put_state_owner(sp);
2384        return status;
2385}
2386
2387static struct inode *
2388nfs4_atomic_open(struct inode *dir, struct nfs_open_context *ctx, int open_flags, struct iattr *attr)
2389{
2390        struct nfs4_state *state;
2391
2392        /* Protect against concurrent sillydeletes */
2393        state = nfs4_do_open(dir, ctx->dentry, ctx->mode, open_flags, attr,
2394                             ctx->cred, &ctx->mdsthreshold);
2395        if (IS_ERR(state))
2396                return ERR_CAST(state);
2397        ctx->state = state;
2398        return igrab(state->inode);
2399}
2400
2401static void nfs4_close_context(struct nfs_open_context *ctx, int is_sync)
2402{
2403        if (ctx->state == NULL)
2404                return;
2405        if (is_sync)
2406                nfs4_close_sync(ctx->state, ctx->mode);
2407        else
2408                nfs4_close_state(ctx->state, ctx->mode);
2409}
2410
2411static int _nfs4_server_capabilities(struct nfs_server *server, struct nfs_fh *fhandle)
2412{
2413        struct nfs4_server_caps_arg args = {
2414                .fhandle = fhandle,
2415        };
2416        struct nfs4_server_caps_res res = {};
2417        struct rpc_message msg = {
2418                .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_SERVER_CAPS],
2419                .rpc_argp = &args,
2420                .rpc_resp = &res,
2421        };
2422        int status;
2423
2424        status = nfs4_call_sync(server->client, server, &msg, &args.seq_args, &res.seq_res, 0);
2425        if (status == 0) {
2426                memcpy(server->attr_bitmask, res.attr_bitmask, sizeof(server->attr_bitmask));
2427                server->caps &= ~(NFS_CAP_ACLS|NFS_CAP_HARDLINKS|
2428                                NFS_CAP_SYMLINKS|NFS_CAP_FILEID|
2429                                NFS_CAP_MODE|NFS_CAP_NLINK|NFS_CAP_OWNER|
2430                                NFS_CAP_OWNER_GROUP|NFS_CAP_ATIME|
2431                                NFS_CAP_CTIME|NFS_CAP_MTIME);
2432                if (res.attr_bitmask[0] & FATTR4_WORD0_ACL)
2433                        server->caps |= NFS_CAP_ACLS;
2434                if (res.has_links != 0)
2435                        server->caps |= NFS_CAP_HARDLINKS;
2436                if (res.has_symlinks != 0)
2437                        server->caps |= NFS_CAP_SYMLINKS;
2438                if (res.attr_bitmask[0] & FATTR4_WORD0_FILEID)
2439                        server->caps |= NFS_CAP_FILEID;
2440                if (res.attr_bitmask[1] & FATTR4_WORD1_MODE)
2441                        server->caps |= NFS_CAP_MODE;
2442                if (res.attr_bitmask[1] & FATTR4_WORD1_NUMLINKS)
2443                        server->caps |= NFS_CAP_NLINK;
2444                if (res.attr_bitmask[1] & FATTR4_WORD1_OWNER)
2445                        server->caps |= NFS_CAP_OWNER;
2446                if (res.attr_bitmask[1] & FATTR4_WORD1_OWNER_GROUP)
2447                        server->caps |= NFS_CAP_OWNER_GROUP;
2448                if (res.attr_bitmask[1] & FATTR4_WORD1_TIME_ACCESS)
2449                        server->caps |= NFS_CAP_ATIME;
2450                if (res.attr_bitmask[1] & FATTR4_WORD1_TIME_METADATA)
2451                        server->caps |= NFS_CAP_CTIME;
2452                if (res.attr_bitmask[1] & FATTR4_WORD1_TIME_MODIFY)
2453                        server->caps |= NFS_CAP_MTIME;
2454
2455                memcpy(server->cache_consistency_bitmask, res.attr_bitmask, sizeof(server->cache_consistency_bitmask));
2456                server->cache_consistency_bitmask[0] &= FATTR4_WORD0_CHANGE|FATTR4_WORD0_SIZE;
2457                server->cache_consistency_bitmask[1] &= FATTR4_WORD1_TIME_METADATA|FATTR4_WORD1_TIME_MODIFY;
2458                server->acl_bitmask = res.acl_bitmask;
2459                server->fh_expire_type = res.fh_expire_type;
2460        }
2461
2462        return status;
2463}
2464
2465int nfs4_server_capabilities(struct nfs_server *server, struct nfs_fh *fhandle)
2466{
2467        struct nfs4_exception exception = { };
2468        int err;
2469        do {
2470                err = nfs4_handle_exception(server,
2471                                _nfs4_server_capabilities(server, fhandle),
2472                                &exception);
2473        } while (exception.retry);
2474        return err;
2475}
2476
2477static int _nfs4_lookup_root(struct nfs_server *server, struct nfs_fh *fhandle,
2478                struct nfs_fsinfo *info)
2479{
2480        struct nfs4_lookup_root_arg args = {
2481                .bitmask = nfs4_fattr_bitmap,
2482        };
2483        struct nfs4_lookup_res res = {
2484                .server = server,
2485                .fattr = info->fattr,
2486                .fh = fhandle,
2487        };
2488        struct rpc_message msg = {
2489                .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_LOOKUP_ROOT],
2490                .rpc_argp = &args,
2491                .rpc_resp = &res,
2492        };
2493
2494        nfs_fattr_init(info->fattr);
2495        return nfs4_call_sync(server->client, server, &msg, &args.seq_args, &res.seq_res, 0);
2496}
2497
2498static int nfs4_lookup_root(struct nfs_server *server, struct nfs_fh *fhandle,
2499                struct nfs_fsinfo *info)
2500{
2501        struct nfs4_exception exception = { };
2502        int err;
2503        do {
2504                err = _nfs4_lookup_root(server, fhandle, info);
2505                switch (err) {
2506                case 0:
2507                case -NFS4ERR_WRONGSEC:
2508                        goto out;
2509                default:
2510                        err = nfs4_handle_exception(server, err, &exception);
2511                }
2512        } while (exception.retry);
2513out:
2514        return err;
2515}
2516
2517static int nfs4_lookup_root_sec(struct nfs_server *server, struct nfs_fh *fhandle,
2518                                struct nfs_fsinfo *info, rpc_authflavor_t flavor)
2519{
2520        struct rpc_auth *auth;
2521        int ret;
2522
2523        auth = rpcauth_create(flavor, server->client);
2524        if (IS_ERR(auth)) {
2525                ret = -EIO;
2526                goto out;
2527        }
2528        ret = nfs4_lookup_root(server, fhandle, info);
2529out:
2530        return ret;
2531}
2532
2533static int nfs4_find_root_sec(struct nfs_server *server, struct nfs_fh *fhandle,
2534                              struct nfs_fsinfo *info)
2535{
2536        int i, len, status = 0;
2537        rpc_authflavor_t flav_array[NFS_MAX_SECFLAVORS];
2538
2539        len = rpcauth_list_flavors(flav_array, ARRAY_SIZE(flav_array));
2540        BUG_ON(len < 0);
2541
2542        for (i = 0; i < len; i++) {
2543                /* AUTH_UNIX is the default flavor if none was specified,
2544                 * thus has already been tried. */
2545                if (flav_array[i] == RPC_AUTH_UNIX)
2546                        continue;
2547
2548                status = nfs4_lookup_root_sec(server, fhandle, info, flav_array[i]);
2549                if (status == -NFS4ERR_WRONGSEC || status == -EACCES)
2550                        continue;
2551                break;
2552        }
2553        /*
2554         * -EACCESS could mean that the user doesn't have correct permissions
2555         * to access the mount.  It could also mean that we tried to mount
2556         * with a gss auth flavor, but rpc.gssd isn't running.  Either way,
2557         * existing mount programs don't handle -EACCES very well so it should
2558         * be mapped to -EPERM instead.
2559         */
2560        if (status == -EACCES)
2561                status = -EPERM;
2562        return status;
2563}
2564
2565/*
2566 * get the file handle for the "/" directory on the server
2567 */
2568int nfs4_proc_get_rootfh(struct nfs_server *server, struct nfs_fh *fhandle,
2569                         struct nfs_fsinfo *info)
2570{
2571        int minor_version = server->nfs_client->cl_minorversion;
2572        int status = nfs4_lookup_root(server, fhandle, info);
2573        if ((status == -NFS4ERR_WRONGSEC) && !(server->flags & NFS_MOUNT_SECFLAVOUR))
2574                /*
2575                 * A status of -NFS4ERR_WRONGSEC will be mapped to -EPERM
2576                 * by nfs4_map_errors() as this function exits.
2577                 */
2578                status = nfs_v4_minor_ops[minor_version]->find_root_sec(server, fhandle, info);
2579        if (status == 0)
2580                status = nfs4_server_capabilities(server, fhandle);
2581        if (status == 0)
2582                status = nfs4_do_fsinfo(server, fhandle, info);
2583        return nfs4_map_errors(status);
2584}
2585
2586static int nfs4_proc_get_root(struct nfs_server *server, struct nfs_fh *mntfh,
2587                              struct nfs_fsinfo *info)
2588{
2589        int error;
2590        struct nfs_fattr *fattr = info->fattr;
2591
2592        error = nfs4_server_capabilities(server, mntfh);
2593        if (error < 0) {
2594                dprintk("nfs4_get_root: getcaps error = %d\n", -error);
2595                return error;
2596        }
2597
2598        error = nfs4_proc_getattr(server, mntfh, fattr);
2599        if (error < 0) {
2600                dprintk("nfs4_get_root: getattr error = %d\n", -error);
2601                return error;
2602        }
2603
2604        if (fattr->valid & NFS_ATTR_FATTR_FSID &&
2605            !nfs_fsid_equal(&server->fsid, &fattr->fsid))
2606                memcpy(&server->fsid, &fattr->fsid, sizeof(server->fsid));
2607
2608        return error;
2609}
2610
2611/*
2612 * Get locations and (maybe) other attributes of a referral.
2613 * Note that we'll actually follow the referral later when
2614 * we detect fsid mismatch in inode revalidation
2615 */
2616static int nfs4_get_referral(struct rpc_clnt *client, struct inode *dir,
2617                             const struct qstr *name, struct nfs_fattr *fattr,
2618                             struct nfs_fh *fhandle)
2619{
2620        int status = -ENOMEM;
2621        struct page *page = NULL;
2622        struct nfs4_fs_locations *locations = NULL;
2623
2624        page = alloc_page(GFP_KERNEL);
2625        if (page == NULL)
2626                goto out;
2627        locations = kmalloc(sizeof(struct nfs4_fs_locations), GFP_KERNEL);
2628        if (locations == NULL)
2629                goto out;
2630
2631        status = nfs4_proc_fs_locations(client, dir, name, locations, page);
2632        if (status != 0)
2633                goto out;
2634        /* Make sure server returned a different fsid for the referral */
2635        if (nfs_fsid_equal(&NFS_SERVER(dir)->fsid, &locations->fattr.fsid)) {
2636                dprintk("%s: server did not return a different fsid for"
2637                        " a referral at %s\n", __func__, name->name);
2638                status = -EIO;
2639                goto out;
2640        }
2641        /* Fixup attributes for the nfs_lookup() call to nfs_fhget() */
2642        nfs_fixup_referral_attributes(&locations->fattr);
2643
2644        /* replace the lookup nfs_fattr with the locations nfs_fattr */
2645        memcpy(fattr, &locations->fattr, sizeof(struct nfs_fattr));
2646        memset(fhandle, 0, sizeof(struct nfs_fh));
2647out:
2648        if (page)
2649                __free_page(page);
2650        kfree(locations);
2651        return status;
2652}
2653
2654static int _nfs4_proc_getattr(struct nfs_server *server, struct nfs_fh *fhandle, struct nfs_fattr *fattr)
2655{
2656        struct nfs4_getattr_arg args = {
2657                .fh = fhandle,
2658                .bitmask = server->attr_bitmask,
2659        };
2660        struct nfs4_getattr_res res = {
2661                .fattr = fattr,
2662                .server = server,
2663        };
2664        struct rpc_message msg = {
2665                .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_GETATTR],
2666                .rpc_argp = &args,
2667                .rpc_resp = &res,
2668        };
2669        
2670        nfs_fattr_init(fattr);
2671        return nfs4_call_sync(server->client, server, &msg, &args.seq_args, &res.seq_res, 0);
2672}
2673
2674static int nfs4_proc_getattr(struct nfs_server *server, struct nfs_fh *fhandle, struct nfs_fattr *fattr)
2675{
2676        struct nfs4_exception exception = { };
2677        int err;
2678        do {
2679                err = nfs4_handle_exception(server,
2680                                _nfs4_proc_getattr(server, fhandle, fattr),
2681                                &exception);
2682        } while (exception.retry);
2683        return err;
2684}
2685
2686/* 
2687 * The file is not closed if it is opened due to the a request to change
2688 * the size of the file. The open call will not be needed once the
2689 * VFS layer lookup-intents are implemented.
2690 *
2691 * Close is called when the inode is destroyed.
2692 * If we haven't opened the file for O_WRONLY, we
2693 * need to in the size_change case to obtain a stateid.
2694 *
2695 * Got race?
2696 * Because OPEN is always done by name in nfsv4, it is
2697 * possible that we opened a different file by the same
2698 * name.  We can recognize this race condition, but we
2699 * can't do anything about it besides returning an error.
2700 *
2701 * This will be fixed with VFS changes (lookup-intent).
2702 */
2703static int
2704nfs4_proc_setattr(struct dentry *dentry, struct nfs_fattr *fattr,
2705                  struct iattr *sattr)
2706{
2707        struct inode *inode = dentry->d_inode;
2708        struct rpc_cred *cred = NULL;
2709        struct nfs4_state *state = NULL;
2710        int status;
2711
2712        if (pnfs_ld_layoutret_on_setattr(inode))
2713                pnfs_return_layout(inode);
2714
2715        nfs_fattr_init(fattr);
2716        
2717        /* Deal with open(O_TRUNC) */
2718        if (sattr->ia_valid & ATTR_OPEN)
2719                sattr->ia_valid &= ~(ATTR_MTIME|ATTR_CTIME|ATTR_OPEN);
2720
2721        /* Optimization: if the end result is no change, don't RPC */
2722        if ((sattr->ia_valid & ~(ATTR_FILE)) == 0)
2723                return 0;
2724
2725        /* Search for an existing open(O_WRITE) file */
2726        if (sattr->ia_valid & ATTR_FILE) {
2727                struct nfs_open_context *ctx;
2728
2729                ctx = nfs_file_open_context(sattr->ia_file);
2730                if (ctx) {
2731                        cred = ctx->cred;
2732                        state = ctx->state;
2733                }
2734        }
2735
2736        status = nfs4_do_setattr(inode, cred, fattr, sattr, state);
2737        if (status == 0)
2738                nfs_setattr_update_inode(inode, sattr);
2739        return status;
2740}
2741
2742static int _nfs4_proc_lookup(struct rpc_clnt *clnt, struct inode *dir,
2743                const struct qstr *name, struct nfs_fh *fhandle,
2744                struct nfs_fattr *fattr)
2745{
2746        struct nfs_server *server = NFS_SERVER(dir);
2747        int                    status;
2748        struct nfs4_lookup_arg args = {
2749                .bitmask = server->attr_bitmask,
2750                .dir_fh = NFS_FH(dir),
2751                .name = name,
2752        };
2753        struct nfs4_lookup_res res = {
2754                .server = server,
2755                .fattr = fattr,
2756                .fh = fhandle,
2757        };
2758        struct rpc_message msg = {
2759                .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_LOOKUP],
2760                .rpc_argp = &args,
2761                .rpc_resp = &res,
2762        };
2763
2764        nfs_fattr_init(fattr);
2765
2766        dprintk("NFS call  lookup %s\n", name->name);
2767        status = nfs4_call_sync(clnt, server, &msg, &args.seq_args, &res.seq_res, 0);
2768        dprintk("NFS reply lookup: %d\n", status);
2769        return status;
2770}
2771
2772static void nfs_fixup_secinfo_attributes(struct nfs_fattr *fattr)
2773{
2774        fattr->valid |= NFS_ATTR_FATTR_TYPE | NFS_ATTR_FATTR_MODE |
2775                NFS_ATTR_FATTR_NLINK | NFS_ATTR_FATTR_MOUNTPOINT;
2776        fattr->mode = S_IFDIR | S_IRUGO | S_IXUGO;
2777        fattr->nlink = 2;
2778}
2779
2780static int nfs4_proc_lookup_common(struct rpc_clnt **clnt, struct inode *dir,
2781                                   struct qstr *name, struct nfs_fh *fhandle,
2782                                   struct nfs_fattr *fattr)
2783{
2784        struct nfs4_exception exception = { };
2785        struct rpc_clnt *client = *clnt;
2786        int err;
2787        do {
2788                err = _nfs4_proc_lookup(client, dir, name, fhandle, fattr);
2789                switch (err) {
2790                case -NFS4ERR_BADNAME:
2791                        err = -ENOENT;
2792                        goto out;
2793                case -NFS4ERR_MOVED:
2794                        err = nfs4_get_referral(client, dir, name, fattr, fhandle);
2795                        goto out;
2796                case -NFS4ERR_WRONGSEC:
2797                        err = -EPERM;
2798                        if (client != *clnt)
2799                                goto out;
2800
2801                        client = nfs4_create_sec_client(client, dir, name);
2802                        if (IS_ERR(client))
2803                                return PTR_ERR(client);
2804
2805                        exception.retry = 1;
2806                        break;
2807                default:
2808                        err = nfs4_handle_exception(NFS_SERVER(dir), err, &exception);
2809                }
2810        } while (exception.retry);
2811
2812out:
2813        if (err == 0)
2814                *clnt = client;
2815        else if (client != *clnt)
2816                rpc_shutdown_client(client);
2817
2818        return err;
2819}
2820
2821static int nfs4_proc_lookup(struct inode *dir, struct qstr *name,
2822                            struct nfs_fh *fhandle, struct nfs_fattr *fattr)
2823{
2824        int status;
2825        struct rpc_clnt *client = NFS_CLIENT(dir);
2826
2827        status = nfs4_proc_lookup_common(&client, dir, name, fhandle, fattr);
2828        if (client != NFS_CLIENT(dir)) {
2829                rpc_shutdown_client(client);
2830                nfs_fixup_secinfo_attributes(fattr);
2831        }
2832        return status;
2833}
2834
2835struct rpc_clnt *
2836nfs4_proc_lookup_mountpoint(struct inode *dir, struct qstr *name,
2837                            struct nfs_fh *fhandle, struct nfs_fattr *fattr)
2838{
2839        int status;
2840        struct rpc_clnt *client = rpc_clone_client(NFS_CLIENT(dir));
2841
2842        status = nfs4_proc_lookup_common(&client, dir, name, fhandle, fattr);
2843        if (status < 0) {
2844                rpc_shutdown_client(client);
2845                return ERR_PTR(status);
2846        }
2847        return client;
2848}
2849
2850static int _nfs4_proc_access(struct inode *inode, struct nfs_access_entry *entry)
2851{
2852        struct nfs_server *server = NFS_SERVER(inode);
2853        struct nfs4_accessargs args = {
2854                .fh = NFS_FH(inode),
2855                .bitmask = server->cache_consistency_bitmask,
2856        };
2857        struct nfs4_accessres res = {
2858                .server = server,
2859        };
2860        struct rpc_message msg = {
2861                .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_ACCESS],
2862                .rpc_argp = &args,
2863                .rpc_resp = &res,
2864                .rpc_cred = entry->cred,
2865        };
2866        int mode = entry->mask;
2867        int status;
2868
2869        /*
2870         * Determine which access bits we want to ask for...
2871         */
2872        if (mode & MAY_READ)
2873                args.access |= NFS4_ACCESS_READ;
2874        if (S_ISDIR(inode->i_mode)) {
2875                if (mode & MAY_WRITE)
2876                        args.access |= NFS4_ACCESS_MODIFY | NFS4_ACCESS_EXTEND | NFS4_ACCESS_DELETE;
2877                if (mode & MAY_EXEC)
2878                        args.access |= NFS4_ACCESS_LOOKUP;
2879        } else {
2880                if (mode & MAY_WRITE)
2881                        args.access |= NFS4_ACCESS_MODIFY | NFS4_ACCESS_EXTEND;
2882                if (mode & MAY_EXEC)
2883                        args.access |= NFS4_ACCESS_EXECUTE;
2884        }
2885
2886        res.fattr = nfs_alloc_fattr();
2887        if (res.fattr == NULL)
2888                return -ENOMEM;
2889
2890        status = nfs4_call_sync(server->client, server, &msg, &args.seq_args, &res.seq_res, 0);
2891        if (!status) {
2892                nfs_access_set_mask(entry, res.access);
2893                nfs_refresh_inode(inode, res.fattr);
2894        }
2895        nfs_free_fattr(res.fattr);
2896        return status;
2897}
2898
2899static int nfs4_proc_access(struct inode *inode, struct nfs_access_entry *entry)
2900{
2901        struct nfs4_exception exception = { };
2902        int err;
2903        do {
2904                err = nfs4_handle_exception(NFS_SERVER(inode),
2905                                _nfs4_proc_access(inode, entry),
2906                                &exception);
2907        } while (exception.retry);
2908        return err;
2909}
2910
2911/*
2912 * TODO: For the time being, we don't try to get any attributes
2913 * along with any of the zero-copy operations READ, READDIR,
2914 * READLINK, WRITE.
2915 *
2916 * In the case of the first three, we want to put the GETATTR
2917 * after the read-type operation -- this is because it is hard
2918 * to predict the length of a GETATTR response in v4, and thus
2919 * align the READ data correctly.  This means that the GETATTR
2920 * may end up partially falling into the page cache, and we should
2921 * shift it into the 'tail' of the xdr_buf before processing.
2922 * To do this efficiently, we need to know the total length
2923 * of data received, which doesn't seem to be available outside
2924 * of the RPC layer.
2925 *
2926 * In the case of WRITE, we also want to put the GETATTR after
2927 * the operation -- in this case because we want to make sure
2928 * we get the post-operation mtime and size.
2929 *
2930 * Both of these changes to the XDR layer would in fact be quite
2931 * minor, but I decided to leave them for a subsequent patch.
2932 */
2933static int _nfs4_proc_readlink(struct inode *inode, struct page *page,
2934                unsigned int pgbase, unsigned int pglen)
2935{
2936        struct nfs4_readlink args = {
2937                .fh       = NFS_FH(inode),
2938                .pgbase   = pgbase,
2939                .pglen    = pglen,
2940                .pages    = &page,
2941        };
2942        struct nfs4_readlink_res res;
2943        struct rpc_message msg = {
2944                .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_READLINK],
2945                .rpc_argp = &args,
2946                .rpc_resp = &res,
2947        };
2948
2949        return nfs4_call_sync(NFS_SERVER(inode)->client, NFS_SERVER(inode), &msg, &args.seq_args, &res.seq_res, 0);
2950}
2951
2952static int nfs4_proc_readlink(struct inode *inode, struct page *page,
2953                unsigned int pgbase, unsigned int pglen)
2954{
2955        struct nfs4_exception exception = { };
2956        int err;
2957        do {
2958                err = nfs4_handle_exception(NFS_SERVER(inode),
2959                                _nfs4_proc_readlink(inode, page, pgbase, pglen),
2960                                &exception);
2961        } while (exception.retry);
2962        return err;
2963}
2964
2965/*
2966 * This is just for mknod.  open(O_CREAT) will always do ->open_context().
2967 */
2968static int
2969nfs4_proc_create(struct inode *dir, struct dentry *dentry, struct iattr *sattr,
2970                 int flags)
2971{
2972        struct nfs_open_context *ctx;
2973        struct nfs4_state *state;
2974        int status = 0;
2975
2976        ctx = alloc_nfs_open_context(dentry, FMODE_READ);
2977        if (IS_ERR(ctx))
2978                return PTR_ERR(ctx);
2979
2980        sattr->ia_mode &= ~current_umask();
2981        state = nfs4_do_open(dir, dentry, ctx->mode,
2982                        flags, sattr, ctx->cred,
2983                        &ctx->mdsthreshold);
2984        d_drop(dentry);
2985        if (IS_ERR(state)) {
2986                status = PTR_ERR(state);
2987                goto out;
2988        }
2989        d_add(dentry, igrab(state->inode));
2990        nfs_set_verifier(dentry, nfs_save_change_attribute(dir));
2991        ctx->state = state;
2992out:
2993        put_nfs_open_context(ctx);
2994        return status;
2995}
2996
2997static int _nfs4_proc_remove(struct inode *dir, struct qstr *name)
2998{
2999        struct nfs_server *server = NFS_SERVER(dir);
3000        struct nfs_removeargs args = {
3001                .fh = NFS_FH(dir),
3002                .name = *name,
3003        };
3004        struct nfs_removeres res = {
3005                .server = server,
3006        };
3007        struct rpc_message msg = {
3008                .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_REMOVE],
3009                .rpc_argp = &args,
3010                .rpc_resp = &res,
3011        };
3012        int status;
3013
3014        status = nfs4_call_sync(server->client, server, &msg, &args.seq_args, &res.seq_res, 1);
3015        if (status == 0)
3016                update_changeattr(dir, &res.cinfo);
3017        return status;
3018}
3019
3020static int nfs4_proc_remove(struct inode *dir, struct qstr *name)
3021{
3022        struct nfs4_exception exception = { };
3023        int err;
3024        do {
3025                err = nfs4_handle_exception(NFS_SERVER(dir),
3026                                _nfs4_proc_remove(dir, name),
3027                                &exception);
3028        } while (exception.retry);
3029        return err;
3030}
3031
3032static void nfs4_proc_unlink_setup(struct rpc_message *msg, struct inode *dir)
3033{
3034        struct nfs_server *server = NFS_SERVER(dir);
3035        struct nfs_removeargs *args = msg->rpc_argp;
3036        struct nfs_removeres *res = msg->rpc_resp;
3037
3038        res->server = server;
3039        msg->rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_REMOVE];
3040        nfs41_init_sequence(&args->seq_args, &res->seq_res, 1);
3041}
3042
3043static void nfs4_proc_unlink_rpc_prepare(struct rpc_task *task, struct nfs_unlinkdata *data)
3044{
3045        if (nfs4_setup_sequence(NFS_SERVER(data->dir),
3046                                &data->args.seq_args,
3047                                &data->res.seq_res,
3048                                task))
3049                return;
3050        rpc_call_start(task);
3051}
3052
3053static int nfs4_proc_unlink_done(struct rpc_task *task, struct inode *dir)
3054{
3055        struct nfs_removeres *res = task->tk_msg.rpc_resp;
3056
3057        if (!nfs4_sequence_done(task, &res->seq_res))
3058                return 0;
3059        if (nfs4_async_handle_error(task, res->server, NULL) == -EAGAIN)
3060                return 0;
3061        update_changeattr(dir, &res->cinfo);
3062        return 1;
3063}
3064
3065static void nfs4_proc_rename_setup(struct rpc_message *msg, struct inode *dir)
3066{
3067        struct nfs_server *server = NFS_SERVER(dir);
3068        struct nfs_renameargs *arg = msg->rpc_argp;
3069        struct nfs_renameres *res = msg->rpc_resp;
3070
3071        msg->rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_RENAME];
3072        res->server = server;
3073        nfs41_init_sequence(&arg->seq_args, &res->seq_res, 1);
3074}
3075
3076static void nfs4_proc_rename_rpc_prepare(struct rpc_task *task, struct nfs_renamedata *data)
3077{
3078        if (nfs4_setup_sequence(NFS_SERVER(data->old_dir),
3079                                &data->args.seq_args,
3080                                &data->res.seq_res,
3081                                task))
3082                return;
3083        rpc_call_start(task);
3084}
3085
3086static int nfs4_proc_rename_done(struct rpc_task *task, struct inode *old_dir,
3087                                 struct inode *new_dir)
3088{
3089        struct nfs_renameres *res = task->tk_msg.rpc_resp;
3090
3091        if (!nfs4_sequence_done(task, &res->seq_res))
3092                return 0;
3093        if (nfs4_async_handle_error(task, res->server, NULL) == -EAGAIN)
3094                return 0;
3095
3096        update_changeattr(old_dir, &res->old_cinfo);
3097        update_changeattr(new_dir, &res->new_cinfo);
3098        return 1;
3099}
3100
3101static int _nfs4_proc_rename(struct inode *old_dir, struct qstr *old_name,
3102                struct inode *new_dir, struct qstr *new_name)
3103{
3104        struct nfs_server *server = NFS_SERVER(old_dir);
3105        struct nfs_renameargs arg = {
3106                .old_dir = NFS_FH(old_dir),
3107                .new_dir = NFS_FH(new_dir),
3108                .old_name = old_name,
3109                .new_name = new_name,
3110        };
3111        struct nfs_renameres res = {
3112                .server = server,
3113        };
3114        struct rpc_message msg = {
3115                .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_RENAME],
3116                .rpc_argp = &arg,
3117                .rpc_resp = &res,
3118        };
3119        int status = -ENOMEM;
3120        
3121        status = nfs4_call_sync(server->client, server, &msg, &arg.seq_args, &res.seq_res, 1);
3122        if (!status) {
3123                update_changeattr(old_dir, &res.old_cinfo);
3124                update_changeattr(new_dir, &res.new_cinfo);
3125        }
3126        return status;
3127}
3128
3129static int nfs4_proc_rename(struct inode *old_dir, struct qstr *old_name,
3130                struct inode *new_dir, struct qstr *new_name)
3131{
3132        struct nfs4_exception exception = { };
3133        int err;
3134        do {
3135                err = nfs4_handle_exception(NFS_SERVER(old_dir),
3136                                _nfs4_proc_rename(old_dir, old_name,
3137                                        new_dir, new_name),
3138                                &exception);
3139        } while (exception.retry);
3140        return err;
3141}
3142
3143static int _nfs4_proc_link(struct inode *inode, struct inode *dir, struct qstr *name)
3144{
3145        struct nfs_server *server = NFS_SERVER(inode);
3146        struct nfs4_link_arg arg = {
3147                .fh     = NFS_FH(inode),
3148                .dir_fh = NFS_FH(dir),
3149                .name   = name,
3150                .bitmask = server->attr_bitmask,
3151        };
3152        struct nfs4_link_res res = {
3153                .server = server,
3154        };
3155        struct rpc_message msg = {
3156                .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_LINK],
3157                .rpc_argp = &arg,
3158                .rpc_resp = &res,
3159        };
3160        int status = -ENOMEM;
3161
3162        res.fattr = nfs_alloc_fattr();
3163        if (res.fattr == NULL)
3164                goto out;
3165
3166        status = nfs4_call_sync(server->client, server, &msg, &arg.seq_args, &res.seq_res, 1);
3167        if (!status) {
3168                update_changeattr(dir, &res.cinfo);
3169                nfs_post_op_update_inode(inode, res.fattr);
3170        }
3171out:
3172        nfs_free_fattr(res.fattr);
3173        return status;
3174}
3175
3176static int nfs4_proc_link(struct inode *inode, struct inode *dir, struct qstr *name)
3177{
3178        struct nfs4_exception exception = { };
3179        int err;
3180        do {
3181                err = nfs4_handle_exception(NFS_SERVER(inode),
3182                                _nfs4_proc_link(inode, dir, name),
3183                                &exception);
3184        } while (exception.retry);
3185        return err;
3186}
3187
3188struct nfs4_createdata {
3189        struct rpc_message msg;
3190        struct nfs4_create_arg arg;
3191        struct nfs4_create_res res;
3192        struct nfs_fh fh;
3193        struct nfs_fattr fattr;
3194};
3195
3196static struct nfs4_createdata *nfs4_alloc_createdata(struct inode *dir,
3197                struct qstr *name, struct iattr *sattr, u32 ftype)
3198{
3199        struct nfs4_createdata *data;
3200
3201        data = kzalloc(sizeof(*data), GFP_KERNEL);
3202        if (data != NULL) {
3203                struct nfs_server *server = NFS_SERVER(dir);
3204
3205                data->msg.rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_CREATE];
3206                data->msg.rpc_argp = &data->arg;
3207                data->msg.rpc_resp = &data->res;
3208                data->arg.dir_fh = NFS_FH(dir);
3209                data->arg.server = server;
3210                data->arg.name = name;
3211                data->arg.attrs = sattr;
3212                data->arg.ftype = ftype;
3213                data->arg.bitmask = server->attr_bitmask;
3214                data->res.server = server;
3215                data->res.fh = &data->fh;
3216                data->res.fattr = &data->fattr;
3217                nfs_fattr_init(data->res.fattr);
3218        }
3219        return data;
3220}
3221
3222static int nfs4_do_create(struct inode *dir, struct dentry *dentry, struct nfs4_createdata *data)
3223{
3224        int status = nfs4_call_sync(NFS_SERVER(dir)->client, NFS_SERVER(dir), &data->msg,
3225                                    &data->arg.seq_args, &data->res.seq_res, 1);
3226        if (status == 0) {
3227                update_changeattr(dir, &data->res.dir_cinfo);
3228                status = nfs_instantiate(dentry, data->res.fh, data->res.fattr);
3229        }
3230        return status;
3231}
3232
3233static void nfs4_free_createdata(struct nfs4_createdata *data)
3234{
3235        kfree(data);
3236}
3237
3238static int _nfs4_proc_symlink(struct inode *dir, struct dentry *dentry,
3239                struct page *page, unsigned int len, struct iattr *sattr)
3240{
3241        struct nfs4_createdata *data;
3242        int status = -ENAMETOOLONG;
3243
3244        if (len > NFS4_MAXPATHLEN)
3245                goto out;
3246
3247        status = -ENOMEM;
3248        data = nfs4_alloc_createdata(dir, &dentry->d_name, sattr, NF4LNK);
3249        if (data == NULL)
3250                goto out;
3251
3252        data->msg.rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_SYMLINK];
3253        data->arg.u.symlink.pages = &page;
3254        data->arg.u.symlink.len = len;
3255        
3256        status = nfs4_do_create(dir, dentry, data);
3257
3258        nfs4_free_createdata(data);
3259out:
3260        return status;
3261}
3262
3263static int nfs4_proc_symlink(struct inode *dir, struct dentry *dentry,
3264                struct page *page, unsigned int len, struct iattr *sattr)
3265{
3266        struct nfs4_exception exception = { };
3267        int err;
3268        do {
3269                err = nfs4_handle_exception(NFS_SERVER(dir),
3270                                _nfs4_proc_symlink(dir, dentry, page,
3271                                                        len, sattr),
3272                                &exception);
3273        } while (exception.retry);
3274        return err;
3275}
3276
3277static int _nfs4_proc_mkdir(struct inode *dir, struct dentry *dentry,
3278                struct iattr *sattr)
3279{
3280        struct nfs4_createdata *data;
3281        int status = -ENOMEM;
3282
3283        data = nfs4_alloc_createdata(dir, &dentry->d_name, sattr, NF4DIR);
3284        if (data == NULL)
3285                goto out;
3286
3287        status = nfs4_do_create(dir, dentry, data);
3288
3289        nfs4_free_createdata(data);
3290out:
3291        return status;
3292}
3293
3294static int nfs4_proc_mkdir(struct inode *dir, struct dentry *dentry,
3295                struct iattr *sattr)
3296{
3297        struct nfs4_exception exception = { };
3298        int err;
3299
3300        sattr->ia_mode &= ~current_umask();
3301        do {
3302                err = nfs4_handle_exception(NFS_SERVER(dir),
3303                                _nfs4_proc_mkdir(dir, dentry, sattr),
3304                                &exception);
3305        } while (exception.retry);
3306        return err;
3307}
3308
3309static int _nfs4_proc_readdir(struct dentry *dentry, struct rpc_cred *cred,
3310                u64 cookie, struct page **pages, unsigned int count, int plus)
3311{
3312        struct inode            *dir = dentry->d_inode;
3313        struct nfs4_readdir_arg args = {
3314                .fh = NFS_FH(dir),
3315                .pages = pages,
3316                .pgbase = 0,
3317                .count = count,
3318                .bitmask = NFS_SERVER(dentry->d_inode)->attr_bitmask,
3319                .plus = plus,
3320        };
3321        struct nfs4_readdir_res res;
3322        struct rpc_message msg = {
3323                .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_READDIR],
3324                .rpc_argp = &args,
3325                .rpc_resp = &res,
3326                .rpc_cred = cred,
3327        };
3328        int                     status;
3329
3330        dprintk("%s: dentry = %s/%s, cookie = %Lu\n", __func__,
3331                        dentry->d_parent->d_name.name,
3332                        dentry->d_name.name,
3333                        (unsigned long long)cookie);
3334        nfs4_setup_readdir(cookie, NFS_I(dir)->cookieverf, dentry, &args);
3335        res.pgbase = args.pgbase;
3336        status = nfs4_call_sync(NFS_SERVER(dir)->client, NFS_SERVER(dir), &msg, &args.seq_args, &res.seq_res, 0);
3337        if (status >= 0) {
3338                memcpy(NFS_I(dir)->cookieverf, res.verifier.data, NFS4_VERIFIER_SIZE);
3339                status += args.pgbase;
3340        }
3341
3342        nfs_invalidate_atime(dir);
3343
3344        dprintk("%s: returns %d\n", __func__, status);
3345        return status;
3346}
3347
3348static int nfs4_proc_readdir(struct dentry *dentry, struct rpc_cred *cred,
3349                u64 cookie, struct page **pages, unsigned int count, int plus)
3350{
3351        struct nfs4_exception exception = { };
3352        int err;
3353        do {
3354                err = nfs4_handle_exception(NFS_SERVER(dentry->d_inode),
3355                                _nfs4_proc_readdir(dentry, cred, cookie,
3356                                        pages, count, plus),
3357                                &exception);
3358        } while (exception.retry);
3359        return err;
3360}
3361
3362static int _nfs4_proc_mknod(struct inode *dir, struct dentry *dentry,
3363                struct iattr *sattr, dev_t rdev)
3364{
3365        struct nfs4_createdata *data;
3366        int mode = sattr->ia_mode;
3367        int status = -ENOMEM;
3368
3369        BUG_ON(!(sattr->ia_valid & ATTR_MODE));
3370        BUG_ON(!S_ISFIFO(mode) && !S_ISBLK(mode) && !S_ISCHR(mode) && !S_ISSOCK(mode));
3371
3372        data = nfs4_alloc_createdata(dir, &dentry->d_name, sattr, NF4SOCK);
3373        if (data == NULL)
3374                goto out;
3375
3376        if (S_ISFIFO(mode))
3377                data->arg.ftype = NF4FIFO;
3378        else if (S_ISBLK(mode)) {
3379                data->arg.ftype = NF4BLK;
3380                data->arg.u.device.specdata1 = MAJOR(rdev);
3381                data->arg.u.device.specdata2 = MINOR(rdev);
3382        }
3383        else if (S_ISCHR(mode)) {
3384                data->arg.ftype = NF4CHR;
3385                data->arg.u.device.specdata1 = MAJOR(rdev);
3386                data->arg.u.device.specdata2 = MINOR(rdev);
3387        }
3388        
3389        status = nfs4_do_create(dir, dentry, data);
3390
3391        nfs4_free_createdata(data);
3392out:
3393        return status;
3394}
3395
3396static int nfs4_proc_mknod(struct inode *dir, struct dentry *dentry,
3397                struct iattr *sattr, dev_t rdev)
3398{
3399        struct nfs4_exception exception = { };
3400        int err;
3401
3402        sattr->ia_mode &= ~current_umask();
3403        do {
3404                err = nfs4_handle_exception(NFS_SERVER(dir),
3405                                _nfs4_proc_mknod(dir, dentry, sattr, rdev),
3406                                &exception);
3407        } while (exception.retry);
3408        return err;
3409}
3410
3411static int _nfs4_proc_statfs(struct nfs_server *server, struct nfs_fh *fhandle,
3412                 struct nfs_fsstat *fsstat)
3413{
3414        struct nfs4_statfs_arg args = {
3415                .fh = fhandle,
3416                .bitmask = server->attr_bitmask,
3417        };
3418        struct nfs4_statfs_res res = {
3419                .fsstat = fsstat,
3420        };
3421        struct rpc_message msg = {
3422                .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_STATFS],
3423                .rpc_argp = &args,
3424                .rpc_resp = &res,
3425        };
3426
3427        nfs_fattr_init(fsstat->fattr);
3428        return  nfs4_call_sync(server->client, server, &msg, &args.seq_args, &res.seq_res, 0);
3429}
3430
3431static int nfs4_proc_statfs(struct nfs_server *server, struct nfs_fh *fhandle, struct nfs_fsstat *fsstat)
3432{
3433        struct nfs4_exception exception = { };
3434        int err;
3435        do {
3436                err = nfs4_handle_exception(server,
3437                                _nfs4_proc_statfs(server, fhandle, fsstat),
3438                                &exception);
3439        } while (exception.retry);
3440        return err;
3441}
3442
3443static int _nfs4_do_fsinfo(struct nfs_server *server, struct nfs_fh *fhandle,
3444                struct nfs_fsinfo *fsinfo)
3445{
3446        struct nfs4_fsinfo_arg args = {
3447                .fh = fhandle,
3448                .bitmask = server->attr_bitmask,
3449        };
3450        struct nfs4_fsinfo_res res = {
3451                .fsinfo = fsinfo,
3452        };
3453        struct rpc_message msg = {
3454                .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_FSINFO],
3455                .rpc_argp = &args,
3456                .rpc_resp = &res,
3457        };
3458
3459        return nfs4_call_sync(server->client, server, &msg, &args.seq_args, &res.seq_res, 0);
3460}
3461
3462static int nfs4_do_fsinfo(struct nfs_server *server, struct nfs_fh *fhandle, struct nfs_fsinfo *fsinfo)
3463{
3464        struct nfs4_exception exception = { };
3465        int err;
3466
3467        do {
3468                err = nfs4_handle_exception(server,
3469                                _nfs4_do_fsinfo(server, fhandle, fsinfo),
3470                                &exception);
3471        } while (exception.retry);
3472        return err;
3473}
3474
3475static int nfs4_proc_fsinfo(struct nfs_server *server, struct nfs_fh *fhandle, struct nfs_fsinfo *fsinfo)
3476{
3477        int error;
3478
3479        nfs_fattr_init(fsinfo->fattr);
3480        error = nfs4_do_fsinfo(server, fhandle, fsinfo);
3481        if (error == 0) {
3482                /* block layout checks this! */
3483                server->pnfs_blksize = fsinfo->blksize;
3484                set_pnfs_layoutdriver(server, fhandle, fsinfo->layouttype);
3485        }
3486
3487        return error;
3488}
3489
3490static int _nfs4_proc_pathconf(struct nfs_server *server, struct nfs_fh *fhandle,
3491                struct nfs_pathconf *pathconf)
3492{
3493        struct nfs4_pathconf_arg args = {
3494                .fh = fhandle,
3495                .bitmask = server->attr_bitmask,
3496        };
3497        struct nfs4_pathconf_res res = {
3498                .pathconf = pathconf,
3499        };
3500        struct rpc_message msg = {
3501                .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_PATHCONF],
3502                .rpc_argp = &args,
3503                .rpc_resp = &res,
3504        };
3505
3506        /* None of the pathconf attributes are mandatory to implement */
3507        if ((args.bitmask[0] & nfs4_pathconf_bitmap[0]) == 0) {
3508                memset(pathconf, 0, sizeof(*pathconf));
3509                return 0;
3510        }
3511
3512        nfs_fattr_init(pathconf->fattr);
3513        return nfs4_call_sync(server->client, server, &msg, &args.seq_args, &res.seq_res, 0);
3514}
3515
3516static int nfs4_proc_pathconf(struct nfs_server *server, struct nfs_fh *fhandle,
3517                struct nfs_pathconf *pathconf)
3518{
3519        struct nfs4_exception exception = { };
3520        int err;
3521
3522        do {
3523                err = nfs4_handle_exception(server,
3524                                _nfs4_proc_pathconf(server, fhandle, pathconf),
3525                                &exception);
3526        } while (exception.retry);
3527        return err;
3528}
3529
3530void __nfs4_read_done_cb(struct nfs_read_data *data)
3531{
3532        nfs_invalidate_atime(data->header->inode);
3533}
3534
3535static int nfs4_read_done_cb(struct rpc_task *task, struct nfs_read_data *data)
3536{
3537        struct nfs_server *server = NFS_SERVER(data->header->inode);
3538
3539        if (nfs4_async_handle_error(task, server, data->args.context->state) == -EAGAIN) {
3540                rpc_restart_call_prepare(task);
3541                return -EAGAIN;
3542        }
3543
3544        __nfs4_read_done_cb(data);
3545        if (task->tk_status > 0)
3546                renew_lease(server, data->timestamp);
3547        return 0;
3548}
3549
3550static int nfs4_read_done(struct rpc_task *task, struct nfs_read_data *data)
3551{
3552
3553        dprintk("--> %s\n", __func__);
3554
3555        if (!nfs4_sequence_done(task, &data->res.seq_res))
3556                return -EAGAIN;
3557
3558        return data->read_done_cb ? data->read_done_cb(task, data) :
3559                                    nfs4_read_done_cb(task, data);
3560}
3561
3562static void nfs4_proc_read_setup(struct nfs_read_data *data, struct rpc_message *msg)
3563{
3564        data->timestamp   = jiffies;
3565        data->read_done_cb = nfs4_read_done_cb;
3566        msg->rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_READ];
3567        nfs41_init_sequence(&data->args.seq_args, &data->res.seq_res, 0);
3568}
3569
3570static void nfs4_proc_read_rpc_prepare(struct rpc_task *task, struct nfs_read_data *data)
3571{
3572        if (nfs4_setup_sequence(NFS_SERVER(data->header->inode),
3573                                &data->args.seq_args,
3574                                &data->res.seq_res,
3575                                task))
3576                return;
3577        rpc_call_start(task);
3578}
3579
3580static int nfs4_write_done_cb(struct rpc_task *task, struct nfs_write_data *data)
3581{
3582        struct inode *inode = data->header->inode;
3583        
3584        if (nfs4_async_handle_error(task, NFS_SERVER(inode), data->args.context->state) == -EAGAIN) {
3585                rpc_restart_call_prepare(task);
3586                return -EAGAIN;
3587        }
3588        if (task->tk_status >= 0) {
3589                renew_lease(NFS_SERVER(inode), data->timestamp);
3590                nfs_post_op_update_inode_force_wcc(inode, &data->fattr);
3591        }
3592        return 0;
3593}
3594
3595static int nfs4_write_done(struct rpc_task *task, struct nfs_write_data *data)
3596{
3597        if (!nfs4_sequence_done(task, &data->res.seq_res))
3598                return -EAGAIN;
3599        return data->write_done_cb ? data->write_done_cb(task, data) :
3600                nfs4_write_done_cb(task, data);
3601}
3602
3603static
3604bool nfs4_write_need_cache_consistency_data(const struct nfs_write_data *data)
3605{
3606        const struct nfs_pgio_header *hdr = data->header;
3607
3608        /* Don't request attributes for pNFS or O_DIRECT writes */
3609        if (data->ds_clp != NULL || hdr->dreq != NULL)
3610                return false;
3611        /* Otherwise, request attributes if and only if we don't hold
3612         * a delegation
3613         */
3614        return nfs4_have_delegation(hdr->inode, FMODE_READ) == 0;
3615}
3616
3617static void nfs4_proc_write_setup(struct nfs_write_data *data, struct rpc_message *msg)
3618{
3619        struct nfs_server *server = NFS_SERVER(data->header->inode);
3620
3621        if (!nfs4_write_need_cache_consistency_data(data)) {
3622                data->args.bitmask = NULL;
3623                data->res.fattr = NULL;
3624        } else
3625                data->args.bitmask = server->cache_consistency_bitmask;
3626
3627        if (!data->write_done_cb)
3628                data->write_done_cb = nfs4_write_done_cb;
3629        data->res.server = server;
3630        data->timestamp   = jiffies;
3631
3632        msg->rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_WRITE];
3633        nfs41_init_sequence(&data->args.seq_args, &data->res.seq_res, 1);
3634}
3635
3636static void nfs4_proc_write_rpc_prepare(struct rpc_task *task, struct nfs_write_data *data)
3637{
3638        if (nfs4_setup_sequence(NFS_SERVER(data->header->inode),
3639                                &data->args.seq_args,
3640                                &data->res.seq_res,
3641                                task))
3642                return;
3643        rpc_call_start(task);
3644}
3645
3646static void nfs4_proc_commit_rpc_prepare(struct rpc_task *task, struct nfs_commit_data *data)
3647{
3648        if (nfs4_setup_sequence(NFS_SERVER(data->inode),
3649                                &data->args.seq_args,
3650                                &data->res.seq_res,
3651                                task))
3652                return;
3653        rpc_call_start(task);
3654}
3655
3656static int nfs4_commit_done_cb(struct rpc_task *task, struct nfs_commit_data *data)
3657{
3658        struct inode *inode = data->inode;
3659
3660        if (nfs4_async_handle_error(task, NFS_SERVER(inode), NULL) == -EAGAIN) {
3661                rpc_restart_call_prepare(task);
3662                return -EAGAIN;
3663        }
3664        return 0;
3665}
3666
3667static int nfs4_commit_done(struct rpc_task *task, struct nfs_commit_data *data)
3668{
3669        if (!nfs4_sequence_done(task, &data->res.seq_res))
3670                return -EAGAIN;
3671        return data->commit_done_cb(task, data);
3672}
3673
3674static void nfs4_proc_commit_setup(struct nfs_commit_data *data, struct rpc_message *msg)
3675{
3676        struct nfs_server *server = NFS_SERVER(data->inode);
3677
3678        if (data->commit_done_cb == NULL)
3679                data->commit_done_cb = nfs4_commit_done_cb;
3680        data->res.server = server;
3681        msg->rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_COMMIT];
3682        nfs41_init_sequence(&data->args.seq_args, &data->res.seq_res, 1);
3683}
3684
3685struct nfs4_renewdata {
3686        struct nfs_client       *client;
3687        unsigned long           timestamp;
3688};
3689
3690/*
3691 * nfs4_proc_async_renew(): This is not one of the nfs_rpc_ops; it is a special
3692 * standalone procedure for queueing an asynchronous RENEW.
3693 */
3694static void nfs4_renew_release(void *calldata)
3695{
3696        struct nfs4_renewdata *data = calldata;
3697        struct nfs_client *clp = data->client;
3698
3699        if (atomic_read(&clp->cl_count) > 1)
3700                nfs4_schedule_state_renewal(clp);
3701        nfs_put_client(clp);
3702        kfree(data);
3703}
3704
3705static void nfs4_renew_done(struct rpc_task *task, void *calldata)
3706{
3707        struct nfs4_renewdata *data = calldata;
3708        struct nfs_client *clp = data->client;
3709        unsigned long timestamp = data->timestamp;
3710
3711        if (task->tk_status < 0) {
3712                /* Unless we're shutting down, schedule state recovery! */
3713                if (test_bit(NFS_CS_RENEWD, &clp->cl_res_state) == 0)
3714                        return;
3715                if (task->tk_status != NFS4ERR_CB_PATH_DOWN) {
3716                        nfs4_schedule_lease_recovery(clp);
3717                        return;
3718                }
3719                nfs4_schedule_path_down_recovery(clp);
3720        }
3721        do_renew_lease(clp, timestamp);
3722}
3723
3724static const struct rpc_call_ops nfs4_renew_ops = {
3725        .rpc_call_done = nfs4_renew_done,
3726        .rpc_release = nfs4_renew_release,
3727};
3728
3729static int nfs4_proc_async_renew(struct nfs_client *clp, struct rpc_cred *cred, unsigned renew_flags)
3730{
3731        struct rpc_message msg = {
3732                .rpc_proc       = &nfs4_procedures[NFSPROC4_CLNT_RENEW],
3733                .rpc_argp       = clp,
3734                .rpc_cred       = cred,
3735        };
3736        struct nfs4_renewdata *data;
3737
3738        if (renew_flags == 0)
3739                return 0;
3740        if (!atomic_inc_not_zero(&clp->cl_count))
3741                return -EIO;
3742        data = kmalloc(sizeof(*data), GFP_NOFS);
3743        if (data == NULL)
3744                return -ENOMEM;
3745        data->client = clp;
3746        data->timestamp = jiffies;
3747        return rpc_call_async(clp->cl_rpcclient, &msg, RPC_TASK_SOFT,
3748                        &nfs4_renew_ops, data);
3749}
3750
3751static int nfs4_proc_renew(struct nfs_client *clp, struct rpc_cred *cred)
3752{
3753        struct rpc_message msg = {
3754                .rpc_proc       = &nfs4_procedures[NFSPROC4_CLNT_RENEW],
3755                .rpc_argp       = clp,
3756                .rpc_cred       = cred,
3757        };
3758        unsigned long now = jiffies;
3759        int status;
3760
3761        status = rpc_call_sync(clp->cl_rpcclient, &msg, 0);
3762        if (status < 0)
3763                return status;
3764        do_renew_lease(clp, now);
3765        return 0;
3766}
3767
3768static inline int nfs4_server_supports_acls(struct nfs_server *server)
3769{
3770        return (server->caps & NFS_CAP_ACLS)
3771                && (server->acl_bitmask & ACL4_SUPPORT_ALLOW_ACL)
3772                && (server->acl_bitmask & ACL4_SUPPORT_DENY_ACL);
3773}
3774
3775/* Assuming that XATTR_SIZE_MAX is a multiple of PAGE_SIZE, and that
3776 * it's OK to put sizeof(void) * (XATTR_SIZE_MAX/PAGE_SIZE) bytes on
3777 * the stack.
3778 */
3779#define NFS4ACL_MAXPAGES DIV_ROUND_UP(XATTR_SIZE_MAX, PAGE_SIZE)
3780
3781static int buf_to_pages_noslab(const void *buf, size_t buflen,
3782                struct page **pages, unsigned int *pgbase)
3783{
3784        struct page *newpage, **spages;
3785        int rc = 0;
3786        size_t len;
3787        spages = pages;
3788
3789        do {
3790                len = min_t(size_t, PAGE_SIZE, buflen);
3791                newpage = alloc_page(GFP_KERNEL);
3792
3793                if (newpage == NULL)
3794                        goto unwind;
3795                memcpy(page_address(newpage), buf, len);
3796                buf += len;
3797                buflen -= len;
3798                *pages++ = newpage;
3799                rc++;
3800        } while (buflen != 0);
3801
3802        return rc;
3803
3804unwind:
3805        for(; rc > 0; rc--)
3806                __free_page(spages[rc-1]);
3807        return -ENOMEM;
3808}
3809
3810struct nfs4_cached_acl {
3811        int cached;
3812        size_t len;
3813        char data[0];
3814};
3815
3816static void nfs4_set_cached_acl(struct inode *inode, struct nfs4_cached_acl *acl)
3817{
3818        struct nfs_inode *nfsi = NFS_I(inode);
3819
3820        spin_lock(&inode->i_lock);
3821        kfree(nfsi->nfs4_acl);
3822        nfsi->nfs4_acl = acl;
3823        spin_unlock(&inode->i_lock);
3824}
3825
3826static void nfs4_zap_acl_attr(struct inode *inode)
3827{
3828        nfs4_set_cached_acl(inode, NULL);
3829}
3830
3831static inline ssize_t nfs4_read_cached_acl(struct inode *inode, char *buf, size_t buflen)
3832{
3833        struct nfs_inode *nfsi = NFS_I(inode);
3834        struct nfs4_cached_acl *acl;
3835        int ret = -ENOENT;
3836
3837        spin_lock(&inode->i_lock);
3838        acl = nfsi->nfs4_acl;
3839        if (acl == NULL)
3840                goto out;
3841        if (buf == NULL) /* user is just asking for length */
3842                goto out_len;
3843        if (acl->cached == 0)
3844                goto out;
3845        ret = -ERANGE; /* see getxattr(2) man page */
3846        if (acl->len > buflen)
3847                goto out;
3848        memcpy(buf, acl->data, acl->len);
3849out_len:
3850        ret = acl->len;
3851out:
3852        spin_unlock(&inode->i_lock);
3853        return ret;
3854}
3855
3856static void nfs4_write_cached_acl(struct inode *inode, struct page **pages, size_t pgbase, size_t acl_len)
3857{
3858        struct nfs4_cached_acl *acl;
3859        size_t buflen = sizeof(*acl) + acl_len;
3860
3861        if (buflen <= PAGE_SIZE) {
3862                acl = kmalloc(buflen, GFP_KERNEL);
3863                if (acl == NULL)
3864                        goto out;
3865                acl->cached = 1;
3866                _copy_from_pages(acl->data, pages, pgbase, acl_len);
3867        } else {
3868                acl = kmalloc(sizeof(*acl), GFP_KERNEL);
3869                if (acl == NULL)
3870                        goto out;
3871                acl->cached = 0;
3872        }
3873        acl->len = acl_len;
3874out:
3875        nfs4_set_cached_acl(inode, acl);
3876}
3877
3878/*
3879 * The getxattr API returns the required buffer length when called with a
3880 * NULL buf. The NFSv4 acl tool then calls getxattr again after allocating
3881 * the required buf.  On a NULL buf, we send a page of data to the server
3882 * guessing that the ACL request can be serviced by a page. If so, we cache
3883 * up to the page of ACL data, and the 2nd call to getxattr is serviced by
3884 * the cache. If not so, we throw away the page, and cache the required
3885 * length. The next getxattr call will then produce another round trip to
3886 * the server, this time with the input buf of the required size.
3887 */
3888static ssize_t __nfs4_get_acl_uncached(struct inode *inode, void *buf, size_t buflen)
3889{
3890        struct page *pages[NFS4ACL_MAXPAGES] = {NULL, };
3891        struct nfs_getaclargs args = {
3892                .fh = NFS_FH(inode),
3893                .acl_pages = pages,
3894                .acl_len = buflen,
3895        };
3896        struct nfs_getaclres res = {
3897                .acl_len = buflen,
3898        };
3899        struct rpc_message msg = {
3900                .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_GETACL],
3901                .rpc_argp = &args,
3902                .rpc_resp = &res,
3903        };
3904        unsigned int npages = DIV_ROUND_UP(buflen, PAGE_SIZE);
3905        int ret = -ENOMEM, i;
3906
3907        /* As long as we're doing a round trip to the server anyway,
3908         * let's be prepared for a page of acl data. */
3909        if (npages == 0)
3910                npages = 1;
3911        if (npages > ARRAY_SIZE(pages))
3912                return -ERANGE;
3913
3914        for (i = 0; i < npages; i++) {
3915                pages[i] = alloc_page(GFP_KERNEL);
3916                if (!pages[i])
3917                        goto out_free;
3918        }
3919
3920        /* for decoding across pages */
3921        res.acl_scratch = alloc_page(GFP_KERNEL);
3922        if (!res.acl_scratch)
3923                goto out_free;
3924
3925        args.acl_len = npages * PAGE_SIZE;
3926        args.acl_pgbase = 0;
3927
3928        dprintk("%s  buf %p buflen %zu npages %d args.acl_len %zu\n",
3929                __func__, buf, buflen, npages, args.acl_len);
3930        ret = nfs4_call_sync(NFS_SERVER(inode)->client, NFS_SERVER(inode),
3931                             &msg, &args.seq_args, &res.seq_res, 0);
3932        if (ret)
3933                goto out_free;
3934
3935        /* Handle the case where the passed-in buffer is too short */
3936        if (res.acl_flags & NFS4_ACL_TRUNC) {
3937                /* Did the user only issue a request for the acl length? */
3938                if (buf == NULL)
3939                        goto out_ok;
3940                ret = -ERANGE;
3941                goto out_free;
3942        }
3943        nfs4_write_cached_acl(inode, pages, res.acl_data_offset, res.acl_len);
3944        if (buf) {
3945                if (res.acl_len > buflen) {
3946                        ret = -ERANGE;
3947                        goto out_free;
3948                }
3949                _copy_from_pages(buf, pages, res.acl_data_offset, res.acl_len);
3950        }
3951out_ok:
3952        ret = res.acl_len;
3953out_free:
3954        for (i = 0; i < npages; i++)
3955                if (pages[i])
3956                        __free_page(pages[i]);
3957        if (res.acl_scratch)
3958                __free_page(res.acl_scratch);
3959        return ret;
3960}
3961
3962static ssize_t nfs4_get_acl_uncached(struct inode *inode, void *buf, size_t buflen)
3963{
3964        struct nfs4_exception exception = { };
3965        ssize_t ret;
3966        do {
3967                ret = __nfs4_get_acl_uncached(inode, buf, buflen);
3968                if (ret >= 0)
3969                        break;
3970                ret = nfs4_handle_exception(NFS_SERVER(inode), ret, &exception);
3971        } while (exception.retry);
3972        return ret;
3973}
3974
3975static ssize_t nfs4_proc_get_acl(struct inode *inode, void *buf, size_t buflen)
3976{
3977        struct nfs_server *server = NFS_SERVER(inode);
3978        int ret;
3979
3980        if (!nfs4_server_supports_acls(server))
3981                return -EOPNOTSUPP;
3982        ret = nfs_revalidate_inode(server, inode);
3983        if (ret < 0)
3984                return ret;
3985        if (NFS_I(inode)->cache_validity & NFS_INO_INVALID_ACL)
3986                nfs_zap_acl_cache(inode);
3987        ret = nfs4_read_cached_acl(inode, buf, buflen);
3988        if (ret != -ENOENT)
3989                /* -ENOENT is returned if there is no ACL or if there is an ACL
3990                 * but no cached acl data, just the acl length */
3991                return ret;
3992        return nfs4_get_acl_uncached(inode, buf, buflen);
3993}
3994
3995static int __nfs4_proc_set_acl(struct inode *inode, const void *buf, size_t buflen)
3996{
3997        struct nfs_server *server = NFS_SERVER(inode);
3998        struct page *pages[NFS4ACL_MAXPAGES];
3999        struct nfs_setaclargs arg = {
4000                .fh             = NFS_FH(inode),
4001                .acl_pages      = pages,
4002                .acl_len        = buflen,
4003        };
4004        struct nfs_setaclres res;
4005        struct rpc_message msg = {
4006                .rpc_proc       = &nfs4_procedures[NFSPROC4_CLNT_SETACL],
4007                .rpc_argp       = &arg,
4008                .rpc_resp       = &res,
4009        };
4010        unsigned int npages = DIV_ROUND_UP(buflen, PAGE_SIZE);
4011        int ret, i;
4012
4013        if (!nfs4_server_supports_acls(server))
4014                return -EOPNOTSUPP;
4015        if (npages > ARRAY_SIZE(pages))
4016                return -ERANGE;
4017        i = buf_to_pages_noslab(buf, buflen, arg.acl_pages, &arg.acl_pgbase);
4018        if (i < 0)
4019                return i;
4020        nfs4_inode_return_delegation(inode);
4021        ret = nfs4_call_sync(server->client, server, &msg, &arg.seq_args, &res.seq_res, 1);
4022
4023        /*
4024         * Free each page after tx, so the only ref left is
4025         * held by the network stack
4026         */
4027        for (; i > 0; i--)
4028                put_page(pages[i-1]);
4029
4030        /*
4031         * Acl update can result in inode attribute update.
4032         * so mark the attribute cache invalid.
4033         */
4034        spin_lock(&inode->i_lock);
4035        NFS_I(inode)->cache_validity |= NFS_INO_INVALID_ATTR;
4036        spin_unlock(&inode->i_lock);
4037        nfs_access_zap_cache(inode);
4038        nfs_zap_acl_cache(inode);
4039        return ret;
4040}
4041
4042static int nfs4_proc_set_acl(struct inode *inode, const void *buf, size_t buflen)
4043{
4044        struct nfs4_exception exception = { };
4045        int err;
4046        do {
4047                err = nfs4_handle_exception(NFS_SERVER(inode),
4048                                __nfs4_proc_set_acl(inode, buf, buflen),
4049                                &exception);
4050        } while (exception.retry);
4051        return err;
4052}
4053
4054static int
4055nfs4_async_handle_error(struct rpc_task *task, const struct nfs_server *server, struct nfs4_state *state)
4056{
4057        struct nfs_client *clp = server->nfs_client;
4058
4059        if (task->tk_status >= 0)
4060                return 0;
4061        switch(task->tk_status) {
4062                case -NFS4ERR_DELEG_REVOKED:
4063                case -NFS4ERR_ADMIN_REVOKED:
4064                case -NFS4ERR_BAD_STATEID:
4065                        if (state == NULL)
4066                                break;
4067                        nfs_remove_bad_delegation(state->inode);
4068                case -NFS4ERR_OPENMODE:
4069                        if (state == NULL)
4070                                break;
4071                        nfs4_schedule_stateid_recovery(server, state);
4072                        goto wait_on_recovery;
4073                case -NFS4ERR_EXPIRED:
4074                        if (state != NULL)
4075                                nfs4_schedule_stateid_recovery(server, state);
4076                case -NFS4ERR_STALE_STATEID:
4077                case -NFS4ERR_STALE_CLIENTID:
4078                        nfs4_schedule_lease_recovery(clp);
4079                        goto wait_on_recovery;
4080#if defined(CONFIG_NFS_V4_1)
4081                case -NFS4ERR_BADSESSION:
4082                case -NFS4ERR_BADSLOT:
4083                case -NFS4ERR_BAD_HIGH_SLOT:
4084                case -NFS4ERR_DEADSESSION:
4085                case -NFS4ERR_CONN_NOT_BOUND_TO_SESSION:
4086                case -NFS4ERR_SEQ_FALSE_RETRY:
4087                case -NFS4ERR_SEQ_MISORDERED:
4088                        dprintk("%s ERROR %d, Reset session\n", __func__,
4089                                task->tk_status);
4090                        nfs4_schedule_session_recovery(clp->cl_session, task->tk_status);
4091                        task->tk_status = 0;
4092                        return -EAGAIN;
4093#endif /* CONFIG_NFS_V4_1 */
4094                case -NFS4ERR_DELAY:
4095                        nfs_inc_server_stats(server, NFSIOS_DELAY);
4096                case -NFS4ERR_GRACE:
4097                case -EKEYEXPIRED:
4098                        rpc_delay(task, NFS4_POLL_RETRY_MAX);
4099                        task->tk_status = 0;
4100                        return -EAGAIN;
4101                case -NFS4ERR_RETRY_UNCACHED_REP:
4102                case -NFS4ERR_OLD_STATEID:
4103                        task->tk_status = 0;
4104                        return -EAGAIN;
4105        }
4106        task->tk_status = nfs4_map_errors(task->tk_status);
4107        return 0;
4108wait_on_recovery:
4109        rpc_sleep_on(&clp->cl_rpcwaitq, task, NULL);
4110        if (test_bit(NFS4CLNT_MANAGER_RUNNING, &clp->cl_state) == 0)
4111                rpc_wake_up_queued_task(&clp->cl_rpcwaitq, task);
4112        task->tk_status = 0;
4113        return -EAGAIN;
4114}
4115
4116static void nfs4_init_boot_verifier(const struct nfs_client *clp,
4117                                    nfs4_verifier *bootverf)
4118{
4119        __be32 verf[2];
4120
4121        if (test_bit(NFS4CLNT_PURGE_STATE, &clp->cl_state)) {
4122                /* An impossible timestamp guarantees this value
4123                 * will never match a generated boot time. */
4124                verf[0] = 0;
4125                verf[1] = (__be32)(NSEC_PER_SEC + 1);
4126        } else {
4127                struct nfs_net *nn = net_generic(clp->cl_net, nfs_net_id);
4128                verf[0] = (__be32)nn->boot_time.tv_sec;
4129                verf[1] = (__be32)nn->boot_time.tv_nsec;
4130        }
4131        memcpy(bootverf->data, verf, sizeof(bootverf->data));
4132}
4133
4134static unsigned int
4135nfs4_init_nonuniform_client_string(const struct nfs_client *clp,
4136                                   char *buf, size_t len)
4137{
4138        unsigned int result;
4139
4140        rcu_read_lock();
4141        result = scnprintf(buf, len, "Linux NFSv4.0 %s/%s %s",
4142                                clp->cl_ipaddr,
4143                                rpc_peeraddr2str(clp->cl_rpcclient,
4144                                                        RPC_DISPLAY_ADDR),
4145                                rpc_peeraddr2str(clp->cl_rpcclient,
4146                                                        RPC_DISPLAY_PROTO));
4147        rcu_read_unlock();
4148        return result;
4149}
4150
4151static unsigned int
4152nfs4_init_uniform_client_string(const struct nfs_client *clp,
4153                                char *buf, size_t len)
4154{
4155        char *nodename = clp->cl_rpcclient->cl_nodename;
4156
4157        if (nfs4_client_id_uniquifier[0] != '\0')
4158                nodename = nfs4_client_id_uniquifier;
4159        return scnprintf(buf, len, "Linux NFSv%u.%u %s",
4160                                clp->rpc_ops->version, clp->cl_minorversion,
4161                                nodename);
4162}
4163
4164/**
4165 * nfs4_proc_setclientid - Negotiate client ID
4166 * @clp: state data structure
4167 * @program: RPC program for NFSv4 callback service
4168 * @port: IP port number for NFS4 callback service
4169 * @cred: RPC credential to use for this call
4170 * @res: where to place the result
4171 *
4172 * Returns zero, a negative errno, or a negative NFS4ERR status code.
4173 */
4174int nfs4_proc_setclientid(struct nfs_client *clp, u32 program,
4175                unsigned short port, struct rpc_cred *cred,
4176                struct nfs4_setclientid_res *res)
4177{
4178        nfs4_verifier sc_verifier;
4179        struct nfs4_setclientid setclientid = {
4180                .sc_verifier = &sc_verifier,
4181                .sc_prog = program,
4182                .sc_cb_ident = clp->cl_cb_ident,
4183        };
4184        struct rpc_message msg = {
4185                .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_SETCLIENTID],
4186                .rpc_argp = &setclientid,
4187                .rpc_resp = res,
4188                .rpc_cred = cred,
4189        };
4190        int status;
4191
4192        /* nfs_client_id4 */
4193        nfs4_init_boot_verifier(clp, &sc_verifier);
4194        if (test_bit(NFS_CS_MIGRATION, &clp->cl_flags))
4195                setclientid.sc_name_len =
4196                                nfs4_init_uniform_client_string(clp,
4197                                                setclientid.sc_name,
4198                                                sizeof(setclientid.sc_name));
4199        else
4200                setclientid.sc_name_len =
4201                                nfs4_init_nonuniform_client_string(clp,
4202                                                setclientid.sc_name,
4203                                                sizeof(setclientid.sc_name));
4204        /* cb_client4 */
4205        rcu_read_lock();
4206        setclientid.sc_netid_len = scnprintf(setclientid.sc_netid,
4207                                sizeof(setclientid.sc_netid),
4208                                rpc_peeraddr2str(clp->cl_rpcclient,
4209                                                        RPC_DISPLAY_NETID));
4210        rcu_read_unlock();
4211        setclientid.sc_uaddr_len = scnprintf(setclientid.sc_uaddr,
4212                                sizeof(setclientid.sc_uaddr), "%s.%u.%u",
4213                                clp->cl_ipaddr, port >> 8, port & 255);
4214
4215        dprintk("NFS call  setclientid auth=%s, '%.*s'\n",
4216                clp->cl_rpcclient->cl_auth->au_ops->au_name,
4217                setclientid.sc_name_len, setclientid.sc_name);
4218        status = rpc_call_sync(clp->cl_rpcclient, &msg, RPC_TASK_TIMEOUT);
4219        dprintk("NFS reply setclientid: %d\n", status);
4220        return status;
4221}
4222
4223/**
4224 * nfs4_proc_setclientid_confirm - Confirm client ID
4225 * @clp: state data structure
4226 * @res: result of a previous SETCLIENTID
4227 * @cred: RPC credential to use for this call
4228 *
4229 * Returns zero, a negative errno, or a negative NFS4ERR status code.
4230 */
4231int nfs4_proc_setclientid_confirm(struct nfs_client *clp,
4232                struct nfs4_setclientid_res *arg,
4233                struct rpc_cred *cred)
4234{
4235        struct nfs_fsinfo fsinfo;
4236        struct rpc_message msg = {
4237                .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_SETCLIENTID_CONFIRM],
4238                .rpc_argp = arg,
4239                .rpc_resp = &fsinfo,
4240                .rpc_cred = cred,
4241        };
4242        unsigned long now;
4243        int status;
4244
4245        dprintk("NFS call  setclientid_confirm auth=%s, (client ID %llx)\n",
4246                clp->cl_rpcclient->cl_auth->au_ops->au_name,
4247                clp->cl_clientid);
4248        now = jiffies;
4249        status = rpc_call_sync(clp->cl_rpcclient, &msg, RPC_TASK_TIMEOUT);
4250        if (status == 0) {
4251                spin_lock(&clp->cl_lock);
4252                clp->cl_lease_time = fsinfo.lease_time * HZ;
4253                clp->cl_last_renewal = now;
4254                spin_unlock(&clp->cl_lock);
4255        }
4256        dprintk("NFS reply setclientid_confirm: %d\n", status);
4257        return status;
4258}
4259
4260struct nfs4_delegreturndata {
4261        struct nfs4_delegreturnargs args;
4262        struct nfs4_delegreturnres res;
4263        struct nfs_fh fh;
4264        nfs4_stateid stateid;
4265        unsigned long timestamp;
4266        struct nfs_fattr fattr;
4267        int rpc_status;
4268};
4269
4270static void nfs4_delegreturn_done(struct rpc_task *task, void *calldata)
4271{
4272        struct nfs4_delegreturndata *data = calldata;
4273
4274        if (!nfs4_sequence_done(task, &data->res.seq_res))
4275                return;
4276
4277        switch (task->tk_status) {
4278        case -NFS4ERR_STALE_STATEID:
4279        case -NFS4ERR_EXPIRED:
4280        case 0:
4281                renew_lease(data->res.server, data->timestamp);
4282                break;
4283        default:
4284                if (nfs4_async_handle_error(task, data->res.server, NULL) ==
4285                                -EAGAIN) {
4286                        rpc_restart_call_prepare(task);
4287                        return;
4288                }
4289        }
4290        data->rpc_status = task->tk_status;
4291}
4292
4293static void nfs4_delegreturn_release(void *calldata)
4294{
4295        kfree(calldata);
4296}
4297
4298#if defined(CONFIG_NFS_V4_1)
4299static void nfs4_delegreturn_prepare(struct rpc_task *task, void *data)
4300{
4301        struct nfs4_delegreturndata *d_data;
4302
4303        d_data = (struct nfs4_delegreturndata *)data;
4304
4305        if (nfs4_setup_sequence(d_data->res.server,
4306                                &d_data->args.seq_args,
4307                                &d_data->res.seq_res, task))
4308                return;
4309        rpc_call_start(task);
4310}
4311#endif /* CONFIG_NFS_V4_1 */
4312
4313static const struct rpc_call_ops nfs4_delegreturn_ops = {
4314#if defined(CONFIG_NFS_V4_1)
4315        .rpc_call_prepare = nfs4_delegreturn_prepare,
4316#endif /* CONFIG_NFS_V4_1 */
4317        .rpc_call_done = nfs4_delegreturn_done,
4318        .rpc_release = nfs4_delegreturn_release,
4319};
4320
4321static int _nfs4_proc_delegreturn(struct inode *inode, struct rpc_cred *cred, const nfs4_stateid *stateid, int issync)
4322{
4323        struct nfs4_delegreturndata *data;
4324        struct nfs_server *server = NFS_SERVER(inode);
4325        struct rpc_task *task;
4326        struct rpc_message msg = {
4327                .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_DELEGRETURN],
4328                .rpc_cred = cred,
4329        };
4330        struct rpc_task_setup task_setup_data = {
4331                .rpc_client = server->client,
4332                .rpc_message = &msg,
4333                .callback_ops = &nfs4_delegreturn_ops,
4334                .flags = RPC_TASK_ASYNC,
4335        };
4336        int status = 0;
4337
4338        data = kzalloc(sizeof(*data), GFP_NOFS);
4339        if (data == NULL)
4340                return -ENOMEM;
4341        nfs41_init_sequence(&data->args.seq_args, &data->res.seq_res, 1);
4342        data->args.fhandle = &data->fh;
4343        data->args.stateid = &data->stateid;
4344        data->args.bitmask = server->cache_consistency_bitmask;
4345        nfs_copy_fh(&data->fh, NFS_FH(inode));
4346        nfs4_stateid_copy(&data->stateid, stateid);
4347        data->res.fattr = &data->fattr;
4348        data->res.server = server;
4349        nfs_fattr_init(data->res.fattr);
4350        data->timestamp = jiffies;
4351        data->rpc_status = 0;
4352
4353        task_setup_data.callback_data = data;
4354        msg.rpc_argp = &data->args;
4355        msg.rpc_resp = &data->res;
4356        task = rpc_run_task(&task_setup_data);
4357        if (IS_ERR(task))
4358                return PTR_ERR(task);
4359        if (!issync)
4360                goto out;
4361        status = nfs4_wait_for_completion_rpc_task(task);
4362        if (status != 0)
4363                goto out;
4364        status = data->rpc_status;
4365        if (status == 0)
4366                nfs_post_op_update_inode_force_wcc(inode, &data->fattr);
4367        else
4368                nfs_refresh_inode(inode, &data->fattr);
4369out:
4370        rpc_put_task(task);
4371        return status;
4372}
4373
4374int nfs4_proc_delegreturn(struct inode *inode, struct rpc_cred *cred, const nfs4_stateid *stateid, int issync)
4375{
4376        struct nfs_server *server = NFS_SERVER(inode);
4377        struct nfs4_exception exception = { };
4378        int err;
4379        do {
4380                err = _nfs4_proc_delegreturn(inode, cred, stateid, issync);
4381                switch (err) {
4382                        case -NFS4ERR_STALE_STATEID:
4383                        case -NFS4ERR_EXPIRED:
4384                        case 0:
4385                                return 0;
4386                }
4387                err = nfs4_handle_exception(server, err, &exception);
4388        } while (exception.retry);
4389        return err;
4390}
4391
4392#define NFS4_LOCK_MINTIMEOUT (1 * HZ)
4393#define NFS4_LOCK_MAXTIMEOUT (30 * HZ)
4394
4395/* 
4396 * sleep, with exponential backoff, and retry the LOCK operation. 
4397 */
4398static unsigned long
4399nfs4_set_lock_task_retry(unsigned long timeout)
4400{
4401        freezable_schedule_timeout_killable(timeout);
4402        timeout <<= 1;
4403        if (timeout > NFS4_LOCK_MAXTIMEOUT)
4404                return NFS4_LOCK_MAXTIMEOUT;
4405        return timeout;
4406}
4407
4408static int _nfs4_proc_getlk(struct nfs4_state *state, int cmd, struct file_lock *request)
4409{
4410        struct inode *inode = state->inode;
4411        struct nfs_server *server = NFS_SERVER(inode);
4412        struct nfs_client *clp = server->nfs_client;
4413        struct nfs_lockt_args arg = {
4414                .fh = NFS_FH(inode),
4415                .fl = request,
4416        };
4417        struct nfs_lockt_res res = {
4418                .denied = request,
4419        };
4420        struct rpc_message msg = {
4421                .rpc_proc       = &nfs4_procedures[NFSPROC4_CLNT_LOCKT],
4422                .rpc_argp       = &arg,
4423                .rpc_resp       = &res,
4424                .rpc_cred       = state->owner->so_cred,
4425        };
4426        struct nfs4_lock_state *lsp;
4427        int status;
4428
4429        arg.lock_owner.clientid = clp->cl_clientid;
4430        status = nfs4_set_lock_state(state, request);
4431        if (status != 0)
4432                goto out;
4433        lsp = request->fl_u.nfs4_fl.owner;
4434        arg.lock_owner.id = lsp->ls_seqid.owner_id;
4435        arg.lock_owner.s_dev = server->s_dev;
4436        status = nfs4_call_sync(server->client, server, &msg, &arg.seq_args, &res.seq_res, 1);
4437        switch (status) {
4438                case 0:
4439                        request->fl_type = F_UNLCK;
4440                        break;
4441                case -NFS4ERR_DENIED:
4442                        status = 0;
4443        }
4444        request->fl_ops->fl_release_private(request);
4445out:
4446        return status;
4447}
4448
4449static int nfs4_proc_getlk(struct nfs4_state *state, int cmd, struct file_lock *request)
4450{
4451        struct nfs4_exception exception = { };
4452        int err;
4453
4454        do {
4455                err = nfs4_handle_exception(NFS_SERVER(state->inode),
4456                                _nfs4_proc_getlk(state, cmd, request),
4457                                &exception);
4458        } while (exception.retry);
4459        return err;
4460}
4461
4462static int do_vfs_lock(struct file *file, struct file_lock *fl)
4463{
4464        int res = 0;
4465        switch (fl->fl_flags & (FL_POSIX|FL_FLOCK)) {
4466                case FL_POSIX:
4467                        res = posix_lock_file_wait(file, fl);
4468                        break;
4469                case FL_FLOCK:
4470                        res = flock_lock_file_wait(file, fl);
4471                        break;
4472                default:
4473                        BUG();
4474        }
4475        return res;
4476}
4477
4478struct nfs4_unlockdata {
4479        struct nfs_locku_args arg;
4480        struct nfs_locku_res res;
4481        struct nfs4_lock_state *lsp;
4482        struct nfs_open_context *ctx;
4483        struct file_lock fl;
4484        const struct nfs_server *server;
4485        unsigned long timestamp;
4486};
4487
4488static struct nfs4_unlockdata *nfs4_alloc_unlockdata(struct file_lock *fl,
4489                struct nfs_open_context *ctx,
4490                struct nfs4_lock_state *lsp,
4491                struct nfs_seqid *seqid)
4492{
4493        struct nfs4_unlockdata *p;
4494        struct inode *inode = lsp->ls_state->inode;
4495
4496        p = kzalloc(sizeof(*p), GFP_NOFS);
4497        if (p == NULL)
4498                return NULL;
4499        p->arg.fh = NFS_FH(inode);
4500        p->arg.fl = &p->fl;
4501        p->arg.seqid = seqid;
4502        p->res.seqid = seqid;
4503        p->arg.stateid = &lsp->ls_stateid;
4504        p->lsp = lsp;
4505        atomic_inc(&lsp->ls_count);
4506        /* Ensure we don't close file until we're done freeing locks! */
4507        p->ctx = get_nfs_open_context(ctx);
4508        memcpy(&p->fl, fl, sizeof(p->fl));
4509        p->server = NFS_SERVER(inode);
4510        return p;
4511}
4512
4513static void nfs4_locku_release_calldata(void *data)
4514{
4515        struct nfs4_unlockdata *calldata = data;
4516        nfs_free_seqid(calldata->arg.seqid);
4517        nfs4_put_lock_state(calldata->lsp);
4518        put_nfs_open_context(calldata->ctx);
4519        kfree(calldata);
4520}
4521
4522static void nfs4_locku_done(struct rpc_task *task, void *data)
4523{
4524        struct nfs4_unlockdata *calldata = data;
4525
4526        if (!nfs4_sequence_done(task, &calldata->res.seq_res))
4527                return;
4528        switch (task->tk_status) {
4529                case 0:
4530                        nfs4_stateid_copy(&calldata->lsp->ls_stateid,
4531                                        &calldata->res.stateid);
4532                        renew_lease(calldata->server, calldata->timestamp);
4533                        break;
4534                case -NFS4ERR_BAD_STATEID:
4535                case -NFS4ERR_OLD_STATEID:
4536                case -NFS4ERR_STALE_STATEID:
4537                case -NFS4ERR_EXPIRED:
4538                        break;
4539                default:
4540                        if (nfs4_async_handle_error(task, calldata->server, NULL) == -EAGAIN)
4541                                rpc_restart_call_prepare(task);
4542        }
4543        nfs_release_seqid(calldata->arg.seqid);
4544}
4545
4546static void nfs4_locku_prepare(struct rpc_task *task, void *data)
4547{
4548        struct nfs4_unlockdata *calldata = data;
4549
4550        if (nfs_wait_on_sequence(calldata->arg.seqid, task) != 0)
4551                return;
4552        if (test_bit(NFS_LOCK_INITIALIZED, &calldata->lsp->ls_flags) == 0) {
4553                /* Note: exit _without_ running nfs4_locku_done */
4554                task->tk_action = NULL;
4555                return;
4556        }
4557        calldata->timestamp = jiffies;
4558        if (nfs4_setup_sequence(calldata->server,
4559                                &calldata->arg.seq_args,
4560                                &calldata->res.seq_res,
4561                                task) != 0)
4562                nfs_release_seqid(calldata->arg.seqid);
4563        else
4564                rpc_call_start(task);
4565}
4566
4567static const struct rpc_call_ops nfs4_locku_ops = {
4568        .rpc_call_prepare = nfs4_locku_prepare,
4569        .rpc_call_done = nfs4_locku_done,
4570        .rpc_release = nfs4_locku_release_calldata,
4571};
4572
4573static struct rpc_task *nfs4_do_unlck(struct file_lock *fl,
4574                struct nfs_open_context *ctx,
4575                struct nfs4_lock_state *lsp,
4576                struct nfs_seqid *seqid)
4577{
4578        struct nfs4_unlockdata *data;
4579        struct rpc_message msg = {
4580                .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_LOCKU],
4581                .rpc_cred = ctx->cred,
4582        };
4583        struct rpc_task_setup task_setup_data = {
4584                .rpc_client = NFS_CLIENT(lsp->ls_state->inode),
4585                .rpc_message = &msg,
4586                .callback_ops = &nfs4_locku_ops,
4587                .workqueue = nfsiod_workqueue,
4588                .flags = RPC_TASK_ASYNC,
4589        };
4590
4591        /* Ensure this is an unlock - when canceling a lock, the
4592         * canceled lock is passed in, and it won't be an unlock.
4593         */
4594        fl->fl_type = F_UNLCK;
4595
4596        data = nfs4_alloc_unlockdata(fl, ctx, lsp, seqid);
4597        if (data == NULL) {
4598                nfs_free_seqid(seqid);
4599                return ERR_PTR(-ENOMEM);
4600        }
4601
4602        nfs41_init_sequence(&data->arg.seq_args, &data->res.seq_res, 1);
4603        msg.rpc_argp = &data->arg;
4604        msg.rpc_resp = &data->res;
4605        task_setup_data.callback_data = data;
4606        return rpc_run_task(&task_setup_data);
4607}
4608
4609static int nfs4_proc_unlck(struct nfs4_state *state, int cmd, struct file_lock *request)
4610{
4611        struct nfs_inode *nfsi = NFS_I(state->inode);
4612        struct nfs_seqid *seqid;
4613        struct nfs4_lock_state *lsp;
4614        struct rpc_task *task;
4615        int status = 0;
4616        unsigned char fl_flags = request->fl_flags;
4617
4618        status = nfs4_set_lock_state(state, request);
4619        /* Unlock _before_ we do the RPC call */
4620        request->fl_flags |= FL_EXISTS;
4621        down_read(&nfsi->rwsem);
4622        if (do_vfs_lock(request->fl_file, request) == -ENOENT) {
4623                up_read(&nfsi->rwsem);
4624                goto out;
4625        }
4626        up_read(&nfsi->rwsem);
4627        if (status != 0)
4628                goto out;
4629        /* Is this a delegated lock? */
4630        if (test_bit(NFS_DELEGATED_STATE, &state->flags))
4631                goto out;
4632        lsp = request->fl_u.nfs4_fl.owner;
4633        seqid = nfs_alloc_seqid(&lsp->ls_seqid, GFP_KERNEL);
4634        status = -ENOMEM;
4635        if (seqid == NULL)
4636                goto out;
4637        task = nfs4_do_unlck(request, nfs_file_open_context(request->fl_file), lsp, seqid);
4638        status = PTR_ERR(task);
4639        if (IS_ERR(task))
4640                goto out;
4641        status = nfs4_wait_for_completion_rpc_task(task);
4642        rpc_put_task(task);
4643out:
4644        request->fl_flags = fl_flags;
4645        return status;
4646}
4647
4648struct nfs4_lockdata {
4649        struct nfs_lock_args arg;
4650        struct nfs_lock_res res;
4651        struct nfs4_lock_state *lsp;
4652        struct nfs_open_context *ctx;
4653        struct file_lock fl;
4654        unsigned long timestamp;
4655        int rpc_status;
4656        int cancelled;
4657        struct nfs_server *server;
4658};
4659
4660static struct nfs4_lockdata *nfs4_alloc_lockdata(struct file_lock *fl,
4661                struct nfs_open_context *ctx, struct nfs4_lock_state *lsp,
4662                gfp_t gfp_mask)
4663{
4664        struct nfs4_lockdata *p;
4665        struct inode *inode = lsp->ls_state->inode;
4666        struct nfs_server *server = NFS_SERVER(inode);
4667
4668        p = kzalloc(sizeof(*p), gfp_mask);
4669        if (p == NULL)
4670                return NULL;
4671
4672        p->arg.fh = NFS_FH(inode);
4673        p->arg.fl = &p->fl;
4674        p->arg.open_seqid = nfs_alloc_seqid(&lsp->ls_state->owner->so_seqid, gfp_mask);
4675        if (p->arg.open_seqid == NULL)
4676                goto out_free;
4677        p->arg.lock_seqid = nfs_alloc_seqid(&lsp->ls_seqid, gfp_mask);
4678        if (p->arg.lock_seqid == NULL)
4679                goto out_free_seqid;
4680        p->arg.lock_stateid = &lsp->ls_stateid;
4681        p->arg.lock_owner.clientid = server->nfs_client->cl_clientid;
4682        p->arg.lock_owner.id = lsp->ls_seqid.owner_id;
4683        p->arg.lock_owner.s_dev = server->s_dev;
4684        p->res.lock_seqid = p->arg.lock_seqid;
4685        p->lsp = lsp;
4686        p->server = server;
4687        atomic_inc(&lsp->ls_count);
4688        p->ctx = get_nfs_open_context(ctx);
4689        memcpy(&p->fl, fl, sizeof(p->fl));
4690        return p;
4691out_free_seqid:
4692        nfs_free_seqid(p->arg.open_seqid);
4693out_free:
4694        kfree(p);
4695        return NULL;
4696}
4697
4698static void nfs4_lock_prepare(struct rpc_task *task, void *calldata)
4699{
4700        struct nfs4_lockdata *data = calldata;
4701        struct nfs4_state *state = data->lsp->ls_state;
4702
4703        dprintk("%s: begin!\n", __func__);
4704        if (nfs_wait_on_sequence(data->arg.lock_seqid, task) != 0)
4705                return;
4706        /* Do we need to do an open_to_lock_owner? */
4707        if (!(data->arg.lock_seqid->sequence->flags & NFS_SEQID_CONFIRMED)) {
4708                if (nfs_wait_on_sequence(data->arg.open_seqid, task) != 0)
4709                        goto out_release_lock_seqid;
4710                data->arg.open_stateid = &state->stateid;
4711                data->arg.new_lock_owner = 1;
4712                data->res.open_seqid = data->arg.open_seqid;
4713        } else
4714                data->arg.new_lock_owner = 0;
4715        data->timestamp = jiffies;
4716        if (nfs4_setup_sequence(data->server,
4717                                &data->arg.seq_args,
4718                                &data->res.seq_res,
4719                                task) == 0) {
4720                rpc_call_start(task);
4721                return;
4722        }
4723        nfs_release_seqid(data->arg.open_seqid);
4724out_release_lock_seqid:
4725        nfs_release_seqid(data->arg.lock_seqid);
4726        dprintk("%s: done!, ret = %d\n", __func__, task->tk_status);
4727}
4728
4729static void nfs4_recover_lock_prepare(struct rpc_task *task, void *calldata)
4730{
4731        rpc_task_set_priority(task, RPC_PRIORITY_PRIVILEGED);
4732        nfs4_lock_prepare(task, calldata);
4733}
4734
4735static void nfs4_lock_done(struct rpc_task *task, void *calldata)
4736{
4737        struct nfs4_lockdata *data = calldata;
4738
4739        dprintk("%s: begin!\n", __func__);
4740
4741        if (!nfs4_sequence_done(task, &data->res.seq_res))
4742                return;
4743
4744        data->rpc_status = task->tk_status;
4745        if (data->arg.new_lock_owner != 0) {
4746                if (data->rpc_status == 0)
4747                        nfs_confirm_seqid(&data->lsp->ls_seqid, 0);
4748                else
4749                        goto out;
4750        }
4751        if (data->rpc_status == 0) {
4752                nfs4_stateid_copy(&data->lsp->ls_stateid, &data->res.stateid);
4753                set_bit(NFS_LOCK_INITIALIZED, &data->lsp->ls_flags);
4754                renew_lease(NFS_SERVER(data->ctx->dentry->d_inode), data->timestamp);
4755        }
4756out:
4757        dprintk("%s: done, ret = %d!\n", __func__, data->rpc_status);
4758}
4759
4760static void nfs4_lock_release(void *calldata)
4761{
4762        struct nfs4_lockdata *data = calldata;
4763
4764        dprintk("%s: begin!\n", __func__);
4765        nfs_free_seqid(data->arg.open_seqid);
4766        if (data->cancelled != 0) {
4767                struct rpc_task *task;
4768                task = nfs4_do_unlck(&data->fl, data->ctx, data->lsp,
4769                                data->arg.lock_seqid);
4770                if (!IS_ERR(task))
4771                        rpc_put_task_async(task);
4772                dprintk("%s: cancelling lock!\n", __func__);
4773        } else
4774                nfs_free_seqid(data->arg.lock_seqid);
4775        nfs4_put_lock_state(data->lsp);
4776        put_nfs_open_context(data->ctx);
4777        kfree(data);
4778        dprintk("%s: done!\n", __func__);
4779}
4780
4781static const struct rpc_call_ops nfs4_lock_ops = {
4782        .rpc_call_prepare = nfs4_lock_prepare,
4783        .rpc_call_done = nfs4_lock_done,
4784        .rpc_release = nfs4_lock_release,
4785};
4786
4787static const struct rpc_call_ops nfs4_recover_lock_ops = {
4788        .rpc_call_prepare = nfs4_recover_lock_prepare,
4789        .rpc_call_done = nfs4_lock_done,
4790        .rpc_release = nfs4_lock_release,
4791};
4792
4793static void nfs4_handle_setlk_error(struct nfs_server *server, struct nfs4_lock_state *lsp, int new_lock_owner, int error)
4794{
4795        switch (error) {
4796        case -NFS4ERR_ADMIN_REVOKED:
4797        case -NFS4ERR_BAD_STATEID:
4798                lsp->ls_seqid.flags &= ~NFS_SEQID_CONFIRMED;
4799                if (new_lock_owner != 0 ||
4800                   test_bit(NFS_LOCK_INITIALIZED, &lsp->ls_flags) != 0)
4801                        nfs4_schedule_stateid_recovery(server, lsp->ls_state);
4802                break;
4803        case -NFS4ERR_STALE_STATEID:
4804                lsp->ls_seqid.flags &= ~NFS_SEQID_CONFIRMED;
4805        case -NFS4ERR_EXPIRED:
4806                nfs4_schedule_lease_recovery(server->nfs_client);
4807        };
4808}
4809
4810static int _nfs4_do_setlk(struct nfs4_state *state, int cmd, struct file_lock *fl, int recovery_type)
4811{
4812        struct nfs4_lockdata *data;
4813        struct rpc_task *task;
4814        struct rpc_message msg = {
4815                .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_LOCK],
4816                .rpc_cred = state->owner->so_cred,
4817        };
4818        struct rpc_task_setup task_setup_data = {
4819                .rpc_client = NFS_CLIENT(state->inode),
4820                .rpc_message = &msg,
4821                .callback_ops = &nfs4_lock_ops,
4822                .workqueue = nfsiod_workqueue,
4823                .flags = RPC_TASK_ASYNC,
4824        };
4825        int ret;
4826
4827        dprintk("%s: begin!\n", __func__);
4828        data = nfs4_alloc_lockdata(fl, nfs_file_open_context(fl->fl_file),
4829                        fl->fl_u.nfs4_fl.owner,
4830                        recovery_type == NFS_LOCK_NEW ? GFP_KERNEL : GFP_NOFS);
4831        if (data == NULL)
4832                return -ENOMEM;
4833        if (IS_SETLKW(cmd))
4834                data->arg.block = 1;
4835        if (recovery_type > NFS_LOCK_NEW) {
4836                if (recovery_type == NFS_LOCK_RECLAIM)
4837                        data->arg.reclaim = NFS_LOCK_RECLAIM;
4838                task_setup_data.callback_ops = &nfs4_recover_lock_ops;
4839        }
4840        nfs41_init_sequence(&data->arg.seq_args, &data->res.seq_res, 1);
4841        msg.rpc_argp = &data->arg;
4842        msg.rpc_resp = &data->res;
4843        task_setup_data.callback_data = data;
4844        task = rpc_run_task(&task_setup_data);
4845        if (IS_ERR(task))
4846                return PTR_ERR(task);
4847        ret = nfs4_wait_for_completion_rpc_task(task);
4848        if (ret == 0) {
4849                ret = data->rpc_status;
4850                if (ret)
4851                        nfs4_handle_setlk_error(data->server, data->lsp,
4852                                        data->arg.new_lock_owner, ret);
4853        } else
4854                data->cancelled = 1;
4855        rpc_put_task(task);
4856        dprintk("%s: done, ret = %d!\n", __func__, ret);
4857        return ret;
4858}
4859
4860static int nfs4_lock_reclaim(struct nfs4_state *state, struct file_lock *request)
4861{
4862        struct nfs_server *server = NFS_SERVER(state->inode);
4863        struct nfs4_exception exception = {
4864                .inode = state->inode,
4865        };
4866        int err;
4867
4868        do {
4869                /* Cache the lock if possible... */
4870                if (test_bit(NFS_DELEGATED_STATE, &state->flags) != 0)
4871                        return 0;
4872                err = _nfs4_do_setlk(state, F_SETLK, request, NFS_LOCK_RECLAIM);
4873                if (err != -NFS4ERR_DELAY)
4874                        break;
4875                nfs4_handle_exception(server, err, &exception);
4876        } while (exception.retry);
4877        return err;
4878}
4879
4880static int nfs4_lock_expired(struct nfs4_state *state, struct file_lock *request)
4881{
4882        struct nfs_server *server = NFS_SERVER(state->inode);
4883        struct nfs4_exception exception = {
4884                .inode = state->inode,
4885        };
4886        int err;
4887
4888        err = nfs4_set_lock_state(state, request);
4889        if (err != 0)
4890                return err;
4891        do {
4892                if (test_bit(NFS_DELEGATED_STATE, &state->flags) != 0)
4893                        return 0;
4894                err = _nfs4_do_setlk(state, F_SETLK, request, NFS_LOCK_EXPIRED);
4895                switch (err) {
4896                default:
4897                        goto out;
4898                case -NFS4ERR_GRACE:
4899                case -NFS4ERR_DELAY:
4900                        nfs4_handle_exception(server, err, &exception);
4901                        err = 0;
4902                }
4903        } while (exception.retry);
4904out:
4905        return err;
4906}
4907
4908#if defined(CONFIG_NFS_V4_1)
4909/**
4910 * nfs41_check_expired_locks - possibly free a lock stateid
4911 *
4912 * @state: NFSv4 state for an inode
4913 *
4914 * Returns NFS_OK if recovery for this stateid is now finished.
4915 * Otherwise a negative NFS4ERR value is returned.
4916 */
4917static int nfs41_check_expired_locks(struct nfs4_state *state)
4918{
4919        int status, ret = -NFS4ERR_BAD_STATEID;
4920        struct nfs4_lock_state *lsp;
4921        struct nfs_server *server = NFS_SERVER(state->inode);
4922
4923        list_for_each_entry(lsp, &state->lock_states, ls_locks) {
4924                if (test_bit(NFS_LOCK_INITIALIZED, &lsp->ls_flags)) {
4925                        status = nfs41_test_stateid(server, &lsp->ls_stateid);
4926                        if (status != NFS_OK) {
4927                                /* Free the stateid unless the server
4928                                 * informs us the stateid is unrecognized. */
4929                                if (status != -NFS4ERR_BAD_STATEID)
4930                                        nfs41_free_stateid(server,
4931                                                        &lsp->ls_stateid);
4932                                clear_bit(NFS_LOCK_INITIALIZED, &lsp->ls_flags);
4933                                ret = status;
4934                        }
4935                }
4936        };
4937
4938        return ret;
4939}
4940
4941static int nfs41_lock_expired(struct nfs4_state *state, struct file_lock *request)
4942{
4943        int status = NFS_OK;
4944
4945        if (test_bit(LK_STATE_IN_USE, &state->flags))
4946                status = nfs41_check_expired_locks(state);
4947        if (status != NFS_OK)
4948                status = nfs4_lock_expired(state, request);
4949        return status;
4950}
4951#endif
4952
4953static int _nfs4_proc_setlk(struct nfs4_state *state, int cmd, struct file_lock *request)
4954{
4955        struct nfs_inode *nfsi = NFS_I(state->inode);
4956        unsigned char fl_flags = request->fl_flags;
4957        int status = -ENOLCK;
4958
4959        if ((fl_flags & FL_POSIX) &&
4960                        !test_bit(NFS_STATE_POSIX_LOCKS, &state->flags))
4961                goto out;
4962        /* Is this a delegated open? */
4963        status = nfs4_set_lock_state(state, request);
4964        if (status != 0)
4965                goto out;
4966        request->fl_flags |= FL_ACCESS;
4967        status = do_vfs_lock(request->fl_file, request);
4968        if (status < 0)
4969                goto out;
4970        down_read(&nfsi->rwsem);
4971        if (test_bit(NFS_DELEGATED_STATE, &state->flags)) {
4972                /* Yes: cache locks! */
4973                /* ...but avoid races with delegation recall... */
4974                request->fl_flags = fl_flags & ~FL_SLEEP;
4975                status = do_vfs_lock(request->fl_file, request);
4976                goto out_unlock;
4977        }
4978        status = _nfs4_do_setlk(state, cmd, request, NFS_LOCK_NEW);
4979        if (status != 0)
4980                goto out_unlock;
4981        /* Note: we always want to sleep here! */
4982        request->fl_flags = fl_flags | FL_SLEEP;
4983        if (do_vfs_lock(request->fl_file, request) < 0)
4984                printk(KERN_WARNING "NFS: %s: VFS is out of sync with lock "
4985                        "manager!\n", __func__);
4986out_unlock:
4987        up_read(&nfsi->rwsem);
4988out:
4989        request->fl_flags = fl_flags;
4990        return status;
4991}
4992
4993static int nfs4_proc_setlk(struct nfs4_state *state, int cmd, struct file_lock *request)
4994{
4995        struct nfs4_exception exception = {
4996                .state = state,
4997                .inode = state->inode,
4998        };
4999        int err;
5000
5001        do {
5002                err = _nfs4_proc_setlk(state, cmd, request);
5003                if (err == -NFS4ERR_DENIED)
5004                        err = -EAGAIN;
5005                err = nfs4_handle_exception(NFS_SERVER(state->inode),
5006                                err, &exception);
5007        } while (exception.retry);
5008        return err;
5009}
5010
5011static int
5012nfs4_proc_lock(struct file *filp, int cmd, struct file_lock *request)
5013{
5014        struct nfs_open_context *ctx;
5015        struct nfs4_state *state;
5016        unsigned long timeout = NFS4_LOCK_MINTIMEOUT;
5017        int status;
5018
5019        /* verify open state */
5020        ctx = nfs_file_open_context(filp);
5021        state = ctx->state;
5022
5023        if (request->fl_start < 0 || request->fl_end < 0)
5024                return -EINVAL;
5025
5026        if (IS_GETLK(cmd)) {
5027                if (state != NULL)
5028                        return nfs4_proc_getlk(state, F_GETLK, request);
5029                return 0;
5030        }
5031
5032        if (!(IS_SETLK(cmd) || IS_SETLKW(cmd)))
5033                return -EINVAL;
5034
5035        if (request->fl_type == F_UNLCK) {
5036                if (state != NULL)
5037                        return nfs4_proc_unlck(state, cmd, request);
5038                return 0;
5039        }
5040
5041        if (state == NULL)
5042                return -ENOLCK;
5043        /*
5044         * Don't rely on the VFS having checked the file open mode,
5045         * since it won't do this for flock() locks.
5046         */
5047        switch (request->fl_type) {
5048        case F_RDLCK:
5049                if (!(filp->f_mode & FMODE_READ))
5050                        return -EBADF;
5051                break;
5052        case F_WRLCK:
5053                if (!(filp->f_mode & FMODE_WRITE))
5054                        return -EBADF;
5055        }
5056
5057        do {
5058                status = nfs4_proc_setlk(state, cmd, request);
5059                if ((status != -EAGAIN) || IS_SETLK(cmd))
5060                        break;
5061                timeout = nfs4_set_lock_task_retry(timeout);
5062                status = -ERESTARTSYS;
5063                if (signalled())
5064                        break;
5065        } while(status < 0);
5066        return status;
5067}
5068
5069int nfs4_lock_delegation_recall(struct nfs4_state *state, struct file_lock *fl)
5070{
5071        struct nfs_server *server = NFS_SERVER(state->inode);
5072        struct nfs4_exception exception = { };
5073        int err;
5074
5075        err = nfs4_set_lock_state(state, fl);
5076        if (err != 0)
5077                goto out;
5078        do {
5079                err = _nfs4_do_setlk(state, F_SETLK, fl, NFS_LOCK_NEW);
5080                switch (err) {
5081                        default:
5082                                printk(KERN_ERR "NFS: %s: unhandled error "
5083                                        "%d.\n", __func__, err);
5084                        case 0:
5085                        case -ESTALE:
5086                                goto out;
5087                        case -NFS4ERR_EXPIRED:
5088                                nfs4_schedule_stateid_recovery(server, state);
5089