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