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