linux/fs/nfs/nfs4proc.c
<<
>>
Prefs
   1/*
   2 *  fs/nfs/nfs4proc.c
   3 *
   4 *  Client-side procedure declarations for NFSv4.
   5 *
   6 *  Copyright (c) 2002 The Regents of the University of Michigan.
   7 *  All rights reserved.
   8 *
   9 *  Kendrick Smith <kmsmith@umich.edu>
  10 *  Andy Adamson   <andros@umich.edu>
  11 *
  12 *  Redistribution and use in source and binary forms, with or without
  13 *  modification, are permitted provided that the following conditions
  14 *  are met:
  15 *
  16 *  1. Redistributions of source code must retain the above copyright
  17 *     notice, this list of conditions and the following disclaimer.
  18 *  2. Redistributions in binary form must reproduce the above copyright
  19 *     notice, this list of conditions and the following disclaimer in the
  20 *     documentation and/or other materials provided with the distribution.
  21 *  3. Neither the name of the University nor the names of its
  22 *     contributors may be used to endorse or promote products derived
  23 *     from this software without specific prior written permission.
  24 *
  25 *  THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESS OR IMPLIED
  26 *  WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
  27 *  MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
  28 *  DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
  29 *  FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
  30 *  CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
  31 *  SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR
  32 *  BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
  33 *  LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
  34 *  NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
  35 *  SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  36 */
  37
  38#include <linux/mm.h>
  39#include <linux/delay.h>
  40#include <linux/errno.h>
  41#include <linux/string.h>
  42#include <linux/ratelimit.h>
  43#include <linux/printk.h>
  44#include <linux/slab.h>
  45#include <linux/sunrpc/clnt.h>
  46#include <linux/nfs.h>
  47#include <linux/nfs4.h>
  48#include <linux/nfs_fs.h>
  49#include <linux/nfs_page.h>
  50#include <linux/nfs_mount.h>
  51#include <linux/namei.h>
  52#include <linux/mount.h>
  53#include <linux/module.h>
  54#include <linux/nfs_idmap.h>
  55#include <linux/xattr.h>
  56#include <linux/utsname.h>
  57#include <linux/freezer.h>
  58
  59#include "nfs4_fs.h"
  60#include "delegation.h"
  61#include "internal.h"
  62#include "iostat.h"
  63#include "callback.h"
  64#include "pnfs.h"
  65#include "netns.h"
  66#include "nfs4session.h"
  67#include "fscache.h"
  68
  69#define NFSDBG_FACILITY         NFSDBG_PROC
  70
  71#define NFS4_POLL_RETRY_MIN     (HZ/10)
  72#define NFS4_POLL_RETRY_MAX     (15*HZ)
  73
  74struct nfs4_opendata;
  75static int _nfs4_proc_open(struct nfs4_opendata *data);
  76static int _nfs4_recover_proc_open(struct nfs4_opendata *data);
  77static int nfs4_do_fsinfo(struct nfs_server *, struct nfs_fh *, struct nfs_fsinfo *);
  78static int nfs4_async_handle_error(struct rpc_task *, const struct nfs_server *, struct nfs4_state *);
  79static void nfs_fixup_referral_attributes(struct nfs_fattr *fattr);
  80static int nfs4_proc_getattr(struct nfs_server *, struct nfs_fh *, struct nfs_fattr *);
  81static int _nfs4_proc_getattr(struct nfs_server *server, struct nfs_fh *fhandle, struct nfs_fattr *fattr);
  82static int nfs4_do_setattr(struct inode *inode, struct rpc_cred *cred,
  83                            struct nfs_fattr *fattr, struct iattr *sattr,
  84                            struct nfs4_state *state);
  85#ifdef CONFIG_NFS_V4_1
  86static int nfs41_test_stateid(struct nfs_server *, nfs4_stateid *);
  87static int nfs41_free_stateid(struct nfs_server *, nfs4_stateid *);
  88#endif
  89/* Prevent leaks of NFSv4 errors into userland */
  90static int nfs4_map_errors(int err)
  91{
  92        if (err >= -1000)
  93                return err;
  94        switch (err) {
  95        case -NFS4ERR_RESOURCE:
  96        case -NFS4ERR_LAYOUTTRYLATER:
  97        case -NFS4ERR_RECALLCONFLICT:
  98                return -EREMOTEIO;
  99        case -NFS4ERR_WRONGSEC:
 100                return -EPERM;
 101        case -NFS4ERR_BADOWNER:
 102        case -NFS4ERR_BADNAME:
 103                return -EINVAL;
 104        case -NFS4ERR_SHARE_DENIED:
 105                return -EACCES;
 106        case -NFS4ERR_MINOR_VERS_MISMATCH:
 107                return -EPROTONOSUPPORT;
 108        case -NFS4ERR_ACCESS:
 109                return -EACCES;
 110        default:
 111                dprintk("%s could not handle NFSv4 error %d\n",
 112                                __func__, -err);
 113                break;
 114        }
 115        return -EIO;
 116}
 117
 118/*
 119 * This is our standard bitmap for GETATTR requests.
 120 */
 121const u32 nfs4_fattr_bitmap[3] = {
 122        FATTR4_WORD0_TYPE
 123        | FATTR4_WORD0_CHANGE
 124        | FATTR4_WORD0_SIZE
 125        | FATTR4_WORD0_FSID
 126        | FATTR4_WORD0_FILEID,
 127        FATTR4_WORD1_MODE
 128        | FATTR4_WORD1_NUMLINKS
 129        | FATTR4_WORD1_OWNER
 130        | FATTR4_WORD1_OWNER_GROUP
 131        | FATTR4_WORD1_RAWDEV
 132        | FATTR4_WORD1_SPACE_USED
 133        | FATTR4_WORD1_TIME_ACCESS
 134        | FATTR4_WORD1_TIME_METADATA
 135        | FATTR4_WORD1_TIME_MODIFY
 136};
 137
 138static const u32 nfs4_pnfs_open_bitmap[3] = {
 139        FATTR4_WORD0_TYPE
 140        | FATTR4_WORD0_CHANGE
 141        | FATTR4_WORD0_SIZE
 142        | FATTR4_WORD0_FSID
 143        | FATTR4_WORD0_FILEID,
 144        FATTR4_WORD1_MODE
 145        | FATTR4_WORD1_NUMLINKS
 146        | FATTR4_WORD1_OWNER
 147        | FATTR4_WORD1_OWNER_GROUP
 148        | FATTR4_WORD1_RAWDEV
 149        | FATTR4_WORD1_SPACE_USED
 150        | FATTR4_WORD1_TIME_ACCESS
 151        | FATTR4_WORD1_TIME_METADATA
 152        | FATTR4_WORD1_TIME_MODIFY,
 153        FATTR4_WORD2_MDSTHRESHOLD
 154};
 155
 156static const u32 nfs4_open_noattr_bitmap[3] = {
 157        FATTR4_WORD0_TYPE
 158        | FATTR4_WORD0_CHANGE
 159        | FATTR4_WORD0_FILEID,
 160};
 161
 162const u32 nfs4_statfs_bitmap[2] = {
 163        FATTR4_WORD0_FILES_AVAIL
 164        | FATTR4_WORD0_FILES_FREE
 165        | FATTR4_WORD0_FILES_TOTAL,
 166        FATTR4_WORD1_SPACE_AVAIL
 167        | FATTR4_WORD1_SPACE_FREE
 168        | FATTR4_WORD1_SPACE_TOTAL
 169};
 170
 171const u32 nfs4_pathconf_bitmap[2] = {
 172        FATTR4_WORD0_MAXLINK
 173        | FATTR4_WORD0_MAXNAME,
 174        0
 175};
 176
 177const u32 nfs4_fsinfo_bitmap[3] = { FATTR4_WORD0_MAXFILESIZE
 178                        | FATTR4_WORD0_MAXREAD
 179                        | FATTR4_WORD0_MAXWRITE
 180                        | FATTR4_WORD0_LEASE_TIME,
 181                        FATTR4_WORD1_TIME_DELTA
 182                        | FATTR4_WORD1_FS_LAYOUT_TYPES,
 183                        FATTR4_WORD2_LAYOUT_BLKSIZE
 184};
 185
 186const u32 nfs4_fs_locations_bitmap[2] = {
 187        FATTR4_WORD0_TYPE
 188        | FATTR4_WORD0_CHANGE
 189        | FATTR4_WORD0_SIZE
 190        | FATTR4_WORD0_FSID
 191        | FATTR4_WORD0_FILEID
 192        | FATTR4_WORD0_FS_LOCATIONS,
 193        FATTR4_WORD1_MODE
 194        | FATTR4_WORD1_NUMLINKS
 195        | FATTR4_WORD1_OWNER
 196        | FATTR4_WORD1_OWNER_GROUP
 197        | FATTR4_WORD1_RAWDEV
 198        | FATTR4_WORD1_SPACE_USED
 199        | FATTR4_WORD1_TIME_ACCESS
 200        | FATTR4_WORD1_TIME_METADATA
 201        | FATTR4_WORD1_TIME_MODIFY
 202        | FATTR4_WORD1_MOUNTED_ON_FILEID
 203};
 204
 205static void nfs4_setup_readdir(u64 cookie, __be32 *verifier, struct dentry *dentry,
 206                struct nfs4_readdir_arg *readdir)
 207{
 208        __be32 *start, *p;
 209
 210        if (cookie > 2) {
 211                readdir->cookie = cookie;
 212                memcpy(&readdir->verifier, verifier, sizeof(readdir->verifier));
 213                return;
 214        }
 215
 216        readdir->cookie = 0;
 217        memset(&readdir->verifier, 0, sizeof(readdir->verifier));
 218        if (cookie == 2)
 219                return;
 220        
 221        /*
 222         * NFSv4 servers do not return entries for '.' and '..'
 223         * Therefore, we fake these entries here.  We let '.'
 224         * have cookie 0 and '..' have cookie 1.  Note that
 225         * when talking to the server, we always send cookie 0
 226         * instead of 1 or 2.
 227         */
 228        start = p = kmap_atomic(*readdir->pages);
 229        
 230        if (cookie == 0) {
 231                *p++ = xdr_one;                                  /* next */
 232                *p++ = xdr_zero;                   /* cookie, first word */
 233                *p++ = xdr_one;                   /* cookie, second word */
 234                *p++ = xdr_one;                             /* entry len */
 235                memcpy(p, ".\0\0\0", 4);                        /* entry */
 236                p++;
 237                *p++ = xdr_one;                         /* bitmap length */
 238                *p++ = htonl(FATTR4_WORD0_FILEID);             /* bitmap */
 239                *p++ = htonl(8);              /* attribute buffer length */
 240                p = xdr_encode_hyper(p, NFS_FILEID(dentry->d_inode));
 241        }
 242        
 243        *p++ = xdr_one;                                  /* next */
 244        *p++ = xdr_zero;                   /* cookie, first word */
 245        *p++ = xdr_two;                   /* cookie, second word */
 246        *p++ = xdr_two;                             /* entry len */
 247        memcpy(p, "..\0\0", 4);                         /* entry */
 248        p++;
 249        *p++ = xdr_one;                         /* bitmap length */
 250        *p++ = htonl(FATTR4_WORD0_FILEID);             /* bitmap */
 251        *p++ = htonl(8);              /* attribute buffer length */
 252        p = xdr_encode_hyper(p, NFS_FILEID(dentry->d_parent->d_inode));
 253
 254        readdir->pgbase = (char *)p - (char *)start;
 255        readdir->count -= readdir->pgbase;
 256        kunmap_atomic(start);
 257}
 258
 259static int nfs4_delay(struct rpc_clnt *clnt, long *timeout)
 260{
 261        int res = 0;
 262
 263        might_sleep();
 264
 265        if (*timeout <= 0)
 266                *timeout = NFS4_POLL_RETRY_MIN;
 267        if (*timeout > NFS4_POLL_RETRY_MAX)
 268                *timeout = NFS4_POLL_RETRY_MAX;
 269        freezable_schedule_timeout_killable(*timeout);
 270        if (fatal_signal_pending(current))
 271                res = -ERESTARTSYS;
 272        *timeout <<= 1;
 273        return res;
 274}
 275
 276/* This is the error handling routine for processes that are allowed
 277 * to sleep.
 278 */
 279static int nfs4_handle_exception(struct nfs_server *server, int errorcode, struct nfs4_exception *exception)
 280{
 281        struct nfs_client *clp = server->nfs_client;
 282        struct nfs4_state *state = exception->state;
 283        struct inode *inode = exception->inode;
 284        int ret = errorcode;
 285
 286        exception->retry = 0;
 287        switch(errorcode) {
 288                case 0:
 289                        return 0;
 290                case -NFS4ERR_OPENMODE:
 291                        if (inode && nfs4_have_delegation(inode, FMODE_READ)) {
 292                                nfs4_inode_return_delegation(inode);
 293                                exception->retry = 1;
 294                                return 0;
 295                        }
 296                        if (state == NULL)
 297                                break;
 298                        nfs4_schedule_stateid_recovery(server, state);
 299                        goto wait_on_recovery;
 300                case -NFS4ERR_DELEG_REVOKED:
 301                case -NFS4ERR_ADMIN_REVOKED:
 302                case -NFS4ERR_BAD_STATEID:
 303                        if (state == NULL)
 304                                break;
 305                        nfs_remove_bad_delegation(state->inode);
 306                        nfs4_schedule_stateid_recovery(server, state);
 307                        goto wait_on_recovery;
 308                case -NFS4ERR_EXPIRED:
 309                        if (state != NULL)
 310                                nfs4_schedule_stateid_recovery(server, state);
 311                case -NFS4ERR_STALE_STATEID:
 312                case -NFS4ERR_STALE_CLIENTID:
 313                        nfs4_schedule_lease_recovery(clp);
 314                        goto wait_on_recovery;
 315#if defined(CONFIG_NFS_V4_1)
 316                case -NFS4ERR_BADSESSION:
 317                case -NFS4ERR_BADSLOT:
 318                case -NFS4ERR_BAD_HIGH_SLOT:
 319                case -NFS4ERR_CONN_NOT_BOUND_TO_SESSION:
 320                case -NFS4ERR_DEADSESSION:
 321                case -NFS4ERR_SEQ_FALSE_RETRY:
 322                case -NFS4ERR_SEQ_MISORDERED:
 323                        dprintk("%s ERROR: %d Reset session\n", __func__,
 324                                errorcode);
 325                        nfs4_schedule_session_recovery(clp->cl_session, errorcode);
 326                        goto wait_on_recovery;
 327#endif /* defined(CONFIG_NFS_V4_1) */
 328                case -NFS4ERR_FILE_OPEN:
 329                        if (exception->timeout > HZ) {
 330                                /* We have retried a decent amount, time to
 331                                 * fail
 332                                 */
 333                                ret = -EBUSY;
 334                                break;
 335                        }
 336                case -NFS4ERR_GRACE:
 337                case -NFS4ERR_DELAY:
 338                        ret = nfs4_delay(server->client, &exception->timeout);
 339                        if (ret != 0)
 340                                break;
 341                case -NFS4ERR_RETRY_UNCACHED_REP:
 342                case -NFS4ERR_OLD_STATEID:
 343                        exception->retry = 1;
 344                        break;
 345                case -NFS4ERR_BADOWNER:
 346                        /* The following works around a Linux server bug! */
 347                case -NFS4ERR_BADNAME:
 348                        if (server->caps & NFS_CAP_UIDGID_NOMAP) {
 349                                server->caps &= ~NFS_CAP_UIDGID_NOMAP;
 350                                exception->retry = 1;
 351                                printk(KERN_WARNING "NFS: v4 server %s "
 352                                                "does not accept raw "
 353                                                "uid/gids. "
 354                                                "Reenabling the idmapper.\n",
 355                                                server->nfs_client->cl_hostname);
 356                        }
 357        }
 358        /* We failed to handle the error */
 359        return nfs4_map_errors(ret);
 360wait_on_recovery:
 361        ret = nfs4_wait_clnt_recover(clp);
 362        if (ret == 0)
 363                exception->retry = 1;
 364        return ret;
 365}
 366
 367
 368static void do_renew_lease(struct nfs_client *clp, unsigned long timestamp)
 369{
 370        spin_lock(&clp->cl_lock);
 371        if (time_before(clp->cl_last_renewal,timestamp))
 372                clp->cl_last_renewal = timestamp;
 373        spin_unlock(&clp->cl_lock);
 374}
 375
 376static void renew_lease(const struct nfs_server *server, unsigned long timestamp)
 377{
 378        do_renew_lease(server->nfs_client, timestamp);
 379}
 380
 381#if defined(CONFIG_NFS_V4_1)
 382
 383static void nfs41_sequence_free_slot(struct nfs4_sequence_res *res)
 384{
 385        struct nfs4_session *session;
 386        struct nfs4_slot_table *tbl;
 387        bool send_new_highest_used_slotid = false;
 388
 389        if (!res->sr_slot) {
 390                /* just wake up the next guy waiting since
 391                 * we may have not consumed a slot after all */
 392                dprintk("%s: No slot\n", __func__);
 393                return;
 394        }
 395        tbl = res->sr_slot->table;
 396        session = tbl->session;
 397
 398        spin_lock(&tbl->slot_tbl_lock);
 399        /* Be nice to the server: try to ensure that the last transmitted
 400         * value for highest_user_slotid <= target_highest_slotid
 401         */
 402        if (tbl->highest_used_slotid > tbl->target_highest_slotid)
 403                send_new_highest_used_slotid = true;
 404
 405        if (nfs41_wake_and_assign_slot(tbl, res->sr_slot)) {
 406                send_new_highest_used_slotid = false;
 407                goto out_unlock;
 408        }
 409        nfs4_free_slot(tbl, res->sr_slot);
 410
 411        if (tbl->highest_used_slotid != NFS4_NO_SLOT)
 412                send_new_highest_used_slotid = false;
 413out_unlock:
 414        spin_unlock(&tbl->slot_tbl_lock);
 415        res->sr_slot = NULL;
 416        if (send_new_highest_used_slotid)
 417                nfs41_server_notify_highest_slotid_update(session->clp);
 418}
 419
 420static int nfs41_sequence_done(struct rpc_task *task, struct nfs4_sequence_res *res)
 421{
 422        struct nfs4_session *session;
 423        struct nfs4_slot *slot;
 424        struct nfs_client *clp;
 425        bool interrupted = false;
 426        int ret = 1;
 427
 428        /* don't increment the sequence number if the task wasn't sent */
 429        if (!RPC_WAS_SENT(task))
 430                goto out;
 431
 432        slot = res->sr_slot;
 433        session = slot->table->session;
 434
 435        if (slot->interrupted) {
 436                slot->interrupted = 0;
 437                interrupted = true;
 438        }
 439
 440        /* Check the SEQUENCE operation status */
 441        switch (res->sr_status) {
 442        case 0:
 443                /* Update the slot's sequence and clientid lease timer */
 444                ++slot->seq_nr;
 445                clp = session->clp;
 446                do_renew_lease(clp, res->sr_timestamp);
 447                /* Check sequence flags */
 448                if (res->sr_status_flags != 0)
 449                        nfs4_schedule_lease_recovery(clp);
 450                nfs41_update_target_slotid(slot->table, slot, res);
 451                break;
 452        case 1:
 453                /*
 454                 * sr_status remains 1 if an RPC level error occurred.
 455                 * The server may or may not have processed the sequence
 456                 * operation..
 457                 * Mark the slot as having hosted an interrupted RPC call.
 458                 */
 459                slot->interrupted = 1;
 460                goto out;
 461        case -NFS4ERR_DELAY:
 462                /* The server detected a resend of the RPC call and
 463                 * returned NFS4ERR_DELAY as per Section 2.10.6.2
 464                 * of RFC5661.
 465                 */
 466                dprintk("%s: slot=%u seq=%u: Operation in progress\n",
 467                        __func__,
 468                        slot->slot_nr,
 469                        slot->seq_nr);
 470                goto out_retry;
 471        case -NFS4ERR_BADSLOT:
 472                /*
 473                 * The slot id we used was probably retired. Try again
 474                 * using a different slot id.
 475                 */
 476                goto retry_nowait;
 477        case -NFS4ERR_SEQ_MISORDERED:
 478                /*
 479                 * Was the last operation on this sequence interrupted?
 480                 * If so, retry after bumping the sequence number.
 481                 */
 482                if (interrupted) {
 483                        ++slot->seq_nr;
 484                        goto retry_nowait;
 485                }
 486                /*
 487                 * Could this slot have been previously retired?
 488                 * If so, then the server may be expecting seq_nr = 1!
 489                 */
 490                if (slot->seq_nr != 1) {
 491                        slot->seq_nr = 1;
 492                        goto retry_nowait;
 493                }
 494                break;
 495        case -NFS4ERR_SEQ_FALSE_RETRY:
 496                ++slot->seq_nr;
 497                goto retry_nowait;
 498        default:
 499                /* Just update the slot sequence no. */
 500                ++slot->seq_nr;
 501        }
 502out:
 503        /* The session may be reset by one of the error handlers. */
 504        dprintk("%s: Error %d free the slot \n", __func__, res->sr_status);
 505        nfs41_sequence_free_slot(res);
 506        return ret;
 507retry_nowait:
 508        if (rpc_restart_call_prepare(task)) {
 509                task->tk_status = 0;
 510                ret = 0;
 511        }
 512        goto out;
 513out_retry:
 514        if (!rpc_restart_call(task))
 515                goto out;
 516        rpc_delay(task, NFS4_POLL_RETRY_MAX);
 517        return 0;
 518}
 519
 520static int nfs4_sequence_done(struct rpc_task *task,
 521                               struct nfs4_sequence_res *res)
 522{
 523        if (res->sr_slot == NULL)
 524                return 1;
 525        return nfs41_sequence_done(task, res);
 526}
 527
 528static void nfs41_init_sequence(struct nfs4_sequence_args *args,
 529                struct nfs4_sequence_res *res, int cache_reply)
 530{
 531        args->sa_slot = NULL;
 532        args->sa_cache_this = 0;
 533        args->sa_privileged = 0;
 534        if (cache_reply)
 535                args->sa_cache_this = 1;
 536        res->sr_slot = NULL;
 537}
 538
 539static void nfs4_set_sequence_privileged(struct nfs4_sequence_args *args)
 540{
 541        args->sa_privileged = 1;
 542}
 543
 544int nfs41_setup_sequence(struct nfs4_session *session,
 545                                struct nfs4_sequence_args *args,
 546                                struct nfs4_sequence_res *res,
 547                                struct rpc_task *task)
 548{
 549        struct nfs4_slot *slot;
 550        struct nfs4_slot_table *tbl;
 551
 552        dprintk("--> %s\n", __func__);
 553        /* slot already allocated? */
 554        if (res->sr_slot != NULL)
 555                goto out_success;
 556
 557        tbl = &session->fc_slot_table;
 558
 559        task->tk_timeout = 0;
 560
 561        spin_lock(&tbl->slot_tbl_lock);
 562        if (test_bit(NFS4_SESSION_DRAINING, &session->session_state) &&
 563            !args->sa_privileged) {
 564                /* The state manager will wait until the slot table is empty */
 565                dprintk("%s session is draining\n", __func__);
 566                goto out_sleep;
 567        }
 568
 569        slot = nfs4_alloc_slot(tbl);
 570        if (IS_ERR(slot)) {
 571                /* If out of memory, try again in 1/4 second */
 572                if (slot == ERR_PTR(-ENOMEM))
 573                        task->tk_timeout = HZ >> 2;
 574                dprintk("<-- %s: no free slots\n", __func__);
 575                goto out_sleep;
 576        }
 577        spin_unlock(&tbl->slot_tbl_lock);
 578
 579        args->sa_slot = slot;
 580
 581        dprintk("<-- %s slotid=%d seqid=%d\n", __func__,
 582                        slot->slot_nr, slot->seq_nr);
 583
 584        res->sr_slot = slot;
 585        res->sr_timestamp = jiffies;
 586        res->sr_status_flags = 0;
 587        /*
 588         * sr_status is only set in decode_sequence, and so will remain
 589         * set to 1 if an rpc level failure occurs.
 590         */
 591        res->sr_status = 1;
 592out_success:
 593        rpc_call_start(task);
 594        return 0;
 595out_sleep:
 596        /* Privileged tasks are queued with top priority */
 597        if (args->sa_privileged)
 598                rpc_sleep_on_priority(&tbl->slot_tbl_waitq, task,
 599                                NULL, RPC_PRIORITY_PRIVILEGED);
 600        else
 601                rpc_sleep_on(&tbl->slot_tbl_waitq, task, NULL);
 602        spin_unlock(&tbl->slot_tbl_lock);
 603        return -EAGAIN;
 604}
 605EXPORT_SYMBOL_GPL(nfs41_setup_sequence);
 606
 607int nfs4_setup_sequence(const struct nfs_server *server,
 608                        struct nfs4_sequence_args *args,
 609                        struct nfs4_sequence_res *res,
 610                        struct rpc_task *task)
 611{
 612        struct nfs4_session *session = nfs4_get_session(server);
 613        int ret = 0;
 614
 615        if (session == NULL) {
 616                rpc_call_start(task);
 617                goto out;
 618        }
 619
 620        dprintk("--> %s clp %p session %p sr_slot %d\n",
 621                __func__, session->clp, session, res->sr_slot ?
 622                        res->sr_slot->slot_nr : -1);
 623
 624        ret = nfs41_setup_sequence(session, args, res, task);
 625out:
 626        dprintk("<-- %s status=%d\n", __func__, ret);
 627        return ret;
 628}
 629
 630struct nfs41_call_sync_data {
 631        const struct nfs_server *seq_server;
 632        struct nfs4_sequence_args *seq_args;
 633        struct nfs4_sequence_res *seq_res;
 634};
 635
 636static void nfs41_call_sync_prepare(struct rpc_task *task, void *calldata)
 637{
 638        struct nfs41_call_sync_data *data = calldata;
 639        struct nfs4_session *session = nfs4_get_session(data->seq_server);
 640
 641        dprintk("--> %s data->seq_server %p\n", __func__, data->seq_server);
 642
 643        nfs41_setup_sequence(session, data->seq_args, data->seq_res, task);
 644}
 645
 646static void nfs41_call_sync_done(struct rpc_task *task, void *calldata)
 647{
 648        struct nfs41_call_sync_data *data = calldata;
 649
 650        nfs41_sequence_done(task, data->seq_res);
 651}
 652
 653static const struct rpc_call_ops nfs41_call_sync_ops = {
 654        .rpc_call_prepare = nfs41_call_sync_prepare,
 655        .rpc_call_done = nfs41_call_sync_done,
 656};
 657
 658static int nfs4_call_sync_sequence(struct rpc_clnt *clnt,
 659                                   struct nfs_server *server,
 660                                   struct rpc_message *msg,
 661                                   struct nfs4_sequence_args *args,
 662                                   struct nfs4_sequence_res *res)
 663{
 664        int ret;
 665        struct rpc_task *task;
 666        struct nfs41_call_sync_data data = {
 667                .seq_server = server,
 668                .seq_args = args,
 669                .seq_res = res,
 670        };
 671        struct rpc_task_setup task_setup = {
 672                .rpc_client = clnt,
 673                .rpc_message = msg,
 674                .callback_ops = &nfs41_call_sync_ops,
 675                .callback_data = &data
 676        };
 677
 678        task = rpc_run_task(&task_setup);
 679        if (IS_ERR(task))
 680                ret = PTR_ERR(task);
 681        else {
 682                ret = task->tk_status;
 683                rpc_put_task(task);
 684        }
 685        return ret;
 686}
 687
 688#else
 689static
 690void nfs41_init_sequence(struct nfs4_sequence_args *args,
 691                struct nfs4_sequence_res *res, int cache_reply)
 692{
 693}
 694
 695static void nfs4_set_sequence_privileged(struct nfs4_sequence_args *args)
 696{
 697}
 698
 699
 700static int nfs4_sequence_done(struct rpc_task *task,
 701                               struct nfs4_sequence_res *res)
 702{
 703        return 1;
 704}
 705#endif /* CONFIG_NFS_V4_1 */
 706
 707static
 708int _nfs4_call_sync(struct rpc_clnt *clnt,
 709                    struct nfs_server *server,
 710                    struct rpc_message *msg,
 711                    struct nfs4_sequence_args *args,
 712                    struct nfs4_sequence_res *res)
 713{
 714        return rpc_call_sync(clnt, msg, 0);
 715}
 716
 717static
 718int nfs4_call_sync(struct rpc_clnt *clnt,
 719                   struct nfs_server *server,
 720                   struct rpc_message *msg,
 721                   struct nfs4_sequence_args *args,
 722                   struct nfs4_sequence_res *res,
 723                   int cache_reply)
 724{
 725        nfs41_init_sequence(args, res, cache_reply);
 726        return server->nfs_client->cl_mvops->call_sync(clnt, server, msg,
 727                                                args, res);
 728}
 729
 730static void update_changeattr(struct inode *dir, struct nfs4_change_info *cinfo)
 731{
 732        struct nfs_inode *nfsi = NFS_I(dir);
 733
 734        spin_lock(&dir->i_lock);
 735        nfsi->cache_validity |= NFS_INO_INVALID_ATTR|NFS_INO_INVALID_DATA;
 736        if (!cinfo->atomic || cinfo->before != dir->i_version)
 737                nfs_force_lookup_revalidate(dir);
 738        dir->i_version = cinfo->after;
 739        nfs_fscache_invalidate(dir);
 740        spin_unlock(&dir->i_lock);
 741}
 742
 743struct nfs4_opendata {
 744        struct kref kref;
 745        struct nfs_openargs o_arg;
 746        struct nfs_openres o_res;
 747        struct nfs_open_confirmargs c_arg;
 748        struct nfs_open_confirmres c_res;
 749        struct nfs4_string owner_name;
 750        struct nfs4_string group_name;
 751        struct nfs_fattr f_attr;
 752        struct dentry *dir;
 753        struct dentry *dentry;
 754        struct nfs4_state_owner *owner;
 755        struct nfs4_state *state;
 756        struct iattr attrs;
 757        unsigned long timestamp;
 758        unsigned int rpc_done : 1;
 759        int rpc_status;
 760        int cancelled;
 761};
 762
 763
 764static void nfs4_init_opendata_res(struct nfs4_opendata *p)
 765{
 766        p->o_res.f_attr = &p->f_attr;
 767        p->o_res.seqid = p->o_arg.seqid;
 768        p->c_res.seqid = p->c_arg.seqid;
 769        p->o_res.server = p->o_arg.server;
 770        p->o_res.access_request = p->o_arg.access;
 771        nfs_fattr_init(&p->f_attr);
 772        nfs_fattr_init_names(&p->f_attr, &p->owner_name, &p->group_name);
 773}
 774
 775static struct nfs4_opendata *nfs4_opendata_alloc(struct dentry *dentry,
 776                struct nfs4_state_owner *sp, fmode_t fmode, int flags,
 777                const struct iattr *attrs,
 778                gfp_t gfp_mask)
 779{
 780        struct dentry *parent = dget_parent(dentry);
 781        struct inode *dir = parent->d_inode;
 782        struct nfs_server *server = NFS_SERVER(dir);
 783        struct nfs4_opendata *p;
 784
 785        p = kzalloc(sizeof(*p), gfp_mask);
 786        if (p == NULL)
 787                goto err;
 788        p->o_arg.seqid = nfs_alloc_seqid(&sp->so_seqid, gfp_mask);
 789        if (p->o_arg.seqid == NULL)
 790                goto err_free;
 791        nfs_sb_active(dentry->d_sb);
 792        p->dentry = dget(dentry);
 793        p->dir = parent;
 794        p->owner = sp;
 795        atomic_inc(&sp->so_count);
 796        p->o_arg.fh = NFS_FH(dir);
 797        p->o_arg.open_flags = flags;
 798        p->o_arg.fmode = fmode & (FMODE_READ|FMODE_WRITE);
 799        /* don't put an ACCESS op in OPEN compound if O_EXCL, because ACCESS
 800         * will return permission denied for all bits until close */
 801        if (!(flags & O_EXCL)) {
 802                /* ask server to check for all possible rights as results
 803                 * are cached */
 804                p->o_arg.access = NFS4_ACCESS_READ | NFS4_ACCESS_MODIFY |
 805                                  NFS4_ACCESS_EXTEND | NFS4_ACCESS_EXECUTE;
 806        }
 807        p->o_arg.clientid = server->nfs_client->cl_clientid;
 808        p->o_arg.id.create_time = ktime_to_ns(sp->so_seqid.create_time);
 809        p->o_arg.id.uniquifier = sp->so_seqid.owner_id;
 810        p->o_arg.name = &dentry->d_name;
 811        p->o_arg.server = server;
 812        p->o_arg.bitmask = server->attr_bitmask;
 813        p->o_arg.open_bitmap = &nfs4_fattr_bitmap[0];
 814        p->o_arg.claim = NFS4_OPEN_CLAIM_NULL;
 815        if (attrs != NULL && attrs->ia_valid != 0) {
 816                __be32 verf[2];
 817
 818                p->o_arg.u.attrs = &p->attrs;
 819                memcpy(&p->attrs, attrs, sizeof(p->attrs));
 820
 821                verf[0] = jiffies;
 822                verf[1] = current->pid;
 823                memcpy(p->o_arg.u.verifier.data, verf,
 824                                sizeof(p->o_arg.u.verifier.data));
 825        }
 826        p->c_arg.fh = &p->o_res.fh;
 827        p->c_arg.stateid = &p->o_res.stateid;
 828        p->c_arg.seqid = p->o_arg.seqid;
 829        nfs4_init_opendata_res(p);
 830        kref_init(&p->kref);
 831        return p;
 832err_free:
 833        kfree(p);
 834err:
 835        dput(parent);
 836        return NULL;
 837}
 838
 839static void nfs4_opendata_free(struct kref *kref)
 840{
 841        struct nfs4_opendata *p = container_of(kref,
 842                        struct nfs4_opendata, kref);
 843        struct super_block *sb = p->dentry->d_sb;
 844
 845        nfs_free_seqid(p->o_arg.seqid);
 846        if (p->state != NULL)
 847                nfs4_put_open_state(p->state);
 848        nfs4_put_state_owner(p->owner);
 849        dput(p->dir);
 850        dput(p->dentry);
 851        nfs_sb_deactive(sb);
 852        nfs_fattr_free_names(&p->f_attr);
 853        kfree(p);
 854}
 855
 856static void nfs4_opendata_put(struct nfs4_opendata *p)
 857{
 858        if (p != NULL)
 859                kref_put(&p->kref, nfs4_opendata_free);
 860}
 861
 862static int nfs4_wait_for_completion_rpc_task(struct rpc_task *task)
 863{
 864        int ret;
 865
 866        ret = rpc_wait_for_completion_task(task);
 867        return ret;
 868}
 869
 870static int can_open_cached(struct nfs4_state *state, fmode_t mode, int open_mode)
 871{
 872        int ret = 0;
 873
 874        if (open_mode & (O_EXCL|O_TRUNC))
 875                goto out;
 876        switch (mode & (FMODE_READ|FMODE_WRITE)) {
 877                case FMODE_READ:
 878                        ret |= test_bit(NFS_O_RDONLY_STATE, &state->flags) != 0
 879                                && state->n_rdonly != 0;
 880                        break;
 881                case FMODE_WRITE:
 882                        ret |= test_bit(NFS_O_WRONLY_STATE, &state->flags) != 0
 883                                && state->n_wronly != 0;
 884                        break;
 885                case FMODE_READ|FMODE_WRITE:
 886                        ret |= test_bit(NFS_O_RDWR_STATE, &state->flags) != 0
 887                                && state->n_rdwr != 0;
 888        }
 889out:
 890        return ret;
 891}
 892
 893static int can_open_delegated(struct nfs_delegation *delegation, fmode_t fmode)
 894{
 895        if (delegation == NULL)
 896                return 0;
 897        if ((delegation->type & fmode) != fmode)
 898                return 0;
 899        if (test_bit(NFS_DELEGATION_NEED_RECLAIM, &delegation->flags))
 900                return 0;
 901        if (test_bit(NFS_DELEGATION_RETURNING, &delegation->flags))
 902                return 0;
 903        nfs_mark_delegation_referenced(delegation);
 904        return 1;
 905}
 906
 907static void update_open_stateflags(struct nfs4_state *state, fmode_t fmode)
 908{
 909        switch (fmode) {
 910                case FMODE_WRITE:
 911                        state->n_wronly++;
 912                        break;
 913                case FMODE_READ:
 914                        state->n_rdonly++;
 915                        break;
 916                case FMODE_READ|FMODE_WRITE:
 917                        state->n_rdwr++;
 918        }
 919        nfs4_state_set_mode_locked(state, state->state | fmode);
 920}
 921
 922static void nfs_set_open_stateid_locked(struct nfs4_state *state, nfs4_stateid *stateid, fmode_t fmode)
 923{
 924        if (test_bit(NFS_DELEGATED_STATE, &state->flags) == 0)
 925                nfs4_stateid_copy(&state->stateid, stateid);
 926        nfs4_stateid_copy(&state->open_stateid, stateid);
 927        switch (fmode) {
 928                case FMODE_READ:
 929                        set_bit(NFS_O_RDONLY_STATE, &state->flags);
 930                        break;
 931                case FMODE_WRITE:
 932                        set_bit(NFS_O_WRONLY_STATE, &state->flags);
 933                        break;
 934                case FMODE_READ|FMODE_WRITE:
 935                        set_bit(NFS_O_RDWR_STATE, &state->flags);
 936        }
 937}
 938
 939static void nfs_set_open_stateid(struct nfs4_state *state, nfs4_stateid *stateid, fmode_t fmode)
 940{
 941        write_seqlock(&state->seqlock);
 942        nfs_set_open_stateid_locked(state, stateid, fmode);
 943        write_sequnlock(&state->seqlock);
 944}
 945
 946static void __update_open_stateid(struct nfs4_state *state, nfs4_stateid *open_stateid, const nfs4_stateid *deleg_stateid, fmode_t fmode)
 947{
 948        /*
 949         * Protect the call to nfs4_state_set_mode_locked and
 950         * serialise the stateid update
 951         */
 952        write_seqlock(&state->seqlock);
 953        if (deleg_stateid != NULL) {
 954                nfs4_stateid_copy(&state->stateid, deleg_stateid);
 955                set_bit(NFS_DELEGATED_STATE, &state->flags);
 956        }
 957        if (open_stateid != NULL)
 958                nfs_set_open_stateid_locked(state, open_stateid, fmode);
 959        write_sequnlock(&state->seqlock);
 960        spin_lock(&state->owner->so_lock);
 961        update_open_stateflags(state, fmode);
 962        spin_unlock(&state->owner->so_lock);
 963}
 964
 965static int update_open_stateid(struct nfs4_state *state, nfs4_stateid *open_stateid, nfs4_stateid *delegation, fmode_t fmode)
 966{
 967        struct nfs_inode *nfsi = NFS_I(state->inode);
 968        struct nfs_delegation *deleg_cur;
 969        int ret = 0;
 970
 971        fmode &= (FMODE_READ|FMODE_WRITE);
 972
 973        rcu_read_lock();
 974        deleg_cur = rcu_dereference(nfsi->delegation);
 975        if (deleg_cur == NULL)
 976                goto no_delegation;
 977
 978        spin_lock(&deleg_cur->lock);
 979        if (nfsi->delegation != deleg_cur ||
 980           test_bit(NFS_DELEGATION_RETURNING, &deleg_cur->flags) ||
 981            (deleg_cur->type & fmode) != fmode)
 982                goto no_delegation_unlock;
 983
 984        if (delegation == NULL)
 985                delegation = &deleg_cur->stateid;
 986        else if (!nfs4_stateid_match(&deleg_cur->stateid, delegation))
 987                goto no_delegation_unlock;
 988
 989        nfs_mark_delegation_referenced(deleg_cur);
 990        __update_open_stateid(state, open_stateid, &deleg_cur->stateid, fmode);
 991        ret = 1;
 992no_delegation_unlock:
 993        spin_unlock(&deleg_cur->lock);
 994no_delegation:
 995        rcu_read_unlock();
 996
 997        if (!ret && open_stateid != NULL) {
 998                __update_open_stateid(state, open_stateid, NULL, fmode);
 999                ret = 1;
1000        }
1001
1002        return ret;
1003}
1004
1005
1006static void nfs4_return_incompatible_delegation(struct inode *inode, fmode_t fmode)
1007{
1008        struct nfs_delegation *delegation;
1009
1010        rcu_read_lock();
1011        delegation = rcu_dereference(NFS_I(inode)->delegation);
1012        if (delegation == NULL || (delegation->type & fmode) == fmode) {
1013                rcu_read_unlock();
1014                return;
1015        }
1016        rcu_read_unlock();
1017        nfs4_inode_return_delegation(inode);
1018}
1019
1020static struct nfs4_state *nfs4_try_open_cached(struct nfs4_opendata *opendata)
1021{
1022        struct nfs4_state *state = opendata->state;
1023        struct nfs_inode *nfsi = NFS_I(state->inode);
1024        struct nfs_delegation *delegation;
1025        int open_mode = opendata->o_arg.open_flags & (O_EXCL|O_TRUNC);
1026        fmode_t fmode = opendata->o_arg.fmode;
1027        nfs4_stateid stateid;
1028        int ret = -EAGAIN;
1029
1030        for (;;) {
1031                if (can_open_cached(state, fmode, open_mode)) {
1032                        spin_lock(&state->owner->so_lock);
1033                        if (can_open_cached(state, fmode, open_mode)) {
1034                                update_open_stateflags(state, fmode);
1035                                spin_unlock(&state->owner->so_lock);
1036                                goto out_return_state;
1037                        }
1038                        spin_unlock(&state->owner->so_lock);
1039                }
1040                rcu_read_lock();
1041                delegation = rcu_dereference(nfsi->delegation);
1042                if (!can_open_delegated(delegation, fmode)) {
1043                        rcu_read_unlock();
1044                        break;
1045                }
1046                /* Save the delegation */
1047                nfs4_stateid_copy(&stateid, &delegation->stateid);
1048                rcu_read_unlock();
1049                nfs_release_seqid(opendata->o_arg.seqid);
1050                ret = nfs_may_open(state->inode, state->owner->so_cred, open_mode);
1051                if (ret != 0)
1052                        goto out;
1053                ret = -EAGAIN;
1054
1055                /* Try to update the stateid using the delegation */
1056                if (update_open_stateid(state, NULL, &stateid, fmode))
1057                        goto out_return_state;
1058        }
1059out:
1060        return ERR_PTR(ret);
1061out_return_state:
1062        atomic_inc(&state->count);
1063        return state;
1064}
1065
1066static void
1067nfs4_opendata_check_deleg(struct nfs4_opendata *data, struct nfs4_state *state)
1068{
1069        struct nfs_client *clp = NFS_SERVER(state->inode)->nfs_client;
1070        struct nfs_delegation *delegation;
1071        int delegation_flags = 0;
1072
1073        rcu_read_lock();
1074        delegation = rcu_dereference(NFS_I(state->inode)->delegation);
1075        if (delegation)
1076                delegation_flags = delegation->flags;
1077        rcu_read_unlock();
1078        if (data->o_arg.claim == NFS4_OPEN_CLAIM_DELEGATE_CUR) {
1079                pr_err_ratelimited("NFS: Broken NFSv4 server %s is "
1080                                   "returning a delegation for "
1081                                   "OPEN(CLAIM_DELEGATE_CUR)\n",
1082                                   clp->cl_hostname);
1083        } else if ((delegation_flags & 1UL<<NFS_DELEGATION_NEED_RECLAIM) == 0)
1084                nfs_inode_set_delegation(state->inode,
1085                                         data->owner->so_cred,
1086                                         &data->o_res);
1087        else
1088                nfs_inode_reclaim_delegation(state->inode,
1089                                             data->owner->so_cred,
1090                                             &data->o_res);
1091}
1092
1093/*
1094 * Check the inode attributes against the CLAIM_PREVIOUS returned attributes
1095 * and update the nfs4_state.
1096 */
1097static struct nfs4_state *
1098_nfs4_opendata_reclaim_to_nfs4_state(struct nfs4_opendata *data)
1099{
1100        struct inode *inode = data->state->inode;
1101        struct nfs4_state *state = data->state;
1102        int ret;
1103
1104        if (!data->rpc_done) {
1105                ret = data->rpc_status;
1106                goto err;
1107        }
1108
1109        ret = -ESTALE;
1110        if (!(data->f_attr.valid & NFS_ATTR_FATTR_TYPE) ||
1111            !(data->f_attr.valid & NFS_ATTR_FATTR_FILEID) ||
1112            !(data->f_attr.valid & NFS_ATTR_FATTR_CHANGE))
1113                goto err;
1114
1115        ret = -ENOMEM;
1116        state = nfs4_get_open_state(inode, data->owner);
1117        if (state == NULL)
1118                goto err;
1119
1120        ret = nfs_refresh_inode(inode, &data->f_attr);
1121        if (ret)
1122                goto err;
1123
1124        if (data->o_res.delegation_type != 0)
1125                nfs4_opendata_check_deleg(data, state);
1126        update_open_stateid(state, &data->o_res.stateid, NULL,
1127                            data->o_arg.fmode);
1128
1129        return state;
1130err:
1131        return ERR_PTR(ret);
1132
1133}
1134
1135static struct nfs4_state *
1136_nfs4_opendata_to_nfs4_state(struct nfs4_opendata *data)
1137{
1138        struct inode *inode;
1139        struct nfs4_state *state = NULL;
1140        int ret;
1141
1142        if (!data->rpc_done) {
1143                state = nfs4_try_open_cached(data);
1144                goto out;
1145        }
1146
1147        ret = -EAGAIN;
1148        if (!(data->f_attr.valid & NFS_ATTR_FATTR))
1149                goto err;
1150        inode = nfs_fhget(data->dir->d_sb, &data->o_res.fh, &data->f_attr);
1151        ret = PTR_ERR(inode);
1152        if (IS_ERR(inode))
1153                goto err;
1154        ret = -ENOMEM;
1155        state = nfs4_get_open_state(inode, data->owner);
1156        if (state == NULL)
1157                goto err_put_inode;
1158        if (data->o_res.delegation_type != 0)
1159                nfs4_opendata_check_deleg(data, state);
1160        update_open_stateid(state, &data->o_res.stateid, NULL,
1161                        data->o_arg.fmode);
1162        iput(inode);
1163out:
1164        nfs_release_seqid(data->o_arg.seqid);
1165        return state;
1166err_put_inode:
1167        iput(inode);
1168err:
1169        return ERR_PTR(ret);
1170}
1171
1172static struct nfs4_state *
1173nfs4_opendata_to_nfs4_state(struct nfs4_opendata *data)
1174{
1175        if (data->o_arg.claim == NFS4_OPEN_CLAIM_PREVIOUS)
1176                return _nfs4_opendata_reclaim_to_nfs4_state(data);
1177        return _nfs4_opendata_to_nfs4_state(data);
1178}
1179
1180static struct nfs_open_context *nfs4_state_find_open_context(struct nfs4_state *state)
1181{
1182        struct nfs_inode *nfsi = NFS_I(state->inode);
1183        struct nfs_open_context *ctx;
1184
1185        spin_lock(&state->inode->i_lock);
1186        list_for_each_entry(ctx, &nfsi->open_files, list) {
1187                if (ctx->state != state)
1188                        continue;
1189                get_nfs_open_context(ctx);
1190                spin_unlock(&state->inode->i_lock);
1191                return ctx;
1192        }
1193        spin_unlock(&state->inode->i_lock);
1194        return ERR_PTR(-ENOENT);
1195}
1196
1197static struct nfs4_opendata *nfs4_open_recoverdata_alloc(struct nfs_open_context *ctx, struct nfs4_state *state)
1198{
1199        struct nfs4_opendata *opendata;
1200
1201        opendata = nfs4_opendata_alloc(ctx->dentry, state->owner, 0, 0, NULL, GFP_NOFS);
1202        if (opendata == NULL)
1203                return ERR_PTR(-ENOMEM);
1204        opendata->state = state;
1205        atomic_inc(&state->count);
1206        return opendata;
1207}
1208
1209static int nfs4_open_recover_helper(struct nfs4_opendata *opendata, fmode_t fmode, struct nfs4_state **res)
1210{
1211        struct nfs4_state *newstate;
1212        int ret;
1213
1214        opendata->o_arg.open_flags = 0;
1215        opendata->o_arg.fmode = fmode;
1216        memset(&opendata->o_res, 0, sizeof(opendata->o_res));
1217        memset(&opendata->c_res, 0, sizeof(opendata->c_res));
1218        nfs4_init_opendata_res(opendata);
1219        ret = _nfs4_recover_proc_open(opendata);
1220        if (ret != 0)
1221                return ret; 
1222        newstate = nfs4_opendata_to_nfs4_state(opendata);
1223        if (IS_ERR(newstate))
1224                return PTR_ERR(newstate);
1225        nfs4_close_state(newstate, fmode);
1226        *res = newstate;
1227        return 0;
1228}
1229
1230static int nfs4_open_recover(struct nfs4_opendata *opendata, struct nfs4_state *state)
1231{
1232        struct nfs4_state *newstate;
1233        int ret;
1234
1235        /* memory barrier prior to reading state->n_* */
1236        clear_bit(NFS_DELEGATED_STATE, &state->flags);
1237        smp_rmb();
1238        if (state->n_rdwr != 0) {
1239                clear_bit(NFS_O_RDWR_STATE, &state->flags);
1240                ret = nfs4_open_recover_helper(opendata, FMODE_READ|FMODE_WRITE, &newstate);
1241                if (ret != 0)
1242                        return ret;
1243                if (newstate != state)
1244                        return -ESTALE;
1245        }
1246        if (state->n_wronly != 0) {
1247                clear_bit(NFS_O_WRONLY_STATE, &state->flags);
1248                ret = nfs4_open_recover_helper(opendata, FMODE_WRITE, &newstate);
1249                if (ret != 0)
1250                        return ret;
1251                if (newstate != state)
1252                        return -ESTALE;
1253        }
1254        if (state->n_rdonly != 0) {
1255                clear_bit(NFS_O_RDONLY_STATE, &state->flags);
1256                ret = nfs4_open_recover_helper(opendata, FMODE_READ, &newstate);
1257                if (ret != 0)
1258                        return ret;
1259                if (newstate != state)
1260                        return -ESTALE;
1261        }
1262        /*
1263         * We may have performed cached opens for all three recoveries.
1264         * Check if we need to update the current stateid.
1265         */
1266        if (test_bit(NFS_DELEGATED_STATE, &state->flags) == 0 &&
1267            !nfs4_stateid_match(&state->stateid, &state->open_stateid)) {
1268                write_seqlock(&state->seqlock);
1269                if (test_bit(NFS_DELEGATED_STATE, &state->flags) == 0)
1270                        nfs4_stateid_copy(&state->stateid, &state->open_stateid);
1271                write_sequnlock(&state->seqlock);
1272        }
1273        return 0;
1274}
1275
1276/*
1277 * OPEN_RECLAIM:
1278 *      reclaim state on the server after a reboot.
1279 */
1280static int _nfs4_do_open_reclaim(struct nfs_open_context *ctx, struct nfs4_state *state)
1281{
1282        struct nfs_delegation *delegation;
1283        struct nfs4_opendata *opendata;
1284        fmode_t delegation_type = 0;
1285        int status;
1286
1287        opendata = nfs4_open_recoverdata_alloc(ctx, state);
1288        if (IS_ERR(opendata))
1289                return PTR_ERR(opendata);
1290        opendata->o_arg.claim = NFS4_OPEN_CLAIM_PREVIOUS;
1291        opendata->o_arg.fh = NFS_FH(state->inode);
1292        rcu_read_lock();
1293        delegation = rcu_dereference(NFS_I(state->inode)->delegation);
1294        if (delegation != NULL && test_bit(NFS_DELEGATION_NEED_RECLAIM, &delegation->flags) != 0)
1295                delegation_type = delegation->type;
1296        rcu_read_unlock();
1297        opendata->o_arg.u.delegation_type = delegation_type;
1298        status = nfs4_open_recover(opendata, state);
1299        nfs4_opendata_put(opendata);
1300        return status;
1301}
1302
1303static int nfs4_do_open_reclaim(struct nfs_open_context *ctx, struct nfs4_state *state)
1304{
1305        struct nfs_server *server = NFS_SERVER(state->inode);
1306        struct nfs4_exception exception = { };
1307        int err;
1308        do {
1309                err = _nfs4_do_open_reclaim(ctx, state);
1310                if (err != -NFS4ERR_DELAY)
1311                        break;
1312                nfs4_handle_exception(server, err, &exception);
1313        } while (exception.retry);
1314        return err;
1315}
1316
1317static int nfs4_open_reclaim(struct nfs4_state_owner *sp, struct nfs4_state *state)
1318{
1319        struct nfs_open_context *ctx;
1320        int ret;
1321
1322        ctx = nfs4_state_find_open_context(state);
1323        if (IS_ERR(ctx))
1324                return PTR_ERR(ctx);
1325        ret = nfs4_do_open_reclaim(ctx, state);
1326        put_nfs_open_context(ctx);
1327        return ret;
1328}
1329
1330static int _nfs4_open_delegation_recall(struct nfs_open_context *ctx, struct nfs4_state *state, const nfs4_stateid *stateid)
1331{
1332        struct nfs4_opendata *opendata;
1333        int ret;
1334
1335        opendata = nfs4_open_recoverdata_alloc(ctx, state);
1336        if (IS_ERR(opendata))
1337                return PTR_ERR(opendata);
1338        opendata->o_arg.claim = NFS4_OPEN_CLAIM_DELEGATE_CUR;
1339        nfs4_stateid_copy(&opendata->o_arg.u.delegation, stateid);
1340        ret = nfs4_open_recover(opendata, state);
1341        nfs4_opendata_put(opendata);
1342        return ret;
1343}
1344
1345int nfs4_open_delegation_recall(struct nfs_open_context *ctx, struct nfs4_state *state, const nfs4_stateid *stateid)
1346{
1347        struct nfs4_exception exception = { };
1348        struct nfs_server *server = NFS_SERVER(state->inode);
1349        int err;
1350        do {
1351                err = _nfs4_open_delegation_recall(ctx, state, stateid);
1352                switch (err) {
1353                        case 0:
1354                        case -ENOENT:
1355                        case -ESTALE:
1356                                goto out;
1357                        case -NFS4ERR_BADSESSION:
1358                        case -NFS4ERR_BADSLOT:
1359                        case -NFS4ERR_BAD_HIGH_SLOT:
1360                        case -NFS4ERR_CONN_NOT_BOUND_TO_SESSION:
1361                        case -NFS4ERR_DEADSESSION:
1362                                set_bit(NFS_DELEGATED_STATE, &state->flags);
1363                                nfs4_schedule_session_recovery(server->nfs_client->cl_session, err);
1364                                err = -EAGAIN;
1365                                goto out;
1366                        case -NFS4ERR_STALE_CLIENTID:
1367                        case -NFS4ERR_STALE_STATEID:
1368                                set_bit(NFS_DELEGATED_STATE, &state->flags);
1369                        case -NFS4ERR_EXPIRED:
1370                                /* Don't recall a delegation if it was lost */
1371                                nfs4_schedule_lease_recovery(server->nfs_client);
1372                                err = -EAGAIN;
1373                                goto out;
1374                        case -NFS4ERR_DELEG_REVOKED:
1375                        case -NFS4ERR_ADMIN_REVOKED:
1376                        case -NFS4ERR_BAD_STATEID:
1377                                nfs_inode_find_state_and_recover(state->inode,
1378                                                stateid);
1379                                nfs4_schedule_stateid_recovery(server, state);
1380                        case -ENOMEM:
1381                                err = 0;
1382                                goto out;
1383                }
1384                set_bit(NFS_DELEGATED_STATE, &state->flags);
1385                err = nfs4_handle_exception(server, err, &exception);
1386        } while (exception.retry);
1387out:
1388        return err;
1389}
1390
1391static void nfs4_open_confirm_done(struct rpc_task *task, void *calldata)
1392{
1393        struct nfs4_opendata *data = calldata;
1394
1395        data->rpc_status = task->tk_status;
1396        if (data->rpc_status == 0) {
1397                nfs4_stateid_copy(&data->o_res.stateid, &data->c_res.stateid);
1398                nfs_confirm_seqid(&data->owner->so_seqid, 0);
1399                renew_lease(data->o_res.server, data->timestamp);
1400                data->rpc_done = 1;
1401        }
1402}
1403
1404static void nfs4_open_confirm_release(void *calldata)
1405{
1406        struct nfs4_opendata *data = calldata;
1407        struct nfs4_state *state = NULL;
1408
1409        /* If this request hasn't been cancelled, do nothing */
1410        if (data->cancelled == 0)
1411                goto out_free;
1412        /* In case of error, no cleanup! */
1413        if (!data->rpc_done)
1414                goto out_free;
1415        state = nfs4_opendata_to_nfs4_state(data);
1416        if (!IS_ERR(state))
1417                nfs4_close_state(state, data->o_arg.fmode);
1418out_free:
1419        nfs4_opendata_put(data);
1420}
1421
1422static const struct rpc_call_ops nfs4_open_confirm_ops = {
1423        .rpc_call_done = nfs4_open_confirm_done,
1424        .rpc_release = nfs4_open_confirm_release,
1425};
1426
1427/*
1428 * Note: On error, nfs4_proc_open_confirm will free the struct nfs4_opendata
1429 */
1430static int _nfs4_proc_open_confirm(struct nfs4_opendata *data)
1431{
1432        struct nfs_server *server = NFS_SERVER(data->dir->d_inode);
1433        struct rpc_task *task;
1434        struct  rpc_message msg = {
1435                .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_OPEN_CONFIRM],
1436                .rpc_argp = &data->c_arg,
1437                .rpc_resp = &data->c_res,
1438                .rpc_cred = data->owner->so_cred,
1439        };
1440        struct rpc_task_setup task_setup_data = {
1441                .rpc_client = server->client,
1442                .rpc_message = &msg,
1443                .callback_ops = &nfs4_open_confirm_ops,
1444                .callback_data = data,
1445                .workqueue = nfsiod_workqueue,
1446                .flags = RPC_TASK_ASYNC,
1447        };
1448        int status;
1449
1450        kref_get(&data->kref);
1451        data->rpc_done = 0;
1452        data->rpc_status = 0;
1453        data->timestamp = jiffies;
1454        task = rpc_run_task(&task_setup_data);
1455        if (IS_ERR(task))
1456                return PTR_ERR(task);
1457        status = nfs4_wait_for_completion_rpc_task(task);
1458        if (status != 0) {
1459                data->cancelled = 1;
1460                smp_wmb();
1461        } else
1462                status = data->rpc_status;
1463        rpc_put_task(task);
1464        return status;
1465}
1466
1467static void nfs4_open_prepare(struct rpc_task *task, void *calldata)
1468{
1469        struct nfs4_opendata *data = calldata;
1470        struct nfs4_state_owner *sp = data->owner;
1471
1472        if (nfs_wait_on_sequence(data->o_arg.seqid, task) != 0)
1473                goto out_wait;
1474        /*
1475         * Check if we still need to send an OPEN call, or if we can use
1476         * a delegation instead.
1477         */
1478        if (data->state != NULL) {
1479                struct nfs_delegation *delegation;
1480
1481                if (can_open_cached(data->state, data->o_arg.fmode, data->o_arg.open_flags))
1482                        goto out_no_action;
1483                rcu_read_lock();
1484                delegation = rcu_dereference(NFS_I(data->state->inode)->delegation);
1485                if (data->o_arg.claim != NFS4_OPEN_CLAIM_DELEGATE_CUR &&
1486                    can_open_delegated(delegation, data->o_arg.fmode))
1487                        goto unlock_no_action;
1488                rcu_read_unlock();
1489        }
1490        /* Update client id. */
1491        data->o_arg.clientid = sp->so_server->nfs_client->cl_clientid;
1492        if (data->o_arg.claim == NFS4_OPEN_CLAIM_PREVIOUS) {
1493                task->tk_msg.rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_OPEN_NOATTR];
1494                data->o_arg.open_bitmap = &nfs4_open_noattr_bitmap[0];
1495                nfs_copy_fh(&data->o_res.fh, data->o_arg.fh);
1496        }
1497        data->timestamp = jiffies;
1498        if (nfs4_setup_sequence(data->o_arg.server,
1499                                &data->o_arg.seq_args,
1500                                &data->o_res.seq_res,
1501                                task) != 0)
1502                nfs_release_seqid(data->o_arg.seqid);
1503        return;
1504unlock_no_action:
1505        rcu_read_unlock();
1506out_no_action:
1507        task->tk_action = NULL;
1508out_wait:
1509        nfs4_sequence_done(task, &data->o_res.seq_res);
1510}
1511
1512static void nfs4_open_done(struct rpc_task *task, void *calldata)
1513{
1514        struct nfs4_opendata *data = calldata;
1515
1516        data->rpc_status = task->tk_status;
1517
1518        if (!nfs4_sequence_done(task, &data->o_res.seq_res))
1519                return;
1520
1521        if (task->tk_status == 0) {
1522                if (data->o_res.f_attr->valid & NFS_ATTR_FATTR_TYPE) {
1523                        switch (data->o_res.f_attr->mode & S_IFMT) {
1524                        case S_IFREG:
1525                                break;
1526                        case S_IFLNK:
1527                                data->rpc_status = -ELOOP;
1528                                break;
1529                        case S_IFDIR:
1530                                data->rpc_status = -EISDIR;
1531                                break;
1532                        default:
1533                                data->rpc_status = -ENOTDIR;
1534                        }
1535                }
1536                renew_lease(data->o_res.server, data->timestamp);
1537                if (!(data->o_res.rflags & NFS4_OPEN_RESULT_CONFIRM))
1538                        nfs_confirm_seqid(&data->owner->so_seqid, 0);
1539        }
1540        data->rpc_done = 1;
1541}
1542
1543static void nfs4_open_release(void *calldata)
1544{
1545        struct nfs4_opendata *data = calldata;
1546        struct nfs4_state *state = NULL;
1547
1548        /* If this request hasn't been cancelled, do nothing */
1549        if (data->cancelled == 0)
1550                goto out_free;
1551        /* In case of error, no cleanup! */
1552        if (data->rpc_status != 0 || !data->rpc_done)
1553                goto out_free;
1554        /* In case we need an open_confirm, no cleanup! */
1555        if (data->o_res.rflags & NFS4_OPEN_RESULT_CONFIRM)
1556                goto out_free;
1557        state = nfs4_opendata_to_nfs4_state(data);
1558        if (!IS_ERR(state))
1559                nfs4_close_state(state, data->o_arg.fmode);
1560out_free:
1561        nfs4_opendata_put(data);
1562}
1563
1564static const struct rpc_call_ops nfs4_open_ops = {
1565        .rpc_call_prepare = nfs4_open_prepare,
1566        .rpc_call_done = nfs4_open_done,
1567        .rpc_release = nfs4_open_release,
1568};
1569
1570static int nfs4_run_open_task(struct nfs4_opendata *data, int isrecover)
1571{
1572        struct inode *dir = data->dir->d_inode;
1573        struct nfs_server *server = NFS_SERVER(dir);
1574        struct nfs_openargs *o_arg = &data->o_arg;
1575        struct nfs_openres *o_res = &data->o_res;
1576        struct rpc_task *task;
1577        struct rpc_message msg = {
1578                .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_OPEN],
1579                .rpc_argp = o_arg,
1580                .rpc_resp = o_res,
1581                .rpc_cred = data->owner->so_cred,
1582        };
1583        struct rpc_task_setup task_setup_data = {
1584                .rpc_client = server->client,
1585                .rpc_message = &msg,
1586                .callback_ops = &nfs4_open_ops,
1587                .callback_data = data,
1588                .workqueue = nfsiod_workqueue,
1589                .flags = RPC_TASK_ASYNC,
1590        };
1591        int status;
1592
1593        nfs41_init_sequence(&o_arg->seq_args, &o_res->seq_res, 1);
1594        kref_get(&data->kref);
1595        data->rpc_done = 0;
1596        data->rpc_status = 0;
1597        data->cancelled = 0;
1598        if (isrecover)
1599                nfs4_set_sequence_privileged(&o_arg->seq_args);
1600        task = rpc_run_task(&task_setup_data);
1601        if (IS_ERR(task))
1602                return PTR_ERR(task);
1603        status = nfs4_wait_for_completion_rpc_task(task);
1604        if (status != 0) {
1605                data->cancelled = 1;
1606                smp_wmb();
1607        } else
1608                status = data->rpc_status;
1609        rpc_put_task(task);
1610
1611        return status;
1612}
1613
1614static int _nfs4_recover_proc_open(struct nfs4_opendata *data)
1615{
1616        struct inode *dir = data->dir->d_inode;
1617        struct nfs_openres *o_res = &data->o_res;
1618        int status;
1619
1620        status = nfs4_run_open_task(data, 1);
1621        if (status != 0 || !data->rpc_done)
1622                return status;
1623
1624        nfs_fattr_map_and_free_names(NFS_SERVER(dir), &data->f_attr);
1625
1626        if (o_res->rflags & NFS4_OPEN_RESULT_CONFIRM) {
1627                status = _nfs4_proc_open_confirm(data);
1628                if (status != 0)
1629                        return status;
1630        }
1631
1632        return status;
1633}
1634
1635static int nfs4_opendata_access(struct rpc_cred *cred,
1636                                struct nfs4_opendata *opendata,
1637                                struct nfs4_state *state, fmode_t fmode,
1638                                int openflags)
1639{
1640        struct nfs_access_entry cache;
1641        u32 mask;
1642
1643        /* access call failed or for some reason the server doesn't
1644         * support any access modes -- defer access call until later */
1645        if (opendata->o_res.access_supported == 0)
1646                return 0;
1647
1648        mask = 0;
1649        /* don't check MAY_WRITE - a newly created file may not have
1650         * write mode bits, but POSIX allows the creating process to write.
1651         * use openflags to check for exec, because fmode won't
1652         * always have FMODE_EXEC set when file open for exec. */
1653        if (openflags & __FMODE_EXEC) {
1654                /* ONLY check for exec rights */
1655                mask = MAY_EXEC;
1656        } else if (fmode & FMODE_READ)
1657                mask = MAY_READ;
1658
1659        cache.cred = cred;
1660        cache.jiffies = jiffies;
1661        nfs_access_set_mask(&cache, opendata->o_res.access_result);
1662        nfs_access_add_cache(state->inode, &cache);
1663
1664        if ((mask & ~cache.mask & (MAY_READ | MAY_EXEC)) == 0)
1665                return 0;
1666
1667        /* even though OPEN succeeded, access is denied. Close the file */
1668        nfs4_close_state(state, fmode);
1669        return -EACCES;
1670}
1671
1672/*
1673 * Note: On error, nfs4_proc_open will free the struct nfs4_opendata
1674 */
1675static int _nfs4_proc_open(struct nfs4_opendata *data)
1676{
1677        struct inode *dir = data->dir->d_inode;
1678        struct nfs_server *server = NFS_SERVER(dir);
1679        struct nfs_openargs *o_arg = &data->o_arg;
1680        struct nfs_openres *o_res = &data->o_res;
1681        int status;
1682
1683        status = nfs4_run_open_task(data, 0);
1684        if (!data->rpc_done)
1685                return status;
1686        if (status != 0) {
1687                if (status == -NFS4ERR_BADNAME &&
1688                                !(o_arg->open_flags & O_CREAT))
1689                        return -ENOENT;
1690                return status;
1691        }
1692
1693        nfs_fattr_map_and_free_names(server, &data->f_attr);
1694
1695        if (o_arg->open_flags & O_CREAT)
1696                update_changeattr(dir, &o_res->cinfo);
1697        if ((o_res->rflags & NFS4_OPEN_RESULT_LOCKTYPE_POSIX) == 0)
1698                server->caps &= ~NFS_CAP_POSIX_LOCK;
1699        if(o_res->rflags & NFS4_OPEN_RESULT_CONFIRM) {
1700                status = _nfs4_proc_open_confirm(data);
1701                if (status != 0)
1702                        return status;
1703        }
1704        if (!(o_res->f_attr->valid & NFS_ATTR_FATTR))
1705                _nfs4_proc_getattr(server, &o_res->fh, o_res->f_attr);
1706        return 0;
1707}
1708
1709static int nfs4_recover_expired_lease(struct nfs_server *server)
1710{
1711        return nfs4_client_recover_expired_lease(server->nfs_client);
1712}
1713
1714/*
1715 * OPEN_EXPIRED:
1716 *      reclaim state on the server after a network partition.
1717 *      Assumes caller holds the appropriate lock
1718 */
1719static int _nfs4_open_expired(struct nfs_open_context *ctx, struct nfs4_state *state)
1720{
1721        struct nfs4_opendata *opendata;
1722        int ret;
1723
1724        opendata = nfs4_open_recoverdata_alloc(ctx, state);
1725        if (IS_ERR(opendata))
1726                return PTR_ERR(opendata);
1727        ret = nfs4_open_recover(opendata, state);
1728        if (ret == -ESTALE)
1729                d_drop(ctx->dentry);
1730        nfs4_opendata_put(opendata);
1731        return ret;
1732}
1733
1734static int nfs4_do_open_expired(struct nfs_open_context *ctx, struct nfs4_state *state)
1735{
1736        struct nfs_server *server = NFS_SERVER(state->inode);
1737        struct nfs4_exception exception = { };
1738        int err;
1739
1740        do {
1741                err = _nfs4_open_expired(ctx, state);
1742                switch (err) {
1743                default:
1744                        goto out;
1745                case -NFS4ERR_GRACE:
1746                case -NFS4ERR_DELAY:
1747                        nfs4_handle_exception(server, err, &exception);
1748                        err = 0;
1749                }
1750        } while (exception.retry);
1751out:
1752        return err;
1753}
1754
1755static int nfs4_open_expired(struct nfs4_state_owner *sp, struct nfs4_state *state)
1756{
1757        struct nfs_open_context *ctx;
1758        int ret;
1759
1760        ctx = nfs4_state_find_open_context(state);
1761        if (IS_ERR(ctx))
1762                return PTR_ERR(ctx);
1763        ret = nfs4_do_open_expired(ctx, state);
1764        put_nfs_open_context(ctx);
1765        return ret;
1766}
1767
1768#if defined(CONFIG_NFS_V4_1)
1769static void nfs41_clear_delegation_stateid(struct nfs4_state *state)
1770{
1771        struct nfs_server *server = NFS_SERVER(state->inode);
1772        nfs4_stateid *stateid = &state->stateid;
1773        int status;
1774
1775        /* If a state reset has been done, test_stateid is unneeded */
1776        if (test_bit(NFS_DELEGATED_STATE, &state->flags) == 0)
1777                return;
1778
1779        status = nfs41_test_stateid(server, stateid);
1780        if (status != NFS_OK) {
1781                /* Free the stateid unless the server explicitly
1782                 * informs us the stateid is unrecognized. */
1783                if (status != -NFS4ERR_BAD_STATEID)
1784                        nfs41_free_stateid(server, stateid);
1785                nfs_remove_bad_delegation(state->inode);
1786
1787                write_seqlock(&state->seqlock);
1788                nfs4_stateid_copy(&state->stateid, &state->open_stateid);
1789                write_sequnlock(&state->seqlock);
1790                clear_bit(NFS_DELEGATED_STATE, &state->flags);
1791        }
1792}
1793
1794/**
1795 * nfs41_check_open_stateid - possibly free an open stateid
1796 *
1797 * @state: NFSv4 state for an inode
1798 *
1799 * Returns NFS_OK if recovery for this stateid is now finished.
1800 * Otherwise a negative NFS4ERR value is returned.
1801 */
1802static int nfs41_check_open_stateid(struct nfs4_state *state)
1803{
1804        struct nfs_server *server = NFS_SERVER(state->inode);
1805        nfs4_stateid *stateid = &state->open_stateid;
1806        int status;
1807
1808        /* If a state reset has been done, test_stateid is unneeded */
1809        if ((test_bit(NFS_O_RDONLY_STATE, &state->flags) == 0) &&
1810            (test_bit(NFS_O_WRONLY_STATE, &state->flags) == 0) &&
1811            (test_bit(NFS_O_RDWR_STATE, &state->flags) == 0))
1812                return -NFS4ERR_BAD_STATEID;
1813
1814        status = nfs41_test_stateid(server, stateid);
1815        if (status != NFS_OK) {
1816                /* Free the stateid unless the server explicitly
1817                 * informs us the stateid is unrecognized. */
1818                if (status != -NFS4ERR_BAD_STATEID)
1819                        nfs41_free_stateid(server, stateid);
1820
1821                clear_bit(NFS_O_RDONLY_STATE, &state->flags);
1822                clear_bit(NFS_O_WRONLY_STATE, &state->flags);
1823                clear_bit(NFS_O_RDWR_STATE, &state->flags);
1824        }
1825        return status;
1826}
1827
1828static int nfs41_open_expired(struct nfs4_state_owner *sp, struct nfs4_state *state)
1829{
1830        int status;
1831
1832        nfs41_clear_delegation_stateid(state);
1833        status = nfs41_check_open_stateid(state);
1834        if (status != NFS_OK)
1835                status = nfs4_open_expired(sp, state);
1836        return status;
1837}
1838#endif
1839
1840/*
1841 * on an EXCLUSIVE create, the server should send back a bitmask with FATTR4-*
1842 * fields corresponding to attributes that were used to store the verifier.
1843 * Make sure we clobber those fields in the later setattr call
1844 */
1845static inline void nfs4_exclusive_attrset(struct nfs4_opendata *opendata, struct iattr *sattr)
1846{
1847        if ((opendata->o_res.attrset[1] & FATTR4_WORD1_TIME_ACCESS) &&
1848            !(sattr->ia_valid & ATTR_ATIME_SET))
1849                sattr->ia_valid |= ATTR_ATIME;
1850
1851        if ((opendata->o_res.attrset[1] & FATTR4_WORD1_TIME_MODIFY) &&
1852            !(sattr->ia_valid & ATTR_MTIME_SET))
1853                sattr->ia_valid |= ATTR_MTIME;
1854}
1855
1856static int _nfs4_open_and_get_state(struct nfs4_opendata *opendata,
1857                fmode_t fmode,
1858                int flags,
1859                struct nfs4_state **res)
1860{
1861        struct nfs4_state_owner *sp = opendata->owner;
1862        struct nfs_server *server = sp->so_server;
1863        struct nfs4_state *state;
1864        unsigned int seq;
1865        int ret;
1866
1867        seq = raw_seqcount_begin(&sp->so_reclaim_seqcount);
1868
1869        ret = _nfs4_proc_open(opendata);
1870        if (ret != 0)
1871                goto out;
1872
1873        state = nfs4_opendata_to_nfs4_state(opendata);
1874        ret = PTR_ERR(state);
1875        if (IS_ERR(state))
1876                goto out;
1877        if (server->caps & NFS_CAP_POSIX_LOCK)
1878                set_bit(NFS_STATE_POSIX_LOCKS, &state->flags);
1879
1880        ret = nfs4_opendata_access(sp->so_cred, opendata, state, fmode, flags);
1881        if (ret != 0)
1882                goto out;
1883
1884        if (read_seqcount_retry(&sp->so_reclaim_seqcount, seq)) {
1885                nfs4_schedule_stateid_recovery(server, state);
1886                nfs4_wait_clnt_recover(server->nfs_client);
1887        }
1888        *res = state;
1889out:
1890        return ret;
1891}
1892
1893/*
1894 * Returns a referenced nfs4_state
1895 */
1896static int _nfs4_do_open(struct inode *dir,
1897                        struct dentry *dentry,
1898                        fmode_t fmode,
1899                        int flags,
1900                        struct iattr *sattr,
1901                        struct rpc_cred *cred,
1902                        struct nfs4_state **res,
1903                        struct nfs4_threshold **ctx_th)
1904{
1905        struct nfs4_state_owner  *sp;
1906        struct nfs4_state     *state = NULL;
1907        struct nfs_server       *server = NFS_SERVER(dir);
1908        struct nfs4_opendata *opendata;
1909        int status;
1910
1911        /* Protect against reboot recovery conflicts */
1912        status = -ENOMEM;
1913        sp = nfs4_get_state_owner(server, cred, GFP_KERNEL);
1914        if (sp == NULL) {
1915                dprintk("nfs4_do_open: nfs4_get_state_owner failed!\n");
1916                goto out_err;
1917        }
1918        status = nfs4_recover_expired_lease(server);
1919        if (status != 0)
1920                goto err_put_state_owner;
1921        if (dentry->d_inode != NULL)
1922                nfs4_return_incompatible_delegation(dentry->d_inode, fmode);
1923        status = -ENOMEM;
1924        opendata = nfs4_opendata_alloc(dentry, sp, fmode, flags, sattr, GFP_KERNEL);
1925        if (opendata == NULL)
1926                goto err_put_state_owner;
1927
1928        if (ctx_th && server->attr_bitmask[2] & FATTR4_WORD2_MDSTHRESHOLD) {
1929                opendata->f_attr.mdsthreshold = pnfs_mdsthreshold_alloc();
1930                if (!opendata->f_attr.mdsthreshold)
1931                        goto err_opendata_put;
1932                opendata->o_arg.open_bitmap = &nfs4_pnfs_open_bitmap[0];
1933        }
1934        if (dentry->d_inode != NULL)
1935                opendata->state = nfs4_get_open_state(dentry->d_inode, sp);
1936
1937        status = _nfs4_open_and_get_state(opendata, fmode, flags, &state);
1938        if (status != 0)
1939                goto err_opendata_put;
1940
1941        if (opendata->o_arg.open_flags & O_EXCL) {
1942                nfs4_exclusive_attrset(opendata, sattr);
1943
1944                nfs_fattr_init(opendata->o_res.f_attr);
1945                status = nfs4_do_setattr(state->inode, cred,
1946                                opendata->o_res.f_attr, sattr,
1947                                state);
1948                if (status == 0)
1949                        nfs_setattr_update_inode(state->inode, sattr);
1950                nfs_post_op_update_inode(state->inode, opendata->o_res.f_attr);
1951        }
1952
1953        if (pnfs_use_threshold(ctx_th, opendata->f_attr.mdsthreshold, server))
1954                *ctx_th = opendata->f_attr.mdsthreshold;
1955        else
1956                kfree(opendata->f_attr.mdsthreshold);
1957        opendata->f_attr.mdsthreshold = NULL;
1958
1959        nfs4_opendata_put(opendata);
1960        nfs4_put_state_owner(sp);
1961        *res = state;
1962        return 0;
1963err_opendata_put:
1964        kfree(opendata->f_attr.mdsthreshold);
1965        nfs4_opendata_put(opendata);
1966err_put_state_owner:
1967        nfs4_put_state_owner(sp);
1968out_err:
1969        *res = NULL;
1970        return status;
1971}
1972
1973
1974static struct nfs4_state *nfs4_do_open(struct inode *dir,
1975                                        struct dentry *dentry,
1976                                        fmode_t fmode,
1977                                        int flags,
1978                                        struct iattr *sattr,
1979                                        struct rpc_cred *cred,
1980                                        struct nfs4_threshold **ctx_th)
1981{
1982        struct nfs4_exception exception = { };
1983        struct nfs4_state *res;
1984        int status;
1985
1986        fmode &= FMODE_READ|FMODE_WRITE|FMODE_EXEC;
1987        do {
1988                status = _nfs4_do_open(dir, dentry, fmode, flags, sattr, cred,
1989                                       &res, ctx_th);
1990                if (status == 0)
1991                        break;
1992                /* NOTE: BAD_SEQID means the server and client disagree about the
1993                 * book-keeping w.r.t. state-changing operations
1994                 * (OPEN/CLOSE/LOCK/LOCKU...)
1995                 * It is actually a sign of a bug on the client or on the server.
1996                 *
1997                 * If we receive a BAD_SEQID error in the particular case of
1998                 * doing an OPEN, we assume that nfs_increment_open_seqid() will
1999                 * have unhashed the old state_owner for us, and that we can
2000                 * therefore safely retry using a new one. We should still warn
2001                 * the user though...
2002                 */
2003                if (status == -NFS4ERR_BAD_SEQID) {
2004                        pr_warn_ratelimited("NFS: v4 server %s "
2005                                        " returned a bad sequence-id error!\n",
2006                                        NFS_SERVER(dir)->nfs_client->cl_hostname);
2007                        exception.retry = 1;
2008                        continue;
2009                }
2010                /*
2011                 * BAD_STATEID on OPEN means that the server cancelled our
2012                 * state before it received the OPEN_CONFIRM.
2013                 * Recover by retrying the request as per the discussion
2014                 * on Page 181 of RFC3530.
2015                 */
2016                if (status == -NFS4ERR_BAD_STATEID) {
2017                        exception.retry = 1;
2018                        continue;
2019                }
2020                if (status == -EAGAIN) {
2021                        /* We must have found a delegation */
2022                        exception.retry = 1;
2023                        continue;
2024                }
2025                res = ERR_PTR(nfs4_handle_exception(NFS_SERVER(dir),
2026                                        status, &exception));
2027        } while (exception.retry);
2028        return res;
2029}
2030
2031static int _nfs4_do_setattr(struct inode *inode, struct rpc_cred *cred,
2032                            struct nfs_fattr *fattr, struct iattr *sattr,
2033                            struct nfs4_state *state)
2034{
2035        struct nfs_server *server = NFS_SERVER(inode);
2036        struct nfs_setattrargs  arg = {
2037                .fh             = NFS_FH(inode),
2038                .iap            = sattr,
2039                .server         = server,
2040                .bitmask = server->attr_bitmask,
2041        };
2042        struct nfs_setattrres  res = {
2043                .fattr          = fattr,
2044                .server         = server,
2045        };
2046        struct rpc_message msg = {
2047                .rpc_proc       = &nfs4_procedures[NFSPROC4_CLNT_SETATTR],
2048                .rpc_argp       = &arg,
2049                .rpc_resp       = &res,
2050                .rpc_cred       = cred,
2051        };
2052        unsigned long timestamp = jiffies;
2053        int status;
2054
2055        nfs_fattr_init(fattr);
2056
2057        if (state != NULL) {
2058                struct nfs_lockowner lockowner = {
2059                        .l_owner = current->files,
2060                        .l_pid = current->tgid,
2061                };
2062                nfs4_select_rw_stateid(&arg.stateid, state, FMODE_WRITE,
2063                                &lockowner);
2064        } else if (nfs4_copy_delegation_stateid(&arg.stateid, inode,
2065                                FMODE_WRITE)) {
2066                /* Use that stateid */
2067        } else
2068                nfs4_stateid_copy(&arg.stateid, &zero_stateid);
2069
2070        status = nfs4_call_sync(server->client, server, &msg, &arg.seq_args, &res.seq_res, 1);
2071        if (status == 0 && state != NULL)
2072                renew_lease(server, timestamp);
2073        return status;
2074}
2075
2076static int nfs4_do_setattr(struct inode *inode, struct rpc_cred *cred,
2077                           struct nfs_fattr *fattr, struct iattr *sattr,
2078                           struct nfs4_state *state)
2079{
2080        struct nfs_server *server = NFS_SERVER(inode);
2081        struct nfs4_exception exception = {
2082                .state = state,
2083                .inode = inode,
2084        };
2085        int err;
2086        do {
2087                err = _nfs4_do_setattr(inode, cred, fattr, sattr, state);
2088                switch (err) {
2089                case -NFS4ERR_OPENMODE:
2090                        if (state && !(state->state & FMODE_WRITE)) {
2091                                err = -EBADF;
2092                                if (sattr->ia_valid & ATTR_OPEN)
2093                                        err = -EACCES;
2094                                goto out;
2095                        }
2096                }
2097                err = nfs4_handle_exception(server, err, &exception);
2098        } while (exception.retry);
2099out:
2100        return err;
2101}
2102
2103struct nfs4_closedata {
2104        struct inode *inode;
2105        struct nfs4_state *state;
2106        struct nfs_closeargs arg;
2107        struct nfs_closeres res;
2108        struct nfs_fattr fattr;
2109        unsigned long timestamp;
2110        bool roc;
2111        u32 roc_barrier;
2112};
2113
2114static void nfs4_free_closedata(void *data)
2115{
2116        struct nfs4_closedata *calldata = data;
2117        struct nfs4_state_owner *sp = calldata->state->owner;
2118        struct super_block *sb = calldata->state->inode->i_sb;
2119
2120        if (calldata->roc)
2121                pnfs_roc_release(calldata->state->inode);
2122        nfs4_put_open_state(calldata->state);
2123        nfs_free_seqid(calldata->arg.seqid);
2124        nfs4_put_state_owner(sp);
2125        nfs_sb_deactive(sb);
2126        kfree(calldata);
2127}
2128
2129static void nfs4_close_clear_stateid_flags(struct nfs4_state *state,
2130                fmode_t fmode)
2131{
2132        spin_lock(&state->owner->so_lock);
2133        if (!(fmode & FMODE_READ))
2134                clear_bit(NFS_O_RDONLY_STATE, &state->flags);
2135        if (!(fmode & FMODE_WRITE))
2136                clear_bit(NFS_O_WRONLY_STATE, &state->flags);
2137        clear_bit(NFS_O_RDWR_STATE, &state->flags);
2138        spin_unlock(&state->owner->so_lock);
2139}
2140
2141static void nfs4_close_done(struct rpc_task *task, void *data)
2142{
2143        struct nfs4_closedata *calldata = data;
2144        struct nfs4_state *state = calldata->state;
2145        struct nfs_server *server = NFS_SERVER(calldata->inode);
2146
2147        dprintk("%s: begin!\n", __func__);
2148        if (!nfs4_sequence_done(task, &calldata->res.seq_res))
2149                return;
2150        /* hmm. we are done with the inode, and in the process of freeing
2151         * the state_owner. we keep this around to process errors
2152         */
2153        switch (task->tk_status) {
2154                case 0:
2155                        if (calldata->roc)
2156                                pnfs_roc_set_barrier(state->inode,
2157                                                     calldata->roc_barrier);
2158                        nfs_set_open_stateid(state, &calldata->res.stateid, 0);
2159                        renew_lease(server, calldata->timestamp);
2160                        nfs4_close_clear_stateid_flags(state,
2161                                        calldata->arg.fmode);
2162                        break;
2163                case -NFS4ERR_STALE_STATEID:
2164                case -NFS4ERR_OLD_STATEID:
2165                case -NFS4ERR_BAD_STATEID:
2166                case -NFS4ERR_EXPIRED:
2167                        if (calldata->arg.fmode == 0)
2168                                break;
2169                default:
2170                        if (nfs4_async_handle_error(task, server, state) == -EAGAIN)
2171                                rpc_restart_call_prepare(task);
2172        }
2173        nfs_release_seqid(calldata->arg.seqid);
2174        nfs_refresh_inode(calldata->inode, calldata->res.fattr);
2175        dprintk("%s: done, ret = %d!\n", __func__, task->tk_status);
2176}
2177
2178static void nfs4_close_prepare(struct rpc_task *task, void *data)
2179{
2180        struct nfs4_closedata *calldata = data;
2181        struct nfs4_state *state = calldata->state;
2182        struct inode *inode = calldata->inode;
2183        int call_close = 0;
2184
2185        dprintk("%s: begin!\n", __func__);
2186        if (nfs_wait_on_sequence(calldata->arg.seqid, task) != 0)
2187                goto out_wait;
2188
2189        task->tk_msg.rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_OPEN_DOWNGRADE];
2190        calldata->arg.fmode = FMODE_READ|FMODE_WRITE;
2191        spin_lock(&state->owner->so_lock);
2192        /* Calculate the change in open mode */
2193        if (state->n_rdwr == 0) {
2194                if (state->n_rdonly == 0) {
2195                        call_close |= test_bit(NFS_O_RDONLY_STATE, &state->flags);
2196                        call_close |= test_bit(NFS_O_RDWR_STATE, &state->flags);
2197                        calldata->arg.fmode &= ~FMODE_READ;
2198                }
2199                if (state->n_wronly == 0) {
2200                        call_close |= test_bit(NFS_O_WRONLY_STATE, &state->flags);
2201                        call_close |= test_bit(NFS_O_RDWR_STATE, &state->flags);
2202                        calldata->arg.fmode &= ~FMODE_WRITE;
2203                }
2204        }
2205        spin_unlock(&state->owner->so_lock);
2206
2207        if (!call_close) {
2208                /* Note: exit _without_ calling nfs4_close_done */
2209                goto out_no_action;
2210        }
2211
2212        if (calldata->arg.fmode == 0) {
2213                task->tk_msg.rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_CLOSE];
2214                if (calldata->roc &&
2215                    pnfs_roc_drain(inode, &calldata->roc_barrier, task))
2216                        goto out_wait;
2217        }
2218
2219        nfs_fattr_init(calldata->res.fattr);
2220        calldata->timestamp = jiffies;
2221        if (nfs4_setup_sequence(NFS_SERVER(inode),
2222                                &calldata->arg.seq_args,
2223                                &calldata->res.seq_res,
2224                                task) != 0)
2225                nfs_release_seqid(calldata->arg.seqid);
2226        dprintk("%s: done!\n", __func__);
2227        return;
2228out_no_action:
2229        task->tk_action = NULL;
2230out_wait:
2231        nfs4_sequence_done(task, &calldata->res.seq_res);
2232}
2233
2234static const struct rpc_call_ops nfs4_close_ops = {
2235        .rpc_call_prepare = nfs4_close_prepare,
2236        .rpc_call_done = nfs4_close_done,
2237        .rpc_release = nfs4_free_closedata,
2238};
2239
2240/* 
2241 * It is possible for data to be read/written from a mem-mapped file 
2242 * after the sys_close call (which hits the vfs layer as a flush).
2243 * This means that we can't safely call nfsv4 close on a file until 
2244 * the inode is cleared. This in turn means that we are not good
2245 * NFSv4 citizens - we do not indicate to the server to update the file's 
2246 * share state even when we are done with one of the three share 
2247 * stateid's in the inode.
2248 *
2249 * NOTE: Caller must be holding the sp->so_owner semaphore!
2250 */
2251int nfs4_do_close(struct nfs4_state *state, gfp_t gfp_mask, int wait)
2252{
2253        struct nfs_server *server = NFS_SERVER(state->inode);
2254        struct nfs4_closedata *calldata;
2255        struct nfs4_state_owner *sp = state->owner;
2256        struct rpc_task *task;
2257        struct rpc_message msg = {
2258                .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_CLOSE],
2259                .rpc_cred = state->owner->so_cred,
2260        };
2261        struct rpc_task_setup task_setup_data = {
2262                .rpc_client = server->client,
2263                .rpc_message = &msg,
2264                .callback_ops = &nfs4_close_ops,
2265                .workqueue = nfsiod_workqueue,
2266                .flags = RPC_TASK_ASYNC,
2267        };
2268        int status = -ENOMEM;
2269
2270        calldata = kzalloc(sizeof(*calldata), gfp_mask);
2271        if (calldata == NULL)
2272                goto out;
2273        nfs41_init_sequence(&calldata->arg.seq_args, &calldata->res.seq_res, 1);
2274        calldata->inode = state->inode;
2275        calldata->state = state;
2276        calldata->arg.fh = NFS_FH(state->inode);
2277        calldata->arg.stateid = &state->open_stateid;
2278        /* Serialization for the sequence id */
2279        calldata->arg.seqid = nfs_alloc_seqid(&state->owner->so_seqid, gfp_mask);
2280        if (calldata->arg.seqid == NULL)
2281                goto out_free_calldata;
2282        calldata->arg.fmode = 0;
2283        calldata->arg.bitmask = server->cache_consistency_bitmask;
2284        calldata->res.fattr = &calldata->fattr;
2285        calldata->res.seqid = calldata->arg.seqid;
2286        calldata->res.server = server;
2287        calldata->roc = pnfs_roc(state->inode);
2288        nfs_sb_active(calldata->inode->i_sb);
2289
2290        msg.rpc_argp = &calldata->arg;
2291        msg.rpc_resp = &calldata->res;
2292        task_setup_data.callback_data = calldata;
2293        task = rpc_run_task(&task_setup_data);
2294        if (IS_ERR(task))
2295                return PTR_ERR(task);
2296        status = 0;
2297        if (wait)
2298                status = rpc_wait_for_completion_task(task);
2299        rpc_put_task(task);
2300        return status;
2301out_free_calldata:
2302        kfree(calldata);
2303out:
2304        nfs4_put_open_state(state);
2305        nfs4_put_state_owner(sp);
2306        return status;
2307}
2308
2309static struct inode *
2310nfs4_atomic_open(struct inode *dir, struct nfs_open_context *ctx, int open_flags, struct iattr *attr)
2311{
2312        struct nfs4_state *state;
2313
2314        /* Protect against concurrent sillydeletes */
2315        state = nfs4_do_open(dir, ctx->dentry, ctx->mode, open_flags, attr,
2316                             ctx->cred, &ctx->mdsthreshold);
2317        if (IS_ERR(state))
2318                return ERR_CAST(state);
2319        ctx->state = state;
2320        return igrab(state->inode);
2321}
2322
2323static void nfs4_close_context(struct nfs_open_context *ctx, int is_sync)
2324{
2325        if (ctx->state == NULL)
2326                return;
2327        if (is_sync)
2328                nfs4_close_sync(ctx->state, ctx->mode);
2329        else
2330                nfs4_close_state(ctx->state, ctx->mode);
2331}
2332
2333static int _nfs4_server_capabilities(struct nfs_server *server, struct nfs_fh *fhandle)
2334{
2335        struct nfs4_server_caps_arg args = {
2336                .fhandle = fhandle,
2337        };
2338        struct nfs4_server_caps_res res = {};
2339        struct rpc_message msg = {
2340                .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_SERVER_CAPS],
2341                .rpc_argp = &args,
2342                .rpc_resp = &res,
2343        };
2344        int status;
2345
2346        status = nfs4_call_sync(server->client, server, &msg, &args.seq_args, &res.seq_res, 0);
2347        if (status == 0) {
2348                memcpy(server->attr_bitmask, res.attr_bitmask, sizeof(server->attr_bitmask));
2349                server->caps &= ~(NFS_CAP_ACLS|NFS_CAP_HARDLINKS|
2350                                NFS_CAP_SYMLINKS|NFS_CAP_FILEID|
2351                                NFS_CAP_MODE|NFS_CAP_NLINK|NFS_CAP_OWNER|
2352                                NFS_CAP_OWNER_GROUP|NFS_CAP_ATIME|
2353                                NFS_CAP_CTIME|NFS_CAP_MTIME);
2354                if (res.attr_bitmask[0] & FATTR4_WORD0_ACL)
2355                        server->caps |= NFS_CAP_ACLS;
2356                if (res.has_links != 0)
2357                        server->caps |= NFS_CAP_HARDLINKS;
2358                if (res.has_symlinks != 0)
2359                        server->caps |= NFS_CAP_SYMLINKS;
2360                if (res.attr_bitmask[0] & FATTR4_WORD0_FILEID)
2361                        server->caps |= NFS_CAP_FILEID;
2362                if (res.attr_bitmask[1] & FATTR4_WORD1_MODE)
2363                        server->caps |= NFS_CAP_MODE;
2364                if (res.attr_bitmask[1] & FATTR4_WORD1_NUMLINKS)
2365                        server->caps |= NFS_CAP_NLINK;
2366                if (res.attr_bitmask[1] & FATTR4_WORD1_OWNER)
2367                        server->caps |= NFS_CAP_OWNER;
2368                if (res.attr_bitmask[1] & FATTR4_WORD1_OWNER_GROUP)
2369                        server->caps |= NFS_CAP_OWNER_GROUP;
2370                if (res.attr_bitmask[1] & FATTR4_WORD1_TIME_ACCESS)
2371                        server->caps |= NFS_CAP_ATIME;
2372                if (res.attr_bitmask[1] & FATTR4_WORD1_TIME_METADATA)
2373                        server->caps |= NFS_CAP_CTIME;
2374                if (res.attr_bitmask[1] & FATTR4_WORD1_TIME_MODIFY)
2375                        server->caps |= NFS_CAP_MTIME;
2376
2377                memcpy(server->cache_consistency_bitmask, res.attr_bitmask, sizeof(server->cache_consistency_bitmask));
2378                server->cache_consistency_bitmask[0] &= FATTR4_WORD0_CHANGE|FATTR4_WORD0_SIZE;
2379                server->cache_consistency_bitmask[1] &= FATTR4_WORD1_TIME_METADATA|FATTR4_WORD1_TIME_MODIFY;
2380                server->acl_bitmask = res.acl_bitmask;
2381                server->fh_expire_type = res.fh_expire_type;
2382        }
2383
2384        return status;
2385}
2386
2387int nfs4_server_capabilities(struct nfs_server *server, struct nfs_fh *fhandle)
2388{
2389        struct nfs4_exception exception = { };
2390        int err;
2391        do {
2392                err = nfs4_handle_exception(server,
2393                                _nfs4_server_capabilities(server, fhandle),
2394                                &exception);
2395        } while (exception.retry);
2396        return err;
2397}
2398
2399static int _nfs4_lookup_root(struct nfs_server *server, struct nfs_fh *fhandle,
2400                struct nfs_fsinfo *info)
2401{
2402        struct nfs4_lookup_root_arg args = {
2403                .bitmask = nfs4_fattr_bitmap,
2404        };
2405        struct nfs4_lookup_res res = {
2406                .server = server,
2407                .fattr = info->fattr,
2408                .fh = fhandle,
2409        };
2410        struct rpc_message msg = {
2411                .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_LOOKUP_ROOT],
2412                .rpc_argp = &args,
2413                .rpc_resp = &res,
2414        };
2415
2416        nfs_fattr_init(info->fattr);
2417        return nfs4_call_sync(server->client, server, &msg, &args.seq_args, &res.seq_res, 0);
2418}
2419
2420static int nfs4_lookup_root(struct nfs_server *server, struct nfs_fh *fhandle,
2421                struct nfs_fsinfo *info)
2422{
2423        struct nfs4_exception exception = { };
2424        int err;
2425        do {
2426                err = _nfs4_lookup_root(server, fhandle, info);
2427                switch (err) {
2428                case 0:
2429                case -NFS4ERR_WRONGSEC:
2430                        goto out;
2431                default:
2432                        err = nfs4_handle_exception(server, err, &exception);
2433                }
2434        } while (exception.retry);
2435out:
2436        return err;
2437}
2438
2439static int nfs4_lookup_root_sec(struct nfs_server *server, struct nfs_fh *fhandle,
2440                                struct nfs_fsinfo *info, rpc_authflavor_t flavor)
2441{
2442        struct rpc_auth *auth;
2443        int ret;
2444
2445        auth = rpcauth_create(flavor, server->client);
2446        if (IS_ERR(auth)) {
2447                ret = -EIO;
2448                goto out;
2449        }
2450        ret = nfs4_lookup_root(server, fhandle, info);
2451out:
2452        return ret;
2453}
2454
2455static int nfs4_find_root_sec(struct nfs_server *server, struct nfs_fh *fhandle,
2456                              struct nfs_fsinfo *info)
2457{
2458        int i, len, status = 0;
2459        rpc_authflavor_t flav_array[NFS_MAX_SECFLAVORS];
2460
2461        len = rpcauth_list_flavors(flav_array, ARRAY_SIZE(flav_array));
2462        if (len < 0)
2463                return len;
2464
2465        for (i = 0; i < len; i++) {
2466                /* AUTH_UNIX is the default flavor if none was specified,
2467                 * thus has already been tried. */
2468                if (flav_array[i] == RPC_AUTH_UNIX)
2469                        continue;
2470
2471                status = nfs4_lookup_root_sec(server, fhandle, info, flav_array[i]);
2472                if (status == -NFS4ERR_WRONGSEC || status == -EACCES)
2473                        continue;
2474                break;
2475        }
2476        /*
2477         * -EACCESS could mean that the user doesn't have correct permissions
2478         * to access the mount.  It could also mean that we tried to mount
2479         * with a gss auth flavor, but rpc.gssd isn't running.  Either way,
2480         * existing mount programs don't handle -EACCES very well so it should
2481         * be mapped to -EPERM instead.
2482         */
2483        if (status == -EACCES)
2484                status = -EPERM;
2485        return status;
2486}
2487
2488/*
2489 * get the file handle for the "/" directory on the server
2490 */
2491int nfs4_proc_get_rootfh(struct nfs_server *server, struct nfs_fh *fhandle,
2492                         struct nfs_fsinfo *info)
2493{
2494        int minor_version = server->nfs_client->cl_minorversion;
2495        int status = nfs4_lookup_root(server, fhandle, info);
2496        if ((status == -NFS4ERR_WRONGSEC) && !(server->flags & NFS_MOUNT_SECFLAVOUR))
2497                /*
2498                 * A status of -NFS4ERR_WRONGSEC will be mapped to -EPERM
2499                 * by nfs4_map_errors() as this function exits.
2500                 */
2501                status = nfs_v4_minor_ops[minor_version]->find_root_sec(server, fhandle, info);
2502        if (status == 0)
2503                status = nfs4_server_capabilities(server, fhandle);
2504        if (status == 0)
2505                status = nfs4_do_fsinfo(server, fhandle, info);
2506        return nfs4_map_errors(status);
2507}
2508
2509static int nfs4_proc_get_root(struct nfs_server *server, struct nfs_fh *mntfh,
2510                              struct nfs_fsinfo *info)
2511{
2512        int error;
2513        struct nfs_fattr *fattr = info->fattr;
2514
2515        error = nfs4_server_capabilities(server, mntfh);
2516        if (error < 0) {
2517                dprintk("nfs4_get_root: getcaps error = %d\n", -error);
2518                return error;
2519        }
2520
2521        error = nfs4_proc_getattr(server, mntfh, fattr);
2522        if (error < 0) {
2523                dprintk("nfs4_get_root: getattr error = %d\n", -error);
2524                return error;
2525        }
2526
2527        if (fattr->valid & NFS_ATTR_FATTR_FSID &&
2528            !nfs_fsid_equal(&server->fsid, &fattr->fsid))
2529                memcpy(&server->fsid, &fattr->fsid, sizeof(server->fsid));
2530
2531        return error;
2532}
2533
2534/*
2535 * Get locations and (maybe) other attributes of a referral.
2536 * Note that we'll actually follow the referral later when
2537 * we detect fsid mismatch in inode revalidation
2538 */
2539static int nfs4_get_referral(struct rpc_clnt *client, struct inode *dir,
2540                             const struct qstr *name, struct nfs_fattr *fattr,
2541                             struct nfs_fh *fhandle)
2542{
2543        int status = -ENOMEM;
2544        struct page *page = NULL;
2545        struct nfs4_fs_locations *locations = NULL;
2546
2547        page = alloc_page(GFP_KERNEL);
2548        if (page == NULL)
2549                goto out;
2550        locations = kmalloc(sizeof(struct nfs4_fs_locations), GFP_KERNEL);
2551        if (locations == NULL)
2552                goto out;
2553
2554        status = nfs4_proc_fs_locations(client, dir, name, locations, page);
2555        if (status != 0)
2556                goto out;
2557        /* Make sure server returned a different fsid for the referral */
2558        if (nfs_fsid_equal(&NFS_SERVER(dir)->fsid, &locations->fattr.fsid)) {
2559                dprintk("%s: server did not return a different fsid for"
2560                        " a referral at %s\n", __func__, name->name);
2561                status = -EIO;
2562                goto out;
2563        }
2564        /* Fixup attributes for the nfs_lookup() call to nfs_fhget() */
2565        nfs_fixup_referral_attributes(&locations->fattr);
2566
2567        /* replace the lookup nfs_fattr with the locations nfs_fattr */
2568        memcpy(fattr, &locations->fattr, sizeof(struct nfs_fattr));
2569        memset(fhandle, 0, sizeof(struct nfs_fh));
2570out:
2571        if (page)
2572                __free_page(page);
2573        kfree(locations);
2574        return status;
2575}
2576
2577static int _nfs4_proc_getattr(struct nfs_server *server, struct nfs_fh *fhandle, struct nfs_fattr *fattr)
2578{
2579        struct nfs4_getattr_arg args = {
2580                .fh = fhandle,
2581                .bitmask = server->attr_bitmask,
2582        };
2583        struct nfs4_getattr_res res = {
2584                .fattr = fattr,
2585                .server = server,
2586        };
2587        struct rpc_message msg = {
2588                .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_GETATTR],
2589                .rpc_argp = &args,
2590                .rpc_resp = &res,
2591        };
2592        
2593        nfs_fattr_init(fattr);
2594        return nfs4_call_sync(server->client, server, &msg, &args.seq_args, &res.seq_res, 0);
2595}
2596
2597static int nfs4_proc_getattr(struct nfs_server *server, struct nfs_fh *fhandle, struct nfs_fattr *fattr)
2598{
2599        struct nfs4_exception exception = { };
2600        int err;
2601        do {
2602                err = nfs4_handle_exception(server,
2603                                _nfs4_proc_getattr(server, fhandle, fattr),
2604                                &exception);
2605        } while (exception.retry);
2606        return err;
2607}
2608
2609/* 
2610 * The file is not closed if it is opened due to the a request to change
2611 * the size of the file. The open call will not be needed once the
2612 * VFS layer lookup-intents are implemented.
2613 *
2614 * Close is called when the inode is destroyed.
2615 * If we haven't opened the file for O_WRONLY, we
2616 * need to in the size_change case to obtain a stateid.
2617 *
2618 * Got race?
2619 * Because OPEN is always done by name in nfsv4, it is
2620 * possible that we opened a different file by the same
2621 * name.  We can recognize this race condition, but we
2622 * can't do anything about it besides returning an error.
2623 *
2624 * This will be fixed with VFS changes (lookup-intent).
2625 */
2626static int
2627nfs4_proc_setattr(struct dentry *dentry, struct nfs_fattr *fattr,
2628                  struct iattr *sattr)
2629{
2630        struct inode *inode = dentry->d_inode;
2631        struct rpc_cred *cred = NULL;
2632        struct nfs4_state *state = NULL;
2633        int status;
2634
2635        if (pnfs_ld_layoutret_on_setattr(inode))
2636                pnfs_commit_and_return_layout(inode);
2637
2638        nfs_fattr_init(fattr);
2639        
2640        /* Deal with open(O_TRUNC) */
2641        if (sattr->ia_valid & ATTR_OPEN)
2642                sattr->ia_valid &= ~(ATTR_MTIME|ATTR_CTIME|ATTR_OPEN);
2643
2644        /* Optimization: if the end result is no change, don't RPC */
2645        if ((sattr->ia_valid & ~(ATTR_FILE)) == 0)
2646                return 0;
2647
2648        /* Search for an existing open(O_WRITE) file */
2649        if (sattr->ia_valid & ATTR_FILE) {
2650                struct nfs_open_context *ctx;
2651
2652                ctx = nfs_file_open_context(sattr->ia_file);
2653                if (ctx) {
2654                        cred = ctx->cred;
2655                        state = ctx->state;
2656                }
2657        }
2658
2659        status = nfs4_do_setattr(inode, cred, fattr, sattr, state);
2660        if (status == 0)
2661                nfs_setattr_update_inode(inode, sattr);
2662        return status;
2663}
2664
2665static int _nfs4_proc_lookup(struct rpc_clnt *clnt, struct inode *dir,
2666                const struct qstr *name, struct nfs_fh *fhandle,
2667                struct nfs_fattr *fattr)
2668{
2669        struct nfs_server *server = NFS_SERVER(dir);
2670        int                    status;
2671        struct nfs4_lookup_arg args = {
2672                .bitmask = server->attr_bitmask,
2673                .dir_fh = NFS_FH(dir),
2674                .name = name,
2675        };
2676        struct nfs4_lookup_res res = {
2677                .server = server,
2678                .fattr = fattr,
2679                .fh = fhandle,
2680        };
2681        struct rpc_message msg = {
2682                .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_LOOKUP],
2683                .rpc_argp = &args,
2684                .rpc_resp = &res,
2685        };
2686
2687        nfs_fattr_init(fattr);
2688
2689        dprintk("NFS call  lookup %s\n", name->name);
2690        status = nfs4_call_sync(clnt, server, &msg, &args.seq_args, &res.seq_res, 0);
2691        dprintk("NFS reply lookup: %d\n", status);
2692        return status;
2693}
2694
2695static void nfs_fixup_secinfo_attributes(struct nfs_fattr *fattr)
2696{
2697        fattr->valid |= NFS_ATTR_FATTR_TYPE | NFS_ATTR_FATTR_MODE |
2698                NFS_ATTR_FATTR_NLINK | NFS_ATTR_FATTR_MOUNTPOINT;
2699        fattr->mode = S_IFDIR | S_IRUGO | S_IXUGO;
2700        fattr->nlink = 2;
2701}
2702
2703static int nfs4_proc_lookup_common(struct rpc_clnt **clnt, struct inode *dir,
2704                                   struct qstr *name, struct nfs_fh *fhandle,
2705                                   struct nfs_fattr *fattr)
2706{
2707        struct nfs4_exception exception = { };
2708        struct rpc_clnt *client = *clnt;
2709        int err;
2710        do {
2711                err = _nfs4_proc_lookup(client, dir, name, fhandle, fattr);
2712                switch (err) {
2713                case -NFS4ERR_BADNAME:
2714                        err = -ENOENT;
2715                        goto out;
2716                case -NFS4ERR_MOVED:
2717                        err = nfs4_get_referral(client, dir, name, fattr, fhandle);
2718                        goto out;
2719                case -NFS4ERR_WRONGSEC:
2720                        err = -EPERM;
2721                        if (client != *clnt)
2722                                goto out;
2723
2724                        client = nfs4_create_sec_client(client, dir, name);
2725                        if (IS_ERR(client))
2726                                return PTR_ERR(client);
2727
2728                        exception.retry = 1;
2729                        break;
2730                default:
2731                        err = nfs4_handle_exception(NFS_SERVER(dir), err, &exception);
2732                }
2733        } while (exception.retry);
2734
2735out:
2736        if (err == 0)
2737                *clnt = client;
2738        else if (client != *clnt)
2739                rpc_shutdown_client(client);
2740
2741        return err;
2742}
2743
2744static int nfs4_proc_lookup(struct inode *dir, struct qstr *name,
2745                            struct nfs_fh *fhandle, struct nfs_fattr *fattr)
2746{
2747        int status;
2748        struct rpc_clnt *client = NFS_CLIENT(dir);
2749
2750        status = nfs4_proc_lookup_common(&client, dir, name, fhandle, fattr);
2751        if (client != NFS_CLIENT(dir)) {
2752                rpc_shutdown_client(client);
2753                nfs_fixup_secinfo_attributes(fattr);
2754        }
2755        return status;
2756}
2757
2758struct rpc_clnt *
2759nfs4_proc_lookup_mountpoint(struct inode *dir, struct qstr *name,
2760                            struct nfs_fh *fhandle, struct nfs_fattr *fattr)
2761{
2762        int status;
2763        struct rpc_clnt *client = rpc_clone_client(NFS_CLIENT(dir));
2764
2765        status = nfs4_proc_lookup_common(&client, dir, name, fhandle, fattr);
2766        if (status < 0) {
2767                rpc_shutdown_client(client);
2768                return ERR_PTR(status);
2769        }
2770        return client;
2771}
2772
2773static int _nfs4_proc_access(struct inode *inode, struct nfs_access_entry *entry)
2774{
2775        struct nfs_server *server = NFS_SERVER(inode);
2776        struct nfs4_accessargs args = {
2777                .fh = NFS_FH(inode),
2778                .bitmask = server->cache_consistency_bitmask,
2779        };
2780        struct nfs4_accessres res = {
2781                .server = server,
2782        };
2783        struct rpc_message msg = {
2784                .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_ACCESS],
2785                .rpc_argp = &args,
2786                .rpc_resp = &res,
2787                .rpc_cred = entry->cred,
2788        };
2789        int mode = entry->mask;
2790        int status;
2791
2792        /*
2793         * Determine which access bits we want to ask for...
2794         */
2795        if (mode & MAY_READ)
2796                args.access |= NFS4_ACCESS_READ;
2797        if (S_ISDIR(inode->i_mode)) {
2798                if (mode & MAY_WRITE)
2799                        args.access |= NFS4_ACCESS_MODIFY | NFS4_ACCESS_EXTEND | NFS4_ACCESS_DELETE;
2800                if (mode & MAY_EXEC)
2801                        args.access |= NFS4_ACCESS_LOOKUP;
2802        } else {
2803                if (mode & MAY_WRITE)
2804                        args.access |= NFS4_ACCESS_MODIFY | NFS4_ACCESS_EXTEND;
2805                if (mode & MAY_EXEC)
2806                        args.access |= NFS4_ACCESS_EXECUTE;
2807        }
2808
2809        res.fattr = nfs_alloc_fattr();
2810        if (res.fattr == NULL)
2811                return -ENOMEM;
2812
2813        status = nfs4_call_sync(server->client, server, &msg, &args.seq_args, &res.seq_res, 0);
2814        if (!status) {
2815                nfs_access_set_mask(entry, res.access);
2816                nfs_refresh_inode(inode, res.fattr);
2817        }
2818        nfs_free_fattr(res.fattr);
2819        return status;
2820}
2821
2822static int nfs4_proc_access(struct inode *inode, struct nfs_access_entry *entry)
2823{
2824        struct nfs4_exception exception = { };
2825        int err;
2826        do {
2827                err = nfs4_handle_exception(NFS_SERVER(inode),
2828                                _nfs4_proc_access(inode, entry),
2829                                &exception);
2830        } while (exception.retry);
2831        return err;
2832}
2833
2834/*
2835 * TODO: For the time being, we don't try to get any attributes
2836 * along with any of the zero-copy operations READ, READDIR,
2837 * READLINK, WRITE.
2838 *
2839 * In the case of the first three, we want to put the GETATTR
2840 * after the read-type operation -- this is because it is hard
2841 * to predict the length of a GETATTR response in v4, and thus
2842 * align the READ data correctly.  This means that the GETATTR
2843 * may end up partially falling into the page cache, and we should
2844 * shift it into the 'tail' of the xdr_buf before processing.
2845 * To do this efficiently, we need to know the total length
2846 * of data received, which doesn't seem to be available outside
2847 * of the RPC layer.
2848 *
2849 * In the case of WRITE, we also want to put the GETATTR after
2850 * the operation -- in this case because we want to make sure
2851 * we get the post-operation mtime and size.
2852 *
2853 * Both of these changes to the XDR layer would in fact be quite
2854 * minor, but I decided to leave them for a subsequent patch.
2855 */
2856static int _nfs4_proc_readlink(struct inode *inode, struct page *page,
2857                unsigned int pgbase, unsigned int pglen)
2858{
2859        struct nfs4_readlink args = {
2860                .fh       = NFS_FH(inode),
2861                .pgbase   = pgbase,
2862                .pglen    = pglen,
2863                .pages    = &page,
2864        };
2865        struct nfs4_readlink_res res;
2866        struct rpc_message msg = {
2867                .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_READLINK],
2868                .rpc_argp = &args,
2869                .rpc_resp = &res,
2870        };
2871
2872        return nfs4_call_sync(NFS_SERVER(inode)->client, NFS_SERVER(inode), &msg, &args.seq_args, &res.seq_res, 0);
2873}
2874
2875static int nfs4_proc_readlink(struct inode *inode, struct page *page,
2876                unsigned int pgbase, unsigned int pglen)
2877{
2878        struct nfs4_exception exception = { };
2879        int err;
2880        do {
2881                err = nfs4_handle_exception(NFS_SERVER(inode),
2882                                _nfs4_proc_readlink(inode, page, pgbase, pglen),
2883                                &exception);
2884        } while (exception.retry);
2885        return err;
2886}
2887
2888/*
2889 * This is just for mknod.  open(O_CREAT) will always do ->open_context().
2890 */
2891static int
2892nfs4_proc_create(struct inode *dir, struct dentry *dentry, struct iattr *sattr,
2893                 int flags)
2894{
2895        struct nfs_open_context *ctx;
2896        struct nfs4_state *state;
2897        int status = 0;
2898
2899        ctx = alloc_nfs_open_context(dentry, FMODE_READ);
2900        if (IS_ERR(ctx))
2901                return PTR_ERR(ctx);
2902
2903        sattr->ia_mode &= ~current_umask();
2904        state = nfs4_do_open(dir, dentry, ctx->mode,
2905                        flags, sattr, ctx->cred,
2906                        &ctx->mdsthreshold);
2907        d_drop(dentry);
2908        if (IS_ERR(state)) {
2909                status = PTR_ERR(state);
2910                goto out;
2911        }
2912        d_add(dentry, igrab(state->inode));
2913        nfs_set_verifier(dentry, nfs_save_change_attribute(dir));
2914        ctx->state = state;
2915out:
2916        put_nfs_open_context(ctx);
2917        return status;
2918}
2919
2920static int _nfs4_proc_remove(struct inode *dir, struct qstr *name)
2921{
2922        struct nfs_server *server = NFS_SERVER(dir);
2923        struct nfs_removeargs args = {
2924                .fh = NFS_FH(dir),
2925                .name = *name,
2926        };
2927        struct nfs_removeres res = {
2928                .server = server,
2929        };
2930        struct rpc_message msg = {
2931                .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_REMOVE],
2932                .rpc_argp = &args,
2933                .rpc_resp = &res,
2934        };
2935        int status;
2936
2937        status = nfs4_call_sync(server->client, server, &msg, &args.seq_args, &res.seq_res, 1);
2938        if (status == 0)
2939                update_changeattr(dir, &res.cinfo);
2940        return status;
2941}
2942
2943static int nfs4_proc_remove(struct inode *dir, struct qstr *name)
2944{
2945        struct nfs4_exception exception = { };
2946        int err;
2947        do {
2948                err = nfs4_handle_exception(NFS_SERVER(dir),
2949                                _nfs4_proc_remove(dir, name),
2950                                &exception);
2951        } while (exception.retry);
2952        return err;
2953}
2954
2955static void nfs4_proc_unlink_setup(struct rpc_message *msg, struct inode *dir)
2956{
2957        struct nfs_server *server = NFS_SERVER(dir);
2958        struct nfs_removeargs *args = msg->rpc_argp;
2959        struct nfs_removeres *res = msg->rpc_resp;
2960
2961        res->server = server;
2962        msg->rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_REMOVE];
2963        nfs41_init_sequence(&args->seq_args, &res->seq_res, 1);
2964}
2965
2966static void nfs4_proc_unlink_rpc_prepare(struct rpc_task *task, struct nfs_unlinkdata *data)
2967{
2968        nfs4_setup_sequence(NFS_SERVER(data->dir),
2969                        &data->args.seq_args,
2970                        &data->res.seq_res,
2971                        task);
2972}
2973
2974static int nfs4_proc_unlink_done(struct rpc_task *task, struct inode *dir)
2975{
2976        struct nfs_removeres *res = task->tk_msg.rpc_resp;
2977
2978        if (!nfs4_sequence_done(task, &res->seq_res))
2979                return 0;
2980        if (nfs4_async_handle_error(task, res->server, NULL) == -EAGAIN)
2981                return 0;
2982        update_changeattr(dir, &res->cinfo);
2983        return 1;
2984}
2985
2986static void nfs4_proc_rename_setup(struct rpc_message *msg, struct inode *dir)
2987{
2988        struct nfs_server *server = NFS_SERVER(dir);
2989        struct nfs_renameargs *arg = msg->rpc_argp;
2990        struct nfs_renameres *res = msg->rpc_resp;
2991
2992        msg->rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_RENAME];
2993        res->server = server;
2994        nfs41_init_sequence(&arg->seq_args, &res->seq_res, 1);
2995}
2996
2997static void nfs4_proc_rename_rpc_prepare(struct rpc_task *task, struct nfs_renamedata *data)
2998{
2999        nfs4_setup_sequence(NFS_SERVER(data->old_dir),
3000                        &data->args.seq_args,
3001                        &data->res.seq_res,
3002                        task);
3003}
3004
3005static int nfs4_proc_rename_done(struct rpc_task *task, struct inode *old_dir,
3006                                 struct inode *new_dir)
3007{
3008        struct nfs_renameres *res = task->tk_msg.rpc_resp;
3009
3010        if (!nfs4_sequence_done(task, &res->seq_res))
3011                return 0;
3012        if (nfs4_async_handle_error(task, res->server, NULL) == -EAGAIN)
3013                return 0;
3014
3015        update_changeattr(old_dir, &res->old_cinfo);
3016        update_changeattr(new_dir, &res->new_cinfo);
3017        return 1;
3018}
3019
3020static int _nfs4_proc_rename(struct inode *old_dir, struct qstr *old_name,
3021                struct inode *new_dir, struct qstr *new_name)
3022{
3023        struct nfs_server *server = NFS_SERVER(old_dir);
3024        struct nfs_renameargs arg = {
3025                .old_dir = NFS_FH(old_dir),
3026                .new_dir = NFS_FH(new_dir),
3027                .old_name = old_name,
3028                .new_name = new_name,
3029        };
3030        struct nfs_renameres res = {
3031                .server = server,
3032        };
3033        struct rpc_message msg = {
3034                .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_RENAME],
3035                .rpc_argp = &arg,
3036                .rpc_resp = &res,
3037        };
3038        int status = -ENOMEM;
3039        
3040        status = nfs4_call_sync(server->client, server, &msg, &arg.seq_args, &res.seq_res, 1);
3041        if (!status) {
3042                update_changeattr(old_dir, &res.old_cinfo);
3043                update_changeattr(new_dir, &res.new_cinfo);
3044        }
3045        return status;
3046}
3047
3048static int nfs4_proc_rename(struct inode *old_dir, struct qstr *old_name,
3049                struct inode *new_dir, struct qstr *new_name)
3050{
3051        struct nfs4_exception exception = { };
3052        int err;
3053        do {
3054                err = nfs4_handle_exception(NFS_SERVER(old_dir),
3055                                _nfs4_proc_rename(old_dir, old_name,
3056                                        new_dir, new_name),
3057                                &exception);
3058        } while (exception.retry);
3059        return err;
3060}
3061
3062static int _nfs4_proc_link(struct inode *inode, struct inode *dir, struct qstr *name)
3063{
3064        struct nfs_server *server = NFS_SERVER(inode);
3065        struct nfs4_link_arg arg = {
3066                .fh     = NFS_FH(inode),
3067                .dir_fh = NFS_FH(dir),
3068                .name   = name,
3069                .bitmask = server->attr_bitmask,
3070        };
3071        struct nfs4_link_res res = {
3072                .server = server,
3073        };
3074        struct rpc_message msg = {
3075                .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_LINK],
3076                .rpc_argp = &arg,
3077                .rpc_resp = &res,
3078        };
3079        int status = -ENOMEM;
3080
3081        res.fattr = nfs_alloc_fattr();
3082        if (res.fattr == NULL)
3083                goto out;
3084
3085        status = nfs4_call_sync(server->client, server, &msg, &arg.seq_args, &res.seq_res, 1);
3086        if (!status) {
3087                update_changeattr(dir, &res.cinfo);
3088                nfs_post_op_update_inode(inode, res.fattr);
3089        }
3090out:
3091        nfs_free_fattr(res.fattr);
3092        return status;
3093}
3094
3095static int nfs4_proc_link(struct inode *inode, struct inode *dir, struct qstr *name)
3096{
3097        struct nfs4_exception exception = { };
3098        int err;
3099        do {
3100                err = nfs4_handle_exception(NFS_SERVER(inode),
3101                                _nfs4_proc_link(inode, dir, name),
3102                                &exception);
3103        } while (exception.retry);
3104        return err;
3105}
3106
3107struct nfs4_createdata {
3108        struct rpc_message msg;
3109        struct nfs4_create_arg arg;
3110        struct nfs4_create_res res;
3111        struct nfs_fh fh;
3112        struct nfs_fattr fattr;
3113};
3114
3115static struct nfs4_createdata *nfs4_alloc_createdata(struct inode *dir,
3116                struct qstr *name, struct iattr *sattr, u32 ftype)
3117{
3118        struct nfs4_createdata *data;
3119
3120        data = kzalloc(sizeof(*data), GFP_KERNEL);
3121        if (data != NULL) {
3122                struct nfs_server *server = NFS_SERVER(dir);
3123
3124                data->msg.rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_CREATE];
3125                data->msg.rpc_argp = &data->arg;
3126                data->msg.rpc_resp = &data->res;
3127                data->arg.dir_fh = NFS_FH(dir);
3128                data->arg.server = server;
3129                data->arg.name = name;
3130                data->arg.attrs = sattr;
3131                data->arg.ftype = ftype;
3132                data->arg.bitmask = server->attr_bitmask;
3133                data->res.server = server;
3134                data->res.fh = &data->fh;
3135                data->res.fattr = &data->fattr;
3136                nfs_fattr_init(data->res.fattr);
3137        }
3138        return data;
3139}
3140
3141static int nfs4_do_create(struct inode *dir, struct dentry *dentry, struct nfs4_createdata *data)
3142{
3143        int status = nfs4_call_sync(NFS_SERVER(dir)->client, NFS_SERVER(dir), &data->msg,
3144                                    &data->arg.seq_args, &data->res.seq_res, 1);
3145        if (status == 0) {
3146                update_changeattr(dir, &data->res.dir_cinfo);
3147                status = nfs_instantiate(dentry, data->res.fh, data->res.fattr);
3148        }
3149        return status;
3150}
3151
3152static void nfs4_free_createdata(struct nfs4_createdata *data)
3153{
3154        kfree(data);
3155}
3156
3157static int _nfs4_proc_symlink(struct inode *dir, struct dentry *dentry,
3158                struct page *page, unsigned int len, struct iattr *sattr)
3159{
3160        struct nfs4_createdata *data;
3161        int status = -ENAMETOOLONG;
3162
3163        if (len > NFS4_MAXPATHLEN)
3164                goto out;
3165
3166        status = -ENOMEM;
3167        data = nfs4_alloc_createdata(dir, &dentry->d_name, sattr, NF4LNK);
3168        if (data == NULL)
3169                goto out;
3170
3171        data->msg.rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_SYMLINK];
3172        data->arg.u.symlink.pages = &page;
3173        data->arg.u.symlink.len = len;
3174        
3175        status = nfs4_do_create(dir, dentry, data);
3176
3177        nfs4_free_createdata(data);
3178out:
3179        return status;
3180}
3181
3182static int nfs4_proc_symlink(struct inode *dir, struct dentry *dentry,
3183                struct page *page, unsigned int len, struct iattr *sattr)
3184{
3185        struct nfs4_exception exception = { };
3186        int err;
3187        do {
3188                err = nfs4_handle_exception(NFS_SERVER(dir),
3189                                _nfs4_proc_symlink(dir, dentry, page,
3190                                                        len, sattr),
3191                                &exception);
3192        } while (exception.retry);
3193        return err;
3194}
3195
3196static int _nfs4_proc_mkdir(struct inode *dir, struct dentry *dentry,
3197                struct iattr *sattr)
3198{
3199        struct nfs4_createdata *data;
3200        int status = -ENOMEM;
3201
3202        data = nfs4_alloc_createdata(dir, &dentry->d_name, sattr, NF4DIR);
3203        if (data == NULL)
3204                goto out;
3205
3206        status = nfs4_do_create(dir, dentry, data);
3207
3208        nfs4_free_createdata(data);
3209out:
3210        return status;
3211}
3212
3213static int nfs4_proc_mkdir(struct inode *dir, struct dentry *dentry,
3214                struct iattr *sattr)
3215{
3216        struct nfs4_exception exception = { };
3217        int err;
3218
3219        sattr->ia_mode &= ~current_umask();
3220        do {
3221                err = nfs4_handle_exception(NFS_SERVER(dir),
3222                                _nfs4_proc_mkdir(dir, dentry, sattr),
3223                                &exception);
3224        } while (exception.retry);
3225        return err;
3226}
3227
3228static int _nfs4_proc_readdir(struct dentry *dentry, struct rpc_cred *cred,
3229                u64 cookie, struct page **pages, unsigned int count, int plus)
3230{
3231        struct inode            *dir = dentry->d_inode;
3232        struct nfs4_readdir_arg args = {
3233                .fh = NFS_FH(dir),
3234                .pages = pages,
3235                .pgbase = 0,
3236                .count = count,
3237                .bitmask = NFS_SERVER(dentry->d_inode)->attr_bitmask,
3238                .plus = plus,
3239        };
3240        struct nfs4_readdir_res res;
3241        struct rpc_message msg = {
3242                .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_READDIR],
3243                .rpc_argp = &args,
3244                .rpc_resp = &res,
3245                .rpc_cred = cred,
3246        };
3247        int                     status;
3248
3249        dprintk("%s: dentry = %s/%s, cookie = %Lu\n", __func__,
3250                        dentry->d_parent->d_name.name,
3251                        dentry->d_name.name,
3252                        (unsigned long long)cookie);
3253        nfs4_setup_readdir(cookie, NFS_I(dir)->cookieverf, dentry, &args);
3254        res.pgbase = args.pgbase;
3255        status = nfs4_call_sync(NFS_SERVER(dir)->client, NFS_SERVER(dir), &msg, &args.seq_args, &res.seq_res, 0);
3256        if (status >= 0) {
3257                memcpy(NFS_I(dir)->cookieverf, res.verifier.data, NFS4_VERIFIER_SIZE);
3258                status += args.pgbase;
3259        }
3260
3261        nfs_invalidate_atime(dir);
3262
3263        dprintk("%s: returns %d\n", __func__, status);
3264        return status;
3265}
3266
3267static int nfs4_proc_readdir(struct dentry *dentry, struct rpc_cred *cred,
3268                u64 cookie, struct page **pages, unsigned int count, int plus)
3269{
3270        struct nfs4_exception exception = { };
3271        int err;
3272        do {
3273                err = nfs4_handle_exception(NFS_SERVER(dentry->d_inode),
3274                                _nfs4_proc_readdir(dentry, cred, cookie,
3275                                        pages, count, plus),
3276                                &exception);
3277        } while (exception.retry);
3278        return err;
3279}
3280
3281static int _nfs4_proc_mknod(struct inode *dir, struct dentry *dentry,
3282                struct iattr *sattr, dev_t rdev)
3283{
3284        struct nfs4_createdata *data;
3285        int mode = sattr->ia_mode;
3286        int status = -ENOMEM;
3287
3288        data = nfs4_alloc_createdata(dir, &dentry->d_name, sattr, NF4SOCK);
3289        if (data == NULL)
3290                goto out;
3291
3292        if (S_ISFIFO(mode))
3293                data->arg.ftype = NF4FIFO;
3294        else if (S_ISBLK(mode)) {
3295                data->arg.ftype = NF4BLK;
3296                data->arg.u.device.specdata1 = MAJOR(rdev);
3297                data->arg.u.device.specdata2 = MINOR(rdev);
3298        }
3299        else if (S_ISCHR(mode)) {
3300                data->arg.ftype = NF4CHR;
3301                data->arg.u.device.specdata1 = MAJOR(rdev);
3302                data->arg.u.device.specdata2 = MINOR(rdev);
3303        } else if (!S_ISSOCK(mode)) {
3304                status = -EINVAL;
3305                goto out_free;
3306        }
3307        
3308        status = nfs4_do_create(dir, dentry, data);
3309out_free:
3310        nfs4_free_createdata(data);
3311out:
3312        return status;
3313}
3314
3315static int nfs4_proc_mknod(struct inode *dir, struct dentry *dentry,
3316                struct iattr *sattr, dev_t rdev)
3317{
3318        struct nfs4_exception exception = { };
3319        int err;
3320
3321        sattr->ia_mode &= ~current_umask();
3322        do {
3323                err = nfs4_handle_exception(NFS_SERVER(dir),
3324                                _nfs4_proc_mknod(dir, dentry, sattr, rdev),
3325                                &exception);
3326        } while (exception.retry);
3327        return err;
3328}
3329
3330static int _nfs4_proc_statfs(struct nfs_server *server, struct nfs_fh *fhandle,
3331                 struct nfs_fsstat *fsstat)
3332{
3333        struct nfs4_statfs_arg args = {
3334                .fh = fhandle,
3335                .bitmask = server->attr_bitmask,
3336        };
3337        struct nfs4_statfs_res res = {
3338                .fsstat = fsstat,
3339        };
3340        struct rpc_message msg = {
3341                .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_STATFS],
3342                .rpc_argp = &args,
3343                .rpc_resp = &res,
3344        };
3345
3346        nfs_fattr_init(fsstat->fattr);
3347        return  nfs4_call_sync(server->client, server, &msg, &args.seq_args, &res.seq_res, 0);
3348}
3349
3350static int nfs4_proc_statfs(struct nfs_server *server, struct nfs_fh *fhandle, struct nfs_fsstat *fsstat)
3351{
3352        struct nfs4_exception exception = { };
3353        int err;
3354        do {
3355                err = nfs4_handle_exception(server,
3356                                _nfs4_proc_statfs(server, fhandle, fsstat),
3357                                &exception);
3358        } while (exception.retry);
3359        return err;
3360}
3361
3362static int _nfs4_do_fsinfo(struct nfs_server *server, struct nfs_fh *fhandle,
3363                struct nfs_fsinfo *fsinfo)
3364{
3365        struct nfs4_fsinfo_arg args = {
3366                .fh = fhandle,
3367                .bitmask = server->attr_bitmask,
3368        };
3369        struct nfs4_fsinfo_res res = {
3370                .fsinfo = fsinfo,
3371        };
3372        struct rpc_message msg = {
3373                .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_FSINFO],
3374                .rpc_argp = &args,
3375                .rpc_resp = &res,
3376        };
3377
3378        return nfs4_call_sync(server->client, server, &msg, &args.seq_args, &res.seq_res, 0);
3379}
3380
3381static int nfs4_do_fsinfo(struct nfs_server *server, struct nfs_fh *fhandle, struct nfs_fsinfo *fsinfo)
3382{
3383        struct nfs4_exception exception = { };
3384        int err;
3385
3386        do {
3387                err = nfs4_handle_exception(server,
3388                                _nfs4_do_fsinfo(server, fhandle, fsinfo),
3389                                &exception);
3390        } while (exception.retry);
3391        return err;
3392}
3393
3394static int nfs4_proc_fsinfo(struct nfs_server *server, struct nfs_fh *fhandle, struct nfs_fsinfo *fsinfo)
3395{
3396        int error;
3397
3398        nfs_fattr_init(fsinfo->fattr);
3399        error = nfs4_do_fsinfo(server, fhandle, fsinfo);
3400        if (error == 0) {
3401                /* block layout checks this! */
3402                server->pnfs_blksize = fsinfo->blksize;
3403                set_pnfs_layoutdriver(server, fhandle, fsinfo->layouttype);
3404        }
3405
3406        return error;
3407}
3408
3409static int _nfs4_proc_pathconf(struct nfs_server *server, struct nfs_fh *fhandle,
3410                struct nfs_pathconf *pathconf)
3411{
3412        struct nfs4_pathconf_arg args = {
3413                .fh = fhandle,
3414                .bitmask = server->attr_bitmask,
3415        };
3416        struct nfs4_pathconf_res res = {
3417                .pathconf = pathconf,
3418        };
3419        struct rpc_message msg = {
3420                .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_PATHCONF],
3421                .rpc_argp = &args,
3422                .rpc_resp = &res,
3423        };
3424
3425        /* None of the pathconf attributes are mandatory to implement */
3426        if ((args.bitmask[0] & nfs4_pathconf_bitmap[0]) == 0) {
3427                memset(pathconf, 0, sizeof(*pathconf));
3428                return 0;
3429        }
3430
3431        nfs_fattr_init(pathconf->fattr);
3432        return nfs4_call_sync(server->client, server, &msg, &args.seq_args, &res.seq_res, 0);
3433}
3434
3435static int nfs4_proc_pathconf(struct nfs_server *server, struct nfs_fh *fhandle,
3436                struct nfs_pathconf *pathconf)
3437{
3438        struct nfs4_exception exception = { };
3439        int err;
3440
3441        do {
3442                err = nfs4_handle_exception(server,
3443                                _nfs4_proc_pathconf(server, fhandle, pathconf),
3444                                &exception);
3445        } while (exception.retry);
3446        return err;
3447}
3448
3449void __nfs4_read_done_cb(struct nfs_read_data *data)
3450{
3451        nfs_invalidate_atime(data->header->inode);
3452}
3453
3454static int nfs4_read_done_cb(struct rpc_task *task, struct nfs_read_data *data)
3455{
3456        struct nfs_server *server = NFS_SERVER(data->header->inode);
3457
3458        if (nfs4_async_handle_error(task, server, data->args.context->state) == -EAGAIN) {
3459                rpc_restart_call_prepare(task);
3460                return -EAGAIN;
3461        }
3462
3463        __nfs4_read_done_cb(data);
3464        if (task->tk_status > 0)
3465                renew_lease(server, data->timestamp);
3466        return 0;
3467}
3468
3469static int nfs4_read_done(struct rpc_task *task, struct nfs_read_data *data)
3470{
3471
3472        dprintk("--> %s\n", __func__);
3473
3474        if (!nfs4_sequence_done(task, &data->res.seq_res))
3475                return -EAGAIN;
3476
3477        return data->read_done_cb ? data->read_done_cb(task, data) :
3478                                    nfs4_read_done_cb(task, data);
3479}
3480
3481static void nfs4_proc_read_setup(struct nfs_read_data *data, struct rpc_message *msg)
3482{
3483        data->timestamp   = jiffies;
3484        data->read_done_cb = nfs4_read_done_cb;
3485        msg->rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_READ];
3486        nfs41_init_sequence(&data->args.seq_args, &data->res.seq_res, 0);
3487}
3488
3489static void nfs4_proc_read_rpc_prepare(struct rpc_task *task, struct nfs_read_data *data)
3490{
3491        nfs4_setup_sequence(NFS_SERVER(data->header->inode),
3492                        &data->args.seq_args,
3493                        &data->res.seq_res,
3494                        task);
3495}
3496
3497static int nfs4_write_done_cb(struct rpc_task *task, struct nfs_write_data *data)
3498{
3499        struct inode *inode = data->header->inode;
3500        
3501        if (nfs4_async_handle_error(task, NFS_SERVER(inode), data->args.context->state) == -EAGAIN) {
3502                rpc_restart_call_prepare(task);
3503                return -EAGAIN;
3504        }
3505        if (task->tk_status >= 0) {
3506                renew_lease(NFS_SERVER(inode), data->timestamp);
3507                nfs_post_op_update_inode_force_wcc(inode, &data->fattr);
3508        }
3509        return 0;
3510}
3511
3512static int nfs4_write_done(struct rpc_task *task, struct nfs_write_data *data)
3513{
3514        if (!nfs4_sequence_done(task, &data->res.seq_res))
3515                return -EAGAIN;
3516        return data->write_done_cb ? data->write_done_cb(task, data) :
3517                nfs4_write_done_cb(task, data);
3518}
3519
3520static
3521bool nfs4_write_need_cache_consistency_data(const struct nfs_write_data *data)
3522{
3523        const struct nfs_pgio_header *hdr = data->header;
3524
3525        /* Don't request attributes for pNFS or O_DIRECT writes */
3526        if (data->ds_clp != NULL || hdr->dreq != NULL)
3527                return false;
3528        /* Otherwise, request attributes if and only if we don't hold
3529         * a delegation
3530         */
3531        return nfs4_have_delegation(hdr->inode, FMODE_READ) == 0;
3532}
3533
3534static void nfs4_proc_write_setup(struct nfs_write_data *data, struct rpc_message *msg)
3535{
3536        struct nfs_server *server = NFS_SERVER(data->header->inode);
3537
3538        if (!nfs4_write_need_cache_consistency_data(data)) {
3539                data->args.bitmask = NULL;
3540                data->res.fattr = NULL;
3541        } else
3542                data->args.bitmask = server->cache_consistency_bitmask;
3543
3544        if (!data->write_done_cb)
3545                data->write_done_cb = nfs4_write_done_cb;
3546        data->res.server = server;
3547        data->timestamp   = jiffies;
3548
3549        msg->rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_WRITE];
3550        nfs41_init_sequence(&data->args.seq_args, &data->res.seq_res, 1);
3551}
3552
3553static void nfs4_proc_write_rpc_prepare(struct rpc_task *task, struct nfs_write_data *data)
3554{
3555        nfs4_setup_sequence(NFS_SERVER(data->header->inode),
3556                        &data->args.seq_args,
3557                        &data->res.seq_res,
3558                        task);
3559}
3560
3561static void nfs4_proc_commit_rpc_prepare(struct rpc_task *task, struct nfs_commit_data *data)
3562{
3563        nfs4_setup_sequence(NFS_SERVER(data->inode),
3564                        &data->args.seq_args,
3565                        &data->res.seq_res,
3566                        task);
3567}
3568
3569static int nfs4_commit_done_cb(struct rpc_task *task, struct nfs_commit_data *data)
3570{
3571        struct inode *inode = data->inode;
3572
3573        if (nfs4_async_handle_error(task, NFS_SERVER(inode), NULL) == -EAGAIN) {
3574                rpc_restart_call_prepare(task);
3575                return -EAGAIN;
3576        }
3577        return 0;
3578}
3579
3580static int nfs4_commit_done(struct rpc_task *task, struct nfs_commit_data *data)
3581{
3582        if (!nfs4_sequence_done(task, &data->res.seq_res))
3583                return -EAGAIN;
3584        return data->commit_done_cb(task, data);
3585}
3586
3587static void nfs4_proc_commit_setup(struct nfs_commit_data *data, struct rpc_message *msg)
3588{
3589        struct nfs_server *server = NFS_SERVER(data->inode);
3590
3591        if (data->commit_done_cb == NULL)
3592                data->commit_done_cb = nfs4_commit_done_cb;
3593        data->res.server = server;
3594        msg->rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_COMMIT];
3595        nfs41_init_sequence(&data->args.seq_args, &data->res.seq_res, 1);
3596}
3597
3598struct nfs4_renewdata {
3599        struct nfs_client       *client;
3600        unsigned long           timestamp;
3601};
3602
3603/*
3604 * nfs4_proc_async_renew(): This is not one of the nfs_rpc_ops; it is a special
3605 * standalone procedure for queueing an asynchronous RENEW.
3606 */
3607static void nfs4_renew_release(void *calldata)
3608{
3609        struct nfs4_renewdata *data = calldata;
3610        struct nfs_client *clp = data->client;
3611
3612        if (atomic_read(&clp->cl_count) > 1)
3613                nfs4_schedule_state_renewal(clp);
3614        nfs_put_client(clp);
3615        kfree(data);
3616}
3617
3618static void nfs4_renew_done(struct rpc_task *task, void *calldata)
3619{
3620        struct nfs4_renewdata *data = calldata;
3621        struct nfs_client *clp = data->client;
3622        unsigned long timestamp = data->timestamp;
3623
3624        if (task->tk_status < 0) {
3625                /* Unless we're shutting down, schedule state recovery! */
3626                if (test_bit(NFS_CS_RENEWD, &clp->cl_res_state) == 0)
3627                        return;
3628                if (task->tk_status != NFS4ERR_CB_PATH_DOWN) {
3629                        nfs4_schedule_lease_recovery(clp);
3630                        return;
3631                }
3632                nfs4_schedule_path_down_recovery(clp);
3633        }
3634        do_renew_lease(clp, timestamp);
3635}
3636
3637static const struct rpc_call_ops nfs4_renew_ops = {
3638        .rpc_call_done = nfs4_renew_done,
3639        .rpc_release = nfs4_renew_release,
3640};
3641
3642static int nfs4_proc_async_renew(struct nfs_client *clp, struct rpc_cred *cred, unsigned renew_flags)
3643{
3644        struct rpc_message msg = {
3645                .rpc_proc       = &nfs4_procedures[NFSPROC4_CLNT_RENEW],
3646                .rpc_argp       = clp,
3647                .rpc_cred       = cred,
3648        };
3649        struct nfs4_renewdata *data;
3650
3651        if (renew_flags == 0)
3652                return 0;
3653        if (!atomic_inc_not_zero(&clp->cl_count))
3654                return -EIO;
3655        data = kmalloc(sizeof(*data), GFP_NOFS);
3656        if (data == NULL)
3657                return -ENOMEM;
3658        data->client = clp;
3659        data->timestamp = jiffies;
3660        return rpc_call_async(clp->cl_rpcclient, &msg, RPC_TASK_SOFT,
3661                        &nfs4_renew_ops, data);
3662}
3663
3664static int nfs4_proc_renew(struct nfs_client *clp, struct rpc_cred *cred)
3665{
3666        struct rpc_message msg = {
3667                .rpc_proc       = &nfs4_procedures[NFSPROC4_CLNT_RENEW],
3668                .rpc_argp       = clp,
3669                .rpc_cred       = cred,
3670        };
3671        unsigned long now = jiffies;
3672        int status;
3673
3674        status = rpc_call_sync(clp->cl_rpcclient, &msg, 0);
3675        if (status < 0)
3676                return status;
3677        do_renew_lease(clp, now);
3678        return 0;
3679}
3680
3681static inline int nfs4_server_supports_acls(struct nfs_server *server)
3682{
3683        return (server->caps & NFS_CAP_ACLS)
3684                && (server->acl_bitmask & ACL4_SUPPORT_ALLOW_ACL)
3685                && (server->acl_bitmask & ACL4_SUPPORT_DENY_ACL);
3686}
3687
3688/* Assuming that XATTR_SIZE_MAX is a multiple of PAGE_SIZE, and that
3689 * it's OK to put sizeof(void) * (XATTR_SIZE_MAX/PAGE_SIZE) bytes on
3690 * the stack.
3691 */
3692#define NFS4ACL_MAXPAGES DIV_ROUND_UP(XATTR_SIZE_MAX, PAGE_SIZE)
3693
3694static int buf_to_pages_noslab(const void *buf, size_t buflen,
3695                struct page **pages, unsigned int *pgbase)
3696{
3697        struct page *newpage, **spages;
3698        int rc = 0;
3699        size_t len;
3700        spages = pages;
3701
3702        do {
3703                len = min_t(size_t, PAGE_SIZE, buflen);
3704                newpage = alloc_page(GFP_KERNEL);
3705
3706                if (newpage == NULL)
3707                        goto unwind;
3708                memcpy(page_address(newpage), buf, len);
3709                buf += len;
3710                buflen -= len;
3711                *pages++ = newpage;
3712                rc++;
3713        } while (buflen != 0);
3714
3715        return rc;
3716
3717unwind:
3718        for(; rc > 0; rc--)
3719                __free_page(spages[rc-1]);
3720        return -ENOMEM;
3721}
3722
3723struct nfs4_cached_acl {
3724        int cached;
3725        size_t len;
3726        char data[0];
3727};
3728
3729static void nfs4_set_cached_acl(struct inode *inode, struct nfs4_cached_acl *acl)
3730{
3731        struct nfs_inode *nfsi = NFS_I(inode);
3732
3733        spin_lock(&inode->i_lock);
3734        kfree(nfsi->nfs4_acl);
3735        nfsi->nfs4_acl = acl;
3736        spin_unlock(&inode->i_lock);
3737}
3738
3739static void nfs4_zap_acl_attr(struct inode *inode)
3740{
3741        nfs4_set_cached_acl(inode, NULL);
3742}
3743
3744static inline ssize_t nfs4_read_cached_acl(struct inode *inode, char *buf, size_t buflen)
3745{
3746        struct nfs_inode *nfsi = NFS_I(inode);
3747        struct nfs4_cached_acl *acl;
3748        int ret = -ENOENT;
3749
3750        spin_lock(&inode->i_lock);
3751        acl = nfsi->nfs4_acl;
3752        if (acl == NULL)
3753                goto out;
3754        if (buf == NULL) /* user is just asking for length */
3755                goto out_len;
3756        if (acl->cached == 0)
3757                goto out;
3758        ret = -ERANGE; /* see getxattr(2) man page */
3759        if (acl->len > buflen)
3760                goto out;
3761        memcpy(buf, acl->data, acl->len);
3762out_len:
3763        ret = acl->len;
3764out:
3765        spin_unlock(&inode->i_lock);
3766        return ret;
3767}
3768
3769static void nfs4_write_cached_acl(struct inode *inode, struct page **pages, size_t pgbase, size_t acl_len)
3770{
3771        struct nfs4_cached_acl *acl;
3772        size_t buflen = sizeof(*acl) + acl_len;
3773
3774        if (buflen <= PAGE_SIZE) {
3775                acl = kmalloc(buflen, GFP_KERNEL);
3776                if (acl == NULL)
3777                        goto out;
3778                acl->cached = 1;
3779                _copy_from_pages(acl->data, pages, pgbase, acl_len);
3780        } else {
3781                acl = kmalloc(sizeof(*acl), GFP_KERNEL);
3782                if (acl == NULL)
3783                        goto out;
3784                acl->cached = 0;
3785        }
3786        acl->len = acl_len;
3787out:
3788        nfs4_set_cached_acl(inode, acl);
3789}
3790
3791/*
3792 * The getxattr API returns the required buffer length when called with a
3793 * NULL buf. The NFSv4 acl tool then calls getxattr again after allocating
3794 * the required buf.  On a NULL buf, we send a page of data to the server
3795 * guessing that the ACL request can be serviced by a page. If so, we cache
3796 * up to the page of ACL data, and the 2nd call to getxattr is serviced by
3797 * the cache. If not so, we throw away the page, and cache the required
3798 * length. The next getxattr call will then produce another round trip to
3799 * the server, this time with the input buf of the required size.
3800 */
3801static ssize_t __nfs4_get_acl_uncached(struct inode *inode, void *buf, size_t buflen)
3802{
3803        struct page *pages[NFS4ACL_MAXPAGES] = {NULL, };
3804        struct nfs_getaclargs args = {
3805                .fh = NFS_FH(inode),
3806                .acl_pages = pages,
3807                .acl_len = buflen,
3808        };
3809        struct nfs_getaclres res = {
3810                .acl_len = buflen,
3811        };
3812        struct rpc_message msg = {
3813                .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_GETACL],
3814                .rpc_argp = &args,
3815                .rpc_resp = &res,
3816        };
3817        unsigned int npages = DIV_ROUND_UP(buflen, PAGE_SIZE);
3818        int ret = -ENOMEM, i;
3819
3820        /* As long as we're doing a round trip to the server anyway,
3821         * let's be prepared for a page of acl data. */
3822        if (npages == 0)
3823                npages = 1;
3824        if (npages > ARRAY_SIZE(pages))
3825                return -ERANGE;
3826
3827        for (i = 0; i < npages; i++) {
3828                pages[i] = alloc_page(GFP_KERNEL);
3829                if (!pages[i])
3830                        goto out_free;
3831        }
3832
3833        /* for decoding across pages */
3834        res.acl_scratch = alloc_page(GFP_KERNEL);
3835        if (!res.acl_scratch)
3836                goto out_free;
3837
3838        args.acl_len = npages * PAGE_SIZE;
3839        args.acl_pgbase = 0;
3840
3841        dprintk("%s  buf %p buflen %zu npages %d args.acl_len %zu\n",
3842                __func__, buf, buflen, npages, args.acl_len);
3843        ret = nfs4_call_sync(NFS_SERVER(inode)->client, NFS_SERVER(inode),
3844                             &msg, &args.seq_args, &res.seq_res, 0);
3845        if (ret)
3846                goto out_free;
3847
3848        /* Handle the case where the passed-in buffer is too short */
3849        if (res.acl_flags & NFS4_ACL_TRUNC) {
3850                /* Did the user only issue a request for the acl length? */
3851                if (buf == NULL)
3852                        goto out_ok;
3853                ret = -ERANGE;
3854                goto out_free;
3855        }
3856        nfs4_write_cached_acl(inode, pages, res.acl_data_offset, res.acl_len);
3857        if (buf) {
3858                if (res.acl_len > buflen) {
3859                        ret = -ERANGE;
3860                        goto out_free;
3861                }
3862                _copy_from_pages(buf, pages, res.acl_data_offset, res.acl_len);
3863        }
3864out_ok:
3865        ret = res.acl_len;
3866out_free:
3867        for (i = 0; i < npages; i++)
3868                if (pages[i])
3869                        __free_page(pages[i]);
3870        if (res.acl_scratch)
3871                __free_page(res.acl_scratch);
3872        return ret;
3873}
3874
3875static ssize_t nfs4_get_acl_uncached(struct inode *inode, void *buf, size_t buflen)
3876{
3877        struct nfs4_exception exception = { };
3878        ssize_t ret;
3879        do {
3880                ret = __nfs4_get_acl_uncached(inode, buf, buflen);
3881                if (ret >= 0)
3882                        break;
3883                ret = nfs4_handle_exception(NFS_SERVER(inode), ret, &exception);
3884        } while (exception.retry);
3885        return ret;
3886}
3887
3888static ssize_t nfs4_proc_get_acl(struct inode *inode, void *buf, size_t buflen)
3889{
3890        struct nfs_server *server = NFS_SERVER(inode);
3891        int ret;
3892
3893        if (!nfs4_server_supports_acls(server))
3894                return -EOPNOTSUPP;
3895        ret = nfs_revalidate_inode(server, inode);
3896        if (ret < 0)
3897                return ret;
3898        if (NFS_I(inode)->cache_validity & NFS_INO_INVALID_ACL)
3899                nfs_zap_acl_cache(inode);
3900        ret = nfs4_read_cached_acl(inode, buf, buflen);
3901        if (ret != -ENOENT)
3902                /* -ENOENT is returned if there is no ACL or if there is an ACL
3903                 * but no cached acl data, just the acl length */
3904                return ret;
3905        return nfs4_get_acl_uncached(inode, buf, buflen);
3906}
3907
3908static int __nfs4_proc_set_acl(struct inode *inode, const void *buf, size_t buflen)
3909{
3910        struct nfs_server *server = NFS_SERVER(inode);
3911        struct page *pages[NFS4ACL_MAXPAGES];
3912        struct nfs_setaclargs arg = {
3913                .fh             = NFS_FH(inode),
3914                .acl_pages      = pages,
3915                .acl_len        = buflen,
3916        };
3917        struct nfs_setaclres res;
3918        struct rpc_message msg = {
3919                .rpc_proc       = &nfs4_procedures[NFSPROC4_CLNT_SETACL],
3920                .rpc_argp       = &arg,
3921                .rpc_resp       = &res,
3922        };
3923        unsigned int npages = DIV_ROUND_UP(buflen, PAGE_SIZE);
3924        int ret, i;
3925
3926        if (!nfs4_server_supports_acls(server))
3927                return -EOPNOTSUPP;
3928        if (npages > ARRAY_SIZE(pages))
3929                return -ERANGE;
3930        i = buf_to_pages_noslab(buf, buflen, arg.acl_pages, &arg.acl_pgbase);
3931        if (i < 0)
3932                return i;
3933        nfs4_inode_return_delegation(inode);
3934        ret = nfs4_call_sync(server->client, server, &msg, &arg.seq_args, &res.seq_res, 1);
3935
3936        /*
3937         * Free each page after tx, so the only ref left is
3938         * held by the network stack
3939         */
3940        for (; i > 0; i--)
3941                put_page(pages[i-1]);
3942
3943        /*
3944         * Acl update can result in inode attribute update.
3945         * so mark the attribute cache invalid.
3946         */
3947        spin_lock(&inode->i_lock);
3948        NFS_I(inode)->cache_validity |= NFS_INO_INVALID_ATTR;
3949        spin_unlock(&inode->i_lock);
3950        nfs_access_zap_cache(inode);
3951        nfs_zap_acl_cache(inode);
3952        return ret;
3953}
3954
3955static int nfs4_proc_set_acl(struct inode *inode, const void *buf, size_t buflen)
3956{
3957        struct nfs4_exception exception = { };
3958        int err;
3959        do {
3960                err = nfs4_handle_exception(NFS_SERVER(inode),
3961                                __nfs4_proc_set_acl(inode, buf, buflen),
3962                                &exception);
3963        } while (exception.retry);
3964        return err;
3965}
3966
3967static int
3968nfs4_async_handle_error(struct rpc_task *task, const struct nfs_server *server, struct nfs4_state *state)
3969{
3970        struct nfs_client *clp = server->nfs_client;
3971
3972        if (task->tk_status >= 0)
3973                return 0;
3974        switch(task->tk_status) {
3975                case -NFS4ERR_DELEG_REVOKED:
3976                case -NFS4ERR_ADMIN_REVOKED:
3977                case -NFS4ERR_BAD_STATEID:
3978                        if (state == NULL)
3979                                break;
3980                        nfs_remove_bad_delegation(state->inode);
3981                case -NFS4ERR_OPENMODE:
3982                        if (state == NULL)
3983                                break;
3984                        nfs4_schedule_stateid_recovery(server, state);
3985                        goto wait_on_recovery;
3986                case -NFS4ERR_EXPIRED:
3987                        if (state != NULL)
3988                                nfs4_schedule_stateid_recovery(server, state);
3989                case -NFS4ERR_STALE_STATEID:
3990                case -NFS4ERR_STALE_CLIENTID:
3991                        nfs4_schedule_lease_recovery(clp);
3992                        goto wait_on_recovery;
3993#if defined(CONFIG_NFS_V4_1)
3994                case -NFS4ERR_BADSESSION:
3995                case -NFS4ERR_BADSLOT:
3996                case -NFS4ERR_BAD_HIGH_SLOT:
3997                case -NFS4ERR_DEADSESSION:
3998                case -NFS4ERR_CONN_NOT_BOUND_TO_SESSION:
3999                case -NFS4ERR_SEQ_FALSE_RETRY:
4000                case -NFS4ERR_SEQ_MISORDERED:
4001                        dprintk("%s ERROR %d, Reset session\n", __func__,
4002                                task->tk_status);
4003                        nfs4_schedule_session_recovery(clp->cl_session, task->tk_status);
4004                        task->tk_status = 0;
4005                        return -EAGAIN;
4006#endif /* CONFIG_NFS_V4_1 */
4007                case -NFS4ERR_DELAY:
4008                        nfs_inc_server_stats(server, NFSIOS_DELAY);
4009                case -NFS4ERR_GRACE:
4010                        rpc_delay(task, NFS4_POLL_RETRY_MAX);
4011                        task->tk_status = 0;
4012                        return -EAGAIN;
4013                case -NFS4ERR_RETRY_UNCACHED_REP:
4014                case -NFS4ERR_OLD_STATEID:
4015                        task->tk_status = 0;
4016                        return -EAGAIN;
4017        }
4018        task->tk_status = nfs4_map_errors(task->tk_status);
4019        return 0;
4020wait_on_recovery:
4021        rpc_sleep_on(&clp->cl_rpcwaitq, task, NULL);
4022        if (test_bit(NFS4CLNT_MANAGER_RUNNING, &clp->cl_state) == 0)
4023                rpc_wake_up_queued_task(&clp->cl_rpcwaitq, task);
4024        task->tk_status = 0;
4025        return -EAGAIN;
4026}
4027
4028static void nfs4_init_boot_verifier(const struct nfs_client *clp,
4029                                    nfs4_verifier *bootverf)
4030{
4031        __be32 verf[2];
4032
4033        if (test_bit(NFS4CLNT_PURGE_STATE, &clp->cl_state)) {
4034                /* An impossible timestamp guarantees this value
4035                 * will never match a generated boot time. */
4036                verf[0] = 0;
4037                verf[1] = (__be32)(NSEC_PER_SEC + 1);
4038        } else {
4039                struct nfs_net *nn = net_generic(clp->cl_net, nfs_net_id);
4040                verf[0] = (__be32)nn->boot_time.tv_sec;
4041                verf[1] = (__be32)nn->boot_time.tv_nsec;
4042        }
4043        memcpy(bootverf->data, verf, sizeof(bootverf->data));
4044}
4045
4046static unsigned int
4047nfs4_init_nonuniform_client_string(const struct nfs_client *clp,
4048                                   char *buf, size_t len)
4049{
4050        unsigned int result;
4051
4052        rcu_read_lock();
4053        result = scnprintf(buf, len, "Linux NFSv4.0 %s/%s %s",
4054                                clp->cl_ipaddr,
4055                                rpc_peeraddr2str(clp->cl_rpcclient,
4056                                                        RPC_DISPLAY_ADDR),
4057                                rpc_peeraddr2str(clp->cl_rpcclient,
4058                                                        RPC_DISPLAY_PROTO));
4059        rcu_read_unlock();
4060        return result;
4061}
4062
4063static unsigned int
4064nfs4_init_uniform_client_string(const struct nfs_client *clp,
4065                                char *buf, size_t len)
4066{
4067        char *nodename = clp->cl_rpcclient->cl_nodename;
4068
4069        if (nfs4_client_id_uniquifier[0] != '\0')
4070                nodename = nfs4_client_id_uniquifier;
4071        return scnprintf(buf, len, "Linux NFSv%u.%u %s",
4072                                clp->rpc_ops->version, clp->cl_minorversion,
4073                                nodename);
4074}
4075
4076/**
4077 * nfs4_proc_setclientid - Negotiate client ID
4078 * @clp: state data structure
4079 * @program: RPC program for NFSv4 callback service
4080 * @port: IP port number for NFS4 callback service
4081 * @cred: RPC credential to use for this call
4082 * @res: where to place the result
4083 *
4084 * Returns zero, a negative errno, or a negative NFS4ERR status code.
4085 */
4086int nfs4_proc_setclientid(struct nfs_client *clp, u32 program,
4087                unsigned short port, struct rpc_cred *cred,
4088                struct nfs4_setclientid_res *res)
4089{
4090        nfs4_verifier sc_verifier;
4091        struct nfs4_setclientid setclientid = {
4092                .sc_verifier = &sc_verifier,
4093                .sc_prog = program,
4094                .sc_cb_ident = clp->cl_cb_ident,
4095        };
4096        struct rpc_message msg = {
4097                .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_SETCLIENTID],
4098                .rpc_argp = &setclientid,
4099                .rpc_resp = res,
4100                .rpc_cred = cred,
4101        };
4102        int status;
4103
4104        /* nfs_client_id4 */
4105        nfs4_init_boot_verifier(clp, &sc_verifier);
4106        if (test_bit(NFS_CS_MIGRATION, &clp->cl_flags))
4107                setclientid.sc_name_len =
4108                                nfs4_init_uniform_client_string(clp,
4109                                                setclientid.sc_name,
4110                                                sizeof(setclientid.sc_name));
4111        else
4112                setclientid.sc_name_len =
4113                                nfs4_init_nonuniform_client_string(clp,
4114                                                setclientid.sc_name,
4115                                                sizeof(setclientid.sc_name));
4116        /* cb_client4 */
4117        rcu_read_lock();
4118        setclientid.sc_netid_len = scnprintf(setclientid.sc_netid,
4119                                sizeof(setclientid.sc_netid),
4120                                rpc_peeraddr2str(clp->cl_rpcclient,
4121                                                        RPC_DISPLAY_NETID));
4122        rcu_read_unlock();
4123        setclientid.sc_uaddr_len = scnprintf(setclientid.sc_uaddr,
4124                                sizeof(setclientid.sc_uaddr), "%s.%u.%u",
4125                                clp->cl_ipaddr, port >> 8, port & 255);
4126
4127        dprintk("NFS call  setclientid auth=%s, '%.*s'\n",
4128                clp->cl_rpcclient->cl_auth->au_ops->au_name,
4129                setclientid.sc_name_len, setclientid.sc_name);
4130        status = rpc_call_sync(clp->cl_rpcclient, &msg, RPC_TASK_TIMEOUT);
4131        dprintk("NFS reply setclientid: %d\n", status);
4132        return status;
4133}
4134
4135/**
4136 * nfs4_proc_setclientid_confirm - Confirm client ID
4137 * @clp: state data structure
4138 * @res: result of a previous SETCLIENTID
4139 * @cred: RPC credential to use for this call
4140 *
4141 * Returns zero, a negative errno, or a negative NFS4ERR status code.
4142 */
4143int nfs4_proc_setclientid_confirm(struct nfs_client *clp,
4144                struct nfs4_setclientid_res *arg,
4145                struct rpc_cred *cred)
4146{
4147        struct nfs_fsinfo fsinfo;
4148        struct rpc_message msg = {
4149                .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_SETCLIENTID_CONFIRM],
4150                .rpc_argp = arg,
4151                .rpc_resp = &fsinfo,
4152                .rpc_cred = cred,
4153        };
4154        unsigned long now;
4155        int status;
4156
4157        dprintk("NFS call  setclientid_confirm auth=%s, (client ID %llx)\n",
4158                clp->cl_rpcclient->cl_auth->au_ops->au_name,
4159                clp->cl_clientid);
4160        now = jiffies;
4161        status = rpc_call_sync(clp->cl_rpcclient, &msg, RPC_TASK_TIMEOUT);
4162        if (status == 0) {
4163                spin_lock(&clp->cl_lock);
4164                clp->cl_lease_time = fsinfo.lease_time * HZ;
4165                clp->cl_last_renewal = now;
4166                spin_unlock(&clp->cl_lock);
4167        }
4168        dprintk("NFS reply setclientid_confirm: %d\n", status);
4169        return status;
4170}
4171
4172struct nfs4_delegreturndata {
4173        struct nfs4_delegreturnargs args;
4174        struct nfs4_delegreturnres res;
4175        struct nfs_fh fh;
4176        nfs4_stateid stateid;
4177        unsigned long timestamp;
4178        struct nfs_fattr fattr;
4179        int rpc_status;
4180};
4181
4182static void nfs4_delegreturn_done(struct rpc_task *task, void *calldata)
4183{
4184        struct nfs4_delegreturndata *data = calldata;
4185
4186        if (!nfs4_sequence_done(task, &data->res.seq_res))
4187                return;
4188
4189        switch (task->tk_status) {
4190        case -NFS4ERR_STALE_STATEID:
4191        case -NFS4ERR_EXPIRED:
4192        case 0:
4193                renew_lease(data->res.server, data->timestamp);
4194                break;
4195        default:
4196                if (nfs4_async_handle_error(task, data->res.server, NULL) ==
4197                                -EAGAIN) {
4198                        rpc_restart_call_prepare(task);
4199                        return;
4200                }
4201        }
4202        data->rpc_status = task->tk_status;
4203}
4204
4205static void nfs4_delegreturn_release(void *calldata)
4206{
4207        kfree(calldata);
4208}
4209
4210#if defined(CONFIG_NFS_V4_1)
4211static void nfs4_delegreturn_prepare(struct rpc_task *task, void *data)
4212{
4213        struct nfs4_delegreturndata *d_data;
4214
4215        d_data = (struct nfs4_delegreturndata *)data;
4216
4217        nfs4_setup_sequence(d_data->res.server,
4218                        &d_data->args.seq_args,
4219                        &d_data->res.seq_res,
4220                        task);
4221}
4222#endif /* CONFIG_NFS_V4_1 */
4223
4224static const struct rpc_call_ops nfs4_delegreturn_ops = {
4225#if defined(CONFIG_NFS_V4_1)
4226        .rpc_call_prepare = nfs4_delegreturn_prepare,
4227#endif /* CONFIG_NFS_V4_1 */
4228        .rpc_call_done = nfs4_delegreturn_done,
4229        .rpc_release = nfs4_delegreturn_release,
4230};
4231
4232static int _nfs4_proc_delegreturn(struct inode *inode, struct rpc_cred *cred, const nfs4_stateid *stateid, int issync)
4233{
4234        struct nfs4_delegreturndata *data;
4235        struct nfs_server *server = NFS_SERVER(inode);
4236        struct rpc_task *task;
4237        struct rpc_message msg = {
4238                .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_DELEGRETURN],
4239                .rpc_cred = cred,
4240        };
4241        struct rpc_task_setup task_setup_data = {
4242                .rpc_client = server->client,
4243                .rpc_message = &msg,
4244                .callback_ops = &nfs4_delegreturn_ops,
4245                .flags = RPC_TASK_ASYNC,
4246        };
4247        int status = 0;
4248
4249        data = kzalloc(sizeof(*data), GFP_NOFS);
4250        if (data == NULL)
4251                return -ENOMEM;
4252        nfs41_init_sequence(&data->args.seq_args, &data->res.seq_res, 1);
4253        data->args.fhandle = &data->fh;
4254        data->args.stateid = &data->stateid;
4255        data->args.bitmask = server->cache_consistency_bitmask;
4256        nfs_copy_fh(&data->fh, NFS_FH(inode));
4257        nfs4_stateid_copy(&data->stateid, stateid);
4258        data->res.fattr = &data->fattr;
4259        data->res.server = server;
4260        nfs_fattr_init(data->res.fattr);
4261        data->timestamp = jiffies;
4262        data->rpc_status = 0;
4263
4264        task_setup_data.callback_data = data;
4265        msg.rpc_argp = &data->args;
4266        msg.rpc_resp = &data->res;
4267        task = rpc_run_task(&task_setup_data);
4268        if (IS_ERR(task))
4269                return PTR_ERR(task);
4270        if (!issync)
4271                goto out;
4272        status = nfs4_wait_for_completion_rpc_task(task);
4273        if (status != 0)
4274                goto out;
4275        status = data->rpc_status;
4276        if (status == 0)
4277                nfs_post_op_update_inode_force_wcc(inode, &data->fattr);
4278        else
4279                nfs_refresh_inode(inode, &data->fattr);
4280out:
4281        rpc_put_task(task);
4282        return status;
4283}
4284
4285int nfs4_proc_delegreturn(struct inode *inode, struct rpc_cred *cred, const nfs4_stateid *stateid, int issync)
4286{
4287        struct nfs_server *server = NFS_SERVER(inode);
4288        struct nfs4_exception exception = { };
4289        int err;
4290        do {
4291                err = _nfs4_proc_delegreturn(inode, cred, stateid, issync);
4292                switch (err) {
4293                        case -NFS4ERR_STALE_STATEID:
4294                        case -NFS4ERR_EXPIRED:
4295                        case 0:
4296                                return 0;
4297                }
4298                err = nfs4_handle_exception(server, err, &exception);
4299        } while (exception.retry);
4300        return err;
4301}
4302
4303#define NFS4_LOCK_MINTIMEOUT (1 * HZ)
4304#define NFS4_LOCK_MAXTIMEOUT (30 * HZ)
4305
4306/* 
4307 * sleep, with exponential backoff, and retry the LOCK operation. 
4308 */
4309static unsigned long
4310nfs4_set_lock_task_retry(unsigned long timeout)
4311{
4312        freezable_schedule_timeout_killable(timeout);
4313        timeout <<= 1;
4314        if (timeout > NFS4_LOCK_MAXTIMEOUT)
4315                return NFS4_LOCK_MAXTIMEOUT;
4316        return timeout;
4317}
4318
4319static int _nfs4_proc_getlk(struct nfs4_state *state, int cmd, struct file_lock *request)
4320{
4321        struct inode *inode = state->inode;
4322        struct nfs_server *server = NFS_SERVER(inode);
4323        struct nfs_client *clp = server->nfs_client;
4324        struct nfs_lockt_args arg = {
4325                .fh = NFS_FH(inode),
4326                .fl = request,
4327        };
4328        struct nfs_lockt_res res = {
4329                .denied = request,
4330        };
4331        struct rpc_message msg = {
4332                .rpc_proc       = &nfs4_procedures[NFSPROC4_CLNT_LOCKT],
4333                .rpc_argp       = &arg,
4334                .rpc_resp       = &res,
4335                .rpc_cred       = state->owner->so_cred,
4336        };
4337        struct nfs4_lock_state *lsp;
4338        int status;
4339
4340        arg.lock_owner.clientid = clp->cl_clientid;
4341        status = nfs4_set_lock_state(state, request);
4342        if (status != 0)
4343                goto out;
4344        lsp = request->fl_u.nfs4_fl.owner;
4345        arg.lock_owner.id = lsp->ls_seqid.owner_id;
4346        arg.lock_owner.s_dev = server->s_dev;
4347        status = nfs4_call_sync(server->client, server, &msg, &arg.seq_args, &res.seq_res, 1);
4348        switch (status) {
4349                case 0:
4350                        request->fl_type = F_UNLCK;
4351                        break;
4352                case -NFS4ERR_DENIED:
4353                        status = 0;
4354        }
4355        request->fl_ops->fl_release_private(request);
4356out:
4357        return status;
4358}
4359
4360static int nfs4_proc_getlk(struct nfs4_state *state, int cmd, struct file_lock *request)
4361{
4362        struct nfs4_exception exception = { };
4363        int err;
4364
4365        do {
4366                err = nfs4_handle_exception(NFS_SERVER(state->inode),
4367                                _nfs4_proc_getlk(state, cmd, request),
4368                                &exception);
4369        } while (exception.retry);
4370        return err;
4371}
4372
4373static int do_vfs_lock(struct file *file, struct file_lock *fl)
4374{
4375        int res = 0;
4376        switch (fl->fl_flags & (FL_POSIX|FL_FLOCK)) {
4377                case FL_POSIX:
4378                        res = posix_lock_file_wait(file, fl);
4379                        break;
4380                case FL_FLOCK:
4381                        res = flock_lock_file_wait(file, fl);
4382                        break;
4383                default:
4384                        BUG();
4385        }
4386        return res;
4387}
4388
4389struct nfs4_unlockdata {
4390        struct nfs_locku_args arg;
4391        struct nfs_locku_res res;
4392        struct nfs4_lock_state *lsp;
4393        struct nfs_open_context *ctx;
4394        struct file_lock fl;
4395        const struct nfs_server *server;
4396        unsigned long timestamp;
4397};
4398
4399static struct nfs4_unlockdata *nfs4_alloc_unlockdata(struct file_lock *fl,
4400                struct nfs_open_context *ctx,
4401                struct nfs4_lock_state *lsp,
4402                struct nfs_seqid *seqid)
4403{
4404        struct nfs4_unlockdata *p;
4405        struct inode *inode = lsp->ls_state->inode;
4406
4407        p = kzalloc(sizeof(*p), GFP_NOFS);
4408        if (p == NULL)
4409                return NULL;
4410        p->arg.fh = NFS_FH(inode);
4411        p->arg.fl = &p->fl;
4412        p->arg.seqid = seqid;
4413        p->res.seqid = seqid;
4414        p->arg.stateid = &lsp->ls_stateid;
4415        p->lsp = lsp;
4416        atomic_inc(&lsp->ls_count);
4417        /* Ensure we don't close file until we're done freeing locks! */
4418        p->ctx = get_nfs_open_context(ctx);
4419        memcpy(&p->fl, fl, sizeof(p->fl));
4420        p->server = NFS_SERVER(inode);
4421        return p;
4422}
4423
4424static void nfs4_locku_release_calldata(void *data)
4425{
4426        struct nfs4_unlockdata *calldata = data;
4427        nfs_free_seqid(calldata->arg.seqid);
4428        nfs4_put_lock_state(calldata->lsp);
4429        put_nfs_open_context(calldata->ctx);
4430        kfree(calldata);
4431}
4432
4433static void nfs4_locku_done(struct rpc_task *task, void *data)
4434{
4435        struct nfs4_unlockdata *calldata = data;
4436
4437        if (!nfs4_sequence_done(task, &calldata->res.seq_res))
4438                return;
4439        switch (task->tk_status) {
4440                case 0:
4441                        nfs4_stateid_copy(&calldata->lsp->ls_stateid,
4442                                        &calldata->res.stateid);
4443                        renew_lease(calldata->server, calldata->timestamp);
4444                        break;
4445                case -NFS4ERR_BAD_STATEID:
4446                case -NFS4ERR_OLD_STATEID:
4447                case -NFS4ERR_STALE_STATEID:
4448                case -NFS4ERR_EXPIRED:
4449                        break;
4450                default:
4451                        if (nfs4_async_handle_error(task, calldata->server, NULL) == -EAGAIN)
4452                                rpc_restart_call_prepare(task);
4453        }
4454        nfs_release_seqid(calldata->arg.seqid);
4455}
4456
4457static void nfs4_locku_prepare(struct rpc_task *task, void *data)
4458{
4459        struct nfs4_unlockdata *calldata = data;
4460
4461        if (nfs_wait_on_sequence(calldata->arg.seqid, task) != 0)
4462                goto out_wait;
4463        if (test_bit(NFS_LOCK_INITIALIZED, &calldata->lsp->ls_flags) == 0) {
4464                /* Note: exit _without_ running nfs4_locku_done */
4465                goto out_no_action;
4466        }
4467        calldata->timestamp = jiffies;
4468        if (nfs4_setup_sequence(calldata->server,
4469                                &calldata->arg.seq_args,
4470                                &calldata->res.seq_res,
4471                                task) != 0)
4472                nfs_release_seqid(calldata->arg.seqid);
4473        return;
4474out_no_action:
4475        task->tk_action = NULL;
4476out_wait:
4477        nfs4_sequence_done(task, &calldata->res.seq_res);
4478}
4479
4480static const struct rpc_call_ops nfs4_locku_ops = {
4481        .rpc_call_prepare = nfs4_locku_prepare,
4482        .rpc_call_done = nfs4_locku_done,
4483        .rpc_release = nfs4_locku_release_calldata,
4484};
4485
4486static struct rpc_task *nfs4_do_unlck(struct file_lock *fl,
4487                struct nfs_open_context *ctx,
4488                struct nfs4_lock_state *lsp,
4489                struct nfs_seqid *seqid)
4490{
4491        struct nfs4_unlockdata *data;
4492        struct rpc_message msg = {
4493                .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_LOCKU],
4494                .rpc_cred = ctx->cred,
4495        };
4496        struct rpc_task_setup task_setup_data = {
4497                .rpc_client = NFS_CLIENT(lsp->ls_state->inode),
4498                .rpc_message = &msg,
4499                .callback_ops = &nfs4_locku_ops,
4500                .workqueue = nfsiod_workqueue,
4501                .flags = RPC_TASK_ASYNC,
4502        };
4503
4504        /* Ensure this is an unlock - when canceling a lock, the
4505         * canceled lock is passed in, and it won't be an unlock.
4506         */
4507        fl->fl_type = F_UNLCK;
4508
4509        data = nfs4_alloc_unlockdata(fl, ctx, lsp, seqid);
4510        if (data == NULL) {
4511                nfs_free_seqid(seqid);
4512                return ERR_PTR(-ENOMEM);
4513        }
4514
4515        nfs41_init_sequence(&data->arg.seq_args, &data->res.seq_res, 1);
4516        msg.rpc_argp = &data->arg;
4517        msg.rpc_resp = &data->res;
4518        task_setup_data.callback_data = data;
4519        return rpc_run_task(&task_setup_data);
4520}
4521
4522static int nfs4_proc_unlck(struct nfs4_state *state, int cmd, struct file_lock *request)
4523{
4524        struct inode *inode = state->inode;
4525        struct nfs4_state_owner *sp = state->owner;
4526        struct nfs_inode *nfsi = NFS_I(inode);
4527        struct nfs_seqid *seqid;
4528        struct nfs4_lock_state *lsp;
4529        struct rpc_task *task;
4530        int status = 0;
4531        unsigned char fl_flags = request->fl_flags;
4532
4533        status = nfs4_set_lock_state(state, request);
4534        /* Unlock _before_ we do the RPC call */
4535        request->fl_flags |= FL_EXISTS;
4536        /* Exclude nfs_delegation_claim_locks() */
4537        mutex_lock(&sp->so_delegreturn_mutex);
4538        /* Exclude nfs4_reclaim_open_stateid() - note nesting! */
4539        down_read(&nfsi->rwsem);
4540        if (do_vfs_lock(request->fl_file, request) == -ENOENT) {
4541                up_read(&nfsi->rwsem);
4542                mutex_unlock(&sp->so_delegreturn_mutex);
4543                goto out;
4544        }
4545        up_read(&nfsi->rwsem);
4546        mutex_unlock(&sp->so_delegreturn_mutex);
4547        if (status != 0)
4548                goto out;
4549        /* Is this a delegated lock? */
4550        if (test_bit(NFS_DELEGATED_STATE, &state->flags))
4551                goto out;
4552        lsp = request->fl_u.nfs4_fl.owner;
4553        seqid = nfs_alloc_seqid(&lsp->ls_seqid, GFP_KERNEL);
4554        status = -ENOMEM;
4555        if (seqid == NULL)
4556                goto out;
4557        task = nfs4_do_unlck(request, nfs_file_open_context(request->fl_file), lsp, seqid);
4558        status = PTR_ERR(task);
4559        if (IS_ERR(task))
4560                goto out;
4561        status = nfs4_wait_for_completion_rpc_task(task);
4562        rpc_put_task(task);
4563out:
4564        request->fl_flags = fl_flags;
4565        return status;
4566}
4567
4568struct nfs4_lockdata {
4569        struct nfs_lock_args arg;
4570        struct nfs_lock_res res;
4571        struct nfs4_lock_state *lsp;
4572        struct nfs_open_context *ctx;
4573        struct file_lock fl;
4574        unsigned long timestamp;
4575        int rpc_status;
4576        int cancelled;
4577        struct nfs_server *server;
4578};
4579
4580static struct nfs4_lockdata *nfs4_alloc_lockdata(struct file_lock *fl,
4581                struct nfs_open_context *ctx, struct nfs4_lock_state *lsp,
4582                gfp_t gfp_mask)
4583{
4584        struct nfs4_lockdata *p;
4585        struct inode *inode = lsp->ls_state->inode;
4586        struct nfs_server *server = NFS_SERVER(inode);
4587
4588        p = kzalloc(sizeof(*p), gfp_mask);
4589        if (p == NULL)
4590                return NULL;
4591
4592        p->arg.fh = NFS_FH(inode);
4593        p->arg.fl = &p->fl;
4594        p->arg.open_seqid = nfs_alloc_seqid(&lsp->ls_state->owner->so_seqid, gfp_mask);
4595        if (p->arg.open_seqid == NULL)
4596                goto out_free;
4597        p->arg.lock_seqid = nfs_alloc_seqid(&lsp->ls_seqid, gfp_mask);
4598        if (p->arg.lock_seqid == NULL)
4599                goto out_free_seqid;
4600        p->arg.lock_stateid = &lsp->ls_stateid;
4601        p->arg.lock_owner.clientid = server->nfs_client->cl_clientid;
4602        p->arg.lock_owner.id = lsp->ls_seqid.owner_id;
4603        p->arg.lock_owner.s_dev = server->s_dev;
4604        p->res.lock_seqid = p->arg.lock_seqid;
4605        p->lsp = lsp;
4606        p->server = server;
4607        atomic_inc(&lsp->ls_count);
4608        p->ctx = get_nfs_open_context(ctx);
4609        memcpy(&p->fl, fl, sizeof(p->fl));
4610        return p;
4611out_free_seqid:
4612        nfs_free_seqid(p->arg.open_seqid);
4613out_free:
4614        kfree(p);
4615        return NULL;
4616}
4617
4618static void nfs4_lock_prepare(struct rpc_task *task, void *calldata)
4619{
4620        struct nfs4_lockdata *data = calldata;
4621        struct nfs4_state *state = data->lsp->ls_state;
4622
4623        dprintk("%s: begin!\n", __func__);
4624        if (nfs_wait_on_sequence(data->arg.lock_seqid, task) != 0)
4625                goto out_wait;
4626        /* Do we need to do an open_to_lock_owner? */
4627        if (!(data->arg.lock_seqid->sequence->flags & NFS_SEQID_CONFIRMED)) {
4628                if (nfs_wait_on_sequence(data->arg.open_seqid, task) != 0) {
4629                        goto out_release_lock_seqid;
4630                }
4631                data->arg.open_stateid = &state->stateid;
4632                data->arg.new_lock_owner = 1;
4633                data->res.open_seqid = data->arg.open_seqid;
4634        } else
4635                data->arg.new_lock_owner = 0;
4636        data->timestamp = jiffies;
4637        if (nfs4_setup_sequence(data->server,
4638                                &data->arg.seq_args,
4639                                &data->res.seq_res,
4640                                task) == 0)
4641                return;
4642        nfs_release_seqid(data->arg.open_seqid);
4643out_release_lock_seqid:
4644        nfs_release_seqid(data->arg.lock_seqid);
4645out_wait:
4646        nfs4_sequence_done(task, &data->res.seq_res);
4647        dprintk("%s: done!, ret = %d\n", __func__, data->rpc_status);
4648}
4649
4650static void nfs4_lock_done(struct rpc_task *task, void *calldata)
4651{
4652        struct nfs4_lockdata *data = calldata;
4653
4654        dprintk("%s: begin!\n", __func__);
4655
4656        if (!nfs4_sequence_done(task, &data->res.seq_res))
4657                return;
4658
4659        data->rpc_status = task->tk_status;
4660        if (data->arg.new_lock_owner != 0) {
4661                if (data->rpc_status == 0)
4662                        nfs_confirm_seqid(&data->lsp->ls_seqid, 0);
4663                else
4664                        goto out;
4665        }
4666        if (data->rpc_status == 0) {
4667                nfs4_stateid_copy(&data->lsp->ls_stateid, &data->res.stateid);
4668                set_bit(NFS_LOCK_INITIALIZED, &data->lsp->ls_flags);
4669                renew_lease(NFS_SERVER(data->ctx->dentry->d_inode), data->timestamp);
4670        }
4671out:
4672        dprintk("%s: done, ret = %d!\n", __func__, data->rpc_status);
4673}
4674
4675static void nfs4_lock_release(void *calldata)
4676{
4677        struct nfs4_lockdata *data = calldata;
4678
4679        dprintk("%s: begin!\n", __func__);
4680        nfs_free_seqid(data->arg.open_seqid);
4681        if (data->cancelled != 0) {
4682                struct rpc_task *task;
4683                task = nfs4_do_unlck(&data->fl, data->ctx, data->lsp,
4684                                data->arg.lock_seqid);
4685                if (!IS_ERR(task))
4686                        rpc_put_task_async(task);
4687                dprintk("%s: cancelling lock!\n", __func__);
4688        } else
4689                nfs_free_seqid(data->arg.lock_seqid);
4690        nfs4_put_lock_state(data->lsp);
4691        put_nfs_open_context(data->ctx);
4692        kfree(data);
4693        dprintk("%s: done!\n", __func__);
4694}
4695
4696static const struct rpc_call_ops nfs4_lock_ops = {
4697        .rpc_call_prepare = nfs4_lock_prepare,
4698        .rpc_call_done = nfs4_lock_done,
4699        .rpc_release = nfs4_lock_release,
4700};
4701
4702static void nfs4_handle_setlk_error(struct nfs_server *server, struct nfs4_lock_state *lsp, int new_lock_owner, int error)
4703{
4704        switch (error) {
4705        case -NFS4ERR_ADMIN_REVOKED:
4706        case -NFS4ERR_BAD_STATEID:
4707                lsp->ls_seqid.flags &= ~NFS_SEQID_CONFIRMED;
4708                if (new_lock_owner != 0 ||
4709                   test_bit(NFS_LOCK_INITIALIZED, &lsp->ls_flags) != 0)
4710                        nfs4_schedule_stateid_recovery(server, lsp->ls_state);
4711                break;
4712        case -NFS4ERR_STALE_STATEID:
4713                lsp->ls_seqid.flags &= ~NFS_SEQID_CONFIRMED;
4714        case -NFS4ERR_EXPIRED:
4715                nfs4_schedule_lease_recovery(server->nfs_client);
4716        };
4717}
4718
4719static int _nfs4_do_setlk(struct nfs4_state *state, int cmd, struct file_lock *fl, int recovery_type)
4720{
4721        struct nfs4_lockdata *data;
4722        struct rpc_task *task;
4723        struct rpc_message msg = {
4724                .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_LOCK],
4725                .rpc_cred = state->owner->so_cred,
4726        };
4727        struct rpc_task_setup task_setup_data = {
4728                .rpc_client = NFS_CLIENT(state->inode),
4729                .rpc_message = &msg,
4730                .callback_ops = &nfs4_lock_ops,
4731                .workqueue = nfsiod_workqueue,
4732                .flags = RPC_TASK_ASYNC,
4733        };
4734        int ret;
4735
4736        dprintk("%s: begin!\n", __func__);
4737        data = nfs4_alloc_lockdata(fl, nfs_file_open_context(fl->fl_file),
4738                        fl->fl_u.nfs4_fl.owner,
4739                        recovery_type == NFS_LOCK_NEW ? GFP_KERNEL : GFP_NOFS);
4740        if (data == NULL)
4741                return -ENOMEM;
4742        if (IS_SETLKW(cmd))
4743                data->arg.block = 1;
4744        nfs41_init_sequence(&data->arg.seq_args, &data->res.seq_res, 1);
4745        msg.rpc_argp = &data->arg;
4746        msg.rpc_resp = &data->res;
4747        task_setup_data.callback_data = data;
4748        if (recovery_type > NFS_LOCK_NEW) {
4749                if (recovery_type == NFS_LOCK_RECLAIM)
4750                        data->arg.reclaim = NFS_LOCK_RECLAIM;
4751                nfs4_set_sequence_privileged(&data->arg.seq_args);
4752        }
4753        task = rpc_run_task(&task_setup_data);
4754        if (IS_ERR(task))
4755                return PTR_ERR(task);
4756        ret = nfs4_wait_for_completion_rpc_task(task);
4757        if (ret == 0) {
4758                ret = data->rpc_status;
4759                if (ret)
4760                        nfs4_handle_setlk_error(data->server, data->lsp,
4761                                        data->arg.new_lock_owner, ret);
4762        } else
4763                data->cancelled = 1;
4764        rpc_put_task(task);
4765        dprintk("%s: done, ret = %d!\n", __func__, ret);
4766        return ret;
4767}
4768
4769static int nfs4_lock_reclaim(struct nfs4_state *state, struct file_lock *request)
4770{
4771        struct nfs_server *server = NFS_SERVER(state->inode);
4772        struct nfs4_exception exception = {
4773                .inode = state->inode,
4774        };
4775        int err;
4776
4777        do {
4778                /* Cache the lock if possible... */
4779                if (test_bit(NFS_DELEGATED_STATE, &state->flags) != 0)
4780                        return 0;
4781                err = _nfs4_do_setlk(state, F_SETLK, request, NFS_LOCK_RECLAIM);
4782                if (err != -NFS4ERR_DELAY)
4783                        break;
4784                nfs4_handle_exception(server, err, &exception);
4785        } while (exception.retry);
4786        return err;
4787}
4788
4789static int nfs4_lock_expired(struct nfs4_state *state, struct file_lock *request)
4790{
4791        struct nfs_server *server = NFS_SERVER(state->inode);
4792        struct nfs4_exception exception = {
4793                .inode = state->inode,
4794        };
4795        int err;
4796
4797        err = nfs4_set_lock_state(state, request);
4798        if (err != 0)
4799                return err;
4800        do {
4801                if (test_bit(NFS_DELEGATED_STATE, &state->flags) != 0)
4802                        return 0;
4803                err = _nfs4_do_setlk(state, F_SETLK, request, NFS_LOCK_EXPIRED);
4804                switch (err) {
4805                default:
4806                        goto out;
4807                case -NFS4ERR_GRACE:
4808                case -NFS4ERR_DELAY:
4809                        nfs4_handle_exception(server, err, &exception);
4810                        err = 0;
4811                }
4812        } while (exception.retry);
4813out:
4814        return err;
4815}
4816
4817#if defined(CONFIG_NFS_V4_1)
4818/**
4819 * nfs41_check_expired_locks - possibly free a lock stateid
4820 *
4821 * @state: NFSv4 state for an inode
4822 *
4823 * Returns NFS_OK if recovery for this stateid is now finished.
4824 * Otherwise a negative NFS4ERR value is returned.
4825 */
4826static int nfs41_check_expired_locks(struct nfs4_state *state)
4827{
4828        int status, ret = -NFS4ERR_BAD_STATEID;
4829        struct nfs4_lock_state *lsp;
4830        struct nfs_server *server = NFS_SERVER(state->inode);
4831
4832        list_for_each_entry(lsp, &state->lock_states, ls_locks) {
4833                if (test_bit(NFS_LOCK_INITIALIZED, &lsp->ls_flags)) {
4834                        status = nfs41_test_stateid(server, &lsp->ls_stateid);
4835                        if (status != NFS_OK) {
4836                                /* Free the stateid unless the server
4837                                 * informs us the stateid is unrecognized. */
4838                                if (status != -NFS4ERR_BAD_STATEID)
4839                                        nfs41_free_stateid(server,
4840                                                        &lsp->ls_stateid);
4841                                clear_bit(NFS_LOCK_INITIALIZED, &lsp->ls_flags);
4842                                ret = status;
4843                        }
4844                }
4845        };
4846
4847        return ret;
4848}
4849
4850static int nfs41_lock_expired(struct nfs4_state *state, struct file_lock *request)
4851{
4852        int status = NFS_OK;
4853
4854        if (test_bit(LK_STATE_IN_USE, &state->flags))
4855                status = nfs41_check_expired_locks(state);
4856        if (status != NFS_OK)
4857                status = nfs4_lock_expired(state, request);
4858        return status;
4859}
4860#endif
4861
4862static int _nfs4_proc_setlk(struct nfs4_state *state, int cmd, struct file_lock *request)
4863{
4864        struct nfs4_state_owner *sp = state->owner;
4865        struct nfs_inode *nfsi = NFS_I(state->inode);
4866        unsigned char fl_flags = request->fl_flags;
4867        unsigned int seq;
4868        int status = -ENOLCK;
4869
4870        if ((fl_flags & FL_POSIX) &&
4871                        !test_bit(NFS_STATE_POSIX_LOCKS, &state->flags))
4872                goto out;
4873        /* Is this a delegated open? */
4874        status = nfs4_set_lock_state(state, request);
4875        if (status != 0)
4876                goto out;
4877        request->fl_flags |= FL_ACCESS;
4878        status = do_vfs_lock(request->fl_file, request);
4879        if (status < 0)
4880                goto out;
4881        down_read(&nfsi->rwsem);
4882        if (test_bit(NFS_DELEGATED_STATE, &state->flags)) {
4883                /* Yes: cache locks! */
4884                /* ...but avoid races with delegation recall... */
4885                request->fl_flags = fl_flags & ~FL_SLEEP;
4886                status = do_vfs_lock(request->fl_file, request);
4887                goto out_unlock;
4888        }
4889        seq = raw_seqcount_begin(&sp->so_reclaim_seqcount);
4890        up_read(&nfsi->rwsem);
4891        status = _nfs4_do_setlk(state, cmd, request, NFS_LOCK_NEW);
4892        if (status != 0)
4893                goto out;
4894        down_read(&nfsi->rwsem);
4895        if (read_seqcount_retry(&sp->so_reclaim_seqcount, seq)) {
4896                status = -NFS4ERR_DELAY;
4897                goto out_unlock;
4898        }
4899        /* Note: we always want to sleep here! */
4900        request->fl_flags = fl_flags | FL_SLEEP;
4901        if (do_vfs_lock(request->fl_file, request) < 0)
4902                printk(KERN_WARNING "NFS: %s: VFS is out of sync with lock "
4903                        "manager!\n", __func__);
4904out_unlock:
4905        up_read(&nfsi->rwsem);
4906out:
4907        request->fl_flags = fl_flags;
4908        return status;
4909}
4910
4911static int nfs4_proc_setlk(struct nfs4_state *state, int cmd, struct file_lock *request)
4912{
4913        struct nfs4_exception exception = {
4914                .state = state,
4915                .inode = state->inode,
4916        };
4917        int err;
4918
4919        do {
4920                err = _nfs4_proc_setlk(state, cmd, request);
4921                if (err == -NFS4ERR_DENIED)
4922                        err = -EAGAIN;
4923                err = nfs4_handle_exception(NFS_SERVER(state->inode),
4924                                err, &exception);
4925        } while (exception.retry);
4926        return err;
4927}
4928
4929static int
4930nfs4_proc_lock(struct file *filp, int cmd, struct file_lock *request)
4931{
4932        struct nfs_open_context *ctx;
4933        struct nfs4_state *state;
4934        unsigned long timeout = NFS4_LOCK_MINTIMEOUT;
4935        int status;
4936
4937        /* verify open state */
4938        ctx = nfs_file_open_context(filp);
4939        state = ctx->state;
4940
4941        if (request->fl_start < 0 || request->fl_end < 0)
4942                return -EINVAL;
4943
4944        if (IS_GETLK(cmd)) {
4945                if (state != NULL)
4946                        return nfs4_proc_getlk(state, F_GETLK, request);
4947                return 0;
4948        }
4949
4950        if (!(IS_SETLK(cmd) || IS_SETLKW(cmd)))
4951                return -EINVAL;
4952
4953        if (request->fl_type == F_UNLCK) {
4954                if (state != NULL)
4955                        return nfs4_proc_unlck(state, cmd, request);
4956                return 0;
4957        }
4958
4959        if (state == NULL)
4960                return -ENOLCK;
4961        /*
4962         * Don't rely on the VFS having checked the file open mode,
4963         * since it won't do this for flock() locks.
4964         */
4965        switch (request->fl_type) {
4966        case F_RDLCK:
4967                if (!(filp->f_mode & FMODE_READ))
4968                        return -EBADF;
4969                break;
4970        case F_WRLCK:
4971                if (!(filp->f_mode & FMODE_WRITE))
4972                        return -EBADF;
4973        }
4974
4975        do {
4976                status = nfs4_proc_setlk(state, cmd, request);
4977                if ((status != -EAGAIN) || IS_SETLK(cmd))
4978                        break;
4979                timeout = nfs4_set_lock_task_retry(timeout);
4980                status = -ERESTARTSYS;
4981                if (signalled())
4982                        break;
4983        } while(status < 0);
4984        return status;
4985}
4986
4987int nfs4_lock_delegation_recall(struct nfs4_state *state, struct file_lock *fl)
4988{
4989        struct nfs_server *server = NFS_SERVER(state->inode);
4990        struct nfs4_exception exception = { };
4991        int err;
4992
4993        err = nfs4_set_lock_state(state, fl);
4994        if (err != 0)
4995                goto out;
4996        do {
4997                err = _nfs4_do_setlk(state, F_SETLK, fl, NFS_LOCK_NEW);
4998                switch (err) {
4999                        default:
5000                                printk(KERN_ERR "NFS: %s: unhandled error "
5001                                        "%d.\n", __func__, err);
5002                        case 0:
5003                        case -ESTALE:
5004                                goto out;
5005                        case -NFS4ERR_STALE_CLIENTID:
5006                        case -NFS4ERR_STALE_STATEID:
5007                                set_bit(NFS_DELEGATED_STATE, &state->flags);
5008                        case -NFS4ERR_EXPIRED:
5009                                nfs4_schedule_lease_recovery(server->nfs_client);
5010                                err = -EAGAIN;
5011                                goto out;
5012                        case -NFS4ERR_BADSESSION:
5013                        case -NFS4ERR_BADSLOT:
5014                        case -NFS4ERR_BAD_HIGH_SLOT:
5015                        case -NFS4ERR_CONN_NOT_BOUND_TO_SESSION:
5016                        case -NFS4ERR_DEADSESSION:
5017                                set_bit(NFS_DELEGATED_STATE, &state->flags);
5018                                nfs4_schedule_session_recovery(server->nfs_client->cl_session, err);
5019                                err = -EAGAIN;
5020                                goto out;
5021                        case -NFS4ERR_DELEG_REVOKED:
5022                        case -NFS4ERR_ADMIN_REVOKED:
5023                        case -NFS4ERR_BAD_STATEID:
5024                        case -NFS4ERR_OPENMODE:
5025                                nfs4_schedule_stateid_recovery(server, state);
5026                                err = 0;
5027                                goto out;
5028                        case -ENOMEM:
5029                        case -NFS4ERR_DENIED:
5030                                /* kill_proc(fl->fl_pid, SIGLOST, 1); */
5031                                err = 0;
5032                                goto out;
5033                }
5034                set_bit(NFS_DELEGATED_STATE, &state->flags);
5035                err = nfs4_handle_exception(server, err, &exception);
5036        } while (exception.retry);
5037out:
5038        return err;
5039}
5040
5041struct nfs_release_lockowner_data {
5042        struct nfs4_lock_state *lsp;
5043        struct nfs_server *server;
5044        struct nfs_release_lockowner_args args;
5045};
5046
5047static void nfs4_release_lockowner_release(void *calldata)
5048{
5049        struct nfs_release_lockowner_data *data = calldata;
5050        nfs4_free_lock_state(data->server, data->lsp);
5051        kfree(calldata);
5052}
5053
5054static const struct rpc_call_ops nfs4_release_lockowner_ops = {
5055        .rpc_release = nfs4_release_lockowner_release,
5056};
5057
5058int nfs4_release_lockowner(struct nfs4_lock_state *lsp)
5059{
5060        struct nfs_server *server = lsp->ls_state->owner->so_server;
5061        struct nfs_release_lockowner_data *data;
5062        struct rpc_message msg = {
5063                .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_RELEASE_LOCKOWNER],
5064        };
5065
5066        if (server->nfs_client->cl_mvops->minor_version != 0)
5067                return -EINVAL;
5068        data = kmalloc(sizeof(*data), GFP_NOFS);
5069        if (!data)
5070                return -ENOMEM;
5071        data->lsp = lsp;
5072        data->server = server;
5073        data->args.lock_owner.clientid = server->nfs_client->cl_clientid;
5074        data->args.lock_owner.id = lsp->ls_seqid.owner_id;
5075        data->args.lock_owner.s_dev = server->s_dev;
5076        msg.rpc_argp = &data->args;
5077        rpc_call_async(server->client, &msg, 0, &nfs4_release_lockowner_ops, data);
5078        return 0;
5079}
5080
5081#define XATTR_NAME_NFSV4_ACL "system.nfs4_acl"
5082
5083static int nfs4_xattr_set_nfs4_acl(struct dentry *dentry, const char *key,
5084                                   const void *buf, size_t buflen,
5085                                   int flags, int type)
5086{
5087        if (strcmp(key, "") != 0)
5088                return -EINVAL;
5089
5090        return nfs4_proc_set_acl(dentry->d_inode, buf, buflen);
5091}
5092
5093static int nfs4_xattr_get_nfs4_acl(struct dentry *dentry, const char *key,
5094                                   void *buf, size_t buflen, int type)
5095{
5096        if (strcmp(key, "") != 0)
5097                return -EINVAL;
5098
5099        return nfs4_proc_get_acl(dentry->d_inode, buf, buflen);
5100}
5101
5102static size_t nfs4_xattr_list_nfs4_acl(struct dentry *dentry, char *list,
5103                                       size_t list_len, const char *name,
5104                                       size_t name_len, int type)
5105{
5106        size_t len = sizeof(XATTR_NAME_NFSV4_ACL);
5107
5108        if (!nfs4_server_supports_acls(NFS_SERVER(dentry->d_inode)))
5109                return 0;
5110
5111        if (list && len <= list_len)
5112                memcpy(list, XATTR_NAME_NFSV4_ACL, len);
5113        return len;
5114}
5115
5116/*
5117 * nfs_fhget will use either the mounted_on_fileid or the fileid
5118 */
5119static void nfs_fixup_referral_attributes(struct nfs_fattr *fattr)
5120{
5121        if (!(((fattr->valid & NFS_ATTR_FATTR_MOUNTED_ON_FILEID) ||
5122               (fattr->valid & NFS_ATTR_FATTR_FILEID)) &&
5123              (fattr->valid & NFS_ATTR_FATTR_FSID) &&
5124              (fattr->valid & NFS_ATTR_FATTR_V4_LOCATIONS)))
5125                return;
5126
5127        fattr->valid |= NFS_ATTR_FATTR_TYPE | NFS_ATTR_FATTR_MODE |
5128                NFS_ATTR_FATTR_NLINK | NFS_ATTR_FATTR_V4_REFERRAL;
5129        fattr->mode = S_IFDIR | S_IRUGO | S_IXUGO;
5130        fattr->nlink = 2;
5131}
5132
5133static int _nfs4_proc_fs_locations(struct rpc_clnt *client, struct inode *dir,
5134                                   const struct qstr *name,
5135                                   struct nfs4_fs_locations *fs_locations,
5136                                   struct page *page)
5137{
5138        struct nfs_server *server = NFS_SERVER(dir);
5139        u32 bitmask[2] = {
5140                [0] = FATTR4_WORD0_FSID | FATTR4_WORD0_FS_LOCATIONS,
5141        };
5142        struct nfs4_fs_locations_arg args = {
5143                .dir_fh = NFS_FH(dir),
5144                .name = name,
5145                .page = page,
5146                .bitmask = bitmask,
5147        };
5148        struct nfs4_fs_locations_res res = {
5149                .fs_locations = fs_locations,
5150        };
5151        struct rpc_message msg = {
5152                .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_FS_LOCATIONS],
5153                .rpc_argp = &args,
5154                .rpc_resp = &res,
5155        };
5156        int status;
5157
5158        dprintk("%s: start\n", __func__);
5159
5160        /* Ask for the fileid of the absent filesystem if mounted_on_fileid
5161         * is not supported */
5162        if (NFS_SERVER(dir)->attr_bitmask[1] & FATTR4_WORD1_MOUNTED_ON_FILEID)
5163                bitmask[1] |= FATTR4_WORD1_MOUNTED_ON_FILEID;
5164        else
5165                bitmask[0] |= FATTR4_WORD0_FILEID;
5166
5167        nfs_fattr_init(&fs_locations->fattr);
5168        fs_locations->server = server;
5169        fs_locations->nlocations = 0;
5170        status = nfs4_call_sync(client, server, &msg, &args.seq_args, &res.seq_res, 0);
5171        dprintk("%s: returned status = %d\n", __func__, status);
5172        return status;
5173}
5174
5175int nfs4_proc_fs_locations(struct rpc_clnt *client, struct inode *dir,
5176                           const struct qstr *name,
5177                           struct nfs4_fs_locations *fs_locations,
5178                           struct page *page)
5179{
5180        struct nfs4_exception exception = { };
5181        int err;
5182        do {
5183                err = nfs4_handle_exception(NFS_SERVER(dir),
5184                                _nfs4_proc_fs_locations(client, dir, name, fs_locations, page),
5185                                &exception);
5186        } while (exception.retry);
5187        return err;
5188}
5189
5190static int _nfs4_proc_secinfo(struct inode *dir, const struct qstr *name, struct nfs4_secinfo_flavors *flavors)
5191{
5192        int status;
5193        struct nfs4_secinfo_arg args = {
5194                .dir_fh = NFS_FH(dir),
5195                .name   = name,
5196        };
5197        struct nfs4_secinfo_res res = {
5198                .flavors     = flavors,
5199        };
5200        struct rpc_message msg = {
5201                .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_SECINFO],
5202                .rpc_argp = &args,
5203                .rpc_resp = &res,
5204        };
5205
5206        dprintk("NFS call  secinfo %s\n", name->name);
5207        status = nfs4_call_sync(NFS_SERVER(dir)->client, NFS_SERVER(dir), &msg, &args.seq_args, &res.seq_res, 0);
5208        dprintk("NFS reply  secinfo: %d\n", status);
5209        return status;
5210}
5211
5212int nfs4_proc_secinfo(struct inode *dir, const struct qstr *name,
5213                      struct nfs4_secinfo_flavors *flavors)
5214{
5215        struct nfs4_exception exception = { };
5216        int err;
5217        do {
5218                err = nfs4_handle_exception(NFS_SERVER(dir),
5219                                _nfs4_proc_secinfo(dir, name, flavors),
5220                                &exception);
5221        } while (exception.retry);
5222        return err;
5223}
5224
5225#ifdef CONFIG_NFS_V4_1
5226/*
5227 * Check the exchange flags returned by the server for invalid flags, having
5228 * both PNFS and NON_PNFS flags set, and not having one of NON_PNFS, PNFS, or
5229 * DS flags set.
5230 */
5231static int nfs4_check_cl_exchange_flags(u32 flags)
5232{
5233        if (flags & ~EXCHGID4_FLAG_MASK_R)
5234                goto out_inval;
5235        if ((flags & EXCHGID4_FLAG_USE_PNFS_MDS) &&
5236            (flags & EXCHGID4_FLAG_USE_NON_PNFS))
5237                goto out_inval;
5238        if (!(flags & (EXCHGID4_FLAG_MASK_PNFS)))
5239                goto out_inval;
5240        return NFS_OK;
5241out_inval:
5242        return -NFS4ERR_INVAL;
5243}
5244
5245static bool
5246nfs41_same_server_scope(struct nfs41_server_scope *a,
5247                        struct nfs41_server_scope *b)
5248{
5249        if (a->server_scope_sz == b->server_scope_sz &&
5250            memcmp(a->server_scope, b->server_scope, a->server_scope_sz) == 0)
5251                return true;
5252
5253        return false;
5254}
5255
5256/*
5257 * nfs4_proc_bind_conn_to_session()
5258 *
5259 * The 4.1 client currently uses the same TCP connection for the
5260 * fore and backchannel.
5261 */
5262int nfs4_proc_bind_conn_to_session(struct nfs_client *clp, struct rpc_cred *cred)
5263{
5264        int status;
5265        struct nfs41_bind_conn_to_session_res res;
5266        struct rpc_message msg = {
5267                .rpc_proc =
5268                        &nfs4_procedures[NFSPROC4_CLNT_BIND_CONN_TO_SESSION],
5269                .rpc_argp = clp,
5270                .rpc_resp = &res,
5271                .rpc_cred = cred,
5272        };
5273
5274        dprintk("--> %s\n", __func__);
5275
5276        res.session = kzalloc(sizeof(struct nfs4_session), GFP_NOFS);
5277        if (unlikely(res.session == NULL)) {
5278                status = -ENOMEM;
5279                goto out;
5280        }
5281
5282        status = rpc_call_sync(clp->cl_rpcclient, &msg, RPC_TASK_TIMEOUT);
5283        if (status == 0) {
5284                if (memcmp(res.session->sess_id.data,
5285                    clp->cl_session->sess_id.data, NFS4_MAX_SESSIONID_LEN)) {
5286                        dprintk("NFS: %s: Session ID mismatch\n", __func__);
5287                        status = -EIO;
5288                        goto out_session;
5289                }
5290                if (res.dir != NFS4_CDFS4_BOTH) {
5291                        dprintk("NFS: %s: Unexpected direction from server\n",
5292                                __func__);
5293                        status = -EIO;
5294                        goto out_session;
5295                }
5296                if (res.use_conn_in_rdma_mode) {
5297                        dprintk("NFS: %s: Server returned RDMA mode = true\n",
5298                                __func__);
5299                        status = -EIO;
5300                        goto out_session;
5301                }
5302        }
5303out_session:
5304        kfree(res.session);
5305out:
5306        dprintk("<-- %s status= %d\n", __func__, status);
5307        return status;
5308}
5309
5310/*
5311 * nfs4_proc_exchange_id()
5312 *
5313 * Returns zero, a negative errno, or a negative NFS4ERR status code.
5314 *
5315 * Since the clientid has expired, all compounds using sessions
5316 * associated with the stale clientid will be returning
5317 * NFS4ERR_BADSESSION in the sequence operation, and will therefore
5318 * be in some phase of session reset.
5319 */
5320int nfs4_proc_exchange_id(struct nfs_client *clp, struct rpc_cred *cred)
5321{
5322        nfs4_verifier verifier;
5323        struct nfs41_exchange_id_args args = {
5324                .verifier = &verifier,
5325                .client = clp,
5326                .flags = EXCHGID4_FLAG_SUPP_MOVED_REFER,
5327        };
5328        struct nfs41_exchange_id_res res = {
5329                0
5330        };
5331        int status;
5332        struct rpc_message msg = {
5333                .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_EXCHANGE_ID],
5334                .rpc_argp = &args,
5335                .rpc_resp = &res,
5336                .rpc_cred = cred,
5337        };
5338
5339        nfs4_init_boot_verifier(clp, &verifier);
5340        args.id_len = nfs4_init_uniform_client_string(clp, args.id,
5341                                                        sizeof(args.id));
5342        dprintk("NFS call  exchange_id auth=%s, '%.*s'\n",
5343                clp->cl_rpcclient->cl_auth->au_ops->au_name,
5344                args.id_len, args.id);
5345
5346        res.server_owner = kzalloc(sizeof(struct nfs41_server_owner),
5347                                        GFP_NOFS);
5348        if (unlikely(res.server_owner == NULL)) {
5349                status = -ENOMEM;
5350                goto out;
5351        }
5352
5353        res.server_scope = kzalloc(sizeof(struct nfs41_server_scope),
5354                                        GFP_NOFS);
5355        if (unlikely(res.server_scope == NULL)) {
5356                status = -ENOMEM;
5357                goto out_server_owner;
5358        }
5359
5360        res.impl_id = kzalloc(sizeof(struct nfs41_impl_id), GFP_NOFS);
5361        if (unlikely(res.impl_id == NULL)) {
5362                status = -ENOMEM;
5363                goto out_server_scope;
5364        }
5365
5366        status = rpc_call_sync(clp->cl_rpcclient, &msg, RPC_TASK_TIMEOUT);
5367        if (status == 0)
5368                status = nfs4_check_cl_exchange_flags(res.flags);
5369
5370        if (status == 0) {
5371                clp->cl_clientid = res.clientid;
5372                clp->cl_exchange_flags = (res.flags & ~EXCHGID4_FLAG_CONFIRMED_R);
5373                if (!(res.flags & EXCHGID4_FLAG_CONFIRMED_R))
5374                        clp->cl_seqid = res.seqid;
5375
5376                kfree(clp->cl_serverowner);
5377                clp->cl_serverowner = res.server_owner;
5378                res.server_owner = NULL;
5379
5380                /* use the most recent implementation id */
5381                kfree(clp->cl_implid);
5382                clp->cl_implid = res.impl_id;
5383
5384                if (clp->cl_serverscope != NULL &&
5385                    !nfs41_same_server_scope(clp->cl_serverscope,
5386                                             res.server_scope)) {
5387                        dprintk("%s: server_scope mismatch detected\n",
5388                                __func__);
5389                        set_bit(NFS4CLNT_SERVER_SCOPE_MISMATCH, &clp->cl_state);
5390                        kfree(clp->cl_serverscope);
5391                        clp->cl_serverscope = NULL;
5392                }
5393
5394                if (clp->cl_serverscope == NULL) {
5395                        clp->cl_serverscope = res.server_scope;
5396                        goto out;
5397                }
5398        } else
5399                kfree(res.impl_id);
5400
5401out_server_owner:
5402        kfree(res.server_owner);
5403out_server_scope:
5404        kfree(res.server_scope);
5405out:
5406        if (clp->cl_implid != NULL)
5407                dprintk("NFS reply exchange_id: Server Implementation ID: "
5408                        "domain: %s, name: %s, date: %llu,%u\n",
5409                        clp->cl_implid->domain, clp->cl_implid->name,
5410                        clp->cl_implid->date.seconds,
5411                        clp->cl_implid->date.nseconds);
5412        dprintk("NFS reply exchange_id: %d\n", status);
5413        return status;
5414}
5415
5416static int _nfs4_proc_destroy_clientid(struct nfs_client *clp,
5417                struct rpc_cred *cred)
5418{
5419        struct rpc_message msg = {
5420                .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_DESTROY_CLIENTID],
5421                .rpc_argp = clp,
5422                .rpc_cred = cred,
5423        };
5424        int status;
5425
5426        status = rpc_call_sync(clp->cl_rpcclient, &msg, RPC_TASK_TIMEOUT);
5427        if (status)
5428                dprintk("NFS: Got error %d from the server %s on "
5429                        "DESTROY_CLIENTID.", status, clp->cl_hostname);
5430        return status;
5431}
5432
5433static int nfs4_proc_destroy_clientid(struct nfs_client *clp,
5434                struct rpc_cred *cred)
5435{
5436        unsigned int loop;
5437        int ret;
5438
5439        for (loop = NFS4_MAX_LOOP_ON_RECOVER; loop != 0; loop--) {
5440                ret = _nfs4_proc_destroy_clientid(clp, cred);
5441                switch (ret) {
5442                case -NFS4ERR_DELAY:
5443                case -NFS4ERR_CLIENTID_BUSY:
5444                        ssleep(1);
5445                        break;
5446                default:
5447                        return ret;
5448                }
5449        }
5450        return 0;
5451}
5452
5453int nfs4_destroy_clientid(struct nfs_client *clp)
5454{
5455        struct rpc_cred *cred;
5456        int ret = 0;
5457
5458        if (clp->cl_mvops->minor_version < 1)
5459                goto out;
5460        if (clp->cl_exchange_flags == 0)
5461                goto out;
5462        if (clp->cl_preserve_clid)
5463                goto out;
5464        cred = nfs4_get_exchange_id_cred(clp);
5465        ret = nfs4_proc_destroy_clientid(clp, cred);
5466        if (cred)
5467                put_rpccred(cred);
5468        switch (ret) {
5469        case 0:
5470        case -NFS4ERR_STALE_CLIENTID:
5471                clp->cl_exchange_flags = 0;
5472        }
5473out:
5474        return ret;
5475}
5476
5477struct nfs4_get_lease_time_data {
5478        struct nfs4_get_lease_time_args *args;
5479        struct nfs4_get_lease_time_res *res;
5480        struct nfs_client *clp;
5481};
5482
5483static void nfs4_get_lease_time_prepare(struct rpc_task *task,
5484                                        void *calldata)
5485{
5486        struct nfs4_get_lease_time_data *data =
5487                        (struct nfs4_get_lease_time_data *)calldata;
5488
5489        dprintk("--> %s\n", __func__);
5490        /* just setup sequence, do not trigger session recovery
5491           since we're invoked within one */
5492        nfs41_setup_sequence(data->clp->cl_session,
5493                        &data->args->la_seq_args,
5494                        &data->res->lr_seq_res,
5495                        task);
5496        dprintk("<-- %s\n", __func__);
5497}
5498
5499/*
5500 * Called from nfs4_state_manager thread for session setup, so don't recover
5501 * from sequence operation or clientid errors.
5502 */
5503static void nfs4_get_lease_time_done(struct rpc_task *task, void *calldata)
5504{
5505        struct nfs4_get_lease_time_data *data =
5506                        (struct nfs4_get_lease_time_data *)calldata;
5507
5508        dprintk("--> %s\n", __func__);
5509        if (!nfs41_sequence_done(task, &data->res->lr_seq_res))
5510                return;
5511        switch (task->tk_status) {
5512        case -NFS4ERR_DELAY:
5513        case -NFS4ERR_GRACE:
5514                dprintk("%s Retry: tk_status %d\n", __func__, task->tk_status);
5515                rpc_delay(task, NFS4_POLL_RETRY_MIN);
5516                task->tk_status = 0;
5517                /* fall through */
5518        case -NFS4ERR_RETRY_UNCACHED_REP:
5519                rpc_restart_call_prepare(task);
5520                return;
5521        }
5522        dprintk("<-- %s\n", __func__);
5523}
5524
5525static const struct rpc_call_ops nfs4_get_lease_time_ops = {
5526        .rpc_call_prepare = nfs4_get_lease_time_prepare,
5527        .rpc_call_done = nfs4_get_lease_time_done,
5528};
5529
5530int nfs4_proc_get_lease_time(struct nfs_client *clp, struct nfs_fsinfo *fsinfo)
5531{
5532        struct rpc_task *task;
5533        struct nfs4_get_lease_time_args args;
5534        struct nfs4_get_lease_time_res res = {
5535                .lr_fsinfo = fsinfo,
5536        };
5537        struct nfs4_get_lease_time_data data = {
5538                .args = &args,
5539                .res = &res,
5540                .clp = clp,
5541        };
5542        struct rpc_message msg = {
5543                .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_GET_LEASE_TIME],
5544                .rpc_argp = &args,
5545                .rpc_resp = &res,
5546        };
5547        struct rpc_task_setup task_setup = {
5548                .rpc_client = clp->cl_rpcclient,
5549                .rpc_message = &msg,
5550                .callback_ops = &nfs4_get_lease_time_ops,
5551                .callback_data = &data,
5552                .flags = RPC_TASK_TIMEOUT,
5553        };
5554        int status;
5555
5556        nfs41_init_sequence(&args.la_seq_args, &res.lr_seq_res, 0);
5557        nfs4_set_sequence_privileged(&args.la_seq_args);
5558        dprintk("--> %s\n", __func__);
5559        task = rpc_run_task(&task_setup);
5560
5561        if (IS_ERR(task))
5562                status = PTR_ERR(task);
5563        else {
5564                status = task->tk_status;
5565                rpc_put_task(task);
5566        }
5567        dprintk("<-- %s return %d\n", __func__, status);
5568
5569        return status;
5570}
5571
5572/*
5573 * Initialize the values to be used by the client in CREATE_SESSION
5574 * If nfs4_init_session set the fore channel request and response sizes,
5575 * use them.
5576 *
5577 * Set the back channel max_resp_sz_cached to zero to force the client to
5578 * always set csa_cachethis to FALSE because the current implementation
5579 * of the back channel DRC only supports caching the CB_SEQUENCE operation.
5580 */
5581static void nfs4_init_channel_attrs(struct nfs41_create_session_args *args)
5582{
5583        struct nfs4_session *session = args->client->cl_session;
5584        unsigned int mxrqst_sz = session->fc_target_max_rqst_sz,
5585                     mxresp_sz = session->fc_target_max_resp_sz;
5586
5587        if (mxrqst_sz == 0)
5588                mxrqst_sz = NFS_MAX_FILE_IO_SIZE;
5589        if (mxresp_sz == 0)
5590                mxresp_sz = NFS_MAX_FILE_IO_SIZE;
5591        /* Fore channel attributes */
5592        args->fc_attrs.max_rqst_sz = mxrqst_sz;
5593        args->fc_attrs.max_resp_sz = mxresp_sz;
5594        args->fc_attrs.max_ops = NFS4_MAX_OPS;
5595        args->fc_attrs.max_reqs = max_session_slots;
5596
5597        dprintk("%s: Fore Channel : max_rqst_sz=%u max_resp_sz=%u "
5598                "max_ops=%u max_reqs=%u\n",
5599                __func__,
5600                args->fc_attrs.max_rqst_sz, args->fc_attrs.max_resp_sz,
5601                args->fc_attrs.max_ops, args->fc_attrs.max_reqs);
5602
5603        /* Back channel attributes */
5604        args->bc_attrs.max_rqst_sz = PAGE_SIZE;
5605        args->bc_attrs.max_resp_sz = PAGE_SIZE;
5606        args->bc_attrs.max_resp_sz_cached = 0;
5607        args->bc_attrs.max_ops = NFS4_MAX_BACK_CHANNEL_OPS;
5608        args->bc_attrs.max_reqs = 1;
5609
5610        dprintk("%s: Back Channel : max_rqst_sz=%u max_resp_sz=%u "
5611                "max_resp_sz_cached=%u max_ops=%u max_reqs=%u\n",
5612                __func__,
5613                args->bc_attrs.max_rqst_sz, args->bc_attrs.max_resp_sz,
5614                args->bc_attrs.max_resp_sz_cached, args->bc_attrs.max_ops,
5615                args->bc_attrs.max_reqs);
5616}
5617
5618static int nfs4_verify_fore_channel_attrs(struct nfs41_create_session_args *args, struct nfs4_session *session)
5619{
5620        struct nfs4_channel_attrs *sent = &args->fc_attrs;
5621        struct nfs4_channel_attrs *rcvd = &session->fc_attrs;
5622
5623        if (rcvd->max_resp_sz > sent->max_resp_sz)
5624                return -EINVAL;
5625        /*
5626         * Our requested max_ops is the minimum we need; we're not
5627         * prepared to break up compounds into smaller pieces than that.
5628         * So, no point even trying to continue if the server won't
5629         * cooperate:
5630         */
5631        if (rcvd->max_ops < sent->max_ops)
5632                return -EINVAL;
5633        if (rcvd->max_reqs == 0)
5634                return -EINVAL;
5635        if (rcvd->max_reqs > NFS4_MAX_SLOT_TABLE)
5636                rcvd->max_reqs = NFS4_MAX_SLOT_TABLE;
5637        return 0;
5638}
5639
5640static int nfs4_verify_back_channel_attrs(struct nfs41_create_session_args *args, struct nfs4_session *session)
5641{
5642        struct nfs4_channel_attrs *sent = &args->bc_attrs;
5643        struct nfs4_channel_attrs *rcvd = &session->bc_attrs;
5644
5645        if (rcvd->max_rqst_sz > sent->max_rqst_sz)
5646                return -EINVAL;
5647        if (rcvd->max_resp_sz < sent->max_resp_sz)
5648                return -EINVAL;
5649        if (rcvd->max_resp_sz_cached > sent->max_resp_sz_cached)
5650                return -EINVAL;
5651        /* These would render the backchannel useless: */
5652        if (rcvd->max_ops != sent->max_ops)
5653                return -EINVAL;
5654        if (rcvd->max_reqs != sent->max_reqs)
5655                return -EINVAL;
5656        return 0;
5657}
5658
5659static int nfs4_verify_channel_attrs(struct nfs41_create_session_args *args,
5660                                     struct nfs4_session *session)
5661{
5662        int ret;
5663
5664        ret = nfs4_verify_fore_channel_attrs(args, session);
5665        if (ret)
5666                return ret;
5667        return nfs4_verify_back_channel_attrs(args, session);
5668}
5669
5670static int _nfs4_proc_create_session(struct nfs_client *clp,
5671                struct rpc_cred *cred)
5672{
5673        struct nfs4_session *session = clp->cl_session;
5674        struct nfs41_create_session_args args = {
5675                .client = clp,
5676                .cb_program = NFS4_CALLBACK,
5677        };
5678        struct nfs41_create_session_res res = {
5679                .client = clp,
5680        };
5681        struct rpc_message msg = {
5682                .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_CREATE_SESSION],
5683                .rpc_argp = &args,
5684                .rpc_resp = &res,
5685                .rpc_cred = cred,
5686        };
5687        int status;
5688
5689        nfs4_init_channel_attrs(&args);
5690        args.flags = (SESSION4_PERSIST | SESSION4_BACK_CHAN);
5691
5692        status = rpc_call_sync(session->clp->cl_rpcclient, &msg, RPC_TASK_TIMEOUT);
5693
5694        if (!status) {
5695                /* Verify the session's negotiated channel_attrs values */
5696                status = nfs4_verify_channel_attrs(&args, session);
5697                /* Increment the clientid slot sequence id */
5698                clp->cl_seqid++;
5699        }
5700
5701        return status;
5702}
5703
5704/*
5705 * Issues a CREATE_SESSION operation to the server.
5706 * It is the responsibility of the caller to verify the session is
5707 * expired before calling this routine.
5708 */
5709int nfs4_proc_create_session(struct nfs_client *clp, struct rpc_cred *cred)
5710{
5711        int status;
5712        unsigned *ptr;
5713        struct nfs4_session *session = clp->cl_session;
5714
5715        dprintk("--> %s clp=%p session=%p\n", __func__, clp, session);
5716
5717        status = _nfs4_proc_create_session(clp, cred);
5718        if (status)
5719                goto out;
5720
5721        /* Init or reset the session slot tables */
5722        status = nfs4_setup_session_slot_tables(session);
5723        dprintk("slot table setup returned %d\n", status);
5724        if (status)
5725                goto out;
5726
5727        ptr = (unsigned *)&session->sess_id.data[0];
5728        dprintk("%s client>seqid %d sessionid %u:%u:%u:%u\n", __func__,
5729                clp->cl_seqid, ptr[0], ptr[1], ptr[2], ptr[3]);
5730out:
5731        dprintk("<-- %s\n", __func__);
5732        return status;
5733}
5734
5735/*
5736 * Issue the over-the-wire RPC DESTROY_SESSION.
5737 * The caller must serialize access to this routine.
5738 */
5739int nfs4_proc_destroy_session(struct nfs4_session *session,
5740                struct rpc_cred *cred)
5741{
5742        struct rpc_message msg = {
5743                .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_DESTROY_SESSION],
5744                .rpc_argp = session,
5745                .rpc_cred = cred,
5746        };
5747        int status = 0;
5748
5749        dprintk("--> nfs4_proc_destroy_session\n");
5750
5751        /* session is still being setup */
5752        if (session->clp->cl_cons_state != NFS_CS_READY)
5753                return status;
5754
5755        status = rpc_call_sync(session->clp->cl_rpcclient, &msg, RPC_TASK_TIMEOUT);
5756
5757        if (status)
5758                dprintk("NFS: Got error %d from the server on DESTROY_SESSION. "
5759                        "Session has been destroyed regardless...\n", status);
5760
5761        dprintk("<-- nfs4_proc_destroy_session\n");
5762        return status;
5763}
5764
5765/*
5766 * Renew the cl_session lease.
5767 */
5768struct nfs4_sequence_data {
5769        struct nfs_client *clp;
5770        struct nfs4_sequence_args args;
5771        struct nfs4_sequence_res res;
5772};
5773
5774static void nfs41_sequence_release(void *data)
5775{
5776        struct nfs4_sequence_data *calldata = data;
5777        struct nfs_client *clp = calldata->clp;
5778
5779        if (atomic_read(&clp->cl_count) > 1)
5780                nfs4_schedule_state_renewal(clp);
5781        nfs_put_client(clp);
5782        kfree(calldata);
5783}
5784
5785static int nfs41_sequence_handle_errors(struct rpc_task *task, struct nfs_client *clp)
5786{
5787        switch(task->tk_status) {
5788        case -NFS4ERR_DELAY:
5789                rpc_delay(task, NFS4_POLL_RETRY_MAX);
5790                return -EAGAIN;
5791        default:
5792                nfs4_schedule_lease_recovery(clp);
5793        }
5794        return 0;
5795}
5796
5797static void nfs41_sequence_call_done(struct rpc_task *task, void *data)
5798{
5799        struct nfs4_sequence_data *calldata = data;
5800        struct nfs_client *clp = calldata->clp;
5801
5802        if (!nfs41_sequence_done(task, task->tk_msg.rpc_resp))
5803                return;
5804
5805        if (task->tk_status < 0) {
5806                dprintk("%s ERROR %d\n", __func__, task->tk_status);
5807                if (atomic_read(&clp->cl_count) == 1)
5808                        goto out;
5809
5810                if (nfs41_sequence_handle_errors(task, clp) == -EAGAIN) {
5811                        rpc_restart_call_prepare(task);
5812                        return;
5813                }
5814        }
5815        dprintk("%s rpc_cred %p\n", __func__, task->tk_msg.rpc_cred);
5816out:
5817        dprintk("<-- %s\n", __func__);
5818}
5819
5820static void nfs41_sequence_prepare(struct rpc_task *task, void *data)
5821{
5822        struct nfs4_sequence_data *calldata = data;
5823        struct nfs_client *clp = calldata->clp;
5824        struct nfs4_sequence_args *args;
5825        struct nfs4_sequence_res *res;
5826
5827        args = task->tk_msg.rpc_argp;
5828        res = task->tk_msg.rpc_resp;
5829
5830        nfs41_setup_sequence(clp->cl_session, args, res, task);
5831}
5832
5833static const struct rpc_call_ops nfs41_sequence_ops = {
5834        .rpc_call_done = nfs41_sequence_call_done,
5835        .rpc_call_prepare = nfs41_sequence_prepare,
5836        .rpc_release = nfs41_sequence_release,
5837};
5838
5839static struct rpc_task *_nfs41_proc_sequence(struct nfs_client *clp,
5840                struct rpc_cred *cred,
5841                bool is_privileged)
5842{
5843        struct nfs4_sequence_data *calldata;
5844        struct rpc_message msg = {
5845                .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_SEQUENCE],
5846                .rpc_cred = cred,
5847        };
5848        struct rpc_task_setup task_setup_data = {
5849                .rpc_client = clp->cl_rpcclient,
5850                .rpc_message = &msg,
5851                .callback_ops = &nfs41_sequence_ops,
5852                .flags = RPC_TASK_ASYNC | RPC_TASK_SOFT,
5853        };
5854
5855        if (!atomic_inc_not_zero(&clp->cl_count))
5856                return ERR_PTR(-EIO);
5857        calldata = kzalloc(sizeof(*calldata), GFP_NOFS);
5858        if (calldata == NULL) {
5859                nfs_put_client(clp);
5860                return ERR_PTR(-ENOMEM);
5861        }
5862        nfs41_init_sequence(&calldata->args, &calldata->res, 0);
5863        if (is_privileged)
5864                nfs4_set_sequence_privileged(&calldata->args);
5865        msg.rpc_argp = &calldata->args;
5866        msg.rpc_resp = &calldata->res;
5867        calldata->clp = clp;
5868        task_setup_data.callback_data = calldata;
5869
5870        return rpc_run_task(&task_setup_data);
5871}
5872
5873static int nfs41_proc_async_sequence(struct nfs_client *clp, struct rpc_cred *cred, unsigned renew_flags)
5874{
5875        struct rpc_task *task;
5876        int ret = 0;
5877
5878        if ((renew_flags & NFS4_RENEW_TIMEOUT) == 0)
5879                return 0;
5880        task = _nfs41_proc_sequence(clp, cred, false);
5881        if (IS_ERR(task))
5882                ret = PTR_ERR(task);
5883        else
5884                rpc_put_task_async(task);
5885        dprintk("<-- %s status=%d\n", __func__, ret);
5886        return ret;
5887}
5888
5889static int nfs4_proc_sequence(struct nfs_client *clp, struct rpc_cred *cred)
5890{
5891        struct rpc_task *task;
5892        int ret;
5893
5894        task = _nfs41_proc_sequence(clp, cred, true);
5895        if (IS_ERR(task)) {
5896                ret = PTR_ERR(task);
5897                goto out;
5898        }
5899        ret = rpc_wait_for_completion_task(task);
5900        if (!ret) {
5901                struct nfs4_sequence_res *res = task->tk_msg.rpc_resp;
5902
5903                if (task->tk_status == 0)
5904                        nfs41_handle_sequence_flag_errors(clp, res->sr_status_flags);
5905                ret = task->tk_status;
5906        }
5907        rpc_put_task(task);
5908out:
5909        dprintk("<-- %s status=%d\n", __func__, ret);
5910        return ret;
5911}
5912
5913struct nfs4_reclaim_complete_data {
5914        struct nfs_client *clp;
5915        struct nfs41_reclaim_complete_args arg;
5916        struct nfs41_reclaim_complete_res res;
5917};
5918
5919static void nfs4_reclaim_complete_prepare(struct rpc_task *task, void *data)
5920{
5921        struct nfs4_reclaim_complete_data *calldata = data;
5922
5923        nfs41_setup_sequence(calldata->clp->cl_session,
5924                        &calldata->arg.seq_args,
5925                        &calldata->res.seq_res,
5926                        task);
5927}
5928
5929static int nfs41_reclaim_complete_handle_errors(struct rpc_task *task, struct nfs_client *clp)
5930{
5931        switch(task->tk_status) {
5932        case 0:
5933        case -NFS4ERR_COMPLETE_ALREADY:
5934        case -NFS4ERR_WRONG_CRED: /* What to do here? */
5935                break;
5936        case -NFS4ERR_DELAY:
5937                rpc_delay(task, NFS4_POLL_RETRY_MAX);
5938                /* fall through */
5939        case -NFS4ERR_RETRY_UNCACHED_REP:
5940                return -EAGAIN;
5941        default:
5942                nfs4_schedule_lease_recovery(clp);
5943        }
5944        return 0;
5945}
5946
5947static void nfs4_reclaim_complete_done(struct rpc_task *task, void *data)
5948{
5949        struct nfs4_reclaim_complete_data *calldata = data;
5950        struct nfs_client *clp = calldata->clp;
5951        struct nfs4_sequence_res *res = &calldata->res.seq_res;
5952
5953        dprintk("--> %s\n", __func__);
5954        if (!nfs41_sequence_done(task, res))
5955                return;
5956
5957        if (nfs41_reclaim_complete_handle_errors(task, clp) == -EAGAIN) {
5958                rpc_restart_call_prepare(task);
5959                return;
5960        }
5961        dprintk("<-- %s\n", __func__);
5962}
5963
5964static void nfs4_free_reclaim_complete_data(void *data)
5965{
5966        struct nfs4_reclaim_complete_data *calldata = data;
5967
5968        kfree(calldata);
5969}
5970
5971static const struct rpc_call_ops nfs4_reclaim_complete_call_ops = {
5972        .rpc_call_prepare = nfs4_reclaim_complete_prepare,
5973        .rpc_call_done = nfs4_reclaim_complete_done,
5974        .rpc_release = nfs4_free_reclaim_complete_data,
5975};
5976
5977/*
5978 * Issue a global reclaim complete.
5979 */
5980static int nfs41_proc_reclaim_complete(struct nfs_client *clp)
5981{
5982        struct nfs4_reclaim_complete_data *calldata;
5983        struct rpc_task *task;
5984        struct rpc_message msg = {
5985                .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_RECLAIM_COMPLETE],
5986        };
5987        struct rpc_task_setup task_setup_data = {
5988                .rpc_client = clp->cl_rpcclient,
5989                .rpc_message = &msg,
5990                .callback_ops = &nfs4_reclaim_complete_call_ops,
5991                .flags = RPC_TASK_ASYNC,
5992        };
5993        int status = -ENOMEM;
5994
5995        dprintk("--> %s\n", __func__);
5996        calldata = kzalloc(sizeof(*calldata), GFP_NOFS);
5997        if (calldata == NULL)
5998                goto out;
5999        calldata->clp = clp;
6000        calldata->arg.one_fs = 0;
6001
6002        nfs41_init_sequence(&calldata->arg.seq_args, &calldata->res.seq_res, 0);
6003        nfs4_set_sequence_privileged(&calldata->arg.seq_args);
6004        msg.rpc_argp = &calldata->arg;
6005        msg.rpc_resp = &calldata->res;
6006        task_setup_data.callback_data = calldata;
6007        task = rpc_run_task(&task_setup_data);
6008        if (IS_ERR(task)) {
6009                status = PTR_ERR(task);
6010                goto out;
6011        }
6012        status = nfs4_wait_for_completion_rpc_task(task);
6013        if (status == 0)
6014                status = task->tk_status;
6015        rpc_put_task(task);
6016        return 0;
6017out:
6018        dprintk("<-- %s status=%d\n", __func__, status);
6019        return status;
6020}
6021
6022static void
6023nfs4_layoutget_prepare(struct rpc_task *task, void *calldata)
6024{
6025        struct nfs4_layoutget *lgp = calldata;
6026        struct nfs_server *server = NFS_SERVER(lgp->args.inode);
6027        struct nfs4_session *session = nfs4_get_session(server);
6028
6029        dprintk("--> %s\n", __func__);
6030        /* Note the is a race here, where a CB_LAYOUTRECALL can come in
6031         * right now covering the LAYOUTGET we are about to send.
6032         * However, that is not so catastrophic, and there seems
6033         * to be no way to prevent it completely.
6034         */
6035        if (nfs41_setup_sequence(session, &lgp->args.seq_args,
6036                                &lgp->res.seq_res, task))
6037                return;
6038        if (pnfs_choose_layoutget_stateid(&lgp->args.stateid,
6039                                          NFS_I(lgp->args.inode)->layout,
6040                                          lgp->args.ctx->state)) {
6041                rpc_exit(task, NFS4_OK);
6042        }
6043}
6044
6045static void nfs4_layoutget_done(struct rpc_task *task, void *calldata)
6046{
6047        struct nfs4_layoutget *lgp = calldata;
6048        struct inode *inode = lgp->args.inode;
6049        struct nfs_server *server = NFS_SERVER(inode);
6050        struct pnfs_layout_hdr *lo;
6051        struct nfs4_state *state = NULL;
6052        unsigned long timeo, giveup;
6053
6054        dprintk("--> %s\n", __func__);
6055
6056        if (!nfs41_sequence_done(task, &lgp->res.seq_res))
6057                goto out;
6058
6059        switch (task->tk_status) {
6060        case 0:
6061                goto out;
6062        case -NFS4ERR_LAYOUTTRYLATER:
6063        case -NFS4ERR_RECALLCONFLICT:
6064                timeo = rpc_get_timeout(task->tk_client);
6065                giveup = lgp->args.timestamp + timeo;
6066                if (time_after(giveup, jiffies))
6067                        task->tk_status = -NFS4ERR_DELAY;
6068                break;
6069        case -NFS4ERR_EXPIRED:
6070        case -NFS4ERR_BAD_STATEID:
6071                spin_lock(&inode->i_lock);
6072                lo = NFS_I(inode)->layout;
6073                if (!lo || list_empty(&lo->plh_segs)) {
6074                        spin_unlock(&inode->i_lock);
6075                        /* If the open stateid was bad, then recover it. */
6076                        state = lgp->args.ctx->state;
6077                } else {
6078                        LIST_HEAD(head);
6079
6080                        pnfs_mark_matching_lsegs_invalid(lo, &head, NULL);
6081                        spin_unlock(&inode->i_lock);
6082                        /* Mark the bad layout state as invalid, then
6083                         * retry using the open stateid. */
6084                        pnfs_free_lseg_list(&head);
6085                }
6086        }
6087        if (nfs4_async_handle_error(task, server, state) == -EAGAIN)
6088                rpc_restart_call_prepare(task);
6089out:
6090        dprintk("<-- %s\n", __func__);
6091}
6092
6093static size_t max_response_pages(struct nfs_server *server)
6094{
6095        u32 max_resp_sz = server->nfs_client->cl_session->fc_attrs.max_resp_sz;
6096        return nfs_page_array_len(0, max_resp_sz);
6097}
6098
6099static void nfs4_free_pages(struct page **pages, size_t size)
6100{
6101        int i;
6102
6103        if (!pages)
6104                return;
6105
6106        for (i = 0; i < size; i++) {
6107                if (!pages[i])
6108                        break;
6109                __free_page(pages[i]);
6110        }
6111        kfree(pages);
6112}
6113
6114static struct page **nfs4_alloc_pages(size_t size, gfp_t gfp_flags)
6115{
6116        struct page **pages;
6117        int i;
6118
6119        pages = kcalloc(size, sizeof(struct page *), gfp_flags);
6120        if (!pages) {
6121                dprintk("%s: can't alloc array of %zu pages\n", __func__, size);
6122                return NULL;
6123        }
6124
6125        for (i = 0; i < size; i++) {
6126                pages[i] = alloc_page(gfp_flags);
6127                if (!pages[i]) {
6128                        dprintk("%s: failed to allocate page\n", __func__);
6129                        nfs4_free_pages(pages, size);
6130                        return NULL;
6131                }
6132        }
6133
6134        return pages;
6135}
6136
6137static void nfs4_layoutget_release(void *calldata)
6138{
6139        struct nfs4_layoutget *lgp = calldata;
6140        struct inode *inode = lgp->args.inode;
6141        struct nfs_server *server = NFS_SERVER(inode);
6142        size_t max_pages = max_response_pages(server);
6143
6144        dprintk("--> %s\n", __func__);
6145        nfs4_free_pages(lgp->args.layout.pages, max_pages);
6146        pnfs_put_layout_hdr(NFS_I(inode)->layout);
6147        put_nfs_open_context(lgp->args.ctx);
6148        kfree(calldata);
6149        dprintk("<-- %s\n", __func__);
6150}
6151
6152static const struct rpc_call_ops nfs4_layoutget_call_ops = {
6153        .rpc_call_prepare = nfs4_layoutget_prepare,
6154        .rpc_call_done = nfs4_layoutget_done,
6155        .rpc_release = nfs4_layoutget_release,
6156};
6157
6158struct pnfs_layout_segment *
6159nfs4_proc_layoutget(struct nfs4_layoutget *lgp, gfp_t gfp_flags)
6160{
6161        struct inode *inode = lgp->args.inode;
6162        struct nfs_server *server = NFS_SERVER(inode);
6163        size_t max_pages = max_response_pages(server);
6164        struct rpc_task *task;
6165        struct rpc_message msg = {
6166                .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_LAYOUTGET],
6167                .rpc_argp = &lgp->args,
6168                .rpc_resp = &lgp->res,
6169        };
6170        struct rpc_task_setup task_setup_data = {
6171                .rpc_client = server->client,
6172                .rpc_message = &msg,
6173                .callback_ops = &nfs4_layoutget_call_ops,
6174                .callback_data = lgp,
6175                .flags = RPC_TASK_ASYNC,
6176        };
6177        struct pnfs_layout_segment *lseg = NULL;
6178        int status = 0;
6179
6180        dprintk("--> %s\n", __func__);
6181
6182        lgp->args.layout.pages = nfs4_alloc_pages(max_pages, gfp_flags);
6183        if (!lgp->args.layout.pages) {
6184                nfs4_layoutget_release(lgp);
6185                return ERR_PTR(-ENOMEM);
6186        }
6187        lgp->args.layout.pglen = max_pages * PAGE_SIZE;
6188        lgp->args.timestamp = jiffies;
6189
6190        lgp->res.layoutp = &lgp->args.layout;
6191        lgp->res.seq_res.sr_slot = NULL;
6192        nfs41_init_sequence(&lgp->args.seq_args, &lgp->res.seq_res, 0);
6193
6194        /* nfs4_layoutget_release calls pnfs_put_layout_hdr */
6195        pnfs_get_layout_hdr(NFS_I(inode)->layout);
6196
6197        task = rpc_run_task(&task_setup_data);
6198        if (IS_ERR(task))
6199                return ERR_CAST(task);
6200        status = nfs4_wait_for_completion_rpc_task(task);
6201        if (status == 0)
6202                status = task->tk_status;
6203        /* if layoutp->len is 0, nfs4_layoutget_prepare called rpc_exit */
6204        if (status == 0 && lgp->res.layoutp->len)
6205                lseg = pnfs_layout_process(lgp);
6206        rpc_put_task(task);
6207        dprintk("<-- %s status=%d\n", __func__, status);
6208        if (status)
6209                return ERR_PTR(status);
6210        return lseg;
6211}
6212
6213static void
6214nfs4_layoutreturn_prepare(struct rpc_task *task, void *calldata)
6215{
6216        struct nfs4_layoutreturn *lrp = calldata;
6217
6218        dprintk("--> %s\n", __func__);
6219        nfs41_setup_sequence(lrp->clp->cl_session,
6220                        &lrp->args.seq_args,
6221                        &lrp->res.seq_res,
6222                        task);
6223}
6224
6225static void nfs4_layoutreturn_done(struct rpc_task *task, void *calldata)
6226{
6227        struct nfs4_layoutreturn *lrp = calldata;
6228        struct nfs_server *server;
6229
6230        dprintk("--> %s\n", __func__);
6231
6232        if (!nfs41_sequence_done(task, &lrp->res.seq_res))
6233                return;
6234
6235        server = NFS_SERVER(lrp->args.inode);
6236        if (nfs4_async_handle_error(task, server, NULL) == -EAGAIN) {
6237                rpc_restart_call_prepare(task);
6238                return;
6239        }
6240        dprintk("<-- %s\n", __func__);
6241}
6242
6243static void nfs4_layoutreturn_release(void *calldata)
6244{
6245        struct nfs4_layoutreturn *lrp = calldata;
6246        struct pnfs_layout_hdr *lo = lrp->args.layout;
6247
6248        dprintk("--> %s\n", __func__);
6249        spin_lock(&lo->plh_inode->i_lock);
6250        if (lrp->res.lrs_present)
6251                pnfs_set_layout_stateid(lo, &lrp->res.stateid, true);
6252        lo->plh_block_lgets--;
6253        spin_unlock(&lo->plh_inode->i_lock);
6254        pnfs_put_layout_hdr(lrp->args.layout);
6255        kfree(calldata);
6256        dprintk("<-- %s\n", __func__);
6257}
6258
6259static const struct rpc_call_ops nfs4_layoutreturn_call_ops = {
6260        .rpc_call_prepare = nfs4_layoutreturn_prepare,
6261        .rpc_call_done = nfs4_layoutreturn_done,
6262        .rpc_release = nfs4_layoutreturn_release,
6263};
6264
6265int nfs4_proc_layoutreturn(struct nfs4_layoutreturn *lrp)
6266{
6267        struct rpc_task *task;
6268        struct rpc_message msg = {
6269                .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_LAYOUTRETURN],
6270                .rpc_argp = &lrp->args,
6271                .rpc_resp = &lrp->res,
6272        };
6273        struct rpc_task_setup task_setup_data = {
6274                .rpc_client = lrp->clp->cl_rpcclient,
6275                .rpc_message = &msg,
6276                .callback_ops = &nfs4_layoutreturn_call_ops,
6277                .callback_data = lrp,
6278        };
6279        int status;
6280
6281        dprintk("--> %s\n", __func__);
6282        nfs41_init_sequence(&lrp->args.seq_args, &lrp->res.seq_res, 1);
6283        task = rpc_run_task(&task_setup_data);
6284        if (IS_ERR(task))
6285                return PTR_ERR(task);
6286        status = task->tk_status;
6287        dprintk("<-- %s status=%d\n", __func__, status);
6288        rpc_put_task(task);
6289        return status;
6290}
6291
6292/*
6293 * Retrieve the list of Data Server devices from the MDS.
6294 */
6295static int _nfs4_getdevicelist(struct nfs_server *server,
6296                                    const struct nfs_fh *fh,
6297                                    struct pnfs_devicelist *devlist)
6298{
6299        struct nfs4_getdevicelist_args args = {
6300                .fh = fh,
6301                .layoutclass = server->pnfs_curr_ld->id,
6302        };
6303        struct nfs4_getdevicelist_res res = {
6304                .devlist = devlist,
6305        };
6306        struct rpc_message msg = {
6307                .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_GETDEVICELIST],
6308                .rpc_argp = &args,
6309                .rpc_resp = &res,
6310        };
6311        int status;
6312
6313        dprintk("--> %s\n", __func__);
6314        status = nfs4_call_sync(server->client, server, &msg, &args.seq_args,
6315                                &res.seq_res, 0);
6316        dprintk("<-- %s status=%d\n", __func__, status);
6317        return status;
6318}
6319
6320int nfs4_proc_getdevicelist(struct nfs_server *server,
6321                            const struct nfs_fh *fh,
6322                            struct pnfs_devicelist *devlist)
6323{
6324        struct nfs4_exception exception = { };
6325        int err;
6326
6327        do {
6328                err = nfs4_handle_exception(server,
6329                                _nfs4_getdevicelist(server, fh, devlist),
6330                                &exception);
6331        } while (exception.retry);
6332
6333        dprintk("%s: err=%d, num_devs=%u\n", __func__,
6334                err, devlist->num_devs);
6335
6336        return err;
6337}
6338EXPORT_SYMBOL_GPL(nfs4_proc_getdevicelist);
6339
6340static int
6341_nfs4_proc_getdeviceinfo(struct nfs_server *server, struct pnfs_device *pdev)
6342{
6343        struct nfs4_getdeviceinfo_args args = {
6344                .pdev = pdev,
6345        };
6346        struct nfs4_getdeviceinfo_res res = {
6347                .pdev = pdev,
6348        };
6349        struct rpc_message msg = {
6350                .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_GETDEVICEINFO],
6351                .rpc_argp = &args,
6352                .rpc_resp = &res,
6353        };
6354        int status;
6355
6356        dprintk("--> %s\n", __func__);
6357        status = nfs4_call_sync(server->client, server, &msg, &args.seq_args, &res.seq_res, 0);
6358        dprintk("<-- %s status=%d\n", __func__, status);
6359
6360        return status;
6361}
6362
6363int nfs4_proc_getdeviceinfo(struct nfs_server *server, struct pnfs_device *pdev)
6364{
6365        struct nfs4_exception exception = { };
6366        int err;
6367
6368        do {
6369                err = nfs4_handle_exception(server,
6370                                        _nfs4_proc_getdeviceinfo(server, pdev),
6371                                        &exception);
6372        } while (exception.retry);
6373        return err;
6374}
6375EXPORT_SYMBOL_GPL(nfs4_proc_getdeviceinfo);
6376
6377static void nfs4_layoutcommit_prepare(struct rpc_task *task, void *calldata)
6378{
6379        struct nfs4_layoutcommit_data *data = calldata;
6380        struct nfs_server *server = NFS_SERVER(data->args.inode);
6381        struct nfs4_session *session = nfs4_get_session(server);
6382
6383        nfs41_setup_sequence(session,
6384                        &data->args.seq_args,
6385                        &data->res.seq_res,
6386                        task);
6387}
6388
6389static void
6390nfs4_layoutcommit_done(struct rpc_task *task, void *calldata)
6391{
6392        struct nfs4_layoutcommit_data *data = calldata;
6393        struct nfs_server *server = NFS_SERVER(data->args.inode);
6394
6395        if (!nfs41_sequence_done(task, &data->res.seq_res))
6396                return;
6397
6398        switch (task->tk_status) { /* Just ignore these failures */
6399        case -NFS4ERR_DELEG_REVOKED: /* layout was recalled */
6400        case -NFS4ERR_BADIOMODE:     /* no IOMODE_RW layout for range */
6401        case -NFS4ERR_BADLAYOUT:     /* no layout */
6402        case -NFS4ERR_GRACE:        /* loca_recalim always false */
6403                task->tk_status = 0;
6404                break;
6405        case 0:
6406                nfs_post_op_update_inode_force_wcc(data->args.inode,
6407                                                   data->res.fattr);
6408                break;
6409        default:
6410                if (nfs4_async_handle_error(task, server, NULL) == -EAGAIN) {
6411                        rpc_restart_call_prepare(task);
6412                        return;
6413                }
6414        }
6415}
6416
6417static void nfs4_layoutcommit_release(void *calldata)
6418{
6419        struct nfs4_layoutcommit_data *data = calldata;
6420
6421        pnfs_cleanup_layoutcommit(data);
6422        put_rpccred(data->cred);
6423        kfree(data);
6424}
6425
6426static const struct rpc_call_ops nfs4_layoutcommit_ops = {
6427        .rpc_call_prepare = nfs4_layoutcommit_prepare,
6428        .rpc_call_done = nfs4_layoutcommit_done,
6429        .rpc_release = nfs4_layoutcommit_release,
6430};
6431
6432int
6433nfs4_proc_layoutcommit(struct nfs4_layoutcommit_data *data, bool sync)
6434{
6435        struct rpc_message msg = {
6436                .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_LAYOUTCOMMIT],
6437                .rpc_argp = &data->args,
6438                .rpc_resp = &data->res,
6439                .rpc_cred = data->cred,
6440        };
6441        struct rpc_task_setup task_setup_data = {
6442                .task = &data->task,
6443                .rpc_client = NFS_CLIENT(data->args.inode),
6444                .rpc_message = &msg,
6445                .callback_ops = &nfs4_layoutcommit_ops,
6446                .callback_data = data,
6447                .flags = RPC_TASK_ASYNC,
6448        };
6449        struct rpc_task *task;
6450        int status = 0;
6451
6452        dprintk("NFS: %4d initiating layoutcommit call. sync %d "
6453                "lbw: %llu inode %lu\n",
6454                data->task.tk_pid, sync,
6455                data->args.lastbytewritten,
6456                data->args.inode->i_ino);
6457
6458        nfs41_init_sequence(&data->args.seq_args, &data->res.seq_res, 1);
6459        task = rpc_run_task(&task_setup_data);
6460        if (IS_ERR(task))
6461                return PTR_ERR(task);
6462        if (sync == false)
6463                goto out;
6464        status = nfs4_wait_for_completion_rpc_task(task);
6465        if (status != 0)
6466                goto out;
6467        status = task->tk_status;
6468out:
6469        dprintk("%s: status %d\n", __func__, status);
6470        rpc_put_task(task);
6471        return status;
6472}
6473
6474static int
6475_nfs41_proc_secinfo_no_name(struct nfs_server *server, struct nfs_fh *fhandle,
6476                    struct nfs_fsinfo *info, struct nfs4_secinfo_flavors *flavors)
6477{
6478        struct nfs41_secinfo_no_name_args args = {
6479                .style = SECINFO_STYLE_CURRENT_FH,
6480        };
6481        struct nfs4_secinfo_res res = {
6482                .flavors = flavors,
6483        };
6484        struct rpc_message msg = {
6485                .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_SECINFO_NO_NAME],
6486                .rpc_argp = &args,
6487                .rpc_resp = &res,
6488        };
6489        return nfs4_call_sync(server->client, server, &msg, &args.seq_args, &res.seq_res, 0);
6490}
6491
6492static int
6493nfs41_proc_secinfo_no_name(struct nfs_server *server, struct nfs_fh *fhandle,
6494                           struct nfs_fsinfo *info, struct nfs4_secinfo_flavors *flavors)
6495{
6496        struct nfs4_exception exception = { };
6497        int err;
6498        do {
6499                err = _nfs41_proc_secinfo_no_name(server, fhandle, info, flavors);
6500                switch (err) {
6501                case 0:
6502                case -NFS4ERR_WRONGSEC:
6503                case -NFS4ERR_NOTSUPP:
6504                        goto out;
6505                default:
6506                        err = nfs4_handle_exception(server, err, &exception);
6507                }
6508        } while (exception.retry);
6509out:
6510        return err;
6511}
6512
6513static int
6514nfs41_find_root_sec(struct nfs_server *server, struct nfs_fh *fhandle,
6515                    struct nfs_fsinfo *info)
6516{
6517        int err;
6518        struct page *page;
6519        rpc_authflavor_t flavor;
6520        struct nfs4_secinfo_flavors *flavors;
6521
6522        page = alloc_page(GFP_KERNEL);
6523        if (!page) {
6524                err = -ENOMEM;
6525                goto out;
6526        }
6527
6528        flavors = page_address(page);
6529        err = nfs41_proc_secinfo_no_name(server, fhandle, info, flavors);
6530
6531        /*
6532         * Fall back on "guess and check" method if
6533         * the server doesn't support SECINFO_NO_NAME
6534         */
6535        if (err == -NFS4ERR_WRONGSEC || err == -NFS4ERR_NOTSUPP) {
6536                err = nfs4_find_root_sec(server, fhandle, info);
6537                goto out_freepage;
6538        }
6539        if (err)
6540                goto out_freepage;
6541
6542        flavor = nfs_find_best_sec(flavors);
6543        if (err == 0)
6544                err = nfs4_lookup_root_sec(server, fhandle, info, flavor);
6545
6546out_freepage:
6547        put_page(page);
6548        if (err == -EACCES)
6549                return -EPERM;
6550out:
6551        return err;
6552}
6553
6554static int _nfs41_test_stateid(struct nfs_server *server, nfs4_stateid *stateid)
6555{
6556        int status;
6557        struct nfs41_test_stateid_args args = {
6558                .stateid = stateid,
6559        };
6560        struct nfs41_test_stateid_res res;
6561        struct rpc_message msg = {
6562                .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_TEST_STATEID],
6563                .rpc_argp = &args,
6564                .rpc_resp = &res,
6565        };
6566
6567        dprintk("NFS call  test_stateid %p\n", stateid);
6568        nfs41_init_sequence(&args.seq_args, &res.seq_res, 0);
6569        nfs4_set_sequence_privileged(&args.seq_args);
6570        status = nfs4_call_sync_sequence(server->client, server, &msg,
6571                        &args.seq_args, &res.seq_res);
6572        if (status != NFS_OK) {
6573                dprintk("NFS reply test_stateid: failed, %d\n", status);
6574                return status;
6575        }
6576        dprintk("NFS reply test_stateid: succeeded, %d\n", -res.status);
6577        return -res.status;
6578}
6579
6580/**
6581 * nfs41_test_stateid - perform a TEST_STATEID operation
6582 *
6583 * @server: server / transport on which to perform the operation
6584 * @stateid: state ID to test
6585 *
6586 * Returns NFS_OK if the server recognizes that "stateid" is valid.
6587 * Otherwise a negative NFS4ERR value is returned if the operation
6588 * failed or the state ID is not currently valid.
6589 */
6590static int nfs41_test_stateid(struct nfs_server *server, nfs4_stateid *stateid)
6591{
6592        struct nfs4_exception exception = { };
6593        int err;
6594        do {
6595                err = _nfs41_test_stateid(server, stateid);
6596                if (err != -NFS4ERR_DELAY)
6597                        break;
6598                nfs4_handle_exception(server, err, &exception);
6599        } while (exception.retry);
6600        return err;
6601}
6602
6603static int _nfs4_free_stateid(struct nfs_server *server, nfs4_stateid *stateid)
6604{
6605        struct nfs41_free_stateid_args args = {
6606                .stateid = stateid,
6607        };
6608        struct nfs41_free_stateid_res res;
6609        struct rpc_message msg = {
6610                .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_FREE_STATEID],
6611                .rpc_argp = &args,
6612                .rpc_resp = &res,
6613        };
6614        int status;
6615
6616        dprintk("NFS call  free_stateid %p\n", stateid);
6617        nfs41_init_sequence(&args.seq_args, &res.seq_res, 0);
6618        nfs4_set_sequence_privileged(&args.seq_args);
6619        status = nfs4_call_sync_sequence(server->client, server, &msg,
6620                        &args.seq_args, &res.seq_res);
6621        dprintk("NFS reply free_stateid: %d\n", status);
6622        return status;
6623}
6624
6625/**
6626 * nfs41_free_stateid - perform a FREE_STATEID operation
6627 *
6628 * @server: server / transport on which to perform the operation
6629 * @stateid: state ID to release
6630 *
6631 * Returns NFS_OK if the server freed "stateid".  Otherwise a
6632 * negative NFS4ERR value is returned.
6633 */
6634static int nfs41_free_stateid(struct nfs_server *server, nfs4_stateid *stateid)
6635{
6636        struct nfs4_exception exception = { };
6637        int err;
6638        do {
6639                err = _nfs4_free_stateid(server, stateid);
6640                if (err != -NFS4ERR_DELAY)
6641                        break;
6642                nfs4_handle_exception(server, err, &exception);
6643        } while (exception.retry);
6644        return err;
6645}
6646
6647static bool nfs41_match_stateid(const nfs4_stateid *s1,
6648                const nfs4_stateid *s2)
6649{
6650        if (memcmp(s1->other, s2->other, sizeof(s1->other)) != 0)
6651                return false;
6652
6653        if (s1->seqid == s2->seqid)
6654                return true;
6655        if (s1->seqid == 0 || s2->seqid == 0)
6656                return true;
6657
6658        return false;
6659}
6660
6661#endif /* CONFIG_NFS_V4_1 */
6662
6663static bool nfs4_match_stateid(const nfs4_stateid *s1,
6664                const nfs4_stateid *s2)
6665{
6666        return nfs4_stateid_match(s1, s2);
6667}
6668
6669
6670static const struct nfs4_state_recovery_ops nfs40_reboot_recovery_ops = {
6671        .owner_flag_bit = NFS_OWNER_RECLAIM_REBOOT,
6672        .state_flag_bit = NFS_STATE_RECLAIM_REBOOT,
6673        .recover_open   = nfs4_open_reclaim,
6674        .recover_lock   = nfs4_lock_reclaim,
6675        .establish_clid = nfs4_init_clientid,
6676        .get_clid_cred  = nfs4_get_setclientid_cred,
6677        .detect_trunking = nfs40_discover_server_trunking,
6678};
6679
6680#if defined(CONFIG_NFS_V4_1)
6681static const struct nfs4_state_recovery_ops nfs41_reboot_recovery_ops = {
6682        .owner_flag_bit = NFS_OWNER_RECLAIM_REBOOT,
6683        .state_flag_bit = NFS_STATE_RECLAIM_REBOOT,
6684        .recover_open   = nfs4_open_reclaim,
6685        .recover_lock   = nfs4_lock_reclaim,
6686        .establish_clid = nfs41_init_clientid,
6687        .get_clid_cred  = nfs4_get_exchange_id_cred,
6688        .reclaim_complete = nfs41_proc_reclaim_complete,
6689        .detect_trunking = nfs41_discover_server_trunking,
6690};
6691#endif /* CONFIG_NFS_V4_1 */
6692
6693static const struct nfs4_state_recovery_ops nfs40_nograce_recovery_ops = {
6694        .owner_flag_bit = NFS_OWNER_RECLAIM_NOGRACE,
6695        .state_flag_bit = NFS_STATE_RECLAIM_NOGRACE,
6696        .recover_open   = nfs4_open_expired,
6697        .recover_lock   = nfs4_lock_expired,
6698        .establish_clid = nfs4_init_clientid,
6699        .get_clid_cred  = nfs4_get_setclientid_cred,
6700};
6701
6702#if defined(CONFIG_NFS_V4_1)
6703static const struct nfs4_state_recovery_ops nfs41_nograce_recovery_ops = {
6704        .owner_flag_bit = NFS_OWNER_RECLAIM_NOGRACE,
6705        .state_flag_bit = NFS_STATE_RECLAIM_NOGRACE,
6706        .recover_open   = nfs41_open_expired,
6707        .recover_lock   = nfs41_lock_expired,
6708        .establish_clid = nfs41_init_clientid,
6709        .get_clid_cred  = nfs4_get_exchange_id_cred,
6710};
6711#endif /* CONFIG_NFS_V4_1 */
6712
6713static const struct nfs4_state_maintenance_ops nfs40_state_renewal_ops = {
6714        .sched_state_renewal = nfs4_proc_async_renew,
6715        .get_state_renewal_cred_locked = nfs4_get_renew_cred_locked,
6716        .renew_lease = nfs4_proc_renew,
6717};
6718
6719#if defined(CONFIG_NFS_V4_1)
6720static const struct nfs4_state_maintenance_ops nfs41_state_renewal_ops = {
6721        .sched_state_renewal = nfs41_proc_async_sequence,
6722        .get_state_renewal_cred_locked = nfs4_get_machine_cred_locked,
6723        .renew_lease = nfs4_proc_sequence,
6724};
6725#endif
6726
6727static const struct nfs4_minor_version_ops nfs_v4_0_minor_ops = {
6728        .minor_version = 0,
6729        .call_sync = _nfs4_call_sync,
6730        .match_stateid = nfs4_match_stateid,
6731        .find_root_sec = nfs4_find_root_sec,
6732        .reboot_recovery_ops = &nfs40_reboot_recovery_ops,
6733        .nograce_recovery_ops = &nfs40_nograce_recovery_ops,
6734        .state_renewal_ops = &nfs40_state_renewal_ops,
6735};
6736
6737#if defined(CONFIG_NFS_V4_1)
6738static const struct nfs4_minor_version_ops nfs_v4_1_minor_ops = {
6739        .minor_version = 1,
6740        .call_sync = nfs4_call_sync_sequence,
6741        .match_stateid = nfs41_match_stateid,
6742        .find_root_sec = nfs41_find_root_sec,
6743        .reboot_recovery_ops = &nfs41_reboot_recovery_ops,
6744        .nograce_recovery_ops = &nfs41_nograce_recovery_ops,
6745        .state_renewal_ops = &nfs41_state_renewal_ops,
6746};
6747#endif
6748
6749const struct nfs4_minor_version_ops *nfs_v4_minor_ops[] = {
6750        [0] = &nfs_v4_0_minor_ops,
6751#if defined(CONFIG_NFS_V4_1)
6752        [1] = &nfs_v4_1_minor_ops,
6753#endif
6754};
6755
6756const struct inode_operations nfs4_dir_inode_operations = {
6757        .create         = nfs_create,
6758        .lookup         = nfs_lookup,
6759        .atomic_open    = nfs_atomic_open,
6760        .link           = nfs_link,
6761        .unlink         = nfs_unlink,
6762        .symlink        = nfs_symlink,
6763        .mkdir          = nfs_mkdir,
6764        .rmdir          = nfs_rmdir,
6765        .mknod          = nfs_mknod,
6766        .rename         = nfs_rename,
6767        .permission     = nfs_permission,
6768        .getattr        = nfs_getattr,
6769        .setattr        = nfs_setattr,
6770        .getxattr       = generic_getxattr,
6771        .setxattr       = generic_setxattr,
6772        .listxattr      = generic_listxattr,
6773        .removexattr    = generic_removexattr,
6774};
6775
6776static const struct inode_operations nfs4_file_inode_operations = {
6777        .permission     = nfs_permission,
6778        .getattr        = nfs_getattr,
6779        .setattr        = nfs_setattr,
6780        .getxattr       = generic_getxattr,
6781        .setxattr       = generic_setxattr,
6782        .listxattr      = generic_listxattr,
6783        .removexattr    = generic_removexattr,
6784};
6785
6786const struct nfs_rpc_ops nfs_v4_clientops = {
6787        .version        = 4,                    /* protocol version */
6788        .dentry_ops     = &nfs4_dentry_operations,
6789        .dir_inode_ops  = &nfs4_dir_inode_operations,
6790        .file_inode_ops = &nfs4_file_inode_operations,
6791        .file_ops       = &nfs4_file_operations,
6792        .getroot        = nfs4_proc_get_root,
6793        .submount       = nfs4_submount,
6794        .try_mount      = nfs4_try_mount,
6795        .getattr        = nfs4_proc_getattr,
6796        .setattr        = nfs4_proc_setattr,
6797        .lookup         = nfs4_proc_lookup,
6798        .access         = nfs4_proc_access,
6799        .readlink       = nfs4_proc_readlink,
6800        .create         = nfs4_proc_create,
6801        .remove         = nfs4_proc_remove,
6802        .unlink_setup   = nfs4_proc_unlink_setup,
6803        .unlink_rpc_prepare = nfs4_proc_unlink_rpc_prepare,
6804        .unlink_done    = nfs4_proc_unlink_done,
6805        .rename         = nfs4_proc_rename,
6806        .rename_setup   = nfs4_proc_rename_setup,
6807        .rename_rpc_prepare = nfs4_proc_rename_rpc_prepare,
6808        .rename_done    = nfs4_proc_rename_done,
6809        .link           = nfs4_proc_link,
6810        .symlink        = nfs4_proc_symlink,
6811        .mkdir          = nfs4_proc_mkdir,
6812        .rmdir          = nfs4_proc_remove,
6813        .readdir        = nfs4_proc_readdir,
6814        .mknod          = nfs4_proc_mknod,
6815        .statfs         = nfs4_proc_statfs,
6816        .fsinfo         = nfs4_proc_fsinfo,
6817        .pathconf       = nfs4_proc_pathconf,
6818        .set_capabilities = nfs4_server_capabilities,
6819        .decode_dirent  = nfs4_decode_dirent,
6820        .read_setup     = nfs4_proc_read_setup,
6821        .read_pageio_init = pnfs_pageio_init_read,
6822        .read_rpc_prepare = nfs4_proc_read_rpc_prepare,
6823        .read_done      = nfs4_read_done,
6824        .write_setup    = nfs4_proc_write_setup,
6825        .write_pageio_init = pnfs_pageio_init_write,
6826        .write_rpc_prepare = nfs4_proc_write_rpc_prepare,
6827        .write_done     = nfs4_write_done,
6828        .commit_setup   = nfs4_proc_commit_setup,
6829        .commit_rpc_prepare = nfs4_proc_commit_rpc_prepare,
6830        .commit_done    = nfs4_commit_done,
6831        .lock           = nfs4_proc_lock,
6832        .clear_acl_cache = nfs4_zap_acl_attr,
6833        .close_context  = nfs4_close_context,
6834        .open_context   = nfs4_atomic_open,
6835        .have_delegation = nfs4_have_delegation,
6836        .return_delegation = nfs4_inode_return_delegation,
6837        .alloc_client   = nfs4_alloc_client,
6838        .init_client    = nfs4_init_client,
6839        .free_client    = nfs4_free_client,
6840        .create_server  = nfs4_create_server,
6841        .clone_server   = nfs_clone_server,
6842};
6843
6844static const struct xattr_handler nfs4_xattr_nfs4_acl_handler = {
6845        .prefix = XATTR_NAME_NFSV4_ACL,
6846        .list   = nfs4_xattr_list_nfs4_acl,
6847        .get    = nfs4_xattr_get_nfs4_acl,
6848        .set    = nfs4_xattr_set_nfs4_acl,
6849};
6850
6851const struct xattr_handler *nfs4_xattr_handlers[] = {
6852        &nfs4_xattr_nfs4_acl_handler,
6853        NULL
6854};
6855
6856/*
6857 * Local variables:
6858 *  c-basic-offset: 8
6859 * End:
6860 */
6861