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