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